summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2014-11-22 07:15:16 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2014-11-22 07:15:16 +0000
commit7eca618dfc363f98085cef732b61c7c48c519582 (patch)
tree10cbfd3b0aa9dc3715a48c55e9a17de546c6dc2a
parent1c4abdb6ab6e959418b82f235e3f7c8646c7792c (diff)
InstSimplify: Simplify (sub 0, X) -> X if it's NUW
This is a generalization of the X - (0 - Y) -> X transform. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222611 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Analysis/InstructionSimplify.cpp14
-rw-r--r--test/Transforms/InstSimplify/AndOrXor.ll7
2 files changed, 10 insertions, 11 deletions
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index f151a3a33b4..7cbe6eff4ba 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -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
diff --git a/test/Transforms/InstSimplify/AndOrXor.ll b/test/Transforms/InstSimplify/AndOrXor.ll
index 8ed06e83093..bf7e9a28640 100644
--- a/test/Transforms/InstSimplify/AndOrXor.ll
+++ b/test/Transforms/InstSimplify/AndOrXor.ll
@@ -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
+}