[llvm] fdba1dc - [InstCombine] reduce code for shl-of-sub transform; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 27 12:01:43 PDT 2021


Author: Sanjay Patel
Date: 2021-09-27T14:56:01-04:00
New Revision: fdba1dccbe650de0f5eb9467b4cf61929f4557a2

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

LOG: [InstCombine] reduce code for shl-of-sub transform; NFC

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 c59c21428c69..da650ec2f718 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -963,17 +963,13 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
         break;
       }
       }
+    }
 
-      // If the operand is a subtract with a constant LHS, and the shift
-      // is the only use, we can pull it out of the shift.
-      // This folds (shl (sub C1, X), C) -> (sub (C1 << C), (shl X, C))
-      if (Op0BO->getOpcode() == Instruction::Sub &&
-          match(Op0BO->getOperand(0), m_APInt(C1))) {
-        Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C));
-        Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
-        NewShift->takeName(Op0BO);
-        return BinaryOperator::CreateSub(NewLHS, NewShift);
-      }
+    // (C1 - X) << C --> (C1 << C) - (X << C)
+    if (match(Op0, m_OneUse(m_Sub(m_APInt(C1), m_Value(X))))) {
+      Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C));
+      Value *NewShift = Builder.CreateShl(X, Op1);
+      return BinaryOperator::CreateSub(NewLHS, NewShift);
     }
 
     // If the shifted-out value is known-zero, then this is a NUW shift.


        


More information about the llvm-commits mailing list