From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7901) id 7B33F3858D34; Sat, 6 Apr 2024 11:11:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B33F3858D34 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712401860; bh=RmauCY2M11+sO2FQS6cuWcMc40ndYoQa+GX9l8xVU1g=; h=From:To:Subject:Date:From; b=DdnSZNWre0C4BLmoXwkbHl0mQmpMBn5gyOUQ8F6kSZfTmlXg/VNEPBdwUm+ztzST+ 5LNb/4ZStjA6LZrJPB/KZ+a2nkXE91O2IiIQfuVbUN/++UlMIoFWbzXBiWGytNhazH wO0dvAfuu0GUOKK4TVE87hnqJIHbcR2/wq8HUyuU= 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-9819] Copy condition->expr map when inlining [PR114599] X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= X-Git-Refname: refs/heads/master X-Git-Oldrev: 888bf719728e9ab1da22900d39956e0d80889e30 X-Git-Newrev: c6892a430a9752aea167265621c3ae7a3e11159f Message-Id: <20240406111100.7B33F3858D34@sourceware.org> Date: Sat, 6 Apr 2024 11:11:00 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c6892a430a9752aea167265621c3ae7a3e11159f commit r14-9819-gc6892a430a9752aea167265621c3ae7a3e11159f Author: Jørgen Kvalsvik Date: Fri Apr 5 21:42:07 2024 +0200 Copy condition->expr map when inlining [PR114599] When a function is tree-inlined, copy the condition -> expression mapping from the inlined function into the caller, shifted so uids are not mixed. Tree inlining was always problematic under condition coverage - either through a nullptr dereference (triggered by the test case), or through quietly mixing caller conditions with the callee conditions. PR middle-end/114599 gcc/ChangeLog: * tree-inline.cc (add_local_variables): Copy cond_uids mappings. gcc/testsuite/ChangeLog: * gcc.misc-tests/gcov-pr114599.c: New test. Diff: --- gcc/testsuite/gcc.misc-tests/gcov-pr114599.c | 25 +++++++++++++++++++++++++ gcc/tree-inline.cc | 20 +++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/gcc.misc-tests/gcov-pr114599.c b/gcc/testsuite/gcc.misc-tests/gcov-pr114599.c new file mode 100644 index 00000000000..e4c78c9c121 --- /dev/null +++ b/gcc/testsuite/gcc.misc-tests/gcov-pr114599.c @@ -0,0 +1,25 @@ +/* PR middle-end/114599 */ +/* { dg-do compile } */ +/* { dg-options "-fcondition-coverage" } */ + +/* Check that a function with a condition inlined into a function without a + conditional works. When inlining happens the condition -> expression + mapping must be carried over. */ + +extern int type; + +void fn (void); + +__attribute__((always_inline)) +inline void +do_all_fn_doall_arg (void) +{ + if (type) + fn (); +} + +void +do_all_fn_LHASH_DOALL_ARG_arg2 (void) +{ + do_all_fn_doall_arg (); +} diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc index eebcea8a029..b18917707cc 100644 --- a/gcc/tree-inline.cc +++ b/gcc/tree-inline.cc @@ -4659,7 +4659,8 @@ prepend_lexical_block (tree current_block, tree new_block) BLOCK_SUPERCONTEXT (new_block) = current_block; } -/* Add local variables from CALLEE to CALLER. */ +/* Add local variables from CALLEE to CALLER. If set for condition coverage, + copy basic condition -> expression mapping to CALLER. */ static inline void add_local_variables (struct function *callee, struct function *caller, @@ -4689,6 +4690,23 @@ 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