Bug 31548 - __builtin_cabsf(z) squared should be optimized
Summary: __builtin_cabsf(z) squared should be optimized
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2007-04-12 15:27 UTC by Tobias Burnus
Modified: 2007-04-12 15:53 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-04-12 15:41:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-04-12 15:27:07 UTC
The following Fortran program

program test
 complex :: z
 real :: r
 r = abs(z)**2
 print *, r
end program test

produces the following tree:
    D.1352 = __builtin_cabsf (z);
    D.1353 = D.1352 * D.1352;
    r = D.1353;

The complex value is naively calculated as:
  sqrt( (_Real_ z)*(_Real_ z) + (_Imag_ z)*(_Imag_ z) )

However, since the value is squared afterwards, the square root can be simply removed.

Calculating |...|^2 is not that uncommon in some codes.
Comment 1 Richard Biener 2007-04-12 15:41:02 UTC
Confirmed.
Comment 2 Uroš Bizjak 2007-04-12 15:53:01 UTC
(In reply to comment #0)

> The complex value is naively calculated as:
>   sqrt( (_Real_ z)*(_Real_ z) + (_Imag_ z)*(_Imag_ z) )
> 
> However, since the value is squared afterwards, the square root can be simply
> removed.

This optimization can also be applied to squared hypot().