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