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