InstCombine: Don't take A-B<0 into A<B if A-B has other uses
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineCompares.cpp
index f7075dfc674f363b15e38c9fc0b180fdc82d6701..c07c96d49aab7b9ea70d9c4901e8df2c1a30bb22 100644 (file)
@@ -2686,32 +2686,34 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
         return Res;
     }
 
-    // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
-    if (I.isEquality() && CI->isZero() &&
-        match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
-      // (icmp cond A B) if cond is equality
-      return new ICmpInst(I.getPredicate(), A, B);
-    }
-
-    // (icmp sgt (sub nsw A B), -1) -> (icmp sge A, B)
-    if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isAllOnesValue() &&
-        match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
-      return new ICmpInst(ICmpInst::ICMP_SGE, A, B);
-
-    // (icmp sgt (sub nsw A B), 0) -> (icmp sgt A, B)
-    if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isZero() &&
-        match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
-      return new ICmpInst(ICmpInst::ICMP_SGT, A, B);
-
-    // (icmp slt (sub nsw A B), 0) -> (icmp slt A, B)
-    if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isZero() &&
-        match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
-      return new ICmpInst(ICmpInst::ICMP_SLT, A, B);
-
-    // (icmp slt (sub nsw A B), 1) -> (icmp sle A, B)
-    if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isOne() &&
-        match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
-      return new ICmpInst(ICmpInst::ICMP_SLE, A, B);
+    // The following transforms are only 'worth it' if the only user of the
+    // subtraction is the icmp.
+    if (Op0->hasOneUse()) {
+      // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
+      if (I.isEquality() && CI->isZero() &&
+          match(Op0, m_Sub(m_Value(A), m_Value(B))))
+        return new ICmpInst(I.getPredicate(), A, B);
+
+      // (icmp sgt (sub nsw A B), -1) -> (icmp sge A, B)
+      if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isAllOnesValue() &&
+          match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SGE, A, B);
+
+      // (icmp sgt (sub nsw A B), 0) -> (icmp sgt A, B)
+      if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isZero() &&
+          match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SGT, A, B);
+
+      // (icmp slt (sub nsw A B), 0) -> (icmp slt A, B)
+      if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isZero() &&
+          match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SLT, A, B);
+
+      // (icmp slt (sub nsw A B), 1) -> (icmp sle A, B)
+      if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isOne() &&
+          match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SLE, A, B);
+    }
 
     // If we have an icmp le or icmp ge instruction, turn it into the
     // appropriate icmp lt or icmp gt instruction.  This allows us to rely on