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