[llvm] 8051c1d - [AST] Don't use WeakVH for unknown insts (NFCI)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 15 07:46:16 PST 2022


Author: Nikita Popov
Date: 2022-11-15T16:46:05+01:00
New Revision: 8051c1db956615980770d50b2fb7aea2093372fa

URL: https://github.com/llvm/llvm-project/commit/8051c1db956615980770d50b2fb7aea2093372fa
DIFF: https://github.com/llvm/llvm-project/commit/8051c1db956615980770d50b2fb7aea2093372fa.diff

LOG: [AST] Don't use WeakVH for unknown insts (NFCI)

After D138014 we do not support using AST with IR that is being
mutated. As such, we also no longer need to track unknown
instructions using WeakVH. Replace with AssertingVH to make sure
that they are not invalidated.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/AliasSetTracker.h
    llvm/lib/Analysis/AliasSetTracker.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/AliasSetTracker.h b/llvm/include/llvm/Analysis/AliasSetTracker.h
index 15f00a2341a65..25733fd4198ff 100644
--- a/llvm/include/llvm/Analysis/AliasSetTracker.h
+++ b/llvm/include/llvm/Analysis/AliasSetTracker.h
@@ -143,10 +143,7 @@ class AliasSet : public ilist_node<AliasSet> {
   AliasSet *Forward = nullptr;
 
   /// All instructions without a specific address in this alias set.
-  /// In rare cases this vector can have a null'ed out WeakVH
-  /// instances (can happen if some other loop pass deletes an
-  /// instruction in this list).
-  std::vector<WeakVH> UnknownInsts;
+  std::vector<AssertingVH<Instruction>> UnknownInsts;
 
   /// Number of nodes pointing to this AliasSet plus the number of AliasSets
   /// forwarding to it.
@@ -191,11 +188,6 @@ class AliasSet : public ilist_node<AliasSet> {
       removeFromTracker(AST);
   }
 
-  Instruction *getUnknownInst(unsigned i) const {
-    assert(i < UnknownInsts.size());
-    return cast_or_null<Instruction>(UnknownInsts[i]);
-  }
-
 public:
   AliasSet(const AliasSet &) = delete;
   AliasSet &operator=(const AliasSet &) = delete;
@@ -297,18 +289,6 @@ class AliasSet : public ilist_node<AliasSet> {
                   bool SkipSizeUpdate = false);
   void addUnknownInst(Instruction *I, AAResults &AA);
 
-  void removeUnknownInst(AliasSetTracker &AST, Instruction *I) {
-    bool WasEmpty = UnknownInsts.empty();
-    for (size_t i = 0, e = UnknownInsts.size(); i != e; ++i)
-      if (UnknownInsts[i] == I) {
-        UnknownInsts[i] = UnknownInsts.back();
-        UnknownInsts.pop_back();
-        --i; --e;  // Revisit the moved entry.
-      }
-    if (!WasEmpty && UnknownInsts.empty())
-      dropRef(AST);
-  }
-
 public:
   /// If the specified pointer "may" (or must) alias one of the members in the
   /// set return the appropriate AliasResult. Otherwise return NoAlias.

diff  --git a/llvm/lib/Analysis/AliasSetTracker.cpp b/llvm/lib/Analysis/AliasSetTracker.cpp
index 6523d6a9cde91..dcc45ef265371 100644
--- a/llvm/lib/Analysis/AliasSetTracker.cpp
+++ b/llvm/lib/Analysis/AliasSetTracker.cpp
@@ -216,11 +216,10 @@ AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
 
   // Check the unknown instructions...
   if (!UnknownInsts.empty()) {
-    for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
-      if (auto *Inst = getUnknownInst(i))
-        if (isModOrRefSet(
-                AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
-          return AliasResult::MayAlias;
+    for (Instruction *Inst : UnknownInsts)
+      if (isModOrRefSet(
+              AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
+        return AliasResult::MayAlias;
   }
 
   return AliasResult::NoAlias;
@@ -235,14 +234,12 @@ bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
   if (!Inst->mayReadOrWriteMemory())
     return false;
 
-  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
-    if (auto *UnknownInst = getUnknownInst(i)) {
-      const auto *C1 = dyn_cast<CallBase>(UnknownInst);
-      const auto *C2 = dyn_cast<CallBase>(Inst);
-      if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
-          isModOrRefSet(AA.getModRefInfo(C2, C1)))
-        return true;
-    }
+  for (Instruction *UnknownInst : UnknownInsts) {
+    const auto *C1 = dyn_cast<CallBase>(UnknownInst);
+    const auto *C2 = dyn_cast<CallBase>(Inst);
+    if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
+        isModOrRefSet(AA.getModRefInfo(C2, C1)))
+      return true;
   }
 
   for (iterator I = begin(), E = end(); I != E; ++I)
@@ -497,9 +494,8 @@ void AliasSetTracker::add(const AliasSetTracker &AST) {
       continue; // Ignore forwarding alias sets
 
     // If there are any call sites in the alias set, add them to this AST.
-    for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
-      if (auto *Inst = AS.getUnknownInst(i))
-        add(Inst);
+    for (Instruction *Inst : AS.UnknownInsts)
+      add(Inst);
 
     // Loop over all of the pointers in this alias set.
     for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
@@ -592,15 +588,14 @@ void AliasSet::print(raw_ostream &OS) const {
     }
   }
   if (!UnknownInsts.empty()) {
+    ListSeparator LS;
     OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
-    for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
-      if (i) OS << ", ";
-      if (auto *I = getUnknownInst(i)) {
-        if (I->hasName())
-          I->printAsOperand(OS);
-        else
-          I->print(OS);
-      }
+    for (Instruction *I : UnknownInsts) {
+      OS << LS;
+      if (I->hasName())
+        I->printAsOperand(OS);
+      else
+        I->print(OS);
     }
   }
   OS << "\n";


        


More information about the llvm-commits mailing list