From patchwork Mon Jun 19 07:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 1796345 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=JSRg16Fy; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ql26s75ffz20Wk for ; Mon, 19 Jun 2023 17:52:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F00083858C60 for ; Mon, 19 Jun 2023 07:52:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F00083858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687161156; bh=bR4Yl5t2KLXaC4DjPdLCO6IwktcWmYvqrKVMGPeSNpU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JSRg16Fym7xBZiNhB5N3LHyufF1+1eSZJE8NWp/HwUT9QCcsPGBThiiB53tVIJZb/ 244ZJnBnlPLppYJBN+DZMktihoT6tqPU8eoCw/VSiARfuLR4ZP4G7K39Bn+En7GkeO w4F7oiMS3zpOMeuFsGl8/YZXdRO2EkVKukdHSHoM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id 690FA3858D28 for ; Mon, 19 Jun 2023 07:52:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 690FA3858D28 Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id 99C1128AEB9; Mon, 19 Jun 2023 09:52:16 +0200 (CEST) Date: Mon, 19 Jun 2023 09:52:16 +0200 To: gcc-patches@gcc.gnu.org Subject: Do not account __builtin_unreachable guards in inliner Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jan Hubicka via Gcc-patches From: Jan Hubicka Reply-To: Jan Hubicka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, this was suggested earlier somewhere, but I can not find the thread. C++ has assume attribute that expands int if (conditional) __builtin_unreachable () We do not want to account the conditional in inline heuristics since we know that it is going to be optimized out. Bootstrapped/regtested x86_64-linux, will commit it later today if thre are no complains. gcc/ChangeLog: * ipa-fnsummary.cc (builtin_unreachable_bb_p): New function. (analyze_function_body): Do not account conditionals guarding builtin_unreachable calls. gcc/testsuite/ChangeLog: * gcc.dg/ipa/fnsummary-1.c: New test. diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc index a5f5a50c8a5..987da29ec34 100644 --- a/gcc/ipa-fnsummary.cc +++ b/gcc/ipa-fnsummary.cc @@ -2649,6 +2649,54 @@ points_to_possible_sra_candidate_p (tree t) return false; } +/* Return true if BB is builtin_unreachable. + We skip empty basic blocks, debug statements, clobbers and predicts. + CACHE is used to memoize already analyzed blocks. */ + +static bool +builtin_unreachable_bb_p (basic_block bb, vec &cache) +{ + if (cache[bb->index]) + return cache[bb->index] - 1; + gimple_stmt_iterator si; + auto_vec visited_bbs; + bool ret = false; + while (true) + { + bool empty_bb = true; + visited_bbs.safe_push (bb); + cache[bb->index] = 3; + for (si = gsi_start_nondebug_bb (bb); + !gsi_end_p (si) && empty_bb; + gsi_next_nondebug (&si)) + { + if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT + && !gimple_clobber_p (gsi_stmt (si)) + && !gimple_nop_p (gsi_stmt (si))) + { + empty_bb = false; + break; + } + } + if (!empty_bb) + break; + else + bb = single_succ_edge (bb)->dest; + if (cache[bb->index]) + { + ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1; + goto done; + } + } + if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE) + || gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP)) + ret = true; +done: + for (basic_block vbb:visited_bbs) + cache[vbb->index] = (unsigned char)ret + 1; + return ret; +} + /* Analyze function body for NODE. EARLY indicates run from early optimization pipeline. */ @@ -2743,6 +2791,8 @@ analyze_function_body (struct cgraph_node *node, bool early) const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count; order = XNEWVEC (int, n_basic_blocks_for_fn (cfun)); nblocks = pre_and_rev_post_order_compute (NULL, order, false); + auto_vec cache; + cache.safe_grow_cleared (last_basic_block_for_fn (cfun)); for (n = 0; n < nblocks; n++) { bb = BASIC_BLOCK_FOR_FN (cfun, order[n]); @@ -2901,6 +2951,24 @@ analyze_function_body (struct cgraph_node *node, bool early) } } + /* Conditionals guarding __builtin_unreachable will be + optimized out. */ + if (gimple_code (stmt) == GIMPLE_COND) + { + edge_iterator ei; + edge e; + FOR_EACH_EDGE (e, ei, bb->succs) + if (builtin_unreachable_bb_p (e->dest, cache)) + { + if (dump_file) + fprintf (dump_file, + "\t\tConditional guarding __builtin_unreachable; ignored\n"); + this_time = 0; + this_size = 0; + break; + } + } + /* TODO: When conditional jump or switch is known to be constant, but we did not translate it into the predicates, we really can account just maximum of the possible paths. */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-1.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-1.c new file mode 100644 index 00000000000..a0ece0c300b --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-1.c @@ -0,0 +1,8 @@ +/* { dg-options "-O2 -fdump-ipa-fnsummary" } */ +int +test(int a) +{ + if (a > 10) + __builtin_unreachable (); +} +/* { dg-final { scan-ipa-dump "Conditional guarding __builtin_unreachable" "fnsummary" } } */