From: Sanjay Patel Date: Thu, 6 Oct 2022 15:33:19 +0000 (-0400) Subject: [InstCombine] fold udiv with hidden common factor X-Git-Tag: upstream/17.0.6~31391 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6b869be8100d6c1b93bdaff1dc96a80a903c8fc7;p=platform%2Fupstream%2Fllvm.git [InstCombine] fold udiv with hidden common factor (X * Y) u/ (X << Z) --> Y u>> Z https://alive2.llvm.org/ce/z/4G9D_W --- diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index ab1cfe9..2267e00 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(Op0); + auto *OBO1 = cast(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 804255f..cfa8d55 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<