From patchwork Thu Oct 26 15:44:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 158595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp770621vqb; Thu, 26 Oct 2023 08:44:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQtuK8IRs3iFk+Tu6cu+PIgWnvJ6D4HvVWt+UQjDJe5wmk8IcLOz5drZBvXrrrgGDig6Yl X-Received: by 2002:a05:6214:925:b0:66d:2ce2:8651 with SMTP id dk5-20020a056214092500b0066d2ce28651mr3449413qvb.0.1698335094121; Thu, 26 Oct 2023 08:44:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698335094; cv=pass; d=google.com; s=arc-20160816; b=tyrucODboP+8KtBxQj+3Gs97qINzoPNetISrAf+CbbBiMiVRBzNNsKFnvwL0GVu8Fa AmaR48PGfEbaR3K4pycOuVUtob8cUSG9AuQaw4QyYpMrdp0n+/AwnGKx3fkSXXXL6vhK S9vf9R0zbiHqdT0rHe17JKU3yi+UYoRsfdAt1u1If0uhi0V9Ums/BkD69CI22tr7LbB3 A+d6QcOlzpMDHX9eCi/+7z41iYcjo/o4ZC/0wlS1TM/GVcOcxhlamqY6slLMbr2RnEwM JXT5ek7ACTYrS3NLSoAvPtP7C+Qwsppp5LviVrA97IN3ZXdf5XpvYd/Nxa6RqoghzNT2 Ro4g== 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:mime-version:user-agent :message-id:date:organization:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=+QuMxi3JU0314yU/bycbpwEhBXkZ9sSm8yK3I8h/yoA=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=FIEsEIh4Qa6ZlHmI52Yt0NY1MUZaRWPHKMZzAW9iCnR3vWPAufr4y87CCw7VyNj6jt jVQS4tnmbcLhHHTvSTk2MiJp7+tpbye78W9TdEmozwoPUsAc0Z+UbQC8NIGHCdNgXqX8 UHOxlWIl6ND1XGVXfPIvy5Ps6hWuhDYuo8pOaIItMRdHe2MUuAS1xF8Mi/CajnUwJ6fo g12HNSkSkulImSKgC9CRqBDxyjnBaE9wVI1L1Uu5Vy6LyfdZUCN/vwEJBKXsohupQhEG s6FmU21a3FvGQ7TR0ac9uUkx4OaoVrKQH37V61W/SIGwaZeFc03w/I7Vyhp5uSdH48uQ gj8A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=W9Dk3tBI; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k5-20020ac85fc5000000b0041812c64665si9574726qta.793.2023.10.26.08.44.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 08:44:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=W9Dk3tBI; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF06D385DC2D for ; Thu, 26 Oct 2023 15:44:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 97461385E007 for ; Thu, 26 Oct 2023 15:44:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 97461385E007 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 97461385E007 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::629 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698335070; cv=none; b=AcI3nmls176WUatgGe9ku4e6aRMdxruPV/Iw7b63R+SqHcH4l84LbyAs/TYh7w+q3t5wKfo2Z9mDnyKUv9Dr+z0ap6gk12ia28ZLAIXlFG16E2vaBXpM6dQ+NRTwRa2dCPuaLKoAyCmOnhITF6h8/NPq2AnUOXE233kYghJLz+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698335070; c=relaxed/simple; bh=NvopzaeLwivlzDs0HixmE4W5UYqao3xEbN9AaqOnC5s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aKiPkRcbAWjcFHGhdNVMwztR0UCSrmzAt1g2tkmjV8A0K+D7V/qafItNpHK/0fZRqWNytB9e3leQ3UEUzbOwfvuP5YtRIQ8tOcpxVLWA4aM4BVn9CR65euAIXgr5cXaf/QgAP41KFK7B2gaof5ONnzFpvDeHOSycnNXZ2CbIFQA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1cc131e52f1so3941875ad.0 for ; Thu, 26 Oct 2023 08:44:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1698335067; x=1698939867; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:date:organization:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=+QuMxi3JU0314yU/bycbpwEhBXkZ9sSm8yK3I8h/yoA=; b=W9Dk3tBIapohT/HrLkynUpRVXfBBywNtxntV6TyY/vb1iwPbGGCoSI2Mehjw/it4IO zZfqtFykNnaoz5IhGX7dHbaPiGDseXWmrJKu71zgmhpTp0QT5Pp0Bko3sZLzqhHYXL2H Z5DZX67fSJGUFYhMpz7um5NPtQdWRBEEOF6KlASXPmnY16TFqTX16YXPsgOf8zo31mco i4ukjjvqXQknNgePS8RPYE35h3m9dXiMBCbp7UgV1haiEGILKA6Mrc6JBfM/07033Pm1 bRGuqoiYKcAPYQI4IZJ/u8obv03BBTDMXZP5eWvX7p6My6Hz6yz1BgMPgjgVeJGsdy09 gfjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698335067; x=1698939867; h=mime-version:user-agent:message-id:date:organization:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+QuMxi3JU0314yU/bycbpwEhBXkZ9sSm8yK3I8h/yoA=; b=Mz4V2BWYqXxsw6+itFxd36gNFLzazut38PYtAne7xcruLGNcj6A7ZFI3PpDc38RBSj j8XF/biMI3mFKgn2L/sUlaQFmA6zy8e4Of6Q/0IgknuB6BosobZwdzL8cHxdA7iJoeE/ be+xKg332S7/WLRrDa2K1xHDtIglLBuIU6g8LQNa4aaDMxrVTTcmkGAz5RDWdkQczvMJ xN6DRzeziqEOewWuI9LLyvdhSz8L/ctxlyQDDC2P9huSi2IBMvFTdroQSyGO5ig7QKDr D0l4+T94aQGPmKnkGbprtzXgFtwoU/m/g2Rfgj3tP+TuwmleduUA8RcnrQjaYZHtscyf ZNMQ== X-Gm-Message-State: AOJu0YxHO3bYXC9/2l4ib4pTZRbOoj1Im7dpF52UXUZmtkzeNeVK+Im2 hh1F3e44iqdkr7ixOSkqM3QxbLVkfw9eTS0b8yNrtQ== X-Received: by 2002:a17:902:d2ce:b0:1c9:dba6:417a with SMTP id n14-20020a170902d2ce00b001c9dba6417amr4057412plc.9.1698335067206; Thu, 26 Oct 2023 08:44:27 -0700 (PDT) Received: from free.home ([2804:7f1:2080:b35a:ae4d:21ed:38ee:2673]) by smtp.gmail.com with ESMTPSA id 197-20020a6300ce000000b005b82611378bsm10882676pga.52.2023.10.26.08.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 08:44:26 -0700 (PDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 39QFiFdB1723137 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 26 Oct 2023 12:44:15 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: hardcfr: support checking at abnormal edges [PR111943] Organization: Free thinker, does not speak for AdaCore Date: Thu, 26 Oct 2023 12:44:15 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_QUOTING 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: 1780833419373431609 X-GMAIL-MSGID: 1780833419373431609 Control flow redundancy may choose abnormal edges for early checking, but that breaks because we can't insert checks on such edges. Introduce conditional checking on the dest block of abnormal edges, and leave it for the optimizer to drop the conditional. Also, oops, I noticed the new files went in with an incorrect copyright notice, that this patch fixes. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog PR tree-optimization/111943 * gimple-harden-control-flow.cc: Adjust copyright year. (rt_bb_visited): Add vfalse and vtrue data members. Zero-initialize them in the ctor. (rt_bb_visited::insert_exit_check_on_edge): Upon encountering abnormal edges, insert initializers for vfalse and vtrue on entry, and insert the check sequence guarded by a conditional in the dest block. for libgcc/ChangeLog * hardcfr.c: Adjust copyright year. for gcc/testsuite/ChangeLog PR tree-optimization/111943 * gcc.dg/harden-cfr-pr111943.c: New. --- gcc/gimple-harden-control-flow.cc | 78 +++++++++++++++++++++++++++- gcc/testsuite/gcc.dg/harden-cfr-pr111943.c | 33 ++++++++++++ libgcc/hardcfr.c | 2 - 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/harden-cfr-pr111943.c diff --git a/gcc/gimple-harden-control-flow.cc b/gcc/gimple-harden-control-flow.cc index 3711b25d09123..77c140178060e 100644 --- a/gcc/gimple-harden-control-flow.cc +++ b/gcc/gimple-harden-control-flow.cc @@ -1,5 +1,5 @@ /* Control flow redundancy hardening. - Copyright (C) 2022 Free Software Foundation, Inc. + Copyright (C) 2022-2023 Free Software Foundation, Inc. Contributed by Alexandre Oliva . This file is part of GCC. @@ -460,6 +460,10 @@ class rt_bb_visited at the end of a block's predecessors or successors list. */ tree ckfail, ckpart, ckinv, ckblk; + /* If we need to deal with abnormal edges, we insert SSA_NAMEs for + boolean true and false. */ + tree vfalse, vtrue; + /* Convert a block index N to a block vindex, the index used to identify it in the VISITED array. Check that it's in range: neither ENTRY nor EXIT, but maybe one-past-the-end, to compute @@ -596,7 +600,8 @@ public: /* Prepare to add control flow redundancy testing to CFUN. */ rt_bb_visited (int checkpoints) : nblocks (n_basic_blocks_for_fn (cfun)), - vword_type (NULL), ckseq (NULL), rtcfg (NULL) + vword_type (NULL), ckseq (NULL), rtcfg (NULL), + vfalse (NULL), vtrue (NULL) { /* If we've already added a declaration for the builtin checker, extract vword_type and vword_bits from its declaration. */ @@ -703,7 +708,74 @@ public: /* Insert SEQ on E. */ void insert_exit_check_on_edge (gimple_seq seq, edge e) { - gsi_insert_seq_on_edge_immediate (e, seq); + if (!(e->flags & EDGE_ABNORMAL)) + { + gsi_insert_seq_on_edge_immediate (e, seq); + return; + } + + /* Initialize SSA boolean constants for use in abnormal PHIs. */ + if (!vfalse) + { + vfalse = make_ssa_name (boolean_type_node); + vtrue = make_ssa_name (boolean_type_node); + + gimple_seq vft_seq = NULL; + gassign *vfalse_init = gimple_build_assign (vfalse, boolean_false_node); + gimple_seq_add_stmt (&vft_seq, vfalse_init); + gassign *vtrue_init = gimple_build_assign (vtrue, boolean_true_node); + gimple_seq_add_stmt (&vft_seq, vtrue_init); + + gsi_insert_seq_on_edge_immediate (single_succ_edge + (ENTRY_BLOCK_PTR_FOR_FN (cfun)), + vft_seq); + } + + /* We can't insert on abnormal edges, but we can arrange for SEQ + to execute conditionally at dest. Add a PHI boolean with TRUE + from E and FALES from other preds, split the whole block, add a + test for the PHI to run a new block with SEQ or skip straight + to the original block. If there are multiple incoming abnormal + edges, we'll do this multiple times. ??? Unless there are + multiple abnormal edges with different postcheck status, we + could split the block and redirect other edges, rearranging the + PHI nodes. Optimizers already know how to do this, so we can + keep things simple here. */ + basic_block bb = e->dest; + basic_block bb_postcheck = split_block_after_labels (bb)->dest; + + basic_block bb_check = create_empty_bb (e->dest); + bb_check->count = e->count (); + if (dom_info_available_p (CDI_DOMINATORS)) + set_immediate_dominator (CDI_DOMINATORS, bb_check, bb); + if (current_loops) + add_bb_to_loop (bb_check, current_loops->tree_root); + + gimple_stmt_iterator chkpt = gsi_after_labels (bb_check); + gsi_insert_seq_before_without_update (&chkpt, seq, GSI_SAME_STMT); + edge edge_postcheck = make_edge (bb_check, bb_postcheck, EDGE_FALLTHRU); + edge_postcheck->probability = profile_probability::always (); + + tree cond_var = make_ssa_name (boolean_type_node); + gcond *cond = gimple_build_cond (NE_EXPR, cond_var, boolean_false_node, + NULL, NULL); + gimple_stmt_iterator condpt = gsi_after_labels (bb); + gsi_insert_before (&condpt, cond, GSI_SAME_STMT); + edge edge_nocheck = single_succ_edge (bb); + edge_nocheck->flags &= ~EDGE_FALLTHRU; + edge_nocheck->flags |= EDGE_FALSE_VALUE; + edge edge_check = make_edge (bb, bb_check, EDGE_TRUE_VALUE); + edge_check->probability = e->count ().probability_in (bb->count); + edge_nocheck->probability = edge_check->probability.invert (); + + gphi *cond_phi = create_phi_node (cond_var, bb); + for (int i = 0, ei = EDGE_COUNT (bb->preds); i < ei; i++) + { + edge pred = EDGE_PRED (bb, i); + bool check_edge = pred == e; + tree val = check_edge ? vtrue : vfalse; + add_phi_arg (cond_phi, val, pred, UNKNOWN_LOCATION); + } } /* Add checking code to CHK_EDGES and CHKCALL_BLOCKS, and diff --git a/gcc/testsuite/gcc.dg/harden-cfr-pr111943.c b/gcc/testsuite/gcc.dg/harden-cfr-pr111943.c new file mode 100644 index 0000000000000..5a5a2133b1368 --- /dev/null +++ b/gcc/testsuite/gcc.dg/harden-cfr-pr111943.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-fharden-control-flow-redundancy --param=max-jump-thread-duplication-stmts=0 -Ofast -fdump-tree-hardcfr -fdump-tree-optimized" } */ +/* { dg-require-effective-target indirect_jumps } */ +/* { dg-require-effective-target label_values } */ + +/* Based on gcc.c-torture/compile/20050510-1.c. */ + +extern void dont_remove (void); + +void bar (int k) +{ + void *label = (k) ? &&x : &&y; + + if (k >= 0) + goto *label; + +x: + if (k <= 0) + dont_remove (); + /* else goto y; */ + +y: + return; +} + +/* Check before calling dont_remove(), in the 'else goto y' edge, and in the + abnormal edge to y. */ +/* { dg-final { scan-tree-dump-times "hardcfr_check" 3 "hardcfr" } } */ +/* { dg-final { scan-tree-dump-times "hardcfr_check" 3 "optimized" } } */ +/* Check that hardcfr introduces an abnormal PHI node (this could be avoided, + but it's not worth the effort), and that it gets optimized out. */ +/* { dg-final { scan-tree-dump-times {\(ab\) = PHI .*\(ab\)} 1 "hardcfr" } } */ +/* { dg-final { scan-tree-dump-not {\(ab\) = PHI .*\(ab\)} "optimized" } } */ diff --git a/libgcc/hardcfr.c b/libgcc/hardcfr.c index 7496095b8666c..25ff06742cb44 100644 --- a/libgcc/hardcfr.c +++ b/libgcc/hardcfr.c @@ -1,5 +1,5 @@ /* Control flow redundancy hardening - Copyright (C) 2022 Free Software Foundation, Inc. + Copyright (C) 2022-2023 Free Software Foundation, Inc. Contributed by Alexandre Oliva This file is part of GCC.