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