David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 1 | //===----------- VectorUtils.cpp - Vectorizer utility functions -----------===// |
| 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 defines vectorizer utilities. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/EquivalenceClasses.h" |
| 15 | #include "llvm/Analysis/DemandedBits.h" |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
| 17 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 18 | #include "llvm/Analysis/ScalarEvolution.h" |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ValueTracking.h" |
David Blaikie | b447ac6 | 2015-06-26 18:02:52 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/VectorUtils.h" |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 23 | #include "llvm/IR/PatternMatch.h" |
| 24 | #include "llvm/IR/Value.h" |
Renato Golin | 3b1d3b0 | 2015-08-30 10:49:04 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
| 26 | |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | using namespace llvm::PatternMatch; |
David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 29 | |
| 30 | /// \brief Identify if the intrinsic is trivially vectorizable. |
| 31 | /// This method returns true if the intrinsic's argument types are all |
| 32 | /// scalars for the scalar form of the intrinsic and all vectors for |
| 33 | /// the vector form of the intrinsic. |
| 34 | bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) { |
| 35 | switch (ID) { |
| 36 | case Intrinsic::sqrt: |
| 37 | case Intrinsic::sin: |
| 38 | case Intrinsic::cos: |
| 39 | case Intrinsic::exp: |
| 40 | case Intrinsic::exp2: |
| 41 | case Intrinsic::log: |
| 42 | case Intrinsic::log10: |
| 43 | case Intrinsic::log2: |
| 44 | case Intrinsic::fabs: |
| 45 | case Intrinsic::minnum: |
| 46 | case Intrinsic::maxnum: |
| 47 | case Intrinsic::copysign: |
| 48 | case Intrinsic::floor: |
| 49 | case Intrinsic::ceil: |
| 50 | case Intrinsic::trunc: |
| 51 | case Intrinsic::rint: |
| 52 | case Intrinsic::nearbyint: |
| 53 | case Intrinsic::round: |
| 54 | case Intrinsic::bswap: |
Simon Pilgrim | ba319de | 2016-06-04 20:21:07 +0000 | [diff] [blame] | 55 | case Intrinsic::bitreverse: |
David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 56 | case Intrinsic::ctpop: |
| 57 | case Intrinsic::pow: |
| 58 | case Intrinsic::fma: |
| 59 | case Intrinsic::fmuladd: |
| 60 | case Intrinsic::ctlz: |
| 61 | case Intrinsic::cttz: |
| 62 | case Intrinsic::powi: |
| 63 | return true; |
| 64 | default: |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// \brief Identifies if the intrinsic has a scalar operand. It check for |
| 70 | /// ctlz,cttz and powi special intrinsics whose argument is scalar. |
| 71 | bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID, |
| 72 | unsigned ScalarOpdIdx) { |
| 73 | switch (ID) { |
| 74 | case Intrinsic::ctlz: |
| 75 | case Intrinsic::cttz: |
| 76 | case Intrinsic::powi: |
| 77 | return (ScalarOpdIdx == 1); |
| 78 | default: |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 83 | /// \brief Returns intrinsic ID for call. |
| 84 | /// For the input call instruction it finds mapping intrinsic and returns |
| 85 | /// its ID, in case it does not found it return not_intrinsic. |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 86 | Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI, |
| 87 | const TargetLibraryInfo *TLI) { |
| 88 | Intrinsic::ID ID = getIntrinsicForCallSite(CI, TLI); |
| 89 | if (ID == Intrinsic::not_intrinsic) |
David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 90 | return Intrinsic::not_intrinsic; |
| 91 | |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 92 | if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start || |
| 93 | ID == Intrinsic::lifetime_end || ID == Intrinsic::assume) |
| 94 | return ID; |
David Blaikie | 1213dbf | 2015-06-26 16:57:30 +0000 | [diff] [blame] | 95 | return Intrinsic::not_intrinsic; |
| 96 | } |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 97 | |
| 98 | /// \brief Find the operand of the GEP that should be checked for consecutive |
| 99 | /// stores. This ignores trailing indices that have no effect on the final |
| 100 | /// pointer. |
| 101 | unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) { |
| 102 | const DataLayout &DL = Gep->getModule()->getDataLayout(); |
| 103 | unsigned LastOperand = Gep->getNumOperands() - 1; |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 104 | unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType()); |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 105 | |
| 106 | // Walk backwards and try to peel off zeros. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 107 | while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) { |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 108 | // Find the type we're currently indexing into. |
| 109 | gep_type_iterator GEPTI = gep_type_begin(Gep); |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame^] | 110 | std::advance(GEPTI, LastOperand - 2); |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 111 | |
| 112 | // If it's a type with the same allocation size as the result of the GEP we |
| 113 | // can peel off the zero index. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame^] | 114 | if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize) |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 115 | break; |
| 116 | --LastOperand; |
| 117 | } |
| 118 | |
| 119 | return LastOperand; |
| 120 | } |
| 121 | |
| 122 | /// \brief If the argument is a GEP, then returns the operand identified by |
| 123 | /// getGEPInductionOperand. However, if there is some other non-loop-invariant |
| 124 | /// operand, it returns that instead. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 125 | Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) { |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 126 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr); |
| 127 | if (!GEP) |
| 128 | return Ptr; |
| 129 | |
| 130 | unsigned InductionOperand = getGEPInductionOperand(GEP); |
| 131 | |
| 132 | // Check that all of the gep indices are uniform except for our induction |
| 133 | // operand. |
| 134 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) |
| 135 | if (i != InductionOperand && |
| 136 | !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp)) |
| 137 | return Ptr; |
| 138 | return GEP->getOperand(InductionOperand); |
| 139 | } |
| 140 | |
| 141 | /// \brief If a value has only one user that is a CastInst, return it. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 142 | Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) { |
| 143 | Value *UniqueCast = nullptr; |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 144 | for (User *U : Ptr->users()) { |
| 145 | CastInst *CI = dyn_cast<CastInst>(U); |
| 146 | if (CI && CI->getType() == Ty) { |
| 147 | if (!UniqueCast) |
| 148 | UniqueCast = CI; |
| 149 | else |
| 150 | return nullptr; |
| 151 | } |
| 152 | } |
| 153 | return UniqueCast; |
| 154 | } |
| 155 | |
| 156 | /// \brief Get the stride of a pointer access in a loop. Looks for symbolic |
| 157 | /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 158 | Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) { |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 159 | auto *PtrTy = dyn_cast<PointerType>(Ptr->getType()); |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 160 | if (!PtrTy || PtrTy->isAggregateType()) |
| 161 | return nullptr; |
| 162 | |
| 163 | // Try to remove a gep instruction to make the pointer (actually index at this |
| 164 | // point) easier analyzable. If OrigPtr is equal to Ptr we are analzying the |
| 165 | // pointer, otherwise, we are analyzing the index. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 166 | Value *OrigPtr = Ptr; |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 167 | |
| 168 | // The size of the pointer access. |
| 169 | int64_t PtrAccessSize = 1; |
| 170 | |
| 171 | Ptr = stripGetElementPtr(Ptr, SE, Lp); |
| 172 | const SCEV *V = SE->getSCEV(Ptr); |
| 173 | |
| 174 | if (Ptr != OrigPtr) |
| 175 | // Strip off casts. |
| 176 | while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) |
| 177 | V = C->getOperand(); |
| 178 | |
| 179 | const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V); |
| 180 | if (!S) |
| 181 | return nullptr; |
| 182 | |
| 183 | V = S->getStepRecurrence(*SE); |
| 184 | if (!V) |
| 185 | return nullptr; |
| 186 | |
| 187 | // Strip off the size of access multiplication if we are still analyzing the |
| 188 | // pointer. |
| 189 | if (OrigPtr == Ptr) { |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 190 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) { |
| 191 | if (M->getOperand(0)->getSCEVType() != scConstant) |
| 192 | return nullptr; |
| 193 | |
Sanjoy Das | 0de2fec | 2015-12-17 20:28:46 +0000 | [diff] [blame] | 194 | const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt(); |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 195 | |
| 196 | // Huge step value - give up. |
| 197 | if (APStepVal.getBitWidth() > 64) |
| 198 | return nullptr; |
| 199 | |
| 200 | int64_t StepVal = APStepVal.getSExtValue(); |
| 201 | if (PtrAccessSize != StepVal) |
| 202 | return nullptr; |
| 203 | V = M->getOperand(1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Strip off casts. |
| 208 | Type *StripedOffRecurrenceCast = nullptr; |
| 209 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) { |
| 210 | StripedOffRecurrenceCast = C->getType(); |
| 211 | V = C->getOperand(); |
| 212 | } |
| 213 | |
| 214 | // Look for the loop invariant symbolic value. |
| 215 | const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V); |
| 216 | if (!U) |
| 217 | return nullptr; |
| 218 | |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 219 | Value *Stride = U->getValue(); |
Hal Finkel | 9cf58c4 | 2015-07-11 10:52:42 +0000 | [diff] [blame] | 220 | if (!Lp->isLoopInvariant(Stride)) |
| 221 | return nullptr; |
| 222 | |
| 223 | // If we have stripped off the recurrence cast we have to make sure that we |
| 224 | // return the value that is used in this loop so that we can replace it later. |
| 225 | if (StripedOffRecurrenceCast) |
| 226 | Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast); |
| 227 | |
| 228 | return Stride; |
| 229 | } |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 230 | |
| 231 | /// \brief Given a vector and an element number, see if the scalar value is |
| 232 | /// already around as a register, for example if it were inserted then extracted |
| 233 | /// from the vector. |
David Majnemer | 5eaf08f | 2015-08-18 22:07:20 +0000 | [diff] [blame] | 234 | Value *llvm::findScalarElement(Value *V, unsigned EltNo) { |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 235 | assert(V->getType()->isVectorTy() && "Not looking at a vector?"); |
| 236 | VectorType *VTy = cast<VectorType>(V->getType()); |
| 237 | unsigned Width = VTy->getNumElements(); |
| 238 | if (EltNo >= Width) // Out of range access. |
| 239 | return UndefValue::get(VTy->getElementType()); |
| 240 | |
| 241 | if (Constant *C = dyn_cast<Constant>(V)) |
| 242 | return C->getAggregateElement(EltNo); |
| 243 | |
| 244 | if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 245 | // If this is an insert to a variable element, we don't know what it is. |
| 246 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 247 | return nullptr; |
| 248 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
| 249 | |
| 250 | // If this is an insert to the element we are looking for, return the |
| 251 | // inserted value. |
| 252 | if (EltNo == IIElt) |
| 253 | return III->getOperand(1); |
| 254 | |
| 255 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 256 | // vector input. |
| 257 | return findScalarElement(III->getOperand(0), EltNo); |
| 258 | } |
| 259 | |
| 260 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
| 261 | unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements(); |
| 262 | int InEl = SVI->getMaskValue(EltNo); |
| 263 | if (InEl < 0) |
| 264 | return UndefValue::get(VTy->getElementType()); |
| 265 | if (InEl < (int)LHSWidth) |
| 266 | return findScalarElement(SVI->getOperand(0), InEl); |
| 267 | return findScalarElement(SVI->getOperand(1), InEl - LHSWidth); |
| 268 | } |
| 269 | |
| 270 | // Extract a value from a vector add operation with a constant zero. |
| 271 | Value *Val = nullptr; Constant *Con = nullptr; |
David Majnemer | c6bb0e2 | 2015-08-18 22:07:25 +0000 | [diff] [blame] | 272 | if (match(V, m_Add(m_Value(Val), m_Constant(Con)))) |
| 273 | if (Constant *Elt = Con->getAggregateElement(EltNo)) |
| 274 | if (Elt->isNullValue()) |
| 275 | return findScalarElement(Val, EltNo); |
David Majnemer | 599ca44 | 2015-07-13 01:15:53 +0000 | [diff] [blame] | 276 | |
| 277 | // Otherwise, we don't know. |
| 278 | return nullptr; |
| 279 | } |
Renato Golin | 3b1d3b0 | 2015-08-30 10:49:04 +0000 | [diff] [blame] | 280 | |
| 281 | /// \brief Get splat value if the input is a splat vector or return nullptr. |
Elena Demikhovsky | 63a7ca9 | 2015-08-30 13:48:02 +0000 | [diff] [blame] | 282 | /// This function is not fully general. It checks only 2 cases: |
| 283 | /// the input value is (1) a splat constants vector or (2) a sequence |
| 284 | /// of instructions that broadcast a single value into a vector. |
| 285 | /// |
Elena Demikhovsky | 0781d7b | 2015-12-01 12:08:36 +0000 | [diff] [blame] | 286 | const llvm::Value *llvm::getSplatValue(const Value *V) { |
| 287 | |
| 288 | if (auto *C = dyn_cast<Constant>(V)) |
Elena Demikhovsky | 47fa271 | 2015-12-01 12:30:40 +0000 | [diff] [blame] | 289 | if (isa<VectorType>(V->getType())) |
| 290 | return C->getSplatValue(); |
Elena Demikhovsky | 63a7ca9 | 2015-08-30 13:48:02 +0000 | [diff] [blame] | 291 | |
| 292 | auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V); |
Renato Golin | 3b1d3b0 | 2015-08-30 10:49:04 +0000 | [diff] [blame] | 293 | if (!ShuffleInst) |
| 294 | return nullptr; |
Elena Demikhovsky | 63a7ca9 | 2015-08-30 13:48:02 +0000 | [diff] [blame] | 295 | // All-zero (or undef) shuffle mask elements. |
| 296 | for (int MaskElt : ShuffleInst->getShuffleMask()) |
| 297 | if (MaskElt != 0 && MaskElt != -1) |
Renato Golin | 3b1d3b0 | 2015-08-30 10:49:04 +0000 | [diff] [blame] | 298 | return nullptr; |
| 299 | // The first shuffle source is 'insertelement' with index 0. |
Elena Demikhovsky | 63a7ca9 | 2015-08-30 13:48:02 +0000 | [diff] [blame] | 300 | auto *InsertEltInst = |
| 301 | dyn_cast<InsertElementInst>(ShuffleInst->getOperand(0)); |
Renato Golin | 3b1d3b0 | 2015-08-30 10:49:04 +0000 | [diff] [blame] | 302 | if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) || |
| 303 | !cast<ConstantInt>(InsertEltInst->getOperand(2))->isNullValue()) |
| 304 | return nullptr; |
| 305 | |
| 306 | return InsertEltInst->getOperand(1); |
| 307 | } |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 308 | |
Charlie Turner | 54336a5 | 2015-11-26 20:39:51 +0000 | [diff] [blame] | 309 | MapVector<Instruction *, uint64_t> |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 310 | llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB, |
| 311 | const TargetTransformInfo *TTI) { |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 312 | |
| 313 | // DemandedBits will give us every value's live-out bits. But we want |
| 314 | // to ensure no extra casts would need to be inserted, so every DAG |
| 315 | // of connected values must have the same minimum bitwidth. |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 316 | EquivalenceClasses<Value *> ECs; |
| 317 | SmallVector<Value *, 16> Worklist; |
| 318 | SmallPtrSet<Value *, 4> Roots; |
| 319 | SmallPtrSet<Value *, 16> Visited; |
| 320 | DenseMap<Value *, uint64_t> DBits; |
| 321 | SmallPtrSet<Instruction *, 4> InstructionSet; |
Charlie Turner | 54336a5 | 2015-11-26 20:39:51 +0000 | [diff] [blame] | 322 | MapVector<Instruction *, uint64_t> MinBWs; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 323 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 324 | // Determine the roots. We work bottom-up, from truncs or icmps. |
| 325 | bool SeenExtFromIllegalType = false; |
| 326 | for (auto *BB : Blocks) |
| 327 | for (auto &I : *BB) { |
| 328 | InstructionSet.insert(&I); |
| 329 | |
| 330 | if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) && |
| 331 | !TTI->isTypeLegal(I.getOperand(0)->getType())) |
| 332 | SeenExtFromIllegalType = true; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 333 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 334 | // Only deal with non-vector integers up to 64-bits wide. |
| 335 | if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) && |
| 336 | !I.getType()->isVectorTy() && |
| 337 | I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) { |
| 338 | // Don't make work for ourselves. If we know the loaded type is legal, |
| 339 | // don't add it to the worklist. |
| 340 | if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType())) |
| 341 | continue; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 342 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 343 | Worklist.push_back(&I); |
| 344 | Roots.insert(&I); |
| 345 | } |
| 346 | } |
| 347 | // Early exit. |
| 348 | if (Worklist.empty() || (TTI && !SeenExtFromIllegalType)) |
| 349 | return MinBWs; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 350 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 351 | // Now proceed breadth-first, unioning values together. |
| 352 | while (!Worklist.empty()) { |
| 353 | Value *Val = Worklist.pop_back_val(); |
| 354 | Value *Leader = ECs.getOrInsertLeaderValue(Val); |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 355 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 356 | if (Visited.count(Val)) |
| 357 | continue; |
| 358 | Visited.insert(Val); |
| 359 | |
| 360 | // Non-instructions terminate a chain successfully. |
| 361 | if (!isa<Instruction>(Val)) |
| 362 | continue; |
| 363 | Instruction *I = cast<Instruction>(Val); |
| 364 | |
| 365 | // If we encounter a type that is larger than 64 bits, we can't represent |
| 366 | // it so bail out. |
James Molloy | aa1d638 | 2016-05-10 12:27:23 +0000 | [diff] [blame] | 367 | if (DB.getDemandedBits(I).getBitWidth() > 64) |
Charlie Turner | 54336a5 | 2015-11-26 20:39:51 +0000 | [diff] [blame] | 368 | return MapVector<Instruction *, uint64_t>(); |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 369 | |
James Molloy | aa1d638 | 2016-05-10 12:27:23 +0000 | [diff] [blame] | 370 | uint64_t V = DB.getDemandedBits(I).getZExtValue(); |
| 371 | DBits[Leader] |= V; |
| 372 | DBits[I] = V; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 373 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 374 | // Casts, loads and instructions outside of our range terminate a chain |
| 375 | // successfully. |
| 376 | if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) || |
| 377 | !InstructionSet.count(I)) |
| 378 | continue; |
| 379 | |
| 380 | // Unsafe casts terminate a chain unsuccessfully. We can't do anything |
| 381 | // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to |
| 382 | // transform anything that relies on them. |
| 383 | if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) || |
| 384 | !I->getType()->isIntegerTy()) { |
| 385 | DBits[Leader] |= ~0ULL; |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | // We don't modify the types of PHIs. Reductions will already have been |
| 390 | // truncated if possible, and inductions' sizes will have been chosen by |
| 391 | // indvars. |
| 392 | if (isa<PHINode>(I)) |
| 393 | continue; |
| 394 | |
| 395 | if (DBits[Leader] == ~0ULL) |
| 396 | // All bits demanded, no point continuing. |
| 397 | continue; |
| 398 | |
| 399 | for (Value *O : cast<User>(I)->operands()) { |
| 400 | ECs.unionSets(Leader, O); |
| 401 | Worklist.push_back(O); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Now we've discovered all values, walk them to see if there are |
| 406 | // any users we didn't see. If there are, we can't optimize that |
| 407 | // chain. |
| 408 | for (auto &I : DBits) |
| 409 | for (auto *U : I.first->users()) |
| 410 | if (U->getType()->isIntegerTy() && DBits.count(U) == 0) |
| 411 | DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL; |
James Molloy | 45f67d5 | 2015-11-09 14:32:05 +0000 | [diff] [blame] | 412 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 413 | for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) { |
| 414 | uint64_t LeaderDemandedBits = 0; |
| 415 | for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) |
| 416 | LeaderDemandedBits |= DBits[*MI]; |
| 417 | |
| 418 | uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) - |
| 419 | llvm::countLeadingZeros(LeaderDemandedBits); |
| 420 | // Round up to a power of 2 |
| 421 | if (!isPowerOf2_64((uint64_t)MinBW)) |
| 422 | MinBW = NextPowerOf2(MinBW); |
James Molloy | 8e46cd0 | 2016-03-30 10:11:43 +0000 | [diff] [blame] | 423 | |
| 424 | // We don't modify the types of PHIs. Reductions will already have been |
| 425 | // truncated if possible, and inductions' sizes will have been chosen by |
| 426 | // indvars. |
| 427 | // If we are required to shrink a PHI, abandon this entire equivalence class. |
| 428 | bool Abort = false; |
| 429 | for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) |
| 430 | if (isa<PHINode>(*MI) && MinBW < (*MI)->getType()->getScalarSizeInBits()) { |
| 431 | Abort = true; |
| 432 | break; |
| 433 | } |
| 434 | if (Abort) |
| 435 | continue; |
| 436 | |
James Molloy | 55d633b | 2015-10-12 12:34:45 +0000 | [diff] [blame] | 437 | for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) { |
| 438 | if (!isa<Instruction>(*MI)) |
| 439 | continue; |
| 440 | Type *Ty = (*MI)->getType(); |
| 441 | if (Roots.count(*MI)) |
| 442 | Ty = cast<Instruction>(*MI)->getOperand(0)->getType(); |
| 443 | if (MinBW < Ty->getScalarSizeInBits()) |
| 444 | MinBWs[cast<Instruction>(*MI)] = MinBW; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return MinBWs; |
| 449 | } |
Matt Arsenault | 727e279 | 2016-06-30 21:17:59 +0000 | [diff] [blame] | 450 | |
| 451 | /// \returns \p I after propagating metadata from \p VL. |
| 452 | Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) { |
| 453 | Instruction *I0 = cast<Instruction>(VL[0]); |
| 454 | SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; |
| 455 | I0->getAllMetadataOtherThanDebugLoc(Metadata); |
| 456 | |
Justin Lebar | 11a3204 | 2016-09-11 01:39:08 +0000 | [diff] [blame] | 457 | for (auto Kind : |
| 458 | {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 459 | LLVMContext::MD_noalias, LLVMContext::MD_fpmath, |
| 460 | LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load}) { |
Matt Arsenault | 727e279 | 2016-06-30 21:17:59 +0000 | [diff] [blame] | 461 | MDNode *MD = I0->getMetadata(Kind); |
| 462 | |
| 463 | for (int J = 1, E = VL.size(); MD && J != E; ++J) { |
| 464 | const Instruction *IJ = cast<Instruction>(VL[J]); |
| 465 | MDNode *IMD = IJ->getMetadata(Kind); |
| 466 | switch (Kind) { |
| 467 | case LLVMContext::MD_tbaa: |
| 468 | MD = MDNode::getMostGenericTBAA(MD, IMD); |
| 469 | break; |
| 470 | case LLVMContext::MD_alias_scope: |
| 471 | MD = MDNode::getMostGenericAliasScope(MD, IMD); |
| 472 | break; |
Matt Arsenault | 727e279 | 2016-06-30 21:17:59 +0000 | [diff] [blame] | 473 | case LLVMContext::MD_fpmath: |
| 474 | MD = MDNode::getMostGenericFPMath(MD, IMD); |
| 475 | break; |
Justin Lebar | 11a3204 | 2016-09-11 01:39:08 +0000 | [diff] [blame] | 476 | case LLVMContext::MD_noalias: |
Matt Arsenault | 727e279 | 2016-06-30 21:17:59 +0000 | [diff] [blame] | 477 | case LLVMContext::MD_nontemporal: |
Justin Lebar | 11a3204 | 2016-09-11 01:39:08 +0000 | [diff] [blame] | 478 | case LLVMContext::MD_invariant_load: |
Matt Arsenault | 727e279 | 2016-06-30 21:17:59 +0000 | [diff] [blame] | 479 | MD = MDNode::intersect(MD, IMD); |
| 480 | break; |
| 481 | default: |
| 482 | llvm_unreachable("unhandled metadata"); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | Inst->setMetadata(Kind, MD); |
| 487 | } |
| 488 | |
| 489 | return Inst; |
| 490 | } |