[llvm] Add flipped version of (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C) (PR #97973)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 7 16:26:45 PDT 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/97973
>From bb75785330b26cc0d1d892ed776fa1bc0fe5c502 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sun, 7 Jul 2024 17:51:02 -0400
Subject: [PATCH 1/3] Add flipped version of (X u< ~C) ? (X + C) : -1 -->
uadd.sat(X, C)
For some reason, LLVM is not considering X u > C, a, b to be the same as X <= C, b, a.
Move that to after the canonicalization.
---
.../InstCombine/InstCombineSelect.cpp | 24 +++++++++++++------
.../InstCombine/saturating-add-sub.ll | 12 ++++++++++
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 394dfca262e130..39827170dbf379 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -978,13 +978,6 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
ICmpInst::Predicate Pred = Cmp->getPredicate();
Value *X;
const APInt *C, *CmpC;
- if (Pred == ICmpInst::ICMP_ULT &&
- match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
- match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
- // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
- return Builder.CreateBinaryIntrinsic(
- Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C));
- }
// Match unsigned saturated add of 2 variables with an unnecessary 'not'.
// There are 8 commuted variants.
@@ -1004,6 +997,23 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
return nullptr;
+ if (Pred == ICmpInst::ICMP_ULT &&
+ match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
+ match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
+ // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
+ return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X,
+ ConstantInt::get(X->getType(), *C));
+ }
+
+ if (Pred == ICmpInst::ICMP_ULE &&
+ match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
+ match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) &&
+ *CmpC == (~*C - 1)) {
+ // (X u<= ~C - 1) ? (X + C) : -1 --> uadd.sat(X, C)
+ return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X,
+ ConstantInt::get(X->getType(), *C));
+ }
+
// Match unsigned saturated add of 2 variables with an unnecessary 'not'.
// Strictness of the comparison is irrelevant.
Value *Y;
diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index bf1568f1cd8c09..d220109ac17c05 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1395,6 +1395,18 @@ define i32 @uadd_sat(i32 %x, i32 %y) {
%r = select i1 %c, i32 -1, i32 %a
ret i32 %r
}
+
+define i32 @uadd_sat_flipped(i32 %x) {
+; CHECK-LABEL: @uadd_sat_flipped(
+; CHECK-NEXT: [[COND:%.*]] = tail call i32 @llvm.uadd.sat.i32(i32 %x, i32 9)
+; CHECK-NEXT: ret i32 [[COND]]
+;
+ %cmp = icmp uge i32 %x, -10
+ %add = add i32 %x, 9
+ %cond = select i1 %cmp, i32 -1, i32 %add
+ ret i32 %cond
+}
+
define i32 @uadd_sat_nonstrict(i32 %x, i32 %y) {
; CHECK-LABEL: @uadd_sat_nonstrict(
; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
>From 7b75797bc6fa2851a7bc2e237cf0b40974b08839 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sun, 7 Jul 2024 19:16:16 -0400
Subject: [PATCH 2/3] Revert "Add flipped version of (X u< ~C) ? (X + C) : -1
--> uadd.sat(X, C)"
This reverts commit e15693a288d47119e155c76067c4632ca92dcc11.
---
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 39827170dbf379..ad99a44d397672 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -979,14 +979,7 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
Value *X;
const APInt *C, *CmpC;
- // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
- // There are 8 commuted variants.
- // Canonicalize -1 (saturated result) to true value of the select.
- if (match(FVal, m_AllOnes())) {
- std::swap(TVal, FVal);
- Pred = CmpInst::getInversePredicate(Pred);
- }
- if (!match(TVal, m_AllOnes()))
+ if (!match(TVal, m_AllOnes()) && !match(FVal, m_AllOnes()))
return nullptr;
// Canonicalize predicate to less-than or less-or-equal-than.
>From 85e140698bc8995a74961f428e31263dd5dc23a9 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sun, 7 Jul 2024 19:18:38 -0400
Subject: [PATCH 3/3] Revert "Add flipped version of (X u< ~C) ? (X + C) : -1
--> uadd.sat(X, C)"
This reverts commit e15693a288d47119e155c76067c4632ca92dcc11.
---
.../InstCombine/InstCombineSelect.cpp | 43 +++++++++++--------
.../InstCombine/saturating-add-sub.ll | 2 +-
2 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index ad99a44d397672..e0594b24fb1e63 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -979,34 +979,41 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
Value *X;
const APInt *C, *CmpC;
- if (!match(TVal, m_AllOnes()) && !match(FVal, m_AllOnes()))
- return nullptr;
-
- // Canonicalize predicate to less-than or less-or-equal-than.
- if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
- std::swap(Cmp0, Cmp1);
- Pred = CmpInst::getSwappedPredicate(Pred);
+ // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
+ // There are 8 commuted variants.
+ // Canonicalize -1 (saturated result) to true value of the select.
+ if (match(FVal, m_AllOnes())) {
+ std::swap(TVal, FVal);
+ Pred = CmpInst::getInversePredicate(Pred);
}
- if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
+ if (!match(TVal, m_AllOnes()))
return nullptr;
- if (Pred == ICmpInst::ICMP_ULT &&
- match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
- match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
- // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
+ if (Pred == ICmpInst::ICMP_UGE &&
+ match(FVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
+ match(TVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
+ // (X u >= ~C) ? -1 : (X + C) --> uadd.sat(X, C)
return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X,
ConstantInt::get(X->getType(), *C));
}
- if (Pred == ICmpInst::ICMP_ULE &&
- match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
- match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) &&
+ if (Pred == ICmpInst::ICMP_UGT &&
+ match(FVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
+ match(TVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) &&
*CmpC == (~*C - 1)) {
- // (X u<= ~C - 1) ? (X + C) : -1 --> uadd.sat(X, C)
+ // (X u > ~C - 1) ? -1 : (X + C) --> uadd.sat(X, C)
return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X,
ConstantInt::get(X->getType(), *C));
}
+ // Canonicalize predicate to less-than or less-or-equal-than.
+ if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
+ std::swap(Cmp0, Cmp1);
+ Pred = CmpInst::getSwappedPredicate(Pred);
+ }
+ if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
+ return nullptr;
+
// Match unsigned saturated add of 2 variables with an unnecessary 'not'.
// Strictness of the comparison is irrelevant.
Value *Y;
@@ -1024,8 +1031,8 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
// (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
// (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
BinaryOperator *BO = cast<BinaryOperator>(FVal);
- return Builder.CreateBinaryIntrinsic(
- Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
+ return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, BO->getOperand(0),
+ BO->getOperand(1));
}
// The overflow may be detected via the add wrapping round.
// This is only valid for strict comparison!
diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
index d220109ac17c05..1db6277c10d023 100644
--- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1398,7 +1398,7 @@ define i32 @uadd_sat(i32 %x, i32 %y) {
define i32 @uadd_sat_flipped(i32 %x) {
; CHECK-LABEL: @uadd_sat_flipped(
-; CHECK-NEXT: [[COND:%.*]] = tail call i32 @llvm.uadd.sat.i32(i32 %x, i32 9)
+; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[X:%.*]], i32 9)
; CHECK-NEXT: ret i32 [[COND]]
;
%cmp = icmp uge i32 %x, -10
More information about the llvm-commits
mailing list