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