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