This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Patch, Fortran] Enable flag_associative_math (almost) by default
- From: Tobias Burnus <burnus at net-b dot de>
- To: gfortran <fortran at gcc dot gnu dot org>, gcc patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 06 May 2010 14:24:44 +0200
- Subject: [Patch, Fortran] Enable flag_associative_math (almost) by default
The GCC manual claims:
-fassociative-math
Allow re-association of operands in series of floating-point operations.
This violates the ISO C and C++ language standard by possibly changing
computation result. [...] This option requires that both
-fno-signed-zeros and -fno-trapping-math be in effect. [...] For Fortran
the option is automatically enabled when both -fno-signed-zeros and
-fno-trapping-math are in effect.
And related the gfortran manual has:
-fno-protect-parens
By default the parentheses in expression are honoured for all
optimization levels such that the compiler does not do any
re-association. [...] Note that for the reassociation optimization
-fno-signed-zeros and -fno-trapping-math need to be in effect.
If one now looks for flag_associative_math one sees that gfortran does
not touch it at all and also toplev.c has only the trapping/signed zero
check and no special case for Fortran.
Well, the following patch does what is written in the manual; as
-funsave-math-optimization implies both
-fno-trapping-math/-fno-signed-zeros and -fassociate-math -- and as
-ffast-math implies -funsave-math-optimizations, the practical impact of
the change is small.
Note: Even with -ffast-math -fassociate-math, the parentheses are
protected (unless -fno-protect-parens is used).
Build on x86-64-linux.
OK for the trunk?
Tobias
PS: Thanks go to Dominique pointed out the error in the documentation.
PPS: There is also a "(!flag_trapping_math && !flag_signed_zeros)" check
in toplev.c, which prevents enabling -freassociative-math - but the
function prints an ignore warning. Otherwise, one could simply set the
flag to 1 in the option init section.
2010-05-06 Tobias Burnus <burnus@net-b.de>
* options.c (gfc_init_options,gfc_post_options): Enable
flag_associative_math by default.
Index: gcc/fortran/options.c
===================================================================
--- gcc/fortran/options.c (revision 159087)
+++ gcc/fortran/options.c (working copy)
@@ -133,6 +133,7 @@ gfc_init_options (unsigned int argc, con
gfc_option.coarray = GFC_FCOARRAY_NONE;
flag_errno_math = 0;
+ flag_associative_math = -1;
set_default_std_flags ();
@@ -246,6 +247,11 @@ gfc_post_options (const char **pfilename
if (flag_lto || flag_whopr)
gfc_option.flag_whole_file = 1;
+ /* Fortran allows associative math - but we cannot reassociate if
+ we want traps or signed zeros. Cf. also flag_protect_parens. */
+ if (flag_associative_math == -1)
+ flag_associative_math = (!flag_trapping_math && !flag_signed_zeros);
+
/* -fbounds-check is equivalent to -fcheck=bounds */
if (flag_bounds_check)
gfc_option.rtcheck |= GFC_RTCHECK_BOUNDS;