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) { |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 29 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); |
| 30 | if (VT == MVT::Other || !VT.isSimple()) |
| 31 | // Unhandled type. Halt "fast" selection and bail. |
| 32 | return false; |
Dan Gohman | b71fea2 | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 33 | // We only handle legal types. For example, on x86-32 the instruction |
| 34 | // selector contains all of the 64-bit instructions from x86-64, |
| 35 | // under the assumption that i64 won't be used if the target doesn't |
| 36 | // support it. |
| 37 | if (!TLI.isTypeLegal(VT)) |
| 38 | return false; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 39 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 40 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 41 | if (Op0 == 0) |
| 42 | // Unhandled operand. Halt "fast" selection and bail. |
| 43 | return false; |
| 44 | |
| 45 | // Check if the second operand is a constant and handle it appropriately. |
| 46 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 47 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, |
| 48 | CI->getZExtValue(), VT.getSimpleVT()); |
| 49 | if (ResultReg == 0) |
| 50 | // Target-specific code wasn't able to find a machine opcode for |
| 51 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 52 | return false; |
| 53 | |
| 54 | // We successfully emitted code for the given LLVM Instruction. |
| 55 | ValueMap[I] = ResultReg; |
| 56 | return true; |
| 57 | } |
| 58 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame^] | 59 | // Check if the second operand is a constant float. |
| 60 | if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { |
| 61 | unsigned ResultReg = FastEmit_rf_(VT.getSimpleVT(), ISDOpcode, Op0, |
| 62 | CF, VT.getSimpleVT()); |
| 63 | if (ResultReg == 0) |
| 64 | // Target-specific code wasn't able to find a machine opcode for |
| 65 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 66 | return false; |
| 67 | |
| 68 | // We successfully emitted code for the given LLVM Instruction. |
| 69 | ValueMap[I] = ResultReg; |
| 70 | return true; |
| 71 | } |
| 72 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 73 | unsigned Op1 = ValueMap[I->getOperand(1)]; |
| 74 | if (Op1 == 0) |
| 75 | // Unhandled operand. Halt "fast" selection and bail. |
| 76 | return false; |
| 77 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 78 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
| 79 | ISDOpcode, Op0, Op1); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 80 | if (ResultReg == 0) |
| 81 | // Target-specific code wasn't able to find a machine opcode for |
| 82 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 83 | return false; |
| 84 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 85 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 86 | ValueMap[I] = ResultReg; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool FastISel::SelectGetElementPtr(Instruction *I, |
| 91 | DenseMap<const Value*, unsigned> &ValueMap) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 92 | unsigned N = ValueMap[I->getOperand(0)]; |
| 93 | if (N == 0) |
| 94 | // Unhandled operand. Halt "fast" selection and bail. |
| 95 | return false; |
| 96 | |
| 97 | const Type *Ty = I->getOperand(0)->getType(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 98 | MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT(); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 99 | for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); |
| 100 | OI != E; ++OI) { |
| 101 | Value *Idx = *OI; |
| 102 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 103 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 104 | if (Field) { |
| 105 | // N = N + Offset |
| 106 | uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); |
| 107 | // FIXME: This can be optimized by combining the add with a |
| 108 | // subsequent one. |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 109 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 110 | if (N == 0) |
| 111 | // Unhandled operand. Halt "fast" selection and bail. |
| 112 | return false; |
| 113 | } |
| 114 | Ty = StTy->getElementType(Field); |
| 115 | } else { |
| 116 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 117 | |
| 118 | // If this is a constant subscript, handle it quickly. |
| 119 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 120 | if (CI->getZExtValue() == 0) continue; |
| 121 | uint64_t Offs = |
| 122 | TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 123 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 124 | if (N == 0) |
| 125 | // Unhandled operand. Halt "fast" selection and bail. |
| 126 | return false; |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | // N = N + Idx * ElementSize; |
| 131 | uint64_t ElementSize = TD.getABITypeSize(Ty); |
| 132 | unsigned IdxN = ValueMap[Idx]; |
| 133 | if (IdxN == 0) |
| 134 | // Unhandled operand. Halt "fast" selection and bail. |
| 135 | return false; |
| 136 | |
| 137 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 138 | // it. |
Evan Cheng | 2076aa8 | 2008-08-21 01:19:11 +0000 | [diff] [blame] | 139 | MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 140 | if (IdxVT.bitsLT(VT)) |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 141 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 142 | else if (IdxVT.bitsGT(VT)) |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 143 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 144 | if (IdxN == 0) |
| 145 | // Unhandled operand. Halt "fast" selection and bail. |
| 146 | return false; |
| 147 | |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 148 | if (ElementSize != 1) { |
Dan Gohman | f93cf79 | 2008-08-21 17:37:05 +0000 | [diff] [blame] | 149 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 150 | if (IdxN == 0) |
| 151 | // Unhandled operand. Halt "fast" selection and bail. |
| 152 | return false; |
| 153 | } |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 154 | N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 155 | if (N == 0) |
| 156 | // Unhandled operand. Halt "fast" selection and bail. |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // We successfully emitted code for the given LLVM Instruction. |
| 162 | ValueMap[I] = N; |
| 163 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 166 | bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode, |
| 167 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 168 | MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); |
| 169 | MVT DstVT = MVT::getMVT(I->getType()); |
| 170 | |
| 171 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 172 | DstVT == MVT::Other || !DstVT.isSimple() || |
| 173 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 174 | // Unhandled type. Halt "fast" selection and bail. |
| 175 | return false; |
| 176 | |
| 177 | unsigned InputReg = ValueMap[I->getOperand(0)]; |
| 178 | if (!InputReg) |
| 179 | // Unhandled operand. Halt "fast" selection and bail. |
| 180 | return false; |
| 181 | |
| 182 | unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), |
| 183 | DstVT.getSimpleVT(), |
| 184 | Opcode, |
| 185 | InputReg); |
| 186 | if (!ResultReg) |
| 187 | return false; |
| 188 | |
| 189 | ValueMap[I] = ResultReg; |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool FastISel::SelectConstantCast(Instruction* I, ISD::NodeType Opcode, |
| 194 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 195 | // Materialize constant and convert. |
| 196 | ConstantInt* CI = cast<ConstantInt>(I->getOperand(0)); |
| 197 | MVT SrcVT = MVT::getMVT(CI->getType()); |
| 198 | MVT DstVT = MVT::getMVT(I->getType()); |
| 199 | |
| 200 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 201 | DstVT == MVT::Other || !DstVT.isSimple() || |
| 202 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 203 | // Unhandled type. Halt "fast" selection and bail. |
| 204 | return false; |
| 205 | |
| 206 | unsigned ResultReg1 = FastEmit_i(SrcVT.getSimpleVT(), |
| 207 | SrcVT.getSimpleVT(), |
| 208 | ISD::Constant, CI->getZExtValue()); |
| 209 | if (!ResultReg1) |
| 210 | return false; |
| 211 | |
| 212 | unsigned ResultReg2 = FastEmit_r(SrcVT.getSimpleVT(), |
| 213 | DstVT.getSimpleVT(), |
| 214 | Opcode, |
| 215 | ResultReg1); |
| 216 | if (!ResultReg2) |
| 217 | return false; |
| 218 | |
| 219 | ValueMap[I] = ResultReg2; |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool FastISel::SelectConstantFPCast(Instruction* I, ISD::NodeType Opcode, |
| 224 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 225 | // TODO: Implement casting of FP constants by materialization |
| 226 | // followed by conversion. |
| 227 | return false; |
| 228 | } |
| 229 | |
Dan Gohman | 763d893 | 2008-08-26 21:28:54 +0000 | [diff] [blame] | 230 | bool FastISel::SelectBitCast(Instruction *I, |
| 231 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 232 | // BitCast consists of either an immediate to register move |
| 233 | // or a register to register move. |
| 234 | if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) { |
| 235 | if (I->getType()->isInteger()) { |
| 236 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false); |
| 237 | unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(), |
| 238 | ISD::Constant, |
| 239 | CI->getZExtValue()); |
| 240 | if (!result) |
| 241 | return false; |
| 242 | |
| 243 | ValueMap[I] = result; |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | // TODO: Support vector and fp constants. |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (!isa<Constant>(I->getOperand(0))) { |
| 252 | // Bitcasts of non-constant values become reg-reg copies. |
| 253 | MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); |
| 254 | MVT DstVT = MVT::getMVT(I->getType()); |
| 255 | |
| 256 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 257 | DstVT == MVT::Other || !DstVT.isSimple() || |
| 258 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 259 | // Unhandled type. Halt "fast" selection and bail. |
| 260 | return false; |
| 261 | |
| 262 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 263 | if (Op0 == 0) |
| 264 | // Unhandled operand. Halt "fast" selection and bail. |
| 265 | return false; |
| 266 | |
| 267 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 268 | unsigned ResultReg = 0; |
| 269 | if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { |
| 270 | TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 271 | TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
| 272 | ResultReg = createResultReg(DstClass); |
| 273 | |
| 274 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 275 | Op0, DstClass, SrcClass); |
| 276 | if (!InsertedCopy) |
| 277 | ResultReg = 0; |
| 278 | } |
| 279 | |
| 280 | // If the reg-reg copy failed, select a BIT_CONVERT opcode. |
| 281 | if (!ResultReg) |
| 282 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), |
| 283 | ISD::BIT_CONVERT, Op0); |
| 284 | |
| 285 | if (!ResultReg) |
| 286 | return false; |
| 287 | |
| 288 | ValueMap[I] = ResultReg; |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | // TODO: Casting a non-integral constant? |
| 293 | return false; |
| 294 | } |
| 295 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 296 | BasicBlock::iterator |
Dan Gohman | b7864a9 | 2008-08-20 18:09:02 +0000 | [diff] [blame] | 297 | FastISel::SelectInstructions(BasicBlock::iterator Begin, |
| 298 | BasicBlock::iterator End, |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 299 | DenseMap<const Value*, unsigned> &ValueMap, |
Dan Gohman | 6ecf509 | 2008-08-23 02:44:46 +0000 | [diff] [blame] | 300 | DenseMap<const BasicBlock*, |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 301 | MachineBasicBlock *> &MBBMap, |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 302 | MachineBasicBlock *mbb) { |
| 303 | MBB = mbb; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 304 | BasicBlock::iterator I = Begin; |
| 305 | |
| 306 | for (; I != End; ++I) { |
| 307 | switch (I->getOpcode()) { |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 308 | case Instruction::Add: { |
| 309 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD; |
| 310 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 311 | } |
| 312 | case Instruction::Sub: { |
| 313 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB; |
| 314 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 315 | } |
| 316 | case Instruction::Mul: { |
| 317 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL; |
| 318 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 319 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 320 | case Instruction::SDiv: |
| 321 | if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break; |
| 322 | case Instruction::UDiv: |
| 323 | if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break; |
| 324 | case Instruction::FDiv: |
| 325 | if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break; |
| 326 | case Instruction::SRem: |
| 327 | if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break; |
| 328 | case Instruction::URem: |
| 329 | if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break; |
| 330 | case Instruction::FRem: |
| 331 | if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break; |
| 332 | case Instruction::Shl: |
| 333 | if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break; |
| 334 | case Instruction::LShr: |
| 335 | if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break; |
| 336 | case Instruction::AShr: |
| 337 | if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break; |
| 338 | case Instruction::And: |
| 339 | if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break; |
| 340 | case Instruction::Or: |
| 341 | if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break; |
| 342 | case Instruction::Xor: |
| 343 | if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break; |
| 344 | |
| 345 | case Instruction::GetElementPtr: |
| 346 | if (!SelectGetElementPtr(I, ValueMap)) return I; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 347 | break; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 348 | |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 349 | case Instruction::Br: { |
| 350 | BranchInst *BI = cast<BranchInst>(I); |
| 351 | |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 352 | if (BI->isUnconditional()) { |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 353 | MachineFunction::iterator NextMBB = |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 354 | next(MachineFunction::iterator(MBB)); |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 355 | BasicBlock *LLVMSucc = BI->getSuccessor(0); |
| 356 | MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; |
| 357 | |
| 358 | if (NextMBB != MF.end() && MSucc == NextMBB) { |
| 359 | // The unconditional fall-through case, which needs no instructions. |
| 360 | } else { |
| 361 | // The unconditional branch case. |
| 362 | TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 363 | } |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 364 | MBB->addSuccessor(MSucc); |
| 365 | break; |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 368 | // Conditional branches are not handed yet. |
| 369 | // Halt "fast" selection and bail. |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 370 | return I; |
| 371 | } |
Dan Gohman | 3b7753b | 2008-08-22 17:37:48 +0000 | [diff] [blame] | 372 | |
| 373 | case Instruction::PHI: |
| 374 | // PHI nodes are already emitted. |
| 375 | break; |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 376 | |
| 377 | case Instruction::BitCast: |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 378 | if (!SelectBitCast(I, ValueMap)) return I; break; |
Owen Anderson | 46aa2f5 | 2008-08-26 17:44:42 +0000 | [diff] [blame] | 379 | |
| 380 | case Instruction::FPToSI: |
| 381 | if (!isa<ConstantFP>(I->getOperand(0))) { |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 382 | if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I; |
Owen Anderson | 46aa2f5 | 2008-08-26 17:44:42 +0000 | [diff] [blame] | 383 | } else |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 384 | if (!SelectConstantFPCast(I, ISD::FP_TO_SINT, ValueMap)) return I; |
| 385 | break; |
Owen Anderson | 97e2568 | 2008-08-26 23:14:49 +0000 | [diff] [blame] | 386 | case Instruction::ZExt: |
| 387 | if (!isa<ConstantInt>(I->getOperand(0))) { |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 388 | if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; |
Owen Anderson | 97e2568 | 2008-08-26 23:14:49 +0000 | [diff] [blame] | 389 | } else |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 390 | if (!SelectConstantCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; |
| 391 | break; |
| 392 | case Instruction::SExt: |
| 393 | if (!isa<ConstantInt>(I->getOperand(0))) { |
| 394 | if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I; |
| 395 | } else |
| 396 | if (!SelectConstantCast(I, ISD::SIGN_EXTEND, ValueMap)) return I; |
| 397 | break; |
Owen Anderson | a843b8d | 2008-08-26 20:37:00 +0000 | [diff] [blame] | 398 | case Instruction::SIToFP: |
| 399 | if (!isa<ConstantInt>(I->getOperand(0))) { |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 400 | if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I; |
| 401 | } else |
| 402 | if (!SelectConstantCast(I, ISD::SINT_TO_FP, ValueMap)) return I; |
| 403 | break; |
Dan Gohman | 763d893 | 2008-08-26 21:28:54 +0000 | [diff] [blame] | 404 | |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 405 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 406 | case Instruction::PtrToInt: { |
| 407 | MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 408 | MVT DstVT = TLI.getValueType(I->getType()); |
| 409 | if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { |
Owen Anderson | 96c5ea8 | 2008-08-27 00:35:37 +0000 | [diff] [blame] | 410 | if (ValueMap[I->getOperand(0)]) { |
| 411 | ValueMap[I] = ValueMap[I->getOperand(0)]; |
| 412 | break; |
| 413 | } else |
| 414 | // Unhandled operand |
| 415 | return I; |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 416 | } else if (DstVT.bitsGT(SrcVT)) { |
| 417 | if (!isa<ConstantInt>(I->getOperand(0))) { |
| 418 | if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; |
| 419 | } else |
| 420 | if (!SelectConstantCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; |
| 421 | break; |
| 422 | } else { |
| 423 | // TODO: Handle SrcVT > DstVT, where truncation is needed. |
| 424 | return I; |
| 425 | } |
| 426 | } |
| 427 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 428 | default: |
| 429 | // Unhandled instruction. Halt "fast" selection and bail. |
| 430 | return I; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return I; |
| 435 | } |
| 436 | |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 437 | FastISel::FastISel(MachineFunction &mf) |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 438 | : MF(mf), |
| 439 | MRI(mf.getRegInfo()), |
| 440 | TM(mf.getTarget()), |
| 441 | TD(*TM.getTargetData()), |
| 442 | TII(*TM.getInstrInfo()), |
| 443 | TLI(*TM.getTargetLowering()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 446 | FastISel::~FastISel() {} |
| 447 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 448 | unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 452 | unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType, |
| 453 | ISD::NodeType, unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 457 | unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, |
| 458 | ISD::NodeType, unsigned /*Op0*/, |
| 459 | unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 463 | unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType, |
| 464 | ISD::NodeType, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame^] | 468 | unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType, |
| 469 | ISD::NodeType, ConstantFP * /*FPImm*/) { |
| 470 | return 0; |
| 471 | } |
| 472 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 473 | unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType, |
| 474 | ISD::NodeType, unsigned /*Op0*/, |
| 475 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame^] | 479 | unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType, |
| 480 | ISD::NodeType, unsigned /*Op0*/, |
| 481 | ConstantFP * /*FPImm*/) { |
| 482 | return 0; |
| 483 | } |
| 484 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 485 | unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType, |
| 486 | ISD::NodeType, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 487 | unsigned /*Op0*/, unsigned /*Op1*/, |
| 488 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 493 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 494 | /// If that fails, it materializes the immediate into a register and try |
| 495 | /// FastEmit_rr instead. |
| 496 | unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 497 | unsigned Op0, uint64_t Imm, |
| 498 | MVT::SimpleValueType ImmType) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 499 | unsigned ResultReg = 0; |
| 500 | // First check if immediate type is legal. If not, we can't use the ri form. |
| 501 | if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal) |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 502 | ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 503 | if (ResultReg != 0) |
| 504 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 505 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 506 | if (MaterialReg == 0) |
| 507 | return 0; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 508 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame^] | 511 | /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries |
| 512 | /// to emit an instruction with a floating-point immediate operand using |
| 513 | /// FastEmit_rf. If that fails, it materializes the immediate into a register |
| 514 | /// and try FastEmit_rr instead. |
| 515 | unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode, |
| 516 | unsigned Op0, ConstantFP *FPImm, |
| 517 | MVT::SimpleValueType ImmType) { |
| 518 | unsigned ResultReg = 0; |
| 519 | // First check if immediate type is legal. If not, we can't use the rf form. |
| 520 | if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal) |
| 521 | ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm); |
| 522 | if (ResultReg != 0) |
| 523 | return ResultReg; |
| 524 | |
| 525 | // Materialize the constant in a register. |
| 526 | unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm); |
| 527 | if (MaterialReg == 0) { |
| 528 | const APFloat &Flt = FPImm->getValueAPF(); |
| 529 | MVT IntVT = TLI.getPointerTy(); |
| 530 | |
| 531 | uint64_t x[2]; |
| 532 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
| 533 | if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 534 | APFloat::rmTowardZero) != APFloat::opOK) |
| 535 | return 0; |
| 536 | APInt IntVal(IntBitWidth, 2, x); |
| 537 | |
| 538 | unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(), |
| 539 | ISD::Constant, IntVal.getZExtValue()); |
| 540 | if (IntegerReg == 0) |
| 541 | return 0; |
| 542 | MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT, |
| 543 | ISD::SINT_TO_FP, IntegerReg); |
| 544 | if (MaterialReg == 0) |
| 545 | return 0; |
| 546 | } |
| 547 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
| 548 | } |
| 549 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 550 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 551 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 554 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 555 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 556 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 557 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 558 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 559 | BuildMI(MBB, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 560 | return ResultReg; |
| 561 | } |
| 562 | |
| 563 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 564 | const TargetRegisterClass *RC, |
| 565 | unsigned Op0) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 566 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 567 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 568 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 569 | BuildMI(MBB, II, ResultReg).addReg(Op0); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 570 | return ResultReg; |
| 571 | } |
| 572 | |
| 573 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 574 | const TargetRegisterClass *RC, |
| 575 | unsigned Op0, unsigned Op1) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 576 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 577 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 578 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 579 | BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 580 | return ResultReg; |
| 581 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 582 | |
| 583 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 584 | const TargetRegisterClass *RC, |
| 585 | unsigned Op0, uint64_t Imm) { |
| 586 | unsigned ResultReg = createResultReg(RC); |
| 587 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 588 | |
| 589 | BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm); |
| 590 | return ResultReg; |
| 591 | } |
| 592 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame^] | 593 | unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 594 | const TargetRegisterClass *RC, |
| 595 | unsigned Op0, ConstantFP *FPImm) { |
| 596 | unsigned ResultReg = createResultReg(RC); |
| 597 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 598 | |
| 599 | BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm); |
| 600 | return ResultReg; |
| 601 | } |
| 602 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 603 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 604 | const TargetRegisterClass *RC, |
| 605 | unsigned Op0, unsigned Op1, uint64_t Imm) { |
| 606 | unsigned ResultReg = createResultReg(RC); |
| 607 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 608 | |
| 609 | BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); |
| 610 | return ResultReg; |
| 611 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 612 | |
| 613 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 614 | const TargetRegisterClass *RC, |
| 615 | uint64_t Imm) { |
| 616 | unsigned ResultReg = createResultReg(RC); |
| 617 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 618 | |
| 619 | BuildMI(MBB, II, ResultReg).addImm(Imm); |
| 620 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 621 | } |