From aae5d08a8d4dcee576a85ea76febe56c53675ef2 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Sat, 4 Apr 2020 09:16:07 +0200 Subject: [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441] My recent protected_set_expr_location changes work well when that function is called unconditionally, but as the testcase shows, the C++ FE has a few spots that do: if (!EXPR_HAS_LOCATION (stmt)) protected_set_expr_location (stmt, locus); or similar. Now, if we have for -g0 stmt of some expression that can have location and has != UNKNOWN_LOCATION, while -g instead has a STATEMENT_LIST containing some DEBUG_BEGIN_STMTs + that expression with that location, we don't call protected_set_expr_location in the -g0 case, but do call it in the -g case, because on the STATEMENT_LIST !EXPR_HAS_LOCATION. The following patch introduces a helper function which digs up the single expression of a STATEMENT_LIST and uses that expression in the EXPR_HAS_LOCATION check (plus changes protected_set_expr_location to also use that helper). Or do we want a further wrapper, perhaps C++ FE only, that would do this protected_set_expr_location_if_unset (stmt, locus)? 2020-04-04 Jakub Jelinek PR debug/94441 * tree-iterator.h (expr_single): Declare. * tree-iterator.c (expr_single): New function. * tree.h (protected_set_expr_location_if_unset): Declare. * tree.c (protected_set_expr_location): Use expr_single. (protected_set_expr_location_if_unset): New function. * parser.c (cp_parser_omp_for_loop): Use protected_set_expr_location_if_unset. * cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise. * g++.dg/opt/pr94441.C: New test. --- gcc/ChangeLog | 9 +++++++ gcc/cp/ChangeLog | 5 ++++ gcc/cp/cp-gimplify.c | 6 ++--- gcc/cp/parser.c | 3 +-- gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/g++.dg/opt/pr94441.C | 16 ++++++++++++ gcc/tree-iterator.c | 41 ++++++++++++++++++++++++++++++ gcc/tree-iterator.h | 1 + gcc/tree.c | 38 ++++++++++----------------- gcc/tree.h | 1 + 10 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr94441.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 25d1c5d1c38..fdb685a99d1 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2020-04-04 Jakub Jelinek + + PR debug/94441 + * tree-iterator.h (expr_single): Declare. + * tree-iterator.c (expr_single): New function. + * tree.h (protected_set_expr_location_if_unset): Declare. + * tree.c (protected_set_expr_location): Use expr_single. + (protected_set_expr_location_if_unset): New function. + 2020-04-03 Jeff Law PR rtl-optimization/92264 diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index fc3ad899a4a..14038ccfe90 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,10 @@ 2020-04-04 Jakub Jelinek + PR debug/94441 + * parser.c (cp_parser_omp_for_loop): Use + protected_set_expr_location_if_unset. + * cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise. + PR c++/94477 * pt.c (tsubst_expr) : Clear omp_parallel_combined_clauses. diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 3999695ae93..f326d71637b 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -226,8 +226,7 @@ genericize_if_stmt (tree *stmt_p) stmt = else_; else stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_); - if (!EXPR_HAS_LOCATION (stmt)) - protected_set_expr_location (stmt, locus); + protected_set_expr_location_if_unset (stmt, locus); *stmt_p = stmt; } @@ -248,8 +247,7 @@ genericize_cp_loop (tree *stmt_p, location_t start_locus, tree cond, tree body, tree stmt_list = NULL; tree debug_begin = NULL; - if (EXPR_LOCATION (incr) == UNKNOWN_LOCATION) - protected_set_expr_location (incr, start_locus); + protected_set_expr_location_if_unset (incr, start_locus); cp_walk_tree (&cond, cp_genericize_r, data, NULL); cp_walk_tree (&incr, cp_genericize_r, data, NULL); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index fc3c2976e3d..7e5921e039f 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -39149,8 +39149,7 @@ cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses, incr = cp_parser_omp_for_incr (parser, real_decl); else incr = cp_parser_expression (parser); - if (!EXPR_HAS_LOCATION (incr)) - protected_set_expr_location (incr, input_location); + protected_set_expr_location_if_unset (incr, input_location); } parse_close_paren: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 494533f2a55..c08bc0d68f7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2020-04-04 Jakub Jelinek + PR debug/94441 + * g++.dg/opt/pr94441.C: New test. + PR c++/94477 * g++.dg/gomp/pr94477.C: New test. diff --git a/gcc/testsuite/g++.dg/opt/pr94441.C b/gcc/testsuite/g++.dg/opt/pr94441.C new file mode 100644 index 00000000000..07ce43e04db --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr94441.C @@ -0,0 +1,16 @@ +// PR debug/94441 +// { dg-do compile } +// { dg-options "-O3 -fno-forward-propagate --param=max-cse-insns=0 -flive-range-shrinkage -std=c++17 -fcompare-debug" } + +template struct Same; +template struct Same {}; + +auto f() +{ + if constexpr (sizeof(int)==3) + return 42; + else + return 42L; +} + +Same s; diff --git a/gcc/tree-iterator.c b/gcc/tree-iterator.c index ddc908f387c..81833b592b5 100644 --- a/gcc/tree-iterator.c +++ b/gcc/tree-iterator.c @@ -354,4 +354,45 @@ expr_last (tree expr) return expr; } +/* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and + a single other stmt, return that other stmt (recursively). + If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or + multiple, return NULL_TREE. + Otherwise return EXPR. */ + +tree +expr_single (tree expr) +{ + if (expr == NULL_TREE) + return expr; + + if (TREE_CODE (expr) == STATEMENT_LIST) + { + /* With -gstatement-frontiers we could have a STATEMENT_LIST with + DEBUG_BEGIN_STMT(s) and only a single other stmt, which with + -g wouldn't be present and we'd have that single other stmt + directly instead. */ + struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr); + if (!n) + return NULL_TREE; + while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT) + { + n = n->next; + if (!n) + return NULL_TREE; + } + expr = n->stmt; + do + { + n = n->next; + if (!n) + return expr_single (expr); + } + while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT); + return NULL_TREE; + } + + return expr; +} + #include "gt-tree-iterator.h" diff --git a/gcc/tree-iterator.h b/gcc/tree-iterator.h index 0b36221150e..b16ff130728 100644 --- a/gcc/tree-iterator.h +++ b/gcc/tree-iterator.h @@ -119,5 +119,6 @@ extern void append_to_statement_list (tree, tree *); extern void append_to_statement_list_force (tree, tree *); extern tree expr_first (tree); extern tree expr_last (tree); +extern tree expr_single (tree); #endif /* GCC_TREE_ITERATOR_H */ diff --git a/gcc/tree.c b/gcc/tree.c index 0fe3afce1b6..fa956da28a4 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -5148,33 +5148,23 @@ protected_set_expr_location (tree t, location_t loc) SET_EXPR_LOCATION (t, loc); else if (t && TREE_CODE (t) == STATEMENT_LIST) { - /* With -gstatement-frontiers we could have a STATEMENT_LIST with - DEBUG_BEGIN_STMT(s) and only a single other stmt, which with - -g wouldn't be present and we'd have that single other stmt - directly instead. */ - struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (t); - if (!n) - return; - while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT) - { - n = n->next; - if (!n) - return; - } - tree t2 = n->stmt; - do - { - n = n->next; - if (!n) - { - protected_set_expr_location (t2, loc); - return; - } - } - while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT); + t = expr_single (t); + if (t && CAN_HAVE_LOCATION_P (t)) + SET_EXPR_LOCATION (t, loc); } } +/* Like PROTECTED_SET_EXPR_LOCATION, but only do that if T has + UNKNOWN_LOCATION. */ + +void +protected_set_expr_location_if_unset (tree t, location_t loc) +{ + t = expr_single (t); + if (t && !EXPR_HAS_LOCATION (t)) + protected_set_expr_location (t, loc); +} + /* Data used when collecting DECLs and TYPEs for language data removal. */ class free_lang_data_d diff --git a/gcc/tree.h b/gcc/tree.h index 85ce6b3bd6a..66dfa876256 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -1203,6 +1203,7 @@ get_expr_source_range (tree expr) } extern void protected_set_expr_location (tree, location_t); +extern void protected_set_expr_location_if_unset (tree, location_t); extern tree maybe_wrap_with_location (tree, location_t); -- 2.30.2