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