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