[llvm] [InstCombine] Fold copysign of selects from sign comparison to sign operand (PR #85627)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 09:38:35 PDT 2024


================
@@ -2479,7 +2479,47 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     if (match(Mag, m_FAbs(m_Value(X))) || match(Mag, m_FNeg(m_Value(X))))
       return replaceOperand(*II, 0, X);
 
-    break;
+    Value *A, *B;
+    CmpInst::Predicate Pred;
+    const APFloat *TC, *FC;
+    if (!match(Sign, m_Select((m_And(m_FCmp(Pred, m_Value(A), m_PosZeroFP()),
+                                     m_Value(B))),
+                              m_APFloat(TC), m_APFloat(FC))))
+      return nullptr;
+    /*
+    Match select ?, TC, FC where the constants are equal but negated.
+    Check for these conditions:
+    olt/ule
+    copysign(Mag, B & (A < 0.0) ? -TC : TC) --> copysign(Mag, A) B->true, A<0.
+    copysign(Mag, B & (A < 0.0) ? -TC : TC) --> copysign(Mag, A) B->true, A>0.
+    copysign(Mag, B & (A < 0.0) ? -TC : TC) --> copysign(Mag, A) B->false, A>0.
+    copysign(Mag, B & (A < 0.0) ? -TC : TC) --> copysign(Mag, -A) B->false, A<0.
+    ogt/uge
+    copysign(Mag, B & !(A < 0.0) ? -TC : TC) --> copysign(Mag, -A) B->true, A<0.
+    copysign(Mag, B & !(A < 0.0) ? -TC : TC) --> copysign(Mag, -A) B->true, A>0.
+    copysign(Mag, B & !(A < 0.0) ? -TC : TC) --> copysign(Mag, A) B->false, A>0.
+    copysign(Mag, B & !(A < 0.0) ? -TC : TC) --> copysign(Mag, -A) B->false,
+    A<0.
+    */
+    if (Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_ULE)
+      if (match(A, m_Negative()))
+        if (!match(B, m_ZeroInt()) && TC->isNegative())
+          return replaceOperand(*II, 1, A);
+        if (match(B, m_ZeroInt()) && TC->isNegative())
+          A = Builder.CreateFNeg(A);
+        return replaceOperand(*II, 1, A);
+        return replaceOperand(*II, 1, A);
+        if (Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_UGE)
+          if (match(A, m_Negative())) {
+            A = Builder.CreateFNeg(A);
+            return replaceOperand(*II, 1, A);
+          }
+        if (!match(B, m_ZeroInt()) && TC->isNegative())
+          A = Builder.CreateFNeg(A);
+        return replaceOperand(*II, 1, A);
+        if (match(B, m_ZeroInt()) && TC->isNegative())
+          return replaceOperand(*II, 1, A);
----------------
arsenm wrote:

Dead code 

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


More information about the llvm-commits mailing list