public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Make flag_trapping_math a non-binary Boolean.
@ 2021-09-25  7:32 Roger Sayle
  2021-09-27 20:04 ` Joseph Myers
  0 siblings, 1 reply; 5+ messages in thread
From: Roger Sayle @ 2021-09-25  7:32 UTC (permalink / raw)
  To: 'GCC Patches'; +Cc: 'Eric Botcazou'

[-- Attachment #1: Type: text/plain, Size: 1605 bytes --]


Normally Boolean options/flags in GCC take the values zero or one.
This patch tweaks flag_trapping_math to take the values 0 or 65535.
More accurately it introduces a new trapping_math_model enumeration in
flag-types.h, and uses this to allow front-ends to (potentially) control
which expressions may be constant folded at compile-time by the middle-end.
Floating point/language experts may recognize these flags (bits) as being
modelled upon (extended) FENV_ACCESS.

This instalment simply introduces the necessary infrastructure without
yet changing any functionality.  The test "if (flag_trapping_math)" will
remain perfectly valid (but pessimistic).  The goal is to allow time
for out-of-tree front-ends (modula-2, rust, etc.) to update themselves,
if required, and to confirm that this change doesn't introduce problems
for LTO, or elsewhere.

This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap"
and "make -k check", all languages including Ada, with no new failures.
Ok for mainline?


2021-09-25  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* flag-types.h (trapping_math_model): New enumeration (of bits)
	specifying possible floating-point (and integer) exceptions/traps.
	* common.opt (ftrapping-math): Specify UInteger and initialize to
	flag_trapping_math to TRAPPING_MATH_DEFAULT.
	* toplev.c (process_options): The option -fsignaling-nans should
	set flag_trapping_math to TRAPPING_MATH_DEFAULT.

gcc/ada/ChangeLog
	* gcc-interface/misc.c (gnat_init_gcc_fp): Set flag_trapping_math
	to TRAPPING_MATH_DEFAULT (instead of 1) if S'Machine_Overflow.

Roger
--


[-- Attachment #2: patcht.txt --]
[-- Type: text/plain, Size: 2688 bytes --]

diff --git a/gcc/ada/gcc-interface/misc.c b/gcc/ada/gcc-interface/misc.c
index 96199bd..93cbc71 100644
--- a/gcc/ada/gcc-interface/misc.c
+++ b/gcc/ada/gcc-interface/misc.c
@@ -451,7 +451,7 @@ gnat_init_gcc_fp (void)
   /* Assume that FP operations can trap if S'Machine_Overflow is true,
      but don't override the user if not.  */
   if (Machine_Overflows_On_Target)
-    flag_trapping_math = 1;
+    flag_trapping_math = TRAPPING_MATH_DEFAULT;
   else if (!global_options_set.x_flag_trapping_math)
     flag_trapping_math = 0;
 }
diff --git a/gcc/common.opt b/gcc/common.opt
index b921f5e..314f9a7 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2750,7 +2750,7 @@ generate them instead of using descriptors.
 ; (user-visible) trap.  This is the case, for example, in nonstop
 ; IEEE 754 arithmetic.
 ftrapping-math
-Common Var(flag_trapping_math) Init(1) Optimization SetByCombined
+Common Var(flag_trapping_math) Init(TRAPPING_MATH_DEFAULT) Optimization SetByCombined UInteger
 Assume floating-point operations can trap.
 
 ftrapv
diff --git a/gcc/flag-types.h b/gcc/flag-types.h
index 5bd1f77..98b9ff0 100644
--- a/gcc/flag-types.h
+++ b/gcc/flag-types.h
@@ -481,6 +481,33 @@ enum openacc_privatization
   OPENACC_PRIVATIZATION_NOISY
 };
 
+/* Trapping math exception classes.  */
+enum trapping_math_model
+{
+  TRAPPING_MATH_NONE = 0,
+  TRAPPING_MATH_QNANOP = 1UL << 0,
+  TRAPPING_MATH_SNANOP = 1UL << 1,
+  TRAPPING_MATH_QNANCMP = 1UL << 2,
+  TRAPPING_MATH_SNANCMP = 1UL << 3,
+  TRAPPING_MATH_INTCONV = 1UL << 4,
+  TRAPPING_MATH_SQRTNEG = 1UL << 5,
+  TRAPPING_MATH_LIBMFUN = 1UL << 6,
+  TRAPPING_MATH_FDIVZERO = 1UL << 7,
+  TRAPPING_MATH_IDIVZERO = 1UL << 8,
+  TRAPPING_MATH_FPDENORM = 1UL << 9,
+  TRAPPING_MATH_OVERFLOW = 1UL << 10,
+  TRAPPING_MATH_UNDERFLOW = 1UL << 11,
+  TRAPPING_MATH_INFDIVINF = 1UL << 12,
+  TRAPPING_MATH_INFSUBINF = 1UL << 13,
+  TRAPPING_MATH_INFMULZERO = 1UL << 14,
+  TRAPPING_MATH_ZERODIVZERO = 1UL << 15,
+
+  TRAPPING_MATH_DEFAULT = (1UL << 16) - 1,
+
+  TRAPPING_MATH_INEXACT = 1UL << 16,
+  TRAPPING_MATH_TRAPV = 1UL << 17
+};
+
 #endif
 
 #endif /* ! GCC_FLAG_TYPES_H */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 14d1335..6cd71cc 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1664,7 +1664,7 @@ process_options (void)
 
   /* The presence of IEEE signaling NaNs, implies all math can trap.  */
   if (flag_signaling_nans)
-    flag_trapping_math = 1;
+    flag_trapping_math = TRAPPING_MATH_DEFAULT;
 
   /* We cannot reassociate if we want traps or signed zeros.  */
   if (flag_associative_math && (flag_trapping_math || flag_signed_zeros))

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-09-28 21:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-25  7:32 [PATCH] Make flag_trapping_math a non-binary Boolean Roger Sayle
2021-09-27 20:04 ` Joseph Myers
2021-09-28 11:34   ` Roger Sayle
2021-09-28 12:15     ` Richard Biener
2021-09-28 21:27     ` Joseph Myers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).