From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by sourceware.org (Postfix) with ESMTP id CD507385E036 for ; Thu, 2 Apr 2020 07:48:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CD507385E036 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-290-PxEZLGWyMhKLP-ni2KI6CA-1; Thu, 02 Apr 2020 03:48:10 -0400 X-MC-Unique: PxEZLGWyMhKLP-ni2KI6CA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E471118A6ECD for ; Thu, 2 Apr 2020 07:48:09 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-113-52.ams2.redhat.com [10.36.113.52]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7596760BF4; Thu, 2 Apr 2020 07:48:09 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id 0327m5n1021286; Thu, 2 Apr 2020 09:48:06 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id 0327m5hA021285; Thu, 2 Apr 2020 09:48:05 +0200 Date: Thu, 2 Apr 2020 09:48:05 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix further protected_set_expr_location related -fcompare-debug issues [PR94441] Message-ID: <20200402074805.GN2212@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 User-Agent: Mutt/1.11.3 (2019-02-01) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Spam-Status: No, score=-20.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Apr 2020 07:48:14 -0000 Hi! 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 !=3D 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). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 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-02 Jakub Jelinek =09PR debug/94441 =09* tree-iterator.h (expr_single): Declare. =09* tree-iterator.c (expr_single): New function. =09* tree.c (protected_set_expr_location): Use it. =09* parser.c (cp_parser_omp_for_loop): Use expr_single. =09* cp-gimplify.c (genericize_if_stmt, genericize_cp_loop): Likewise. =09* g++.dg/opt/pr94441.C: New test. --- gcc/tree-iterator.h.jj=092020-01-12 11:54:38.497381967 +0100 +++ gcc/tree-iterator.h=092020-04-01 16:58:00.034347283 +0200 @@ -119,5 +119,6 @@ extern void append_to_statement_list (tr extern void append_to_statement_list_force (tree, tree *); extern tree expr_first (tree); extern tree expr_last (tree); +extern tree expr_single (tree); =20 #endif /* GCC_TREE_ITERATOR_H */ --- gcc/tree-iterator.c.jj=092020-01-12 11:54:38.497381967 +0100 +++ gcc/tree-iterator.c=092020-04-01 17:05:23.388920122 +0200 @@ -354,4 +354,45 @@ expr_last (tree expr) return expr; } =20 +/* 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 =3D=3D NULL_TREE) + return expr; + + if (TREE_CODE (expr) =3D=3D STATEMENT_LIST) + { + /* With -gstatement-frontiers we could have a STATEMENT_LIST with +=09 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with +=09 -g wouldn't be present and we'd have that single other stmt +=09 directly instead. */ + struct tree_statement_list_node *n =3D STATEMENT_LIST_HEAD (expr); + if (!n) +=09return NULL_TREE; + while (TREE_CODE (n->stmt) =3D=3D DEBUG_BEGIN_STMT) +=09{ +=09 n =3D n->next; +=09 if (!n) +=09 return NULL_TREE; +=09} + expr =3D n->stmt; + do +=09{ +=09 n =3D n->next; +=09 if (!n) +=09 return expr_single (expr); +=09} + while (TREE_CODE (n->stmt) =3D=3D DEBUG_BEGIN_STMT); + return NULL_TREE; + } + + return expr; +} + #include "gt-tree-iterator.h" --- gcc/tree.c.jj=092020-03-26 10:35:29.984667559 +0100 +++ gcc/tree.c=092020-04-01 16:59:01.689453490 +0200 @@ -5148,30 +5148,9 @@ protected_set_expr_location (tree t, loc SET_EXPR_LOCATION (t, loc); else if (t && TREE_CODE (t) =3D=3D STATEMENT_LIST) { - /* With -gstatement-frontiers we could have a STATEMENT_LIST with -=09 DEBUG_BEGIN_STMT(s) and only a single other stmt, which with -=09 -g wouldn't be present and we'd have that single other stmt -=09 directly instead. */ - struct tree_statement_list_node *n =3D STATEMENT_LIST_HEAD (t); - if (!n) -=09return; - while (TREE_CODE (n->stmt) =3D=3D DEBUG_BEGIN_STMT) -=09{ -=09 n =3D n->next; -=09 if (!n) -=09 return; -=09} - tree t2 =3D n->stmt; - do -=09{ -=09 n =3D n->next; -=09 if (!n) -=09 { -=09 protected_set_expr_location (t2, loc); -=09 return; -=09 } -=09} - while (TREE_CODE (n->stmt) =3D=3D DEBUG_BEGIN_STMT); + t =3D expr_single (t); + if (t && CAN_HAVE_LOCATION_P (t)) +=09SET_EXPR_LOCATION (t, loc); } } =20 --- gcc/cp/parser.c.jj=092020-03-29 11:14:41.087556278 +0200 +++ gcc/cp/parser.c=092020-04-01 17:03:00.730988186 +0200 @@ -39149,8 +39149,9 @@ cp_parser_omp_for_loop (cp_parser *parse =09 incr =3D cp_parser_omp_for_incr (parser, real_decl); =09 else =09 incr =3D cp_parser_expression (parser); -=09 if (!EXPR_HAS_LOCATION (incr)) -=09 protected_set_expr_location (incr, input_location); +=09 if (tree incrl =3D expr_single (incr)) +=09 if (!EXPR_HAS_LOCATION (incrl)) +=09 protected_set_expr_location (incrl, input_location); =09} =20 parse_close_paren: --- gcc/cp/cp-gimplify.c.jj=092020-03-29 11:14:41.057556738 +0200 +++ gcc/cp/cp-gimplify.c=092020-04-01 17:01:28.916319192 +0200 @@ -226,8 +226,9 @@ genericize_if_stmt (tree *stmt_p) stmt =3D else_; else stmt =3D build3 (COND_EXPR, void_type_node, cond, then_, else_); - if (!EXPR_HAS_LOCATION (stmt)) - protected_set_expr_location (stmt, locus); + if (tree stmtl =3D expr_single (stmt)) + if (!EXPR_HAS_LOCATION (stmtl)) + protected_set_expr_location (stmtl, locus); *stmt_p =3D stmt; } =20 @@ -248,8 +249,9 @@ genericize_cp_loop (tree *stmt_p, locati tree stmt_list =3D NULL; tree debug_begin =3D NULL; =20 - if (EXPR_LOCATION (incr) =3D=3D UNKNOWN_LOCATION) - protected_set_expr_location (incr, start_locus); + if (tree incrl =3D expr_single (incr)) + if (!EXPR_HAS_LOCATION (incrl)) + protected_set_expr_location (incrl, start_locus); =20 cp_walk_tree (&cond, cp_genericize_r, data, NULL); cp_walk_tree (&incr, cp_genericize_r, data, NULL); --- gcc/testsuite/g++.dg/opt/pr94441.C.jj=092020-04-01 17:13:43.014677438 += 0200 +++ gcc/testsuite/g++.dg/opt/pr94441.C=092020-04-01 17:14:33.495946020 +020= 0 @@ -0,0 +1,16 @@ +// PR debug/94441 +// { dg-do compile } +// { dg-options "-O3 -fno-forward-propagate --param=3Dmax-cse-insns=3D0 -f= live-range-shrinkage -std=3Dc++17 -fcompare-debug" } + +template struct Same; +template struct Same {}; + +auto f() +{ + if constexpr (sizeof(int)=3D=3D3) + return 42; + else + return 42L; +} + +Same s; =09Jakub