[PATCH] New command line option: -frounding-math

Roger Sayle roger@eyesopen.com
Wed Sep 3 13:29:00 GMT 2003


This is the first of three or four patches that are intended to resolve
PR optimization/576.  The first step is to introduce a command line
option that can be used to control whether the current compilation unit
should guard against transformations that depend upon the floating
point rounding mode.  This provides a handle with which to control such
optimizations.

Initially, this flag is used to replace -funsafe-math-optimizations to
modify the behaviour of HONOR_SIGN_DEPENDENT_ROUNDING.  A follow-up
patch with disable compile-time evaluation of floating point expressions
such as 1.0/3.0, whose result may depend upon the dynamically specified
IEEE/FPU rounding mode at run-time.

Perhaps controversial, but certainly noteworthy, is that -frounding-math
is false by default.  This is allowed by the ISO C standards that state
that the initial value of "pragma STDC FENV_ACCESS" is implementation
defined.  Up until now we've disabled optimization based up sign-dependent
rounding not because they we disallowed by the language specification,
but because we had no way of disabling them if we needed to.  One
immediate benefit is that this implicitly enables several more floating
point optimizations by default.  This will also save us from having to
disable many of the uses of the PowerPC's fused-multiply-add patterns
(but more on that in the follow-up patches).

The -frounding-math command line option is available to all front-ends
and is disabled automatically by -ffast-math.

Although I don't immediately intend to implement ISO C99's FENV_ACCESS
pragma, this patch is clearly a first step in that direction.  I know
nothing of how pragmas are implemented, but a first-cut at a compliant
implementation would be to set flag_rounding_math whenever GCC sees a
"#pragma STDC FENV_ACCESS ON" but never clear it.  Yes, this has some
quality of implementation issues, but it does implement the required
functionality.


The following patch has been tested on i686-pc-linux-gnu with a complete
"make bootstrap", all languages except treelang, and regression tested
with a top-level "make -k check" with no new failures.

Ok for mainline?


2003-09-03  Roger Sayle  <roger@eyesopen.com>

	* toplev.c (flag_rounding_math): New global variable.
	(f_options): Add to the list of language independent options.
	* flags.h (flag_rounding_math): Prototype here.
	(HONOR_SIGN_DEPENDENT_ROUNDING): Use flag_rounding_math instead.
	* common.opt (frounding-math): New common command line option.
	* opts.c (common_handle_option): Handle OPT_frounding_math.
	(set_fast_math_flags): -ffast-math clears flag_rounding_math.

	* doc/invoke.texi: Document this new command line option.


Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.819
diff -c -3 -p -r1.819 toplev.c
*** toplev.c	29 Aug 2003 22:44:05 -0000	1.819
--- toplev.c	3 Sep 2003 04:16:24 -0000
*************** int flag_finite_math_only = 0;
*** 633,638 ****
--- 633,643 ----

  int flag_trapping_math = 1;

+ /* Nonzero means disable transformations that assume default floating
+    point rounding behavior.  */
+
+ int flag_rounding_math = 0;
+
  /* Nonzero means disable transformations observable by signaling NaNs.
     This option implies that any operation on an IEEE signaling NaN can
     generate a (user-visible) trap.  */
*************** static const lang_independent_options f_
*** 1113,1118 ****
--- 1118,1124 ----
    { "guess-branch-probability", &flag_guess_branch_prob, 1 },
    {"math-errno", &flag_errno_math, 1 },
    {"trapping-math", &flag_trapping_math, 1 },
+   {"rounding-math", &flag_rounding_math, 1 },
    {"unsafe-math-optimizations", &flag_unsafe_math_optimizations, 1 },
    {"signaling-nans", &flag_signaling_nans, 1 },
    {"bounds-check", &flag_bounds_check, 1 },
Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.119
diff -c -3 -p -r1.119 flags.h
*** flags.h	8 Aug 2003 19:22:42 -0000	1.119
--- flags.h	3 Sep 2003 04:16:24 -0000
*************** extern int flag_finite_math_only;
*** 363,368 ****
--- 363,373 ----

  extern int flag_trapping_math;

+ /* Nonzero means disable transformations that assume default floating
+    point rounding behavior.  */
+
+ extern int flag_rounding_math;
+
  /* 0 means straightforward implementation of complex divide acceptable.
     1 means wide ranges of inputs must work for complex divide.
     2 means C99-like requirements for complex divide (not yet implemented).  */
*************** extern const char *flag_random_seed;
*** 716,721 ****
  /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
     and the rounding mode is important.  */
  #define HONOR_SIGN_DEPENDENT_ROUNDING(MODE) \
!   (MODE_HAS_SIGN_DEPENDENT_ROUNDING (MODE) && !flag_unsafe_math_optimizations)

  #endif /* ! GCC_FLAGS_H */
--- 721,726 ----
  /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
     and the rounding mode is important.  */
  #define HONOR_SIGN_DEPENDENT_ROUNDING(MODE) \
!   (MODE_HAS_SIGN_DEPENDENT_ROUNDING (MODE) && flag_rounding_math)

  #endif /* ! GCC_FLAGS_H */
