[clang] 0b57d27 - [LifetimeSafety][NFC] Index the dataflow block states by block ID (#246491)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 19:44:05 PST 2026


Author: Gábor Horváth
Date: 2026-11-27T03:43:59Z
New Revision: 0b57d272e8ef857018bf726633c1ca540f91ea90

URL: https://github.com/llvm/llvm-project/commit/0b57d272e8ef857018bf726633c1ca540f91ea90
DIFF: https://github.com/llvm/llvm-project/commit/0b57d272e8ef857018bf726633c1ca540f91ea90.diff

LOG: [LifetimeSafety][NFC] Index the dataflow block states by block ID (#246491)

Added: 
    

Modified: 
    clang/lib/Analysis/LifetimeSafety/Dataflow.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/LifetimeSafety/Dataflow.h b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
index 1e96628185b65..7c232dc72cf93 100644
--- a/clang/lib/Analysis/LifetimeSafety/Dataflow.h
+++ b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
@@ -66,10 +66,12 @@ class DataflowAnalysis {
   const CFG &Cfg;
   AnalysisDeclContext ∾
 
-  /// The dataflow state before a basic block is processed.
-  llvm::DenseMap<const CFGBlock *, Lattice> InStates;
-  /// The dataflow state after a basic block is processed.
-  llvm::DenseMap<const CFGBlock *, Lattice> OutStates;
+  /// The dataflow state before each basic block is processed, indexed by block
+  /// ID. Unset for blocks that have not been reached yet.
+  llvm::SmallVector<std::optional<Lattice>> InStates;
+  /// The dataflow state after each basic block is processed, indexed by block
+  /// ID.
+  llvm::SmallVector<Lattice> OutStates;
   /// Dataflow state at each program point, indexed by Fact ID.
   /// In a forward analysis, this is the state after the Fact at that point has
   /// been applied, while in a backward analysis, it is the state before.
@@ -90,6 +92,8 @@ class DataflowAnalysis {
     llvm::TimeTraceScope Time(D.getAnalysisName());
 
     PointToState.resize(FactMgr.getNumFacts());
+    InStates.resize(Cfg.getNumBlockIDs());
+    OutStates.resize(Cfg.getNumBlockIDs());
 
     using Worklist =
         std::conditional_t<Dir == Direction::Forward, ForwardDataflowWorklist,
@@ -97,7 +101,7 @@ class DataflowAnalysis {
     Worklist W(Cfg, AC);
 
     const CFGBlock *Start = isForward() ? &Cfg.getEntry() : &Cfg.getExit();
-    InStates[Start] = D.getInitialState();
+    InStates[Start->getBlockID()] = D.getInitialState();
     W.enqueueBlock(Start);
     drainWorklist(D, W);
 
@@ -107,7 +111,7 @@ class DataflowAnalysis {
     // block analyzed in each such cycle starts from the initial state.
     if constexpr (!isForward()) {
       for (const CFGBlock *B : Cfg)
-        if (!InStates.contains(B))
+        if (!InStates[B->getBlockID()])
           W.enqueueBlock(B);
       drainWorklist(D, W);
     }
@@ -119,7 +123,7 @@ class DataflowAnalysis {
       std::optional<Lattice> StateIn = getInState(B);
       Lattice StateOut =
           transferBlock(B, StateIn ? *StateIn : D.getInitialState());
-      OutStates[B] = StateOut;
+      OutStates[B->getBlockID()] = StateOut;
       for (const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
         if (!AdjacentB)
           continue;
@@ -129,7 +133,7 @@ class DataflowAnalysis {
         // Enqueue the adjacent block if its in-state has changed or if we have
         // never seen it.
         if (!OldInState || NewInState != *OldInState) {
-          InStates[AdjacentB] = NewInState;
+          InStates[AdjacentB->getBlockID()] = NewInState;
           W.enqueueBlock(AdjacentB);
         }
       }
@@ -142,13 +146,12 @@ class DataflowAnalysis {
   }
 
   std::optional<Lattice> getInState(const CFGBlock *B) const {
-    auto It = InStates.find(B);
-    if (It == InStates.end())
-      return std::nullopt;
-    return It->second;
+    return InStates[B->getBlockID()];
   }
 
-  Lattice getOutState(const CFGBlock *B) const { return OutStates.lookup(B); }
+  Lattice getOutState(const CFGBlock *B) const {
+    return OutStates[B->getBlockID()];
+  }
 
   void dump() const {
     const Derived *D = static_cast<const Derived *>(this);


        


More information about the cfe-commits mailing list