For consistency.
[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 "ARMRegisterInfo.h"
19 #include "ARMTargetMachine.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/CallingConv.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/CodeGen/Analysis.h"
27 #include "llvm/CodeGen/FastISel.h"
28 #include "llvm/CodeGen/FunctionLoweringInfo.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineConstantPool.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/Support/CallSite.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetLowering.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 using namespace llvm;
44
45 static cl::opt<bool>
46 EnableARMFastISel("arm-fast-isel",
47                   cl::desc("Turn on experimental ARM fast-isel support"),
48                   cl::init(false), cl::Hidden);
49
50 namespace {
51
52 class ARMFastISel : public FastISel {
53
54   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55   /// make the right decision when generating code for different targets.
56   const ARMSubtarget *Subtarget;
57   const TargetMachine &TM;
58   const TargetInstrInfo &TII;
59   const TargetLowering &TLI;
60   const ARMFunctionInfo *AFI;
61
62   // Convenience variable to avoid checking all the time.
63   bool isThumb;
64
65   public:
66     explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
67     : FastISel(funcInfo),
68       TM(funcInfo.MF->getTarget()),
69       TII(*TM.getInstrInfo()),
70       TLI(*TM.getTargetLowering()) {
71       Subtarget = &TM.getSubtarget<ARMSubtarget>();
72       AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
73       isThumb = AFI->isThumbFunction();
74     }
75
76     // Code from FastISel.cpp.
77     virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
78                                    const TargetRegisterClass *RC);
79     virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
80                                     const TargetRegisterClass *RC,
81                                     unsigned Op0, bool Op0IsKill);
82     virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
83                                      const TargetRegisterClass *RC,
84                                      unsigned Op0, bool Op0IsKill,
85                                      unsigned Op1, bool Op1IsKill);
86     virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
87                                      const TargetRegisterClass *RC,
88                                      unsigned Op0, bool Op0IsKill,
89                                      uint64_t Imm);
90     virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
91                                      const TargetRegisterClass *RC,
92                                      unsigned Op0, bool Op0IsKill,
93                                      const ConstantFP *FPImm);
94     virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
95                                     const TargetRegisterClass *RC,
96                                     uint64_t Imm);
97     virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
98                                       const TargetRegisterClass *RC,
99                                       unsigned Op0, bool Op0IsKill,
100                                       unsigned Op1, bool Op1IsKill,
101                                       uint64_t Imm);
102     virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
103                                                 unsigned Op0, bool Op0IsKill,
104                                                 uint32_t Idx);
105
106     // Backend specific FastISel code.
107     virtual bool TargetSelectInstruction(const Instruction *I);
108     virtual unsigned TargetMaterializeConstant(const Constant *C);
109
110   #include "ARMGenFastISel.inc"
111
112     // Instruction selection routines.
113   private:
114     virtual bool ARMSelectLoad(const Instruction *I);
115     virtual bool ARMSelectStore(const Instruction *I);
116     virtual bool ARMSelectBranch(const Instruction *I);
117     virtual bool ARMSelectCmp(const Instruction *I);
118     virtual bool ARMSelectFPExt(const Instruction *I);
119     virtual bool ARMSelectFPTrunc(const Instruction *I);
120     virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
121     virtual bool ARMSelectSIToFP(const Instruction *I);
122     virtual bool ARMSelectFPToSI(const Instruction *I);
123
124     // Utility routines.
125   private:
126     bool isTypeLegal(const Type *Ty, EVT &VT);
127     bool isLoadTypeLegal(const Type *Ty, EVT &VT);
128     bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
129     bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
130     bool ARMLoadAlloca(const Instruction *I, EVT VT);
131     bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
132     bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
133     unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
134     unsigned ARMMaterializeInt(const Constant *C);
135     unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
136     unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
137
138     bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
139     const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
140 };
141
142 } // end anonymous namespace
143
144 // #include "ARMGenCallingConv.inc"
145
146 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
147 // we don't care about implicit defs here, just places we'll need to add a
148 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
149 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
150   const TargetInstrDesc &TID = MI->getDesc();
151   if (!TID.hasOptionalDef())
152     return false;
153
154   // Look to see if our OptionalDef is defining CPSR or CCR.
155   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156     const MachineOperand &MO = MI->getOperand(i);
157     if (!MO.isReg() || !MO.isDef()) continue;
158     if (MO.getReg() == ARM::CPSR)
159       *CPSR = true;
160   }
161   return true;
162 }
163
164 // If the machine is predicable go ahead and add the predicate operands, if
165 // it needs default CC operands add those.
166 const MachineInstrBuilder &
167 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
168   MachineInstr *MI = &*MIB;
169
170   // Do we use a predicate?
171   if (TII.isPredicable(MI))
172     AddDefaultPred(MIB);
173
174   // Do we optionally set a predicate?  Preds is size > 0 iff the predicate
175   // defines CPSR. All other OptionalDefines in ARM are the CCR register.
176   bool CPSR = false;
177   if (DefinesOptionalPredicate(MI, &CPSR)) {
178     if (CPSR)
179       AddDefaultT1CC(MIB);
180     else
181       AddDefaultCC(MIB);
182   }
183   return MIB;
184 }
185
186 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
187                                     const TargetRegisterClass* RC) {
188   unsigned ResultReg = createResultReg(RC);
189   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
190
191   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
192   return ResultReg;
193 }
194
195 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
196                                      const TargetRegisterClass *RC,
197                                      unsigned Op0, bool Op0IsKill) {
198   unsigned ResultReg = createResultReg(RC);
199   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
200
201   if (II.getNumDefs() >= 1)
202     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
203                    .addReg(Op0, Op0IsKill * RegState::Kill));
204   else {
205     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
206                    .addReg(Op0, Op0IsKill * RegState::Kill));
207     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
208                    TII.get(TargetOpcode::COPY), ResultReg)
209                    .addReg(II.ImplicitDefs[0]));
210   }
211   return ResultReg;
212 }
213
214 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
215                                       const TargetRegisterClass *RC,
216                                       unsigned Op0, bool Op0IsKill,
217                                       unsigned Op1, bool Op1IsKill) {
218   unsigned ResultReg = createResultReg(RC);
219   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
220
221   if (II.getNumDefs() >= 1)
222     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
223                    .addReg(Op0, Op0IsKill * RegState::Kill)
224                    .addReg(Op1, Op1IsKill * RegState::Kill));
225   else {
226     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
227                    .addReg(Op0, Op0IsKill * RegState::Kill)
228                    .addReg(Op1, Op1IsKill * 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_ri(unsigned MachineInstOpcode,
237                                       const TargetRegisterClass *RC,
238                                       unsigned Op0, bool Op0IsKill,
239                                       uint64_t Imm) {
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                    .addImm(Imm));
247   else {
248     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
249                    .addReg(Op0, Op0IsKill * RegState::Kill)
250                    .addImm(Imm));
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_rf(unsigned MachineInstOpcode,
259                                       const TargetRegisterClass *RC,
260                                       unsigned Op0, bool Op0IsKill,
261                                       const ConstantFP *FPImm) {
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                    .addFPImm(FPImm));
269   else {
270     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
271                    .addReg(Op0, Op0IsKill * RegState::Kill)
272                    .addFPImm(FPImm));
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_rri(unsigned MachineInstOpcode,
281                                        const TargetRegisterClass *RC,
282                                        unsigned Op0, bool Op0IsKill,
283                                        unsigned Op1, bool Op1IsKill,
284                                        uint64_t Imm) {
285   unsigned ResultReg = createResultReg(RC);
286   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
287
288   if (II.getNumDefs() >= 1)
289     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
290                    .addReg(Op0, Op0IsKill * RegState::Kill)
291                    .addReg(Op1, Op1IsKill * RegState::Kill)
292                    .addImm(Imm));
293   else {
294     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
295                    .addReg(Op0, Op0IsKill * RegState::Kill)
296                    .addReg(Op1, Op1IsKill * RegState::Kill)
297                    .addImm(Imm));
298     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
299                            TII.get(TargetOpcode::COPY), ResultReg)
300                    .addReg(II.ImplicitDefs[0]));
301   }
302   return ResultReg;
303 }
304
305 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
306                                      const TargetRegisterClass *RC,
307                                      uint64_t Imm) {
308   unsigned ResultReg = createResultReg(RC);
309   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
310
311   if (II.getNumDefs() >= 1)
312     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
313                    .addImm(Imm));
314   else {
315     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
316                    .addImm(Imm));
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_extractsubreg(MVT RetVT,
325                                                  unsigned Op0, bool Op0IsKill,
326                                                  uint32_t Idx) {
327   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
328   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
329          "Cannot yet extract from physregs");
330   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
331                          DL, TII.get(TargetOpcode::COPY), ResultReg)
332                  .addReg(Op0, getKillRegState(Op0IsKill), Idx));
333   return ResultReg;
334 }
335
336 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
337 // checks from the various callers.
338 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
339   if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
340   
341   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
342   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
343                           TII.get(ARM::VMOVRS), MoveReg)
344                   .addReg(SrcReg));
345   return MoveReg;
346 }
347
348 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
349   if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
350   
351   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
352   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
353                           TII.get(ARM::VMOVSR), MoveReg)
354                   .addReg(SrcReg));
355   return MoveReg;
356 }
357
358 // For double width floating point we need to materialize two constants
359 // (the high and the low) into integer registers then use a move to get
360 // the combined constant into an FP reg.
361 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
362   const APFloat Val = CFP->getValueAPF();
363   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
364
365   // This checks to see if we can use VFP3 instructions to materialize
366   // a constant, otherwise we have to go through the constant pool.
367   if (TLI.isFPImmLegal(Val, VT)) {
368     unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
369     unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
370     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
371                             DestReg)
372                     .addFPImm(CFP));
373     return DestReg;
374   }
375   
376   // Require VFP2 for loading fp constants.
377   if (!Subtarget->hasVFP2()) return false;
378   
379   // MachineConstantPool wants an explicit alignment.
380   unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
381   if (Align == 0) {
382     // TODO: Figure out if this is correct.
383     Align = TD.getTypeAllocSize(CFP->getType());
384   }
385   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
386   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
387   unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
388   
389   // The extra reg is for addrmode5.
390   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
391                   .addReg(DestReg).addConstantPoolIndex(Idx)
392                   .addReg(0));
393   return DestReg;
394 }
395
396 // TODO: Verify 64-bit.
397 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
398   // MachineConstantPool wants an explicit alignment.
399   unsigned Align = TD.getPrefTypeAlignment(C->getType());
400   if (Align == 0) {
401     // TODO: Figure out if this is correct.
402     Align = TD.getTypeAllocSize(C->getType());
403   }
404   unsigned Idx = MCP.getConstantPoolIndex(C, Align);
405   unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
406   
407   if (isThumb)
408     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
409                             TII.get(ARM::t2LDRpci))
410                     .addReg(DestReg).addConstantPoolIndex(Idx));
411   else
412     // The extra reg and immediate are for addrmode2.
413     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
414                             TII.get(ARM::LDRcp))
415                             .addReg(DestReg).addConstantPoolIndex(Idx)
416                     .addReg(0).addImm(0));
417
418   return DestReg;
419 }
420
421 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
422   EVT VT = TLI.getValueType(C->getType(), true);
423
424   // Only handle simple types.
425   if (!VT.isSimple()) return 0;
426
427   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
428     return ARMMaterializeFP(CFP, VT);
429   return ARMMaterializeInt(C);
430 }
431
432 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
433   VT = TLI.getValueType(Ty, true);
434
435   // Only handle simple types.
436   if (VT == MVT::Other || !VT.isSimple()) return false;
437
438   // Handle all legal types, i.e. a register that will directly hold this
439   // value.
440   return TLI.isTypeLegal(VT);
441 }
442
443 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
444   if (isTypeLegal(Ty, VT)) return true;
445
446   // If this is a type than can be sign or zero-extended to a basic operation
447   // go ahead and accept it now.
448   if (VT == MVT::i8 || VT == MVT::i16)
449     return true;
450
451   return false;
452 }
453
454 // Computes the Reg+Offset to get to an object.
455 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
456                                       int &Offset) {
457   // Some boilerplate from the X86 FastISel.
458   const User *U = NULL;
459   unsigned Opcode = Instruction::UserOp1;
460   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
461     // Don't walk into other basic blocks; it's possible we haven't
462     // visited them yet, so the instructions may not yet be assigned
463     // virtual registers.
464     if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
465       return false;
466     Opcode = I->getOpcode();
467     U = I;
468   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
469     Opcode = C->getOpcode();
470     U = C;
471   }
472
473   if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
474     if (Ty->getAddressSpace() > 255)
475       // Fast instruction selection doesn't support the special
476       // address spaces.
477       return false;
478
479   switch (Opcode) {
480     default:
481     break;
482     case Instruction::Alloca: {
483       assert(false && "Alloca should have been handled earlier!");
484       return false;
485     }
486   }
487
488   // FIXME: Handle global variables.
489   if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
490     (void)GV;
491     return false;
492   }
493
494   // Try to get this in a register if nothing else has worked.
495   Reg = getRegForValue(Obj);
496   if (Reg == 0) return false;
497
498   // Since the offset may be too large for the load instruction
499   // get the reg+offset into a register.
500   // TODO: Verify the additions work, otherwise we'll need to add the
501   // offset instead of 0 to the instructions and do all sorts of operand
502   // munging.
503   // TODO: Optimize this somewhat.
504   if (Offset != 0) {
505     ARMCC::CondCodes Pred = ARMCC::AL;
506     unsigned PredReg = 0;
507
508     if (!isThumb)
509       emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
510                               Reg, Reg, Offset, Pred, PredReg,
511                               static_cast<const ARMBaseInstrInfo&>(TII));
512     else {
513       assert(AFI->isThumb2Function());
514       emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
515                              Reg, Reg, Offset, Pred, PredReg,
516                              static_cast<const ARMBaseInstrInfo&>(TII));
517     }
518   }
519   return true;
520 }
521
522 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
523   Value *Op0 = I->getOperand(0);
524
525   // Verify it's an alloca.
526   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
527     DenseMap<const AllocaInst*, int>::iterator SI =
528       FuncInfo.StaticAllocaMap.find(AI);
529
530     if (SI != FuncInfo.StaticAllocaMap.end()) {
531       TargetRegisterClass* RC = TLI.getRegClassFor(VT);
532       unsigned ResultReg = createResultReg(RC);
533       TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
534                                ResultReg, SI->second, RC,
535                                TM.getRegisterInfo());
536       UpdateValueMap(I, ResultReg);
537       return true;
538     }
539   }
540   return false;
541 }
542
543 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
544                               unsigned Reg, int Offset) {
545
546   assert(VT.isSimple() && "Non-simple types are invalid here!");
547   unsigned Opc;
548
549   switch (VT.getSimpleVT().SimpleTy) {
550     default:
551       assert(false && "Trying to emit for an unhandled type!");
552       return false;
553     case MVT::i16:
554       Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
555       VT = MVT::i32;
556       break;
557     case MVT::i8:
558       Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
559       VT = MVT::i32;
560       break;
561     case MVT::i32:
562       Opc = isThumb ? ARM::tLDR : ARM::LDR;
563       break;
564   }
565
566   ResultReg = createResultReg(TLI.getRegClassFor(VT));
567
568   // TODO: Fix the Addressing modes so that these can share some code.
569   // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
570   if (isThumb)
571     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
572                             TII.get(Opc), ResultReg)
573                     .addReg(Reg).addImm(Offset).addReg(0));
574   else
575     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
576                             TII.get(Opc), ResultReg)
577                     .addReg(Reg).addReg(0).addImm(Offset));
578   return true;
579 }
580
581 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
582   // Verify we have a legal type before going any further.
583   EVT VT;
584   if (!isLoadTypeLegal(I->getType(), VT))
585     return false;
586
587   // If we're an alloca we know we have a frame index and can emit the load
588   // directly in short order.
589   if (ARMLoadAlloca(I, VT))
590     return true;
591
592   // Our register and offset with innocuous defaults.
593   unsigned Reg = 0;
594   int Offset = 0;
595
596   // See if we can handle this as Reg + Offset
597   if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
598     return false;
599
600   unsigned ResultReg;
601   if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
602
603   UpdateValueMap(I, ResultReg);
604   return true;
605 }
606
607 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
608   Value *Op1 = I->getOperand(1);
609
610   // Verify it's an alloca.
611   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
612     DenseMap<const AllocaInst*, int>::iterator SI =
613       FuncInfo.StaticAllocaMap.find(AI);
614
615     if (SI != FuncInfo.StaticAllocaMap.end()) {
616       TargetRegisterClass* RC = TLI.getRegClassFor(VT);
617       assert(SrcReg != 0 && "Nothing to store!");
618       TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
619                               SrcReg, true /*isKill*/, SI->second, RC,
620                               TM.getRegisterInfo());
621       return true;
622     }
623   }
624   return false;
625 }
626
627 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
628                                unsigned DstReg, int Offset) {
629   unsigned StrOpc;
630   switch (VT.getSimpleVT().SimpleTy) {
631     default: return false;
632     case MVT::i1:
633     case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
634     case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
635     case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
636     case MVT::f32:
637       if (!Subtarget->hasVFP2()) return false;
638       StrOpc = ARM::VSTRS;
639       break;
640     case MVT::f64:
641       if (!Subtarget->hasVFP2()) return false;
642       StrOpc = ARM::VSTRD;
643       break;
644   }
645
646   if (isThumb)
647     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
648                             TII.get(StrOpc), SrcReg)
649                     .addReg(DstReg).addImm(Offset).addReg(0));
650   else
651     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
652                             TII.get(StrOpc), SrcReg)
653                     .addReg(DstReg).addReg(0).addImm(Offset));
654
655   return true;
656 }
657
658 bool ARMFastISel::ARMSelectStore(const Instruction *I) {
659   Value *Op0 = I->getOperand(0);
660   unsigned SrcReg = 0;
661
662   // Yay type legalization
663   EVT VT;
664   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
665     return false;
666
667   // Get the value to be stored into a register.
668   SrcReg = getRegForValue(Op0);
669   if (SrcReg == 0)
670     return false;
671
672   // If we're an alloca we know we have a frame index and can emit the store
673   // quickly.
674   if (ARMStoreAlloca(I, SrcReg, VT))
675     return true;
676
677   // Our register and offset with innocuous defaults.
678   unsigned Reg = 0;
679   int Offset = 0;
680
681   // See if we can handle this as Reg + Offset
682   if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
683     return false;
684
685   if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
686
687   return false;
688 }
689
690 bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
691   const BranchInst *BI = cast<BranchInst>(I);
692   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
693   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
694
695   // Simple branch support.
696   unsigned CondReg = getRegForValue(BI->getCondition());
697   if (CondReg == 0) return false;
698
699   unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
700   unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
701   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
702                   .addReg(CondReg).addReg(CondReg));
703   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
704                   .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
705   FastEmitBranch(FBB, DL);
706   FuncInfo.MBB->addSuccessor(TBB);
707   return true;
708 }
709
710 bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
711   const CmpInst *CI = cast<CmpInst>(I);
712
713   EVT VT;
714   const Type *Ty = CI->getOperand(0)->getType();
715   if (!isTypeLegal(Ty, VT))
716     return false;
717
718   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
719   if (isFloat && !Subtarget->hasVFP2())
720     return false;
721
722   unsigned CmpOpc;
723   switch (VT.getSimpleVT().SimpleTy) {
724     default: return false;
725     // TODO: Verify compares.
726     case MVT::f32:
727       CmpOpc = ARM::VCMPES;
728       break;
729     case MVT::f64:
730       CmpOpc = ARM::VCMPED;
731       break;
732     case MVT::i32:
733       CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
734       break;
735   }
736
737   unsigned Arg1 = getRegForValue(CI->getOperand(0));
738   if (Arg1 == 0) return false;
739
740   unsigned Arg2 = getRegForValue(CI->getOperand(1));
741   if (Arg2 == 0) return false;
742
743   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
744                   .addReg(Arg1).addReg(Arg2));
745
746   // For floating point we need to move the result to a comparison register
747   // that we can then use for branches.
748   if (isFloat)
749     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
750                             TII.get(ARM::FMSTAT)));
751
752   // TODO: How to update the value map when there's no result reg?
753   return true;
754 }
755
756 bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
757   // Make sure we have VFP and that we're extending float to double.
758   if (!Subtarget->hasVFP2()) return false;
759
760   Value *V = I->getOperand(0);
761   if (!I->getType()->isDoubleTy() ||
762       !V->getType()->isFloatTy()) return false;
763
764   unsigned Op = getRegForValue(V);
765   if (Op == 0) return false;
766
767   unsigned Result = createResultReg(ARM::DPRRegisterClass);
768   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
769                           TII.get(ARM::VCVTDS), Result)
770                   .addReg(Op));
771   UpdateValueMap(I, Result);
772   return true;
773 }
774
775 bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
776   // Make sure we have VFP and that we're truncating double to float.
777   if (!Subtarget->hasVFP2()) return false;
778
779   Value *V = I->getOperand(0);
780   if (!I->getType()->isFloatTy() ||
781       !V->getType()->isDoubleTy()) return false;
782
783   unsigned Op = getRegForValue(V);
784   if (Op == 0) return false;
785
786   unsigned Result = createResultReg(ARM::SPRRegisterClass);
787   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
788                           TII.get(ARM::VCVTSD), Result)
789                   .addReg(Op));
790   UpdateValueMap(I, Result);
791   return true;
792 }
793
794 bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
795   // Make sure we have VFP.
796   if (!Subtarget->hasVFP2()) return false;
797   
798   EVT DstVT;
799   const Type *Ty = I->getType();
800   if (!isTypeLegal(Ty, DstVT))
801     return false;
802   
803   unsigned Op = getRegForValue(I->getOperand(0));
804   if (Op == 0) return false;
805   
806   // The conversion routine works on fp-reg to fp-reg and the operand above
807   // was an integer, move it to the fp registers if possible.
808   unsigned FP = ARMMoveToFPReg(DstVT, Op);
809   if (FP == 0) return false;
810   
811   unsigned Opc;
812   if (Ty->isFloatTy()) Opc = ARM::VSITOS;
813   else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
814   else return 0;
815   
816   unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
817   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
818                           ResultReg)
819                   .addReg(FP));
820   UpdateValueMap(I, ResultReg);
821   return true;
822 }
823
824 bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
825   // Make sure we have VFP.
826   if (!Subtarget->hasVFP2()) return false;
827   
828   EVT DstVT;
829   const Type *RetTy = I->getType();
830   if (!isTypeLegal(RetTy, DstVT))
831     return false;
832   
833   unsigned Op = getRegForValue(I->getOperand(0));
834   if (Op == 0) return false;
835   
836   unsigned Opc;
837   const Type *OpTy = I->getOperand(0)->getType();
838   if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
839   else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
840   else return 0;
841   EVT OpVT = TLI.getValueType(OpTy, true);
842   
843   unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
844   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
845                           ResultReg)
846                   .addReg(Op));
847         
848   // This result needs to be in an integer register, but the conversion only
849   // takes place in fp-regs.
850   unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
851   if (IntReg == 0) return false;
852   
853   UpdateValueMap(I, IntReg);
854   return true;
855 }
856
857 bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
858   EVT VT  = TLI.getValueType(I->getType(), true);
859
860   // We can get here in the case when we want to use NEON for our fp
861   // operations, but can't figure out how to. Just use the vfp instructions
862   // if we have them.
863   // FIXME: It'd be nice to use NEON instructions.
864   const Type *Ty = I->getType();
865   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
866   if (isFloat && !Subtarget->hasVFP2())
867     return false;
868
869   unsigned Op1 = getRegForValue(I->getOperand(0));
870   if (Op1 == 0) return false;
871
872   unsigned Op2 = getRegForValue(I->getOperand(1));
873   if (Op2 == 0) return false;
874
875   unsigned Opc;
876   bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
877                  VT.getSimpleVT().SimpleTy == MVT::i64;
878   switch (ISDOpcode) {
879     default: return false;
880     case ISD::FADD:
881       Opc = is64bit ? ARM::VADDD : ARM::VADDS;
882       break;
883     case ISD::FSUB:
884       Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
885       break;
886     case ISD::FMUL:
887       Opc = is64bit ? ARM::VMULD : ARM::VMULS;
888       break;
889   }
890   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
891   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
892                           TII.get(Opc), ResultReg)
893                   .addReg(Op1).addReg(Op2));
894   UpdateValueMap(I, ResultReg);
895   return true;
896 }
897
898 // TODO: SoftFP support.
899 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
900   // No Thumb-1 for now.
901   if (isThumb && !AFI->isThumb2Function()) return false;
902
903   switch (I->getOpcode()) {
904     case Instruction::Load:
905       return ARMSelectLoad(I);
906     case Instruction::Store:
907       return ARMSelectStore(I);
908     case Instruction::Br:
909       return ARMSelectBranch(I);
910     case Instruction::ICmp:
911     case Instruction::FCmp:
912       return ARMSelectCmp(I);
913     case Instruction::FPExt:
914       return ARMSelectFPExt(I);
915     case Instruction::FPTrunc:
916       return ARMSelectFPTrunc(I);
917     case Instruction::SIToFP:
918       return ARMSelectSIToFP(I);
919     case Instruction::FPToSI:
920       return ARMSelectFPToSI(I);
921     case Instruction::FAdd:
922       return ARMSelectBinaryOp(I, ISD::FADD);
923     case Instruction::FSub:
924       return ARMSelectBinaryOp(I, ISD::FSUB);
925     case Instruction::FMul:
926       return ARMSelectBinaryOp(I, ISD::FMUL);
927     default: break;
928   }
929   return false;
930 }
931
932 namespace llvm {
933   llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
934     if (EnableARMFastISel) return new ARMFastISel(funcInfo);
935     return 0;
936   }
937 }