[llvm] [InstCombine] Fold copysign(floor(fabs(X)), X) to trunc(X) (PR #200836)

Aayush Shrivastava via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 05:17:13 PDT 2026


================
@@ -3116,6 +3116,15 @@ 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);
 
+    // copysign(floor(fabs(X)), X) --> copysign(trunc(X), X)
+    // copysign ignores the sign bit of its magnitude argument (implicit fabs),
+    // so replacing floor(fabs(X)) with trunc(X) is correct for all inputs
+    // including NaN without requiring nnan.
+    if (match(Mag, m_Intrinsic<Intrinsic::floor>(m_FAbs(m_Specific(Sign))))) {
----------------
iamaayushrivastava wrote:

Thanks for the suggestion! Updated to use `stripSignOnlyFPOps` on the inner fabs argument to also handle `fabs(fneg(X))` and `fabs(copysign(X, Y))` patterns. Added a test to cover the new case.

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


More information about the llvm-commits mailing list