Bug 30568 - -ftrapping-math should prevent folding/dead code stripping of some builtins
Summary: -ftrapping-math should prevent folding/dead code stripping of some builtins
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: 16989 29186
  Show dependency treegraph
 
Reported: 2007-01-24 02:09 UTC by Geoff Keating
Modified: 2009-12-29 21:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-12-29 21:51:37


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Geoff Keating 2007-01-24 02:09:49 UTC
This program:

#include <stdio.h>
#include <math.h>
#include <fenv.h>

main()
{
unsigned flags;

feclearexcept(FE_ALL_EXCEPT);
(void)acos(1.1);
flags = fetestexcept(FE_ALL_EXCEPT);

printf("(void)acos( %e ) gives flags 0x%8x\n", 1.1, flags & FE_ALL_EXCEPT);
}

when compiled with -ftrapping-math, should show that some flags are set, FE_INVALID I think (maybe others).  Instead it shows that no flags are set, because GCC has completely folded out the call to acos().
Comment 1 Geoff Keating 2007-01-24 02:11:02 UTC
Other builtins where this can apply are pow() and lround().
Comment 2 joseph@codesourcery.com 2007-01-24 02:37:20 UTC
Subject: Re:   New: -ftrapping-math should prevent
 folding/dead code stripping of some builtins

On Wed, 24 Jan 2007, geoffk at gcc dot gnu dot org wrote:

> when compiled with -ftrapping-math, should show that some flags are set,
> FE_INVALID I think (maybe others).  Instead it shows that no flags are set,
> because GCC has completely folded out the call to acos().

I think this is the same as bug 29186.

Comment 3 Andrew Pinski 2007-01-24 06:54:07 UTC
Part of the problem here is that darwin turns on no math-errno by default which turns the builtin functions into pure functions and since we don't use the  result, we DCE it.
pure functions are not allowed to trap IIRC the rules.
Comment 4 Richard Biener 2007-01-24 10:25:28 UTC
This is an (easier) variant of PR29186.  Confirmed.
Comment 5 Richard B. Kreckel 2009-02-15 20:09:08 UTC
(In reply to comment #4)
> This is an (easier) variant of PR29186.  Confirmed.

The difference between this bug and PR29186 is that this one here can be explained by failing to correctly treat the exception flags at compile-time (after const folding), whereas PR29186 is all about code movements that make these flags unreliable during runtime.
Comment 6 Francois-Xavier Coudert 2009-12-29 21:51:37 UTC
Confirming this as generic, non Darwin-specific bug. On i686-linux with current trunk:

$ cat a.c
#include <stdio.h>
#include <math.h>
#include <fenv.h>

main()
{
unsigned flags;

feclearexcept(FE_ALL_EXCEPT);
(void)acos(1.1);
flags = fetestexcept(FE_ALL_EXCEPT);

printf("(void)acos( %e ) gives flags 0x%8x\n", 1.1, flags & FE_ALL_EXCEPT);
}
$ gcc a.c -ftrapping-math -O0 -fno-math-errno -lm && ./a.out
(void)acos( 1.100000e+00 ) gives flags 0x       0