From patchwork Mon Apr 8 14:18:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= X-Patchwork-Id: 88177 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B5AE1385B50A for ; Mon, 8 Apr 2024 14:19:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx.kolabnow.com (mx.kolabnow.com [212.103.80.154]) by sourceware.org (Postfix) with ESMTPS id 8D6FD3858D32 for ; Mon, 8 Apr 2024 14:18:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D6FD3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=lambda.is Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=lambda.is ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8D6FD3858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.103.80.154 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712585919; cv=none; b=OCwNl5AJTfzYB6dnB0Jb7geiLP4cKKF4pikBIjR39+adRlIQiH6r85toQuTpEjk/q8g886HPYwW+hG1Kh180Gz3eFKiv94hinW8I5ThgTI+enEcevpOyhiZd77ciIP8vQ0ITS+QgwdwkEGVvJ0mHTh+xh0lJYnbYY6TX+jEGhK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712585919; c=relaxed/simple; bh=vrCL3gABywPdjDGZstbBkOfJGxad9MXwD/A9j5TVtpE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YglciYZoj9R4Yeypg/HD1CUCNmftj23I272bBEcJ/hVJHcTzciK5oVM4DTcCQ5pjujrKWOa1tWIYWdfKcD8fLX0eiieP6MEADubE6Q9qWlmCW4hX18AC+n0cDIozO1A/ocm+xaeobX9BmihZS+wnYF2OXpold+jleFhFW2feVDU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id 386AD30D84B3; Mon, 8 Apr 2024 16:18:33 +0200 (CEST) Authentication-Results: ext-mx-out013.mykolab.com (amavis); dkim=pass (4096-bit key) reason="pass (just generated, assumed good)" header.d=kolabnow.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1712585910; x=1714400311; bh=O33mcM6wI5gXOrzAfhC+vXYFvX4OlZBno0u+sNlIszc=; b=kMwzAONWvP42 9sVQkRh3Xc5qPq1HUt+dIL77rjaVkTXk17WbtQfsOmSTWlp4br60gFvC2ePTuOte HXpge0SMSGxBanUiHrc5aOcQqzmxmZv6ISXQAGwqSdalBBTWnOrqAMpEHLGc6n9R cXmafBUKO073DJLvCSFwKRKVGzQBcrsyxTSHUks0g9e2Lf7pfBZAOleTPQQ24Zvn M25zoM2XxV/ZiVaj0uNcwslQ+gD/vIiQULd4zc+AzpFP8UW5XXeMFt4/E94bcLm2 p/M1oG/PiSdiy0Ah3TLb3aVPKzLa7i8TeUSIpPBeHbAz9QpGEQMeaWlEL44+XLgD BhlYByWAmNVRi7YohBpk4N/XQUeGItp+QkDqi+CbC6iDj0BDoM25jBCN7tNfBHOP oCXbtZ1YsKwBqQul7Myc1bFjSt72TNbalLP5qnUcIZFMrlOZvwljQBC5ucyWelB3 /bTL6tHVgXDfkSZO7XaBhTK+QyO8LsswA+wF440Q4kYbt00g0P3LGlCT9LNVZii+ UGrcUiCrrilCp0VhJZF8Kpb8zhgwSevAFVQtyS2YRfFicCHFzWRX9iOdcc+w56Rw a0gdlYJHW1wcV1UvRCzkMvWzUqtekSYaUmYFup6yaktuf4DD3TwKcIxh5IKPR/d3 M5/bbn5fHmnJVNlzWwEjgdm5U/vrQrc= X-Virus-Scanned: amavis at mykolab.com X-Spam-Score: -1 X-Spam-Level: X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out013.mykolab.com [127.0.0.1]) (amavis, port 10024) with ESMTP id xuf03GG0YsJr; Mon, 8 Apr 2024 16:18:30 +0200 (CEST) Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id 02E2330D84A6; Mon, 8 Apr 2024 16:18:29 +0200 (CEST) Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id B3C2E211474C; Mon, 8 Apr 2024 16:18:29 +0200 (CEST) From: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= To: gcc-patches@gcc.gnu.org Cc: hjl.tools@gmail.com, rguenther@suse.de, hubicka@ucw.cz, sam@gentoo.org, =?utf-8?q?J=C3=B8rgen_Kvalsvik?= Subject: [PATCH 1/2] Add tree-inlined gconds to caller cond->expr map Date: Mon, 8 Apr 2024 16:18:14 +0200 Message-Id: <20240408141815.127984-2-j@lambda.is> In-Reply-To: <20240408141815.127984-1-j@lambda.is> References: <20240408141815.127984-1-j@lambda.is> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org 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. --- gcc/testsuite/gcc.misc-tests/gcov-19.c | 37 ++++++++++++++++++++++ gcc/tree-inline.cc | 43 ++++++++++++++------------ 2 files changed, 61 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..78769fa57b8 100644 --- a/gcc/testsuite/gcc.misc-tests/gcov-19.c +++ b/gcc/testsuite/gcc.misc-tests/gcov-19.c @@ -1468,6 +1468,40 @@ 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; +} + int main () { mcdc001a (0, 1); @@ -1721,6 +1755,9 @@ int main () mcdc026e (1, 1, 1, 0, 1); mcdc026e (1, 1, 0, 0, 1); mcdc026e (1, 1, 1, 1, 1); + + mcdc027a (1); + mcdc027b (1); } /* { 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