From 4efa9ff8ca832d2a87f553c65960f81f02bb2843 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sat, 22 Nov 2014 07:15:16 +0000 Subject: [PATCH] InstSimplify: Simplify (sub 0, X) -> X if it's NUW This is a generalization of the X - (0 - Y) -> X transform. llvm-svn: 222611 --- llvm/lib/Analysis/InstructionSimplify.cpp | 14 +++----------- llvm/test/Transforms/InstSimplify/AndOrXor.ll | 7 +++++++ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index f151a3a33b48..7cbe6eff4ba7 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/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(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/llvm/test/Transforms/InstSimplify/AndOrXor.ll b/llvm/test/Transforms/InstSimplify/AndOrXor.ll index 8ed06e83093b..bf7e9a28640f 100644 --- a/llvm/test/Transforms/InstSimplify/AndOrXor.ll +++ b/llvm/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 +}