Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1 | ///===-- FastISel.cpp - Implementation of the FastISel class --------------===// |
| 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 contains the implementation of the FastISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 14 | #include "llvm/Instructions.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/FastISel.h" |
| 16 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 24 | /// SelectBinaryOp - Select and emit code for a binary operator instruction, |
| 25 | /// which has an opcode which directly corresponds to the given ISD opcode. |
| 26 | /// |
| 27 | bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode, |
| 28 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 29 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 30 | unsigned Op1 = ValueMap[I->getOperand(1)]; |
Dan Gohman | a7f2dff | 2008-08-20 00:35:17 +0000 | [diff] [blame] | 31 | if (Op0 == 0 || Op1 == 0) |
| 32 | // Unhandled operand. Halt "fast" selection and bail. |
| 33 | return false; |
| 34 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 35 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); |
| 36 | if (VT == MVT::Other || !VT.isSimple()) |
| 37 | // Unhandled type. Halt "fast" selection and bail. |
| 38 | return false; |
| 39 | |
| 40 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), ISDOpcode, Op0, Op1); |
| 41 | if (ResultReg == 0) |
| 42 | // Target-specific code wasn't able to find a machine opcode for |
| 43 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 44 | return false; |
| 45 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 46 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 47 | ValueMap[I] = ResultReg; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool FastISel::SelectGetElementPtr(Instruction *I, |
| 52 | DenseMap<const Value*, unsigned> &ValueMap) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 53 | unsigned N = ValueMap[I->getOperand(0)]; |
| 54 | if (N == 0) |
| 55 | // Unhandled operand. Halt "fast" selection and bail. |
| 56 | return false; |
| 57 | |
| 58 | const Type *Ty = I->getOperand(0)->getType(); |
| 59 | MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true); |
| 60 | MVT::SimpleValueType PtrVT = TLI.getPointerTy().getSimpleVT(); |
| 61 | |
| 62 | for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); |
| 63 | OI != E; ++OI) { |
| 64 | Value *Idx = *OI; |
| 65 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 66 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 67 | if (Field) { |
| 68 | // N = N + Offset |
| 69 | uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); |
| 70 | // FIXME: This can be optimized by combining the add with a |
| 71 | // subsequent one. |
| 72 | N = FastEmit_ri(VT.getSimpleVT(), ISD::ADD, N, Offs, PtrVT); |
| 73 | if (N == 0) |
| 74 | // Unhandled operand. Halt "fast" selection and bail. |
| 75 | return false; |
| 76 | } |
| 77 | Ty = StTy->getElementType(Field); |
| 78 | } else { |
| 79 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 80 | |
| 81 | // If this is a constant subscript, handle it quickly. |
| 82 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 83 | if (CI->getZExtValue() == 0) continue; |
| 84 | uint64_t Offs = |
| 85 | TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
| 86 | N = FastEmit_ri(VT.getSimpleVT(), ISD::ADD, N, Offs, PtrVT); |
| 87 | if (N == 0) |
| 88 | // Unhandled operand. Halt "fast" selection and bail. |
| 89 | return false; |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | // N = N + Idx * ElementSize; |
| 94 | uint64_t ElementSize = TD.getABITypeSize(Ty); |
| 95 | unsigned IdxN = ValueMap[Idx]; |
| 96 | if (IdxN == 0) |
| 97 | // Unhandled operand. Halt "fast" selection and bail. |
| 98 | return false; |
| 99 | |
| 100 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 101 | // it. |
| 102 | MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/true); |
| 103 | if (IdxVT.bitsLT(VT)) |
| 104 | IdxN = FastEmit_r(VT.getSimpleVT(), ISD::SIGN_EXTEND, IdxN); |
| 105 | else if (IdxVT.bitsGT(VT)) |
| 106 | IdxN = FastEmit_r(VT.getSimpleVT(), ISD::TRUNCATE, IdxN); |
| 107 | if (IdxN == 0) |
| 108 | // Unhandled operand. Halt "fast" selection and bail. |
| 109 | return false; |
| 110 | |
| 111 | // FIXME: If multiple is power of two, turn it into a shift. The |
| 112 | // optimization should be in FastEmit_ri? |
| 113 | IdxN = FastEmit_ri(VT.getSimpleVT(), ISD::MUL, IdxN, |
| 114 | ElementSize, PtrVT); |
| 115 | if (IdxN == 0) |
| 116 | // Unhandled operand. Halt "fast" selection and bail. |
| 117 | return false; |
| 118 | N = FastEmit_rr(VT.getSimpleVT(), ISD::ADD, N, IdxN); |
| 119 | if (N == 0) |
| 120 | // Unhandled operand. Halt "fast" selection and bail. |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // We successfully emitted code for the given LLVM Instruction. |
| 126 | ValueMap[I] = N; |
| 127 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 130 | BasicBlock::iterator |
Dan Gohman | b7864a9 | 2008-08-20 18:09:02 +0000 | [diff] [blame] | 131 | FastISel::SelectInstructions(BasicBlock::iterator Begin, |
| 132 | BasicBlock::iterator End, |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 133 | DenseMap<const Value*, unsigned> &ValueMap, |
| 134 | MachineBasicBlock *mbb) { |
| 135 | MBB = mbb; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 136 | BasicBlock::iterator I = Begin; |
| 137 | |
| 138 | for (; I != End; ++I) { |
| 139 | switch (I->getOpcode()) { |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 140 | case Instruction::Add: { |
| 141 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD; |
| 142 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 143 | } |
| 144 | case Instruction::Sub: { |
| 145 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB; |
| 146 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 147 | } |
| 148 | case Instruction::Mul: { |
| 149 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL; |
| 150 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 151 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 152 | case Instruction::SDiv: |
| 153 | if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break; |
| 154 | case Instruction::UDiv: |
| 155 | if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break; |
| 156 | case Instruction::FDiv: |
| 157 | if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break; |
| 158 | case Instruction::SRem: |
| 159 | if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break; |
| 160 | case Instruction::URem: |
| 161 | if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break; |
| 162 | case Instruction::FRem: |
| 163 | if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break; |
| 164 | case Instruction::Shl: |
| 165 | if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break; |
| 166 | case Instruction::LShr: |
| 167 | if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break; |
| 168 | case Instruction::AShr: |
| 169 | if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break; |
| 170 | case Instruction::And: |
| 171 | if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break; |
| 172 | case Instruction::Or: |
| 173 | if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break; |
| 174 | case Instruction::Xor: |
| 175 | if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break; |
| 176 | |
| 177 | case Instruction::GetElementPtr: |
| 178 | if (!SelectGetElementPtr(I, ValueMap)) return I; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 179 | break; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 180 | |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 181 | case Instruction::Br: { |
| 182 | BranchInst *BI = cast<BranchInst>(I); |
| 183 | |
| 184 | // For now, check for and handle just the most trivial case: an |
| 185 | // unconditional fall-through branch. |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 186 | if (BI->isUnconditional()) { |
| 187 | MachineFunction::iterator NextMBB = |
| 188 | next(MachineFunction::iterator(MBB)); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 189 | if (NextMBB != MF.end() && |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 190 | NextMBB->getBasicBlock() == BI->getSuccessor(0)) { |
| 191 | MBB->addSuccessor(NextMBB); |
| 192 | break; |
| 193 | } |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Something more complicated. Halt "fast" selection and bail. |
| 197 | return I; |
| 198 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 199 | default: |
| 200 | // Unhandled instruction. Halt "fast" selection and bail. |
| 201 | return I; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return I; |
| 206 | } |
| 207 | |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 208 | FastISel::FastISel(MachineFunction &mf) |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 209 | : MF(mf), MRI(mf.getRegInfo()), |
| 210 | TD(*mf.getTarget().getTargetData()), |
| 211 | TII(*mf.getTarget().getInstrInfo()), |
| 212 | TLI(*mf.getTarget().getTargetLowering()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 215 | FastISel::~FastISel() {} |
| 216 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 217 | unsigned FastISel::FastEmit_(MVT::SimpleValueType, ISD::NodeType) { |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | unsigned FastISel::FastEmit_r(MVT::SimpleValueType, ISD::NodeType, |
| 222 | unsigned /*Op0*/) { |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, ISD::NodeType, |
| 227 | unsigned /*Op0*/, unsigned /*Op0*/) { |
| 228 | return 0; |
| 229 | } |
| 230 | |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 231 | unsigned FastISel::FastEmit_i(MVT::SimpleValueType, uint64_t) { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, ISD::NodeType, |
| 236 | unsigned /*Op0*/, uint64_t Imm, |
| 237 | MVT::SimpleValueType ImmType) { |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 242 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 243 | /// If that fails, it materializes the immediate into a register and try |
| 244 | /// FastEmit_rr instead. |
| 245 | unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode, |
| 246 | unsigned Op0, uint64_t Imm, |
| 247 | MVT::SimpleValueType ImmType) { |
| 248 | unsigned ResultReg = 0; |
| 249 | // First check if immediate type is legal. If not, we can't use the ri form. |
| 250 | if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal) |
| 251 | ResultReg = FastEmit_ri(VT, Opcode, Op0, Imm, ImmType); |
| 252 | if (ResultReg != 0) |
| 253 | return ResultReg; |
| 254 | return FastEmit_rr(VT, Opcode, Op0, FastEmit_i(ImmType, Imm)); |
| 255 | } |
| 256 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 257 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 258 | const TargetRegisterClass* RC) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 259 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 260 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 261 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame^] | 262 | BuildMI(MBB, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 263 | return ResultReg; |
| 264 | } |
| 265 | |
| 266 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 267 | const TargetRegisterClass *RC, |
| 268 | unsigned Op0) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 269 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 270 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 271 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame^] | 272 | BuildMI(MBB, II, ResultReg).addReg(Op0); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 273 | return ResultReg; |
| 274 | } |
| 275 | |
| 276 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 277 | const TargetRegisterClass *RC, |
| 278 | unsigned Op0, unsigned Op1) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 279 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 280 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 281 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame^] | 282 | BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 283 | return ResultReg; |
| 284 | } |