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