[llvm] [GVN] add reverse leader map to improve compile time (PR #175870)

Akshay Deodhar via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 14 14:06:15 PST 2026


================
@@ -276,6 +276,37 @@ class GVNPass : public PassInfoMixin<GVNPass> {
     DenseMap<uint32_t, LeaderListNode> NumToLeaders;
     BumpPtrAllocator TableAllocator;
 
+#ifndef NDEBUG
+    // For a given value, a list of its basic blocks and leader numbers
+    class ReverseMap {
+      DenseMap<Value *, DenseMap<const BasicBlock *, DenseSet<uint32_t>>> Map;
+
+    public:
+      void add(uint32_t N, Value *V, const BasicBlock *BB) {
+        Map[V][BB].insert(N);
+      }
+
+      void del(uint32_t N, Value *V, const BasicBlock *BB) {
+        if (auto It = Map.find(V); It != Map.end()) {
+          if (auto BBIt = It->second.find(BB); BBIt != It->second.end()) {
+            BBIt->second.erase(N);
+            if (BBIt->second.empty())
+              It->second.erase(BB);
+            if (It->second.empty())
+              Map.erase(V);
+          }
+        }
+      }
+
+      bool has(const Value *V) const { return Map.contains(V); }
----------------
akshayrdeodhar wrote:

"has" is the only API that is being _used_, everything else is just tracking. If we can guarantee that del() will be called if and only if a (N => (BB, V)) entry exists, then can we get away with just maintaining a count of the number of entries corresponding to a value, instead of tracking the exact entries? 

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


More information about the llvm-commits mailing list