Index: common.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/common.opt,v
retrieving revision 1.15
diff -c -3 -p -r1.15 common.opt
*** common.opt	8 Aug 2003 19:22:42 -0000	1.15
--- common.opt	3 Sep 2003 04:16:24 -0000
*************** frerun-loop-opt
*** 552,557 ****
--- 552,561 ----
  Common
  Run the loop optimizer twice

+ frounding-math
+ Common
+ Disable optimizations that assume default FP rounding behavior
+
  fsched-interblock
  Common
  Enable scheduling across basic blocks
Index: opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/opts.c,v
retrieving revision 1.36
diff -c -3 -p -r1.36 opts.c
*** opts.c	19 Aug 2003 21:04:38 -0000	1.36
--- opts.c	3 Sep 2003 04:16:24 -0000
*************** common_handle_option (size_t scode, cons
*** 1220,1227 ****
        flag_rerun_loop_opt = value;
        break;

      case OPT_fsched_interblock:
!       flag_schedule_interblock= value;
        break;

      case OPT_fsched_spec:
--- 1220,1231 ----
        flag_rerun_loop_opt = value;
        break;

+     case OPT_frounding_math:
+       flag_rounding_math = value;
+       break;
+
      case OPT_fsched_interblock:
!       flag_schedule_interblock = value;
        break;

      case OPT_fsched_spec:
*************** set_fast_math_flags (int set)
*** 1547,1553 ****
    flag_finite_math_only = set;
    flag_errno_math = !set;
    if (set)
!     flag_signaling_nans = 0;
  }

  /* Return true iff flags are set as if -ffast-math.  */
--- 1551,1560 ----
    flag_finite_math_only = set;
    flag_errno_math = !set;
    if (set)
!     {
!       flag_signaling_nans = 0;
!       flag_rounding_math = 0;
!     }
  }

  /* Return true iff flags are set as if -ffast-math.  */
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.330
diff -c -3 -p -r1.330 invoke.texi
*** doc/invoke.texi	22 Aug 2003 22:36:42 -0000	1.330
--- doc/invoke.texi	3 Sep 2003 04:16:27 -0000
*************** in the following sections.
*** 280,286 ****
  -freduce-all-givs  -fregmove  -frename-registers @gol
  -freorder-blocks  -freorder-functions @gol
  -frerun-cse-after-loop  -frerun-loop-opt @gol
! -fschedule-insns  -fschedule-insns2 @gol
  -fno-sched-interblock  -fno-sched-spec  -fsched-spec-load @gol
  -fsched-spec-load-dangerous  -fsched2-use-superblocks @gol
  -fsched2-use-traces  -fsignaling-nans @gol
--- 280,286 ----
  -freduce-all-givs  -fregmove  -frename-registers @gol
  -freorder-blocks  -freorder-functions @gol
  -frerun-cse-after-loop  -frerun-loop-opt @gol
! -frounding-math -fschedule-insns  -fschedule-insns2 @gol
  -fno-sched-interblock  -fno-sched-spec  -fsched-spec-load @gol
  -fsched-spec-load-dangerous  -fsched2-use-superblocks @gol
  -fsched2-use-traces  -fsignaling-nans @gol
*************** them to store all pertinent intermediate
*** 4260,4267 ****
  @item -ffast-math
  @opindex ffast-math
  Sets @option{-fno-math-errno}, @option{-funsafe-math-optimizations}, @*
! @option{-fno-trapping-math}, @option{-ffinite-math-only} and @*
! @option{-fno-signaling-nans}.

  This option causes the preprocessor macro @code{__FAST_MATH__} to be defined.

--- 4260,4267 ----
  @item -ffast-math
  @opindex ffast-math
  Sets @option{-fno-math-errno}, @option{-funsafe-math-optimizations}, @*
! @option{-fno-trapping-math}, @option{-ffinite-math-only},
! @option{-fno-rounding-math} and @option{-fno-signaling-nans}.

  This option causes the preprocessor macro @code{__FAST_MATH__} to be defined.

*************** an exact implementation of IEEE or ISO r
*** 4324,4329 ****
--- 4324,4349 ----
  math functions.

  The default is @option{-ftrapping-math}.
+
+ @item -frounding-math
+ @opindex frounding-math
+ Disable transformations and optimizations that assume default floating
+ point rounding behavior.  This is round-to-zero for all floating point
+ to integer conversions, and round-to-nearest for all other arithmetic
+ truncations.  This option should be specified for programs that change
+ the FP rounding mode dynamically, or that may be executed with a
+ non-default rounding mode.  This option disables constant folding of
+ floating point expressions at compile-time (which may be affected by
+ rounding mode) and arithmetic transformations that are unsafe in the
+ presence of sign-dependent rounding modes.
+
+ The default is @option{-fno-rounding-math}.
+
+ This option is experimental and does not currently guarantee to
+ disable all GCC optimizations that are affected by rounding mode.
+ Future versions of gcc may provide finer control of this setting
+ using C99's @code{FENV_ACCESS} pragma.  This command line option
+ will be used to specify the default state for @code{FENV_ACCESS}.

  @item -fsignaling-nans
  @opindex fsignaling-nans


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833



More information about the Gcc-patches mailing list