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