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