Add trunc->branch support, this won't help with clang's i8->i1 truncations
[oota-llvm.git] / lib / Target / ARM / ARMFastISel.cpp
1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
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 the ARM-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // ARMGenFastISel.inc, which is #included here.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "ARM.h"
17 #include "ARMBaseInstrInfo.h"
18 #include "ARMCallingConv.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMSubtarget.h"
22 #include "ARMConstantPoolValue.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/Operator.h"
30 #include "llvm/CodeGen/Analysis.h"
31 #include "llvm/CodeGen/FastISel.h"
32 #include "llvm/CodeGen/FunctionLoweringInfo.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineConstantPool.h"
36 #include "llvm/CodeGen/MachineFrameInfo.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/PseudoSourceValue.h"
40 #include "llvm/Support/CallSite.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/GetElementPtrTypeIterator.h"
44 #include "llvm/Target/TargetData.h"
45 #include "llvm/Target/TargetInstrInfo.h"
46 #include "llvm/Target/TargetLowering.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Target/TargetOptions.h"
49 using namespace llvm;
50
51 static cl::opt<bool>
52 DisableARMFastISel("disable-arm-fast-isel",
53                     cl::desc("Turn off experimental ARM fast-isel support"),
54                     cl::init(false), cl::Hidden);
55
56 extern cl::opt<bool> EnableARMLongCalls;
57
58 namespace {
59
60   // All possible address modes, plus some.
61   typedef struct Address {
62     enum {
63       RegBase,
64       FrameIndexBase
65     } BaseType;
66
67     union {
68       unsigned Reg;
69       int FI;
70     } Base;
71
72     int Offset;
73     unsigned Scale;
74     unsigned PlusReg;
75
76     // Innocuous defaults for our address.
77     Address()
78      : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
79        Base.Reg = 0;
80      }
81   } Address;
82
83 class ARMFastISel : public FastISel {
84
85   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
86   /// make the right decision when generating code for different targets.
87   const ARMSubtarget *Subtarget;
88   const TargetMachine &TM;
89   const TargetInstrInfo &TII;
90   const TargetLowering &TLI;
91   ARMFunctionInfo *AFI;
92
93   // Convenience variables to avoid some queries.
94   bool isThumb;
95   LLVMContext *Context;
96
97   public:
98     explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
99     : FastISel(funcInfo),
100       TM(funcInfo.MF->getTarget()),
101       TII(*TM.getInstrInfo()),
102       TLI(*TM.getTargetLowering()) {
103       Subtarget = &TM.getSubtarget<ARMSubtarget>();
104       AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
105       isThumb = AFI->isThumbFunction();
106       Context = &funcInfo.Fn->getContext();
107     }
108
109     // Code from FastISel.cpp.
110     virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
111                                    const TargetRegisterClass *RC);
112     virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
113                                     const TargetRegisterClass *RC,
114                                     unsigned Op0, bool Op0IsKill);
115     virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
116                                      const TargetRegisterClass *RC,
117                                      unsigned Op0, bool Op0IsKill,
118                                      unsigned Op1, bool Op1IsKill);
119     virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
120                                       const TargetRegisterClass *RC,
121                                       unsigned Op0, bool Op0IsKill,
122                                       unsigned Op1, bool Op1IsKill,
123                                       unsigned Op2, bool Op2IsKill);
124     virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
125                                      const TargetRegisterClass *RC,
126                                      unsigned Op0, bool Op0IsKill,
127                                      uint64_t Imm);
128     virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
129                                      const TargetRegisterClass *RC,
130                                      unsigned Op0, bool Op0IsKill,
131                                      const ConstantFP *FPImm);
132     virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
133                                       const TargetRegisterClass *RC,
134                                       unsigned Op0, bool Op0IsKill,
135                                       unsigned Op1, bool Op1IsKill,
136                                       uint64_t Imm);
137     virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
138                                     const TargetRegisterClass *RC,
139                                     uint64_t Imm);
140
141     virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
142                                                 unsigned Op0, bool Op0IsKill,
143                                                 uint32_t Idx);
144
145     // Backend specific FastISel code.
146     virtual bool TargetSelectInstruction(const Instruction *I);
147     virtual unsigned TargetMaterializeConstant(const Constant *C);
148     virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
149
150   #include "ARMGenFastISel.inc"
151
152     // Instruction selection routines.
153   private:
154     bool SelectLoad(const Instruction *I);
155     bool SelectStore(const Instruction *I);
156     bool SelectBranch(const Instruction *I);
157     bool SelectCmp(const Instruction *I);
158     bool SelectFPExt(const Instruction *I);
159     bool SelectFPTrunc(const Instruction *I);
160     bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
161     bool SelectSIToFP(const Instruction *I);
162     bool SelectFPToSI(const Instruction *I);
163     bool SelectSDiv(const Instruction *I);
164     bool SelectSRem(const Instruction *I);
165     bool SelectCall(const Instruction *I);
166     bool SelectSelect(const Instruction *I);
167     bool SelectRet(const Instruction *I);
168
169     // Utility routines.
170   private:
171     bool isTypeLegal(const Type *Ty, MVT &VT);
172     bool isLoadTypeLegal(const Type *Ty, MVT &VT);
173     bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
174     bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
175     bool ARMComputeAddress(const Value *Obj, Address &Addr);
176     void ARMSimplifyAddress(Address &Addr, EVT VT);
177     unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
178     unsigned ARMMaterializeInt(const Constant *C, EVT VT);
179     unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
180     unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
181     unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
182     unsigned ARMSelectCallOp(const GlobalValue *GV);
183
184     // Call handling routines.
185   private:
186     bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
187                         unsigned &ResultReg);
188     CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
189     bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
190                          SmallVectorImpl<unsigned> &ArgRegs,
191                          SmallVectorImpl<MVT> &ArgVTs,
192                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
193                          SmallVectorImpl<unsigned> &RegArgs,
194                          CallingConv::ID CC,
195                          unsigned &NumBytes);
196     bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
197                     const Instruction *I, CallingConv::ID CC,
198                     unsigned &NumBytes);
199     bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
200
201     // OptionalDef handling routines.
202   private:
203     bool isARMNEONPred(const MachineInstr *MI);
204     bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
205     const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
206     void AddLoadStoreOperands(EVT VT, Address &Addr,
207                               const MachineInstrBuilder &MIB);
208 };
209
210 } // end anonymous namespace
211
212 #include "ARMGenCallingConv.inc"
213
214 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
215 // we don't care about implicit defs here, just places we'll need to add a
216 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
217 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
218   const TargetInstrDesc &TID = MI->getDesc();
219   if (!TID.hasOptionalDef())
220     return false;
221
222   // Look to see if our OptionalDef is defining CPSR or CCR.
223   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224     const MachineOperand &MO = MI->getOperand(i);
225     if (!MO.isReg() || !MO.isDef()) continue;
226     if (MO.getReg() == ARM::CPSR)
227       *CPSR = true;
228   }
229   return true;
230 }
231
232 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
233   const TargetInstrDesc &TID = MI->getDesc();
234
235   // If we're a thumb2 or not NEON function we were handled via isPredicable.
236   if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
237        AFI->isThumb2Function())
238     return false;
239
240   for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
241     if (TID.OpInfo[i].isPredicate())
242       return true;
243
244   return false;
245 }
246
247 // If the machine is predicable go ahead and add the predicate operands, if
248 // it needs default CC operands add those.
249 // TODO: If we want to support thumb1 then we'll need to deal with optional
250 // CPSR defs that need to be added before the remaining operands. See s_cc_out
251 // for descriptions why.
252 const MachineInstrBuilder &
253 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
254   MachineInstr *MI = &*MIB;
255
256   // Do we use a predicate? or...
257   // Are we NEON in ARM mode and have a predicate operand? If so, I know
258   // we're not predicable but add it anyways.
259   if (TII.isPredicable(MI) || isARMNEONPred(MI))
260     AddDefaultPred(MIB);
261
262   // Do we optionally set a predicate?  Preds is size > 0 iff the predicate
263   // defines CPSR. All other OptionalDefines in ARM are the CCR register.
264   bool CPSR = false;
265   if (DefinesOptionalPredicate(MI, &CPSR)) {
266     if (CPSR)
267       AddDefaultT1CC(MIB);
268     else
269       AddDefaultCC(MIB);
270   }
271   return MIB;
272 }
273
274 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
275                                     const TargetRegisterClass* RC) {
276   unsigned ResultReg = createResultReg(RC);
277   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
278
279   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
280   return ResultReg;
281 }
282
283 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
284                                      const TargetRegisterClass *RC,
285                                      unsigned Op0, bool Op0IsKill) {
286   unsigned ResultReg = createResultReg(RC);
287   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
288
289   if (II.getNumDefs() >= 1)
290     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
291                    .addReg(Op0, Op0IsKill * RegState::Kill));
292   else {
293     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
294                    .addReg(Op0, Op0IsKill * RegState::Kill));
295     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
296                    TII.get(TargetOpcode::COPY), ResultReg)
297                    .addReg(II.ImplicitDefs[0]));
298   }
299   return ResultReg;
300 }
301
302 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
303                                       const TargetRegisterClass *RC,
304                                       unsigned Op0, bool Op0IsKill,
305                                       unsigned Op1, bool Op1IsKill) {
306   unsigned ResultReg = createResultReg(RC);
307   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
308
309   if (II.getNumDefs() >= 1)
310     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
311                    .addReg(Op0, Op0IsKill * RegState::Kill)
312                    .addReg(Op1, Op1IsKill * RegState::Kill));
313   else {
314     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
315                    .addReg(Op0, Op0IsKill * RegState::Kill)
316                    .addReg(Op1, Op1IsKill * RegState::Kill));
317     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
318                            TII.get(TargetOpcode::COPY), ResultReg)
319                    .addReg(II.ImplicitDefs[0]));
320   }
321   return ResultReg;
322 }
323
324 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
325                                        const TargetRegisterClass *RC,
326                                        unsigned Op0, bool Op0IsKill,
327                                        unsigned Op1, bool Op1IsKill,
328                                        unsigned Op2, bool Op2IsKill) {
329   unsigned ResultReg = createResultReg(RC);
330   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
331
332   if (II.getNumDefs() >= 1)
333     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
334                    .addReg(Op0, Op0IsKill * RegState::Kill)
335                    .addReg(Op1, Op1IsKill * RegState::Kill)
336                    .addReg(Op2, Op2IsKill * RegState::Kill));
337   else {
338     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
339                    .addReg(Op0, Op0IsKill * RegState::Kill)
340                    .addReg(Op1, Op1IsKill * RegState::Kill)
341                    .addReg(Op2, Op2IsKill * RegState::Kill));
342     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
343                            TII.get(TargetOpcode::COPY), ResultReg)
344                    .addReg(II.ImplicitDefs[0]));
345   }
346   return ResultReg;
347 }
348
349 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
350                                       const TargetRegisterClass *RC,
351                                       unsigned Op0, bool Op0IsKill,
352                                       uint64_t Imm) {
353   unsigned ResultReg = createResultReg(RC);
354   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
355
356   if (II.getNumDefs() >= 1)
357     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
358                    .addReg(Op0, Op0IsKill * RegState::Kill)
359                    .addImm(Imm));
360   else {
361     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
362                    .addReg(Op0, Op0IsKill * RegState::Kill)
363                    .addImm(Imm));
364     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
365                            TII.get(TargetOpcode::COPY), ResultReg)
366                    .addReg(II.ImplicitDefs[0]));
367   }
368   return ResultReg;
369 }
370
371 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
372                                       const TargetRegisterClass *RC,
373                                       unsigned Op0, bool Op0IsKill,
374                                       const ConstantFP *FPImm) {
375   unsigned ResultReg = createResultReg(RC);
376   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
377
378   if (II.getNumDefs() >= 1)
379     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
380                    .addReg(Op0, Op0IsKill * RegState::Kill)
381                    .addFPImm(FPImm));
382   else {
383     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
384                    .addReg(Op0, Op0IsKill * RegState::Kill)
385                    .addFPImm(FPImm));
386     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
387                            TII.get(TargetOpcode::COPY), ResultReg)
388                    .addReg(II.ImplicitDefs[0]));
389   }
390   return ResultReg;
391 }
392
393 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
394                                        const TargetRegisterClass *RC,
395                                        unsigned Op0, bool Op0IsKill,
396                                        unsigned Op1, bool Op1IsKill,
397                                        uint64_t Imm) {
398   unsigned ResultReg = createResultReg(RC);
399   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
400
401   if (II.getNumDefs() >= 1)
402     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
403                    .addReg(Op0, Op0IsKill * RegState::Kill)
404                    .addReg(Op1, Op1IsKill * RegState::Kill)
405                    .addImm(Imm));
406   else {
407     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
408                    .addReg(Op0, Op0IsKill * RegState::Kill)
409                    .addReg(Op1, Op1IsKill * RegState::Kill)
410                    .addImm(Imm));
411     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
412                            TII.get(TargetOpcode::COPY), ResultReg)
413                    .addReg(II.ImplicitDefs[0]));
414   }
415   return ResultReg;
416 }
417
418 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
419                                      const TargetRegisterClass *RC,
420                                      uint64_t Imm) {
421   unsigned ResultReg = createResultReg(RC);
422   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
423
424   if (II.getNumDefs() >= 1)
425     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
426                    .addImm(Imm));
427   else {
428     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
429                    .addImm(Imm));
430     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
431                            TII.get(TargetOpcode::COPY), ResultReg)
432                    .addReg(II.ImplicitDefs[0]));
433   }
434   return ResultReg;
435 }
436
437 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
438                                                  unsigned Op0, bool Op0IsKill,
439                                                  uint32_t Idx) {
440   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
441   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
442          "Cannot yet extract from physregs");
443   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
444                          DL, TII.get(TargetOpcode::COPY), ResultReg)
445                  .addReg(Op0, getKillRegState(Op0IsKill), Idx));
446   return ResultReg;
447 }
448
449 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
450 // checks from the various callers.
451 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
452   if (VT == MVT::f64) return 0;
453
454   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
455   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
456                           TII.get(ARM::VMOVRS), MoveReg)
457                   .addReg(SrcReg));
458   return MoveReg;
459 }
460
461 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
462   if (VT == MVT::i64) return 0;
463
464   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
465   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
466                           TII.get(ARM::VMOVSR), MoveReg)
467                   .addReg(SrcReg));
468   return MoveReg;
469 }
470
471 // For double width floating point we need to materialize two constants
472 // (the high and the low) into integer registers then use a move to get
473 // the combined constant into an FP reg.
474 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
475   const APFloat Val = CFP->getValueAPF();
476   bool is64bit = VT == MVT::f64;
477
478   // This checks to see if we can use VFP3 instructions to materialize
479   // a constant, otherwise we have to go through the constant pool.
480   if (TLI.isFPImmLegal(Val, VT)) {
481     unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
482     unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
483     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
484                             DestReg)
485                     .addFPImm(CFP));
486     return DestReg;
487   }
488
489   // Require VFP2 for loading fp constants.
490   if (!Subtarget->hasVFP2()) return false;
491
492   // MachineConstantPool wants an explicit alignment.
493   unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
494   if (Align == 0) {
495     // TODO: Figure out if this is correct.
496     Align = TD.getTypeAllocSize(CFP->getType());
497   }
498   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
499   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
500   unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
501
502   // The extra reg is for addrmode5.
503   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
504                           DestReg)
505                   .addConstantPoolIndex(Idx)
506                   .addReg(0));
507   return DestReg;
508 }
509
510 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
511
512   // For now 32-bit only.
513   if (VT != MVT::i32) return false;
514
515   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
516
517   // If we can do this in a single instruction without a constant pool entry
518   // do so now.
519   const ConstantInt *CI = cast<ConstantInt>(C);
520   if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
521     unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
522     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
523                             TII.get(Opc), DestReg)
524                     .addImm(CI->getSExtValue()));
525     return DestReg;
526   }
527
528   // MachineConstantPool wants an explicit alignment.
529   unsigned Align = TD.getPrefTypeAlignment(C->getType());
530   if (Align == 0) {
531     // TODO: Figure out if this is correct.
532     Align = TD.getTypeAllocSize(C->getType());
533   }
534   unsigned Idx = MCP.getConstantPoolIndex(C, Align);
535
536   if (isThumb)
537     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
538                             TII.get(ARM::t2LDRpci), DestReg)
539                     .addConstantPoolIndex(Idx));
540   else
541     // The extra immediate is for addrmode2.
542     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
543                             TII.get(ARM::LDRcp), DestReg)
544                     .addConstantPoolIndex(Idx)
545                     .addImm(0));
546
547   return DestReg;
548 }
549
550 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
551   // For now 32-bit only.
552   if (VT != MVT::i32) return 0;
553
554   Reloc::Model RelocM = TM.getRelocationModel();
555
556   // TODO: No external globals for now.
557   if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
558
559   // TODO: Need more magic for ARM PIC.
560   if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
561
562   // MachineConstantPool wants an explicit alignment.
563   unsigned Align = TD.getPrefTypeAlignment(GV->getType());
564   if (Align == 0) {
565     // TODO: Figure out if this is correct.
566     Align = TD.getTypeAllocSize(GV->getType());
567   }
568
569   // Grab index.
570   unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
571   unsigned Id = AFI->createPICLabelUId();
572   ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
573                                                        ARMCP::CPValue, PCAdj);
574   unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
575
576   // Load value.
577   MachineInstrBuilder MIB;
578   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
579   if (isThumb) {
580     unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
581     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
582           .addConstantPoolIndex(Idx);
583     if (RelocM == Reloc::PIC_)
584       MIB.addImm(Id);
585   } else {
586     // The extra immediate is for addrmode2.
587     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
588                   DestReg)
589           .addConstantPoolIndex(Idx)
590           .addImm(0);
591   }
592   AddOptionalDefs(MIB);
593   return DestReg;
594 }
595
596 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
597   EVT VT = TLI.getValueType(C->getType(), true);
598
599   // Only handle simple types.
600   if (!VT.isSimple()) return 0;
601
602   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
603     return ARMMaterializeFP(CFP, VT);
604   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
605     return ARMMaterializeGV(GV, VT);
606   else if (isa<ConstantInt>(C))
607     return ARMMaterializeInt(C, VT);
608
609   return 0;
610 }
611
612 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
613   // Don't handle dynamic allocas.
614   if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
615
616   MVT VT;
617   if (!isLoadTypeLegal(AI->getType(), VT)) return false;
618
619   DenseMap<const AllocaInst*, int>::iterator SI =
620     FuncInfo.StaticAllocaMap.find(AI);
621
622   // This will get lowered later into the correct offsets and registers
623   // via rewriteXFrameIndex.
624   if (SI != FuncInfo.StaticAllocaMap.end()) {
625     TargetRegisterClass* RC = TLI.getRegClassFor(VT);
626     unsigned ResultReg = createResultReg(RC);
627     unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
628     AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
629                             TII.get(Opc), ResultReg)
630                             .addFrameIndex(SI->second)
631                             .addImm(0));
632     return ResultReg;
633   }
634
635   return 0;
636 }
637
638 bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
639   EVT evt = TLI.getValueType(Ty, true);
640
641   // Only handle simple types.
642   if (evt == MVT::Other || !evt.isSimple()) return false;
643   VT = evt.getSimpleVT();
644
645   // Handle all legal types, i.e. a register that will directly hold this
646   // value.
647   return TLI.isTypeLegal(VT);
648 }
649
650 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
651   if (isTypeLegal(Ty, VT)) return true;
652
653   // If this is a type than can be sign or zero-extended to a basic operation
654   // go ahead and accept it now.
655   if (VT == MVT::i8 || VT == MVT::i16)
656     return true;
657
658   return false;
659 }
660
661 // Computes the address to get to an object.
662 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
663   // Some boilerplate from the X86 FastISel.
664   const User *U = NULL;
665   unsigned Opcode = Instruction::UserOp1;
666   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
667     // Don't walk into other basic blocks unless the object is an alloca from
668     // another block, otherwise it may not have a virtual register assigned.
669     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
670         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
671       Opcode = I->getOpcode();
672       U = I;
673     }
674   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
675     Opcode = C->getOpcode();
676     U = C;
677   }
678
679   if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
680     if (Ty->getAddressSpace() > 255)
681       // Fast instruction selection doesn't support the special
682       // address spaces.
683       return false;
684
685   switch (Opcode) {
686     default:
687     break;
688     case Instruction::BitCast: {
689       // Look through bitcasts.
690       return ARMComputeAddress(U->getOperand(0), Addr);
691     }
692     case Instruction::IntToPtr: {
693       // Look past no-op inttoptrs.
694       if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
695         return ARMComputeAddress(U->getOperand(0), Addr);
696       break;
697     }
698     case Instruction::PtrToInt: {
699       // Look past no-op ptrtoints.
700       if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
701         return ARMComputeAddress(U->getOperand(0), Addr);
702       break;
703     }
704     case Instruction::GetElementPtr: {
705       Address SavedAddr = Addr;
706       int TmpOffset = Addr.Offset;
707
708       // Iterate through the GEP folding the constants into offsets where
709       // we can.
710       gep_type_iterator GTI = gep_type_begin(U);
711       for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
712            i != e; ++i, ++GTI) {
713         const Value *Op = *i;
714         if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
715           const StructLayout *SL = TD.getStructLayout(STy);
716           unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
717           TmpOffset += SL->getElementOffset(Idx);
718         } else {
719           uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
720           for (;;) {
721             if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
722               // Constant-offset addressing.
723               TmpOffset += CI->getSExtValue() * S;
724               break;
725             }
726             if (isa<AddOperator>(Op) &&
727                 (!isa<Instruction>(Op) ||
728                  FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
729                  == FuncInfo.MBB) &&
730                 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
731               // An add (in the same block) with a constant operand. Fold the
732               // constant.
733               ConstantInt *CI =
734               cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
735               TmpOffset += CI->getSExtValue() * S;
736               // Iterate on the other operand.
737               Op = cast<AddOperator>(Op)->getOperand(0);
738               continue;
739             }
740             // Unsupported
741             goto unsupported_gep;
742           }
743         }
744       }
745
746       // Try to grab the base operand now.
747       Addr.Offset = TmpOffset;
748       if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
749
750       // We failed, restore everything and try the other options.
751       Addr = SavedAddr;
752
753       unsupported_gep:
754       break;
755     }
756     case Instruction::Alloca: {
757       const AllocaInst *AI = cast<AllocaInst>(Obj);
758       DenseMap<const AllocaInst*, int>::iterator SI =
759         FuncInfo.StaticAllocaMap.find(AI);
760       if (SI != FuncInfo.StaticAllocaMap.end()) {
761         Addr.BaseType = Address::FrameIndexBase;
762         Addr.Base.FI = SI->second;
763         return true;
764       }
765       break;
766     }
767   }
768
769   // Materialize the global variable's address into a reg which can
770   // then be used later to load the variable.
771   if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
772     unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
773     if (Tmp == 0) return false;
774
775     Addr.Base.Reg = Tmp;
776     return true;
777   }
778
779   // Try to get this in a register if nothing else has worked.
780   if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
781   return Addr.Base.Reg != 0;
782 }
783
784 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
785
786   assert(VT.isSimple() && "Non-simple types are invalid here!");
787
788   bool needsLowering = false;
789   switch (VT.getSimpleVT().SimpleTy) {
790     default:
791       assert(false && "Unhandled load/store type!");
792     case MVT::i1:
793     case MVT::i8:
794     case MVT::i16:
795     case MVT::i32:
796       // Integer loads/stores handle 12-bit offsets.
797       needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
798       break;
799     case MVT::f32:
800     case MVT::f64:
801       // Floating point operands handle 8-bit offsets.
802       needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
803       break;
804   }
805
806   // If this is a stack pointer and the offset needs to be simplified then
807   // put the alloca address into a register, set the base type back to
808   // register and continue. This should almost never happen.
809   if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
810     TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
811                               ARM::GPRRegisterClass;
812     unsigned ResultReg = createResultReg(RC);
813     unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
814     AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
815                             TII.get(Opc), ResultReg)
816                             .addFrameIndex(Addr.Base.FI)
817                             .addImm(0));
818     Addr.Base.Reg = ResultReg;
819     Addr.BaseType = Address::RegBase;
820   }
821
822   // Since the offset is too large for the load/store instruction
823   // get the reg+offset into a register.
824   if (needsLowering) {
825     ARMCC::CondCodes Pred = ARMCC::AL;
826     unsigned PredReg = 0;
827
828     TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
829       ARM::GPRRegisterClass;
830     unsigned BaseReg = createResultReg(RC);
831
832     if (!isThumb)
833       emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
834                               BaseReg, Addr.Base.Reg, Addr.Offset,
835                               Pred, PredReg,
836                               static_cast<const ARMBaseInstrInfo&>(TII));
837     else {
838       assert(AFI->isThumb2Function());
839       emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
840                              BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
841                              static_cast<const ARMBaseInstrInfo&>(TII));
842     }
843     Addr.Offset = 0;
844     Addr.Base.Reg = BaseReg;
845   }
846 }
847
848 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
849                                        const MachineInstrBuilder &MIB) {
850   // addrmode5 output depends on the selection dag addressing dividing the
851   // offset by 4 that it then later multiplies. Do this here as well.
852   if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
853       VT.getSimpleVT().SimpleTy == MVT::f64)
854     Addr.Offset /= 4;
855
856   // Frame base works a bit differently. Handle it separately.
857   if (Addr.BaseType == Address::FrameIndexBase) {
858     int FI = Addr.Base.FI;
859     int Offset = Addr.Offset;
860     MachineMemOperand *MMO =
861           FuncInfo.MF->getMachineMemOperand(
862                                   MachinePointerInfo::getFixedStack(FI, Offset),
863                                   MachineMemOperand::MOLoad,
864                                   MFI.getObjectSize(FI),
865                                   MFI.getObjectAlignment(FI));
866     // Now add the rest of the operands.
867     MIB.addFrameIndex(FI);
868
869     // ARM halfword load/stores need an additional operand.
870     if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
871
872     MIB.addImm(Addr.Offset);
873     MIB.addMemOperand(MMO);
874   } else {
875     // Now add the rest of the operands.
876     MIB.addReg(Addr.Base.Reg);
877
878     // ARM halfword load/stores need an additional operand.
879     if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
880
881     MIB.addImm(Addr.Offset);
882   }
883   AddOptionalDefs(MIB);
884 }
885
886 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
887
888   assert(VT.isSimple() && "Non-simple types are invalid here!");
889   unsigned Opc;
890   TargetRegisterClass *RC;
891   switch (VT.getSimpleVT().SimpleTy) {
892     // This is mostly going to be Neon/vector support.
893     default: return false;
894     case MVT::i16:
895       Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
896       RC = ARM::GPRRegisterClass;
897       break;
898     case MVT::i8:
899       Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
900       RC = ARM::GPRRegisterClass;
901       break;
902     case MVT::i32:
903       Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
904       RC = ARM::GPRRegisterClass;
905       break;
906     case MVT::f32:
907       Opc = ARM::VLDRS;
908       RC = TLI.getRegClassFor(VT);
909       break;
910     case MVT::f64:
911       Opc = ARM::VLDRD;
912       RC = TLI.getRegClassFor(VT);
913       break;
914   }
915   // Simplify this down to something we can handle.
916   ARMSimplifyAddress(Addr, VT);
917
918   // Create the base instruction, then add the operands.
919   ResultReg = createResultReg(RC);
920   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
921                                     TII.get(Opc), ResultReg);
922   AddLoadStoreOperands(VT, Addr, MIB);
923   return true;
924 }
925
926 bool ARMFastISel::SelectLoad(const Instruction *I) {
927   // Verify we have a legal type before going any further.
928   MVT VT;
929   if (!isLoadTypeLegal(I->getType(), VT))
930     return false;
931
932   // See if we can handle this address.
933   Address Addr;
934   if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
935
936   unsigned ResultReg;
937   if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
938   UpdateValueMap(I, ResultReg);
939   return true;
940 }
941
942 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
943   unsigned StrOpc;
944   switch (VT.getSimpleVT().SimpleTy) {
945     // This is mostly going to be Neon/vector support.
946     default: return false;
947     case MVT::i1: {
948       unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
949                                                ARM::GPRRegisterClass);
950       unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
951       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
952                               TII.get(Opc), Res)
953                       .addReg(SrcReg).addImm(1));
954       SrcReg = Res;
955     } // Fallthrough here.
956     case MVT::i8:
957       StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
958       break;
959     case MVT::i16:
960       StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
961       break;
962     case MVT::i32:
963       StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
964       break;
965     case MVT::f32:
966       if (!Subtarget->hasVFP2()) return false;
967       StrOpc = ARM::VSTRS;
968       break;
969     case MVT::f64:
970       if (!Subtarget->hasVFP2()) return false;
971       StrOpc = ARM::VSTRD;
972       break;
973   }
974   // Simplify this down to something we can handle.
975   ARMSimplifyAddress(Addr, VT);
976
977   // Create the base instruction, then add the operands.
978   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
979                                     TII.get(StrOpc))
980                             .addReg(SrcReg, getKillRegState(true));
981   AddLoadStoreOperands(VT, Addr, MIB);
982   return true;
983 }
984
985 bool ARMFastISel::SelectStore(const Instruction *I) {
986   Value *Op0 = I->getOperand(0);
987   unsigned SrcReg = 0;
988
989   // Verify we have a legal type before going any further.
990   MVT VT;
991   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
992     return false;
993
994   // Get the value to be stored into a register.
995   SrcReg = getRegForValue(Op0);
996   if (SrcReg == 0) return false;
997
998   // See if we can handle this address.
999   Address Addr;
1000   if (!ARMComputeAddress(I->getOperand(1), Addr))
1001     return false;
1002
1003   if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
1004   return true;
1005 }
1006
1007 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1008   switch (Pred) {
1009     // Needs two compares...
1010     case CmpInst::FCMP_ONE:
1011     case CmpInst::FCMP_UEQ:
1012     default:
1013       // AL is our "false" for now. The other two need more compares.
1014       return ARMCC::AL;
1015     case CmpInst::ICMP_EQ:
1016     case CmpInst::FCMP_OEQ:
1017       return ARMCC::EQ;
1018     case CmpInst::ICMP_SGT:
1019     case CmpInst::FCMP_OGT:
1020       return ARMCC::GT;
1021     case CmpInst::ICMP_SGE:
1022     case CmpInst::FCMP_OGE:
1023       return ARMCC::GE;
1024     case CmpInst::ICMP_UGT:
1025     case CmpInst::FCMP_UGT:
1026       return ARMCC::HI;
1027     case CmpInst::FCMP_OLT:
1028       return ARMCC::MI;
1029     case CmpInst::ICMP_ULE:
1030     case CmpInst::FCMP_OLE:
1031       return ARMCC::LS;
1032     case CmpInst::FCMP_ORD:
1033       return ARMCC::VC;
1034     case CmpInst::FCMP_UNO:
1035       return ARMCC::VS;
1036     case CmpInst::FCMP_UGE:
1037       return ARMCC::PL;
1038     case CmpInst::ICMP_SLT:
1039     case CmpInst::FCMP_ULT:
1040       return ARMCC::LT;
1041     case CmpInst::ICMP_SLE:
1042     case CmpInst::FCMP_ULE:
1043       return ARMCC::LE;
1044     case CmpInst::FCMP_UNE:
1045     case CmpInst::ICMP_NE:
1046       return ARMCC::NE;
1047     case CmpInst::ICMP_UGE:
1048       return ARMCC::HS;
1049     case CmpInst::ICMP_ULT:
1050       return ARMCC::LO;
1051   }
1052 }
1053
1054 bool ARMFastISel::SelectBranch(const Instruction *I) {
1055   const BranchInst *BI = cast<BranchInst>(I);
1056   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1057   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1058
1059   // Simple branch support.
1060
1061   // If we can, avoid recomputing the compare - redoing it could lead to wonky
1062   // behavior.
1063   // TODO: Factor this out.
1064   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1065     if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1066       MVT VT;
1067       const Type *Ty = CI->getOperand(0)->getType();
1068       if (!isTypeLegal(Ty, VT))
1069         return false;
1070
1071       bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1072       if (isFloat && !Subtarget->hasVFP2())
1073         return false;
1074
1075       unsigned CmpOpc;
1076       switch (VT.SimpleTy) {
1077         default: return false;
1078         // TODO: Verify compares.
1079         case MVT::f32:
1080           CmpOpc = ARM::VCMPES;
1081           break;
1082         case MVT::f64:
1083           CmpOpc = ARM::VCMPED;
1084           break;
1085         case MVT::i32:
1086           CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1087           break;
1088       }
1089
1090       // Get the compare predicate.
1091       ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1092
1093       // We may not handle every CC for now.
1094       if (ARMPred == ARMCC::AL) return false;
1095
1096       unsigned Arg1 = getRegForValue(CI->getOperand(0));
1097       if (Arg1 == 0) return false;
1098
1099       unsigned Arg2 = getRegForValue(CI->getOperand(1));
1100       if (Arg2 == 0) return false;
1101
1102       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1103                               TII.get(CmpOpc))
1104                       .addReg(Arg1).addReg(Arg2));
1105
1106       // For floating point we need to move the result to a comparison register
1107       // that we can then use for branches.
1108       if (isFloat)
1109         AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1110                                 TII.get(ARM::FMSTAT)));
1111
1112       unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1113       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1114       .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1115       FastEmitBranch(FBB, DL);
1116       FuncInfo.MBB->addSuccessor(TBB);
1117       return true;
1118     }
1119   } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1120     MVT SourceVT;
1121     if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1122         (isTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1123       unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1124       unsigned OpReg = getRegForValue(TI->getOperand(0));
1125       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1126                               TII.get(TstOpc))
1127                       .addReg(OpReg).addImm(1));
1128
1129       unsigned CCMode = ARMCC::NE;
1130       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1131         std::swap(TBB, FBB);
1132         CCMode = ARMCC::EQ;
1133       }
1134
1135       unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1136       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1137       .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1138
1139       FastEmitBranch(FBB, DL);
1140       FuncInfo.MBB->addSuccessor(TBB);
1141       return true;
1142     }
1143   }
1144
1145   unsigned CmpReg = getRegForValue(BI->getCondition());
1146   if (CmpReg == 0) return false;
1147
1148   // We've been divorced from our compare!  Our block was split, and
1149   // now our compare lives in a predecessor block.  We musn't
1150   // re-compare here, as the children of the compare aren't guaranteed
1151   // live across the block boundary (we *could* check for this).
1152   // Regardless, the compare has been done in the predecessor block,
1153   // and it left a value for us in a virtual register.  Ergo, we test
1154   // the one-bit value left in the virtual register.
1155   unsigned TstOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1156   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1157                   .addReg(CmpReg).addImm(1));
1158
1159   unsigned CCMode = ARMCC::NE;
1160   if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1161     std::swap(TBB, FBB);
1162     CCMode = ARMCC::EQ;
1163   }
1164
1165   unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1166   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1167                   .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1168   FastEmitBranch(FBB, DL);
1169   FuncInfo.MBB->addSuccessor(TBB);
1170   return true;
1171 }
1172
1173 bool ARMFastISel::SelectCmp(const Instruction *I) {
1174   const CmpInst *CI = cast<CmpInst>(I);
1175
1176   MVT VT;
1177   const Type *Ty = CI->getOperand(0)->getType();
1178   if (!isTypeLegal(Ty, VT))
1179     return false;
1180
1181   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1182   if (isFloat && !Subtarget->hasVFP2())
1183     return false;
1184
1185   unsigned CmpOpc;
1186   unsigned CondReg;
1187   switch (VT.SimpleTy) {
1188     default: return false;
1189     // TODO: Verify compares.
1190     case MVT::f32:
1191       CmpOpc = ARM::VCMPES;
1192       CondReg = ARM::FPSCR;
1193       break;
1194     case MVT::f64:
1195       CmpOpc = ARM::VCMPED;
1196       CondReg = ARM::FPSCR;
1197       break;
1198     case MVT::i32:
1199       CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1200       CondReg = ARM::CPSR;
1201       break;
1202   }
1203
1204   // Get the compare predicate.
1205   ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1206
1207   // We may not handle every CC for now.
1208   if (ARMPred == ARMCC::AL) return false;
1209
1210   unsigned Arg1 = getRegForValue(CI->getOperand(0));
1211   if (Arg1 == 0) return false;
1212
1213   unsigned Arg2 = getRegForValue(CI->getOperand(1));
1214   if (Arg2 == 0) return false;
1215
1216   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1217                   .addReg(Arg1).addReg(Arg2));
1218
1219   // For floating point we need to move the result to a comparison register
1220   // that we can then use for branches.
1221   if (isFloat)
1222     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1223                             TII.get(ARM::FMSTAT)));
1224
1225   // Now set a register based on the comparison. Explicitly set the predicates
1226   // here.
1227   unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
1228   TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
1229                                     : ARM::GPRRegisterClass;
1230   unsigned DestReg = createResultReg(RC);
1231   Constant *Zero
1232     = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1233   unsigned ZeroReg = TargetMaterializeConstant(Zero);
1234   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1235           .addReg(ZeroReg).addImm(1)
1236           .addImm(ARMPred).addReg(CondReg);
1237
1238   UpdateValueMap(I, DestReg);
1239   return true;
1240 }
1241
1242 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1243   // Make sure we have VFP and that we're extending float to double.
1244   if (!Subtarget->hasVFP2()) return false;
1245
1246   Value *V = I->getOperand(0);
1247   if (!I->getType()->isDoubleTy() ||
1248       !V->getType()->isFloatTy()) return false;
1249
1250   unsigned Op = getRegForValue(V);
1251   if (Op == 0) return false;
1252
1253   unsigned Result = createResultReg(ARM::DPRRegisterClass);
1254   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1255                           TII.get(ARM::VCVTDS), Result)
1256                   .addReg(Op));
1257   UpdateValueMap(I, Result);
1258   return true;
1259 }
1260
1261 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1262   // Make sure we have VFP and that we're truncating double to float.
1263   if (!Subtarget->hasVFP2()) return false;
1264
1265   Value *V = I->getOperand(0);
1266   if (!(I->getType()->isFloatTy() &&
1267         V->getType()->isDoubleTy())) return false;
1268
1269   unsigned Op = getRegForValue(V);
1270   if (Op == 0) return false;
1271
1272   unsigned Result = createResultReg(ARM::SPRRegisterClass);
1273   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1274                           TII.get(ARM::VCVTSD), Result)
1275                   .addReg(Op));
1276   UpdateValueMap(I, Result);
1277   return true;
1278 }
1279
1280 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1281   // Make sure we have VFP.
1282   if (!Subtarget->hasVFP2()) return false;
1283
1284   MVT DstVT;
1285   const Type *Ty = I->getType();
1286   if (!isTypeLegal(Ty, DstVT))
1287     return false;
1288
1289   unsigned Op = getRegForValue(I->getOperand(0));
1290   if (Op == 0) return false;
1291
1292   // The conversion routine works on fp-reg to fp-reg and the operand above
1293   // was an integer, move it to the fp registers if possible.
1294   unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
1295   if (FP == 0) return false;
1296
1297   unsigned Opc;
1298   if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1299   else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1300   else return 0;
1301
1302   unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1303   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1304                           ResultReg)
1305                   .addReg(FP));
1306   UpdateValueMap(I, ResultReg);
1307   return true;
1308 }
1309
1310 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1311   // Make sure we have VFP.
1312   if (!Subtarget->hasVFP2()) return false;
1313
1314   MVT DstVT;
1315   const Type *RetTy = I->getType();
1316   if (!isTypeLegal(RetTy, DstVT))
1317     return false;
1318
1319   unsigned Op = getRegForValue(I->getOperand(0));
1320   if (Op == 0) return false;
1321
1322   unsigned Opc;
1323   const Type *OpTy = I->getOperand(0)->getType();
1324   if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1325   else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1326   else return 0;
1327
1328   // f64->s32 or f32->s32 both need an intermediate f32 reg.
1329   unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1330   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1331                           ResultReg)
1332                   .addReg(Op));
1333
1334   // This result needs to be in an integer register, but the conversion only
1335   // takes place in fp-regs.
1336   unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1337   if (IntReg == 0) return false;
1338
1339   UpdateValueMap(I, IntReg);
1340   return true;
1341 }
1342
1343 bool ARMFastISel::SelectSelect(const Instruction *I) {
1344   MVT VT;
1345   if (!isTypeLegal(I->getType(), VT))
1346     return false;
1347
1348   // Things need to be register sized for register moves.
1349   if (VT != MVT::i32) return false;
1350   const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1351
1352   unsigned CondReg = getRegForValue(I->getOperand(0));
1353   if (CondReg == 0) return false;
1354   unsigned Op1Reg = getRegForValue(I->getOperand(1));
1355   if (Op1Reg == 0) return false;
1356   unsigned Op2Reg = getRegForValue(I->getOperand(2));
1357   if (Op2Reg == 0) return false;
1358
1359   unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1360   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1361                   .addReg(CondReg).addImm(1));
1362   unsigned ResultReg = createResultReg(RC);
1363   unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1364   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1365     .addReg(Op1Reg).addReg(Op2Reg)
1366     .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1367   UpdateValueMap(I, ResultReg);
1368   return true;
1369 }
1370
1371 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1372   MVT VT;
1373   const Type *Ty = I->getType();
1374   if (!isTypeLegal(Ty, VT))
1375     return false;
1376
1377   // If we have integer div support we should have selected this automagically.
1378   // In case we have a real miss go ahead and return false and we'll pick
1379   // it up later.
1380   if (Subtarget->hasDivide()) return false;
1381
1382   // Otherwise emit a libcall.
1383   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1384   if (VT == MVT::i8)
1385     LC = RTLIB::SDIV_I8;
1386   else if (VT == MVT::i16)
1387     LC = RTLIB::SDIV_I16;
1388   else if (VT == MVT::i32)
1389     LC = RTLIB::SDIV_I32;
1390   else if (VT == MVT::i64)
1391     LC = RTLIB::SDIV_I64;
1392   else if (VT == MVT::i128)
1393     LC = RTLIB::SDIV_I128;
1394   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1395
1396   return ARMEmitLibcall(I, LC);
1397 }
1398
1399 bool ARMFastISel::SelectSRem(const Instruction *I) {
1400   MVT VT;
1401   const Type *Ty = I->getType();
1402   if (!isTypeLegal(Ty, VT))
1403     return false;
1404
1405   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1406   if (VT == MVT::i8)
1407     LC = RTLIB::SREM_I8;
1408   else if (VT == MVT::i16)
1409     LC = RTLIB::SREM_I16;
1410   else if (VT == MVT::i32)
1411     LC = RTLIB::SREM_I32;
1412   else if (VT == MVT::i64)
1413     LC = RTLIB::SREM_I64;
1414   else if (VT == MVT::i128)
1415     LC = RTLIB::SREM_I128;
1416   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1417
1418   return ARMEmitLibcall(I, LC);
1419 }
1420
1421 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1422   EVT VT  = TLI.getValueType(I->getType(), true);
1423
1424   // We can get here in the case when we want to use NEON for our fp
1425   // operations, but can't figure out how to. Just use the vfp instructions
1426   // if we have them.
1427   // FIXME: It'd be nice to use NEON instructions.
1428   const Type *Ty = I->getType();
1429   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1430   if (isFloat && !Subtarget->hasVFP2())
1431     return false;
1432
1433   unsigned Op1 = getRegForValue(I->getOperand(0));
1434   if (Op1 == 0) return false;
1435
1436   unsigned Op2 = getRegForValue(I->getOperand(1));
1437   if (Op2 == 0) return false;
1438
1439   unsigned Opc;
1440   bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1441   switch (ISDOpcode) {
1442     default: return false;
1443     case ISD::FADD:
1444       Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1445       break;
1446     case ISD::FSUB:
1447       Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1448       break;
1449     case ISD::FMUL:
1450       Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1451       break;
1452   }
1453   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1454   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1455                           TII.get(Opc), ResultReg)
1456                   .addReg(Op1).addReg(Op2));
1457   UpdateValueMap(I, ResultReg);
1458   return true;
1459 }
1460
1461 // Call Handling Code
1462
1463 bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1464                                  EVT SrcVT, unsigned &ResultReg) {
1465   unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1466                            Src, /*TODO: Kill=*/false);
1467
1468   if (RR != 0) {
1469     ResultReg = RR;
1470     return true;
1471   } else
1472     return false;
1473 }
1474
1475 // This is largely taken directly from CCAssignFnForNode - we don't support
1476 // varargs in FastISel so that part has been removed.
1477 // TODO: We may not support all of this.
1478 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1479   switch (CC) {
1480   default:
1481     llvm_unreachable("Unsupported calling convention");
1482   case CallingConv::Fast:
1483     // Ignore fastcc. Silence compiler warnings.
1484     (void)RetFastCC_ARM_APCS;
1485     (void)FastCC_ARM_APCS;
1486     // Fallthrough
1487   case CallingConv::C:
1488     // Use target triple & subtarget features to do actual dispatch.
1489     if (Subtarget->isAAPCS_ABI()) {
1490       if (Subtarget->hasVFP2() &&
1491           FloatABIType == FloatABI::Hard)
1492         return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1493       else
1494         return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1495     } else
1496         return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1497   case CallingConv::ARM_AAPCS_VFP:
1498     return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1499   case CallingConv::ARM_AAPCS:
1500     return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1501   case CallingConv::ARM_APCS:
1502     return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1503   }
1504 }
1505
1506 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1507                                   SmallVectorImpl<unsigned> &ArgRegs,
1508                                   SmallVectorImpl<MVT> &ArgVTs,
1509                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1510                                   SmallVectorImpl<unsigned> &RegArgs,
1511                                   CallingConv::ID CC,
1512                                   unsigned &NumBytes) {
1513   SmallVector<CCValAssign, 16> ArgLocs;
1514   CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1515   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1516
1517   // Get a count of how many bytes are to be pushed on the stack.
1518   NumBytes = CCInfo.getNextStackOffset();
1519
1520   // Issue CALLSEQ_START
1521   unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1522   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1523                           TII.get(AdjStackDown))
1524                   .addImm(NumBytes));
1525
1526   // Process the args.
1527   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1528     CCValAssign &VA = ArgLocs[i];
1529     unsigned Arg = ArgRegs[VA.getValNo()];
1530     MVT ArgVT = ArgVTs[VA.getValNo()];
1531
1532     // We don't handle NEON/vector parameters yet.
1533     if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1534       return false;
1535
1536     // Handle arg promotion, etc.
1537     switch (VA.getLocInfo()) {
1538       case CCValAssign::Full: break;
1539       case CCValAssign::SExt: {
1540         bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1541                                          Arg, ArgVT, Arg);
1542         assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
1543         Emitted = true;
1544         ArgVT = VA.getLocVT();
1545         break;
1546       }
1547       case CCValAssign::ZExt: {
1548         bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1549                                          Arg, ArgVT, Arg);
1550         assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
1551         Emitted = true;
1552         ArgVT = VA.getLocVT();
1553         break;
1554       }
1555       case CCValAssign::AExt: {
1556         bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1557                                          Arg, ArgVT, Arg);
1558         if (!Emitted)
1559           Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1560                                       Arg, ArgVT, Arg);
1561         if (!Emitted)
1562           Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1563                                       Arg, ArgVT, Arg);
1564
1565         assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
1566         ArgVT = VA.getLocVT();
1567         break;
1568       }
1569       case CCValAssign::BCvt: {
1570         unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1571                                  /*TODO: Kill=*/false);
1572         assert(BC != 0 && "Failed to emit a bitcast!");
1573         Arg = BC;
1574         ArgVT = VA.getLocVT();
1575         break;
1576       }
1577       default: llvm_unreachable("Unknown arg promotion!");
1578     }
1579
1580     // Now copy/store arg to correct locations.
1581     if (VA.isRegLoc() && !VA.needsCustom()) {
1582       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1583               VA.getLocReg())
1584       .addReg(Arg);
1585       RegArgs.push_back(VA.getLocReg());
1586     } else if (VA.needsCustom()) {
1587       // TODO: We need custom lowering for vector (v2f64) args.
1588       if (VA.getLocVT() != MVT::f64) return false;
1589
1590       CCValAssign &NextVA = ArgLocs[++i];
1591
1592       // TODO: Only handle register args for now.
1593       if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1594
1595       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1596                               TII.get(ARM::VMOVRRD), VA.getLocReg())
1597                       .addReg(NextVA.getLocReg(), RegState::Define)
1598                       .addReg(Arg));
1599       RegArgs.push_back(VA.getLocReg());
1600       RegArgs.push_back(NextVA.getLocReg());
1601     } else {
1602       assert(VA.isMemLoc());
1603       // Need to store on the stack.
1604       Address Addr;
1605       Addr.BaseType = Address::RegBase;
1606       Addr.Base.Reg = ARM::SP;
1607       Addr.Offset = VA.getLocMemOffset();
1608
1609       if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
1610     }
1611   }
1612   return true;
1613 }
1614
1615 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1616                              const Instruction *I, CallingConv::ID CC,
1617                              unsigned &NumBytes) {
1618   // Issue CALLSEQ_END
1619   unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1620   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1621                           TII.get(AdjStackUp))
1622                   .addImm(NumBytes).addImm(0));
1623
1624   // Now the return value.
1625   if (RetVT != MVT::isVoid) {
1626     SmallVector<CCValAssign, 16> RVLocs;
1627     CCState CCInfo(CC, false, TM, RVLocs, *Context);
1628     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1629
1630     // Copy all of the result registers out of their specified physreg.
1631     if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1632       // For this move we copy into two registers and then move into the
1633       // double fp reg we want.
1634       EVT DestVT = RVLocs[0].getValVT();
1635       TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1636       unsigned ResultReg = createResultReg(DstRC);
1637       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1638                               TII.get(ARM::VMOVDRR), ResultReg)
1639                       .addReg(RVLocs[0].getLocReg())
1640                       .addReg(RVLocs[1].getLocReg()));
1641
1642       UsedRegs.push_back(RVLocs[0].getLocReg());
1643       UsedRegs.push_back(RVLocs[1].getLocReg());
1644
1645       // Finally update the result.
1646       UpdateValueMap(I, ResultReg);
1647     } else {
1648       assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1649       EVT CopyVT = RVLocs[0].getValVT();
1650       TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1651
1652       unsigned ResultReg = createResultReg(DstRC);
1653       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1654               ResultReg).addReg(RVLocs[0].getLocReg());
1655       UsedRegs.push_back(RVLocs[0].getLocReg());
1656
1657       // Finally update the result.
1658       UpdateValueMap(I, ResultReg);
1659     }
1660   }
1661
1662   return true;
1663 }
1664
1665 bool ARMFastISel::SelectRet(const Instruction *I) {
1666   const ReturnInst *Ret = cast<ReturnInst>(I);
1667   const Function &F = *I->getParent()->getParent();
1668
1669   if (!FuncInfo.CanLowerReturn)
1670     return false;
1671
1672   if (F.isVarArg())
1673     return false;
1674
1675   CallingConv::ID CC = F.getCallingConv();
1676   if (Ret->getNumOperands() > 0) {
1677     SmallVector<ISD::OutputArg, 4> Outs;
1678     GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1679                   Outs, TLI);
1680
1681     // Analyze operands of the call, assigning locations to each operand.
1682     SmallVector<CCValAssign, 16> ValLocs;
1683     CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1684     CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1685
1686     const Value *RV = Ret->getOperand(0);
1687     unsigned Reg = getRegForValue(RV);
1688     if (Reg == 0)
1689       return false;
1690
1691     // Only handle a single return value for now.
1692     if (ValLocs.size() != 1)
1693       return false;
1694
1695     CCValAssign &VA = ValLocs[0];
1696
1697     // Don't bother handling odd stuff for now.
1698     if (VA.getLocInfo() != CCValAssign::Full)
1699       return false;
1700     // Only handle register returns for now.
1701     if (!VA.isRegLoc())
1702       return false;
1703     // TODO: For now, don't try to handle cases where getLocInfo()
1704     // says Full but the types don't match.
1705     if (TLI.getValueType(RV->getType()) != VA.getValVT())
1706       return false;
1707
1708     // Make the copy.
1709     unsigned SrcReg = Reg + VA.getValNo();
1710     unsigned DstReg = VA.getLocReg();
1711     const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1712     // Avoid a cross-class copy. This is very unlikely.
1713     if (!SrcRC->contains(DstReg))
1714       return false;
1715     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1716             DstReg).addReg(SrcReg);
1717
1718     // Mark the register as live out of the function.
1719     MRI.addLiveOut(VA.getLocReg());
1720   }
1721
1722   unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1723   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1724                           TII.get(RetOpc)));
1725   return true;
1726 }
1727
1728 unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1729
1730   // Darwin needs the r9 versions of the opcodes.
1731   bool isDarwin = Subtarget->isTargetDarwin();
1732   if (isThumb) {
1733     return isDarwin ? ARM::tBLr9 : ARM::tBL;
1734   } else  {
1735     return isDarwin ? ARM::BLr9 : ARM::BL;
1736   }
1737 }
1738
1739 // A quick function that will emit a call for a named libcall in F with the
1740 // vector of passed arguments for the Instruction in I. We can assume that we
1741 // can emit a call for any libcall we can produce. This is an abridged version
1742 // of the full call infrastructure since we won't need to worry about things
1743 // like computed function pointers or strange arguments at call sites.
1744 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1745 // with X86.
1746 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1747   CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1748
1749   // Handle *simple* calls for now.
1750   const Type *RetTy = I->getType();
1751   MVT RetVT;
1752   if (RetTy->isVoidTy())
1753     RetVT = MVT::isVoid;
1754   else if (!isTypeLegal(RetTy, RetVT))
1755     return false;
1756
1757   // TODO: For now if we have long calls specified we don't handle the call.
1758   if (EnableARMLongCalls) return false;
1759
1760   // Set up the argument vectors.
1761   SmallVector<Value*, 8> Args;
1762   SmallVector<unsigned, 8> ArgRegs;
1763   SmallVector<MVT, 8> ArgVTs;
1764   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1765   Args.reserve(I->getNumOperands());
1766   ArgRegs.reserve(I->getNumOperands());
1767   ArgVTs.reserve(I->getNumOperands());
1768   ArgFlags.reserve(I->getNumOperands());
1769   for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1770     Value *Op = I->getOperand(i);
1771     unsigned Arg = getRegForValue(Op);
1772     if (Arg == 0) return false;
1773
1774     const Type *ArgTy = Op->getType();
1775     MVT ArgVT;
1776     if (!isTypeLegal(ArgTy, ArgVT)) return false;
1777
1778     ISD::ArgFlagsTy Flags;
1779     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1780     Flags.setOrigAlign(OriginalAlignment);
1781
1782     Args.push_back(Op);
1783     ArgRegs.push_back(Arg);
1784     ArgVTs.push_back(ArgVT);
1785     ArgFlags.push_back(Flags);
1786   }
1787
1788   // Handle the arguments now that we've gotten them.
1789   SmallVector<unsigned, 4> RegArgs;
1790   unsigned NumBytes;
1791   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1792     return false;
1793
1794   // Issue the call, BLr9 for darwin, BL otherwise.
1795   // TODO: Turn this into the table of arm call ops.
1796   MachineInstrBuilder MIB;
1797   unsigned CallOpc = ARMSelectCallOp(NULL);
1798   if(isThumb)
1799     // Explicitly adding the predicate here.
1800     MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1801                          TII.get(CallOpc)))
1802                          .addExternalSymbol(TLI.getLibcallName(Call));
1803   else
1804     // Explicitly adding the predicate here.
1805     MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1806                          TII.get(CallOpc))
1807           .addExternalSymbol(TLI.getLibcallName(Call)));
1808
1809   // Add implicit physical register uses to the call.
1810   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1811     MIB.addReg(RegArgs[i]);
1812
1813   // Finish off the call including any return values.
1814   SmallVector<unsigned, 4> UsedRegs;
1815   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1816
1817   // Set all unused physreg defs as dead.
1818   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1819
1820   return true;
1821 }
1822
1823 bool ARMFastISel::SelectCall(const Instruction *I) {
1824   const CallInst *CI = cast<CallInst>(I);
1825   const Value *Callee = CI->getCalledValue();
1826
1827   // Can't handle inline asm or worry about intrinsics yet.
1828   if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1829
1830   // Only handle global variable Callees that are direct calls.
1831   const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1832   if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1833     return false;
1834
1835   // Check the calling convention.
1836   ImmutableCallSite CS(CI);
1837   CallingConv::ID CC = CS.getCallingConv();
1838
1839   // TODO: Avoid some calling conventions?
1840
1841   // Let SDISel handle vararg functions.
1842   const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1843   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1844   if (FTy->isVarArg())
1845     return false;
1846
1847   // Handle *simple* calls for now.
1848   const Type *RetTy = I->getType();
1849   MVT RetVT;
1850   if (RetTy->isVoidTy())
1851     RetVT = MVT::isVoid;
1852   else if (!isTypeLegal(RetTy, RetVT))
1853     return false;
1854
1855   // TODO: For now if we have long calls specified we don't handle the call.
1856   if (EnableARMLongCalls) return false;
1857
1858   // Set up the argument vectors.
1859   SmallVector<Value*, 8> Args;
1860   SmallVector<unsigned, 8> ArgRegs;
1861   SmallVector<MVT, 8> ArgVTs;
1862   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1863   Args.reserve(CS.arg_size());
1864   ArgRegs.reserve(CS.arg_size());
1865   ArgVTs.reserve(CS.arg_size());
1866   ArgFlags.reserve(CS.arg_size());
1867   for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1868        i != e; ++i) {
1869     unsigned Arg = getRegForValue(*i);
1870
1871     if (Arg == 0)
1872       return false;
1873     ISD::ArgFlagsTy Flags;
1874     unsigned AttrInd = i - CS.arg_begin() + 1;
1875     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1876       Flags.setSExt();
1877     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1878       Flags.setZExt();
1879
1880          // FIXME: Only handle *easy* calls for now.
1881     if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1882         CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1883         CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1884         CS.paramHasAttr(AttrInd, Attribute::ByVal))
1885       return false;
1886
1887     const Type *ArgTy = (*i)->getType();
1888     MVT ArgVT;
1889     if (!isTypeLegal(ArgTy, ArgVT))
1890       return false;
1891     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1892     Flags.setOrigAlign(OriginalAlignment);
1893
1894     Args.push_back(*i);
1895     ArgRegs.push_back(Arg);
1896     ArgVTs.push_back(ArgVT);
1897     ArgFlags.push_back(Flags);
1898   }
1899
1900   // Handle the arguments now that we've gotten them.
1901   SmallVector<unsigned, 4> RegArgs;
1902   unsigned NumBytes;
1903   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1904     return false;
1905
1906   // Issue the call, BLr9 for darwin, BL otherwise.
1907   // TODO: Turn this into the table of arm call ops.
1908   MachineInstrBuilder MIB;
1909   unsigned CallOpc = ARMSelectCallOp(GV);
1910   // Explicitly adding the predicate here.
1911   if(isThumb)
1912     // Explicitly adding the predicate here.
1913     MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1914                          TII.get(CallOpc)))
1915           .addGlobalAddress(GV, 0, 0);
1916   else
1917     // Explicitly adding the predicate here.
1918     MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1919                          TII.get(CallOpc))
1920           .addGlobalAddress(GV, 0, 0));
1921
1922   // Add implicit physical register uses to the call.
1923   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1924     MIB.addReg(RegArgs[i]);
1925
1926   // Finish off the call including any return values.
1927   SmallVector<unsigned, 4> UsedRegs;
1928   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1929
1930   // Set all unused physreg defs as dead.
1931   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1932
1933   return true;
1934
1935 }
1936
1937 // TODO: SoftFP support.
1938 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1939
1940   switch (I->getOpcode()) {
1941     case Instruction::Load:
1942       return SelectLoad(I);
1943     case Instruction::Store:
1944       return SelectStore(I);
1945     case Instruction::Br:
1946       return SelectBranch(I);
1947     case Instruction::ICmp:
1948     case Instruction::FCmp:
1949       return SelectCmp(I);
1950     case Instruction::FPExt:
1951       return SelectFPExt(I);
1952     case Instruction::FPTrunc:
1953       return SelectFPTrunc(I);
1954     case Instruction::SIToFP:
1955       return SelectSIToFP(I);
1956     case Instruction::FPToSI:
1957       return SelectFPToSI(I);
1958     case Instruction::FAdd:
1959       return SelectBinaryOp(I, ISD::FADD);
1960     case Instruction::FSub:
1961       return SelectBinaryOp(I, ISD::FSUB);
1962     case Instruction::FMul:
1963       return SelectBinaryOp(I, ISD::FMUL);
1964     case Instruction::SDiv:
1965       return SelectSDiv(I);
1966     case Instruction::SRem:
1967       return SelectSRem(I);
1968     case Instruction::Call:
1969       return SelectCall(I);
1970     case Instruction::Select:
1971       return SelectSelect(I);
1972     case Instruction::Ret:
1973       return SelectRet(I);
1974     default: break;
1975   }
1976   return false;
1977 }
1978
1979 namespace llvm {
1980   llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1981     // Completely untested on non-darwin.
1982     const TargetMachine &TM = funcInfo.MF->getTarget();
1983
1984     // Darwin and thumb1 only for now.
1985     const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
1986     if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
1987         !DisableARMFastISel)
1988       return new ARMFastISel(funcInfo);
1989     return 0;
1990   }
1991 }