Also fold (A+B) == A -> B == 0 when the add is commuted.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 11 Feb 2011 21:46:48 +0000 (21:46 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 11 Feb 2011 21:46:48 +0000 (21:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125411 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/add.ll

index a24d4ca7eb3a21e9d6c37b39e6f83d7e2e642da0..c34e698e3e799122f88647ac235550977286f0cf 100644 (file)
@@ -2351,12 +2351,14 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
                           Constant::getNullValue(B->getType()));
 
     // (A+B) == A  ->  B == 0
-    if (match(Op0, m_Add(m_Specific(Op1), m_Value(B))))
+    if (match(Op0, m_Add(m_Specific(Op1), m_Value(B))) ||
+        match(Op0, m_Add(m_Value(B), m_Specific(Op1))))
       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))))
+    if (match(Op1, m_Add(m_Specific(Op0), m_Value(B))) ||
+        match(Op1, m_Add(m_Value(B), m_Specific(Op0))))
       return new ICmpInst(I.getPredicate(), B,
                           Constant::getNullValue(B->getType()));
 
index b3a7f5260799bdf7e8b63a5f72c644f4756d5fa5..a316d06894ac6d1b63503c497002e8743fd0d018 100644 (file)
@@ -276,18 +276,26 @@ define i32 @test36(i32 %a) {
        ret i32 %q
 }
 
-define i32 @test37(i32 %a, i32 %b) nounwind readnone {
-entry:
-  %add = add nsw i32 %a, %b
+define i1 @test37(i32 %a, i32 %b) nounwind readnone {
+  %add = add i32 %a, %b
   %cmp = icmp eq i32 %add, %a
-  %conv = zext i1 %cmp to i32
-  ret i32 %conv
+  ret i1 %cmp
 }
 
-define i32 @test38(i32 %a, i32 %b) nounwind readnone {
-entry:
-  %add = add nsw i32 %a, %b
+define i1 @test38(i32 %a, i32 %b) nounwind readnone {
+  %add = add i32 %a, %b
+  %cmp = icmp eq i32 %add, %b
+  ret i1 %cmp
+}
+
+define i1 @test39(i32 %a, i32 %b) nounwind readnone {
+  %add = add i32 %b, %a
   %cmp = icmp eq i32 %add, %a
-  %conv = zext i1 %cmp to i32
-  ret i32 %conv
+  ret i1 %cmp
+}
+
+define i1 @test40(i32 %a, i32 %b) nounwind readnone {
+  %add = add i32 %b, %a
+  %cmp = icmp eq i32 %add, %b
+  ret i1 %cmp
 }