https://gcc.gnu.org/g:95c85b404da4fe6eea7b7f3b51e1270b77f0dab8

commit r17-3902-g95c85b404da4fe6eea7b7f3b51e1270b77f0dab8
Author: Andrea Pinski <[email protected]>
Date:   Tue Sep 1 23:07:29 2026 -0700

    eh: Use tree_could_throw_1/lhs_could_trap_p for lhs in non-call exception 
case [PR127183]
    
    Since `this` argument was added as a non-trapping, we incorrectly had 
thought
    it would not cause an non-call exception when it was on the LHS.  With the 
fix for
    PR127133, we can change the places which were checking for trapping on the 
lhs
    to use lhs_could_trap_p/tree_could_throw_1 instead of using 
tree_could_throw_p.
    That fixes the non-call exceptions on the lhs of assignments with respect to
    this argument.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/127183
    
    gcc/ChangeLog:
    
            * tree-eh.cc (lower_eh_constructs_2): Use lhs_could_trap_p for lhs.
            (stmt_could_throw_1_p): Use tree_could_throw_1 for lhs.
            (tree_could_throw_p): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/eh/noncall-this-method-1.C: New test.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/testsuite/g++.dg/eh/noncall-this-method-1.C | 25 +++++++++++++++++++++++++
 gcc/tree-eh.cc                                  |  6 +++---
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/g++.dg/eh/noncall-this-method-1.C 
b/gcc/testsuite/g++.dg/eh/noncall-this-method-1.C
new file mode 100644
index 000000000000..544bf0396b87
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/noncall-this-method-1.C
@@ -0,0 +1,25 @@
+// PR tree-optimization/127183
+// { dg-do compile }
+// { dg-options "-O2 -fnon-call-exceptions -fdump-tree-optimized" }
+
+struct s1
+{
+    int f();
+    int a;
+};
+
+int s1::f()
+{
+    try {
+      this->a = 1;
+    }catch(...)
+    {
+        __builtin_trap ();
+    }
+    return 0;
+}
+
+// An write access to this should be still considered as trapping
+// and an throwable for non-call exceptions.
+
+// { dg-final { scan-tree-dump "__builtin_trap " "optimized" } } */
diff --git a/gcc/tree-eh.cc b/gcc/tree-eh.cc
index 2d2d391f0a97..d2c2f3b3157d 100644
--- a/gcc/tree-eh.cc
+++ b/gcc/tree-eh.cc
@@ -2118,7 +2118,7 @@ lower_eh_constructs_2 (struct leh_state *state, 
gimple_stmt_iterator *gsi)
       if (stmt_could_throw_p (cfun, stmt)
          && gimple_has_lhs (stmt)
          && gimple_stmt_may_fallthru (stmt)
-         && !tree_could_throw_p (gimple_get_lhs (stmt))
+         && !lhs_could_trap_p (gimple_get_lhs (stmt))
          && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
        {
          tree lhs = gimple_get_lhs (stmt);
@@ -3057,7 +3057,7 @@ stmt_could_throw_1_p (gassign *stmt)
     }
 
   /* First check the LHS.  */
-  if (tree_could_trap_p (gimple_assign_lhs (stmt)))
+  if (tree_could_trap_1 (gimple_assign_lhs (stmt), true))
     return true;
 
   /* Check if the main expression may trap.  */
@@ -3144,7 +3144,7 @@ tree_could_throw_p (tree t)
   if (TREE_CODE (t) == MODIFY_EXPR)
     {
       if (cfun->can_throw_non_call_exceptions
-          && tree_could_trap_p (TREE_OPERAND (t, 0)))
+         && tree_could_trap_1 (TREE_OPERAND (t, 0), true))
         return true;
       t = TREE_OPERAND (t, 1);
     }

Reply via email to