[llvm] r185808 - InstCombine: Fold X-C1 <u 2 -> (X & -2) == C1

David Majnemer david.majnemer at gmail.com
Mon Jul 8 04:53:08 PDT 2013


Author: majnemer
Date: Mon Jul  8 06:53:08 2013
New Revision: 185808

URL: http://llvm.org/viewvc/llvm-project?rev=185808&view=rev
Log:
InstCombine: Fold X-C1 <u 2 -> (X & -2) == C1

Back in r179493 we determined that two transforms collided with each
other.  The fix back then was to reorder the transforms so that the
preferred transform would give it a try and then we would try the
secondary transform.  However, it was noted that the best approach would
canonicalize one transform into the other, removing the collision and
allowing us to optimize IR given to us in that form.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=185808&r1=185807&r2=185808&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Mon Jul  8 06:53:08 2013
@@ -1539,6 +1539,14 @@ Instruction *InstCombiner::visitICmpInst
                               Builder->getInt(CR.getLower()));
         }
       }
+
+      // X-C1 <u 2 -> (X & -2) == C1
+      //   iff C1 & 1 == 0
+      if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
+          LHSV[0] == 0 && RHSV == 2)
+        return new ICmpInst(ICmpInst::ICMP_EQ,
+                            Builder->CreateAnd(LHSI->getOperand(0), -RHSV),
+                            ConstantExpr::getNeg(LHSC));
     }
     break;
   }

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=185808&r1=185807&r2=185808&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Mon Jul  8 06:53:08 2013
@@ -1113,3 +1113,13 @@ define i1 @or_icmp_eq_B_0_icmp_ult_A_B(i
   %3 = or i1 %1, %2
   ret i1 %3
 }
+
+; CHECK: @icmp_add_ult_2
+; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %X, -2
+; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[AND]], 14
+; CHECK-NEXT: ret i1 [[CMP]]
+define i1 @icmp_add_ult_2(i32 %X) {
+  %add = add i32 %X, -14
+  %cmp = icmp ult i32 %add, 2
+  ret i1 %cmp
+}





More information about the llvm-commits mailing list