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 | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 18 | #include "ARMRegisterInfo.h" |
| 19 | #include "ARMTargetMachine.h" |
| 20 | #include "ARMSubtarget.h" |
| 21 | #include "llvm/CallingConv.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/IntrinsicInst.h" |
| 26 | #include "llvm/CodeGen/Analysis.h" |
| 27 | #include "llvm/CodeGen/FastISel.h" |
| 28 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 32 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 34 | #include "llvm/Support/CallSite.h" |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
| 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetLowering.h" |
| 41 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetOptions.h" |
| 43 | using namespace llvm; |
| 44 | |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | EnableARMFastISel("arm-fast-isel", |
| 47 | cl::desc("Turn on experimental ARM fast-isel support"), |
| 48 | cl::init(false), cl::Hidden); |
| 49 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 52 | class ARMFastISel : public FastISel { |
| 53 | |
| 54 | /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can |
| 55 | /// make the right decision when generating code for different targets. |
| 56 | const ARMSubtarget *Subtarget; |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 57 | const TargetMachine &TM; |
| 58 | const TargetInstrInfo &TII; |
| 59 | const TargetLowering &TLI; |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 60 | const ARMFunctionInfo *AFI; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 61 | |
| 62 | public: |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 63 | explicit ARMFastISel(FunctionLoweringInfo &funcInfo) |
| 64 | : FastISel(funcInfo), |
| 65 | TM(funcInfo.MF->getTarget()), |
| 66 | TII(*TM.getInstrInfo()), |
| 67 | TLI(*TM.getTargetLowering()) { |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 68 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 69 | AFI = funcInfo.MF->getInfo<ARMFunctionInfo>(); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 72 | // Code from FastISel.cpp. |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 73 | virtual unsigned FastEmitInst_(unsigned MachineInstOpcode, |
| 74 | const TargetRegisterClass *RC); |
| 75 | virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode, |
| 76 | const TargetRegisterClass *RC, |
| 77 | unsigned Op0, bool Op0IsKill); |
| 78 | virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode, |
| 79 | const TargetRegisterClass *RC, |
| 80 | unsigned Op0, bool Op0IsKill, |
| 81 | unsigned Op1, bool Op1IsKill); |
| 82 | virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode, |
| 83 | const TargetRegisterClass *RC, |
| 84 | unsigned Op0, bool Op0IsKill, |
| 85 | uint64_t Imm); |
| 86 | virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode, |
| 87 | const TargetRegisterClass *RC, |
| 88 | unsigned Op0, bool Op0IsKill, |
| 89 | const ConstantFP *FPImm); |
| 90 | virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode, |
| 91 | const TargetRegisterClass *RC, |
| 92 | uint64_t Imm); |
| 93 | virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode, |
| 94 | const TargetRegisterClass *RC, |
| 95 | unsigned Op0, bool Op0IsKill, |
| 96 | unsigned Op1, bool Op1IsKill, |
| 97 | uint64_t Imm); |
| 98 | virtual unsigned FastEmitInst_extractsubreg(MVT RetVT, |
| 99 | unsigned Op0, bool Op0IsKill, |
| 100 | uint32_t Idx); |
Eric Christopher | cb59229 | 2010-08-20 00:20:31 +0000 | [diff] [blame] | 101 | |
| 102 | // Backend specific FastISel code. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 103 | virtual bool TargetSelectInstruction(const Instruction *I); |
| 104 | |
| 105 | #include "ARMGenFastISel.inc" |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 106 | |
| 107 | // Instruction selection routines. |
| 108 | virtual bool ARMSelectLoad(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 109 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 110 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 111 | private: |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 112 | bool isTypeLegal(const Type *Ty, EVT &VT); |
| 113 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset); |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 114 | bool ARMLoadAlloca(const Instruction *I); |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 115 | bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 116 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 117 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 118 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
| 119 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 120 | |
| 121 | } // end anonymous namespace |
| 122 | |
| 123 | // #include "ARMGenCallingConv.inc" |
| 124 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 125 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 126 | // we don't care about implicit defs here, just places we'll need to add a |
| 127 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 128 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
| 129 | const TargetInstrDesc &TID = MI->getDesc(); |
| 130 | if (!TID.hasOptionalDef()) |
| 131 | return false; |
| 132 | |
| 133 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 134 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 135 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 136 | if (!MO.isReg() || !MO.isDef()) continue; |
| 137 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 138 | *CPSR = true; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // If the machine is predicable go ahead and add the predicate operands, if |
| 144 | // it needs default CC operands add those. |
| 145 | const MachineInstrBuilder & |
| 146 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 147 | MachineInstr *MI = &*MIB; |
| 148 | |
| 149 | // Do we use a predicate? |
| 150 | if (TII.isPredicable(MI)) |
| 151 | AddDefaultPred(MIB); |
| 152 | |
| 153 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 154 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 155 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 156 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 157 | if (CPSR) |
| 158 | AddDefaultT1CC(MIB); |
| 159 | else |
| 160 | AddDefaultCC(MIB); |
| 161 | } |
| 162 | return MIB; |
| 163 | } |
| 164 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 165 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 166 | const TargetRegisterClass* RC) { |
| 167 | unsigned ResultReg = createResultReg(RC); |
| 168 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 169 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 170 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 171 | return ResultReg; |
| 172 | } |
| 173 | |
| 174 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 175 | const TargetRegisterClass *RC, |
| 176 | unsigned Op0, bool Op0IsKill) { |
| 177 | unsigned ResultReg = createResultReg(RC); |
| 178 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 179 | |
| 180 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 181 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 182 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 183 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 184 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 185 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 186 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 187 | TII.get(TargetOpcode::COPY), ResultReg) |
| 188 | .addReg(II.ImplicitDefs[0])); |
| 189 | } |
| 190 | return ResultReg; |
| 191 | } |
| 192 | |
| 193 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 194 | const TargetRegisterClass *RC, |
| 195 | unsigned Op0, bool Op0IsKill, |
| 196 | unsigned Op1, bool Op1IsKill) { |
| 197 | unsigned ResultReg = createResultReg(RC); |
| 198 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 199 | |
| 200 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 201 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 202 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 203 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 204 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 205 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 206 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 207 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 208 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 209 | TII.get(TargetOpcode::COPY), ResultReg) |
| 210 | .addReg(II.ImplicitDefs[0])); |
| 211 | } |
| 212 | return ResultReg; |
| 213 | } |
| 214 | |
| 215 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 216 | const TargetRegisterClass *RC, |
| 217 | unsigned Op0, bool Op0IsKill, |
| 218 | uint64_t Imm) { |
| 219 | unsigned ResultReg = createResultReg(RC); |
| 220 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 221 | |
| 222 | if (II.getNumDefs() >= 1) |
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 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 225 | .addImm(Imm)); |
| 226 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 227 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 228 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 229 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 230 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 231 | TII.get(TargetOpcode::COPY), ResultReg) |
| 232 | .addReg(II.ImplicitDefs[0])); |
| 233 | } |
| 234 | return ResultReg; |
| 235 | } |
| 236 | |
| 237 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 238 | const TargetRegisterClass *RC, |
| 239 | unsigned Op0, bool Op0IsKill, |
| 240 | const ConstantFP *FPImm) { |
| 241 | unsigned ResultReg = createResultReg(RC); |
| 242 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 243 | |
| 244 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 245 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 246 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 247 | .addFPImm(FPImm)); |
| 248 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 249 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 250 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 251 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 252 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 253 | TII.get(TargetOpcode::COPY), ResultReg) |
| 254 | .addReg(II.ImplicitDefs[0])); |
| 255 | } |
| 256 | return ResultReg; |
| 257 | } |
| 258 | |
| 259 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 260 | const TargetRegisterClass *RC, |
| 261 | unsigned Op0, bool Op0IsKill, |
| 262 | unsigned Op1, bool Op1IsKill, |
| 263 | uint64_t Imm) { |
| 264 | unsigned ResultReg = createResultReg(RC); |
| 265 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 266 | |
| 267 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 268 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 269 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 270 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 271 | .addImm(Imm)); |
| 272 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 273 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 274 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 275 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 276 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 277 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 278 | TII.get(TargetOpcode::COPY), ResultReg) |
| 279 | .addReg(II.ImplicitDefs[0])); |
| 280 | } |
| 281 | return ResultReg; |
| 282 | } |
| 283 | |
| 284 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 285 | const TargetRegisterClass *RC, |
| 286 | uint64_t Imm) { |
| 287 | unsigned ResultReg = createResultReg(RC); |
| 288 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 289 | |
| 290 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 291 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 292 | .addImm(Imm)); |
| 293 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 294 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 295 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 296 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 297 | TII.get(TargetOpcode::COPY), ResultReg) |
| 298 | .addReg(II.ImplicitDefs[0])); |
| 299 | } |
| 300 | return ResultReg; |
| 301 | } |
| 302 | |
| 303 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 304 | unsigned Op0, bool Op0IsKill, |
| 305 | uint32_t Idx) { |
| 306 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 307 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 308 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 309 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 310 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 311 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 312 | return ResultReg; |
| 313 | } |
| 314 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 315 | bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) { |
| 316 | VT = TLI.getValueType(Ty, true); |
| 317 | |
| 318 | // Only handle simple types. |
| 319 | if (VT == MVT::Other || !VT.isSimple()) return false; |
| 320 | |
| 321 | // For now, only handle 32-bit types. |
| 322 | return VT == MVT::i32; |
| 323 | } |
| 324 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 325 | // Computes the Reg+Offset to get to an object. |
| 326 | bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg, |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 327 | int &Offset) { |
| 328 | // Some boilerplate from the X86 FastISel. |
| 329 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 330 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 331 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 332 | // Don't walk into other basic blocks; it's possible we haven't |
| 333 | // visited them yet, so the instructions may not yet be assigned |
| 334 | // virtual registers. |
| 335 | if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB) |
| 336 | return false; |
| 337 | |
| 338 | Opcode = I->getOpcode(); |
| 339 | U = I; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 340 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 341 | Opcode = C->getOpcode(); |
| 342 | U = C; |
| 343 | } |
| 344 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 345 | if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 346 | if (Ty->getAddressSpace() > 255) |
| 347 | // Fast instruction selection doesn't support the special |
| 348 | // address spaces. |
| 349 | return false; |
| 350 | |
| 351 | switch (Opcode) { |
| 352 | default: |
| 353 | //errs() << "Failing Opcode is: " << *Op1 << "\n"; |
| 354 | break; |
| 355 | case Instruction::Alloca: { |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 356 | assert(false && "Alloca should have been handled earlier!"); |
| 357 | return false; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 360 | |
| 361 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
| 362 | //errs() << "Failing GV is: " << GV << "\n"; |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 363 | (void)GV; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | |
| 367 | // Try to get this in a register if nothing else has worked. |
| 368 | Reg = getRegForValue(Obj); |
| 369 | return Reg != 0; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 372 | bool ARMFastISel::ARMLoadAlloca(const Instruction *I) { |
| 373 | Value *Op0 = I->getOperand(0); |
| 374 | |
| 375 | // Verify it's an alloca. |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 376 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) { |
| 377 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 378 | FuncInfo.StaticAllocaMap.find(AI); |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 379 | |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 380 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 381 | TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy()); |
| 382 | unsigned ResultReg = createResultReg(RC); |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 383 | TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt, |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 384 | ResultReg, SI->second, RC, |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 385 | TM.getRegisterInfo()); |
| 386 | UpdateValueMap(I, ResultReg); |
| 387 | return true; |
| 388 | } |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 394 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, |
| 395 | unsigned Reg, int Offset) { |
| 396 | |
| 397 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
| 398 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 548d1bb | 2010-08-30 23:48:26 +0000 | [diff] [blame^] | 399 | default: |
| 400 | assert(false && "Trying to emit for an unhandled type!"); |
| 401 | return false; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 402 | case MVT::i32: { |
| 403 | ResultReg = createResultReg(ARM::GPRRegisterClass); |
| 404 | // TODO: Fix the Addressing modes so that these can share some code. |
| 405 | // Since this is a Thumb1 load this will work in Thumb1 or 2 mode. |
| 406 | if (AFI->isThumbFunction()) |
| 407 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 408 | TII.get(ARM::tLDR), ResultReg) |
| 409 | .addReg(Reg).addImm(Offset).addReg(0)); |
| 410 | else |
| 411 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 412 | TII.get(ARM::LDR), ResultReg) |
| 413 | .addReg(Reg).addReg(0).addImm(Offset)); |
| 414 | return true; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 419 | bool ARMFastISel::ARMSelectLoad(const Instruction *I) { |
Eric Christopher | 882d62e | 2010-08-24 01:10:52 +0000 | [diff] [blame] | 420 | // If we're an alloca we know we have a frame index and can emit the load |
| 421 | // directly in short order. |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 422 | if (ARMLoadAlloca(I)) |
| 423 | return true; |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 424 | |
| 425 | // Verify we have a legal type before going any further. |
| 426 | EVT VT; |
| 427 | if (!isTypeLegal(I->getType(), VT)) |
| 428 | return false; |
| 429 | |
| 430 | // Our register and offset with innocuous defaults. |
| 431 | unsigned Reg = 0; |
| 432 | int Offset = 0; |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 433 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 434 | // See if we can handle this as Reg + Offset |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 435 | if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset)) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 436 | return false; |
| 437 | |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 438 | // Since the offset may be too large for the load instruction |
| 439 | // get the reg+offset into a register. |
| 440 | // TODO: Optimize this somewhat. |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 441 | ARMCC::CondCodes Pred = ARMCC::AL; |
| 442 | unsigned PredReg = 0; |
| 443 | |
| 444 | if (!AFI->isThumbFunction()) |
| 445 | emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 446 | Reg, Reg, Offset, Pred, PredReg, |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 447 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 448 | else { |
| 449 | assert(AFI->isThumb2Function()); |
| 450 | emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 451 | Reg, Reg, Offset, Pred, PredReg, |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 452 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 453 | } |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 454 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 455 | unsigned ResultReg; |
Eric Christopher | 2012c7b | 2010-08-24 01:10:04 +0000 | [diff] [blame] | 456 | // TODO: Verify the additions above work, otherwise we'll need to add the |
| 457 | // offset instead of 0 and do all sorts of operand munging. |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 458 | if (!ARMEmitLoad(VT, ResultReg, Reg, 0)) return false; |
| 459 | |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 460 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 464 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 465 | // No Thumb-1 for now. |
| 466 | if (AFI->isThumbFunction() && !AFI->isThumb2Function()) return false; |
| 467 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 468 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 469 | case Instruction::Load: |
| 470 | return ARMSelectLoad(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 471 | default: break; |
| 472 | } |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | namespace llvm { |
| 477 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 478 | if (EnableARMFastISel) return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 479 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 480 | } |
| 481 | } |