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