blob: a56b90887bfa183a8cf4a0e1cd9431fc561f05bd [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);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000100 setOperationAction(ISD::FSIN , MVT::f32, Expand);
101 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Andrew Lenharth39424472006-01-19 21:10:38 +0000102
103 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000104 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
105
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000106 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000107
108 // We don't have line number support yet.
109 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000110 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
111 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000112
113 // Not implemented yet.
114 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
115 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharth739027e2006-01-16 21:22:38 +0000116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
117
Andrew Lenharth53d89702005-12-25 01:34:27 +0000118 // We want to legalize GlobalAddress and ConstantPool and
119 // ExternalSymbols nodes into the appropriate instructions to
120 // materialize the address.
121 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
122 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
123 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000124
Andrew Lenharth739027e2006-01-16 21:22:38 +0000125 setStackPointerRegisterToSaveRestore(Alpha::R30);
126
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000127 addLegalFPImmediate(+0.0); //F31
128 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000129
130 computeRegisterProperties();
131
132 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000133}
134
Andrew Lenharth84a06052006-01-16 19:53:25 +0000135const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
136 switch (Opcode) {
137 default: return 0;
138 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
139 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
140 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
141 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
142 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
143 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
144 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
145 case AlphaISD::RelLit: return "Alpha::RelLit";
146 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
147 case AlphaISD::DivCall: return "Alpha::DivCall";
148 }
149}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000150
151//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
152
153//For now, just use variable size stack frame format
154
155//In a standard call, the first six items are passed in registers $16
156//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
157//of argument-to-register correspondence.) The remaining items are
158//collected in a memory argument list that is a naturally aligned
159//array of quadwords. In a standard call, this list, if present, must
160//be passed at 0(SP).
161//7 ... n 0(SP) ... (n-7)*8(SP)
162
163// //#define FP $15
164// //#define RA $26
165// //#define PV $27
166// //#define GP $29
167// //#define SP $30
168
169std::vector<SDOperand>
170AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
171{
172 MachineFunction &MF = DAG.getMachineFunction();
173 MachineFrameInfo *MFI = MF.getFrameInfo();
174 MachineBasicBlock& BB = MF.front();
175 std::vector<SDOperand> ArgValues;
176
Andrew Lenharthf71df332005-09-04 06:12:19 +0000177 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000178 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000179 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000180 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000181
182 int count = 0;
183
184 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
185 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
186
187 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
188 {
189 SDOperand argt;
190 if (count < 6) {
191 unsigned Vreg;
192 MVT::ValueType VT = getValueType(I->getType());
193 switch (VT) {
194 default:
195 std::cerr << "Unknown Type " << VT << "\n";
196 abort();
197 case MVT::f64:
198 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000199 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000200 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
201 DAG.setRoot(argt.getValue(1));
202 break;
203 case MVT::i1:
204 case MVT::i8:
205 case MVT::i16:
206 case MVT::i32:
207 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000208 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000209 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
210 DAG.setRoot(argt.getValue(1));
211 if (VT != MVT::i64) {
212 unsigned AssertOp =
213 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
214 argt = DAG.getNode(AssertOp, MVT::i64, argt,
215 DAG.getValueType(VT));
216 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
217 }
218 break;
219 }
220 } else { //more args
221 // Create the frame index object for this incoming parameter...
222 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
223
224 // Create the SelectionDAG nodes corresponding to a load
225 //from this parameter
226 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
227 argt = DAG.getLoad(getValueType(I->getType()),
228 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
229 }
230 ++count;
231 ArgValues.push_back(argt);
232 }
233
234 // If the functions takes variable number of arguments, copy all regs to stack
235 if (F.isVarArg()) {
236 VarArgsOffset = count * 8;
237 std::vector<SDOperand> LS;
238 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000239 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000240 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000241 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
242 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
243 if (i == 0) VarArgsBase = FI;
244 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
245 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
246 SDFI, DAG.getSrcValue(NULL)));
247
Chris Lattnerf2cded72005-09-13 19:03:13 +0000248 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000249 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000250 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
251 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
252 SDFI = DAG.getFrameIndex(FI, MVT::i64);
253 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
254 SDFI, DAG.getSrcValue(NULL)));
255 }
256
257 //Set up a token factor with all the stack traffic
258 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
259 }
260
261 // Finally, inform the code generator which regs we return values in.
262 switch (getValueType(F.getReturnType())) {
263 default: assert(0 && "Unknown type!");
264 case MVT::isVoid: break;
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 case MVT::i32:
269 case MVT::i64:
270 MF.addLiveOut(Alpha::R0);
271 break;
272 case MVT::f32:
273 case MVT::f64:
274 MF.addLiveOut(Alpha::F0);
275 break;
276 }
277
278 //return the arguments
279 return ArgValues;
280}
281
282std::pair<SDOperand, SDOperand>
283AlphaTargetLowering::LowerCallTo(SDOperand Chain,
284 const Type *RetTy, bool isVarArg,
285 unsigned CallingConv, bool isTailCall,
286 SDOperand Callee, ArgListTy &Args,
287 SelectionDAG &DAG) {
288 int NumBytes = 0;
289 if (Args.size() > 6)
290 NumBytes = (Args.size() - 6) * 8;
291
292 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
293 DAG.getConstant(NumBytes, getPointerTy()));
294 std::vector<SDOperand> args_to_use;
295 for (unsigned i = 0, e = Args.size(); i != e; ++i)
296 {
297 switch (getValueType(Args[i].second)) {
298 default: assert(0 && "Unexpected ValueType for argument!");
299 case MVT::i1:
300 case MVT::i8:
301 case MVT::i16:
302 case MVT::i32:
303 // Promote the integer to 64 bits. If the input type is signed use a
304 // sign extend, otherwise use a zero extend.
305 if (Args[i].second->isSigned())
306 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
307 else
308 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
309 break;
310 case MVT::i64:
311 case MVT::f64:
312 case MVT::f32:
313 break;
314 }
315 args_to_use.push_back(Args[i].first);
316 }
317
318 std::vector<MVT::ValueType> RetVals;
319 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000320 MVT::ValueType ActualRetTyVT = RetTyVT;
321 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
322 ActualRetTyVT = MVT::i64;
323
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000324 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000325 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000326 RetVals.push_back(MVT::Other);
327
328 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
329 Chain, Callee, args_to_use), 0);
330 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
331 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
332 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000333 SDOperand RetVal = TheCall;
334
335 if (RetTyVT != ActualRetTyVT) {
336 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
337 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
338 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
339 }
340
341 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000342}
343
344SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
345 Value *VAListV, SelectionDAG &DAG) {
346 // vastart stores the address of the VarArgsBase and VarArgsOffset
347 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
348 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
349 DAG.getSrcValue(VAListV));
350 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
351 DAG.getConstant(8, MVT::i64));
352 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
353 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
354 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
355}
356
357std::pair<SDOperand,SDOperand> AlphaTargetLowering::
358LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
359 const Type *ArgTy, SelectionDAG &DAG) {
360 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
361 DAG.getSrcValue(VAListV));
362 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
363 DAG.getConstant(8, MVT::i64));
364 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
365 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
366 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
367 if (ArgTy->isFloatingPoint())
368 {
369 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
370 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
371 DAG.getConstant(8*6, MVT::i64));
372 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
373 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
374 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
375 }
376
377 SDOperand Result;
378 if (ArgTy == Type::IntTy)
379 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
380 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
381 else if (ArgTy == Type::UIntTy)
382 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
383 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
384 else
385 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
386 DAG.getSrcValue(NULL));
387
388 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
389 DAG.getConstant(8, MVT::i64));
390 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
391 Result.getValue(1), NewOffset,
392 Tmp, DAG.getSrcValue(VAListV, 8),
393 DAG.getValueType(MVT::i32));
394 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
395
396 return std::make_pair(Result, Update);
397}
398
399
400SDOperand AlphaTargetLowering::
401LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
402 Value *DestV, SelectionDAG &DAG) {
403 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
404 DAG.getSrcValue(SrcV));
405 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
406 Val, DestP, DAG.getSrcValue(DestV));
407 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
408 DAG.getConstant(8, MVT::i64));
409 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
410 DAG.getSrcValue(SrcV, 8), MVT::i32);
411 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
412 DAG.getConstant(8, MVT::i64));
413 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
414 Val, NPD, DAG.getSrcValue(DestV, 8),
415 DAG.getValueType(MVT::i32));
416}
417
418void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
419{
420 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
421}
422void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
423{
424 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
425}
426
427
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000428/// LowerOperation - Provide custom lowering hooks for some operations.
429///
430SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
431 switch (Op.getOpcode()) {
432 default: assert(0 && "Wasn't expecting to be able to lower this!");
433 case ISD::SINT_TO_FP: {
434 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
435 "Unhandled SINT_TO_FP type in custom expander!");
436 SDOperand LD;
437 bool isDouble = MVT::f64 == Op.getValueType();
438 if (useITOF) {
439 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
440 } else {
441 int FrameIdx =
442 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
443 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
444 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
445 Op.getOperand(0), FI, DAG.getSrcValue(0));
446 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
447 }
448 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
449 isDouble?MVT::f64:MVT::f32, LD);
450 return FP;
451 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000452 case ISD::FP_TO_SINT: {
453 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
454 SDOperand src = Op.getOperand(0);
455
456 if (!isDouble) //Promote
457 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
458
459 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
460
461 if (useITOF) {
462 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
463 } else {
464 int FrameIdx =
465 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
466 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
467 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
468 src, FI, DAG.getSrcValue(0));
469 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
470 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000471 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000472 case ISD::ConstantPool: {
473 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
474 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
475
476 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
477 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
478 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
479 return Lo;
480 }
481 case ISD::GlobalAddress: {
482 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
483 GlobalValue *GV = GSDN->getGlobal();
484 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
485
486 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
487 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
488 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
489 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
490 return Lo;
491 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000492 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000493 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000494 case ISD::ExternalSymbol: {
495 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
496 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
497 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
498 }
499
500 case ISD::SDIV:
501 case ISD::UDIV:
502 case ISD::UREM:
503 case ISD::SREM:
504 if (MVT::isInteger(Op.getValueType())) {
505 const char* opstr = 0;
506 switch(Op.getOpcode()) {
507 case ISD::UREM: opstr = "__remqu"; break;
508 case ISD::SREM: opstr = "__remq"; break;
509 case ISD::UDIV: opstr = "__divqu"; break;
510 case ISD::SDIV: opstr = "__divq"; break;
511 }
512 SDOperand Tmp1 = Op.getOperand(0),
513 Tmp2 = Op.getOperand(1),
514 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
515 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
516 }
517 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000518
519 }
520
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000521 return SDOperand();
522}