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::FPRCRegisterClass);
54 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
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::FREM, MVT::f32, Expand);
70 setOperationAction(ISD::FREM, MVT::f64, Expand);
72 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
74 if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
75 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
76 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
77 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
80 //If this didn't legalize into a div....
81 // setOperationAction(ISD::SREM , MVT::i64, Expand);
82 // setOperationAction(ISD::UREM , MVT::i64, Expand);
84 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
85 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
86 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
88 // We don't support sin/cos/sqrt
89 setOperationAction(ISD::FSIN , MVT::f64, Expand);
90 setOperationAction(ISD::FCOS , MVT::f64, Expand);
91 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
92 setOperationAction(ISD::FSIN , MVT::f32, Expand);
93 setOperationAction(ISD::FCOS , MVT::f32, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
97 setOperationAction(ISD::SETCC, MVT::f32, Promote);
99 computeRegisterProperties();
101 addLegalFPImmediate(+0.0); //F31
102 addLegalFPImmediate(-0.0); //-F31
106 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
108 //For now, just use variable size stack frame format
110 //In a standard call, the first six items are passed in registers $16
111 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
112 //of argument-to-register correspondence.) The remaining items are
113 //collected in a memory argument list that is a naturally aligned
114 //array of quadwords. In a standard call, this list, if present, must
115 //be passed at 0(SP).
116 //7 ... n 0(SP) ... (n-7)*8(SP)
124 std::vector<SDOperand>
125 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
127 MachineFunction &MF = DAG.getMachineFunction();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
129 MachineBasicBlock& BB = MF.front();
130 std::vector<SDOperand> ArgValues;
132 unsigned args_int[] = {
133 Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
134 unsigned args_float[] = {
135 Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
139 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
140 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
142 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
147 MVT::ValueType VT = getValueType(I->getType());
150 std::cerr << "Unknown Type " << VT << "\n";
154 args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
155 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
156 DAG.setRoot(argt.getValue(1));
163 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
164 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
165 DAG.setRoot(argt.getValue(1));
166 if (VT != MVT::i64) {
168 I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
169 argt = DAG.getNode(AssertOp, MVT::i64, argt,
170 DAG.getValueType(VT));
171 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
176 // Create the frame index object for this incoming parameter...
177 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
179 // Create the SelectionDAG nodes corresponding to a load
180 //from this parameter
181 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
182 argt = DAG.getLoad(getValueType(I->getType()),
183 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
186 ArgValues.push_back(argt);
189 // If the functions takes variable number of arguments, copy all regs to stack
191 VarArgsOffset = count * 8;
192 std::vector<SDOperand> LS;
193 for (int i = 0; i < 6; ++i) {
194 if (MRegisterInfo::isPhysicalRegister(args_int[i]))
195 args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
196 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
197 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
198 if (i == 0) VarArgsBase = FI;
199 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
200 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
201 SDFI, DAG.getSrcValue(NULL)));
203 if (MRegisterInfo::isPhysicalRegister(args_float[i]))
204 args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
205 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
206 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
207 SDFI = DAG.getFrameIndex(FI, MVT::i64);
208 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
209 SDFI, DAG.getSrcValue(NULL)));
212 //Set up a token factor with all the stack traffic
213 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
216 // Finally, inform the code generator which regs we return values in.
217 switch (getValueType(F.getReturnType())) {
218 default: assert(0 && "Unknown type!");
219 case MVT::isVoid: break;
225 MF.addLiveOut(Alpha::R0);
229 MF.addLiveOut(Alpha::F0);
233 //return the arguments
237 std::pair<SDOperand, SDOperand>
238 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
239 const Type *RetTy, bool isVarArg,
240 unsigned CallingConv, bool isTailCall,
241 SDOperand Callee, ArgListTy &Args,
245 NumBytes = (Args.size() - 6) * 8;
247 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
248 DAG.getConstant(NumBytes, getPointerTy()));
249 std::vector<SDOperand> args_to_use;
250 for (unsigned i = 0, e = Args.size(); i != e; ++i)
252 switch (getValueType(Args[i].second)) {
253 default: assert(0 && "Unexpected ValueType for argument!");
258 // Promote the integer to 64 bits. If the input type is signed use a
259 // sign extend, otherwise use a zero extend.
260 if (Args[i].second->isSigned())
261 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
263 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
270 args_to_use.push_back(Args[i].first);
273 std::vector<MVT::ValueType> RetVals;
274 MVT::ValueType RetTyVT = getValueType(RetTy);
275 MVT::ValueType ActualRetTyVT = RetTyVT;
276 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
277 ActualRetTyVT = MVT::i64;
279 if (RetTyVT != MVT::isVoid)
280 RetVals.push_back(ActualRetTyVT);
281 RetVals.push_back(MVT::Other);
283 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
284 Chain, Callee, args_to_use), 0);
285 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
286 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
287 DAG.getConstant(NumBytes, getPointerTy()));
288 SDOperand RetVal = TheCall;
290 if (RetTyVT != ActualRetTyVT) {
291 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
292 MVT::i64, RetVal, DAG.getValueType(RetTyVT));
293 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
296 return std::make_pair(RetVal, Chain);
299 SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
300 Value *VAListV, SelectionDAG &DAG) {
301 // vastart stores the address of the VarArgsBase and VarArgsOffset
302 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
303 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
304 DAG.getSrcValue(VAListV));
305 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
306 DAG.getConstant(8, MVT::i64));
307 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
308 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
309 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
312 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
313 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
314 const Type *ArgTy, SelectionDAG &DAG) {
315 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
316 DAG.getSrcValue(VAListV));
317 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
318 DAG.getConstant(8, MVT::i64));
319 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
320 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
321 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
322 if (ArgTy->isFloatingPoint())
324 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
325 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
326 DAG.getConstant(8*6, MVT::i64));
327 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
328 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
329 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
333 if (ArgTy == Type::IntTy)
334 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
335 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
336 else if (ArgTy == Type::UIntTy)
337 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
338 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
340 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
341 DAG.getSrcValue(NULL));
343 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
344 DAG.getConstant(8, MVT::i64));
345 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
346 Result.getValue(1), NewOffset,
347 Tmp, DAG.getSrcValue(VAListV, 8),
348 DAG.getValueType(MVT::i32));
349 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
351 return std::make_pair(Result, Update);
355 SDOperand AlphaTargetLowering::
356 LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
357 Value *DestV, SelectionDAG &DAG) {
358 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
359 DAG.getSrcValue(SrcV));
360 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
361 Val, DestP, DAG.getSrcValue(DestV));
362 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
363 DAG.getConstant(8, MVT::i64));
364 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
365 DAG.getSrcValue(SrcV, 8), MVT::i32);
366 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
367 DAG.getConstant(8, MVT::i64));
368 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
369 Val, NPD, DAG.getSrcValue(DestV, 8),
370 DAG.getValueType(MVT::i32));
373 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
375 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
377 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
379 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);