blob: f75009ca2db60cc50eec2f7765d646705a6b8b8a [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 {
30 extern cl::opt<bool> EnableAlphaIDIV;
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000031 extern cl::opt<bool> EnableAlphaCount;
32 extern cl::opt<bool> EnableAlphaLSMark;
33}
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);
76 }
77
Chris Lattner3e2bafd2005-09-28 22:29:17 +000078 setOperationAction(ISD::FREM, MVT::f32, Expand);
79 setOperationAction(ISD::FREM, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000080
81 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth7f0db912005-11-30 07:19:56 +000082 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Andrew Lenharthcd804962005-11-30 16:10:29 +000083 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
84 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
85
Andrew Lenharth120ab482005-09-29 22:54:56 +000086 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000087 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
88 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
89 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
90 }
Nate Begemand88fc032006-01-14 03:14:10 +000091 setOperationAction(ISD::BSWAP , MVT::i64, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +000092 setOperationAction(ISD::ROTL , MVT::i64, Expand);
93 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000094
Andrew Lenharth53d89702005-12-25 01:34:27 +000095 setOperationAction(ISD::SREM , MVT::i64, Custom);
96 setOperationAction(ISD::UREM , MVT::i64, Custom);
97 setOperationAction(ISD::SDIV , MVT::i64, Custom);
98 setOperationAction(ISD::UDIV , MVT::i64, Custom);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000099
100 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
101 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
102 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
103
104 // We don't support sin/cos/sqrt
105 setOperationAction(ISD::FSIN , MVT::f64, Expand);
106 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000107 setOperationAction(ISD::FSIN , MVT::f32, Expand);
108 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Andrew Lenharth39424472006-01-19 21:10:38 +0000109
110 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000111 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
112
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000113 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000114
115 // We don't have line number support yet.
116 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000117 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
118 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000119
120 // Not implemented yet.
121 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
122 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharth739027e2006-01-16 21:22:38 +0000123 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
124
Andrew Lenharth53d89702005-12-25 01:34:27 +0000125 // We want to legalize GlobalAddress and ConstantPool and
126 // ExternalSymbols nodes into the appropriate instructions to
127 // materialize the address.
128 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
129 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
130 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000131
Andrew Lenharth739027e2006-01-16 21:22:38 +0000132 setStackPointerRegisterToSaveRestore(Alpha::R30);
133
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000134 addLegalFPImmediate(+0.0); //F31
135 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000136
137 computeRegisterProperties();
138
139 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000140}
141
Andrew Lenharth84a06052006-01-16 19:53:25 +0000142const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
143 switch (Opcode) {
144 default: return 0;
145 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
146 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
147 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
148 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
149 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
150 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
151 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
152 case AlphaISD::RelLit: return "Alpha::RelLit";
153 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
154 case AlphaISD::DivCall: return "Alpha::DivCall";
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000155 case AlphaISD::LDQ_: return "Alpha::LDQ_";
156 case AlphaISD::LDT_: return "Alpha::LDT_";
157 case AlphaISD::LDS_: return "Alpha::LDS_";
158 case AlphaISD::LDL_: return "Alpha::LDL_";
159 case AlphaISD::LDWU_: return "Alpha::LDWU_";
160 case AlphaISD::LDBU_: return "Alpha::LDBU_";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000161 }
162}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000163
164//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
165
166//For now, just use variable size stack frame format
167
168//In a standard call, the first six items are passed in registers $16
169//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
170//of argument-to-register correspondence.) The remaining items are
171//collected in a memory argument list that is a naturally aligned
172//array of quadwords. In a standard call, this list, if present, must
173//be passed at 0(SP).
174//7 ... n 0(SP) ... (n-7)*8(SP)
175
176// //#define FP $15
177// //#define RA $26
178// //#define PV $27
179// //#define GP $29
180// //#define SP $30
181
182std::vector<SDOperand>
183AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
184{
185 MachineFunction &MF = DAG.getMachineFunction();
186 MachineFrameInfo *MFI = MF.getFrameInfo();
187 MachineBasicBlock& BB = MF.front();
188 std::vector<SDOperand> ArgValues;
189
Andrew Lenharthf71df332005-09-04 06:12:19 +0000190 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000191 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000192 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000193 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000194
195 int count = 0;
196
197 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
198 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
199
200 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
201 {
202 SDOperand argt;
203 if (count < 6) {
204 unsigned Vreg;
205 MVT::ValueType VT = getValueType(I->getType());
206 switch (VT) {
207 default:
208 std::cerr << "Unknown Type " << VT << "\n";
209 abort();
210 case MVT::f64:
211 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000212 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000213 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
214 DAG.setRoot(argt.getValue(1));
215 break;
216 case MVT::i1:
217 case MVT::i8:
218 case MVT::i16:
219 case MVT::i32:
220 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000221 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000222 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
223 DAG.setRoot(argt.getValue(1));
224 if (VT != MVT::i64) {
225 unsigned AssertOp =
226 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
227 argt = DAG.getNode(AssertOp, MVT::i64, argt,
228 DAG.getValueType(VT));
229 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
230 }
231 break;
232 }
233 } else { //more args
234 // Create the frame index object for this incoming parameter...
235 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
236
237 // Create the SelectionDAG nodes corresponding to a load
238 //from this parameter
239 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
240 argt = DAG.getLoad(getValueType(I->getType()),
241 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
242 }
243 ++count;
244 ArgValues.push_back(argt);
245 }
246
247 // If the functions takes variable number of arguments, copy all regs to stack
248 if (F.isVarArg()) {
249 VarArgsOffset = count * 8;
250 std::vector<SDOperand> LS;
251 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000252 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000253 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000254 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
255 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
256 if (i == 0) VarArgsBase = FI;
257 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
258 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
259 SDFI, DAG.getSrcValue(NULL)));
260
Chris Lattnerf2cded72005-09-13 19:03:13 +0000261 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000262 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000263 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
264 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
265 SDFI = DAG.getFrameIndex(FI, MVT::i64);
266 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
267 SDFI, DAG.getSrcValue(NULL)));
268 }
269
270 //Set up a token factor with all the stack traffic
271 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
272 }
273
274 // Finally, inform the code generator which regs we return values in.
275 switch (getValueType(F.getReturnType())) {
276 default: assert(0 && "Unknown type!");
277 case MVT::isVoid: break;
278 case MVT::i1:
279 case MVT::i8:
280 case MVT::i16:
281 case MVT::i32:
282 case MVT::i64:
283 MF.addLiveOut(Alpha::R0);
284 break;
285 case MVT::f32:
286 case MVT::f64:
287 MF.addLiveOut(Alpha::F0);
288 break;
289 }
290
291 //return the arguments
292 return ArgValues;
293}
294
295std::pair<SDOperand, SDOperand>
296AlphaTargetLowering::LowerCallTo(SDOperand Chain,
297 const Type *RetTy, bool isVarArg,
298 unsigned CallingConv, bool isTailCall,
299 SDOperand Callee, ArgListTy &Args,
300 SelectionDAG &DAG) {
301 int NumBytes = 0;
302 if (Args.size() > 6)
303 NumBytes = (Args.size() - 6) * 8;
304
305 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
306 DAG.getConstant(NumBytes, getPointerTy()));
307 std::vector<SDOperand> args_to_use;
308 for (unsigned i = 0, e = Args.size(); i != e; ++i)
309 {
310 switch (getValueType(Args[i].second)) {
311 default: assert(0 && "Unexpected ValueType for argument!");
312 case MVT::i1:
313 case MVT::i8:
314 case MVT::i16:
315 case MVT::i32:
316 // Promote the integer to 64 bits. If the input type is signed use a
317 // sign extend, otherwise use a zero extend.
318 if (Args[i].second->isSigned())
319 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
320 else
321 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
322 break;
323 case MVT::i64:
324 case MVT::f64:
325 case MVT::f32:
326 break;
327 }
328 args_to_use.push_back(Args[i].first);
329 }
330
331 std::vector<MVT::ValueType> RetVals;
332 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000333 MVT::ValueType ActualRetTyVT = RetTyVT;
334 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
335 ActualRetTyVT = MVT::i64;
336
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000337 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000338 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000339 RetVals.push_back(MVT::Other);
340
341 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
342 Chain, Callee, args_to_use), 0);
343 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
344 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
345 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000346 SDOperand RetVal = TheCall;
347
348 if (RetTyVT != ActualRetTyVT) {
349 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
350 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
351 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
352 }
353
354 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000355}
356
357SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
358 Value *VAListV, SelectionDAG &DAG) {
359 // vastart stores the address of the VarArgsBase and VarArgsOffset
360 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
361 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
362 DAG.getSrcValue(VAListV));
363 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
364 DAG.getConstant(8, MVT::i64));
365 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
366 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
367 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
368}
369
370std::pair<SDOperand,SDOperand> AlphaTargetLowering::
371LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
372 const Type *ArgTy, SelectionDAG &DAG) {
373 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
374 DAG.getSrcValue(VAListV));
375 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
376 DAG.getConstant(8, MVT::i64));
377 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
378 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
379 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
380 if (ArgTy->isFloatingPoint())
381 {
382 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
383 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
384 DAG.getConstant(8*6, MVT::i64));
385 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
386 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
387 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
388 }
389
390 SDOperand Result;
391 if (ArgTy == Type::IntTy)
392 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
393 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
394 else if (ArgTy == Type::UIntTy)
395 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
396 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
397 else
398 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
399 DAG.getSrcValue(NULL));
400
401 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
402 DAG.getConstant(8, MVT::i64));
403 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
404 Result.getValue(1), NewOffset,
405 Tmp, DAG.getSrcValue(VAListV, 8),
406 DAG.getValueType(MVT::i32));
407 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
408
409 return std::make_pair(Result, Update);
410}
411
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000412SDOperand AlphaTargetLowering::
413LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
414 Value *DestV, SelectionDAG &DAG) {
415 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
416 DAG.getSrcValue(SrcV));
417 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
418 Val, DestP, DAG.getSrcValue(DestV));
419 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
420 DAG.getConstant(8, MVT::i64));
421 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
422 DAG.getSrcValue(SrcV, 8), MVT::i32);
423 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
424 DAG.getConstant(8, MVT::i64));
425 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
426 Val, NPD, DAG.getSrcValue(DestV, 8),
427 DAG.getValueType(MVT::i32));
428}
429
430void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
431{
432 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
433}
434void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
435{
436 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
437}
438
439
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000440
441static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
442{
443 fun = type = offset = 0;
444 if (v == NULL) {
445 type = 0;
446 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
447 type = 1;
448 const Module* M = GV->getParent();
449 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
450 ++offset;
451 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
452 type = 2;
453 const Function* F = Arg->getParent();
454 const Module* M = F->getParent();
455 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
456 ++fun;
457 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
458 ++offset;
459 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
460 assert(dyn_cast<PointerType>(I->getType()));
461 type = 3;
462 const BasicBlock* bb = I->getParent();
463 const Function* F = bb->getParent();
464 const Module* M = F->getParent();
465 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
466 ++fun;
467 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
468 offset += ii->size();
469 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
470 ++offset;
471 } else if (const Constant* C = dyn_cast<Constant>(v)) {
472 //Don't know how to look these up yet
473 type = 0;
474 } else {
475 assert(0 && "Error in value marking");
476 }
477 //type = 4: register spilling
478 //type = 5: global address loading or constant loading
479}
480
481static int getUID()
482{
483 static int id = 0;
484 return ++id;
485}
486
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000487/// LowerOperation - Provide custom lowering hooks for some operations.
488///
489SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
490 switch (Op.getOpcode()) {
491 default: assert(0 && "Wasn't expecting to be able to lower this!");
492 case ISD::SINT_TO_FP: {
493 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
494 "Unhandled SINT_TO_FP type in custom expander!");
495 SDOperand LD;
496 bool isDouble = MVT::f64 == Op.getValueType();
497 if (useITOF) {
498 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
499 } else {
500 int FrameIdx =
501 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
502 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
503 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
504 Op.getOperand(0), FI, DAG.getSrcValue(0));
505 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
506 }
507 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
508 isDouble?MVT::f64:MVT::f32, LD);
509 return FP;
510 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000511 case ISD::FP_TO_SINT: {
512 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
513 SDOperand src = Op.getOperand(0);
514
515 if (!isDouble) //Promote
516 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
517
518 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
519
520 if (useITOF) {
521 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
522 } else {
523 int FrameIdx =
524 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
525 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
526 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
527 src, FI, DAG.getSrcValue(0));
528 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
529 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000530 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000531 case ISD::ConstantPool: {
532 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
533 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
534
535 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
536 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
537 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
538 return Lo;
539 }
540 case ISD::GlobalAddress: {
541 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
542 GlobalValue *GV = GSDN->getGlobal();
543 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
544
545 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
546 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
547 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
548 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
549 return Lo;
550 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000551 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000552 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000553 case ISD::ExternalSymbol: {
554 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
555 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
556 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
557 }
558
559 case ISD::SDIV:
560 case ISD::UDIV:
561 case ISD::UREM:
562 case ISD::SREM:
563 if (MVT::isInteger(Op.getValueType())) {
564 const char* opstr = 0;
565 switch(Op.getOpcode()) {
566 case ISD::UREM: opstr = "__remqu"; break;
567 case ISD::SREM: opstr = "__remq"; break;
568 case ISD::UDIV: opstr = "__divqu"; break;
569 case ISD::SDIV: opstr = "__divq"; break;
570 }
571 SDOperand Tmp1 = Op.getOperand(0),
572 Tmp2 = Op.getOperand(1),
573 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
574 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
575 }
576 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000577
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000578 case ISD::LOAD:
579 case ISD::SEXTLOAD:
580 case ISD::ZEXTLOAD:
581 {
582 SDOperand Chain = Op.getOperand(0);
583 SDOperand Address = Op.getOperand(1);
584
585 unsigned Opc;
586 unsigned opcode = Op.getOpcode();
587
588 if (opcode == ISD::LOAD)
589 switch (Op.Val->getValueType(0)) {
590 default: Op.Val->dump(); assert(0 && "Bad load!");
591 case MVT::i64: Opc = AlphaISD::LDQ_; break;
592 case MVT::f64: Opc = AlphaISD::LDT_; break;
593 case MVT::f32: Opc = AlphaISD::LDS_; break;
594 }
595 else
596 switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
597 default: Op.Val->dump(); assert(0 && "Bad sign extend!");
598 case MVT::i32: Opc = AlphaISD::LDL_;
599 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
600 case MVT::i16: Opc = AlphaISD::LDWU_;
601 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
602 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
603 case MVT::i8: Opc = AlphaISD::LDBU_;
604 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
605 }
606
607 int i, j, k;
608 getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
609
610 SDOperand Zero = DAG.getConstant(0, MVT::i64);
611 std::vector<MVT::ValueType> VTS;
612 VTS.push_back(Op.Val->getValueType(0));
613 VTS.push_back(MVT::Other);
614 std::vector<SDOperand> ARGS;
615 ARGS.push_back(Zero);
616 ARGS.push_back(Address);
617 ARGS.push_back(DAG.getConstant(i, MVT::i64));
618 ARGS.push_back(DAG.getConstant(j, MVT::i64));
619 ARGS.push_back(DAG.getConstant(k, MVT::i64));
620 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
621 ARGS.push_back(Chain);
622 return DAG.getNode(Opc, VTS, ARGS);
623 }
624
Andrew Lenharthcd804962005-11-30 16:10:29 +0000625 }
626
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000627 return SDOperand();
628}