c9642caef73d5a052d915c43bd1d5016addca733
[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 "ARMISelLowering.h"
16 #include "ARMTargetMachine.h"
17 #include "ARMAddressingModes.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/CodeGen/SSARegMap.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Support/Debug.h"
31 using namespace llvm;
32
33 //===--------------------------------------------------------------------===//
34 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
35 /// instructions for SelectionDAG operations.
36 ///
37 namespace {
38 class ARMDAGToDAGISel : public SelectionDAGISel {
39   ARMTargetLowering Lowering;
40
41   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
42   /// make the right decision when generating code for different targets.
43   const ARMSubtarget *Subtarget;
44
45 public:
46   ARMDAGToDAGISel(ARMTargetMachine &TM)
47     : SelectionDAGISel(Lowering), Lowering(TM),
48     Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
49   }
50
51   virtual const char *getPassName() const {
52     return "ARM Instruction Selection";
53   } 
54   
55   SDNode *Select(SDOperand Op);
56   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
57   bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Base,
58                        SDOperand &Offset, SDOperand &Opc);
59   bool SelectAddrMode2Offset(SDOperand Op, SDOperand N,
60                              SDOperand &Offset, SDOperand &Opc);
61   bool SelectAddrMode3(SDOperand Op, SDOperand N, SDOperand &Base,
62                        SDOperand &Offset, SDOperand &Opc);
63   bool SelectAddrMode3Offset(SDOperand Op, SDOperand N,
64                              SDOperand &Offset, SDOperand &Opc);
65   bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Base,
66                        SDOperand &Offset);
67
68   bool SelectAddrModePC(SDOperand Op, SDOperand N, SDOperand &Offset,
69                          SDOperand &Label);
70
71   bool SelectThumbAddrModeRR(SDOperand Op, SDOperand N, SDOperand &Base,
72                              SDOperand &Offset);
73   bool SelectThumbAddrModeRI5(SDOperand Op, SDOperand N, unsigned Scale,
74                               SDOperand &Base, SDOperand &OffImm,
75                               SDOperand &Offset);
76   bool SelectThumbAddrModeS1(SDOperand Op, SDOperand N, SDOperand &Base,
77                              SDOperand &OffImm, SDOperand &Offset);
78   bool SelectThumbAddrModeS2(SDOperand Op, SDOperand N, SDOperand &Base,
79                              SDOperand &OffImm, SDOperand &Offset);
80   bool SelectThumbAddrModeS4(SDOperand Op, SDOperand N, SDOperand &Base,
81                              SDOperand &OffImm, SDOperand &Offset);
82   bool SelectThumbAddrModeSP(SDOperand Op, SDOperand N, SDOperand &Base,
83                              SDOperand &OffImm);
84
85   bool SelectShifterOperandReg(SDOperand Op, SDOperand N, SDOperand &A,
86                                SDOperand &B, SDOperand &C);
87   
88   // Include the pieces autogenerated from the target description.
89 #include "ARMGenDAGISel.inc"
90 };
91 }
92
93 void ARMDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
94   DEBUG(BB->dump());
95
96   DAG.setRoot(SelectRoot(DAG.getRoot()));
97   DAG.RemoveDeadNodes();
98
99   ScheduleAndEmitDAG(DAG);
100 }
101
102 bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
103                                       SDOperand &Base, SDOperand &Offset,
104                                       SDOperand &Opc) {
105   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
106     Base = N;
107     if (N.getOpcode() == ISD::FrameIndex) {
108       int FI = cast<FrameIndexSDNode>(N)->getIndex();
109       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
110     } else if (N.getOpcode() == ARMISD::Wrapper) {
111       Base = N.getOperand(0);
112     }
113     Offset = CurDAG->getRegister(0, MVT::i32);
114     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
115                                                       ARM_AM::no_shift),
116                                     MVT::i32);
117     return true;
118   }
119   
120   // Match simple R +/- imm12 operands.
121   if (N.getOpcode() == ISD::ADD)
122     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
123       int RHSC = (int)RHS->getValue();
124       if ((RHSC >= 0 && RHSC < 0x1000) ||
125           (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
126         Base = N.getOperand(0);
127         if (Base.getOpcode() == ISD::FrameIndex) {
128           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
129           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
130         }
131         Offset = CurDAG->getRegister(0, MVT::i32);
132
133         ARM_AM::AddrOpc AddSub = ARM_AM::add;
134         if (RHSC < 0) {
135           AddSub = ARM_AM::sub;
136           RHSC = - RHSC;
137         }
138         Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
139                                                           ARM_AM::no_shift),
140                                         MVT::i32);
141         return true;
142       }
143     }
144   
145   // Otherwise this is R +/- [possibly shifted] R
146   ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
147   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
148   unsigned ShAmt = 0;
149   
150   Base   = N.getOperand(0);
151   Offset = N.getOperand(1);
152   
153   if (ShOpcVal != ARM_AM::no_shift) {
154     // Check to see if the RHS of the shift is a constant, if not, we can't fold
155     // it.
156     if (ConstantSDNode *Sh =
157            dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
158       ShAmt = Sh->getValue();
159       Offset = N.getOperand(1).getOperand(0);
160     } else {
161       ShOpcVal = ARM_AM::no_shift;
162     }
163   }
164   
165   // Try matching (R shl C) + (R).
166   if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
167     ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
168     if (ShOpcVal != ARM_AM::no_shift) {
169       // Check to see if the RHS of the shift is a constant, if not, we can't
170       // fold it.
171       if (ConstantSDNode *Sh =
172           dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
173         ShAmt = Sh->getValue();
174         Offset = N.getOperand(0).getOperand(0);
175         Base = N.getOperand(1);
176       } else {
177         ShOpcVal = ARM_AM::no_shift;
178       }
179     }
180   }
181   
182   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
183                                   MVT::i32);
184   return true;
185 }
186
187 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDOperand Op, SDOperand N,
188                                             SDOperand &Offset, SDOperand &Opc) {
189   unsigned Opcode = Op.getOpcode();
190   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
191     ? cast<LoadSDNode>(Op)->getAddressingMode()
192     : cast<StoreSDNode>(Op)->getAddressingMode();
193   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
194     ? ARM_AM::add : ARM_AM::sub;
195   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
196     int Val = (int)C->getValue();
197     if (Val >= 0 && Val < 0x1000) { // 12 bits.
198       Offset = CurDAG->getRegister(0, MVT::i32);
199       Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
200                                                         ARM_AM::no_shift),
201                                       MVT::i32);
202       return true;
203     }
204   }
205
206   Offset = N;
207   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
208   unsigned ShAmt = 0;
209   if (ShOpcVal != ARM_AM::no_shift) {
210     // Check to see if the RHS of the shift is a constant, if not, we can't fold
211     // it.
212     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
213       ShAmt = Sh->getValue();
214       Offset = N.getOperand(0);
215     } else {
216       ShOpcVal = ARM_AM::no_shift;
217     }
218   }
219
220   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
221                                   MVT::i32);
222   return true;
223 }
224
225
226 bool ARMDAGToDAGISel::SelectAddrMode3(SDOperand Op, SDOperand N,
227                                       SDOperand &Base, SDOperand &Offset,
228                                       SDOperand &Opc) {
229   if (N.getOpcode() == ISD::SUB) {
230     // X - C  is canonicalize to X + -C, no need to handle it here.
231     Base = N.getOperand(0);
232     Offset = N.getOperand(1);
233     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
234     return true;
235   }
236   
237   if (N.getOpcode() != ISD::ADD) {
238     Base = N;
239     if (N.getOpcode() == ISD::FrameIndex) {
240       int FI = cast<FrameIndexSDNode>(N)->getIndex();
241       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
242     }
243     Offset = CurDAG->getRegister(0, MVT::i32);
244     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
245     return true;
246   }
247   
248   // If the RHS is +/- imm8, fold into addr mode.
249   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
250     int RHSC = (int)RHS->getValue();
251     if ((RHSC >= 0 && RHSC < 256) ||
252         (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
253       Base = N.getOperand(0);
254       if (Base.getOpcode() == ISD::FrameIndex) {
255         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
256         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
257       }
258       Offset = CurDAG->getRegister(0, MVT::i32);
259
260       ARM_AM::AddrOpc AddSub = ARM_AM::add;
261       if (RHSC < 0) {
262         AddSub = ARM_AM::sub;
263         RHSC = - RHSC;
264       }
265       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
266       return true;
267     }
268   }
269   
270   Base = N.getOperand(0);
271   Offset = N.getOperand(1);
272   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
273   return true;
274 }
275
276 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDOperand Op, SDOperand N,
277                                             SDOperand &Offset, SDOperand &Opc) {
278   unsigned Opcode = Op.getOpcode();
279   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
280     ? cast<LoadSDNode>(Op)->getAddressingMode()
281     : cast<StoreSDNode>(Op)->getAddressingMode();
282   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
283     ? ARM_AM::add : ARM_AM::sub;
284   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
285     int Val = (int)C->getValue();
286     if (Val >= 0 && Val < 256) {
287       Offset = CurDAG->getRegister(0, MVT::i32);
288       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
289       return true;
290     }
291   }
292
293   Offset = N;
294   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
295   return true;
296 }
297
298
299 bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op, SDOperand N,
300                                       SDOperand &Base, SDOperand &Offset) {
301   if (N.getOpcode() != ISD::ADD) {
302     Base = N;
303     if (N.getOpcode() == ISD::FrameIndex) {
304       int FI = cast<FrameIndexSDNode>(N)->getIndex();
305       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
306     } else if (N.getOpcode() == ARMISD::Wrapper) {
307       Base = N.getOperand(0);
308     }
309     Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
310                                        MVT::i32);
311     return true;
312   }
313   
314   // If the RHS is +/- imm8, fold into addr mode.
315   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
316     int RHSC = (int)RHS->getValue();
317     if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied by 4.
318       RHSC >>= 2;
319       if ((RHSC >= 0 && RHSC < 256) ||
320           (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
321         Base = N.getOperand(0);
322         if (Base.getOpcode() == ISD::FrameIndex) {
323           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
324           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
325         }
326
327         ARM_AM::AddrOpc AddSub = ARM_AM::add;
328         if (RHSC < 0) {
329           AddSub = ARM_AM::sub;
330           RHSC = - RHSC;
331         }
332         Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
333                                            MVT::i32);
334         return true;
335       }
336     }
337   }
338   
339   Base = N;
340   Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
341                                      MVT::i32);
342   return true;
343 }
344
345 bool ARMDAGToDAGISel::SelectAddrModePC(SDOperand Op, SDOperand N,
346                                         SDOperand &Offset, SDOperand &Label) {
347   if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
348     Offset = N.getOperand(0);
349     SDOperand N1 = N.getOperand(1);
350     Label  = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getValue(),
351                                        MVT::i32);
352     return true;
353   }
354   return false;
355 }
356
357 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDOperand Op, SDOperand N,
358                                             SDOperand &Base, SDOperand &Offset){
359   if (N.getOpcode() != ISD::ADD) {
360     Base = N;
361     // We must materialize a zero in a reg! Returning an constant here won't
362     // work since its node is -1 so it won't get added to the selection queue.
363     // Explicitly issue a tMOVri8 node!
364     Offset = SDOperand(CurDAG->getTargetNode(ARM::tMOVri8, MVT::i32,
365                                     CurDAG->getTargetConstant(0, MVT::i32)), 0);
366     return true;
367   }
368
369   Base = N.getOperand(0);
370   Offset = N.getOperand(1);
371   return true;
372 }
373
374 bool
375 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDOperand Op, SDOperand N,
376                                         unsigned Scale, SDOperand &Base,
377                                         SDOperand &OffImm, SDOperand &Offset) {
378   if (Scale == 4) {
379     SDOperand TmpBase, TmpOffImm;
380     if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
381       return false;  // We want to select tLDRspi / tSTRspi instead.
382     if (N.getOpcode() == ARMISD::Wrapper &&
383         N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
384       return false;  // We want to select tLDRpci instead.
385   }
386
387   if (N.getOpcode() != ISD::ADD) {
388     Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
389     Offset = CurDAG->getRegister(0, MVT::i32);
390     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
391     return true;
392   }
393
394   // Thumb does not have [sp, r] address mode.
395   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
396   RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
397   if ((LHSR && LHSR->getReg() == ARM::SP) ||
398       (RHSR && RHSR->getReg() == ARM::SP)) {
399     Base = N;
400     Offset = CurDAG->getRegister(0, MVT::i32);
401     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
402     return true;
403   }
404
405   // If the RHS is + imm5 * scale, fold into addr mode.
406   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
407     int RHSC = (int)RHS->getValue();
408     if ((RHSC & (Scale-1)) == 0) {  // The constant is implicitly multiplied.
409       RHSC /= Scale;
410       if (RHSC >= 0 && RHSC < 32) {
411         Base = N.getOperand(0);
412         Offset = CurDAG->getRegister(0, MVT::i32);
413         OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
414         return true;
415       }
416     }
417   }
418
419   Base = N.getOperand(0);
420   Offset = N.getOperand(1);
421   OffImm = CurDAG->getTargetConstant(0, MVT::i32);
422   return true;
423 }
424
425 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
426                                             SDOperand &Base, SDOperand &OffImm,
427                                             SDOperand &Offset) {
428   return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
429 }
430
431 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
432                                             SDOperand &Base, SDOperand &OffImm,
433                                             SDOperand &Offset) {
434   return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
435 }
436
437 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
438                                             SDOperand &Base, SDOperand &OffImm,
439                                             SDOperand &Offset) {
440   return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
441 }
442
443 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
444                                            SDOperand &Base, SDOperand &OffImm) {
445   if (N.getOpcode() == ISD::FrameIndex) {
446     int FI = cast<FrameIndexSDNode>(N)->getIndex();
447     Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
448     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
449     return true;
450   }
451
452   if (N.getOpcode() != ISD::ADD)
453     return false;
454
455   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
456   if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
457       (LHSR && LHSR->getReg() == ARM::SP)) {
458     // If the RHS is + imm8 * scale, fold into addr mode.
459     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
460       int RHSC = (int)RHS->getValue();
461       if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied.
462         RHSC >>= 2;
463         if (RHSC >= 0 && RHSC < 256) {
464           Base = N.getOperand(0);
465           if (Base.getOpcode() == ISD::FrameIndex) {
466             int FI = cast<FrameIndexSDNode>(Base)->getIndex();
467             Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
468           }
469           OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
470           return true;
471         }
472       }
473     }
474   }
475   
476   return false;
477 }
478
479 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
480                                               SDOperand N, 
481                                               SDOperand &BaseReg,
482                                               SDOperand &ShReg,
483                                               SDOperand &Opc) {
484   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
485
486   // Don't match base register only case. That is matched to a separate
487   // lower complexity pattern with explicit register operand.
488   if (ShOpcVal == ARM_AM::no_shift) return false;
489   
490   BaseReg = N.getOperand(0);
491   unsigned ShImmVal = 0;
492   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
493     ShReg = CurDAG->getRegister(0, MVT::i32);
494     ShImmVal = RHS->getValue() & 31;
495   } else {
496     ShReg = N.getOperand(1);
497   }
498   Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
499                                   MVT::i32);
500   return true;
501 }
502
503
504 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
505   SDNode *N = Op.Val;
506   unsigned Opcode = N->getOpcode();
507
508   if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
509     return NULL;   // Already selected.
510
511   switch (N->getOpcode()) {
512   default: break;
513   case ISD::Constant: {
514     unsigned Val = cast<ConstantSDNode>(N)->getValue();
515     bool UseCP = true;
516     if (Subtarget->isThumb())
517       UseCP = (Val > 255 &&                          // MOV
518                ~Val > 255 &&                         // MOV + MVN
519                !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
520     else
521       UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
522                ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
523                !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
524     if (UseCP) {
525       SDOperand CPIdx =
526         CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
527                                       TLI.getPointerTy());
528
529       SDNode *ResNode;
530       if (Subtarget->isThumb())
531         ResNode = CurDAG->getTargetNode(ARM::tLDRpci, MVT::i32, MVT::Other,
532                                         CPIdx, CurDAG->getEntryNode());
533       else {
534         SDOperand Ops[] = {
535           CPIdx, 
536           CurDAG->getRegister(0, MVT::i32),
537           CurDAG->getTargetConstant(0, MVT::i32),
538           CurDAG->getEntryNode()
539         };
540         ResNode = CurDAG->getTargetNode(ARM::LDR, MVT::i32, MVT::Other, Ops, 4);
541       }
542       ReplaceUses(Op, SDOperand(ResNode, 0));
543       return NULL;
544     }
545       
546     // Other cases are autogenerated.
547     break;
548   }
549   case ISD::FrameIndex: {
550     // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
551     int FI = cast<FrameIndexSDNode>(N)->getIndex();
552     unsigned Opc = Subtarget->isThumb() ? ARM::tADDrSPi : ARM::ADDri;
553     SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
554     return CurDAG->SelectNodeTo(N, Opc, MVT::i32, TFI,
555                                 CurDAG->getTargetConstant(0, MVT::i32));
556   }
557   case ISD::ADD: {
558     // Select add sp, c to tADDhirr.
559     SDOperand N0 = Op.getOperand(0);
560     SDOperand N1 = Op.getOperand(1);
561     RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(Op.getOperand(0));
562     RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(Op.getOperand(1));
563     if (LHSR && LHSR->getReg() == ARM::SP) {
564       std::swap(N0, N1);
565       std::swap(LHSR, RHSR);
566     }
567     if (RHSR && RHSR->getReg() == ARM::SP) {
568       AddToISelQueue(N0);
569       AddToISelQueue(N1);
570       return CurDAG->SelectNodeTo(N, ARM::tADDhirr, Op.getValueType(), N0, N1);
571     }
572     break;
573   }
574   case ISD::MUL:
575     if (Subtarget->isThumb())
576       break;
577     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
578       unsigned RHSV = C->getValue();
579       if (!RHSV) break;
580       if (isPowerOf2_32(RHSV-1)) {  // 2^n+1?
581         SDOperand V = Op.getOperand(0);
582         AddToISelQueue(V);
583         unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
584         SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
585           CurDAG->getTargetConstant(ShImm, MVT::i32)
586         };
587         return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 4);
588       }
589       if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
590         SDOperand V = Op.getOperand(0);
591         AddToISelQueue(V);
592         unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
593         SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
594           CurDAG->getTargetConstant(ShImm, MVT::i32)
595         };
596         return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 4);
597       }
598     }
599     break;
600   case ARMISD::FMRRD:
601     AddToISelQueue(Op.getOperand(0));
602     return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
603                                  Op.getOperand(0));
604   case ARMISD::MULHILOU:
605     AddToISelQueue(Op.getOperand(0));
606     AddToISelQueue(Op.getOperand(1));
607     return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32,
608                                  Op.getOperand(0), Op.getOperand(1));
609   case ARMISD::MULHILOS:
610     AddToISelQueue(Op.getOperand(0));
611     AddToISelQueue(Op.getOperand(1));
612     return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32,
613                                  Op.getOperand(0), Op.getOperand(1));
614   case ISD::LOAD: {
615     LoadSDNode *LD = cast<LoadSDNode>(Op);
616     ISD::MemIndexedMode AM = LD->getAddressingMode();
617     MVT::ValueType LoadedVT = LD->getLoadedVT();
618     if (AM != ISD::UNINDEXED) {
619       SDOperand Offset, AMOpc;
620       bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
621       unsigned Opcode = 0;
622       bool Match = false;
623       if (LoadedVT == MVT::i32 &&
624           SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
625         Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
626         Match = true;
627       } else if (LoadedVT == MVT::i16 &&
628                  SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
629         Match = true;
630         Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
631           ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
632           : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
633       } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
634         if (LD->getExtensionType() == ISD::SEXTLOAD) {
635           if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
636             Match = true;
637             Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
638           }
639         } else {
640           if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
641             Match = true;
642             Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
643           }
644         }
645       }
646
647       if (Match) {
648         SDOperand Chain = LD->getChain();
649         SDOperand Base = LD->getBasePtr();
650         AddToISelQueue(Chain);
651         AddToISelQueue(Base);
652         AddToISelQueue(Offset);
653         SDOperand Ops[] = { Base, Offset, AMOpc, Chain };
654         return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
655                                      MVT::Other, Ops, 4);
656       }
657     }
658     // Other cases are autogenerated.
659     break;
660   }
661   }
662
663   return SelectCode(Op);
664 }
665
666 /// createARMISelDag - This pass converts a legalized DAG into a
667 /// ARM-specific DAG, ready for instruction scheduling.
668 ///
669 FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
670   return new ARMDAGToDAGISel(TM);
671 }