[llvm-commits] [llvm] r45256 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2007-12-19-IcmpSub.ll

Christopher Lamb christopher.lamb at gmail.com
Wed Dec 19 23:21:11 PST 2007


Author: clamb
Date: Thu Dec 20 01:21:11 2007
New Revision: 45256

URL: http://llvm.org/viewvc/llvm-project?rev=45256&view=rev
Log:
Implement review feedback, including additional transforms

(icmp slt (sub A B) 1) -> (icmp sle A B)
icmp sgt (sub A B) -1) -> (icmp sge A B)

and add testcase.


Added:
    llvm/trunk/test/Transforms/InstCombine/2007-12-19-IcmpSub.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=45256&r1=45255&r2=45256&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Dec 20 01:21:11 2007
@@ -4785,23 +4785,6 @@
 
   if (isa<UndefValue>(Op1))                  // X icmp undef -> undef
     return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
-    
-  // (icmp cond (sub m A) 0) ->
-  //   (icmp cond m A)
-  {
-    ConstantInt *C1, *C2;
-    Value *A;
-    // Check both arguments of the compare for a matching subtract.
-    if (match(Op0, m_ConstantInt(C1)) && C1->getValue() == 0 &&
-        match(Op1, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
-      // We managed to fold the add into the RHS of the select condition.
-      return new ICmpInst(I.getPredicate(), A, C2);
-    } else if (match(Op1, m_ConstantInt(C1)) && C1->getValue() == 0 &&
-               match(Op0, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
-      // We managed to fold the add into the LHS of the select condition.
-      return new ICmpInst(I.getPredicate(), C2, A);
-    }
-  }
   
   // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
   // addresses never equal each other!  We already know that Op0 != Op1.
@@ -4850,6 +4833,12 @@
   // See if we are doing a comparison between a constant and an instruction that
   // can be folded into the comparison.
   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
+      Value *A, *B;
+    
+    // (icmp cond (sub A B) 0) -> (icmp cond A B)
+    if (CI->isNullValue() && match(Op0, m_Sub(m_Value(A), m_Value(B))))
+      return new ICmpInst(I.getPredicate(), A, B);
+    
     switch (I.getPredicate()) {
     default: break;
     case ICmpInst::ICMP_ULT:                        // A <u MIN -> FALSE
@@ -4873,6 +4862,10 @@
         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
       if (isMinValuePlusOne(CI,true))              // A <s MIN+1 -> A == MIN
         return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI));
+      
+      // (icmp slt (sub A B) 1) -> (icmp sle A B)
+      if (CI->isOne() && match(Op0, m_Sub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SLE, A, B);
       break;
 
     case ICmpInst::ICMP_UGT:
@@ -4896,6 +4889,11 @@
         return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
       if (isMaxValueMinusOne(CI, true))           // A >s MAX-1 -> A == MAX
         return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI));
+      
+      // (icmp sgt (sub A B) -1) -> (icmp sge A B)
+      if (CI->getValue().getSExtValue() == -1 && 
+          match(Op0, m_Sub(m_Value(A), m_Value(B))))
+        return new ICmpInst(ICmpInst::ICMP_SGE, A, B);
       break;
 
     case ICmpInst::ICMP_ULE:

Added: llvm/trunk/test/Transforms/InstCombine/2007-12-19-IcmpSub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2007-12-19-IcmpSub.ll?rev=45256&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2007-12-19-IcmpSub.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/2007-12-19-IcmpSub.ll Thu Dec 20 01:21:11 2007
@@ -0,0 +1,25 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {sub}
+
+define i32 @foo(i32 %a) {
+entry:
+	%tmp2 = sub i32 99, %a		; <i32> [#uses=1]
+	%tmp3 = icmp sgt i32 %tmp2, -1		; <i1> [#uses=1]
+	%retval = select i1 %tmp3, i32 %a, i32 0		; <i32> [#uses=1]
+	ret i32 %retval
+}
+
+define i32 @bar(i32 %a) {
+entry:
+	%tmp2 = sub i32 99, %a		; <i32> [#uses=1]
+	%tmp3 = icmp sge i32 %tmp2, 0; <i1> [#uses=1]
+	%retval = select i1 %tmp3, i32 %a, i32 0		; <i32> [#uses=1]
+	ret i32 %retval
+}
+
+define i32 @baz(i32 %a) {
+entry:
+	%tmp2 = sub i32 99, %a		; <i32> [#uses=1]
+	%tmp3 = icmp slt i32 %tmp2, 1		; <i1> [#uses=1]
+	%retval = select i1 %tmp3, i32 %a, i32 0		; <i32> [#uses=1]
+	ret i32 %retval
+}
\ No newline at end of file





More information about the llvm-commits mailing list