blob: c9cafd478e51052da7ae2e48bc3e3a7270e9b05b [file] [log] [blame]
Andrew Lenharthaa38ce42005-09-02 18:46:02 +00001//===-- AlphaISelLowering.cpp - Alpha DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Andrew Lenharth and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AlphaISelLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AlphaISelLowering.h"
15#include "AlphaTargetMachine.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/SelectionDAG.h"
20#include "llvm/CodeGen/SSARegMap.h"
21#include "llvm/Constants.h"
22#include "llvm/Function.h"
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000023#include "llvm/Module.h"
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000024#include "llvm/Support/CommandLine.h"
25#include <iostream>
26
27using namespace llvm;
28
29namespace llvm {
Andrew Lenharthfabd5ba2006-01-23 21:56:07 +000030 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
31 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
32 cl::Hidden);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000033}
34
35/// AddLiveIn - This helper function adds the specified physical register to the
36/// MachineFunction as a live in value. It also creates a corresponding virtual
37/// register for it.
38static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
39 TargetRegisterClass *RC) {
40 assert(RC->contains(PReg) && "Not the correct regclass!");
41 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
42 MF.addLiveIn(PReg, VReg);
43 return VReg;
44}
45
46AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
47 // Set up the TargetLowering object.
48 //I am having problems with shr n ubyte 1
49 setShiftAmountType(MVT::i64);
50 setSetCCResultType(MVT::i64);
51 setSetCCResultContents(ZeroOrOneSetCCResult);
52
53 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000054 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
55 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000056
57 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
58 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +000059 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
60 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000061
62 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
63 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
64
65 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
66 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
67
68 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
69 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
70 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
71
Andrew Lenharthf3fb71b2005-10-06 16:54:29 +000072 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
73
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000074 if (EnableAlphaLSMark) {
75 setOperationAction(ISD::LOAD, MVT::i64, Custom);
76 setOperationAction(ISD::LOAD, MVT::f64, Custom);
77 setOperationAction(ISD::LOAD, MVT::f32, Custom);
Andrew Lenharth87076052006-01-23 21:23:26 +000078
79 setOperationAction(ISD::ZEXTLOAD, MVT::i8, Custom);
80 setOperationAction(ISD::ZEXTLOAD, MVT::i16, Custom);
81 setOperationAction(ISD::SEXTLOAD, MVT::i32, Custom);
82
83 setOperationAction(ISD::EXTLOAD, MVT::i8, Custom);
84 setOperationAction(ISD::EXTLOAD, MVT::i16, Custom);
85 setOperationAction(ISD::EXTLOAD, MVT::i32, Custom);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +000086 }
87
Chris Lattner3e2bafd2005-09-28 22:29:17 +000088 setOperationAction(ISD::FREM, MVT::f32, Expand);
89 setOperationAction(ISD::FREM, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000090
91 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth7f0db912005-11-30 07:19:56 +000092 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Andrew Lenharthcd804962005-11-30 16:10:29 +000093 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
94 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
95
Andrew Lenharth120ab482005-09-29 22:54:56 +000096 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000097 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
98 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
99 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
100 }
Nate Begemand88fc032006-01-14 03:14:10 +0000101 setOperationAction(ISD::BSWAP , MVT::i64, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000102 setOperationAction(ISD::ROTL , MVT::i64, Expand);
103 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000104
Andrew Lenharth53d89702005-12-25 01:34:27 +0000105 setOperationAction(ISD::SREM , MVT::i64, Custom);
106 setOperationAction(ISD::UREM , MVT::i64, Custom);
107 setOperationAction(ISD::SDIV , MVT::i64, Custom);
108 setOperationAction(ISD::UDIV , MVT::i64, Custom);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000109
110 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
111 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
112 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
113
114 // We don't support sin/cos/sqrt
115 setOperationAction(ISD::FSIN , MVT::f64, Expand);
116 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000117 setOperationAction(ISD::FSIN , MVT::f32, Expand);
118 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Andrew Lenharth39424472006-01-19 21:10:38 +0000119
120 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000121 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
122
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000123 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000124
125 // We don't have line number support yet.
126 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000127 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
128 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000129
130 // Not implemented yet.
131 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
132 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharth739027e2006-01-16 21:22:38 +0000133 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
134
Andrew Lenharth53d89702005-12-25 01:34:27 +0000135 // We want to legalize GlobalAddress and ConstantPool and
136 // ExternalSymbols nodes into the appropriate instructions to
137 // materialize the address.
138 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
139 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
140 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000141
Andrew Lenharth0e538792006-01-25 21:54:38 +0000142 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Andrew Lenharth677c4f22006-01-25 23:33:32 +0000143 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000144 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
Andrew Lenharth5f8f0e22006-01-25 22:28:07 +0000145 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Nate Begeman0aed7842006-01-28 03:14:31 +0000146 setOperationAction(ISD::VAARG, MVT::i32, Custom);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000147
Andrew Lenharth739027e2006-01-16 21:22:38 +0000148 setStackPointerRegisterToSaveRestore(Alpha::R30);
149
Chris Lattner08a90222006-01-29 06:25:22 +0000150 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
151 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000152 addLegalFPImmediate(+0.0); //F31
153 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000154
155 computeRegisterProperties();
156
157 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000158}
159
Andrew Lenharth84a06052006-01-16 19:53:25 +0000160const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
161 switch (Opcode) {
162 default: return 0;
163 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
164 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
165 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
166 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
167 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
168 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
169 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
170 case AlphaISD::RelLit: return "Alpha::RelLit";
171 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
Chris Lattner2d90bd52006-01-27 23:39:00 +0000172 case AlphaISD::CALL: return "Alpha::CALL";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000173 case AlphaISD::DivCall: return "Alpha::DivCall";
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000174 case AlphaISD::LDQ_: return "Alpha::LDQ_";
175 case AlphaISD::LDT_: return "Alpha::LDT_";
176 case AlphaISD::LDS_: return "Alpha::LDS_";
177 case AlphaISD::LDL_: return "Alpha::LDL_";
178 case AlphaISD::LDWU_: return "Alpha::LDWU_";
179 case AlphaISD::LDBU_: return "Alpha::LDBU_";
Andrew Lenharth66e49582006-01-23 21:51:33 +0000180 case AlphaISD::STQ_: return "Alpha::STQ_";
181 case AlphaISD::STT_: return "Alpha::STT_";
182 case AlphaISD::STS_: return "Alpha::STS_";
183 case AlphaISD::STL_: return "Alpha::STL_";
184 case AlphaISD::STW_: return "Alpha::STW_";
185 case AlphaISD::STB_: return "Alpha::STB_";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000186 }
187}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000188
189//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
190
191//For now, just use variable size stack frame format
192
193//In a standard call, the first six items are passed in registers $16
194//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
195//of argument-to-register correspondence.) The remaining items are
196//collected in a memory argument list that is a naturally aligned
197//array of quadwords. In a standard call, this list, if present, must
198//be passed at 0(SP).
199//7 ... n 0(SP) ... (n-7)*8(SP)
200
201// //#define FP $15
202// //#define RA $26
203// //#define PV $27
204// //#define GP $29
205// //#define SP $30
206
207std::vector<SDOperand>
208AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
209{
210 MachineFunction &MF = DAG.getMachineFunction();
211 MachineFrameInfo *MFI = MF.getFrameInfo();
212 MachineBasicBlock& BB = MF.front();
213 std::vector<SDOperand> ArgValues;
214
Andrew Lenharthf71df332005-09-04 06:12:19 +0000215 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000216 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000217 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000218 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000219
220 int count = 0;
221
222 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
223 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
224
225 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
226 {
227 SDOperand argt;
228 if (count < 6) {
229 unsigned Vreg;
230 MVT::ValueType VT = getValueType(I->getType());
231 switch (VT) {
232 default:
233 std::cerr << "Unknown Type " << VT << "\n";
234 abort();
235 case MVT::f64:
236 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000237 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000238 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
239 DAG.setRoot(argt.getValue(1));
240 break;
241 case MVT::i1:
242 case MVT::i8:
243 case MVT::i16:
244 case MVT::i32:
245 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000246 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000247 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
248 DAG.setRoot(argt.getValue(1));
249 if (VT != MVT::i64) {
250 unsigned AssertOp =
251 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
252 argt = DAG.getNode(AssertOp, MVT::i64, argt,
253 DAG.getValueType(VT));
254 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
255 }
256 break;
257 }
258 } else { //more args
259 // Create the frame index object for this incoming parameter...
260 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
261
262 // Create the SelectionDAG nodes corresponding to a load
263 //from this parameter
264 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
265 argt = DAG.getLoad(getValueType(I->getType()),
266 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
267 }
268 ++count;
269 ArgValues.push_back(argt);
270 }
271
272 // If the functions takes variable number of arguments, copy all regs to stack
273 if (F.isVarArg()) {
274 VarArgsOffset = count * 8;
275 std::vector<SDOperand> LS;
276 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000277 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000278 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000279 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
280 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
281 if (i == 0) VarArgsBase = FI;
282 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
283 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
284 SDFI, DAG.getSrcValue(NULL)));
285
Chris Lattnerf2cded72005-09-13 19:03:13 +0000286 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000287 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000288 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
289 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
290 SDFI = DAG.getFrameIndex(FI, MVT::i64);
291 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
292 SDFI, DAG.getSrcValue(NULL)));
293 }
294
295 //Set up a token factor with all the stack traffic
296 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
297 }
298
299 // Finally, inform the code generator which regs we return values in.
300 switch (getValueType(F.getReturnType())) {
301 default: assert(0 && "Unknown type!");
302 case MVT::isVoid: break;
303 case MVT::i1:
304 case MVT::i8:
305 case MVT::i16:
306 case MVT::i32:
307 case MVT::i64:
308 MF.addLiveOut(Alpha::R0);
309 break;
310 case MVT::f32:
311 case MVT::f64:
312 MF.addLiveOut(Alpha::F0);
313 break;
314 }
315
316 //return the arguments
317 return ArgValues;
318}
319
320std::pair<SDOperand, SDOperand>
321AlphaTargetLowering::LowerCallTo(SDOperand Chain,
322 const Type *RetTy, bool isVarArg,
323 unsigned CallingConv, bool isTailCall,
324 SDOperand Callee, ArgListTy &Args,
325 SelectionDAG &DAG) {
326 int NumBytes = 0;
327 if (Args.size() > 6)
328 NumBytes = (Args.size() - 6) * 8;
329
330 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
331 DAG.getConstant(NumBytes, getPointerTy()));
332 std::vector<SDOperand> args_to_use;
333 for (unsigned i = 0, e = Args.size(); i != e; ++i)
334 {
335 switch (getValueType(Args[i].second)) {
336 default: assert(0 && "Unexpected ValueType for argument!");
337 case MVT::i1:
338 case MVT::i8:
339 case MVT::i16:
340 case MVT::i32:
341 // Promote the integer to 64 bits. If the input type is signed use a
342 // sign extend, otherwise use a zero extend.
343 if (Args[i].second->isSigned())
344 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
345 else
346 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
347 break;
348 case MVT::i64:
349 case MVT::f64:
350 case MVT::f32:
351 break;
352 }
353 args_to_use.push_back(Args[i].first);
354 }
355
356 std::vector<MVT::ValueType> RetVals;
357 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000358 MVT::ValueType ActualRetTyVT = RetTyVT;
359 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
360 ActualRetTyVT = MVT::i64;
361
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000362 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000363 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000364 RetVals.push_back(MVT::Other);
365
Chris Lattner2d90bd52006-01-27 23:39:00 +0000366 std::vector<SDOperand> Ops;
367 Ops.push_back(Chain);
368 Ops.push_back(Callee);
369 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
370 SDOperand TheCall = DAG.getNode(AlphaISD::CALL, RetVals, Ops);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000371 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
372 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
373 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000374 SDOperand RetVal = TheCall;
375
376 if (RetTyVT != ActualRetTyVT) {
377 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
378 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
379 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
380 }
381
382 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000383}
384
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000385void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
386{
387 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
388}
389void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
390{
391 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
392}
393
394
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000395
396static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
397{
398 fun = type = offset = 0;
399 if (v == NULL) {
400 type = 0;
401 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
402 type = 1;
403 const Module* M = GV->getParent();
404 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
405 ++offset;
406 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
407 type = 2;
408 const Function* F = Arg->getParent();
409 const Module* M = F->getParent();
410 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
411 ++fun;
412 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
413 ++offset;
414 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
415 assert(dyn_cast<PointerType>(I->getType()));
416 type = 3;
417 const BasicBlock* bb = I->getParent();
418 const Function* F = bb->getParent();
419 const Module* M = F->getParent();
420 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
421 ++fun;
422 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
423 offset += ii->size();
424 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
425 ++offset;
426 } else if (const Constant* C = dyn_cast<Constant>(v)) {
427 //Don't know how to look these up yet
428 type = 0;
429 } else {
430 assert(0 && "Error in value marking");
431 }
432 //type = 4: register spilling
433 //type = 5: global address loading or constant loading
434}
435
436static int getUID()
437{
438 static int id = 0;
439 return ++id;
440}
441
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000442/// LowerOperation - Provide custom lowering hooks for some operations.
443///
444SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
445 switch (Op.getOpcode()) {
446 default: assert(0 && "Wasn't expecting to be able to lower this!");
447 case ISD::SINT_TO_FP: {
448 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
449 "Unhandled SINT_TO_FP type in custom expander!");
450 SDOperand LD;
451 bool isDouble = MVT::f64 == Op.getValueType();
452 if (useITOF) {
453 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
454 } else {
455 int FrameIdx =
456 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
457 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
458 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
459 Op.getOperand(0), FI, DAG.getSrcValue(0));
460 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
461 }
462 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
463 isDouble?MVT::f64:MVT::f32, LD);
464 return FP;
465 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000466 case ISD::FP_TO_SINT: {
467 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
468 SDOperand src = Op.getOperand(0);
469
470 if (!isDouble) //Promote
471 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
472
473 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
474
475 if (useITOF) {
476 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
477 } else {
478 int FrameIdx =
479 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
480 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
481 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
482 src, FI, DAG.getSrcValue(0));
483 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
484 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000485 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000486 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000487 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
488 Constant *C = CP->get();
489 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
Andrew Lenharth4e629512005-12-24 05:36:33 +0000490
491 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
492 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
493 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
494 return Lo;
495 }
496 case ISD::GlobalAddress: {
497 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
498 GlobalValue *GV = GSDN->getGlobal();
499 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
500
501 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
502 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
503 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
504 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
505 return Lo;
506 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000507 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000508 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000509 case ISD::ExternalSymbol: {
510 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
511 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
512 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
513 }
514
515 case ISD::SDIV:
516 case ISD::UDIV:
517 case ISD::UREM:
518 case ISD::SREM:
519 if (MVT::isInteger(Op.getValueType())) {
520 const char* opstr = 0;
521 switch(Op.getOpcode()) {
522 case ISD::UREM: opstr = "__remqu"; break;
523 case ISD::SREM: opstr = "__remq"; break;
524 case ISD::UDIV: opstr = "__divqu"; break;
525 case ISD::SDIV: opstr = "__divq"; break;
526 }
527 SDOperand Tmp1 = Op.getOperand(0),
528 Tmp2 = Op.getOperand(1),
529 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
530 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
531 }
532 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000533
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000534 case ISD::LOAD:
535 case ISD::SEXTLOAD:
536 case ISD::ZEXTLOAD:
Andrew Lenharth87076052006-01-23 21:23:26 +0000537 case ISD::EXTLOAD:
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000538 {
539 SDOperand Chain = Op.getOperand(0);
540 SDOperand Address = Op.getOperand(1);
541
542 unsigned Opc;
543 unsigned opcode = Op.getOpcode();
544
545 if (opcode == ISD::LOAD)
546 switch (Op.Val->getValueType(0)) {
547 default: Op.Val->dump(); assert(0 && "Bad load!");
548 case MVT::i64: Opc = AlphaISD::LDQ_; break;
549 case MVT::f64: Opc = AlphaISD::LDT_; break;
550 case MVT::f32: Opc = AlphaISD::LDS_; break;
551 }
552 else
553 switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
554 default: Op.Val->dump(); assert(0 && "Bad sign extend!");
555 case MVT::i32: Opc = AlphaISD::LDL_;
556 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
557 case MVT::i16: Opc = AlphaISD::LDWU_;
558 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
559 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
560 case MVT::i8: Opc = AlphaISD::LDBU_;
561 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
562 }
563
564 int i, j, k;
565 getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
566
567 SDOperand Zero = DAG.getConstant(0, MVT::i64);
568 std::vector<MVT::ValueType> VTS;
569 VTS.push_back(Op.Val->getValueType(0));
570 VTS.push_back(MVT::Other);
571 std::vector<SDOperand> ARGS;
Andrew Lenharth87076052006-01-23 21:23:26 +0000572 ARGS.push_back(Chain);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000573 ARGS.push_back(Zero);
574 ARGS.push_back(Address);
575 ARGS.push_back(DAG.getConstant(i, MVT::i64));
576 ARGS.push_back(DAG.getConstant(j, MVT::i64));
577 ARGS.push_back(DAG.getConstant(k, MVT::i64));
578 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000579 return DAG.getNode(Opc, VTS, ARGS);
580 }
581
Andrew Lenharth66e49582006-01-23 21:51:33 +0000582 case ISD::TRUNCSTORE:
583 case ISD::STORE:
584 {
585 SDOperand Chain = Op.getOperand(0);
586 SDOperand Value = Op.getOperand(1);
587 SDOperand Address = Op.getOperand(2);
588
589 unsigned Opc;
590 unsigned opcode = Op.getOpcode();
591
592 if (opcode == ISD::STORE) {
593 switch(Value.getValueType()) {
594 default: assert(0 && "unknown Type in store");
595 case MVT::i64: Opc = AlphaISD::STQ_; break;
596 case MVT::f64: Opc = AlphaISD::STT_; break;
597 case MVT::f32: Opc = AlphaISD::STS_; break;
598 }
599 } else { //ISD::TRUNCSTORE
600 switch(cast<VTSDNode>(Op.getOperand(4))->getVT()) {
601 default: assert(0 && "unknown Type in store");
602 case MVT::i8: Opc = AlphaISD::STB_; break;
603 case MVT::i16: Opc = AlphaISD::STW_; break;
604 case MVT::i32: Opc = AlphaISD::STL_; break;
605 }
606 }
607
608 int i, j, k;
609 getValueInfo(cast<SrcValueSDNode>(Op.getOperand(3))->getValue(), i, j, k);
610
611 SDOperand Zero = DAG.getConstant(0, MVT::i64);
612 std::vector<MVT::ValueType> VTS;
613 VTS.push_back(MVT::Other);
614 std::vector<SDOperand> ARGS;
615 ARGS.push_back(Chain);
616 ARGS.push_back(Value);
617 ARGS.push_back(Zero);
618 ARGS.push_back(Address);
619 ARGS.push_back(DAG.getConstant(i, MVT::i64));
620 ARGS.push_back(DAG.getConstant(j, MVT::i64));
621 ARGS.push_back(DAG.getConstant(k, MVT::i64));
622 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
623 return DAG.getNode(Opc, VTS, ARGS);
624 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000625 case ISD::VAARG: {
626 SDOperand Chain = Op.getOperand(0);
627 SDOperand VAListP = Op.getOperand(1);
628 SDOperand VAListS = Op.getOperand(2);
629
630 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP, VAListS);
631 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
632 DAG.getConstant(8, MVT::i64));
633 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
634 Tmp, DAG.getSrcValue(0), MVT::i32);
635 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
636 if (MVT::isFloatingPoint(Op.getValueType()))
637 {
638 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
639 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
640 DAG.getConstant(8*6, MVT::i64));
641 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
642 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
643 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
644 }
Andrew Lenharth66e49582006-01-23 21:51:33 +0000645
Nate Begemanacc398c2006-01-25 18:21:52 +0000646 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
647 DAG.getConstant(8, MVT::i64));
648 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
649 Offset.getValue(1), NewOffset,
650 Tmp, DAG.getSrcValue(0),
651 DAG.getValueType(MVT::i32));
652
653 SDOperand Result;
654 if (Op.getValueType() == MVT::i32)
655 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Update, DataPtr,
656 DAG.getSrcValue(0), MVT::i32);
657 else
658 Result = DAG.getLoad(Op.getValueType(), Update, DataPtr,
659 DAG.getSrcValue(0));
660 return Result;
661 }
662 case ISD::VACOPY: {
663 SDOperand Chain = Op.getOperand(0);
664 SDOperand DestP = Op.getOperand(1);
665 SDOperand SrcP = Op.getOperand(2);
666 SDOperand DestS = Op.getOperand(3);
667 SDOperand SrcS = Op.getOperand(4);
668
669 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP, SrcS);
670 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val,
671 DestP, DestS);
672 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
673 DAG.getConstant(8, MVT::i64));
674 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
675 DAG.getSrcValue(0), MVT::i32);
676 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
677 DAG.getConstant(8, MVT::i64));
678 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
679 Val, NPD, DAG.getSrcValue(0),DAG.getValueType(MVT::i32));
680 }
681 case ISD::VASTART: {
682 SDOperand Chain = Op.getOperand(0);
683 SDOperand VAListP = Op.getOperand(1);
684 SDOperand VAListS = Op.getOperand(2);
685
686 // vastart stores the address of the VarArgsBase and VarArgsOffset
687 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
688 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
689 VAListS);
690 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
691 DAG.getConstant(8, MVT::i64));
692 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
693 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
694 DAG.getSrcValue(0), DAG.getValueType(MVT::i32));
695 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000696 }
697
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000698 return SDOperand();
699}
Nate Begeman0aed7842006-01-28 03:14:31 +0000700
701SDOperand AlphaTargetLowering::CustomPromoteOperation(SDOperand Op,
702 SelectionDAG &DAG) {
703 assert(Op.getValueType() == MVT::i32 &&
704 Op.getOpcode() == ISD::VAARG &&
705 "Unknown node to custom promote!");
706
707 // The code in LowerOperation already handles i32 vaarg
708 return LowerOperation(Op, DAG);
709}