blob: 2990146899b507234363788c66ceb85304a4e57b [file] [log] [blame]
Andrew Lenharthaa38ce42005-09-02 18:46:02 +00001//===-- AlphaISelLowering.cpp - Alpha DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Andrew Lenharth and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AlphaISelLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AlphaISelLowering.h"
15#include "AlphaTargetMachine.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/SelectionDAG.h"
20#include "llvm/CodeGen/SSARegMap.h"
21#include "llvm/Constants.h"
22#include "llvm/Function.h"
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000023#include "llvm/Module.h"
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000024#include "llvm/Support/CommandLine.h"
25#include <iostream>
26
27using namespace llvm;
28
29namespace llvm {
Andrew Lenharthfabd5ba2006-01-23 21:56:07 +000030 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
31 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
32 cl::Hidden);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000033}
34
35/// AddLiveIn - This helper function adds the specified physical register to the
36/// MachineFunction as a live in value. It also creates a corresponding virtual
37/// register for it.
38static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
39 TargetRegisterClass *RC) {
40 assert(RC->contains(PReg) && "Not the correct regclass!");
41 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
42 MF.addLiveIn(PReg, VReg);
43 return VReg;
44}
45
46AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
47 // Set up the TargetLowering object.
48 //I am having problems with shr n ubyte 1
49 setShiftAmountType(MVT::i64);
50 setSetCCResultType(MVT::i64);
51 setSetCCResultContents(ZeroOrOneSetCCResult);
52
53 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000054 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
55 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000056
57 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
58 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
59
60 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
61 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
62
63 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
64 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
65
66 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
67 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
68 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
69
Andrew Lenharthf3fb71b2005-10-06 16:54:29 +000070 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
71
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000072 if (EnableAlphaLSMark) {
73 setOperationAction(ISD::LOAD, MVT::i64, Custom);
74 setOperationAction(ISD::LOAD, MVT::f64, Custom);
75 setOperationAction(ISD::LOAD, MVT::f32, Custom);
Andrew Lenharth87076052006-01-23 21:23:26 +000076
77 setOperationAction(ISD::ZEXTLOAD, MVT::i8, Custom);
78 setOperationAction(ISD::ZEXTLOAD, MVT::i16, Custom);
79 setOperationAction(ISD::SEXTLOAD, MVT::i32, Custom);
80
81 setOperationAction(ISD::EXTLOAD, MVT::i8, Custom);
82 setOperationAction(ISD::EXTLOAD, MVT::i16, Custom);
83 setOperationAction(ISD::EXTLOAD, MVT::i32, Custom);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000084 }
85
Chris Lattner3e2bafd2005-09-28 22:29:17 +000086 setOperationAction(ISD::FREM, MVT::f32, Expand);
87 setOperationAction(ISD::FREM, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000088
89 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth7f0db912005-11-30 07:19:56 +000090 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Andrew Lenharthcd804962005-11-30 16:10:29 +000091 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
92 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
93
Andrew Lenharth120ab482005-09-29 22:54:56 +000094 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000095 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
96 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
97 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
98 }
Nate Begemand88fc032006-01-14 03:14:10 +000099 setOperationAction(ISD::BSWAP , MVT::i64, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000100 setOperationAction(ISD::ROTL , MVT::i64, Expand);
101 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000102
Andrew Lenharth53d89702005-12-25 01:34:27 +0000103 setOperationAction(ISD::SREM , MVT::i64, Custom);
104 setOperationAction(ISD::UREM , MVT::i64, Custom);
105 setOperationAction(ISD::SDIV , MVT::i64, Custom);
106 setOperationAction(ISD::UDIV , MVT::i64, Custom);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000107
108 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
109 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
110 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
111
112 // We don't support sin/cos/sqrt
113 setOperationAction(ISD::FSIN , MVT::f64, Expand);
114 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000115 setOperationAction(ISD::FSIN , MVT::f32, Expand);
116 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Andrew Lenharth39424472006-01-19 21:10:38 +0000117
118 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000119 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
120
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000121 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000122
123 // We don't have line number support yet.
124 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000125 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
126 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000127
128 // Not implemented yet.
129 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
130 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharth739027e2006-01-16 21:22:38 +0000131 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
132
Andrew Lenharth53d89702005-12-25 01:34:27 +0000133 // We want to legalize GlobalAddress and ConstantPool and
134 // ExternalSymbols nodes into the appropriate instructions to
135 // materialize the address.
136 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
137 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
138 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000139
Andrew Lenharth0e538792006-01-25 21:54:38 +0000140 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Andrew Lenharth677c4f22006-01-25 23:33:32 +0000141 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000142 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
Andrew Lenharth5f8f0e22006-01-25 22:28:07 +0000143 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Nate Begeman0aed7842006-01-28 03:14:31 +0000144 setOperationAction(ISD::VAARG, MVT::i32, Custom);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000145
Andrew Lenharth739027e2006-01-16 21:22:38 +0000146 setStackPointerRegisterToSaveRestore(Alpha::R30);
147
Chris Lattner08a90222006-01-29 06:25:22 +0000148 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
149 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000150 addLegalFPImmediate(+0.0); //F31
151 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000152
153 computeRegisterProperties();
154
155 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000156}
157
Andrew Lenharth84a06052006-01-16 19:53:25 +0000158const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
159 switch (Opcode) {
160 default: return 0;
161 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
162 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
163 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
164 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
165 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
166 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
167 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
168 case AlphaISD::RelLit: return "Alpha::RelLit";
169 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
Chris Lattner2d90bd52006-01-27 23:39:00 +0000170 case AlphaISD::CALL: return "Alpha::CALL";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000171 case AlphaISD::DivCall: return "Alpha::DivCall";
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000172 case AlphaISD::LDQ_: return "Alpha::LDQ_";
173 case AlphaISD::LDT_: return "Alpha::LDT_";
174 case AlphaISD::LDS_: return "Alpha::LDS_";
175 case AlphaISD::LDL_: return "Alpha::LDL_";
176 case AlphaISD::LDWU_: return "Alpha::LDWU_";
177 case AlphaISD::LDBU_: return "Alpha::LDBU_";
Andrew Lenharth66e49582006-01-23 21:51:33 +0000178 case AlphaISD::STQ_: return "Alpha::STQ_";
179 case AlphaISD::STT_: return "Alpha::STT_";
180 case AlphaISD::STS_: return "Alpha::STS_";
181 case AlphaISD::STL_: return "Alpha::STL_";
182 case AlphaISD::STW_: return "Alpha::STW_";
183 case AlphaISD::STB_: return "Alpha::STB_";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000184 }
185}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000186
187//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
188
189//For now, just use variable size stack frame format
190
191//In a standard call, the first six items are passed in registers $16
192//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
193//of argument-to-register correspondence.) The remaining items are
194//collected in a memory argument list that is a naturally aligned
195//array of quadwords. In a standard call, this list, if present, must
196//be passed at 0(SP).
197//7 ... n 0(SP) ... (n-7)*8(SP)
198
199// //#define FP $15
200// //#define RA $26
201// //#define PV $27
202// //#define GP $29
203// //#define SP $30
204
205std::vector<SDOperand>
206AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
207{
208 MachineFunction &MF = DAG.getMachineFunction();
209 MachineFrameInfo *MFI = MF.getFrameInfo();
210 MachineBasicBlock& BB = MF.front();
211 std::vector<SDOperand> ArgValues;
212
Andrew Lenharthf71df332005-09-04 06:12:19 +0000213 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000214 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000215 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000216 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000217
218 int count = 0;
219
220 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
221 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
222
223 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
224 {
225 SDOperand argt;
226 if (count < 6) {
227 unsigned Vreg;
228 MVT::ValueType VT = getValueType(I->getType());
229 switch (VT) {
230 default:
231 std::cerr << "Unknown Type " << VT << "\n";
232 abort();
233 case MVT::f64:
234 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000235 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000236 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
237 DAG.setRoot(argt.getValue(1));
238 break;
239 case MVT::i1:
240 case MVT::i8:
241 case MVT::i16:
242 case MVT::i32:
243 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000244 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000245 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
246 DAG.setRoot(argt.getValue(1));
247 if (VT != MVT::i64) {
248 unsigned AssertOp =
249 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
250 argt = DAG.getNode(AssertOp, MVT::i64, argt,
251 DAG.getValueType(VT));
252 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
253 }
254 break;
255 }
256 } else { //more args
257 // Create the frame index object for this incoming parameter...
258 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
259
260 // Create the SelectionDAG nodes corresponding to a load
261 //from this parameter
262 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
263 argt = DAG.getLoad(getValueType(I->getType()),
264 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
265 }
266 ++count;
267 ArgValues.push_back(argt);
268 }
269
270 // If the functions takes variable number of arguments, copy all regs to stack
271 if (F.isVarArg()) {
272 VarArgsOffset = count * 8;
273 std::vector<SDOperand> LS;
274 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000275 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000276 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000277 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
278 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
279 if (i == 0) VarArgsBase = FI;
280 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
281 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
282 SDFI, DAG.getSrcValue(NULL)));
283
Chris Lattnerf2cded72005-09-13 19:03:13 +0000284 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000285 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000286 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
287 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
288 SDFI = DAG.getFrameIndex(FI, MVT::i64);
289 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
290 SDFI, DAG.getSrcValue(NULL)));
291 }
292
293 //Set up a token factor with all the stack traffic
294 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
295 }
296
297 // Finally, inform the code generator which regs we return values in.
298 switch (getValueType(F.getReturnType())) {
299 default: assert(0 && "Unknown type!");
300 case MVT::isVoid: break;
301 case MVT::i1:
302 case MVT::i8:
303 case MVT::i16:
304 case MVT::i32:
305 case MVT::i64:
306 MF.addLiveOut(Alpha::R0);
307 break;
308 case MVT::f32:
309 case MVT::f64:
310 MF.addLiveOut(Alpha::F0);
311 break;
312 }
313
314 //return the arguments
315 return ArgValues;
316}
317
318std::pair<SDOperand, SDOperand>
319AlphaTargetLowering::LowerCallTo(SDOperand Chain,
320 const Type *RetTy, bool isVarArg,
321 unsigned CallingConv, bool isTailCall,
322 SDOperand Callee, ArgListTy &Args,
323 SelectionDAG &DAG) {
324 int NumBytes = 0;
325 if (Args.size() > 6)
326 NumBytes = (Args.size() - 6) * 8;
327
328 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
329 DAG.getConstant(NumBytes, getPointerTy()));
330 std::vector<SDOperand> args_to_use;
331 for (unsigned i = 0, e = Args.size(); i != e; ++i)
332 {
333 switch (getValueType(Args[i].second)) {
334 default: assert(0 && "Unexpected ValueType for argument!");
335 case MVT::i1:
336 case MVT::i8:
337 case MVT::i16:
338 case MVT::i32:
339 // Promote the integer to 64 bits. If the input type is signed use a
340 // sign extend, otherwise use a zero extend.
341 if (Args[i].second->isSigned())
342 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
343 else
344 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
345 break;
346 case MVT::i64:
347 case MVT::f64:
348 case MVT::f32:
349 break;
350 }
351 args_to_use.push_back(Args[i].first);
352 }
353
354 std::vector<MVT::ValueType> RetVals;
355 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000356 MVT::ValueType ActualRetTyVT = RetTyVT;
357 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
358 ActualRetTyVT = MVT::i64;
359
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000360 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000361 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000362 RetVals.push_back(MVT::Other);
363
Chris Lattner2d90bd52006-01-27 23:39:00 +0000364 std::vector<SDOperand> Ops;
365 Ops.push_back(Chain);
366 Ops.push_back(Callee);
367 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
368 SDOperand TheCall = DAG.getNode(AlphaISD::CALL, RetVals, Ops);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000369 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
370 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
371 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000372 SDOperand RetVal = TheCall;
373
374 if (RetTyVT != ActualRetTyVT) {
375 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
376 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
377 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
378 }
379
380 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000381}
382
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000383void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
384{
385 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
386}
387void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
388{
389 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
390}
391
392
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000393
394static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
395{
396 fun = type = offset = 0;
397 if (v == NULL) {
398 type = 0;
399 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
400 type = 1;
401 const Module* M = GV->getParent();
402 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
403 ++offset;
404 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
405 type = 2;
406 const Function* F = Arg->getParent();
407 const Module* M = F->getParent();
408 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
409 ++fun;
410 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
411 ++offset;
412 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
413 assert(dyn_cast<PointerType>(I->getType()));
414 type = 3;
415 const BasicBlock* bb = I->getParent();
416 const Function* F = bb->getParent();
417 const Module* M = F->getParent();
418 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
419 ++fun;
420 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
421 offset += ii->size();
422 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
423 ++offset;
424 } else if (const Constant* C = dyn_cast<Constant>(v)) {
425 //Don't know how to look these up yet
426 type = 0;
427 } else {
428 assert(0 && "Error in value marking");
429 }
430 //type = 4: register spilling
431 //type = 5: global address loading or constant loading
432}
433
434static int getUID()
435{
436 static int id = 0;
437 return ++id;
438}
439
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000440/// LowerOperation - Provide custom lowering hooks for some operations.
441///
442SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
443 switch (Op.getOpcode()) {
444 default: assert(0 && "Wasn't expecting to be able to lower this!");
445 case ISD::SINT_TO_FP: {
446 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
447 "Unhandled SINT_TO_FP type in custom expander!");
448 SDOperand LD;
449 bool isDouble = MVT::f64 == Op.getValueType();
450 if (useITOF) {
451 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
452 } else {
453 int FrameIdx =
454 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
455 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
456 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
457 Op.getOperand(0), FI, DAG.getSrcValue(0));
458 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
459 }
460 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
461 isDouble?MVT::f64:MVT::f32, LD);
462 return FP;
463 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000464 case ISD::FP_TO_SINT: {
465 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
466 SDOperand src = Op.getOperand(0);
467
468 if (!isDouble) //Promote
469 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
470
471 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
472
473 if (useITOF) {
474 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
475 } else {
476 int FrameIdx =
477 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
478 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
479 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
480 src, FI, DAG.getSrcValue(0));
481 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
482 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000483 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000484 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000485 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
486 Constant *C = CP->get();
487 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
Andrew Lenharth4e629512005-12-24 05:36:33 +0000488
489 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
490 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
491 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
492 return Lo;
493 }
494 case ISD::GlobalAddress: {
495 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
496 GlobalValue *GV = GSDN->getGlobal();
497 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
498
499 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
500 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
501 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
502 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
503 return Lo;
504 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000505 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000506 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000507 case ISD::ExternalSymbol: {
508 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
509 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
510 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
511 }
512
513 case ISD::SDIV:
514 case ISD::UDIV:
515 case ISD::UREM:
516 case ISD::SREM:
517 if (MVT::isInteger(Op.getValueType())) {
518 const char* opstr = 0;
519 switch(Op.getOpcode()) {
520 case ISD::UREM: opstr = "__remqu"; break;
521 case ISD::SREM: opstr = "__remq"; break;
522 case ISD::UDIV: opstr = "__divqu"; break;
523 case ISD::SDIV: opstr = "__divq"; break;
524 }
525 SDOperand Tmp1 = Op.getOperand(0),
526 Tmp2 = Op.getOperand(1),
527 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
528 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
529 }
530 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000531
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000532 case ISD::LOAD:
533 case ISD::SEXTLOAD:
534 case ISD::ZEXTLOAD:
Andrew Lenharth87076052006-01-23 21:23:26 +0000535 case ISD::EXTLOAD:
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000536 {
537 SDOperand Chain = Op.getOperand(0);
538 SDOperand Address = Op.getOperand(1);
539
540 unsigned Opc;
541 unsigned opcode = Op.getOpcode();
542
543 if (opcode == ISD::LOAD)
544 switch (Op.Val->getValueType(0)) {
545 default: Op.Val->dump(); assert(0 && "Bad load!");
546 case MVT::i64: Opc = AlphaISD::LDQ_; break;
547 case MVT::f64: Opc = AlphaISD::LDT_; break;
548 case MVT::f32: Opc = AlphaISD::LDS_; break;
549 }
550 else
551 switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
552 default: Op.Val->dump(); assert(0 && "Bad sign extend!");
553 case MVT::i32: Opc = AlphaISD::LDL_;
554 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
555 case MVT::i16: Opc = AlphaISD::LDWU_;
556 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
557 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
558 case MVT::i8: Opc = AlphaISD::LDBU_;
559 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
560 }
561
562 int i, j, k;
563 getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
564
565 SDOperand Zero = DAG.getConstant(0, MVT::i64);
566 std::vector<MVT::ValueType> VTS;
567 VTS.push_back(Op.Val->getValueType(0));
568 VTS.push_back(MVT::Other);
569 std::vector<SDOperand> ARGS;
Andrew Lenharth87076052006-01-23 21:23:26 +0000570 ARGS.push_back(Chain);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000571 ARGS.push_back(Zero);
572 ARGS.push_back(Address);
573 ARGS.push_back(DAG.getConstant(i, MVT::i64));
574 ARGS.push_back(DAG.getConstant(j, MVT::i64));
575 ARGS.push_back(DAG.getConstant(k, MVT::i64));
576 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000577 return DAG.getNode(Opc, VTS, ARGS);
578 }
579
Andrew Lenharth66e49582006-01-23 21:51:33 +0000580 case ISD::TRUNCSTORE:
581 case ISD::STORE:
582 {
583 SDOperand Chain = Op.getOperand(0);
584 SDOperand Value = Op.getOperand(1);
585 SDOperand Address = Op.getOperand(2);
586
587 unsigned Opc;
588 unsigned opcode = Op.getOpcode();
589
590 if (opcode == ISD::STORE) {
591 switch(Value.getValueType()) {
592 default: assert(0 && "unknown Type in store");
593 case MVT::i64: Opc = AlphaISD::STQ_; break;
594 case MVT::f64: Opc = AlphaISD::STT_; break;
595 case MVT::f32: Opc = AlphaISD::STS_; break;
596 }
597 } else { //ISD::TRUNCSTORE
598 switch(cast<VTSDNode>(Op.getOperand(4))->getVT()) {
599 default: assert(0 && "unknown Type in store");
600 case MVT::i8: Opc = AlphaISD::STB_; break;
601 case MVT::i16: Opc = AlphaISD::STW_; break;
602 case MVT::i32: Opc = AlphaISD::STL_; break;
603 }
604 }
605
606 int i, j, k;
607 getValueInfo(cast<SrcValueSDNode>(Op.getOperand(3))->getValue(), i, j, k);
608
609 SDOperand Zero = DAG.getConstant(0, MVT::i64);
610 std::vector<MVT::ValueType> VTS;
611 VTS.push_back(MVT::Other);
612 std::vector<SDOperand> ARGS;
613 ARGS.push_back(Chain);
614 ARGS.push_back(Value);
615 ARGS.push_back(Zero);
616 ARGS.push_back(Address);
617 ARGS.push_back(DAG.getConstant(i, MVT::i64));
618 ARGS.push_back(DAG.getConstant(j, MVT::i64));
619 ARGS.push_back(DAG.getConstant(k, MVT::i64));
620 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
621 return DAG.getNode(Opc, VTS, ARGS);
622 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000623 case ISD::VAARG: {
624 SDOperand Chain = Op.getOperand(0);
625 SDOperand VAListP = Op.getOperand(1);
626 SDOperand VAListS = Op.getOperand(2);
627
628 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP, VAListS);
629 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
630 DAG.getConstant(8, MVT::i64));
631 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
632 Tmp, DAG.getSrcValue(0), MVT::i32);
633 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
634 if (MVT::isFloatingPoint(Op.getValueType()))
635 {
636 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
637 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
638 DAG.getConstant(8*6, MVT::i64));
639 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
640 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
641 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
642 }
Andrew Lenharth66e49582006-01-23 21:51:33 +0000643
Nate Begemanacc398c2006-01-25 18:21:52 +0000644 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
645 DAG.getConstant(8, MVT::i64));
646 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
647 Offset.getValue(1), NewOffset,
648 Tmp, DAG.getSrcValue(0),
649 DAG.getValueType(MVT::i32));
650
651 SDOperand Result;
652 if (Op.getValueType() == MVT::i32)
653 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Update, DataPtr,
654 DAG.getSrcValue(0), MVT::i32);
655 else
656 Result = DAG.getLoad(Op.getValueType(), Update, DataPtr,
657 DAG.getSrcValue(0));
658 return Result;
659 }
660 case ISD::VACOPY: {
661 SDOperand Chain = Op.getOperand(0);
662 SDOperand DestP = Op.getOperand(1);
663 SDOperand SrcP = Op.getOperand(2);
664 SDOperand DestS = Op.getOperand(3);
665 SDOperand SrcS = Op.getOperand(4);
666
667 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP, SrcS);
668 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val,
669 DestP, DestS);
670 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
671 DAG.getConstant(8, MVT::i64));
672 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
673 DAG.getSrcValue(0), MVT::i32);
674 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
675 DAG.getConstant(8, MVT::i64));
676 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
677 Val, NPD, DAG.getSrcValue(0),DAG.getValueType(MVT::i32));
678 }
679 case ISD::VASTART: {
680 SDOperand Chain = Op.getOperand(0);
681 SDOperand VAListP = Op.getOperand(1);
682 SDOperand VAListS = Op.getOperand(2);
683
684 // vastart stores the address of the VarArgsBase and VarArgsOffset
685 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
686 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
687 VAListS);
688 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
689 DAG.getConstant(8, MVT::i64));
690 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
691 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
692 DAG.getSrcValue(0), DAG.getValueType(MVT::i32));
693 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000694 }
695
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000696 return SDOperand();
697}
Nate Begeman0aed7842006-01-28 03:14:31 +0000698
699SDOperand AlphaTargetLowering::CustomPromoteOperation(SDOperand Op,
700 SelectionDAG &DAG) {
701 assert(Op.getValueType() == MVT::i32 &&
702 Op.getOpcode() == ISD::VAARG &&
703 "Unknown node to custom promote!");
704
705 // The code in LowerOperation already handles i32 vaarg
706 return LowerOperation(Op, DAG);
707}