[llvm] [InstCombine] simplify `icmp pred x, ~x` (PR #73990)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 00:53:47 PST 2023


================
@@ -7058,6 +7058,45 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
       }
     }
 
+    // Transform X s<  ~X  -->   X s<  0
+    //           X s>  ~X  -->   X s>  -1
+    //           X s>= ~X  -->   X s>  -1
+    //           X s<= ~X  -->   X s<  0
+    //           X u<  ~X  -->   X s>  -1
+    //           X u>  ~X  -->   X s<  0
+    //           X u<= ~X  -->   X s>  -1
+    //           X u>= ~X  -->   X s<  0
+    //           X ==  ~X  -->   false
+    //           X !=  ~X  -->   true
+    if (match(&I, m_c_ICmp(Pred, m_Value(X), m_Not(m_Deferred(X))))) {
+      CmpInst::Predicate NewPred;
+      switch (Pred) {
+      case ICmpInst::ICMP_EQ:
+        return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
+      case ICmpInst::ICMP_NE:
+        return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
+      case ICmpInst::ICMP_UGT:
+      case ICmpInst::ICMP_UGE:
+      case ICmpInst::ICMP_ULT:
+      case ICmpInst::ICMP_ULE:
+        NewPred = CmpInst::getSwappedPredicate(
+            CmpInst::getSignedPredicate(CmpInst::getStrictPredicate(Pred)));
+        break;
+      case ICmpInst::ICMP_SGT:
+      case ICmpInst::ICMP_SGE:
+      case ICmpInst::ICMP_SLT:
+      case ICmpInst::ICMP_SLE:
+        NewPred = CmpInst::getStrictPredicate(Pred);
----------------
ParkHanbum wrote:

@dtcxzyw Done! I changed it a little more. 

https://github.com/llvm/llvm-project/pull/73990


More information about the llvm-commits mailing list