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 | |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "isel" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/FastISel.h" |
| 44 | #include "llvm/ADT/Statistic.h" |
| 45 | #include "llvm/Analysis/Loads.h" |
| 46 | #include "llvm/CodeGen/Analysis.h" |
| 47 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 48 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 49 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 50 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 51 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 52 | #include "llvm/IR/DataLayout.h" |
| 53 | #include "llvm/IR/Function.h" |
| 54 | #include "llvm/IR/GlobalVariable.h" |
| 55 | #include "llvm/IR/Instructions.h" |
| 56 | #include "llvm/IR/IntrinsicInst.h" |
| 57 | #include "llvm/IR/Operator.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Debug.h" |
| 59 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 60 | #include "llvm/Target/TargetInstrInfo.h" |
Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 61 | #include "llvm/Target/TargetLibraryInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 62 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 63 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 64 | using namespace llvm; |
| 65 | |
Eli Bendersky | 0a4da5f | 2013-02-28 18:05:12 +0000 | [diff] [blame] | 66 | #ifndef NDEBUG |
Chad Rosier | aa5656c | 2011-11-28 19:59:09 +0000 | [diff] [blame] | 67 | STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by " |
| 68 | "target-independent selector"); |
| 69 | STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by " |
| 70 | "target-specific selector"); |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 71 | STATISTIC(NumFastIselDead, "Number of dead insts removed on failure"); |
Eli Bendersky | 0a4da5f | 2013-02-28 18:05:12 +0000 | [diff] [blame] | 72 | #endif // NDEBUG |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 73 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 74 | /// startNewBlock - Set the current block to which generated machine |
| 75 | /// instructions will be appended, and clear the local CSE map. |
| 76 | /// |
| 77 | void FastISel::startNewBlock() { |
| 78 | LocalValueMap.clear(); |
| 79 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 80 | EmitStartPt = 0; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 81 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 82 | // Advance the emit start point past any EH_LABEL instructions. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 83 | MachineBasicBlock::iterator |
| 84 | I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end(); |
| 85 | while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) { |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 86 | EmitStartPt = I; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 87 | ++I; |
| 88 | } |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 89 | LastLocalValue = EmitStartPt; |
| 90 | } |
| 91 | |
Evan Cheng | 092e5e7 | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 92 | bool FastISel::LowerArguments() { |
| 93 | if (!FuncInfo.CanLowerReturn) |
| 94 | // Fallback to SDISel argument lowering code to deal with sret pointer |
| 95 | // parameter. |
| 96 | return false; |
| 97 | |
| 98 | if (!FastLowerArguments()) |
| 99 | return false; |
| 100 | |
| 101 | // Enter non-dead arguments into ValueMap for uses in non-entry BBs. |
| 102 | for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(), |
| 103 | E = FuncInfo.Fn->arg_end(); I != E; ++I) { |
| 104 | if (!I->use_empty()) { |
| 105 | DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I); |
| 106 | assert(VI != LocalValueMap.end() && "Missed an argument?"); |
| 107 | FuncInfo.ValueMap[I] = VI->second; |
| 108 | } |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 113 | void FastISel::flushLocalValueMap() { |
| 114 | LocalValueMap.clear(); |
| 115 | LastLocalValue = EmitStartPt; |
| 116 | recomputeInsertPt(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 119 | bool FastISel::hasTrivialKill(const Value *V) const { |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 120 | // Don't consider constants or arguments to have trivial kills. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 121 | const Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 122 | if (!I) |
| 123 | return false; |
| 124 | |
| 125 | // No-op casts are trivially coalesced by fast-isel. |
| 126 | if (const CastInst *Cast = dyn_cast<CastInst>(I)) |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 127 | if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) && |
| 128 | !hasTrivialKill(Cast->getOperand(0))) |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 129 | return false; |
| 130 | |
Chad Rosier | 22b34cc | 2011-11-15 23:34:05 +0000 | [diff] [blame] | 131 | // GEPs with all zero indices are trivially coalesced by fast-isel. |
| 132 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) |
| 133 | if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0))) |
| 134 | return false; |
| 135 | |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 136 | // Only instructions with a single use in the same basic block are considered |
| 137 | // to have trivial kills. |
| 138 | return I->hasOneUse() && |
| 139 | !(I->getOpcode() == Instruction::BitCast || |
| 140 | I->getOpcode() == Instruction::PtrToInt || |
| 141 | I->getOpcode() == Instruction::IntToPtr) && |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 142 | cast<Instruction>(*I->use_begin())->getParent() == I->getParent(); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 145 | unsigned FastISel::getRegForValue(const Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 146 | EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true); |
Dan Gohman | 4fd5528 | 2009-04-07 20:40:11 +0000 | [diff] [blame] | 147 | // Don't handle non-simple values in FastISel. |
| 148 | if (!RealVT.isSimple()) |
| 149 | return 0; |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 150 | |
| 151 | // Ignore illegal types. We must do this before looking up the value |
| 152 | // in ValueMap because Arguments are given virtual registers regardless |
| 153 | // of whether FastISel can handle them. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 154 | MVT VT = RealVT.getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 155 | if (!TLI.isTypeLegal(VT)) { |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 156 | // Handle integer promotions, though, because they're common and easy. |
| 157 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 158 | VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 159 | else |
| 160 | return 0; |
| 161 | } |
| 162 | |
Eric Christopher | 4e27027 | 2012-03-20 01:07:47 +0000 | [diff] [blame] | 163 | // Look up the value to see if we already have a register for it. |
| 164 | unsigned Reg = lookUpRegForValue(V); |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 165 | if (Reg != 0) |
| 166 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 167 | |
Dan Gohman | 97c94b8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 168 | // In bottom-up mode, just create the virtual register which will be used |
| 169 | // to hold the value. It will be materialized later. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 170 | if (isa<Instruction>(V) && |
| 171 | (!isa<AllocaInst>(V) || |
| 172 | !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) |
| 173 | return FuncInfo.InitializeRegForValue(V); |
Dan Gohman | 97c94b8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 174 | |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 175 | SavePoint SaveInsertPt = enterLocalValueArea(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 176 | |
| 177 | // Materialize the value in a register. Emit any instructions in the |
| 178 | // local value area. |
| 179 | Reg = materializeRegForValue(V, VT); |
| 180 | |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 181 | leaveLocalValueArea(SaveInsertPt); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 182 | |
| 183 | return Reg; |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Eric Christopher | 44a2c34 | 2010-08-17 01:30:33 +0000 | [diff] [blame] | 186 | /// materializeRegForValue - Helper for getRegForValue. This function is |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 187 | /// called when the value isn't already available in a register and must |
| 188 | /// be materialized with new instructions. |
| 189 | unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) { |
| 190 | unsigned Reg = 0; |
| 191 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 192 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 193 | if (CI->getValue().getActiveBits() <= 64) |
| 194 | Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 195 | } else if (isa<AllocaInst>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 196 | Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 197 | } else if (isa<ConstantPointerNull>(V)) { |
Dan Gohman | 1e9e8c3 | 2008-10-07 22:03:27 +0000 | [diff] [blame] | 198 | // Translate this as an integer zero so that it can be |
| 199 | // local-CSE'd with actual integer zeros. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 200 | Reg = |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 201 | getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext()))); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 202 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Eli Friedman | bd12538 | 2011-04-28 00:42:03 +0000 | [diff] [blame] | 203 | if (CF->isNullValue()) { |
Eli Friedman | 2790ba8 | 2011-04-27 22:41:55 +0000 | [diff] [blame] | 204 | Reg = TargetMaterializeFloatZero(CF); |
| 205 | } else { |
| 206 | // Try to emit the constant directly. |
| 207 | Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); |
| 208 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 209 | |
| 210 | if (!Reg) { |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 211 | // 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] | 212 | const APFloat &Flt = CF->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 213 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 214 | |
| 215 | uint64_t x[2]; |
| 216 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 217 | bool isExact; |
| 218 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
Eric Christopher | c415af2 | 2012-03-20 01:07:56 +0000 | [diff] [blame] | 219 | APFloat::rmTowardZero, &isExact); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 220 | if (isExact) { |
Jeffrey Yasskin | 3ba292d | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 221 | APInt IntVal(IntBitWidth, x); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 222 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 223 | unsigned IntegerReg = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 224 | getRegForValue(ConstantInt::get(V->getContext(), IntVal)); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 225 | if (IntegerReg != 0) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 226 | Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, |
| 227 | IntegerReg, /*Kill=*/false); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 228 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 229 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 230 | } else if (const Operator *Op = dyn_cast<Operator>(V)) { |
Dan Gohman | 20d4be1 | 2010-07-01 02:58:57 +0000 | [diff] [blame] | 231 | if (!SelectOperator(Op, Op->getOpcode())) |
| 232 | if (!isa<Instruction>(Op) || |
| 233 | !TargetSelectInstruction(cast<Instruction>(Op))) |
| 234 | return 0; |
Dan Gohman | 37db6cd | 2010-06-21 14:17:46 +0000 | [diff] [blame] | 235 | Reg = lookUpRegForValue(Op); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 236 | } else if (isa<UndefValue>(V)) { |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 237 | Reg = createResultReg(TLI.getRegClassFor(VT)); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 238 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 239 | TII.get(TargetOpcode::IMPLICIT_DEF), Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 240 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 241 | |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 242 | // If target-independent code couldn't handle the value, give target-specific |
| 243 | // code a try. |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 244 | if (!Reg && isa<Constant>(V)) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 245 | Reg = TargetMaterializeConstant(cast<Constant>(V)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 246 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 247 | // Don't cache constant materializations in the general ValueMap. |
| 248 | // To do so would require tracking what uses they dominate. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 249 | if (Reg != 0) { |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 250 | LocalValueMap[V] = Reg; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 251 | LastLocalValue = MRI.getVRegDef(Reg); |
| 252 | } |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 253 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 256 | unsigned FastISel::lookUpRegForValue(const Value *V) { |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 257 | // Look up the value to see if we already have a register for it. We |
| 258 | // cache values defined by Instructions across blocks, and other values |
| 259 | // only locally. This is because Instructions already have the SSA |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 260 | // def-dominates-use requirement enforced. |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 261 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V); |
| 262 | if (I != FuncInfo.ValueMap.end()) |
Dan Gohman | 3193a68 | 2010-06-21 14:21:47 +0000 | [diff] [blame] | 263 | return I->second; |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 264 | return LocalValueMap[V]; |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 267 | /// UpdateValueMap - Update the value map to include the new mapping for this |
| 268 | /// instruction, or insert an extra copy to get the result in a previous |
| 269 | /// determined register. |
| 270 | /// NOTE: This is only necessary because we might select a block that uses |
| 271 | /// a value before we select the block that defines the value. It might be |
| 272 | /// possible to fix this by selecting blocks in reverse postorder. |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 273 | void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 274 | if (!isa<Instruction>(I)) { |
| 275 | LocalValueMap[I] = Reg; |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 276 | return; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 277 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 278 | |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 279 | unsigned &AssignedReg = FuncInfo.ValueMap[I]; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 280 | if (AssignedReg == 0) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 281 | // Use the new register. |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 282 | AssignedReg = Reg; |
Chris Lattner | 36e3946 | 2009-04-12 07:46:30 +0000 | [diff] [blame] | 283 | else if (Reg != AssignedReg) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 284 | // Arrange for uses of AssignedReg to be replaced by uses of Reg. |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 285 | for (unsigned i = 0; i < NumRegs; i++) |
| 286 | FuncInfo.RegFixups[AssignedReg+i] = Reg+i; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 287 | |
| 288 | AssignedReg = Reg; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 289 | } |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 292 | std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) { |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 293 | unsigned IdxN = getRegForValue(Idx); |
| 294 | if (IdxN == 0) |
| 295 | // Unhandled operand. Halt "fast" selection and bail. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 296 | return std::pair<unsigned, bool>(0, false); |
| 297 | |
| 298 | bool IdxNIsKill = hasTrivialKill(Idx); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 299 | |
| 300 | // 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] | 301 | MVT PtrVT = TLI.getPointerTy(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 302 | EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 303 | if (IdxVT.bitsLT(PtrVT)) { |
| 304 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, |
| 305 | IdxN, IdxNIsKill); |
| 306 | IdxNIsKill = true; |
| 307 | } |
| 308 | else if (IdxVT.bitsGT(PtrVT)) { |
| 309 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, |
| 310 | IdxN, IdxNIsKill); |
| 311 | IdxNIsKill = true; |
| 312 | } |
| 313 | return std::pair<unsigned, bool>(IdxN, IdxNIsKill); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 316 | void FastISel::recomputeInsertPt() { |
| 317 | if (getLastLocalValue()) { |
| 318 | FuncInfo.InsertPt = getLastLocalValue(); |
Dan Gohman | c6e59b7 | 2010-07-19 22:48:56 +0000 | [diff] [blame] | 319 | FuncInfo.MBB = FuncInfo.InsertPt->getParent(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 320 | ++FuncInfo.InsertPt; |
| 321 | } else |
| 322 | FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI(); |
| 323 | |
| 324 | // Now skip past any EH_LABELs, which must remain at the beginning. |
| 325 | while (FuncInfo.InsertPt != FuncInfo.MBB->end() && |
| 326 | FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL) |
| 327 | ++FuncInfo.InsertPt; |
| 328 | } |
| 329 | |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 330 | void FastISel::removeDeadCode(MachineBasicBlock::iterator I, |
| 331 | MachineBasicBlock::iterator E) { |
| 332 | assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!"); |
| 333 | while (I != E) { |
| 334 | MachineInstr *Dead = &*I; |
| 335 | ++I; |
| 336 | Dead->eraseFromParent(); |
Eli Bendersky | 0a4da5f | 2013-02-28 18:05:12 +0000 | [diff] [blame] | 337 | DEBUG(++NumFastIselDead); |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 338 | } |
| 339 | recomputeInsertPt(); |
| 340 | } |
| 341 | |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 342 | FastISel::SavePoint FastISel::enterLocalValueArea() { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 343 | MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt; |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 344 | DebugLoc OldDL = DL; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 345 | recomputeInsertPt(); |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 346 | DL = DebugLoc(); |
| 347 | SavePoint SP = { OldInsertPt, OldDL }; |
| 348 | return SP; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 351 | void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 352 | if (FuncInfo.InsertPt != FuncInfo.MBB->begin()) |
| 353 | LastLocalValue = llvm::prior(FuncInfo.InsertPt); |
| 354 | |
| 355 | // Restore the previous insert position. |
Eric Christopher | 76ad43c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 356 | FuncInfo.InsertPt = OldInsertPt.InsertPt; |
| 357 | DL = OldInsertPt.DL; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 360 | /// SelectBinaryOp - Select and emit code for a binary operator instruction, |
| 361 | /// which has an opcode which directly corresponds to the given ISD opcode. |
| 362 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 363 | bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 364 | EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 365 | if (VT == MVT::Other || !VT.isSimple()) |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 366 | // Unhandled type. Halt "fast" selection and bail. |
| 367 | return false; |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 368 | |
Dan Gohman | b71fea2 | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 369 | // We only handle legal types. For example, on x86-32 the instruction |
| 370 | // selector contains all of the 64-bit instructions from x86-64, |
| 371 | // under the assumption that i64 won't be used if the target doesn't |
| 372 | // support it. |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 373 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 374 | // MVT::i1 is special. Allow AND, OR, or XOR because they |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 375 | // don't require additional zeroing, which makes them easy. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 376 | if (VT == MVT::i1 && |
Dan Gohman | 5dd9c2e | 2008-09-25 17:22:52 +0000 | [diff] [blame] | 377 | (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || |
| 378 | ISDOpcode == ISD::XOR)) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 379 | VT = TLI.getTypeToTransformTo(I->getContext(), VT); |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 380 | else |
| 381 | return false; |
| 382 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 383 | |
Chris Lattner | fff65b3 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 384 | // Check if the first operand is a constant, and handle it as "ri". At -O0, |
| 385 | // we don't have anything that canonicalizes operand order. |
| 386 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0))) |
| 387 | if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) { |
| 388 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
| 389 | if (Op1 == 0) return false; |
| 390 | |
| 391 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 393 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, |
| 394 | Op1IsKill, CI->getZExtValue(), |
| 395 | VT.getSimpleVT()); |
| 396 | if (ResultReg == 0) return false; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 398 | // We successfully emitted code for the given LLVM Instruction. |
| 399 | UpdateValueMap(I, ResultReg); |
| 400 | return true; |
Chris Lattner | fff65b3 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 401 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 402 | |
| 403 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 404 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 405 | if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail. |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 406 | return false; |
| 407 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 408 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
| 409 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 410 | // Check if the second operand is a constant and handle it appropriately. |
| 411 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 412 | uint64_t Imm = CI->getZExtValue(); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 413 | |
Chris Lattner | f051c1a | 2011-04-18 07:00:40 +0000 | [diff] [blame] | 414 | // Transform "sdiv exact X, 8" -> "sra X, 3". |
| 415 | if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) && |
| 416 | cast<BinaryOperator>(I)->isExact() && |
| 417 | isPowerOf2_64(Imm)) { |
| 418 | Imm = Log2_64(Imm); |
| 419 | ISDOpcode = ISD::SRA; |
| 420 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 421 | |
Chad Rosier | 544b9b4 | 2012-03-22 00:21:17 +0000 | [diff] [blame] | 422 | // Transform "urem x, pow2" -> "and x, pow2-1". |
| 423 | if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) && |
| 424 | isPowerOf2_64(Imm)) { |
| 425 | --Imm; |
| 426 | ISDOpcode = ISD::AND; |
| 427 | } |
| 428 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 429 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, |
| 430 | Op0IsKill, Imm, VT.getSimpleVT()); |
| 431 | if (ResultReg == 0) return false; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 433 | // We successfully emitted code for the given LLVM Instruction. |
| 434 | UpdateValueMap(I, ResultReg); |
| 435 | return true; |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 438 | // Check if the second operand is a constant float. |
| 439 | if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 440 | unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 441 | ISDOpcode, Op0, Op0IsKill, CF); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 442 | if (ResultReg != 0) { |
| 443 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 444 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 449 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 450 | if (Op1 == 0) |
| 451 | // Unhandled operand. Halt "fast" selection and bail. |
| 452 | return false; |
| 453 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 454 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
| 455 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 456 | // Now we have both operands in registers. Emit the instruction. |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 457 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 458 | ISDOpcode, |
| 459 | Op0, Op0IsKill, |
| 460 | Op1, Op1IsKill); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 461 | if (ResultReg == 0) |
| 462 | // Target-specific code wasn't able to find a machine opcode for |
| 463 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 464 | return false; |
| 465 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 466 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 467 | UpdateValueMap(I, ResultReg); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 471 | bool FastISel::SelectGetElementPtr(const User *I) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 472 | unsigned N = getRegForValue(I->getOperand(0)); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 473 | if (N == 0) |
| 474 | // Unhandled operand. Halt "fast" selection and bail. |
| 475 | return false; |
| 476 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 477 | bool NIsKill = hasTrivialKill(I->getOperand(0)); |
| 478 | |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 479 | // Keep a running tab of the total offset to coalesce multiple N = N + Offset |
| 480 | // into a single N = N + TotalOffset. |
| 481 | uint64_t TotalOffs = 0; |
| 482 | // FIXME: What's a good SWAG number for MaxOffs? |
| 483 | uint64_t MaxOffs = 2048; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 484 | Type *Ty = I->getOperand(0)->getType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 485 | MVT VT = TLI.getPointerTy(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 486 | for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1, |
| 487 | E = I->op_end(); OI != E; ++OI) { |
| 488 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 489 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 490 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 491 | if (Field) { |
| 492 | // N = N + Offset |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 493 | TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field); |
| 494 | if (TotalOffs >= MaxOffs) { |
| 495 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 496 | if (N == 0) |
| 497 | // Unhandled operand. Halt "fast" selection and bail. |
| 498 | return false; |
| 499 | NIsKill = true; |
| 500 | TotalOffs = 0; |
| 501 | } |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 502 | } |
| 503 | Ty = StTy->getElementType(Field); |
| 504 | } else { |
| 505 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 506 | |
| 507 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 508 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 509 | if (CI->isZero()) continue; |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 510 | // N = N + Offset |
Chad Rosier | 6016a4a | 2012-07-06 17:44:22 +0000 | [diff] [blame] | 511 | TotalOffs += |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 512 | TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 513 | if (TotalOffs >= MaxOffs) { |
| 514 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 515 | if (N == 0) |
| 516 | // Unhandled operand. Halt "fast" selection and bail. |
| 517 | return false; |
| 518 | NIsKill = true; |
| 519 | TotalOffs = 0; |
| 520 | } |
| 521 | continue; |
| 522 | } |
| 523 | if (TotalOffs) { |
| 524 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 525 | if (N == 0) |
| 526 | // Unhandled operand. Halt "fast" selection and bail. |
| 527 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 528 | NIsKill = true; |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 529 | TotalOffs = 0; |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 530 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 531 | |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 532 | // N = N + Idx * ElementSize; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 533 | uint64_t ElementSize = TD.getTypeAllocSize(Ty); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 534 | std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx); |
| 535 | unsigned IdxN = Pair.first; |
| 536 | bool IdxNIsKill = Pair.second; |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 537 | if (IdxN == 0) |
| 538 | // Unhandled operand. Halt "fast" selection and bail. |
| 539 | return false; |
| 540 | |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 541 | if (ElementSize != 1) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 542 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT); |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 543 | if (IdxN == 0) |
| 544 | // Unhandled operand. Halt "fast" selection and bail. |
| 545 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 546 | IdxNIsKill = true; |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 547 | } |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 548 | N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 549 | if (N == 0) |
| 550 | // Unhandled operand. Halt "fast" selection and bail. |
| 551 | return false; |
| 552 | } |
| 553 | } |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 554 | if (TotalOffs) { |
| 555 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 556 | if (N == 0) |
| 557 | // Unhandled operand. Halt "fast" selection and bail. |
| 558 | return false; |
| 559 | } |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 560 | |
| 561 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 562 | UpdateValueMap(I, N); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 563 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 566 | bool FastISel::SelectCall(const User *I) { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 567 | const CallInst *Call = cast<CallInst>(I); |
| 568 | |
| 569 | // Handle simple inline asms. |
Dan Gohman | 9e15d65 | 2011-10-12 15:56:56 +0000 | [diff] [blame] | 570 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 571 | // Don't attempt to handle constraints. |
| 572 | if (!IA->getConstraintString().empty()) |
| 573 | return false; |
| 574 | |
| 575 | unsigned ExtraInfo = 0; |
| 576 | if (IA->hasSideEffects()) |
| 577 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 578 | if (IA->isAlignStack()) |
| 579 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
| 580 | |
| 581 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 582 | TII.get(TargetOpcode::INLINEASM)) |
| 583 | .addExternalSymbol(IA->getAsmString().c_str()) |
| 584 | .addImm(ExtraInfo); |
| 585 | return true; |
| 586 | } |
| 587 | |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 588 | MachineModuleInfo &MMI = FuncInfo.MF->getMMI(); |
| 589 | ComputeUsesVAFloatArgument(*Call, &MMI); |
| 590 | |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 591 | const Function *F = Call->getCalledFunction(); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 592 | if (!F) return false; |
| 593 | |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 594 | // Handle selected intrinsic function calls. |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 595 | switch (F->getIntrinsicID()) { |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 596 | default: break; |
Chad Rosier | aefd36b | 2012-05-11 23:21:01 +0000 | [diff] [blame] | 597 | // At -O0 we don't care about the lifetime intrinsics. |
Eric Christopher | 9b5d6b8 | 2012-02-17 23:03:39 +0000 | [diff] [blame] | 598 | case Intrinsic::lifetime_start: |
| 599 | case Intrinsic::lifetime_end: |
Chad Rosier | fd065bb | 2012-07-06 17:33:39 +0000 | [diff] [blame] | 600 | // The donothing intrinsic does, well, nothing. |
| 601 | case Intrinsic::donothing: |
Eric Christopher | 9b5d6b8 | 2012-02-17 23:03:39 +0000 | [diff] [blame] | 602 | return true; |
Chad Rosier | fd065bb | 2012-07-06 17:33:39 +0000 | [diff] [blame] | 603 | |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 604 | case Intrinsic::dbg_declare: { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 605 | const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 606 | if (!DIVariable(DI->getVariable()).Verify() || |
Eric Christopher | bb54d21 | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 607 | !FuncInfo.MF->getMMI().hasDebugInfo()) { |
| 608 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 609 | return true; |
Eric Christopher | bb54d21 | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 610 | } |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 611 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 612 | const Value *Address = DI->getAddress(); |
Eric Christopher | ccaea7d | 2012-03-15 21:33:47 +0000 | [diff] [blame] | 613 | if (!Address || isa<UndefValue>(Address)) { |
Eric Christopher | bb54d21 | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 614 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
Dale Johannesen | dc91856 | 2010-02-06 02:26:02 +0000 | [diff] [blame] | 615 | return true; |
Eric Christopher | bb54d21 | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 616 | } |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 617 | |
| 618 | unsigned Reg = 0; |
| 619 | unsigned Offset = 0; |
| 620 | if (const Argument *Arg = dyn_cast<Argument>(Address)) { |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 621 | // Some arguments' frame index is recorded during argument lowering. |
| 622 | Offset = FuncInfo.getArgumentFrameIndex(Arg); |
| 623 | if (Offset) |
Eric Christopher | c415af2 | 2012-03-20 01:07:56 +0000 | [diff] [blame] | 624 | Reg = TRI.getFrameRegister(*FuncInfo.MF); |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 625 | } |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 626 | if (!Reg) |
Eric Christopher | 8c5293c | 2012-03-20 01:07:58 +0000 | [diff] [blame] | 627 | Reg = lookUpRegForValue(Address); |
| 628 | |
Bill Wendling | 84364a4 | 2012-03-30 00:02:55 +0000 | [diff] [blame] | 629 | // If we have a VLA that has a "use" in a metadata node that's then used |
| 630 | // here but it has no other uses, then we have a problem. E.g., |
| 631 | // |
| 632 | // int foo (const int *x) { |
| 633 | // char a[*x]; |
| 634 | // return 0; |
| 635 | // } |
| 636 | // |
| 637 | // If we assign 'a' a vreg and fast isel later on has to use the selection |
| 638 | // DAG isel, it will want to copy the value to the vreg. However, there are |
| 639 | // no uses, which goes counter to what selection DAG isel expects. |
| 640 | if (!Reg && !Address->use_empty() && isa<Instruction>(Address) && |
Eric Christopher | 8c5293c | 2012-03-20 01:07:58 +0000 | [diff] [blame] | 641 | (!isa<AllocaInst>(Address) || |
| 642 | !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address)))) |
| 643 | Reg = FuncInfo.InitializeRegForValue(Address); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 644 | |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 645 | if (Reg) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 646 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 647 | TII.get(TargetOpcode::DBG_VALUE)) |
| 648 | .addReg(Reg, RegState::Debug).addImm(Offset) |
| 649 | .addMetadata(DI->getVariable()); |
Eric Christopher | 4476bae | 2012-03-20 01:07:53 +0000 | [diff] [blame] | 650 | else |
| 651 | // We can't yet handle anything else here because it would require |
| 652 | // generating code, thus altering codegen because of debug info. |
| 653 | DEBUG(dbgs() << "Dropping debug info for " << DI); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 654 | return true; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 655 | } |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 656 | case Intrinsic::dbg_value: { |
Dale Johannesen | 343b42e | 2010-04-07 01:15:14 +0000 | [diff] [blame] | 657 | // This form of DBG_VALUE is target-independent. |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 658 | const DbgValueInst *DI = cast<DbgValueInst>(Call); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 659 | const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 660 | const Value *V = DI->getValue(); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 661 | if (!V) { |
| 662 | // Currently the optimizer can produce this; insert an undef to |
| 663 | // help debugging. Probably the optimizer should not do this. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 664 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 665 | .addReg(0U).addImm(DI->getOffset()) |
| 666 | .addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 667 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Devang Patel | 8594d42 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 668 | if (CI->getBitWidth() > 64) |
| 669 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 670 | .addCImm(CI).addImm(DI->getOffset()) |
| 671 | .addMetadata(DI->getVariable()); |
Chad Rosier | 6016a4a | 2012-07-06 17:44:22 +0000 | [diff] [blame] | 672 | else |
Devang Patel | 8594d42 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 673 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 674 | .addImm(CI->getZExtValue()).addImm(DI->getOffset()) |
| 675 | .addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 676 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 677 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 678 | .addFPImm(CF).addImm(DI->getOffset()) |
| 679 | .addMetadata(DI->getVariable()); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 680 | } else if (unsigned Reg = lookUpRegForValue(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 681 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 682 | .addReg(Reg, RegState::Debug).addImm(DI->getOffset()) |
| 683 | .addMetadata(DI->getVariable()); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 684 | } else { |
| 685 | // We can't yet handle anything else here because it would require |
| 686 | // generating code, thus altering codegen because of debug info. |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 687 | DEBUG(dbgs() << "Dropping debug info for " << DI); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 688 | } |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 689 | return true; |
| 690 | } |
Eli Friedman | d0118a2 | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 691 | case Intrinsic::objectsize: { |
| 692 | ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1)); |
| 693 | unsigned long long Res = CI->isZero() ? -1ULL : 0; |
| 694 | Constant *ResCI = ConstantInt::get(Call->getType(), Res); |
| 695 | unsigned ResultReg = getRegForValue(ResCI); |
| 696 | if (ResultReg == 0) |
| 697 | return false; |
| 698 | UpdateValueMap(Call, ResultReg); |
| 699 | return true; |
| 700 | } |
Chad Rosier | 33947b4 | 2013-03-07 20:42:17 +0000 | [diff] [blame^] | 701 | case Intrinsic::expect: { |
| 702 | // unsigned ResultReg = getRegForValue(Call->getArgOperand(0)); |
| 703 | // UpdateValueMap(Call, ResultReg); |
| 704 | // return true; |
| 705 | } |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 706 | } |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 707 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 708 | // Usually, it does not make sense to initialize a value, |
| 709 | // make an unrelated function call and use the value, because |
| 710 | // it tends to be spilled on the stack. So, we move the pointer |
| 711 | // to the last local value to the beginning of the block, so that |
| 712 | // all the values which have already been materialized, |
| 713 | // appear after the call. It also makes sense to skip intrinsics |
| 714 | // since they tend to be inlined. |
Pete Cooper | b704ffb | 2013-02-22 01:50:38 +0000 | [diff] [blame] | 715 | if (!isa<IntrinsicInst>(Call)) |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 716 | flushLocalValueMap(); |
| 717 | |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 718 | // An arbitrary call. Bail. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 722 | bool FastISel::SelectCast(const User *I, unsigned Opcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 723 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 724 | EVT DstVT = TLI.getValueType(I->getType()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 725 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 726 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 727 | DstVT == MVT::Other || !DstVT.isSimple()) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 728 | // Unhandled type. Halt "fast" selection and bail. |
| 729 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 730 | |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 731 | // Check if the destination type is legal. |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 732 | if (!TLI.isTypeLegal(DstVT)) |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 733 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 734 | |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 735 | // Check if the source operand is legal. |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 736 | if (!TLI.isTypeLegal(SrcVT)) |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 737 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 738 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 739 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 740 | if (!InputReg) |
| 741 | // Unhandled operand. Halt "fast" selection and bail. |
| 742 | return false; |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 743 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 744 | bool InputRegIsKill = hasTrivialKill(I->getOperand(0)); |
| 745 | |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 746 | unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), |
| 747 | DstVT.getSimpleVT(), |
| 748 | Opcode, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 749 | InputReg, InputRegIsKill); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 750 | if (!ResultReg) |
| 751 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 752 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 753 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 754 | return true; |
| 755 | } |
| 756 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 757 | bool FastISel::SelectBitCast(const User *I) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 758 | // If the bitcast doesn't change the type, just use the operand value. |
| 759 | if (I->getType() == I->getOperand(0)->getType()) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 760 | unsigned Reg = getRegForValue(I->getOperand(0)); |
Dan Gohman | a318dab | 2008-08-27 20:41:38 +0000 | [diff] [blame] | 761 | if (Reg == 0) |
| 762 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 763 | UpdateValueMap(I, Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 764 | return true; |
| 765 | } |
| 766 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 767 | // Bitcasts of other values become reg-reg copies or BITCAST operators. |
Patrik Hagglund | 3d170e6 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 768 | EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 769 | EVT DstEVT = TLI.getValueType(I->getType()); |
| 770 | if (SrcEVT == MVT::Other || DstEVT == MVT::Other || |
| 771 | !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT)) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 772 | // Unhandled type. Halt "fast" selection and bail. |
| 773 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 774 | |
Patrik Hagglund | 3d170e6 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 775 | MVT SrcVT = SrcEVT.getSimpleVT(); |
| 776 | MVT DstVT = DstEVT.getSimpleVT(); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 777 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 778 | if (Op0 == 0) |
| 779 | // Unhandled operand. Halt "fast" selection and bail. |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 780 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 781 | |
| 782 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 783 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 784 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 785 | unsigned ResultReg = 0; |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 786 | if (SrcVT == DstVT) { |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 787 | const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 788 | const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
Jakob Stoklund Olesen | e7917bb | 2010-07-11 05:16:54 +0000 | [diff] [blame] | 789 | // Don't attempt a cross-class copy. It will likely fail. |
| 790 | if (SrcClass == DstClass) { |
| 791 | ResultReg = createResultReg(DstClass); |
| 792 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 793 | ResultReg).addReg(Op0); |
| 794 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 795 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 796 | |
| 797 | // If the reg-reg copy failed, select a BITCAST opcode. |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 798 | if (!ResultReg) |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 799 | ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 800 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 801 | if (!ResultReg) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 802 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 803 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 804 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 805 | return true; |
| 806 | } |
| 807 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 808 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 809 | FastISel::SelectInstruction(const Instruction *I) { |
Dan Gohman | e8c92dd | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 810 | // Just before the terminator instruction, insert instructions to |
| 811 | // feed PHI nodes in successor blocks. |
| 812 | if (isa<TerminatorInst>(I)) |
| 813 | if (!HandlePHINodesInSuccessorBlocks(I->getParent())) |
| 814 | return false; |
| 815 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 816 | DL = I->getDebugLoc(); |
| 817 | |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 818 | MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt; |
| 819 | |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 820 | // As a special case, don't handle calls to builtin library functions that |
| 821 | // may be translated directly to target instructions. |
Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 822 | if (const CallInst *Call = dyn_cast<CallInst>(I)) { |
| 823 | const Function *F = Call->getCalledFunction(); |
| 824 | LibFunc::Func Func; |
| 825 | if (F && !F->hasLocalLinkage() && F->hasName() && |
| 826 | LibInfo->getLibFunc(F->getName(), Func) && |
Bob Wilson | 982dc84 | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 827 | LibInfo->hasOptimizedCodeGen(Func)) |
Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 831 | // First, try doing target-independent selection. |
Michael Ilseman | 7dbd34b | 2013-02-27 19:54:00 +0000 | [diff] [blame] | 832 | if (SelectOperator(I, I->getOpcode())) { |
Eli Bendersky | 0a4da5f | 2013-02-28 18:05:12 +0000 | [diff] [blame] | 833 | DEBUG(++NumFastIselSuccessIndependent); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 834 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 835 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 836 | } |
Chad Rosier | 6016a4a | 2012-07-06 17:44:22 +0000 | [diff] [blame] | 837 | // Remove dead code. However, ignore call instructions since we've flushed |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 838 | // the local value map and recomputed the insert point. |
| 839 | if (!isa<CallInst>(I)) { |
| 840 | recomputeInsertPt(); |
| 841 | if (SavedInsertPt != FuncInfo.InsertPt) |
| 842 | removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); |
| 843 | } |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 844 | |
| 845 | // Next, try calling the target to attempt to handle the instruction. |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 846 | SavedInsertPt = FuncInfo.InsertPt; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 847 | if (TargetSelectInstruction(I)) { |
Eli Bendersky | 0a4da5f | 2013-02-28 18:05:12 +0000 | [diff] [blame] | 848 | DEBUG(++NumFastIselSuccessTarget); |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 849 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 850 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 851 | } |
Chad Rosier | ae6f2cb | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 852 | // Check for dead code and remove as necessary. |
| 853 | recomputeInsertPt(); |
| 854 | if (SavedInsertPt != FuncInfo.InsertPt) |
| 855 | removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 856 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 857 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 858 | return false; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 861 | /// FastEmitBranch - Emit an unconditional branch to the given block, |
| 862 | /// unless it is the immediate (fall-through) successor, and update |
| 863 | /// the CFG. |
| 864 | void |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 865 | FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) { |
Eric Christopher | 18112d8 | 2012-04-10 18:18:10 +0000 | [diff] [blame] | 866 | |
Evan Cheng | 092e5e7 | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 867 | if (FuncInfo.MBB->getBasicBlock()->size() > 1 && |
| 868 | FuncInfo.MBB->isLayoutSuccessor(MSucc)) { |
Eric Christopher | 18112d8 | 2012-04-10 18:18:10 +0000 | [diff] [blame] | 869 | // For more accurate line information if this is the only instruction |
| 870 | // in the block then emit it, otherwise we have the unconditional |
| 871 | // fall-through case, which needs no instructions. |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 872 | } else { |
| 873 | // The unconditional branch case. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 874 | TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL, |
| 875 | SmallVector<MachineOperand, 0>(), DL); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 876 | } |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 877 | FuncInfo.MBB->addSuccessor(MSucc); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 880 | /// SelectFNeg - Emit an FNeg operation. |
| 881 | /// |
| 882 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 883 | FastISel::SelectFNeg(const User *I) { |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 884 | unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); |
| 885 | if (OpReg == 0) return false; |
| 886 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 887 | bool OpRegIsKill = hasTrivialKill(I); |
| 888 | |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 889 | // If the target has ISD::FNEG, use it. |
| 890 | EVT VT = TLI.getValueType(I->getType()); |
| 891 | unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 892 | ISD::FNEG, OpReg, OpRegIsKill); |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 893 | if (ResultReg != 0) { |
| 894 | UpdateValueMap(I, ResultReg); |
| 895 | return true; |
| 896 | } |
| 897 | |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 898 | // Bitcast the value to integer, twiddle the sign bit with xor, |
| 899 | // and then bitcast it back to floating-point. |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 900 | if (VT.getSizeInBits() > 64) return false; |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 901 | EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); |
| 902 | if (!TLI.isTypeLegal(IntVT)) |
| 903 | return false; |
| 904 | |
| 905 | unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 906 | ISD::BITCAST, OpReg, OpRegIsKill); |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 907 | if (IntReg == 0) |
| 908 | return false; |
| 909 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 910 | unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, |
| 911 | IntReg, /*Kill=*/true, |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 912 | UINT64_C(1) << (VT.getSizeInBits()-1), |
| 913 | IntVT.getSimpleVT()); |
| 914 | if (IntResultReg == 0) |
| 915 | return false; |
| 916 | |
| 917 | ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 918 | ISD::BITCAST, IntResultReg, /*Kill=*/true); |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 919 | if (ResultReg == 0) |
| 920 | return false; |
| 921 | |
| 922 | UpdateValueMap(I, ResultReg); |
| 923 | return true; |
| 924 | } |
| 925 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 926 | bool |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 927 | FastISel::SelectExtractValue(const User *U) { |
| 928 | const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); |
Eli Friedman | a4c920d | 2011-05-16 20:34:53 +0000 | [diff] [blame] | 929 | if (!EVI) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 930 | return false; |
| 931 | |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 932 | // Make sure we only try to handle extracts with a legal result. But also |
| 933 | // allow i1 because it's easy. |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 934 | EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true); |
| 935 | if (!RealVT.isSimple()) |
| 936 | return false; |
| 937 | MVT VT = RealVT.getSimpleVT(); |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 938 | if (!TLI.isTypeLegal(VT) && VT != MVT::i1) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 939 | return false; |
| 940 | |
| 941 | const Value *Op0 = EVI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 942 | Type *AggTy = Op0->getType(); |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 943 | |
| 944 | // Get the base result register. |
| 945 | unsigned ResultReg; |
| 946 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0); |
| 947 | if (I != FuncInfo.ValueMap.end()) |
| 948 | ResultReg = I->second; |
Eli Friedman | 0b4d96b | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 949 | else if (isa<Instruction>(Op0)) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 950 | ResultReg = FuncInfo.InitializeRegForValue(Op0); |
Eli Friedman | 0b4d96b | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 951 | else |
| 952 | return false; // fast-isel can't handle aggregate constants at the moment |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 953 | |
| 954 | // Get the actual result register, which is an offset from the base register. |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 955 | unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices()); |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 956 | |
| 957 | SmallVector<EVT, 4> AggValueVTs; |
| 958 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 959 | |
| 960 | for (unsigned i = 0; i < VTIndex; i++) |
| 961 | ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]); |
| 962 | |
| 963 | UpdateValueMap(EVI, ResultReg); |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 968 | FastISel::SelectOperator(const User *I, unsigned Opcode) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 969 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 970 | case Instruction::Add: |
| 971 | return SelectBinaryOp(I, ISD::ADD); |
| 972 | case Instruction::FAdd: |
| 973 | return SelectBinaryOp(I, ISD::FADD); |
| 974 | case Instruction::Sub: |
| 975 | return SelectBinaryOp(I, ISD::SUB); |
| 976 | case Instruction::FSub: |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 977 | // FNeg is currently represented in LLVM IR as a special case of FSub. |
| 978 | if (BinaryOperator::isFNeg(I)) |
| 979 | return SelectFNeg(I); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 980 | return SelectBinaryOp(I, ISD::FSUB); |
| 981 | case Instruction::Mul: |
| 982 | return SelectBinaryOp(I, ISD::MUL); |
| 983 | case Instruction::FMul: |
| 984 | return SelectBinaryOp(I, ISD::FMUL); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 985 | case Instruction::SDiv: |
| 986 | return SelectBinaryOp(I, ISD::SDIV); |
| 987 | case Instruction::UDiv: |
| 988 | return SelectBinaryOp(I, ISD::UDIV); |
| 989 | case Instruction::FDiv: |
| 990 | return SelectBinaryOp(I, ISD::FDIV); |
| 991 | case Instruction::SRem: |
| 992 | return SelectBinaryOp(I, ISD::SREM); |
| 993 | case Instruction::URem: |
| 994 | return SelectBinaryOp(I, ISD::UREM); |
| 995 | case Instruction::FRem: |
| 996 | return SelectBinaryOp(I, ISD::FREM); |
| 997 | case Instruction::Shl: |
| 998 | return SelectBinaryOp(I, ISD::SHL); |
| 999 | case Instruction::LShr: |
| 1000 | return SelectBinaryOp(I, ISD::SRL); |
| 1001 | case Instruction::AShr: |
| 1002 | return SelectBinaryOp(I, ISD::SRA); |
| 1003 | case Instruction::And: |
| 1004 | return SelectBinaryOp(I, ISD::AND); |
| 1005 | case Instruction::Or: |
| 1006 | return SelectBinaryOp(I, ISD::OR); |
| 1007 | case Instruction::Xor: |
| 1008 | return SelectBinaryOp(I, ISD::XOR); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1009 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1010 | case Instruction::GetElementPtr: |
| 1011 | return SelectGetElementPtr(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 1012 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1013 | case Instruction::Br: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1014 | const BranchInst *BI = cast<BranchInst>(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 1015 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1016 | if (BI->isUnconditional()) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1017 | const BasicBlock *LLVMSucc = BI->getSuccessor(0); |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1018 | MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc]; |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 1019 | FastEmitBranch(MSucc, BI->getDebugLoc()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1020 | return true; |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 1021 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Conditional branches are not handed yet. |
| 1024 | // Halt "fast" selection and bail. |
| 1025 | return false; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Dan Gohman | 087c850 | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 1028 | case Instruction::Unreachable: |
| 1029 | // Nothing to emit. |
| 1030 | return true; |
| 1031 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1032 | case Instruction::Alloca: |
| 1033 | // FunctionLowering has the static-sized case covered. |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1034 | if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I))) |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1035 | return true; |
| 1036 | |
| 1037 | // Dynamic-sized alloca is not handled yet. |
| 1038 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1039 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1040 | case Instruction::Call: |
| 1041 | return SelectCall(I); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1042 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1043 | case Instruction::BitCast: |
| 1044 | return SelectBitCast(I); |
| 1045 | |
| 1046 | case Instruction::FPToSI: |
| 1047 | return SelectCast(I, ISD::FP_TO_SINT); |
| 1048 | case Instruction::ZExt: |
| 1049 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 1050 | case Instruction::SExt: |
| 1051 | return SelectCast(I, ISD::SIGN_EXTEND); |
| 1052 | case Instruction::Trunc: |
| 1053 | return SelectCast(I, ISD::TRUNCATE); |
| 1054 | case Instruction::SIToFP: |
| 1055 | return SelectCast(I, ISD::SINT_TO_FP); |
| 1056 | |
| 1057 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 1058 | case Instruction::PtrToInt: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1059 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 1060 | EVT DstVT = TLI.getValueType(I->getType()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1061 | if (DstVT.bitsGT(SrcVT)) |
| 1062 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 1063 | if (DstVT.bitsLT(SrcVT)) |
| 1064 | return SelectCast(I, ISD::TRUNCATE); |
| 1065 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 1066 | if (Reg == 0) return false; |
| 1067 | UpdateValueMap(I, Reg); |
| 1068 | return true; |
| 1069 | } |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 1070 | |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1071 | case Instruction::ExtractValue: |
| 1072 | return SelectExtractValue(I); |
| 1073 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 1074 | case Instruction::PHI: |
| 1075 | llvm_unreachable("FastISel shouldn't visit PHI nodes!"); |
| 1076 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1077 | default: |
| 1078 | // Unhandled instruction. Halt "fast" selection and bail. |
| 1079 | return false; |
| 1080 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1083 | FastISel::FastISel(FunctionLoweringInfo &funcInfo, |
| 1084 | const TargetLibraryInfo *libInfo) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1085 | : FuncInfo(funcInfo), |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1086 | MRI(FuncInfo.MF->getRegInfo()), |
| 1087 | MFI(*FuncInfo.MF->getFrameInfo()), |
| 1088 | MCP(*FuncInfo.MF->getConstantPool()), |
| 1089 | TM(FuncInfo.MF->getTarget()), |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1090 | TD(*TM.getDataLayout()), |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 1091 | TII(*TM.getInstrInfo()), |
Dan Gohman | a7a0ed7 | 2010-05-05 23:58:35 +0000 | [diff] [blame] | 1092 | TLI(*TM.getTargetLowering()), |
Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1093 | TRI(*TM.getRegisterInfo()), |
| 1094 | LibInfo(libInfo) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 1097 | FastISel::~FastISel() {} |
| 1098 | |
Evan Cheng | 092e5e7 | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 1099 | bool FastISel::FastLowerArguments() { |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1103 | unsigned FastISel::FastEmit_(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1104 | unsigned) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1108 | unsigned FastISel::FastEmit_r(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1109 | unsigned, |
| 1110 | unsigned /*Op0*/, bool /*Op0IsKill*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1114 | unsigned FastISel::FastEmit_rr(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1115 | unsigned, |
| 1116 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
| 1117 | unsigned /*Op1*/, bool /*Op1IsKill*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1121 | unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1125 | unsigned FastISel::FastEmit_f(MVT, MVT, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1126 | unsigned, const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1130 | unsigned FastISel::FastEmit_ri(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1131 | unsigned, |
| 1132 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 1133 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1137 | unsigned FastISel::FastEmit_rf(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1138 | unsigned, |
| 1139 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1140 | const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1144 | unsigned FastISel::FastEmit_rri(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1145 | unsigned, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1146 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
| 1147 | unsigned /*Op1*/, bool /*Op1IsKill*/, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1148 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 1153 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 1154 | /// If that fails, it materializes the immediate into a register and try |
| 1155 | /// FastEmit_rr instead. |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1156 | unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1157 | unsigned Op0, bool Op0IsKill, |
| 1158 | uint64_t Imm, MVT ImmType) { |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1159 | // If this is a multiply by a power of two, emit this as a shift left. |
| 1160 | if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) { |
| 1161 | Opcode = ISD::SHL; |
| 1162 | Imm = Log2_64(Imm); |
Chris Lattner | 090ca91 | 2011-04-18 06:55:51 +0000 | [diff] [blame] | 1163 | } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) { |
| 1164 | // div x, 8 -> srl x, 3 |
| 1165 | Opcode = ISD::SRL; |
| 1166 | Imm = Log2_64(Imm); |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1167 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1169 | // Horrible hack (to be removed), check to make sure shift amounts are |
| 1170 | // in-range. |
| 1171 | if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) && |
| 1172 | Imm >= VT.getSizeInBits()) |
| 1173 | return 0; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1174 | |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1175 | // 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] | 1176 | unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1177 | if (ResultReg != 0) |
| 1178 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 1179 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Eli Friedman | b2b03fc | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1180 | if (MaterialReg == 0) { |
| 1181 | // This is a bit ugly/slow, but failing here means falling out of |
| 1182 | // fast-isel, which would be very slow. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1183 | IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(), |
Eli Friedman | b2b03fc | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1184 | VT.getSizeInBits()); |
| 1185 | MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm)); |
| 1186 | } |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1187 | return FastEmit_rr(VT, VT, Opcode, |
| 1188 | Op0, Op0IsKill, |
| 1189 | MaterialReg, /*Kill=*/true); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 1193 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1196 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 1197 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1198 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1199 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1200 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1201 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1202 | return ResultReg; |
| 1203 | } |
| 1204 | |
| 1205 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 1206 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1207 | unsigned Op0, bool Op0IsKill) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1208 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1209 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1210 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1211 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1212 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1213 | .addReg(Op0, Op0IsKill * RegState::Kill); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1214 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1215 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1216 | .addReg(Op0, Op0IsKill * RegState::Kill); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1217 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1218 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1221 | return ResultReg; |
| 1222 | } |
| 1223 | |
| 1224 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 1225 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1226 | unsigned Op0, bool Op0IsKill, |
| 1227 | unsigned Op1, bool Op1IsKill) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1228 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1229 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1230 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1231 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1232 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1233 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1234 | .addReg(Op1, Op1IsKill * RegState::Kill); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1235 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1236 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1237 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1238 | .addReg(Op1, Op1IsKill * RegState::Kill); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1239 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1240 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1241 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1242 | return ResultReg; |
| 1243 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1244 | |
Owen Anderson | d71867a | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1245 | unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 1246 | const TargetRegisterClass *RC, |
| 1247 | unsigned Op0, bool Op0IsKill, |
| 1248 | unsigned Op1, bool Op1IsKill, |
| 1249 | unsigned Op2, bool Op2IsKill) { |
| 1250 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1251 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | d71867a | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (II.getNumDefs() >= 1) |
| 1254 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1255 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1256 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1257 | .addReg(Op2, Op2IsKill * RegState::Kill); |
| 1258 | else { |
| 1259 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1260 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1261 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1262 | .addReg(Op2, Op2IsKill * RegState::Kill); |
| 1263 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1264 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1265 | } |
| 1266 | return ResultReg; |
| 1267 | } |
| 1268 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1269 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 1270 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1271 | unsigned Op0, bool Op0IsKill, |
| 1272 | uint64_t Imm) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1273 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1274 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1275 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1276 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1277 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1278 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1279 | .addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1280 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1281 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1282 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1283 | .addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1284 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1285 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1286 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1287 | return ResultReg; |
| 1288 | } |
| 1289 | |
Owen Anderson | 2ce5bf1 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1290 | unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode, |
| 1291 | const TargetRegisterClass *RC, |
| 1292 | unsigned Op0, bool Op0IsKill, |
| 1293 | uint64_t Imm1, uint64_t Imm2) { |
| 1294 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1295 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | 2ce5bf1 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (II.getNumDefs() >= 1) |
| 1298 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1299 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1300 | .addImm(Imm1) |
| 1301 | .addImm(Imm2); |
| 1302 | else { |
| 1303 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1304 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1305 | .addImm(Imm1) |
| 1306 | .addImm(Imm2); |
| 1307 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1308 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1309 | } |
| 1310 | return ResultReg; |
| 1311 | } |
| 1312 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1313 | unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 1314 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1315 | unsigned Op0, bool Op0IsKill, |
| 1316 | const ConstantFP *FPImm) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1317 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1318 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1319 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1320 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1321 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1322 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1323 | .addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1324 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1325 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1326 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1327 | .addFPImm(FPImm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1328 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1329 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1330 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1331 | return ResultReg; |
| 1332 | } |
| 1333 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1334 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 1335 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1336 | unsigned Op0, bool Op0IsKill, |
| 1337 | unsigned Op1, bool Op1IsKill, |
| 1338 | uint64_t Imm) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1339 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1340 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1341 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1342 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1343 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1344 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1345 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1346 | .addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1347 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1348 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1349 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1350 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1351 | .addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1352 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1353 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1354 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1355 | return ResultReg; |
| 1356 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1357 | |
Manman Ren | 68f2557 | 2012-06-01 19:33:18 +0000 | [diff] [blame] | 1358 | unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode, |
| 1359 | const TargetRegisterClass *RC, |
| 1360 | unsigned Op0, bool Op0IsKill, |
| 1361 | unsigned Op1, bool Op1IsKill, |
| 1362 | uint64_t Imm1, uint64_t Imm2) { |
| 1363 | unsigned ResultReg = createResultReg(RC); |
| 1364 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| 1365 | |
| 1366 | if (II.getNumDefs() >= 1) |
| 1367 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1368 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1369 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1370 | .addImm(Imm1).addImm(Imm2); |
| 1371 | else { |
| 1372 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1373 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1374 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1375 | .addImm(Imm1).addImm(Imm2); |
| 1376 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1377 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1378 | } |
| 1379 | return ResultReg; |
| 1380 | } |
| 1381 | |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1382 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 1383 | const TargetRegisterClass *RC, |
| 1384 | uint64_t Imm) { |
| 1385 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1386 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1387 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1388 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1389 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1390 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1391 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1392 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1393 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1394 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1395 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 1396 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1397 | |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1398 | unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode, |
| 1399 | const TargetRegisterClass *RC, |
| 1400 | uint64_t Imm1, uint64_t Imm2) { |
| 1401 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1402 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1403 | |
| 1404 | if (II.getNumDefs() >= 1) |
| 1405 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1406 | .addImm(Imm1).addImm(Imm2); |
| 1407 | else { |
| 1408 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2); |
| 1409 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1410 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1411 | } |
| 1412 | return ResultReg; |
| 1413 | } |
| 1414 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1415 | unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1416 | unsigned Op0, bool Op0IsKill, |
| 1417 | uint32_t Idx) { |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 1418 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1419 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 1420 | "Cannot yet extract from physregs"); |
Jakob Stoklund Olesen | ee0d5d4 | 2012-05-20 06:38:37 +0000 | [diff] [blame] | 1421 | const TargetRegisterClass *RC = MRI.getRegClass(Op0); |
| 1422 | MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx)); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1423 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
| 1424 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1425 | .addReg(Op0, getKillRegState(Op0IsKill), Idx); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1426 | return ResultReg; |
| 1427 | } |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1428 | |
| 1429 | /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op |
| 1430 | /// with all but the least significant bit set to zero. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1431 | unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) { |
| 1432 | return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1433 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1434 | |
| 1435 | /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. |
| 1436 | /// Emit code to ensure constants are copied into registers when needed. |
| 1437 | /// Remember the virtual registers that need to be added to the Machine PHI |
| 1438 | /// nodes as input. We cannot just directly add them, because expansion |
| 1439 | /// might result in multiple MBB's for one BB. As such, the start of the |
| 1440 | /// BB might correspond to a different MBB than the end. |
| 1441 | bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
| 1442 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1443 | |
| 1444 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1445 | unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 1448 | // from this block. |
| 1449 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1450 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1451 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1452 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1453 | |
| 1454 | // If this terminator has multiple identical successors (common for |
| 1455 | // switches), only handle each succ once. |
| 1456 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 1457 | |
| 1458 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 1459 | |
| 1460 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1461 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1462 | // emitted yet. |
| 1463 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 1464 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1465 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1466 | // Ignore dead phi's. |
| 1467 | if (PN->use_empty()) continue; |
| 1468 | |
| 1469 | // Only handle legal types. Two interesting things to note here. First, |
| 1470 | // by bailing out early, we may leave behind some dead instructions, |
| 1471 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1472 | // own moves. Second, this check is necessary because FastISel doesn't |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 1473 | // use CreateRegs to create registers, so it always creates |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1474 | // exactly one register for each non-void instruction. |
| 1475 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 1476 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
Chad Rosier | 2f2d1d7 | 2012-02-04 00:39:19 +0000 | [diff] [blame] | 1477 | // Handle integer promotions, though, because they're common and easy. |
| 1478 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1479 | VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT); |
| 1480 | else { |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1481 | FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1487 | |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1488 | // Set the DebugLoc for the copy. Prefer the location of the operand |
| 1489 | // if there is one; use the location of the PHI otherwise. |
| 1490 | DL = PN->getDebugLoc(); |
| 1491 | if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp)) |
| 1492 | DL = Inst->getDebugLoc(); |
| 1493 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1494 | unsigned Reg = getRegForValue(PHIOp); |
| 1495 | if (Reg == 0) { |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1496 | FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1497 | return false; |
| 1498 | } |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1499 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1500 | DL = DebugLoc(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | return true; |
| 1505 | } |