Dan Gohman | 3b172f1 | 2010-04-22 20:06:42 +0000 | [diff] [blame] | 1 | //===-- FastISel.cpp - Implementation of the FastISel class ---------------===// |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 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" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/DebugInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 51 | #include "llvm/Target/TargetData.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 56 | #include "FunctionLoweringInfo.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 59 | unsigned FastISel::getRegForValue(const Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 60 | EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true); |
Dan Gohman | 4fd5528 | 2009-04-07 20:40:11 +0000 | [diff] [blame] | 61 | // Don't handle non-simple values in FastISel. |
| 62 | if (!RealVT.isSimple()) |
| 63 | return 0; |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 64 | |
| 65 | // Ignore illegal types. We must do this before looking up the value |
| 66 | // in ValueMap because Arguments are given virtual registers regardless |
| 67 | // of whether FastISel can handle them. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 68 | MVT VT = RealVT.getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 69 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 70 | // Promote MVT::i1 to a legal type though, because it's common and easy. |
| 71 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 72 | VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 73 | else |
| 74 | return 0; |
| 75 | } |
| 76 | |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 77 | // Look up the value to see if we already have a register for it. We |
| 78 | // cache values defined by Instructions across blocks, and other values |
| 79 | // only locally. This is because Instructions already have the SSA |
Dan Gohman | 5c9cf19 | 2010-01-12 04:30:26 +0000 | [diff] [blame] | 80 | // def-dominates-use requirement enforced. |
Owen Anderson | 99aaf10 | 2008-09-03 17:37:03 +0000 | [diff] [blame] | 81 | if (ValueMap.count(V)) |
| 82 | return ValueMap[V]; |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 83 | unsigned Reg = LocalValueMap[V]; |
| 84 | if (Reg != 0) |
| 85 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 86 | |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame^] | 87 | return materializeRegForValue(V, VT); |
| 88 | } |
| 89 | |
| 90 | /// materializeRegForValue - Helper for getRegForVale. This function is |
| 91 | /// called when the value isn't already available in a register and must |
| 92 | /// be materialized with new instructions. |
| 93 | unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) { |
| 94 | unsigned Reg = 0; |
| 95 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 96 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 97 | if (CI->getValue().getActiveBits() <= 64) |
| 98 | Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 99 | } else if (isa<AllocaInst>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 100 | Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 101 | } else if (isa<ConstantPointerNull>(V)) { |
Dan Gohman | 1e9e8c3 | 2008-10-07 22:03:27 +0000 | [diff] [blame] | 102 | // Translate this as an integer zero so that it can be |
| 103 | // local-CSE'd with actual integer zeros. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 104 | Reg = |
| 105 | getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext()))); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 106 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 107 | // Try to emit the constant directly. |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 108 | Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 109 | |
| 110 | if (!Reg) { |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 111 | // Try to emit the constant by using an integer constant with a cast. |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 112 | const APFloat &Flt = CF->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 113 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 114 | |
| 115 | uint64_t x[2]; |
| 116 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 117 | bool isExact; |
| 118 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 119 | APFloat::rmTowardZero, &isExact); |
| 120 | if (isExact) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 121 | APInt IntVal(IntBitWidth, 2, x); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 122 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 123 | unsigned IntegerReg = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 124 | getRegForValue(ConstantInt::get(V->getContext(), IntVal)); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 125 | if (IntegerReg != 0) |
| 126 | Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg); |
| 127 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 128 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 129 | } else if (const Operator *Op = dyn_cast<Operator>(V)) { |
Dan Gohman | 32acbc1 | 2010-04-14 02:33:23 +0000 | [diff] [blame] | 130 | if (!SelectOperator(Op, Op->getOpcode())) return 0; |
| 131 | Reg = LocalValueMap[Op]; |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 132 | } else if (isa<UndefValue>(V)) { |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 133 | Reg = createResultReg(TLI.getRegClassFor(VT)); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 134 | BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 135 | } |
Owen Anderson | d5d81a4 | 2008-09-03 17:51:57 +0000 | [diff] [blame] | 136 | |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 137 | // If target-independent code couldn't handle the value, give target-specific |
| 138 | // code a try. |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 139 | if (!Reg && isa<Constant>(V)) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 140 | Reg = TargetMaterializeConstant(cast<Constant>(V)); |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 141 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 142 | // Don't cache constant materializations in the general ValueMap. |
| 143 | // To do so would require tracking what uses they dominate. |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 144 | if (Reg != 0) |
| 145 | LocalValueMap[V] = Reg; |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 146 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 149 | unsigned FastISel::lookUpRegForValue(const Value *V) { |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 150 | // Look up the value to see if we already have a register for it. We |
| 151 | // cache values defined by Instructions across blocks, and other values |
| 152 | // only locally. This is because Instructions already have the SSA |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame^] | 153 | // def-dominates-use requirement enforced. |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 154 | if (ValueMap.count(V)) |
| 155 | return ValueMap[V]; |
| 156 | return LocalValueMap[V]; |
| 157 | } |
| 158 | |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 159 | /// UpdateValueMap - Update the value map to include the new mapping for this |
| 160 | /// instruction, or insert an extra copy to get the result in a previous |
| 161 | /// determined register. |
| 162 | /// NOTE: This is only necessary because we might select a block that uses |
| 163 | /// a value before we select the block that defines the value. It might be |
| 164 | /// possible to fix this by selecting blocks in reverse postorder. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 165 | unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 166 | if (!isa<Instruction>(I)) { |
| 167 | LocalValueMap[I] = Reg; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 168 | return Reg; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 170 | |
| 171 | unsigned &AssignedReg = ValueMap[I]; |
| 172 | if (AssignedReg == 0) |
| 173 | AssignedReg = Reg; |
Chris Lattner | 36e3946 | 2009-04-12 07:46:30 +0000 | [diff] [blame] | 174 | else if (Reg != AssignedReg) { |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 175 | const TargetRegisterClass *RegClass = MRI.getRegClass(Reg); |
| 176 | TII.copyRegToReg(*MBB, MBB->end(), AssignedReg, |
| 177 | Reg, RegClass, RegClass); |
| 178 | } |
| 179 | return AssignedReg; |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 182 | unsigned FastISel::getRegForGEPIndex(const Value *Idx) { |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 183 | unsigned IdxN = getRegForValue(Idx); |
| 184 | if (IdxN == 0) |
| 185 | // Unhandled operand. Halt "fast" selection and bail. |
| 186 | return 0; |
| 187 | |
| 188 | // If the index is smaller or larger than intptr_t, truncate or extend it. |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 189 | MVT PtrVT = TLI.getPointerTy(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 190 | EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 191 | if (IdxVT.bitsLT(PtrVT)) |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 192 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 193 | else if (IdxVT.bitsGT(PtrVT)) |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 194 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 195 | return IdxN; |
| 196 | } |
| 197 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 198 | /// SelectBinaryOp - Select and emit code for a binary operator instruction, |
| 199 | /// which has an opcode which directly corresponds to the given ISD opcode. |
| 200 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 201 | bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 202 | EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 203 | if (VT == MVT::Other || !VT.isSimple()) |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 204 | // Unhandled type. Halt "fast" selection and bail. |
| 205 | return false; |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 206 | |
Dan Gohman | b71fea2 | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 207 | // We only handle legal types. For example, on x86-32 the instruction |
| 208 | // selector contains all of the 64-bit instructions from x86-64, |
| 209 | // under the assumption that i64 won't be used if the target doesn't |
| 210 | // support it. |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 211 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 212 | // MVT::i1 is special. Allow AND, OR, or XOR because they |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 213 | // don't require additional zeroing, which makes them easy. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 214 | if (VT == MVT::i1 && |
Dan Gohman | 5dd9c2e | 2008-09-25 17:22:52 +0000 | [diff] [blame] | 215 | (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || |
| 216 | ISDOpcode == ISD::XOR)) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 217 | VT = TLI.getTypeToTransformTo(I->getContext(), VT); |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 218 | else |
| 219 | return false; |
| 220 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 221 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 222 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 223 | if (Op0 == 0) |
| 224 | // Unhandled operand. Halt "fast" selection and bail. |
| 225 | return false; |
| 226 | |
| 227 | // Check if the second operand is a constant and handle it appropriately. |
| 228 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 229 | unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(), |
| 230 | ISDOpcode, Op0, CI->getZExtValue()); |
| 231 | if (ResultReg != 0) { |
| 232 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 233 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 238 | // Check if the second operand is a constant float. |
| 239 | if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 240 | unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), |
| 241 | ISDOpcode, Op0, CF); |
| 242 | if (ResultReg != 0) { |
| 243 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 244 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 249 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 250 | if (Op1 == 0) |
| 251 | // Unhandled operand. Halt "fast" selection and bail. |
| 252 | return false; |
| 253 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 254 | // Now we have both operands in registers. Emit the instruction. |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 255 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
| 256 | ISDOpcode, Op0, Op1); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 257 | if (ResultReg == 0) |
| 258 | // Target-specific code wasn't able to find a machine opcode for |
| 259 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 260 | return false; |
| 261 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 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, ResultReg); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 264 | return true; |
| 265 | } |
| 266 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 267 | bool FastISel::SelectGetElementPtr(const User *I) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 268 | unsigned N = getRegForValue(I->getOperand(0)); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 269 | if (N == 0) |
| 270 | // Unhandled operand. Halt "fast" selection and bail. |
| 271 | return false; |
| 272 | |
| 273 | const Type *Ty = I->getOperand(0)->getType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 274 | MVT VT = TLI.getPointerTy(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 275 | for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1, |
| 276 | E = I->op_end(); OI != E; ++OI) { |
| 277 | const Value *Idx = *OI; |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 278 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 279 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 280 | if (Field) { |
| 281 | // N = N + Offset |
| 282 | uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); |
| 283 | // FIXME: This can be optimized by combining the add with a |
| 284 | // subsequent one. |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 285 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 286 | if (N == 0) |
| 287 | // Unhandled operand. Halt "fast" selection and bail. |
| 288 | return false; |
| 289 | } |
| 290 | Ty = StTy->getElementType(Field); |
| 291 | } else { |
| 292 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 293 | |
| 294 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 295 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 296 | if (CI->getZExtValue() == 0) continue; |
| 297 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 298 | TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 299 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 300 | if (N == 0) |
| 301 | // Unhandled operand. Halt "fast" selection and bail. |
| 302 | return false; |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | // N = N + Idx * ElementSize; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 307 | uint64_t ElementSize = TD.getTypeAllocSize(Ty); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 308 | unsigned IdxN = getRegForGEPIndex(Idx); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 309 | if (IdxN == 0) |
| 310 | // Unhandled operand. Halt "fast" selection and bail. |
| 311 | return false; |
| 312 | |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 313 | if (ElementSize != 1) { |
Dan Gohman | f93cf79 | 2008-08-21 17:37:05 +0000 | [diff] [blame] | 314 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 315 | if (IdxN == 0) |
| 316 | // Unhandled operand. Halt "fast" selection and bail. |
| 317 | return false; |
| 318 | } |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 319 | N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 320 | if (N == 0) |
| 321 | // Unhandled operand. Halt "fast" selection and bail. |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 327 | UpdateValueMap(I, N); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 328 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 331 | bool FastISel::SelectCall(const User *I) { |
| 332 | const Function *F = cast<CallInst>(I)->getCalledFunction(); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 333 | if (!F) return false; |
| 334 | |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 335 | // Handle selected intrinsic function calls. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 336 | unsigned IID = F->getIntrinsicID(); |
| 337 | switch (IID) { |
| 338 | default: break; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 339 | case Intrinsic::dbg_declare: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 340 | const DbgDeclareInst *DI = cast<DbgDeclareInst>(I); |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 341 | if (!DIDescriptor::ValidDebugInfo(DI->getVariable(), CodeGenOpt::None) || |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 342 | !MF.getMMI().hasDebugInfo()) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 343 | return true; |
| 344 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 345 | const Value *Address = DI->getAddress(); |
Dale Johannesen | dc91856 | 2010-02-06 02:26:02 +0000 | [diff] [blame] | 346 | if (!Address) |
| 347 | return true; |
Dale Johannesen | 343b42e | 2010-04-07 01:15:14 +0000 | [diff] [blame] | 348 | if (isa<UndefValue>(Address)) |
| 349 | return true; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 350 | const AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 351 | // Don't handle byval struct arguments or VLAs, for example. |
Dale Johannesen | 7dc7840 | 2010-04-25 21:03:54 +0000 | [diff] [blame] | 352 | // Note that if we have a byval struct argument, fast ISel is turned off; |
| 353 | // those are handled in SelectionDAGBuilder. |
Devang Patel | 54fc4d6 | 2010-04-28 19:27:33 +0000 | [diff] [blame] | 354 | if (AI) { |
| 355 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 356 | StaticAllocaMap.find(AI); |
| 357 | if (SI == StaticAllocaMap.end()) break; // VLAs. |
| 358 | int FI = SI->second; |
| 359 | if (!DI->getDebugLoc().isUnknown()) |
| 360 | MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc()); |
| 361 | } else |
| 362 | // Building the map above is target independent. Generating DBG_VALUE |
| 363 | // inline is target dependent; do this now. |
| 364 | (void)TargetSelectInstruction(cast<Instruction>(I)); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 365 | return true; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 366 | } |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 367 | case Intrinsic::dbg_value: { |
Dale Johannesen | 343b42e | 2010-04-07 01:15:14 +0000 | [diff] [blame] | 368 | // This form of DBG_VALUE is target-independent. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 369 | const DbgValueInst *DI = cast<DbgValueInst>(I); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 370 | const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 371 | const Value *V = DI->getValue(); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 372 | if (!V) { |
| 373 | // Currently the optimizer can produce this; insert an undef to |
| 374 | // help debugging. Probably the optimizer should not do this. |
| 375 | BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()). |
| 376 | addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 377 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 378 | BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()). |
| 379 | addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 380 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 381 | BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()). |
| 382 | addMetadata(DI->getVariable()); |
| 383 | } else if (unsigned Reg = lookUpRegForValue(V)) { |
| 384 | BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()). |
| 385 | addMetadata(DI->getVariable()); |
| 386 | } else { |
| 387 | // We can't yet handle anything else here because it would require |
| 388 | // generating code, thus altering codegen because of debug info. |
| 389 | // Insert an undef so we can see what we dropped. |
| 390 | BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()). |
| 391 | addMetadata(DI->getVariable()); |
| 392 | } |
| 393 | return true; |
| 394 | } |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 395 | case Intrinsic::eh_exception: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 396 | EVT VT = TLI.getValueType(I->getType()); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 397 | switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) { |
| 398 | default: break; |
| 399 | case TargetLowering::Expand: { |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 400 | assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!"); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 401 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 402 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 403 | unsigned ResultReg = createResultReg(RC); |
| 404 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 405 | Reg, RC, RC); |
| 406 | assert(InsertedCopy && "Can't copy address registers!"); |
Evan Cheng | 24ac408 | 2008-11-24 07:09:49 +0000 | [diff] [blame] | 407 | InsertedCopy = InsertedCopy; |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 408 | UpdateValueMap(I, ResultReg); |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | break; |
| 413 | } |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 414 | case Intrinsic::eh_selector: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 415 | EVT VT = TLI.getValueType(I->getType()); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 416 | switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) { |
| 417 | default: break; |
| 418 | case TargetLowering::Expand: { |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 419 | if (MBB->isLandingPad()) |
| 420 | AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB); |
| 421 | else { |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 422 | #ifndef NDEBUG |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 423 | CatchInfoLost.insert(cast<CallInst>(I)); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 424 | #endif |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 425 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 426 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 427 | if (Reg) MBB->addLiveIn(Reg); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 428 | } |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 429 | |
| 430 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 431 | EVT SrcVT = TLI.getPointerTy(); |
| 432 | const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT); |
| 433 | unsigned ResultReg = createResultReg(RC); |
| 434 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg, |
| 435 | RC, RC); |
| 436 | assert(InsertedCopy && "Can't copy address registers!"); |
| 437 | InsertedCopy = InsertedCopy; |
| 438 | |
| 439 | // Cast the register to the type of the selector. |
| 440 | if (SrcVT.bitsGT(MVT::i32)) |
| 441 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE, |
| 442 | ResultReg); |
| 443 | else if (SrcVT.bitsLT(MVT::i32)) |
| 444 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, |
| 445 | ISD::SIGN_EXTEND, ResultReg); |
| 446 | if (ResultReg == 0) |
| 447 | // Unhandled operand. Halt "fast" selection and bail. |
| 448 | return false; |
| 449 | |
| 450 | UpdateValueMap(I, ResultReg); |
| 451 | |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 452 | return true; |
| 453 | } |
| 454 | } |
| 455 | break; |
| 456 | } |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 457 | } |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 458 | |
| 459 | // An arbitrary call. Bail. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 460 | return false; |
| 461 | } |
| 462 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 463 | bool FastISel::SelectCast(const User *I, unsigned Opcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 464 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 465 | EVT DstVT = TLI.getValueType(I->getType()); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 466 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 467 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 468 | DstVT == MVT::Other || !DstVT.isSimple()) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 469 | // Unhandled type. Halt "fast" selection and bail. |
| 470 | return false; |
| 471 | |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 472 | // Check if the destination type is legal. Or as a special case, |
| 473 | // it may be i1 if we're doing a truncate because that's |
| 474 | // easy and somewhat common. |
| 475 | if (!TLI.isTypeLegal(DstVT)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 476 | if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE) |
Dan Gohman | 91b6f97 | 2008-10-03 01:28:47 +0000 | [diff] [blame] | 477 | // Unhandled type. Halt "fast" selection and bail. |
| 478 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 479 | |
| 480 | // Check if the source operand is legal. Or as a special case, |
| 481 | // it may be i1 if we're doing zero-extension because that's |
| 482 | // easy and somewhat common. |
| 483 | if (!TLI.isTypeLegal(SrcVT)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 484 | if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND) |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 485 | // Unhandled type. Halt "fast" selection and bail. |
| 486 | return false; |
| 487 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 488 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 489 | if (!InputReg) |
| 490 | // Unhandled operand. Halt "fast" selection and bail. |
| 491 | return false; |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 492 | |
| 493 | // If the operand is i1, arrange for the high bits in the register to be zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 494 | if (SrcVT == MVT::i1) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 495 | SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 496 | InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg); |
| 497 | if (!InputReg) |
| 498 | return false; |
| 499 | } |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 500 | // If the result is i1, truncate to the target's type for i1 first. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 501 | if (DstVT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 502 | DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 503 | |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 504 | unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), |
| 505 | DstVT.getSimpleVT(), |
| 506 | Opcode, |
| 507 | InputReg); |
| 508 | if (!ResultReg) |
| 509 | return false; |
| 510 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 511 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 515 | bool FastISel::SelectBitCast(const User *I) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 516 | // If the bitcast doesn't change the type, just use the operand value. |
| 517 | if (I->getType() == I->getOperand(0)->getType()) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 518 | unsigned Reg = getRegForValue(I->getOperand(0)); |
Dan Gohman | a318dab | 2008-08-27 20:41:38 +0000 | [diff] [blame] | 519 | if (Reg == 0) |
| 520 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 521 | UpdateValueMap(I, Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 526 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 527 | EVT DstVT = TLI.getValueType(I->getType()); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 528 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 529 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 530 | DstVT == MVT::Other || !DstVT.isSimple() || |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 531 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 532 | // Unhandled type. Halt "fast" selection and bail. |
| 533 | return false; |
| 534 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 535 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 536 | if (Op0 == 0) |
| 537 | // Unhandled operand. Halt "fast" selection and bail. |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 538 | return false; |
| 539 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 540 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 541 | unsigned ResultReg = 0; |
| 542 | if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { |
| 543 | TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 544 | TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
| 545 | ResultReg = createResultReg(DstClass); |
| 546 | |
| 547 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 548 | Op0, DstClass, SrcClass); |
| 549 | if (!InsertedCopy) |
| 550 | ResultReg = 0; |
| 551 | } |
| 552 | |
| 553 | // If the reg-reg copy failed, select a BIT_CONVERT opcode. |
| 554 | if (!ResultReg) |
| 555 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), |
| 556 | ISD::BIT_CONVERT, Op0); |
| 557 | |
| 558 | if (!ResultReg) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 559 | return false; |
| 560 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 561 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 565 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 566 | FastISel::SelectInstruction(const Instruction *I) { |
Dan Gohman | e8c92dd | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 567 | // Just before the terminator instruction, insert instructions to |
| 568 | // feed PHI nodes in successor blocks. |
| 569 | if (isa<TerminatorInst>(I)) |
| 570 | if (!HandlePHINodesInSuccessorBlocks(I->getParent())) |
| 571 | return false; |
| 572 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 573 | DL = I->getDebugLoc(); |
| 574 | |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 575 | // First, try doing target-independent selection. |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 576 | if (SelectOperator(I, I->getOpcode())) { |
| 577 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 578 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 579 | } |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 580 | |
| 581 | // Next, try calling the target to attempt to handle the instruction. |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 582 | if (TargetSelectInstruction(I)) { |
| 583 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 584 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 585 | } |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 586 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 587 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 588 | return false; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 591 | /// FastEmitBranch - Emit an unconditional branch to the given block, |
| 592 | /// unless it is the immediate (fall-through) successor, and update |
| 593 | /// the CFG. |
| 594 | void |
| 595 | FastISel::FastEmitBranch(MachineBasicBlock *MSucc) { |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 596 | if (MBB->isLayoutSuccessor(MSucc)) { |
| 597 | // The unconditional fall-through case, which needs no instructions. |
| 598 | } else { |
| 599 | // The unconditional branch case. |
| 600 | TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); |
| 601 | } |
| 602 | MBB->addSuccessor(MSucc); |
| 603 | } |
| 604 | |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 605 | /// SelectFNeg - Emit an FNeg operation. |
| 606 | /// |
| 607 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 608 | FastISel::SelectFNeg(const User *I) { |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 609 | unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); |
| 610 | if (OpReg == 0) return false; |
| 611 | |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 612 | // If the target has ISD::FNEG, use it. |
| 613 | EVT VT = TLI.getValueType(I->getType()); |
| 614 | unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), |
| 615 | ISD::FNEG, OpReg); |
| 616 | if (ResultReg != 0) { |
| 617 | UpdateValueMap(I, ResultReg); |
| 618 | return true; |
| 619 | } |
| 620 | |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 621 | // Bitcast the value to integer, twiddle the sign bit with xor, |
| 622 | // and then bitcast it back to floating-point. |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 623 | if (VT.getSizeInBits() > 64) return false; |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 624 | EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); |
| 625 | if (!TLI.isTypeLegal(IntVT)) |
| 626 | return false; |
| 627 | |
| 628 | unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), |
| 629 | ISD::BIT_CONVERT, OpReg); |
| 630 | if (IntReg == 0) |
| 631 | return false; |
| 632 | |
| 633 | unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg, |
| 634 | UINT64_C(1) << (VT.getSizeInBits()-1), |
| 635 | IntVT.getSimpleVT()); |
| 636 | if (IntResultReg == 0) |
| 637 | return false; |
| 638 | |
| 639 | ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), |
| 640 | ISD::BIT_CONVERT, IntResultReg); |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 641 | if (ResultReg == 0) |
| 642 | return false; |
| 643 | |
| 644 | UpdateValueMap(I, ResultReg); |
| 645 | return true; |
| 646 | } |
| 647 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 648 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 649 | FastISel::SelectOperator(const User *I, unsigned Opcode) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 650 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 651 | case Instruction::Add: |
| 652 | return SelectBinaryOp(I, ISD::ADD); |
| 653 | case Instruction::FAdd: |
| 654 | return SelectBinaryOp(I, ISD::FADD); |
| 655 | case Instruction::Sub: |
| 656 | return SelectBinaryOp(I, ISD::SUB); |
| 657 | case Instruction::FSub: |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 658 | // FNeg is currently represented in LLVM IR as a special case of FSub. |
| 659 | if (BinaryOperator::isFNeg(I)) |
| 660 | return SelectFNeg(I); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 661 | return SelectBinaryOp(I, ISD::FSUB); |
| 662 | case Instruction::Mul: |
| 663 | return SelectBinaryOp(I, ISD::MUL); |
| 664 | case Instruction::FMul: |
| 665 | return SelectBinaryOp(I, ISD::FMUL); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 666 | case Instruction::SDiv: |
| 667 | return SelectBinaryOp(I, ISD::SDIV); |
| 668 | case Instruction::UDiv: |
| 669 | return SelectBinaryOp(I, ISD::UDIV); |
| 670 | case Instruction::FDiv: |
| 671 | return SelectBinaryOp(I, ISD::FDIV); |
| 672 | case Instruction::SRem: |
| 673 | return SelectBinaryOp(I, ISD::SREM); |
| 674 | case Instruction::URem: |
| 675 | return SelectBinaryOp(I, ISD::UREM); |
| 676 | case Instruction::FRem: |
| 677 | return SelectBinaryOp(I, ISD::FREM); |
| 678 | case Instruction::Shl: |
| 679 | return SelectBinaryOp(I, ISD::SHL); |
| 680 | case Instruction::LShr: |
| 681 | return SelectBinaryOp(I, ISD::SRL); |
| 682 | case Instruction::AShr: |
| 683 | return SelectBinaryOp(I, ISD::SRA); |
| 684 | case Instruction::And: |
| 685 | return SelectBinaryOp(I, ISD::AND); |
| 686 | case Instruction::Or: |
| 687 | return SelectBinaryOp(I, ISD::OR); |
| 688 | case Instruction::Xor: |
| 689 | return SelectBinaryOp(I, ISD::XOR); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 690 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 691 | case Instruction::GetElementPtr: |
| 692 | return SelectGetElementPtr(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 693 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 694 | case Instruction::Br: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 695 | const BranchInst *BI = cast<BranchInst>(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 696 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 697 | if (BI->isUnconditional()) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 698 | const BasicBlock *LLVMSucc = BI->getSuccessor(0); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 699 | MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 700 | FastEmitBranch(MSucc); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 701 | return true; |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 702 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 703 | |
| 704 | // Conditional branches are not handed yet. |
| 705 | // Halt "fast" selection and bail. |
| 706 | return false; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Dan Gohman | 087c850 | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 709 | case Instruction::Unreachable: |
| 710 | // Nothing to emit. |
| 711 | return true; |
| 712 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 713 | case Instruction::Alloca: |
| 714 | // FunctionLowering has the static-sized case covered. |
| 715 | if (StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 716 | return true; |
| 717 | |
| 718 | // Dynamic-sized alloca is not handled yet. |
| 719 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 720 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 721 | case Instruction::Call: |
| 722 | return SelectCall(I); |
| 723 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 724 | case Instruction::BitCast: |
| 725 | return SelectBitCast(I); |
| 726 | |
| 727 | case Instruction::FPToSI: |
| 728 | return SelectCast(I, ISD::FP_TO_SINT); |
| 729 | case Instruction::ZExt: |
| 730 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 731 | case Instruction::SExt: |
| 732 | return SelectCast(I, ISD::SIGN_EXTEND); |
| 733 | case Instruction::Trunc: |
| 734 | return SelectCast(I, ISD::TRUNCATE); |
| 735 | case Instruction::SIToFP: |
| 736 | return SelectCast(I, ISD::SINT_TO_FP); |
| 737 | |
| 738 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 739 | case Instruction::PtrToInt: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 740 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 741 | EVT DstVT = TLI.getValueType(I->getType()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 742 | if (DstVT.bitsGT(SrcVT)) |
| 743 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 744 | if (DstVT.bitsLT(SrcVT)) |
| 745 | return SelectCast(I, ISD::TRUNCATE); |
| 746 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 747 | if (Reg == 0) return false; |
| 748 | UpdateValueMap(I, Reg); |
| 749 | return true; |
| 750 | } |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 751 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 752 | case Instruction::PHI: |
| 753 | llvm_unreachable("FastISel shouldn't visit PHI nodes!"); |
| 754 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 755 | default: |
| 756 | // Unhandled instruction. Halt "fast" selection and bail. |
| 757 | return false; |
| 758 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 761 | FastISel::FastISel(MachineFunction &mf, |
| 762 | DenseMap<const Value *, unsigned> &vm, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 763 | DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 764 | DenseMap<const AllocaInst *, int> &am, |
| 765 | std::vector<std::pair<MachineInstr*, unsigned> > &pn |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 766 | #ifndef NDEBUG |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 767 | , SmallSet<const Instruction *, 8> &cil |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 768 | #endif |
| 769 | ) |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 770 | : MBB(0), |
| 771 | ValueMap(vm), |
| 772 | MBBMap(bm), |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 773 | StaticAllocaMap(am), |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 774 | PHINodesToUpdate(pn), |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 775 | #ifndef NDEBUG |
| 776 | CatchInfoLost(cil), |
| 777 | #endif |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 778 | MF(mf), |
| 779 | MRI(MF.getRegInfo()), |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 780 | MFI(*MF.getFrameInfo()), |
| 781 | MCP(*MF.getConstantPool()), |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 782 | TM(MF.getTarget()), |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 783 | TD(*TM.getTargetData()), |
| 784 | TII(*TM.getInstrInfo()), |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 785 | TLI(*TM.getTargetLowering()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 788 | FastISel::~FastISel() {} |
| 789 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 790 | unsigned FastISel::FastEmit_(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 791 | unsigned) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 795 | unsigned FastISel::FastEmit_r(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 796 | unsigned, unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 800 | unsigned FastISel::FastEmit_rr(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 801 | unsigned, unsigned /*Op0*/, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 802 | unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 806 | unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 807 | return 0; |
| 808 | } |
| 809 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 810 | unsigned FastISel::FastEmit_f(MVT, MVT, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 811 | unsigned, const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 812 | return 0; |
| 813 | } |
| 814 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 815 | unsigned FastISel::FastEmit_ri(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 816 | unsigned, unsigned /*Op0*/, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 817 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 818 | return 0; |
| 819 | } |
| 820 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 821 | unsigned FastISel::FastEmit_rf(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 822 | unsigned, unsigned /*Op0*/, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 823 | const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 824 | return 0; |
| 825 | } |
| 826 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 827 | unsigned FastISel::FastEmit_rri(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 828 | unsigned, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 829 | unsigned /*Op0*/, unsigned /*Op1*/, |
| 830 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 835 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 836 | /// If that fails, it materializes the immediate into a register and try |
| 837 | /// FastEmit_rr instead. |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 838 | unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 839 | unsigned Op0, uint64_t Imm, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 840 | MVT ImmType) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 841 | // 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] | 842 | unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 843 | if (ResultReg != 0) |
| 844 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 845 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 846 | if (MaterialReg == 0) |
| 847 | return 0; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 848 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 851 | /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries |
| 852 | /// to emit an instruction with a floating-point immediate operand using |
| 853 | /// FastEmit_rf. If that fails, it materializes the immediate into a register |
| 854 | /// and try FastEmit_rr instead. |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 855 | unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 856 | unsigned Op0, const ConstantFP *FPImm, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 857 | MVT ImmType) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 858 | // 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] | 859 | unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 860 | if (ResultReg != 0) |
| 861 | return ResultReg; |
| 862 | |
| 863 | // Materialize the constant in a register. |
| 864 | unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm); |
| 865 | if (MaterialReg == 0) { |
Dan Gohman | 96a9999 | 2008-08-27 18:01:42 +0000 | [diff] [blame] | 866 | // If the target doesn't have a way to directly enter a floating-point |
| 867 | // value into a register, use an alternate approach. |
| 868 | // TODO: The current approach only supports floating-point constants |
| 869 | // that can be constructed by conversion from integer values. This should |
| 870 | // be replaced by code that creates a load from a constant-pool entry, |
| 871 | // which will require some target-specific work. |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 872 | const APFloat &Flt = FPImm->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 873 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 874 | |
| 875 | uint64_t x[2]; |
| 876 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 877 | bool isExact; |
| 878 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 879 | APFloat::rmTowardZero, &isExact); |
| 880 | if (!isExact) |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 881 | return 0; |
| 882 | APInt IntVal(IntBitWidth, 2, x); |
| 883 | |
| 884 | unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(), |
| 885 | ISD::Constant, IntVal.getZExtValue()); |
| 886 | if (IntegerReg == 0) |
| 887 | return 0; |
| 888 | MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT, |
| 889 | ISD::SINT_TO_FP, IntegerReg); |
| 890 | if (MaterialReg == 0) |
| 891 | return 0; |
| 892 | } |
| 893 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
| 894 | } |
| 895 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 896 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 897 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 900 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 901 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 902 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 903 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 904 | |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 905 | BuildMI(MBB, DL, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 906 | return ResultReg; |
| 907 | } |
| 908 | |
| 909 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 910 | const TargetRegisterClass *RC, |
| 911 | unsigned Op0) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 912 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 913 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 914 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 915 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 916 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 917 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 918 | BuildMI(MBB, DL, II).addReg(Op0); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 919 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 920 | II.ImplicitDefs[0], RC, RC); |
| 921 | if (!InsertedCopy) |
| 922 | ResultReg = 0; |
| 923 | } |
| 924 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 925 | return ResultReg; |
| 926 | } |
| 927 | |
| 928 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 929 | const TargetRegisterClass *RC, |
| 930 | unsigned Op0, unsigned Op1) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 931 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 932 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 933 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 934 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 935 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 936 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 937 | BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 938 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 939 | II.ImplicitDefs[0], RC, RC); |
| 940 | if (!InsertedCopy) |
| 941 | ResultReg = 0; |
| 942 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 943 | return ResultReg; |
| 944 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 945 | |
| 946 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 947 | const TargetRegisterClass *RC, |
| 948 | unsigned Op0, uint64_t Imm) { |
| 949 | unsigned ResultReg = createResultReg(RC); |
| 950 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 951 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 952 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 953 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 954 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 955 | BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 956 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 957 | II.ImplicitDefs[0], RC, RC); |
| 958 | if (!InsertedCopy) |
| 959 | ResultReg = 0; |
| 960 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 961 | return ResultReg; |
| 962 | } |
| 963 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 964 | unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 965 | const TargetRegisterClass *RC, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 966 | unsigned Op0, const ConstantFP *FPImm) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 967 | unsigned ResultReg = createResultReg(RC); |
| 968 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 969 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 970 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 971 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 972 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 973 | BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 974 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 975 | II.ImplicitDefs[0], RC, RC); |
| 976 | if (!InsertedCopy) |
| 977 | ResultReg = 0; |
| 978 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 979 | return ResultReg; |
| 980 | } |
| 981 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 982 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 983 | const TargetRegisterClass *RC, |
| 984 | unsigned Op0, unsigned Op1, uint64_t Imm) { |
| 985 | unsigned ResultReg = createResultReg(RC); |
| 986 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 987 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 988 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 989 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 990 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 991 | BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 992 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 993 | II.ImplicitDefs[0], RC, RC); |
| 994 | if (!InsertedCopy) |
| 995 | ResultReg = 0; |
| 996 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 997 | return ResultReg; |
| 998 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 999 | |
| 1000 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 1001 | const TargetRegisterClass *RC, |
| 1002 | uint64_t Imm) { |
| 1003 | unsigned ResultReg = createResultReg(RC); |
| 1004 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 1005 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1006 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 1007 | BuildMI(MBB, DL, II, ResultReg).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1008 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 1009 | BuildMI(MBB, DL, II).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1010 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 1011 | II.ImplicitDefs[0], RC, RC); |
| 1012 | if (!InsertedCopy) |
| 1013 | ResultReg = 0; |
| 1014 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1015 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 1016 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1017 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1018 | unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 1019 | unsigned Op0, uint32_t Idx) { |
Owen Anderson | 40a468f | 2008-08-28 17:47:37 +0000 | [diff] [blame] | 1020 | const TargetRegisterClass* RC = MRI.getRegClass(Op0); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1021 | |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 1022 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1023 | const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1024 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1025 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 1026 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1027 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 1028 | BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1029 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 1030 | II.ImplicitDefs[0], RC, RC); |
| 1031 | if (!InsertedCopy) |
| 1032 | ResultReg = 0; |
| 1033 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1034 | return ResultReg; |
| 1035 | } |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1036 | |
| 1037 | /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op |
| 1038 | /// with all but the least significant bit set to zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1039 | unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) { |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1040 | return FastEmit_ri(VT, VT, ISD::AND, Op, 1); |
| 1041 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1042 | |
| 1043 | /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. |
| 1044 | /// Emit code to ensure constants are copied into registers when needed. |
| 1045 | /// Remember the virtual registers that need to be added to the Machine PHI |
| 1046 | /// nodes as input. We cannot just directly add them, because expansion |
| 1047 | /// might result in multiple MBB's for one BB. As such, the start of the |
| 1048 | /// BB might correspond to a different MBB than the end. |
| 1049 | bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
| 1050 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1051 | |
| 1052 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| 1053 | unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size(); |
| 1054 | |
| 1055 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 1056 | // from this block. |
| 1057 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1058 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1059 | if (!isa<PHINode>(SuccBB->begin())) continue; |
| 1060 | MachineBasicBlock *SuccMBB = MBBMap[SuccBB]; |
| 1061 | |
| 1062 | // If this terminator has multiple identical successors (common for |
| 1063 | // switches), only handle each succ once. |
| 1064 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 1065 | |
| 1066 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 1067 | |
| 1068 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1069 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1070 | // emitted yet. |
| 1071 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 1072 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 1073 | // Ignore dead phi's. |
| 1074 | if (PN->use_empty()) continue; |
| 1075 | |
| 1076 | // Only handle legal types. Two interesting things to note here. First, |
| 1077 | // by bailing out early, we may leave behind some dead instructions, |
| 1078 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| 1079 | // own moves. Second, this check is necessary becuase FastISel doesn't |
| 1080 | // use CreateRegForValue to create registers, so it always creates |
| 1081 | // exactly one register for each non-void instruction. |
| 1082 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 1083 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 1084 | // Promote MVT::i1. |
| 1085 | if (VT == MVT::i1) |
| 1086 | VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT); |
| 1087 | else { |
| 1088 | PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 1089 | return false; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1094 | |
| 1095 | unsigned Reg = getRegForValue(PHIOp); |
| 1096 | if (Reg == 0) { |
| 1097 | PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
| 1098 | return false; |
| 1099 | } |
| 1100 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | return true; |
| 1105 | } |