[llvm] 05281d9 - [InstCombine] move fold for "(X-Y) == 0"; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 10 08:40:56 PDT 2021


Author: Sanjay Patel
Date: 2021-10-10T11:26:03-04:00
New Revision: 05281d95f2385fd7e266c1b439e5045f0187b149

URL: https://github.com/llvm/llvm-project/commit/05281d95f2385fd7e266c1b439e5045f0187b149
DIFF: https://github.com/llvm/llvm-project/commit/05281d95f2385fd7e266c1b439e5045f0187b149.diff

LOG: [InstCombine] move fold for "(X-Y) == 0"; NFC

This consolidates related folds that all have a
similar use restriction that may not be necessary.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 63be48fe352f..d47e6160acbe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2602,9 +2602,16 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
 
   // The following transforms are only worth it if the only user of the subtract
   // is the icmp.
+  // TODO: This is an artificial restriction for all of the transforms below
+  //       that only need a single replacement icmp.
   if (!Sub->hasOneUse())
     return nullptr;
 
+  // X - Y == 0 --> X == Y.
+  // X - Y != 0 --> X != Y.
+  if (Cmp.isEquality() && C.isZero())
+    return new ICmpInst(Pred, X, Y);
+
   if (Sub->hasNoSignedWrap()) {
     // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y)
     if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())
@@ -3130,14 +3137,6 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
       }
     }
     break;
-  case Instruction::Sub:
-    if (BO->hasOneUse()) {
-      if (C.isZero()) {
-        // Replace ((sub A, B) != 0) with (A != B).
-        return new ICmpInst(Pred, BOp0, BOp1);
-      }
-    }
-    break;
   case Instruction::Or: {
     const APInt *BOC;
     if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) {


        


More information about the llvm-commits mailing list