mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-06 23:32:27 +00:00
InstCombine: Fix miscompile in X % -Y -> X % Y transform
We assumed that negation operations of the form (0 - %Z) resulted in a negative number. This isn't true if %Z was originally negative. Substituting the negative number into the remainder operation may result in undefined behavior because the dividend might be INT_MIN. This fixes PR21256. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219639 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
3a143ce2e7
commit
af6be11a60
@ -1324,15 +1324,15 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
|
|||||||
if (Instruction *Common = commonIRemTransforms(I))
|
if (Instruction *Common = commonIRemTransforms(I))
|
||||||
return Common;
|
return Common;
|
||||||
|
|
||||||
if (Value *RHSNeg = dyn_castNegVal(Op1))
|
{
|
||||||
if (!isa<Constant>(RHSNeg) ||
|
const APInt *Y;
|
||||||
(isa<ConstantInt>(RHSNeg) &&
|
// X % -Y -> X % Y
|
||||||
cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
|
if (match(Op1, m_APInt(Y)) && Y->isNegative() && !Y->isMinSignedValue()) {
|
||||||
// X % -Y -> X % Y
|
|
||||||
Worklist.AddValue(I.getOperand(1));
|
Worklist.AddValue(I.getOperand(1));
|
||||||
I.setOperand(1, RHSNeg);
|
I.setOperand(1, ConstantInt::get(I.getType(), -*Y));
|
||||||
return &I;
|
return &I;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If the sign bits of both operands are zero (i.e. we can prove they are
|
// If the sign bits of both operands are zero (i.e. we can prove they are
|
||||||
// unsigned inputs), turn this into a urem.
|
// unsigned inputs), turn this into a urem.
|
||||||
|
@ -95,12 +95,6 @@ define i1024 @test14(i1024 %A) {
|
|||||||
ret i1024 %D
|
ret i1024 %D
|
||||||
}
|
}
|
||||||
|
|
||||||
define i14 @test15(i14 %A, i14 %B) {
|
|
||||||
%C = sub i14 0, %A ; <i14> [#uses=1]
|
|
||||||
%D = srem i14 %B, %C ; <i14> [#uses=1]
|
|
||||||
ret i14 %D
|
|
||||||
}
|
|
||||||
|
|
||||||
define i51 @test16(i51 %A) {
|
define i51 @test16(i51 %A) {
|
||||||
%X = sdiv i51 %A, 1123 ; <i51> [#uses=1]
|
%X = sdiv i51 %A, 1123 ; <i51> [#uses=1]
|
||||||
%Y = sub i51 0, %X ; <i51> [#uses=1]
|
%Y = sub i51 0, %X ; <i51> [#uses=1]
|
||||||
|
@ -142,8 +142,9 @@ define i32 @test15(i32 %A, i32 %B) {
|
|||||||
%D = srem i32 %B, %C
|
%D = srem i32 %B, %C
|
||||||
ret i32 %D
|
ret i32 %D
|
||||||
; CHECK-LABEL: @test15(
|
; CHECK-LABEL: @test15(
|
||||||
; CHECK: %D = srem i32 %B, %A
|
; CHECK: %[[sub:.*]] = sub i32 0, %A
|
||||||
; CHECK: ret i32 %D
|
; CHECK-NEXT: %[[rem:.*]] = srem i32 %B, %[[sub]]
|
||||||
|
; CHECK: ret i32 %[[rem]]
|
||||||
}
|
}
|
||||||
|
|
||||||
define i32 @test16(i32 %A) {
|
define i32 @test16(i32 %A) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user