Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1 | //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ARM-specific support for the FastISel class. Some |
| 11 | // of the target-specific code is generated by tablegen in the file |
| 12 | // ARMGenFastISel.inc, which is #included here. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "ARM.h" |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 17 | #include "ARMBaseInstrInfo.h" |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 18 | #include "ARMCallingConv.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 19 | #include "ARMRegisterInfo.h" |
| 20 | #include "ARMTargetMachine.h" |
| 21 | #include "ARMSubtarget.h" |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 22 | #include "ARMConstantPoolValue.h" |
Evan Cheng | ee04a6d | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/ARMAddressingModes.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 24 | #include "llvm/CallingConv.h" |
| 25 | #include "llvm/DerivedTypes.h" |
| 26 | #include "llvm/GlobalVariable.h" |
| 27 | #include "llvm/Instructions.h" |
| 28 | #include "llvm/IntrinsicInst.h" |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 30 | #include "llvm/Operator.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Analysis.h" |
| 32 | #include "llvm/CodeGen/FastISel.h" |
| 33 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 35 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 37 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eric Christopher | d56d61a | 2010-10-17 01:51:42 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 40 | #include "llvm/Support/CallSite.h" |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
| 43 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
| 45 | #include "llvm/Target/TargetInstrInfo.h" |
| 46 | #include "llvm/Target/TargetLowering.h" |
| 47 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 48 | #include "llvm/Target/TargetOptions.h" |
| 49 | using namespace llvm; |
| 50 | |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
Eric Christopher | 6e5367d | 2010-10-18 22:53:53 +0000 | [diff] [blame] | 52 | DisableARMFastISel("disable-arm-fast-isel", |
| 53 | cl::desc("Turn off experimental ARM fast-isel support"), |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 54 | cl::init(false), cl::Hidden); |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 55 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 56 | extern cl::opt<bool> EnableARMLongCalls; |
| 57 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 58 | namespace { |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 59 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 60 | // All possible address modes, plus some. |
| 61 | typedef struct Address { |
| 62 | enum { |
| 63 | RegBase, |
| 64 | FrameIndexBase |
| 65 | } BaseType; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 66 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 67 | union { |
| 68 | unsigned Reg; |
| 69 | int FI; |
| 70 | } Base; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 71 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 72 | int Offset; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 73 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 74 | // Innocuous defaults for our address. |
| 75 | Address() |
Jim Grosbach | 0c72076 | 2011-05-16 22:24:07 +0000 | [diff] [blame] | 76 | : BaseType(RegBase), Offset(0) { |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 77 | Base.Reg = 0; |
| 78 | } |
| 79 | } Address; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 80 | |
| 81 | class ARMFastISel : public FastISel { |
| 82 | |
| 83 | /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can |
| 84 | /// make the right decision when generating code for different targets. |
| 85 | const ARMSubtarget *Subtarget; |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 86 | const TargetMachine &TM; |
| 87 | const TargetInstrInfo &TII; |
| 88 | const TargetLowering &TLI; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 89 | ARMFunctionInfo *AFI; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 90 | |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 91 | // Convenience variables to avoid some queries. |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 92 | bool isThumb2; |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 93 | LLVMContext *Context; |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 94 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 95 | public: |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 96 | explicit ARMFastISel(FunctionLoweringInfo &funcInfo) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 97 | : FastISel(funcInfo), |
| 98 | TM(funcInfo.MF->getTarget()), |
| 99 | TII(*TM.getInstrInfo()), |
| 100 | TLI(*TM.getTargetLowering()) { |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 101 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 102 | AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 103 | isThumb2 = AFI->isThumbFunction(); |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 104 | Context = &funcInfo.Fn->getContext(); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 107 | // Code from FastISel.cpp. |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 108 | virtual unsigned FastEmitInst_(unsigned MachineInstOpcode, |
| 109 | const TargetRegisterClass *RC); |
| 110 | virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode, |
| 111 | const TargetRegisterClass *RC, |
| 112 | unsigned Op0, bool Op0IsKill); |
| 113 | virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode, |
| 114 | const TargetRegisterClass *RC, |
| 115 | unsigned Op0, bool Op0IsKill, |
| 116 | unsigned Op1, bool Op1IsKill); |
Cameron Zwarich | c0e6d78 | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 117 | virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 118 | const TargetRegisterClass *RC, |
| 119 | unsigned Op0, bool Op0IsKill, |
| 120 | unsigned Op1, bool Op1IsKill, |
| 121 | unsigned Op2, bool Op2IsKill); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 122 | virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode, |
| 123 | const TargetRegisterClass *RC, |
| 124 | unsigned Op0, bool Op0IsKill, |
| 125 | uint64_t Imm); |
| 126 | virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode, |
| 127 | const TargetRegisterClass *RC, |
| 128 | unsigned Op0, bool Op0IsKill, |
| 129 | const ConstantFP *FPImm); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 130 | virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode, |
| 131 | const TargetRegisterClass *RC, |
| 132 | unsigned Op0, bool Op0IsKill, |
| 133 | unsigned Op1, bool Op1IsKill, |
| 134 | uint64_t Imm); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 135 | virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode, |
| 136 | const TargetRegisterClass *RC, |
| 137 | uint64_t Imm); |
Eric Christopher | d94bc54 | 2011-04-29 22:07:50 +0000 | [diff] [blame] | 138 | virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode, |
| 139 | const TargetRegisterClass *RC, |
| 140 | uint64_t Imm1, uint64_t Imm2); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 141 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 142 | virtual unsigned FastEmitInst_extractsubreg(MVT RetVT, |
| 143 | unsigned Op0, bool Op0IsKill, |
| 144 | uint32_t Idx); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 145 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 146 | // Backend specific FastISel code. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 147 | virtual bool TargetSelectInstruction(const Instruction *I); |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 148 | virtual unsigned TargetMaterializeConstant(const Constant *C); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 149 | virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI); |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 150 | virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo, |
| 151 | const LoadInst *LI); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 152 | |
| 153 | #include "ARMGenFastISel.inc" |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 154 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 155 | // Instruction selection routines. |
Eric Christopher | 44bff90 | 2010-09-10 23:10:30 +0000 | [diff] [blame] | 156 | private: |
Eric Christopher | 1778772 | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 157 | bool SelectLoad(const Instruction *I); |
| 158 | bool SelectStore(const Instruction *I); |
| 159 | bool SelectBranch(const Instruction *I); |
| 160 | bool SelectCmp(const Instruction *I); |
| 161 | bool SelectFPExt(const Instruction *I); |
| 162 | bool SelectFPTrunc(const Instruction *I); |
| 163 | bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode); |
| 164 | bool SelectSIToFP(const Instruction *I); |
| 165 | bool SelectFPToSI(const Instruction *I); |
| 166 | bool SelectSDiv(const Instruction *I); |
| 167 | bool SelectSRem(const Instruction *I); |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 168 | bool SelectCall(const Instruction *I, const char *IntrMemName); |
| 169 | bool SelectIntrinsicCall(const IntrinsicInst &I); |
Eric Christopher | 1778772 | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 170 | bool SelectSelect(const Instruction *I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 171 | bool SelectRet(const Instruction *I); |
Chad Rosier | 0d7b231 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 172 | bool SelectTrunc(const Instruction *I); |
| 173 | bool SelectIntExt(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 174 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 175 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 176 | private: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 177 | bool isTypeLegal(Type *Ty, MVT &VT); |
| 178 | bool isLoadTypeLegal(Type *Ty, MVT &VT); |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 179 | bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, |
| 180 | bool isZExt); |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 181 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, bool isZExt, |
| 182 | bool allocReg); |
| 183 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 184 | bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr); |
| 185 | bool ARMComputeAddress(const Value *Obj, Address &Addr); |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 186 | void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3); |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 187 | bool ARMIsMemCpySmall(uint64_t Len); |
| 188 | bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len); |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 189 | unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt); |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 190 | unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT); |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 191 | unsigned ARMMaterializeInt(const Constant *C, EVT VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 192 | unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT); |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 193 | unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 194 | unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 195 | unsigned ARMSelectCallOp(const GlobalValue *GV); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 196 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 197 | // Call handling routines. |
| 198 | private: |
| 199 | CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 200 | bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 201 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 202 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 203 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 204 | SmallVectorImpl<unsigned> &RegArgs, |
| 205 | CallingConv::ID CC, |
| 206 | unsigned &NumBytes); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 207 | bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 208 | const Instruction *I, CallingConv::ID CC, |
| 209 | unsigned &NumBytes); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 210 | bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 211 | |
| 212 | // OptionalDef handling routines. |
| 213 | private: |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 214 | bool isARMNEONPred(const MachineInstr *MI); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 215 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 216 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 217 | void AddLoadStoreOperands(EVT VT, Address &Addr, |
Cameron Zwarich | c152aa6 | 2011-05-28 20:34:49 +0000 | [diff] [blame] | 218 | const MachineInstrBuilder &MIB, |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 219 | unsigned Flags, bool useAM3); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 220 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 221 | |
| 222 | } // end anonymous namespace |
| 223 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 224 | #include "ARMGenCallingConv.inc" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 225 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 226 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 227 | // we don't care about implicit defs here, just places we'll need to add a |
| 228 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 229 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 230 | const MCInstrDesc &MCID = MI->getDesc(); |
| 231 | if (!MCID.hasOptionalDef()) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 232 | return false; |
| 233 | |
| 234 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 235 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 236 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 237 | if (!MO.isReg() || !MO.isDef()) continue; |
| 238 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 239 | *CPSR = true; |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 244 | bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 245 | const MCInstrDesc &MCID = MI->getDesc(); |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 246 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 247 | // If we're a thumb2 or not NEON function we were handled via isPredicable. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 248 | if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 249 | AFI->isThumb2Function()) |
| 250 | return false; |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 251 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 252 | for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) |
| 253 | if (MCID.OpInfo[i].isPredicate()) |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 254 | return true; |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 255 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 259 | // If the machine is predicable go ahead and add the predicate operands, if |
| 260 | // it needs default CC operands add those. |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 261 | // TODO: If we want to support thumb1 then we'll need to deal with optional |
| 262 | // CPSR defs that need to be added before the remaining operands. See s_cc_out |
| 263 | // for descriptions why. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 264 | const MachineInstrBuilder & |
| 265 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 266 | MachineInstr *MI = &*MIB; |
| 267 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 268 | // Do we use a predicate? or... |
| 269 | // Are we NEON in ARM mode and have a predicate operand? If so, I know |
| 270 | // we're not predicable but add it anyways. |
| 271 | if (TII.isPredicable(MI) || isARMNEONPred(MI)) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 272 | AddDefaultPred(MIB); |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 273 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 274 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 275 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 276 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 277 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 278 | if (CPSR) |
| 279 | AddDefaultT1CC(MIB); |
| 280 | else |
| 281 | AddDefaultCC(MIB); |
| 282 | } |
| 283 | return MIB; |
| 284 | } |
| 285 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 286 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 287 | const TargetRegisterClass* RC) { |
| 288 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 289 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 290 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 291 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 292 | return ResultReg; |
| 293 | } |
| 294 | |
| 295 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 296 | const TargetRegisterClass *RC, |
| 297 | unsigned Op0, bool Op0IsKill) { |
| 298 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 299 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 300 | |
| 301 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 302 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 303 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 304 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 305 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 306 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 307 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 308 | TII.get(TargetOpcode::COPY), ResultReg) |
| 309 | .addReg(II.ImplicitDefs[0])); |
| 310 | } |
| 311 | return ResultReg; |
| 312 | } |
| 313 | |
| 314 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 315 | const TargetRegisterClass *RC, |
| 316 | unsigned Op0, bool Op0IsKill, |
| 317 | unsigned Op1, bool Op1IsKill) { |
| 318 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 319 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 320 | |
| 321 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 322 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 323 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 324 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 325 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 326 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 327 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 328 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 329 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 330 | TII.get(TargetOpcode::COPY), ResultReg) |
| 331 | .addReg(II.ImplicitDefs[0])); |
| 332 | } |
| 333 | return ResultReg; |
| 334 | } |
| 335 | |
Cameron Zwarich | c0e6d78 | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 336 | unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 337 | const TargetRegisterClass *RC, |
| 338 | unsigned Op0, bool Op0IsKill, |
| 339 | unsigned Op1, bool Op1IsKill, |
| 340 | unsigned Op2, bool Op2IsKill) { |
| 341 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 342 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Cameron Zwarich | c0e6d78 | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 343 | |
| 344 | if (II.getNumDefs() >= 1) |
| 345 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 346 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 347 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 348 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
| 349 | else { |
| 350 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 351 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 352 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 353 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
| 354 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 355 | TII.get(TargetOpcode::COPY), ResultReg) |
| 356 | .addReg(II.ImplicitDefs[0])); |
| 357 | } |
| 358 | return ResultReg; |
| 359 | } |
| 360 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 361 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 362 | const TargetRegisterClass *RC, |
| 363 | unsigned Op0, bool Op0IsKill, |
| 364 | uint64_t Imm) { |
| 365 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 366 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 367 | |
| 368 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 369 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 370 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 371 | .addImm(Imm)); |
| 372 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 373 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 374 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 375 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 376 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 377 | TII.get(TargetOpcode::COPY), ResultReg) |
| 378 | .addReg(II.ImplicitDefs[0])); |
| 379 | } |
| 380 | return ResultReg; |
| 381 | } |
| 382 | |
| 383 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 384 | const TargetRegisterClass *RC, |
| 385 | unsigned Op0, bool Op0IsKill, |
| 386 | const ConstantFP *FPImm) { |
| 387 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 388 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 389 | |
| 390 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 391 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 392 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 393 | .addFPImm(FPImm)); |
| 394 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 395 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 396 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 397 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 398 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 399 | TII.get(TargetOpcode::COPY), ResultReg) |
| 400 | .addReg(II.ImplicitDefs[0])); |
| 401 | } |
| 402 | return ResultReg; |
| 403 | } |
| 404 | |
| 405 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 406 | const TargetRegisterClass *RC, |
| 407 | unsigned Op0, bool Op0IsKill, |
| 408 | unsigned Op1, bool Op1IsKill, |
| 409 | uint64_t Imm) { |
| 410 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 411 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 412 | |
| 413 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 414 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 415 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 416 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 417 | .addImm(Imm)); |
| 418 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 419 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 420 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 421 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 422 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 423 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 424 | TII.get(TargetOpcode::COPY), ResultReg) |
| 425 | .addReg(II.ImplicitDefs[0])); |
| 426 | } |
| 427 | return ResultReg; |
| 428 | } |
| 429 | |
| 430 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 431 | const TargetRegisterClass *RC, |
| 432 | uint64_t Imm) { |
| 433 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 434 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 435 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 436 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 437 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 438 | .addImm(Imm)); |
| 439 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 440 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 441 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 442 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 443 | TII.get(TargetOpcode::COPY), ResultReg) |
| 444 | .addReg(II.ImplicitDefs[0])); |
| 445 | } |
| 446 | return ResultReg; |
| 447 | } |
| 448 | |
Eric Christopher | d94bc54 | 2011-04-29 22:07:50 +0000 | [diff] [blame] | 449 | unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode, |
| 450 | const TargetRegisterClass *RC, |
| 451 | uint64_t Imm1, uint64_t Imm2) { |
| 452 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 453 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 454 | |
Eric Christopher | d94bc54 | 2011-04-29 22:07:50 +0000 | [diff] [blame] | 455 | if (II.getNumDefs() >= 1) |
| 456 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 457 | .addImm(Imm1).addImm(Imm2)); |
| 458 | else { |
| 459 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 460 | .addImm(Imm1).addImm(Imm2)); |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 461 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | d94bc54 | 2011-04-29 22:07:50 +0000 | [diff] [blame] | 462 | TII.get(TargetOpcode::COPY), |
| 463 | ResultReg) |
| 464 | .addReg(II.ImplicitDefs[0])); |
| 465 | } |
| 466 | return ResultReg; |
| 467 | } |
| 468 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 469 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 470 | unsigned Op0, bool Op0IsKill, |
| 471 | uint32_t Idx) { |
| 472 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 473 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 474 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 475 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 476 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 477 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 478 | return ResultReg; |
| 479 | } |
| 480 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 481 | // TODO: Don't worry about 64-bit now, but when this is fixed remove the |
| 482 | // checks from the various callers. |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 483 | unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 484 | if (VT == MVT::f64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 485 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 486 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 487 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 488 | TII.get(ARM::VMOVRS), MoveReg) |
| 489 | .addReg(SrcReg)); |
| 490 | return MoveReg; |
| 491 | } |
| 492 | |
| 493 | unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 494 | if (VT == MVT::i64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 495 | |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 496 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 497 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 498 | TII.get(ARM::VMOVSR), MoveReg) |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 499 | .addReg(SrcReg)); |
| 500 | return MoveReg; |
| 501 | } |
| 502 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 503 | // For double width floating point we need to materialize two constants |
| 504 | // (the high and the low) into integer registers then use a move to get |
| 505 | // the combined constant into an FP reg. |
| 506 | unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) { |
| 507 | const APFloat Val = CFP->getValueAPF(); |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 508 | bool is64bit = VT == MVT::f64; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 509 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 510 | // This checks to see if we can use VFP3 instructions to materialize |
| 511 | // a constant, otherwise we have to go through the constant pool. |
| 512 | if (TLI.isFPImmLegal(Val, VT)) { |
Jim Grosbach | 4ebbf7b | 2011-09-30 00:50:06 +0000 | [diff] [blame] | 513 | int Imm; |
| 514 | unsigned Opc; |
| 515 | if (is64bit) { |
| 516 | Imm = ARM_AM::getFP64Imm(Val); |
| 517 | Opc = ARM::FCONSTD; |
| 518 | } else { |
| 519 | Imm = ARM_AM::getFP32Imm(Val); |
| 520 | Opc = ARM::FCONSTS; |
| 521 | } |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 522 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 523 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 524 | DestReg) |
Jim Grosbach | 4ebbf7b | 2011-09-30 00:50:06 +0000 | [diff] [blame] | 525 | .addImm(Imm)); |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 526 | return DestReg; |
| 527 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 528 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 529 | // Require VFP2 for loading fp constants. |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 530 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 531 | |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 532 | // MachineConstantPool wants an explicit alignment. |
| 533 | unsigned Align = TD.getPrefTypeAlignment(CFP->getType()); |
| 534 | if (Align == 0) { |
| 535 | // TODO: Figure out if this is correct. |
| 536 | Align = TD.getTypeAllocSize(CFP->getType()); |
| 537 | } |
| 538 | unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); |
| 539 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 540 | unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 541 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 542 | // The extra reg is for addrmode5. |
Eric Christopher | f5732c4 | 2010-09-28 00:35:09 +0000 | [diff] [blame] | 543 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 544 | DestReg) |
| 545 | .addConstantPoolIndex(Idx) |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 546 | .addReg(0)); |
| 547 | return DestReg; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 550 | unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) { |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 551 | |
Chad Rosier | 44e8957 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 552 | if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) |
| 553 | return false; |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 554 | |
| 555 | // If we can do this in a single instruction without a constant pool entry |
| 556 | // do so now. |
| 557 | const ConstantInt *CI = cast<ConstantInt>(C); |
Chad Rosier | a4e0727 | 2011-11-04 23:09:49 +0000 | [diff] [blame] | 558 | if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) { |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 559 | unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16; |
Chad Rosier | 4e89d97 | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 560 | unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32)); |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 561 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Chad Rosier | 44e8957 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 562 | TII.get(Opc), ImmReg) |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 563 | .addImm(CI->getZExtValue())); |
Chad Rosier | 44e8957 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 564 | return ImmReg; |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Chad Rosier | 4e89d97 | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 567 | // Use MVN to emit negative constants. |
| 568 | if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) { |
| 569 | unsigned Imm = (unsigned)~(CI->getSExtValue()); |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 570 | bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
Chad Rosier | 4e89d97 | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 571 | (ARM_AM::getSOImmVal(Imm) != -1); |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 572 | if (UseImm) { |
Chad Rosier | 4e89d97 | 2011-11-11 00:36:21 +0000 | [diff] [blame] | 573 | unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi; |
| 574 | unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32)); |
| 575 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 576 | TII.get(Opc), ImmReg) |
| 577 | .addImm(Imm)); |
| 578 | return ImmReg; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Load from constant pool. For now 32-bit only. |
Chad Rosier | 44e8957 | 2011-11-04 22:29:00 +0000 | [diff] [blame] | 583 | if (VT != MVT::i32) |
| 584 | return false; |
| 585 | |
| 586 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 587 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 588 | // MachineConstantPool wants an explicit alignment. |
| 589 | unsigned Align = TD.getPrefTypeAlignment(C->getType()); |
| 590 | if (Align == 0) { |
| 591 | // TODO: Figure out if this is correct. |
| 592 | Align = TD.getTypeAllocSize(C->getType()); |
| 593 | } |
| 594 | unsigned Idx = MCP.getConstantPoolIndex(C, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 595 | |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 596 | if (isThumb2) |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 597 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 598 | TII.get(ARM::t2LDRpci), DestReg) |
| 599 | .addConstantPoolIndex(Idx)); |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 600 | else |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 601 | // The extra immediate is for addrmode2. |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 602 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 603 | TII.get(ARM::LDRcp), DestReg) |
| 604 | .addConstantPoolIndex(Idx) |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 605 | .addImm(0)); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 606 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 607 | return DestReg; |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 610 | unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) { |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 611 | // For now 32-bit only. |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 612 | if (VT != MVT::i32) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 613 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 614 | Reloc::Model RelocM = TM.getRelocationModel(); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 615 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 616 | // TODO: Need more magic for ARM PIC. |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 617 | if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 618 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 619 | // MachineConstantPool wants an explicit alignment. |
| 620 | unsigned Align = TD.getPrefTypeAlignment(GV->getType()); |
| 621 | if (Align == 0) { |
| 622 | // TODO: Figure out if this is correct. |
| 623 | Align = TD.getTypeAllocSize(GV->getType()); |
| 624 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 625 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 626 | // Grab index. |
| 627 | unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8); |
Evan Cheng | 5de5d4b | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 628 | unsigned Id = AFI->createPICLabelUId(); |
Bill Wendling | 5bb7799 | 2011-10-01 08:00:54 +0000 | [diff] [blame] | 629 | ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id, |
| 630 | ARMCP::CPValue, |
| 631 | PCAdj); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 632 | unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 633 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 634 | // Load value. |
| 635 | MachineInstrBuilder MIB; |
| 636 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 637 | if (isThumb2) { |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 638 | unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic; |
| 639 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg) |
| 640 | .addConstantPoolIndex(Idx); |
| 641 | if (RelocM == Reloc::PIC_) |
| 642 | MIB.addImm(Id); |
| 643 | } else { |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 644 | // The extra immediate is for addrmode2. |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 645 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp), |
| 646 | DestReg) |
| 647 | .addConstantPoolIndex(Idx) |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 648 | .addImm(0); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 649 | } |
| 650 | AddOptionalDefs(MIB); |
Eli Friedman | d6412c9 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 651 | |
| 652 | if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) { |
| 653 | unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT)); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 654 | if (isThumb2) |
Jim Grosbach | b04546f | 2011-09-13 20:30:37 +0000 | [diff] [blame] | 655 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 656 | TII.get(ARM::t2LDRi12), NewDestReg) |
Eli Friedman | d6412c9 | 2011-06-03 01:13:19 +0000 | [diff] [blame] | 657 | .addReg(DestReg) |
| 658 | .addImm(0); |
| 659 | else |
| 660 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12), |
| 661 | NewDestReg) |
| 662 | .addReg(DestReg) |
| 663 | .addImm(0); |
| 664 | DestReg = NewDestReg; |
| 665 | AddOptionalDefs(MIB); |
| 666 | } |
| 667 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 668 | return DestReg; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 671 | unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) { |
| 672 | EVT VT = TLI.getValueType(C->getType(), true); |
| 673 | |
| 674 | // Only handle simple types. |
| 675 | if (!VT.isSimple()) return 0; |
| 676 | |
| 677 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 678 | return ARMMaterializeFP(CFP, VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 679 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 680 | return ARMMaterializeGV(GV, VT); |
| 681 | else if (isa<ConstantInt>(C)) |
| 682 | return ARMMaterializeInt(C, VT); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 683 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 684 | return 0; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 687 | unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) { |
| 688 | // Don't handle dynamic allocas. |
| 689 | if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 690 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 691 | MVT VT; |
Eric Christopher | ec8bf97 | 2010-10-17 06:07:26 +0000 | [diff] [blame] | 692 | if (!isLoadTypeLegal(AI->getType(), VT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 693 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 694 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 695 | FuncInfo.StaticAllocaMap.find(AI); |
| 696 | |
| 697 | // This will get lowered later into the correct offsets and registers |
| 698 | // via rewriteXFrameIndex. |
| 699 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 700 | TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
| 701 | unsigned ResultReg = createResultReg(RC); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 702 | unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 703 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 704 | TII.get(Opc), ResultReg) |
| 705 | .addFrameIndex(SI->second) |
| 706 | .addImm(0)); |
| 707 | return ResultReg; |
| 708 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 709 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 713 | bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 714 | EVT evt = TLI.getValueType(Ty, true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 715 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 716 | // Only handle simple types. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 717 | if (evt == MVT::Other || !evt.isSimple()) return false; |
| 718 | VT = evt.getSimpleVT(); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 719 | |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 720 | // Handle all legal types, i.e. a register that will directly hold this |
| 721 | // value. |
| 722 | return TLI.isTypeLegal(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 725 | bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 726 | if (isTypeLegal(Ty, VT)) return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 727 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 728 | // If this is a type than can be sign or zero-extended to a basic operation |
| 729 | // go ahead and accept it now. |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 730 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 731 | return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 732 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 733 | return false; |
| 734 | } |
| 735 | |
Eric Christopher | 88de86b | 2010-11-19 22:36:41 +0000 | [diff] [blame] | 736 | // Computes the address to get to an object. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 737 | bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 738 | // Some boilerplate from the X86 FastISel. |
| 739 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 740 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 741 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 2d630d7 | 2010-11-19 22:37:58 +0000 | [diff] [blame] | 742 | // Don't walk into other basic blocks unless the object is an alloca from |
| 743 | // another block, otherwise it may not have a virtual register assigned. |
Eric Christopher | 76dda7e | 2010-11-15 21:11:06 +0000 | [diff] [blame] | 744 | if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || |
| 745 | FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { |
| 746 | Opcode = I->getOpcode(); |
| 747 | U = I; |
| 748 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 749 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 750 | Opcode = C->getOpcode(); |
| 751 | U = C; |
| 752 | } |
| 753 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 754 | if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 755 | if (Ty->getAddressSpace() > 255) |
| 756 | // Fast instruction selection doesn't support the special |
| 757 | // address spaces. |
| 758 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 759 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 760 | switch (Opcode) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 761 | default: |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 762 | break; |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 763 | case Instruction::BitCast: { |
| 764 | // Look through bitcasts. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 765 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 766 | } |
| 767 | case Instruction::IntToPtr: { |
| 768 | // Look past no-op inttoptrs. |
| 769 | if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 770 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | case Instruction::PtrToInt: { |
| 774 | // Look past no-op ptrtoints. |
| 775 | if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 776 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 777 | break; |
| 778 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 779 | case Instruction::GetElementPtr: { |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 780 | Address SavedAddr = Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 781 | int TmpOffset = Addr.Offset; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 782 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 783 | // Iterate through the GEP folding the constants into offsets where |
| 784 | // we can. |
| 785 | gep_type_iterator GTI = gep_type_begin(U); |
| 786 | for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 787 | i != e; ++i, ++GTI) { |
| 788 | const Value *Op = *i; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 789 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 790 | const StructLayout *SL = TD.getStructLayout(STy); |
| 791 | unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); |
| 792 | TmpOffset += SL->getElementOffset(Idx); |
| 793 | } else { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 794 | uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType()); |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 795 | for (;;) { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 796 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 797 | // Constant-offset addressing. |
| 798 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | if (isa<AddOperator>(Op) && |
| 802 | (!isa<Instruction>(Op) || |
| 803 | FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()] |
| 804 | == FuncInfo.MBB) && |
| 805 | isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) { |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 806 | // An add (in the same block) with a constant operand. Fold the |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 807 | // constant. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 808 | ConstantInt *CI = |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 809 | cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 810 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 811 | // Iterate on the other operand. |
| 812 | Op = cast<AddOperator>(Op)->getOperand(0); |
| 813 | continue; |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 814 | } |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 815 | // Unsupported |
| 816 | goto unsupported_gep; |
| 817 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 820 | |
| 821 | // Try to grab the base operand now. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 822 | Addr.Offset = TmpOffset; |
| 823 | if (ARMComputeAddress(U->getOperand(0), Addr)) return true; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 824 | |
| 825 | // We failed, restore everything and try the other options. |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 826 | Addr = SavedAddr; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 827 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 828 | unsupported_gep: |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 829 | break; |
| 830 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 831 | case Instruction::Alloca: { |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 832 | const AllocaInst *AI = cast<AllocaInst>(Obj); |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 833 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 834 | FuncInfo.StaticAllocaMap.find(AI); |
| 835 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 836 | Addr.BaseType = Address::FrameIndexBase; |
| 837 | Addr.Base.FI = SI->second; |
| 838 | return true; |
| 839 | } |
| 840 | break; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 843 | |
Eric Christopher | a9c5751 | 2010-10-13 21:41:51 +0000 | [diff] [blame] | 844 | // Materialize the global variable's address into a reg which can |
| 845 | // then be used later to load the variable. |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 846 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 847 | unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType())); |
| 848 | if (Tmp == 0) return false; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 849 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 850 | Addr.Base.Reg = Tmp; |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 851 | return true; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 852 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 853 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 854 | // Try to get this in a register if nothing else has worked. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 855 | if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); |
| 856 | return Addr.Base.Reg != 0; |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 859 | void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) { |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 860 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 861 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 862 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 863 | bool needsLowering = false; |
| 864 | switch (VT.getSimpleVT().SimpleTy) { |
| 865 | default: |
| 866 | assert(false && "Unhandled load/store type!"); |
Chad Rosier | 7346347 | 2011-11-09 21:30:12 +0000 | [diff] [blame] | 867 | break; |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 868 | case MVT::i1: |
| 869 | case MVT::i8: |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 870 | case MVT::i16: |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 871 | case MVT::i32: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 872 | if (!useAM3) { |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 873 | // Integer loads/stores handle 12-bit offsets. |
| 874 | needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 875 | // Handle negative offsets. |
Chad Rosier | e489af8 | 2011-11-14 22:34:48 +0000 | [diff] [blame] | 876 | if (needsLowering && isThumb2) |
| 877 | needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 && |
| 878 | Addr.Offset > -256); |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 879 | } else { |
Chad Rosier | 5be833d | 2011-11-13 04:25:02 +0000 | [diff] [blame] | 880 | // ARM halfword load/stores and signed byte loads use +/-imm8 offsets. |
Chad Rosier | dc9205d | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 881 | needsLowering = (Addr.Offset > 255 || Addr.Offset < -255); |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 882 | } |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 883 | break; |
| 884 | case MVT::f32: |
| 885 | case MVT::f64: |
| 886 | // Floating point operands handle 8-bit offsets. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 887 | needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 888 | break; |
| 889 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 890 | |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 891 | // If this is a stack pointer and the offset needs to be simplified then |
| 892 | // put the alloca address into a register, set the base type back to |
| 893 | // register and continue. This should almost never happen. |
| 894 | if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 895 | TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass : |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 896 | ARM::GPRRegisterClass; |
| 897 | unsigned ResultReg = createResultReg(RC); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 898 | unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 899 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 900 | TII.get(Opc), ResultReg) |
| 901 | .addFrameIndex(Addr.Base.FI) |
| 902 | .addImm(0)); |
| 903 | Addr.Base.Reg = ResultReg; |
| 904 | Addr.BaseType = Address::RegBase; |
| 905 | } |
| 906 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 907 | // Since the offset is too large for the load/store instruction |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 908 | // get the reg+offset into a register. |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 909 | if (needsLowering) { |
Eli Friedman | 9ebf57a | 2011-04-29 21:22:56 +0000 | [diff] [blame] | 910 | Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg, |
| 911 | /*Op0IsKill*/false, Addr.Offset, MVT::i32); |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 912 | Addr.Offset = 0; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 913 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 916 | void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr, |
Cameron Zwarich | c152aa6 | 2011-05-28 20:34:49 +0000 | [diff] [blame] | 917 | const MachineInstrBuilder &MIB, |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 918 | unsigned Flags, bool useAM3) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 919 | // addrmode5 output depends on the selection dag addressing dividing the |
| 920 | // offset by 4 that it then later multiplies. Do this here as well. |
| 921 | if (VT.getSimpleVT().SimpleTy == MVT::f32 || |
| 922 | VT.getSimpleVT().SimpleTy == MVT::f64) |
| 923 | Addr.Offset /= 4; |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 924 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 925 | // Frame base works a bit differently. Handle it separately. |
| 926 | if (Addr.BaseType == Address::FrameIndexBase) { |
| 927 | int FI = Addr.Base.FI; |
| 928 | int Offset = Addr.Offset; |
| 929 | MachineMemOperand *MMO = |
| 930 | FuncInfo.MF->getMachineMemOperand( |
| 931 | MachinePointerInfo::getFixedStack(FI, Offset), |
Cameron Zwarich | c152aa6 | 2011-05-28 20:34:49 +0000 | [diff] [blame] | 932 | Flags, |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 933 | MFI.getObjectSize(FI), |
| 934 | MFI.getObjectAlignment(FI)); |
| 935 | // Now add the rest of the operands. |
| 936 | MIB.addFrameIndex(FI); |
| 937 | |
Chad Rosier | 5be833d | 2011-11-13 04:25:02 +0000 | [diff] [blame] | 938 | // ARM halfword load/stores and signed byte loads need an additional operand. |
Chad Rosier | dc9205d | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 939 | if (useAM3) { |
| 940 | signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; |
| 941 | MIB.addReg(0); |
| 942 | MIB.addImm(Imm); |
| 943 | } else { |
| 944 | MIB.addImm(Addr.Offset); |
| 945 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 946 | MIB.addMemOperand(MMO); |
| 947 | } else { |
| 948 | // Now add the rest of the operands. |
| 949 | MIB.addReg(Addr.Base.Reg); |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 950 | |
Chad Rosier | 5be833d | 2011-11-13 04:25:02 +0000 | [diff] [blame] | 951 | // ARM halfword load/stores and signed byte loads need an additional operand. |
Chad Rosier | dc9205d | 2011-11-14 04:09:28 +0000 | [diff] [blame] | 952 | if (useAM3) { |
| 953 | signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset; |
| 954 | MIB.addReg(0); |
| 955 | MIB.addImm(Imm); |
| 956 | } else { |
| 957 | MIB.addImm(Addr.Offset); |
| 958 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 959 | } |
| 960 | AddOptionalDefs(MIB); |
| 961 | } |
| 962 | |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 963 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, |
| 964 | bool isZExt = true, bool allocReg = true) { |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 965 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 966 | unsigned Opc; |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 967 | bool useAM3 = false; |
| 968 | TargetRegisterClass *RC; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 969 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 970 | // This is mostly going to be Neon/vector support. |
| 971 | default: return false; |
Chad Rosier | 646abbf | 2011-11-11 02:38:59 +0000 | [diff] [blame] | 972 | case MVT::i1: |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 973 | case MVT::i8: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 974 | if (isThumb2) { |
| 975 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 976 | Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8; |
| 977 | else |
| 978 | Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12; |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 979 | } else { |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 980 | if (isZExt) { |
| 981 | Opc = ARM::LDRBi12; |
| 982 | } else { |
| 983 | Opc = ARM::LDRSB; |
| 984 | useAM3 = true; |
| 985 | } |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 986 | } |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 987 | RC = ARM::GPRRegisterClass; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 988 | break; |
Chad Rosier | 7346347 | 2011-11-09 21:30:12 +0000 | [diff] [blame] | 989 | case MVT::i16: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 990 | if (isThumb2) { |
| 991 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 992 | Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8; |
| 993 | else |
| 994 | Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12; |
| 995 | } else { |
| 996 | Opc = isZExt ? ARM::LDRH : ARM::LDRSH; |
| 997 | useAM3 = true; |
| 998 | } |
Chad Rosier | 7346347 | 2011-11-09 21:30:12 +0000 | [diff] [blame] | 999 | RC = ARM::GPRRegisterClass; |
| 1000 | break; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 1001 | case MVT::i32: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1002 | if (isThumb2) { |
| 1003 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1004 | Opc = ARM::t2LDRi8; |
| 1005 | else |
| 1006 | Opc = ARM::t2LDRi12; |
| 1007 | } else { |
| 1008 | Opc = ARM::LDRi12; |
| 1009 | } |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 1010 | RC = ARM::GPRRegisterClass; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 1011 | break; |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1012 | case MVT::f32: |
| 1013 | Opc = ARM::VLDRS; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 1014 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | case MVT::f64: |
| 1017 | Opc = ARM::VLDRD; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 1018 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 1019 | break; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 1020 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1021 | // Simplify this down to something we can handle. |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1022 | ARMSimplifyAddress(Addr, VT, useAM3); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1023 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1024 | // Create the base instruction, then add the operands. |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1025 | if (allocReg) |
| 1026 | ResultReg = createResultReg(RC); |
| 1027 | assert (ResultReg > 255 && "Expected an allocated virtual register."); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1028 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1029 | TII.get(Opc), ResultReg); |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1030 | AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 1031 | return true; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1034 | bool ARMFastISel::SelectLoad(const Instruction *I) { |
Eli Friedman | 4136d23 | 2011-09-02 22:33:24 +0000 | [diff] [blame] | 1035 | // Atomic loads need special handling. |
| 1036 | if (cast<LoadInst>(I)->isAtomic()) |
| 1037 | return false; |
| 1038 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1039 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1040 | MVT VT; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1041 | if (!isLoadTypeLegal(I->getType(), VT)) |
| 1042 | return false; |
| 1043 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1044 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1045 | Address Addr; |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1046 | if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1047 | |
| 1048 | unsigned ResultReg; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1049 | if (!ARMEmitLoad(VT, ResultReg, Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1050 | UpdateValueMap(I, ResultReg); |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1054 | bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1055 | unsigned StrOpc; |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1056 | bool useAM3 = false; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1057 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1058 | // This is mostly going to be Neon/vector support. |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1059 | default: return false; |
Eric Christopher | 4c91412 | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 1060 | case MVT::i1: { |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1061 | unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass : |
Eric Christopher | 4c91412 | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 1062 | ARM::GPRRegisterClass); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1063 | unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; |
Eric Christopher | 4c91412 | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 1064 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1065 | TII.get(Opc), Res) |
| 1066 | .addReg(SrcReg).addImm(1)); |
| 1067 | SrcReg = Res; |
| 1068 | } // Fallthrough here. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1069 | case MVT::i8: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1070 | if (isThumb2) { |
| 1071 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1072 | StrOpc = ARM::t2STRBi8; |
| 1073 | else |
| 1074 | StrOpc = ARM::t2STRBi12; |
| 1075 | } else { |
| 1076 | StrOpc = ARM::STRBi12; |
| 1077 | } |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | case MVT::i16: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1080 | if (isThumb2) { |
| 1081 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1082 | StrOpc = ARM::t2STRHi8; |
| 1083 | else |
| 1084 | StrOpc = ARM::t2STRHi12; |
| 1085 | } else { |
| 1086 | StrOpc = ARM::STRH; |
| 1087 | useAM3 = true; |
| 1088 | } |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 1089 | break; |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 1090 | case MVT::i32: |
Chad Rosier | 57b2997 | 2011-11-14 20:22:27 +0000 | [diff] [blame] | 1091 | if (isThumb2) { |
| 1092 | if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops()) |
| 1093 | StrOpc = ARM::t2STRi8; |
| 1094 | else |
| 1095 | StrOpc = ARM::t2STRi12; |
| 1096 | } else { |
| 1097 | StrOpc = ARM::STRi12; |
| 1098 | } |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 1099 | break; |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1100 | case MVT::f32: |
| 1101 | if (!Subtarget->hasVFP2()) return false; |
| 1102 | StrOpc = ARM::VSTRS; |
| 1103 | break; |
| 1104 | case MVT::f64: |
| 1105 | if (!Subtarget->hasVFP2()) return false; |
| 1106 | StrOpc = ARM::VSTRD; |
| 1107 | break; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1108 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1109 | // Simplify this down to something we can handle. |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1110 | ARMSimplifyAddress(Addr, VT, useAM3); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1111 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1112 | // Create the base instruction, then add the operands. |
| 1113 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1114 | TII.get(StrOpc)) |
Chad Rosier | 3bdb3c9 | 2011-11-17 01:16:53 +0000 | [diff] [blame] | 1115 | .addReg(SrcReg); |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 1116 | AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1117 | return true; |
| 1118 | } |
| 1119 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1120 | bool ARMFastISel::SelectStore(const Instruction *I) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1121 | Value *Op0 = I->getOperand(0); |
| 1122 | unsigned SrcReg = 0; |
| 1123 | |
Eli Friedman | 4136d23 | 2011-09-02 22:33:24 +0000 | [diff] [blame] | 1124 | // Atomic stores need special handling. |
| 1125 | if (cast<StoreInst>(I)->isAtomic()) |
| 1126 | return false; |
| 1127 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1128 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1129 | MVT VT; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1130 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1131 | return false; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1132 | |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 1133 | // Get the value to be stored into a register. |
| 1134 | SrcReg = getRegForValue(Op0); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1135 | if (SrcReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1136 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 1137 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1138 | Address Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1139 | if (!ARMComputeAddress(I->getOperand(1), Addr)) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1140 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1141 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1142 | if (!ARMEmitStore(VT, SrcReg, Addr)) return false; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { |
| 1147 | switch (Pred) { |
| 1148 | // Needs two compares... |
| 1149 | case CmpInst::FCMP_ONE: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1150 | case CmpInst::FCMP_UEQ: |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1151 | default: |
Eric Christopher | 4053e63 | 2010-11-02 01:24:49 +0000 | [diff] [blame] | 1152 | // AL is our "false" for now. The other two need more compares. |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1153 | return ARMCC::AL; |
| 1154 | case CmpInst::ICMP_EQ: |
| 1155 | case CmpInst::FCMP_OEQ: |
| 1156 | return ARMCC::EQ; |
| 1157 | case CmpInst::ICMP_SGT: |
| 1158 | case CmpInst::FCMP_OGT: |
| 1159 | return ARMCC::GT; |
| 1160 | case CmpInst::ICMP_SGE: |
| 1161 | case CmpInst::FCMP_OGE: |
| 1162 | return ARMCC::GE; |
| 1163 | case CmpInst::ICMP_UGT: |
| 1164 | case CmpInst::FCMP_UGT: |
| 1165 | return ARMCC::HI; |
| 1166 | case CmpInst::FCMP_OLT: |
| 1167 | return ARMCC::MI; |
| 1168 | case CmpInst::ICMP_ULE: |
| 1169 | case CmpInst::FCMP_OLE: |
| 1170 | return ARMCC::LS; |
| 1171 | case CmpInst::FCMP_ORD: |
| 1172 | return ARMCC::VC; |
| 1173 | case CmpInst::FCMP_UNO: |
| 1174 | return ARMCC::VS; |
| 1175 | case CmpInst::FCMP_UGE: |
| 1176 | return ARMCC::PL; |
| 1177 | case CmpInst::ICMP_SLT: |
| 1178 | case CmpInst::FCMP_ULT: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1179 | return ARMCC::LT; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1180 | case CmpInst::ICMP_SLE: |
| 1181 | case CmpInst::FCMP_ULE: |
| 1182 | return ARMCC::LE; |
| 1183 | case CmpInst::FCMP_UNE: |
| 1184 | case CmpInst::ICMP_NE: |
| 1185 | return ARMCC::NE; |
| 1186 | case CmpInst::ICMP_UGE: |
| 1187 | return ARMCC::HS; |
| 1188 | case CmpInst::ICMP_ULT: |
| 1189 | return ARMCC::LO; |
| 1190 | } |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1193 | bool ARMFastISel::SelectBranch(const Instruction *I) { |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1194 | const BranchInst *BI = cast<BranchInst>(I); |
| 1195 | MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 1196 | MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1197 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1198 | // Simple branch support. |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1199 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1200 | // If we can, avoid recomputing the compare - redoing it could lead to wonky |
| 1201 | // behavior. |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1202 | if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
Chad Rosier | 75698f3 | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1203 | if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Get the compare predicate. |
Eric Christopher | 632ae89 | 2011-04-29 21:56:31 +0000 | [diff] [blame] | 1206 | // Try to take advantage of fallthrough opportunities. |
| 1207 | CmpInst::Predicate Predicate = CI->getPredicate(); |
| 1208 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1209 | std::swap(TBB, FBB); |
| 1210 | Predicate = CmpInst::getInversePredicate(Predicate); |
| 1211 | } |
| 1212 | |
| 1213 | ARMCC::CondCodes ARMPred = getComparePred(Predicate); |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1214 | |
| 1215 | // We may not handle every CC for now. |
| 1216 | if (ARMPred == ARMCC::AL) return false; |
| 1217 | |
Chad Rosier | 75698f3 | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1218 | // Emit the compare. |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1219 | if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) |
Chad Rosier | 75698f3 | 2011-10-26 23:17:28 +0000 | [diff] [blame] | 1220 | return false; |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1221 | |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1222 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1223 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
| 1224 | .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); |
| 1225 | FastEmitBranch(FBB, DL); |
| 1226 | FuncInfo.MBB->addSuccessor(TBB); |
| 1227 | return true; |
| 1228 | } |
Eric Christopher | bcf26ae | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1229 | } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { |
| 1230 | MVT SourceVT; |
| 1231 | if (TI->hasOneUse() && TI->getParent() == I->getParent() && |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1232 | (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) { |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1233 | unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; |
Eric Christopher | bcf26ae | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1234 | unsigned OpReg = getRegForValue(TI->getOperand(0)); |
| 1235 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1236 | TII.get(TstOpc)) |
| 1237 | .addReg(OpReg).addImm(1)); |
| 1238 | |
| 1239 | unsigned CCMode = ARMCC::NE; |
| 1240 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1241 | std::swap(TBB, FBB); |
| 1242 | CCMode = ARMCC::EQ; |
| 1243 | } |
| 1244 | |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1245 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Eric Christopher | bcf26ae | 2011-04-29 20:02:39 +0000 | [diff] [blame] | 1246 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
| 1247 | .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); |
| 1248 | |
| 1249 | FastEmitBranch(FBB, DL); |
| 1250 | FuncInfo.MBB->addSuccessor(TBB); |
| 1251 | return true; |
| 1252 | } |
Chad Rosier | 6d64b3a | 2011-10-27 00:21:16 +0000 | [diff] [blame] | 1253 | } else if (const ConstantInt *CI = |
| 1254 | dyn_cast<ConstantInt>(BI->getCondition())) { |
| 1255 | uint64_t Imm = CI->getZExtValue(); |
| 1256 | MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; |
| 1257 | FastEmitBranch(Target, DL); |
| 1258 | return true; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1259 | } |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1260 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1261 | unsigned CmpReg = getRegForValue(BI->getCondition()); |
| 1262 | if (CmpReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1263 | |
Stuart Hastings | c5eecbc | 2011-04-16 03:31:26 +0000 | [diff] [blame] | 1264 | // We've been divorced from our compare! Our block was split, and |
| 1265 | // now our compare lives in a predecessor block. We musn't |
| 1266 | // re-compare here, as the children of the compare aren't guaranteed |
| 1267 | // live across the block boundary (we *could* check for this). |
| 1268 | // Regardless, the compare has been done in the predecessor block, |
| 1269 | // and it left a value for us in a virtual register. Ergo, we test |
| 1270 | // the one-bit value left in the virtual register. |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1271 | unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri; |
Stuart Hastings | c5eecbc | 2011-04-16 03:31:26 +0000 | [diff] [blame] | 1272 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc)) |
| 1273 | .addReg(CmpReg).addImm(1)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1274 | |
Eric Christopher | 7a20a37 | 2011-04-28 16:52:09 +0000 | [diff] [blame] | 1275 | unsigned CCMode = ARMCC::NE; |
| 1276 | if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { |
| 1277 | std::swap(TBB, FBB); |
| 1278 | CCMode = ARMCC::EQ; |
| 1279 | } |
| 1280 | |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1281 | unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1282 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
Eric Christopher | 7a20a37 | 2011-04-28 16:52:09 +0000 | [diff] [blame] | 1283 | .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1284 | FastEmitBranch(FBB, DL); |
| 1285 | FuncInfo.MBB->addSuccessor(TBB); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1286 | return true; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1289 | bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value, |
| 1290 | bool isZExt) { |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1291 | Type *Ty = Src1Value->getType(); |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1292 | EVT SrcVT = TLI.getValueType(Ty, true); |
| 1293 | if (!SrcVT.isSimple()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1294 | |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1295 | bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy()); |
| 1296 | if (isFloat && !Subtarget->hasVFP2()) |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1297 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1298 | |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1299 | // Check to see if the 2nd operand is a constant that we can encode directly |
| 1300 | // in the compare. |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1301 | int Imm = 0; |
| 1302 | bool UseImm = false; |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1303 | bool isNegativeImm = false; |
Chad Rosier | f56c60b | 2011-11-16 00:32:20 +0000 | [diff] [blame] | 1304 | // FIXME: At -O0 we don't have anything that canonicalizes operand order. |
| 1305 | // Thus, Src1Value may be a ConstantInt, but we're missing it. |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1306 | if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) { |
| 1307 | if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 || |
| 1308 | SrcVT == MVT::i1) { |
| 1309 | const APInt &CIVal = ConstInt->getValue(); |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1310 | Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue(); |
| 1311 | if (Imm < 0) { |
Chad Rosier | 6cba97c | 2011-11-10 01:30:39 +0000 | [diff] [blame] | 1312 | isNegativeImm = true; |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1313 | Imm = -Imm; |
Chad Rosier | 6cba97c | 2011-11-10 01:30:39 +0000 | [diff] [blame] | 1314 | } |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1315 | UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
| 1316 | (ARM_AM::getSOImmVal(Imm) != -1); |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) { |
| 1319 | if (SrcVT == MVT::f32 || SrcVT == MVT::f64) |
| 1320 | if (ConstFP->isZero() && !ConstFP->isNegative()) |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1321 | UseImm = true; |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1324 | unsigned CmpOpc; |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1325 | bool isICmp = true; |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1326 | bool needsExt = false; |
| 1327 | switch (SrcVT.getSimpleVT().SimpleTy) { |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1328 | default: return false; |
| 1329 | // TODO: Verify compares. |
| 1330 | case MVT::f32: |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1331 | isICmp = false; |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1332 | CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1333 | break; |
| 1334 | case MVT::f64: |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1335 | isICmp = false; |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1336 | CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1337 | break; |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1338 | case MVT::i1: |
| 1339 | case MVT::i8: |
| 1340 | case MVT::i16: |
| 1341 | needsExt = true; |
| 1342 | // Intentional fall-through. |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1343 | case MVT::i32: |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1344 | if (isThumb2) { |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1345 | if (!UseImm) |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1346 | CmpOpc = ARM::t2CMPrr; |
| 1347 | else |
| 1348 | CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri; |
| 1349 | } else { |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1350 | if (!UseImm) |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1351 | CmpOpc = ARM::CMPrr; |
| 1352 | else |
| 1353 | CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri; |
| 1354 | } |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | } |
| 1357 | |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1358 | unsigned SrcReg1 = getRegForValue(Src1Value); |
| 1359 | if (SrcReg1 == 0) return false; |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1360 | |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1361 | unsigned SrcReg2; |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1362 | if (!UseImm) { |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1363 | SrcReg2 = getRegForValue(Src2Value); |
| 1364 | if (SrcReg2 == 0) return false; |
| 1365 | } |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1366 | |
| 1367 | // We have i1, i8, or i16, we need to either zero extend or sign extend. |
| 1368 | if (needsExt) { |
| 1369 | unsigned ResultReg; |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1370 | ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt); |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1371 | if (ResultReg == 0) return false; |
| 1372 | SrcReg1 = ResultReg; |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1373 | if (!UseImm) { |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1374 | ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt); |
| 1375 | if (ResultReg == 0) return false; |
| 1376 | SrcReg2 = ResultReg; |
| 1377 | } |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1378 | } |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1379 | |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1380 | if (!UseImm) { |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1381 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1382 | TII.get(CmpOpc)) |
| 1383 | .addReg(SrcReg1).addReg(SrcReg2)); |
| 1384 | } else { |
| 1385 | MachineInstrBuilder MIB; |
| 1386 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 1387 | .addReg(SrcReg1); |
| 1388 | |
| 1389 | // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0. |
| 1390 | if (isICmp) |
Chad Rosier | 1c47de8 | 2011-11-11 06:27:41 +0000 | [diff] [blame] | 1391 | MIB.addImm(Imm); |
Chad Rosier | 2f2fe41 | 2011-11-09 03:22:02 +0000 | [diff] [blame] | 1392 | AddOptionalDefs(MIB); |
| 1393 | } |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1394 | |
| 1395 | // For floating point we need to move the result to a comparison register |
| 1396 | // that we can then use for branches. |
| 1397 | if (Ty->isFloatTy() || Ty->isDoubleTy()) |
| 1398 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1399 | TII.get(ARM::FMSTAT))); |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1400 | return true; |
| 1401 | } |
| 1402 | |
| 1403 | bool ARMFastISel::SelectCmp(const Instruction *I) { |
| 1404 | const CmpInst *CI = cast<CmpInst>(I); |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1405 | Type *Ty = CI->getOperand(0)->getType(); |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1406 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1407 | // Get the compare predicate. |
| 1408 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1409 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1410 | // We may not handle every CC for now. |
| 1411 | if (ARMPred == ARMCC::AL) return false; |
| 1412 | |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1413 | // Emit the compare. |
Chad Rosier | e07cd5e | 2011-11-02 18:08:25 +0000 | [diff] [blame] | 1414 | if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1415 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1416 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1417 | // Now set a register based on the comparison. Explicitly set the predicates |
| 1418 | // here. |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1419 | unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; |
| 1420 | TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass |
Eric Christopher | 5d18d92 | 2010-10-07 05:39:19 +0000 | [diff] [blame] | 1421 | : ARM::GPRRegisterClass; |
| 1422 | unsigned DestReg = createResultReg(RC); |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1423 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0); |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1424 | unsigned ZeroReg = TargetMaterializeConstant(Zero); |
Chad Rosier | ade6200 | 2011-10-26 23:25:44 +0000 | [diff] [blame] | 1425 | bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy()); |
Chad Rosier | 530f7ce | 2011-10-26 22:47:55 +0000 | [diff] [blame] | 1426 | unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1427 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg) |
| 1428 | .addReg(ZeroReg).addImm(1) |
| 1429 | .addImm(ARMPred).addReg(CondReg); |
| 1430 | |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1431 | UpdateValueMap(I, DestReg); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1432 | return true; |
| 1433 | } |
| 1434 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1435 | bool ARMFastISel::SelectFPExt(const Instruction *I) { |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1436 | // Make sure we have VFP and that we're extending float to double. |
| 1437 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1438 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1439 | Value *V = I->getOperand(0); |
| 1440 | if (!I->getType()->isDoubleTy() || |
| 1441 | !V->getType()->isFloatTy()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1442 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1443 | unsigned Op = getRegForValue(V); |
| 1444 | if (Op == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1445 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1446 | unsigned Result = createResultReg(ARM::DPRRegisterClass); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1447 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1448 | TII.get(ARM::VCVTDS), Result) |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1449 | .addReg(Op)); |
| 1450 | UpdateValueMap(I, Result); |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1454 | bool ARMFastISel::SelectFPTrunc(const Instruction *I) { |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1455 | // Make sure we have VFP and that we're truncating double to float. |
| 1456 | if (!Subtarget->hasVFP2()) return false; |
| 1457 | |
| 1458 | Value *V = I->getOperand(0); |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1459 | if (!(I->getType()->isFloatTy() && |
| 1460 | V->getType()->isDoubleTy())) return false; |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1461 | |
| 1462 | unsigned Op = getRegForValue(V); |
| 1463 | if (Op == 0) return false; |
| 1464 | |
| 1465 | unsigned Result = createResultReg(ARM::SPRRegisterClass); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1466 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1467 | TII.get(ARM::VCVTSD), Result) |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1468 | .addReg(Op)); |
| 1469 | UpdateValueMap(I, Result); |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1473 | bool ARMFastISel::SelectSIToFP(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1474 | // Make sure we have VFP. |
| 1475 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1476 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1477 | MVT DstVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1478 | Type *Ty = I->getType(); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1479 | if (!isTypeLegal(Ty, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1480 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1481 | |
Chad Rosier | 463fe24 | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1482 | Value *Src = I->getOperand(0); |
| 1483 | EVT SrcVT = TLI.getValueType(Src->getType(), true); |
| 1484 | if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) |
Eli Friedman | 783c664 | 2011-05-25 19:09:45 +0000 | [diff] [blame] | 1485 | return false; |
| 1486 | |
Chad Rosier | 463fe24 | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1487 | unsigned SrcReg = getRegForValue(Src); |
| 1488 | if (SrcReg == 0) return false; |
| 1489 | |
| 1490 | // Handle sign-extension. |
| 1491 | if (SrcVT == MVT::i16 || SrcVT == MVT::i8) { |
| 1492 | EVT DestVT = MVT::i32; |
| 1493 | unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false); |
| 1494 | if (ResultReg == 0) return false; |
| 1495 | SrcReg = ResultReg; |
| 1496 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1497 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1498 | // The conversion routine works on fp-reg to fp-reg and the operand above |
| 1499 | // was an integer, move it to the fp registers if possible. |
Chad Rosier | 463fe24 | 2011-11-03 02:04:59 +0000 | [diff] [blame] | 1500 | unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1501 | if (FP == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1502 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1503 | unsigned Opc; |
| 1504 | if (Ty->isFloatTy()) Opc = ARM::VSITOS; |
| 1505 | else if (Ty->isDoubleTy()) Opc = ARM::VSITOD; |
Chad Rosier | dd1e751 | 2011-08-31 23:49:05 +0000 | [diff] [blame] | 1506 | else return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1507 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1508 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1509 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1510 | ResultReg) |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1511 | .addReg(FP)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1512 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1516 | bool ARMFastISel::SelectFPToSI(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1517 | // Make sure we have VFP. |
| 1518 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1519 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1520 | MVT DstVT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1521 | Type *RetTy = I->getType(); |
Eric Christopher | 920a208 | 2010-09-10 00:35:09 +0000 | [diff] [blame] | 1522 | if (!isTypeLegal(RetTy, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1523 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1524 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1525 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1526 | if (Op == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1527 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1528 | unsigned Opc; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1529 | Type *OpTy = I->getOperand(0)->getType(); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1530 | if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS; |
| 1531 | else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD; |
Chad Rosier | dd1e751 | 2011-08-31 23:49:05 +0000 | [diff] [blame] | 1532 | else return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1533 | |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1534 | // f64->s32 or f32->s32 both need an intermediate f32 reg. |
| 1535 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1536 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1537 | ResultReg) |
| 1538 | .addReg(Op)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1539 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1540 | // This result needs to be in an integer register, but the conversion only |
| 1541 | // takes place in fp-regs. |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1542 | unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1543 | if (IntReg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1544 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1545 | UpdateValueMap(I, IntReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1546 | return true; |
| 1547 | } |
| 1548 | |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1549 | bool ARMFastISel::SelectSelect(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1550 | MVT VT; |
| 1551 | if (!isTypeLegal(I->getType(), VT)) |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1552 | return false; |
| 1553 | |
| 1554 | // Things need to be register sized for register moves. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1555 | if (VT != MVT::i32) return false; |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1556 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 1557 | |
| 1558 | unsigned CondReg = getRegForValue(I->getOperand(0)); |
| 1559 | if (CondReg == 0) return false; |
| 1560 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1561 | if (Op1Reg == 0) return false; |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1562 | |
Chad Rosier | a07d3fc | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1563 | // Check to see if we can use an immediate in the conditional move. |
| 1564 | int Imm = 0; |
| 1565 | bool UseImm = false; |
| 1566 | bool isNegativeImm = false; |
| 1567 | if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) { |
| 1568 | assert (VT == MVT::i32 && "Expecting an i32."); |
| 1569 | Imm = (int)ConstInt->getValue().getZExtValue(); |
| 1570 | if (Imm < 0) { |
| 1571 | isNegativeImm = true; |
| 1572 | Imm = ~Imm; |
| 1573 | } |
| 1574 | UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) : |
| 1575 | (ARM_AM::getSOImmVal(Imm) != -1); |
| 1576 | } |
| 1577 | |
| 1578 | unsigned Op2Reg; |
| 1579 | if (!UseImm) { |
| 1580 | Op2Reg = getRegForValue(I->getOperand(2)); |
| 1581 | if (Op2Reg == 0) return false; |
| 1582 | } |
| 1583 | |
| 1584 | unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri; |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1585 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
Chad Rosier | a07d3fc | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1586 | .addReg(CondReg).addImm(0)); |
| 1587 | |
| 1588 | unsigned MovCCOpc; |
| 1589 | if (!UseImm) { |
| 1590 | MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr; |
| 1591 | } else { |
| 1592 | if (!isNegativeImm) { |
| 1593 | MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi; |
| 1594 | } else { |
| 1595 | MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi; |
| 1596 | } |
| 1597 | } |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1598 | unsigned ResultReg = createResultReg(RC); |
Chad Rosier | a07d3fc | 2011-11-11 06:20:39 +0000 | [diff] [blame] | 1599 | if (!UseImm) |
| 1600 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) |
| 1601 | .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR); |
| 1602 | else |
| 1603 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) |
| 1604 | .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR); |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1605 | UpdateValueMap(I, ResultReg); |
| 1606 | return true; |
| 1607 | } |
| 1608 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1609 | bool ARMFastISel::SelectSDiv(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1610 | MVT VT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1611 | Type *Ty = I->getType(); |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1612 | if (!isTypeLegal(Ty, VT)) |
| 1613 | return false; |
| 1614 | |
| 1615 | // If we have integer div support we should have selected this automagically. |
| 1616 | // In case we have a real miss go ahead and return false and we'll pick |
| 1617 | // it up later. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1618 | if (Subtarget->hasDivide()) return false; |
| 1619 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1620 | // Otherwise emit a libcall. |
| 1621 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Eric Christopher | 7bdc4de | 2010-10-11 08:31:54 +0000 | [diff] [blame] | 1622 | if (VT == MVT::i8) |
| 1623 | LC = RTLIB::SDIV_I8; |
| 1624 | else if (VT == MVT::i16) |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1625 | LC = RTLIB::SDIV_I16; |
| 1626 | else if (VT == MVT::i32) |
| 1627 | LC = RTLIB::SDIV_I32; |
| 1628 | else if (VT == MVT::i64) |
| 1629 | LC = RTLIB::SDIV_I64; |
| 1630 | else if (VT == MVT::i128) |
| 1631 | LC = RTLIB::SDIV_I128; |
| 1632 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1633 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1634 | return ARMEmitLibcall(I, LC); |
| 1635 | } |
| 1636 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1637 | bool ARMFastISel::SelectSRem(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1638 | MVT VT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1639 | Type *Ty = I->getType(); |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1640 | if (!isTypeLegal(Ty, VT)) |
| 1641 | return false; |
| 1642 | |
| 1643 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 1644 | if (VT == MVT::i8) |
| 1645 | LC = RTLIB::SREM_I8; |
| 1646 | else if (VT == MVT::i16) |
| 1647 | LC = RTLIB::SREM_I16; |
| 1648 | else if (VT == MVT::i32) |
| 1649 | LC = RTLIB::SREM_I32; |
| 1650 | else if (VT == MVT::i64) |
| 1651 | LC = RTLIB::SREM_I64; |
| 1652 | else if (VT == MVT::i128) |
| 1653 | LC = RTLIB::SREM_I128; |
Eric Christopher | a1640d9 | 2010-10-11 08:40:05 +0000 | [diff] [blame] | 1654 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1655 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1656 | return ARMEmitLibcall(I, LC); |
| 1657 | } |
| 1658 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1659 | bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) { |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1660 | EVT VT = TLI.getValueType(I->getType(), true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1661 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1662 | // We can get here in the case when we want to use NEON for our fp |
| 1663 | // operations, but can't figure out how to. Just use the vfp instructions |
| 1664 | // if we have them. |
| 1665 | // FIXME: It'd be nice to use NEON instructions. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1666 | Type *Ty = I->getType(); |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1667 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1668 | if (isFloat && !Subtarget->hasVFP2()) |
| 1669 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1670 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1671 | unsigned Opc; |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 1672 | bool is64bit = VT == MVT::f64 || VT == MVT::i64; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1673 | switch (ISDOpcode) { |
| 1674 | default: return false; |
| 1675 | case ISD::FADD: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1676 | Opc = is64bit ? ARM::VADDD : ARM::VADDS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | case ISD::FSUB: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1679 | Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1680 | break; |
| 1681 | case ISD::FMUL: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1682 | Opc = is64bit ? ARM::VMULD : ARM::VMULS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | } |
Chad Rosier | 508a1f4 | 2011-11-16 18:39:44 +0000 | [diff] [blame] | 1685 | unsigned Op1 = getRegForValue(I->getOperand(0)); |
| 1686 | if (Op1 == 0) return false; |
| 1687 | |
| 1688 | unsigned Op2 = getRegForValue(I->getOperand(1)); |
| 1689 | if (Op2 == 0) return false; |
| 1690 | |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1691 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1692 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1693 | TII.get(Opc), ResultReg) |
| 1694 | .addReg(Op1).addReg(Op2)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1695 | UpdateValueMap(I, ResultReg); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1696 | return true; |
| 1697 | } |
| 1698 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1699 | // Call Handling Code |
| 1700 | |
| 1701 | // This is largely taken directly from CCAssignFnForNode - we don't support |
| 1702 | // varargs in FastISel so that part has been removed. |
| 1703 | // TODO: We may not support all of this. |
| 1704 | CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) { |
| 1705 | switch (CC) { |
| 1706 | default: |
| 1707 | llvm_unreachable("Unsupported calling convention"); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1708 | case CallingConv::Fast: |
Evan Cheng | 1f8b40d | 2010-10-22 18:57:05 +0000 | [diff] [blame] | 1709 | // Ignore fastcc. Silence compiler warnings. |
| 1710 | (void)RetFastCC_ARM_APCS; |
| 1711 | (void)FastCC_ARM_APCS; |
| 1712 | // Fallthrough |
| 1713 | case CallingConv::C: |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1714 | // Use target triple & subtarget features to do actual dispatch. |
| 1715 | if (Subtarget->isAAPCS_ABI()) { |
| 1716 | if (Subtarget->hasVFP2() && |
| 1717 | FloatABIType == FloatABI::Hard) |
| 1718 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1719 | else |
| 1720 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1721 | } else |
| 1722 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1723 | case CallingConv::ARM_AAPCS_VFP: |
| 1724 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1725 | case CallingConv::ARM_AAPCS: |
| 1726 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1727 | case CallingConv::ARM_APCS: |
| 1728 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1729 | } |
| 1730 | } |
| 1731 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1732 | bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
| 1733 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1734 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1735 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 1736 | SmallVectorImpl<unsigned> &RegArgs, |
| 1737 | CallingConv::ID CC, |
| 1738 | unsigned &NumBytes) { |
| 1739 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1740 | CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1741 | CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false)); |
| 1742 | |
| 1743 | // Get a count of how many bytes are to be pushed on the stack. |
| 1744 | NumBytes = CCInfo.getNextStackOffset(); |
| 1745 | |
| 1746 | // Issue CALLSEQ_START |
Evan Cheng | d5b03f2 | 2011-06-28 21:14:33 +0000 | [diff] [blame] | 1747 | unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1748 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1749 | TII.get(AdjStackDown)) |
| 1750 | .addImm(NumBytes)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1751 | |
| 1752 | // Process the args. |
| 1753 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1754 | CCValAssign &VA = ArgLocs[i]; |
| 1755 | unsigned Arg = ArgRegs[VA.getValNo()]; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1756 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1757 | |
Eric Christopher | 4a2b316 | 2011-01-27 05:44:56 +0000 | [diff] [blame] | 1758 | // We don't handle NEON/vector parameters yet. |
| 1759 | if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) |
Eric Christopher | a4633f5 | 2010-10-23 09:37:17 +0000 | [diff] [blame] | 1760 | return false; |
| 1761 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1762 | // Handle arg promotion, etc. |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1763 | switch (VA.getLocInfo()) { |
| 1764 | case CCValAssign::Full: break; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1765 | case CCValAssign::SExt: { |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 1766 | EVT DestVT = VA.getLocVT(); |
| 1767 | unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT, |
| 1768 | /*isZExt*/false); |
| 1769 | assert (ResultReg != 0 && "Failed to emit a sext"); |
| 1770 | Arg = ResultReg; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | } |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 1773 | case CCValAssign::AExt: |
| 1774 | // Intentional fall-through. Handle AExt and ZExt. |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1775 | case CCValAssign::ZExt: { |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 1776 | EVT DestVT = VA.getLocVT(); |
| 1777 | unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT, |
| 1778 | /*isZExt*/true); |
| 1779 | assert (ResultReg != 0 && "Failed to emit a sext"); |
| 1780 | Arg = ResultReg; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1781 | break; |
| 1782 | } |
| 1783 | case CCValAssign::BCvt: { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1784 | unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1785 | /*TODO: Kill=*/false); |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1786 | assert(BC != 0 && "Failed to emit a bitcast!"); |
| 1787 | Arg = BC; |
| 1788 | ArgVT = VA.getLocVT(); |
| 1789 | break; |
| 1790 | } |
| 1791 | default: llvm_unreachable("Unknown arg promotion!"); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | // Now copy/store arg to correct locations. |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1795 | if (VA.isRegLoc() && !VA.needsCustom()) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1796 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1797 | VA.getLocReg()) |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 1798 | .addReg(Arg); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1799 | RegArgs.push_back(VA.getLocReg()); |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1800 | } else if (VA.needsCustom()) { |
| 1801 | // TODO: We need custom lowering for vector (v2f64) args. |
| 1802 | if (VA.getLocVT() != MVT::f64) return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1803 | |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1804 | CCValAssign &NextVA = ArgLocs[++i]; |
| 1805 | |
| 1806 | // TODO: Only handle register args for now. |
| 1807 | if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false; |
| 1808 | |
| 1809 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1810 | TII.get(ARM::VMOVRRD), VA.getLocReg()) |
| 1811 | .addReg(NextVA.getLocReg(), RegState::Define) |
| 1812 | .addReg(Arg)); |
| 1813 | RegArgs.push_back(VA.getLocReg()); |
| 1814 | RegArgs.push_back(NextVA.getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1815 | } else { |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1816 | assert(VA.isMemLoc()); |
| 1817 | // Need to store on the stack. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1818 | Address Addr; |
| 1819 | Addr.BaseType = Address::RegBase; |
| 1820 | Addr.Base.Reg = ARM::SP; |
| 1821 | Addr.Offset = VA.getLocMemOffset(); |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1822 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1823 | if (!ARMEmitStore(ArgVT, Arg, Addr)) return false; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1824 | } |
| 1825 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1826 | return true; |
| 1827 | } |
| 1828 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1829 | bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1830 | const Instruction *I, CallingConv::ID CC, |
| 1831 | unsigned &NumBytes) { |
| 1832 | // Issue CALLSEQ_END |
Evan Cheng | d5b03f2 | 2011-06-28 21:14:33 +0000 | [diff] [blame] | 1833 | unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1834 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1835 | TII.get(AdjStackUp)) |
| 1836 | .addImm(NumBytes).addImm(0)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1837 | |
| 1838 | // Now the return value. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1839 | if (RetVT != MVT::isVoid) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1840 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1841 | CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1842 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true)); |
| 1843 | |
| 1844 | // Copy all of the result registers out of their specified physreg. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1845 | if (RVLocs.size() == 2 && RetVT == MVT::f64) { |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1846 | // For this move we copy into two registers and then move into the |
| 1847 | // double fp reg we want. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1848 | EVT DestVT = RVLocs[0].getValVT(); |
| 1849 | TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); |
| 1850 | unsigned ResultReg = createResultReg(DstRC); |
| 1851 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1852 | TII.get(ARM::VMOVDRR), ResultReg) |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1853 | .addReg(RVLocs[0].getLocReg()) |
| 1854 | .addReg(RVLocs[1].getLocReg())); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1855 | |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1856 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
| 1857 | UsedRegs.push_back(RVLocs[1].getLocReg()); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1858 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1859 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1860 | UpdateValueMap(I, ResultReg); |
| 1861 | } else { |
Jim Grosbach | 9536959 | 2010-10-13 23:34:31 +0000 | [diff] [blame] | 1862 | assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1863 | EVT CopyVT = RVLocs[0].getValVT(); |
Chad Rosier | 0eff39f | 2011-11-08 00:03:32 +0000 | [diff] [blame] | 1864 | |
| 1865 | // Special handling for extended integers. |
| 1866 | if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) |
| 1867 | CopyVT = MVT::i32; |
| 1868 | |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1869 | TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1870 | |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1871 | unsigned ResultReg = createResultReg(DstRC); |
| 1872 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1873 | ResultReg).addReg(RVLocs[0].getLocReg()); |
| 1874 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1875 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1876 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1877 | UpdateValueMap(I, ResultReg); |
| 1878 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1881 | return true; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1884 | bool ARMFastISel::SelectRet(const Instruction *I) { |
| 1885 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 1886 | const Function &F = *I->getParent()->getParent(); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1887 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1888 | if (!FuncInfo.CanLowerReturn) |
| 1889 | return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1890 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1891 | if (F.isVarArg()) |
| 1892 | return false; |
| 1893 | |
| 1894 | CallingConv::ID CC = F.getCallingConv(); |
| 1895 | if (Ret->getNumOperands() > 0) { |
| 1896 | SmallVector<ISD::OutputArg, 4> Outs; |
| 1897 | GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
| 1898 | Outs, TLI); |
| 1899 | |
| 1900 | // Analyze operands of the call, assigning locations to each operand. |
| 1901 | SmallVector<CCValAssign, 16> ValLocs; |
Jim Grosbach | b04546f | 2011-09-13 20:30:37 +0000 | [diff] [blame] | 1902 | CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext()); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1903 | CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */)); |
| 1904 | |
| 1905 | const Value *RV = Ret->getOperand(0); |
| 1906 | unsigned Reg = getRegForValue(RV); |
| 1907 | if (Reg == 0) |
| 1908 | return false; |
| 1909 | |
| 1910 | // Only handle a single return value for now. |
| 1911 | if (ValLocs.size() != 1) |
| 1912 | return false; |
| 1913 | |
| 1914 | CCValAssign &VA = ValLocs[0]; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1915 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1916 | // Don't bother handling odd stuff for now. |
| 1917 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1918 | return false; |
| 1919 | // Only handle register returns for now. |
| 1920 | if (!VA.isRegLoc()) |
| 1921 | return false; |
Chad Rosier | f470cbb | 2011-11-04 00:50:21 +0000 | [diff] [blame] | 1922 | |
| 1923 | unsigned SrcReg = Reg + VA.getValNo(); |
| 1924 | EVT RVVT = TLI.getValueType(RV->getType()); |
| 1925 | EVT DestVT = VA.getValVT(); |
| 1926 | // Special handling for extended integers. |
| 1927 | if (RVVT != DestVT) { |
| 1928 | if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) |
| 1929 | return false; |
| 1930 | |
| 1931 | if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt()) |
| 1932 | return false; |
| 1933 | |
| 1934 | assert(DestVT == MVT::i32 && "ARM should always ext to i32"); |
| 1935 | |
| 1936 | bool isZExt = Outs[0].Flags.isZExt(); |
| 1937 | unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt); |
| 1938 | if (ResultReg == 0) return false; |
| 1939 | SrcReg = ResultReg; |
| 1940 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1941 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1942 | // Make the copy. |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1943 | unsigned DstReg = VA.getLocReg(); |
| 1944 | const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); |
| 1945 | // Avoid a cross-class copy. This is very unlikely. |
| 1946 | if (!SrcRC->contains(DstReg)) |
| 1947 | return false; |
| 1948 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1949 | DstReg).addReg(SrcReg); |
| 1950 | |
| 1951 | // Mark the register as live out of the function. |
| 1952 | MRI.addLiveOut(VA.getLocReg()); |
| 1953 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1954 | |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1955 | unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET; |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1956 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1957 | TII.get(RetOpc))); |
| 1958 | return true; |
| 1959 | } |
| 1960 | |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1961 | unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) { |
| 1962 | |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1963 | // Darwin needs the r9 versions of the opcodes. |
| 1964 | bool isDarwin = Subtarget->isTargetDarwin(); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 1965 | if (isThumb2) { |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1966 | return isDarwin ? ARM::tBLr9 : ARM::tBL; |
| 1967 | } else { |
| 1968 | return isDarwin ? ARM::BLr9 : ARM::BL; |
| 1969 | } |
| 1970 | } |
| 1971 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1972 | // A quick function that will emit a call for a named libcall in F with the |
| 1973 | // vector of passed arguments for the Instruction in I. We can assume that we |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1974 | // can emit a call for any libcall we can produce. This is an abridged version |
| 1975 | // of the full call infrastructure since we won't need to worry about things |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1976 | // like computed function pointers or strange arguments at call sites. |
| 1977 | // TODO: Try to unify this and the normal call bits for ARM, then try to unify |
| 1978 | // with X86. |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1979 | bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { |
| 1980 | CallingConv::ID CC = TLI.getLibcallCallingConv(Call); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1981 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1982 | // Handle *simple* calls for now. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1983 | Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1984 | MVT RetVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1985 | if (RetTy->isVoidTy()) |
| 1986 | RetVT = MVT::isVoid; |
| 1987 | else if (!isTypeLegal(RetTy, RetVT)) |
| 1988 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1989 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 1990 | // TODO: For now if we have long calls specified we don't handle the call. |
| 1991 | if (EnableARMLongCalls) return false; |
| 1992 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1993 | // Set up the argument vectors. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1994 | SmallVector<Value*, 8> Args; |
| 1995 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1996 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1997 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 1998 | Args.reserve(I->getNumOperands()); |
| 1999 | ArgRegs.reserve(I->getNumOperands()); |
| 2000 | ArgVTs.reserve(I->getNumOperands()); |
| 2001 | ArgFlags.reserve(I->getNumOperands()); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 2002 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2003 | Value *Op = I->getOperand(i); |
| 2004 | unsigned Arg = getRegForValue(Op); |
| 2005 | if (Arg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2007 | Type *ArgTy = Op->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2008 | MVT ArgVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2009 | if (!isTypeLegal(ArgTy, ArgVT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2010 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2011 | ISD::ArgFlagsTy Flags; |
| 2012 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 2013 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2014 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2015 | Args.push_back(Op); |
| 2016 | ArgRegs.push_back(Arg); |
| 2017 | ArgVTs.push_back(ArgVT); |
| 2018 | ArgFlags.push_back(Flags); |
| 2019 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2020 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2021 | // Handle the arguments now that we've gotten them. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2022 | SmallVector<unsigned, 4> RegArgs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2023 | unsigned NumBytes; |
| 2024 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 2025 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2026 | |
Eric Christopher | 6344a5f | 2011-04-29 00:07:20 +0000 | [diff] [blame] | 2027 | // Issue the call, BLr9 for darwin, BL otherwise. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2028 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2029 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 2030 | unsigned CallOpc = ARMSelectCallOp(NULL); |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2031 | if(isThumb2) |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 2032 | // Explicitly adding the predicate here. |
| 2033 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 2034 | TII.get(CallOpc))) |
| 2035 | .addExternalSymbol(TLI.getLibcallName(Call)); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 2036 | else |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 2037 | // Explicitly adding the predicate here. |
| 2038 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 2039 | TII.get(CallOpc)) |
| 2040 | .addExternalSymbol(TLI.getLibcallName(Call))); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2041 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2042 | // Add implicit physical register uses to the call. |
| 2043 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 2044 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2045 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2046 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2047 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 2048 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2049 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2050 | // Set all unused physreg defs as dead. |
| 2051 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2052 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2053 | return true; |
| 2054 | } |
| 2055 | |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2056 | bool ARMFastISel::SelectCall(const Instruction *I, |
| 2057 | const char *IntrMemName = 0) { |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2058 | const CallInst *CI = cast<CallInst>(I); |
| 2059 | const Value *Callee = CI->getCalledValue(); |
| 2060 | |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2061 | // Can't handle inline asm. |
| 2062 | if (isa<InlineAsm>(Callee)) return false; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2063 | |
Eric Christopher | 52f6c03 | 2011-05-02 20:16:33 +0000 | [diff] [blame] | 2064 | // Only handle global variable Callees. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2065 | const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); |
Eric Christopher | 52f6c03 | 2011-05-02 20:16:33 +0000 | [diff] [blame] | 2066 | if (!GV) |
Eric Christopher | e6ca677 | 2010-10-01 21:33:12 +0000 | [diff] [blame] | 2067 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2068 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2069 | // Check the calling convention. |
| 2070 | ImmutableCallSite CS(CI); |
| 2071 | CallingConv::ID CC = CS.getCallingConv(); |
Eric Christopher | 4cf34c6 | 2010-10-18 06:49:12 +0000 | [diff] [blame] | 2072 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2073 | // TODO: Avoid some calling conventions? |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2074 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2075 | // Let SDISel handle vararg functions. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2076 | PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 2077 | FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2078 | if (FTy->isVarArg()) |
| 2079 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2080 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2081 | // Handle *simple* calls for now. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2082 | Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2083 | MVT RetVT; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2084 | if (RetTy->isVoidTy()) |
| 2085 | RetVT = MVT::isVoid; |
Chad Rosier | 0eff39f | 2011-11-08 00:03:32 +0000 | [diff] [blame] | 2086 | else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && |
| 2087 | RetVT != MVT::i8 && RetVT != MVT::i1) |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2088 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2089 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 2090 | // TODO: For now if we have long calls specified we don't handle the call. |
| 2091 | if (EnableARMLongCalls) return false; |
Eric Christopher | 299bbb2 | 2011-04-29 00:03:10 +0000 | [diff] [blame] | 2092 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2093 | // Set up the argument vectors. |
| 2094 | SmallVector<Value*, 8> Args; |
| 2095 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2096 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2097 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 2098 | Args.reserve(CS.arg_size()); |
| 2099 | ArgRegs.reserve(CS.arg_size()); |
| 2100 | ArgVTs.reserve(CS.arg_size()); |
| 2101 | ArgFlags.reserve(CS.arg_size()); |
| 2102 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 2103 | i != e; ++i) { |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2104 | // If we're lowering a memory intrinsic instead of a regular call, skip the |
| 2105 | // last two arguments, which shouldn't be passed to the underlying function. |
| 2106 | if (IntrMemName && e-i <= 2) |
| 2107 | break; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2108 | |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2109 | unsigned Arg = getRegForValue(*i); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2110 | if (Arg == 0) |
| 2111 | return false; |
| 2112 | ISD::ArgFlagsTy Flags; |
| 2113 | unsigned AttrInd = i - CS.arg_begin() + 1; |
| 2114 | if (CS.paramHasAttr(AttrInd, Attribute::SExt)) |
| 2115 | Flags.setSExt(); |
| 2116 | if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) |
| 2117 | Flags.setZExt(); |
| 2118 | |
Chad Rosier | 8e4a2e4 | 2011-11-04 00:58:10 +0000 | [diff] [blame] | 2119 | // FIXME: Only handle *easy* calls for now. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2120 | if (CS.paramHasAttr(AttrInd, Attribute::InReg) || |
| 2121 | CS.paramHasAttr(AttrInd, Attribute::StructRet) || |
| 2122 | CS.paramHasAttr(AttrInd, Attribute::Nest) || |
| 2123 | CS.paramHasAttr(AttrInd, Attribute::ByVal)) |
| 2124 | return false; |
| 2125 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2126 | Type *ArgTy = (*i)->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 2127 | MVT ArgVT; |
Chad Rosier | 42536af | 2011-11-05 20:16:15 +0000 | [diff] [blame] | 2128 | if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 && |
| 2129 | ArgVT != MVT::i1) |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2130 | return false; |
| 2131 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 2132 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2133 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2134 | Args.push_back(*i); |
| 2135 | ArgRegs.push_back(Arg); |
| 2136 | ArgVTs.push_back(ArgVT); |
| 2137 | ArgFlags.push_back(Flags); |
| 2138 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2139 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2140 | // Handle the arguments now that we've gotten them. |
| 2141 | SmallVector<unsigned, 4> RegArgs; |
| 2142 | unsigned NumBytes; |
| 2143 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 2144 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2145 | |
Eric Christopher | 6344a5f | 2011-04-29 00:07:20 +0000 | [diff] [blame] | 2146 | // Issue the call, BLr9 for darwin, BL otherwise. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2147 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2148 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 2149 | unsigned CallOpc = ARMSelectCallOp(GV); |
Eric Christopher | 7bb5996 | 2010-11-29 21:56:23 +0000 | [diff] [blame] | 2150 | // Explicitly adding the predicate here. |
Chad Rosier | 9eb6748 | 2011-11-13 09:44:21 +0000 | [diff] [blame] | 2151 | if(isThumb2) { |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 2152 | // Explicitly adding the predicate here. |
| 2153 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2154 | TII.get(CallOpc))); |
Chad Rosier | 9eb6748 | 2011-11-13 09:44:21 +0000 | [diff] [blame] | 2155 | if (!IntrMemName) |
| 2156 | MIB.addGlobalAddress(GV, 0, 0); |
| 2157 | else |
| 2158 | MIB.addExternalSymbol(IntrMemName, 0); |
| 2159 | } else { |
| 2160 | if (!IntrMemName) |
| 2161 | // Explicitly adding the predicate here. |
| 2162 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 2163 | TII.get(CallOpc)) |
| 2164 | .addGlobalAddress(GV, 0, 0)); |
| 2165 | else |
| 2166 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 2167 | TII.get(CallOpc)) |
| 2168 | .addExternalSymbol(IntrMemName, 0)); |
| 2169 | } |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2170 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2171 | // Add implicit physical register uses to the call. |
| 2172 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 2173 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2174 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2175 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2176 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2177 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2178 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2179 | // Set all unused physreg defs as dead. |
| 2180 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 2181 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2182 | return true; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2185 | bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) { |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2186 | return Len <= 16; |
| 2187 | } |
| 2188 | |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2189 | bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) { |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2190 | // Make sure we don't bloat code by inlining very large memcpy's. |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2191 | if (!ARMIsMemCpySmall(Len)) |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2192 | return false; |
| 2193 | |
| 2194 | // We don't care about alignment here since we just emit integer accesses. |
| 2195 | while (Len) { |
| 2196 | MVT VT; |
| 2197 | if (Len >= 4) |
| 2198 | VT = MVT::i32; |
| 2199 | else if (Len >= 2) |
| 2200 | VT = MVT::i16; |
| 2201 | else { |
| 2202 | assert(Len == 1); |
| 2203 | VT = MVT::i8; |
| 2204 | } |
| 2205 | |
| 2206 | bool RV; |
| 2207 | unsigned ResultReg; |
| 2208 | RV = ARMEmitLoad(VT, ResultReg, Src); |
| 2209 | assert (RV = true && "Should be able to handle this load."); |
| 2210 | RV = ARMEmitStore(VT, ResultReg, Dest); |
| 2211 | assert (RV = true && "Should be able to handle this store."); |
| 2212 | |
| 2213 | unsigned Size = VT.getSizeInBits()/8; |
| 2214 | Len -= Size; |
| 2215 | Dest.Offset += Size; |
| 2216 | Src.Offset += Size; |
| 2217 | } |
| 2218 | |
| 2219 | return true; |
| 2220 | } |
| 2221 | |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2222 | bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) { |
| 2223 | // FIXME: Handle more intrinsics. |
| 2224 | switch (I.getIntrinsicID()) { |
| 2225 | default: return false; |
| 2226 | case Intrinsic::memcpy: |
| 2227 | case Intrinsic::memmove: { |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2228 | const MemTransferInst &MTI = cast<MemTransferInst>(I); |
| 2229 | // Don't handle volatile. |
| 2230 | if (MTI.isVolatile()) |
| 2231 | return false; |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Disable inlining for memmove before calls to ComputeAddress. Otherwise, |
| 2234 | // we would emit dead code because we don't currently handle memmoves. |
| 2235 | bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy); |
| 2236 | if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) { |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2237 | // Small memcpy's are common enough that we want to do them without a call |
| 2238 | // if possible. |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2239 | uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue(); |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2240 | if (ARMIsMemCpySmall(Len)) { |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2241 | Address Dest, Src; |
| 2242 | if (!ARMComputeAddress(MTI.getRawDest(), Dest) || |
| 2243 | !ARMComputeAddress(MTI.getRawSource(), Src)) |
| 2244 | return false; |
Chad Rosier | 2c42b8c | 2011-11-14 23:04:09 +0000 | [diff] [blame] | 2245 | if (ARMTryEmitSmallMemCpy(Dest, Src, Len)) |
Chad Rosier | 909cb4f | 2011-11-14 22:46:17 +0000 | [diff] [blame] | 2246 | return true; |
| 2247 | } |
| 2248 | } |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2249 | |
| 2250 | if (!MTI.getLength()->getType()->isIntegerTy(32)) |
| 2251 | return false; |
| 2252 | |
| 2253 | if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255) |
| 2254 | return false; |
| 2255 | |
| 2256 | const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove"; |
| 2257 | return SelectCall(&I, IntrMemName); |
| 2258 | } |
| 2259 | case Intrinsic::memset: { |
| 2260 | const MemSetInst &MSI = cast<MemSetInst>(I); |
| 2261 | // Don't handle volatile. |
| 2262 | if (MSI.isVolatile()) |
| 2263 | return false; |
| 2264 | |
| 2265 | if (!MSI.getLength()->getType()->isIntegerTy(32)) |
| 2266 | return false; |
| 2267 | |
| 2268 | if (MSI.getDestAddressSpace() > 255) |
| 2269 | return false; |
| 2270 | |
| 2271 | return SelectCall(&I, "memset"); |
| 2272 | } |
| 2273 | } |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
Chad Rosier | 0d7b231 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2277 | bool ARMFastISel::SelectTrunc(const Instruction *I) { |
| 2278 | // The high bits for a type smaller than the register size are assumed to be |
| 2279 | // undefined. |
| 2280 | Value *Op = I->getOperand(0); |
| 2281 | |
| 2282 | EVT SrcVT, DestVT; |
| 2283 | SrcVT = TLI.getValueType(Op->getType(), true); |
| 2284 | DestVT = TLI.getValueType(I->getType(), true); |
| 2285 | |
| 2286 | if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) |
| 2287 | return false; |
| 2288 | if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) |
| 2289 | return false; |
| 2290 | |
| 2291 | unsigned SrcReg = getRegForValue(Op); |
| 2292 | if (!SrcReg) return false; |
| 2293 | |
| 2294 | // Because the high bits are undefined, a truncate doesn't generate |
| 2295 | // any code. |
| 2296 | UpdateValueMap(I, SrcReg); |
| 2297 | return true; |
| 2298 | } |
| 2299 | |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2300 | unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, |
| 2301 | bool isZExt) { |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2302 | if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2303 | return 0; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2304 | |
| 2305 | unsigned Opc; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2306 | bool isBoolZext = false; |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2307 | if (!SrcVT.isSimple()) return 0; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2308 | switch (SrcVT.getSimpleVT().SimpleTy) { |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2309 | default: return 0; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2310 | case MVT::i16: |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2311 | if (!Subtarget->hasV6Ops()) return 0; |
| 2312 | if (isZExt) |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2313 | Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2314 | else |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2315 | Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2316 | break; |
| 2317 | case MVT::i8: |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2318 | if (!Subtarget->hasV6Ops()) return 0; |
| 2319 | if (isZExt) |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2320 | Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2321 | else |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2322 | Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2323 | break; |
| 2324 | case MVT::i1: |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2325 | if (isZExt) { |
Chad Rosier | 66dc8ca | 2011-11-08 21:12:00 +0000 | [diff] [blame] | 2326 | Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2327 | isBoolZext = true; |
| 2328 | break; |
| 2329 | } |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2330 | return 0; |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2333 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32)); |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2334 | MachineInstrBuilder MIB; |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2335 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg) |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2336 | .addReg(SrcReg); |
| 2337 | if (isBoolZext) |
| 2338 | MIB.addImm(1); |
Jim Grosbach | c5a8c86 | 2011-07-27 16:47:19 +0000 | [diff] [blame] | 2339 | else |
| 2340 | MIB.addImm(0); |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2341 | AddOptionalDefs(MIB); |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2342 | return ResultReg; |
| 2343 | } |
| 2344 | |
| 2345 | bool ARMFastISel::SelectIntExt(const Instruction *I) { |
| 2346 | // On ARM, in general, integer casts don't involve legal types; this code |
| 2347 | // handles promotable integers. |
Chad Rosier | 8763302 | 2011-11-02 17:20:24 +0000 | [diff] [blame] | 2348 | Type *DestTy = I->getType(); |
| 2349 | Value *Src = I->getOperand(0); |
| 2350 | Type *SrcTy = Src->getType(); |
| 2351 | |
| 2352 | EVT SrcVT, DestVT; |
| 2353 | SrcVT = TLI.getValueType(SrcTy, true); |
| 2354 | DestVT = TLI.getValueType(DestTy, true); |
| 2355 | |
| 2356 | bool isZExt = isa<ZExtInst>(I); |
| 2357 | unsigned SrcReg = getRegForValue(Src); |
| 2358 | if (!SrcReg) return false; |
| 2359 | |
| 2360 | unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt); |
| 2361 | if (ResultReg == 0) return false; |
| 2362 | UpdateValueMap(I, ResultReg); |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2363 | return true; |
| 2364 | } |
| 2365 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 2366 | // TODO: SoftFP support. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2367 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 2368 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2369 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 2370 | case Instruction::Load: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2371 | return SelectLoad(I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 2372 | case Instruction::Store: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2373 | return SelectStore(I); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 2374 | case Instruction::Br: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2375 | return SelectBranch(I); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 2376 | case Instruction::ICmp: |
| 2377 | case Instruction::FCmp: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2378 | return SelectCmp(I); |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 2379 | case Instruction::FPExt: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2380 | return SelectFPExt(I); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 2381 | case Instruction::FPTrunc: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2382 | return SelectFPTrunc(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 2383 | case Instruction::SIToFP: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2384 | return SelectSIToFP(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 2385 | case Instruction::FPToSI: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2386 | return SelectFPToSI(I); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2387 | case Instruction::FAdd: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2388 | return SelectBinaryOp(I, ISD::FADD); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2389 | case Instruction::FSub: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2390 | return SelectBinaryOp(I, ISD::FSUB); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 2391 | case Instruction::FMul: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2392 | return SelectBinaryOp(I, ISD::FMUL); |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 2393 | case Instruction::SDiv: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 2394 | return SelectSDiv(I); |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 2395 | case Instruction::SRem: |
| 2396 | return SelectSRem(I); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2397 | case Instruction::Call: |
Chad Rosier | 11add26 | 2011-11-11 23:31:03 +0000 | [diff] [blame] | 2398 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 2399 | return SelectIntrinsicCall(*II); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 2400 | return SelectCall(I); |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 2401 | case Instruction::Select: |
| 2402 | return SelectSelect(I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 2403 | case Instruction::Ret: |
| 2404 | return SelectRet(I); |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2405 | case Instruction::Trunc: |
Chad Rosier | 0d7b231 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2406 | return SelectTrunc(I); |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 2407 | case Instruction::ZExt: |
| 2408 | case Instruction::SExt: |
Chad Rosier | 0d7b231 | 2011-11-02 00:18:48 +0000 | [diff] [blame] | 2409 | return SelectIntExt(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2410 | default: break; |
| 2411 | } |
| 2412 | return false; |
| 2413 | } |
| 2414 | |
Chad Rosier | b29b950 | 2011-11-13 02:23:59 +0000 | [diff] [blame] | 2415 | /// TryToFoldLoad - The specified machine instr operand is a vreg, and that |
| 2416 | /// vreg is being provided by the specified load instruction. If possible, |
| 2417 | /// try to fold the load as an operand to the instruction, returning true if |
| 2418 | /// successful. |
| 2419 | bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo, |
| 2420 | const LoadInst *LI) { |
| 2421 | // Verify we have a legal type before going any further. |
| 2422 | MVT VT; |
| 2423 | if (!isLoadTypeLegal(LI->getType(), VT)) |
| 2424 | return false; |
| 2425 | |
| 2426 | // Combine load followed by zero- or sign-extend. |
| 2427 | // ldrb r1, [r0] ldrb r1, [r0] |
| 2428 | // uxtb r2, r1 => |
| 2429 | // mov r3, r2 mov r3, r1 |
| 2430 | bool isZExt = true; |
| 2431 | switch(MI->getOpcode()) { |
| 2432 | default: return false; |
| 2433 | case ARM::SXTH: |
| 2434 | case ARM::t2SXTH: |
| 2435 | isZExt = false; |
| 2436 | case ARM::UXTH: |
| 2437 | case ARM::t2UXTH: |
| 2438 | if (VT != MVT::i16) |
| 2439 | return false; |
| 2440 | break; |
| 2441 | case ARM::SXTB: |
| 2442 | case ARM::t2SXTB: |
| 2443 | isZExt = false; |
| 2444 | case ARM::UXTB: |
| 2445 | case ARM::t2UXTB: |
| 2446 | if (VT != MVT::i8) |
| 2447 | return false; |
| 2448 | break; |
| 2449 | } |
| 2450 | // See if we can handle this address. |
| 2451 | Address Addr; |
| 2452 | if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false; |
| 2453 | |
| 2454 | unsigned ResultReg = MI->getOperand(0).getReg(); |
| 2455 | if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false)) |
| 2456 | return false; |
| 2457 | MI->eraseFromParent(); |
| 2458 | return true; |
| 2459 | } |
| 2460 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2461 | namespace llvm { |
| 2462 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 2463 | // Completely untested on non-darwin. |
| 2464 | const TargetMachine &TM = funcInfo.MF->getTarget(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 2465 | |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 2466 | // Darwin and thumb1 only for now. |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 2467 | const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 2468 | if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() && |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 2469 | !DisableARMFastISel) |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 2470 | return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 2471 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 2472 | } |
| 2473 | } |