revert part of the last change, should fix regressions
[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> EnableAlphaFTOI;
31   extern cl::opt<bool> EnableAlphaCT;
32   extern cl::opt<bool> EnableAlphaCount;
33   extern cl::opt<bool> EnableAlphaLSMark;
34 }
35
36 /// AddLiveIn - This helper function adds the specified physical register to the
37 /// MachineFunction as a live in value.  It also creates a corresponding virtual
38 /// register for it.
39 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
40                           TargetRegisterClass *RC) {
41   assert(RC->contains(PReg) && "Not the correct regclass!");
42   unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
43   MF.addLiveIn(PReg, VReg);
44   return VReg;
45 }
46
47 AlphaTargetLowering::AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
48   // Set up the TargetLowering object.
49   //I am having problems with shr n ubyte 1
50   setShiftAmountType(MVT::i64);
51   setSetCCResultType(MVT::i64);
52   setSetCCResultContents(ZeroOrOneSetCCResult);
53   
54   addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
55   addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
56   addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
57   
58   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
59   setOperationAction(ISD::BRTWOWAY_CC,  MVT::Other, Expand);
60   
61   setOperationAction(ISD::EXTLOAD, MVT::i1,  Promote);
62   setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
63   
64   setOperationAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
65   setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
66   
67   setOperationAction(ISD::SEXTLOAD, MVT::i1,  Promote);
68   setOperationAction(ISD::SEXTLOAD, MVT::i8,  Expand);
69   setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
70   
71   setOperationAction(ISD::SREM, MVT::f32, Expand);
72   setOperationAction(ISD::SREM, MVT::f64, Expand);
73   
74   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
75   
76   if (!EnableAlphaCT) {
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   unsigned added_int = 0;
139   unsigned added_fp = 0;
140
141   int count = 0;
142
143   GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
144   RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
145
146   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
147   {
148     SDOperand argt;
149     if (count  < 6) {
150       unsigned Vreg;
151       MVT::ValueType VT = getValueType(I->getType());
152       switch (VT) {
153       default:
154         std::cerr << "Unknown Type " << VT << "\n";
155         abort();
156       case MVT::f64:
157       case MVT::f32:
158         args_float[count] = AddLiveIn(MF, args_float[count], getRegClassFor(VT));
159         added_fp |= (1 << count);
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         added_int |= (1 << count);
170         argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
171         DAG.setRoot(argt.getValue(1));
172         if (VT != MVT::i64) {
173           unsigned AssertOp = 
174             I->getType()->isSigned() ? ISD::AssertSext : ISD::AssertZext;
175           argt = DAG.getNode(AssertOp, MVT::i64, argt, 
176                              DAG.getValueType(VT));
177           argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
178         }
179         break;
180       }
181     } else { //more args
182       // Create the frame index object for this incoming parameter...
183       int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
184
185       // Create the SelectionDAG nodes corresponding to a load
186       //from this parameter
187       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
188       argt = DAG.getLoad(getValueType(I->getType()),
189                          DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
190     }
191     ++count;
192     ArgValues.push_back(argt);
193   }
194
195   // If the functions takes variable number of arguments, copy all regs to stack
196   if (F.isVarArg()) {
197     VarArgsOffset = count * 8;
198     std::vector<SDOperand> LS;
199     for (int i = 0; i < 6; ++i) {
200       if (!(added_int & (1 << i)))
201         args_int[i] = AddLiveIn(MF, args_int[i], getRegClassFor(MVT::i64));
202       SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
203       int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
204       if (i == 0) VarArgsBase = FI;
205       SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
206       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
207                                SDFI, DAG.getSrcValue(NULL)));
208
209       if (!(added_fp & (1 << i)))
210         args_float[i] = AddLiveIn(MF, args_float[i], getRegClassFor(MVT::f64));
211       argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
212       FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
213       SDFI = DAG.getFrameIndex(FI, MVT::i64);
214       LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
215                                SDFI, DAG.getSrcValue(NULL)));
216     }
217
218     //Set up a token factor with all the stack traffic
219     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
220   }
221
222   // Finally, inform the code generator which regs we return values in.
223   switch (getValueType(F.getReturnType())) {
224   default: assert(0 && "Unknown type!");
225   case MVT::isVoid: break;
226   case MVT::i1:
227   case MVT::i8:
228   case MVT::i16:
229   case MVT::i32:
230   case MVT::i64:
231     MF.addLiveOut(Alpha::R0);
232     break;
233   case MVT::f32:
234   case MVT::f64:
235     MF.addLiveOut(Alpha::F0);
236     break;
237   }
238
239   //return the arguments
240   return ArgValues;
241 }
242
243 std::pair<SDOperand, SDOperand>
244 AlphaTargetLowering::LowerCallTo(SDOperand Chain,
245                                  const Type *RetTy, bool isVarArg,
246                                  unsigned CallingConv, bool isTailCall,
247                                  SDOperand Callee, ArgListTy &Args,
248                                  SelectionDAG &DAG) {
249   int NumBytes = 0;
250   if (Args.size() > 6)
251     NumBytes = (Args.size() - 6) * 8;
252
253   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
254                       DAG.getConstant(NumBytes, getPointerTy()));
255   std::vector<SDOperand> args_to_use;
256   for (unsigned i = 0, e = Args.size(); i != e; ++i)
257   {
258     switch (getValueType(Args[i].second)) {
259     default: assert(0 && "Unexpected ValueType for argument!");
260     case MVT::i1:
261     case MVT::i8:
262     case MVT::i16:
263     case MVT::i32:
264       // Promote the integer to 64 bits.  If the input type is signed use a
265       // sign extend, otherwise use a zero extend.
266       if (Args[i].second->isSigned())
267         Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
268       else
269         Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
270       break;
271     case MVT::i64:
272     case MVT::f64:
273     case MVT::f32:
274       break;
275     }
276     args_to_use.push_back(Args[i].first);
277   }
278
279   std::vector<MVT::ValueType> RetVals;
280   MVT::ValueType RetTyVT = getValueType(RetTy);
281   if (RetTyVT != MVT::isVoid)
282     RetVals.push_back(RetTyVT);
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   return std::make_pair(TheCall, Chain);
291 }
292
293 SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
294                                             Value *VAListV, SelectionDAG &DAG) {
295   // vastart stores the address of the VarArgsBase and VarArgsOffset
296   SDOperand FR  = DAG.getFrameIndex(VarArgsBase, MVT::i64);
297   SDOperand S1  = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
298                               DAG.getSrcValue(VAListV));
299   SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
300                               DAG.getConstant(8, MVT::i64));
301   return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
302                      DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
303                      DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
304 }
305
306 std::pair<SDOperand,SDOperand> AlphaTargetLowering::
307 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
308            const Type *ArgTy, SelectionDAG &DAG) {
309   SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
310                                DAG.getSrcValue(VAListV));
311   SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
312                               DAG.getConstant(8, MVT::i64));
313   SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
314                                     Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
315   SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
316   if (ArgTy->isFloatingPoint())
317   {
318     //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
319       SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
320                                         DAG.getConstant(8*6, MVT::i64));
321       SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
322                                   DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
323       DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
324   }
325
326   SDOperand Result;
327   if (ArgTy == Type::IntTy)
328     Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
329                             DataPtr, DAG.getSrcValue(NULL), MVT::i32);
330   else if (ArgTy == Type::UIntTy)
331     Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
332                             DataPtr, DAG.getSrcValue(NULL), MVT::i32);
333   else
334     Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
335                          DAG.getSrcValue(NULL));
336
337   SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
338                                     DAG.getConstant(8, MVT::i64));
339   SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
340                                  Result.getValue(1), NewOffset,
341                                  Tmp, DAG.getSrcValue(VAListV, 8),
342                                  DAG.getValueType(MVT::i32));
343   Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
344
345   return std::make_pair(Result, Update);
346 }
347
348
349 SDOperand AlphaTargetLowering::
350 LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
351             Value *DestV, SelectionDAG &DAG) {
352   SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
353                               DAG.getSrcValue(SrcV));
354   SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
355                                  Val, DestP, DAG.getSrcValue(DestV));
356   SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
357                              DAG.getConstant(8, MVT::i64));
358   Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
359                        DAG.getSrcValue(SrcV, 8), MVT::i32);
360   SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
361                              DAG.getConstant(8, MVT::i64));
362   return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
363                      Val, NPD, DAG.getSrcValue(DestV, 8),
364                      DAG.getValueType(MVT::i32));
365 }
366
367 void AlphaTargetLowering::restoreGP(MachineBasicBlock* BB)
368 {
369   BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
370 }
371 void AlphaTargetLowering::restoreRA(MachineBasicBlock* BB)
372 {
373   BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
374 }
375
376