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