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