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