LLVM API Documentation

ARMFastISel.cpp
Go to the documentation of this file.
00001 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ARM-specific support for the FastISel class. Some
00011 // of the target-specific code is generated by tablegen in the file
00012 // ARMGenFastISel.inc, which is #included here.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "ARM.h"
00017 #include "ARMBaseRegisterInfo.h"
00018 #include "ARMCallingConv.h"
00019 #include "ARMConstantPoolValue.h"
00020 #include "ARMISelLowering.h"
00021 #include "ARMMachineFunctionInfo.h"
00022 #include "ARMSubtarget.h"
00023 #include "MCTargetDesc/ARMAddressingModes.h"
00024 #include "llvm/ADT/STLExtras.h"
00025 #include "llvm/CodeGen/Analysis.h"
00026 #include "llvm/CodeGen/FastISel.h"
00027 #include "llvm/CodeGen/FunctionLoweringInfo.h"
00028 #include "llvm/CodeGen/MachineConstantPool.h"
00029 #include "llvm/CodeGen/MachineFrameInfo.h"
00030 #include "llvm/CodeGen/MachineInstrBuilder.h"
00031 #include "llvm/CodeGen/MachineMemOperand.h"
00032 #include "llvm/CodeGen/MachineModuleInfo.h"
00033 #include "llvm/CodeGen/MachineRegisterInfo.h"
00034 #include "llvm/IR/CallSite.h"
00035 #include "llvm/IR/CallingConv.h"
00036 #include "llvm/IR/DataLayout.h"
00037 #include "llvm/IR/DerivedTypes.h"
00038 #include "llvm/IR/GetElementPtrTypeIterator.h"
00039 #include "llvm/IR/GlobalVariable.h"
00040 #include "llvm/IR/Instructions.h"
00041 #include "llvm/IR/IntrinsicInst.h"
00042 #include "llvm/IR/Module.h"
00043 #include "llvm/IR/Operator.h"
00044 #include "llvm/Support/CommandLine.h"
00045 #include "llvm/Support/ErrorHandling.h"
00046 #include "llvm/Target/TargetInstrInfo.h"
00047 #include "llvm/Target/TargetLowering.h"
00048 #include "llvm/Target/TargetMachine.h"
00049 #include "llvm/Target/TargetOptions.h"
00050 using namespace llvm;
00051 
00052 extern cl::opt<bool> EnableARMLongCalls;
00053 
00054 namespace {
00055 
00056   // All possible address modes, plus some.
00057   typedef struct Address {
00058     enum {
00059       RegBase,
00060       FrameIndexBase
00061     } BaseType;
00062 
00063     union {
00064       unsigned Reg;
00065       int FI;
00066     } Base;
00067 
00068     int Offset;
00069 
00070     // Innocuous defaults for our address.
00071     Address()
00072      : BaseType(RegBase), Offset(0) {
00073        Base.Reg = 0;
00074      }
00075   } Address;
00076 
00077 class ARMFastISel final : public FastISel {
00078 
00079   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
00080   /// make the right decision when generating code for different targets.
00081   const ARMSubtarget *Subtarget;
00082   Module &M;
00083   const TargetMachine &TM;
00084   const TargetInstrInfo &TII;
00085   const TargetLowering &TLI;
00086   ARMFunctionInfo *AFI;
00087 
00088   // Convenience variables to avoid some queries.
00089   bool isThumb2;
00090   LLVMContext *Context;
00091 
00092   public:
00093     explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
00094                          const TargetLibraryInfo *libInfo)
00095         : FastISel(funcInfo, libInfo),
00096           M(const_cast<Module &>(*funcInfo.Fn->getParent())),
00097           TM(funcInfo.MF->getTarget()),
00098           TII(*TM.getSubtargetImpl()->getInstrInfo()),
00099           TLI(*TM.getSubtargetImpl()->getTargetLowering()) {
00100       Subtarget = &TM.getSubtarget<ARMSubtarget>();
00101       AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
00102       isThumb2 = AFI->isThumbFunction();
00103       Context = &funcInfo.Fn->getContext();
00104     }
00105 
00106     // Code from FastISel.cpp.
00107   private:
00108     unsigned fastEmitInst_r(unsigned MachineInstOpcode,
00109                             const TargetRegisterClass *RC,
00110                             unsigned Op0, bool Op0IsKill);
00111     unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
00112                              const TargetRegisterClass *RC,
00113                              unsigned Op0, bool Op0IsKill,
00114                              unsigned Op1, bool Op1IsKill);
00115     unsigned fastEmitInst_rrr(unsigned MachineInstOpcode,
00116                               const TargetRegisterClass *RC,
00117                               unsigned Op0, bool Op0IsKill,
00118                               unsigned Op1, bool Op1IsKill,
00119                               unsigned Op2, bool Op2IsKill);
00120     unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
00121                              const TargetRegisterClass *RC,
00122                              unsigned Op0, bool Op0IsKill,
00123                              uint64_t Imm);
00124     unsigned fastEmitInst_rri(unsigned MachineInstOpcode,
00125                               const TargetRegisterClass *RC,
00126                               unsigned Op0, bool Op0IsKill,
00127                               unsigned Op1, bool Op1IsKill,
00128                               uint64_t Imm);
00129     unsigned fastEmitInst_i(unsigned MachineInstOpcode,
00130                             const TargetRegisterClass *RC,
00131                             uint64_t Imm);
00132 
00133     // Backend specific FastISel code.
00134   private:
00135     bool fastSelectInstruction(const Instruction *I) override;
00136     unsigned fastMaterializeConstant(const Constant *C) override;
00137     unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
00138     bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
00139                              const LoadInst *LI) override;
00140     bool fastLowerArguments() override;
00141   private:
00142   #include "ARMGenFastISel.inc"
00143 
00144     // Instruction selection routines.
00145   private:
00146     bool SelectLoad(const Instruction *I);
00147     bool SelectStore(const Instruction *I);
00148     bool SelectBranch(const Instruction *I);
00149     bool SelectIndirectBr(const Instruction *I);
00150     bool SelectCmp(const Instruction *I);
00151     bool SelectFPExt(const Instruction *I);
00152     bool SelectFPTrunc(const Instruction *I);
00153     bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
00154     bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
00155     bool SelectIToFP(const Instruction *I, bool isSigned);
00156     bool SelectFPToI(const Instruction *I, bool isSigned);
00157     bool SelectDiv(const Instruction *I, bool isSigned);
00158     bool SelectRem(const Instruction *I, bool isSigned);
00159     bool SelectCall(const Instruction *I, const char *IntrMemName);
00160     bool SelectIntrinsicCall(const IntrinsicInst &I);
00161     bool SelectSelect(const Instruction *I);
00162     bool SelectRet(const Instruction *I);
00163     bool SelectTrunc(const Instruction *I);
00164     bool SelectIntExt(const Instruction *I);
00165     bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
00166 
00167     // Utility routines.
00168   private:
00169     bool isTypeLegal(Type *Ty, MVT &VT);
00170     bool isLoadTypeLegal(Type *Ty, MVT &VT);
00171     bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
00172                     bool isZExt);
00173     bool ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
00174                      unsigned Alignment = 0, bool isZExt = true,
00175                      bool allocReg = true);
00176     bool ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
00177                       unsigned Alignment = 0);
00178     bool ARMComputeAddress(const Value *Obj, Address &Addr);
00179     void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
00180     bool ARMIsMemCpySmall(uint64_t Len);
00181     bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
00182                                unsigned Alignment);
00183     unsigned ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
00184     unsigned ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
00185     unsigned ARMMaterializeInt(const Constant *C, MVT VT);
00186     unsigned ARMMaterializeGV(const GlobalValue *GV, MVT VT);
00187     unsigned ARMMoveToFPReg(MVT VT, unsigned SrcReg);
00188     unsigned ARMMoveToIntReg(MVT VT, unsigned SrcReg);
00189     unsigned ARMSelectCallOp(bool UseReg);
00190     unsigned ARMLowerPICELF(const GlobalValue *GV, unsigned Align, MVT VT);
00191 
00192     const TargetLowering *getTargetLowering() {
00193       return TM.getSubtargetImpl()->getTargetLowering();
00194     }
00195 
00196     // Call handling routines.
00197   private:
00198     CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
00199                                   bool Return,
00200                                   bool isVarArg);
00201     bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
00202                          SmallVectorImpl<unsigned> &ArgRegs,
00203                          SmallVectorImpl<MVT> &ArgVTs,
00204                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
00205                          SmallVectorImpl<unsigned> &RegArgs,
00206                          CallingConv::ID CC,
00207                          unsigned &NumBytes,
00208                          bool isVarArg);
00209     unsigned getLibcallReg(const Twine &Name);
00210     bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
00211                     const Instruction *I, CallingConv::ID CC,
00212                     unsigned &NumBytes, bool isVarArg);
00213     bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
00214 
00215     // OptionalDef handling routines.
00216   private:
00217     bool isARMNEONPred(const MachineInstr *MI);
00218     bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
00219     const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
00220     void AddLoadStoreOperands(MVT VT, Address &Addr,
00221                               const MachineInstrBuilder &MIB,
00222                               unsigned Flags, bool useAM3);
00223 };
00224 
00225 } // end anonymous namespace
00226 
00227 #include "ARMGenCallingConv.inc"
00228 
00229 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
00230 // we don't care about implicit defs here, just places we'll need to add a
00231 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
00232 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
00233   if (!MI->hasOptionalDef())
00234     return false;
00235 
00236   // Look to see if our OptionalDef is defining CPSR or CCR.
00237   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00238     const MachineOperand &MO = MI->getOperand(i);
00239     if (!MO.isReg() || !MO.isDef()) continue;
00240     if (MO.getReg() == ARM::CPSR)
00241       *CPSR = true;
00242   }
00243   return true;
00244 }
00245 
00246 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
00247   const MCInstrDesc &MCID = MI->getDesc();
00248 
00249   // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
00250   if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
00251        AFI->isThumb2Function())
00252     return MI->isPredicable();
00253 
00254   for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
00255     if (MCID.OpInfo[i].isPredicate())
00256       return true;
00257 
00258   return false;
00259 }
00260 
00261 // If the machine is predicable go ahead and add the predicate operands, if
00262 // it needs default CC operands add those.
00263 // TODO: If we want to support thumb1 then we'll need to deal with optional
00264 // CPSR defs that need to be added before the remaining operands. See s_cc_out
00265 // for descriptions why.
00266 const MachineInstrBuilder &
00267 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
00268   MachineInstr *MI = &*MIB;
00269 
00270   // Do we use a predicate? or...
00271   // Are we NEON in ARM mode and have a predicate operand? If so, I know
00272   // we're not predicable but add it anyways.
00273   if (isARMNEONPred(MI))
00274     AddDefaultPred(MIB);
00275 
00276   // Do we optionally set a predicate?  Preds is size > 0 iff the predicate
00277   // defines CPSR. All other OptionalDefines in ARM are the CCR register.
00278   bool CPSR = false;
00279   if (DefinesOptionalPredicate(MI, &CPSR)) {
00280     if (CPSR)
00281       AddDefaultT1CC(MIB);
00282     else
00283       AddDefaultCC(MIB);
00284   }
00285   return MIB;
00286 }
00287 
00288 unsigned ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
00289                                      const TargetRegisterClass *RC,
00290                                      unsigned Op0, bool Op0IsKill) {
00291   unsigned ResultReg = createResultReg(RC);
00292   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00293 
00294   // Make sure the input operand is sufficiently constrained to be legal
00295   // for this instruction.
00296   Op0 = constrainOperandRegClass(II, Op0, 1);
00297   if (II.getNumDefs() >= 1) {
00298     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
00299                             ResultReg).addReg(Op0, Op0IsKill * RegState::Kill));
00300   } else {
00301     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00302                    .addReg(Op0, Op0IsKill * RegState::Kill));
00303     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00304                    TII.get(TargetOpcode::COPY), ResultReg)
00305                    .addReg(II.ImplicitDefs[0]));
00306   }
00307   return ResultReg;
00308 }
00309 
00310 unsigned ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
00311                                       const TargetRegisterClass *RC,
00312                                       unsigned Op0, bool Op0IsKill,
00313                                       unsigned Op1, bool Op1IsKill) {
00314   unsigned ResultReg = createResultReg(RC);
00315   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00316 
00317   // Make sure the input operands are sufficiently constrained to be legal
00318   // for this instruction.
00319   Op0 = constrainOperandRegClass(II, Op0, 1);
00320   Op1 = constrainOperandRegClass(II, Op1, 2);
00321 
00322   if (II.getNumDefs() >= 1) {
00323     AddOptionalDefs(
00324         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
00325             .addReg(Op0, Op0IsKill * RegState::Kill)
00326             .addReg(Op1, Op1IsKill * RegState::Kill));
00327   } else {
00328     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00329                    .addReg(Op0, Op0IsKill * RegState::Kill)
00330                    .addReg(Op1, Op1IsKill * RegState::Kill));
00331     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00332                            TII.get(TargetOpcode::COPY), ResultReg)
00333                    .addReg(II.ImplicitDefs[0]));
00334   }
00335   return ResultReg;
00336 }
00337 
00338 unsigned ARMFastISel::fastEmitInst_rrr(unsigned MachineInstOpcode,
00339                                        const TargetRegisterClass *RC,
00340                                        unsigned Op0, bool Op0IsKill,
00341                                        unsigned Op1, bool Op1IsKill,
00342                                        unsigned Op2, bool Op2IsKill) {
00343   unsigned ResultReg = createResultReg(RC);
00344   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00345 
00346   // Make sure the input operands are sufficiently constrained to be legal
00347   // for this instruction.
00348   Op0 = constrainOperandRegClass(II, Op0, 1);
00349   Op1 = constrainOperandRegClass(II, Op1, 2);
00350   Op2 = constrainOperandRegClass(II, Op1, 3);
00351 
00352   if (II.getNumDefs() >= 1) {
00353     AddOptionalDefs(
00354         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
00355             .addReg(Op0, Op0IsKill * RegState::Kill)
00356             .addReg(Op1, Op1IsKill * RegState::Kill)
00357             .addReg(Op2, Op2IsKill * RegState::Kill));
00358   } else {
00359     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00360                    .addReg(Op0, Op0IsKill * RegState::Kill)
00361                    .addReg(Op1, Op1IsKill * RegState::Kill)
00362                    .addReg(Op2, Op2IsKill * RegState::Kill));
00363     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00364                            TII.get(TargetOpcode::COPY), ResultReg)
00365                    .addReg(II.ImplicitDefs[0]));
00366   }
00367   return ResultReg;
00368 }
00369 
00370 unsigned ARMFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
00371                                       const TargetRegisterClass *RC,
00372                                       unsigned Op0, bool Op0IsKill,
00373                                       uint64_t Imm) {
00374   unsigned ResultReg = createResultReg(RC);
00375   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00376 
00377   // Make sure the input operand is sufficiently constrained to be legal
00378   // for this instruction.
00379   Op0 = constrainOperandRegClass(II, Op0, 1);
00380   if (II.getNumDefs() >= 1) {
00381     AddOptionalDefs(
00382         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
00383             .addReg(Op0, Op0IsKill * RegState::Kill)
00384             .addImm(Imm));
00385   } else {
00386     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00387                    .addReg(Op0, Op0IsKill * RegState::Kill)
00388                    .addImm(Imm));
00389     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00390                            TII.get(TargetOpcode::COPY), ResultReg)
00391                    .addReg(II.ImplicitDefs[0]));
00392   }
00393   return ResultReg;
00394 }
00395 
00396 unsigned ARMFastISel::fastEmitInst_rri(unsigned MachineInstOpcode,
00397                                        const TargetRegisterClass *RC,
00398                                        unsigned Op0, bool Op0IsKill,
00399                                        unsigned Op1, bool Op1IsKill,
00400                                        uint64_t Imm) {
00401   unsigned ResultReg = createResultReg(RC);
00402   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00403 
00404   // Make sure the input operands are sufficiently constrained to be legal
00405   // for this instruction.
00406   Op0 = constrainOperandRegClass(II, Op0, 1);
00407   Op1 = constrainOperandRegClass(II, Op1, 2);
00408   if (II.getNumDefs() >= 1) {
00409     AddOptionalDefs(
00410         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
00411             .addReg(Op0, Op0IsKill * RegState::Kill)
00412             .addReg(Op1, Op1IsKill * RegState::Kill)
00413             .addImm(Imm));
00414   } else {
00415     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00416                    .addReg(Op0, Op0IsKill * RegState::Kill)
00417                    .addReg(Op1, Op1IsKill * RegState::Kill)
00418                    .addImm(Imm));
00419     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00420                            TII.get(TargetOpcode::COPY), ResultReg)
00421                    .addReg(II.ImplicitDefs[0]));
00422   }
00423   return ResultReg;
00424 }
00425 
00426 unsigned ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode,
00427                                      const TargetRegisterClass *RC,
00428                                      uint64_t Imm) {
00429   unsigned ResultReg = createResultReg(RC);
00430   const MCInstrDesc &II = TII.get(MachineInstOpcode);
00431 
00432   if (II.getNumDefs() >= 1) {
00433     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
00434                             ResultReg).addImm(Imm));
00435   } else {
00436     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
00437                    .addImm(Imm));
00438     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00439                            TII.get(TargetOpcode::COPY), ResultReg)
00440                    .addReg(II.ImplicitDefs[0]));
00441   }
00442   return ResultReg;
00443 }
00444 
00445 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
00446 // checks from the various callers.
00447 unsigned ARMFastISel::ARMMoveToFPReg(MVT VT, unsigned SrcReg) {
00448   if (VT == MVT::f64) return 0;
00449 
00450   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
00451   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00452                           TII.get(ARM::VMOVSR), MoveReg)
00453                   .addReg(SrcReg));
00454   return MoveReg;
00455 }
00456 
00457 unsigned ARMFastISel::ARMMoveToIntReg(MVT VT, unsigned SrcReg) {
00458   if (VT == MVT::i64) return 0;
00459 
00460   unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
00461   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00462                           TII.get(ARM::VMOVRS), MoveReg)
00463                   .addReg(SrcReg));
00464   return MoveReg;
00465 }
00466 
00467 // For double width floating point we need to materialize two constants
00468 // (the high and the low) into integer registers then use a move to get
00469 // the combined constant into an FP reg.
00470 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
00471   const APFloat Val = CFP->getValueAPF();
00472   bool is64bit = VT == MVT::f64;
00473 
00474   // This checks to see if we can use VFP3 instructions to materialize
00475   // a constant, otherwise we have to go through the constant pool.
00476   if (TLI.isFPImmLegal(Val, VT)) {
00477     int Imm;
00478     unsigned Opc;
00479     if (is64bit) {
00480       Imm = ARM_AM::getFP64Imm(Val);
00481       Opc = ARM::FCONSTD;
00482     } else {
00483       Imm = ARM_AM::getFP32Imm(Val);
00484       Opc = ARM::FCONSTS;
00485     }
00486     unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
00487     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00488                             TII.get(Opc), DestReg).addImm(Imm));
00489     return DestReg;
00490   }
00491 
00492   // Require VFP2 for loading fp constants.
00493   if (!Subtarget->hasVFP2()) return false;
00494 
00495   // MachineConstantPool wants an explicit alignment.
00496   unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
00497   if (Align == 0) {
00498     // TODO: Figure out if this is correct.
00499     Align = DL.getTypeAllocSize(CFP->getType());
00500   }
00501   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
00502   unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
00503   unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
00504 
00505   // The extra reg is for addrmode5.
00506   AddOptionalDefs(
00507       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
00508           .addConstantPoolIndex(Idx)
00509           .addReg(0));
00510   return DestReg;
00511 }
00512 
00513 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
00514 
00515   if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
00516     return 0;
00517 
00518   // If we can do this in a single instruction without a constant pool entry
00519   // do so now.
00520   const ConstantInt *CI = cast<ConstantInt>(C);
00521   if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
00522     unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
00523     const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
00524       &ARM::GPRRegClass;
00525     unsigned ImmReg = createResultReg(RC);
00526     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00527                             TII.get(Opc), ImmReg)
00528                     .addImm(CI->getZExtValue()));
00529     return ImmReg;
00530   }
00531 
00532   // Use MVN to emit negative constants.
00533   if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
00534     unsigned Imm = (unsigned)~(CI->getSExtValue());
00535     bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
00536       (ARM_AM::getSOImmVal(Imm) != -1);
00537     if (UseImm) {
00538       unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
00539       const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
00540                                                  &ARM::GPRRegClass;
00541       unsigned ImmReg = createResultReg(RC);
00542       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00543                               TII.get(Opc), ImmReg)
00544                       .addImm(Imm));
00545       return ImmReg;
00546     }
00547   }
00548 
00549   unsigned ResultReg = 0;
00550   if (Subtarget->useMovt(*FuncInfo.MF))
00551     ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
00552 
00553   if (ResultReg)
00554     return ResultReg;
00555 
00556   // Load from constant pool.  For now 32-bit only.
00557   if (VT != MVT::i32)
00558     return 0;
00559 
00560   // MachineConstantPool wants an explicit alignment.
00561   unsigned Align = DL.getPrefTypeAlignment(C->getType());
00562   if (Align == 0) {
00563     // TODO: Figure out if this is correct.
00564     Align = DL.getTypeAllocSize(C->getType());
00565   }
00566   unsigned Idx = MCP.getConstantPoolIndex(C, Align);
00567   ResultReg = createResultReg(TLI.getRegClassFor(VT));
00568   if (isThumb2)
00569     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00570                             TII.get(ARM::t2LDRpci), ResultReg)
00571                       .addConstantPoolIndex(Idx));
00572   else {
00573     // The extra immediate is for addrmode2.
00574     ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0);
00575     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00576                             TII.get(ARM::LDRcp), ResultReg)
00577                       .addConstantPoolIndex(Idx)
00578                       .addImm(0));
00579   }
00580   return ResultReg;
00581 }
00582 
00583 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
00584   // For now 32-bit only.
00585   if (VT != MVT::i32) return 0;
00586 
00587   Reloc::Model RelocM = TM.getRelocationModel();
00588   bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
00589   const TargetRegisterClass *RC = isThumb2 ?
00590     (const TargetRegisterClass*)&ARM::rGPRRegClass :
00591     (const TargetRegisterClass*)&ARM::GPRRegClass;
00592   unsigned DestReg = createResultReg(RC);
00593 
00594   // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
00595   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
00596   bool IsThreadLocal = GVar && GVar->isThreadLocal();
00597   if (!Subtarget->isTargetMachO() && IsThreadLocal) return 0;
00598 
00599   // Use movw+movt when possible, it avoids constant pool entries.
00600   // Non-darwin targets only support static movt relocations in FastISel.
00601   if (Subtarget->useMovt(*FuncInfo.MF) &&
00602       (Subtarget->isTargetMachO() || RelocM == Reloc::Static)) {
00603     unsigned Opc;
00604     unsigned char TF = 0;
00605     if (Subtarget->isTargetMachO())
00606       TF = ARMII::MO_NONLAZY;
00607 
00608     switch (RelocM) {
00609     case Reloc::PIC_:
00610       Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
00611       break;
00612     default:
00613       Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
00614       break;
00615     }
00616     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00617                             TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
00618   } else {
00619     // MachineConstantPool wants an explicit alignment.
00620     unsigned Align = DL.getPrefTypeAlignment(GV->getType());
00621     if (Align == 0) {
00622       // TODO: Figure out if this is correct.
00623       Align = DL.getTypeAllocSize(GV->getType());
00624     }
00625 
00626     if (Subtarget->isTargetELF() && RelocM == Reloc::PIC_)
00627       return ARMLowerPICELF(GV, Align, VT);
00628 
00629     // Grab index.
00630     unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
00631       (Subtarget->isThumb() ? 4 : 8);
00632     unsigned Id = AFI->createPICLabelUId();
00633     ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
00634                                                                 ARMCP::CPValue,
00635                                                                 PCAdj);
00636     unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
00637 
00638     // Load value.
00639     MachineInstrBuilder MIB;
00640     if (isThumb2) {
00641       unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
00642       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
00643                     DestReg).addConstantPoolIndex(Idx);
00644       if (RelocM == Reloc::PIC_)
00645         MIB.addImm(Id);
00646       AddOptionalDefs(MIB);
00647     } else {
00648       // The extra immediate is for addrmode2.
00649       DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
00650       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00651                     TII.get(ARM::LDRcp), DestReg)
00652                 .addConstantPoolIndex(Idx)
00653                 .addImm(0);
00654       AddOptionalDefs(MIB);
00655 
00656       if (RelocM == Reloc::PIC_) {
00657         unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
00658         unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
00659 
00660         MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
00661                                           DbgLoc, TII.get(Opc), NewDestReg)
00662                                   .addReg(DestReg)
00663                                   .addImm(Id);
00664         AddOptionalDefs(MIB);
00665         return NewDestReg;
00666       }
00667     }
00668   }
00669 
00670   if (IsIndirect) {
00671     MachineInstrBuilder MIB;
00672     unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
00673     if (isThumb2)
00674       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00675                     TII.get(ARM::t2LDRi12), NewDestReg)
00676             .addReg(DestReg)
00677             .addImm(0);
00678     else
00679       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00680                     TII.get(ARM::LDRi12), NewDestReg)
00681                 .addReg(DestReg)
00682                 .addImm(0);
00683     DestReg = NewDestReg;
00684     AddOptionalDefs(MIB);
00685   }
00686 
00687   return DestReg;
00688 }
00689 
00690 unsigned ARMFastISel::fastMaterializeConstant(const Constant *C) {
00691   EVT CEVT = TLI.getValueType(C->getType(), true);
00692 
00693   // Only handle simple types.
00694   if (!CEVT.isSimple()) return 0;
00695   MVT VT = CEVT.getSimpleVT();
00696 
00697   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
00698     return ARMMaterializeFP(CFP, VT);
00699   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
00700     return ARMMaterializeGV(GV, VT);
00701   else if (isa<ConstantInt>(C))
00702     return ARMMaterializeInt(C, VT);
00703 
00704   return 0;
00705 }
00706 
00707 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
00708 
00709 unsigned ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
00710   // Don't handle dynamic allocas.
00711   if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
00712 
00713   MVT VT;
00714   if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
00715 
00716   DenseMap<const AllocaInst*, int>::iterator SI =
00717     FuncInfo.StaticAllocaMap.find(AI);
00718 
00719   // This will get lowered later into the correct offsets and registers
00720   // via rewriteXFrameIndex.
00721   if (SI != FuncInfo.StaticAllocaMap.end()) {
00722     unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
00723     const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
00724     unsigned ResultReg = createResultReg(RC);
00725     ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
00726 
00727     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00728                             TII.get(Opc), ResultReg)
00729                             .addFrameIndex(SI->second)
00730                             .addImm(0));
00731     return ResultReg;
00732   }
00733 
00734   return 0;
00735 }
00736 
00737 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
00738   EVT evt = TLI.getValueType(Ty, true);
00739 
00740   // Only handle simple types.
00741   if (evt == MVT::Other || !evt.isSimple()) return false;
00742   VT = evt.getSimpleVT();
00743 
00744   // Handle all legal types, i.e. a register that will directly hold this
00745   // value.
00746   return TLI.isTypeLegal(VT);
00747 }
00748 
00749 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
00750   if (isTypeLegal(Ty, VT)) return true;
00751 
00752   // If this is a type than can be sign or zero-extended to a basic operation
00753   // go ahead and accept it now.
00754   if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
00755     return true;
00756 
00757   return false;
00758 }
00759 
00760 // Computes the address to get to an object.
00761 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
00762   // Some boilerplate from the X86 FastISel.
00763   const User *U = nullptr;
00764   unsigned Opcode = Instruction::UserOp1;
00765   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
00766     // Don't walk into other basic blocks unless the object is an alloca from
00767     // another block, otherwise it may not have a virtual register assigned.
00768     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
00769         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
00770       Opcode = I->getOpcode();
00771       U = I;
00772     }
00773   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
00774     Opcode = C->getOpcode();
00775     U = C;
00776   }
00777 
00778   if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
00779     if (Ty->getAddressSpace() > 255)
00780       // Fast instruction selection doesn't support the special
00781       // address spaces.
00782       return false;
00783 
00784   switch (Opcode) {
00785     default:
00786     break;
00787     case Instruction::BitCast:
00788       // Look through bitcasts.
00789       return ARMComputeAddress(U->getOperand(0), Addr);
00790     case Instruction::IntToPtr:
00791       // Look past no-op inttoptrs.
00792       if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
00793         return ARMComputeAddress(U->getOperand(0), Addr);
00794       break;
00795     case Instruction::PtrToInt:
00796       // Look past no-op ptrtoints.
00797       if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
00798         return ARMComputeAddress(U->getOperand(0), Addr);
00799       break;
00800     case Instruction::GetElementPtr: {
00801       Address SavedAddr = Addr;
00802       int TmpOffset = Addr.Offset;
00803 
00804       // Iterate through the GEP folding the constants into offsets where
00805       // we can.
00806       gep_type_iterator GTI = gep_type_begin(U);
00807       for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
00808            i != e; ++i, ++GTI) {
00809         const Value *Op = *i;
00810         if (StructType *STy = dyn_cast<StructType>(*GTI)) {
00811           const StructLayout *SL = DL.getStructLayout(STy);
00812           unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
00813           TmpOffset += SL->getElementOffset(Idx);
00814         } else {
00815           uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
00816           for (;;) {
00817             if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
00818               // Constant-offset addressing.
00819               TmpOffset += CI->getSExtValue() * S;
00820               break;
00821             }
00822             if (canFoldAddIntoGEP(U, Op)) {
00823               // A compatible add with a constant operand. Fold the constant.
00824               ConstantInt *CI =
00825               cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
00826               TmpOffset += CI->getSExtValue() * S;
00827               // Iterate on the other operand.
00828               Op = cast<AddOperator>(Op)->getOperand(0);
00829               continue;
00830             }
00831             // Unsupported
00832             goto unsupported_gep;
00833           }
00834         }
00835       }
00836 
00837       // Try to grab the base operand now.
00838       Addr.Offset = TmpOffset;
00839       if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
00840 
00841       // We failed, restore everything and try the other options.
00842       Addr = SavedAddr;
00843 
00844       unsupported_gep:
00845       break;
00846     }
00847     case Instruction::Alloca: {
00848       const AllocaInst *AI = cast<AllocaInst>(Obj);
00849       DenseMap<const AllocaInst*, int>::iterator SI =
00850         FuncInfo.StaticAllocaMap.find(AI);
00851       if (SI != FuncInfo.StaticAllocaMap.end()) {
00852         Addr.BaseType = Address::FrameIndexBase;
00853         Addr.Base.FI = SI->second;
00854         return true;
00855       }
00856       break;
00857     }
00858   }
00859 
00860   // Try to get this in a register if nothing else has worked.
00861   if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
00862   return Addr.Base.Reg != 0;
00863 }
00864 
00865 void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
00866   bool needsLowering = false;
00867   switch (VT.SimpleTy) {
00868     default: llvm_unreachable("Unhandled load/store type!");
00869     case MVT::i1:
00870     case MVT::i8:
00871     case MVT::i16:
00872     case MVT::i32:
00873       if (!useAM3) {
00874         // Integer loads/stores handle 12-bit offsets.
00875         needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
00876         // Handle negative offsets.
00877         if (needsLowering && isThumb2)
00878           needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
00879                             Addr.Offset > -256);
00880       } else {
00881         // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
00882         needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
00883       }
00884       break;
00885     case MVT::f32:
00886     case MVT::f64:
00887       // Floating point operands handle 8-bit offsets.
00888       needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
00889       break;
00890   }
00891 
00892   // If this is a stack pointer and the offset needs to be simplified then
00893   // put the alloca address into a register, set the base type back to
00894   // register and continue. This should almost never happen.
00895   if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
00896     const TargetRegisterClass *RC = isThumb2 ?
00897       (const TargetRegisterClass*)&ARM::tGPRRegClass :
00898       (const TargetRegisterClass*)&ARM::GPRRegClass;
00899     unsigned ResultReg = createResultReg(RC);
00900     unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
00901     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00902                             TII.get(Opc), ResultReg)
00903                             .addFrameIndex(Addr.Base.FI)
00904                             .addImm(0));
00905     Addr.Base.Reg = ResultReg;
00906     Addr.BaseType = Address::RegBase;
00907   }
00908 
00909   // Since the offset is too large for the load/store instruction
00910   // get the reg+offset into a register.
00911   if (needsLowering) {
00912     Addr.Base.Reg = fastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
00913                                  /*Op0IsKill*/false, Addr.Offset, MVT::i32);
00914     Addr.Offset = 0;
00915   }
00916 }
00917 
00918 void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
00919                                        const MachineInstrBuilder &MIB,
00920                                        unsigned Flags, bool useAM3) {
00921   // addrmode5 output depends on the selection dag addressing dividing the
00922   // offset by 4 that it then later multiplies. Do this here as well.
00923   if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
00924     Addr.Offset /= 4;
00925 
00926   // Frame base works a bit differently. Handle it separately.
00927   if (Addr.BaseType == Address::FrameIndexBase) {
00928     int FI = Addr.Base.FI;
00929     int Offset = Addr.Offset;
00930     MachineMemOperand *MMO =
00931           FuncInfo.MF->getMachineMemOperand(
00932                                   MachinePointerInfo::getFixedStack(FI, Offset),
00933                                   Flags,
00934                                   MFI.getObjectSize(FI),
00935                                   MFI.getObjectAlignment(FI));
00936     // Now add the rest of the operands.
00937     MIB.addFrameIndex(FI);
00938 
00939     // ARM halfword load/stores and signed byte loads need an additional
00940     // operand.
00941     if (useAM3) {
00942       signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
00943       MIB.addReg(0);
00944       MIB.addImm(Imm);
00945     } else {
00946       MIB.addImm(Addr.Offset);
00947     }
00948     MIB.addMemOperand(MMO);
00949   } else {
00950     // Now add the rest of the operands.
00951     MIB.addReg(Addr.Base.Reg);
00952 
00953     // ARM halfword load/stores and signed byte loads need an additional
00954     // operand.
00955     if (useAM3) {
00956       signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
00957       MIB.addReg(0);
00958       MIB.addImm(Imm);
00959     } else {
00960       MIB.addImm(Addr.Offset);
00961     }
00962   }
00963   AddOptionalDefs(MIB);
00964 }
00965 
00966 bool ARMFastISel::ARMEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
00967                               unsigned Alignment, bool isZExt, bool allocReg) {
00968   unsigned Opc;
00969   bool useAM3 = false;
00970   bool needVMOV = false;
00971   const TargetRegisterClass *RC;
00972   switch (VT.SimpleTy) {
00973     // This is mostly going to be Neon/vector support.
00974     default: return false;
00975     case MVT::i1:
00976     case MVT::i8:
00977       if (isThumb2) {
00978         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
00979           Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
00980         else
00981           Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
00982       } else {
00983         if (isZExt) {
00984           Opc = ARM::LDRBi12;
00985         } else {
00986           Opc = ARM::LDRSB;
00987           useAM3 = true;
00988         }
00989       }
00990       RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
00991       break;
00992     case MVT::i16:
00993       if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
00994         return false;
00995 
00996       if (isThumb2) {
00997         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
00998           Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
00999         else
01000           Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
01001       } else {
01002         Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
01003         useAM3 = true;
01004       }
01005       RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
01006       break;
01007     case MVT::i32:
01008       if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
01009         return false;
01010 
01011       if (isThumb2) {
01012         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
01013           Opc = ARM::t2LDRi8;
01014         else
01015           Opc = ARM::t2LDRi12;
01016       } else {
01017         Opc = ARM::LDRi12;
01018       }
01019       RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
01020       break;
01021     case MVT::f32:
01022       if (!Subtarget->hasVFP2()) return false;
01023       // Unaligned loads need special handling. Floats require word-alignment.
01024       if (Alignment && Alignment < 4) {
01025         needVMOV = true;
01026         VT = MVT::i32;
01027         Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
01028         RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
01029       } else {
01030         Opc = ARM::VLDRS;
01031         RC = TLI.getRegClassFor(VT);
01032       }
01033       break;
01034     case MVT::f64:
01035       if (!Subtarget->hasVFP2()) return false;
01036       // FIXME: Unaligned loads need special handling.  Doublewords require
01037       // word-alignment.
01038       if (Alignment && Alignment < 4)
01039         return false;
01040 
01041       Opc = ARM::VLDRD;
01042       RC = TLI.getRegClassFor(VT);
01043       break;
01044   }
01045   // Simplify this down to something we can handle.
01046   ARMSimplifyAddress(Addr, VT, useAM3);
01047 
01048   // Create the base instruction, then add the operands.
01049   if (allocReg)
01050     ResultReg = createResultReg(RC);
01051   assert (ResultReg > 255 && "Expected an allocated virtual register.");
01052   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01053                                     TII.get(Opc), ResultReg);
01054   AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
01055 
01056   // If we had an unaligned load of a float we've converted it to an regular
01057   // load.  Now we must move from the GRP to the FP register.
01058   if (needVMOV) {
01059     unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
01060     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01061                             TII.get(ARM::VMOVSR), MoveReg)
01062                     .addReg(ResultReg));
01063     ResultReg = MoveReg;
01064   }
01065   return true;
01066 }
01067 
01068 bool ARMFastISel::SelectLoad(const Instruction *I) {
01069   // Atomic loads need special handling.
01070   if (cast<LoadInst>(I)->isAtomic())
01071     return false;
01072 
01073   // Verify we have a legal type before going any further.
01074   MVT VT;
01075   if (!isLoadTypeLegal(I->getType(), VT))
01076     return false;
01077 
01078   // See if we can handle this address.
01079   Address Addr;
01080   if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
01081 
01082   unsigned ResultReg;
01083   if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
01084     return false;
01085   updateValueMap(I, ResultReg);
01086   return true;
01087 }
01088 
01089 bool ARMFastISel::ARMEmitStore(MVT VT, unsigned SrcReg, Address &Addr,
01090                                unsigned Alignment) {
01091   unsigned StrOpc;
01092   bool useAM3 = false;
01093   switch (VT.SimpleTy) {
01094     // This is mostly going to be Neon/vector support.
01095     default: return false;
01096     case MVT::i1: {
01097       unsigned Res = createResultReg(isThumb2 ?
01098         (const TargetRegisterClass*)&ARM::tGPRRegClass :
01099         (const TargetRegisterClass*)&ARM::GPRRegClass);
01100       unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
01101       SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
01102       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01103                               TII.get(Opc), Res)
01104                       .addReg(SrcReg).addImm(1));
01105       SrcReg = Res;
01106     } // Fallthrough here.
01107     case MVT::i8:
01108       if (isThumb2) {
01109         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
01110           StrOpc = ARM::t2STRBi8;
01111         else
01112           StrOpc = ARM::t2STRBi12;
01113       } else {
01114         StrOpc = ARM::STRBi12;
01115       }
01116       break;
01117     case MVT::i16:
01118       if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
01119         return false;
01120 
01121       if (isThumb2) {
01122         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
01123           StrOpc = ARM::t2STRHi8;
01124         else
01125           StrOpc = ARM::t2STRHi12;
01126       } else {
01127         StrOpc = ARM::STRH;
01128         useAM3 = true;
01129       }
01130       break;
01131     case MVT::i32:
01132       if (Alignment && Alignment < 4 && !Subtarget->allowsUnalignedMem())
01133         return false;
01134 
01135       if (isThumb2) {
01136         if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
01137           StrOpc = ARM::t2STRi8;
01138         else
01139           StrOpc = ARM::t2STRi12;
01140       } else {
01141         StrOpc = ARM::STRi12;
01142       }
01143       break;
01144     case MVT::f32:
01145       if (!Subtarget->hasVFP2()) return false;
01146       // Unaligned stores need special handling. Floats require word-alignment.
01147       if (Alignment && Alignment < 4) {
01148         unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
01149         AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01150                                 TII.get(ARM::VMOVRS), MoveReg)
01151                         .addReg(SrcReg));
01152         SrcReg = MoveReg;
01153         VT = MVT::i32;
01154         StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
01155       } else {
01156         StrOpc = ARM::VSTRS;
01157       }
01158       break;
01159     case MVT::f64:
01160       if (!Subtarget->hasVFP2()) return false;
01161       // FIXME: Unaligned stores need special handling.  Doublewords require
01162       // word-alignment.
01163       if (Alignment && Alignment < 4)
01164           return false;
01165 
01166       StrOpc = ARM::VSTRD;
01167       break;
01168   }
01169   // Simplify this down to something we can handle.
01170   ARMSimplifyAddress(Addr, VT, useAM3);
01171 
01172   // Create the base instruction, then add the operands.
01173   SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
01174   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01175                                     TII.get(StrOpc))
01176                             .addReg(SrcReg);
01177   AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
01178   return true;
01179 }
01180 
01181 bool ARMFastISel::SelectStore(const Instruction *I) {
01182   Value *Op0 = I->getOperand(0);
01183   unsigned SrcReg = 0;
01184 
01185   // Atomic stores need special handling.
01186   if (cast<StoreInst>(I)->isAtomic())
01187     return false;
01188 
01189   // Verify we have a legal type before going any further.
01190   MVT VT;
01191   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
01192     return false;
01193 
01194   // Get the value to be stored into a register.
01195   SrcReg = getRegForValue(Op0);
01196   if (SrcReg == 0) return false;
01197 
01198   // See if we can handle this address.
01199   Address Addr;
01200   if (!ARMComputeAddress(I->getOperand(1), Addr))
01201     return false;
01202 
01203   if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
01204     return false;
01205   return true;
01206 }
01207 
01208 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
01209   switch (Pred) {
01210     // Needs two compares...
01211     case CmpInst::FCMP_ONE:
01212     case CmpInst::FCMP_UEQ:
01213     default:
01214       // AL is our "false" for now. The other two need more compares.
01215       return ARMCC::AL;
01216     case CmpInst::ICMP_EQ:
01217     case CmpInst::FCMP_OEQ:
01218       return ARMCC::EQ;
01219     case CmpInst::ICMP_SGT:
01220     case CmpInst::FCMP_OGT:
01221       return ARMCC::GT;
01222     case CmpInst::ICMP_SGE:
01223     case CmpInst::FCMP_OGE:
01224       return ARMCC::GE;
01225     case CmpInst::ICMP_UGT:
01226     case CmpInst::FCMP_UGT:
01227       return ARMCC::HI;
01228     case CmpInst::FCMP_OLT:
01229       return ARMCC::MI;
01230     case CmpInst::ICMP_ULE:
01231     case CmpInst::FCMP_OLE:
01232       return ARMCC::LS;
01233     case CmpInst::FCMP_ORD:
01234       return ARMCC::VC;
01235     case CmpInst::FCMP_UNO:
01236       return ARMCC::VS;
01237     case CmpInst::FCMP_UGE:
01238       return ARMCC::PL;
01239     case CmpInst::ICMP_SLT:
01240     case CmpInst::FCMP_ULT:
01241       return ARMCC::LT;
01242     case CmpInst::ICMP_SLE:
01243     case CmpInst::FCMP_ULE:
01244       return ARMCC::LE;
01245     case CmpInst::FCMP_UNE:
01246     case CmpInst::ICMP_NE:
01247       return ARMCC::NE;
01248     case CmpInst::ICMP_UGE:
01249       return ARMCC::HS;
01250     case CmpInst::ICMP_ULT:
01251       return ARMCC::LO;
01252   }
01253 }
01254 
01255 bool ARMFastISel::SelectBranch(const Instruction *I) {
01256   const BranchInst *BI = cast<BranchInst>(I);
01257   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
01258   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
01259 
01260   // Simple branch support.
01261 
01262   // If we can, avoid recomputing the compare - redoing it could lead to wonky
01263   // behavior.
01264   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
01265     if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
01266 
01267       // Get the compare predicate.
01268       // Try to take advantage of fallthrough opportunities.
01269       CmpInst::Predicate Predicate = CI->getPredicate();
01270       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
01271         std::swap(TBB, FBB);
01272         Predicate = CmpInst::getInversePredicate(Predicate);
01273       }
01274 
01275       ARMCC::CondCodes ARMPred = getComparePred(Predicate);
01276 
01277       // We may not handle every CC for now.
01278       if (ARMPred == ARMCC::AL) return false;
01279 
01280       // Emit the compare.
01281       if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
01282         return false;
01283 
01284       unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
01285       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
01286       .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
01287       fastEmitBranch(FBB, DbgLoc);
01288       FuncInfo.MBB->addSuccessor(TBB);
01289       return true;
01290     }
01291   } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
01292     MVT SourceVT;
01293     if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
01294         (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
01295       unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
01296       unsigned OpReg = getRegForValue(TI->getOperand(0));
01297       OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
01298       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01299                               TII.get(TstOpc))
01300                       .addReg(OpReg).addImm(1));
01301 
01302       unsigned CCMode = ARMCC::NE;
01303       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
01304         std::swap(TBB, FBB);
01305         CCMode = ARMCC::EQ;
01306       }
01307 
01308       unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
01309       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
01310       .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
01311 
01312       fastEmitBranch(FBB, DbgLoc);
01313       FuncInfo.MBB->addSuccessor(TBB);
01314       return true;
01315     }
01316   } else if (const ConstantInt *CI =
01317              dyn_cast<ConstantInt>(BI->getCondition())) {
01318     uint64_t Imm = CI->getZExtValue();
01319     MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
01320     fastEmitBranch(Target, DbgLoc);
01321     return true;
01322   }
01323 
01324   unsigned CmpReg = getRegForValue(BI->getCondition());
01325   if (CmpReg == 0) return false;
01326 
01327   // We've been divorced from our compare!  Our block was split, and
01328   // now our compare lives in a predecessor block.  We musn't
01329   // re-compare here, as the children of the compare aren't guaranteed
01330   // live across the block boundary (we *could* check for this).
01331   // Regardless, the compare has been done in the predecessor block,
01332   // and it left a value for us in a virtual register.  Ergo, we test
01333   // the one-bit value left in the virtual register.
01334   unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
01335   CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
01336   AddOptionalDefs(
01337       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TstOpc))
01338           .addReg(CmpReg)
01339           .addImm(1));
01340 
01341   unsigned CCMode = ARMCC::NE;
01342   if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
01343     std::swap(TBB, FBB);
01344     CCMode = ARMCC::EQ;
01345   }
01346 
01347   unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
01348   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BrOpc))
01349                   .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
01350   fastEmitBranch(FBB, DbgLoc);
01351   FuncInfo.MBB->addSuccessor(TBB);
01352   return true;
01353 }
01354 
01355 bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
01356   unsigned AddrReg = getRegForValue(I->getOperand(0));
01357   if (AddrReg == 0) return false;
01358 
01359   unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
01360   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01361                           TII.get(Opc)).addReg(AddrReg));
01362 
01363   const IndirectBrInst *IB = cast<IndirectBrInst>(I);
01364   for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
01365     FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
01366 
01367   return true;
01368 }
01369 
01370 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
01371                              bool isZExt) {
01372   Type *Ty = Src1Value->getType();
01373   EVT SrcEVT = TLI.getValueType(Ty, true);
01374   if (!SrcEVT.isSimple()) return false;
01375   MVT SrcVT = SrcEVT.getSimpleVT();
01376 
01377   bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
01378   if (isFloat && !Subtarget->hasVFP2())
01379     return false;
01380 
01381   // Check to see if the 2nd operand is a constant that we can encode directly
01382   // in the compare.
01383   int Imm = 0;
01384   bool UseImm = false;
01385   bool isNegativeImm = false;
01386   // FIXME: At -O0 we don't have anything that canonicalizes operand order.
01387   // Thus, Src1Value may be a ConstantInt, but we're missing it.
01388   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
01389     if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
01390         SrcVT == MVT::i1) {
01391       const APInt &CIVal = ConstInt->getValue();
01392       Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
01393       // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
01394       // then a cmn, because there is no way to represent 2147483648 as a
01395       // signed 32-bit int.
01396       if (Imm < 0 && Imm != (int)0x80000000) {
01397         isNegativeImm = true;
01398         Imm = -Imm;
01399       }
01400       UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
01401         (ARM_AM::getSOImmVal(Imm) != -1);
01402     }
01403   } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
01404     if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
01405       if (ConstFP->isZero() && !ConstFP->isNegative())
01406         UseImm = true;
01407   }
01408 
01409   unsigned CmpOpc;
01410   bool isICmp = true;
01411   bool needsExt = false;
01412   switch (SrcVT.SimpleTy) {
01413     default: return false;
01414     // TODO: Verify compares.
01415     case MVT::f32:
01416       isICmp = false;
01417       CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
01418       break;
01419     case MVT::f64:
01420       isICmp = false;
01421       CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
01422       break;
01423     case MVT::i1:
01424     case MVT::i8:
01425     case MVT::i16:
01426       needsExt = true;
01427     // Intentional fall-through.
01428     case MVT::i32:
01429       if (isThumb2) {
01430         if (!UseImm)
01431           CmpOpc = ARM::t2CMPrr;
01432         else
01433           CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
01434       } else {
01435         if (!UseImm)
01436           CmpOpc = ARM::CMPrr;
01437         else
01438           CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
01439       }
01440       break;
01441   }
01442 
01443   unsigned SrcReg1 = getRegForValue(Src1Value);
01444   if (SrcReg1 == 0) return false;
01445 
01446   unsigned SrcReg2 = 0;
01447   if (!UseImm) {
01448     SrcReg2 = getRegForValue(Src2Value);
01449     if (SrcReg2 == 0) return false;
01450   }
01451 
01452   // We have i1, i8, or i16, we need to either zero extend or sign extend.
01453   if (needsExt) {
01454     SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
01455     if (SrcReg1 == 0) return false;
01456     if (!UseImm) {
01457       SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
01458       if (SrcReg2 == 0) return false;
01459     }
01460   }
01461 
01462   const MCInstrDesc &II = TII.get(CmpOpc);
01463   SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
01464   if (!UseImm) {
01465     SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
01466     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01467                     .addReg(SrcReg1).addReg(SrcReg2));
01468   } else {
01469     MachineInstrBuilder MIB;
01470     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01471       .addReg(SrcReg1);
01472 
01473     // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
01474     if (isICmp)
01475       MIB.addImm(Imm);
01476     AddOptionalDefs(MIB);
01477   }
01478 
01479   // For floating point we need to move the result to a comparison register
01480   // that we can then use for branches.
01481   if (Ty->isFloatTy() || Ty->isDoubleTy())
01482     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01483                             TII.get(ARM::FMSTAT)));
01484   return true;
01485 }
01486 
01487 bool ARMFastISel::SelectCmp(const Instruction *I) {
01488   const CmpInst *CI = cast<CmpInst>(I);
01489 
01490   // Get the compare predicate.
01491   ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
01492 
01493   // We may not handle every CC for now.
01494   if (ARMPred == ARMCC::AL) return false;
01495 
01496   // Emit the compare.
01497   if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
01498     return false;
01499 
01500   // Now set a register based on the comparison. Explicitly set the predicates
01501   // here.
01502   unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
01503   const TargetRegisterClass *RC = isThumb2 ?
01504     (const TargetRegisterClass*)&ARM::rGPRRegClass :
01505     (const TargetRegisterClass*)&ARM::GPRRegClass;
01506   unsigned DestReg = createResultReg(RC);
01507   Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
01508   unsigned ZeroReg = fastMaterializeConstant(Zero);
01509   // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
01510   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc), DestReg)
01511           .addReg(ZeroReg).addImm(1)
01512           .addImm(ARMPred).addReg(ARM::CPSR);
01513 
01514   updateValueMap(I, DestReg);
01515   return true;
01516 }
01517 
01518 bool ARMFastISel::SelectFPExt(const Instruction *I) {
01519   // Make sure we have VFP and that we're extending float to double.
01520   if (!Subtarget->hasVFP2()) return false;
01521 
01522   Value *V = I->getOperand(0);
01523   if (!I->getType()->isDoubleTy() ||
01524       !V->getType()->isFloatTy()) return false;
01525 
01526   unsigned Op = getRegForValue(V);
01527   if (Op == 0) return false;
01528 
01529   unsigned Result = createResultReg(&ARM::DPRRegClass);
01530   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01531                           TII.get(ARM::VCVTDS), Result)
01532                   .addReg(Op));
01533   updateValueMap(I, Result);
01534   return true;
01535 }
01536 
01537 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
01538   // Make sure we have VFP and that we're truncating double to float.
01539   if (!Subtarget->hasVFP2()) return false;
01540 
01541   Value *V = I->getOperand(0);
01542   if (!(I->getType()->isFloatTy() &&
01543         V->getType()->isDoubleTy())) return false;
01544 
01545   unsigned Op = getRegForValue(V);
01546   if (Op == 0) return false;
01547 
01548   unsigned Result = createResultReg(&ARM::SPRRegClass);
01549   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01550                           TII.get(ARM::VCVTSD), Result)
01551                   .addReg(Op));
01552   updateValueMap(I, Result);
01553   return true;
01554 }
01555 
01556 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
01557   // Make sure we have VFP.
01558   if (!Subtarget->hasVFP2()) return false;
01559 
01560   MVT DstVT;
01561   Type *Ty = I->getType();
01562   if (!isTypeLegal(Ty, DstVT))
01563     return false;
01564 
01565   Value *Src = I->getOperand(0);
01566   EVT SrcEVT = TLI.getValueType(Src->getType(), true);
01567   if (!SrcEVT.isSimple())
01568     return false;
01569   MVT SrcVT = SrcEVT.getSimpleVT();
01570   if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
01571     return false;
01572 
01573   unsigned SrcReg = getRegForValue(Src);
01574   if (SrcReg == 0) return false;
01575 
01576   // Handle sign-extension.
01577   if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
01578     SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
01579                                        /*isZExt*/!isSigned);
01580     if (SrcReg == 0) return false;
01581   }
01582 
01583   // The conversion routine works on fp-reg to fp-reg and the operand above
01584   // was an integer, move it to the fp registers if possible.
01585   unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
01586   if (FP == 0) return false;
01587 
01588   unsigned Opc;
01589   if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
01590   else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
01591   else return false;
01592 
01593   unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
01594   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01595                           TII.get(Opc), ResultReg).addReg(FP));
01596   updateValueMap(I, ResultReg);
01597   return true;
01598 }
01599 
01600 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
01601   // Make sure we have VFP.
01602   if (!Subtarget->hasVFP2()) return false;
01603 
01604   MVT DstVT;
01605   Type *RetTy = I->getType();
01606   if (!isTypeLegal(RetTy, DstVT))
01607     return false;
01608 
01609   unsigned Op = getRegForValue(I->getOperand(0));
01610   if (Op == 0) return false;
01611 
01612   unsigned Opc;
01613   Type *OpTy = I->getOperand(0)->getType();
01614   if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
01615   else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
01616   else return false;
01617 
01618   // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
01619   unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
01620   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01621                           TII.get(Opc), ResultReg).addReg(Op));
01622 
01623   // This result needs to be in an integer register, but the conversion only
01624   // takes place in fp-regs.
01625   unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
01626   if (IntReg == 0) return false;
01627 
01628   updateValueMap(I, IntReg);
01629   return true;
01630 }
01631 
01632 bool ARMFastISel::SelectSelect(const Instruction *I) {
01633   MVT VT;
01634   if (!isTypeLegal(I->getType(), VT))
01635     return false;
01636 
01637   // Things need to be register sized for register moves.
01638   if (VT != MVT::i32) return false;
01639 
01640   unsigned CondReg = getRegForValue(I->getOperand(0));
01641   if (CondReg == 0) return false;
01642   unsigned Op1Reg = getRegForValue(I->getOperand(1));
01643   if (Op1Reg == 0) return false;
01644 
01645   // Check to see if we can use an immediate in the conditional move.
01646   int Imm = 0;
01647   bool UseImm = false;
01648   bool isNegativeImm = false;
01649   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
01650     assert (VT == MVT::i32 && "Expecting an i32.");
01651     Imm = (int)ConstInt->getValue().getZExtValue();
01652     if (Imm < 0) {
01653       isNegativeImm = true;
01654       Imm = ~Imm;
01655     }
01656     UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
01657       (ARM_AM::getSOImmVal(Imm) != -1);
01658   }
01659 
01660   unsigned Op2Reg = 0;
01661   if (!UseImm) {
01662     Op2Reg = getRegForValue(I->getOperand(2));
01663     if (Op2Reg == 0) return false;
01664   }
01665 
01666   unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
01667   CondReg = constrainOperandRegClass(TII.get(CmpOpc), CondReg, 0);
01668   AddOptionalDefs(
01669       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
01670           .addReg(CondReg)
01671           .addImm(0));
01672 
01673   unsigned MovCCOpc;
01674   const TargetRegisterClass *RC;
01675   if (!UseImm) {
01676     RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
01677     MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
01678   } else {
01679     RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
01680     if (!isNegativeImm)
01681       MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
01682     else
01683       MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
01684   }
01685   unsigned ResultReg = createResultReg(RC);
01686   if (!UseImm) {
01687     Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
01688     Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
01689     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
01690             ResultReg)
01691         .addReg(Op2Reg)
01692         .addReg(Op1Reg)
01693         .addImm(ARMCC::NE)
01694         .addReg(ARM::CPSR);
01695   } else {
01696     Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
01697     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovCCOpc),
01698             ResultReg)
01699         .addReg(Op1Reg)
01700         .addImm(Imm)
01701         .addImm(ARMCC::EQ)
01702         .addReg(ARM::CPSR);
01703   }
01704   updateValueMap(I, ResultReg);
01705   return true;
01706 }
01707 
01708 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
01709   MVT VT;
01710   Type *Ty = I->getType();
01711   if (!isTypeLegal(Ty, VT))
01712     return false;
01713 
01714   // If we have integer div support we should have selected this automagically.
01715   // In case we have a real miss go ahead and return false and we'll pick
01716   // it up later.
01717   if (Subtarget->hasDivide()) return false;
01718 
01719   // Otherwise emit a libcall.
01720   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01721   if (VT == MVT::i8)
01722     LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
01723   else if (VT == MVT::i16)
01724     LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
01725   else if (VT == MVT::i32)
01726     LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
01727   else if (VT == MVT::i64)
01728     LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
01729   else if (VT == MVT::i128)
01730     LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
01731   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
01732 
01733   return ARMEmitLibcall(I, LC);
01734 }
01735 
01736 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
01737   MVT VT;
01738   Type *Ty = I->getType();
01739   if (!isTypeLegal(Ty, VT))
01740     return false;
01741 
01742   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01743   if (VT == MVT::i8)
01744     LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
01745   else if (VT == MVT::i16)
01746     LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
01747   else if (VT == MVT::i32)
01748     LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
01749   else if (VT == MVT::i64)
01750     LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
01751   else if (VT == MVT::i128)
01752     LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
01753   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
01754 
01755   return ARMEmitLibcall(I, LC);
01756 }
01757 
01758 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
01759   EVT DestVT  = TLI.getValueType(I->getType(), true);
01760 
01761   // We can get here in the case when we have a binary operation on a non-legal
01762   // type and the target independent selector doesn't know how to handle it.
01763   if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
01764     return false;
01765 
01766   unsigned Opc;
01767   switch (ISDOpcode) {
01768     default: return false;
01769     case ISD::ADD:
01770       Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
01771       break;
01772     case ISD::OR:
01773       Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
01774       break;
01775     case ISD::SUB:
01776       Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
01777       break;
01778   }
01779 
01780   unsigned SrcReg1 = getRegForValue(I->getOperand(0));
01781   if (SrcReg1 == 0) return false;
01782 
01783   // TODO: Often the 2nd operand is an immediate, which can be encoded directly
01784   // in the instruction, rather then materializing the value in a register.
01785   unsigned SrcReg2 = getRegForValue(I->getOperand(1));
01786   if (SrcReg2 == 0) return false;
01787 
01788   unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
01789   SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
01790   SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
01791   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01792                           TII.get(Opc), ResultReg)
01793                   .addReg(SrcReg1).addReg(SrcReg2));
01794   updateValueMap(I, ResultReg);
01795   return true;
01796 }
01797 
01798 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
01799   EVT FPVT = TLI.getValueType(I->getType(), true);
01800   if (!FPVT.isSimple()) return false;
01801   MVT VT = FPVT.getSimpleVT();
01802 
01803   // We can get here in the case when we want to use NEON for our fp
01804   // operations, but can't figure out how to. Just use the vfp instructions
01805   // if we have them.
01806   // FIXME: It'd be nice to use NEON instructions.
01807   Type *Ty = I->getType();
01808   bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
01809   if (isFloat && !Subtarget->hasVFP2())
01810     return false;
01811 
01812   unsigned Opc;
01813   bool is64bit = VT == MVT::f64 || VT == MVT::i64;
01814   switch (ISDOpcode) {
01815     default: return false;
01816     case ISD::FADD:
01817       Opc = is64bit ? ARM::VADDD : ARM::VADDS;
01818       break;
01819     case ISD::FSUB:
01820       Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
01821       break;
01822     case ISD::FMUL:
01823       Opc = is64bit ? ARM::VMULD : ARM::VMULS;
01824       break;
01825   }
01826   unsigned Op1 = getRegForValue(I->getOperand(0));
01827   if (Op1 == 0) return false;
01828 
01829   unsigned Op2 = getRegForValue(I->getOperand(1));
01830   if (Op2 == 0) return false;
01831 
01832   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
01833   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01834                           TII.get(Opc), ResultReg)
01835                   .addReg(Op1).addReg(Op2));
01836   updateValueMap(I, ResultReg);
01837   return true;
01838 }
01839 
01840 // Call Handling Code
01841 
01842 // This is largely taken directly from CCAssignFnForNode
01843 // TODO: We may not support all of this.
01844 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
01845                                            bool Return,
01846                                            bool isVarArg) {
01847   switch (CC) {
01848   default:
01849     llvm_unreachable("Unsupported calling convention");
01850   case CallingConv::Fast:
01851     if (Subtarget->hasVFP2() && !isVarArg) {
01852       if (!Subtarget->isAAPCS_ABI())
01853         return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
01854       // For AAPCS ABI targets, just use VFP variant of the calling convention.
01855       return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
01856     }
01857     // Fallthrough
01858   case CallingConv::C:
01859     // Use target triple & subtarget features to do actual dispatch.
01860     if (Subtarget->isAAPCS_ABI()) {
01861       if (Subtarget->hasVFP2() &&
01862           TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
01863         return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
01864       else
01865         return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
01866     } else
01867         return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
01868   case CallingConv::ARM_AAPCS_VFP:
01869     if (!isVarArg)
01870       return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
01871     // Fall through to soft float variant, variadic functions don't
01872     // use hard floating point ABI.
01873   case CallingConv::ARM_AAPCS:
01874     return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
01875   case CallingConv::ARM_APCS:
01876     return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
01877   case CallingConv::GHC:
01878     if (Return)
01879       llvm_unreachable("Can't return in GHC call convention");
01880     else
01881       return CC_ARM_APCS_GHC;
01882   }
01883 }
01884 
01885 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
01886                                   SmallVectorImpl<unsigned> &ArgRegs,
01887                                   SmallVectorImpl<MVT> &ArgVTs,
01888                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
01889                                   SmallVectorImpl<unsigned> &RegArgs,
01890                                   CallingConv::ID CC,
01891                                   unsigned &NumBytes,
01892                                   bool isVarArg) {
01893   SmallVector<CCValAssign, 16> ArgLocs;
01894   CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
01895   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
01896                              CCAssignFnForCall(CC, false, isVarArg));
01897 
01898   // Check that we can handle all of the arguments. If we can't, then bail out
01899   // now before we add code to the MBB.
01900   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01901     CCValAssign &VA = ArgLocs[i];
01902     MVT ArgVT = ArgVTs[VA.getValNo()];
01903 
01904     // We don't handle NEON/vector parameters yet.
01905     if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
01906       return false;
01907 
01908     // Now copy/store arg to correct locations.
01909     if (VA.isRegLoc() && !VA.needsCustom()) {
01910       continue;
01911     } else if (VA.needsCustom()) {
01912       // TODO: We need custom lowering for vector (v2f64) args.
01913       if (VA.getLocVT() != MVT::f64 ||
01914           // TODO: Only handle register args for now.
01915           !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
01916         return false;
01917     } else {
01918       switch (ArgVT.SimpleTy) {
01919       default:
01920         return false;
01921       case MVT::i1:
01922       case MVT::i8:
01923       case MVT::i16:
01924       case MVT::i32:
01925         break;
01926       case MVT::f32:
01927         if (!Subtarget->hasVFP2())
01928           return false;
01929         break;
01930       case MVT::f64:
01931         if (!Subtarget->hasVFP2())
01932           return false;
01933         break;
01934       }
01935     }
01936   }
01937 
01938   // At the point, we are able to handle the call's arguments in fast isel.
01939 
01940   // Get a count of how many bytes are to be pushed on the stack.
01941   NumBytes = CCInfo.getNextStackOffset();
01942 
01943   // Issue CALLSEQ_START
01944   unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
01945   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01946                           TII.get(AdjStackDown))
01947                   .addImm(NumBytes));
01948 
01949   // Process the args.
01950   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01951     CCValAssign &VA = ArgLocs[i];
01952     const Value *ArgVal = Args[VA.getValNo()];
01953     unsigned Arg = ArgRegs[VA.getValNo()];
01954     MVT ArgVT = ArgVTs[VA.getValNo()];
01955 
01956     assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
01957            "We don't handle NEON/vector parameters yet.");
01958 
01959     // Handle arg promotion, etc.
01960     switch (VA.getLocInfo()) {
01961       case CCValAssign::Full: break;
01962       case CCValAssign::SExt: {
01963         MVT DestVT = VA.getLocVT();
01964         Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
01965         assert (Arg != 0 && "Failed to emit a sext");
01966         ArgVT = DestVT;
01967         break;
01968       }
01969       case CCValAssign::AExt:
01970         // Intentional fall-through.  Handle AExt and ZExt.
01971       case CCValAssign::ZExt: {
01972         MVT DestVT = VA.getLocVT();
01973         Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
01974         assert (Arg != 0 && "Failed to emit a zext");
01975         ArgVT = DestVT;
01976         break;
01977       }
01978       case CCValAssign::BCvt: {
01979         unsigned BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
01980                                  /*TODO: Kill=*/false);
01981         assert(BC != 0 && "Failed to emit a bitcast!");
01982         Arg = BC;
01983         ArgVT = VA.getLocVT();
01984         break;
01985       }
01986       default: llvm_unreachable("Unknown arg promotion!");
01987     }
01988 
01989     // Now copy/store arg to correct locations.
01990     if (VA.isRegLoc() && !VA.needsCustom()) {
01991       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01992               TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
01993       RegArgs.push_back(VA.getLocReg());
01994     } else if (VA.needsCustom()) {
01995       // TODO: We need custom lowering for vector (v2f64) args.
01996       assert(VA.getLocVT() == MVT::f64 &&
01997              "Custom lowering for v2f64 args not available");
01998 
01999       CCValAssign &NextVA = ArgLocs[++i];
02000 
02001       assert(VA.isRegLoc() && NextVA.isRegLoc() &&
02002              "We only handle register args!");
02003 
02004       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02005                               TII.get(ARM::VMOVRRD), VA.getLocReg())
02006                       .addReg(NextVA.getLocReg(), RegState::Define)
02007                       .addReg(Arg));
02008       RegArgs.push_back(VA.getLocReg());
02009       RegArgs.push_back(NextVA.getLocReg());
02010     } else {
02011       assert(VA.isMemLoc());
02012       // Need to store on the stack.
02013 
02014       // Don't emit stores for undef values.
02015       if (isa<UndefValue>(ArgVal))
02016         continue;
02017 
02018       Address Addr;
02019       Addr.BaseType = Address::RegBase;
02020       Addr.Base.Reg = ARM::SP;
02021       Addr.Offset = VA.getLocMemOffset();
02022 
02023       bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
02024       assert(EmitRet && "Could not emit a store for argument!");
02025     }
02026   }
02027 
02028   return true;
02029 }
02030 
02031 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
02032                              const Instruction *I, CallingConv::ID CC,
02033                              unsigned &NumBytes, bool isVarArg) {
02034   // Issue CALLSEQ_END
02035   unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
02036   AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02037                           TII.get(AdjStackUp))
02038                   .addImm(NumBytes).addImm(0));
02039 
02040   // Now the return value.
02041   if (RetVT != MVT::isVoid) {
02042     SmallVector<CCValAssign, 16> RVLocs;
02043     CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
02044     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
02045 
02046     // Copy all of the result registers out of their specified physreg.
02047     if (RVLocs.size() == 2 && RetVT == MVT::f64) {
02048       // For this move we copy into two registers and then move into the
02049       // double fp reg we want.
02050       MVT DestVT = RVLocs[0].getValVT();
02051       const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
02052       unsigned ResultReg = createResultReg(DstRC);
02053       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02054                               TII.get(ARM::VMOVDRR), ResultReg)
02055                       .addReg(RVLocs[0].getLocReg())
02056                       .addReg(RVLocs[1].getLocReg()));
02057 
02058       UsedRegs.push_back(RVLocs[0].getLocReg());
02059       UsedRegs.push_back(RVLocs[1].getLocReg());
02060 
02061       // Finally update the result.
02062       updateValueMap(I, ResultReg);
02063     } else {
02064       assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
02065       MVT CopyVT = RVLocs[0].getValVT();
02066 
02067       // Special handling for extended integers.
02068       if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
02069         CopyVT = MVT::i32;
02070 
02071       const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
02072 
02073       unsigned ResultReg = createResultReg(DstRC);
02074       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02075               TII.get(TargetOpcode::COPY),
02076               ResultReg).addReg(RVLocs[0].getLocReg());
02077       UsedRegs.push_back(RVLocs[0].getLocReg());
02078 
02079       // Finally update the result.
02080       updateValueMap(I, ResultReg);
02081     }
02082   }
02083 
02084   return true;
02085 }
02086 
02087 bool ARMFastISel::SelectRet(const Instruction *I) {
02088   const ReturnInst *Ret = cast<ReturnInst>(I);
02089   const Function &F = *I->getParent()->getParent();
02090 
02091   if (!FuncInfo.CanLowerReturn)
02092     return false;
02093 
02094   // Build a list of return value registers.
02095   SmallVector<unsigned, 4> RetRegs;
02096 
02097   CallingConv::ID CC = F.getCallingConv();
02098   if (Ret->getNumOperands() > 0) {
02099     SmallVector<ISD::OutputArg, 4> Outs;
02100     GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
02101 
02102     // Analyze operands of the call, assigning locations to each operand.
02103     SmallVector<CCValAssign, 16> ValLocs;
02104     CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
02105     CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
02106                                                  F.isVarArg()));
02107 
02108     const Value *RV = Ret->getOperand(0);
02109     unsigned Reg = getRegForValue(RV);
02110     if (Reg == 0)
02111       return false;
02112 
02113     // Only handle a single return value for now.
02114     if (ValLocs.size() != 1)
02115       return false;
02116 
02117     CCValAssign &VA = ValLocs[0];
02118 
02119     // Don't bother handling odd stuff for now.
02120     if (VA.getLocInfo() != CCValAssign::Full)
02121       return false;
02122     // Only handle register returns for now.
02123     if (!VA.isRegLoc())
02124       return false;
02125 
02126     unsigned SrcReg = Reg + VA.getValNo();
02127     EVT RVEVT = TLI.getValueType(RV->getType());
02128     if (!RVEVT.isSimple()) return false;
02129     MVT RVVT = RVEVT.getSimpleVT();
02130     MVT DestVT = VA.getValVT();
02131     // Special handling for extended integers.
02132     if (RVVT != DestVT) {
02133       if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
02134         return false;
02135 
02136       assert(DestVT == MVT::i32 && "ARM should always ext to i32");
02137 
02138       // Perform extension if flagged as either zext or sext.  Otherwise, do
02139       // nothing.
02140       if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
02141         SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
02142         if (SrcReg == 0) return false;
02143       }
02144     }
02145 
02146     // Make the copy.
02147     unsigned DstReg = VA.getLocReg();
02148     const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
02149     // Avoid a cross-class copy. This is very unlikely.
02150     if (!SrcRC->contains(DstReg))
02151       return false;
02152     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02153             TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
02154 
02155     // Add register to return instruction.
02156     RetRegs.push_back(VA.getLocReg());
02157   }
02158 
02159   unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
02160   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02161                                     TII.get(RetOpc));
02162   AddOptionalDefs(MIB);
02163   for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
02164     MIB.addReg(RetRegs[i], RegState::Implicit);
02165   return true;
02166 }
02167 
02168 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
02169   if (UseReg)
02170     return isThumb2 ? ARM::tBLXr : ARM::BLX;
02171   else
02172     return isThumb2 ? ARM::tBL : ARM::BL;
02173 }
02174 
02175 unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
02176   // Manually compute the global's type to avoid building it when unnecessary.
02177   Type *GVTy = Type::getInt32PtrTy(*Context, /*AS=*/0);
02178   EVT LCREVT = TLI.getValueType(GVTy);
02179   if (!LCREVT.isSimple()) return 0;
02180 
02181   GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
02182                                        GlobalValue::ExternalLinkage, nullptr,
02183                                        Name);
02184   assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
02185   return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
02186 }
02187 
02188 // A quick function that will emit a call for a named libcall in F with the
02189 // vector of passed arguments for the Instruction in I. We can assume that we
02190 // can emit a call for any libcall we can produce. This is an abridged version
02191 // of the full call infrastructure since we won't need to worry about things
02192 // like computed function pointers or strange arguments at call sites.
02193 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
02194 // with X86.
02195 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
02196   CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
02197 
02198   // Handle *simple* calls for now.
02199   Type *RetTy = I->getType();
02200   MVT RetVT;
02201   if (RetTy->isVoidTy())
02202     RetVT = MVT::isVoid;
02203   else if (!isTypeLegal(RetTy, RetVT))
02204     return false;
02205 
02206   // Can't handle non-double multi-reg retvals.
02207   if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
02208     SmallVector<CCValAssign, 16> RVLocs;
02209     CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
02210     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
02211     if (RVLocs.size() >= 2 && RetVT != MVT::f64)
02212       return false;
02213   }
02214 
02215   // Set up the argument vectors.
02216   SmallVector<Value*, 8> Args;
02217   SmallVector<unsigned, 8> ArgRegs;
02218   SmallVector<MVT, 8> ArgVTs;
02219   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
02220   Args.reserve(I->getNumOperands());
02221   ArgRegs.reserve(I->getNumOperands());
02222   ArgVTs.reserve(I->getNumOperands());
02223   ArgFlags.reserve(I->getNumOperands());
02224   for (unsigned i = 0; i < I->getNumOperands(); ++i) {
02225     Value *Op = I->getOperand(i);
02226     unsigned Arg = getRegForValue(Op);
02227     if (Arg == 0) return false;
02228 
02229     Type *ArgTy = Op->getType();
02230     MVT ArgVT;
02231     if (!isTypeLegal(ArgTy, ArgVT)) return false;
02232 
02233     ISD::ArgFlagsTy Flags;
02234     unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
02235     Flags.setOrigAlign(OriginalAlignment);
02236 
02237     Args.push_back(Op);
02238     ArgRegs.push_back(Arg);
02239     ArgVTs.push_back(ArgVT);
02240     ArgFlags.push_back(Flags);
02241   }
02242 
02243   // Handle the arguments now that we've gotten them.
02244   SmallVector<unsigned, 4> RegArgs;
02245   unsigned NumBytes;
02246   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
02247                        RegArgs, CC, NumBytes, false))
02248     return false;
02249 
02250   unsigned CalleeReg = 0;
02251   if (EnableARMLongCalls) {
02252     CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
02253     if (CalleeReg == 0) return false;
02254   }
02255 
02256   // Issue the call.
02257   unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
02258   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
02259                                     DbgLoc, TII.get(CallOpc));
02260   // BL / BLX don't take a predicate, but tBL / tBLX do.
02261   if (isThumb2)
02262     AddDefaultPred(MIB);
02263   if (EnableARMLongCalls)
02264     MIB.addReg(CalleeReg);
02265   else
02266     MIB.addExternalSymbol(TLI.getLibcallName(Call));
02267 
02268   // Add implicit physical register uses to the call.
02269   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
02270     MIB.addReg(RegArgs[i], RegState::Implicit);
02271 
02272   // Add a register mask with the call-preserved registers.
02273   // Proper defs for return values will be added by setPhysRegsDeadExcept().
02274   MIB.addRegMask(TRI.getCallPreservedMask(CC));
02275 
02276   // Finish off the call including any return values.
02277   SmallVector<unsigned, 4> UsedRegs;
02278   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
02279 
02280   // Set all unused physreg defs as dead.
02281   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
02282 
02283   return true;
02284 }
02285 
02286 bool ARMFastISel::SelectCall(const Instruction *I,
02287                              const char *IntrMemName = nullptr) {
02288   const CallInst *CI = cast<CallInst>(I);
02289   const Value *Callee = CI->getCalledValue();
02290 
02291   // Can't handle inline asm.
02292   if (isa<InlineAsm>(Callee)) return false;
02293 
02294   // Allow SelectionDAG isel to handle tail calls.
02295   if (CI->isTailCall()) return false;
02296 
02297   // Check the calling convention.
02298   ImmutableCallSite CS(CI);
02299   CallingConv::ID CC = CS.getCallingConv();
02300 
02301   // TODO: Avoid some calling conventions?
02302 
02303   PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
02304   FunctionType *FTy = cast<FunctionType>(PT->getElementType());
02305   bool isVarArg = FTy->isVarArg();
02306 
02307   // Handle *simple* calls for now.
02308   Type *RetTy = I->getType();
02309   MVT RetVT;
02310   if (RetTy->isVoidTy())
02311     RetVT = MVT::isVoid;
02312   else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
02313            RetVT != MVT::i8  && RetVT != MVT::i1)
02314     return false;
02315 
02316   // Can't handle non-double multi-reg retvals.
02317   if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
02318       RetVT != MVT::i16 && RetVT != MVT::i32) {
02319     SmallVector<CCValAssign, 16> RVLocs;
02320     CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
02321     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
02322     if (RVLocs.size() >= 2 && RetVT != MVT::f64)
02323       return false;
02324   }
02325 
02326   // Set up the argument vectors.
02327   SmallVector<Value*, 8> Args;
02328   SmallVector<unsigned, 8> ArgRegs;
02329   SmallVector<MVT, 8> ArgVTs;
02330   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
02331   unsigned arg_size = CS.arg_size();
02332   Args.reserve(arg_size);
02333   ArgRegs.reserve(arg_size);
02334   ArgVTs.reserve(arg_size);
02335   ArgFlags.reserve(arg_size);
02336   for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
02337        i != e; ++i) {
02338     // If we're lowering a memory intrinsic instead of a regular call, skip the
02339     // last two arguments, which shouldn't be passed to the underlying function.
02340     if (IntrMemName && e-i <= 2)
02341       break;
02342 
02343     ISD::ArgFlagsTy Flags;
02344     unsigned AttrInd = i - CS.arg_begin() + 1;
02345     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
02346       Flags.setSExt();
02347     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
02348       Flags.setZExt();
02349 
02350     // FIXME: Only handle *easy* calls for now.
02351     if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
02352         CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
02353         CS.paramHasAttr(AttrInd, Attribute::Nest) ||
02354         CS.paramHasAttr(AttrInd, Attribute::ByVal))
02355       return false;
02356 
02357     Type *ArgTy = (*i)->getType();
02358     MVT ArgVT;
02359     if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
02360         ArgVT != MVT::i1)
02361       return false;
02362 
02363     unsigned Arg = getRegForValue(*i);
02364     if (Arg == 0)
02365       return false;
02366 
02367     unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
02368     Flags.setOrigAlign(OriginalAlignment);
02369 
02370     Args.push_back(*i);
02371     ArgRegs.push_back(Arg);
02372     ArgVTs.push_back(ArgVT);
02373     ArgFlags.push_back(Flags);
02374   }
02375 
02376   // Handle the arguments now that we've gotten them.
02377   SmallVector<unsigned, 4> RegArgs;
02378   unsigned NumBytes;
02379   if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
02380                        RegArgs, CC, NumBytes, isVarArg))
02381     return false;
02382 
02383   bool UseReg = false;
02384   const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
02385   if (!GV || EnableARMLongCalls) UseReg = true;
02386 
02387   unsigned CalleeReg = 0;
02388   if (UseReg) {
02389     if (IntrMemName)
02390       CalleeReg = getLibcallReg(IntrMemName);
02391     else
02392       CalleeReg = getRegForValue(Callee);
02393 
02394     if (CalleeReg == 0) return false;
02395   }
02396 
02397   // Issue the call.
02398   unsigned CallOpc = ARMSelectCallOp(UseReg);
02399   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
02400                                     DbgLoc, TII.get(CallOpc));
02401 
02402   unsigned char OpFlags = 0;
02403 
02404   // Add MO_PLT for global address or external symbol in the PIC relocation
02405   // model.
02406   if (Subtarget->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_)
02407     OpFlags = ARMII::MO_PLT;
02408 
02409   // ARM calls don't take a predicate, but tBL / tBLX do.
02410   if(isThumb2)
02411     AddDefaultPred(MIB);
02412   if (UseReg)
02413     MIB.addReg(CalleeReg);
02414   else if (!IntrMemName)
02415     MIB.addGlobalAddress(GV, 0, OpFlags);
02416   else
02417     MIB.addExternalSymbol(IntrMemName, OpFlags);
02418 
02419   // Add implicit physical register uses to the call.
02420   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
02421     MIB.addReg(RegArgs[i], RegState::Implicit);
02422 
02423   // Add a register mask with the call-preserved registers.
02424   // Proper defs for return values will be added by setPhysRegsDeadExcept().
02425   MIB.addRegMask(TRI.getCallPreservedMask(CC));
02426 
02427   // Finish off the call including any return values.
02428   SmallVector<unsigned, 4> UsedRegs;
02429   if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
02430     return false;
02431 
02432   // Set all unused physreg defs as dead.
02433   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
02434 
02435   return true;
02436 }
02437 
02438 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
02439   return Len <= 16;
02440 }
02441 
02442 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
02443                                         uint64_t Len, unsigned Alignment) {
02444   // Make sure we don't bloat code by inlining very large memcpy's.
02445   if (!ARMIsMemCpySmall(Len))
02446     return false;
02447 
02448   while (Len) {
02449     MVT VT;
02450     if (!Alignment || Alignment >= 4) {
02451       if (Len >= 4)
02452         VT = MVT::i32;
02453       else if (Len >= 2)
02454         VT = MVT::i16;
02455       else {
02456         assert (Len == 1 && "Expected a length of 1!");
02457         VT = MVT::i8;
02458       }
02459     } else {
02460       // Bound based on alignment.
02461       if (Len >= 2 && Alignment == 2)
02462         VT = MVT::i16;
02463       else {
02464         VT = MVT::i8;
02465       }
02466     }
02467 
02468     bool RV;
02469     unsigned ResultReg;
02470     RV = ARMEmitLoad(VT, ResultReg, Src);
02471     assert (RV == true && "Should be able to handle this load.");
02472     RV = ARMEmitStore(VT, ResultReg, Dest);
02473     assert (RV == true && "Should be able to handle this store.");
02474     (void)RV;
02475 
02476     unsigned Size = VT.getSizeInBits()/8;
02477     Len -= Size;
02478     Dest.Offset += Size;
02479     Src.Offset += Size;
02480   }
02481 
02482   return true;
02483 }
02484 
02485 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
02486   // FIXME: Handle more intrinsics.
02487   switch (I.getIntrinsicID()) {
02488   default: return false;
02489   case Intrinsic::frameaddress: {
02490     MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
02491     MFI->setFrameAddressIsTaken(true);
02492 
02493     unsigned LdrOpc;
02494     const TargetRegisterClass *RC;
02495     if (isThumb2) {
02496       LdrOpc =  ARM::t2LDRi12;
02497       RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
02498     } else {
02499       LdrOpc =  ARM::LDRi12;
02500       RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
02501     }
02502 
02503     const ARMBaseRegisterInfo *RegInfo =
02504         static_cast<const ARMBaseRegisterInfo *>(
02505             TM.getSubtargetImpl()->getRegisterInfo());
02506     unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
02507     unsigned SrcReg = FramePtr;
02508 
02509     // Recursively load frame address
02510     // ldr r0 [fp]
02511     // ldr r0 [r0]
02512     // ldr r0 [r0]
02513     // ...
02514     unsigned DestReg;
02515     unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
02516     while (Depth--) {
02517       DestReg = createResultReg(RC);
02518       AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02519                               TII.get(LdrOpc), DestReg)
02520                       .addReg(SrcReg).addImm(0));
02521       SrcReg = DestReg;
02522     }
02523     updateValueMap(&I, SrcReg);
02524     return true;
02525   }
02526   case Intrinsic::memcpy:
02527   case Intrinsic::memmove: {
02528     const MemTransferInst &MTI = cast<MemTransferInst>(I);
02529     // Don't handle volatile.
02530     if (MTI.isVolatile())
02531       return false;
02532 
02533     // Disable inlining for memmove before calls to ComputeAddress.  Otherwise,
02534     // we would emit dead code because we don't currently handle memmoves.
02535     bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
02536     if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
02537       // Small memcpy's are common enough that we want to do them without a call
02538       // if possible.
02539       uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
02540       if (ARMIsMemCpySmall(Len)) {
02541         Address Dest, Src;
02542         if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
02543             !ARMComputeAddress(MTI.getRawSource(), Src))
02544           return false;
02545         unsigned Alignment = MTI.getAlignment();
02546         if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
02547           return true;
02548       }
02549     }
02550 
02551     if (!MTI.getLength()->getType()->isIntegerTy(32))
02552       return false;
02553 
02554     if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
02555       return false;
02556 
02557     const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
02558     return SelectCall(&I, IntrMemName);
02559   }
02560   case Intrinsic::memset: {
02561     const MemSetInst &MSI = cast<MemSetInst>(I);
02562     // Don't handle volatile.
02563     if (MSI.isVolatile())
02564       return false;
02565 
02566     if (!MSI.getLength()->getType()->isIntegerTy(32))
02567       return false;
02568 
02569     if (MSI.getDestAddressSpace() > 255)
02570       return false;
02571 
02572     return SelectCall(&I, "memset");
02573   }
02574   case Intrinsic::trap: {
02575     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(
02576       Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP));
02577     return true;
02578   }
02579   }
02580 }
02581 
02582 bool ARMFastISel::SelectTrunc(const Instruction *I) {
02583   // The high bits for a type smaller than the register size are assumed to be
02584   // undefined.
02585   Value *Op = I->getOperand(0);
02586 
02587   EVT SrcVT, DestVT;
02588   SrcVT = TLI.getValueType(Op->getType(), true);
02589   DestVT = TLI.getValueType(I->getType(), true);
02590 
02591   if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
02592     return false;
02593   if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
02594     return false;
02595 
02596   unsigned SrcReg = getRegForValue(Op);
02597   if (!SrcReg) return false;
02598 
02599   // Because the high bits are undefined, a truncate doesn't generate
02600   // any code.
02601   updateValueMap(I, SrcReg);
02602   return true;
02603 }
02604 
02605 unsigned ARMFastISel::ARMEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
02606                                     bool isZExt) {
02607   if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
02608     return 0;
02609   if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
02610     return 0;
02611 
02612   // Table of which combinations can be emitted as a single instruction,
02613   // and which will require two.
02614   static const uint8_t isSingleInstrTbl[3][2][2][2] = {
02615     //            ARM                     Thumb
02616     //           !hasV6Ops  hasV6Ops     !hasV6Ops  hasV6Ops
02617     //    ext:     s  z      s  z          s  z      s  z
02618     /*  1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
02619     /*  8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
02620     /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
02621   };
02622 
02623   // Target registers for:
02624   //  - For ARM can never be PC.
02625   //  - For 16-bit Thumb are restricted to lower 8 registers.
02626   //  - For 32-bit Thumb are restricted to non-SP and non-PC.
02627   static const TargetRegisterClass *RCTbl[2][2] = {
02628     // Instructions: Two                     Single
02629     /* ARM      */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
02630     /* Thumb    */ { &ARM::tGPRRegClass,    &ARM::rGPRRegClass    }
02631   };
02632 
02633   // Table governing the instruction(s) to be emitted.
02634   static const struct InstructionTable {
02635     uint32_t Opc   : 16;
02636     uint32_t hasS  :  1; // Some instructions have an S bit, always set it to 0.
02637     uint32_t Shift :  7; // For shift operand addressing mode, used by MOVsi.
02638     uint32_t Imm   :  8; // All instructions have either a shift or a mask.
02639   } IT[2][2][3][2] = {
02640     { // Two instructions (first is left shift, second is in this table).
02641       { // ARM                Opc           S  Shift             Imm
02642         /*  1 bit sext */ { { ARM::MOVsi  , 1, ARM_AM::asr     ,  31 },
02643         /*  1 bit zext */   { ARM::MOVsi  , 1, ARM_AM::lsr     ,  31 } },
02644         /*  8 bit sext */ { { ARM::MOVsi  , 1, ARM_AM::asr     ,  24 },
02645         /*  8 bit zext */   { ARM::MOVsi  , 1, ARM_AM::lsr     ,  24 } },
02646         /* 16 bit sext */ { { ARM::MOVsi  , 1, ARM_AM::asr     ,  16 },
02647         /* 16 bit zext */   { ARM::MOVsi  , 1, ARM_AM::lsr     ,  16 } }
02648       },
02649       { // Thumb              Opc           S  Shift             Imm
02650         /*  1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift,  31 },
02651         /*  1 bit zext */   { ARM::tLSRri , 0, ARM_AM::no_shift,  31 } },
02652         /*  8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift,  24 },
02653         /*  8 bit zext */   { ARM::tLSRri , 0, ARM_AM::no_shift,  24 } },
02654         /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift,  16 },
02655         /* 16 bit zext */   { ARM::tLSRri , 0, ARM_AM::no_shift,  16 } }
02656       }
02657     },
02658     { // Single instruction.
02659       { // ARM                Opc           S  Shift             Imm
02660         /*  1 bit sext */ { { ARM::KILL   , 0, ARM_AM::no_shift,   0 },
02661         /*  1 bit zext */   { ARM::ANDri  , 1, ARM_AM::no_shift,   1 } },
02662         /*  8 bit sext */ { { ARM::SXTB   , 0, ARM_AM::no_shift,   0 },
02663         /*  8 bit zext */   { ARM::ANDri  , 1, ARM_AM::no_shift, 255 } },
02664         /* 16 bit sext */ { { ARM::SXTH   , 0, ARM_AM::no_shift,   0 },
02665         /* 16 bit zext */   { ARM::UXTH   , 0, ARM_AM::no_shift,   0 } }
02666       },
02667       { // Thumb              Opc           S  Shift             Imm
02668         /*  1 bit sext */ { { ARM::KILL   , 0, ARM_AM::no_shift,   0 },
02669         /*  1 bit zext */   { ARM::t2ANDri, 1, ARM_AM::no_shift,   1 } },
02670         /*  8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift,   0 },
02671         /*  8 bit zext */   { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
02672         /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift,   0 },
02673         /* 16 bit zext */   { ARM::t2UXTH , 0, ARM_AM::no_shift,   0 } }
02674       }
02675     }
02676   };
02677 
02678   unsigned SrcBits = SrcVT.getSizeInBits();
02679   unsigned DestBits = DestVT.getSizeInBits();
02680   (void) DestBits;
02681   assert((SrcBits < DestBits) && "can only extend to larger types");
02682   assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
02683          "other sizes unimplemented");
02684   assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
02685          "other sizes unimplemented");
02686 
02687   bool hasV6Ops = Subtarget->hasV6Ops();
02688   unsigned Bitness = SrcBits / 8;  // {1,8,16}=>{0,1,2}
02689   assert((Bitness < 3) && "sanity-check table bounds");
02690 
02691   bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
02692   const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
02693   const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
02694   unsigned Opc = ITP->Opc;
02695   assert(ARM::KILL != Opc && "Invalid table entry");
02696   unsigned hasS = ITP->hasS;
02697   ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
02698   assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
02699          "only MOVsi has shift operand addressing mode");
02700   unsigned Imm = ITP->Imm;
02701 
02702   // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
02703   bool setsCPSR = &ARM::tGPRRegClass == RC;
02704   unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
02705   unsigned ResultReg;
02706   // MOVsi encodes shift and immediate in shift operand addressing mode.
02707   // The following condition has the same value when emitting two
02708   // instruction sequences: both are shifts.
02709   bool ImmIsSO = (Shift != ARM_AM::no_shift);
02710 
02711   // Either one or two instructions are emitted.
02712   // They're always of the form:
02713   //   dst = in OP imm
02714   // CPSR is set only by 16-bit Thumb instructions.
02715   // Predicate, if any, is AL.
02716   // S bit, if available, is always 0.
02717   // When two are emitted the first's result will feed as the second's input,
02718   // that value is then dead.
02719   unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
02720   for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
02721     ResultReg = createResultReg(RC);
02722     bool isLsl = (0 == Instr) && !isSingleInstr;
02723     unsigned Opcode = isLsl ? LSLOpc : Opc;
02724     ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
02725     unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
02726     bool isKill = 1 == Instr;
02727     MachineInstrBuilder MIB = BuildMI(
02728         *FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opcode), ResultReg);
02729     if (setsCPSR)
02730       MIB.addReg(ARM::CPSR, RegState::Define);
02731     SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
02732     AddDefaultPred(MIB.addReg(SrcReg, isKill * RegState::Kill).addImm(ImmEnc));
02733     if (hasS)
02734       AddDefaultCC(MIB);
02735     // Second instruction consumes the first's result.
02736     SrcReg = ResultReg;
02737   }
02738 
02739   return ResultReg;
02740 }
02741 
02742 bool ARMFastISel::SelectIntExt(const Instruction *I) {
02743   // On ARM, in general, integer casts don't involve legal types; this code
02744   // handles promotable integers.
02745   Type *DestTy = I->getType();
02746   Value *Src = I->getOperand(0);
02747   Type *SrcTy = Src->getType();
02748 
02749   bool isZExt = isa<ZExtInst>(I);
02750   unsigned SrcReg = getRegForValue(Src);
02751   if (!SrcReg) return false;
02752 
02753   EVT SrcEVT, DestEVT;
02754   SrcEVT = TLI.getValueType(SrcTy, true);
02755   DestEVT = TLI.getValueType(DestTy, true);
02756   if (!SrcEVT.isSimple()) return false;
02757   if (!DestEVT.isSimple()) return false;
02758 
02759   MVT SrcVT = SrcEVT.getSimpleVT();
02760   MVT DestVT = DestEVT.getSimpleVT();
02761   unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
02762   if (ResultReg == 0) return false;
02763   updateValueMap(I, ResultReg);
02764   return true;
02765 }
02766 
02767 bool ARMFastISel::SelectShift(const Instruction *I,
02768                               ARM_AM::ShiftOpc ShiftTy) {
02769   // We handle thumb2 mode by target independent selector
02770   // or SelectionDAG ISel.
02771   if (isThumb2)
02772     return false;
02773 
02774   // Only handle i32 now.
02775   EVT DestVT = TLI.getValueType(I->getType(), true);
02776   if (DestVT != MVT::i32)
02777     return false;
02778 
02779   unsigned Opc = ARM::MOVsr;
02780   unsigned ShiftImm;
02781   Value *Src2Value = I->getOperand(1);
02782   if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
02783     ShiftImm = CI->getZExtValue();
02784 
02785     // Fall back to selection DAG isel if the shift amount
02786     // is zero or greater than the width of the value type.
02787     if (ShiftImm == 0 || ShiftImm >=32)
02788       return false;
02789 
02790     Opc = ARM::MOVsi;
02791   }
02792 
02793   Value *Src1Value = I->getOperand(0);
02794   unsigned Reg1 = getRegForValue(Src1Value);
02795   if (Reg1 == 0) return false;
02796 
02797   unsigned Reg2 = 0;
02798   if (Opc == ARM::MOVsr) {
02799     Reg2 = getRegForValue(Src2Value);
02800     if (Reg2 == 0) return false;
02801   }
02802 
02803   unsigned ResultReg = createResultReg(&ARM::GPRnopcRegClass);
02804   if(ResultReg == 0) return false;
02805 
02806   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02807                                     TII.get(Opc), ResultReg)
02808                             .addReg(Reg1);
02809 
02810   if (Opc == ARM::MOVsi)
02811     MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
02812   else if (Opc == ARM::MOVsr) {
02813     MIB.addReg(Reg2);
02814     MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
02815   }
02816 
02817   AddOptionalDefs(MIB);
02818   updateValueMap(I, ResultReg);
02819   return true;
02820 }
02821 
02822 // TODO: SoftFP support.
02823 bool ARMFastISel::fastSelectInstruction(const Instruction *I) {
02824 
02825   switch (I->getOpcode()) {
02826     case Instruction::Load:
02827       return SelectLoad(I);
02828     case Instruction::Store:
02829       return SelectStore(I);
02830     case Instruction::Br:
02831       return SelectBranch(I);
02832     case Instruction::IndirectBr:
02833       return SelectIndirectBr(I);
02834     case Instruction::ICmp:
02835     case Instruction::FCmp:
02836       return SelectCmp(I);
02837     case Instruction::FPExt:
02838       return SelectFPExt(I);
02839     case Instruction::FPTrunc:
02840       return SelectFPTrunc(I);
02841     case Instruction::SIToFP:
02842       return SelectIToFP(I, /*isSigned*/ true);
02843     case Instruction::UIToFP:
02844       return SelectIToFP(I, /*isSigned*/ false);
02845     case Instruction::FPToSI:
02846       return SelectFPToI(I, /*isSigned*/ true);
02847     case Instruction::FPToUI:
02848       return SelectFPToI(I, /*isSigned*/ false);
02849     case Instruction::Add:
02850       return SelectBinaryIntOp(I, ISD::ADD);
02851     case Instruction::Or:
02852       return SelectBinaryIntOp(I, ISD::OR);
02853     case Instruction::Sub:
02854       return SelectBinaryIntOp(I, ISD::SUB);
02855     case Instruction::FAdd:
02856       return SelectBinaryFPOp(I, ISD::FADD);
02857     case Instruction::FSub:
02858       return SelectBinaryFPOp(I, ISD::FSUB);
02859     case Instruction::FMul:
02860       return SelectBinaryFPOp(I, ISD::FMUL);
02861     case Instruction::SDiv:
02862       return SelectDiv(I, /*isSigned*/ true);
02863     case Instruction::UDiv:
02864       return SelectDiv(I, /*isSigned*/ false);
02865     case Instruction::SRem:
02866       return SelectRem(I, /*isSigned*/ true);
02867     case Instruction::URem:
02868       return SelectRem(I, /*isSigned*/ false);
02869     case Instruction::Call:
02870       if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
02871         return SelectIntrinsicCall(*II);
02872       return SelectCall(I);
02873     case Instruction::Select:
02874       return SelectSelect(I);
02875     case Instruction::Ret:
02876       return SelectRet(I);
02877     case Instruction::Trunc:
02878       return SelectTrunc(I);
02879     case Instruction::ZExt:
02880     case Instruction::SExt:
02881       return SelectIntExt(I);
02882     case Instruction::Shl:
02883       return SelectShift(I, ARM_AM::lsl);
02884     case Instruction::LShr:
02885       return SelectShift(I, ARM_AM::lsr);
02886     case Instruction::AShr:
02887       return SelectShift(I, ARM_AM::asr);
02888     default: break;
02889   }
02890   return false;
02891 }
02892 
02893 namespace {
02894 // This table describes sign- and zero-extend instructions which can be
02895 // folded into a preceding load. All of these extends have an immediate
02896 // (sometimes a mask and sometimes a shift) that's applied after
02897 // extension.
02898 const struct FoldableLoadExtendsStruct {
02899   uint16_t Opc[2];  // ARM, Thumb.
02900   uint8_t ExpectedImm;
02901   uint8_t isZExt     : 1;
02902   uint8_t ExpectedVT : 7;
02903 } FoldableLoadExtends[] = {
02904   { { ARM::SXTH,  ARM::t2SXTH  },   0, 0, MVT::i16 },
02905   { { ARM::UXTH,  ARM::t2UXTH  },   0, 1, MVT::i16 },
02906   { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8  },
02907   { { ARM::SXTB,  ARM::t2SXTB  },   0, 0, MVT::i8  },
02908   { { ARM::UXTB,  ARM::t2UXTB  },   0, 1, MVT::i8  }
02909 };
02910 }
02911 
02912 /// \brief The specified machine instr operand is a vreg, and that
02913 /// vreg is being provided by the specified load instruction.  If possible,
02914 /// try to fold the load as an operand to the instruction, returning true if
02915 /// successful.
02916 bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
02917                                       const LoadInst *LI) {
02918   // Verify we have a legal type before going any further.
02919   MVT VT;
02920   if (!isLoadTypeLegal(LI->getType(), VT))
02921     return false;
02922 
02923   // Combine load followed by zero- or sign-extend.
02924   // ldrb r1, [r0]       ldrb r1, [r0]
02925   // uxtb r2, r1     =>
02926   // mov  r3, r2         mov  r3, r1
02927   if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
02928     return false;
02929   const uint64_t Imm = MI->getOperand(2).getImm();
02930 
02931   bool Found = false;
02932   bool isZExt;
02933   for (unsigned i = 0, e = array_lengthof(FoldableLoadExtends);
02934        i != e; ++i) {
02935     if (FoldableLoadExtends[i].Opc[isThumb2] == MI->getOpcode() &&
02936         (uint64_t)FoldableLoadExtends[i].ExpectedImm == Imm &&
02937         MVT((MVT::SimpleValueType)FoldableLoadExtends[i].ExpectedVT) == VT) {
02938       Found = true;
02939       isZExt = FoldableLoadExtends[i].isZExt;
02940     }
02941   }
02942   if (!Found) return false;
02943 
02944   // See if we can handle this address.
02945   Address Addr;
02946   if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
02947 
02948   unsigned ResultReg = MI->getOperand(0).getReg();
02949   if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
02950     return false;
02951   MI->eraseFromParent();
02952   return true;
02953 }
02954 
02955 unsigned ARMFastISel::ARMLowerPICELF(const GlobalValue *GV,
02956                                      unsigned Align, MVT VT) {
02957   bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
02958   ARMConstantPoolConstant *CPV =
02959     ARMConstantPoolConstant::Create(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
02960   unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
02961 
02962   unsigned Opc;
02963   unsigned DestReg1 = createResultReg(TLI.getRegClassFor(VT));
02964   // Load value.
02965   if (isThumb2) {
02966     DestReg1 = constrainOperandRegClass(TII.get(ARM::t2LDRpci), DestReg1, 0);
02967     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
02968                             TII.get(ARM::t2LDRpci), DestReg1)
02969                     .addConstantPoolIndex(Idx));
02970     Opc = UseGOTOFF ? ARM::t2ADDrr : ARM::t2LDRs;
02971   } else {
02972     // The extra immediate is for addrmode2.
02973     DestReg1 = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg1, 0);
02974     AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
02975                             DbgLoc, TII.get(ARM::LDRcp), DestReg1)
02976                     .addConstantPoolIndex(Idx).addImm(0));
02977     Opc = UseGOTOFF ? ARM::ADDrr : ARM::LDRrs;
02978   }
02979 
02980   unsigned GlobalBaseReg = AFI->getGlobalBaseReg();
02981   if (GlobalBaseReg == 0) {
02982     GlobalBaseReg = MRI.createVirtualRegister(TLI.getRegClassFor(VT));
02983     AFI->setGlobalBaseReg(GlobalBaseReg);
02984   }
02985 
02986   unsigned DestReg2 = createResultReg(TLI.getRegClassFor(VT));
02987   DestReg2 = constrainOperandRegClass(TII.get(Opc), DestReg2, 0);
02988   DestReg1 = constrainOperandRegClass(TII.get(Opc), DestReg1, 1);
02989   GlobalBaseReg = constrainOperandRegClass(TII.get(Opc), GlobalBaseReg, 2);
02990   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
02991                                     DbgLoc, TII.get(Opc), DestReg2)
02992                             .addReg(DestReg1)
02993                             .addReg(GlobalBaseReg);
02994   if (!UseGOTOFF)
02995     MIB.addImm(0);
02996   AddOptionalDefs(MIB);
02997 
02998   return DestReg2;
02999 }
03000 
03001 bool ARMFastISel::fastLowerArguments() {
03002   if (!FuncInfo.CanLowerReturn)
03003     return false;
03004 
03005   const Function *F = FuncInfo.Fn;
03006   if (F->isVarArg())
03007     return false;
03008 
03009   CallingConv::ID CC = F->getCallingConv();
03010   switch (CC) {
03011   default:
03012     return false;
03013   case CallingConv::Fast:
03014   case CallingConv::C:
03015   case CallingConv::ARM_AAPCS_VFP:
03016   case CallingConv::ARM_AAPCS:
03017   case CallingConv::ARM_APCS:
03018     break;
03019   }
03020 
03021   // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
03022   // which are passed in r0 - r3.
03023   unsigned Idx = 1;
03024   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
03025        I != E; ++I, ++Idx) {
03026     if (Idx > 4)
03027       return false;
03028 
03029     if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
03030         F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
03031         F->getAttributes().hasAttribute(Idx, Attribute::ByVal))
03032       return false;
03033 
03034     Type *ArgTy = I->getType();
03035     if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
03036       return false;
03037 
03038     EVT ArgVT = TLI.getValueType(ArgTy);
03039     if (!ArgVT.isSimple()) return false;
03040     switch (ArgVT.getSimpleVT().SimpleTy) {
03041     case MVT::i8:
03042     case MVT::i16:
03043     case MVT::i32:
03044       break;
03045     default:
03046       return false;
03047     }
03048   }
03049 
03050 
03051   static const uint16_t GPRArgRegs[] = {
03052     ARM::R0, ARM::R1, ARM::R2, ARM::R3
03053   };
03054 
03055   const TargetRegisterClass *RC = &ARM::rGPRRegClass;
03056   Idx = 0;
03057   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
03058        I != E; ++I, ++Idx) {
03059     unsigned SrcReg = GPRArgRegs[Idx];
03060     unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
03061     // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
03062     // Without this, EmitLiveInCopies may eliminate the livein if its only
03063     // use is a bitcast (which isn't turned into an instruction).
03064     unsigned ResultReg = createResultReg(RC);
03065     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
03066             TII.get(TargetOpcode::COPY),
03067             ResultReg).addReg(DstReg, getKillRegState(true));
03068     updateValueMap(I, ResultReg);
03069   }
03070 
03071   return true;
03072 }
03073 
03074 namespace llvm {
03075   FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
03076                                 const TargetLibraryInfo *libInfo) {
03077     const TargetMachine &TM = funcInfo.MF->getTarget();
03078 
03079     const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
03080     // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
03081     bool UseFastISel = false;
03082     UseFastISel |= Subtarget->isTargetMachO() && !Subtarget->isThumb1Only();
03083     UseFastISel |= Subtarget->isTargetLinux() && !Subtarget->isThumb();
03084     UseFastISel |= Subtarget->isTargetNaCl() && !Subtarget->isThumb();
03085 
03086     if (UseFastISel) {
03087       // iOS always has a FP for backtracking, force other targets
03088       // to keep their FP when doing FastISel. The emitted code is
03089       // currently superior, and in cases like test-suite's lencod
03090       // FastISel isn't quite correct when FP is eliminated.
03091       TM.Options.NoFramePointerElim = true;
03092       return new ARMFastISel(funcInfo, libInfo);
03093     }
03094     return nullptr;
03095   }
03096 }