[llvm] bf09a92 - [InstCombine] remove likely redundant ValueTracking-based folds for shifts

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 20 08:28:43 PDT 2022


Author: Sanjay Patel
Date: 2022-04-20T11:28:31-04:00
New Revision: bf09a925f21ad04bcd541dd2dbf51bc880ea813d

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

LOG: [InstCombine] remove likely redundant ValueTracking-based folds for shifts

This is not expected to have a functional difference as discussed in the
post-commit comments for 8a9c70fc01e6. All of the motivating tests for
the older fold still optimize as expected because other code can infer
the 'nuw'.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index cc4d53d27f031..69ace4d0f4041 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -399,16 +399,15 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
           reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
     return NewShift;
 
-  // C0 shift (A add C) -> (C0 shift C) shift A
-  // iff A and C2 are both positive or the add has 'nuw'.
+  // Pre-shift a constant shifted by a variable amount with constant offset:
+  // C shift (A add nuw C1) --> (C shift C1) shift A
   Value *A;
-  Constant *C;
-  if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
-    if (cast<BinaryOperator>(Op1)->hasNoUnsignedWrap() ||
-        (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
-         isKnownNonNegative(C, DL, 0, &AC, &I, &DT)))
-      return BinaryOperator::Create(
-          I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
+  Constant *C, *C1;
+  if (match(Op0, m_Constant(C)) &&
+      match(Op1, m_NUWAdd(m_Value(A), m_Constant(C1)))) {
+    Constant *NewC = ConstantExpr::get(I.getOpcode(), C, C1);
+    return BinaryOperator::Create(I.getOpcode(), NewC, A);
+  }
 
   // X shift (A srem C) -> X shift (A and (C - 1)) iff C is a power of 2.
   // Because shifts by negative values (which could occur if A were negative)


        


More information about the llvm-commits mailing list