diff mbox series

[pushed] c++: enable -fdelete-dead-exceptions by default

Message ID 20210714185916.350035-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: enable -fdelete-dead-exceptions by default | expand

Commit Message

Jason Merrill July 14, 2021, 6:59 p.m. UTC
As I was discussing with richi, I don't think it makes sense to protect
calls to pure/const functions from DCE just because they aren't explicitly
declared noexcept.  PR100382 indicates that there are different
considerations for Go, which has non-call exceptions.  But still turn the
flag off for that specific testcase.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/c-family/ChangeLog:

	* c-opts.c (c_common_post_options): Set -fdelete-dead-exceptions.
---
 gcc/doc/invoke.texi                     | 6 ++++--
 gcc/c-family/c-opts.c                   | 4 ++++
 gcc/testsuite/g++.dg/torture/pr100382.C | 1 +
 3 files changed, 9 insertions(+), 2 deletions(-)


base-commit: 6d1cdb27828d2ef1ae1ab0209836646a269b9610

Comments

Richard Biener July 15, 2021, 7:40 a.m. UTC | #1
On Wed, 14 Jul 2021, Jason Merrill wrote:

> As I was discussing with richi, I don't think it makes sense to protect
> calls to pure/const functions from DCE just because they aren't explicitly
> declared noexcept.  PR100382 indicates that there are different
> considerations for Go, which has non-call exceptions.  But still turn the
> flag off for that specific testcase.

I don't disagree.  Note this means that

void test_div (int x, int y)
{
  x / y;
}

will no longer throw externally with -fnon-call-exceptions
unless you now specify -fno-delete-dead-exceptions
(in a separate thread we question what the -fnon-call-exceptions 
-fno-exceptions state we "support" actually means).

IIRC -fdelete-dead-exceptions was specifically added for 
-fnon-call-exceptions and dead code that could trap (we don't
generally preserve possibly trapping stmts as traps are not
observable but generally result from triggering behavior that
is undefined in terms of language definition).

But yes, -fdelete-dead-exceptions naturally applies to
const/pure function calls.

Since you change c_common_post_options you also affect the C and
Objective C/C++ compliers so you might want to adjust your
documentation change.  I guess cross-referencing 
-fdelete-dead-exceptions in the -fnon-call-exceptions documentation
makes sense as well.

Richard.

> Tested x86_64-pc-linux-gnu, applying to trunk.
> 
> gcc/c-family/ChangeLog:
> 
> 	* c-opts.c (c_common_post_options): Set -fdelete-dead-exceptions.
> ---
>  gcc/doc/invoke.texi                     | 6 ++++--
>  gcc/c-family/c-opts.c                   | 4 ++++
>  gcc/testsuite/g++.dg/torture/pr100382.C | 1 +
>  3 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index e67d47af676..ea8812425e9 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -16335,8 +16335,10 @@ arbitrary signal handlers such as @code{SIGALRM}.
>  @opindex fdelete-dead-exceptions
>  Consider that instructions that may throw exceptions but don't otherwise
>  contribute to the execution of the program can be optimized away.
> -This option is enabled by default for the Ada compiler, as permitted by
> -the Ada language specification.
> +This does not affect calls to functions except those with the
> +@code{pure} or @code{const} attributes.
> +This option is enabled by default for the Ada and C++ compilers, as permitted by
> +the language specifications.
>  Optimization passes that cause dead exceptions to be removed are enabled independently at different optimization levels.
>  
>  @item -funwind-tables
> diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
> index 60b5802722c..1212edd1b28 100644
> --- a/gcc/c-family/c-opts.c
> +++ b/gcc/c-family/c-opts.c
> @@ -1015,6 +1015,10 @@ c_common_post_options (const char **pfilename)
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
>  		       optimize >= 2 && cxx_dialect >= cxx11);
>  
> +  /* It's OK to discard calls to pure/const functions that throw.  */
> +  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
> +		       flag_delete_dead_exceptions, true);
> +
>    if (cxx_dialect >= cxx11)
>      {
>        /* If we're allowing C++0x constructs, don't warn about C++98
> diff --git a/gcc/testsuite/g++.dg/torture/pr100382.C b/gcc/testsuite/g++.dg/torture/pr100382.C
> index ffc4182cfce..eac5743b956 100644
> --- a/gcc/testsuite/g++.dg/torture/pr100382.C
> +++ b/gcc/testsuite/g++.dg/torture/pr100382.C
> @@ -1,4 +1,5 @@
>  // { dg-do run }
> +// { dg-additional-options -fno-delete-dead-exceptions }
>  
>  int x, y;
>  int __attribute__((pure,noinline)) foo () { if (x) throw 1; return y; }
> 
> base-commit: 6d1cdb27828d2ef1ae1ab0209836646a269b9610
>
diff mbox series

Patch

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index e67d47af676..ea8812425e9 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -16335,8 +16335,10 @@  arbitrary signal handlers such as @code{SIGALRM}.
 @opindex fdelete-dead-exceptions
 Consider that instructions that may throw exceptions but don't otherwise
 contribute to the execution of the program can be optimized away.
-This option is enabled by default for the Ada compiler, as permitted by
-the Ada language specification.
+This does not affect calls to functions except those with the
+@code{pure} or @code{const} attributes.
+This option is enabled by default for the Ada and C++ compilers, as permitted by
+the language specifications.
 Optimization passes that cause dead exceptions to be removed are enabled independently at different optimization levels.
 
 @item -funwind-tables
diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 60b5802722c..1212edd1b28 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -1015,6 +1015,10 @@  c_common_post_options (const char **pfilename)
   SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
 		       optimize >= 2 && cxx_dialect >= cxx11);
 
+  /* It's OK to discard calls to pure/const functions that throw.  */
+  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+		       flag_delete_dead_exceptions, true);
+
   if (cxx_dialect >= cxx11)
     {
       /* If we're allowing C++0x constructs, don't warn about C++98
diff --git a/gcc/testsuite/g++.dg/torture/pr100382.C b/gcc/testsuite/g++.dg/torture/pr100382.C
index ffc4182cfce..eac5743b956 100644
--- a/gcc/testsuite/g++.dg/torture/pr100382.C
+++ b/gcc/testsuite/g++.dg/torture/pr100382.C
@@ -1,4 +1,5 @@ 
 // { dg-do run }
+// { dg-additional-options -fno-delete-dead-exceptions }
 
 int x, y;
 int __attribute__((pure,noinline)) foo () { if (x) throw 1; return y; }