It is pointless to turn a UINT_TO_FP into an
[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 is distributed under the University of Illinois Open Source
6 // 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/Target/TargetLowering.h"
29 #include "llvm/Target/TargetOptions.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 using namespace llvm;
33
34 //===--------------------------------------------------------------------===//
35 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
36 /// instructions for SelectionDAG operations.
37 ///
38 namespace {
39 class ARMDAGToDAGISel : public SelectionDAGISel {
40   ARMTargetLowering Lowering;
41
42   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
43   /// make the right decision when generating code for different targets.
44   const ARMSubtarget *Subtarget;
45
46 public:
47   explicit ARMDAGToDAGISel(ARMTargetMachine &TM)
48     : SelectionDAGISel(Lowering), Lowering(TM),
49     Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
50   }
51
52   virtual const char *getPassName() const {
53     return "ARM Instruction Selection";
54   } 
55   
56   SDNode *Select(SDOperand Op);
57   virtual void InstructionSelect(SelectionDAG &DAG);
58   bool SelectAddrMode2(SDOperand Op, SDOperand N, SDOperand &Base,
59                        SDOperand &Offset, SDOperand &Opc);
60   bool SelectAddrMode2Offset(SDOperand Op, SDOperand N,
61                              SDOperand &Offset, SDOperand &Opc);
62   bool SelectAddrMode3(SDOperand Op, SDOperand N, SDOperand &Base,
63                        SDOperand &Offset, SDOperand &Opc);
64   bool SelectAddrMode3Offset(SDOperand Op, SDOperand N,
65                              SDOperand &Offset, SDOperand &Opc);
66   bool SelectAddrMode5(SDOperand Op, SDOperand N, SDOperand &Base,
67                        SDOperand &Offset);
68
69   bool SelectAddrModePC(SDOperand Op, SDOperand N, SDOperand &Offset,
70                          SDOperand &Label);
71
72   bool SelectThumbAddrModeRR(SDOperand Op, SDOperand N, SDOperand &Base,
73                              SDOperand &Offset);
74   bool SelectThumbAddrModeRI5(SDOperand Op, SDOperand N, unsigned Scale,
75                               SDOperand &Base, SDOperand &OffImm,
76                               SDOperand &Offset);
77   bool SelectThumbAddrModeS1(SDOperand Op, SDOperand N, SDOperand &Base,
78                              SDOperand &OffImm, SDOperand &Offset);
79   bool SelectThumbAddrModeS2(SDOperand Op, SDOperand N, SDOperand &Base,
80                              SDOperand &OffImm, SDOperand &Offset);
81   bool SelectThumbAddrModeS4(SDOperand Op, SDOperand N, SDOperand &Base,
82                              SDOperand &OffImm, SDOperand &Offset);
83   bool SelectThumbAddrModeSP(SDOperand Op, SDOperand N, SDOperand &Base,
84                              SDOperand &OffImm);
85
86   bool SelectShifterOperandReg(SDOperand Op, SDOperand N, SDOperand &A,
87                                SDOperand &B, SDOperand &C);
88   
89   // Include the pieces autogenerated from the target description.
90 #include "ARMGenDAGISel.inc"
91 };
92 }
93
94 void ARMDAGToDAGISel::InstructionSelect(SelectionDAG &DAG) {
95   DEBUG(BB->dump());
96
97   DAG.setRoot(SelectRoot(DAG.getRoot()));
98   DAG.RemoveDeadNodes();
99 }
100
101 bool ARMDAGToDAGISel::SelectAddrMode2(SDOperand Op, SDOperand N,
102                                       SDOperand &Base, SDOperand &Offset,
103                                       SDOperand &Opc) {
104   if (N.getOpcode() == ISD::MUL) {
105     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
106       // X * [3,5,9] -> X + X * [2,4,8] etc.
107       int RHSC = (int)RHS->getValue();
108       if (RHSC & 1) {
109         RHSC = RHSC & ~1;
110         ARM_AM::AddrOpc AddSub = ARM_AM::add;
111         if (RHSC < 0) {
112           AddSub = ARM_AM::sub;
113           RHSC = - RHSC;
114         }
115         if (isPowerOf2_32(RHSC)) {
116           unsigned ShAmt = Log2_32(RHSC);
117           Base = Offset = N.getOperand(0);
118           Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
119                                                             ARM_AM::lsl),
120                                           MVT::i32);
121           return true;
122         }
123       }
124     }
125   }
126
127   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
128     Base = N;
129     if (N.getOpcode() == ISD::FrameIndex) {
130       int FI = cast<FrameIndexSDNode>(N)->getIndex();
131       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
132     } else if (N.getOpcode() == ARMISD::Wrapper) {
133       Base = N.getOperand(0);
134     }
135     Offset = CurDAG->getRegister(0, MVT::i32);
136     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
137                                                       ARM_AM::no_shift),
138                                     MVT::i32);
139     return true;
140   }
141   
142   // Match simple R +/- imm12 operands.
143   if (N.getOpcode() == ISD::ADD)
144     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
145       int RHSC = (int)RHS->getValue();
146       if ((RHSC >= 0 && RHSC < 0x1000) ||
147           (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
148         Base = N.getOperand(0);
149         if (Base.getOpcode() == ISD::FrameIndex) {
150           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
151           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
152         }
153         Offset = CurDAG->getRegister(0, MVT::i32);
154
155         ARM_AM::AddrOpc AddSub = ARM_AM::add;
156         if (RHSC < 0) {
157           AddSub = ARM_AM::sub;
158           RHSC = - RHSC;
159         }
160         Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
161                                                           ARM_AM::no_shift),
162                                         MVT::i32);
163         return true;
164       }
165     }
166   
167   // Otherwise this is R +/- [possibly shifted] R
168   ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
169   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
170   unsigned ShAmt = 0;
171   
172   Base   = N.getOperand(0);
173   Offset = N.getOperand(1);
174   
175   if (ShOpcVal != ARM_AM::no_shift) {
176     // Check to see if the RHS of the shift is a constant, if not, we can't fold
177     // it.
178     if (ConstantSDNode *Sh =
179            dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
180       ShAmt = Sh->getValue();
181       Offset = N.getOperand(1).getOperand(0);
182     } else {
183       ShOpcVal = ARM_AM::no_shift;
184     }
185   }
186   
187   // Try matching (R shl C) + (R).
188   if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
189     ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
190     if (ShOpcVal != ARM_AM::no_shift) {
191       // Check to see if the RHS of the shift is a constant, if not, we can't
192       // fold it.
193       if (ConstantSDNode *Sh =
194           dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
195         ShAmt = Sh->getValue();
196         Offset = N.getOperand(0).getOperand(0);
197         Base = N.getOperand(1);
198       } else {
199         ShOpcVal = ARM_AM::no_shift;
200       }
201     }
202   }
203   
204   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
205                                   MVT::i32);
206   return true;
207 }
208
209 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDOperand Op, SDOperand N,
210                                             SDOperand &Offset, SDOperand &Opc) {
211   unsigned Opcode = Op.getOpcode();
212   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
213     ? cast<LoadSDNode>(Op)->getAddressingMode()
214     : cast<StoreSDNode>(Op)->getAddressingMode();
215   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
216     ? ARM_AM::add : ARM_AM::sub;
217   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
218     int Val = (int)C->getValue();
219     if (Val >= 0 && Val < 0x1000) { // 12 bits.
220       Offset = CurDAG->getRegister(0, MVT::i32);
221       Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
222                                                         ARM_AM::no_shift),
223                                       MVT::i32);
224       return true;
225     }
226   }
227
228   Offset = N;
229   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
230   unsigned ShAmt = 0;
231   if (ShOpcVal != ARM_AM::no_shift) {
232     // Check to see if the RHS of the shift is a constant, if not, we can't fold
233     // it.
234     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
235       ShAmt = Sh->getValue();
236       Offset = N.getOperand(0);
237     } else {
238       ShOpcVal = ARM_AM::no_shift;
239     }
240   }
241
242   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
243                                   MVT::i32);
244   return true;
245 }
246
247
248 bool ARMDAGToDAGISel::SelectAddrMode3(SDOperand Op, SDOperand N,
249                                       SDOperand &Base, SDOperand &Offset,
250                                       SDOperand &Opc) {
251   if (N.getOpcode() == ISD::SUB) {
252     // X - C  is canonicalize to X + -C, no need to handle it here.
253     Base = N.getOperand(0);
254     Offset = N.getOperand(1);
255     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
256     return true;
257   }
258   
259   if (N.getOpcode() != ISD::ADD) {
260     Base = N;
261     if (N.getOpcode() == ISD::FrameIndex) {
262       int FI = cast<FrameIndexSDNode>(N)->getIndex();
263       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
264     }
265     Offset = CurDAG->getRegister(0, MVT::i32);
266     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
267     return true;
268   }
269   
270   // If the RHS is +/- imm8, fold into addr mode.
271   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
272     int RHSC = (int)RHS->getValue();
273     if ((RHSC >= 0 && RHSC < 256) ||
274         (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
275       Base = N.getOperand(0);
276       if (Base.getOpcode() == ISD::FrameIndex) {
277         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
278         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
279       }
280       Offset = CurDAG->getRegister(0, MVT::i32);
281
282       ARM_AM::AddrOpc AddSub = ARM_AM::add;
283       if (RHSC < 0) {
284         AddSub = ARM_AM::sub;
285         RHSC = - RHSC;
286       }
287       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
288       return true;
289     }
290   }
291   
292   Base = N.getOperand(0);
293   Offset = N.getOperand(1);
294   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
295   return true;
296 }
297
298 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDOperand Op, SDOperand N,
299                                             SDOperand &Offset, SDOperand &Opc) {
300   unsigned Opcode = Op.getOpcode();
301   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
302     ? cast<LoadSDNode>(Op)->getAddressingMode()
303     : cast<StoreSDNode>(Op)->getAddressingMode();
304   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
305     ? ARM_AM::add : ARM_AM::sub;
306   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
307     int Val = (int)C->getValue();
308     if (Val >= 0 && Val < 256) {
309       Offset = CurDAG->getRegister(0, MVT::i32);
310       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
311       return true;
312     }
313   }
314
315   Offset = N;
316   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
317   return true;
318 }
319
320
321 bool ARMDAGToDAGISel::SelectAddrMode5(SDOperand Op, SDOperand N,
322                                       SDOperand &Base, SDOperand &Offset) {
323   if (N.getOpcode() != ISD::ADD) {
324     Base = N;
325     if (N.getOpcode() == ISD::FrameIndex) {
326       int FI = cast<FrameIndexSDNode>(N)->getIndex();
327       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
328     } else if (N.getOpcode() == ARMISD::Wrapper) {
329       Base = N.getOperand(0);
330     }
331     Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
332                                        MVT::i32);
333     return true;
334   }
335   
336   // If the RHS is +/- imm8, fold into addr mode.
337   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
338     int RHSC = (int)RHS->getValue();
339     if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied by 4.
340       RHSC >>= 2;
341       if ((RHSC >= 0 && RHSC < 256) ||
342           (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
343         Base = N.getOperand(0);
344         if (Base.getOpcode() == ISD::FrameIndex) {
345           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
346           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
347         }
348
349         ARM_AM::AddrOpc AddSub = ARM_AM::add;
350         if (RHSC < 0) {
351           AddSub = ARM_AM::sub;
352           RHSC = - RHSC;
353         }
354         Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
355                                            MVT::i32);
356         return true;
357       }
358     }
359   }
360   
361   Base = N;
362   Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
363                                      MVT::i32);
364   return true;
365 }
366
367 bool ARMDAGToDAGISel::SelectAddrModePC(SDOperand Op, SDOperand N,
368                                         SDOperand &Offset, SDOperand &Label) {
369   if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
370     Offset = N.getOperand(0);
371     SDOperand N1 = N.getOperand(1);
372     Label  = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getValue(),
373                                        MVT::i32);
374     return true;
375   }
376   return false;
377 }
378
379 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDOperand Op, SDOperand N,
380                                             SDOperand &Base, SDOperand &Offset){
381   if (N.getOpcode() != ISD::ADD) {
382     Base = N;
383     // We must materialize a zero in a reg! Returning an constant here won't
384     // work since its node is -1 so it won't get added to the selection queue.
385     // Explicitly issue a tMOVri8 node!
386     Offset = SDOperand(CurDAG->getTargetNode(ARM::tMOVi8, MVT::i32,
387                                     CurDAG->getTargetConstant(0, MVT::i32)), 0);
388     return true;
389   }
390
391   Base = N.getOperand(0);
392   Offset = N.getOperand(1);
393   return true;
394 }
395
396 bool
397 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDOperand Op, SDOperand N,
398                                         unsigned Scale, SDOperand &Base,
399                                         SDOperand &OffImm, SDOperand &Offset) {
400   if (Scale == 4) {
401     SDOperand TmpBase, TmpOffImm;
402     if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
403       return false;  // We want to select tLDRspi / tSTRspi instead.
404     if (N.getOpcode() == ARMISD::Wrapper &&
405         N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
406       return false;  // We want to select tLDRpci instead.
407   }
408
409   if (N.getOpcode() != ISD::ADD) {
410     Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
411     Offset = CurDAG->getRegister(0, MVT::i32);
412     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
413     return true;
414   }
415
416   // Thumb does not have [sp, r] address mode.
417   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
418   RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
419   if ((LHSR && LHSR->getReg() == ARM::SP) ||
420       (RHSR && RHSR->getReg() == ARM::SP)) {
421     Base = N;
422     Offset = CurDAG->getRegister(0, MVT::i32);
423     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
424     return true;
425   }
426
427   // If the RHS is + imm5 * scale, fold into addr mode.
428   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
429     int RHSC = (int)RHS->getValue();
430     if ((RHSC & (Scale-1)) == 0) {  // The constant is implicitly multiplied.
431       RHSC /= Scale;
432       if (RHSC >= 0 && RHSC < 32) {
433         Base = N.getOperand(0);
434         Offset = CurDAG->getRegister(0, MVT::i32);
435         OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
436         return true;
437       }
438     }
439   }
440
441   Base = N.getOperand(0);
442   Offset = N.getOperand(1);
443   OffImm = CurDAG->getTargetConstant(0, MVT::i32);
444   return true;
445 }
446
447 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDOperand Op, SDOperand N,
448                                             SDOperand &Base, SDOperand &OffImm,
449                                             SDOperand &Offset) {
450   return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
451 }
452
453 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDOperand Op, SDOperand N,
454                                             SDOperand &Base, SDOperand &OffImm,
455                                             SDOperand &Offset) {
456   return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
457 }
458
459 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDOperand Op, SDOperand N,
460                                             SDOperand &Base, SDOperand &OffImm,
461                                             SDOperand &Offset) {
462   return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
463 }
464
465 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDOperand Op, SDOperand N,
466                                            SDOperand &Base, SDOperand &OffImm) {
467   if (N.getOpcode() == ISD::FrameIndex) {
468     int FI = cast<FrameIndexSDNode>(N)->getIndex();
469     Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
470     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
471     return true;
472   }
473
474   if (N.getOpcode() != ISD::ADD)
475     return false;
476
477   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
478   if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
479       (LHSR && LHSR->getReg() == ARM::SP)) {
480     // If the RHS is + imm8 * scale, fold into addr mode.
481     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
482       int RHSC = (int)RHS->getValue();
483       if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied.
484         RHSC >>= 2;
485         if (RHSC >= 0 && RHSC < 256) {
486           Base = N.getOperand(0);
487           if (Base.getOpcode() == ISD::FrameIndex) {
488             int FI = cast<FrameIndexSDNode>(Base)->getIndex();
489             Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
490           }
491           OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
492           return true;
493         }
494       }
495     }
496   }
497   
498   return false;
499 }
500
501 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDOperand Op,
502                                               SDOperand N, 
503                                               SDOperand &BaseReg,
504                                               SDOperand &ShReg,
505                                               SDOperand &Opc) {
506   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
507
508   // Don't match base register only case. That is matched to a separate
509   // lower complexity pattern with explicit register operand.
510   if (ShOpcVal == ARM_AM::no_shift) return false;
511   
512   BaseReg = N.getOperand(0);
513   unsigned ShImmVal = 0;
514   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
515     ShReg = CurDAG->getRegister(0, MVT::i32);
516     ShImmVal = RHS->getValue() & 31;
517   } else {
518     ShReg = N.getOperand(1);
519   }
520   Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
521                                   MVT::i32);
522   return true;
523 }
524
525 /// getAL - Returns a ARMCC::AL immediate node.
526 static inline SDOperand getAL(SelectionDAG *CurDAG) {
527   return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
528 }
529
530
531 SDNode *ARMDAGToDAGISel::Select(SDOperand Op) {
532   SDNode *N = Op.Val;
533   unsigned Opcode = N->getOpcode();
534
535   if (Opcode >= ISD::BUILTIN_OP_END && Opcode < ARMISD::FIRST_NUMBER)
536     return NULL;   // Already selected.
537
538   switch (N->getOpcode()) {
539   default: break;
540   case ISD::Constant: {
541     unsigned Val = cast<ConstantSDNode>(N)->getValue();
542     bool UseCP = true;
543     if (Subtarget->isThumb())
544       UseCP = (Val > 255 &&                          // MOV
545                ~Val > 255 &&                         // MOV + MVN
546                !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
547     else
548       UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
549                ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
550                !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
551     if (UseCP) {
552       SDOperand CPIdx =
553         CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
554                                       TLI.getPointerTy());
555
556       SDNode *ResNode;
557       if (Subtarget->isThumb())
558         ResNode = CurDAG->getTargetNode(ARM::tLDRcp, MVT::i32, MVT::Other,
559                                         CPIdx, CurDAG->getEntryNode());
560       else {
561         SDOperand Ops[] = {
562           CPIdx, 
563           CurDAG->getRegister(0, MVT::i32),
564           CurDAG->getTargetConstant(0, MVT::i32),
565           getAL(CurDAG),
566           CurDAG->getRegister(0, MVT::i32),
567           CurDAG->getEntryNode()
568         };
569         ResNode=CurDAG->getTargetNode(ARM::LDRcp, MVT::i32, MVT::Other, Ops, 6);
570       }
571       ReplaceUses(Op, SDOperand(ResNode, 0));
572       return NULL;
573     }
574       
575     // Other cases are autogenerated.
576     break;
577   }
578   case ISD::FrameIndex: {
579     // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
580     int FI = cast<FrameIndexSDNode>(N)->getIndex();
581     SDOperand TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
582     if (Subtarget->isThumb())
583       return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
584                                   CurDAG->getTargetConstant(0, MVT::i32));
585     else {
586       SDOperand Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
587                           getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
588                           CurDAG->getRegister(0, MVT::i32) };
589       return CurDAG->SelectNodeTo(N, ARM::ADDri, MVT::i32, Ops, 5);
590     }
591   }
592   case ISD::ADD: {
593     // Select add sp, c to tADDhirr.
594     SDOperand N0 = Op.getOperand(0);
595     SDOperand N1 = Op.getOperand(1);
596     RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(Op.getOperand(0));
597     RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(Op.getOperand(1));
598     if (LHSR && LHSR->getReg() == ARM::SP) {
599       std::swap(N0, N1);
600       std::swap(LHSR, RHSR);
601     }
602     if (RHSR && RHSR->getReg() == ARM::SP) {
603       AddToISelQueue(N0);
604       AddToISelQueue(N1);
605       return CurDAG->SelectNodeTo(N, ARM::tADDhirr, Op.getValueType(), N0, N1);
606     }
607     break;
608   }
609   case ISD::MUL:
610     if (Subtarget->isThumb())
611       break;
612     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
613       unsigned RHSV = C->getValue();
614       if (!RHSV) break;
615       if (isPowerOf2_32(RHSV-1)) {  // 2^n+1?
616         SDOperand V = Op.getOperand(0);
617         AddToISelQueue(V);
618         unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV-1));
619         SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
620                             CurDAG->getTargetConstant(ShImm, MVT::i32),
621                             getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
622                             CurDAG->getRegister(0, MVT::i32) };
623         return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
624       }
625       if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
626         SDOperand V = Op.getOperand(0);
627         AddToISelQueue(V);
628         unsigned ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, Log2_32(RHSV+1));
629         SDOperand Ops[] = { V, V, CurDAG->getRegister(0, MVT::i32),
630                             CurDAG->getTargetConstant(ShImm, MVT::i32),
631                             getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
632                             CurDAG->getRegister(0, MVT::i32) };
633         return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
634       }
635     }
636     break;
637   case ARMISD::FMRRD:
638     AddToISelQueue(Op.getOperand(0));
639     return CurDAG->getTargetNode(ARM::FMRRD, MVT::i32, MVT::i32,
640                                  Op.getOperand(0), getAL(CurDAG),
641                                  CurDAG->getRegister(0, MVT::i32));
642   case ISD::UMUL_LOHI: {
643     AddToISelQueue(Op.getOperand(0));
644     AddToISelQueue(Op.getOperand(1));
645     SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1),
646                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
647                         CurDAG->getRegister(0, MVT::i32) };
648     return CurDAG->getTargetNode(ARM::UMULL, MVT::i32, MVT::i32, Ops, 5);
649   }
650   case ISD::SMUL_LOHI: {
651     AddToISelQueue(Op.getOperand(0));
652     AddToISelQueue(Op.getOperand(1));
653     SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1),
654                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
655                         CurDAG->getRegister(0, MVT::i32) };
656     return CurDAG->getTargetNode(ARM::SMULL, MVT::i32, MVT::i32, Ops, 5);
657   }
658   case ISD::LOAD: {
659     LoadSDNode *LD = cast<LoadSDNode>(Op);
660     ISD::MemIndexedMode AM = LD->getAddressingMode();
661     MVT LoadedVT = LD->getMemoryVT();
662     if (AM != ISD::UNINDEXED) {
663       SDOperand Offset, AMOpc;
664       bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
665       unsigned Opcode = 0;
666       bool Match = false;
667       if (LoadedVT == MVT::i32 &&
668           SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
669         Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
670         Match = true;
671       } else if (LoadedVT == MVT::i16 &&
672                  SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
673         Match = true;
674         Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
675           ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
676           : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
677       } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
678         if (LD->getExtensionType() == ISD::SEXTLOAD) {
679           if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
680             Match = true;
681             Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
682           }
683         } else {
684           if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
685             Match = true;
686             Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
687           }
688         }
689       }
690
691       if (Match) {
692         SDOperand Chain = LD->getChain();
693         SDOperand Base = LD->getBasePtr();
694         AddToISelQueue(Chain);
695         AddToISelQueue(Base);
696         AddToISelQueue(Offset);
697         SDOperand Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
698                            CurDAG->getRegister(0, MVT::i32), Chain };
699         return CurDAG->getTargetNode(Opcode, MVT::i32, MVT::i32,
700                                      MVT::Other, Ops, 6);
701       }
702     }
703     // Other cases are autogenerated.
704     break;
705   }
706   case ARMISD::BRCOND: {
707     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
708     // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
709     // Pattern complexity = 6  cost = 1  size = 0
710
711     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
712     // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
713     // Pattern complexity = 6  cost = 1  size = 0
714
715     unsigned Opc = Subtarget->isThumb() ? ARM::tBcc : ARM::Bcc;
716     SDOperand Chain = Op.getOperand(0);
717     SDOperand N1 = Op.getOperand(1);
718     SDOperand N2 = Op.getOperand(2);
719     SDOperand N3 = Op.getOperand(3);
720     SDOperand InFlag = Op.getOperand(4);
721     assert(N1.getOpcode() == ISD::BasicBlock);
722     assert(N2.getOpcode() == ISD::Constant);
723     assert(N3.getOpcode() == ISD::Register);
724
725     AddToISelQueue(Chain);
726     AddToISelQueue(N1);
727     AddToISelQueue(InFlag);
728     SDOperand Tmp2 = CurDAG->getTargetConstant(((unsigned)
729                                cast<ConstantSDNode>(N2)->getValue()), MVT::i32);
730     SDOperand Ops[] = { N1, Tmp2, N3, Chain, InFlag };
731     SDNode *ResNode = CurDAG->getTargetNode(Opc, MVT::Other, MVT::Flag, Ops, 5);
732     Chain = SDOperand(ResNode, 0);
733     if (Op.Val->getNumValues() == 2) {
734       InFlag = SDOperand(ResNode, 1);
735       ReplaceUses(SDOperand(Op.Val, 1), InFlag);
736     }
737     ReplaceUses(SDOperand(Op.Val, 0), SDOperand(Chain.Val, Chain.ResNo));
738     return NULL;
739   }
740   case ARMISD::CMOV: {
741     bool isThumb = Subtarget->isThumb();
742     MVT VT = Op.getValueType();
743     SDOperand N0 = Op.getOperand(0);
744     SDOperand N1 = Op.getOperand(1);
745     SDOperand N2 = Op.getOperand(2);
746     SDOperand N3 = Op.getOperand(3);
747     SDOperand InFlag = Op.getOperand(4);
748     assert(N2.getOpcode() == ISD::Constant);
749     assert(N3.getOpcode() == ISD::Register);
750
751     // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
752     // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
753     // Pattern complexity = 18  cost = 1  size = 0
754     SDOperand CPTmp0;
755     SDOperand CPTmp1;
756     SDOperand CPTmp2;
757     if (!isThumb && VT == MVT::i32 &&
758         SelectShifterOperandReg(Op, N1, CPTmp0, CPTmp1, CPTmp2)) {
759       AddToISelQueue(N0);
760       AddToISelQueue(CPTmp0);
761       AddToISelQueue(CPTmp1);
762       AddToISelQueue(CPTmp2);
763       AddToISelQueue(InFlag);
764       SDOperand Tmp2 = CurDAG->getTargetConstant(((unsigned)
765                                cast<ConstantSDNode>(N2)->getValue()), MVT::i32);
766       SDOperand Ops[] = { N0, CPTmp0, CPTmp1, CPTmp2, Tmp2, N3, InFlag };
767       return CurDAG->SelectNodeTo(Op.Val, ARM::MOVCCs, MVT::i32, Ops, 7);
768     }
769
770     // Pattern: (ARMcmov:i32 GPR:i32:$false,
771     //             (imm:i32)<<P:Predicate_so_imm>><<X:so_imm_XFORM>>:$true,
772     //             (imm:i32):$cc)
773     // Emits: (MOVCCi:i32 GPR:i32:$false,
774     //           (so_imm_XFORM:i32 (imm:i32):$true), (imm:i32):$cc)
775     // Pattern complexity = 10  cost = 1  size = 0
776     if (VT == MVT::i32 &&
777         N3.getOpcode() == ISD::Constant &&
778         Predicate_so_imm(N3.Val)) {
779       AddToISelQueue(N0);
780       AddToISelQueue(InFlag);
781       SDOperand Tmp1 = CurDAG->getTargetConstant(((unsigned)
782                                cast<ConstantSDNode>(N1)->getValue()), MVT::i32);
783       Tmp1 = Transform_so_imm_XFORM(Tmp1.Val);
784       SDOperand Tmp2 = CurDAG->getTargetConstant(((unsigned)
785                                cast<ConstantSDNode>(N2)->getValue()), MVT::i32);
786       SDOperand Ops[] = { N0, Tmp1, Tmp2, N3, InFlag };
787       return CurDAG->SelectNodeTo(Op.Val, ARM::MOVCCi, MVT::i32, Ops, 5);
788     }
789
790     // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
791     // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
792     // Pattern complexity = 6  cost = 1  size = 0
793     //
794     // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
795     // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
796     // Pattern complexity = 6  cost = 11  size = 0
797     //
798     // Also FCPYScc and FCPYDcc.
799     AddToISelQueue(N0);
800     AddToISelQueue(N1);
801     AddToISelQueue(InFlag);
802     SDOperand Tmp2 = CurDAG->getTargetConstant(((unsigned)
803                                cast<ConstantSDNode>(N2)->getValue()), MVT::i32);
804     SDOperand Ops[] = { N0, N1, Tmp2, N3, InFlag };
805     unsigned Opc = 0;
806     switch (VT.getSimpleVT()) {
807     default: assert(false && "Illegal conditional move type!");
808       break;
809     case MVT::i32:
810       Opc = isThumb ? ARM::tMOVCCr : ARM::MOVCCr;
811       break;
812     case MVT::f32:
813       Opc = ARM::FCPYScc;
814       break;
815     case MVT::f64:
816       Opc = ARM::FCPYDcc;
817       break; 
818     }
819     return CurDAG->SelectNodeTo(Op.Val, Opc, VT, Ops, 5);
820   }
821   case ARMISD::CNEG: {
822     MVT VT = Op.getValueType();
823     SDOperand N0 = Op.getOperand(0);
824     SDOperand N1 = Op.getOperand(1);
825     SDOperand N2 = Op.getOperand(2);
826     SDOperand N3 = Op.getOperand(3);
827     SDOperand InFlag = Op.getOperand(4);
828     assert(N2.getOpcode() == ISD::Constant);
829     assert(N3.getOpcode() == ISD::Register);
830
831     AddToISelQueue(N0);
832     AddToISelQueue(N1);
833     AddToISelQueue(InFlag);
834     SDOperand Tmp2 = CurDAG->getTargetConstant(((unsigned)
835                                cast<ConstantSDNode>(N2)->getValue()), MVT::i32);
836     SDOperand Ops[] = { N0, N1, Tmp2, N3, InFlag };
837     unsigned Opc = 0;
838     switch (VT.getSimpleVT()) {
839     default: assert(false && "Illegal conditional move type!");
840       break;
841     case MVT::f32:
842       Opc = ARM::FNEGScc;
843       break;
844     case MVT::f64:
845       Opc = ARM::FNEGDcc;
846       break; 
847     }
848     return CurDAG->SelectNodeTo(Op.Val, Opc, VT, Ops, 5);
849   }
850   }
851   return SelectCode(Op);
852 }
853
854 /// createARMISelDag - This pass converts a legalized DAG into a
855 /// ARM-specific DAG, ready for instruction scheduling.
856 ///
857 FunctionPass *llvm::createARMISelDag(ARMTargetMachine &TM) {
858   return new ARMDAGToDAGISel(TM);
859 }