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