[llvm] [DAGCombine] Correctly extend the constant RHS in `TargetLowering::SimplifySetCC` (PR #152862)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 9 08:25:45 PDT 2025
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/152862
In https://github.com/llvm/llvm-project/pull/150270, when the predicate is eq/ne and the trunc has only an nsw flag, the RHS is incorrectly zero-extended.
Closes https://github.com/llvm/llvm-project/issues/152630.
>From 372b28b3d248b67b7b9ff913eeca3391d01dbe79 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 9 Aug 2025 22:45:22 +0800
Subject: [PATCH 1/2] [DAGCombine] Add pre-commit tests. NFC.
---
llvm/test/CodeGen/X86/pr152630.ll | 34 +++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/pr152630.ll
diff --git a/llvm/test/CodeGen/X86/pr152630.ll b/llvm/test/CodeGen/X86/pr152630.ll
new file mode 100644
index 0000000000000..a10612c23d435
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pr152630.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
+
+define i32 @pr152630(i1 %cond) nounwind {
+; CHECK-LABEL: pr152630:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: andl $1, %edi
+; CHECK-NEXT: decl %edi
+; CHECK-NEXT: cmpl $255, %edi
+; CHECK-NEXT: je .LBB0_2
+; CHECK-NEXT: # %bb.1: # %entry
+; CHECK-NEXT: movzbl %dil, %eax
+; CHECK-NEXT: testl %eax, %eax
+; CHECK-NEXT: jne .LBB0_3
+; CHECK-NEXT: .LBB0_2: # %if.then
+; CHECK-NEXT: xorl %eax, %eax
+; CHECK-NEXT: retq
+; CHECK-NEXT: .LBB0_3: # %if.else
+; CHECK-NEXT: movl $1, %eax
+; CHECK-NEXT: retq
+entry:
+ %sel = select i1 %cond, i32 0, i32 -1
+ %conv = trunc nsw i32 %sel to i8
+ switch i8 %conv, label %if.else [
+ i8 -1, label %if.then
+ i8 0, label %if.then
+ ]
+
+if.then:
+ ret i32 0
+
+if.else:
+ ret i32 1
+}
>From b073e8711b20dc380b410d139ee93a3f2d1f9c14 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 9 Aug 2025 23:18:11 +0800
Subject: [PATCH 2/2] [DAGCombine] Correctly extend the constant RHS
---
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 9 +++++----
llvm/test/CodeGen/X86/pr152630.ll | 2 +-
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index e235d144e85ff..8fbabfa55e93f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -5125,10 +5125,11 @@ SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
!ISD::isUnsignedIntSetCC(Cond))) &&
isTypeDesirableForOp(ISD::SETCC, N0.getOperand(0).getValueType())) {
EVT NewVT = N0.getOperand(0).getValueType();
- SDValue NewConst = DAG.getConstant(ISD::isSignedIntSetCC(Cond)
- ? C1.sext(NewVT.getSizeInBits())
- : C1.zext(NewVT.getSizeInBits()),
- dl, NewVT);
+ SDValue NewConst = DAG.getConstant(
+ (N0->getFlags().hasNoSignedWrap() && !ISD::isUnsignedIntSetCC(Cond))
+ ? C1.sext(NewVT.getSizeInBits())
+ : C1.zext(NewVT.getSizeInBits()),
+ dl, NewVT);
return DAG.getSetCC(dl, VT, N0.getOperand(0), NewConst, Cond);
}
diff --git a/llvm/test/CodeGen/X86/pr152630.ll b/llvm/test/CodeGen/X86/pr152630.ll
index a10612c23d435..8fa98831c37b0 100644
--- a/llvm/test/CodeGen/X86/pr152630.ll
+++ b/llvm/test/CodeGen/X86/pr152630.ll
@@ -6,7 +6,7 @@ define i32 @pr152630(i1 %cond) nounwind {
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: andl $1, %edi
; CHECK-NEXT: decl %edi
-; CHECK-NEXT: cmpl $255, %edi
+; CHECK-NEXT: cmpl $-1, %edi
; CHECK-NEXT: je .LBB0_2
; CHECK-NEXT: # %bb.1: # %entry
; CHECK-NEXT: movzbl %dil, %eax
More information about the llvm-commits
mailing list