[llvm] 897d92f - [InstCombine] generalize 2 LSB of demanded bits for X*X
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 7 08:35:45 PST 2022
Author: Sanjay Patel
Date: 2022-02-07T11:33:55-05:00
New Revision: 897d92faeff65e5fcccaeaf28cd992bec8f05a50
URL: https://github.com/llvm/llvm-project/commit/897d92faeff65e5fcccaeaf28cd992bec8f05a50
DIFF: https://github.com/llvm/llvm-project/commit/897d92faeff65e5fcccaeaf28cd992bec8f05a50.diff
LOG: [InstCombine] generalize 2 LSB of demanded bits for X*X
This is a follow-up suggested in D119060.
Instead of checking each of the bottom 2 bits individually,
we can check them together and handle the possibility that
we demand both together.
https://alive2.llvm.org/ce/z/C2ihC2
Differential Revision: https://reviews.llvm.org/D119139
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
llvm/test/Transforms/InstCombine/mul-masked-bits.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index b0b89d5d4e1de..8778df1845b35 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -546,16 +546,6 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
}
case Instruction::Mul: {
if (DemandedMask.isPowerOf2()) {
- if (I->getOperand(0) == I->getOperand(1)) {
- // X * X is odd iff X is odd.
- if (DemandedMask == 1)
- return I->getOperand(0);
-
- // 'Quadratic Reciprocity': mul(x,x) -> 0 if we're only demanding bit[1]
- if (DemandedMask == 2)
- return ConstantInt::getNullValue(VTy);
- }
-
// The LSB of X*Y is set only if (X & 1) == 1 and (Y & 1) == 1.
// If we demand exactly one bit N and we have "X * (C' << N)" where C' is
// odd (has LSB set), then the left-shifted low bit of X is the answer.
@@ -568,6 +558,15 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
return InsertNewInstWith(Shl, *I);
}
}
+ // For a squared value "X * X", the bottom 2 bits are 0 and X[0] because:
+ // X * X is odd iff X is odd.
+ // 'Quadratic Reciprocity': X * X -> 0 for bit[1]
+ if (I->getOperand(0) == I->getOperand(1) && DemandedMask.ult(4)) {
+ Constant *One = ConstantInt::get(VTy, 1);
+ Instruction *And1 = BinaryOperator::CreateAnd(I->getOperand(0), One);
+ return InsertNewInstWith(And1, *I);
+ }
+
computeKnownBits(I, Known, Depth, CxtI);
break;
}
diff --git a/llvm/test/Transforms/InstCombine/mul-masked-bits.ll b/llvm/test/Transforms/InstCombine/mul-masked-bits.ll
index 52783707eb0de..1914f9fb22fc1 100644
--- a/llvm/test/Transforms/InstCombine/mul-masked-bits.ll
+++ b/llvm/test/Transforms/InstCombine/mul-masked-bits.ll
@@ -113,8 +113,8 @@ define i67 @one_demanded_low_bit(i67 %x) {
define i33 @squared_one_demanded_low_bit(i33 %x) {
; CHECK-LABEL: @squared_one_demanded_low_bit(
-; CHECK-NEXT: [[AND:%.*]] = and i33 [[X:%.*]], 1
-; CHECK-NEXT: ret i33 [[AND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 1
+; CHECK-NEXT: ret i33 [[TMP1]]
;
%mul = mul i33 %x, %x
%and = and i33 %mul, 1
@@ -133,9 +133,8 @@ define <2 x i8> @squared_one_demanded_low_bit_splat(<2 x i8> %x) {
define i33 @squared_demanded_2_low_bits(i33 %x) {
; CHECK-LABEL: @squared_demanded_2_low_bits(
-; CHECK-NEXT: [[MUL:%.*]] = mul i33 [[X:%.*]], [[X]]
-; CHECK-NEXT: [[AND:%.*]] = and i33 [[MUL]], 3
-; CHECK-NEXT: ret i33 [[AND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 1
+; CHECK-NEXT: ret i33 [[TMP1]]
;
%mul = mul i33 %x, %x
%and = and i33 %mul, 3
@@ -144,11 +143,24 @@ define i33 @squared_demanded_2_low_bits(i33 %x) {
define <2 x i8> @squared_demanded_2_low_bits_splat(<2 x i8> %x) {
; CHECK-LABEL: @squared_demanded_2_low_bits_splat(
-; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i8> [[X:%.*]], [[X]]
-; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[MUL]], <i8 -4, i8 -4>
+; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 1, i8 1>
+; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[TMP1]], <i8 -4, i8 -4>
; CHECK-NEXT: ret <2 x i8> [[AND]]
;
%mul = mul <2 x i8> %x, %x
%and = or <2 x i8> %mul, <i8 252, i8 252>
ret <2 x i8> %and
}
+
+; negative test
+
+define i33 @squared_demanded_3_low_bits(i33 %x) {
+; CHECK-LABEL: @squared_demanded_3_low_bits(
+; CHECK-NEXT: [[MUL:%.*]] = mul i33 [[X:%.*]], [[X]]
+; CHECK-NEXT: [[AND:%.*]] = and i33 [[MUL]], 7
+; CHECK-NEXT: ret i33 [[AND]]
+;
+ %mul = mul i33 %x, %x
+ %and = and i33 %mul, 7
+ ret i33 %and
+}
More information about the llvm-commits
mailing list