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);
76 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
77 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
79 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
80 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
81 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
82 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
85 //If this didn't legalize into a div....
86 // setOperationAction(ISD::SREM , MVT::i64, Expand);
87 // setOperationAction(ISD::UREM , MVT::i64, Expand);
89 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
90 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
91 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
93 // We don't support sin/cos/sqrt
94 setOperationAction(ISD::FSIN , MVT::f64, Expand);
95 setOperationAction(ISD::FCOS , MVT::f64, Expand);
96 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
97 setOperationAction(ISD::FSIN , MVT::f32, Expand);
98 setOperationAction(ISD::FCOS , MVT::f32, Expand);
99 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
101 setOperationAction(ISD::SETCC, MVT::f32, Promote);
103 // We don't have line number support yet.
104 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
106 addLegalFPImmediate(+0.0); //F31
107 addLegalFPImmediate(-0.0); //-F31
109 computeRegisterProperties();
111 useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
115 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
117 //For now, just use variable size stack frame format
119 //In a standard call, the first six items are passed in registers $16
120 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
121 //of argument-to-register correspondence.) The remaining items are
122 //collected in a memory argument list that is a naturally aligned
123 //array of quadwords. In a standard call, this list, if present, must
124 //be passed at 0(SP).
125 //7 ... n 0(SP) ... (n-7)*8(SP)
133 std::vector<SDOperand>
134 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
136 MachineFunction &MF = DAG.getMachineFunction();
137 MachineFrameInfo *MFI = MF.getFrameInfo();
138 MachineBasicBlock& BB = MF.front();
139 std::vector<SDOperand> ArgValues;
141 unsigned args_int[] = {
142 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
143 unsigned args_float[] = {
144 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
148 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
149 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
151 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
156 MVT::ValueType VT = getValueType(I->getType());
159 std::cerr << "Unknown Type " << VT << "\n";
163 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
164 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
165 DAG.setRoot(argt.getValue(1));
172 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
173 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
174 DAG.setRoot(argt.getValue(1));
175 if (VT != MVT::i64) {
177 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
178 argt = DAG.getNode(AssertOp, MVT::i64, argt,
179 DAG.getValueType(VT));
180 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
185 // Create the frame index object for this incoming parameter...
186 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
188 // Create the SelectionDAG nodes corresponding to a load
189 //from this parameter
190 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
191 argt = DAG.getLoad(getValueType(I->getType()),
192 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
195 ArgValues.push_back(argt);
198 // If the functions takes variable number of arguments, copy all regs to stack
200 VarArgsOffset = count * 8;
201 std::vector<SDOperand> LS;
202 for (int i = 0; i < 6; ++i) {
203 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
204 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
205 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
206 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
207 if (i == 0) VarArgsBase = FI;
208 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
209 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
210 SDFI, DAG.getSrcValue(NULL)));
212 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
213 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
214 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
215 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
216 SDFI = DAG.getFrameIndex(FI, MVT::i64);
217 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
218 SDFI, DAG.getSrcValue(NULL)));
221 //Set up a token factor with all the stack traffic
222 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
225 // Finally, inform the code generator which regs we return values in.
226 switch (getValueType(F.getReturnType())) {
227 default: assert(0 && "Unknown type!");
228 case MVT::isVoid: break;
234 MF.addLiveOut(Alpha::R0);
238 MF.addLiveOut(Alpha::F0);
242 //return the arguments
246 std::pair<SDOperand, SDOperand>
247 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
248 const Type *RetTy, bool isVarArg,
249 unsigned CallingConv, bool isTailCall,
250 SDOperand Callee, ArgListTy &Args,
254 NumBytes = (Args.size() - 6) * 8;
256 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
257 DAG.getConstant(NumBytes, getPointerTy()));
258 std::vector<SDOperand> args_to_use;
259 for (unsigned i = 0, e = Args.size(); i != e; ++i)
261 switch (getValueType(Args[i].second)) {
262 default: assert(0 && "Unexpected ValueType for argument!");
267 // Promote the integer to 64 bits. If the input type is signed use a
268 // sign extend, otherwise use a zero extend.
269 if (Args[i].second->isSigned())
270 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
272 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
279 args_to_use.push_back(Args[i].first);
282 std::vector<MVT::ValueType> RetVals;
283 MVT::ValueType RetTyVT = getValueType(RetTy);
284 MVT::ValueType ActualRetTyVT = RetTyVT;
285 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
286 ActualRetTyVT = MVT::i64;
288 if (RetTyVT != MVT::isVoid)
289 RetVals.push_back(ActualRetTyVT);
290 RetVals.push_back(MVT::Other);
292 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
293 Chain, Callee, args_to_use), 0);
294 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
295 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
296 DAG.getConstant(NumBytes, getPointerTy()));
297 SDOperand RetVal = TheCall;
299 if (RetTyVT != ActualRetTyVT) {
300 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
301 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
302 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
305 return std::make_pair(RetVal, Chain);
308 SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
309 Value *VAListV, SelectionDAG &DAG) {
310 // vastart stores the address of the VarArgsBase and VarArgsOffset
311 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
312 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
313 DAG.getSrcValue(VAListV));
314 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
315 DAG.getConstant(8, MVT::i64));
316 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
317 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
318 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
321 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
322 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
323 const Type *ArgTy, SelectionDAG &DAG) {
324 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
325 DAG.getSrcValue(VAListV));
326 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
327 DAG.getConstant(8, MVT::i64));
328 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
329 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
330 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
331 if (ArgTy->isFloatingPoint())
333 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
334 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
335 DAG.getConstant(8*6, MVT::i64));
336 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
337 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
338 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
342 if (ArgTy == Type::IntTy)
343 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
344 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
345 else if (ArgTy == Type::UIntTy)
346 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
347 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
349 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
350 DAG.getSrcValue(NULL));
352 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
353 DAG.getConstant(8, MVT::i64));
354 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
355 Result.getValue(1), NewOffset,
356 Tmp, DAG.getSrcValue(VAListV, 8),
357 DAG.getValueType(MVT::i32));
358 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
360 return std::make_pair(Result, Update);
364 SDOperand AlphaTargetLowering::
365 LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
366 Value *DestV, SelectionDAG &DAG) {
367 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
368 DAG.getSrcValue(SrcV));
369 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
370 Val, DestP, DAG.getSrcValue(DestV));
371 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
372 DAG.getConstant(8, MVT::i64));
373 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
374 DAG.getSrcValue(SrcV, 8), MVT::i32);
375 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
376 DAG.getConstant(8, MVT::i64));
377 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
378 Val, NPD, DAG.getSrcValue(DestV, 8),
379 DAG.getValueType(MVT::i32));
382 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
384 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
386 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
388 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
392 /// LowerOperation - Provide custom lowering hooks for some operations.
394 SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
395 switch (Op.getOpcode()) {
396 default: assert(0 && "Wasn't expecting to be able to lower this!");
397 case ISD::SINT_TO_FP: {
398 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
399 "Unhandled SINT_TO_FP type in custom expander!");
401 bool isDouble = MVT::f64 == Op.getValueType();
403 LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
406 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
407 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
408 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
409 Op.getOperand(0), FI, DAG.getSrcValue(0));
410 LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
412 SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
413 isDouble?MVT::f64:MVT::f32, LD);
416 case ISD::FP_TO_SINT: {
417 bool isDouble = MVT::f64 == Op.getOperand(0).getValueType();
418 SDOperand src = Op.getOperand(0);
420 if (!isDouble) //Promote
421 src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, src);
423 src = DAG.getNode(AlphaISD::CVTTQ_, MVT::f64, src);
426 return DAG.getNode(AlphaISD::FTOIT_, MVT::i64, src);
429 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
430 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i64);
431 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
432 src, FI, DAG.getSrcValue(0));
433 return DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));