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 | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 109 | virtual bool ARMSelectStore(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 110 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 111 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 112 | private: |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 113 | bool isTypeLegal(const Type *Ty, EVT &VT); |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 114 | bool isLoadTypeLegal(const Type *Ty, EVT &VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 115 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset); |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 116 | bool ARMLoadAlloca(const Instruction *I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 117 | bool ARMStoreAlloca(const Instruction *I); |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 118 | bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 119 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 120 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 121 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
| 122 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 123 | |
| 124 | } // end anonymous namespace |
| 125 | |
| 126 | // #include "ARMGenCallingConv.inc" |
| 127 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 128 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 129 | // we don't care about implicit defs here, just places we'll need to add a |
| 130 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 131 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
| 132 | const TargetInstrDesc &TID = MI->getDesc(); |
| 133 | if (!TID.hasOptionalDef()) |
| 134 | return false; |
| 135 | |
| 136 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 137 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 138 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 139 | if (!MO.isReg() || !MO.isDef()) continue; |
| 140 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 141 | *CPSR = true; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | // If the machine is predicable go ahead and add the predicate operands, if |
| 147 | // it needs default CC operands add those. |
| 148 | const MachineInstrBuilder & |
| 149 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 150 | MachineInstr *MI = &*MIB; |
| 151 | |
| 152 | // Do we use a predicate? |
| 153 | if (TII.isPredicable(MI)) |
| 154 | AddDefaultPred(MIB); |
| 155 | |
| 156 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 157 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 158 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 159 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 160 | if (CPSR) |
| 161 | AddDefaultT1CC(MIB); |
| 162 | else |
| 163 | AddDefaultCC(MIB); |
| 164 | } |
| 165 | return MIB; |
| 166 | } |
| 167 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 168 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 169 | const TargetRegisterClass* RC) { |
| 170 | unsigned ResultReg = createResultReg(RC); |
| 171 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 172 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 173 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)); |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 174 | return ResultReg; |
| 175 | } |
| 176 | |
| 177 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 178 | const TargetRegisterClass *RC, |
| 179 | unsigned Op0, bool Op0IsKill) { |
| 180 | unsigned ResultReg = createResultReg(RC); |
| 181 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 182 | |
| 183 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 184 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 185 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 186 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 187 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 188 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 189 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 190 | TII.get(TargetOpcode::COPY), ResultReg) |
| 191 | .addReg(II.ImplicitDefs[0])); |
| 192 | } |
| 193 | return ResultReg; |
| 194 | } |
| 195 | |
| 196 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 197 | const TargetRegisterClass *RC, |
| 198 | unsigned Op0, bool Op0IsKill, |
| 199 | unsigned Op1, bool Op1IsKill) { |
| 200 | unsigned ResultReg = createResultReg(RC); |
| 201 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 202 | |
| 203 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 204 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 205 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 206 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 207 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 208 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 209 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 210 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 211 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 212 | TII.get(TargetOpcode::COPY), ResultReg) |
| 213 | .addReg(II.ImplicitDefs[0])); |
| 214 | } |
| 215 | return ResultReg; |
| 216 | } |
| 217 | |
| 218 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 219 | const TargetRegisterClass *RC, |
| 220 | unsigned Op0, bool Op0IsKill, |
| 221 | uint64_t Imm) { |
| 222 | unsigned ResultReg = createResultReg(RC); |
| 223 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 224 | |
| 225 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 226 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 227 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 228 | .addImm(Imm)); |
| 229 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 230 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 231 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 232 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 233 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 234 | TII.get(TargetOpcode::COPY), ResultReg) |
| 235 | .addReg(II.ImplicitDefs[0])); |
| 236 | } |
| 237 | return ResultReg; |
| 238 | } |
| 239 | |
| 240 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 241 | const TargetRegisterClass *RC, |
| 242 | unsigned Op0, bool Op0IsKill, |
| 243 | const ConstantFP *FPImm) { |
| 244 | unsigned ResultReg = createResultReg(RC); |
| 245 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 246 | |
| 247 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 248 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 249 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 250 | .addFPImm(FPImm)); |
| 251 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 252 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 253 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 254 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 255 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 256 | TII.get(TargetOpcode::COPY), ResultReg) |
| 257 | .addReg(II.ImplicitDefs[0])); |
| 258 | } |
| 259 | return ResultReg; |
| 260 | } |
| 261 | |
| 262 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 263 | const TargetRegisterClass *RC, |
| 264 | unsigned Op0, bool Op0IsKill, |
| 265 | unsigned Op1, bool Op1IsKill, |
| 266 | uint64_t Imm) { |
| 267 | unsigned ResultReg = createResultReg(RC); |
| 268 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 269 | |
| 270 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 271 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 272 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 273 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 274 | .addImm(Imm)); |
| 275 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 276 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 277 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 278 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 279 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 280 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 281 | TII.get(TargetOpcode::COPY), ResultReg) |
| 282 | .addReg(II.ImplicitDefs[0])); |
| 283 | } |
| 284 | return ResultReg; |
| 285 | } |
| 286 | |
| 287 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 288 | const TargetRegisterClass *RC, |
| 289 | uint64_t Imm) { |
| 290 | unsigned ResultReg = createResultReg(RC); |
| 291 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 292 | |
| 293 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 294 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 295 | .addImm(Imm)); |
| 296 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 297 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 298 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 299 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 300 | TII.get(TargetOpcode::COPY), ResultReg) |
| 301 | .addReg(II.ImplicitDefs[0])); |
| 302 | } |
| 303 | return ResultReg; |
| 304 | } |
| 305 | |
| 306 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 307 | unsigned Op0, bool Op0IsKill, |
| 308 | uint32_t Idx) { |
| 309 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 310 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 311 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 312 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 313 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 314 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 315 | return ResultReg; |
| 316 | } |
| 317 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 318 | bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) { |
| 319 | VT = TLI.getValueType(Ty, true); |
| 320 | |
| 321 | // Only handle simple types. |
| 322 | if (VT == MVT::Other || !VT.isSimple()) return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 323 | |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 324 | // Handle all legal types, i.e. a register that will directly hold this |
| 325 | // value. |
| 326 | return TLI.isTypeLegal(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 329 | bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) { |
| 330 | if (isTypeLegal(Ty, VT)) return true; |
| 331 | |
| 332 | // If this is a type than can be sign or zero-extended to a basic operation |
| 333 | // go ahead and accept it now. |
| 334 | if (VT == MVT::i8 || VT == MVT::i16) |
| 335 | return true; |
| 336 | |
| 337 | return false; |
| 338 | } |
| 339 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 340 | // Computes the Reg+Offset to get to an object. |
| 341 | bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg, |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 342 | int &Offset) { |
| 343 | // Some boilerplate from the X86 FastISel. |
| 344 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 345 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 346 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 347 | // Don't walk into other basic blocks; it's possible we haven't |
| 348 | // visited them yet, so the instructions may not yet be assigned |
| 349 | // virtual registers. |
| 350 | if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB) |
| 351 | return false; |
| 352 | |
| 353 | Opcode = I->getOpcode(); |
| 354 | U = I; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 355 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 356 | Opcode = C->getOpcode(); |
| 357 | U = C; |
| 358 | } |
| 359 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 360 | if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 361 | if (Ty->getAddressSpace() > 255) |
| 362 | // Fast instruction selection doesn't support the special |
| 363 | // address spaces. |
| 364 | return false; |
| 365 | |
| 366 | switch (Opcode) { |
| 367 | default: |
| 368 | //errs() << "Failing Opcode is: " << *Op1 << "\n"; |
| 369 | break; |
| 370 | case Instruction::Alloca: { |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 371 | assert(false && "Alloca should have been handled earlier!"); |
| 372 | return false; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 375 | |
| 376 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
| 377 | //errs() << "Failing GV is: " << GV << "\n"; |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 378 | (void)GV; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // Try to get this in a register if nothing else has worked. |
| 383 | Reg = getRegForValue(Obj); |
| 384 | return Reg != 0; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 387 | bool ARMFastISel::ARMLoadAlloca(const Instruction *I) { |
| 388 | Value *Op0 = I->getOperand(0); |
| 389 | |
| 390 | // Verify it's an alloca. |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 391 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) { |
| 392 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 393 | FuncInfo.StaticAllocaMap.find(AI); |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 394 | |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 395 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 396 | TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy()); |
| 397 | unsigned ResultReg = createResultReg(RC); |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 398 | TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt, |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 399 | ResultReg, SI->second, RC, |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 400 | TM.getRegisterInfo()); |
| 401 | UpdateValueMap(I, ResultReg); |
| 402 | return true; |
| 403 | } |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 404 | } |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 408 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, |
| 409 | unsigned Reg, int Offset) { |
| 410 | |
| 411 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 412 | |
| 413 | bool isThumb = AFI->isThumbFunction(); |
| 414 | unsigned Opc; |
| 415 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 416 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 548d1bb | 2010-08-30 23:48:26 +0000 | [diff] [blame] | 417 | default: |
| 418 | assert(false && "Trying to emit for an unhandled type!"); |
| 419 | return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 420 | case MVT::i16: |
| 421 | Opc = isThumb ? ARM::tLDRH : ARM::LDRH; |
| 422 | VT = MVT::i32; |
| 423 | break; |
| 424 | case MVT::i8: |
| 425 | Opc = isThumb ? ARM::tLDRB : ARM::LDRB; |
| 426 | VT = MVT::i32; |
| 427 | break; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 428 | case MVT::i32: |
| 429 | Opc = isThumb ? ARM::tLDR : ARM::LDR; |
| 430 | break; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 431 | } |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 432 | |
| 433 | ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
| 434 | |
| 435 | // TODO: Fix the Addressing modes so that these can share some code. |
| 436 | // Since this is a Thumb1 load this will work in Thumb1 or 2 mode. |
| 437 | if (isThumb) |
| 438 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 439 | TII.get(Opc), ResultReg) |
| 440 | .addReg(Reg).addImm(Offset).addReg(0)); |
| 441 | else |
| 442 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 443 | TII.get(Opc), ResultReg) |
| 444 | .addReg(Reg).addReg(0).addImm(Offset)); |
| 445 | |
| 446 | return true; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 449 | bool ARMFastISel::ARMStoreAlloca(const Instruction *I) { |
| 450 | Value *Op1 = I->getOperand(1); |
| 451 | |
| 452 | // Verify it's an alloca. |
| 453 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) { |
| 454 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 455 | FuncInfo.StaticAllocaMap.find(AI); |
| 456 | |
| 457 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
| 458 | TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy()); |
| 459 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 460 | // Make sure we can get this into a register. |
| 461 | if (Reg == 0) return false; |
| 462 | TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt, |
| 463 | Reg, true /*isKill*/, SI->second, RC, |
| 464 | TM.getRegisterInfo()); |
| 465 | return true; |
| 466 | } |
| 467 | } |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | bool ARMFastISel::ARMSelectStore(const Instruction *I) { |
| 472 | // If we're an alloca we know we have a frame index and can emit the store |
| 473 | // quickly. |
| 474 | if (ARMStoreAlloca(I)) |
| 475 | return true; |
| 476 | |
| 477 | // Yay type legalization |
| 478 | EVT VT; |
| 479 | if (!isLoadTypeLegal(I->getType(), VT)) |
| 480 | return false; |
| 481 | |
| 482 | return false; |
| 483 | |
| 484 | } |
| 485 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 486 | bool ARMFastISel::ARMSelectLoad(const Instruction *I) { |
Eric Christopher | 882d62e | 2010-08-24 01:10:52 +0000 | [diff] [blame] | 487 | // If we're an alloca we know we have a frame index and can emit the load |
| 488 | // directly in short order. |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 489 | if (ARMLoadAlloca(I)) |
| 490 | return true; |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 491 | |
| 492 | // Verify we have a legal type before going any further. |
| 493 | EVT VT; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 494 | if (!isLoadTypeLegal(I->getType(), VT)) |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 495 | return false; |
| 496 | |
| 497 | // Our register and offset with innocuous defaults. |
| 498 | unsigned Reg = 0; |
| 499 | int Offset = 0; |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 500 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 501 | // See if we can handle this as Reg + Offset |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 502 | if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset)) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 503 | return false; |
| 504 | |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 505 | // Since the offset may be too large for the load instruction |
| 506 | // get the reg+offset into a register. |
| 507 | // TODO: Optimize this somewhat. |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 508 | ARMCC::CondCodes Pred = ARMCC::AL; |
| 509 | unsigned PredReg = 0; |
| 510 | |
| 511 | if (!AFI->isThumbFunction()) |
| 512 | emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 513 | Reg, Reg, Offset, Pred, PredReg, |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 514 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 515 | else { |
| 516 | assert(AFI->isThumb2Function()); |
| 517 | emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 518 | Reg, Reg, Offset, Pred, PredReg, |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 519 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 520 | } |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 521 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 522 | unsigned ResultReg; |
Eric Christopher | 2012c7b | 2010-08-24 01:10:04 +0000 | [diff] [blame] | 523 | // TODO: Verify the additions above work, otherwise we'll need to add the |
| 524 | // offset instead of 0 and do all sorts of operand munging. |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 525 | if (!ARMEmitLoad(VT, ResultReg, Reg, 0)) return false; |
| 526 | |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 527 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 528 | return true; |
| 529 | } |
| 530 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 531 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 532 | // No Thumb-1 for now. |
| 533 | if (AFI->isThumbFunction() && !AFI->isThumb2Function()) return false; |
| 534 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 535 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 536 | case Instruction::Load: |
| 537 | return ARMSelectLoad(I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 538 | case Instruction::Store: |
| 539 | return ARMSelectStore(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 540 | default: break; |
| 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | namespace llvm { |
| 546 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 547 | if (EnableARMFastISel) return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 548 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 549 | } |
| 550 | } |