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