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