-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[KnownBits] Add KnownBits::add and KnownBits::sub helper wrappers. #99468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-globalisel @llvm/pr-subscribers-llvm-support Author: Simon Pilgrim (RKSimon) ChangesAssumes NSW = NUW = false Simplifies uses in callbacks by avoiding the need for a custom KnownBits::computeForAddSub wrapper Full diff: https://github.com/llvm/llvm-project/pull/99468.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 7ed3d525bd8fb..c49b0395b268b 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -329,6 +329,19 @@ struct KnownBits {
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
const KnownBits &Borrow);
+ /// Compute knownbits resulting from addition of LHS and RHS.
+ /// NSW and NUW flags are assumed to be false.
+ static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) {
+ return computeForAddSub(/*Add=*/true, /*NSW=*/false, /*NUW=*/false, LHS,
+ RHS);
+ }
+ /// Compute knownbits resulting from subtraction of LHS and RHS.
+ /// NSW and NUW flags are assumed to be false.
+ static KnownBits sub(const KnownBits& LHS, const KnownBits& RHS) {
+ return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
+ RHS);
+ }
+
/// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 535a248a5f1a2..64386f6c11c08 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1800,13 +1800,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
case Intrinsic::x86_ssse3_phadd_w_128:
case Intrinsic::x86_avx2_phadd_d:
case Intrinsic::x86_avx2_phadd_w: {
- Known = computeKnownBitsForHorizontalOperation(
- I, DemandedElts, Depth, Q,
- [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
- /*NUW=*/false, KnownLHS,
- KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+ Q, KnownBits::add);
break;
}
case Intrinsic::x86_ssse3_phadd_sw_128:
@@ -1819,13 +1814,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
case Intrinsic::x86_ssse3_phsub_w_128:
case Intrinsic::x86_avx2_phsub_d:
case Intrinsic::x86_avx2_phsub_w: {
- Known = computeKnownBitsForHorizontalOperation(
- I, DemandedElts, Depth, Q,
- [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
- /*NUW=*/false, KnownLHS,
- KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+ Q, KnownBits::sub);
break;
}
case Intrinsic::x86_ssse3_phsub_sw_128:
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 881e06e5f78b4..16d32d5fe040c 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -37511,15 +37511,14 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
}
break;
}
- case X86ISD::HADD:
+ case X86ISD::HADD: {
+ Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+ KnownBits::add);
+ break;
+ }
case X86ISD::HSUB: {
- Known = computeKnownBitsForHorizontalOperation(
- Op, DemandedElts, Depth, DAG,
- [Opc](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(
- /*Add=*/Opc == X86ISD::HADD, /*NSW=*/false, /*NUW=*/false,
- KnownLHS, KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+ KnownBits::sub);
break;
}
case ISD::INTRINSIC_WO_CHAIN: {
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 84882550117d8..9d2fcf77b29cc 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -300,6 +300,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
return Known1 ^ Known2;
},
[](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
+ testBinaryOpExhaustive(
+ "add", KnownBits::add,
+ [](const APInt &N1, const APInt &N2) { return N1 + N2; },
+ /*CheckOptimality=*/false);
+ testBinaryOpExhaustive(
+ "sub", KnownBits::sub,
+ [](const APInt &N1, const APInt &N2) { return N1 - N2; },
+ /*CheckOptimality=*/false);
testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);
|
@llvm/pr-subscribers-llvm-analysis Author: Simon Pilgrim (RKSimon) ChangesAssumes NSW = NUW = false Simplifies uses in callbacks by avoiding the need for a custom KnownBits::computeForAddSub wrapper Full diff: https://github.com/llvm/llvm-project/pull/99468.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 7ed3d525bd8fb..c49b0395b268b 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -329,6 +329,19 @@ struct KnownBits {
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
const KnownBits &Borrow);
+ /// Compute knownbits resulting from addition of LHS and RHS.
+ /// NSW and NUW flags are assumed to be false.
+ static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) {
+ return computeForAddSub(/*Add=*/true, /*NSW=*/false, /*NUW=*/false, LHS,
+ RHS);
+ }
+ /// Compute knownbits resulting from subtraction of LHS and RHS.
+ /// NSW and NUW flags are assumed to be false.
+ static KnownBits sub(const KnownBits& LHS, const KnownBits& RHS) {
+ return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
+ RHS);
+ }
+
/// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 535a248a5f1a2..64386f6c11c08 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1800,13 +1800,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
case Intrinsic::x86_ssse3_phadd_w_128:
case Intrinsic::x86_avx2_phadd_d:
case Intrinsic::x86_avx2_phadd_w: {
- Known = computeKnownBitsForHorizontalOperation(
- I, DemandedElts, Depth, Q,
- [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
- /*NUW=*/false, KnownLHS,
- KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+ Q, KnownBits::add);
break;
}
case Intrinsic::x86_ssse3_phadd_sw_128:
@@ -1819,13 +1814,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
case Intrinsic::x86_ssse3_phsub_w_128:
case Intrinsic::x86_avx2_phsub_d:
case Intrinsic::x86_avx2_phsub_w: {
- Known = computeKnownBitsForHorizontalOperation(
- I, DemandedElts, Depth, Q,
- [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
- /*NUW=*/false, KnownLHS,
- KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+ Q, KnownBits::sub);
break;
}
case Intrinsic::x86_ssse3_phsub_sw_128:
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 881e06e5f78b4..16d32d5fe040c 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -37511,15 +37511,14 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
}
break;
}
- case X86ISD::HADD:
+ case X86ISD::HADD: {
+ Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+ KnownBits::add);
+ break;
+ }
case X86ISD::HSUB: {
- Known = computeKnownBitsForHorizontalOperation(
- Op, DemandedElts, Depth, DAG,
- [Opc](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
- return KnownBits::computeForAddSub(
- /*Add=*/Opc == X86ISD::HADD, /*NSW=*/false, /*NUW=*/false,
- KnownLHS, KnownRHS);
- });
+ Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+ KnownBits::sub);
break;
}
case ISD::INTRINSIC_WO_CHAIN: {
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 84882550117d8..9d2fcf77b29cc 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -300,6 +300,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
return Known1 ^ Known2;
},
[](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
+ testBinaryOpExhaustive(
+ "add", KnownBits::add,
+ [](const APInt &N1, const APInt &N2) { return N1 + N2; },
+ /*CheckOptimality=*/false);
+ testBinaryOpExhaustive(
+ "sub", KnownBits::sub,
+ [](const APInt &N1, const APInt &N2) { return N1 - N2; },
+ /*CheckOptimality=*/false);
testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
@@ -329,6 +329,19 @@ struct KnownBits { | |||
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, | |||
const KnownBits &Borrow); | |||
|
|||
/// Compute knownbits resulting from addition of LHS and RHS. | |||
/// NSW and NUW flags are assumed to be false. | |||
static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not call it operator+
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly because I loathe operators for this kind of stuff :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we already use them for &
, |
and ^
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*cough* and we shouldn't *cough*
I'm opposed to no option for Can you add them as default false params? |
3339ffe
to
9cc7de8
Compare
I've added NSW/NUW arguments that default to false - although this means we can't easily use these in llvm::function_ref callbacks anymore |
You could add a wrapper |
LGTM |
No description provided.