[PATCH] D139598: [InstCombine] Fold (X << Z) / (X * Y) -> (1 << Z) / Y
Chenbing.Zheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 15 18:56:27 PST 2022
Chenbing.Zheng updated this revision to Diff 483412.
Chenbing.Zheng added a comment.
add nuw for 1<<Z
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139598/new/
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 nuw 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 nuw i5 1, [[Z:%.*]]
+; CHECK-NEXT: [[D:%.*]] = udiv i5 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i5 [[D]]
;
%m1 = shl nuw i5 %x, %z
@@ -558,9 +556,8 @@
define i5 @udiv_shl_mul_nuw_exact(i5 %x, i5 %y, i5 %z) {
; CHECK-LABEL: @udiv_shl_mul_nuw_exact(
-; CHECK-NEXT: [[M1:%.*]] = shl nuw i5 [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT: [[M2:%.*]] = mul nuw i5 [[X]], [[Y:%.*]]
-; CHECK-NEXT: [[D:%.*]] = udiv exact i5 [[M1]], [[M2]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
+; CHECK-NEXT: [[D:%.*]] = udiv exact i5 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i5 [[D]]
;
%m1 = shl nuw i5 %x, %z
@@ -571,9 +568,8 @@
define <2 x i4> @udiv_shl_mul_nuw_vec(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
; CHECK-LABEL: @udiv_shl_mul_nuw_vec(
-; CHECK-NEXT: [[M1:%.*]] = shl nuw <2 x i4> [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT: [[M2:%.*]] = mul nuw <2 x i4> [[Y:%.*]], [[X]]
-; CHECK-NEXT: [[D:%.*]] = udiv <2 x i4> [[M1]], [[M2]]
+; CHECK-NEXT: [[TMP1:%.*]] = shl nuw <2 x i4> <i4 1, i4 1>, [[Z:%.*]]
+; CHECK-NEXT: [[D:%.*]] = udiv <2 x i4> [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i4> [[D]]
;
%m1 = shl nuw <2 x i4> %x, %z
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1053,6 +1053,18 @@
}
}
+ // (X << Z) / (X * Y) -> (1 << Z) / Y
+ // TODO: Handle sdiv.
+ if (!IsSigned && Op0->hasOneUse() && Op1->hasOneUse() &&
+ match(Op0, m_NUWShl(m_Value(X), m_Value(Z))) &&
+ match(Op1, m_c_Mul(m_Specific(X), m_Value(Y))))
+ if (cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap()) {
+ Instruction *NewDiv = BinaryOperator::CreateUDiv(
+ Builder.CreateShl(ConstantInt::get(Ty, 1), Z, "", /*NUW*/ true), Y);
+ NewDiv->setIsExact(I.isExact());
+ return NewDiv;
+ }
+
if (Instruction *R = foldIDivShl(I, Builder))
return R;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139598.483412.patch
Type: text/x-patch
Size: 3146 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221216/f0f545a3/attachment.bin>
More information about the llvm-commits
mailing list