From bc5c524b71d97f31a8954b6e81b65c077763b1b3 Mon Sep 17 00:00:00 2001 From: Pete Cooper Date: Thu, 1 Dec 2011 03:58:40 +0000 Subject: [PATCH] Added instcombine pattern to spot comparing -val or val against 0. (val != 0) == (-val != 0) so "abs(val) != 0" becomes "val != 0" Fixes llvm-svn: 145563 --- .../Transforms/InstCombine/InstCombineCompares.cpp | 14 ++++++++++++++ .../Transforms/InstCombine/compare-abs-nonzero.ll | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/compare-abs-nonzero.ll diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 274758ccb4ba..cebe37b7b0a5 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1795,6 +1795,20 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, TD)) return ReplaceInstUsesWith(I, V); + // comparing -val or val with non-zero is the same as just comparing val + // ie, (val != 0) == (-val != 0) + if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) + { + Value *Cond, *SubSrc, *SelectFalse; + if (match(Op0, m_Select(m_Value(Cond), m_Sub(m_Zero(), m_Value(SubSrc)), + m_Value(SelectFalse)))) { + if (SubSrc == SelectFalse) { + return CmpInst::Create(Instruction::ICmp, I.getPredicate(), + SubSrc, Op1); + } + } + } + Type *Ty = Op0->getType(); // icmp's with boolean values can always be turned into bitwise operations diff --git a/llvm/test/Transforms/InstCombine/compare-abs-nonzero.ll b/llvm/test/Transforms/InstCombine/compare-abs-nonzero.ll new file mode 100644 index 000000000000..49cd688bda02 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/compare-abs-nonzero.ll @@ -0,0 +1,10 @@ +; RUN: opt < %s -instcombine -S | grep -v {select} + +define zeroext i1 @cmpabs(i64 %val) nounwind uwtable readnone ssp { +entry: + %sub = sub nsw i64 0, %val + %cmp = icmp slt i64 %val, 0 + %sub.val = select i1 %cmp, i64 %sub, i64 %val + %tobool = icmp ne i64 %sub.val, 0 + ret i1 %tobool +}