Refactor arm fast isel libcall handling so that pieces can be used
[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 "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/Module.h"
28 #include "llvm/CodeGen/Analysis.h"
29 #include "llvm/CodeGen/FastISel.h"
30 #include "llvm/CodeGen/FunctionLoweringInfo.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineConstantPool.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/Support/CallSite.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/GetElementPtrTypeIterator.h"
40 #include "llvm/Target/TargetData.h"
41 #include "llvm/Target/TargetInstrInfo.h"
42 #include "llvm/Target/TargetLowering.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 using namespace llvm;
46
47 static cl::opt<bool>
48 EnableARMFastISel("arm-fast-isel",
49                   cl::desc("Turn on experimental ARM fast-isel support"),
50                   cl::init(false), cl::Hidden);
51
52 namespace {
53
54 class ARMFastISel : public FastISel {
55
56   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
57   /// make the right decision when generating code for different targets.
58   const ARMSubtarget *Subtarget;
59   const TargetMachine &TM;
60   const TargetInstrInfo &TII;
61   const TargetLowering &TLI;
62   const ARMFunctionInfo *AFI;
63
64   // Convenience variables to avoid some queries.
65   bool isThumb;
66   LLVMContext *Context;
67
68   public:
69     explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
70     : FastISel(funcInfo),
71       TM(funcInfo.MF->getTarget()),
72       TII(*TM.getInstrInfo()),
73       TLI(*TM.getTargetLowering()) {
74       Subtarget = &TM.getSubtarget<ARMSubtarget>();
75       AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
76       isThumb = AFI->isThumbFunction();
77       Context = &funcInfo.Fn->getContext();
78     }
79
80     // Code from FastISel.cpp.
81     virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
82                                    const TargetRegisterClass *RC);
83     virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
84                                     const TargetRegisterClass *RC,
85                                     unsigned Op0, bool Op0IsKill);
86     virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
87                                      const TargetRegisterClass *RC,
88                                      unsigned Op0, bool Op0IsKill,
89                                      unsigned Op1, bool Op1IsKill);
90     virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
91                                      const TargetRegisterClass *RC,
92                                      unsigned Op0, bool Op0IsKill,
93                                      uint64_t Imm);
94     virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
95                                      const TargetRegisterClass *RC,
96                                      unsigned Op0, bool Op0IsKill,
97                                      const ConstantFP *FPImm);
98     virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
99                                     const TargetRegisterClass *RC,
100                                     uint64_t Imm);
101     virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
102                                       const TargetRegisterClass *RC,
103                                       unsigned Op0, bool Op0IsKill,
104                                       unsigned Op1, bool Op1IsKill,
105                                       uint64_t Imm);
106     virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
107                                                 unsigned Op0, bool Op0IsKill,
108                                                 uint32_t Idx);
109
110     // Backend specific FastISel code.
111     virtual bool TargetSelectInstruction(const Instruction *I);
112     virtual unsigned TargetMaterializeConstant(const Constant *C);
113
114   #include "ARMGenFastISel.inc"
115
116     // Instruction selection routines.
117   private:
118     virtual bool SelectLoad(const Instruction *I);
119     virtual bool SelectStore(const Instruction *I);
120     virtual bool SelectBranch(const Instruction *I);
121     virtual bool SelectCmp(const Instruction *I);
122     virtual bool SelectFPExt(const Instruction *I);
123     virtual bool SelectFPTrunc(const Instruction *I);
124     virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
125     virtual bool SelectSIToFP(const Instruction *I);
126     virtual bool SelectFPToSI(const Instruction *I);
127     virtual bool SelectSDiv(const Instruction *I);
128
129     // Utility routines.
130   private:
131     bool isTypeLegal(const Type *Ty, EVT &VT);
132     bool isLoadTypeLegal(const Type *Ty, EVT &VT);
133     bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
134     bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
135     bool ARMLoadAlloca(const Instruction *I, EVT VT);
136     bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
137     bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
138     unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
139     unsigned ARMMaterializeInt(const Constant *C, EVT VT);
140     unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
141     unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
142
143     // Call handling routines.
144   private:
145     CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
146     bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, 
147                          SmallVectorImpl<unsigned> &ArgRegs,
148                          SmallVectorImpl<EVT> &ArgVTs,
149                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
150                          SmallVectorImpl<unsigned> &RegArgs,
151                          CallingConv::ID CC,
152                          unsigned &NumBytes);
153     bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
154                     const Instruction *I, CallingConv::ID CC,
155                     unsigned &NumBytes);
156     bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
157
158     // OptionalDef handling routines.
159   private:
160     bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
161     const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
162 };
163
164 } // end anonymous namespace
165
166 #include "ARMGenCallingConv.inc"
167
168 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
169 // we don't care about implicit defs here, just places we'll need to add a
170 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
171 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
172   const TargetInstrDesc &TID = MI->getDesc();
173   if (!TID.hasOptionalDef())
174     return false;
175
176   // Look to see if our OptionalDef is defining CPSR or CCR.
177   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178     const MachineOperand &MO = MI->getOperand(i);
179     if (!MO.isReg() || !MO.isDef()) continue;
180     if (MO.getReg() == ARM::CPSR)
181       *CPSR = true;
182   }
183   return true;
184 }
185
186 // If the machine is predicable go ahead and add the predicate operands, if
187 // it needs default CC operands add those.
188 const MachineInstrBuilder &
189 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
190   MachineInstr *MI = &*MIB;
191
192   // Do we use a predicate?
193   if (TII.isPredicable(MI))
194     AddDefaultPred(MIB);
195
196   // Do we optionally set a predicate?  Preds is size > 0 iff the predicate
197   // defines CPSR. All other OptionalDefines in ARM are the CCR register.
198   bool CPSR = false;
199   if (DefinesOptionalPredicate(MI, &CPSR)) {
200     if (CPSR)
201       AddDefaultT1CC(MIB);
202     else
203       AddDefaultCC(MIB);
204   }
205   return MIB;
206 }
207
208 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
209                                     const TargetRegisterClass* RC) {
210   unsigned ResultReg = createResultReg(RC);
211   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
212
213   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
214   return ResultReg;
215 }
216
217 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
218                                      const TargetRegisterClass *RC,
219                                      unsigned Op0, bool Op0IsKill) {
220   unsigned ResultReg = createResultReg(RC);
221   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222
223   if (II.getNumDefs() >= 1)
224     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
225                    .addReg(Op0, Op0IsKill * RegState::Kill));
226   else {
227     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
228                    .addReg(Op0, Op0IsKill * RegState::Kill));
229     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
230                    TII.get(TargetOpcode::COPY), ResultReg)
231                    .addReg(II.ImplicitDefs[0]));
232   }
233   return ResultReg;
234 }
235
236 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
237                                       const TargetRegisterClass *RC,
238                                       unsigned Op0, bool Op0IsKill,
239                                       unsigned Op1, bool Op1IsKill) {
240   unsigned ResultReg = createResultReg(RC);
241   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
242
243   if (II.getNumDefs() >= 1)
244     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
245                    .addReg(Op0, Op0IsKill * RegState::Kill)
246                    .addReg(Op1, Op1IsKill * RegState::Kill));
247   else {
248     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
249                    .addReg(Op0, Op0IsKill * RegState::Kill)
250                    .addReg(Op1, Op1IsKill * RegState::Kill));
251     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
252                            TII.get(TargetOpcode::COPY), ResultReg)
253                    .addReg(II.ImplicitDefs[0]));
254   }
255   return ResultReg;
256 }
257
258 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
259                                       const TargetRegisterClass *RC,
260                                       unsigned Op0, bool Op0IsKill,
261                                       uint64_t Imm) {
262   unsigned ResultReg = createResultReg(RC);
263   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
264
265   if (II.getNumDefs() >= 1)
266     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
267                    .addReg(Op0, Op0IsKill * RegState::Kill)
268                    .addImm(Imm));
269   else {
270     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
271                    .addReg(Op0, Op0IsKill * RegState::Kill)
272                    .addImm(Imm));
273     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
274                            TII.get(TargetOpcode::COPY), ResultReg)
275                    .addReg(II.ImplicitDefs[0]));
276   }
277   return ResultReg;
278 }
279
280 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
281                                       const TargetRegisterClass *RC,
282                                       unsigned Op0, bool Op0IsKill,
283                                       const ConstantFP *FPImm) {
284   unsigned ResultReg = createResultReg(RC);
285   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
286
287   if (II.getNumDefs() >= 1)
288     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
289                    .addReg(Op0, Op0IsKill * RegState::Kill)
290                    .addFPImm(FPImm));
291   else {
292     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
293                    .addReg(Op0, Op0IsKill * RegState::Kill)
294                    .addFPImm(FPImm));
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_rri(unsigned MachineInstOpcode,
303                                        const TargetRegisterClass *RC,
304                                        unsigned Op0, bool Op0IsKill,
305                                        unsigned Op1, bool Op1IsKill,
306                                        uint64_t Imm) {
307   unsigned ResultReg = createResultReg(RC);
308   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
309
310   if (II.getNumDefs() >= 1)
311     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
312                    .addReg(Op0, Op0IsKill * RegState::Kill)
313                    .addReg(Op1, Op1IsKill * RegState::Kill)
314                    .addImm(Imm));
315   else {
316     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
317                    .addReg(Op0, Op0IsKill * RegState::Kill)
318                    .addReg(Op1, Op1IsKill * RegState::Kill)
319                    .addImm(Imm));
320     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
321                            TII.get(TargetOpcode::COPY), ResultReg)
322                    .addReg(II.ImplicitDefs[0]));
323   }
324   return ResultReg;
325 }
326
327 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
328                                      const TargetRegisterClass *RC,
329                                      uint64_t Imm) {
330   unsigned ResultReg = createResultReg(RC);
331   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
332
333   if (II.getNumDefs() >= 1)
334     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
335                    .addImm(Imm));
336   else {
337     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
338                    .addImm(Imm));
339     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
340                            TII.get(TargetOpcode::COPY), ResultReg)
341                    .addReg(II.ImplicitDefs[0]));
342   }
343   return ResultReg;
344 }
345
346 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
347                                                  unsigned Op0, bool Op0IsKill,
348                                                  uint32_t Idx) {
349   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
350   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
351          "Cannot yet extract from physregs");
352   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
353                          DL, TII.get(TargetOpcode::COPY), ResultReg)
354                  .addReg(Op0, getKillRegState(Op0IsKill), Idx));
355   return ResultReg;
356 }
357
358 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
359 // checks from the various callers.
360 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
361   if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
362   
363   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
364   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
365                           TII.get(ARM::VMOVRS), MoveReg)
366                   .addReg(SrcReg));
367   return MoveReg;
368 }
369
370 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
371   if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
372   
373   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
374   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
375                           TII.get(ARM::VMOVSR), MoveReg)
376                   .addReg(SrcReg));
377   return MoveReg;
378 }
379
380 // For double width floating point we need to materialize two constants
381 // (the high and the low) into integer registers then use a move to get
382 // the combined constant into an FP reg.
383 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
384   const APFloat Val = CFP->getValueAPF();
385   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
386
387   // This checks to see if we can use VFP3 instructions to materialize
388   // a constant, otherwise we have to go through the constant pool.
389   if (TLI.isFPImmLegal(Val, VT)) {
390     unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
391     unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
392     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
393                             DestReg)
394                     .addFPImm(CFP));
395     return DestReg;
396   }
397   
398   // Require VFP2 for loading fp constants.
399   if (!Subtarget->hasVFP2()) return false;
400   
401   // MachineConstantPool wants an explicit alignment.
402   unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
403   if (Align == 0) {
404     // TODO: Figure out if this is correct.
405     Align = TD.getTypeAllocSize(CFP->getType());
406   }
407   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
408   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
409   unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
410   
411   // The extra reg is for addrmode5.
412   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
413                           DestReg)
414                   .addConstantPoolIndex(Idx)
415                   .addReg(0));
416   return DestReg;
417 }
418
419 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
420   
421   // For now 32-bit only.
422   if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
423   
424   // MachineConstantPool wants an explicit alignment.
425   unsigned Align = TD.getPrefTypeAlignment(C->getType());
426   if (Align == 0) {
427     // TODO: Figure out if this is correct.
428     Align = TD.getTypeAllocSize(C->getType());
429   }
430   unsigned Idx = MCP.getConstantPoolIndex(C, Align);
431   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
432   
433   if (isThumb)
434     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
435                             TII.get(ARM::t2LDRpci), DestReg)
436                     .addConstantPoolIndex(Idx));
437   else
438     // The extra reg and immediate are for addrmode2.
439     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
440                             TII.get(ARM::LDRcp), DestReg)
441                     .addConstantPoolIndex(Idx)
442                     .addReg(0).addImm(0));
443
444   return DestReg;
445 }
446
447 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
448   EVT VT = TLI.getValueType(C->getType(), true);
449
450   // Only handle simple types.
451   if (!VT.isSimple()) return 0;
452
453   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
454     return ARMMaterializeFP(CFP, VT);
455   return ARMMaterializeInt(C, VT);
456 }
457
458 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
459   VT = TLI.getValueType(Ty, true);
460
461   // Only handle simple types.
462   if (VT == MVT::Other || !VT.isSimple()) return false;
463
464   // Handle all legal types, i.e. a register that will directly hold this
465   // value.
466   return TLI.isTypeLegal(VT);
467 }
468
469 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
470   if (isTypeLegal(Ty, VT)) return true;
471
472   // If this is a type than can be sign or zero-extended to a basic operation
473   // go ahead and accept it now.
474   if (VT == MVT::i8 || VT == MVT::i16)
475     return true;
476
477   return false;
478 }
479
480 // Computes the Reg+Offset to get to an object.
481 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
482                                       int &Offset) {
483   // Some boilerplate from the X86 FastISel.
484   const User *U = NULL;
485   unsigned Opcode = Instruction::UserOp1;
486   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
487     // Don't walk into other basic blocks; it's possible we haven't
488     // visited them yet, so the instructions may not yet be assigned
489     // virtual registers.
490     if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
491       return false;
492     Opcode = I->getOpcode();
493     U = I;
494   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
495     Opcode = C->getOpcode();
496     U = C;
497   }
498
499   if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
500     if (Ty->getAddressSpace() > 255)
501       // Fast instruction selection doesn't support the special
502       // address spaces.
503       return false;
504
505   switch (Opcode) {
506     default:
507     break;
508     case Instruction::Alloca: {
509       assert(false && "Alloca should have been handled earlier!");
510       return false;
511     }
512   }
513
514   // FIXME: Handle global variables.
515   if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
516     (void)GV;
517     return false;
518   }
519
520   // Try to get this in a register if nothing else has worked.
521   Reg = getRegForValue(Obj);
522   if (Reg == 0) return false;
523
524   // Since the offset may be too large for the load instruction
525   // get the reg+offset into a register.
526   // TODO: Verify the additions work, otherwise we'll need to add the
527   // offset instead of 0 to the instructions and do all sorts of operand
528   // munging.
529   // TODO: Optimize this somewhat.
530   if (Offset != 0) {
531     ARMCC::CondCodes Pred = ARMCC::AL;
532     unsigned PredReg = 0;
533
534     if (!isThumb)
535       emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
536                               Reg, Reg, Offset, Pred, PredReg,
537                               static_cast<const ARMBaseInstrInfo&>(TII));
538     else {
539       assert(AFI->isThumb2Function());
540       emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
541                              Reg, Reg, Offset, Pred, PredReg,
542                              static_cast<const ARMBaseInstrInfo&>(TII));
543     }
544   }
545   return true;
546 }
547
548 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
549   Value *Op0 = I->getOperand(0);
550
551   // Verify it's an alloca.
552   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
553     DenseMap<const AllocaInst*, int>::iterator SI =
554       FuncInfo.StaticAllocaMap.find(AI);
555
556     if (SI != FuncInfo.StaticAllocaMap.end()) {
557       TargetRegisterClass* RC = TLI.getRegClassFor(VT);
558       unsigned ResultReg = createResultReg(RC);
559       TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
560                                ResultReg, SI->second, RC,
561                                TM.getRegisterInfo());
562       UpdateValueMap(I, ResultReg);
563       return true;
564     }
565   }
566   return false;
567 }
568
569 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
570                               unsigned Reg, int Offset) {
571
572   assert(VT.isSimple() && "Non-simple types are invalid here!");
573   unsigned Opc;
574   bool isFloat = false;
575   switch (VT.getSimpleVT().SimpleTy) {
576     default:
577       // This is mostly going to be Neon/vector support.
578       return false;
579     case MVT::i16:
580       Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
581       VT = MVT::i32;
582       break;
583     case MVT::i8:
584       Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
585       VT = MVT::i32;
586       break;
587     case MVT::i32:
588       Opc = isThumb ? ARM::tLDR : ARM::LDR;
589       break;
590     case MVT::f32:
591       Opc = ARM::VLDRS;
592       isFloat = true;
593       break;
594     case MVT::f64:
595       Opc = ARM::VLDRD;
596       isFloat = true;
597       break;
598   }
599
600   ResultReg = createResultReg(TLI.getRegClassFor(VT));
601
602   // TODO: Fix the Addressing modes so that these can share some code.
603   // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
604   // The thumb addressing mode has operands swapped from the arm addressing
605   // mode, the floating point one only has two operands.
606   if (isFloat)
607     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
608                             TII.get(Opc), ResultReg)
609                     .addReg(Reg).addImm(Offset));
610   else if (isThumb)
611     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
612                             TII.get(Opc), ResultReg)
613                     .addReg(Reg).addImm(Offset).addReg(0));
614   else
615     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
616                             TII.get(Opc), ResultReg)
617                     .addReg(Reg).addReg(0).addImm(Offset));
618   return true;
619 }
620
621 bool ARMFastISel::SelectLoad(const Instruction *I) {
622   // Verify we have a legal type before going any further.
623   EVT VT;
624   if (!isLoadTypeLegal(I->getType(), VT))
625     return false;
626
627   // If we're an alloca we know we have a frame index and can emit the load
628   // directly in short order.
629   if (ARMLoadAlloca(I, VT))
630     return true;
631
632   // Our register and offset with innocuous defaults.
633   unsigned Reg = 0;
634   int Offset = 0;
635
636   // See if we can handle this as Reg + Offset
637   if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
638     return false;
639
640   unsigned ResultReg;
641   if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
642
643   UpdateValueMap(I, ResultReg);
644   return true;
645 }
646
647 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
648   Value *Op1 = I->getOperand(1);
649
650   // Verify it's an alloca.
651   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
652     DenseMap<const AllocaInst*, int>::iterator SI =
653       FuncInfo.StaticAllocaMap.find(AI);
654
655     if (SI != FuncInfo.StaticAllocaMap.end()) {
656       TargetRegisterClass* RC = TLI.getRegClassFor(VT);
657       assert(SrcReg != 0 && "Nothing to store!");
658       TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
659                               SrcReg, true /*isKill*/, SI->second, RC,
660                               TM.getRegisterInfo());
661       return true;
662     }
663   }
664   return false;
665 }
666
667 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
668                                unsigned DstReg, int Offset) {
669   unsigned StrOpc;
670   bool isFloat = false;
671   switch (VT.getSimpleVT().SimpleTy) {
672     default: return false;
673     case MVT::i1:
674     case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
675     case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
676     case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
677     case MVT::f32:
678       if (!Subtarget->hasVFP2()) return false;
679       StrOpc = ARM::VSTRS;
680       isFloat = true;
681       break;
682     case MVT::f64:
683       if (!Subtarget->hasVFP2()) return false;
684       StrOpc = ARM::VSTRD;
685       isFloat = true;
686       break;
687   }
688
689   // The thumb addressing mode has operands swapped from the arm addressing
690   // mode, the floating point one only has two operands.
691   if (isFloat)
692     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
693                             TII.get(StrOpc), SrcReg)
694                     .addReg(DstReg).addImm(Offset));
695   else if (isThumb)
696     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
697                             TII.get(StrOpc), SrcReg)
698                     .addReg(DstReg).addImm(Offset).addReg(0));
699
700   else
701     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
702                             TII.get(StrOpc), SrcReg)
703                     .addReg(DstReg).addReg(0).addImm(Offset));
704
705   return true;
706 }
707
708 bool ARMFastISel::SelectStore(const Instruction *I) {
709   Value *Op0 = I->getOperand(0);
710   unsigned SrcReg = 0;
711
712   // Yay type legalization
713   EVT VT;
714   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
715     return false;
716
717   // Get the value to be stored into a register.
718   SrcReg = getRegForValue(Op0);
719   if (SrcReg == 0)
720     return false;
721
722   // If we're an alloca we know we have a frame index and can emit the store
723   // quickly.
724   if (ARMStoreAlloca(I, SrcReg, VT))
725     return true;
726
727   // Our register and offset with innocuous defaults.
728   unsigned Reg = 0;
729   int Offset = 0;
730
731   // See if we can handle this as Reg + Offset
732   if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
733     return false;
734
735   if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
736
737   return true;
738 }
739
740 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
741   switch (Pred) {
742     // Needs two compares...
743     case CmpInst::FCMP_ONE:
744     case CmpInst::FCMP_UEQ:    
745     default:
746       assert(false && "Unhandled CmpInst::Predicate!");
747       return ARMCC::AL;
748     case CmpInst::ICMP_EQ:
749     case CmpInst::FCMP_OEQ:
750       return ARMCC::EQ;
751     case CmpInst::ICMP_SGT:
752     case CmpInst::FCMP_OGT:
753       return ARMCC::GT;
754     case CmpInst::ICMP_SGE:
755     case CmpInst::FCMP_OGE:
756       return ARMCC::GE;
757     case CmpInst::ICMP_UGT:
758     case CmpInst::FCMP_UGT:
759       return ARMCC::HI;
760     case CmpInst::FCMP_OLT:
761       return ARMCC::MI;
762     case CmpInst::ICMP_ULE:
763     case CmpInst::FCMP_OLE:
764       return ARMCC::LS;
765     case CmpInst::FCMP_ORD:
766       return ARMCC::VC;
767     case CmpInst::FCMP_UNO:
768       return ARMCC::VS;
769     case CmpInst::FCMP_UGE:
770       return ARMCC::PL;
771     case CmpInst::ICMP_SLT:
772     case CmpInst::FCMP_ULT:
773       return ARMCC::LT;  
774     case CmpInst::ICMP_SLE:
775     case CmpInst::FCMP_ULE:
776       return ARMCC::LE;
777     case CmpInst::FCMP_UNE:
778     case CmpInst::ICMP_NE:
779       return ARMCC::NE;
780     case CmpInst::ICMP_UGE:
781       return ARMCC::HS;
782     case CmpInst::ICMP_ULT:
783       return ARMCC::LO;
784   }
785 }
786
787 bool ARMFastISel::SelectBranch(const Instruction *I) {
788   const BranchInst *BI = cast<BranchInst>(I);
789   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
790   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
791
792   // Simple branch support.
793   // TODO: Try to avoid the re-computation in some places.
794   unsigned CondReg = getRegForValue(BI->getCondition());
795   if (CondReg == 0) return false;
796
797   // Re-set the flags just in case.
798   unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
799   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
800                   .addReg(CondReg).addImm(1));
801   
802   unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
803   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
804                   .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
805   FastEmitBranch(FBB, DL);
806   FuncInfo.MBB->addSuccessor(TBB);
807   return true;  
808 }
809
810 bool ARMFastISel::SelectCmp(const Instruction *I) {
811   const CmpInst *CI = cast<CmpInst>(I);
812
813   EVT VT;
814   const Type *Ty = CI->getOperand(0)->getType();
815   if (!isTypeLegal(Ty, VT))
816     return false;
817
818   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
819   if (isFloat && !Subtarget->hasVFP2())
820     return false;
821
822   unsigned CmpOpc;
823   unsigned CondReg;
824   switch (VT.getSimpleVT().SimpleTy) {
825     default: return false;
826     // TODO: Verify compares.
827     case MVT::f32:
828       CmpOpc = ARM::VCMPES;
829       CondReg = ARM::FPSCR;
830       break;
831     case MVT::f64:
832       CmpOpc = ARM::VCMPED;
833       CondReg = ARM::FPSCR;
834       break;
835     case MVT::i32:
836       CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
837       CondReg = ARM::CPSR;
838       break;
839   }
840
841   // Get the compare predicate.
842   ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
843     
844   // We may not handle every CC for now.
845   if (ARMPred == ARMCC::AL) return false;
846
847   unsigned Arg1 = getRegForValue(CI->getOperand(0));
848   if (Arg1 == 0) return false;
849
850   unsigned Arg2 = getRegForValue(CI->getOperand(1));
851   if (Arg2 == 0) return false;
852
853   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
854                   .addReg(Arg1).addReg(Arg2));
855
856   // For floating point we need to move the result to a comparison register
857   // that we can then use for branches.
858   if (isFloat)
859     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
860                             TII.get(ARM::FMSTAT)));
861
862   // Now set a register based on the comparison. Explicitly set the predicates
863   // here.
864   unsigned MovCCOpc = isThumb ? ARM::tMOVCCi : ARM::MOVCCi;
865   unsigned DestReg = createResultReg(ARM::GPRRegisterClass);
866   Constant *Zero 
867     = ConstantInt::get(Type::getInt32Ty(*Context), 0);
868   unsigned ZeroReg = TargetMaterializeConstant(Zero);
869   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
870           .addReg(ZeroReg).addImm(1)
871           .addImm(ARMPred).addReg(CondReg);
872
873   UpdateValueMap(I, DestReg);
874   return true;
875 }
876
877 bool ARMFastISel::SelectFPExt(const Instruction *I) {
878   // Make sure we have VFP and that we're extending float to double.
879   if (!Subtarget->hasVFP2()) return false;
880
881   Value *V = I->getOperand(0);
882   if (!I->getType()->isDoubleTy() ||
883       !V->getType()->isFloatTy()) return false;
884
885   unsigned Op = getRegForValue(V);
886   if (Op == 0) return false;
887
888   unsigned Result = createResultReg(ARM::DPRRegisterClass);
889   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
890                           TII.get(ARM::VCVTDS), Result)
891                   .addReg(Op));
892   UpdateValueMap(I, Result);
893   return true;
894 }
895
896 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
897   // Make sure we have VFP and that we're truncating double to float.
898   if (!Subtarget->hasVFP2()) return false;
899
900   Value *V = I->getOperand(0);
901   if (!I->getType()->isFloatTy() ||
902       !V->getType()->isDoubleTy()) return false;
903
904   unsigned Op = getRegForValue(V);
905   if (Op == 0) return false;
906
907   unsigned Result = createResultReg(ARM::SPRRegisterClass);
908   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
909                           TII.get(ARM::VCVTSD), Result)
910                   .addReg(Op));
911   UpdateValueMap(I, Result);
912   return true;
913 }
914
915 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
916   // Make sure we have VFP.
917   if (!Subtarget->hasVFP2()) return false;
918   
919   EVT DstVT;
920   const Type *Ty = I->getType();
921   if (!isTypeLegal(Ty, DstVT))
922     return false;
923   
924   unsigned Op = getRegForValue(I->getOperand(0));
925   if (Op == 0) return false;
926   
927   // The conversion routine works on fp-reg to fp-reg and the operand above
928   // was an integer, move it to the fp registers if possible.
929   unsigned FP = ARMMoveToFPReg(DstVT, Op);
930   if (FP == 0) return false;
931   
932   unsigned Opc;
933   if (Ty->isFloatTy()) Opc = ARM::VSITOS;
934   else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
935   else return 0;
936   
937   unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
938   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
939                           ResultReg)
940                   .addReg(FP));
941   UpdateValueMap(I, ResultReg);
942   return true;
943 }
944
945 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
946   // Make sure we have VFP.
947   if (!Subtarget->hasVFP2()) return false;
948   
949   EVT DstVT;
950   const Type *RetTy = I->getType();
951   if (!isTypeLegal(RetTy, DstVT))
952     return false;
953   
954   unsigned Op = getRegForValue(I->getOperand(0));
955   if (Op == 0) return false;
956   
957   unsigned Opc;
958   const Type *OpTy = I->getOperand(0)->getType();
959   if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
960   else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
961   else return 0;
962   EVT OpVT = TLI.getValueType(OpTy, true);
963   
964   unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
965   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
966                           ResultReg)
967                   .addReg(Op));
968         
969   // This result needs to be in an integer register, but the conversion only
970   // takes place in fp-regs.
971   unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
972   if (IntReg == 0) return false;
973   
974   UpdateValueMap(I, IntReg);
975   return true;
976 }
977
978 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
979   EVT VT  = TLI.getValueType(I->getType(), true);
980
981   // We can get here in the case when we want to use NEON for our fp
982   // operations, but can't figure out how to. Just use the vfp instructions
983   // if we have them.
984   // FIXME: It'd be nice to use NEON instructions.
985   const Type *Ty = I->getType();
986   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
987   if (isFloat && !Subtarget->hasVFP2())
988     return false;
989
990   unsigned Op1 = getRegForValue(I->getOperand(0));
991   if (Op1 == 0) return false;
992
993   unsigned Op2 = getRegForValue(I->getOperand(1));
994   if (Op2 == 0) return false;
995
996   unsigned Opc;
997   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
998                  VT.getSimpleVT().SimpleTy == MVT::i64;
999   switch (ISDOpcode) {
1000     default: return false;
1001     case ISD::FADD:
1002       Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1003       break;
1004     case ISD::FSUB:
1005       Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1006       break;
1007     case ISD::FMUL:
1008       Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1009       break;
1010   }
1011   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1012   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1013                           TII.get(Opc), ResultReg)
1014                   .addReg(Op1).addReg(Op2));
1015   UpdateValueMap(I, ResultReg);
1016   return true;
1017 }
1018
1019 // Call Handling Code
1020
1021 // This is largely taken directly from CCAssignFnForNode - we don't support
1022 // varargs in FastISel so that part has been removed.
1023 // TODO: We may not support all of this.
1024 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1025   switch (CC) {
1026   default:
1027     llvm_unreachable("Unsupported calling convention");
1028   case CallingConv::C:
1029   case CallingConv::Fast:
1030     // Use target triple & subtarget features to do actual dispatch.
1031     if (Subtarget->isAAPCS_ABI()) {
1032       if (Subtarget->hasVFP2() &&
1033           FloatABIType == FloatABI::Hard)
1034         return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1035       else
1036         return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1037     } else
1038         return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1039   case CallingConv::ARM_AAPCS_VFP:
1040     return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1041   case CallingConv::ARM_AAPCS:
1042     return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1043   case CallingConv::ARM_APCS:
1044     return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1045   }
1046 }
1047
1048 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1049                                   SmallVectorImpl<unsigned> &ArgRegs,
1050                                   SmallVectorImpl<EVT> &ArgVTs,
1051                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1052                                   SmallVectorImpl<unsigned> &RegArgs,
1053                                   CallingConv::ID CC,
1054                                   unsigned &NumBytes) {
1055   SmallVector<CCValAssign, 16> ArgLocs;
1056   CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1057   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1058
1059   // Get a count of how many bytes are to be pushed on the stack.
1060   NumBytes = CCInfo.getNextStackOffset();
1061
1062   // Issue CALLSEQ_START
1063   unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1064   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1065           .addImm(NumBytes);
1066
1067   // Process the args.
1068   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1069     CCValAssign &VA = ArgLocs[i];
1070     unsigned Arg = ArgRegs[VA.getValNo()];
1071     EVT ArgVT = ArgVTs[VA.getValNo()];
1072
1073                                       // Should we ever have to promote?
1074     switch (VA.getLocInfo()) {
1075       case CCValAssign::Full: break;
1076       default:
1077       assert(false && "Handle arg promotion for libcalls?");
1078       return false;
1079     }
1080
1081     // Now copy/store arg to correct locations.
1082     if (VA.isRegLoc()) {
1083       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1084         VA.getLocReg())
1085         .addReg(Arg);
1086       RegArgs.push_back(VA.getLocReg());
1087     } else {
1088       // Need to store
1089       return false;
1090     }
1091   }
1092   
1093   return true;
1094 }
1095
1096 bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1097                              const Instruction *I, CallingConv::ID CC,
1098                              unsigned &NumBytes) {
1099   // Issue CALLSEQ_END
1100   unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1101   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1102           .addImm(NumBytes).addImm(0);
1103
1104   // Now the return value.
1105   if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1106     SmallVector<CCValAssign, 16> RVLocs;
1107     CCState CCInfo(CC, false, TM, RVLocs, *Context);
1108     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1109
1110     // Copy all of the result registers out of their specified physreg.
1111     assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1112     EVT CopyVT = RVLocs[0].getValVT();
1113     TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1114
1115     unsigned ResultReg = createResultReg(DstRC);
1116     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1117             ResultReg).addReg(RVLocs[0].getLocReg());
1118     UsedRegs.push_back(RVLocs[0].getLocReg());
1119
1120     // Finally update the result.        
1121     UpdateValueMap(I, ResultReg);
1122   }
1123
1124   return true;                           
1125 }
1126
1127 // A quick function that will emit a call for a named libcall in F with the
1128 // vector of passed arguments for the Instruction in I. We can assume that we
1129 // can emit a call for any libcall we can produce. This is an abridged version 
1130 // of the full call infrastructure since we won't need to worry about things 
1131 // like computed function pointers or strange arguments at call sites.
1132 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1133 // with X86.
1134 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1135   CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1136     
1137   // Handle *simple* calls for now.
1138   const Type *RetTy = I->getType();
1139   EVT RetVT;
1140   if (RetTy->isVoidTy())
1141     RetVT = MVT::isVoid;
1142   else if (!isTypeLegal(RetTy, RetVT))
1143     return false;
1144   
1145   // For now we're using BLX etc on the assumption that we have v5t ops.
1146   if (!Subtarget->hasV5TOps()) return false;
1147   
1148   // Set up the argument vectors.
1149   SmallVector<Value*, 8> Args;
1150   SmallVector<unsigned, 8> ArgRegs;
1151   SmallVector<EVT, 8> ArgVTs;
1152   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1153   Args.reserve(I->getNumOperands());
1154   ArgRegs.reserve(I->getNumOperands());
1155   ArgVTs.reserve(I->getNumOperands());
1156   ArgFlags.reserve(I->getNumOperands());
1157   for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1158     Value *Op = I->getOperand(i);
1159     unsigned Arg = getRegForValue(Op);
1160     if (Arg == 0) return false;
1161     
1162     const Type *ArgTy = Op->getType();
1163     EVT ArgVT;
1164     if (!isTypeLegal(ArgTy, ArgVT)) return false;
1165     
1166     ISD::ArgFlagsTy Flags;
1167     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1168     Flags.setOrigAlign(OriginalAlignment);
1169     
1170     Args.push_back(Op);
1171     ArgRegs.push_back(Arg);
1172     ArgVTs.push_back(ArgVT);
1173     ArgFlags.push_back(Flags);
1174   }
1175   
1176   // Handle the arguments now that we've gotten them.
1177   SmallVector<unsigned, 4> RegArgs;
1178   unsigned NumBytes;
1179   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1180     return false;
1181   
1182   // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1183   // TODO: Turn this into the table of arm call ops.  
1184   MachineInstrBuilder MIB;
1185   unsigned CallOpc;
1186   if(isThumb)
1187     CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1188   else
1189     CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1190   MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1191         .addExternalSymbol(TLI.getLibcallName(Call));
1192     
1193   // Add implicit physical register uses to the call.
1194   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1195     MIB.addReg(RegArgs[i]);
1196     
1197   // Finish off the call including any return values.
1198   SmallVector<unsigned, 4> UsedRegs;  
1199   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1200   
1201   // Set all unused physreg defs as dead.
1202   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1203   
1204   return true;
1205 }
1206
1207 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1208   EVT VT;
1209   const Type *Ty = I->getType();
1210   if (!isTypeLegal(Ty, VT))
1211     return false;
1212
1213   // If we have integer div support we should have selected this automagically.
1214   // In case we have a real miss go ahead and return false and we'll pick
1215   // it up later.
1216   if (Subtarget->hasDivide()) return false;  
1217   
1218   // Otherwise emit a libcall.
1219   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1220   if (VT == MVT::i16)
1221     LC = RTLIB::SDIV_I16;
1222   else if (VT == MVT::i32)
1223     LC = RTLIB::SDIV_I32;
1224   else if (VT == MVT::i64)
1225     LC = RTLIB::SDIV_I64;
1226   else if (VT == MVT::i128)
1227     LC = RTLIB::SDIV_I128;
1228   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1229     
1230   return ARMEmitLibcall(I, LC);
1231 }
1232
1233 // TODO: SoftFP support.
1234 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1235   // No Thumb-1 for now.
1236   if (isThumb && !AFI->isThumb2Function()) return false;
1237
1238   switch (I->getOpcode()) {
1239     case Instruction::Load:
1240       return SelectLoad(I);
1241     case Instruction::Store:
1242       return SelectStore(I);
1243     case Instruction::Br:
1244       return SelectBranch(I);
1245     case Instruction::ICmp:
1246     case Instruction::FCmp:
1247       return SelectCmp(I);
1248     case Instruction::FPExt:
1249       return SelectFPExt(I);
1250     case Instruction::FPTrunc:
1251       return SelectFPTrunc(I);
1252     case Instruction::SIToFP:
1253       return SelectSIToFP(I);
1254     case Instruction::FPToSI:
1255       return SelectFPToSI(I);
1256     case Instruction::FAdd:
1257       return SelectBinaryOp(I, ISD::FADD);
1258     case Instruction::FSub:
1259       return SelectBinaryOp(I, ISD::FSUB);
1260     case Instruction::FMul:
1261       return SelectBinaryOp(I, ISD::FMUL);
1262     case Instruction::SDiv:
1263       return SelectSDiv(I);
1264     default: break;
1265   }
1266   return false;
1267 }
1268
1269 namespace llvm {
1270   llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1271     if (EnableARMFastISel) return new ARMFastISel(funcInfo);
1272     return 0;
1273   }
1274 }