[llvm] [Semilattice] Introduce for dataflow analysis with KnownBits (PR #177616)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 11:27:36 PST 2026


================
@@ -0,0 +1,184 @@
+//===- Semilattice.h - A semilattice of KnownBits -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// Builds a semilattice structure from the integral values in a given function,
+// and holds an associated KnownBits for each value, representing the dataflow
+// of KnownBits. Intended to be used to cache KnownBits for the entire function,
+// with invalidation APIs.
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_SEMILATTICE_H
+#define LLVM_ANALYSIS_SEMILATTICE_H
+
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/GraphTraits.h"
+#include "llvm/ADT/PointerIntPair.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/KnownBits.h"
+
+namespace llvm {
+class Semilattice;
+
+class SemilatticeNode {
+  friend class Semilattice;
+  PointerIntPair<Value *, 1> ValHasKnownBits;
+  KnownBits Known;
+  SmallVector<SemilatticeNode *, 4> Parents;
----------------
artagnon wrote:

Yes, a Value could have multiple users, and these users could have uses of different values:

```llvm
void @test(i32 %a, i32 %b) {
   %foo = add i32 %a, %b
   ret i32 %foo
}
```

Here, the roots are a and b (ignoring the sentinel root), and foo is a child leaf value of both of them. foo's parents are both a and b.

Not sure what you mean by using the def-use chain to invalidate: we'd still have to look up each of the nodes in the Map and invalidate the KnownBits inside each of the nodes; instead, we store the nodes directly, for a simple graph traversal. 

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


More information about the llvm-commits mailing list