diff --git a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h --- a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h +++ b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h @@ -92,7 +92,7 @@ private: struct DecomposedGEP; - /// Tracks phi nodes we have visited. + /// Tracks whether the accesses may be on different cycle iterations. /// /// When interpret "Value" pointer equality as value equality we need to make /// sure that the "Value" is not part of a cycle. Otherwise, two uses could @@ -106,7 +106,7 @@ /// %addr2 = gep %alloca2, 0, (%l + 1) /// alias(%p, %addr1) -> MayAlias ! /// store %l, ... - SmallPtrSet VisitedPhiBBs; + bool MayBeCrossIteration = false; /// Tracks instructions visited by pointsToConstantMemory. SmallPtrSet Visited; diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -54,6 +54,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/KnownBits.h" +#include "llvm/Support/SaveAndRestore.h" #include #include #include @@ -1252,12 +1253,12 @@ } else if (DecompGEP1.VarIndices.size() == 2) { // VarIndex = Scale*V0 + (-Scale)*V1. // If V0 != V1 then abs(VarIndex) >= abs(Scale). - // Check that VisitedPhiBBs is empty, to avoid reasoning about + // Check that MayBeCrossIteration is false, to avoid reasoning about // inequality of values across loop iterations. const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0]; const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1]; if (Var0.Scale == -Var1.Scale && Var0.Val.TruncBits == 0 && - Var0.Val.hasSameCastsAs(Var1.Val) && VisitedPhiBBs.empty() && + Var0.Val.hasSameCastsAs(Var1.Val) && !MayBeCrossIteration && isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr, DT)) MinAbsVarIndex = Var0.Scale.abs(); @@ -1432,19 +1433,14 @@ PNSize = LocationSize::beforeOrAfterPointer(); // In the recursive alias queries below, we may compare values from two - // different loop iterations. Keep track of visited phi blocks, which will - // be used when determining value equivalence. - bool BlockInserted = VisitedPhiBBs.insert(PN->getParent()).second; - auto _ = make_scope_exit([&]() { - if (BlockInserted) - VisitedPhiBBs.erase(PN->getParent()); - }); - - // If we inserted a block into VisitedPhiBBs, alias analysis results that + // different loop iterations. + SaveAndRestore SavedMayBeCrossIteration(MayBeCrossIteration, true); + + // If we enabled the MayBeCrossIteration flag, alias analysis results that // have been cached earlier may no longer be valid. Perform recursive queries // with a new AAQueryInfo. AAQueryInfo NewAAQI = AAQI.withEmptyCache(); - AAQueryInfo *UseAAQI = BlockInserted ? &NewAAQI : &AAQI; + AAQueryInfo *UseAAQI = !SavedMayBeCrossIteration.get() ? &NewAAQI : &AAQI; AliasResult Alias = AAQI.AAR.alias(MemoryLocation(V1Srcs[0], PNSize), MemoryLocation(V2, V2Size), *UseAAQI); @@ -1690,34 +1686,29 @@ /// Check whether two Values can be considered equivalent. /// -/// In addition to pointer equivalence of \p V1 and \p V2 this checks whether -/// they can not be part of a cycle in the value graph by looking at all -/// visited phi nodes an making sure that the phis cannot reach the value. We -/// have to do this because we are looking through phi nodes (That is we say +/// If the values may come from different cycle iterations, this will also +/// check that the values are not part of cycle. We have to do this because we +/// are looking through phi nodes, that is we say /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB). bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V, const Value *V2) { if (V != V2) return false; - const Instruction *Inst = dyn_cast(V); - if (!Inst) + if (!MayBeCrossIteration) return true; - if (VisitedPhiBBs.empty()) + // Non-instructions and instructions in the entry block cannot be part of + // a loop. + const Instruction *Inst = dyn_cast(V); + if (!Inst || Inst->getParent()->isEntryBlock()) return true; - if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck) - return false; - - // Make sure that the visited phis cannot reach the Value. This ensures that - // the Values cannot come from different iterations of a potential cycle the - // phi nodes could be involved in. - for (const auto *P : VisitedPhiBBs) - if (isPotentiallyReachable(&P->front(), Inst, nullptr, DT)) - return false; - - return true; + // Check whether the instruction is part of a cycle, by checking whether the + // block can (non-trivially) reach itself. + BasicBlock *BB = const_cast(Inst->getParent()); + SmallVector Succs(successors(BB)); + return !isPotentiallyReachableFromMany(Succs, BB, nullptr, DT); } /// Computes the symbolic difference between two de-composed GEPs.