1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00

InstCombine: Don't fold (X <<s log(INT_MIN)) /s INT_MIN to X

Consider the case where X is 2.  (2 <<s 31)/s-2147483648 is zero but we
would fold to X.  Note that this is valid when we are in the unsigned
domain because we require NUW: 2 <<u 31 results in poison.

This fixes PR21245.

llvm-svn: 219568
This commit is contained in:
David Majnemer 2014-10-11 10:20:04 +00:00
parent 475b056c9a
commit 26e62e8e03
2 changed files with 19 additions and 1 deletions

View File

@ -762,7 +762,8 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
} }
} }
if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1)))) || if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1))) &&
*C1 != C1->getBitWidth() - 1) ||
(!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) { (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) {
APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
APInt C1Shifted = APInt::getOneBitSet( APInt C1Shifted = APInt::getOneBitSet(

View File

@ -247,3 +247,20 @@ define i32 @test28(i32 %a) {
; CHECK-NEXT: %div = mul nuw i32 %a, 12 ; CHECK-NEXT: %div = mul nuw i32 %a, 12
; CHECK-NEXT: ret i32 %div ; CHECK-NEXT: ret i32 %div
} }
define i32 @test29(i32 %a) {
%mul = shl nsw i32 %a, 31
%div = sdiv i32 %mul, -2147483648
ret i32 %div
; CHECK-LABEL: @test29(
; CHECK-NEXT: %[[and:.*]] = and i32 %a, 1
; CHECK-NEXT: ret i32 %[[and]]
}
define i32 @test30(i32 %a) {
%mul = shl nuw i32 %a, 31
%div = udiv i32 %mul, -2147483648
ret i32 %div
; CHECK-LABEL: @test30(
; CHECK-NEXT: ret i32 %a
}