From patchwork Thu May 12 01:46:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 53829 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 14D643856241 for ; Thu, 12 May 2022 01:47:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14D643856241 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652320076; bh=B9OP8XTC2Lqzza62Wx3T2W5o5lDa/xJo8F/d3ERLTdQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=w4FWYGeyaHExSxIR0Z1bBVBf16PQdwGWDWn6mX1eyUWzA5EabVj4cOoVBKeI+Ohw5 jQZzcoX8Cvufmw1Wek6VRFB++14vlIz0s+F+zMks2MCMf+eVdmM2yvy/PvxwfimxFe AD3P18AU9Mitj3eOmdr6e7CKdMEz2GZPYrrRhAJk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTPS id 86C183857432 for ; Thu, 12 May 2022 01:47:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 86C183857432 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 53959116A1C; Wed, 11 May 2022 21:47:02 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 8xKgdw7D5jOJ; Wed, 11 May 2022 21:47:02 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id 1C222116809; Wed, 11 May 2022 21:47:01 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 24C1ksvi106030 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 11 May 2022 22:46:55 -0300 To: gcc-patches@gcc.gnu.org Subject: [PATCH] [PR105455] Set edge probabilities when hardening conditionals Organization: Free thinker, does not speak for AdaCore Date: Wed, 11 May 2022 22:46:54 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alexandre Oliva via Gcc-patches From: Alexandre Oliva Reply-To: Alexandre Oliva Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When turning unconditional edges into conditional, as in gimple-harden-conditionals.cc:insert_check_and_trap, the newly-created edge's probability comes out uninitialized, while the previously unconditional edge's probability is presumably profile_probability::always. Mixing initialized and uninitialized probabilities before expand breaks predict.cc:force_edge_cold: the initialized probability may end up copied to a REG_BR_PROB note in a conditional branch insn, but if force_edge_cold is called on that edge, it will find another edge with uninitialized probability and assume the note is absent. Later on, rtl_verify_edges complains that the note does not match the probability modified by force_edge_cold in the edge. This patch sets probabilities for edges affected by hardening of conditionals, both the newly-created edges to trap blocks and the previously-unconditional edges, so that the former are considered never taken, while the latter are confirmed as always taken. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog PR rtl-optimization/105455 * gimple-harden-conditionals.cc (insert_check_and_trap): Set probabilities for newly-conditional edges. for gcc/testsuite/ChangeLog PR rtl-optimization/105455 * gcc.dg/pr105455.c: New. --- gcc/gimple-harden-conditionals.cc | 2 ++ gcc/testsuite/gcc.dg/pr105455.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr105455.c diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc index 28c4810f0a78e..240d0e5acd87a 100644 --- a/gcc/gimple-harden-conditionals.cc +++ b/gcc/gimple-harden-conditionals.cc @@ -254,8 +254,10 @@ insert_check_and_trap (location_t loc, gimple_stmt_iterator *gsip, equality. */ single_succ_edge (chk)->flags &= ~EDGE_FALLTHRU; single_succ_edge (chk)->flags |= neg_true_false_flag; + single_succ_edge (chk)->probability = profile_probability::always (); edge e = make_edge (chk, trp, true_false_flag); e->goto_locus = loc; + e->probability = profile_probability::never (); if (dom_info_available_p (CDI_DOMINATORS)) set_immediate_dominator (CDI_DOMINATORS, trp, chk); diff --git a/gcc/testsuite/gcc.dg/pr105455.c b/gcc/testsuite/gcc.dg/pr105455.c new file mode 100644 index 0000000000000..81e9154baa1c3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr105455.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fharden-conditional-branches -funroll-loops --param max-loop-header-insns=1" } */ + +__attribute__ ((cold)) void +bar (void); + +void +foo (int x) +{ + if (x) + { + int i; + + for (i = 0; i < 101; ++i) + bar (); + } +}