Skip to content

[llvm][GVN] Propagate trunc nuw to i1 equalities #143273

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/Transforms/GVN/trunc-nuw-equality.ll
Original file line number Diff line number Diff line change
@@ -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
}