]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
forwprop: Don't do copy-prop-aggregates from statements that could throw [PR120599]
authorAndrew Pinski <quic_apinski@quicinc.com>
Mon, 28 Jul 2025 03:37:55 +0000 (20:37 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 8 Aug 2025 21:30:28 +0000 (14:30 -0700)
In the testcase provided, currently we lose the landing pad for the exception that could
throw from the aggregate load as we remove one copy and the second statement where load
happens was not marked as throwable before so the landing pad for that internal throw is
now gone.

The fix is to ignore statements that could throw (internally or externally).

PR tree-optimization/120599

gcc/ChangeLog:

* tree-ssa-forwprop.cc (optimize_agr_copyprop): Don't try to copy
from statements that throw.

gcc/testsuite/ChangeLog:

* g++.dg/torture/noncall-eh-1.C: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/testsuite/g++.dg/torture/noncall-eh-1.C [new file with mode: 0644]
gcc/tree-ssa-forwprop.cc

diff --git a/gcc/testsuite/g++.dg/torture/noncall-eh-1.C b/gcc/testsuite/g++.dg/torture/noncall-eh-1.C
new file mode 100644 (file)
index 0000000..ea8fd79
--- /dev/null
@@ -0,0 +1,26 @@
+// { dg-do compile }
+// For slim LTO there's no optimized dump
+// { dg-skip-if "" { *-*-* } { "-flto" } { "" } }
+// { dg-additional-options "-fnon-call-exceptions -fexceptions -fdump-tree-optimized-eh" }
+
+// PR tree-optimization/120599
+// Copying prop for aggregates should not touch `a = *__val` since that statement
+//  can throw (internally) so we need to be able to keep the landing pad.
+
+struct RefitOption {
+  char subtype;
+  int string;
+} n;
+void h(RefitOption) __attribute__((nothrow));
+void k(RefitOption *__val, RefitOption a)
+{
+  try {
+    a = *__val;
+    RefitOption __trans_tmp_2 = a;
+    h(__trans_tmp_2);
+  }
+  catch(...){}
+}
+
+// Make sure There is a landing pad for the non-call exception from the aggregate load.
+// { dg-final { scan-tree-dump "LP " "optimized" } }
index 156ea32286766837402bc8218d7b37baba090efe..3d38d88844bff1a9a65394cfa793e6ad593fc65f 100644 (file)
@@ -1417,6 +1417,10 @@ optimize_agr_copyprop (gimple_stmt_iterator *gsip)
   if (gimple_has_volatile_ops (stmt))
     return false;
 
+  /* Can't prop if the statement could throw.  */
+  if (stmt_could_throw_p (cfun, stmt))
+    return false;
+
   tree dest = gimple_assign_lhs (stmt);
   tree src = gimple_assign_rhs1 (stmt);
   /* If the statement is `src = src;` then ignore it. */