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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 22 06:36:07 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));
+    }
----------------
dtcxzyw wrote:

It has been addressed by [D156238](https://reviews.llvm.org/D156238).

https://github.com/llvm/llvm-project/blob/07151f0241d3f893cb36eb2dbc395d4098f74a87/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L5064-L5097

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


More information about the llvm-commits mailing list