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