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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 03:27:11 PDT 2026


================
@@ -3119,8 +3119,11 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     // 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))))) {
+    // including NaN without requiring nnan. Use stripSignOnlyFPOps inside the
+    // fabs to also handle fabs(fneg(X)) and fabs(copysign(X,Y)) patterns.
+    Value *FAbsArg;
+    if (match(Mag, m_Intrinsic<Intrinsic::floor>(m_FAbs(m_Value(FAbsArg)))) &&
+        stripSignOnlyFPOps(FAbsArg) == Sign) {
----------------
arsenm wrote:

Oh sorry, this stripSignOnlyFPOps is unnecessary. Those will be folded out separately already. You can just drop that.

The more general form would be to check if the sign bit is known 0 with computeKnownFPClass, but probably not worth it 

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


More information about the llvm-commits mailing list