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 | |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 62 | // Convenience variable to avoid checking all the time. |
| 63 | bool isThumb; |
| 64 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 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 | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 73 | isThumb = AFI->isThumbFunction(); |
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); |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 108 | virtual unsigned TargetMaterializeConstant(const Constant *C); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 109 | |
| 110 | #include "ARMGenFastISel.inc" |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 111 | |
| 112 | // Instruction selection routines. |
| 113 | virtual bool ARMSelectLoad(const Instruction *I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 114 | virtual bool ARMSelectStore(const Instruction *I); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 115 | virtual bool ARMSelectBranch(const Instruction *I); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 116 | virtual bool ARMSelectCmp(const Instruction *I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 117 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 118 | // Utility routines. |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 119 | private: |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 120 | bool isTypeLegal(const Type *Ty, EVT &VT); |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 121 | bool isLoadTypeLegal(const Type *Ty, EVT &VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 122 | bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 123 | bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset); |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 124 | bool ARMLoadAlloca(const Instruction *I, EVT VT); |
| 125 | bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT); |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 126 | bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset); |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame^] | 127 | unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT); |
| 128 | unsigned ARMMaterializeInt(const Constant *C); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 129 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 130 | bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR); |
| 131 | const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB); |
| 132 | }; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 133 | |
| 134 | } // end anonymous namespace |
| 135 | |
| 136 | // #include "ARMGenCallingConv.inc" |
| 137 | |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 138 | // DefinesOptionalPredicate - This is different from DefinesPredicate in that |
| 139 | // we don't care about implicit defs here, just places we'll need to add a |
| 140 | // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR. |
| 141 | bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) { |
| 142 | const TargetInstrDesc &TID = MI->getDesc(); |
| 143 | if (!TID.hasOptionalDef()) |
| 144 | return false; |
| 145 | |
| 146 | // Look to see if our OptionalDef is defining CPSR or CCR. |
| 147 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 148 | const MachineOperand &MO = MI->getOperand(i); |
Eric Christopher | f762fbe | 2010-08-20 00:36:24 +0000 | [diff] [blame] | 149 | if (!MO.isReg() || !MO.isDef()) continue; |
| 150 | if (MO.getReg() == ARM::CPSR) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 151 | *CPSR = true; |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | // If the machine is predicable go ahead and add the predicate operands, if |
| 157 | // it needs default CC operands add those. |
| 158 | const MachineInstrBuilder & |
| 159 | ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) { |
| 160 | MachineInstr *MI = &*MIB; |
| 161 | |
| 162 | // Do we use a predicate? |
| 163 | if (TII.isPredicable(MI)) |
| 164 | AddDefaultPred(MIB); |
| 165 | |
| 166 | // Do we optionally set a predicate? Preds is size > 0 iff the predicate |
| 167 | // defines CPSR. All other OptionalDefines in ARM are the CCR register. |
Eric Christopher | 979e0a1 | 2010-08-19 15:35:27 +0000 | [diff] [blame] | 168 | bool CPSR = false; |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 169 | if (DefinesOptionalPredicate(MI, &CPSR)) { |
| 170 | if (CPSR) |
| 171 | AddDefaultT1CC(MIB); |
| 172 | else |
| 173 | AddDefaultCC(MIB); |
| 174 | } |
| 175 | return MIB; |
| 176 | } |
| 177 | |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 178 | unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 179 | const TargetRegisterClass* RC) { |
| 180 | unsigned ResultReg = createResultReg(RC); |
| 181 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 182 | |
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 | return ResultReg; |
| 185 | } |
| 186 | |
| 187 | unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 188 | const TargetRegisterClass *RC, |
| 189 | unsigned Op0, bool Op0IsKill) { |
| 190 | unsigned ResultReg = createResultReg(RC); |
| 191 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 192 | |
| 193 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 194 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 195 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
| 196 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 197 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 198 | .addReg(Op0, Op0IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 199 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 200 | TII.get(TargetOpcode::COPY), ResultReg) |
| 201 | .addReg(II.ImplicitDefs[0])); |
| 202 | } |
| 203 | return ResultReg; |
| 204 | } |
| 205 | |
| 206 | unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 207 | const TargetRegisterClass *RC, |
| 208 | unsigned Op0, bool Op0IsKill, |
| 209 | unsigned Op1, bool Op1IsKill) { |
| 210 | unsigned ResultReg = createResultReg(RC); |
| 211 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 212 | |
| 213 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 214 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 215 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 216 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
| 217 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 218 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 219 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 220 | .addReg(Op1, Op1IsKill * RegState::Kill)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 221 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 222 | TII.get(TargetOpcode::COPY), ResultReg) |
| 223 | .addReg(II.ImplicitDefs[0])); |
| 224 | } |
| 225 | return ResultReg; |
| 226 | } |
| 227 | |
| 228 | unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 229 | const TargetRegisterClass *RC, |
| 230 | unsigned Op0, bool Op0IsKill, |
| 231 | uint64_t Imm) { |
| 232 | unsigned ResultReg = createResultReg(RC); |
| 233 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 234 | |
| 235 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 236 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 237 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 238 | .addImm(Imm)); |
| 239 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 240 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 241 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 242 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 243 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 244 | TII.get(TargetOpcode::COPY), ResultReg) |
| 245 | .addReg(II.ImplicitDefs[0])); |
| 246 | } |
| 247 | return ResultReg; |
| 248 | } |
| 249 | |
| 250 | unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 251 | const TargetRegisterClass *RC, |
| 252 | unsigned Op0, bool Op0IsKill, |
| 253 | const ConstantFP *FPImm) { |
| 254 | unsigned ResultReg = createResultReg(RC); |
| 255 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 256 | |
| 257 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 258 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 259 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 260 | .addFPImm(FPImm)); |
| 261 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 262 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 263 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 264 | .addFPImm(FPImm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 265 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 266 | TII.get(TargetOpcode::COPY), ResultReg) |
| 267 | .addReg(II.ImplicitDefs[0])); |
| 268 | } |
| 269 | return ResultReg; |
| 270 | } |
| 271 | |
| 272 | unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 273 | const TargetRegisterClass *RC, |
| 274 | unsigned Op0, bool Op0IsKill, |
| 275 | unsigned Op1, bool Op1IsKill, |
| 276 | uint64_t Imm) { |
| 277 | unsigned ResultReg = createResultReg(RC); |
| 278 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 279 | |
| 280 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 281 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 282 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 283 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 284 | .addImm(Imm)); |
| 285 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 286 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 287 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 288 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 289 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 290 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 291 | TII.get(TargetOpcode::COPY), ResultReg) |
| 292 | .addReg(II.ImplicitDefs[0])); |
| 293 | } |
| 294 | return ResultReg; |
| 295 | } |
| 296 | |
| 297 | unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 298 | const TargetRegisterClass *RC, |
| 299 | uint64_t Imm) { |
| 300 | unsigned ResultReg = createResultReg(RC); |
| 301 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 302 | |
| 303 | if (II.getNumDefs() >= 1) |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 304 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 305 | .addImm(Imm)); |
| 306 | else { |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 307 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 308 | .addImm(Imm)); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 309 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 310 | TII.get(TargetOpcode::COPY), ResultReg) |
| 311 | .addReg(II.ImplicitDefs[0])); |
| 312 | } |
| 313 | return ResultReg; |
| 314 | } |
| 315 | |
| 316 | unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT, |
| 317 | unsigned Op0, bool Op0IsKill, |
| 318 | uint32_t Idx) { |
| 319 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| 320 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 321 | "Cannot yet extract from physregs"); |
Eric Christopher | 456144e | 2010-08-19 00:37:05 +0000 | [diff] [blame] | 322 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
Eric Christopher | 0fe7d54 | 2010-08-17 01:25:29 +0000 | [diff] [blame] | 323 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
| 324 | .addReg(Op0, getKillRegState(Op0IsKill), Idx)); |
| 325 | return ResultReg; |
| 326 | } |
| 327 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame^] | 328 | // For double width floating point we need to materialize two constants |
| 329 | // (the high and the low) into integer registers then use a move to get |
| 330 | // the combined constant into an FP reg. |
| 331 | unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) { |
| 332 | const APFloat Val = CFP->getValueAPF(); |
| 333 | bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64; |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 334 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame^] | 335 | // This checks to see if we can use VFP3 instructions to materialize |
| 336 | // a constant, otherwise we have to go through the constant pool. |
| 337 | if (TLI.isFPImmLegal(Val, VT)) { |
| 338 | unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS; |
| 339 | unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); |
| 340 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), |
| 341 | DestReg) |
| 342 | .addFPImm(CFP)); |
| 343 | return DestReg; |
| 344 | } |
| 345 | |
| 346 | // No 64-bit at the moment. |
| 347 | if (is64bit) return 0; |
| 348 | |
| 349 | // Load this from the constant pool. |
| 350 | unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP)); |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 351 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame^] | 352 | // If we have a floating point constant we expect it in a floating point |
| 353 | // register. |
| 354 | unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT)); |
| 355 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 356 | TII.get(ARM::VMOVRS), MoveReg) |
| 357 | .addReg(DestReg)); |
| 358 | return MoveReg; |
| 359 | } |
| 360 | |
| 361 | unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) { |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 362 | // MachineConstantPool wants an explicit alignment. |
| 363 | unsigned Align = TD.getPrefTypeAlignment(C->getType()); |
| 364 | if (Align == 0) { |
| 365 | // TODO: Figure out if this is correct. |
| 366 | Align = TD.getTypeAllocSize(C->getType()); |
| 367 | } |
| 368 | unsigned Idx = MCP.getConstantPoolIndex(C, Align); |
| 369 | |
Eric Christopher | 845c575 | 2010-09-08 18:56:34 +0000 | [diff] [blame] | 370 | unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32)); |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 371 | if (isThumb) |
| 372 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 373 | TII.get(ARM::t2LDRpci)) |
| 374 | .addReg(DestReg).addConstantPoolIndex(Idx)); |
| 375 | else |
| 376 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 377 | TII.get(ARM::LDRcp)) |
Eric Christopher | 845c575 | 2010-09-08 18:56:34 +0000 | [diff] [blame] | 378 | .addReg(DestReg).addConstantPoolIndex(Idx) |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 379 | .addReg(0).addImm(0)); |
| 380 | |
| 381 | return DestReg; |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Eric Christopher | 9ed58df | 2010-09-09 00:19:41 +0000 | [diff] [blame^] | 384 | unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) { |
| 385 | EVT VT = TLI.getValueType(C->getType(), true); |
| 386 | |
| 387 | // Only handle simple types. |
| 388 | if (!VT.isSimple()) return 0; |
| 389 | |
| 390 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 391 | return ARMMaterializeFP(CFP, VT); |
| 392 | return ARMMaterializeInt(C); |
| 393 | } |
| 394 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 395 | bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) { |
| 396 | VT = TLI.getValueType(Ty, true); |
| 397 | |
| 398 | // Only handle simple types. |
| 399 | if (VT == MVT::Other || !VT.isSimple()) return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 400 | |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 401 | // Handle all legal types, i.e. a register that will directly hold this |
| 402 | // value. |
| 403 | return TLI.isTypeLegal(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 406 | bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) { |
| 407 | if (isTypeLegal(Ty, VT)) return true; |
| 408 | |
| 409 | // If this is a type than can be sign or zero-extended to a basic operation |
| 410 | // go ahead and accept it now. |
| 411 | if (VT == MVT::i8 || VT == MVT::i16) |
| 412 | return true; |
| 413 | |
| 414 | return false; |
| 415 | } |
| 416 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 417 | // Computes the Reg+Offset to get to an object. |
| 418 | bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg, |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 419 | int &Offset) { |
| 420 | // Some boilerplate from the X86 FastISel. |
| 421 | const User *U = NULL; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 422 | unsigned Opcode = Instruction::UserOp1; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 423 | if (const Instruction *I = dyn_cast<Instruction>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 424 | // Don't walk into other basic blocks; it's possible we haven't |
| 425 | // visited them yet, so the instructions may not yet be assigned |
| 426 | // virtual registers. |
| 427 | if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB) |
| 428 | return false; |
| 429 | |
| 430 | Opcode = I->getOpcode(); |
| 431 | U = I; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 432 | } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 433 | Opcode = C->getOpcode(); |
| 434 | U = C; |
| 435 | } |
| 436 | |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 437 | if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 438 | if (Ty->getAddressSpace() > 255) |
| 439 | // Fast instruction selection doesn't support the special |
| 440 | // address spaces. |
| 441 | return false; |
| 442 | |
| 443 | switch (Opcode) { |
| 444 | default: |
| 445 | //errs() << "Failing Opcode is: " << *Op1 << "\n"; |
| 446 | break; |
| 447 | case Instruction::Alloca: { |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 448 | assert(false && "Alloca should have been handled earlier!"); |
| 449 | return false; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 452 | |
| 453 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) { |
| 454 | //errs() << "Failing GV is: " << GV << "\n"; |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 455 | (void)GV; |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // Try to get this in a register if nothing else has worked. |
| 460 | Reg = getRegForValue(Obj); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 461 | if (Reg == 0) return false; |
| 462 | |
| 463 | // Since the offset may be too large for the load instruction |
| 464 | // get the reg+offset into a register. |
| 465 | // TODO: Verify the additions work, otherwise we'll need to add the |
| 466 | // offset instead of 0 to the instructions and do all sorts of operand |
| 467 | // munging. |
| 468 | // TODO: Optimize this somewhat. |
| 469 | if (Offset != 0) { |
| 470 | ARMCC::CondCodes Pred = ARMCC::AL; |
| 471 | unsigned PredReg = 0; |
| 472 | |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 473 | if (!isThumb) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 474 | emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 475 | Reg, Reg, Offset, Pred, PredReg, |
| 476 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 477 | else { |
| 478 | assert(AFI->isThumb2Function()); |
| 479 | emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 480 | Reg, Reg, Offset, Pred, PredReg, |
| 481 | static_cast<const ARMBaseInstrInfo&>(TII)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return true; |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 488 | bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) { |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 489 | Value *Op0 = I->getOperand(0); |
| 490 | |
| 491 | // Verify it's an alloca. |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 492 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) { |
| 493 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 494 | FuncInfo.StaticAllocaMap.find(AI); |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 495 | |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 496 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 497 | TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 498 | unsigned ResultReg = createResultReg(RC); |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 499 | TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt, |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 500 | ResultReg, SI->second, RC, |
Eric Christopher | e24d66f | 2010-08-24 22:07:27 +0000 | [diff] [blame] | 501 | TM.getRegisterInfo()); |
| 502 | UpdateValueMap(I, ResultReg); |
| 503 | return true; |
| 504 | } |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 505 | } |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 509 | bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, |
| 510 | unsigned Reg, int Offset) { |
| 511 | |
| 512 | assert(VT.isSimple() && "Non-simple types are invalid here!"); |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 513 | unsigned Opc; |
| 514 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 515 | switch (VT.getSimpleVT().SimpleTy) { |
Eric Christopher | 548d1bb | 2010-08-30 23:48:26 +0000 | [diff] [blame] | 516 | default: |
| 517 | assert(false && "Trying to emit for an unhandled type!"); |
| 518 | return false; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 519 | case MVT::i16: |
| 520 | Opc = isThumb ? ARM::tLDRH : ARM::LDRH; |
| 521 | VT = MVT::i32; |
| 522 | break; |
| 523 | case MVT::i8: |
| 524 | Opc = isThumb ? ARM::tLDRB : ARM::LDRB; |
| 525 | VT = MVT::i32; |
| 526 | break; |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 527 | case MVT::i32: |
| 528 | Opc = isThumb ? ARM::tLDR : ARM::LDR; |
| 529 | break; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 530 | } |
Eric Christopher | dc90804 | 2010-08-31 01:28:42 +0000 | [diff] [blame] | 531 | |
| 532 | ResultReg = createResultReg(TLI.getRegClassFor(VT)); |
| 533 | |
| 534 | // TODO: Fix the Addressing modes so that these can share some code. |
| 535 | // Since this is a Thumb1 load this will work in Thumb1 or 2 mode. |
| 536 | if (isThumb) |
| 537 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 538 | TII.get(Opc), ResultReg) |
| 539 | .addReg(Reg).addImm(Offset).addReg(0)); |
| 540 | else |
| 541 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 542 | TII.get(Opc), ResultReg) |
| 543 | .addReg(Reg).addReg(0).addImm(Offset)); |
| 544 | |
| 545 | return true; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 548 | bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){ |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 549 | Value *Op1 = I->getOperand(1); |
| 550 | |
| 551 | // Verify it's an alloca. |
| 552 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) { |
| 553 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 554 | FuncInfo.StaticAllocaMap.find(AI); |
| 555 | |
| 556 | if (SI != FuncInfo.StaticAllocaMap.end()) { |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 557 | TargetRegisterClass* RC = TLI.getRegClassFor(VT); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 558 | assert(SrcReg != 0 && "Nothing to store!"); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 559 | TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt, |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 560 | SrcReg, true /*isKill*/, SI->second, RC, |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 561 | TM.getRegisterInfo()); |
| 562 | return true; |
| 563 | } |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 568 | bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, |
| 569 | unsigned DstReg, int Offset) { |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 570 | unsigned StrOpc; |
| 571 | switch (VT.getSimpleVT().SimpleTy) { |
| 572 | default: return false; |
| 573 | case MVT::i1: |
| 574 | case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break; |
| 575 | case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break; |
| 576 | case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break; |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 577 | case MVT::f32: |
| 578 | if (!Subtarget->hasVFP2()) return false; |
| 579 | StrOpc = ARM::VSTRS; |
| 580 | break; |
| 581 | case MVT::f64: |
| 582 | if (!Subtarget->hasVFP2()) return false; |
| 583 | StrOpc = ARM::VSTRD; |
| 584 | break; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (isThumb) |
| 588 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 589 | TII.get(StrOpc), SrcReg) |
| 590 | .addReg(DstReg).addImm(Offset).addReg(0)); |
| 591 | else |
| 592 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 593 | TII.get(StrOpc), SrcReg) |
| 594 | .addReg(DstReg).addReg(0).addImm(Offset)); |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | bool ARMFastISel::ARMSelectStore(const Instruction *I) { |
| 600 | Value *Op0 = I->getOperand(0); |
| 601 | unsigned SrcReg = 0; |
| 602 | |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 603 | // Yay type legalization |
| 604 | EVT VT; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 605 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 606 | return false; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 607 | |
Eric Christopher | 1b61ef4 | 2010-09-02 01:48:11 +0000 | [diff] [blame] | 608 | // Get the value to be stored into a register. |
| 609 | SrcReg = getRegForValue(Op0); |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 610 | if (SrcReg == 0) |
| 611 | return false; |
| 612 | |
| 613 | // If we're an alloca we know we have a frame index and can emit the store |
| 614 | // quickly. |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 615 | if (ARMStoreAlloca(I, SrcReg, VT)) |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 616 | return true; |
| 617 | |
| 618 | // Our register and offset with innocuous defaults. |
| 619 | unsigned Reg = 0; |
| 620 | int Offset = 0; |
| 621 | |
| 622 | // See if we can handle this as Reg + Offset |
| 623 | if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset)) |
| 624 | return false; |
| 625 | |
| 626 | if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false; |
| 627 | |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 628 | return false; |
| 629 | |
| 630 | } |
| 631 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 632 | bool ARMFastISel::ARMSelectLoad(const Instruction *I) { |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 633 | // Verify we have a legal type before going any further. |
| 634 | EVT VT; |
Eric Christopher | 4e68c7c | 2010-09-01 18:01:32 +0000 | [diff] [blame] | 635 | if (!isLoadTypeLegal(I->getType(), VT)) |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 636 | return false; |
| 637 | |
Eric Christopher | 30b6633 | 2010-09-08 21:49:50 +0000 | [diff] [blame] | 638 | // If we're an alloca we know we have a frame index and can emit the load |
| 639 | // directly in short order. |
| 640 | if (ARMLoadAlloca(I, VT)) |
| 641 | return true; |
| 642 | |
Eric Christopher | 61c3f9a | 2010-08-25 08:43:57 +0000 | [diff] [blame] | 643 | // Our register and offset with innocuous defaults. |
| 644 | unsigned Reg = 0; |
| 645 | int Offset = 0; |
Eric Christopher | 8654c71 | 2010-08-23 23:14:31 +0000 | [diff] [blame] | 646 | |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 647 | // See if we can handle this as Reg + Offset |
Eric Christopher | cb0b04b | 2010-08-24 00:07:24 +0000 | [diff] [blame] | 648 | if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset)) |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 649 | return false; |
Eric Christopher | 1dfb4d3 | 2010-08-23 23:28:04 +0000 | [diff] [blame] | 650 | |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 651 | unsigned ResultReg; |
Eric Christopher | 318b6ee | 2010-09-02 00:53:56 +0000 | [diff] [blame] | 652 | if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false; |
Eric Christopher | b1cc848 | 2010-08-25 07:23:49 +0000 | [diff] [blame] | 653 | |
Eric Christopher | f06f309 | 2010-08-24 00:50:47 +0000 | [diff] [blame] | 654 | UpdateValueMap(I, ResultReg); |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 655 | return true; |
| 656 | } |
| 657 | |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 658 | bool ARMFastISel::ARMSelectBranch(const Instruction *I) { |
| 659 | const BranchInst *BI = cast<BranchInst>(I); |
| 660 | MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; |
| 661 | MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; |
| 662 | |
| 663 | // Simple branch support. |
| 664 | unsigned CondReg = getRegForValue(BI->getCondition()); |
| 665 | if (CondReg == 0) return false; |
| 666 | |
| 667 | unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
| 668 | unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc; |
| 669 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 670 | .addReg(CondReg).addReg(CondReg)); |
| 671 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc)) |
| 672 | .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR); |
| 673 | FastEmitBranch(FBB, DL); |
| 674 | FuncInfo.MBB->addSuccessor(TBB); |
| 675 | return true; |
| 676 | } |
| 677 | |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 678 | bool ARMFastISel::ARMSelectCmp(const Instruction *I) { |
| 679 | const CmpInst *CI = cast<CmpInst>(I); |
| 680 | |
| 681 | EVT VT; |
| 682 | const Type *Ty = CI->getOperand(0)->getType(); |
| 683 | if (!isTypeLegal(Ty, VT)) |
| 684 | return false; |
| 685 | |
| 686 | bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy()); |
| 687 | if (isFloat && !Subtarget->hasVFP2()) |
| 688 | return false; |
| 689 | |
| 690 | unsigned CmpOpc; |
| 691 | switch (VT.getSimpleVT().SimpleTy) { |
| 692 | default: return false; |
| 693 | // TODO: Verify compares. |
| 694 | case MVT::f32: |
| 695 | CmpOpc = ARM::VCMPES; |
| 696 | break; |
| 697 | case MVT::f64: |
| 698 | CmpOpc = ARM::VCMPED; |
| 699 | break; |
| 700 | case MVT::i32: |
| 701 | CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr; |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | unsigned Arg1 = getRegForValue(CI->getOperand(0)); |
| 706 | if (Arg1 == 0) return false; |
| 707 | |
| 708 | unsigned Arg2 = getRegForValue(CI->getOperand(1)); |
| 709 | if (Arg2 == 0) return false; |
| 710 | |
| 711 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc)) |
| 712 | .addReg(Arg1).addReg(Arg2)); |
| 713 | |
| 714 | // For floating point we need to move the result to a register we can |
| 715 | // actually do something with. |
| 716 | if (isFloat) |
| 717 | AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 718 | TII.get(ARM::FMSTAT))); |
| 719 | return true; |
| 720 | } |
| 721 | |
Eric Christopher | 56d2b72 | 2010-09-02 23:43:26 +0000 | [diff] [blame] | 722 | // TODO: SoftFP support. |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 723 | bool ARMFastISel::TargetSelectInstruction(const Instruction *I) { |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 724 | // No Thumb-1 for now. |
Eric Christopher | eaa204b | 2010-09-02 01:39:14 +0000 | [diff] [blame] | 725 | if (isThumb && !AFI->isThumb2Function()) return false; |
Eric Christopher | 7fe55b7 | 2010-08-23 22:32:45 +0000 | [diff] [blame] | 726 | |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 727 | switch (I->getOpcode()) { |
Eric Christopher | 8300712 | 2010-08-23 21:44:12 +0000 | [diff] [blame] | 728 | case Instruction::Load: |
| 729 | return ARMSelectLoad(I); |
Eric Christopher | 543cf05 | 2010-09-01 22:16:27 +0000 | [diff] [blame] | 730 | case Instruction::Store: |
| 731 | return ARMSelectStore(I); |
Eric Christopher | e573410 | 2010-09-03 00:35:47 +0000 | [diff] [blame] | 732 | case Instruction::Br: |
| 733 | return ARMSelectBranch(I); |
Eric Christopher | d43393a | 2010-09-08 23:13:45 +0000 | [diff] [blame] | 734 | case Instruction::ICmp: |
| 735 | case Instruction::FCmp: |
| 736 | return ARMSelectCmp(I); |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 737 | default: break; |
| 738 | } |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | namespace llvm { |
| 743 | llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) { |
Eric Christopher | 038fea5 | 2010-08-17 00:46:57 +0000 | [diff] [blame] | 744 | if (EnableARMFastISel) return new ARMFastISel(funcInfo); |
Evan Cheng | 0944795 | 2010-07-26 18:32:55 +0000 | [diff] [blame] | 745 | return 0; |
Eric Christopher | ab69588 | 2010-07-21 22:26:11 +0000 | [diff] [blame] | 746 | } |
| 747 | } |