[PATCH] D139598: [InstCombine] Fold (X << Z) / (X * Y) -> (1 << Z) / Y

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 18:00:26 PST 2022


Chenbing.Zheng created this revision.
Chenbing.Zheng added reviewers: spatel, RKSimon, benshi001.
Chenbing.Zheng added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Chenbing.Zheng requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.

Alive2: https://alive2.llvm.org/ce/z/CBJLeP

Note that both shl and mul require nuw and already
have negtive test cases in div-shift.ll for it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139598

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


Index: llvm/test/Transforms/InstCombine/div-shift.ll
===================================================================
--- llvm/test/Transforms/InstCombine/div-shift.ll
+++ llvm/test/Transforms/InstCombine/div-shift.ll
@@ -528,13 +528,12 @@
   ret i8 %d
 }
 
-; TODO: This can fold to (1<<z) / y
+; (X << Z) / (X * Y) -> (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 [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT:    [[M2:%.*]] = mul nuw i5 [[X]], [[Y:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[M1]], [[M2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i5 1, [[Z:%.*]]
+; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[TMP1]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
   %m1 = shl nuw i5 %x, %z
@@ -545,9 +544,8 @@
 
 define i5 @udiv_shl_mul_nuw_swap(i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @udiv_shl_mul_nuw_swap(
-; CHECK-NEXT:    [[M1:%.*]] = shl nuw i5 [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT:    [[M2:%.*]] = mul nuw i5 [[Y:%.*]], [[X]]
-; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[M1]], [[M2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i5 1, [[Z:%.*]]
+; CHECK-NEXT:    [[D:%.*]] = udiv i5 [[TMP1]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i5 [[D]]
 ;
   %m1 = shl nuw i5 %x, %z
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1053,6 +1053,15 @@
     }
   }
 
+  // (X << Z) / (X * Y) -> (1 << Z) / Y
+  if (match(Op0, m_NUWShl(m_Value(X), m_Value(Z))) &&
+      match(Op1, m_c_Mul(m_Specific(X), m_Value(Y)))) {
+    bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
+    if (!IsSigned && HasNUW)
+      return BinaryOperator::CreateUDiv(
+          Builder.CreateShl(ConstantInt::get(Ty, 1), Z), Y);
+  }
+
   if (Instruction *R = foldIDivShl(I, Builder))
     return R;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139598.481124.patch
Type: text/x-patch
Size: 1967 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221208/f6d0276b/attachment.bin>


More information about the llvm-commits mailing list