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