blob: 6779b90bd3a69a5aa66a971a0c9d1d72ad65bc6b [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
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000148 addLegalFPImmediate(+0.0); //F31
149 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000150
151 computeRegisterProperties();
152
153 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000154}
155
Andrew Lenharth84a06052006-01-16 19:53:25 +0000156const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
157 switch (Opcode) {
158 default: return 0;
159 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
160 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
161 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
162 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
163 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
164 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
165 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
166 case AlphaISD::RelLit: return "Alpha::RelLit";
167 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
Chris Lattner2d90bd52006-01-27 23:39:00 +0000168 case AlphaISD::CALL: return "Alpha::CALL";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000169 case AlphaISD::DivCall: return "Alpha::DivCall";
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000170 case AlphaISD::LDQ_: return "Alpha::LDQ_";
171 case AlphaISD::LDT_: return "Alpha::LDT_";
172 case AlphaISD::LDS_: return "Alpha::LDS_";
173 case AlphaISD::LDL_: return "Alpha::LDL_";
174 case AlphaISD::LDWU_: return "Alpha::LDWU_";
175 case AlphaISD::LDBU_: return "Alpha::LDBU_";
Andrew Lenharth66e49582006-01-23 21:51:33 +0000176 case AlphaISD::STQ_: return "Alpha::STQ_";
177 case AlphaISD::STT_: return "Alpha::STT_";
178 case AlphaISD::STS_: return "Alpha::STS_";
179 case AlphaISD::STL_: return "Alpha::STL_";
180 case AlphaISD::STW_: return "Alpha::STW_";
181 case AlphaISD::STB_: return "Alpha::STB_";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000182 }
183}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000184
185//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
186
187//For now, just use variable size stack frame format
188
189//In a standard call, the first six items are passed in registers $16
190//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
191//of argument-to-register correspondence.) The remaining items are
192//collected in a memory argument list that is a naturally aligned
193//array of quadwords. In a standard call, this list, if present, must
194//be passed at 0(SP).
195//7 ... n 0(SP) ... (n-7)*8(SP)
196
197// //#define FP $15
198// //#define RA $26
199// //#define PV $27
200// //#define GP $29
201// //#define SP $30
202
203std::vector<SDOperand>
204AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
205{
206 MachineFunction &MF = DAG.getMachineFunction();
207 MachineFrameInfo *MFI = MF.getFrameInfo();
208 MachineBasicBlock& BB = MF.front();
209 std::vector<SDOperand> ArgValues;
210
Andrew Lenharthf71df332005-09-04 06:12:19 +0000211 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000212 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000213 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000214 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000215
216 int count = 0;
217
218 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
219 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
220
221 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
222 {
223 SDOperand argt;
224 if (count < 6) {
225 unsigned Vreg;
226 MVT::ValueType VT = getValueType(I->getType());
227 switch (VT) {
228 default:
229 std::cerr << "Unknown Type " << VT << "\n";
230 abort();
231 case MVT::f64:
232 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000233 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000234 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
235 DAG.setRoot(argt.getValue(1));
236 break;
237 case MVT::i1:
238 case MVT::i8:
239 case MVT::i16:
240 case MVT::i32:
241 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000242 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000243 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
244 DAG.setRoot(argt.getValue(1));
245 if (VT != MVT::i64) {
246 unsigned AssertOp =
247 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
248 argt = DAG.getNode(AssertOp, MVT::i64, argt,
249 DAG.getValueType(VT));
250 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
251 }
252 break;
253 }
254 } else { //more args
255 // Create the frame index object for this incoming parameter...
256 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
257
258 // Create the SelectionDAG nodes corresponding to a load
259 //from this parameter
260 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
261 argt = DAG.getLoad(getValueType(I->getType()),
262 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
263 }
264 ++count;
265 ArgValues.push_back(argt);
266 }
267
268 // If the functions takes variable number of arguments, copy all regs to stack
269 if (F.isVarArg()) {
270 VarArgsOffset = count * 8;
271 std::vector<SDOperand> LS;
272 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000273 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000274 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000275 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
276 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
277 if (i == 0) VarArgsBase = FI;
278 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
279 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
280 SDFI, DAG.getSrcValue(NULL)));
281
Chris Lattnerf2cded72005-09-13 19:03:13 +0000282 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000283 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000284 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
285 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
286 SDFI = DAG.getFrameIndex(FI, MVT::i64);
287 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
288 SDFI, DAG.getSrcValue(NULL)));
289 }
290
291 //Set up a token factor with all the stack traffic
292 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
293 }
294
295 // Finally, inform the code generator which regs we return values in.
296 switch (getValueType(F.getReturnType())) {
297 default: assert(0 && "Unknown type!");
298 case MVT::isVoid: break;
299 case MVT::i1:
300 case MVT::i8:
301 case MVT::i16:
302 case MVT::i32:
303 case MVT::i64:
304 MF.addLiveOut(Alpha::R0);
305 break;
306 case MVT::f32:
307 case MVT::f64:
308 MF.addLiveOut(Alpha::F0);
309 break;
310 }
311
312 //return the arguments
313 return ArgValues;
314}
315
316std::pair<SDOperand, SDOperand>
317AlphaTargetLowering::LowerCallTo(SDOperand Chain,
318 const Type *RetTy, bool isVarArg,
319 unsigned CallingConv, bool isTailCall,
320 SDOperand Callee, ArgListTy &Args,
321 SelectionDAG &DAG) {
322 int NumBytes = 0;
323 if (Args.size() > 6)
324 NumBytes = (Args.size() - 6) * 8;
325
326 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
327 DAG.getConstant(NumBytes, getPointerTy()));
328 std::vector<SDOperand> args_to_use;
329 for (unsigned i = 0, e = Args.size(); i != e; ++i)
330 {
331 switch (getValueType(Args[i].second)) {
332 default: assert(0 && "Unexpected ValueType for argument!");
333 case MVT::i1:
334 case MVT::i8:
335 case MVT::i16:
336 case MVT::i32:
337 // Promote the integer to 64 bits. If the input type is signed use a
338 // sign extend, otherwise use a zero extend.
339 if (Args[i].second->isSigned())
340 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
341 else
342 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
343 break;
344 case MVT::i64:
345 case MVT::f64:
346 case MVT::f32:
347 break;
348 }
349 args_to_use.push_back(Args[i].first);
350 }
351
352 std::vector<MVT::ValueType> RetVals;
353 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000354 MVT::ValueType ActualRetTyVT = RetTyVT;
355 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
356 ActualRetTyVT = MVT::i64;
357
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000358 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000359 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000360 RetVals.push_back(MVT::Other);
361
Chris Lattner2d90bd52006-01-27 23:39:00 +0000362 std::vector<SDOperand> Ops;
363 Ops.push_back(Chain);
364 Ops.push_back(Callee);
365 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
366 SDOperand TheCall = DAG.getNode(AlphaISD::CALL, RetVals, Ops);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000367 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
368 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
369 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000370 SDOperand RetVal = TheCall;
371
372 if (RetTyVT != ActualRetTyVT) {
373 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
374 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
375 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
376 }
377
378 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000379}
380
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000381void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
382{
383 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
384}
385void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
386{
387 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
388}
389
390
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000391
392static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
393{
394 fun = type = offset = 0;
395 if (v == NULL) {
396 type = 0;
397 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
398 type = 1;
399 const Module* M = GV->getParent();
400 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
401 ++offset;
402 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
403 type = 2;
404 const Function* F = Arg->getParent();
405 const Module* M = F->getParent();
406 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
407 ++fun;
408 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
409 ++offset;
410 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
411 assert(dyn_cast<PointerType>(I->getType()));
412 type = 3;
413 const BasicBlock* bb = I->getParent();
414 const Function* F = bb->getParent();
415 const Module* M = F->getParent();
416 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
417 ++fun;
418 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
419 offset += ii->size();
420 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
421 ++offset;
422 } else if (const Constant* C = dyn_cast<Constant>(v)) {
423 //Don't know how to look these up yet
424 type = 0;
425 } else {
426 assert(0 && "Error in value marking");
427 }
428 //type = 4: register spilling
429 //type = 5: global address loading or constant loading
430}
431
432static int getUID()
433{
434 static int id = 0;
435 return ++id;
436}
437
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000438/// LowerOperation - Provide custom lowering hooks for some operations.
439///
440SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
441 switch (Op.getOpcode()) {
442 default: assert(0 && "Wasn't expecting to be able to lower this!");
443 case ISD::SINT_TO_FP: {
444 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
445 "Unhandled SINT_TO_FP type in custom expander!");
446 SDOperand LD;
447 bool isDouble = MVT::f64 == Op.getValueType();
448 if (useITOF) {
449 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
450 } else {
451 int FrameIdx =
452 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
453 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
454 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
455 Op.getOperand(0), FI, DAG.getSrcValue(0));
456 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
457 }
458 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
459 isDouble?MVT::f64:MVT::f32, LD);
460 return FP;
461 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000462 case ISD::FP_TO_SINT: {
463 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
464 SDOperand src = Op.getOperand(0);
465
466 if (!isDouble) //Promote
467 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
468
469 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
470
471 if (useITOF) {
472 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
473 } else {
474 int FrameIdx =
475 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
476 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
477 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
478 src, FI, DAG.getSrcValue(0));
479 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
480 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000481 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000482 case ISD::ConstantPool: {
483 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
484 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
485
486 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
487 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
488 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
489 return Lo;
490 }
491 case ISD::GlobalAddress: {
492 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
493 GlobalValue *GV = GSDN->getGlobal();
494 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
495
496 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
497 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
498 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
499 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
500 return Lo;
501 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000502 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000503 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000504 case ISD::ExternalSymbol: {
505 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
506 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
507 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
508 }
509
510 case ISD::SDIV:
511 case ISD::UDIV:
512 case ISD::UREM:
513 case ISD::SREM:
514 if (MVT::isInteger(Op.getValueType())) {
515 const char* opstr = 0;
516 switch(Op.getOpcode()) {
517 case ISD::UREM: opstr = "__remqu"; break;
518 case ISD::SREM: opstr = "__remq"; break;
519 case ISD::UDIV: opstr = "__divqu"; break;
520 case ISD::SDIV: opstr = "__divq"; break;
521 }
522 SDOperand Tmp1 = Op.getOperand(0),
523 Tmp2 = Op.getOperand(1),
524 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
525 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
526 }
527 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000528
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000529 case ISD::LOAD:
530 case ISD::SEXTLOAD:
531 case ISD::ZEXTLOAD:
Andrew Lenharth87076052006-01-23 21:23:26 +0000532 case ISD::EXTLOAD:
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000533 {
534 SDOperand Chain = Op.getOperand(0);
535 SDOperand Address = Op.getOperand(1);
536
537 unsigned Opc;
538 unsigned opcode = Op.getOpcode();
539
540 if (opcode == ISD::LOAD)
541 switch (Op.Val->getValueType(0)) {
542 default: Op.Val->dump(); assert(0 && "Bad load!");
543 case MVT::i64: Opc = AlphaISD::LDQ_; break;
544 case MVT::f64: Opc = AlphaISD::LDT_; break;
545 case MVT::f32: Opc = AlphaISD::LDS_; break;
546 }
547 else
548 switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
549 default: Op.Val->dump(); assert(0 && "Bad sign extend!");
550 case MVT::i32: Opc = AlphaISD::LDL_;
551 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
552 case MVT::i16: Opc = AlphaISD::LDWU_;
553 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
554 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
555 case MVT::i8: Opc = AlphaISD::LDBU_;
556 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
557 }
558
559 int i, j, k;
560 getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
561
562 SDOperand Zero = DAG.getConstant(0, MVT::i64);
563 std::vector<MVT::ValueType> VTS;
564 VTS.push_back(Op.Val->getValueType(0));
565 VTS.push_back(MVT::Other);
566 std::vector<SDOperand> ARGS;
Andrew Lenharth87076052006-01-23 21:23:26 +0000567 ARGS.push_back(Chain);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000568 ARGS.push_back(Zero);
569 ARGS.push_back(Address);
570 ARGS.push_back(DAG.getConstant(i, MVT::i64));
571 ARGS.push_back(DAG.getConstant(j, MVT::i64));
572 ARGS.push_back(DAG.getConstant(k, MVT::i64));
573 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000574 return DAG.getNode(Opc, VTS, ARGS);
575 }
576
Andrew Lenharth66e49582006-01-23 21:51:33 +0000577 case ISD::TRUNCSTORE:
578 case ISD::STORE:
579 {
580 SDOperand Chain = Op.getOperand(0);
581 SDOperand Value = Op.getOperand(1);
582 SDOperand Address = Op.getOperand(2);
583
584 unsigned Opc;
585 unsigned opcode = Op.getOpcode();
586
587 if (opcode == ISD::STORE) {
588 switch(Value.getValueType()) {
589 default: assert(0 && "unknown Type in store");
590 case MVT::i64: Opc = AlphaISD::STQ_; break;
591 case MVT::f64: Opc = AlphaISD::STT_; break;
592 case MVT::f32: Opc = AlphaISD::STS_; break;
593 }
594 } else { //ISD::TRUNCSTORE
595 switch(cast<VTSDNode>(Op.getOperand(4))->getVT()) {
596 default: assert(0 && "unknown Type in store");
597 case MVT::i8: Opc = AlphaISD::STB_; break;
598 case MVT::i16: Opc = AlphaISD::STW_; break;
599 case MVT::i32: Opc = AlphaISD::STL_; break;
600 }
601 }
602
603 int i, j, k;
604 getValueInfo(cast<SrcValueSDNode>(Op.getOperand(3))->getValue(), i, j, k);
605
606 SDOperand Zero = DAG.getConstant(0, MVT::i64);
607 std::vector<MVT::ValueType> VTS;
608 VTS.push_back(MVT::Other);
609 std::vector<SDOperand> ARGS;
610 ARGS.push_back(Chain);
611 ARGS.push_back(Value);
612 ARGS.push_back(Zero);
613 ARGS.push_back(Address);
614 ARGS.push_back(DAG.getConstant(i, MVT::i64));
615 ARGS.push_back(DAG.getConstant(j, MVT::i64));
616 ARGS.push_back(DAG.getConstant(k, MVT::i64));
617 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
618 return DAG.getNode(Opc, VTS, ARGS);
619 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000620 case ISD::VAARG: {
621 SDOperand Chain = Op.getOperand(0);
622 SDOperand VAListP = Op.getOperand(1);
623 SDOperand VAListS = Op.getOperand(2);
624
625 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP, VAListS);
626 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
627 DAG.getConstant(8, MVT::i64));
628 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
629 Tmp, DAG.getSrcValue(0), MVT::i32);
630 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
631 if (MVT::isFloatingPoint(Op.getValueType()))
632 {
633 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
634 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
635 DAG.getConstant(8*6, MVT::i64));
636 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
637 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
638 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
639 }
Andrew Lenharth66e49582006-01-23 21:51:33 +0000640
Nate Begemanacc398c2006-01-25 18:21:52 +0000641 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
642 DAG.getConstant(8, MVT::i64));
643 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
644 Offset.getValue(1), NewOffset,
645 Tmp, DAG.getSrcValue(0),
646 DAG.getValueType(MVT::i32));
647
648 SDOperand Result;
649 if (Op.getValueType() == MVT::i32)
650 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Update, DataPtr,
651 DAG.getSrcValue(0), MVT::i32);
652 else
653 Result = DAG.getLoad(Op.getValueType(), Update, DataPtr,
654 DAG.getSrcValue(0));
655 return Result;
656 }
657 case ISD::VACOPY: {
658 SDOperand Chain = Op.getOperand(0);
659 SDOperand DestP = Op.getOperand(1);
660 SDOperand SrcP = Op.getOperand(2);
661 SDOperand DestS = Op.getOperand(3);
662 SDOperand SrcS = Op.getOperand(4);
663
664 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP, SrcS);
665 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val,
666 DestP, DestS);
667 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
668 DAG.getConstant(8, MVT::i64));
669 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
670 DAG.getSrcValue(0), MVT::i32);
671 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
672 DAG.getConstant(8, MVT::i64));
673 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
674 Val, NPD, DAG.getSrcValue(0),DAG.getValueType(MVT::i32));
675 }
676 case ISD::VASTART: {
677 SDOperand Chain = Op.getOperand(0);
678 SDOperand VAListP = Op.getOperand(1);
679 SDOperand VAListS = Op.getOperand(2);
680
681 // vastart stores the address of the VarArgsBase and VarArgsOffset
682 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
683 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
684 VAListS);
685 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
686 DAG.getConstant(8, MVT::i64));
687 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
688 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
689 DAG.getSrcValue(0), DAG.getValueType(MVT::i32));
690 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000691 }
692
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000693 return SDOperand();
694}
Nate Begeman0aed7842006-01-28 03:14:31 +0000695
696SDOperand AlphaTargetLowering::CustomPromoteOperation(SDOperand Op,
697 SelectionDAG &DAG) {
698 assert(Op.getValueType() == MVT::i32 &&
699 Op.getOpcode() == ISD::VAARG &&
700 "Unknown node to custom promote!");
701
702 // The code in LowerOperation already handles i32 vaarg
703 return LowerOperation(Op, DAG);
704}