[llvm] 73a351e - [llvm][GVN] Propagate `trunc nuw to i1` equalities (#143273)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 8 09:58:19 PDT 2025


Author: Yash Solanki
Date: 2025-06-08T18:58:16+02:00
New Revision: 73a351e7d42c1da46a915c7edb7e6697a9dea640

URL: https://github.com/llvm/llvm-project/commit/73a351e7d42c1da46a915c7edb7e6697a9dea640
DIFF: https://github.com/llvm/llvm-project/commit/73a351e7d42c1da46a915c7edb7e6697a9dea640.diff

LOG: [llvm][GVN] Propagate `trunc nuw to i1` equalities (#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

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`.

Resolves #142744

Added: 
    llvm/test/Transforms/GVN/trunc-nuw-equality.ll

Modified: 
    llvm/lib/Transforms/Scalar/GVN.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 77ca14f88a834..a0eed31fde792 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2579,6 +2579,14 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
 
       continue;
     }
+
+    // Propagate equalities that results from truncation with no unsigned wrap
+    // like (trunc nuw i64 %v to i1) == "true" or (trunc nuw i64 %v to i1) ==
+    // "false"
+    if (match(LHS, m_NUWTrunc(m_Value(A)))) {
+      Worklist.emplace_back(A, ConstantInt::get(A->getType(), IsKnownTrue));
+      continue;
+    }
   }
 
   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..efe2ca3932031
--- /dev/null
+++ b/llvm/test/Transforms/GVN/trunc-nuw-equality.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=gvn -S < %s | FileCheck %s
+
+define void @test(ptr %p, i64 %v) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: ptr [[P:%.*]], i64 [[V:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TR:%.*]] = trunc nuw i64 [[V]] to i1
+; CHECK-NEXT:    br i1 [[TR]], label %[[RET:.*]], label %[[STORE:.*]]
+; CHECK:       [[STORE]]:
+; CHECK-NEXT:    store i64 0, ptr [[P]], align 4
+; CHECK-NEXT:    ret void
+; CHECK:       [[RET]]:
+; CHECK-NEXT:    store i64 1, ptr [[P]], align 4
+; CHECK-NEXT:    ret void
+;
+entry:
+  %tr = trunc nuw i64 %v to i1
+  br i1 %tr, label %ret, label %store
+
+store:
+  store i64 %v, ptr %p
+  ret void
+
+ret:
+  store i64 %v, ptr %p
+  ret void
+}


        


More information about the llvm-commits mailing list