llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LLVM][IR] LLVM fails to optimize copysign(floor(fabs(x)), x) to trunc(x)

Open johnplatts opened this issue 6 months ago • 1 comments

Here is a link to a C snippet demonstrating the missed optimization of copysign(floor(fabs(x)), x) to trunc(x) by Clang: https://godbolt.org/z/PxG1fxbzM

Here is a link to a LLVM IR snippet demonstrating the missed optimization of copysign(floor(fabs(x)), x) to trunc(x): https://alive2.llvm.org/ce/z/EBCyrQ

If x >= 0 is true, then floor(x) == trunc(x) is true as floor(x) will be rounded towards zero if x >= 0 is true.

If x is NaN, then both floor(x) and trunc(x) will return NaN.

Otherwise, if x < 0 is true, then floor(-x) == trunc(-x) will be true as -x > 0 will be true if x < 0 is true. -floor(-x) == -trunc(-x) == trunc(x) will be true if x < 0 as -trunc(-x) == trunc(x) is true for all non-NaN values of x.

If x is equal to negative zero, then trunc(x) will return negative zero.

Combining the above cases, trunc(x) has the same magnitude as floor(abs(x)) and trunc(x) has the same sign as x, making copysign(floor(fabs(x)), x) being equivalent to trunc(x).

johnplatts avatar May 29 '26 22:05 johnplatts

hey can you assign this issue to me ?

AnkitDubeycs25 avatar May 30 '26 05:05 AnkitDubeycs25