From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7901) id 7CFE5385840B; Tue, 9 Apr 2024 07:57:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CFE5385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712649479; bh=s2Mc7mhwHgU0tdSCMB4kcSzyf3YymSElaBY1MatGe/Q=; h=From:To:Subject:Date:From; b=q2MixWaZ1zQrTkDdJdd9QbdC8k6dDh/wmGTjPzkyTCun/dm4MM9P8sFfJvArYvcRH +Las2gQwuDYwdCVRDFGHYAMYqPuIUlcZVjR8kivvWB5VDWD3owCJzFDckFvDKWGyvW TrsWMU+Wk+XPPFb+45gRkmPRQyokg96UT0JHF3ug= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: J?rgen Kvalsvik To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9863] Add tree-inlined gconds to caller cond->expr map X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= X-Git-Refname: refs/heads/master X-Git-Oldrev: 21c9fd9688d9de9562b3cb491e4ab50ce09e663a X-Git-Newrev: 2daeb89d6f025d6daf7e560575863b3280120be8 Message-Id: <20240409075759.7CFE5385840B@sourceware.org> Date: Tue, 9 Apr 2024 07:57:59 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2daeb89d6f025d6daf7e560575863b3280120be8 commit r14-9863-g2daeb89d6f025d6daf7e560575863b3280120be8 Author: Jørgen Kvalsvik Date: Mon Apr 8 09:28:27 2024 +0200 Add tree-inlined gconds to caller cond->expr map Properly add the condition -> expression mapping of inlined gconds from the caller into the callee map. This is a fix for PR114599 that works beyond fixing the segfault, as the previous fixed copied references to the source gconds, not the deep copied ones that end up in the calle body. The new tests checks this, both in the case of a calle without conditions (which triggered the segfault), and a test that shows that conditions are properly mapped, and not mixed. PR middle-end/114599 gcc/ChangeLog: * tree-inline.cc (copy_bb): Copy cond_uids into callee. (prepend_lexical_block): Remove outdated comment. (add_local_variables): Remove bad cond_uids copy. gcc/testsuite/ChangeLog: * gcc.misc-tests/gcov-19.c: New test. Diff: --- gcc/testsuite/gcc.misc-tests/gcov-19.c | 52 ++++++++++++++++++++++++++++++++++ gcc/tree-inline.cc | 43 +++++++++++++++------------- 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/gcc/testsuite/gcc.misc-tests/gcov-19.c b/gcc/testsuite/gcc.misc-tests/gcov-19.c index b83a38531ba..44e2f135e2a 100644 --- a/gcc/testsuite/gcc.misc-tests/gcov-19.c +++ b/gcc/testsuite/gcc.misc-tests/gcov-19.c @@ -1468,6 +1468,54 @@ mcdc026e (int a, int b, int c, int d, int e) return x + y; } +__attribute__((always_inline)) +inline int +mcdc027_inlined (int a) +{ + if (a) + x = a; + else + x = 1; + + return a + 1; +} + +/* Check that conditions in a function inlined into a function without + conditionals works. */ +void +mcdc027a (int a) /* conditions(1/2) false(0) */ + /* conditions(end) */ +{ + mcdc027_inlined (a); +} + +/* Check that conditions in a function inlined into a function with + conditionals works and that the conditions are not mixed up. */ +void +mcdc027b (int a) /* conditions(1/2) false(0) */ + /* conditions(end) */ +{ + int v = mcdc027_inlined (a); + + if (v > 10) /* conditions(1/2) true(0) */ + /* condiitions(end) */ + x = 10; +} + +/* Check that conditions in a function inlined into a function with + conditionals works and that the conditions are not mixed up. Use different + number of conditions to distinguish the expressions. */ +void +mcdc027c (int a, int b) /* conditions(1/2) false(0) */ + /* conditions(end) */ +{ + int v = mcdc027_inlined (a); + + if (v > 10 && b) /* conditions(1/4) true(0 1) false(1) */ + /* condiitions(end) */ + x = 10; +} + int main () { mcdc001a (0, 1); @@ -1721,6 +1769,10 @@ int main () mcdc026e (1, 1, 1, 0, 1); mcdc026e (1, 1, 0, 0, 1); mcdc026e (1, 1, 1, 1, 1); + + mcdc027a (1); + mcdc027b (1); + mcdc027c (1, 0); } /* { dg-final { run-gcov conditions { --conditions gcov-19.c } } } */ diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc index b18917707cc..5f852885e7f 100644 --- a/gcc/tree-inline.cc +++ b/gcc/tree-inline.cc @@ -2049,6 +2049,17 @@ copy_bb (copy_body_data *id, basic_block bb, copy_gsi = gsi_start_bb (copy_basic_block); + unsigned min_cond_uid = 0; + if (id->src_cfun->cond_uids) + { + if (!cfun->cond_uids) + cfun->cond_uids = new hash_map (); + + for (auto itr : *id->src_cfun->cond_uids) + if (itr.second >= min_cond_uid) + min_cond_uid = itr.second + 1; + } + for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple_seq stmts; @@ -2076,6 +2087,18 @@ copy_bb (copy_body_data *id, basic_block bb, if (gimple_nop_p (stmt)) continue; + /* If -fcondition-coverage is used, register the inlined conditions + in the cond->expression mapping of the caller. The expression tag + is shifted conditions from the two bodies are not mixed. */ + if (id->src_cfun->cond_uids && is_a (orig_stmt)) + { + gcond *orig_cond = as_a (orig_stmt); + gcond *cond = as_a (stmt); + unsigned *v = id->src_cfun->cond_uids->get (orig_cond); + if (v) + cfun->cond_uids->put (cond, *v + min_cond_uid); + } + gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt); @@ -4659,8 +4682,7 @@ prepend_lexical_block (tree current_block, tree new_block) BLOCK_SUPERCONTEXT (new_block) = current_block; } -/* Add local variables from CALLEE to CALLER. If set for condition coverage, - copy basic condition -> expression mapping to CALLER. */ +/* Add local variables from CALLEE to CALLER. */ static inline void add_local_variables (struct function *callee, struct function *caller, @@ -4690,23 +4712,6 @@ add_local_variables (struct function *callee, struct function *caller, } add_local_decl (caller, new_var); } - - /* If -fcondition-coverage is used and the caller has conditions, copy the - mapping into the caller but and the end so the caller and callee - expressions aren't mixed. */ - if (callee->cond_uids) - { - if (!caller->cond_uids) - caller->cond_uids = new hash_map (); - - unsigned dst_max_uid = 0; - for (auto itr : *callee->cond_uids) - if (itr.second >= dst_max_uid) - dst_max_uid = itr.second + 1; - - for (auto itr : *callee->cond_uids) - caller->cond_uids->put (itr.first, itr.second + dst_max_uid); - } } /* Add to BINDINGS a debug stmt resetting SRCVAR if inlining might