10b57cec5SDimitry Andric //===- IVUsers.cpp - Induction Variable Users -------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements bookkeeping for "interesting" users of expressions
100b57cec5SDimitry Andric // computed from induction variables.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Analysis/IVUsers.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
160b57cec5SDimitry Andric #include "llvm/Analysis/CodeMetrics.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/LoopAnalysisManager.h"
1881ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h"
200b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionExpressions.h"
210b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
220b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
230b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
240b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
250b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
260b57cec5SDimitry Andric #include "llvm/IR/Module.h"
27480093f4SDimitry Andric #include "llvm/InitializePasses.h"
280b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define DEBUG_TYPE "iv-users"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric AnalysisKey IVUsersAnalysis::Key;
350b57cec5SDimitry Andric 
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR)360b57cec5SDimitry Andric IVUsers IVUsersAnalysis::run(Loop &L, LoopAnalysisManager &AM,
370b57cec5SDimitry Andric                              LoopStandardAnalysisResults &AR) {
380b57cec5SDimitry Andric   return IVUsers(&L, &AR.AC, &AR.LI, &AR.DT, &AR.SE);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric char IVUsersWrapperPass::ID = 0;
420b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(IVUsersWrapperPass, "iv-users",
430b57cec5SDimitry Andric                       "Induction Variable Users", false, true)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)440b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
450b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
460b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
470b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
480b57cec5SDimitry Andric INITIALIZE_PASS_END(IVUsersWrapperPass, "iv-users", "Induction Variable Users",
490b57cec5SDimitry Andric                     false, true)
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric Pass *llvm::createIVUsersPass() { return new IVUsersWrapperPass(); }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric /// isInteresting - Test whether the given expression is "interesting" when
540b57cec5SDimitry Andric /// used by the given expression, within the context of analyzing the
550b57cec5SDimitry Andric /// given loop.
isInteresting(const SCEV * S,const Instruction * I,const Loop * L,ScalarEvolution * SE,LoopInfo * LI)560b57cec5SDimitry Andric static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
570b57cec5SDimitry Andric                           ScalarEvolution *SE, LoopInfo *LI) {
580b57cec5SDimitry Andric   // An addrec is interesting if it's affine or if it has an interesting start.
590b57cec5SDimitry Andric   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
600b57cec5SDimitry Andric     // Keep things simple. Don't touch loop-variant strides unless they're
610b57cec5SDimitry Andric     // only used outside the loop and we can simplify them.
620b57cec5SDimitry Andric     if (AR->getLoop() == L)
630b57cec5SDimitry Andric       return AR->isAffine() ||
640b57cec5SDimitry Andric              (!L->contains(I) &&
650b57cec5SDimitry Andric               SE->getSCEVAtScope(AR, LI->getLoopFor(I->getParent())) != AR);
660b57cec5SDimitry Andric     // Otherwise recurse to see if the start value is interesting, and that
670b57cec5SDimitry Andric     // the step value is not interesting, since we don't yet know how to
680b57cec5SDimitry Andric     // do effective SCEV expansions for addrecs with interesting steps.
690b57cec5SDimitry Andric     return isInteresting(AR->getStart(), I, L, SE, LI) &&
700b57cec5SDimitry Andric           !isInteresting(AR->getStepRecurrence(*SE), I, L, SE, LI);
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // An add is interesting if exactly one of its operands is interesting.
740b57cec5SDimitry Andric   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
750b57cec5SDimitry Andric     bool AnyInterestingYet = false;
760b57cec5SDimitry Andric     for (const auto *Op : Add->operands())
770b57cec5SDimitry Andric       if (isInteresting(Op, I, L, SE, LI)) {
780b57cec5SDimitry Andric         if (AnyInterestingYet)
790b57cec5SDimitry Andric           return false;
800b57cec5SDimitry Andric         AnyInterestingYet = true;
810b57cec5SDimitry Andric       }
820b57cec5SDimitry Andric     return AnyInterestingYet;
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Nothing else is interesting here.
860b57cec5SDimitry Andric   return false;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
900b57cec5SDimitry Andric /// and now we need to decide whether the user should use the preinc or post-inc
910b57cec5SDimitry Andric /// value.  If this user should use the post-inc version of the IV, return true.
920b57cec5SDimitry Andric ///
930b57cec5SDimitry Andric /// Choosing wrong here can break dominance properties (if we choose to use the
940b57cec5SDimitry Andric /// post-inc value when we cannot) or it can end up adding extra live-ranges to
950b57cec5SDimitry Andric /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
960b57cec5SDimitry Andric /// should use the post-inc value).
IVUseShouldUsePostIncValue(Instruction * User,Value * Operand,const Loop * L,DominatorTree * DT)970b57cec5SDimitry Andric static bool IVUseShouldUsePostIncValue(Instruction *User, Value *Operand,
980b57cec5SDimitry Andric                                        const Loop *L, DominatorTree *DT) {
990b57cec5SDimitry Andric   // If the user is in the loop, use the preinc value.
1000b57cec5SDimitry Andric   if (L->contains(User))
1010b57cec5SDimitry Andric     return false;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   BasicBlock *LatchBlock = L->getLoopLatch();
1040b57cec5SDimitry Andric   if (!LatchBlock)
1050b57cec5SDimitry Andric     return false;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   // Ok, the user is outside of the loop.  If it is dominated by the latch
1080b57cec5SDimitry Andric   // block, use the post-inc value.
1090b57cec5SDimitry Andric   if (DT->dominates(LatchBlock, User->getParent()))
1100b57cec5SDimitry Andric     return true;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // There is one case we have to be careful of: PHI nodes.  These little guys
1130b57cec5SDimitry Andric   // can live in blocks that are not dominated by the latch block, but (since
1140b57cec5SDimitry Andric   // their uses occur in the predecessor block, not the block the PHI lives in)
1150b57cec5SDimitry Andric   // should still use the post-inc value.  Check for this case now.
1160b57cec5SDimitry Andric   PHINode *PN = dyn_cast<PHINode>(User);
1170b57cec5SDimitry Andric   if (!PN || !Operand)
1180b57cec5SDimitry Andric     return false; // not a phi, not dominated by latch block.
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // Look at all of the uses of Operand by the PHI node.  If any use corresponds
1210b57cec5SDimitry Andric   // to a block that is not dominated by the latch block, give up and use the
1220b57cec5SDimitry Andric   // preincremented value.
1230b57cec5SDimitry Andric   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1240b57cec5SDimitry Andric     if (PN->getIncomingValue(i) == Operand &&
1250b57cec5SDimitry Andric         !DT->dominates(LatchBlock, PN->getIncomingBlock(i)))
1260b57cec5SDimitry Andric       return false;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   // Okay, all uses of Operand by PN are in predecessor blocks that really are
1290b57cec5SDimitry Andric   // dominated by the latch block.  Use the post-incremented value.
1300b57cec5SDimitry Andric   return true;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
133349cc55cSDimitry Andric /// Inspect the specified instruction.  If it is a reducible SCEV, recursively
134349cc55cSDimitry Andric /// add its users to the IVUsesByStride set and return true.  Otherwise, return
135349cc55cSDimitry Andric /// false.
AddUsersIfInteresting(Instruction * I)136349cc55cSDimitry Andric bool IVUsers::AddUsersIfInteresting(Instruction *I) {
1370b57cec5SDimitry Andric   const DataLayout &DL = I->getModule()->getDataLayout();
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   // Add this IV user to the Processed set before returning false to ensure that
1400b57cec5SDimitry Andric   // all IV users are members of the set. See IVUsers::isIVUserOrOperand.
1410b57cec5SDimitry Andric   if (!Processed.insert(I).second)
1420b57cec5SDimitry Andric     return true;    // Instruction already handled.
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   if (!SE->isSCEVable(I->getType()))
1450b57cec5SDimitry Andric     return false;   // Void and FP expressions cannot be reduced.
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   // IVUsers is used by LSR which assumes that all SCEV expressions are safe to
1480b57cec5SDimitry Andric   // pass to SCEVExpander. Expressions are not safe to expand if they represent
1490b57cec5SDimitry Andric   // operations that are not safe to speculate, namely integer division.
1500b57cec5SDimitry Andric   if (!isa<PHINode>(I) && !isSafeToSpeculativelyExecute(I))
1510b57cec5SDimitry Andric     return false;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // LSR is not APInt clean, do not touch integers bigger than 64-bits.
1540b57cec5SDimitry Andric   // Also avoid creating IVs of non-native types. For example, we don't want a
1550b57cec5SDimitry Andric   // 64-bit IV in 32-bit code just because the loop has one 64-bit cast.
1560b57cec5SDimitry Andric   uint64_t Width = SE->getTypeSizeInBits(I->getType());
1570b57cec5SDimitry Andric   if (Width > 64 || !DL.isLegalInteger(Width))
1580b57cec5SDimitry Andric     return false;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Don't attempt to promote ephemeral values to indvars. They will be removed
1610b57cec5SDimitry Andric   // later anyway.
1620b57cec5SDimitry Andric   if (EphValues.count(I))
1630b57cec5SDimitry Andric     return false;
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   // Get the symbolic expression for this instruction.
1660b57cec5SDimitry Andric   const SCEV *ISE = SE->getSCEV(I);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   // If we've come to an uninteresting expression, stop the traversal and
1690b57cec5SDimitry Andric   // call this a user.
1700b57cec5SDimitry Andric   if (!isInteresting(ISE, I, L, SE, LI))
1710b57cec5SDimitry Andric     return false;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   SmallPtrSet<Instruction *, 4> UniqueUsers;
1740b57cec5SDimitry Andric   for (Use &U : I->uses()) {
1750b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(U.getUser());
1760b57cec5SDimitry Andric     if (!UniqueUsers.insert(User).second)
1770b57cec5SDimitry Andric       continue;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric     // Do not infinitely recurse on PHI nodes.
1800b57cec5SDimitry Andric     if (isa<PHINode>(User) && Processed.count(User))
1810b57cec5SDimitry Andric       continue;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric     // Descend recursively, but not into PHI nodes outside the current loop.
1840b57cec5SDimitry Andric     // It's important to see the entire expression outside the loop to get
1850b57cec5SDimitry Andric     // choices that depend on addressing mode use right, although we won't
1860b57cec5SDimitry Andric     // consider references outside the loop in all cases.
1870b57cec5SDimitry Andric     // If User is already in Processed, we don't want to recurse into it again,
1880b57cec5SDimitry Andric     // but do want to record a second reference in the same instruction.
1890b57cec5SDimitry Andric     bool AddUserToIVUsers = false;
1900b57cec5SDimitry Andric     if (LI->getLoopFor(User->getParent()) != L) {
1910b57cec5SDimitry Andric       if (isa<PHINode>(User) || Processed.count(User) ||
192349cc55cSDimitry Andric           !AddUsersIfInteresting(User)) {
1930b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
1940b57cec5SDimitry Andric                           << "   OF SCEV: " << *ISE << '\n');
1950b57cec5SDimitry Andric         AddUserToIVUsers = true;
1960b57cec5SDimitry Andric       }
197349cc55cSDimitry Andric     } else if (Processed.count(User) || !AddUsersIfInteresting(User)) {
1980b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
1990b57cec5SDimitry Andric                         << "   OF SCEV: " << *ISE << '\n');
2000b57cec5SDimitry Andric       AddUserToIVUsers = true;
2010b57cec5SDimitry Andric     }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     if (AddUserToIVUsers) {
2040b57cec5SDimitry Andric       // Okay, we found a user that we cannot reduce.
2050b57cec5SDimitry Andric       IVStrideUse &NewUse = AddUser(User, I);
2060b57cec5SDimitry Andric       // Autodetect the post-inc loop set, populating NewUse.PostIncLoops.
2070b57cec5SDimitry Andric       // The regular return value here is discarded; instead of recording
2080b57cec5SDimitry Andric       // it, we just recompute it when we need it.
2090b57cec5SDimitry Andric       const SCEV *OriginalISE = ISE;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric       auto NormalizePred = [&](const SCEVAddRecExpr *AR) {
2120b57cec5SDimitry Andric         auto *L = AR->getLoop();
2130b57cec5SDimitry Andric         bool Result = IVUseShouldUsePostIncValue(User, I, L, DT);
2140b57cec5SDimitry Andric         if (Result)
2150b57cec5SDimitry Andric           NewUse.PostIncLoops.insert(L);
2160b57cec5SDimitry Andric         return Result;
2170b57cec5SDimitry Andric       };
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric       ISE = normalizeForPostIncUseIf(ISE, NormalizePred, *SE);
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric       // PostIncNormalization effectively simplifies the expression under
2220b57cec5SDimitry Andric       // pre-increment assumptions. Those assumptions (no wrapping) might not
2230b57cec5SDimitry Andric       // hold for the post-inc value. Catch such cases by making sure the
2240b57cec5SDimitry Andric       // transformation is invertible.
2250b57cec5SDimitry Andric       if (OriginalISE != ISE) {
2260b57cec5SDimitry Andric         const SCEV *DenormalizedISE =
2270b57cec5SDimitry Andric             denormalizeForPostIncUse(ISE, NewUse.PostIncLoops, *SE);
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric         // If we normalized the expression, but denormalization doesn't give the
2300b57cec5SDimitry Andric         // original one, discard this user.
2310b57cec5SDimitry Andric         if (OriginalISE != DenormalizedISE) {
2320b57cec5SDimitry Andric           LLVM_DEBUG(dbgs()
2330b57cec5SDimitry Andric                      << "   DISCARDING (NORMALIZATION ISN'T INVERTIBLE): "
2340b57cec5SDimitry Andric                      << *ISE << '\n');
2350b57cec5SDimitry Andric           IVUses.pop_back();
2360b57cec5SDimitry Andric           return false;
2370b57cec5SDimitry Andric         }
2380b57cec5SDimitry Andric       }
2390b57cec5SDimitry Andric       LLVM_DEBUG(if (SE->getSCEV(I) != ISE) dbgs()
2400b57cec5SDimitry Andric                  << "   NORMALIZED TO: " << *ISE << '\n');
2410b57cec5SDimitry Andric     }
2420b57cec5SDimitry Andric   }
2430b57cec5SDimitry Andric   return true;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
AddUser(Instruction * User,Value * Operand)2460b57cec5SDimitry Andric IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
2470b57cec5SDimitry Andric   IVUses.push_back(new IVStrideUse(this, User, Operand));
2480b57cec5SDimitry Andric   return IVUses.back();
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
IVUsers(Loop * L,AssumptionCache * AC,LoopInfo * LI,DominatorTree * DT,ScalarEvolution * SE)2510b57cec5SDimitry Andric IVUsers::IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
2520b57cec5SDimitry Andric                  ScalarEvolution *SE)
25304eeddc0SDimitry Andric     : L(L), AC(AC), LI(LI), DT(DT), SE(SE) {
2540b57cec5SDimitry Andric   // Collect ephemeral values so that AddUsersIfInteresting skips them.
2550b57cec5SDimitry Andric   EphValues.clear();
2560b57cec5SDimitry Andric   CodeMetrics::collectEphemeralValues(L, AC, EphValues);
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   // Find all uses of induction variables in this loop, and categorize
2590b57cec5SDimitry Andric   // them by stride.  Start by finding all of the PHI nodes in the header for
2600b57cec5SDimitry Andric   // this loop.  If they are induction variables, inspect their uses.
2610b57cec5SDimitry Andric   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
2620b57cec5SDimitry Andric     (void)AddUsersIfInteresting(&*I);
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
print(raw_ostream & OS,const Module * M) const2650b57cec5SDimitry Andric void IVUsers::print(raw_ostream &OS, const Module *M) const {
2660b57cec5SDimitry Andric   OS << "IV Users for loop ";
2670b57cec5SDimitry Andric   L->getHeader()->printAsOperand(OS, false);
2680b57cec5SDimitry Andric   if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
2690b57cec5SDimitry Andric     OS << " with backedge-taken count " << *SE->getBackedgeTakenCount(L);
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric   OS << ":\n";
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   for (const IVStrideUse &IVUse : IVUses) {
2740b57cec5SDimitry Andric     OS << "  ";
2750b57cec5SDimitry Andric     IVUse.getOperandValToReplace()->printAsOperand(OS, false);
2760b57cec5SDimitry Andric     OS << " = " << *getReplacementExpr(IVUse);
277fcaf7f86SDimitry Andric     for (const auto *PostIncLoop : IVUse.PostIncLoops) {
2780b57cec5SDimitry Andric       OS << " (post-inc with loop ";
2790b57cec5SDimitry Andric       PostIncLoop->getHeader()->printAsOperand(OS, false);
2800b57cec5SDimitry Andric       OS << ")";
2810b57cec5SDimitry Andric     }
2820b57cec5SDimitry Andric     OS << " in  ";
2830b57cec5SDimitry Andric     if (IVUse.getUser())
2840b57cec5SDimitry Andric       IVUse.getUser()->print(OS);
2850b57cec5SDimitry Andric     else
2860b57cec5SDimitry Andric       OS << "Printing <null> User";
2870b57cec5SDimitry Andric     OS << '\n';
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const2920b57cec5SDimitry Andric LLVM_DUMP_METHOD void IVUsers::dump() const { print(dbgs()); }
2930b57cec5SDimitry Andric #endif
2940b57cec5SDimitry Andric 
releaseMemory()2950b57cec5SDimitry Andric void IVUsers::releaseMemory() {
2960b57cec5SDimitry Andric   Processed.clear();
2970b57cec5SDimitry Andric   IVUses.clear();
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
IVUsersWrapperPass()3000b57cec5SDimitry Andric IVUsersWrapperPass::IVUsersWrapperPass() : LoopPass(ID) {
3010b57cec5SDimitry Andric   initializeIVUsersWrapperPassPass(*PassRegistry::getPassRegistry());
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const3040b57cec5SDimitry Andric void IVUsersWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
3050b57cec5SDimitry Andric   AU.addRequired<AssumptionCacheTracker>();
3060b57cec5SDimitry Andric   AU.addRequired<LoopInfoWrapperPass>();
3070b57cec5SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
3080b57cec5SDimitry Andric   AU.addRequired<ScalarEvolutionWrapperPass>();
3090b57cec5SDimitry Andric   AU.setPreservesAll();
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
runOnLoop(Loop * L,LPPassManager & LPM)3120b57cec5SDimitry Andric bool IVUsersWrapperPass::runOnLoop(Loop *L, LPPassManager &LPM) {
3130b57cec5SDimitry Andric   auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
3140b57cec5SDimitry Andric       *L->getHeader()->getParent());
3150b57cec5SDimitry Andric   auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
3160b57cec5SDimitry Andric   auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
3170b57cec5SDimitry Andric   auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   IU.reset(new IVUsers(L, AC, LI, DT, SE));
3200b57cec5SDimitry Andric   return false;
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
print(raw_ostream & OS,const Module * M) const3230b57cec5SDimitry Andric void IVUsersWrapperPass::print(raw_ostream &OS, const Module *M) const {
3240b57cec5SDimitry Andric   IU->print(OS, M);
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
releaseMemory()3270b57cec5SDimitry Andric void IVUsersWrapperPass::releaseMemory() { IU->releaseMemory(); }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric /// getReplacementExpr - Return a SCEV expression which computes the
3300b57cec5SDimitry Andric /// value of the OperandValToReplace.
getReplacementExpr(const IVStrideUse & IU) const3310b57cec5SDimitry Andric const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
3320b57cec5SDimitry Andric   return SE->getSCEV(IU.getOperandValToReplace());
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric /// getExpr - Return the expression for the use.
getExpr(const IVStrideUse & IU) const3360b57cec5SDimitry Andric const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
33706c3fb27SDimitry Andric   const SCEV *Replacement = getReplacementExpr(IU);
33806c3fb27SDimitry Andric   return normalizeForPostIncUse(Replacement, IU.getPostIncLoops(), *SE);
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric 
findAddRecForLoop(const SCEV * S,const Loop * L)3410b57cec5SDimitry Andric static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
3420b57cec5SDimitry Andric   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
3430b57cec5SDimitry Andric     if (AR->getLoop() == L)
3440b57cec5SDimitry Andric       return AR;
3450b57cec5SDimitry Andric     return findAddRecForLoop(AR->getStart(), L);
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3490b57cec5SDimitry Andric     for (const auto *Op : Add->operands())
3500b57cec5SDimitry Andric       if (const SCEVAddRecExpr *AR = findAddRecForLoop(Op, L))
3510b57cec5SDimitry Andric         return AR;
3520b57cec5SDimitry Andric     return nullptr;
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   return nullptr;
3560b57cec5SDimitry Andric }
3570b57cec5SDimitry Andric 
getStride(const IVStrideUse & IU,const Loop * L) const3580b57cec5SDimitry Andric const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
35906c3fb27SDimitry Andric   const SCEV *Expr = getExpr(IU);
36006c3fb27SDimitry Andric   if (!Expr)
36106c3fb27SDimitry Andric     return nullptr;
36206c3fb27SDimitry Andric   if (const SCEVAddRecExpr *AR = findAddRecForLoop(Expr, L))
3630b57cec5SDimitry Andric     return AR->getStepRecurrence(*SE);
3640b57cec5SDimitry Andric   return nullptr;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
transformToPostInc(const Loop * L)3670b57cec5SDimitry Andric void IVStrideUse::transformToPostInc(const Loop *L) {
3680b57cec5SDimitry Andric   PostIncLoops.insert(L);
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
deleted()3710b57cec5SDimitry Andric void IVStrideUse::deleted() {
3720b57cec5SDimitry Andric   // Remove this user from the list.
3730b57cec5SDimitry Andric   Parent->Processed.erase(this->getUser());
3740b57cec5SDimitry Andric   Parent->IVUses.erase(this);
3750b57cec5SDimitry Andric   // this now dangles!
3760b57cec5SDimitry Andric }
377