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