mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-26 23:29:22 +00:00
InstCombine: Don't unconditionally preserve 'nuw' when shrinking constants
Consider: %add = add nuw i32 %a, -16777216 %and = and i32 %add, 255 Regardless of whether or not we demand the sign bit of %add, we cannot replace -16777216 with 2130706432 without also removing 'nuw' from the instruction. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216273 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
0e4fc41b0d
commit
5939f08f5e
@ -44,12 +44,18 @@ static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
|
|||||||
Demanded &= OpC->getValue();
|
Demanded &= OpC->getValue();
|
||||||
I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
|
I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
|
||||||
|
|
||||||
// If 'nsw' is set and the constant is negative, removing *any* bits from the
|
// If either 'nsw' or 'nuw' is set and the constant is negative,
|
||||||
// constant could make overflow occur. Remove 'nsw' from the instruction in
|
// removing *any* bits from the constant could make overflow occur.
|
||||||
// this case.
|
// Remove 'nsw' and 'nuw' from the instruction in this case.
|
||||||
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I))
|
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I)) {
|
||||||
if (OBO->hasNoSignedWrap() && OpC->getValue().isNegative())
|
assert(OBO->getOpcode() == Instruction::Add);
|
||||||
cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
|
if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap()) {
|
||||||
|
if (OpC->getValue().isNegative()) {
|
||||||
|
cast<BinaryOperator>(OBO)->setHasNoSignedWrap(false);
|
||||||
|
cast<BinaryOperator>(OBO)->setHasNoUnsignedWrap(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1038,3 +1038,15 @@ define i8 @test84(i32 %a) {
|
|||||||
; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
|
; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
|
||||||
; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
|
; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define i8 @test85(i32 %a) {
|
||||||
|
%add = add nuw i32 %a, -16777216
|
||||||
|
%shr = lshr exact i32 %add, 23
|
||||||
|
%trunc = trunc i32 %shr to i8
|
||||||
|
ret i8 %trunc
|
||||||
|
|
||||||
|
; CHECK-LABEL: @test85(
|
||||||
|
; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432
|
||||||
|
; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
|
||||||
|
; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user