[llvm] [InstSimplify] Simplify the select with integer comparison relationship (PR #66668)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 19 01:21:23 PDT 2023
https://github.com/vfdff updated https://github.com/llvm/llvm-project/pull/66668
>From 06d8a895ab2edb29e9042331c777c66422d7d68b Mon Sep 17 00:00:00 2001
From: zhongyunde 00443407 <zhongyunde at huawei.com>
Date: Mon, 18 Sep 2023 05:07:53 -0400
Subject: [PATCH 1/2] [tests] precommit tests for ValueTracking
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1
Fixes https://github.com/llvm/llvm-project/issues/54735
---
.../Transforms/InstSimplify/select-icmp.ll | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
create mode 100755 llvm/test/Transforms/InstSimplify/select-icmp.ll
diff --git a/llvm/test/Transforms/InstSimplify/select-icmp.ll b/llvm/test/Transforms/InstSimplify/select-icmp.ll
new file mode 100755
index 000000000000000..bdc3af0b96078d4
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/select-icmp.ll
@@ -0,0 +1,36 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+; https://alive2.llvm.org/ce/z/zKX64J
+define i32 @pr54735(i32 noundef %x, i32 noundef %y) {
+; CHECK-LABEL: @pr54735(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
+; CHECK: cond.true:
+; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
+; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
+; CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i32 [[SUB]], -1
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[ABS]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: ret i32 [[COND]]
+;
+entry:
+ %cmp = icmp sgt i32 %x, %y
+ br i1 %cmp, label %cond.true, label %cond.end
+
+cond.true: ; preds = %entry
+ %sub = sub nsw i32 %x, %y
+ %add = add nsw i32 %sub, 1
+ %neg = xor i32 %sub, -1 ; sub nsw i32 0, %add
+ %abscond = icmp slt i32 %sub, -1
+ %abs = select i1 %abscond, i32 %neg, i32 %add
+ br label %cond.end
+
+cond.end: ; preds = %entry, %cond.true
+ %cond = phi i32 [ %abs, %cond.true ], [ 0, %entry ]
+ ret i32 %cond
+}
>From 343af0a2c659ceff4027ee0bdcef461158c2482c Mon Sep 17 00:00:00 2001
From: zhongyunde 00443407 <zhongyunde at huawei.com>
Date: Mon, 18 Sep 2023 05:07:53 -0400
Subject: [PATCH 2/2] [ValueTracking] Infer relationship for the select with
ICmp
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1
Fixes https://github.com/llvm/llvm-project/issues/54735
---
llvm/lib/Analysis/ValueTracking.cpp | 9 +++++++++
llvm/test/Transforms/InstSimplify/select-icmp.ll | 5 +----
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index c4153b824c37e0a..3a2fa545059a885 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8285,6 +8285,15 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
+ if (RPred == ICmpInst::ICMP_SLT) {
+ Value *X;
+ Value *Y;
+ // x-y+1 is positive when x >= y or non-positive when x < y
+ if (match(R0, m_NSWSub(m_Value(X), m_Value(Y))) && match(R1, m_AllOnes()) &&
+ areMatchingOperands(L0, L1, X, Y, AreSwappedOps))
+ return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
+ }
+
// Can we infer anything when the 0-operands match and the 1-operands are
// constants (not necessarily matching)?
const APInt *LC, *RC;
diff --git a/llvm/test/Transforms/InstSimplify/select-icmp.ll b/llvm/test/Transforms/InstSimplify/select-icmp.ll
index bdc3af0b96078d4..1271941565d9dc3 100755
--- a/llvm/test/Transforms/InstSimplify/select-icmp.ll
+++ b/llvm/test/Transforms/InstSimplify/select-icmp.ll
@@ -10,12 +10,9 @@ define i32 @pr54735(i32 noundef %x, i32 noundef %y) {
; CHECK: cond.true:
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
-; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
-; CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i32 [[SUB]], -1
-; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
; CHECK-NEXT: br label [[COND_END]]
; CHECK: cond.end:
-; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[ABS]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[ADD]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: ret i32 [[COND]]
;
entry:
More information about the llvm-commits
mailing list