From patchwork Wed Dec 8 01:57:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1565054 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=FArQlv8G; dkim-atps=neutral Authentication-Results: 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=) 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J80g40H3cz9sRN for ; Wed, 8 Dec 2021 12:57:50 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 56AA53858411 for ; Wed, 8 Dec 2021 01:57:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56AA53858411 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638928667; bh=rVRGxvvrz5H9tZLCCytNLuxs5ZKdCU9ALg1EK7XBGnQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=FArQlv8GpZy+/jllf9SLdvPtBzeRuo0N7uRDXv35NYeZKfN77sd9DCR+ZHVcHgEXQ 0j8u5SGje0NyJW8RWK2sEW4V45c6v7vd2Uzk8t7ecnwFjoCK9X+A9KUcKS3680Wtg/ Vzpg5+Iedou+mAxW7cWHe9XfXqf1zaJAc/GVQAJQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id D18113858D3C; Wed, 8 Dec 2021 01:57:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D18113858D3C Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 61FD8116359; Tue, 7 Dec 2021 20:57:25 -0500 (EST) 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 qvpgSQrOK+xn; Tue, 7 Dec 2021 20:57:25 -0500 (EST) 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 057D21162EC; Tue, 7 Dec 2021 20:57:24 -0500 (EST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 1B81vFWn912040 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 7 Dec 2021 22:57:16 -0300 To: gcc-patches@gcc.gnu.org Subject: [PR103024,PR103530] support throwing compares and non-boolean types in harden-compares Organization: Free thinker, does not speak for AdaCore Date: Tue, 07 Dec 2021 22:57:15 -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.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Cc: marxin@gcc.gnu.org, zsojka@seznam.cz, pinskia@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch adjusts the harden-compares pass to cope with compares that must end basic blocks, and to accept non-boolean integral types whose conversion to boolean may have been discarded. (Andrew, thanks for the tip on how the discarding comes about, it's saved me some head-scratching and investigation ;-) Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog PR tree-optimization/103024 PR middle-end/103530 * gimple-harden-conditionals.cc (non_eh_succ_edge): New. (pass_harden_compares::execute): Accept 1-bit integral types, and cope with throwing compares. for gcc/testsuite/ChangeLog PR tree-optimization/103024 PR middle-end/103530 * g++.dg/pr103024.C: New. * g++.dg/pr103530.C: New. --- gcc/gimple-harden-conditionals.cc | 74 +++++++++++++++++++++++++++++++++++-- gcc/testsuite/g++.dg/pr103024.C | 12 ++++++ gcc/testsuite/g++.dg/pr103530.C | 27 ++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/pr103024.C create mode 100644 gcc/testsuite/g++.dg/pr103530.C diff --git a/gcc/gimple-harden-conditionals.cc b/gcc/gimple-harden-conditionals.cc index 81867d6e4275f..5e709c66416b7 100644 --- a/gcc/gimple-harden-conditionals.cc +++ b/gcc/gimple-harden-conditionals.cc @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #include "basic-block.h" #include "cfghooks.h" #include "cfgloop.h" +#include "tree-eh.h" #include "diagnostic.h" #include "intl.h" @@ -359,6 +360,24 @@ make_pass_harden_conditional_branches (gcc::context *ctxt) return new pass_harden_conditional_branches (ctxt); } +/* Return the fallthru edge of a block whose other edge is an EH + edge. */ +static inline edge +non_eh_succ_edge (basic_block bb) +{ + gcc_checking_assert (EDGE_COUNT (bb->succs) == 2); + + edge ret = find_fallthru_edge (bb->succs); + + int eh_idx = EDGE_SUCC (bb, 0) == ret; + edge eh = EDGE_SUCC (bb, eh_idx); + + gcc_checking_assert (!(ret->flags & EDGE_EH) + && (eh->flags & EDGE_EH)); + + return ret; +} + /* Harden boolean-yielding compares in FUN. */ unsigned int @@ -449,7 +468,11 @@ pass_harden_compares::execute (function *fun) if (VECTOR_TYPE_P (TREE_TYPE (op1))) continue; - gcc_checking_assert (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE); + /* useless_type_conversion_p enables conversions from 1-bit + integer types to boolean to be discarded. */ + gcc_checking_assert (TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE + || (INTEGRAL_TYPE_P (TREE_TYPE (lhs)) + && TYPE_PRECISION (TREE_TYPE (lhs)) == 1)); tree rhs = copy_ssa_name (lhs); @@ -460,6 +483,20 @@ pass_harden_compares::execute (function *fun) won't be debug stmts only. */ gsi_next_nondebug (&gsi_split); + bool throwing_compare_p = stmt_ends_bb_p (asgn); + if (throwing_compare_p) + { + basic_block nbb = split_edge (non_eh_succ_edge + (gimple_bb (asgn))); + gsi_split = gsi_start_bb (nbb); + + if (dump_file) + fprintf (dump_file, + "Splitting non-EH edge from block %i into %i" + " after a throwing compare\n", + gimple_bb (asgn)->index, nbb->index); + } + bool same_p = (op1 == op2); op1 = detach_value (loc, &gsi_split, op1); op2 = same_p ? op1 : detach_value (loc, &gsi_split, op2); @@ -473,17 +510,46 @@ pass_harden_compares::execute (function *fun) if (!gsi_end_p (gsi_split)) { gsi_prev (&gsi_split); - split_block (bb, gsi_stmt (gsi_split)); + basic_block obb = gsi_bb (gsi_split); + basic_block nbb = split_block (obb, gsi_stmt (gsi_split))->dest; gsi_next (&gsi_split); gcc_checking_assert (gsi_end_p (gsi_split)); single_succ_edge (bb)->goto_locus = loc; if (dump_file) - fprintf (dump_file, "Splitting block %i\n", bb->index); + fprintf (dump_file, + "Splitting block %i into %i" + " before the conditional trap branch\n", + obb->index, nbb->index); + } + + /* If the check assignment must end a basic block, we can't + insert the conditional branch in the same block, so split + the block again, and prepare to insert the conditional + branch in the new block. + + Also assign an EH region to the compare. Even though it's + unlikely that the hardening compare will throw after the + original compare didn't, the compiler won't even know that + it's the same compare operands, so add the EH edge anyway. */ + if (throwing_compare_p) + { + add_stmt_to_eh_lp (asgnck, lookup_stmt_eh_lp (asgn)); + make_eh_edges (asgnck); + + basic_block nbb = split_edge (non_eh_succ_edge + (gimple_bb (asgnck))); + gsi_split = gsi_start_bb (nbb); + + if (dump_file) + fprintf (dump_file, + "Splitting non-EH edge from block %i into %i after" + " the newly-inserted reversed throwing compare\n", + gimple_bb (asgnck)->index, nbb->index); } - gcc_checking_assert (single_succ_p (bb)); + gcc_checking_assert (single_succ_p (gsi_bb (gsi_split))); insert_check_and_trap (loc, &gsi_split, EDGE_TRUE_VALUE, EQ_EXPR, lhs, rhs); diff --git a/gcc/testsuite/g++.dg/pr103024.C b/gcc/testsuite/g++.dg/pr103024.C new file mode 100644 index 0000000000000..15e68d4021328 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr103024.C @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fnon-call-exceptions -fharden-compares -fsignaling-nans" } */ + +struct G4ErrorMatrix { + G4ErrorMatrix(int); + ~G4ErrorMatrix(); +}; +double PropagateError_charge; +void PropagateError() { + G4ErrorMatrix transf(0); + int field(PropagateError_charge && field); +} diff --git a/gcc/testsuite/g++.dg/pr103530.C b/gcc/testsuite/g++.dg/pr103530.C new file mode 100644 index 0000000000000..c1d2059542ba3 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr103530.C @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fharden-compares -Wno-c++11-extensions" } */ + +enum E:bool +{ E0, E1 }; + +int x; + +E +baz (E rtt) +{ + return rtt == E0 ? E1 : E0; +} + +bool bar (); + +void +foo (E) +{ + E a = x ? E1 : E0; + if (bar ()) + if (bar ()) + { + E b = baz (a); + foo (b); + } +}