From patchwork Tue Nov 14 22:08:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Huber X-Patchwork-Id: 165107 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp2232918rwb; Tue, 14 Nov 2023 14:09:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFSpKYlIi3yJ11TbZdw2+QQq4+Frkuz8FXtomQ1IKUQhOsLnmBOBObJPXAvADGpxcDCNb5Z X-Received: by 2002:a05:620a:4807:b0:77b:c7f8:8a10 with SMTP id eb7-20020a05620a480700b0077bc7f88a10mr4390537qkb.72.1699999755488; Tue, 14 Nov 2023 14:09:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699999755; cv=pass; d=google.com; s=arc-20160816; b=kJ2u+NBoTolBcbfANTvap+NhPCe3WKGdktSUSUqMptdmyU6s8BYeNG9jhYyn7j1ds3 gHcHgYE0oAYtuKcvlhfwFxVawCvMD940jPIXCCQSnsIafeSVu898nm6zdCOWPK4eQemB fPf740Aw1rstyVbcII03uJJKqx1MPOTxGQspm/dmbUL0GTqdihQq1/WWhkNRtqxw/Gte lN9Sv8jfjWx68gxHEHvItKHgDgFJIl/DlGbhIm8JJq88z3l1ESowIotgNpNgVmMsCAB3 UHGt5l+gWzOpuArYTALHfymgTqkVLe4L2koPT6/yG97lf1ZcHiquTypmlmVMDq7enHEm CbRQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :arc-filter:dmarc-filter:delivered-to; bh=dIZoWb3lEuJRsrFimuPTclh9CJO9QdJiSV/rSIndg6Q=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=oSrOs++vBsZxvT4tV2eswxeW5YhCB5MeZDp1nFNQFJpHGn2i9kJBGgHgchetTLi3Vf 56DH5jZH55T/A4iPruemZj3Sj8HWS+9J41B4r7WMjDa/4wB+0pJwZdz+oGvNMXuufGZi /YeKPd+6+i14IeH/YbUdee+ONnVYFv52sV6/fmGwaa5+tnAnIdVrc29Z8nbhnojdlP/i kayN/sqP38ahlxbkLznfXisznOA83oqrm5hzYWkxDcbgmnHiSfIeQeUU1pjyZn6FV55j fArFefzED7jDjsa1uWGARhWWOkPpFdgDx8q+hzSaGMuAWMd4TdCCHBGxlBdQl4Zr8BOQ c4dQ== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d20-20020a05620a241400b0076dac93a764si7969683qkn.379.2023.11.14.14.09.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 14:09:15 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7E6C3857C72 for ; Tue, 14 Nov 2023 22:09:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) by sourceware.org (Postfix) with ESMTPS id C91913858D37 for ; Tue, 14 Nov 2023 22:08:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C91913858D37 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embedded-brains.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embedded-brains.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C91913858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=85.10.215.148 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699999719; cv=none; b=t/UedLxR7tGWQtWDdwVswZ/B/9aczUqSEtvqCSt23kP7e/2NzX07qI5a7a3TWWAcpH8w08o2bTF3mZCmRZI7OKGe8VhwY4n6Fd7iZa3JzP5Al5lZFv+QgX5BO9pPrI20zVx4shYr2RKUNEURt/BU8hFmtdaJrBA01bR6jSVgEEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699999719; c=relaxed/simple; bh=srB7PDIhuHv8i4ciHhzay7E9s1jD1e+U3T1sC5w5UOs=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=lnDrtvUJ+nN6LY/CFyJPk2qf+NrJmHRwAFcoExSljioSrn71TJHqnjiJrf3Cme++zSU5h+OY19DYxjWK5e9v2To7UqnbmJfzs3pi5BWffkBFvYZBinl7IW7M593nF2CL7xBqgMa9ggjEz1F2yPC3Z3M0DgmXfo1MSpUkqv0NJ0I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from sslproxy05.your-server.de ([78.46.172.2]) by dedi548.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r31aG-0000T8-Mw for gcc-patches@gcc.gnu.org; Tue, 14 Nov 2023 23:08:36 +0100 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r31aG-000QaZ-Je for gcc-patches@gcc.gnu.org; Tue, 14 Nov 2023 23:08:36 +0100 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id C86504801A4 for ; Tue, 14 Nov 2023 23:08:35 +0100 (CET) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavis, port 10032) with ESMTP id LA9U-ufri_Ii for ; Tue, 14 Nov 2023 23:08:33 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 04B654801D1 for ; Tue, 14 Nov 2023 23:08:32 +0100 (CET) X-Virus-Scanned: amavis at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavis, port 10026) with ESMTP id KQhfZ-nxSQEo for ; Tue, 14 Nov 2023 23:08:31 +0100 (CET) Received: from zimbra.eb.localhost (unknown [10.10.171.18]) by mail.embedded-brains.de (Postfix) with ESMTPSA id 2E17A4801A4 for ; Tue, 14 Nov 2023 23:08:31 +0100 (CET) From: Sebastian Huber To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/4] gcov: Add gen_counter_update() Date: Tue, 14 Nov 2023 23:08:24 +0100 Message-Id: <20231114220825.22074-4-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231114220825.22074-1-sebastian.huber@embedded-brains.de> References: <20231114220825.22074-1-sebastian.huber@embedded-brains.de> MIME-Version: 1.0 X-Authenticated-Sender: smtp-embedded@poldi-networks.de X-Virus-Scanned: Clear (ClamAV 0.103.10/27093/Tue Nov 14 09:38:37 2023) X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782578943532523496 X-GMAIL-MSGID: 1782578943532523496 Move the counter update to the new gen_counter_update() helper function. Use it in gimple_gen_edge_profiler() and gimple_gen_time_profiler(). The resulting gimple instructions should be identical with the exception of the removed unshare_expr() call. The unshare_expr() call was used in gimple_gen_edge_profiler(). gcc/ChangeLog: * tree-profile.cc (gen_assign_counter_update): New. (gen_counter_update): Likewise. (gimple_gen_edge_profiler): Use gen_counter_update(). (gimple_gen_time_profiler): Likewise. --- gcc/tree-profile.cc | 133 +++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 71 deletions(-) diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc index da300d5f9e8d..24805ff905c7 100644 --- a/gcc/tree-profile.cc +++ b/gcc/tree-profile.cc @@ -235,47 +235,78 @@ gimple_init_gcov_profiler (void) } } -/* Output instructions as GIMPLE trees to increment the edge - execution count, and insert them on E. We rely on - gsi_insert_on_edge to preserve the order. */ +/* If RESULT is not null, then output instructions as GIMPLE trees to assign + the updated counter from CALL of FUNC to RESULT. Insert the CALL and the + optional assignment instructions to GSI. Use NAME for temporary values. */ -void -gimple_gen_edge_profiler (int edgeno, edge e) +static inline void +gen_assign_counter_update (gimple_stmt_iterator *gsi, gcall *call, tree func, + tree result, const char *name) { - tree one; + if (result) + { + tree result_type = TREE_TYPE (TREE_TYPE (func)); + tree tmp = make_temp_ssa_name (result_type, NULL, name); + gimple_set_lhs (call, tmp); + gsi_insert_after (gsi, call, GSI_NEW_STMT); + gassign *assign = gimple_build_assign (result, tmp); + gsi_insert_after (gsi, assign, GSI_NEW_STMT); + } + else + gsi_insert_after (gsi, call, GSI_NEW_STMT); +} - one = build_int_cst (gcov_type_node, 1); +/* Output instructions as GIMPLE trees to increment the COUNTER. If RESULT is + not null, then assign the updated counter value to RESULT. Insert the + instructions to GSI. Use NAME for temporary values. */ + +static inline void +gen_counter_update (gimple_stmt_iterator *gsi, tree counter, tree result, + const char *name) +{ + tree type = gcov_type_node; + tree addr = build_fold_addr_expr (counter); + tree one = build_int_cst (type, 1); + tree relaxed = build_int_cst (integer_type_node, MEMMODEL_RELAXED); if (flag_profile_update == PROFILE_UPDATE_ATOMIC) { /* __atomic_fetch_add (&counter, 1, MEMMODEL_RELAXED); */ - tree addr = tree_coverage_counter_addr (GCOV_COUNTER_ARCS, edgeno); - tree f = builtin_decl_explicit (TYPE_PRECISION (gcov_type_node) > 32 - ? BUILT_IN_ATOMIC_FETCH_ADD_8: - BUILT_IN_ATOMIC_FETCH_ADD_4); - gcall *stmt = gimple_build_call (f, 3, addr, one, - build_int_cst (integer_type_node, - MEMMODEL_RELAXED)); - gsi_insert_on_edge (e, stmt); + tree f = builtin_decl_explicit (TYPE_PRECISION (type) > 32 + ? BUILT_IN_ATOMIC_ADD_FETCH_8: + BUILT_IN_ATOMIC_ADD_FETCH_4); + gcall *call = gimple_build_call (f, 3, addr, one, relaxed); + gen_assign_counter_update (gsi, call, f, result, name); } else { - tree ref = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno); - tree gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node, - NULL, "PROF_edge_counter"); - gassign *stmt1 = gimple_build_assign (gcov_type_tmp_var, ref); - gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node, - NULL, "PROF_edge_counter"); - gassign *stmt2 = gimple_build_assign (gcov_type_tmp_var, PLUS_EXPR, - gimple_assign_lhs (stmt1), one); - gassign *stmt3 = gimple_build_assign (unshare_expr (ref), - gimple_assign_lhs (stmt2)); - gsi_insert_on_edge (e, stmt1); - gsi_insert_on_edge (e, stmt2); - gsi_insert_on_edge (e, stmt3); + tree tmp1 = make_temp_ssa_name (type, NULL, name); + gassign *assign1 = gimple_build_assign (tmp1, counter); + gsi_insert_after (gsi, assign1, GSI_NEW_STMT); + tree tmp2 = make_temp_ssa_name (type, NULL, name); + gassign *assign2 = gimple_build_assign (tmp2, PLUS_EXPR, tmp1, one); + gsi_insert_after (gsi, assign2, GSI_NEW_STMT); + gassign *assign3 = gimple_build_assign (counter, tmp2); + gsi_insert_after (gsi, assign3, GSI_NEW_STMT); + if (result) + { + gassign *assign4 = gimple_build_assign (result, tmp2); + gsi_insert_after (gsi, assign4, GSI_NEW_STMT); + } } } +/* Output instructions as GIMPLE trees to increment the edge + execution count, and insert them on E. */ + +void +gimple_gen_edge_profiler (int edgeno, edge e) +{ + gimple_stmt_iterator gsi = gsi_last (PENDING_STMT (e)); + tree counter = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno); + gen_counter_update (&gsi, counter, NULL_TREE, "PROF_edge_counter"); +} + /* Emits code to get VALUE to instrument at GSI, and returns the variable containing the value. */ @@ -507,56 +538,16 @@ gimple_gen_time_profiler (unsigned tag) tree original_ref = tree_coverage_counter_ref (tag, 0); tree ref = force_gimple_operand_gsi (&gsi, original_ref, true, NULL_TREE, true, GSI_SAME_STMT); - tree one = build_int_cst (type, 1); /* Emit: if (counters[0] != 0). */ gcond *cond = gimple_build_cond (EQ_EXPR, ref, build_int_cst (type, 0), NULL, NULL); gsi_insert_before (&gsi, cond, GSI_NEW_STMT); - gsi = gsi_start_bb (update_bb); - /* Emit: counters[0] = ++__gcov_time_profiler_counter. */ - if (flag_profile_update == PROFILE_UPDATE_ATOMIC) - { - tree ptr = make_temp_ssa_name (build_pointer_type (type), NULL, - "PROF_time_profiler_counter_ptr"); - tree addr = build1 (ADDR_EXPR, TREE_TYPE (ptr), - tree_time_profiler_counter); - gassign *assign = gimple_build_assign (ptr, NOP_EXPR, addr); - gsi_insert_before (&gsi, assign, GSI_NEW_STMT); - tree f = builtin_decl_explicit (TYPE_PRECISION (gcov_type_node) > 32 - ? BUILT_IN_ATOMIC_ADD_FETCH_8: - BUILT_IN_ATOMIC_ADD_FETCH_4); - gcall *stmt = gimple_build_call (f, 3, ptr, one, - build_int_cst (integer_type_node, - MEMMODEL_RELAXED)); - tree result_type = TREE_TYPE (TREE_TYPE (f)); - tree tmp = make_temp_ssa_name (result_type, NULL, "PROF_time_profile"); - gimple_set_lhs (stmt, tmp); - gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); - tmp = make_temp_ssa_name (type, NULL, "PROF_time_profile"); - assign = gimple_build_assign (tmp, NOP_EXPR, - gimple_call_lhs (stmt)); - gsi_insert_after (&gsi, assign, GSI_NEW_STMT); - assign = gimple_build_assign (original_ref, tmp); - gsi_insert_after (&gsi, assign, GSI_NEW_STMT); - } - else - { - tree tmp = make_temp_ssa_name (type, NULL, "PROF_time_profile"); - gassign *assign = gimple_build_assign (tmp, tree_time_profiler_counter); - gsi_insert_before (&gsi, assign, GSI_NEW_STMT); - - tmp = make_temp_ssa_name (type, NULL, "PROF_time_profile"); - assign = gimple_build_assign (tmp, PLUS_EXPR, gimple_assign_lhs (assign), - one); - gsi_insert_after (&gsi, assign, GSI_NEW_STMT); - assign = gimple_build_assign (original_ref, tmp); - gsi_insert_after (&gsi, assign, GSI_NEW_STMT); - assign = gimple_build_assign (tree_time_profiler_counter, tmp); - gsi_insert_after (&gsi, assign, GSI_NEW_STMT); - } + gsi = gsi_start_bb (update_bb); + gen_counter_update (&gsi, tree_time_profiler_counter, original_ref, + "PROF_time_profile"); } /* Output instructions as GIMPLE trees to increment the average histogram