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