[llvm] [llvm][GVN] Propagate `trunc nuw to i1` equalities (PR #143273)

Yash Solanki via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 7 09:05:26 PDT 2025


https://github.com/yashnator created https://github.com/llvm/llvm-project/pull/143273

This patch adds to GVN's `propagateEquality()` to reason about equality constraints through `trunc nuw iN to i1`.

Given:
  %tr = trunc nuw iN %v to i1
  br i1 %tr, ...

We can deduce that if `%tr == true`, then `%v == 1`, and if `%tr == false`, then `%v == 0`. This is valid because `nuw` guarantees that truncation didn't lose unsigned bits, so `%v` must have been either 0 or 1.

The patch adds logic to propagate this information via the GVN worklist. This enables further simplification opportunities downstream, such as folding redundant stores or conditionals that depend on `%v`.

Includes a test case in `GVN/trunc-nuw-equality.ll`.


>From c0ff60e85e795257b86e32559c5745233fd9e5b7 Mon Sep 17 00:00:00 2001
From: Yash Solanki <252yash at gmail.com>
Date: Sat, 7 Jun 2025 20:26:56 +0530
Subject: [PATCH] [GVN] Optimize store under trunc nuw to i1 condition

If `trunc nuw i64 %v to i1` evaluates to false, we know `%v` is zero.
Similarly, if it evaluates to true, we know `%v` is one.

This is folded under GVN's propagateEquality.

Fixes missed optimization noted in #142744
---
 llvm/lib/Transforms/Scalar/GVN.cpp            | 16 +++++++++++++
 .../test/Transforms/GVN/trunc-nuw-equality.ll | 23 +++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 llvm/test/Transforms/GVN/trunc-nuw-equality.ll

diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 77ca14f88a834..046918d8cf6a2 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2579,6 +2579,22 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
 
       continue;
     }
+
+    // Propagate equality that result from truncation with no unsigned wrap
+    // like (trunc nuw i64 %v to i1) == "true" or (trunc nuw i64 %v to i1) ==
+    // "false"
+    if (auto *Trunc = dyn_cast<TruncInst>(LHS)) {
+      if (Trunc->hasNoUnsignedWrap() && Trunc->getType()->isIntegerTy(1)) {
+        Value *Input = Trunc->getOperand(0);
+        if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
+          if (CI->isZero()) {
+            Worklist.push_back({Input, ConstantInt::get(Input->getType(), 0)});
+          } else if (CI->isOne()) {
+            Worklist.push_back({Input, ConstantInt::get(Input->getType(), 1)});
+          }
+        }
+      }
+    }
   }
 
   return Changed;
diff --git a/llvm/test/Transforms/GVN/trunc-nuw-equality.ll b/llvm/test/Transforms/GVN/trunc-nuw-equality.ll
new file mode 100644
index 0000000000000..03d4a16aeaf20
--- /dev/null
+++ b/llvm/test/Transforms/GVN/trunc-nuw-equality.ll
@@ -0,0 +1,23 @@
+; RUN: opt -passes=gvn -S < %s | FileCheck %s
+
+define void @test(ptr %p, i64 %v) {
+; CHECK-LABEL: @test(
+; CHECK: %tr = trunc nuw i64 %v to i1
+; CHECK-NEXT: br i1 %tr, label %ret, label %store
+
+entry:
+  %tr = trunc nuw i64 %v to i1
+  br i1 %tr, label %ret, label %store
+
+store:
+  ; CHECK-LABEL: store:
+  ; CHECK: store i64 0, ptr %p
+  store i64 %v, ptr %p
+  ret void
+
+ret:
+  ; CHECK-LABEL: ret:
+  ; CHECK: store i64 1, ptr %p
+  store i64 %v, ptr %p
+  ret void
+}



More information about the llvm-commits mailing list