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" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/DerivedTypes.h" |
| 25 | #include "llvm/GlobalVariable.h" |
| 26 | #include "llvm/Instructions.h" |
| 27 | #include "llvm/IntrinsicInst.h" |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/Analysis.h" |
| 30 | #include "llvm/CodeGen/FastISel.h" |
| 31 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 33 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 35 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eric Christopher | d56d61a | 2010-10-17 01:51:42 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | d56d61a | 2010-10-17 01:51:42 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CallSite.h" |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
| 44 | #include "llvm/Target/TargetInstrInfo.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
| 46 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetOptions.h" |
| 48 | using namespace llvm; |
| 49 | |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 50 | static cl::opt<bool> |
Eric Christopher | 6e5367d | 2010-10-18 22:53:53 +0000 | [diff] [blame] | 51 | DisableARMFastISel("disable-arm-fast-isel", |
| 52 | cl::desc("Turn off experimental ARM fast-isel support"), |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 53 | cl::init(false), cl::Hidden); |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 54 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 55 | extern cl::opt<bool> EnableARMLongCalls; |
| 56 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 57 | namespace { |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 58 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 59 | // All possible address modes, plus some. |
| 60 | typedef struct Address { |
| 61 | enum { |
| 62 | RegBase, |
| 63 | FrameIndexBase |
| 64 | } BaseType; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 65 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 66 | union { |
| 67 | unsigned Reg; |
| 68 | int FI; |
| 69 | } Base; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 70 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 71 | int Offset; |
| 72 | unsigned Scale; |
| 73 | unsigned PlusReg; |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 74 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 75 | // Innocuous defaults for our address. |
| 76 | Address() |
| 77 | : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) { |
| 78 | Base.Reg = 0; |
| 79 | } |
| 80 | } Address; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 81 | |
| 82 | class ARMFastISel : public FastISel { |
| 83 | |
| 84 | /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can |
| 85 | /// make the right decision when generating code for different targets. |
| 86 | const ARMSubtarget *Subtarget; |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 87 | const TargetMachine &TM; |
| 88 | const TargetInstrInfo &TII; |
| 89 | const TargetLowering &TLI; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 90 | ARMFunctionInfo *AFI; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 91 | |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 92 | // Convenience variables to avoid some queries. |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 93 | bool isThumb; |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 94 | LLVMContext *Context; |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 95 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 96 | public: |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 97 | explicit ARMFastISel(FunctionLoweringInfo &funcInfo) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 98 | : FastISel(funcInfo), |
| 99 | TM(funcInfo.MF->getTarget()), |
| 100 | TII(*TM.getInstrInfo()), |
| 101 | TLI(*TM.getTargetLowering()) { |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 102 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 103 | AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 104 | isThumb = AFI->isThumbFunction(); |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 105 | Context = &funcInfo.Fn->getContext(); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 108 | // Code from FastISel.cpp. |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 109 | virtual unsigned FastEmitInst_(unsigned MachineInstOpcode, |
| 110 | const TargetRegisterClass *RC); |
| 111 | virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode, |
| 112 | const TargetRegisterClass *RC, |
| 113 | unsigned Op0, bool Op0IsKill); |
| 114 | virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode, |
| 115 | const TargetRegisterClass *RC, |
| 116 | unsigned Op0, bool Op0IsKill, |
| 117 | unsigned Op1, bool Op1IsKill); |
Cameron Zwarich | c0e6d78 | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 118 | virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 119 | const TargetRegisterClass *RC, |
| 120 | unsigned Op0, bool Op0IsKill, |
| 121 | unsigned Op1, bool Op1IsKill, |
| 122 | unsigned Op2, bool Op2IsKill); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 123 | virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode, |
| 124 | const TargetRegisterClass *RC, |
| 125 | unsigned Op0, bool Op0IsKill, |
| 126 | uint64_t Imm); |
| 127 | virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode, |
| 128 | const TargetRegisterClass *RC, |
| 129 | unsigned Op0, bool Op0IsKill, |
| 130 | const ConstantFP *FPImm); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 131 | virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode, |
| 132 | const TargetRegisterClass *RC, |
| 133 | unsigned Op0, bool Op0IsKill, |
| 134 | unsigned Op1, bool Op1IsKill, |
| 135 | uint64_t Imm); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 136 | virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode, |
| 137 | const TargetRegisterClass *RC, |
| 138 | uint64_t Imm); |
| 139 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 140 | virtual unsigned FastEmitInst_extractsubreg(MVT RetVT, |
| 141 | unsigned Op0, bool Op0IsKill, |
| 142 | uint32_t Idx); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 143 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 144 | // Backend specific FastISel code. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 145 | virtual bool TargetSelectInstruction(const Instruction *I); |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 146 | virtual unsigned TargetMaterializeConstant(const Constant *C); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 147 | virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 148 | |
| 149 | #include "ARMGenFastISel.inc" |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 150 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 151 | // Instruction selection routines. |
Eric Christopher | 44bff90 | 2010-09-10 23:10:30 +0000 | [diff] [blame] | 152 | private: |
Eric Christopher | 1778772 | 2010-10-21 21:47:51 +0000 | [diff] [blame] | 153 | bool SelectLoad(const Instruction *I); |
| 154 | bool SelectStore(const Instruction *I); |
| 155 | bool SelectBranch(const Instruction *I); |
| 156 | bool SelectCmp(const Instruction *I); |
| 157 | bool SelectFPExt(const Instruction *I); |
| 158 | bool SelectFPTrunc(const Instruction *I); |
| 159 | bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode); |
| 160 | bool SelectSIToFP(const Instruction *I); |
| 161 | bool SelectFPToSI(const Instruction *I); |
| 162 | bool SelectSDiv(const Instruction *I); |
| 163 | bool SelectSRem(const Instruction *I); |
| 164 | bool SelectCall(const Instruction *I); |
| 165 | bool SelectSelect(const Instruction *I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 166 | bool SelectRet(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 167 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 168 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 169 | private: |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 170 | bool isTypeLegal(const Type *Ty, MVT &VT); |
| 171 | bool isLoadTypeLegal(const Type *Ty, MVT &VT); |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 172 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr); |
| 173 | bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr); |
| 174 | bool ARMComputeAddress(const Value *Obj, Address &Addr); |
| 175 | void ARMSimplifyAddress(Address &Addr, EVT VT); |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 176 | unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT); |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 177 | unsigned ARMMaterializeInt(const Constant *C, EVT VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 178 | unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT); |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 179 | unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 180 | unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 181 | unsigned ARMSelectCallOp(const GlobalValue *GV); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 182 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 183 | // Call handling routines. |
| 184 | private: |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 185 | bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT, |
| 186 | unsigned &ResultReg); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 187 | CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 188 | bool ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 189 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 190 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 191 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 192 | SmallVectorImpl<unsigned> &RegArgs, |
| 193 | CallingConv::ID CC, |
| 194 | unsigned &NumBytes); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 195 | bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 196 | const Instruction *I, CallingConv::ID CC, |
| 197 | unsigned &NumBytes); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 198 | bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 199 | |
| 200 | // OptionalDef handling routines. |
| 201 | private: |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 202 | bool isARMNEONPred(const MachineInstr *MI); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 203 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 204 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 205 | void AddLoadStoreOperands(EVT VT, Address &Addr, |
| 206 | const MachineInstrBuilder &MIB); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 207 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 208 | |
| 209 | } // end anonymous namespace |
| 210 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 211 | #include "ARMGenCallingConv.inc" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 212 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 213 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 214 | // we don't care about implicit defs here, just places we'll need to add a |
| 215 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 216 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
| 217 | const TargetInstrDesc &TID = MI->getDesc(); |
| 218 | if (!TID.hasOptionalDef()) |
| 219 | return false; |
| 220 | |
| 221 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 222 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 223 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 224 | if (!MO.isReg() || !MO.isDef()) continue; |
| 225 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 226 | *CPSR = true; |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 231 | bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) { |
| 232 | const TargetInstrDesc &TID = MI->getDesc(); |
| 233 | |
| 234 | // If we're a thumb2 or not NEON function we were handled via isPredicable. |
| 235 | if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON || |
| 236 | AFI->isThumb2Function()) |
| 237 | return false; |
| 238 | |
| 239 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) |
| 240 | if (TID.OpInfo[i].isPredicate()) |
| 241 | return true; |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 246 | // If the machine is predicable go ahead and add the predicate operands, if |
| 247 | // it needs default CC operands add those. |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 248 | // TODO: If we want to support thumb1 then we'll need to deal with optional |
| 249 | // CPSR defs that need to be added before the remaining operands. See s_cc_out |
| 250 | // for descriptions why. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 251 | const MachineInstrBuilder & |
| 252 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 253 | MachineInstr *MI = &*MIB; |
| 254 | |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 255 | // Do we use a predicate? or... |
| 256 | // Are we NEON in ARM mode and have a predicate operand? If so, I know |
| 257 | // we're not predicable but add it anyways. |
| 258 | if (TII.isPredicable(MI) || isARMNEONPred(MI)) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 259 | AddDefaultPred(MIB); |
Eric Christopher | af3dce5 | 2011-03-12 01:09:29 +0000 | [diff] [blame] | 260 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 261 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 262 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 263 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 264 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 265 | if (CPSR) |
| 266 | AddDefaultT1CC(MIB); |
| 267 | else |
| 268 | AddDefaultCC(MIB); |
| 269 | } |
| 270 | return MIB; |
| 271 | } |
| 272 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 273 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 274 | const TargetRegisterClass* RC) { |
| 275 | unsigned ResultReg = createResultReg(RC); |
| 276 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 277 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 278 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 279 | return ResultReg; |
| 280 | } |
| 281 | |
| 282 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 283 | const TargetRegisterClass *RC, |
| 284 | unsigned Op0, bool Op0IsKill) { |
| 285 | unsigned ResultReg = createResultReg(RC); |
| 286 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 287 | |
| 288 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 289 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 290 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 291 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 292 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 293 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 294 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 295 | TII.get(TargetOpcode::COPY), ResultReg) |
| 296 | .addReg(II.ImplicitDefs[0])); |
| 297 | } |
| 298 | return ResultReg; |
| 299 | } |
| 300 | |
| 301 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 302 | const TargetRegisterClass *RC, |
| 303 | unsigned Op0, bool Op0IsKill, |
| 304 | unsigned Op1, bool Op1IsKill) { |
| 305 | unsigned ResultReg = createResultReg(RC); |
| 306 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 307 | |
| 308 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 309 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 310 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 311 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 312 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 313 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 314 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 315 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 316 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 317 | TII.get(TargetOpcode::COPY), ResultReg) |
| 318 | .addReg(II.ImplicitDefs[0])); |
| 319 | } |
| 320 | return ResultReg; |
| 321 | } |
| 322 | |
Cameron Zwarich | c0e6d78 | 2011-03-30 23:01:21 +0000 | [diff] [blame] | 323 | unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 324 | const TargetRegisterClass *RC, |
| 325 | unsigned Op0, bool Op0IsKill, |
| 326 | unsigned Op1, bool Op1IsKill, |
| 327 | unsigned Op2, bool Op2IsKill) { |
| 328 | unsigned ResultReg = createResultReg(RC); |
| 329 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 330 | |
| 331 | if (II.getNumDefs() >= 1) |
| 332 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 333 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 334 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 335 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
| 336 | else { |
| 337 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 338 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 339 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 340 | .addReg(Op2, Op2IsKill * RegState::Kill)); |
| 341 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 342 | TII.get(TargetOpcode::COPY), ResultReg) |
| 343 | .addReg(II.ImplicitDefs[0])); |
| 344 | } |
| 345 | return ResultReg; |
| 346 | } |
| 347 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 348 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 349 | const TargetRegisterClass *RC, |
| 350 | unsigned Op0, bool Op0IsKill, |
| 351 | uint64_t Imm) { |
| 352 | unsigned ResultReg = createResultReg(RC); |
| 353 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 354 | |
| 355 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 356 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 357 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 358 | .addImm(Imm)); |
| 359 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 360 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 361 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 362 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 363 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 364 | TII.get(TargetOpcode::COPY), ResultReg) |
| 365 | .addReg(II.ImplicitDefs[0])); |
| 366 | } |
| 367 | return ResultReg; |
| 368 | } |
| 369 | |
| 370 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 371 | const TargetRegisterClass *RC, |
| 372 | unsigned Op0, bool Op0IsKill, |
| 373 | const ConstantFP *FPImm) { |
| 374 | unsigned ResultReg = createResultReg(RC); |
| 375 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 376 | |
| 377 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 378 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 379 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 380 | .addFPImm(FPImm)); |
| 381 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 382 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 383 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 384 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 385 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 386 | TII.get(TargetOpcode::COPY), ResultReg) |
| 387 | .addReg(II.ImplicitDefs[0])); |
| 388 | } |
| 389 | return ResultReg; |
| 390 | } |
| 391 | |
| 392 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 393 | const TargetRegisterClass *RC, |
| 394 | unsigned Op0, bool Op0IsKill, |
| 395 | unsigned Op1, bool Op1IsKill, |
| 396 | uint64_t Imm) { |
| 397 | unsigned ResultReg = createResultReg(RC); |
| 398 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 399 | |
| 400 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 401 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 402 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 403 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 404 | .addImm(Imm)); |
| 405 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 406 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 407 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 408 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 409 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 410 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 411 | TII.get(TargetOpcode::COPY), ResultReg) |
| 412 | .addReg(II.ImplicitDefs[0])); |
| 413 | } |
| 414 | return ResultReg; |
| 415 | } |
| 416 | |
| 417 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 418 | const TargetRegisterClass *RC, |
| 419 | uint64_t Imm) { |
| 420 | unsigned ResultReg = createResultReg(RC); |
| 421 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 422 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 423 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 424 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 425 | .addImm(Imm)); |
| 426 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 427 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 428 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 429 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 430 | TII.get(TargetOpcode::COPY), ResultReg) |
| 431 | .addReg(II.ImplicitDefs[0])); |
| 432 | } |
| 433 | return ResultReg; |
| 434 | } |
| 435 | |
| 436 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 437 | unsigned Op0, bool Op0IsKill, |
| 438 | uint32_t Idx) { |
| 439 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 440 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 441 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 442 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 443 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 444 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 445 | return ResultReg; |
| 446 | } |
| 447 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 448 | // TODO: Don't worry about 64-bit now, but when this is fixed remove the |
| 449 | // checks from the various callers. |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 450 | unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 451 | if (VT == MVT::f64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 452 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 453 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 454 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 455 | TII.get(ARM::VMOVRS), MoveReg) |
| 456 | .addReg(SrcReg)); |
| 457 | return MoveReg; |
| 458 | } |
| 459 | |
| 460 | unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 461 | if (VT == MVT::i64) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 462 | |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 463 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 464 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 465 | TII.get(ARM::VMOVSR), MoveReg) |
Eric Christopher | aa3ace1 | 2010-09-09 20:49:25 +0000 | [diff] [blame] | 466 | .addReg(SrcReg)); |
| 467 | return MoveReg; |
| 468 | } |
| 469 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 470 | // For double width floating point we need to materialize two constants |
| 471 | // (the high and the low) into integer registers then use a move to get |
| 472 | // the combined constant into an FP reg. |
| 473 | unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) { |
| 474 | const APFloat Val = CFP->getValueAPF(); |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 475 | bool is64bit = VT == MVT::f64; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 476 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 477 | // This checks to see if we can use VFP3 instructions to materialize |
| 478 | // a constant, otherwise we have to go through the constant pool. |
| 479 | if (TLI.isFPImmLegal(Val, VT)) { |
| 480 | unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS; |
| 481 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 482 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 483 | DestReg) |
| 484 | .addFPImm(CFP)); |
| 485 | return DestReg; |
| 486 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 487 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 488 | // Require VFP2 for loading fp constants. |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 489 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 490 | |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 491 | // MachineConstantPool wants an explicit alignment. |
| 492 | unsigned Align = TD.getPrefTypeAlignment(CFP->getType()); |
| 493 | if (Align == 0) { |
| 494 | // TODO: Figure out if this is correct. |
| 495 | Align = TD.getTypeAllocSize(CFP->getType()); |
| 496 | } |
| 497 | unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); |
| 498 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 499 | unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 500 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 501 | // The extra reg is for addrmode5. |
Eric Christopher | f5732c4 | 2010-09-28 00:35:09 +0000 | [diff] [blame] | 502 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 503 | DestReg) |
| 504 | .addConstantPoolIndex(Idx) |
Eric Christopher | 238bb16 | 2010-09-09 23:50:00 +0000 | [diff] [blame] | 505 | .addReg(0)); |
| 506 | return DestReg; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 509 | unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) { |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 510 | |
Eric Christopher | 744c7c8 | 2010-09-28 22:47:54 +0000 | [diff] [blame] | 511 | // For now 32-bit only. |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 512 | if (VT != MVT::i32) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 513 | |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 514 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 515 | |
| 516 | // If we can do this in a single instruction without a constant pool entry |
| 517 | // do so now. |
| 518 | const ConstantInt *CI = cast<ConstantInt>(C); |
Eric Christopher | 5e262bc | 2010-11-06 07:53:11 +0000 | [diff] [blame] | 519 | if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) { |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 520 | unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16; |
| 521 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Jim Grosbach | 3ea4daa | 2010-11-19 18:01:37 +0000 | [diff] [blame] | 522 | TII.get(Opc), DestReg) |
| 523 | .addImm(CI->getSExtValue())); |
Eric Christopher | e5b13cf | 2010-11-03 20:21:17 +0000 | [diff] [blame] | 524 | return DestReg; |
| 525 | } |
| 526 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 527 | // MachineConstantPool wants an explicit alignment. |
| 528 | unsigned Align = TD.getPrefTypeAlignment(C->getType()); |
| 529 | if (Align == 0) { |
| 530 | // TODO: Figure out if this is correct. |
| 531 | Align = TD.getTypeAllocSize(C->getType()); |
| 532 | } |
| 533 | unsigned Idx = MCP.getConstantPoolIndex(C, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 534 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 535 | if (isThumb) |
| 536 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 537 | TII.get(ARM::t2LDRpci), DestReg) |
| 538 | .addConstantPoolIndex(Idx)); |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 539 | else |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 540 | // The extra immediate is for addrmode2. |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 541 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | fd60980 | 2010-09-28 21:55:34 +0000 | [diff] [blame] | 542 | TII.get(ARM::LDRcp), DestReg) |
| 543 | .addConstantPoolIndex(Idx) |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 544 | .addImm(0)); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 545 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 546 | return DestReg; |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 549 | unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) { |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 550 | // For now 32-bit only. |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 551 | if (VT != MVT::i32) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 552 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 553 | Reloc::Model RelocM = TM.getRelocationModel(); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 554 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 555 | // TODO: No external globals for now. |
| 556 | if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 557 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 558 | // TODO: Need more magic for ARM PIC. |
| 559 | if (!isThumb && (RelocM == Reloc::PIC_)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 560 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 561 | // MachineConstantPool wants an explicit alignment. |
| 562 | unsigned Align = TD.getPrefTypeAlignment(GV->getType()); |
| 563 | if (Align == 0) { |
| 564 | // TODO: Figure out if this is correct. |
| 565 | Align = TD.getTypeAllocSize(GV->getType()); |
| 566 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 567 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 568 | // Grab index. |
| 569 | unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8); |
Evan Cheng | 5de5d4b | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 570 | unsigned Id = AFI->createPICLabelUId(); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 571 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id, |
| 572 | ARMCP::CPValue, PCAdj); |
| 573 | unsigned Idx = MCP.getConstantPoolIndex(CPV, Align); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 574 | |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 575 | // Load value. |
| 576 | MachineInstrBuilder MIB; |
| 577 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 578 | if (isThumb) { |
| 579 | unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic; |
| 580 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg) |
| 581 | .addConstantPoolIndex(Idx); |
| 582 | if (RelocM == Reloc::PIC_) |
| 583 | MIB.addImm(Id); |
| 584 | } else { |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 585 | // The extra immediate is for addrmode2. |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 586 | MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp), |
| 587 | DestReg) |
| 588 | .addConstantPoolIndex(Idx) |
Eric Christopher | d0c82a6 | 2010-11-12 09:48:30 +0000 | [diff] [blame] | 589 | .addImm(0); |
Eric Christopher | 890dbbe | 2010-10-02 00:32:44 +0000 | [diff] [blame] | 590 | } |
| 591 | AddOptionalDefs(MIB); |
| 592 | return DestReg; |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 595 | unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) { |
| 596 | EVT VT = TLI.getValueType(C->getType(), true); |
| 597 | |
| 598 | // Only handle simple types. |
| 599 | if (!VT.isSimple()) return 0; |
| 600 | |
| 601 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 602 | return ARMMaterializeFP(CFP, VT); |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 603 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 604 | return ARMMaterializeGV(GV, VT); |
| 605 | else if (isa<ConstantInt>(C)) |
| 606 | return ARMMaterializeInt(C, VT); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 607 | |
Eric Christopher | c9932f6 | 2010-10-01 23:24:42 +0000 | [diff] [blame] | 608 | return 0; |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 611 | unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) { |
| 612 | // Don't handle dynamic allocas. |
| 613 | if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 614 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 615 | MVT VT; |
Eric Christopher | ec8bf97 | 2010-10-17 06:07:26 +0000 | [diff] [blame] | 616 | if (!isLoadTypeLegal(AI->getType(), VT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 617 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 618 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 619 | FuncInfo.StaticAllocaMap.find(AI); |
| 620 | |
| 621 | // This will get lowered later into the correct offsets and registers |
| 622 | // via rewriteXFrameIndex. |
| 623 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 624 | TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
| 625 | unsigned ResultReg = createResultReg(RC); |
| 626 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 627 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 628 | TII.get(Opc), ResultReg) |
| 629 | .addFrameIndex(SI->second) |
| 630 | .addImm(0)); |
| 631 | return ResultReg; |
| 632 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 633 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 637 | bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) { |
| 638 | EVT evt = TLI.getValueType(Ty, true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 639 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 640 | // Only handle simple types. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 641 | if (evt == MVT::Other || !evt.isSimple()) return false; |
| 642 | VT = evt.getSimpleVT(); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 643 | |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 644 | // Handle all legal types, i.e. a register that will directly hold this |
| 645 | // value. |
| 646 | return TLI.isTypeLegal(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 649 | bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) { |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 650 | if (isTypeLegal(Ty, VT)) return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 651 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 652 | // If this is a type than can be sign or zero-extended to a basic operation |
| 653 | // go ahead and accept it now. |
| 654 | if (VT == MVT::i8 || VT == MVT::i16) |
| 655 | return true; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 656 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 657 | return false; |
| 658 | } |
| 659 | |
Eric Christopher | 88de86b | 2010-11-19 22:36:41 +0000 | [diff] [blame] | 660 | // Computes the address to get to an object. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 661 | bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 662 | // Some boilerplate from the X86 FastISel. |
| 663 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 664 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 665 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 2d630d7 | 2010-11-19 22:37:58 +0000 | [diff] [blame] | 666 | // Don't walk into other basic blocks unless the object is an alloca from |
| 667 | // another block, otherwise it may not have a virtual register assigned. |
Eric Christopher | 76dda7e | 2010-11-15 21:11:06 +0000 | [diff] [blame] | 668 | if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || |
| 669 | FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { |
| 670 | Opcode = I->getOpcode(); |
| 671 | U = I; |
| 672 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 673 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 674 | Opcode = C->getOpcode(); |
| 675 | U = C; |
| 676 | } |
| 677 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 678 | if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 679 | if (Ty->getAddressSpace() > 255) |
| 680 | // Fast instruction selection doesn't support the special |
| 681 | // address spaces. |
| 682 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 683 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 684 | switch (Opcode) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 685 | default: |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 686 | break; |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 687 | case Instruction::BitCast: { |
| 688 | // Look through bitcasts. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 689 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 690 | } |
| 691 | case Instruction::IntToPtr: { |
| 692 | // Look past no-op inttoptrs. |
| 693 | if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 694 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | case Instruction::PtrToInt: { |
| 698 | // Look past no-op ptrtoints. |
| 699 | if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 700 | return ARMComputeAddress(U->getOperand(0), Addr); |
Eric Christopher | 5532433 | 2010-10-12 00:43:21 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 703 | case Instruction::GetElementPtr: { |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 704 | Address SavedAddr = Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 705 | int TmpOffset = Addr.Offset; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 706 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 707 | // Iterate through the GEP folding the constants into offsets where |
| 708 | // we can. |
| 709 | gep_type_iterator GTI = gep_type_begin(U); |
| 710 | for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 711 | i != e; ++i, ++GTI) { |
| 712 | const Value *Op = *i; |
| 713 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 714 | const StructLayout *SL = TD.getStructLayout(STy); |
| 715 | unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); |
| 716 | TmpOffset += SL->getElementOffset(Idx); |
| 717 | } else { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 718 | uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType()); |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 719 | for (;;) { |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 720 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 721 | // Constant-offset addressing. |
| 722 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | if (isa<AddOperator>(Op) && |
| 726 | (!isa<Instruction>(Op) || |
| 727 | FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()] |
| 728 | == FuncInfo.MBB) && |
| 729 | isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) { |
| 730 | // An add (in the same block) with a constant operand. Fold the |
| 731 | // constant. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 732 | ConstantInt *CI = |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 733 | cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 734 | TmpOffset += CI->getSExtValue() * S; |
Eric Christopher | 7244d7c | 2011-03-22 19:39:17 +0000 | [diff] [blame] | 735 | // Iterate on the other operand. |
| 736 | Op = cast<AddOperator>(Op)->getOperand(0); |
| 737 | continue; |
| 738 | } |
| 739 | // Unsupported |
| 740 | goto unsupported_gep; |
| 741 | } |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 744 | |
| 745 | // Try to grab the base operand now. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 746 | Addr.Offset = TmpOffset; |
| 747 | if (ARMComputeAddress(U->getOperand(0), Addr)) return true; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 748 | |
| 749 | // We failed, restore everything and try the other options. |
Eric Christopher | b371658 | 2010-11-19 22:39:56 +0000 | [diff] [blame] | 750 | Addr = SavedAddr; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 751 | |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 752 | unsupported_gep: |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 755 | case Instruction::Alloca: { |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 756 | const AllocaInst *AI = cast<AllocaInst>(Obj); |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 757 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 758 | FuncInfo.StaticAllocaMap.find(AI); |
| 759 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 760 | Addr.BaseType = Address::FrameIndexBase; |
| 761 | Addr.Base.FI = SI->second; |
| 762 | return true; |
| 763 | } |
| 764 | break; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 767 | |
Eric Christopher | a9c5751 | 2010-10-13 21:41:51 +0000 | [diff] [blame] | 768 | // Materialize the global variable's address into a reg which can |
| 769 | // then be used later to load the variable. |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 770 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 771 | unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType())); |
| 772 | if (Tmp == 0) return false; |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 773 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 774 | Addr.Base.Reg = Tmp; |
Eric Christopher | ede42b0 | 2010-10-13 09:11:46 +0000 | [diff] [blame] | 775 | return true; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 776 | } |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 777 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 778 | // Try to get this in a register if nothing else has worked. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 779 | if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj); |
| 780 | return Addr.Base.Reg != 0; |
Eric Christopher | eae8439 | 2010-10-14 09:29:41 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 783 | void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) { |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 784 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 785 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 786 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 787 | bool needsLowering = false; |
| 788 | switch (VT.getSimpleVT().SimpleTy) { |
| 789 | default: |
| 790 | assert(false && "Unhandled load/store type!"); |
| 791 | case MVT::i1: |
| 792 | case MVT::i8: |
| 793 | case MVT::i16: |
| 794 | case MVT::i32: |
| 795 | // Integer loads/stores handle 12-bit offsets. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 796 | needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset); |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 797 | break; |
| 798 | case MVT::f32: |
| 799 | case MVT::f64: |
| 800 | // Floating point operands handle 8-bit offsets. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 801 | needsLowering = ((Addr.Offset & 0xff) != Addr.Offset); |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 804 | |
Eric Christopher | 827656d | 2010-11-20 22:38:27 +0000 | [diff] [blame] | 805 | // If this is a stack pointer and the offset needs to be simplified then |
| 806 | // put the alloca address into a register, set the base type back to |
| 807 | // register and continue. This should almost never happen. |
| 808 | if (needsLowering && Addr.BaseType == Address::FrameIndexBase) { |
| 809 | TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass : |
| 810 | ARM::GPRRegisterClass; |
| 811 | unsigned ResultReg = createResultReg(RC); |
| 812 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 813 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL, |
| 814 | TII.get(Opc), ResultReg) |
| 815 | .addFrameIndex(Addr.Base.FI) |
| 816 | .addImm(0)); |
| 817 | Addr.Base.Reg = ResultReg; |
| 818 | Addr.BaseType = Address::RegBase; |
| 819 | } |
| 820 | |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 821 | // Since the offset is too large for the load/store instruction |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 822 | // get the reg+offset into a register. |
Eric Christopher | 212ae93 | 2010-10-21 19:40:30 +0000 | [diff] [blame] | 823 | if (needsLowering) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 824 | ARMCC::CondCodes Pred = ARMCC::AL; |
| 825 | unsigned PredReg = 0; |
| 826 | |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 827 | TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass : |
| 828 | ARM::GPRRegisterClass; |
| 829 | unsigned BaseReg = createResultReg(RC); |
| 830 | |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 831 | if (!isThumb) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 832 | emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 833 | BaseReg, Addr.Base.Reg, Addr.Offset, |
| 834 | Pred, PredReg, |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 835 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 836 | else { |
| 837 | assert(AFI->isThumb2Function()); |
| 838 | emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 839 | BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg, |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 840 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 841 | } |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 842 | Addr.Offset = 0; |
| 843 | Addr.Base.Reg = BaseReg; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 844 | } |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 847 | void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr, |
| 848 | const MachineInstrBuilder &MIB) { |
| 849 | // addrmode5 output depends on the selection dag addressing dividing the |
| 850 | // offset by 4 that it then later multiplies. Do this here as well. |
| 851 | if (VT.getSimpleVT().SimpleTy == MVT::f32 || |
| 852 | VT.getSimpleVT().SimpleTy == MVT::f64) |
| 853 | Addr.Offset /= 4; |
| 854 | |
| 855 | // Frame base works a bit differently. Handle it separately. |
| 856 | if (Addr.BaseType == Address::FrameIndexBase) { |
| 857 | int FI = Addr.Base.FI; |
| 858 | int Offset = Addr.Offset; |
| 859 | MachineMemOperand *MMO = |
| 860 | FuncInfo.MF->getMachineMemOperand( |
| 861 | MachinePointerInfo::getFixedStack(FI, Offset), |
| 862 | MachineMemOperand::MOLoad, |
| 863 | MFI.getObjectSize(FI), |
| 864 | MFI.getObjectAlignment(FI)); |
| 865 | // Now add the rest of the operands. |
| 866 | MIB.addFrameIndex(FI); |
| 867 | |
| 868 | // ARM halfword load/stores need an additional operand. |
| 869 | if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0); |
| 870 | |
| 871 | MIB.addImm(Addr.Offset); |
| 872 | MIB.addMemOperand(MMO); |
| 873 | } else { |
| 874 | // Now add the rest of the operands. |
| 875 | MIB.addReg(Addr.Base.Reg); |
| 876 | |
| 877 | // ARM halfword load/stores need an additional operand. |
| 878 | if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0); |
| 879 | |
| 880 | MIB.addImm(Addr.Offset); |
| 881 | } |
| 882 | AddOptionalDefs(MIB); |
| 883 | } |
| 884 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 885 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 886 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 887 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 888 | unsigned Opc; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 889 | TargetRegisterClass *RC; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 890 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 891 | // This is mostly going to be Neon/vector support. |
| 892 | default: return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 893 | case MVT::i16: |
Eric Christopher | 45c6071 | 2010-10-17 01:40:27 +0000 | [diff] [blame] | 894 | Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 895 | RC = ARM::GPRRegisterClass; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 896 | break; |
| 897 | case MVT::i8: |
Jim Grosbach | c1d3021 | 2010-10-27 00:19:44 +0000 | [diff] [blame] | 898 | Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 899 | RC = ARM::GPRRegisterClass; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 900 | break; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 901 | case MVT::i32: |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 902 | Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12; |
Eric Christopher | 7a56f33 | 2010-10-08 01:13:17 +0000 | [diff] [blame] | 903 | RC = ARM::GPRRegisterClass; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 904 | break; |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 905 | case MVT::f32: |
| 906 | Opc = ARM::VLDRS; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 907 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 908 | break; |
| 909 | case MVT::f64: |
| 910 | Opc = ARM::VLDRD; |
Eric Christopher | ee56ea6 | 2010-10-07 05:50:44 +0000 | [diff] [blame] | 911 | RC = TLI.getRegClassFor(VT); |
Eric Christopher | 6dab137 | 2010-09-18 01:59:37 +0000 | [diff] [blame] | 912 | break; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 913 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 914 | // Simplify this down to something we can handle. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 915 | ARMSimplifyAddress(Addr, VT); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 916 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 917 | // Create the base instruction, then add the operands. |
| 918 | ResultReg = createResultReg(RC); |
| 919 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 920 | TII.get(Opc), ResultReg); |
| 921 | AddLoadStoreOperands(VT, Addr, MIB); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 922 | return true; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 925 | bool ARMFastISel::SelectLoad(const Instruction *I) { |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 926 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 927 | MVT VT; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 928 | if (!isLoadTypeLegal(I->getType(), VT)) |
| 929 | return false; |
| 930 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 931 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 932 | Address Addr; |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 933 | if (!ARMComputeAddress(I->getOperand(0), Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 934 | |
| 935 | unsigned ResultReg; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 936 | if (!ARMEmitLoad(VT, ResultReg, Addr)) return false; |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 937 | UpdateValueMap(I, ResultReg); |
| 938 | return true; |
| 939 | } |
| 940 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 941 | bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 942 | unsigned StrOpc; |
| 943 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 944 | // This is mostly going to be Neon/vector support. |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 945 | default: return false; |
Eric Christopher | 4c91412 | 2010-11-02 23:59:09 +0000 | [diff] [blame] | 946 | case MVT::i1: { |
| 947 | unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass : |
| 948 | ARM::GPRRegisterClass); |
| 949 | unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri; |
| 950 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 951 | TII.get(Opc), Res) |
| 952 | .addReg(SrcReg).addImm(1)); |
| 953 | SrcReg = Res; |
| 954 | } // Fallthrough here. |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 955 | case MVT::i8: |
Jim Grosbach | 7e3383c | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 956 | StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12; |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 957 | break; |
| 958 | case MVT::i16: |
Eric Christopher | 45c6071 | 2010-10-17 01:40:27 +0000 | [diff] [blame] | 959 | StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH; |
Eric Christopher | 1541877 | 2010-10-12 05:39:06 +0000 | [diff] [blame] | 960 | break; |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 961 | case MVT::i32: |
Jim Grosbach | 7e3383c | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 962 | StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12; |
Eric Christopher | 47650ec | 2010-10-16 01:10:35 +0000 | [diff] [blame] | 963 | break; |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 964 | case MVT::f32: |
| 965 | if (!Subtarget->hasVFP2()) return false; |
| 966 | StrOpc = ARM::VSTRS; |
| 967 | break; |
| 968 | case MVT::f64: |
| 969 | if (!Subtarget->hasVFP2()) return false; |
| 970 | StrOpc = ARM::VSTRD; |
| 971 | break; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 972 | } |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 973 | // Simplify this down to something we can handle. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 974 | ARMSimplifyAddress(Addr, VT); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 975 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 976 | // Create the base instruction, then add the operands. |
| 977 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 978 | TII.get(StrOpc)) |
| 979 | .addReg(SrcReg, getKillRegState(true)); |
| 980 | AddLoadStoreOperands(VT, Addr, MIB); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 981 | return true; |
| 982 | } |
| 983 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 984 | bool ARMFastISel::SelectStore(const Instruction *I) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 985 | Value *Op0 = I->getOperand(0); |
| 986 | unsigned SrcReg = 0; |
| 987 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 988 | // Verify we have a legal type before going any further. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 989 | MVT VT; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 990 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 991 | return false; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 992 | |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 993 | // Get the value to be stored into a register. |
| 994 | SrcReg = getRegForValue(Op0); |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 995 | if (SrcReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 996 | |
Eric Christopher | 564857f | 2010-12-01 01:40:24 +0000 | [diff] [blame] | 997 | // See if we can handle this address. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 998 | Address Addr; |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 999 | if (!ARMComputeAddress(I->getOperand(1), Addr)) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 1000 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1001 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1002 | if (!ARMEmitStore(VT, SrcReg, Addr)) return false; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1003 | return true; |
| 1004 | } |
| 1005 | |
| 1006 | static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) { |
| 1007 | switch (Pred) { |
| 1008 | // Needs two compares... |
| 1009 | case CmpInst::FCMP_ONE: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1010 | case CmpInst::FCMP_UEQ: |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1011 | default: |
Eric Christopher | 4053e63 | 2010-11-02 01:24:49 +0000 | [diff] [blame] | 1012 | // AL is our "false" for now. The other two need more compares. |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1013 | return ARMCC::AL; |
| 1014 | case CmpInst::ICMP_EQ: |
| 1015 | case CmpInst::FCMP_OEQ: |
| 1016 | return ARMCC::EQ; |
| 1017 | case CmpInst::ICMP_SGT: |
| 1018 | case CmpInst::FCMP_OGT: |
| 1019 | return ARMCC::GT; |
| 1020 | case CmpInst::ICMP_SGE: |
| 1021 | case CmpInst::FCMP_OGE: |
| 1022 | return ARMCC::GE; |
| 1023 | case CmpInst::ICMP_UGT: |
| 1024 | case CmpInst::FCMP_UGT: |
| 1025 | return ARMCC::HI; |
| 1026 | case CmpInst::FCMP_OLT: |
| 1027 | return ARMCC::MI; |
| 1028 | case CmpInst::ICMP_ULE: |
| 1029 | case CmpInst::FCMP_OLE: |
| 1030 | return ARMCC::LS; |
| 1031 | case CmpInst::FCMP_ORD: |
| 1032 | return ARMCC::VC; |
| 1033 | case CmpInst::FCMP_UNO: |
| 1034 | return ARMCC::VS; |
| 1035 | case CmpInst::FCMP_UGE: |
| 1036 | return ARMCC::PL; |
| 1037 | case CmpInst::ICMP_SLT: |
| 1038 | case CmpInst::FCMP_ULT: |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1039 | return ARMCC::LT; |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1040 | case CmpInst::ICMP_SLE: |
| 1041 | case CmpInst::FCMP_ULE: |
| 1042 | return ARMCC::LE; |
| 1043 | case CmpInst::FCMP_UNE: |
| 1044 | case CmpInst::ICMP_NE: |
| 1045 | return ARMCC::NE; |
| 1046 | case CmpInst::ICMP_UGE: |
| 1047 | return ARMCC::HS; |
| 1048 | case CmpInst::ICMP_ULT: |
| 1049 | return ARMCC::LO; |
| 1050 | } |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1053 | bool ARMFastISel::SelectBranch(const Instruction *I) { |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1054 | const BranchInst *BI = cast<BranchInst>(I); |
| 1055 | MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 1056 | MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1057 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1058 | // Simple branch support. |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1059 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1060 | // If we can, avoid recomputing the compare - redoing it could lead to wonky |
| 1061 | // behavior. |
| 1062 | // TODO: Factor this out. |
| 1063 | if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
| 1064 | if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1065 | MVT VT; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1066 | const Type *Ty = CI->getOperand(0)->getType(); |
Eric Christopher | 76d6147 | 2010-10-30 21:25:26 +0000 | [diff] [blame] | 1067 | if (!isTypeLegal(Ty, VT)) |
| 1068 | return false; |
| 1069 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1070 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1071 | if (isFloat && !Subtarget->hasVFP2()) |
| 1072 | return false; |
| 1073 | |
| 1074 | unsigned CmpOpc; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1075 | switch (VT.SimpleTy) { |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1076 | default: return false; |
| 1077 | // TODO: Verify compares. |
| 1078 | case MVT::f32: |
| 1079 | CmpOpc = ARM::VCMPES; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | case MVT::f64: |
| 1082 | CmpOpc = ARM::VCMPED; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | case MVT::i32: |
| 1085 | CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | // Get the compare predicate. |
| 1090 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
| 1091 | |
| 1092 | // We may not handle every CC for now. |
| 1093 | if (ARMPred == ARMCC::AL) return false; |
| 1094 | |
| 1095 | unsigned Arg1 = getRegForValue(CI->getOperand(0)); |
| 1096 | if (Arg1 == 0) return false; |
| 1097 | |
| 1098 | unsigned Arg2 = getRegForValue(CI->getOperand(1)); |
| 1099 | if (Arg2 == 0) return false; |
| 1100 | |
| 1101 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1102 | TII.get(CmpOpc)) |
| 1103 | .addReg(Arg1).addReg(Arg2)); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1104 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1105 | // For floating point we need to move the result to a comparison register |
| 1106 | // that we can then use for branches. |
| 1107 | if (isFloat) |
| 1108 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1109 | TII.get(ARM::FMSTAT))); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1110 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1111 | unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc; |
| 1112 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
| 1113 | .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR); |
| 1114 | FastEmitBranch(FBB, DL); |
| 1115 | FuncInfo.MBB->addSuccessor(TBB); |
| 1116 | return true; |
| 1117 | } |
| 1118 | } |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1119 | |
Eric Christopher | 0e6233b | 2010-10-29 21:08:19 +0000 | [diff] [blame] | 1120 | unsigned CmpReg = getRegForValue(BI->getCondition()); |
| 1121 | if (CmpReg == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1122 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1123 | // Re-set the flags just in case. |
| 1124 | unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri; |
| 1125 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
Eric Christopher | 000cf70 | 2010-11-03 04:29:11 +0000 | [diff] [blame] | 1126 | .addReg(CmpReg).addImm(0)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1127 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1128 | unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1129 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
Eric Christopher | 000cf70 | 2010-11-03 04:29:11 +0000 | [diff] [blame] | 1130 | .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1131 | FastEmitBranch(FBB, DL); |
| 1132 | FuncInfo.MBB->addSuccessor(TBB); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1133 | return true; |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1136 | bool ARMFastISel::SelectCmp(const Instruction *I) { |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1137 | const CmpInst *CI = cast<CmpInst>(I); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1138 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1139 | MVT VT; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1140 | const Type *Ty = CI->getOperand(0)->getType(); |
| 1141 | if (!isTypeLegal(Ty, VT)) |
| 1142 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1143 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1144 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1145 | if (isFloat && !Subtarget->hasVFP2()) |
| 1146 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1147 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1148 | unsigned CmpOpc; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1149 | unsigned CondReg; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1150 | switch (VT.SimpleTy) { |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1151 | default: return false; |
| 1152 | // TODO: Verify compares. |
| 1153 | case MVT::f32: |
| 1154 | CmpOpc = ARM::VCMPES; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1155 | CondReg = ARM::FPSCR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | case MVT::f64: |
| 1158 | CmpOpc = ARM::VCMPED; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1159 | CondReg = ARM::FPSCR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | case MVT::i32: |
| 1162 | CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1163 | CondReg = ARM::CPSR; |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
| 1166 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1167 | // Get the compare predicate. |
| 1168 | ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate()); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1169 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1170 | // We may not handle every CC for now. |
| 1171 | if (ARMPred == ARMCC::AL) return false; |
| 1172 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1173 | unsigned Arg1 = getRegForValue(CI->getOperand(0)); |
| 1174 | if (Arg1 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1175 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1176 | unsigned Arg2 = getRegForValue(CI->getOperand(1)); |
| 1177 | if (Arg2 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1178 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1179 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 1180 | .addReg(Arg1).addReg(Arg2)); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1181 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1182 | // For floating point we need to move the result to a comparison register |
| 1183 | // that we can then use for branches. |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1184 | if (isFloat) |
| 1185 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1186 | TII.get(ARM::FMSTAT))); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1187 | |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1188 | // Now set a register based on the comparison. Explicitly set the predicates |
| 1189 | // here. |
Eric Christopher | 338c253 | 2010-10-07 05:31:49 +0000 | [diff] [blame] | 1190 | unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1191 | TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass |
Eric Christopher | 5d18d92 | 2010-10-07 05:39:19 +0000 | [diff] [blame] | 1192 | : ARM::GPRRegisterClass; |
| 1193 | unsigned DestReg = createResultReg(RC); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1194 | Constant *Zero |
Eric Christopher | 8cf6c60 | 2010-09-29 22:24:45 +0000 | [diff] [blame] | 1195 | = ConstantInt::get(Type::getInt32Ty(*Context), 0); |
Eric Christopher | 229207a | 2010-09-29 01:14:47 +0000 | [diff] [blame] | 1196 | unsigned ZeroReg = TargetMaterializeConstant(Zero); |
| 1197 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg) |
| 1198 | .addReg(ZeroReg).addImm(1) |
| 1199 | .addImm(ARMPred).addReg(CondReg); |
| 1200 | |
Eric Christopher | a5b1e68 | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 1201 | UpdateValueMap(I, DestReg); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1202 | return true; |
| 1203 | } |
| 1204 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1205 | bool ARMFastISel::SelectFPExt(const Instruction *I) { |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1206 | // Make sure we have VFP and that we're extending float to double. |
| 1207 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1208 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1209 | Value *V = I->getOperand(0); |
| 1210 | if (!I->getType()->isDoubleTy() || |
| 1211 | !V->getType()->isFloatTy()) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1212 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1213 | unsigned Op = getRegForValue(V); |
| 1214 | if (Op == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1215 | |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1216 | unsigned Result = createResultReg(ARM::DPRRegisterClass); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1217 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1218 | TII.get(ARM::VCVTDS), Result) |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1219 | .addReg(Op)); |
| 1220 | UpdateValueMap(I, Result); |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1224 | bool ARMFastISel::SelectFPTrunc(const Instruction *I) { |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1225 | // Make sure we have VFP and that we're truncating double to float. |
| 1226 | if (!Subtarget->hasVFP2()) return false; |
| 1227 | |
| 1228 | Value *V = I->getOperand(0); |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1229 | if (!(I->getType()->isFloatTy() && |
| 1230 | V->getType()->isDoubleTy())) return false; |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1231 | |
| 1232 | unsigned Op = getRegForValue(V); |
| 1233 | if (Op == 0) return false; |
| 1234 | |
| 1235 | unsigned Result = createResultReg(ARM::SPRRegisterClass); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1236 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | ef2fdd2 | 2010-09-09 20:36:19 +0000 | [diff] [blame] | 1237 | TII.get(ARM::VCVTSD), Result) |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1238 | .addReg(Op)); |
| 1239 | UpdateValueMap(I, Result); |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1243 | bool ARMFastISel::SelectSIToFP(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1244 | // Make sure we have VFP. |
| 1245 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1246 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1247 | MVT DstVT; |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1248 | const Type *Ty = I->getType(); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1249 | if (!isTypeLegal(Ty, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1250 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1251 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1252 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1253 | if (Op == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1254 | |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1255 | // The conversion routine works on fp-reg to fp-reg and the operand above |
| 1256 | // was an integer, move it to the fp registers if possible. |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1257 | unsigned FP = ARMMoveToFPReg(MVT::f32, Op); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1258 | if (FP == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1259 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1260 | unsigned Opc; |
| 1261 | if (Ty->isFloatTy()) Opc = ARM::VSITOS; |
| 1262 | else if (Ty->isDoubleTy()) Opc = ARM::VSITOD; |
| 1263 | else return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1264 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1265 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1266 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1267 | ResultReg) |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1268 | .addReg(FP)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1269 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1270 | return true; |
| 1271 | } |
| 1272 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1273 | bool ARMFastISel::SelectFPToSI(const Instruction *I) { |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1274 | // Make sure we have VFP. |
| 1275 | if (!Subtarget->hasVFP2()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1276 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1277 | MVT DstVT; |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1278 | const Type *RetTy = I->getType(); |
Eric Christopher | 920a208 | 2010-09-10 00:35:09 +0000 | [diff] [blame] | 1279 | if (!isTypeLegal(RetTy, DstVT)) |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1280 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1281 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1282 | unsigned Op = getRegForValue(I->getOperand(0)); |
| 1283 | if (Op == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1284 | |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1285 | unsigned Opc; |
| 1286 | const Type *OpTy = I->getOperand(0)->getType(); |
| 1287 | if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS; |
| 1288 | else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD; |
| 1289 | else return 0; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1290 | |
Eric Christopher | 022b7fb | 2010-10-05 23:13:24 +0000 | [diff] [blame] | 1291 | // f64->s32 or f32->s32 both need an intermediate f32 reg. |
| 1292 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32)); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1293 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 1294 | ResultReg) |
| 1295 | .addReg(Op)); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1296 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1297 | // This result needs to be in an integer register, but the conversion only |
| 1298 | // takes place in fp-regs. |
Eric Christopher | db12b2b | 2010-09-10 00:34:35 +0000 | [diff] [blame] | 1299 | unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg); |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1300 | if (IntReg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1301 | |
Eric Christopher | 9ee4ce2 | 2010-09-09 21:44:45 +0000 | [diff] [blame] | 1302 | UpdateValueMap(I, IntReg); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1303 | return true; |
| 1304 | } |
| 1305 | |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1306 | bool ARMFastISel::SelectSelect(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1307 | MVT VT; |
| 1308 | if (!isTypeLegal(I->getType(), VT)) |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | |
| 1311 | // Things need to be register sized for register moves. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1312 | if (VT != MVT::i32) return false; |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1313 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 1314 | |
| 1315 | unsigned CondReg = getRegForValue(I->getOperand(0)); |
| 1316 | if (CondReg == 0) return false; |
| 1317 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 1318 | if (Op1Reg == 0) return false; |
| 1319 | unsigned Op2Reg = getRegForValue(I->getOperand(2)); |
| 1320 | if (Op2Reg == 0) return false; |
| 1321 | |
| 1322 | unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri; |
| 1323 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 1324 | .addReg(CondReg).addImm(1)); |
| 1325 | unsigned ResultReg = createResultReg(RC); |
| 1326 | unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr; |
| 1327 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg) |
| 1328 | .addReg(Op1Reg).addReg(Op2Reg) |
| 1329 | .addImm(ARMCC::EQ).addReg(ARM::CPSR); |
| 1330 | UpdateValueMap(I, ResultReg); |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1334 | bool ARMFastISel::SelectSDiv(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1335 | MVT VT; |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1336 | const Type *Ty = I->getType(); |
| 1337 | if (!isTypeLegal(Ty, VT)) |
| 1338 | return false; |
| 1339 | |
| 1340 | // If we have integer div support we should have selected this automagically. |
| 1341 | // In case we have a real miss go ahead and return false and we'll pick |
| 1342 | // it up later. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1343 | if (Subtarget->hasDivide()) return false; |
| 1344 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1345 | // Otherwise emit a libcall. |
| 1346 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Eric Christopher | 7bdc4de | 2010-10-11 08:31:54 +0000 | [diff] [blame] | 1347 | if (VT == MVT::i8) |
| 1348 | LC = RTLIB::SDIV_I8; |
| 1349 | else if (VT == MVT::i16) |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1350 | LC = RTLIB::SDIV_I16; |
| 1351 | else if (VT == MVT::i32) |
| 1352 | LC = RTLIB::SDIV_I32; |
| 1353 | else if (VT == MVT::i64) |
| 1354 | LC = RTLIB::SDIV_I64; |
| 1355 | else if (VT == MVT::i128) |
| 1356 | LC = RTLIB::SDIV_I128; |
| 1357 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1358 | |
Eric Christopher | 0863785 | 2010-09-30 22:34:19 +0000 | [diff] [blame] | 1359 | return ARMEmitLibcall(I, LC); |
| 1360 | } |
| 1361 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1362 | bool ARMFastISel::SelectSRem(const Instruction *I) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1363 | MVT VT; |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1364 | const Type *Ty = I->getType(); |
| 1365 | if (!isTypeLegal(Ty, VT)) |
| 1366 | return false; |
| 1367 | |
| 1368 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 1369 | if (VT == MVT::i8) |
| 1370 | LC = RTLIB::SREM_I8; |
| 1371 | else if (VT == MVT::i16) |
| 1372 | LC = RTLIB::SREM_I16; |
| 1373 | else if (VT == MVT::i32) |
| 1374 | LC = RTLIB::SREM_I32; |
| 1375 | else if (VT == MVT::i64) |
| 1376 | LC = RTLIB::SREM_I64; |
| 1377 | else if (VT == MVT::i128) |
| 1378 | LC = RTLIB::SREM_I128; |
Eric Christopher | a1640d9 | 2010-10-11 08:40:05 +0000 | [diff] [blame] | 1379 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); |
Eric Christopher | 2896df8 | 2010-10-15 18:02:07 +0000 | [diff] [blame] | 1380 | |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1381 | return ARMEmitLibcall(I, LC); |
| 1382 | } |
| 1383 | |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1384 | bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) { |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1385 | EVT VT = TLI.getValueType(I->getType(), true); |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1386 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1387 | // We can get here in the case when we want to use NEON for our fp |
| 1388 | // operations, but can't figure out how to. Just use the vfp instructions |
| 1389 | // if we have them. |
| 1390 | // FIXME: It'd be nice to use NEON instructions. |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1391 | const Type *Ty = I->getType(); |
| 1392 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 1393 | if (isFloat && !Subtarget->hasVFP2()) |
| 1394 | return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1395 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1396 | unsigned Op1 = getRegForValue(I->getOperand(0)); |
| 1397 | if (Op1 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1398 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1399 | unsigned Op2 = getRegForValue(I->getOperand(1)); |
| 1400 | if (Op2 == 0) return false; |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1401 | |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1402 | unsigned Opc; |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 1403 | bool is64bit = VT == MVT::f64 || VT == MVT::i64; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1404 | switch (ISDOpcode) { |
| 1405 | default: return false; |
| 1406 | case ISD::FADD: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1407 | Opc = is64bit ? ARM::VADDD : ARM::VADDS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | case ISD::FSUB: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1410 | Opc = is64bit ? ARM::VSUBD : ARM::VSUBS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | case ISD::FMUL: |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1413 | Opc = is64bit ? ARM::VMULD : ARM::VMULS; |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | } |
Eric Christopher | bd6bf08 | 2010-09-09 01:02:03 +0000 | [diff] [blame] | 1416 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1417 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1418 | TII.get(Opc), ResultReg) |
| 1419 | .addReg(Op1).addReg(Op2)); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1420 | UpdateValueMap(I, ResultReg); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1421 | return true; |
| 1422 | } |
| 1423 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1424 | // Call Handling Code |
| 1425 | |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1426 | bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, |
| 1427 | EVT SrcVT, unsigned &ResultReg) { |
| 1428 | unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, |
| 1429 | Src, /*TODO: Kill=*/false); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1430 | |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1431 | if (RR != 0) { |
| 1432 | ResultReg = RR; |
| 1433 | return true; |
| 1434 | } else |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1435 | return false; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1438 | // This is largely taken directly from CCAssignFnForNode - we don't support |
| 1439 | // varargs in FastISel so that part has been removed. |
| 1440 | // TODO: We may not support all of this. |
| 1441 | CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) { |
| 1442 | switch (CC) { |
| 1443 | default: |
| 1444 | llvm_unreachable("Unsupported calling convention"); |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1445 | case CallingConv::Fast: |
Evan Cheng | 1f8b40d | 2010-10-22 18:57:05 +0000 | [diff] [blame] | 1446 | // Ignore fastcc. Silence compiler warnings. |
| 1447 | (void)RetFastCC_ARM_APCS; |
| 1448 | (void)FastCC_ARM_APCS; |
| 1449 | // Fallthrough |
| 1450 | case CallingConv::C: |
Eric Christopher | d10cd7b | 2010-09-10 23:18:12 +0000 | [diff] [blame] | 1451 | // Use target triple & subtarget features to do actual dispatch. |
| 1452 | if (Subtarget->isAAPCS_ABI()) { |
| 1453 | if (Subtarget->hasVFP2() && |
| 1454 | FloatABIType == FloatABI::Hard) |
| 1455 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1456 | else |
| 1457 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1458 | } else |
| 1459 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1460 | case CallingConv::ARM_AAPCS_VFP: |
| 1461 | return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP); |
| 1462 | case CallingConv::ARM_AAPCS: |
| 1463 | return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS); |
| 1464 | case CallingConv::ARM_APCS: |
| 1465 | return (Return ? RetCC_ARM_APCS: CC_ARM_APCS); |
| 1466 | } |
| 1467 | } |
| 1468 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1469 | bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args, |
| 1470 | SmallVectorImpl<unsigned> &ArgRegs, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1471 | SmallVectorImpl<MVT> &ArgVTs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1472 | SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, |
| 1473 | SmallVectorImpl<unsigned> &RegArgs, |
| 1474 | CallingConv::ID CC, |
| 1475 | unsigned &NumBytes) { |
| 1476 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1477 | CCState CCInfo(CC, false, TM, ArgLocs, *Context); |
| 1478 | CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false)); |
| 1479 | |
| 1480 | // Get a count of how many bytes are to be pushed on the stack. |
| 1481 | NumBytes = CCInfo.getNextStackOffset(); |
| 1482 | |
| 1483 | // Issue CALLSEQ_START |
| 1484 | unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1485 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1486 | TII.get(AdjStackDown)) |
| 1487 | .addImm(NumBytes)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Process the args. |
| 1490 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1491 | CCValAssign &VA = ArgLocs[i]; |
| 1492 | unsigned Arg = ArgRegs[VA.getValNo()]; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1493 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1494 | |
Eric Christopher | 4a2b316 | 2011-01-27 05:44:56 +0000 | [diff] [blame] | 1495 | // We don't handle NEON/vector parameters yet. |
| 1496 | if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64) |
Eric Christopher | a4633f5 | 2010-10-23 09:37:17 +0000 | [diff] [blame] | 1497 | return false; |
| 1498 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1499 | // Handle arg promotion, etc. |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1500 | switch (VA.getLocInfo()) { |
| 1501 | case CCValAssign::Full: break; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1502 | case CCValAssign::SExt: { |
| 1503 | bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1504 | Arg, ArgVT, Arg); |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1505 | assert(Emitted && "Failed to emit a sext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1506 | Emitted = true; |
| 1507 | ArgVT = VA.getLocVT(); |
| 1508 | break; |
| 1509 | } |
| 1510 | case CCValAssign::ZExt: { |
| 1511 | bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1512 | Arg, ArgVT, Arg); |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1513 | assert(Emitted && "Failed to emit a zext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1514 | Emitted = true; |
| 1515 | ArgVT = VA.getLocVT(); |
| 1516 | break; |
| 1517 | } |
| 1518 | case CCValAssign::AExt: { |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1519 | bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), |
| 1520 | Arg, ArgVT, Arg); |
| 1521 | if (!Emitted) |
| 1522 | Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1523 | Arg, ArgVT, Arg); |
| 1524 | if (!Emitted) |
| 1525 | Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1526 | Arg, ArgVT, Arg); |
| 1527 | |
Chris Lattner | 54c6d6f | 2011-01-05 18:41:05 +0000 | [diff] [blame] | 1528 | assert(Emitted && "Failed to emit a aext!"); (void)Emitted; |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1529 | ArgVT = VA.getLocVT(); |
| 1530 | break; |
| 1531 | } |
| 1532 | case CCValAssign::BCvt: { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1533 | unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1534 | /*TODO: Kill=*/false); |
Eric Christopher | fa87d66 | 2010-10-18 02:17:53 +0000 | [diff] [blame] | 1535 | assert(BC != 0 && "Failed to emit a bitcast!"); |
| 1536 | Arg = BC; |
| 1537 | ArgVT = VA.getLocVT(); |
| 1538 | break; |
| 1539 | } |
| 1540 | default: llvm_unreachable("Unknown arg promotion!"); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // Now copy/store arg to correct locations. |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1544 | if (VA.isRegLoc() && !VA.needsCustom()) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1545 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1546 | VA.getLocReg()) |
| 1547 | .addReg(Arg); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1548 | RegArgs.push_back(VA.getLocReg()); |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1549 | } else if (VA.needsCustom()) { |
| 1550 | // TODO: We need custom lowering for vector (v2f64) args. |
| 1551 | if (VA.getLocVT() != MVT::f64) return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1552 | |
Eric Christopher | 2d8f6fe | 2010-10-21 00:01:47 +0000 | [diff] [blame] | 1553 | CCValAssign &NextVA = ArgLocs[++i]; |
| 1554 | |
| 1555 | // TODO: Only handle register args for now. |
| 1556 | if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false; |
| 1557 | |
| 1558 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1559 | TII.get(ARM::VMOVRRD), VA.getLocReg()) |
| 1560 | .addReg(NextVA.getLocReg(), RegState::Define) |
| 1561 | .addReg(Arg)); |
| 1562 | RegArgs.push_back(VA.getLocReg()); |
| 1563 | RegArgs.push_back(NextVA.getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1564 | } else { |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1565 | assert(VA.isMemLoc()); |
| 1566 | // Need to store on the stack. |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1567 | Address Addr; |
| 1568 | Addr.BaseType = Address::RegBase; |
| 1569 | Addr.Base.Reg = ARM::SP; |
| 1570 | Addr.Offset = VA.getLocMemOffset(); |
Eric Christopher | 5b92480 | 2010-10-21 20:09:54 +0000 | [diff] [blame] | 1571 | |
Eric Christopher | 0d58122 | 2010-11-19 22:30:02 +0000 | [diff] [blame] | 1572 | if (!ARMEmitStore(ArgVT, Arg, Addr)) return false; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1575 | return true; |
| 1576 | } |
| 1577 | |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1578 | bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1579 | const Instruction *I, CallingConv::ID CC, |
| 1580 | unsigned &NumBytes) { |
| 1581 | // Issue CALLSEQ_END |
| 1582 | unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode(); |
Eric Christopher | fb0b892 | 2010-10-11 21:20:02 +0000 | [diff] [blame] | 1583 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1584 | TII.get(AdjStackUp)) |
| 1585 | .addImm(NumBytes).addImm(0)); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Now the return value. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1588 | if (RetVT != MVT::isVoid) { |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1589 | SmallVector<CCValAssign, 16> RVLocs; |
| 1590 | CCState CCInfo(CC, false, TM, RVLocs, *Context); |
| 1591 | CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true)); |
| 1592 | |
| 1593 | // Copy all of the result registers out of their specified physreg. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1594 | if (RVLocs.size() == 2 && RetVT == MVT::f64) { |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1595 | // For this move we copy into two registers and then move into the |
| 1596 | // double fp reg we want. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1597 | EVT DestVT = RVLocs[0].getValVT(); |
| 1598 | TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT); |
| 1599 | unsigned ResultReg = createResultReg(DstRC); |
| 1600 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1601 | TII.get(ARM::VMOVDRR), ResultReg) |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1602 | .addReg(RVLocs[0].getLocReg()) |
| 1603 | .addReg(RVLocs[1].getLocReg())); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1604 | |
Eric Christopher | 3659ac2 | 2010-10-20 08:02:24 +0000 | [diff] [blame] | 1605 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
| 1606 | UsedRegs.push_back(RVLocs[1].getLocReg()); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1607 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1608 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1609 | UpdateValueMap(I, ResultReg); |
| 1610 | } else { |
Jim Grosbach | 9536959 | 2010-10-13 23:34:31 +0000 | [diff] [blame] | 1611 | assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!"); |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1612 | EVT CopyVT = RVLocs[0].getValVT(); |
| 1613 | TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1614 | |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1615 | unsigned ResultReg = createResultReg(DstRC); |
| 1616 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1617 | ResultReg).addReg(RVLocs[0].getLocReg()); |
| 1618 | UsedRegs.push_back(RVLocs[0].getLocReg()); |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1619 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1620 | // Finally update the result. |
Eric Christopher | 14df882 | 2010-10-01 00:00:11 +0000 | [diff] [blame] | 1621 | UpdateValueMap(I, ResultReg); |
| 1622 | } |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1625 | return true; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1628 | bool ARMFastISel::SelectRet(const Instruction *I) { |
| 1629 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 1630 | const Function &F = *I->getParent()->getParent(); |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1631 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1632 | if (!FuncInfo.CanLowerReturn) |
| 1633 | return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1634 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1635 | if (F.isVarArg()) |
| 1636 | return false; |
| 1637 | |
| 1638 | CallingConv::ID CC = F.getCallingConv(); |
| 1639 | if (Ret->getNumOperands() > 0) { |
| 1640 | SmallVector<ISD::OutputArg, 4> Outs; |
| 1641 | GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(), |
| 1642 | Outs, TLI); |
| 1643 | |
| 1644 | // Analyze operands of the call, assigning locations to each operand. |
| 1645 | SmallVector<CCValAssign, 16> ValLocs; |
| 1646 | CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext()); |
| 1647 | CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */)); |
| 1648 | |
| 1649 | const Value *RV = Ret->getOperand(0); |
| 1650 | unsigned Reg = getRegForValue(RV); |
| 1651 | if (Reg == 0) |
| 1652 | return false; |
| 1653 | |
| 1654 | // Only handle a single return value for now. |
| 1655 | if (ValLocs.size() != 1) |
| 1656 | return false; |
| 1657 | |
| 1658 | CCValAssign &VA = ValLocs[0]; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1659 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1660 | // Don't bother handling odd stuff for now. |
| 1661 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1662 | return false; |
| 1663 | // Only handle register returns for now. |
| 1664 | if (!VA.isRegLoc()) |
| 1665 | return false; |
| 1666 | // TODO: For now, don't try to handle cases where getLocInfo() |
| 1667 | // says Full but the types don't match. |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 1668 | if (TLI.getValueType(RV->getType()) != VA.getValVT()) |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1669 | return false; |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1670 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1671 | // Make the copy. |
| 1672 | unsigned SrcReg = Reg + VA.getValNo(); |
| 1673 | unsigned DstReg = VA.getLocReg(); |
| 1674 | const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg); |
| 1675 | // Avoid a cross-class copy. This is very unlikely. |
| 1676 | if (!SrcRC->contains(DstReg)) |
| 1677 | return false; |
| 1678 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1679 | DstReg).addReg(SrcReg); |
| 1680 | |
| 1681 | // Mark the register as live out of the function. |
| 1682 | MRI.addLiveOut(VA.getLocReg()); |
| 1683 | } |
Jim Grosbach | 6b15639 | 2010-10-27 21:39:08 +0000 | [diff] [blame] | 1684 | |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1685 | unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET; |
| 1686 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1687 | TII.get(RetOpc))); |
| 1688 | return true; |
| 1689 | } |
| 1690 | |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1691 | unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) { |
| 1692 | |
| 1693 | // Depend our opcode for thumb on whether or not we're targeting an |
| 1694 | // externally callable function. For libcalls we'll just pass a NULL GV |
| 1695 | // in here. |
| 1696 | bool isExternal = false; |
| 1697 | if (!GV || GV->hasExternalLinkage()) isExternal = true; |
| 1698 | |
| 1699 | // Darwin needs the r9 versions of the opcodes. |
| 1700 | bool isDarwin = Subtarget->isTargetDarwin(); |
| 1701 | if (isThumb && isExternal) { |
| 1702 | return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi; |
| 1703 | } else if (isThumb) { |
| 1704 | return isDarwin ? ARM::tBLr9 : ARM::tBL; |
| 1705 | } else { |
| 1706 | return isDarwin ? ARM::BLr9 : ARM::BL; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1710 | // A quick function that will emit a call for a named libcall in F with the |
| 1711 | // 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] | 1712 | // can emit a call for any libcall we can produce. This is an abridged version |
| 1713 | // 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] | 1714 | // like computed function pointers or strange arguments at call sites. |
| 1715 | // TODO: Try to unify this and the normal call bits for ARM, then try to unify |
| 1716 | // with X86. |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1717 | bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) { |
| 1718 | CallingConv::ID CC = TLI.getLibcallCallingConv(Call); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1719 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1720 | // Handle *simple* calls for now. |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1721 | const Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1722 | MVT RetVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1723 | if (RetTy->isVoidTy()) |
| 1724 | RetVT = MVT::isVoid; |
| 1725 | else if (!isTypeLegal(RetTy, RetVT)) |
| 1726 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1727 | |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1728 | // For now we're using BLX etc on the assumption that we have v5t ops. |
| 1729 | if (!Subtarget->hasV5TOps()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1730 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 1731 | // TODO: For now if we have long calls specified we don't handle the call. |
| 1732 | if (EnableARMLongCalls) return false; |
| 1733 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1734 | // Set up the argument vectors. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1735 | SmallVector<Value*, 8> Args; |
| 1736 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1737 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1738 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 1739 | Args.reserve(I->getNumOperands()); |
| 1740 | ArgRegs.reserve(I->getNumOperands()); |
| 1741 | ArgVTs.reserve(I->getNumOperands()); |
| 1742 | ArgFlags.reserve(I->getNumOperands()); |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1743 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1744 | Value *Op = I->getOperand(i); |
| 1745 | unsigned Arg = getRegForValue(Op); |
| 1746 | if (Arg == 0) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1747 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1748 | const Type *ArgTy = Op->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1749 | MVT ArgVT; |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1750 | if (!isTypeLegal(ArgTy, ArgVT)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1751 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1752 | ISD::ArgFlagsTy Flags; |
| 1753 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 1754 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1755 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1756 | Args.push_back(Op); |
| 1757 | ArgRegs.push_back(Arg); |
| 1758 | ArgVTs.push_back(ArgVT); |
| 1759 | ArgFlags.push_back(Flags); |
| 1760 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1761 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1762 | // Handle the arguments now that we've gotten them. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1763 | SmallVector<unsigned, 4> RegArgs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1764 | unsigned NumBytes; |
| 1765 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 1766 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1767 | |
Eric Christopher | 7ed8ec9 | 2010-09-28 01:21:42 +0000 | [diff] [blame] | 1768 | // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1769 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1770 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1771 | unsigned CallOpc = ARMSelectCallOp(NULL); |
| 1772 | if(isThumb) |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1773 | // Explicitly adding the predicate here. |
| 1774 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1775 | TII.get(CallOpc))) |
| 1776 | .addExternalSymbol(TLI.getLibcallName(Call)); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1777 | else |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1778 | // Explicitly adding the predicate here. |
| 1779 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1780 | TII.get(CallOpc)) |
| 1781 | .addExternalSymbol(TLI.getLibcallName(Call))); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1782 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1783 | // Add implicit physical register uses to the call. |
| 1784 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 1785 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1786 | |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1787 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1788 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | a9a7a1a | 2010-09-29 23:11:09 +0000 | [diff] [blame] | 1789 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1790 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1791 | // Set all unused physreg defs as dead. |
| 1792 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1793 | |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1794 | return true; |
| 1795 | } |
| 1796 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1797 | bool ARMFastISel::SelectCall(const Instruction *I) { |
| 1798 | const CallInst *CI = cast<CallInst>(I); |
| 1799 | const Value *Callee = CI->getCalledValue(); |
| 1800 | |
| 1801 | // Can't handle inline asm or worry about intrinsics yet. |
| 1802 | if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false; |
| 1803 | |
Eric Christopher | e6ca677 | 2010-10-01 21:33:12 +0000 | [diff] [blame] | 1804 | // Only handle global variable Callees that are direct calls. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1805 | const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); |
Eric Christopher | e6ca677 | 2010-10-01 21:33:12 +0000 | [diff] [blame] | 1806 | if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel())) |
| 1807 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1808 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1809 | // Check the calling convention. |
| 1810 | ImmutableCallSite CS(CI); |
| 1811 | CallingConv::ID CC = CS.getCallingConv(); |
Eric Christopher | 4cf34c6 | 2010-10-18 06:49:12 +0000 | [diff] [blame] | 1812 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1813 | // TODO: Avoid some calling conventions? |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1814 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1815 | // Let SDISel handle vararg functions. |
| 1816 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 1817 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 1818 | if (FTy->isVarArg()) |
| 1819 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1820 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1821 | // Handle *simple* calls for now. |
| 1822 | const Type *RetTy = I->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1823 | MVT RetVT; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1824 | if (RetTy->isVoidTy()) |
| 1825 | RetVT = MVT::isVoid; |
| 1826 | else if (!isTypeLegal(RetTy, RetVT)) |
| 1827 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1828 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1829 | // For now we're using BLX etc on the assumption that we have v5t ops. |
| 1830 | // TODO: Maybe? |
| 1831 | if (!Subtarget->hasV5TOps()) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1832 | |
Eric Christopher | 836c624 | 2010-12-15 23:47:29 +0000 | [diff] [blame] | 1833 | // TODO: For now if we have long calls specified we don't handle the call. |
| 1834 | if (EnableARMLongCalls) return false; |
| 1835 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1836 | // Set up the argument vectors. |
| 1837 | SmallVector<Value*, 8> Args; |
| 1838 | SmallVector<unsigned, 8> ArgRegs; |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1839 | SmallVector<MVT, 8> ArgVTs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1840 | SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; |
| 1841 | Args.reserve(CS.arg_size()); |
| 1842 | ArgRegs.reserve(CS.arg_size()); |
| 1843 | ArgVTs.reserve(CS.arg_size()); |
| 1844 | ArgFlags.reserve(CS.arg_size()); |
| 1845 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 1846 | i != e; ++i) { |
| 1847 | unsigned Arg = getRegForValue(*i); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1848 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1849 | if (Arg == 0) |
| 1850 | return false; |
| 1851 | ISD::ArgFlagsTy Flags; |
| 1852 | unsigned AttrInd = i - CS.arg_begin() + 1; |
| 1853 | if (CS.paramHasAttr(AttrInd, Attribute::SExt)) |
| 1854 | Flags.setSExt(); |
| 1855 | if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) |
| 1856 | Flags.setZExt(); |
| 1857 | |
| 1858 | // FIXME: Only handle *easy* calls for now. |
| 1859 | if (CS.paramHasAttr(AttrInd, Attribute::InReg) || |
| 1860 | CS.paramHasAttr(AttrInd, Attribute::StructRet) || |
| 1861 | CS.paramHasAttr(AttrInd, Attribute::Nest) || |
| 1862 | CS.paramHasAttr(AttrInd, Attribute::ByVal)) |
| 1863 | return false; |
| 1864 | |
| 1865 | const Type *ArgTy = (*i)->getType(); |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 1866 | MVT ArgVT; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1867 | if (!isTypeLegal(ArgTy, ArgVT)) |
| 1868 | return false; |
| 1869 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 1870 | Flags.setOrigAlign(OriginalAlignment); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1871 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1872 | Args.push_back(*i); |
| 1873 | ArgRegs.push_back(Arg); |
| 1874 | ArgVTs.push_back(ArgVT); |
| 1875 | ArgFlags.push_back(Flags); |
| 1876 | } |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1877 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1878 | // Handle the arguments now that we've gotten them. |
| 1879 | SmallVector<unsigned, 4> RegArgs; |
| 1880 | unsigned NumBytes; |
| 1881 | if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes)) |
| 1882 | return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1883 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1884 | // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1885 | // TODO: Turn this into the table of arm call ops. |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1886 | MachineInstrBuilder MIB; |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1887 | unsigned CallOpc = ARMSelectCallOp(GV); |
Eric Christopher | 7bb5996 | 2010-11-29 21:56:23 +0000 | [diff] [blame] | 1888 | // Explicitly adding the predicate here. |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1889 | if(isThumb) |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1890 | // Explicitly adding the predicate here. |
| 1891 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1892 | TII.get(CallOpc))) |
| 1893 | .addGlobalAddress(GV, 0, 0); |
Eric Christopher | 872f4a2 | 2011-02-22 01:37:10 +0000 | [diff] [blame] | 1894 | else |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1895 | // Explicitly adding the predicate here. |
| 1896 | MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 1897 | TII.get(CallOpc)) |
| 1898 | .addGlobalAddress(GV, 0, 0)); |
Eric Christopher | c19aadb | 2010-12-21 03:50:43 +0000 | [diff] [blame] | 1899 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1900 | // Add implicit physical register uses to the call. |
| 1901 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 1902 | MIB.addReg(RegArgs[i]); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1903 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1904 | // Finish off the call including any return values. |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1905 | SmallVector<unsigned, 4> UsedRegs; |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1906 | if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1907 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1908 | // Set all unused physreg defs as dead. |
| 1909 | static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1910 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1911 | return true; |
Eric Christopher | dccd2c3 | 2010-10-11 08:38:55 +0000 | [diff] [blame] | 1912 | |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 1915 | // TODO: SoftFP support. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1916 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | ac1a19e | 2010-09-09 01:06:51 +0000 | [diff] [blame] | 1917 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1918 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 1919 | case Instruction::Load: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1920 | return SelectLoad(I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 1921 | case Instruction::Store: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1922 | return SelectStore(I); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 1923 | case Instruction::Br: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1924 | return SelectBranch(I); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 1925 | case Instruction::ICmp: |
| 1926 | case Instruction::FCmp: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1927 | return SelectCmp(I); |
Eric Christopher | 4620360 | 2010-09-09 00:26:48 +0000 | [diff] [blame] | 1928 | case Instruction::FPExt: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1929 | return SelectFPExt(I); |
Eric Christopher | ce07b54 | 2010-09-09 20:26:31 +0000 | [diff] [blame] | 1930 | case Instruction::FPTrunc: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1931 | return SelectFPTrunc(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1932 | case Instruction::SIToFP: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1933 | return SelectSIToFP(I); |
Eric Christopher | 9a04049 | 2010-09-09 18:54:59 +0000 | [diff] [blame] | 1934 | case Instruction::FPToSI: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1935 | return SelectFPToSI(I); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1936 | case Instruction::FAdd: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1937 | return SelectBinaryOp(I, ISD::FADD); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1938 | case Instruction::FSub: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1939 | return SelectBinaryOp(I, ISD::FSUB); |
Eric Christopher | bc39b82 | 2010-09-09 00:53:57 +0000 | [diff] [blame] | 1940 | case Instruction::FMul: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1941 | return SelectBinaryOp(I, ISD::FMUL); |
Eric Christopher | bb3e5da | 2010-09-14 23:03:37 +0000 | [diff] [blame] | 1942 | case Instruction::SDiv: |
Eric Christopher | 43b62be | 2010-09-27 06:02:23 +0000 | [diff] [blame] | 1943 | return SelectSDiv(I); |
Eric Christopher | 6a880d6 | 2010-10-11 08:37:26 +0000 | [diff] [blame] | 1944 | case Instruction::SRem: |
| 1945 | return SelectSRem(I); |
Eric Christopher | f9764fa | 2010-09-30 20:49:44 +0000 | [diff] [blame] | 1946 | case Instruction::Call: |
| 1947 | return SelectCall(I); |
Eric Christopher | 3bbd396 | 2010-10-11 08:27:59 +0000 | [diff] [blame] | 1948 | case Instruction::Select: |
| 1949 | return SelectSelect(I); |
Eric Christopher | 4f512ef | 2010-10-22 01:28:00 +0000 | [diff] [blame] | 1950 | case Instruction::Ret: |
| 1951 | return SelectRet(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1952 | default: break; |
| 1953 | } |
| 1954 | return false; |
| 1955 | } |
| 1956 | |
| 1957 | namespace llvm { |
| 1958 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1959 | // Completely untested on non-darwin. |
| 1960 | const TargetMachine &TM = funcInfo.MF->getTarget(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1961 | |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 1962 | // Darwin and thumb1 only for now. |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1963 | const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Jim Grosbach | 16cb376 | 2010-11-09 19:22:26 +0000 | [diff] [blame] | 1964 | if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() && |
Eric Christopher | aaa8df4 | 2010-11-02 01:21:28 +0000 | [diff] [blame] | 1965 | !DisableARMFastISel) |
Eric Christopher | feadddd | 2010-10-11 20:05:22 +0000 | [diff] [blame] | 1966 | return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 1967 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |