diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp index 3e1adaf84f6f..30de50dc3c88 100644 --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -14,6 +14,7 @@ #include "llvm/Analysis/BasicAliasAnalysis.h" #include "llvm/ADT/APInt.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" @@ -814,7 +815,7 @@ AliasResult BasicAAResult::alias(const MemoryLocation &LocA, AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags, LocB.Ptr, LocB.Size, LocB.AATags, AAQI); - VisitedPhiBBs.clear(); + assert(VisitedPhiBBs.empty()); return Alias; } @@ -1654,7 +1655,11 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize, // 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. - VisitedPhiBBs.insert(PN->getParent()); + auto Pair = VisitedPhiBBs.insert(PN->getParent()); + auto _ = make_scope_exit([&]() { + if (Pair.second) + VisitedPhiBBs.erase(PN->getParent()); + }); AliasResult Alias = aliasCheck(V2, V2Size, V2AAInfo, V1Srcs[0], PNSize, PNAAInfo, AAQI, UnderV2); diff --git a/llvm/test/Analysis/BasicAA/phi-aa.ll b/llvm/test/Analysis/BasicAA/phi-aa.ll index 299dc01e7e90..aa2e078000a7 100644 --- a/llvm/test/Analysis/BasicAA/phi-aa.ll +++ b/llvm/test/Analysis/BasicAA/phi-aa.ll @@ -116,7 +116,7 @@ end: } ; CHECK-LABEL: phi_and_select -; CHECK: MayAlias: i32* %p, i32* %s +; CHECK: MustAlias: i32* %p, i32* %s define void @phi_and_select(i1 %c, i1 %c2, i32* %x, i32* %y) { entry: br i1 %c, label %true, label %false