[llvm] [LLVM][Transforms][Attributor] - Optimize AAIsDeadFunction::isAssumedDead (PR #189467)

Pranav Bhandarkar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 13:05:56 PDT 2026


================
@@ -4606,14 +4609,43 @@ struct AAIsDeadFunction : public AAIsDead {
     if (!AssumedLiveBlocks.count(I->getParent()))
       return true;
 
-    // If it is not after a liveness barrier it is live.
-    const Instruction *PrevI = I->getPrevNode();
-    while (PrevI) {
-      if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
-        return true;
-      PrevI = PrevI->getPrevNode();
+    // We cache the *first* liveness barrier in the block. A liveness barrier
+    // is an instruction in KnownDeadEnds or ToBeExploredFrom — these are
+    // always terminators or calls that are known or assumed to not transfer
+    // control to their successor. If such an instruction exists and precedes
+    // I in the block, then I is unreachable and therefore dead.
+    // Previously, we used to do a backwards linear scan from I to
+    // the beginning of the block, checking KnownDeadEnds and ToBeExploredFrom
+    // at each step. By caching we trade complexity for storage.
+    const BasicBlock *BB = I->getParent();
+    auto It = FirstDeadInstCache.find(BB);
+    if (It == FirstDeadInstCache.end()) {
+      // Cache miss. Scan the block forward to find the first dead end.
+      const Instruction *FirstDead = nullptr;
+      for (const Instruction &Inst : *BB) {
+        if (KnownDeadEnds.count(&Inst) || ToBeExploredFrom.count(&Inst)) {
+          FirstDead = &Inst;
+          break;
+        }
+      }
----------------
bhandarkar-pranav wrote:

Nice catch! I'll move to eager population. At `KnownDeadEnds.insert(I)` and `NewToBeExploredFrom.insert(I)`, I'll update the cache if the newly inserted instruction precedes the 
currently cached barrier (or if no entry exists yet).

https://github.com/llvm/llvm-project/pull/189467


More information about the llvm-commits mailing list