[PATCH] PR tree-optimization/38943: Preserve trapping instructions with -fnon-call-exceptions

Roger Sayle roger@nextmovesoftware.com
Thu Jul 8 09:53:37 GMT 2021


This patch addresses PR tree-optimization/38943 where gcc may optimize
away trapping instructions even when -fnon-call-exceptions is specified.
Interestingly this only affects the C compiler (when -fexceptions is not
specified) as g++ (or -fexceptions) supports C++-style exception handling,
where -fnon-call-exceptions triggers the stmt_could_throw_p machinery.
Without -fexceptions, trapping instructions aren't always considered
visible side-effects.

This patch fixes this in two place.  Firstly, gimple_has_side_effects
is tweaked such that gimple_could_trap_p is considered a side-effect
if the current function can throw non-call exceptions.  And secondly,
check_stmt in ipa-pure-const.c is tweaked such that a function
containing a trapping statement is considered to have a side-effect
with -fnon-call-exceptions, and therefore cannot be pure or const.

Calling gimple_could_trap_p (which previously took a non-const gimple)
from gimple_has_side_effects (which takes a const gimple) required
improving the const-safety of gimple_could_trap_p (a relatively minor
tweak) and its prototypes.  Hopefully this is considered a clean-up/
improvement.

This patch has been tested on x86_64-pc-linux-gnu with a "make
bootstrap" and "make -k check" with no new failures.  This should
be relatively safe, as there are no changes in behaviour unless
the user explicitly specifies -fnon-call-exceptions, when the C
compiler then behaves more like the C++/Ada compiler.

Ok for mainline?


2021-07-08  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	PR tree-optimization/38943
	* gimple.c (gimple_has_side_effects): Consider trapping to
	be a side-effect when -fnon-call-exceptions is specified.
	(gimple_coult_trap_p_1):  Make S argument a "const gimple*".
	Preserve constness in call to gimple_asm_volatile_p.
	(gimple_could_trap_p): Make S argument a "const gimple*".
	* gimple.h (gimple_could_trap_p_1, gimple_could_trap_p):
	Update function prototypes.
	* ipa-pure-const.c (check_stmt): When the current function
	can throw non-call exceptions, a trapping statement should
	be considered a side-effect, so the function is neither
	const nor pure.

gcc/testsuite/ChangeLog
	PR tree-optimization/38943
	* gcc.dg/pr38943.c: New test case.

Roger
--
Roger Sayle
NextMove Software
Cambridge, UK

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patche4b.txt
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20210708/530c363b/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pr38943.c
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20210708/530c363b/attachment-0001.c>


More information about the Gcc-patches mailing list