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