[llvm-commits] [llvm] r45173 - /llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Christopher Lamb christopher.lamb at gmail.com
Tue Dec 18 13:32:20 PST 2007


Author: clamb
Date: Tue Dec 18 15:32:20 2007
New Revision: 45173

URL: http://llvm.org/viewvc/llvm-project?rev=45173&view=rev
Log:
Fold subtracts into integer compares vs. zero. This improves generate code for this case on X86 
from
_foo:
        movl    $99, %ecx
        movl    4(%esp), %eax
        subl    %eax, %ecx
        xorl    %edx, %edx
        testl   %ecx, %ecx
        cmovs   %edx, %eax
        ret

to
_foo:
        xorl    %ecx, %ecx
        movl    4(%esp), %eax
        cmpl    $99, %eax
        cmovg   %ecx, %eax
        ret

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=45173&r1=45172&r2=45173&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Tue Dec 18 15:32:20 2007
@@ -4793,7 +4793,24 @@
 
   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.
   if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||





More information about the llvm-commits mailing list