[llvm] f791ad7 - [InstCombine] remove scalar constraint for mask-of-add fold

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 17 09:14:10 PST 2020


Author: Sanjay Patel
Date: 2020-11-17T12:13:45-05:00
New Revision: f791ad7e1e9850a6dc28747ad3d8b21b9e382c31

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

LOG: [InstCombine] remove scalar constraint for mask-of-add fold

https://rise4fun.com/Alive/V6fP

  Name: add with low mask
  Pre: (C1 & (-1 u>> countLeadingZeros(C2))) == 0
  %a = add i8 %x, C1
  %r = and i8 %a, C2
  =>
  %r = and i8 %x, C2

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/test/Transforms/InstCombine/and.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 78562fce5e32..e9f9f4dcb535 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -119,20 +119,12 @@ Instruction *InstCombinerImpl::OptAndOp(BinaryOperator *Op, ConstantInt *AndRHS,
                                         BinaryOperator &TheAnd) {
   Value *X;
   const APInt *C;
-  if (!match(Op, m_Add(m_Value(X), m_APInt(C))))
+  if (!match(Op, m_OneUse(m_Add(m_Value(X), m_APInt(C)))))
     return nullptr;
 
-  // If we are adding zeros to every bit below a mask, the add has no effect:
-  // (X + HighC) & LowMaskC --> X & LowMaskC
-  const APInt &AndRHSV = AndRHS->getValue();
-  unsigned Ctlz = AndRHSV.countLeadingZeros();
-  unsigned BitWidth = AndRHSV.getBitWidth();
-  APInt LowMask(APInt::getLowBitsSet(BitWidth, BitWidth - Ctlz));
-  if ((*C & LowMask).isNullValue())
-    return BinaryOperator::CreateAnd(X, TheAnd.getOperand(1));
-
   // If there is only one bit set.
-  if (AndRHSV.isPowerOf2() && Op->hasOneUse()) {
+  const APInt &AndRHSV = AndRHS->getValue();
+  if (AndRHSV.isPowerOf2()) {
     // Ok, at this point, we know that we are masking the result of the
     // ADD down to exactly one bit.  If the constant we are adding has
     // no bits set below this bit, then we can eliminate the ADD.
@@ -1807,9 +1799,10 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
         return BinaryOperator::Create(BinOp, NewLHS, Y);
       }
     }
+
+    unsigned Width = Ty->getScalarSizeInBits();
     const APInt *ShiftC;
     if (match(Op0, m_OneUse(m_SExt(m_AShr(m_Value(X), m_APInt(ShiftC)))))) {
-      unsigned Width = Ty->getScalarSizeInBits();
       if (*C == APInt::getLowBitsSet(Width, Width - ShiftC->getZExtValue())) {
         // We are clearing high bits that were potentially set by sext+ashr:
         // and (sext (ashr X, ShiftC)), C --> lshr (sext X), ShiftC
@@ -1818,6 +1811,16 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
         return BinaryOperator::CreateLShr(Sext, ShAmtC);
       }
     }
+
+    const APInt *AddC;
+    if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
+      // If we add zeros to every bit below a mask, the add has no effect:
+      // (X + AddC) & LowMaskC --> X & LowMaskC
+      unsigned Ctlz = C->countLeadingZeros();
+      APInt LowMask(APInt::getLowBitsSet(Width, Width - Ctlz));
+      if ((*AddC & LowMask).isNullValue())
+        return BinaryOperator::CreateAnd(X, Op1);
+    }
   }
 
   ConstantInt *AndRHS;

diff  --git a/llvm/test/Transforms/InstCombine/and.ll b/llvm/test/Transforms/InstCombine/and.ll
index ec45b5183e70..415ce217396c 100644
--- a/llvm/test/Transforms/InstCombine/and.ll
+++ b/llvm/test/Transforms/InstCombine/and.ll
@@ -1089,7 +1089,7 @@ define <2 x i8> @lowmask_add_2_splat(<2 x i8> %x, <2 x i8>* %p) {
 ; CHECK-LABEL: @lowmask_add_2_splat(
 ; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -64, i8 -64>
 ; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
-; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[A]], <i8 63, i8 63>
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[X]], <i8 63, i8 63>
 ; CHECK-NEXT:    ret <2 x i8> [[R]]
 ;
   %a = add <2 x i8> %x, <i8 -64, i8 -64> ; 0xc0
@@ -1128,7 +1128,7 @@ define <2 x i8> @lowmask_add_splat(<2 x i8> %x, <2 x i8>* %p) {
 ; CHECK-LABEL: @lowmask_add_splat(
 ; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -64, i8 -64>
 ; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
-; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[A]], <i8 32, i8 32>
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[X]], <i8 32, i8 32>
 ; CHECK-NEXT:    ret <2 x i8> [[R]]
 ;
   %a = add <2 x i8> %x, <i8 -64, i8 -64> ; 0xc0


        


More information about the llvm-commits mailing list