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