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