begining alpha subtarget support
[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::FREM, MVT::f32, Expand);
70   setOperationAction(ISD::FREM, MVT::f64, Expand);
71   
72   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
73   
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);
78   }
79   
80   //If this didn't legalize into a div....
81   //      setOperationAction(ISD::SREM     , MVT::i64, Expand);
82   //      setOperationAction(ISD::UREM     , MVT::i64, Expand);
83   
84   setOperationAction(ISD::MEMMOVE  , MVT::Other, Expand);
85   setOperationAction(ISD::MEMSET   , MVT::Other, Expand);
86   setOperationAction(ISD::MEMCPY   , MVT::Other, Expand);
87   
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);
95
96   //Doesn't work yet
97   setOperationAction(ISD::SETCC, MVT::f32,   Promote);
98   
99   computeRegisterProperties();
100   
101   addLegalFPImmediate(+0.0); //F31
102   addLegalFPImmediate(-0.0); //-F31
103 }
104
105
106 //http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
107
108 //For now, just use variable size stack frame format
109
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)
117
118 // //#define FP    $15
119 // //#define RA    $26
120 // //#define PV    $27
121 // //#define GP    $29
122 // //#define SP    $30
123
124 std::vector<SDOperand>
125 AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
126 {
127   MachineFunction &MF = DAG.getMachineFunction();
128   MachineFrameInfo *MFI = MF.getFrameInfo();
129   MachineBasicBlock& BB = MF.front();
130   std::vector<SDOperand> ArgValues;
131
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};
136
137   int count = 0;
138
139   GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
140   RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
141
142   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
143   {
144     SDOperand argt;
145     if (count  < 6) {
146       unsigned Vreg;
147       MVT::ValueType VT = getValueType(I->getType());
148       switch (VT) {
149       default:
150         std::cerr << "Unknown Type " << VT << "\n";
151         abort();
152       case MVT::f64:
153       case MVT::f32:
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));
157         break;
158       case MVT::i1:
159       case MVT::i8:
160       case MVT::i16:
161       case MVT::i32:
162       case MVT::i64:
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) {
167           unsigned AssertOp = 
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);
172         }
173         break;
174       }
175     } else { //more args
176       // Create the frame index object for this incoming parameter...
177       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
178
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));
184     }
185     ++count;
186     ArgValues.push_back(argt);
187   }
188
189   // If the functions takes variable number of arguments, copy all regs to stack
190   if (F.isVarArg()) {
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)));
202
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)));
210     }
211
212     //Set up a token factor with all the stack traffic
213     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
214   }
215
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;
220   case MVT::i1:
221   case MVT::i8:
222   case MVT::i16:
223   case MVT::i32:
224   case MVT::i64:
225     MF.addLiveOut(Alpha::R0);
226     break;
227   case MVT::f32:
228   case MVT::f64:
229     MF.addLiveOut(Alpha::F0);
230     break;
231   }
232
233   //return the arguments
234   return ArgValues;
235 }
236
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,
242                                  SelectionDAG &DAG) {
243   int NumBytes = 0;
244   if (Args.size() > 6)
245     NumBytes = (Args.size() - 6) * 8;
246
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)
251   {
252     switch (getValueType(Args[i].second)) {
253     default: assert(0 && "Unexpected ValueType for argument!");
254     case MVT::i1:
255     case MVT::i8:
256     case MVT::i16:
257     case MVT::i32:
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);
262       else
263         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
264       break;
265     case MVT::i64:
266     case MVT::f64:
267     case MVT::f32:
268       break;
269     }
270     args_to_use.push_back(Args[i].first);
271   }
272
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;
278
279   if (RetTyVT != MVT::isVoid)
280     RetVals.push_back(ActualRetTyVT);
281   RetVals.push_back(MVT::Other);
282
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;
289
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);
294   }
295
296   return std::make_pair(RetVal, Chain);
297 }
298
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));
310 }
311
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())
323   {
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);
330   }
331
332   SDOperand Result;
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);
339   else
340     Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
341                          DAG.getSrcValue(NULL));
342
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);
350
351   return std::make_pair(Result, Update);
352 }
353
354
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));
371 }
372
373 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
374 {
375   BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
376 }
377 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
378 {
379   BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
380 }
381
382