[llvm] [InstCombine] Fold `icmp eq/ne min|max(X, Y), Z` (PR #67087)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 22 06:15:47 PDT 2023


================
@@ -5038,12 +5044,32 @@ InstCombinerImpl::foldICmpWithMinMaxImpl(Instruction &I,
         NewPred = ICmpInst::getInversePredicate(NewPred);
       return ICmpInst::Create(Instruction::ICmp, NewPred, X, Y);
     }
-    // Otherwise (X != Z, nofold):
-    //       Expr      Result
-    // min(X, Y) == Z X > Y || Y == Z
-    // max(X, Y) == Z X < Y || Y == Z
-    // min(X, Y) != Z X <= Y && Y != Z
-    // max(X, Y) != Z X >= Y && Y != Z
+    // Otherwise (X != Z):
+    ICmpInst::Predicate NewPred = MinMax->getPredicate();
+    auto NewCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
+    if (!NewCmpXZ.has_value()) {
+      std::swap(X, Y);
+      std::swap(CmpXZ, CmpYZ);
+      NewCmpXZ = IsCondKnownTrue(simplifyICmpInst(NewPred, X, Z, Q));
+    }
----------------
goldsteinn wrote:

This is also applicable to other predicates no?
I.e
`max(X, Y) u<= Z`
If you have `MinMax`(`UGT`) known true -> true. Known false -> `Y u<= Z`. 

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


More information about the llvm-commits mailing list