All sorts of stuff.
[oota-llvm.git] / lib / Target / Alpha / AlphaISelLowering.cpp
1 //===-- 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"
23 #include "llvm/Support/CommandLine.h"
24 #include <iostream>
25
26 using namespace llvm;
27
28 namespace llvm {
29   extern cl::opt<bool> EnableAlphaIDIV;
30   extern cl::opt<bool> EnableAlphaCount;
31   extern cl::opt<bool> EnableAlphaLSMark;
32 }
33
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
36 /// register for it.
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);
42   return VReg;
43 }
44
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);
51   
52   addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
53   addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
54   addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
55   
56   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
57   setOperationAction(ISD::BRTWOWAY_CC,  MVT::Other, Expand);
58   
59   setOperationAction(ISD::EXTLOAD, MVT::i1,  Promote);
60   setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
61   
62   setOperationAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
63   setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
64   
65   setOperationAction(ISD::SEXTLOAD, MVT::i1,  Promote);
66   setOperationAction(ISD::SEXTLOAD, MVT::i8,  Expand);
67   setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
68   
69   setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
70
71   setOperationAction(ISD::FREM, MVT::f32, Expand);
72   setOperationAction(ISD::FREM, MVT::f64, Expand);
73   
74   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
75   setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
76   
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);
81   }
82   
83   //If this didn't legalize into a div....
84   //      setOperationAction(ISD::SREM     , MVT::i64, Expand);
85   //      setOperationAction(ISD::UREM     , MVT::i64, Expand);
86   
87   setOperationAction(ISD::MEMMOVE  , MVT::Other, Expand);
88   setOperationAction(ISD::MEMSET   , MVT::Other, Expand);
89   setOperationAction(ISD::MEMCPY   , MVT::Other, Expand);
90   
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);
98
99   //Doesn't work yet
100   setOperationAction(ISD::SETCC, MVT::f32,   Promote);
101
102   // We don't have line number support yet.
103   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
104   
105   addLegalFPImmediate(+0.0); //F31
106   addLegalFPImmediate(-0.0); //-F31
107
108   computeRegisterProperties();
109
110   useITOF = TM.getSubtarget<AlphaSubtarget>().hasF2I();
111 }
112
113
114 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
115
116 //For now, just use variable size stack frame format
117
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)
125
126 // //#define FP    $15
127 // //#define RA    $26
128 // //#define PV    $27
129 // //#define GP    $29
130 // //#define SP    $30
131
132 std::vector<SDOperand>
133 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
134 {
135   MachineFunction &MF = DAG.getMachineFunction();
136   MachineFrameInfo *MFI = MF.getFrameInfo();
137   MachineBasicBlock& BB = MF.front();
138   std::vector<SDOperand> ArgValues;
139
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};
144
145   int count = 0;
146
147   GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
148   RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
149
150   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
151   {
152     SDOperand argt;
153     if (count  < 6) {
154       unsigned Vreg;
155       MVT::ValueType VT = getValueType(I->getType());
156       switch (VT) {
157       default:
158         std::cerr << "Unknown Type " << VT << "\n";
159         abort();
160       case MVT::f64:
161       case MVT::f32:
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));
165         break;
166       case MVT::i1:
167       case MVT::i8:
168       case MVT::i16:
169       case MVT::i32:
170       case MVT::i64:
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) {
175           unsigned AssertOp = 
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);
180         }
181         break;
182       }
183     } else { //more args
184       // Create the frame index object for this incoming parameter...
185       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
186
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));
192     }
193     ++count;
194     ArgValues.push_back(argt);
195   }
196
197   // If the functions takes variable number of arguments, copy all regs to stack
198   if (F.isVarArg()) {
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)));
210
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)));
218     }
219
220     //Set up a token factor with all the stack traffic
221     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
222   }
223
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;
228   case MVT::i1:
229   case MVT::i8:
230   case MVT::i16:
231   case MVT::i32:
232   case MVT::i64:
233     MF.addLiveOut(Alpha::R0);
234     break;
235   case MVT::f32:
236   case MVT::f64:
237     MF.addLiveOut(Alpha::F0);
238     break;
239   }
240
241   //return the arguments
242   return ArgValues;
243 }
244
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,
250                                  SelectionDAG &DAG) {
251   int NumBytes = 0;
252   if (Args.size() > 6)
253     NumBytes = (Args.size() - 6) * 8;
254
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)
259   {
260     switch (getValueType(Args[i].second)) {
261     default: assert(0 && "Unexpected ValueType for argument!");
262     case MVT::i1:
263     case MVT::i8:
264     case MVT::i16:
265     case MVT::i32:
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);
270       else
271         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
272       break;
273     case MVT::i64:
274     case MVT::f64:
275     case MVT::f32:
276       break;
277     }
278     args_to_use.push_back(Args[i].first);
279   }
280
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;
286
287   if (RetTyVT != MVT::isVoid)
288     RetVals.push_back(ActualRetTyVT);
289   RetVals.push_back(MVT::Other);
290
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;
297
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);
302   }
303
304   return std::make_pair(RetVal, Chain);
305 }
306
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));
318 }
319
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())
331   {
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);
338   }
339
340   SDOperand Result;
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);
347   else
348     Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
349                          DAG.getSrcValue(NULL));
350
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);
358
359   return std::make_pair(Result, Update);
360 }
361
362
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));
379 }
380
381 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
382 {
383   BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
384 }
385 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
386 {
387   BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
388 }
389
390
391 /// LowerOperation - Provide custom lowering hooks for some operations.
392 ///
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!");
399     SDOperand LD;
400     bool isDouble = MVT::f64 == Op.getValueType();
401     if (useITOF) {
402       LD = DAG.getNode(AlphaISD::ITOFT_, MVT::f64, Op.getOperand(0));
403     } else {
404       int FrameIdx =
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));
410       }
411     SDOperand FP = DAG.getNode(isDouble?AlphaISD::CVTQT_:AlphaISD::CVTQS_,
412                                isDouble?MVT::f64:MVT::f32, LD);
413     return FP;
414   }
415   }
416   return SDOperand();
417 }
418