blob: 6eaf88f36d717645d2195b5a0559ccfcd23b46ce [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);
Chris Lattner9601a862006-03-05 05:08:37 +0000122
123 // FIXME: Alpha supports fcopysign natively!?
124 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
125 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000126
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000127 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000128
129 // We don't have line number support yet.
130 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000131 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
132 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000133
134 // Not implemented yet.
135 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
136 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Andrew Lenharth739027e2006-01-16 21:22:38 +0000137 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
138
Andrew Lenharth53d89702005-12-25 01:34:27 +0000139 // We want to legalize GlobalAddress and ConstantPool and
140 // ExternalSymbols nodes into the appropriate instructions to
141 // materialize the address.
142 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
143 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
144 setOperationAction(ISD::ExternalSymbol, MVT::i64, Custom);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000145
Andrew Lenharth0e538792006-01-25 21:54:38 +0000146 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Andrew Lenharth677c4f22006-01-25 23:33:32 +0000147 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000148 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
Andrew Lenharth5f8f0e22006-01-25 22:28:07 +0000149 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Nate Begeman0aed7842006-01-28 03:14:31 +0000150 setOperationAction(ISD::VAARG, MVT::i32, Custom);
Andrew Lenharth0e538792006-01-25 21:54:38 +0000151
Andrew Lenharth739027e2006-01-16 21:22:38 +0000152 setStackPointerRegisterToSaveRestore(Alpha::R30);
153
Chris Lattner08a90222006-01-29 06:25:22 +0000154 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
155 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000156 addLegalFPImmediate(+0.0); //F31
157 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000158
159 computeRegisterProperties();
160
161 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000162}
163
Andrew Lenharth84a06052006-01-16 19:53:25 +0000164const char *AlphaTargetLowering::getTargetNodeName(unsigned Opcode) const {
165 switch (Opcode) {
166 default: return 0;
167 case AlphaISD::ITOFT_: return "Alpha::ITOFT_";
168 case AlphaISD::FTOIT_: return "Alpha::FTOIT_";
169 case AlphaISD::CVTQT_: return "Alpha::CVTQT_";
170 case AlphaISD::CVTQS_: return "Alpha::CVTQS_";
171 case AlphaISD::CVTTQ_: return "Alpha::CVTTQ_";
172 case AlphaISD::GPRelHi: return "Alpha::GPRelHi";
173 case AlphaISD::GPRelLo: return "Alpha::GPRelLo";
174 case AlphaISD::RelLit: return "Alpha::RelLit";
175 case AlphaISD::GlobalBaseReg: return "Alpha::GlobalBaseReg";
Chris Lattner2d90bd52006-01-27 23:39:00 +0000176 case AlphaISD::CALL: return "Alpha::CALL";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000177 case AlphaISD::DivCall: return "Alpha::DivCall";
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000178 case AlphaISD::LDQ_: return "Alpha::LDQ_";
179 case AlphaISD::LDT_: return "Alpha::LDT_";
180 case AlphaISD::LDS_: return "Alpha::LDS_";
181 case AlphaISD::LDL_: return "Alpha::LDL_";
182 case AlphaISD::LDWU_: return "Alpha::LDWU_";
183 case AlphaISD::LDBU_: return "Alpha::LDBU_";
Andrew Lenharth66e49582006-01-23 21:51:33 +0000184 case AlphaISD::STQ_: return "Alpha::STQ_";
185 case AlphaISD::STT_: return "Alpha::STT_";
186 case AlphaISD::STS_: return "Alpha::STS_";
187 case AlphaISD::STL_: return "Alpha::STL_";
188 case AlphaISD::STW_: return "Alpha::STW_";
189 case AlphaISD::STB_: return "Alpha::STB_";
Andrew Lenharth84a06052006-01-16 19:53:25 +0000190 }
191}
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000192
193//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
194
195//For now, just use variable size stack frame format
196
197//In a standard call, the first six items are passed in registers $16
198//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
199//of argument-to-register correspondence.) The remaining items are
200//collected in a memory argument list that is a naturally aligned
201//array of quadwords. In a standard call, this list, if present, must
202//be passed at 0(SP).
203//7 ... n 0(SP) ... (n-7)*8(SP)
204
205// //#define FP $15
206// //#define RA $26
207// //#define PV $27
208// //#define GP $29
209// //#define SP $30
210
211std::vector<SDOperand>
212AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
213{
214 MachineFunction &MF = DAG.getMachineFunction();
215 MachineFrameInfo *MFI = MF.getFrameInfo();
216 MachineBasicBlock& BB = MF.front();
217 std::vector<SDOperand> ArgValues;
218
Andrew Lenharthf71df332005-09-04 06:12:19 +0000219 unsigned args_int[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000220 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
Andrew Lenharthf71df332005-09-04 06:12:19 +0000221 unsigned args_float[] = {
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000222 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000223
224 int count = 0;
225
226 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
227 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
228
229 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
230 {
231 SDOperand argt;
232 if (count < 6) {
233 unsigned Vreg;
234 MVT::ValueType VT = getValueType(I->getType());
235 switch (VT) {
236 default:
237 std::cerr << "Unknown Type " << VT << "\n";
238 abort();
239 case MVT::f64:
240 case MVT::f32:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000241 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000242 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
243 DAG.setRoot(argt.getValue(1));
244 break;
245 case MVT::i1:
246 case MVT::i8:
247 case MVT::i16:
248 case MVT::i32:
249 case MVT::i64:
Andrew Lenharthf71df332005-09-04 06:12:19 +0000250 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000251 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
252 DAG.setRoot(argt.getValue(1));
253 if (VT != MVT::i64) {
254 unsigned AssertOp =
255 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
256 argt = DAG.getNode(AssertOp, MVT::i64, argt,
257 DAG.getValueType(VT));
258 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
259 }
260 break;
261 }
262 } else { //more args
263 // Create the frame index object for this incoming parameter...
264 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
265
266 // Create the SelectionDAG nodes corresponding to a load
267 //from this parameter
268 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
269 argt = DAG.getLoad(getValueType(I->getType()),
270 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
271 }
272 ++count;
273 ArgValues.push_back(argt);
274 }
275
276 // If the functions takes variable number of arguments, copy all regs to stack
277 if (F.isVarArg()) {
278 VarArgsOffset = count * 8;
279 std::vector<SDOperand> LS;
280 for (int i = 0; i < 6; ++i) {
Chris Lattnerf2cded72005-09-13 19:03:13 +0000281 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000282 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000283 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
284 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
285 if (i == 0) VarArgsBase = FI;
286 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
287 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
288 SDFI, DAG.getSrcValue(NULL)));
289
Chris Lattnerf2cded72005-09-13 19:03:13 +0000290 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
Andrew Lenharthf71df332005-09-04 06:12:19 +0000291 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000292 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
293 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
294 SDFI = DAG.getFrameIndex(FI, MVT::i64);
295 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
296 SDFI, DAG.getSrcValue(NULL)));
297 }
298
299 //Set up a token factor with all the stack traffic
300 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
301 }
302
303 // Finally, inform the code generator which regs we return values in.
304 switch (getValueType(F.getReturnType())) {
305 default: assert(0 && "Unknown type!");
306 case MVT::isVoid: break;
307 case MVT::i1:
308 case MVT::i8:
309 case MVT::i16:
310 case MVT::i32:
311 case MVT::i64:
312 MF.addLiveOut(Alpha::R0);
313 break;
314 case MVT::f32:
315 case MVT::f64:
316 MF.addLiveOut(Alpha::F0);
317 break;
318 }
319
320 //return the arguments
321 return ArgValues;
322}
323
324std::pair<SDOperand, SDOperand>
325AlphaTargetLowering::LowerCallTo(SDOperand Chain,
326 const Type *RetTy, bool isVarArg,
327 unsigned CallingConv, bool isTailCall,
328 SDOperand Callee, ArgListTy &Args,
329 SelectionDAG &DAG) {
330 int NumBytes = 0;
331 if (Args.size() > 6)
332 NumBytes = (Args.size() - 6) * 8;
333
Chris Lattner94dd2922006-02-13 09:00:43 +0000334 Chain = DAG.getCALLSEQ_START(Chain,
335 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000336 std::vector<SDOperand> args_to_use;
337 for (unsigned i = 0, e = Args.size(); i != e; ++i)
338 {
339 switch (getValueType(Args[i].second)) {
340 default: assert(0 && "Unexpected ValueType for argument!");
341 case MVT::i1:
342 case MVT::i8:
343 case MVT::i16:
344 case MVT::i32:
345 // Promote the integer to 64 bits. If the input type is signed use a
346 // sign extend, otherwise use a zero extend.
347 if (Args[i].second->isSigned())
348 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
349 else
350 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
351 break;
352 case MVT::i64:
353 case MVT::f64:
354 case MVT::f32:
355 break;
356 }
357 args_to_use.push_back(Args[i].first);
358 }
359
360 std::vector<MVT::ValueType> RetVals;
361 MVT::ValueType RetTyVT = getValueType(RetTy);
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000362 MVT::ValueType ActualRetTyVT = RetTyVT;
363 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
364 ActualRetTyVT = MVT::i64;
365
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000366 if (RetTyVT != MVT::isVoid)
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000367 RetVals.push_back(ActualRetTyVT);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000368 RetVals.push_back(MVT::Other);
369
Chris Lattner2d90bd52006-01-27 23:39:00 +0000370 std::vector<SDOperand> Ops;
371 Ops.push_back(Chain);
372 Ops.push_back(Callee);
373 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
374 SDOperand TheCall = DAG.getNode(AlphaISD::CALL, RetVals, Ops);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000375 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
376 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
377 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000378 SDOperand RetVal = TheCall;
379
380 if (RetTyVT != ActualRetTyVT) {
381 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
382 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
383 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
384 }
385
386 return std::make_pair(RetVal, Chain);
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000387}
388
Andrew Lenharthaa38ce42005-09-02 18:46:02 +0000389void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
390{
391 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
392}
393void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
394{
395 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
396}
397
398
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000399
400static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
401{
402 fun = type = offset = 0;
403 if (v == NULL) {
404 type = 0;
405 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
406 type = 1;
407 const Module* M = GV->getParent();
408 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
409 ++offset;
410 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
411 type = 2;
412 const Function* F = Arg->getParent();
413 const Module* M = F->getParent();
414 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
415 ++fun;
416 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
417 ++offset;
418 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
419 assert(dyn_cast<PointerType>(I->getType()));
420 type = 3;
421 const BasicBlock* bb = I->getParent();
422 const Function* F = bb->getParent();
423 const Module* M = F->getParent();
424 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
425 ++fun;
426 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
427 offset += ii->size();
428 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
429 ++offset;
430 } else if (const Constant* C = dyn_cast<Constant>(v)) {
431 //Don't know how to look these up yet
432 type = 0;
433 } else {
434 assert(0 && "Error in value marking");
435 }
436 //type = 4: register spilling
437 //type = 5: global address loading or constant loading
438}
439
440static int getUID()
441{
442 static int id = 0;
443 return ++id;
444}
445
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000446/// LowerOperation - Provide custom lowering hooks for some operations.
447///
448SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
449 switch (Op.getOpcode()) {
450 default: assert(0 && "Wasn't expecting to be able to lower this!");
451 case ISD::SINT_TO_FP: {
452 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
453 "Unhandled SINT_TO_FP type in custom expander!");
454 SDOperand LD;
455 bool isDouble = MVT::f64 == Op.getValueType();
456 if (useITOF) {
457 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
458 } else {
459 int FrameIdx =
460 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
461 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
462 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
463 Op.getOperand(0), FI, DAG.getSrcValue(0));
464 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
465 }
466 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
467 isDouble?MVT::f64:MVT::f32, LD);
468 return FP;
469 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000470 case ISD::FP_TO_SINT: {
471 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
472 SDOperand src = Op.getOperand(0);
473
474 if (!isDouble) //Promote
475 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
476
477 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
478
479 if (useITOF) {
480 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
481 } else {
482 int FrameIdx =
483 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
484 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
485 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
486 src, FI, DAG.getSrcValue(0));
487 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
488 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000489 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000490 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000491 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
492 Constant *C = CP->get();
493 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
Andrew Lenharth4e629512005-12-24 05:36:33 +0000494
495 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, CPI,
496 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
497 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, CPI, Hi);
498 return Lo;
499 }
500 case ISD::GlobalAddress: {
501 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
502 GlobalValue *GV = GSDN->getGlobal();
503 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i64, GSDN->getOffset());
504
505 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
506 SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi, MVT::i64, GA,
507 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
508 SDOperand Lo = DAG.getNode(AlphaISD::GPRelLo, MVT::i64, GA, Hi);
509 return Lo;
510 } else
Andrew Lenharthc687b482005-12-24 08:29:32 +0000511 return DAG.getNode(AlphaISD::RelLit, MVT::i64, GA, DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000512 }
Andrew Lenharth53d89702005-12-25 01:34:27 +0000513 case ISD::ExternalSymbol: {
514 return DAG.getNode(AlphaISD::RelLit, MVT::i64,
515 DAG.getTargetExternalSymbol(cast<ExternalSymbolSDNode>(Op)->getSymbol(), MVT::i64),
516 DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
517 }
518
519 case ISD::SDIV:
520 case ISD::UDIV:
521 case ISD::UREM:
522 case ISD::SREM:
523 if (MVT::isInteger(Op.getValueType())) {
524 const char* opstr = 0;
525 switch(Op.getOpcode()) {
526 case ISD::UREM: opstr = "__remqu"; break;
527 case ISD::SREM: opstr = "__remq"; break;
528 case ISD::UDIV: opstr = "__divqu"; break;
529 case ISD::SDIV: opstr = "__divq"; break;
530 }
531 SDOperand Tmp1 = Op.getOperand(0),
532 Tmp2 = Op.getOperand(1),
533 Addr = DAG.getExternalSymbol(opstr, MVT::i64);
534 return DAG.getNode(AlphaISD::DivCall, MVT::i64, Addr, Tmp1, Tmp2);
535 }
536 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000537
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000538 case ISD::LOAD:
539 case ISD::SEXTLOAD:
540 case ISD::ZEXTLOAD:
Andrew Lenharth87076052006-01-23 21:23:26 +0000541 case ISD::EXTLOAD:
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000542 {
543 SDOperand Chain = Op.getOperand(0);
544 SDOperand Address = Op.getOperand(1);
545
546 unsigned Opc;
547 unsigned opcode = Op.getOpcode();
548
549 if (opcode == ISD::LOAD)
550 switch (Op.Val->getValueType(0)) {
551 default: Op.Val->dump(); assert(0 && "Bad load!");
552 case MVT::i64: Opc = AlphaISD::LDQ_; break;
553 case MVT::f64: Opc = AlphaISD::LDT_; break;
554 case MVT::f32: Opc = AlphaISD::LDS_; break;
555 }
556 else
557 switch (cast<VTSDNode>(Op.getOperand(3))->getVT()) {
558 default: Op.Val->dump(); assert(0 && "Bad sign extend!");
559 case MVT::i32: Opc = AlphaISD::LDL_;
560 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
561 case MVT::i16: Opc = AlphaISD::LDWU_;
562 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
563 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
564 case MVT::i8: Opc = AlphaISD::LDBU_;
565 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
566 }
567
568 int i, j, k;
569 getValueInfo(dyn_cast<SrcValueSDNode>(Op.getOperand(2))->getValue(), i, j, k);
570
571 SDOperand Zero = DAG.getConstant(0, MVT::i64);
572 std::vector<MVT::ValueType> VTS;
573 VTS.push_back(Op.Val->getValueType(0));
574 VTS.push_back(MVT::Other);
575 std::vector<SDOperand> ARGS;
Andrew Lenharth87076052006-01-23 21:23:26 +0000576 ARGS.push_back(Chain);
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000577 ARGS.push_back(Zero);
578 ARGS.push_back(Address);
579 ARGS.push_back(DAG.getConstant(i, MVT::i64));
580 ARGS.push_back(DAG.getConstant(j, MVT::i64));
581 ARGS.push_back(DAG.getConstant(k, MVT::i64));
582 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
Andrew Lenharth167bc6e2006-01-23 20:59:50 +0000583 return DAG.getNode(Opc, VTS, ARGS);
584 }
585
Andrew Lenharth66e49582006-01-23 21:51:33 +0000586 case ISD::TRUNCSTORE:
587 case ISD::STORE:
588 {
589 SDOperand Chain = Op.getOperand(0);
590 SDOperand Value = Op.getOperand(1);
591 SDOperand Address = Op.getOperand(2);
592
593 unsigned Opc;
594 unsigned opcode = Op.getOpcode();
595
596 if (opcode == ISD::STORE) {
597 switch(Value.getValueType()) {
598 default: assert(0 && "unknown Type in store");
599 case MVT::i64: Opc = AlphaISD::STQ_; break;
600 case MVT::f64: Opc = AlphaISD::STT_; break;
601 case MVT::f32: Opc = AlphaISD::STS_; break;
602 }
603 } else { //ISD::TRUNCSTORE
604 switch(cast<VTSDNode>(Op.getOperand(4))->getVT()) {
605 default: assert(0 && "unknown Type in store");
606 case MVT::i8: Opc = AlphaISD::STB_; break;
607 case MVT::i16: Opc = AlphaISD::STW_; break;
608 case MVT::i32: Opc = AlphaISD::STL_; break;
609 }
610 }
611
612 int i, j, k;
613 getValueInfo(cast<SrcValueSDNode>(Op.getOperand(3))->getValue(), i, j, k);
614
615 SDOperand Zero = DAG.getConstant(0, MVT::i64);
616 std::vector<MVT::ValueType> VTS;
617 VTS.push_back(MVT::Other);
618 std::vector<SDOperand> ARGS;
619 ARGS.push_back(Chain);
620 ARGS.push_back(Value);
621 ARGS.push_back(Zero);
622 ARGS.push_back(Address);
623 ARGS.push_back(DAG.getConstant(i, MVT::i64));
624 ARGS.push_back(DAG.getConstant(j, MVT::i64));
625 ARGS.push_back(DAG.getConstant(k, MVT::i64));
626 ARGS.push_back(DAG.getConstant(getUID(), MVT::i64));
627 return DAG.getNode(Opc, VTS, ARGS);
628 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000629 case ISD::VAARG: {
630 SDOperand Chain = Op.getOperand(0);
631 SDOperand VAListP = Op.getOperand(1);
632 SDOperand VAListS = Op.getOperand(2);
633
634 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP, VAListS);
635 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
636 DAG.getConstant(8, MVT::i64));
637 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
638 Tmp, DAG.getSrcValue(0), MVT::i32);
639 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
640 if (MVT::isFloatingPoint(Op.getValueType()))
641 {
642 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
643 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
644 DAG.getConstant(8*6, MVT::i64));
645 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
646 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
647 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
648 }
Andrew Lenharth66e49582006-01-23 21:51:33 +0000649
Nate Begemanacc398c2006-01-25 18:21:52 +0000650 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
651 DAG.getConstant(8, MVT::i64));
652 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
653 Offset.getValue(1), NewOffset,
654 Tmp, DAG.getSrcValue(0),
655 DAG.getValueType(MVT::i32));
656
657 SDOperand Result;
658 if (Op.getValueType() == MVT::i32)
659 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Update, DataPtr,
660 DAG.getSrcValue(0), MVT::i32);
661 else
662 Result = DAG.getLoad(Op.getValueType(), Update, DataPtr,
663 DAG.getSrcValue(0));
664 return Result;
665 }
666 case ISD::VACOPY: {
667 SDOperand Chain = Op.getOperand(0);
668 SDOperand DestP = Op.getOperand(1);
669 SDOperand SrcP = Op.getOperand(2);
670 SDOperand DestS = Op.getOperand(3);
671 SDOperand SrcS = Op.getOperand(4);
672
673 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP, SrcS);
674 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val,
675 DestP, DestS);
676 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
677 DAG.getConstant(8, MVT::i64));
678 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
679 DAG.getSrcValue(0), MVT::i32);
680 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
681 DAG.getConstant(8, MVT::i64));
682 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
683 Val, NPD, DAG.getSrcValue(0),DAG.getValueType(MVT::i32));
684 }
685 case ISD::VASTART: {
686 SDOperand Chain = Op.getOperand(0);
687 SDOperand VAListP = Op.getOperand(1);
688 SDOperand VAListS = Op.getOperand(2);
689
690 // vastart stores the address of the VarArgsBase and VarArgsOffset
691 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
692 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
693 VAListS);
694 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
695 DAG.getConstant(8, MVT::i64));
696 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
697 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
698 DAG.getSrcValue(0), DAG.getValueType(MVT::i32));
699 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000700 }
701
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000702 return SDOperand();
703}
Nate Begeman0aed7842006-01-28 03:14:31 +0000704
705SDOperand AlphaTargetLowering::CustomPromoteOperation(SDOperand Op,
706 SelectionDAG &DAG) {
707 assert(Op.getValueType() == MVT::i32 &&
708 Op.getOpcode() == ISD::VAARG &&
709 "Unknown node to custom promote!");
710
711 // The code in LowerOperation already handles i32 vaarg
712 return LowerOperation(Op, DAG);
713}