1 //===-- AlphaISelLowering.cpp - Alpha DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AlphaISelLowering class.
12 //===----------------------------------------------------------------------===//
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"
29 extern cl::opt<bool> EnableAlphaIDIV;
30 extern cl::opt<bool> EnableAlphaCount;
31 extern cl::opt<bool> EnableAlphaLSMark;
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
37 static 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);
45 AlphaTargetLowering::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);
52 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
53 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
54 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
56 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
57 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
59 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
60 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
62 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
63 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
65 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
66 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
67 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
69 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
71 setOperationAction(ISD::FREM, MVT::f32, Expand);
72 setOperationAction(ISD::FREM, MVT::f64, Expand);
74 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
75 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
77 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
78 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
80 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
83 //If this didn't legalize into a div....
84 // setOperationAction(ISD::SREM , MVT::i64, Expand);
85 // setOperationAction(ISD::UREM , MVT::i64, Expand);
87 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
88 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
89 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
91 // We don't support sin/cos/sqrt
92 setOperationAction(ISD::FSIN , MVT::f64, Expand);
93 setOperationAction(ISD::FCOS , MVT::f64, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
95 setOperationAction(ISD::FSIN , MVT::f32, Expand);
96 setOperationAction(ISD::FCOS , MVT::f32, Expand);
97 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
100 setOperationAction(ISD::SETCC, MVT::f32, Promote);
102 // We don't have line number support yet.
103 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
105 addLegalFPImmediate(+0.0); //F31
106 addLegalFPImmediate(-0.0); //-F31
108 computeRegisterProperties();
110 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
114 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
116 //For now, just use variable size stack frame format
118 //In a standard call, the first six items are passed in registers $16
119 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
120 //of argument-to-register correspondence.) The remaining items are
121 //collected in a memory argument list that is a naturally aligned
122 //array of quadwords. In a standard call, this list, if present, must
123 //be passed at 0(SP).
124 //7 ... n 0(SP) ... (n-7)*8(SP)
132 std::vector<SDOperand>
133 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
135 MachineFunction &MF = DAG.getMachineFunction();
136 MachineFrameInfo *MFI = MF.getFrameInfo();
137 MachineBasicBlock& BB = MF.front();
138 std::vector<SDOperand> ArgValues;
140 unsigned args_int[] = {
141 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
142 unsigned args_float[] = {
143 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
147 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
148 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
150 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
155 MVT::ValueType VT = getValueType(I->getType());
158 std::cerr << "Unknown Type " << VT << "\n";
162 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
163 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
164 DAG.setRoot(argt.getValue(1));
171 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
172 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
173 DAG.setRoot(argt.getValue(1));
174 if (VT != MVT::i64) {
176 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
177 argt = DAG.getNode(AssertOp, MVT::i64, argt,
178 DAG.getValueType(VT));
179 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
184 // Create the frame index object for this incoming parameter...
185 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
187 // Create the SelectionDAG nodes corresponding to a load
188 //from this parameter
189 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
190 argt = DAG.getLoad(getValueType(I->getType()),
191 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
194 ArgValues.push_back(argt);
197 // If the functions takes variable number of arguments, copy all regs to stack
199 VarArgsOffset = count * 8;
200 std::vector<SDOperand> LS;
201 for (int i = 0; i < 6; ++i) {
202 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
203 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
204 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
205 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
206 if (i == 0) VarArgsBase = FI;
207 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
208 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
209 SDFI, DAG.getSrcValue(NULL)));
211 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
212 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
213 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
214 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
215 SDFI = DAG.getFrameIndex(FI, MVT::i64);
216 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
217 SDFI, DAG.getSrcValue(NULL)));
220 //Set up a token factor with all the stack traffic
221 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
224 // Finally, inform the code generator which regs we return values in.
225 switch (getValueType(F.getReturnType())) {
226 default: assert(0 && "Unknown type!");
227 case MVT::isVoid: break;
233 MF.addLiveOut(Alpha::R0);
237 MF.addLiveOut(Alpha::F0);
241 //return the arguments
245 std::pair<SDOperand, SDOperand>
246 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
247 const Type *RetTy, bool isVarArg,
248 unsigned CallingConv, bool isTailCall,
249 SDOperand Callee, ArgListTy &Args,
253 NumBytes = (Args.size() - 6) * 8;
255 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
256 DAG.getConstant(NumBytes, getPointerTy()));
257 std::vector<SDOperand> args_to_use;
258 for (unsigned i = 0, e = Args.size(); i != e; ++i)
260 switch (getValueType(Args[i].second)) {
261 default: assert(0 && "Unexpected ValueType for argument!");
266 // Promote the integer to 64 bits. If the input type is signed use a
267 // sign extend, otherwise use a zero extend.
268 if (Args[i].second->isSigned())
269 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
271 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
278 args_to_use.push_back(Args[i].first);
281 std::vector<MVT::ValueType> RetVals;
282 MVT::ValueType RetTyVT = getValueType(RetTy);
283 MVT::ValueType ActualRetTyVT = RetTyVT;
284 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
285 ActualRetTyVT = MVT::i64;
287 if (RetTyVT != MVT::isVoid)
288 RetVals.push_back(ActualRetTyVT);
289 RetVals.push_back(MVT::Other);
291 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
292 Chain, Callee, args_to_use), 0);
293 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
294 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
295 DAG.getConstant(NumBytes, getPointerTy()));
296 SDOperand RetVal = TheCall;
298 if (RetTyVT != ActualRetTyVT) {
299 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
300 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
301 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
304 return std::make_pair(RetVal, Chain);
307 SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
308 Value *VAListV, SelectionDAG &DAG) {
309 // vastart stores the address of the VarArgsBase and VarArgsOffset
310 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
311 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
312 DAG.getSrcValue(VAListV));
313 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
314 DAG.getConstant(8, MVT::i64));
315 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
316 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
317 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
320 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
321 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
322 const Type *ArgTy, SelectionDAG &DAG) {
323 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
324 DAG.getSrcValue(VAListV));
325 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
326 DAG.getConstant(8, MVT::i64));
327 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
328 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
329 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
330 if (ArgTy->isFloatingPoint())
332 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
333 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
334 DAG.getConstant(8*6, MVT::i64));
335 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
336 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
337 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
341 if (ArgTy == Type::IntTy)
342 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
343 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
344 else if (ArgTy == Type::UIntTy)
345 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
346 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
348 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
349 DAG.getSrcValue(NULL));
351 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
352 DAG.getConstant(8, MVT::i64));
353 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
354 Result.getValue(1), NewOffset,
355 Tmp, DAG.getSrcValue(VAListV, 8),
356 DAG.getValueType(MVT::i32));
357 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
359 return std::make_pair(Result, Update);
363 SDOperand AlphaTargetLowering::
364 LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
365 Value *DestV, SelectionDAG &DAG) {
366 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
367 DAG.getSrcValue(SrcV));
368 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
369 Val, DestP, DAG.getSrcValue(DestV));
370 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
371 DAG.getConstant(8, MVT::i64));
372 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
373 DAG.getSrcValue(SrcV, 8), MVT::i32);
374 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
375 DAG.getConstant(8, MVT::i64));
376 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
377 Val, NPD, DAG.getSrcValue(DestV, 8),
378 DAG.getValueType(MVT::i32));
381 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
383 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
385 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
387 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
391 /// LowerOperation - Provide custom lowering hooks for some operations.
393 SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
394 switch (Op.getOpcode()) {
395 default: assert(0 && "Wasn't expecting to be able to lower this!");
396 case ISD::SINT_TO_FP: {
397 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
398 "Unhandled SINT_TO_FP type in custom expander!");
400 bool isDouble = MVT::f64 == Op.getValueType();
402 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
405 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
406 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
407 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
408 Op.getOperand(0), FI, DAG.getSrcValue(0));
409 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
411 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
412 isDouble?MVT::f64:MVT::f32, LD);