[llvm] 6b869be - [InstCombine] fold udiv with hidden common factor

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 08:45:01 PDT 2022


Author: Sanjay Patel
Date: 2022-10-06T11:35:27-04:00
New Revision: 6b869be8100d6c1b93bdaff1dc96a80a903c8fc7

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

LOG: [InstCombine] fold udiv with hidden common factor

(X * Y) u/ (X << Z) --> Y u>> Z

https://alive2.llvm.org/ce/z/4G9D_W

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/test/Transforms/InstCombine/div-shift.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index ab1cfe99e706..2267e00fee24 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -962,6 +962,22 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
     }
   }
 
+  // With appropriate no-wrap constraints, remove a common factor in the
+  // dividend and divisor that is disguised as a left-shift.
+  if (match(Op1, m_Shl(m_Value(X), m_Value(Z))) &&
+      match(Op0, m_c_Mul(m_Specific(X), m_Value(Y)))) {
+    // Both operands must have the matching no-wrap for this kind of division.
+    auto *OBO0 = cast<OverflowingBinaryOperator>(Op0);
+    auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
+    bool HasNUW = OBO0->hasNoUnsignedWrap() && OBO1->hasNoUnsignedWrap();
+
+    // (X * Y) u/ (X << Z) --> Y u>> Z
+    if (!IsSigned && HasNUW)
+      return BinaryOperator::CreateLShr(Y, Z);
+
+    // TODO: Handle signed division.
+  }
+
   return nullptr;
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/div-shift.ll b/llvm/test/Transforms/InstCombine/div-shift.ll
index 804255fd6ca5..cfa8d55ff6d7 100644
--- a/llvm/test/Transforms/InstCombine/div-shift.ll
+++ b/llvm/test/Transforms/InstCombine/div-shift.ll
@@ -323,12 +323,12 @@ define i5 @sdiv_mul_shl_nsw_commute1(i5 %x, i5 %y, i5 %z) {
 
 define i5 @sdiv_mul_shl_nsw_commute2(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @sdiv_mul_shl_nsw_commute2(
-; CHECK-NEXT:    [[M1:%.*]] = mul nsw i5 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[M1:%.*]] = mul nsw i5 [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    [[M2:%.*]] = shl nsw i5 [[Z:%.*]], [[X]]
 ; CHECK-NEXT:    [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
-  %m1 = mul nsw i5 %x, %y
+  %m1 = mul nsw i5 %y, %x
   %m2 = shl nsw i5 %z, %x
   %d = sdiv i5 %m1, %m2
   ret i5 %d
@@ -420,11 +420,11 @@ define i5 @sdiv_mul_shl_missing_nsw2(i5 %x, i5 %y, i5 %z) {
   ret i5 %d
 }
 
+; (X * Y) u/ (X << Z) --> Y u>> Z
+
 define i5 @udiv_mul_shl_nuw(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nuw(
-; CHECK-NEXT:    [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[M2:%.*]] = shl nuw i5 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[M1]], [[M2]]
+; CHECK-NEXT:    [[D:%.*]] = lshr i5 [[Y:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
   %m1 = mul nuw i5 %x, %y
@@ -433,11 +433,11 @@ define i5 @udiv_mul_shl_nuw(i5 %x, i5 %y, i5 %z) {
   ret i5 %d
 }
 
+; (Y * X) u/ (X << Z) --> Y u>> Z
+
 define i5 @udiv_mul_shl_nuw_commute1(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nuw_commute1(
-; CHECK-NEXT:    [[M1:%.*]] = mul nuw i5 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[M2:%.*]] = shl nuw i5 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[M1]], [[M2]]
+; CHECK-NEXT:    [[D:%.*]] = lshr i5 [[Y:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
   %m1 = mul nuw i5 %y, %x
@@ -446,6 +446,8 @@ define i5 @udiv_mul_shl_nuw_commute1(i5 %x, i5 %y, i5 %z) {
   ret i5 %d
 }
 
+; negative test - shl is not commutative
+
 define i5 @udiv_mul_shl_nuw_commute2(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nuw_commute2(
 ; CHECK-NEXT:    [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]
@@ -459,12 +461,13 @@ define i5 @udiv_mul_shl_nuw_commute2(i5 %x, i5 %y, i5 %z) {
   ret i5 %d
 }
 
+; extra uses are ok
+
 define i8 @udiv_mul_shl_nsw_use1(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nsw_use1(
 ; CHECK-NEXT:    [[M1:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[M1]])
-; CHECK-NEXT:    [[M2:%.*]] = shl nuw i8 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = udiv i8 [[M1]], [[M2]]
+; CHECK-NEXT:    [[D:%.*]] = lshr i8 [[Y]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[D]]
 ;
   %m1 = mul nuw i8 %x, %y
@@ -474,12 +477,13 @@ define i8 @udiv_mul_shl_nsw_use1(i8 %x, i8 %y, i8 %z) {
   ret i8 %d
 }
 
+; extra uses are ok
+
 define i8 @udiv_mul_shl_nsw_use2(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nsw_use2(
-; CHECK-NEXT:    [[M1:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[M2:%.*]] = shl nuw i8 [[X]], [[Z:%.*]]
+; CHECK-NEXT:    [[M2:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[M2]])
-; CHECK-NEXT:    [[D:%.*]] = udiv i8 [[M1]], [[M2]]
+; CHECK-NEXT:    [[D:%.*]] = lshr i8 [[Y:%.*]], [[Z]]
 ; CHECK-NEXT:    ret i8 [[D]]
 ;
   %m1 = mul nuw i8 %x, %y
@@ -489,13 +493,15 @@ define i8 @udiv_mul_shl_nsw_use2(i8 %x, i8 %y, i8 %z) {
   ret i8 %d
 }
 
+; extra uses are ok
+
 define i8 @udiv_mul_shl_nsw_use3(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_nsw_use3(
 ; CHECK-NEXT:    [[M1:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[M1]])
 ; CHECK-NEXT:    [[M2:%.*]] = shl nuw i8 [[X]], [[Z:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[M2]])
-; CHECK-NEXT:    [[D:%.*]] = udiv i8 [[M1]], [[M2]]
+; CHECK-NEXT:    [[D:%.*]] = lshr i8 [[Y]], [[Z]]
 ; CHECK-NEXT:    ret i8 [[D]]
 ;
   %m1 = mul nuw i8 %x, %y
@@ -506,19 +512,23 @@ define i8 @udiv_mul_shl_nsw_use3(i8 %x, i8 %y, i8 %z) {
   ret i8 %d
 }
 
+; TODO: This can fold to (1<<z) / y
+
 define i5 @udiv_shl_mul_nuw(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_shl_mul_nuw(
-; CHECK-NEXT:    [[M1:%.*]] = shl nuw i5 [[Z:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[M1:%.*]] = shl nuw i5 [[X:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    [[M2:%.*]] = mul nuw i5 [[X]], [[Y:%.*]]
 ; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[M1]], [[M2]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
-  %m1 = shl nuw i5 %z, %x
+  %m1 = shl nuw i5 %x, %z
   %m2 = mul nuw i5 %x, %y
   %d = udiv i5 %m1, %m2
   ret i5 %d
 }
 
+; negative test - wrong no-wrap
+
 define i5 @udiv_mul_shl_missing_nsw1(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_missing_nsw1(
 ; CHECK-NEXT:    [[M1:%.*]] = mul nsw i5 [[X:%.*]], [[Y:%.*]]
@@ -532,6 +542,8 @@ define i5 @udiv_mul_shl_missing_nsw1(i5 %x, i5 %y, i5 %z) {
   ret i5 %d
 }
 
+; negative test - wrong no-wrap
+
 define i5 @udiv_mul_shl_missing_nsw2(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_mul_shl_missing_nsw2(
 ; CHECK-NEXT:    [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]


        


More information about the llvm-commits mailing list