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