mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-10-30 16:17:05 +00:00
The srem -> urem transform is not safe for any divisor that's not a power of two.
E.g. -5 % 5 is 0 with srem and 1 with urem. Also addresses Frits van Bommel's comments. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120049 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1704,8 +1704,8 @@ Missed instcombine transformation:
|
|||||||
%384 = shl i64 %381, %383 ; [#uses=1]
|
%384 = shl i64 %381, %383 ; [#uses=1]
|
||||||
%385 = icmp slt i32 %tmp14.i, 64 ; [#uses=1]
|
%385 = icmp slt i32 %tmp14.i, 64 ; [#uses=1]
|
||||||
|
|
||||||
The srem can be transformed to an and because if x is negative, the shift is
|
The srem can be transformed to an and because if %tmp14.i is negative, the
|
||||||
undefined. Testcase derived from 403.gcc.
|
shift is undefined. Testcase derived from 403.gcc.
|
||||||
|
|
||||||
//===---------------------------------------------------------------------===//
|
//===---------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
|||||||
@@ -54,19 +54,17 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
|
|||||||
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
|
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
|
||||||
return Res;
|
return Res;
|
||||||
|
|
||||||
// X shift (A srem B) -> X shift (A urem B) iff B is positive.
|
// X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
|
||||||
// Because shifts by negative values are undefined.
|
// Because shifts by negative values are undefined.
|
||||||
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op1))
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op1))
|
||||||
if (BO->getOpcode() == Instruction::SRem && BO->getType()->isIntegerTy()) {
|
if (BO->hasOneUse() && BO->getOpcode() == Instruction::SRem)
|
||||||
// Make sure the divisor's sign bit is zero.
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
|
||||||
APInt Mask = APInt::getSignBit(BO->getType()->getPrimitiveSizeInBits());
|
if (CI->getValue().isPowerOf2()) {
|
||||||
if (MaskedValueIsZero(BO->getOperand(1), Mask)) {
|
Constant *C = ConstantInt::get(BO->getType(), CI->getValue()-1);
|
||||||
Value *URem = Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
|
Value *Rem = Builder->CreateAnd(BO->getOperand(0), C, BO->getName());
|
||||||
BO->getName());
|
I.setOperand(1, Rem);
|
||||||
I.setOperand(1, URem);
|
|
||||||
return &I;
|
return &I;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -443,12 +443,12 @@ entry:
|
|||||||
}
|
}
|
||||||
|
|
||||||
define i32 @test38(i32 %x) nounwind readnone {
|
define i32 @test38(i32 %x) nounwind readnone {
|
||||||
entry:
|
|
||||||
%rem = srem i32 %x, 32
|
%rem = srem i32 %x, 32
|
||||||
%shl = shl i32 1, %rem
|
%shl = shl i32 1, %rem
|
||||||
ret i32 %shl
|
ret i32 %shl
|
||||||
; CHECK: @test38
|
; CHECK: @test38
|
||||||
; CHECK-NOT: srem
|
; CHECK-NEXT: and i32 %x, 31
|
||||||
; CHECK: ret i32
|
; CHECK-NEXT: shl i32 1
|
||||||
|
; CHECK-NEXT: ret i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user