[llvm] 79b3fe8 - [InstCombine] SimplifyDemandedBits - mul(x, x) is odd iff x is odd

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 7 05:53:35 PST 2022


Author: Sanjay Patel
Date: 2022-02-07T08:43:12-05:00
New Revision: 79b3fe80707b2eb9a38c1517a829fb58062fb687

URL: https://github.com/llvm/llvm-project/commit/79b3fe80707b2eb9a38c1517a829fb58062fb687
DIFF: https://github.com/llvm/llvm-project/commit/79b3fe80707b2eb9a38c1517a829fb58062fb687.diff

LOG: [InstCombine] SimplifyDemandedBits - mul(x,x) is odd iff x is odd

https://alive2.llvm.org/ce/z/AXPr3k

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 d1640a02334b2..b0b89d5d4e1de 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -545,10 +545,20 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
     break;
   }
   case Instruction::Mul: {
-    // 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.
     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.
       unsigned CTZ = DemandedMask.countTrailingZeros();
       const APInt *C;
       if (match(I->getOperand(1), m_APInt(C)) &&
@@ -557,9 +567,6 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
         Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC);
         return InsertNewInstWith(Shl, *I);
       }
-      // 'Quadratic Reciprocity': mul(x,x) -> 0 if we're only demanding bit[1]
-      if (DemandedMask == 2 && I->getOperand(0) == I->getOperand(1))
-        return ConstantInt::getNullValue(VTy);
     }
     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 30c4e9141aa15..d375b0a008133 100644
--- a/llvm/test/Transforms/InstCombine/mul-masked-bits.ll
+++ b/llvm/test/Transforms/InstCombine/mul-masked-bits.ll
@@ -113,9 +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:    [[MUL:%.*]] = mul i33 [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[AND:%.*]] = and i33 [[MUL]], 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
@@ -124,8 +123,7 @@ define i33 @squared_one_demanded_low_bit(i33 %x) {
 
 define <2 x i8> @squared_one_demanded_low_bit_splat(<2 x i8> %x) {
 ; CHECK-LABEL: @squared_one_demanded_low_bit_splat(
-; CHECK-NEXT:    [[MUL:%.*]] = mul <2 x i8> [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[AND:%.*]] = or <2 x i8> [[MUL]], <i8 -2, i8 -2>
+; CHECK-NEXT:    [[AND:%.*]] = or <2 x i8> [[X:%.*]], <i8 -2, i8 -2>
 ; CHECK-NEXT:    ret <2 x i8> [[AND]]
 ;
   %mul = mul <2 x i8> %x, %x


        


More information about the llvm-commits mailing list