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; |
| 33 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 34 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 35 | if (Op0 == 0) |
| 36 | // Unhandled operand. Halt "fast" selection and bail. |
| 37 | return false; |
| 38 | |
| 39 | // Check if the second operand is a constant and handle it appropriately. |
| 40 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 41 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, |
| 42 | CI->getZExtValue(), VT.getSimpleVT()); |
| 43 | if (ResultReg == 0) |
| 44 | // Target-specific code wasn't able to find a machine opcode for |
| 45 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 46 | return false; |
| 47 | |
| 48 | // We successfully emitted code for the given LLVM Instruction. |
| 49 | ValueMap[I] = ResultReg; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | unsigned Op1 = ValueMap[I->getOperand(1)]; |
| 54 | if (Op1 == 0) |
| 55 | // Unhandled operand. Halt "fast" selection and bail. |
| 56 | return false; |
| 57 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 58 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
| 59 | ISDOpcode, Op0, Op1); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 60 | if (ResultReg == 0) |
| 61 | // Target-specific code wasn't able to find a machine opcode for |
| 62 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 63 | return false; |
| 64 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 65 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 66 | ValueMap[I] = ResultReg; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | bool FastISel::SelectGetElementPtr(Instruction *I, |
| 71 | DenseMap<const Value*, unsigned> &ValueMap) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 72 | unsigned N = ValueMap[I->getOperand(0)]; |
| 73 | if (N == 0) |
| 74 | // Unhandled operand. Halt "fast" selection and bail. |
| 75 | return false; |
| 76 | |
| 77 | const Type *Ty = I->getOperand(0)->getType(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 78 | MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT(); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 79 | for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); |
| 80 | OI != E; ++OI) { |
| 81 | Value *Idx = *OI; |
| 82 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 83 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 84 | if (Field) { |
| 85 | // N = N + Offset |
| 86 | uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); |
| 87 | // FIXME: This can be optimized by combining the add with a |
| 88 | // subsequent one. |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 89 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 90 | if (N == 0) |
| 91 | // Unhandled operand. Halt "fast" selection and bail. |
| 92 | return false; |
| 93 | } |
| 94 | Ty = StTy->getElementType(Field); |
| 95 | } else { |
| 96 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 97 | |
| 98 | // If this is a constant subscript, handle it quickly. |
| 99 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 100 | if (CI->getZExtValue() == 0) continue; |
| 101 | uint64_t Offs = |
| 102 | TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 103 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 104 | if (N == 0) |
| 105 | // Unhandled operand. Halt "fast" selection and bail. |
| 106 | return false; |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | // N = N + Idx * ElementSize; |
| 111 | uint64_t ElementSize = TD.getABITypeSize(Ty); |
| 112 | unsigned IdxN = ValueMap[Idx]; |
| 113 | if (IdxN == 0) |
| 114 | // Unhandled operand. Halt "fast" selection and bail. |
| 115 | return false; |
| 116 | |
| 117 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 118 | // it. |
Evan Cheng | 2076aa8 | 2008-08-21 01:19:11 +0000 | [diff] [blame] | 119 | MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 120 | if (IdxVT.bitsLT(VT)) |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 121 | IdxN = FastEmit_r(VT, VT, ISD::SIGN_EXTEND, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 122 | else if (IdxVT.bitsGT(VT)) |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 123 | IdxN = FastEmit_r(VT, VT, ISD::TRUNCATE, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 124 | if (IdxN == 0) |
| 125 | // Unhandled operand. Halt "fast" selection and bail. |
| 126 | return false; |
| 127 | |
Dan Gohman | f93cf79 | 2008-08-21 17:37:05 +0000 | [diff] [blame] | 128 | if (ElementSize != 1) |
| 129 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); |
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; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 133 | N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 134 | if (N == 0) |
| 135 | // Unhandled operand. Halt "fast" selection and bail. |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // We successfully emitted code for the given LLVM Instruction. |
| 141 | ValueMap[I] = N; |
| 142 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 145 | BasicBlock::iterator |
Dan Gohman | b7864a9 | 2008-08-20 18:09:02 +0000 | [diff] [blame] | 146 | FastISel::SelectInstructions(BasicBlock::iterator Begin, |
| 147 | BasicBlock::iterator End, |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 148 | DenseMap<const Value*, unsigned> &ValueMap, |
Dan Gohman | 6ecf509 | 2008-08-23 02:44:46 +0000 | [diff] [blame] | 149 | DenseMap<const BasicBlock*, |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 150 | MachineBasicBlock *> &MBBMap, |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 151 | MachineBasicBlock *mbb) { |
| 152 | MBB = mbb; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 153 | BasicBlock::iterator I = Begin; |
| 154 | |
| 155 | for (; I != End; ++I) { |
| 156 | switch (I->getOpcode()) { |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 157 | case Instruction::Add: { |
| 158 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD; |
| 159 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 160 | } |
| 161 | case Instruction::Sub: { |
| 162 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB; |
| 163 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 164 | } |
| 165 | case Instruction::Mul: { |
| 166 | ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL; |
| 167 | if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; |
| 168 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 169 | case Instruction::SDiv: |
| 170 | if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break; |
| 171 | case Instruction::UDiv: |
| 172 | if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break; |
| 173 | case Instruction::FDiv: |
| 174 | if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break; |
| 175 | case Instruction::SRem: |
| 176 | if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break; |
| 177 | case Instruction::URem: |
| 178 | if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break; |
| 179 | case Instruction::FRem: |
| 180 | if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break; |
| 181 | case Instruction::Shl: |
| 182 | if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break; |
| 183 | case Instruction::LShr: |
| 184 | if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break; |
| 185 | case Instruction::AShr: |
| 186 | if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break; |
| 187 | case Instruction::And: |
| 188 | if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break; |
| 189 | case Instruction::Or: |
| 190 | if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break; |
| 191 | case Instruction::Xor: |
| 192 | if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break; |
| 193 | |
| 194 | case Instruction::GetElementPtr: |
| 195 | if (!SelectGetElementPtr(I, ValueMap)) return I; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 196 | break; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 197 | |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 198 | case Instruction::Br: { |
| 199 | BranchInst *BI = cast<BranchInst>(I); |
| 200 | |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 201 | if (BI->isUnconditional()) { |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 202 | MachineFunction::iterator NextMBB = |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 203 | next(MachineFunction::iterator(MBB)); |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 204 | BasicBlock *LLVMSucc = BI->getSuccessor(0); |
| 205 | MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; |
| 206 | |
| 207 | if (NextMBB != MF.end() && MSucc == NextMBB) { |
| 208 | // The unconditional fall-through case, which needs no instructions. |
| 209 | } else { |
| 210 | // The unconditional branch case. |
| 211 | TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); |
Dan Gohman | e6798b7 | 2008-08-20 01:17:01 +0000 | [diff] [blame] | 212 | } |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 213 | MBB->addSuccessor(MSucc); |
| 214 | break; |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Dan Gohman | 3c8f36f | 2008-08-22 21:28:19 +0000 | [diff] [blame] | 217 | // Conditional branches are not handed yet. |
| 218 | // Halt "fast" selection and bail. |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 219 | return I; |
| 220 | } |
Dan Gohman | 3b7753b | 2008-08-22 17:37:48 +0000 | [diff] [blame] | 221 | |
| 222 | case Instruction::PHI: |
| 223 | // PHI nodes are already emitted. |
| 224 | break; |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 225 | |
| 226 | case Instruction::BitCast: |
| 227 | // BitCast consists of either an immediate to register move |
| 228 | // or a register to register move. |
| 229 | if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) { |
| 230 | if (I->getType()->isInteger()) { |
| 231 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 232 | ValueMap[I] = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(), |
| 233 | ISD::Constant, |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 234 | CI->getZExtValue()); |
| 235 | break; |
| 236 | } else |
| 237 | // TODO: Support vector and fp constants. |
| 238 | return I; |
Owen Anderson | d894f1d | 2008-08-25 21:32:34 +0000 | [diff] [blame] | 239 | } else if (!isa<Constant>(I->getOperand(0))) { |
| 240 | // Bitcasts of non-constant values become reg-reg copies. |
| 241 | MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); |
| 242 | MVT DstVT = MVT::getMVT(I->getOperand(0)->getType()); |
| 243 | |
| 244 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 245 | DstVT == MVT::Other || !DstVT.isSimple() || |
| 246 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 247 | // Unhandled type. Halt "fast" selection and bail. |
| 248 | return I; |
| 249 | if (!TLI.isConvertLegal(SrcVT, DstVT)) |
| 250 | // Illegal conversion. Halt "fast" selection and bail. |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 251 | return I; |
Owen Anderson | d894f1d | 2008-08-25 21:32:34 +0000 | [diff] [blame] | 252 | |
| 253 | // Otherwise, insert a register-to-register copy. |
| 254 | TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 255 | TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
| 256 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 257 | unsigned ResultReg = createResultReg(DstClass); |
| 258 | |
| 259 | if (Op0 == 0) |
| 260 | // Unhandled operand. Halt "fast" selection and bail. |
| 261 | return false; |
| 262 | |
| 263 | TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Op0, DstClass, SrcClass); |
| 264 | ValueMap[I] = ResultReg; |
| 265 | |
| 266 | break; |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 267 | } else |
Owen Anderson | d894f1d | 2008-08-25 21:32:34 +0000 | [diff] [blame] | 268 | // Casting a non-integral constant? |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 269 | return I; |
Dan Gohman | 3b7753b | 2008-08-22 17:37:48 +0000 | [diff] [blame] | 270 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 271 | default: |
| 272 | // Unhandled instruction. Halt "fast" selection and bail. |
| 273 | return I; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return I; |
| 278 | } |
| 279 | |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 280 | FastISel::FastISel(MachineFunction &mf) |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 281 | : MF(mf), |
| 282 | MRI(mf.getRegInfo()), |
| 283 | TM(mf.getTarget()), |
| 284 | TD(*TM.getTargetData()), |
| 285 | TII(*TM.getInstrInfo()), |
| 286 | TLI(*TM.getTargetLowering()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 289 | FastISel::~FastISel() {} |
| 290 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 291 | unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 295 | unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType, |
| 296 | ISD::NodeType, unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 300 | unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, |
| 301 | ISD::NodeType, unsigned /*Op0*/, |
| 302 | unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 306 | unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType, |
| 307 | ISD::NodeType, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 311 | unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType, |
| 312 | ISD::NodeType, unsigned /*Op0*/, |
| 313 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 317 | unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType, |
| 318 | ISD::NodeType, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 319 | unsigned /*Op0*/, unsigned /*Op1*/, |
| 320 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 325 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 326 | /// If that fails, it materializes the immediate into a register and try |
| 327 | /// FastEmit_rr instead. |
| 328 | unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 329 | unsigned Op0, uint64_t Imm, |
| 330 | MVT::SimpleValueType ImmType) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 331 | unsigned ResultReg = 0; |
| 332 | // First check if immediate type is legal. If not, we can't use the ri form. |
| 333 | if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal) |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 334 | ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 335 | if (ResultReg != 0) |
| 336 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 337 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 338 | if (MaterialReg == 0) |
| 339 | return 0; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 340 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 344 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 347 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 348 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 349 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 350 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 351 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 352 | BuildMI(MBB, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 353 | return ResultReg; |
| 354 | } |
| 355 | |
| 356 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 357 | const TargetRegisterClass *RC, |
| 358 | unsigned Op0) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 359 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 360 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 361 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 362 | BuildMI(MBB, II, ResultReg).addReg(Op0); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 363 | return ResultReg; |
| 364 | } |
| 365 | |
| 366 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 367 | const TargetRegisterClass *RC, |
| 368 | unsigned Op0, unsigned Op1) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 369 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 370 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 371 | |
Dan Gohman | fd90394 | 2008-08-20 23:53:10 +0000 | [diff] [blame] | 372 | BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 373 | return ResultReg; |
| 374 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 375 | |
| 376 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 377 | const TargetRegisterClass *RC, |
| 378 | unsigned Op0, uint64_t Imm) { |
| 379 | unsigned ResultReg = createResultReg(RC); |
| 380 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 381 | |
| 382 | BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm); |
| 383 | return ResultReg; |
| 384 | } |
| 385 | |
| 386 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 387 | const TargetRegisterClass *RC, |
| 388 | unsigned Op0, unsigned Op1, uint64_t Imm) { |
| 389 | unsigned ResultReg = createResultReg(RC); |
| 390 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 391 | |
| 392 | BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); |
| 393 | return ResultReg; |
| 394 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 395 | |
| 396 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 397 | const TargetRegisterClass *RC, |
| 398 | uint64_t Imm) { |
| 399 | unsigned ResultReg = createResultReg(RC); |
| 400 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 401 | |
| 402 | BuildMI(MBB, II, ResultReg).addImm(Imm); |
| 403 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 404 | } |