From: Pete Cooper Date: Thu, 1 Dec 2011 19:13:26 +0000 (+0000) Subject: Improved fix for abs(val) != 0 to check other similar case. Also fixed style issues... X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=165695d26161912f528e0a8dca0f22e9b6cfa57b Improved fix for abs(val) != 0 to check other similar case. Also fixed style issues and confusing comment git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@145618 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index cebe37b7b0a..249de571e55 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1796,15 +1796,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { return ReplaceInstUsesWith(I, V); // comparing -val or val with non-zero is the same as just comparing val - // ie, (val != 0) == (-val != 0) + // ie, abs(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)), + Value *Cond, *SelectTrue, *SelectFalse; + if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue), m_Value(SelectFalse)))) { - if (SubSrc == SelectFalse) { - return CmpInst::Create(Instruction::ICmp, I.getPredicate(), - SubSrc, Op1); + if (Value *V = dyn_castNegVal(SelectTrue)) { + if (V == SelectFalse) + return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); + } + else if (Value *V = dyn_castNegVal(SelectFalse)) { + if (V == SelectTrue) + return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); } } } diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index e3d12f7fff7..016e8c55fb3 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -561,12 +561,22 @@ define i1 @test57(i32 %a) { } ; rdar://problem/10482509 -; CHECK: @cmpabs +; CHECK: @cmpabs1 ; CHECK-NEXT: icmp ne -define zeroext i1 @cmpabs(i64 %val) { +define zeroext i1 @cmpabs1(i64 %val) { %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 } + +; CHECK: @cmpabs2 +; CHECK-NEXT: icmp ne +define zeroext i1 @cmpabs2(i64 %val) { + %sub = sub nsw i64 0, %val + %cmp = icmp slt i64 %val, 0 + %sub.val = select i1 %cmp, i64 %val, i64 %sub + %tobool = icmp ne i64 %sub.val, 0 + ret i1 %tobool +}