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