From 77bc49e5e2e2a5e549d65bc0bedd86ff3df6b161 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 30 Jan 2011 22:01:13 +0000 Subject: [PATCH] Recognize and simplify (A+B) == A -> B == 0 A == (A+B) -> B == 0 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124567 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineCompares.cpp | 12 +++++++++++- test/Transforms/InstCombine/add.ll | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index fe436bce210..8c5e7e48c44 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2341,7 +2341,17 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)))) return new ICmpInst(I.getPredicate(), B, Constant::getNullValue(B->getType())); - + + // (A+B) == A -> B == 0 + if (match(Op0, m_Add(m_Specific(Op1), m_Value(B)))) + return new ICmpInst(I.getPredicate(), B, + Constant::getNullValue(B->getType())); + + // A == (A+B) -> B == 0 + if (match(Op1, m_Add(m_Specific(Op0), m_Value(B)))) + return new ICmpInst(I.getPredicate(), B, + Constant::getNullValue(B->getType())); + // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 if (Op0->hasOneUse() && Op1->hasOneUse() && match(Op0, m_And(m_Value(A), m_Value(B))) && diff --git a/test/Transforms/InstCombine/add.ll b/test/Transforms/InstCombine/add.ll index 4719809d6d3..b3a7f526079 100644 --- a/test/Transforms/InstCombine/add.ll +++ b/test/Transforms/InstCombine/add.ll @@ -275,3 +275,19 @@ define i32 @test36(i32 %a) { %q = and i32 %z, 1 ; always zero ret i32 %q } + +define i32 @test37(i32 %a, i32 %b) nounwind readnone { +entry: + %add = add nsw i32 %a, %b + %cmp = icmp eq i32 %add, %a + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +define i32 @test38(i32 %a, i32 %b) nounwind readnone { +entry: + %add = add nsw i32 %a, %b + %cmp = icmp eq i32 %add, %a + %conv = zext i1 %cmp to i32 + ret i32 %conv +} -- 2.34.1