[llvm] 8960ba7 - Revert "[InstCombine] C0 <<{nsw, nuw} (X - C1) --> (C0 >> C1) << X"

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 21 09:15:33 PDT 2022


Author: Sanjay Patel
Date: 2022-04-21T12:15:27-04:00
New Revision: 8960ba7491e8551f28bbc7a177e8cfb8404513b0

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

LOG: Revert "[InstCombine] C0 <<{nsw, nuw} (X - C1) --> (C0 >> C1) << X"

This reverts commit 5819f4a422865fc9a8ea4dc772769e14010ff6a7.
This caused bots to fail with a crash/assert during the fold,
so some constraint was missed.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
    llvm/test/Transforms/InstCombine/shift-add.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 73684495e8bf3..89558ee06745c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -988,23 +988,6 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
     return BinaryOperator::CreateLShr(
         ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
 
-  // Try to pre-shift a constant shifted by a variable amount:
-  // C << (X + AddC) --> (C >> -AddC) << X
-  // This requires a no-wrap flag and negative offset constant.
-  const APInt *AddC;
-  if ((I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
-      match(Op0, m_APInt(C)) &&
-      match(Op1, m_Add(m_Value(X), m_APInt(AddC))) && AddC->isNegative()) {
-    assert(!C->isZero() && "Expected simplify of shifted zero");
-    unsigned PosOffset = (-*AddC).getLimitedValue();
-    if (C->eq(C->lshr(PosOffset).shl(PosOffset))) {
-      Constant *NewC = ConstantInt::get(Ty, C->lshr(PosOffset));
-      Instruction *NewShl = BinaryOperator::CreateShl(NewC, X);
-      NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
-      return NewShl;
-    }
-  }
-
   return nullptr;
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/shift-add.ll b/llvm/test/Transforms/InstCombine/shift-add.ll
index 6cf5451274185..141551978377e 100644
--- a/llvm/test/Transforms/InstCombine/shift-add.ll
+++ b/llvm/test/Transforms/InstCombine/shift-add.ll
@@ -174,7 +174,8 @@ define i32 @shl_add_nsw(i32 %x) {
 
 define i32 @shl_nsw_add_negative(i32 %x) {
 ; CHECK-LABEL: @shl_nsw_add_negative(
-; CHECK-NEXT:    [[R:%.*]] = shl i32 1, [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], -1
+; CHECK-NEXT:    [[R:%.*]] = shl nsw i32 2, [[A]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %a = add i32 %x, -1
@@ -182,14 +183,11 @@ define i32 @shl_nsw_add_negative(i32 %x) {
   ret i32 %r
 }
 
-; vectors and extra uses are allowed
-; nuw propagates to the new shift
-
 define <2 x i8> @shl_nuw_add_negative_splat_uses(<2 x i8> %x, <2 x i8>* %p) {
 ; CHECK-LABEL: @shl_nuw_add_negative_splat_uses(
 ; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -2, i8 -2>
 ; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
-; CHECK-NEXT:    [[R:%.*]] = shl nuw <2 x i8> <i8 3, i8 3>, [[X]]
+; CHECK-NEXT:    [[R:%.*]] = shl nuw <2 x i8> <i8 12, i8 12>, [[A]]
 ; CHECK-NEXT:    ret <2 x i8> [[R]]
 ;
   %a = add <2 x i8> %x, <i8 -2, i8 -2>
@@ -198,8 +196,6 @@ define <2 x i8> @shl_nuw_add_negative_splat_uses(<2 x i8> %x, <2 x i8>* %p) {
   ret <2 x i8> %r
 }
 
-; negative test - shift constant must have enough trailing zeros to allow the pre-shift
-
 define i32 @shl_nsw_add_negative_invalid_constant(i32 %x) {
 ; CHECK-LABEL: @shl_nsw_add_negative_invalid_constant(
 ; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], -2
@@ -211,8 +207,6 @@ define i32 @shl_nsw_add_negative_invalid_constant(i32 %x) {
   ret i32 %r
 }
 
-; negative test - the offset constant must be negative
-
 define i32 @shl_nsw_add_positive_invalid_constant(i32 %x) {
 ; CHECK-LABEL: @shl_nsw_add_positive_invalid_constant(
 ; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], 2


        


More information about the llvm-commits mailing list