[llvm] 442cb88 - [InstCombine] Generalize sdiv exact X, 1<<C --> ashr exact X, C fold to handle non-splat vectors

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 13:45:31 PDT 2020


Author: Roman Lebedev
Date: 2020-08-06T23:37:15+03:00
New Revision: 442cb88f5344560e49fab681a9c909654d85fcc7

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

LOG: [InstCombine] Generalize  sdiv exact X, 1<<C  -->  ashr exact X, C  fold to handle non-splat vectors

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 172b304b7762..cd6f5c7096fc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1130,14 +1130,13 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
   if (match(Op1, m_SignMask()))
     return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType());
 
+  // sdiv exact X,  1<<C  -->    ashr exact X, C   iff  1<<C  is non-negative
+  if (I.isExact() && match(Op1, m_Power2()) && match(Op1, m_NonNegative()))
+    return BinaryOperator::CreateExactAShr(
+        Op0, getLogBase2(I.getType(), cast<Constant>(Op1)), I.getName());
+
   const APInt *Op1C;
   if (match(Op1, m_APInt(Op1C))) {
-    // sdiv exact X, C  -->  ashr exact X, log2(C)
-    if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) {
-      Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2());
-      return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
-    }
-
     // If the dividend is sign-extended and the constant divisor is small enough
     // to fit in the source type, shrink the division to the narrower type:
     // (sext X) sdiv C --> sext (X sdiv C)

diff  --git a/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll b/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
index f8bcff913902..40e8c9634700 100644
--- a/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
+++ b/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
@@ -44,7 +44,7 @@ define <2 x i8> @t3_vec_splat(<2 x i8> %x) {
 
 define <2 x i8> @t4_vec(<2 x i8> %x) {
 ; CHECK-LABEL: @t4_vec(
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 32, i8 16>
+; CHECK-NEXT:    [[DIV:%.*]] = ashr exact <2 x i8> [[X:%.*]], <i8 5, i8 4>
 ; CHECK-NEXT:    ret <2 x i8> [[DIV]]
 ;
   %div = sdiv exact <2 x i8> %x, <i8 32, i8 16>


        


More information about the llvm-commits mailing list