InstSimplify: Simplify (sub 0, X) -> X if it's NUW

This is a generalization of the X - (0 - Y) -> X transform.

llvm-svn: 222611
This commit is contained in:
David Majnemer 2014-11-22 07:15:16 +00:00
parent 6fc2d813cb
commit 4efa9ff8ca
2 changed files with 10 additions and 11 deletions

View File

@ -683,17 +683,9 @@ static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
if (Op0 == Op1)
return Constant::getNullValue(Op0->getType());
// X - (0 - Y) -> X if the second sub is NUW.
// If Y != 0, 0 - Y is a poison value.
// If Y == 0, 0 - Y simplifies to 0.
if (BinaryOperator::isNeg(Op1)) {
if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) {
assert(BO->getOpcode() == Instruction::Sub &&
"Expected a subtraction operator!");
if (BO->hasNoUnsignedWrap())
return Op0;
}
}
// 0 - X -> 0 if the sub is NUW.
if (isNUW && match(Op0, m_Zero()))
return Op0;
// (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
// For example, (X + Y) - Y -> X; (Y + X) - Y -> X

View File

@ -148,3 +148,10 @@ define i1 @or_of_icmps5(i32 %b) {
ret i1 %cmp
; CHECK: ret i1 true
}
define i32 @neg_nuw(i32 %x) {
; CHECK-LABEL: @neg_nuw(
%neg = sub nuw i32 0, %x
ret i32 %neg
; CHECK: ret i32 0
}