DAG->DAG instruction selection for ia64! "hello world" works, not much else.
[oota-llvm.git] / lib / Target / IA64 / IA64ISelLowering.cpp
1 //===-- IA64ISelLowering.cpp - IA64 DAG Lowering Implementation -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Duraid Madina 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 IA64ISelLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "IA64ISelLowering.h"
15 #include "IA64MachineFunctionInfo.h"
16 #include "IA64TargetMachine.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/CodeGen/SSARegMap.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Function.h"
24 using namespace llvm;
25
26 IA64TargetLowering::IA64TargetLowering(TargetMachine &TM)
27   : TargetLowering(TM) {
28  
29       // register class for general registers
30       addRegisterClass(MVT::i64, IA64::GRRegisterClass);
31
32       // register class for FP registers
33       addRegisterClass(MVT::f64, IA64::FPRegisterClass);
34
35       // register class for predicate registers
36       addRegisterClass(MVT::i1, IA64::PRRegisterClass);
37
38       setOperationAction(ISD::BRCONDTWOWAY     , MVT::Other, Expand);
39       setOperationAction(ISD::BRTWOWAY_CC      , MVT::Other, Expand);
40       setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
41
42       setSetCCResultType(MVT::i1);
43       setShiftAmountType(MVT::i64);
44
45       setOperationAction(ISD::EXTLOAD          , MVT::i1   , Promote);
46
47       setOperationAction(ISD::ZEXTLOAD         , MVT::i1   , Expand);
48
49       setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
50       setOperationAction(ISD::SEXTLOAD         , MVT::i8   , Expand);
51       setOperationAction(ISD::SEXTLOAD         , MVT::i16  , Expand);
52       setOperationAction(ISD::SEXTLOAD         , MVT::i32  , Expand);
53
54       setOperationAction(ISD::FREM             , MVT::f32  , Expand);
55       setOperationAction(ISD::FREM             , MVT::f64  , Expand);
56
57       setOperationAction(ISD::UREM             , MVT::f32  , Expand);
58       setOperationAction(ISD::UREM             , MVT::f64  , Expand);
59
60       setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
61       setOperationAction(ISD::MEMSET           , MVT::Other, Expand);
62       setOperationAction(ISD::MEMCPY           , MVT::Other, Expand);
63       
64       setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
65       setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
66
67       // We don't support sin/cos/sqrt
68       setOperationAction(ISD::FSIN , MVT::f64, Expand);
69       setOperationAction(ISD::FCOS , MVT::f64, Expand);
70       setOperationAction(ISD::FSQRT, MVT::f64, Expand);
71       setOperationAction(ISD::FSIN , MVT::f32, Expand);
72       setOperationAction(ISD::FCOS , MVT::f32, Expand);
73       setOperationAction(ISD::FSQRT, MVT::f32, Expand);
74
75       //IA64 has these, but they are not implemented
76       setOperationAction(ISD::CTTZ , MVT::i64  , Expand);
77       setOperationAction(ISD::CTLZ , MVT::i64  , Expand);
78
79       computeRegisterProperties();
80
81       addLegalFPImmediate(+0.0);
82       addLegalFPImmediate(+1.0);
83       addLegalFPImmediate(-0.0);
84       addLegalFPImmediate(-1.0);
85     
86 }
87
88 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
89 static bool isFloatingPointZero(SDOperand Op) {
90   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
91     return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
92   else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
93     // Maybe this has already been legalized into the constant pool?
94     if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
95       if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
96         return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
97   }
98   return false;
99 }
100
101 std::vector<SDOperand>
102 IA64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
103   std::vector<SDOperand> ArgValues;
104   //
105   // add beautiful description of IA64 stack frame format
106   // here (from intel 24535803.pdf most likely)
107   //
108   MachineFunction &MF = DAG.getMachineFunction();
109   MachineFrameInfo *MFI = MF.getFrameInfo();
110   
111   GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
112   SP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
113   RP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
114   
115   MachineBasicBlock& BB = MF.front();
116
117   unsigned args_int[] = {IA64::r32, IA64::r33, IA64::r34, IA64::r35,
118                          IA64::r36, IA64::r37, IA64::r38, IA64::r39};
119
120   unsigned args_FP[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
121                         IA64::F12,IA64::F13,IA64::F14, IA64::F15};
122
123   unsigned argVreg[8];
124   unsigned argPreg[8];
125   unsigned argOpc[8];
126
127   unsigned used_FPArgs = 0; // how many FP args have been used so far?
128
129   unsigned ArgOffset = 0;
130   int count = 0;
131
132   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
133     {
134       SDOperand newroot, argt;
135       if(count < 8) { // need to fix this logic? maybe.
136
137         switch (getValueType(I->getType())) {
138           default:
139             assert(0 && "ERROR in LowerArgs: can't lower this type of arg.\n"); 
140           case MVT::f32:
141             // fixme? (well, will need to for weird FP structy stuff,
142             // see intel ABI docs)
143           case MVT::f64:
144 //XXX            BuildMI(&BB, IA64::IDEF, 0, args_FP[used_FPArgs]);
145             MF.addLiveIn(args_FP[used_FPArgs]); // mark this reg as liveIn
146             // floating point args go into f8..f15 as-needed, the increment
147             argVreg[count] =                              // is below..:
148             MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
149             // FP args go into f8..f15 as needed: (hence the ++)
150             argPreg[count] = args_FP[used_FPArgs++];
151             argOpc[count] = IA64::FMOV;
152             argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), argVreg[count],
153                                                 MVT::f64);
154             if (I->getType() == Type::FloatTy)
155               argt = DAG.getNode(ISD::FP_ROUND, MVT::f32, argt);
156             break;
157           case MVT::i1: // NOTE: as far as C abi stuff goes,
158                         // bools are just boring old ints
159           case MVT::i8:
160           case MVT::i16:
161           case MVT::i32:
162           case MVT::i64:
163 //XXX            BuildMI(&BB, IA64::IDEF, 0, args_int[count]);
164             MF.addLiveIn(args_int[count]); // mark this register as liveIn
165             argVreg[count] =
166             MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167             argPreg[count] = args_int[count];
168             argOpc[count] = IA64::MOV;
169             argt = newroot =
170               DAG.getCopyFromReg(DAG.getRoot(), argVreg[count], MVT::i64);
171             if ( getValueType(I->getType()) != MVT::i64)
172               argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()),
173                   newroot);
174             break;
175         }
176       } else { // more than 8 args go into the frame
177         // Create the frame index object for this incoming parameter...
178         ArgOffset = 16 + 8 * (count - 8);
179         int FI = MFI->CreateFixedObject(8, ArgOffset);
180
181         // Create the SelectionDAG nodes corresponding to a load
182         //from this parameter
183         SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
184         argt = newroot = DAG.getLoad(getValueType(I->getType()),
185                                      DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
186       }
187       ++count;
188       DAG.setRoot(newroot.getValue(1));
189       ArgValues.push_back(argt);
190     }
191
192
193   // Create a vreg to hold the output of (what will become)
194   // the "alloc" instruction
195   VirtGPR = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
196   BuildMI(&BB, IA64::PSEUDO_ALLOC, 0, VirtGPR);
197   // we create a PSEUDO_ALLOC (pseudo)instruction for now
198
199   BuildMI(&BB, IA64::IDEF, 0, IA64::r1);
200
201   // hmm:
202   BuildMI(&BB, IA64::IDEF, 0, IA64::r12);
203   BuildMI(&BB, IA64::IDEF, 0, IA64::rp);
204   // ..hmm.
205
206   BuildMI(&BB, IA64::MOV, 1, GP).addReg(IA64::r1);
207
208   // hmm:
209   BuildMI(&BB, IA64::MOV, 1, SP).addReg(IA64::r12);
210   BuildMI(&BB, IA64::MOV, 1, RP).addReg(IA64::rp);
211   // ..hmm.
212
213   unsigned tempOffset=0;
214
215   // if this is a varargs function, we simply lower llvm.va_start by
216   // pointing to the first entry
217   if(F.isVarArg()) {
218     tempOffset=0;
219     VarArgsFrameIndex = MFI->CreateFixedObject(8, tempOffset);
220   }
221
222   // here we actually do the moving of args, and store them to the stack
223   // too if this is a varargs function:
224   for (int i = 0; i < count && i < 8; ++i) {
225     BuildMI(&BB, argOpc[i], 1, argVreg[i]).addReg(argPreg[i]);
226     if(F.isVarArg()) {
227       // if this is a varargs function, we copy the input registers to the stack
228       int FI = MFI->CreateFixedObject(8, tempOffset);
229       tempOffset+=8;   //XXX: is it safe to use r22 like this?
230       BuildMI(&BB, IA64::MOV, 1, IA64::r22).addFrameIndex(FI);
231       // FIXME: we should use st8.spill here, one day
232       BuildMI(&BB, IA64::ST8, 1, IA64::r22).addReg(argPreg[i]);
233     }
234   }
235
236   // Finally, inform the code generator which regs we return values in.
237   // (see the ISD::RET: case in the instruction selector)
238   switch (getValueType(F.getReturnType())) {
239   default: assert(0 && "i have no idea where to return this type!");
240   case MVT::isVoid: break;
241   case MVT::i1:
242   case MVT::i8:
243   case MVT::i16:
244   case MVT::i32:
245   case MVT::i64:
246     MF.addLiveOut(IA64::r8);
247     break;
248   case MVT::f32:
249   case MVT::f64:
250     MF.addLiveOut(IA64::F8);
251     break;
252   }
253
254   return ArgValues;
255 }
256
257 std::pair<SDOperand, SDOperand>
258 IA64TargetLowering::LowerCallTo(SDOperand Chain,
259                                 const Type *RetTy, bool isVarArg,
260                                 unsigned CallingConv, bool isTailCall,
261                                 SDOperand Callee, ArgListTy &Args,
262                                 SelectionDAG &DAG) {
263
264   MachineFunction &MF = DAG.getMachineFunction();
265
266   unsigned NumBytes = 16;
267   unsigned outRegsUsed = 0;
268
269   if (Args.size() > 8) {
270     NumBytes += (Args.size() - 8) * 8;
271     outRegsUsed = 8;
272   } else {
273     outRegsUsed = Args.size();
274   }
275
276   // FIXME? this WILL fail if we ever try to pass around an arg that
277   // consumes more than a single output slot (a 'real' double, int128
278   // some sort of aggregate etc.), as we'll underestimate how many 'outX'
279   // registers we use. Hopefully, the assembler will notice.
280   MF.getInfo<IA64FunctionInfo>()->outRegsUsed=
281     std::max(outRegsUsed, MF.getInfo<IA64FunctionInfo>()->outRegsUsed);
282
283   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
284                         DAG.getConstant(NumBytes, getPointerTy()));
285
286   std::vector<SDOperand> args_to_use;
287   for (unsigned i = 0, e = Args.size(); i != e; ++i)
288     {
289       switch (getValueType(Args[i].second)) {
290       default: assert(0 && "unexpected argument type!");
291       case MVT::i1:
292       case MVT::i8:
293       case MVT::i16:
294       case MVT::i32:
295         //promote to 64-bits, sign/zero extending based on type
296         //of the argument
297         if(Args[i].second->isSigned())
298           Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64,
299               Args[i].first);
300         else
301           Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64,
302               Args[i].first);
303         break;
304       case MVT::f32:
305         //promote to 64-bits
306         Args[i].first = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Args[i].first);
307       case MVT::f64:
308       case MVT::i64:
309         break;
310       }
311       args_to_use.push_back(Args[i].first);
312     }
313
314   std::vector<MVT::ValueType> RetVals;
315   MVT::ValueType RetTyVT = getValueType(RetTy);
316   if (RetTyVT != MVT::isVoid)
317     RetVals.push_back(RetTyVT);
318   RetVals.push_back(MVT::Other);
319
320   SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain,
321                                             Callee, args_to_use), 0);
322   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
323   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
324                       DAG.getConstant(NumBytes, getPointerTy()));
325   return std::make_pair(TheCall, Chain);
326 }
327
328 SDOperand
329 IA64TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
330                                  Value *VAListV, SelectionDAG &DAG) {
331   // vastart just stores the address of the VarArgsFrameIndex slot.
332   SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
333   return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR,
334                      VAListP, DAG.getSrcValue(VAListV));
335 }
336
337 std::pair<SDOperand,SDOperand> IA64TargetLowering::
338 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
339            const Type *ArgTy, SelectionDAG &DAG) {
340
341   MVT::ValueType ArgVT = getValueType(ArgTy);
342   SDOperand Val = DAG.getLoad(MVT::i64, Chain,
343                               VAListP, DAG.getSrcValue(VAListV));
344   SDOperand Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), Val,
345                                  DAG.getSrcValue(NULL));
346   unsigned Amt;
347   if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
348     Amt = 8;
349   else {
350     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
351            "Other types should have been promoted for varargs!");
352     Amt = 8;
353   }
354   Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
355                     DAG.getConstant(Amt, Val.getValueType()));
356   Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
357                       Val, VAListP, DAG.getSrcValue(VAListV));
358   return std::make_pair(Result, Chain);
359 }
360
361
362
363 std::pair<SDOperand, SDOperand> IA64TargetLowering::
364 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
365                         SelectionDAG &DAG) {
366   assert(0 && "LowerFrameReturnAddress unimplemented");
367   abort();
368 }
369