blob: 67e49c54f2d4998aee6aa3a3aaece2d24e8eab55 [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"
23#include "llvm/Support/CommandLine.h"
24#include <iostream>
25
26using namespace llvm;
27
28namespace llvm {
29 extern cl::opt<bool> EnableAlphaIDIV;
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000030 extern cl::opt<bool> EnableAlphaCount;
31 extern cl::opt<bool> EnableAlphaLSMark;
32}
33
34/// AddLiveIn - This helper function adds the specified physical register to the
35/// MachineFunction as a live in value. It also creates a corresponding virtual
36/// register for it.
37static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
38 TargetRegisterClass *RC) {
39 assert(RC->contains(PReg) && "Not the correct regclass!");
40 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
41 MF.addLiveIn(PReg, VReg);
42 return VReg;
43}
44
45AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
46 // Set up the TargetLowering object.
47 //I am having problems with shr n ubyte 1
48 setShiftAmountType(MVT::i64);
49 setSetCCResultType(MVT::i64);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
51
52 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000053 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
54 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000055
56 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
57 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
58
59 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
60 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
61
62 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
63 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
64
65 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
66 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
67 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
68
Andrew Lenharthf3fb71b2005-10-06 16:54:29 +000069 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
70
Chris Lattner3e2bafd2005-09-28 22:29:17 +000071 setOperationAction(ISD::FREM, MVT::f32, Expand);
72 setOperationAction(ISD::FREM, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000073
74 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth7f0db912005-11-30 07:19:56 +000075 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Andrew Lenharthcd804962005-11-30 16:10:29 +000076 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
77 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
78
Andrew Lenharth120ab482005-09-29 22:54:56 +000079 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000080 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
81 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
82 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
83 }
Nate Begemand88fc032006-01-14 03:14:10 +000084 setOperationAction(ISD::BSWAP , MVT::i64, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +000085 setOperationAction(ISD::ROTL , MVT::i64, Expand);
86 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000087
Andrew Lenharth53d89702005-12-25 01:34:27 +000088 setOperationAction(ISD::SREM , MVT::i64, Custom);
89 setOperationAction(ISD::UREM , MVT::i64, Custom);
90 setOperationAction(ISD::SDIV , MVT::i64, Custom);
91 setOperationAction(ISD::UDIV , MVT::i64, Custom);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000092
93 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
94 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
95 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
96
97 // We don't support sin/cos/sqrt
98 setOperationAction(ISD::FSIN , MVT::f64, Expand);
99 setOperationAction(ISD::FCOS , MVT::f64, Expand);
100 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
101 setOperationAction(ISD::FSIN , MVT::f32, Expand);
102 setOperationAction(ISD::FCOS , MVT::f32, Expand);
103 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
104
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000105 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000106
107 // We don't have line number support yet.
108 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000109 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
110 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000111
112 // Not implemented yet.
113 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
114 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000115
Andrew Lenharth53d89702005-12-25 01:34:27 +0000116 // We want to legalize GlobalAddress and ConstantPool and
117 // ExternalSymbols nodes into the appropriate instructions to
118 // materialize the address.
119 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
120 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
121 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000122
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000123 addLegalFPImmediate(+0.0); //F31
124 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000125
126 computeRegisterProperties();
127
128 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000129}
130
Andrew Lenharth84a06052006-01-16 19:53:25 +0000131const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
132 switch (Opcode) {
133 default: return 0;
134 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
135 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
136 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
137 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
138 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
139 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
140 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
141 case AlphaISD::RelLit: return "Alpha::RelLit";
142 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
143 case AlphaISD::DivCall: return "Alpha::DivCall";
144 }
145}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000146
147//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
148
149//For now, just use variable size stack frame format
150
151//In a standard call, the first six items are passed in registers $16
152//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
153//of argument-to-register correspondence.) The remaining items are
154//collected in a memory argument list that is a naturally aligned
155//array of quadwords. In a standard call, this list, if present, must
156//be passed at 0(SP).
157//7 ... n 0(SP) ... (n-7)*8(SP)
158
159// //#define FP $15
160// //#define RA $26
161// //#define PV $27
162// //#define GP $29
163// //#define SP $30
164
165std::vector<SDOperand>
166AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
167{
168 MachineFunction &MF = DAG.getMachineFunction();
169 MachineFrameInfo *MFI = MF.getFrameInfo();
170 MachineBasicBlock& BB = MF.front();
171 std::vector<SDOperand> ArgValues;
172
Andrew Lenharthf71df332005-09-04 06:12:19 +0000173 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000174 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000175 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000176 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000177
178 int count = 0;
179
180 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
181 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
182
183 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
184 {
185 SDOperand argt;
186 if (count < 6) {
187 unsigned Vreg;
188 MVT::ValueType VT = getValueType(I->getType());
189 switch (VT) {
190 default:
191 std::cerr << "Unknown Type " << VT << "\n";
192 abort();
193 case MVT::f64:
194 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000195 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000196 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
197 DAG.setRoot(argt.getValue(1));
198 break;
199 case MVT::i1:
200 case MVT::i8:
201 case MVT::i16:
202 case MVT::i32:
203 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000204 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000205 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
206 DAG.setRoot(argt.getValue(1));
207 if (VT != MVT::i64) {
208 unsigned AssertOp =
209 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
210 argt = DAG.getNode(AssertOp, MVT::i64, argt,
211 DAG.getValueType(VT));
212 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
213 }
214 break;
215 }
216 } else { //more args
217 // Create the frame index object for this incoming parameter...
218 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
219
220 // Create the SelectionDAG nodes corresponding to a load
221 //from this parameter
222 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
223 argt = DAG.getLoad(getValueType(I->getType()),
224 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
225 }
226 ++count;
227 ArgValues.push_back(argt);
228 }
229
230 // If the functions takes variable number of arguments, copy all regs to stack
231 if (F.isVarArg()) {
232 VarArgsOffset = count * 8;
233 std::vector<SDOperand> LS;
234 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000235 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000236 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000237 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
238 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
239 if (i == 0) VarArgsBase = FI;
240 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
241 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
242 SDFI, DAG.getSrcValue(NULL)));
243
Chris Lattnerf2cded72005-09-13 19:03:13 +0000244 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000245 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000246 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
247 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
248 SDFI = DAG.getFrameIndex(FI, MVT::i64);
249 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
250 SDFI, DAG.getSrcValue(NULL)));
251 }
252
253 //Set up a token factor with all the stack traffic
254 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
255 }
256
257 // Finally, inform the code generator which regs we return values in.
258 switch (getValueType(F.getReturnType())) {
259 default: assert(0 && "Unknown type!");
260 case MVT::isVoid: break;
261 case MVT::i1:
262 case MVT::i8:
263 case MVT::i16:
264 case MVT::i32:
265 case MVT::i64:
266 MF.addLiveOut(Alpha::R0);
267 break;
268 case MVT::f32:
269 case MVT::f64:
270 MF.addLiveOut(Alpha::F0);
271 break;
272 }
273
274 //return the arguments
275 return ArgValues;
276}
277
278std::pair<SDOperand, SDOperand>
279AlphaTargetLowering::LowerCallTo(SDOperand Chain,
280 const Type *RetTy, bool isVarArg,
281 unsigned CallingConv, bool isTailCall,
282 SDOperand Callee, ArgListTy &Args,
283 SelectionDAG &DAG) {
284 int NumBytes = 0;
285 if (Args.size() > 6)
286 NumBytes = (Args.size() - 6) * 8;
287
288 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
289 DAG.getConstant(NumBytes, getPointerTy()));
290 std::vector<SDOperand> args_to_use;
291 for (unsigned i = 0, e = Args.size(); i != e; ++i)
292 {
293 switch (getValueType(Args[i].second)) {
294 default: assert(0 && "Unexpected ValueType for argument!");
295 case MVT::i1:
296 case MVT::i8:
297 case MVT::i16:
298 case MVT::i32:
299 // Promote the integer to 64 bits. If the input type is signed use a
300 // sign extend, otherwise use a zero extend.
301 if (Args[i].second->isSigned())
302 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
303 else
304 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
305 break;
306 case MVT::i64:
307 case MVT::f64:
308 case MVT::f32:
309 break;
310 }
311 args_to_use.push_back(Args[i].first);
312 }
313
314 std::vector<MVT::ValueType> RetVals;
315 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000316 MVT::ValueType ActualRetTyVT = RetTyVT;
317 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
318 ActualRetTyVT = MVT::i64;
319
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000320 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000321 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000322 RetVals.push_back(MVT::Other);
323
324 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
325 Chain, Callee, args_to_use), 0);
326 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
327 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
328 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000329 SDOperand RetVal = TheCall;
330
331 if (RetTyVT != ActualRetTyVT) {
332 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
333 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
334 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
335 }
336
337 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000338}
339
340SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
341 Value *VAListV, SelectionDAG &DAG) {
342 // vastart stores the address of the VarArgsBase and VarArgsOffset
343 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
344 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
345 DAG.getSrcValue(VAListV));
346 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
347 DAG.getConstant(8, MVT::i64));
348 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
349 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
350 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
351}
352
353std::pair<SDOperand,SDOperand> AlphaTargetLowering::
354LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
355 const Type *ArgTy, SelectionDAG &DAG) {
356 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
357 DAG.getSrcValue(VAListV));
358 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
359 DAG.getConstant(8, MVT::i64));
360 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
361 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
362 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
363 if (ArgTy->isFloatingPoint())
364 {
365 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
366 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
367 DAG.getConstant(8*6, MVT::i64));
368 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
369 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
370 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
371 }
372
373 SDOperand Result;
374 if (ArgTy == Type::IntTy)
375 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
376 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
377 else if (ArgTy == Type::UIntTy)
378 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
379 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
380 else
381 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
382 DAG.getSrcValue(NULL));
383
384 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
385 DAG.getConstant(8, MVT::i64));
386 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
387 Result.getValue(1), NewOffset,
388 Tmp, DAG.getSrcValue(VAListV, 8),
389 DAG.getValueType(MVT::i32));
390 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
391
392 return std::make_pair(Result, Update);
393}
394
395
396SDOperand AlphaTargetLowering::
397LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
398 Value *DestV, SelectionDAG &DAG) {
399 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
400 DAG.getSrcValue(SrcV));
401 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
402 Val, DestP, DAG.getSrcValue(DestV));
403 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
404 DAG.getConstant(8, MVT::i64));
405 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
406 DAG.getSrcValue(SrcV, 8), MVT::i32);
407 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
408 DAG.getConstant(8, MVT::i64));
409 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
410 Val, NPD, DAG.getSrcValue(DestV, 8),
411 DAG.getValueType(MVT::i32));
412}
413
414void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
415{
416 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
417}
418void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
419{
420 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
421}
422
423
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000424/// LowerOperation - Provide custom lowering hooks for some operations.
425///
426SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
427 switch (Op.getOpcode()) {
428 default: assert(0 && "Wasn't expecting to be able to lower this!");
429 case ISD::SINT_TO_FP: {
430 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
431 "Unhandled SINT_TO_FP type in custom expander!");
432 SDOperand LD;
433 bool isDouble = MVT::f64 == Op.getValueType();
434 if (useITOF) {
435 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
436 } else {
437 int FrameIdx =
438 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
439 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
440 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
441 Op.getOperand(0), FI, DAG.getSrcValue(0));
442 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
443 }
444 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
445 isDouble?MVT::f64:MVT::f32, LD);
446 return FP;
447 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000448 case ISD::FP_TO_SINT: {
449 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
450 SDOperand src = Op.getOperand(0);
451
452 if (!isDouble) //Promote
453 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
454
455 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
456
457 if (useITOF) {
458 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
459 } else {
460 int FrameIdx =
461 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
462 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
463 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
464 src, FI, DAG.getSrcValue(0));
465 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
466 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000467 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000468 case ISD::ConstantPool: {
469 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
470 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
471
472 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
473 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
474 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
475 return Lo;
476 }
477 case ISD::GlobalAddress: {
478 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
479 GlobalValue *GV = GSDN->getGlobal();
480 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
481
482 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
483 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
484 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
485 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
486 return Lo;
487 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000488 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000489 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000490 case ISD::ExternalSymbol: {
491 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
492 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
493 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
494 }
495
496 case ISD::SDIV:
497 case ISD::UDIV:
498 case ISD::UREM:
499 case ISD::SREM:
500 if (MVT::isInteger(Op.getValueType())) {
501 const char* opstr = 0;
502 switch(Op.getOpcode()) {
503 case ISD::UREM: opstr = "__remqu"; break;
504 case ISD::SREM: opstr = "__remq"; break;
505 case ISD::UDIV: opstr = "__divqu"; break;
506 case ISD::SDIV: opstr = "__divq"; break;
507 }
508 SDOperand Tmp1 = Op.getOperand(0),
509 Tmp2 = Op.getOperand(1),
510 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
511 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
512 }
513 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000514
515 }
516
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000517 return SDOperand();
518}