Skip to content

Commit

Permalink
[InstCombine] fold udiv with hidden common factor
Browse files Browse the repository at this point in the history
(X * Y) u/ (X << Z) --> Y u>> Z

https://alive2.llvm.org/ce/z/4G9D_W
  • Loading branch information
rotateright committed Oct 6, 2022
1 parent 34cca2b commit 6b869be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
16 changes: 16 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Expand Up @@ -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;
}

Expand Down
44 changes: 28 additions & 16 deletions llvm/test/Transforms/InstCombine/div-shift.ll
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:%.*]]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:%.*]]
Expand All @@ -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:%.*]]
Expand Down

0 comments on commit 6b869be

Please sign in to comment.