This is suppose to work now
[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::FPRCRegisterClass);
54   addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
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   
76   if (!TM.getSubtarget<AlphaSubtarget>().hasCT()) {
77     setOperationAction(ISD::CTPOP    , MVT::i64  , Expand);
78     setOperationAction(ISD::CTTZ     , MVT::i64  , Expand);
79     setOperationAction(ISD::CTLZ     , MVT::i64  , Expand);
80   }
81   
82   //If this didn't legalize into a div....
83   //      setOperationAction(ISD::SREM     , MVT::i64, Expand);
84   //      setOperationAction(ISD::UREM     , MVT::i64, Expand);
85   
86   setOperationAction(ISD::MEMMOVE  , MVT::Other, Expand);
87   setOperationAction(ISD::MEMSET   , MVT::Other, Expand);
88   setOperationAction(ISD::MEMCPY   , MVT::Other, Expand);
89   
90   // We don't support sin/cos/sqrt
91   setOperationAction(ISD::FSIN , MVT::f64, Expand);
92   setOperationAction(ISD::FCOS , MVT::f64, Expand);
93   setOperationAction(ISD::FSQRT, MVT::f64, Expand);
94   setOperationAction(ISD::FSIN , MVT::f32, Expand);
95   setOperationAction(ISD::FCOS , MVT::f32, Expand);
96   setOperationAction(ISD::FSQRT, MVT::f32, Expand);
97
98   //Doesn't work yet
99   setOperationAction(ISD::SETCC, MVT::f32,   Promote);
100   
101   computeRegisterProperties();
102   
103   addLegalFPImmediate(+0.0); //F31
104   addLegalFPImmediate(-0.0); //-F31
105 }
106
107
108 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
109
110 //For now, just use variable size stack frame format
111
112 //In a standard call, the first six items are passed in registers $16
113 //- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
114 //of argument-to-register correspondence.) The remaining items are
115 //collected in a memory argument list that is a naturally aligned
116 //array of quadwords. In a standard call, this list, if present, must
117 //be passed at 0(SP).
118 //7 ... n         0(SP) ... (n-7)*8(SP)
119
120 // //#define FP    $15
121 // //#define RA    $26
122 // //#define PV    $27
123 // //#define GP    $29
124 // //#define SP    $30
125
126 std::vector<SDOperand>
127 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
128 {
129   MachineFunction &MF = DAG.getMachineFunction();
130   MachineFrameInfo *MFI = MF.getFrameInfo();
131   MachineBasicBlock& BB = MF.front();
132   std::vector<SDOperand> ArgValues;
133
134   unsigned args_int[] = {
135     Alpha::R16, Alpha::R17, Alpha::R18, Alpha::R19, Alpha::R20, Alpha::R21};
136   unsigned args_float[] = {
137     Alpha::F16, Alpha::F17, Alpha::F18, Alpha::F19, Alpha::F20, Alpha::F21};
138
139   int count = 0;
140
141   GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
142   RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
143
144   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
145   {
146     SDOperand argt;
147     if (count  < 6) {
148       unsigned Vreg;
149       MVT::ValueType VT = getValueType(I->getType());
150       switch (VT) {
151       default:
152         std::cerr << "Unknown Type " << VT << "\n";
153         abort();
154       case MVT::f64:
155       case MVT::f32:
156         args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
157         argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
158         DAG.setRoot(argt.getValue(1));
159         break;
160       case MVT::i1:
161       case MVT::i8:
162       case MVT::i16:
163       case MVT::i32:
164       case MVT::i64:
165         args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
166         argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
167         DAG.setRoot(argt.getValue(1));
168         if (VT != MVT::i64) {
169           unsigned AssertOp = 
170             I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
171           argt = DAG.getNode(AssertOp, MVT::i64, argt, 
172                              DAG.getValueType(VT));
173           argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
174         }
175         break;
176       }
177     } else { //more args
178       // Create the frame index object for this incoming parameter...
179       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
180
181       // Create the SelectionDAG nodes corresponding to a load
182       //from this parameter
183       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
184       argt = DAG.getLoad(getValueType(I->getType()),
185                          DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
186     }
187     ++count;
188     ArgValues.push_back(argt);
189   }
190
191   // If the functions takes variable number of arguments, copy all regs to stack
192   if (F.isVarArg()) {
193     VarArgsOffset = count * 8;
194     std::vector<SDOperand> LS;
195     for (int i = 0; i < 6; ++i) {
196       if (MRegisterInfo::isPhysicalRegister(args_int[i]))
197         args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
198       SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
199       int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
200       if (i == 0) VarArgsBase = FI;
201       SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
202       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
203                                SDFI, DAG.getSrcValue(NULL)));
204
205       if (MRegisterInfo::isPhysicalRegister(args_float[i]))
206         args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
207       argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
208       FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
209       SDFI = DAG.getFrameIndex(FI, MVT::i64);
210       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
211                                SDFI, DAG.getSrcValue(NULL)));
212     }
213
214     //Set up a token factor with all the stack traffic
215     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
216   }
217
218   // Finally, inform the code generator which regs we return values in.
219   switch (getValueType(F.getReturnType())) {
220   default: assert(0 && "Unknown type!");
221   case MVT::isVoid: break;
222   case MVT::i1:
223   case MVT::i8:
224   case MVT::i16:
225   case MVT::i32:
226   case MVT::i64:
227     MF.addLiveOut(Alpha::R0);
228     break;
229   case MVT::f32:
230   case MVT::f64:
231     MF.addLiveOut(Alpha::F0);
232     break;
233   }
234
235   //return the arguments
236   return ArgValues;
237 }
238
239 std::pair<SDOperand, SDOperand>
240 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
241                                  const Type *RetTy, bool isVarArg,
242                                  unsigned CallingConv, bool isTailCall,
243                                  SDOperand Callee, ArgListTy &Args,
244                                  SelectionDAG &DAG) {
245   int NumBytes = 0;
246   if (Args.size() > 6)
247     NumBytes = (Args.size() - 6) * 8;
248
249   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
250                       DAG.getConstant(NumBytes, getPointerTy()));
251   std::vector<SDOperand> args_to_use;
252   for (unsigned i = 0, e = Args.size(); i != e; ++i)
253   {
254     switch (getValueType(Args[i].second)) {
255     default: assert(0 && "Unexpected ValueType for argument!");
256     case MVT::i1:
257     case MVT::i8:
258     case MVT::i16:
259     case MVT::i32:
260       // Promote the integer to 64 bits.  If the input type is signed use a
261       // sign extend, otherwise use a zero extend.
262       if (Args[i].second->isSigned())
263         Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
264       else
265         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
266       break;
267     case MVT::i64:
268     case MVT::f64:
269     case MVT::f32:
270       break;
271     }
272     args_to_use.push_back(Args[i].first);
273   }
274
275   std::vector<MVT::ValueType> RetVals;
276   MVT::ValueType RetTyVT = getValueType(RetTy);
277   MVT::ValueType ActualRetTyVT = RetTyVT;
278   if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i32)
279     ActualRetTyVT = MVT::i64;
280
281   if (RetTyVT != MVT::isVoid)
282     RetVals.push_back(ActualRetTyVT);
283   RetVals.push_back(MVT::Other);
284
285   SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
286                                             Chain, Callee, args_to_use), 0);
287   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
288   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
289                       DAG.getConstant(NumBytes, getPointerTy()));
290   SDOperand RetVal = TheCall;
291
292   if (RetTyVT != ActualRetTyVT) {
293     RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
294                          MVT::i64, RetVal, DAG.getValueType(RetTyVT));
295     RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
296   }
297
298   return std::make_pair(RetVal, Chain);
299 }
300
301 SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
302                                             Value *VAListV, SelectionDAG &DAG) {
303   // vastart stores the address of the VarArgsBase and VarArgsOffset
304   SDOperand FR  = DAG.getFrameIndex(VarArgsBase, MVT::i64);
305   SDOperand S1  = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
306                               DAG.getSrcValue(VAListV));
307   SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
308                               DAG.getConstant(8, MVT::i64));
309   return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
310                      DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
311                      DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
312 }
313
314 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
315 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
316            const Type *ArgTy, SelectionDAG &DAG) {
317   SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
318                                DAG.getSrcValue(VAListV));
319   SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
320                               DAG.getConstant(8, MVT::i64));
321   SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
322                                     Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
323   SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
324   if (ArgTy->isFloatingPoint())
325   {
326     //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
327       SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
328                                         DAG.getConstant(8*6, MVT::i64));
329       SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
330                                   DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
331       DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
332   }
333
334   SDOperand Result;
335   if (ArgTy == Type::IntTy)
336     Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
337                             DataPtr, DAG.getSrcValue(NULL), MVT::i32);
338   else if (ArgTy == Type::UIntTy)
339     Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
340                             DataPtr, DAG.getSrcValue(NULL), MVT::i32);
341   else
342     Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
343                          DAG.getSrcValue(NULL));
344
345   SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
346                                     DAG.getConstant(8, MVT::i64));
347   SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
348                                  Result.getValue(1), NewOffset,
349                                  Tmp, DAG.getSrcValue(VAListV, 8),
350                                  DAG.getValueType(MVT::i32));
351   Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
352
353   return std::make_pair(Result, Update);
354 }
355
356
357 SDOperand AlphaTargetLowering::
358 LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
359             Value *DestV, SelectionDAG &DAG) {
360   SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
361                               DAG.getSrcValue(SrcV));
362   SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
363                                  Val, DestP, DAG.getSrcValue(DestV));
364   SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
365                              DAG.getConstant(8, MVT::i64));
366   Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
367                        DAG.getSrcValue(SrcV, 8), MVT::i32);
368   SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
369                              DAG.getConstant(8, MVT::i64));
370   return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
371                      Val, NPD, DAG.getSrcValue(DestV, 8),
372                      DAG.getValueType(MVT::i32));
373 }
374
375 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
376 {
377   BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
378 }
379 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
380 {
381   BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
382 }
383
384