diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 010625a7157..2befe1934f4 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -997,9 +997,14 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { match(Op1, m_APInt(C2))) { bool Overflow; APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); - if (!Overflow) - return BinaryOperator::CreateUDiv( + if (!Overflow) { + bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); + BinaryOperator *BO = BinaryOperator::CreateUDiv( X, ConstantInt::get(X->getType(), C2ShlC1)); + if (IsExact) + BO->setIsExact(); + return BO; + } } } diff --git a/test/Transforms/InstCombine/div.ll b/test/Transforms/InstCombine/div.ll index 5af86680697..585b04a4d5d 100644 --- a/test/Transforms/InstCombine/div.ll +++ b/test/Transforms/InstCombine/div.ll @@ -286,3 +286,12 @@ define i32 @test32(i32 %a, i32 %b) { ; CHECK-NEXT: %[[div:.*]] = udiv i32 %a, %[[shr]] ; CHECK-NEXT: ret i32 } + +define <2 x i64> @test33(<2 x i64> %x) nounwind { + %shr = lshr exact <2 x i64> %x, + %div = udiv exact <2 x i64> %shr, + ret <2 x i64> %div +; CHECK-LABEL: @test33( +; CHECK-NEXT: udiv exact <2 x i64> %x, +; CHECK-NEXT: ret <2 x i64> +}