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