[llvm] [InstCombine] Optimize redundant floating point comparisons in `or` inst (PR #158097)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 11 08:46:25 PDT 2025


================
@@ -4514,6 +4514,30 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
     if (Value *V = SimplifyAddWithRemainder(I))
       return replaceInstUsesWith(I, V);
 
+  Value *V0, *V1;
+  const APFloat *V0Op1, *V1Op1;
+  if (match(Op0, m_SpecificFCmp(FCmpInst::FCMP_OLT, m_Value(V0),
+                                m_APFloat(V0Op1))) &&
+      match(Op1, m_SpecificFCmp(FCmpInst::FCMP_OLT, m_Value(V1),
+                                m_APFloat(V1Op1)))) {
+    if (V0 == V1) {
+      if (V0Op1 > V1Op1)
+        replaceInstUsesWith(I, Op0);
+      else if (V1Op1 > V0Op1)
+        replaceInstUsesWith(I, Op1);
+    }
+  } else if (match(Op0, m_SpecificFCmp(FCmpInst::FCMP_OGT, m_Value(V0),
+                                       m_APFloat(V0Op1))) &&
+             match(Op1, m_SpecificFCmp(FCmpInst::FCMP_OGT, m_Value(V1),
+                                       m_APFloat(V1Op1)))) {
+    if (V0 == V1) {
+      if (V0Op1 < V1Op1)
+        replaceInstUsesWith(I, Op1);
----------------
dtcxzyw wrote:

Should be Op0? `(fcmp ogt X, 1.9 | fcmp ogt X, 2.3)` should be folded into `fcmp ogt X, 1.9`.

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


More information about the llvm-commits mailing list