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

`cabs (x + 0i)`and `cabs(0.0 + xi)` should be optimized to `abs(x)` even without `-ffast-math`

Open pinskia opened this issue 8 months ago • 5 comments

Take the fortran testcase:

subroutine foo(a,b,n)
  real(kind(1d0))::a
  real(kind(1d0))::b
  integer::i,n
  complex(kind(1d0))::c

  c = a
     b=abs(c)

end subroutine foo

This should be just optimized to abs(a) rather than a call to cabs.

C testcase:

double
foo (double e)
{
  _Complex double x = __builtin_complex(0.0,e);
  return __builtin_cabs(e);
}
double
foo1 (double e)
{
  return __builtin_cabs(e);
}

Likewise for hypot too.

double
fooh (double e)
{
  return __builtin_hypot(e, 0.0);
}

The definition of hypot in C99 allows for this.

GCC has been handling foo1 function since before GCC 3.4.0. fooh function since GCC 4.4.0. This is more likely to show up with fortran code than C or C++ code.

pinskia avatar Jul 01 '24 18:07 pinskia