[llvm] [InstCombine] Fold copysign(floor(fabs(X)), X) to trunc(X) (PR #200836)
Aayush Shrivastava via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 2 12:47:05 PDT 2026
================
@@ -3116,6 +3116,16 @@ 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) --> trunc(X)
+ // Requires nnan: for NaN X the source returns a NaN with X's sign bit
+ // preserved (via copysign), but trunc(X) may return any NaN, making the
+ // target more undefined.
+ if (II->hasNoNaNs() &&
+ match(Mag, m_Intrinsic<Intrinsic::floor>(m_FAbs(m_Specific(Sign))))) {
----------------
iamaayushrivastava wrote:
Thanks for the suggestion! Updated the fold to `copysign(trunc(X), X`) by replacing only the magnitude operand. This keeps the copysign alive so it's implicit fabs on the magnitude handles NaN correctly without requiring `nnan`.
https://github.com/llvm/llvm-project/pull/200836
More information about the llvm-commits
mailing list