1*0b57cec5SDimitry Andric //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements induction variable simplification. It does 10*0b57cec5SDimitry Andric // not define any actual pass or policy, but provides a single function to 11*0b57cec5SDimitry Andric // simplify a loop's induction variables based on ScalarEvolution. 12*0b57cec5SDimitry Andric // 13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SimplifyIndVar.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 19*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 20*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionExpander.h" 21*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 22*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 23*0b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 24*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 25*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 26*0b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h" 27*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 28*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 29*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 30*0b57cec5SDimitry Andric 31*0b57cec5SDimitry Andric using namespace llvm; 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric #define DEBUG_TYPE "indvars" 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); 36*0b57cec5SDimitry Andric STATISTIC(NumElimOperand, "Number of IV operands folded into a use"); 37*0b57cec5SDimitry Andric STATISTIC(NumFoldedUser, "Number of IV users folded into a constant"); 38*0b57cec5SDimitry Andric STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); 39*0b57cec5SDimitry Andric STATISTIC( 40*0b57cec5SDimitry Andric NumSimplifiedSDiv, 41*0b57cec5SDimitry Andric "Number of IV signed division operations converted to unsigned division"); 42*0b57cec5SDimitry Andric STATISTIC( 43*0b57cec5SDimitry Andric NumSimplifiedSRem, 44*0b57cec5SDimitry Andric "Number of IV signed remainder operations converted to unsigned remainder"); 45*0b57cec5SDimitry Andric STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric namespace { 48*0b57cec5SDimitry Andric /// This is a utility for simplifying induction variables 49*0b57cec5SDimitry Andric /// based on ScalarEvolution. It is the primary instrument of the 50*0b57cec5SDimitry Andric /// IndvarSimplify pass, but it may also be directly invoked to cleanup after 51*0b57cec5SDimitry Andric /// other loop passes that preserve SCEV. 52*0b57cec5SDimitry Andric class SimplifyIndvar { 53*0b57cec5SDimitry Andric Loop *L; 54*0b57cec5SDimitry Andric LoopInfo *LI; 55*0b57cec5SDimitry Andric ScalarEvolution *SE; 56*0b57cec5SDimitry Andric DominatorTree *DT; 57*0b57cec5SDimitry Andric SCEVExpander &Rewriter; 58*0b57cec5SDimitry Andric SmallVectorImpl<WeakTrackingVH> &DeadInsts; 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric bool Changed; 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric public: 63*0b57cec5SDimitry Andric SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT, 64*0b57cec5SDimitry Andric LoopInfo *LI, SCEVExpander &Rewriter, 65*0b57cec5SDimitry Andric SmallVectorImpl<WeakTrackingVH> &Dead) 66*0b57cec5SDimitry Andric : L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead), 67*0b57cec5SDimitry Andric Changed(false) { 68*0b57cec5SDimitry Andric assert(LI && "IV simplification requires LoopInfo"); 69*0b57cec5SDimitry Andric } 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric bool hasChanged() const { return Changed; } 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric /// Iteratively perform simplification on a worklist of users of the 74*0b57cec5SDimitry Andric /// specified induction variable. This is the top-level driver that applies 75*0b57cec5SDimitry Andric /// all simplifications to users of an IV. 76*0b57cec5SDimitry Andric void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr); 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); 81*0b57cec5SDimitry Andric bool replaceIVUserWithLoopInvariant(Instruction *UseInst); 82*0b57cec5SDimitry Andric 83*0b57cec5SDimitry Andric bool eliminateOverflowIntrinsic(WithOverflowInst *WO); 84*0b57cec5SDimitry Andric bool eliminateSaturatingIntrinsic(SaturatingInst *SI); 85*0b57cec5SDimitry Andric bool eliminateTrunc(TruncInst *TI); 86*0b57cec5SDimitry Andric bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); 87*0b57cec5SDimitry Andric bool makeIVComparisonInvariant(ICmpInst *ICmp, Value *IVOperand); 88*0b57cec5SDimitry Andric void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); 89*0b57cec5SDimitry Andric void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand, 90*0b57cec5SDimitry Andric bool IsSigned); 91*0b57cec5SDimitry Andric void replaceRemWithNumerator(BinaryOperator *Rem); 92*0b57cec5SDimitry Andric void replaceRemWithNumeratorOrZero(BinaryOperator *Rem); 93*0b57cec5SDimitry Andric void replaceSRemWithURem(BinaryOperator *Rem); 94*0b57cec5SDimitry Andric bool eliminateSDiv(BinaryOperator *SDiv); 95*0b57cec5SDimitry Andric bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand); 96*0b57cec5SDimitry Andric bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand); 97*0b57cec5SDimitry Andric }; 98*0b57cec5SDimitry Andric } 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric /// Fold an IV operand into its use. This removes increments of an 101*0b57cec5SDimitry Andric /// aligned IV when used by a instruction that ignores the low bits. 102*0b57cec5SDimitry Andric /// 103*0b57cec5SDimitry Andric /// IVOperand is guaranteed SCEVable, but UseInst may not be. 104*0b57cec5SDimitry Andric /// 105*0b57cec5SDimitry Andric /// Return the operand of IVOperand for this induction variable if IVOperand can 106*0b57cec5SDimitry Andric /// be folded (in case more folding opportunities have been exposed). 107*0b57cec5SDimitry Andric /// Otherwise return null. 108*0b57cec5SDimitry Andric Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) { 109*0b57cec5SDimitry Andric Value *IVSrc = nullptr; 110*0b57cec5SDimitry Andric const unsigned OperIdx = 0; 111*0b57cec5SDimitry Andric const SCEV *FoldedExpr = nullptr; 112*0b57cec5SDimitry Andric bool MustDropExactFlag = false; 113*0b57cec5SDimitry Andric switch (UseInst->getOpcode()) { 114*0b57cec5SDimitry Andric default: 115*0b57cec5SDimitry Andric return nullptr; 116*0b57cec5SDimitry Andric case Instruction::UDiv: 117*0b57cec5SDimitry Andric case Instruction::LShr: 118*0b57cec5SDimitry Andric // We're only interested in the case where we know something about 119*0b57cec5SDimitry Andric // the numerator and have a constant denominator. 120*0b57cec5SDimitry Andric if (IVOperand != UseInst->getOperand(OperIdx) || 121*0b57cec5SDimitry Andric !isa<ConstantInt>(UseInst->getOperand(1))) 122*0b57cec5SDimitry Andric return nullptr; 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric // Attempt to fold a binary operator with constant operand. 125*0b57cec5SDimitry Andric // e.g. ((I + 1) >> 2) => I >> 2 126*0b57cec5SDimitry Andric if (!isa<BinaryOperator>(IVOperand) 127*0b57cec5SDimitry Andric || !isa<ConstantInt>(IVOperand->getOperand(1))) 128*0b57cec5SDimitry Andric return nullptr; 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric IVSrc = IVOperand->getOperand(0); 131*0b57cec5SDimitry Andric // IVSrc must be the (SCEVable) IV, since the other operand is const. 132*0b57cec5SDimitry Andric assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand"); 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1)); 135*0b57cec5SDimitry Andric if (UseInst->getOpcode() == Instruction::LShr) { 136*0b57cec5SDimitry Andric // Get a constant for the divisor. See createSCEV. 137*0b57cec5SDimitry Andric uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth(); 138*0b57cec5SDimitry Andric if (D->getValue().uge(BitWidth)) 139*0b57cec5SDimitry Andric return nullptr; 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric D = ConstantInt::get(UseInst->getContext(), 142*0b57cec5SDimitry Andric APInt::getOneBitSet(BitWidth, D->getZExtValue())); 143*0b57cec5SDimitry Andric } 144*0b57cec5SDimitry Andric FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D)); 145*0b57cec5SDimitry Andric // We might have 'exact' flag set at this point which will no longer be 146*0b57cec5SDimitry Andric // correct after we make the replacement. 147*0b57cec5SDimitry Andric if (UseInst->isExact() && 148*0b57cec5SDimitry Andric SE->getSCEV(IVSrc) != SE->getMulExpr(FoldedExpr, SE->getSCEV(D))) 149*0b57cec5SDimitry Andric MustDropExactFlag = true; 150*0b57cec5SDimitry Andric } 151*0b57cec5SDimitry Andric // We have something that might fold it's operand. Compare SCEVs. 152*0b57cec5SDimitry Andric if (!SE->isSCEVable(UseInst->getType())) 153*0b57cec5SDimitry Andric return nullptr; 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric // Bypass the operand if SCEV can prove it has no effect. 156*0b57cec5SDimitry Andric if (SE->getSCEV(UseInst) != FoldedExpr) 157*0b57cec5SDimitry Andric return nullptr; 158*0b57cec5SDimitry Andric 159*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand 160*0b57cec5SDimitry Andric << " -> " << *UseInst << '\n'); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric UseInst->setOperand(OperIdx, IVSrc); 163*0b57cec5SDimitry Andric assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper"); 164*0b57cec5SDimitry Andric 165*0b57cec5SDimitry Andric if (MustDropExactFlag) 166*0b57cec5SDimitry Andric UseInst->dropPoisonGeneratingFlags(); 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric ++NumElimOperand; 169*0b57cec5SDimitry Andric Changed = true; 170*0b57cec5SDimitry Andric if (IVOperand->use_empty()) 171*0b57cec5SDimitry Andric DeadInsts.emplace_back(IVOperand); 172*0b57cec5SDimitry Andric return IVSrc; 173*0b57cec5SDimitry Andric } 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp, 176*0b57cec5SDimitry Andric Value *IVOperand) { 177*0b57cec5SDimitry Andric unsigned IVOperIdx = 0; 178*0b57cec5SDimitry Andric ICmpInst::Predicate Pred = ICmp->getPredicate(); 179*0b57cec5SDimitry Andric if (IVOperand != ICmp->getOperand(0)) { 180*0b57cec5SDimitry Andric // Swapped 181*0b57cec5SDimitry Andric assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); 182*0b57cec5SDimitry Andric IVOperIdx = 1; 183*0b57cec5SDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred); 184*0b57cec5SDimitry Andric } 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric // Get the SCEVs for the ICmp operands (in the specific context of the 187*0b57cec5SDimitry Andric // current loop) 188*0b57cec5SDimitry Andric const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); 189*0b57cec5SDimitry Andric const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop); 190*0b57cec5SDimitry Andric const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop); 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric ICmpInst::Predicate InvariantPredicate; 193*0b57cec5SDimitry Andric const SCEV *InvariantLHS, *InvariantRHS; 194*0b57cec5SDimitry Andric 195*0b57cec5SDimitry Andric auto *PN = dyn_cast<PHINode>(IVOperand); 196*0b57cec5SDimitry Andric if (!PN) 197*0b57cec5SDimitry Andric return false; 198*0b57cec5SDimitry Andric if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate, 199*0b57cec5SDimitry Andric InvariantLHS, InvariantRHS)) 200*0b57cec5SDimitry Andric return false; 201*0b57cec5SDimitry Andric 202*0b57cec5SDimitry Andric // Rewrite the comparison to a loop invariant comparison if it can be done 203*0b57cec5SDimitry Andric // cheaply, where cheaply means "we don't need to emit any new 204*0b57cec5SDimitry Andric // instructions". 205*0b57cec5SDimitry Andric 206*0b57cec5SDimitry Andric SmallDenseMap<const SCEV*, Value*> CheapExpansions; 207*0b57cec5SDimitry Andric CheapExpansions[S] = ICmp->getOperand(IVOperIdx); 208*0b57cec5SDimitry Andric CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx); 209*0b57cec5SDimitry Andric 210*0b57cec5SDimitry Andric // TODO: Support multiple entry loops? (We currently bail out of these in 211*0b57cec5SDimitry Andric // the IndVarSimplify pass) 212*0b57cec5SDimitry Andric if (auto *BB = L->getLoopPredecessor()) { 213*0b57cec5SDimitry Andric const int Idx = PN->getBasicBlockIndex(BB); 214*0b57cec5SDimitry Andric if (Idx >= 0) { 215*0b57cec5SDimitry Andric Value *Incoming = PN->getIncomingValue(Idx); 216*0b57cec5SDimitry Andric const SCEV *IncomingS = SE->getSCEV(Incoming); 217*0b57cec5SDimitry Andric CheapExpansions[IncomingS] = Incoming; 218*0b57cec5SDimitry Andric } 219*0b57cec5SDimitry Andric } 220*0b57cec5SDimitry Andric Value *NewLHS = CheapExpansions[InvariantLHS]; 221*0b57cec5SDimitry Andric Value *NewRHS = CheapExpansions[InvariantRHS]; 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric if (!NewLHS) 224*0b57cec5SDimitry Andric if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS)) 225*0b57cec5SDimitry Andric NewLHS = ConstLHS->getValue(); 226*0b57cec5SDimitry Andric if (!NewRHS) 227*0b57cec5SDimitry Andric if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS)) 228*0b57cec5SDimitry Andric NewRHS = ConstRHS->getValue(); 229*0b57cec5SDimitry Andric 230*0b57cec5SDimitry Andric if (!NewLHS || !NewRHS) 231*0b57cec5SDimitry Andric // We could not find an existing value to replace either LHS or RHS. 232*0b57cec5SDimitry Andric // Generating new instructions has subtler tradeoffs, so avoid doing that 233*0b57cec5SDimitry Andric // for now. 234*0b57cec5SDimitry Andric return false; 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n'); 237*0b57cec5SDimitry Andric ICmp->setPredicate(InvariantPredicate); 238*0b57cec5SDimitry Andric ICmp->setOperand(0, NewLHS); 239*0b57cec5SDimitry Andric ICmp->setOperand(1, NewRHS); 240*0b57cec5SDimitry Andric return true; 241*0b57cec5SDimitry Andric } 242*0b57cec5SDimitry Andric 243*0b57cec5SDimitry Andric /// SimplifyIVUsers helper for eliminating useless 244*0b57cec5SDimitry Andric /// comparisons against an induction variable. 245*0b57cec5SDimitry Andric void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { 246*0b57cec5SDimitry Andric unsigned IVOperIdx = 0; 247*0b57cec5SDimitry Andric ICmpInst::Predicate Pred = ICmp->getPredicate(); 248*0b57cec5SDimitry Andric ICmpInst::Predicate OriginalPred = Pred; 249*0b57cec5SDimitry Andric if (IVOperand != ICmp->getOperand(0)) { 250*0b57cec5SDimitry Andric // Swapped 251*0b57cec5SDimitry Andric assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); 252*0b57cec5SDimitry Andric IVOperIdx = 1; 253*0b57cec5SDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred); 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric 256*0b57cec5SDimitry Andric // Get the SCEVs for the ICmp operands (in the specific context of the 257*0b57cec5SDimitry Andric // current loop) 258*0b57cec5SDimitry Andric const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); 259*0b57cec5SDimitry Andric const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop); 260*0b57cec5SDimitry Andric const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop); 261*0b57cec5SDimitry Andric 262*0b57cec5SDimitry Andric // If the condition is always true or always false, replace it with 263*0b57cec5SDimitry Andric // a constant value. 264*0b57cec5SDimitry Andric if (SE->isKnownPredicate(Pred, S, X)) { 265*0b57cec5SDimitry Andric ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); 266*0b57cec5SDimitry Andric DeadInsts.emplace_back(ICmp); 267*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); 268*0b57cec5SDimitry Andric } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) { 269*0b57cec5SDimitry Andric ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); 270*0b57cec5SDimitry Andric DeadInsts.emplace_back(ICmp); 271*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); 272*0b57cec5SDimitry Andric } else if (makeIVComparisonInvariant(ICmp, IVOperand)) { 273*0b57cec5SDimitry Andric // fallthrough to end of function 274*0b57cec5SDimitry Andric } else if (ICmpInst::isSigned(OriginalPred) && 275*0b57cec5SDimitry Andric SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) { 276*0b57cec5SDimitry Andric // If we were unable to make anything above, all we can is to canonicalize 277*0b57cec5SDimitry Andric // the comparison hoping that it will open the doors for other 278*0b57cec5SDimitry Andric // optimizations. If we find out that we compare two non-negative values, 279*0b57cec5SDimitry Andric // we turn the instruction's predicate to its unsigned version. Note that 280*0b57cec5SDimitry Andric // we cannot rely on Pred here unless we check if we have swapped it. 281*0b57cec5SDimitry Andric assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?"); 282*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp 283*0b57cec5SDimitry Andric << '\n'); 284*0b57cec5SDimitry Andric ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred)); 285*0b57cec5SDimitry Andric } else 286*0b57cec5SDimitry Andric return; 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric ++NumElimCmp; 289*0b57cec5SDimitry Andric Changed = true; 290*0b57cec5SDimitry Andric } 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) { 293*0b57cec5SDimitry Andric // Get the SCEVs for the ICmp operands. 294*0b57cec5SDimitry Andric auto *N = SE->getSCEV(SDiv->getOperand(0)); 295*0b57cec5SDimitry Andric auto *D = SE->getSCEV(SDiv->getOperand(1)); 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric // Simplify unnecessary loops away. 298*0b57cec5SDimitry Andric const Loop *L = LI->getLoopFor(SDiv->getParent()); 299*0b57cec5SDimitry Andric N = SE->getSCEVAtScope(N, L); 300*0b57cec5SDimitry Andric D = SE->getSCEVAtScope(D, L); 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric // Replace sdiv by udiv if both of the operands are non-negative 303*0b57cec5SDimitry Andric if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) { 304*0b57cec5SDimitry Andric auto *UDiv = BinaryOperator::Create( 305*0b57cec5SDimitry Andric BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1), 306*0b57cec5SDimitry Andric SDiv->getName() + ".udiv", SDiv); 307*0b57cec5SDimitry Andric UDiv->setIsExact(SDiv->isExact()); 308*0b57cec5SDimitry Andric SDiv->replaceAllUsesWith(UDiv); 309*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n'); 310*0b57cec5SDimitry Andric ++NumSimplifiedSDiv; 311*0b57cec5SDimitry Andric Changed = true; 312*0b57cec5SDimitry Andric DeadInsts.push_back(SDiv); 313*0b57cec5SDimitry Andric return true; 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric return false; 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric // i %s n -> i %u n if i >= 0 and n >= 0 320*0b57cec5SDimitry Andric void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) { 321*0b57cec5SDimitry Andric auto *N = Rem->getOperand(0), *D = Rem->getOperand(1); 322*0b57cec5SDimitry Andric auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D, 323*0b57cec5SDimitry Andric Rem->getName() + ".urem", Rem); 324*0b57cec5SDimitry Andric Rem->replaceAllUsesWith(URem); 325*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n'); 326*0b57cec5SDimitry Andric ++NumSimplifiedSRem; 327*0b57cec5SDimitry Andric Changed = true; 328*0b57cec5SDimitry Andric DeadInsts.emplace_back(Rem); 329*0b57cec5SDimitry Andric } 330*0b57cec5SDimitry Andric 331*0b57cec5SDimitry Andric // i % n --> i if i is in [0,n). 332*0b57cec5SDimitry Andric void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) { 333*0b57cec5SDimitry Andric Rem->replaceAllUsesWith(Rem->getOperand(0)); 334*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); 335*0b57cec5SDimitry Andric ++NumElimRem; 336*0b57cec5SDimitry Andric Changed = true; 337*0b57cec5SDimitry Andric DeadInsts.emplace_back(Rem); 338*0b57cec5SDimitry Andric } 339*0b57cec5SDimitry Andric 340*0b57cec5SDimitry Andric // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). 341*0b57cec5SDimitry Andric void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) { 342*0b57cec5SDimitry Andric auto *T = Rem->getType(); 343*0b57cec5SDimitry Andric auto *N = Rem->getOperand(0), *D = Rem->getOperand(1); 344*0b57cec5SDimitry Andric ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D); 345*0b57cec5SDimitry Andric SelectInst *Sel = 346*0b57cec5SDimitry Andric SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem); 347*0b57cec5SDimitry Andric Rem->replaceAllUsesWith(Sel); 348*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); 349*0b57cec5SDimitry Andric ++NumElimRem; 350*0b57cec5SDimitry Andric Changed = true; 351*0b57cec5SDimitry Andric DeadInsts.emplace_back(Rem); 352*0b57cec5SDimitry Andric } 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric /// SimplifyIVUsers helper for eliminating useless remainder operations 355*0b57cec5SDimitry Andric /// operating on an induction variable or replacing srem by urem. 356*0b57cec5SDimitry Andric void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand, 357*0b57cec5SDimitry Andric bool IsSigned) { 358*0b57cec5SDimitry Andric auto *NValue = Rem->getOperand(0); 359*0b57cec5SDimitry Andric auto *DValue = Rem->getOperand(1); 360*0b57cec5SDimitry Andric // We're only interested in the case where we know something about 361*0b57cec5SDimitry Andric // the numerator, unless it is a srem, because we want to replace srem by urem 362*0b57cec5SDimitry Andric // in general. 363*0b57cec5SDimitry Andric bool UsedAsNumerator = IVOperand == NValue; 364*0b57cec5SDimitry Andric if (!UsedAsNumerator && !IsSigned) 365*0b57cec5SDimitry Andric return; 366*0b57cec5SDimitry Andric 367*0b57cec5SDimitry Andric const SCEV *N = SE->getSCEV(NValue); 368*0b57cec5SDimitry Andric 369*0b57cec5SDimitry Andric // Simplify unnecessary loops away. 370*0b57cec5SDimitry Andric const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); 371*0b57cec5SDimitry Andric N = SE->getSCEVAtScope(N, ICmpLoop); 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N); 374*0b57cec5SDimitry Andric 375*0b57cec5SDimitry Andric // Do not proceed if the Numerator may be negative 376*0b57cec5SDimitry Andric if (!IsNumeratorNonNegative) 377*0b57cec5SDimitry Andric return; 378*0b57cec5SDimitry Andric 379*0b57cec5SDimitry Andric const SCEV *D = SE->getSCEV(DValue); 380*0b57cec5SDimitry Andric D = SE->getSCEVAtScope(D, ICmpLoop); 381*0b57cec5SDimitry Andric 382*0b57cec5SDimitry Andric if (UsedAsNumerator) { 383*0b57cec5SDimitry Andric auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 384*0b57cec5SDimitry Andric if (SE->isKnownPredicate(LT, N, D)) { 385*0b57cec5SDimitry Andric replaceRemWithNumerator(Rem); 386*0b57cec5SDimitry Andric return; 387*0b57cec5SDimitry Andric } 388*0b57cec5SDimitry Andric 389*0b57cec5SDimitry Andric auto *T = Rem->getType(); 390*0b57cec5SDimitry Andric const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T)); 391*0b57cec5SDimitry Andric if (SE->isKnownPredicate(LT, NLessOne, D)) { 392*0b57cec5SDimitry Andric replaceRemWithNumeratorOrZero(Rem); 393*0b57cec5SDimitry Andric return; 394*0b57cec5SDimitry Andric } 395*0b57cec5SDimitry Andric } 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric // Try to replace SRem with URem, if both N and D are known non-negative. 398*0b57cec5SDimitry Andric // Since we had already check N, we only need to check D now 399*0b57cec5SDimitry Andric if (!IsSigned || !SE->isKnownNonNegative(D)) 400*0b57cec5SDimitry Andric return; 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric replaceSRemWithURem(Rem); 403*0b57cec5SDimitry Andric } 404*0b57cec5SDimitry Andric 405*0b57cec5SDimitry Andric static bool willNotOverflow(ScalarEvolution *SE, Instruction::BinaryOps BinOp, 406*0b57cec5SDimitry Andric bool Signed, const SCEV *LHS, const SCEV *RHS) { 407*0b57cec5SDimitry Andric const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *, 408*0b57cec5SDimitry Andric SCEV::NoWrapFlags, unsigned); 409*0b57cec5SDimitry Andric switch (BinOp) { 410*0b57cec5SDimitry Andric default: 411*0b57cec5SDimitry Andric llvm_unreachable("Unsupported binary op"); 412*0b57cec5SDimitry Andric case Instruction::Add: 413*0b57cec5SDimitry Andric Operation = &ScalarEvolution::getAddExpr; 414*0b57cec5SDimitry Andric break; 415*0b57cec5SDimitry Andric case Instruction::Sub: 416*0b57cec5SDimitry Andric Operation = &ScalarEvolution::getMinusSCEV; 417*0b57cec5SDimitry Andric break; 418*0b57cec5SDimitry Andric case Instruction::Mul: 419*0b57cec5SDimitry Andric Operation = &ScalarEvolution::getMulExpr; 420*0b57cec5SDimitry Andric break; 421*0b57cec5SDimitry Andric } 422*0b57cec5SDimitry Andric 423*0b57cec5SDimitry Andric const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) = 424*0b57cec5SDimitry Andric Signed ? &ScalarEvolution::getSignExtendExpr 425*0b57cec5SDimitry Andric : &ScalarEvolution::getZeroExtendExpr; 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric // Check ext(LHS op RHS) == ext(LHS) op ext(RHS) 428*0b57cec5SDimitry Andric auto *NarrowTy = cast<IntegerType>(LHS->getType()); 429*0b57cec5SDimitry Andric auto *WideTy = 430*0b57cec5SDimitry Andric IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); 431*0b57cec5SDimitry Andric 432*0b57cec5SDimitry Andric const SCEV *A = 433*0b57cec5SDimitry Andric (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), 434*0b57cec5SDimitry Andric WideTy, 0); 435*0b57cec5SDimitry Andric const SCEV *B = 436*0b57cec5SDimitry Andric (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0), 437*0b57cec5SDimitry Andric (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0); 438*0b57cec5SDimitry Andric return A == B; 439*0b57cec5SDimitry Andric } 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateOverflowIntrinsic(WithOverflowInst *WO) { 442*0b57cec5SDimitry Andric const SCEV *LHS = SE->getSCEV(WO->getLHS()); 443*0b57cec5SDimitry Andric const SCEV *RHS = SE->getSCEV(WO->getRHS()); 444*0b57cec5SDimitry Andric if (!willNotOverflow(SE, WO->getBinaryOp(), WO->isSigned(), LHS, RHS)) 445*0b57cec5SDimitry Andric return false; 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric // Proved no overflow, nuke the overflow check and, if possible, the overflow 448*0b57cec5SDimitry Andric // intrinsic as well. 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric BinaryOperator *NewResult = BinaryOperator::Create( 451*0b57cec5SDimitry Andric WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), "", WO); 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric if (WO->isSigned()) 454*0b57cec5SDimitry Andric NewResult->setHasNoSignedWrap(true); 455*0b57cec5SDimitry Andric else 456*0b57cec5SDimitry Andric NewResult->setHasNoUnsignedWrap(true); 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric SmallVector<ExtractValueInst *, 4> ToDelete; 459*0b57cec5SDimitry Andric 460*0b57cec5SDimitry Andric for (auto *U : WO->users()) { 461*0b57cec5SDimitry Andric if (auto *EVI = dyn_cast<ExtractValueInst>(U)) { 462*0b57cec5SDimitry Andric if (EVI->getIndices()[0] == 1) 463*0b57cec5SDimitry Andric EVI->replaceAllUsesWith(ConstantInt::getFalse(WO->getContext())); 464*0b57cec5SDimitry Andric else { 465*0b57cec5SDimitry Andric assert(EVI->getIndices()[0] == 0 && "Only two possibilities!"); 466*0b57cec5SDimitry Andric EVI->replaceAllUsesWith(NewResult); 467*0b57cec5SDimitry Andric } 468*0b57cec5SDimitry Andric ToDelete.push_back(EVI); 469*0b57cec5SDimitry Andric } 470*0b57cec5SDimitry Andric } 471*0b57cec5SDimitry Andric 472*0b57cec5SDimitry Andric for (auto *EVI : ToDelete) 473*0b57cec5SDimitry Andric EVI->eraseFromParent(); 474*0b57cec5SDimitry Andric 475*0b57cec5SDimitry Andric if (WO->use_empty()) 476*0b57cec5SDimitry Andric WO->eraseFromParent(); 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric return true; 479*0b57cec5SDimitry Andric } 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateSaturatingIntrinsic(SaturatingInst *SI) { 482*0b57cec5SDimitry Andric const SCEV *LHS = SE->getSCEV(SI->getLHS()); 483*0b57cec5SDimitry Andric const SCEV *RHS = SE->getSCEV(SI->getRHS()); 484*0b57cec5SDimitry Andric if (!willNotOverflow(SE, SI->getBinaryOp(), SI->isSigned(), LHS, RHS)) 485*0b57cec5SDimitry Andric return false; 486*0b57cec5SDimitry Andric 487*0b57cec5SDimitry Andric BinaryOperator *BO = BinaryOperator::Create( 488*0b57cec5SDimitry Andric SI->getBinaryOp(), SI->getLHS(), SI->getRHS(), SI->getName(), SI); 489*0b57cec5SDimitry Andric if (SI->isSigned()) 490*0b57cec5SDimitry Andric BO->setHasNoSignedWrap(); 491*0b57cec5SDimitry Andric else 492*0b57cec5SDimitry Andric BO->setHasNoUnsignedWrap(); 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric SI->replaceAllUsesWith(BO); 495*0b57cec5SDimitry Andric DeadInsts.emplace_back(SI); 496*0b57cec5SDimitry Andric Changed = true; 497*0b57cec5SDimitry Andric return true; 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) { 501*0b57cec5SDimitry Andric // It is always legal to replace 502*0b57cec5SDimitry Andric // icmp <pred> i32 trunc(iv), n 503*0b57cec5SDimitry Andric // with 504*0b57cec5SDimitry Andric // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate. 505*0b57cec5SDimitry Andric // Or with 506*0b57cec5SDimitry Andric // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate. 507*0b57cec5SDimitry Andric // Or with either of these if pred is an equality predicate. 508*0b57cec5SDimitry Andric // 509*0b57cec5SDimitry Andric // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for 510*0b57cec5SDimitry Andric // every comparison which uses trunc, it means that we can replace each of 511*0b57cec5SDimitry Andric // them with comparison of iv against sext/zext(n). We no longer need trunc 512*0b57cec5SDimitry Andric // after that. 513*0b57cec5SDimitry Andric // 514*0b57cec5SDimitry Andric // TODO: Should we do this if we can widen *some* comparisons, but not all 515*0b57cec5SDimitry Andric // of them? Sometimes it is enough to enable other optimizations, but the 516*0b57cec5SDimitry Andric // trunc instruction will stay in the loop. 517*0b57cec5SDimitry Andric Value *IV = TI->getOperand(0); 518*0b57cec5SDimitry Andric Type *IVTy = IV->getType(); 519*0b57cec5SDimitry Andric const SCEV *IVSCEV = SE->getSCEV(IV); 520*0b57cec5SDimitry Andric const SCEV *TISCEV = SE->getSCEV(TI); 521*0b57cec5SDimitry Andric 522*0b57cec5SDimitry Andric // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can 523*0b57cec5SDimitry Andric // get rid of trunc 524*0b57cec5SDimitry Andric bool DoesSExtCollapse = false; 525*0b57cec5SDimitry Andric bool DoesZExtCollapse = false; 526*0b57cec5SDimitry Andric if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy)) 527*0b57cec5SDimitry Andric DoesSExtCollapse = true; 528*0b57cec5SDimitry Andric if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy)) 529*0b57cec5SDimitry Andric DoesZExtCollapse = true; 530*0b57cec5SDimitry Andric 531*0b57cec5SDimitry Andric // If neither sext nor zext does collapse, it is not profitable to do any 532*0b57cec5SDimitry Andric // transform. Bail. 533*0b57cec5SDimitry Andric if (!DoesSExtCollapse && !DoesZExtCollapse) 534*0b57cec5SDimitry Andric return false; 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric // Collect users of the trunc that look like comparisons against invariants. 537*0b57cec5SDimitry Andric // Bail if we find something different. 538*0b57cec5SDimitry Andric SmallVector<ICmpInst *, 4> ICmpUsers; 539*0b57cec5SDimitry Andric for (auto *U : TI->users()) { 540*0b57cec5SDimitry Andric // We don't care about users in unreachable blocks. 541*0b57cec5SDimitry Andric if (isa<Instruction>(U) && 542*0b57cec5SDimitry Andric !DT->isReachableFromEntry(cast<Instruction>(U)->getParent())) 543*0b57cec5SDimitry Andric continue; 544*0b57cec5SDimitry Andric ICmpInst *ICI = dyn_cast<ICmpInst>(U); 545*0b57cec5SDimitry Andric if (!ICI) return false; 546*0b57cec5SDimitry Andric assert(L->contains(ICI->getParent()) && "LCSSA form broken?"); 547*0b57cec5SDimitry Andric if (!(ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) && 548*0b57cec5SDimitry Andric !(ICI->getOperand(1) == TI && L->isLoopInvariant(ICI->getOperand(0)))) 549*0b57cec5SDimitry Andric return false; 550*0b57cec5SDimitry Andric // If we cannot get rid of trunc, bail. 551*0b57cec5SDimitry Andric if (ICI->isSigned() && !DoesSExtCollapse) 552*0b57cec5SDimitry Andric return false; 553*0b57cec5SDimitry Andric if (ICI->isUnsigned() && !DoesZExtCollapse) 554*0b57cec5SDimitry Andric return false; 555*0b57cec5SDimitry Andric // For equality, either signed or unsigned works. 556*0b57cec5SDimitry Andric ICmpUsers.push_back(ICI); 557*0b57cec5SDimitry Andric } 558*0b57cec5SDimitry Andric 559*0b57cec5SDimitry Andric auto CanUseZExt = [&](ICmpInst *ICI) { 560*0b57cec5SDimitry Andric // Unsigned comparison can be widened as unsigned. 561*0b57cec5SDimitry Andric if (ICI->isUnsigned()) 562*0b57cec5SDimitry Andric return true; 563*0b57cec5SDimitry Andric // Is it profitable to do zext? 564*0b57cec5SDimitry Andric if (!DoesZExtCollapse) 565*0b57cec5SDimitry Andric return false; 566*0b57cec5SDimitry Andric // For equality, we can safely zext both parts. 567*0b57cec5SDimitry Andric if (ICI->isEquality()) 568*0b57cec5SDimitry Andric return true; 569*0b57cec5SDimitry Andric // Otherwise we can only use zext when comparing two non-negative or two 570*0b57cec5SDimitry Andric // negative values. But in practice, we will never pass DoesZExtCollapse 571*0b57cec5SDimitry Andric // check for a negative value, because zext(trunc(x)) is non-negative. So 572*0b57cec5SDimitry Andric // it only make sense to check for non-negativity here. 573*0b57cec5SDimitry Andric const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0)); 574*0b57cec5SDimitry Andric const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1)); 575*0b57cec5SDimitry Andric return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2); 576*0b57cec5SDimitry Andric }; 577*0b57cec5SDimitry Andric // Replace all comparisons against trunc with comparisons against IV. 578*0b57cec5SDimitry Andric for (auto *ICI : ICmpUsers) { 579*0b57cec5SDimitry Andric bool IsSwapped = L->isLoopInvariant(ICI->getOperand(0)); 580*0b57cec5SDimitry Andric auto *Op1 = IsSwapped ? ICI->getOperand(0) : ICI->getOperand(1); 581*0b57cec5SDimitry Andric Instruction *Ext = nullptr; 582*0b57cec5SDimitry Andric // For signed/unsigned predicate, replace the old comparison with comparison 583*0b57cec5SDimitry Andric // of immediate IV against sext/zext of the invariant argument. If we can 584*0b57cec5SDimitry Andric // use either sext or zext (i.e. we are dealing with equality predicate), 585*0b57cec5SDimitry Andric // then prefer zext as a more canonical form. 586*0b57cec5SDimitry Andric // TODO: If we see a signed comparison which can be turned into unsigned, 587*0b57cec5SDimitry Andric // we can do it here for canonicalization purposes. 588*0b57cec5SDimitry Andric ICmpInst::Predicate Pred = ICI->getPredicate(); 589*0b57cec5SDimitry Andric if (IsSwapped) Pred = ICmpInst::getSwappedPredicate(Pred); 590*0b57cec5SDimitry Andric if (CanUseZExt(ICI)) { 591*0b57cec5SDimitry Andric assert(DoesZExtCollapse && "Unprofitable zext?"); 592*0b57cec5SDimitry Andric Ext = new ZExtInst(Op1, IVTy, "zext", ICI); 593*0b57cec5SDimitry Andric Pred = ICmpInst::getUnsignedPredicate(Pred); 594*0b57cec5SDimitry Andric } else { 595*0b57cec5SDimitry Andric assert(DoesSExtCollapse && "Unprofitable sext?"); 596*0b57cec5SDimitry Andric Ext = new SExtInst(Op1, IVTy, "sext", ICI); 597*0b57cec5SDimitry Andric assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!"); 598*0b57cec5SDimitry Andric } 599*0b57cec5SDimitry Andric bool Changed; 600*0b57cec5SDimitry Andric L->makeLoopInvariant(Ext, Changed); 601*0b57cec5SDimitry Andric (void)Changed; 602*0b57cec5SDimitry Andric ICmpInst *NewICI = new ICmpInst(ICI, Pred, IV, Ext); 603*0b57cec5SDimitry Andric ICI->replaceAllUsesWith(NewICI); 604*0b57cec5SDimitry Andric DeadInsts.emplace_back(ICI); 605*0b57cec5SDimitry Andric } 606*0b57cec5SDimitry Andric 607*0b57cec5SDimitry Andric // Trunc no longer needed. 608*0b57cec5SDimitry Andric TI->replaceAllUsesWith(UndefValue::get(TI->getType())); 609*0b57cec5SDimitry Andric DeadInsts.emplace_back(TI); 610*0b57cec5SDimitry Andric return true; 611*0b57cec5SDimitry Andric } 612*0b57cec5SDimitry Andric 613*0b57cec5SDimitry Andric /// Eliminate an operation that consumes a simple IV and has no observable 614*0b57cec5SDimitry Andric /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable, 615*0b57cec5SDimitry Andric /// but UseInst may not be. 616*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, 617*0b57cec5SDimitry Andric Instruction *IVOperand) { 618*0b57cec5SDimitry Andric if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { 619*0b57cec5SDimitry Andric eliminateIVComparison(ICmp, IVOperand); 620*0b57cec5SDimitry Andric return true; 621*0b57cec5SDimitry Andric } 622*0b57cec5SDimitry Andric if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) { 623*0b57cec5SDimitry Andric bool IsSRem = Bin->getOpcode() == Instruction::SRem; 624*0b57cec5SDimitry Andric if (IsSRem || Bin->getOpcode() == Instruction::URem) { 625*0b57cec5SDimitry Andric simplifyIVRemainder(Bin, IVOperand, IsSRem); 626*0b57cec5SDimitry Andric return true; 627*0b57cec5SDimitry Andric } 628*0b57cec5SDimitry Andric 629*0b57cec5SDimitry Andric if (Bin->getOpcode() == Instruction::SDiv) 630*0b57cec5SDimitry Andric return eliminateSDiv(Bin); 631*0b57cec5SDimitry Andric } 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric if (auto *WO = dyn_cast<WithOverflowInst>(UseInst)) 634*0b57cec5SDimitry Andric if (eliminateOverflowIntrinsic(WO)) 635*0b57cec5SDimitry Andric return true; 636*0b57cec5SDimitry Andric 637*0b57cec5SDimitry Andric if (auto *SI = dyn_cast<SaturatingInst>(UseInst)) 638*0b57cec5SDimitry Andric if (eliminateSaturatingIntrinsic(SI)) 639*0b57cec5SDimitry Andric return true; 640*0b57cec5SDimitry Andric 641*0b57cec5SDimitry Andric if (auto *TI = dyn_cast<TruncInst>(UseInst)) 642*0b57cec5SDimitry Andric if (eliminateTrunc(TI)) 643*0b57cec5SDimitry Andric return true; 644*0b57cec5SDimitry Andric 645*0b57cec5SDimitry Andric if (eliminateIdentitySCEV(UseInst, IVOperand)) 646*0b57cec5SDimitry Andric return true; 647*0b57cec5SDimitry Andric 648*0b57cec5SDimitry Andric return false; 649*0b57cec5SDimitry Andric } 650*0b57cec5SDimitry Andric 651*0b57cec5SDimitry Andric static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) { 652*0b57cec5SDimitry Andric if (auto *BB = L->getLoopPreheader()) 653*0b57cec5SDimitry Andric return BB->getTerminator(); 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric return Hint; 656*0b57cec5SDimitry Andric } 657*0b57cec5SDimitry Andric 658*0b57cec5SDimitry Andric /// Replace the UseInst with a constant if possible. 659*0b57cec5SDimitry Andric bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { 660*0b57cec5SDimitry Andric if (!SE->isSCEVable(I->getType())) 661*0b57cec5SDimitry Andric return false; 662*0b57cec5SDimitry Andric 663*0b57cec5SDimitry Andric // Get the symbolic expression for this instruction. 664*0b57cec5SDimitry Andric const SCEV *S = SE->getSCEV(I); 665*0b57cec5SDimitry Andric 666*0b57cec5SDimitry Andric if (!SE->isLoopInvariant(S, L)) 667*0b57cec5SDimitry Andric return false; 668*0b57cec5SDimitry Andric 669*0b57cec5SDimitry Andric // Do not generate something ridiculous even if S is loop invariant. 670*0b57cec5SDimitry Andric if (Rewriter.isHighCostExpansion(S, L, I)) 671*0b57cec5SDimitry Andric return false; 672*0b57cec5SDimitry Andric 673*0b57cec5SDimitry Andric auto *IP = GetLoopInvariantInsertPosition(L, I); 674*0b57cec5SDimitry Andric auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP); 675*0b57cec5SDimitry Andric 676*0b57cec5SDimitry Andric I->replaceAllUsesWith(Invariant); 677*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I 678*0b57cec5SDimitry Andric << " with loop invariant: " << *S << '\n'); 679*0b57cec5SDimitry Andric ++NumFoldedUser; 680*0b57cec5SDimitry Andric Changed = true; 681*0b57cec5SDimitry Andric DeadInsts.emplace_back(I); 682*0b57cec5SDimitry Andric return true; 683*0b57cec5SDimitry Andric } 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric /// Eliminate any operation that SCEV can prove is an identity function. 686*0b57cec5SDimitry Andric bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, 687*0b57cec5SDimitry Andric Instruction *IVOperand) { 688*0b57cec5SDimitry Andric if (!SE->isSCEVable(UseInst->getType()) || 689*0b57cec5SDimitry Andric (UseInst->getType() != IVOperand->getType()) || 690*0b57cec5SDimitry Andric (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) 691*0b57cec5SDimitry Andric return false; 692*0b57cec5SDimitry Andric 693*0b57cec5SDimitry Andric // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the 694*0b57cec5SDimitry Andric // dominator tree, even if X is an operand to Y. For instance, in 695*0b57cec5SDimitry Andric // 696*0b57cec5SDimitry Andric // %iv = phi i32 {0,+,1} 697*0b57cec5SDimitry Andric // br %cond, label %left, label %merge 698*0b57cec5SDimitry Andric // 699*0b57cec5SDimitry Andric // left: 700*0b57cec5SDimitry Andric // %X = add i32 %iv, 0 701*0b57cec5SDimitry Andric // br label %merge 702*0b57cec5SDimitry Andric // 703*0b57cec5SDimitry Andric // merge: 704*0b57cec5SDimitry Andric // %M = phi (%X, %iv) 705*0b57cec5SDimitry Andric // 706*0b57cec5SDimitry Andric // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and 707*0b57cec5SDimitry Andric // %M.replaceAllUsesWith(%X) would be incorrect. 708*0b57cec5SDimitry Andric 709*0b57cec5SDimitry Andric if (isa<PHINode>(UseInst)) 710*0b57cec5SDimitry Andric // If UseInst is not a PHI node then we know that IVOperand dominates 711*0b57cec5SDimitry Andric // UseInst directly from the legality of SSA. 712*0b57cec5SDimitry Andric if (!DT || !DT->dominates(IVOperand, UseInst)) 713*0b57cec5SDimitry Andric return false; 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand)) 716*0b57cec5SDimitry Andric return false; 717*0b57cec5SDimitry Andric 718*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); 719*0b57cec5SDimitry Andric 720*0b57cec5SDimitry Andric UseInst->replaceAllUsesWith(IVOperand); 721*0b57cec5SDimitry Andric ++NumElimIdentity; 722*0b57cec5SDimitry Andric Changed = true; 723*0b57cec5SDimitry Andric DeadInsts.emplace_back(UseInst); 724*0b57cec5SDimitry Andric return true; 725*0b57cec5SDimitry Andric } 726*0b57cec5SDimitry Andric 727*0b57cec5SDimitry Andric /// Annotate BO with nsw / nuw if it provably does not signed-overflow / 728*0b57cec5SDimitry Andric /// unsigned-overflow. Returns true if anything changed, false otherwise. 729*0b57cec5SDimitry Andric bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, 730*0b57cec5SDimitry Andric Value *IVOperand) { 731*0b57cec5SDimitry Andric // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`. 732*0b57cec5SDimitry Andric if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap()) 733*0b57cec5SDimitry Andric return false; 734*0b57cec5SDimitry Andric 735*0b57cec5SDimitry Andric if (BO->getOpcode() != Instruction::Add && 736*0b57cec5SDimitry Andric BO->getOpcode() != Instruction::Sub && 737*0b57cec5SDimitry Andric BO->getOpcode() != Instruction::Mul) 738*0b57cec5SDimitry Andric return false; 739*0b57cec5SDimitry Andric 740*0b57cec5SDimitry Andric const SCEV *LHS = SE->getSCEV(BO->getOperand(0)); 741*0b57cec5SDimitry Andric const SCEV *RHS = SE->getSCEV(BO->getOperand(1)); 742*0b57cec5SDimitry Andric bool Changed = false; 743*0b57cec5SDimitry Andric 744*0b57cec5SDimitry Andric if (!BO->hasNoUnsignedWrap() && 745*0b57cec5SDimitry Andric willNotOverflow(SE, BO->getOpcode(), /* Signed */ false, LHS, RHS)) { 746*0b57cec5SDimitry Andric BO->setHasNoUnsignedWrap(); 747*0b57cec5SDimitry Andric SE->forgetValue(BO); 748*0b57cec5SDimitry Andric Changed = true; 749*0b57cec5SDimitry Andric } 750*0b57cec5SDimitry Andric 751*0b57cec5SDimitry Andric if (!BO->hasNoSignedWrap() && 752*0b57cec5SDimitry Andric willNotOverflow(SE, BO->getOpcode(), /* Signed */ true, LHS, RHS)) { 753*0b57cec5SDimitry Andric BO->setHasNoSignedWrap(); 754*0b57cec5SDimitry Andric SE->forgetValue(BO); 755*0b57cec5SDimitry Andric Changed = true; 756*0b57cec5SDimitry Andric } 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric return Changed; 759*0b57cec5SDimitry Andric } 760*0b57cec5SDimitry Andric 761*0b57cec5SDimitry Andric /// Annotate the Shr in (X << IVOperand) >> C as exact using the 762*0b57cec5SDimitry Andric /// information from the IV's range. Returns true if anything changed, false 763*0b57cec5SDimitry Andric /// otherwise. 764*0b57cec5SDimitry Andric bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO, 765*0b57cec5SDimitry Andric Value *IVOperand) { 766*0b57cec5SDimitry Andric using namespace llvm::PatternMatch; 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric if (BO->getOpcode() == Instruction::Shl) { 769*0b57cec5SDimitry Andric bool Changed = false; 770*0b57cec5SDimitry Andric ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand)); 771*0b57cec5SDimitry Andric for (auto *U : BO->users()) { 772*0b57cec5SDimitry Andric const APInt *C; 773*0b57cec5SDimitry Andric if (match(U, 774*0b57cec5SDimitry Andric m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) || 775*0b57cec5SDimitry Andric match(U, 776*0b57cec5SDimitry Andric m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) { 777*0b57cec5SDimitry Andric BinaryOperator *Shr = cast<BinaryOperator>(U); 778*0b57cec5SDimitry Andric if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) { 779*0b57cec5SDimitry Andric Shr->setIsExact(true); 780*0b57cec5SDimitry Andric Changed = true; 781*0b57cec5SDimitry Andric } 782*0b57cec5SDimitry Andric } 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric return Changed; 785*0b57cec5SDimitry Andric } 786*0b57cec5SDimitry Andric 787*0b57cec5SDimitry Andric return false; 788*0b57cec5SDimitry Andric } 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric /// Add all uses of Def to the current IV's worklist. 791*0b57cec5SDimitry Andric static void pushIVUsers( 792*0b57cec5SDimitry Andric Instruction *Def, Loop *L, 793*0b57cec5SDimitry Andric SmallPtrSet<Instruction*,16> &Simplified, 794*0b57cec5SDimitry Andric SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) { 795*0b57cec5SDimitry Andric 796*0b57cec5SDimitry Andric for (User *U : Def->users()) { 797*0b57cec5SDimitry Andric Instruction *UI = cast<Instruction>(U); 798*0b57cec5SDimitry Andric 799*0b57cec5SDimitry Andric // Avoid infinite or exponential worklist processing. 800*0b57cec5SDimitry Andric // Also ensure unique worklist users. 801*0b57cec5SDimitry Andric // If Def is a LoopPhi, it may not be in the Simplified set, so check for 802*0b57cec5SDimitry Andric // self edges first. 803*0b57cec5SDimitry Andric if (UI == Def) 804*0b57cec5SDimitry Andric continue; 805*0b57cec5SDimitry Andric 806*0b57cec5SDimitry Andric // Only change the current Loop, do not change the other parts (e.g. other 807*0b57cec5SDimitry Andric // Loops). 808*0b57cec5SDimitry Andric if (!L->contains(UI)) 809*0b57cec5SDimitry Andric continue; 810*0b57cec5SDimitry Andric 811*0b57cec5SDimitry Andric // Do not push the same instruction more than once. 812*0b57cec5SDimitry Andric if (!Simplified.insert(UI).second) 813*0b57cec5SDimitry Andric continue; 814*0b57cec5SDimitry Andric 815*0b57cec5SDimitry Andric SimpleIVUsers.push_back(std::make_pair(UI, Def)); 816*0b57cec5SDimitry Andric } 817*0b57cec5SDimitry Andric } 818*0b57cec5SDimitry Andric 819*0b57cec5SDimitry Andric /// Return true if this instruction generates a simple SCEV 820*0b57cec5SDimitry Andric /// expression in terms of that IV. 821*0b57cec5SDimitry Andric /// 822*0b57cec5SDimitry Andric /// This is similar to IVUsers' isInteresting() but processes each instruction 823*0b57cec5SDimitry Andric /// non-recursively when the operand is already known to be a simpleIVUser. 824*0b57cec5SDimitry Andric /// 825*0b57cec5SDimitry Andric static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) { 826*0b57cec5SDimitry Andric if (!SE->isSCEVable(I->getType())) 827*0b57cec5SDimitry Andric return false; 828*0b57cec5SDimitry Andric 829*0b57cec5SDimitry Andric // Get the symbolic expression for this instruction. 830*0b57cec5SDimitry Andric const SCEV *S = SE->getSCEV(I); 831*0b57cec5SDimitry Andric 832*0b57cec5SDimitry Andric // Only consider affine recurrences. 833*0b57cec5SDimitry Andric const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); 834*0b57cec5SDimitry Andric if (AR && AR->getLoop() == L) 835*0b57cec5SDimitry Andric return true; 836*0b57cec5SDimitry Andric 837*0b57cec5SDimitry Andric return false; 838*0b57cec5SDimitry Andric } 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric /// Iteratively perform simplification on a worklist of users 841*0b57cec5SDimitry Andric /// of the specified induction variable. Each successive simplification may push 842*0b57cec5SDimitry Andric /// more users which may themselves be candidates for simplification. 843*0b57cec5SDimitry Andric /// 844*0b57cec5SDimitry Andric /// This algorithm does not require IVUsers analysis. Instead, it simplifies 845*0b57cec5SDimitry Andric /// instructions in-place during analysis. Rather than rewriting induction 846*0b57cec5SDimitry Andric /// variables bottom-up from their users, it transforms a chain of IVUsers 847*0b57cec5SDimitry Andric /// top-down, updating the IR only when it encounters a clear optimization 848*0b57cec5SDimitry Andric /// opportunity. 849*0b57cec5SDimitry Andric /// 850*0b57cec5SDimitry Andric /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. 851*0b57cec5SDimitry Andric /// 852*0b57cec5SDimitry Andric void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { 853*0b57cec5SDimitry Andric if (!SE->isSCEVable(CurrIV->getType())) 854*0b57cec5SDimitry Andric return; 855*0b57cec5SDimitry Andric 856*0b57cec5SDimitry Andric // Instructions processed by SimplifyIndvar for CurrIV. 857*0b57cec5SDimitry Andric SmallPtrSet<Instruction*,16> Simplified; 858*0b57cec5SDimitry Andric 859*0b57cec5SDimitry Andric // Use-def pairs if IV users waiting to be processed for CurrIV. 860*0b57cec5SDimitry Andric SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; 861*0b57cec5SDimitry Andric 862*0b57cec5SDimitry Andric // Push users of the current LoopPhi. In rare cases, pushIVUsers may be 863*0b57cec5SDimitry Andric // called multiple times for the same LoopPhi. This is the proper thing to 864*0b57cec5SDimitry Andric // do for loop header phis that use each other. 865*0b57cec5SDimitry Andric pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers); 866*0b57cec5SDimitry Andric 867*0b57cec5SDimitry Andric while (!SimpleIVUsers.empty()) { 868*0b57cec5SDimitry Andric std::pair<Instruction*, Instruction*> UseOper = 869*0b57cec5SDimitry Andric SimpleIVUsers.pop_back_val(); 870*0b57cec5SDimitry Andric Instruction *UseInst = UseOper.first; 871*0b57cec5SDimitry Andric 872*0b57cec5SDimitry Andric // If a user of the IndVar is trivially dead, we prefer just to mark it dead 873*0b57cec5SDimitry Andric // rather than try to do some complex analysis or transformation (such as 874*0b57cec5SDimitry Andric // widening) basing on it. 875*0b57cec5SDimitry Andric // TODO: Propagate TLI and pass it here to handle more cases. 876*0b57cec5SDimitry Andric if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) { 877*0b57cec5SDimitry Andric DeadInsts.emplace_back(UseInst); 878*0b57cec5SDimitry Andric continue; 879*0b57cec5SDimitry Andric } 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric // Bypass back edges to avoid extra work. 882*0b57cec5SDimitry Andric if (UseInst == CurrIV) continue; 883*0b57cec5SDimitry Andric 884*0b57cec5SDimitry Andric // Try to replace UseInst with a loop invariant before any other 885*0b57cec5SDimitry Andric // simplifications. 886*0b57cec5SDimitry Andric if (replaceIVUserWithLoopInvariant(UseInst)) 887*0b57cec5SDimitry Andric continue; 888*0b57cec5SDimitry Andric 889*0b57cec5SDimitry Andric Instruction *IVOperand = UseOper.second; 890*0b57cec5SDimitry Andric for (unsigned N = 0; IVOperand; ++N) { 891*0b57cec5SDimitry Andric assert(N <= Simplified.size() && "runaway iteration"); 892*0b57cec5SDimitry Andric 893*0b57cec5SDimitry Andric Value *NewOper = foldIVUser(UseInst, IVOperand); 894*0b57cec5SDimitry Andric if (!NewOper) 895*0b57cec5SDimitry Andric break; // done folding 896*0b57cec5SDimitry Andric IVOperand = dyn_cast<Instruction>(NewOper); 897*0b57cec5SDimitry Andric } 898*0b57cec5SDimitry Andric if (!IVOperand) 899*0b57cec5SDimitry Andric continue; 900*0b57cec5SDimitry Andric 901*0b57cec5SDimitry Andric if (eliminateIVUser(UseInst, IVOperand)) { 902*0b57cec5SDimitry Andric pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers); 903*0b57cec5SDimitry Andric continue; 904*0b57cec5SDimitry Andric } 905*0b57cec5SDimitry Andric 906*0b57cec5SDimitry Andric if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) { 907*0b57cec5SDimitry Andric if ((isa<OverflowingBinaryOperator>(BO) && 908*0b57cec5SDimitry Andric strengthenOverflowingOperation(BO, IVOperand)) || 909*0b57cec5SDimitry Andric (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) { 910*0b57cec5SDimitry Andric // re-queue uses of the now modified binary operator and fall 911*0b57cec5SDimitry Andric // through to the checks that remain. 912*0b57cec5SDimitry Andric pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers); 913*0b57cec5SDimitry Andric } 914*0b57cec5SDimitry Andric } 915*0b57cec5SDimitry Andric 916*0b57cec5SDimitry Andric CastInst *Cast = dyn_cast<CastInst>(UseInst); 917*0b57cec5SDimitry Andric if (V && Cast) { 918*0b57cec5SDimitry Andric V->visitCast(Cast); 919*0b57cec5SDimitry Andric continue; 920*0b57cec5SDimitry Andric } 921*0b57cec5SDimitry Andric if (isSimpleIVUser(UseInst, L, SE)) { 922*0b57cec5SDimitry Andric pushIVUsers(UseInst, L, Simplified, SimpleIVUsers); 923*0b57cec5SDimitry Andric } 924*0b57cec5SDimitry Andric } 925*0b57cec5SDimitry Andric } 926*0b57cec5SDimitry Andric 927*0b57cec5SDimitry Andric namespace llvm { 928*0b57cec5SDimitry Andric 929*0b57cec5SDimitry Andric void IVVisitor::anchor() { } 930*0b57cec5SDimitry Andric 931*0b57cec5SDimitry Andric /// Simplify instructions that use this induction variable 932*0b57cec5SDimitry Andric /// by using ScalarEvolution to analyze the IV's recurrence. 933*0b57cec5SDimitry Andric bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, 934*0b57cec5SDimitry Andric LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead, 935*0b57cec5SDimitry Andric SCEVExpander &Rewriter, IVVisitor *V) { 936*0b57cec5SDimitry Andric SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter, 937*0b57cec5SDimitry Andric Dead); 938*0b57cec5SDimitry Andric SIV.simplifyUsers(CurrIV, V); 939*0b57cec5SDimitry Andric return SIV.hasChanged(); 940*0b57cec5SDimitry Andric } 941*0b57cec5SDimitry Andric 942*0b57cec5SDimitry Andric /// Simplify users of induction variables within this 943*0b57cec5SDimitry Andric /// loop. This does not actually change or add IVs. 944*0b57cec5SDimitry Andric bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, 945*0b57cec5SDimitry Andric LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) { 946*0b57cec5SDimitry Andric SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars"); 947*0b57cec5SDimitry Andric #ifndef NDEBUG 948*0b57cec5SDimitry Andric Rewriter.setDebugType(DEBUG_TYPE); 949*0b57cec5SDimitry Andric #endif 950*0b57cec5SDimitry Andric bool Changed = false; 951*0b57cec5SDimitry Andric for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { 952*0b57cec5SDimitry Andric Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter); 953*0b57cec5SDimitry Andric } 954*0b57cec5SDimitry Andric return Changed; 955*0b57cec5SDimitry Andric } 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric } // namespace llvm 958