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