fix some bugs affecting functions with no arguments
[oota-llvm.git] / lib / Target / ARM / ARMISelDAGToDAG.cpp
1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an instruction selector for the ARM target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARM.h"
15 #include "ARMTargetMachine.h"
16 #include "llvm/CallingConv.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/SSARegMap.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Support/Debug.h"
29 #include <iostream>
30 #include <vector>
31 using namespace llvm;
32
33 namespace {
34   class ARMTargetLowering : public TargetLowering {
35     int VarArgsFrameIndex;            // FrameIndex for start of varargs area.
36   public:
37     ARMTargetLowering(TargetMachine &TM);
38     virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
39     virtual const char *getTargetNodeName(unsigned Opcode) const;
40   };
41
42 }
43
44 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
45   : TargetLowering(TM) {
46   addRegisterClass(MVT::i32, ARM::IntRegsRegisterClass);
47   addRegisterClass(MVT::f32, ARM::FPRegsRegisterClass);
48   addRegisterClass(MVT::f64, ARM::DFPRegsRegisterClass);
49
50   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
51
52   setOperationAction(ISD::RET,           MVT::Other, Custom);
53   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
54   setOperationAction(ISD::ConstantPool,  MVT::i32,   Custom);
55
56   setOperationAction(ISD::SETCC, MVT::i32, Expand);
57   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
58   setOperationAction(ISD::BR_CC, MVT::i32, Custom);
59
60   setOperationAction(ISD::VASTART,       MVT::Other, Custom);
61   setOperationAction(ISD::VAEND,         MVT::Other, Expand);
62
63   setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
64   setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
65
66   setSchedulingPreference(SchedulingForRegPressure);
67   computeRegisterProperties();
68 }
69
70 namespace llvm {
71   namespace ARMISD {
72     enum NodeType {
73       // Start the numbering where the builting ops and target ops leave off.
74       FIRST_NUMBER = ISD::BUILTIN_OP_END+ARM::INSTRUCTION_LIST_END,
75       /// CALL - A direct function call.
76       CALL,
77
78       /// Return with a flag operand.
79       RET_FLAG,
80
81       CMP,
82
83       SELECT,
84
85       BR,
86
87       FSITOS,
88
89       FSITOD,
90
91       FMRRD,
92
93       FMDRR
94     };
95   }
96 }
97
98 /// DAGCCToARMCC - Convert a DAG integer condition code to an ARM CC
99 static ARMCC::CondCodes DAGCCToARMCC(ISD::CondCode CC) {
100   switch (CC) {
101   default:
102     std::cerr << "CC = " << CC << "\n";
103     assert(0 && "Unknown condition code!");
104   case ISD::SETUGT: return ARMCC::HI;
105   case ISD::SETULE: return ARMCC::LS;
106   case ISD::SETLE:  return ARMCC::LE;
107   case ISD::SETLT:  return ARMCC::LT;
108   case ISD::SETGT:  return ARMCC::GT;
109   case ISD::SETNE:  return ARMCC::NE;
110   case ISD::SETEQ:  return ARMCC::EQ;
111   case ISD::SETGE:  return ARMCC::GE;
112   case ISD::SETUGE: return ARMCC::CS;
113   case ISD::SETULT: return ARMCC::CC;
114   }
115 }
116
117 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
118   switch (Opcode) {
119   default: return 0;
120   case ARMISD::CALL:          return "ARMISD::CALL";
121   case ARMISD::RET_FLAG:      return "ARMISD::RET_FLAG";
122   case ARMISD::SELECT:        return "ARMISD::SELECT";
123   case ARMISD::CMP:           return "ARMISD::CMP";
124   case ARMISD::BR:            return "ARMISD::BR";
125   case ARMISD::FSITOS:        return "ARMISD::FSITOS";
126   case ARMISD::FSITOD:        return "ARMISD::FSITOD";
127   case ARMISD::FMRRD:         return "ARMISD::FMRRD";
128   case ARMISD::FMDRR:         return "ARMISD::FMDRR";
129   }
130 }
131
132 class ArgumentLayout {
133   std::vector<bool>           is_reg;
134   std::vector<unsigned>       pos;
135   std::vector<MVT::ValueType> types;
136 public:
137   ArgumentLayout(const std::vector<MVT::ValueType> &Types) {
138     types = Types;
139
140     unsigned      RegNum = 0;
141     unsigned StackOffset = 0;
142     for(std::vector<MVT::ValueType>::const_iterator I = Types.begin();
143         I != Types.end();
144         ++I) {
145       MVT::ValueType VT = *I;
146       assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
147       unsigned     size = MVT::getSizeInBits(VT)/32;
148
149       RegNum = ((RegNum + size - 1) / size) * size;
150       if (RegNum < 4) {
151         pos.push_back(RegNum);
152         is_reg.push_back(true);
153         RegNum += size;
154       } else {
155         unsigned bytes = size * 32/8;
156         StackOffset = ((StackOffset + bytes - 1) / bytes) * bytes;
157         pos.push_back(StackOffset);
158         is_reg.push_back(false);
159         StackOffset += bytes;
160       }
161     }
162   }
163   unsigned getRegisterNum(unsigned argNum) {
164     assert(isRegister(argNum));
165     return pos[argNum];
166   }
167   unsigned getOffset(unsigned argNum) {
168     assert(isOffset(argNum));
169     return pos[argNum];
170   }
171   unsigned isRegister(unsigned argNum) {
172     assert(argNum < is_reg.size());
173     return is_reg[argNum];
174   }
175   unsigned isOffset(unsigned argNum) {
176     return !isRegister(argNum);
177   }
178   MVT::ValueType getType(unsigned argNum) {
179     assert(argNum < types.size());
180     return types[argNum];
181   }
182   unsigned getStackSize(void) {
183     int last = is_reg.size() - 1;
184     if (last < 0)
185       return 0;
186     if (isRegister(last))
187       return 0;
188     return getOffset(last) + MVT::getSizeInBits(getType(last))/8;
189   }
190   int lastRegArg(void) {
191     int size = is_reg.size();
192     int last = 0;
193     while(last < size && isRegister(last))
194       last++;
195     last--;
196     return last;
197   }
198   int lastRegNum(void) {
199     int            l = lastRegArg();
200     if (l < 0)
201       return -1;
202     unsigned       r = getRegisterNum(l);
203     MVT::ValueType t = getType(l);
204     assert(t == MVT::i32 || t == MVT::f32 || t == MVT::f64);
205     if (t == MVT::f64)
206       return r + 1;
207     return r;
208   }
209 };
210
211 // This transforms a ISD::CALL node into a
212 // callseq_star <- ARMISD:CALL <- callseq_end
213 // chain
214 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
215   SDOperand Chain    = Op.getOperand(0);
216   unsigned CallConv  = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
217   assert(CallConv == CallingConv::C && "unknown calling convention");
218   bool isVarArg      = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
219   bool isTailCall    = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
220   assert(isTailCall == false && "tail call not supported");
221   SDOperand Callee   = Op.getOperand(4);
222   unsigned NumOps    = (Op.getNumOperands() - 5) / 2;
223   SDOperand StackPtr = DAG.getRegister(ARM::R13, MVT::i32);
224   static const unsigned regs[] = {
225     ARM::R0, ARM::R1, ARM::R2, ARM::R3
226   };
227
228   std::vector<MVT::ValueType> Types;
229   for (unsigned i = 0; i < NumOps; ++i) {
230     MVT::ValueType VT = Op.getOperand(5+2*i).getValueType();
231     Types.push_back(VT);
232   }
233   ArgumentLayout Layout(Types);
234
235   unsigned NumBytes = Layout.getStackSize();
236
237   Chain = DAG.getCALLSEQ_START(Chain,
238                                DAG.getConstant(NumBytes, MVT::i32));
239
240   //Build a sequence of stores
241   std::vector<SDOperand> MemOpChains;
242   for (unsigned i = Layout.lastRegArg() + 1; i < NumOps; ++i) {
243     SDOperand      Arg = Op.getOperand(5+2*i);
244     unsigned ArgOffset = Layout.getOffset(i);
245     SDOperand   PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
246     PtrOff             = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
247     MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff,
248                                        DAG.getSrcValue(NULL)));
249   }
250   if (!MemOpChains.empty())
251     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
252                         &MemOpChains[0], MemOpChains.size());
253
254   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
255   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
256   // node so that legalize doesn't hack it.
257   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
258     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
259
260   // If this is a direct call, pass the chain and the callee.
261   assert (Callee.Val);
262   std::vector<SDOperand> Ops;
263   Ops.push_back(Chain);
264   Ops.push_back(Callee);
265
266   // Build a sequence of copy-to-reg nodes chained together with token chain
267   // and flag operands which copy the outgoing args into the appropriate regs.
268   SDOperand InFlag;
269   for (int i = 0, e = Layout.lastRegArg(); i <= e; ++i) {
270     SDOperand     Arg = Op.getOperand(5+2*i);
271     unsigned   RegNum = Layout.getRegisterNum(i);
272     unsigned     Reg1 = regs[RegNum];
273     MVT::ValueType VT = Layout.getType(i);
274     assert(VT == Arg.getValueType());
275     assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
276
277     // Add argument register to the end of the list so that it is known live
278     // into the call.
279     Ops.push_back(DAG.getRegister(Reg1, MVT::i32));
280     if (VT == MVT::f64) {
281       unsigned    Reg2 = regs[RegNum + 1];
282       SDOperand SDReg1 = DAG.getRegister(Reg1, MVT::i32);
283       SDOperand SDReg2 = DAG.getRegister(Reg2, MVT::i32);
284
285       Ops.push_back(DAG.getRegister(Reg2, MVT::i32));
286       SDVTList    VTs = DAG.getVTList(MVT::Other, MVT::Flag);
287       SDOperand Ops[] = {Chain, SDReg1, SDReg2, Arg}; //missing flag
288       Chain = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
289     } else {
290       if (VT == MVT::f32)
291         Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg);
292       Chain = DAG.getCopyToReg(Chain, Reg1, Arg, InFlag);
293     }
294     InFlag = Chain.getValue(1);
295   }
296
297   std::vector<MVT::ValueType> NodeTys;
298   NodeTys.push_back(MVT::Other);   // Returns a chain
299   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
300
301   unsigned CallOpc = ARMISD::CALL;
302   if (InFlag.Val)
303     Ops.push_back(InFlag);
304   Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
305   InFlag = Chain.getValue(1);
306
307   std::vector<SDOperand> ResultVals;
308   NodeTys.clear();
309
310   // If the call has results, copy the values out of the ret val registers.
311   switch (Op.Val->getValueType(0)) {
312   default: assert(0 && "Unexpected ret value!");
313   case MVT::Other:
314     break;
315   case MVT::i32:
316     Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
317     ResultVals.push_back(Chain.getValue(0));
318     NodeTys.push_back(MVT::i32);
319   }
320
321   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
322                       DAG.getConstant(NumBytes, MVT::i32));
323   NodeTys.push_back(MVT::Other);
324
325   if (ResultVals.empty())
326     return Chain;
327
328   ResultVals.push_back(Chain);
329   SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
330                               ResultVals.size());
331   return Res.getValue(Op.ResNo);
332 }
333
334 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
335   SDOperand Copy;
336   SDOperand Chain = Op.getOperand(0);
337   SDOperand    R0 = DAG.getRegister(ARM::R0, MVT::i32);
338   SDOperand    R1 = DAG.getRegister(ARM::R1, MVT::i32);
339
340   switch(Op.getNumOperands()) {
341   default:
342     assert(0 && "Do not know how to return this many arguments!");
343     abort();
344   case 1: {
345     SDOperand LR = DAG.getRegister(ARM::R14, MVT::i32);
346     return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
347   }
348   case 3: {
349     SDOperand Val = Op.getOperand(1);
350     assert(Val.getValueType() == MVT::i32 ||
351            Val.getValueType() == MVT::f32 ||
352            Val.getValueType() == MVT::f64);
353
354     if (Val.getValueType() == MVT::f64) {
355       SDVTList    VTs = DAG.getVTList(MVT::Other, MVT::Flag);
356       SDOperand Ops[] = {Chain, R0, R1, Val};
357       Copy  = DAG.getNode(ARMISD::FMRRD, VTs, Ops, 4);
358     } else {
359       if (Val.getValueType() == MVT::f32)
360         Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
361       Copy = DAG.getCopyToReg(Chain, R0, Val, SDOperand());
362     }
363
364     if (DAG.getMachineFunction().liveout_empty()) {
365       DAG.getMachineFunction().addLiveOut(ARM::R0);
366       if (Val.getValueType() == MVT::f64)
367         DAG.getMachineFunction().addLiveOut(ARM::R1);
368     }
369     break;
370   }
371   case 5:
372     Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
373     Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
374     // If we haven't noted the R0+R1 are live out, do so now.
375     if (DAG.getMachineFunction().liveout_empty()) {
376       DAG.getMachineFunction().addLiveOut(ARM::R0);
377       DAG.getMachineFunction().addLiveOut(ARM::R1);
378     }
379     break;
380   }
381
382   //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
383   return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
384 }
385
386 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
387   MVT::ValueType PtrVT = Op.getValueType();
388   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
389   Constant *C = CP->getConstVal();
390   SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
391
392   return CPI;
393 }
394
395 static SDOperand LowerGlobalAddress(SDOperand Op,
396                                     SelectionDAG &DAG) {
397   GlobalValue  *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
398   int alignment = 2;
399   SDOperand CPAddr = DAG.getConstantPool(GV, MVT::i32, alignment);
400   return DAG.getLoad(MVT::i32, DAG.getEntryNode(), CPAddr,
401                      DAG.getSrcValue(NULL));
402 }
403
404 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
405                               unsigned VarArgsFrameIndex) {
406   // vastart just stores the address of the VarArgsFrameIndex slot into the
407   // memory location argument.
408   MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
409   SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
410   return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), Op.getOperand(2));
411 }
412
413 static SDOperand LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG,
414                                        int &VarArgsFrameIndex) {
415   MachineFunction   &MF = DAG.getMachineFunction();
416   MachineFrameInfo *MFI = MF.getFrameInfo();
417   SSARegMap     *RegMap = MF.getSSARegMap();
418   unsigned      NumArgs = Op.Val->getNumValues()-1;
419   SDOperand        Root = Op.getOperand(0);
420   bool         isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
421   static const unsigned REGS[] = {
422     ARM::R0, ARM::R1, ARM::R2, ARM::R3
423   };
424
425   std::vector<MVT::ValueType> Types(Op.Val->value_begin(), Op.Val->value_end() - 1);
426   ArgumentLayout Layout(Types);
427
428   std::vector<SDOperand> ArgValues;
429   for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) {
430     MVT::ValueType VT = Types[ArgNo];
431
432     SDOperand Value;
433     if (Layout.isRegister(ArgNo)) {
434       assert(VT == MVT::i32 || VT == MVT::f32 || VT == MVT::f64);
435       unsigned  RegNum = Layout.getRegisterNum(ArgNo);
436       unsigned    Reg1 = REGS[RegNum];
437       unsigned   VReg1 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
438       SDOperand Value1 = DAG.getCopyFromReg(Root, VReg1, MVT::i32);
439       MF.addLiveIn(Reg1, VReg1);
440       if (VT == MVT::f64) {
441         unsigned    Reg2 = REGS[RegNum + 1];
442         unsigned   VReg2 = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
443         SDOperand Value2 = DAG.getCopyFromReg(Root, VReg2, MVT::i32);
444         MF.addLiveIn(Reg2, VReg2);
445         Value            = DAG.getNode(ARMISD::FMDRR, MVT::f64, Value1, Value2);
446       } else {
447         Value = Value1;
448         if (VT == MVT::f32)
449           Value = DAG.getNode(ISD::BIT_CONVERT, VT, Value);
450       }
451     } else {
452       // If the argument is actually used, emit a load from the right stack
453       // slot.
454       if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
455         unsigned Offset = Layout.getOffset(ArgNo);
456         unsigned   Size = MVT::getSizeInBits(VT)/8;
457         int          FI = MFI->CreateFixedObject(Size, Offset);
458         SDOperand   FIN = DAG.getFrameIndex(FI, VT);
459         Value = DAG.getLoad(VT, Root, FIN, DAG.getSrcValue(NULL));
460       } else {
461         Value = DAG.getNode(ISD::UNDEF, VT);
462       }
463     }
464     ArgValues.push_back(Value);
465   }
466
467   unsigned NextRegNum = Layout.lastRegNum() + 1;
468
469   if (isVarArg) {
470     //If this function is vararg we must store the remaing
471     //registers so that they can be acessed with va_start
472     VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
473                                                -16 + NextRegNum * 4);
474
475     SmallVector<SDOperand, 4> MemOps;
476     for (unsigned RegNo = NextRegNum; RegNo < 4; ++RegNo) {
477       int RegOffset = - (4 - RegNo) * 4;
478       int FI = MFI->CreateFixedObject(MVT::getSizeInBits(MVT::i32)/8,
479                                       RegOffset);
480       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
481
482       unsigned VReg = RegMap->createVirtualRegister(&ARM::IntRegsRegClass);
483       MF.addLiveIn(REGS[RegNo], VReg);
484
485       SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
486       SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN,
487                                      DAG.getSrcValue(NULL));
488       MemOps.push_back(Store);
489     }
490     Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
491   }
492
493   ArgValues.push_back(Root);
494
495   // Return the new list of results.
496   std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
497                                     Op.Val->value_end());
498   return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
499 }
500
501 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
502   SDOperand LHS = Op.getOperand(0);
503   SDOperand RHS = Op.getOperand(1);
504   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
505   SDOperand TrueVal = Op.getOperand(2);
506   SDOperand FalseVal = Op.getOperand(3);
507   SDOperand    ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
508
509   SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
510   return DAG.getNode(ARMISD::SELECT, MVT::i32, TrueVal, FalseVal, ARMCC, Cmp);
511 }
512
513 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
514   SDOperand  Chain = Op.getOperand(0);
515   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
516   SDOperand    LHS = Op.getOperand(2);
517   SDOperand    RHS = Op.getOperand(3);
518   SDOperand   Dest = Op.getOperand(4);
519   SDOperand  ARMCC = DAG.getConstant(DAGCCToARMCC(CC), MVT::i32);
520
521   SDOperand Cmp = DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
522   return DAG.getNode(ARMISD::BR, MVT::Other, Chain, Dest, ARMCC, Cmp);
523 }
524
525 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
526   SDOperand IntVal  = Op.getOperand(0);
527   assert(IntVal.getValueType() == MVT::i32);
528   MVT::ValueType vt = Op.getValueType();
529   assert(vt == MVT::f32 ||
530          vt == MVT::f64);
531
532   SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, IntVal);
533   ARMISD::NodeType op = vt == MVT::f32 ? ARMISD::FSITOS : ARMISD::FSITOD;
534   return DAG.getNode(op, vt, Tmp);
535 }
536
537 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
538   switch (Op.getOpcode()) {
539   default:
540     assert(0 && "Should not custom lower this!");
541     abort();
542   case ISD::ConstantPool:
543     return LowerConstantPool(Op, DAG);
544   case ISD::GlobalAddress:
545     return LowerGlobalAddress(Op, DAG);
546   case ISD::SINT_TO_FP:
547     return LowerSINT_TO_FP(Op, DAG);
548   case ISD::FORMAL_ARGUMENTS:
549     return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
550   case ISD::CALL:
551     return LowerCALL(Op, DAG);
552   case ISD::RET:
553     return LowerRET(Op, DAG);
554   case ISD::SELECT_CC:
555     return LowerSELECT_CC(Op, DAG);
556   case ISD::BR_CC:
557     return LowerBR_CC(Op, DAG);
558   case ISD::VASTART:
559     return LowerVASTART(Op, DAG, VarArgsFrameIndex);
560   }
561 }
562
563 //===----------------------------------------------------------------------===//
564 // Instruction Selector Implementation
565 //===----------------------------------------------------------------------===//
566
567 //===--------------------------------------------------------------------===//
568 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
569 /// instructions for SelectionDAG operations.
570 ///
571 namespace {
572 class ARMDAGToDAGISel : public SelectionDAGISel {
573   ARMTargetLowering Lowering;
574
575 public:
576   ARMDAGToDAGISel(TargetMachine &TM)
577     : SelectionDAGISel(Lowering), Lowering(TM) {
578   }
579
580   SDNode *Select(SDOperand Op);
581   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
582   bool SelectAddrRegImm(SDOperand N, SDOperand &Offset, SDOperand &Base);
583   bool SelectAddrMode1(SDOperand N, SDOperand &Arg, SDOperand &Shift,
584                        SDOperand &ShiftType);
585
586   // Include the pieces autogenerated from the target description.
587 #include "ARMGenDAGISel.inc"
588 };
589
590 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
591   DEBUG(BB->dump());
592
593   DAG.setRoot(SelectRoot(DAG.getRoot()));
594   DAG.RemoveDeadNodes();
595
596   ScheduleAndEmitDAG(DAG);
597 }
598
599 static bool isInt12Immediate(SDNode *N, short &Imm) {
600   if (N->getOpcode() != ISD::Constant)
601     return false;
602
603   int32_t t = cast<ConstantSDNode>(N)->getValue();
604   int max = 1<<12;
605   int min = -max;
606   if (t > min && t < max) {
607     Imm = t;
608     return true;
609   }
610   else
611     return false;
612 }
613
614 static bool isInt12Immediate(SDOperand Op, short &Imm) {
615   return isInt12Immediate(Op.Val, Imm);
616 }
617
618 static uint32_t rotateL(uint32_t x) {
619   uint32_t bit31 = (x & (1 << 31)) >> 31;
620   uint32_t     t = x << 1;
621   return t | bit31;
622 }
623
624 static bool isUInt8Immediate(uint32_t x) {
625   return x < (1 << 8);
626 }
627
628 static bool isRotInt8Immediate(uint32_t x) {
629   int r;
630   for (r = 0; r < 16; r++) {
631     if (isUInt8Immediate(x))
632       return true;
633     x = rotateL(rotateL(x));
634   }
635   return false;
636 }
637
638 bool ARMDAGToDAGISel::SelectAddrMode1(SDOperand N,
639                                       SDOperand &Arg,
640                                       SDOperand &Shift,
641                                       SDOperand &ShiftType) {
642   switch(N.getOpcode()) {
643   case ISD::Constant: {
644     uint32_t val = cast<ConstantSDNode>(N)->getValue();
645     if(!isRotInt8Immediate(val)) {
646       const Type  *t =  MVT::getTypeForValueType(MVT::i32);
647       Constant    *C = ConstantUInt::get(t, val);
648       int  alignment = 2;
649       SDOperand Addr = CurDAG->getTargetConstantPool(C, MVT::i32, alignment);
650       SDOperand    Z = CurDAG->getTargetConstant(0,     MVT::i32);
651       SDNode      *n = CurDAG->getTargetNode(ARM::ldr,  MVT::i32, Z, Addr);
652       Arg            = SDOperand(n, 0);
653     } else
654       Arg            = CurDAG->getTargetConstant(val,    MVT::i32);
655
656     Shift     = CurDAG->getTargetConstant(0,             MVT::i32);
657     ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
658     return true;
659   }
660   case ISD::SRA:
661     Arg       = N.getOperand(0);
662     Shift     = N.getOperand(1);
663     ShiftType = CurDAG->getTargetConstant(ARMShift::ASR, MVT::i32);
664     return true;
665   case ISD::SRL:
666     Arg       = N.getOperand(0);
667     Shift     = N.getOperand(1);
668     ShiftType = CurDAG->getTargetConstant(ARMShift::LSR, MVT::i32);
669     return true;
670   case ISD::SHL:
671     Arg       = N.getOperand(0);
672     Shift     = N.getOperand(1);
673     ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
674     return true;
675   }
676
677   Arg       = N;
678   Shift     = CurDAG->getTargetConstant(0, MVT::i32);
679   ShiftType = CurDAG->getTargetConstant(ARMShift::LSL, MVT::i32);
680   return true;
681 }
682
683 //register plus/minus 12 bit offset
684 bool ARMDAGToDAGISel::SelectAddrRegImm(SDOperand N, SDOperand &Offset,
685                                     SDOperand &Base) {
686   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
687     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
688     Offset = CurDAG->getTargetConstant(0, MVT::i32);
689     return true;
690   }
691   if (N.getOpcode() == ISD::ADD) {
692     short imm = 0;
693     if (isInt12Immediate(N.getOperand(1), imm)) {
694       Offset = CurDAG->getTargetConstant(imm, MVT::i32);
695       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
696         Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
697       } else {
698         Base = N.getOperand(0);
699       }
700       return true; // [r+i]
701     }
702   }
703
704   Offset = CurDAG->getTargetConstant(0, MVT::i32);
705   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
706     Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
707   }
708   else
709     Base = N;
710   return true;      //any address fits in a register
711 }
712
713 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
714   SDNode *N = Op.Val;
715
716   switch (N->getOpcode()) {
717   default:
718     return SelectCode(Op);
719     break;
720   }
721   return NULL;
722 }
723
724 }  // end anonymous namespace
725
726 /// createARMISelDag - This pass converts a legalized DAG into a
727 /// ARM-specific DAG, ready for instruction scheduling.
728 ///
729 FunctionPass *llvm::createARMISelDag(TargetMachine &TM) {
730   return new ARMDAGToDAGISel(TM);
731 }