From patchwork Fri Apr 22 08:53:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 53116 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 A4BD23858C2C for ; Fri, 22 Apr 2022 08:54:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4BD23858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1650617641; bh=Q3iprQkxdSf36BgqgOLXu+P0u9/T3VF/ENMfBiPCk20=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Bhgyr+Fy3BGJI0IEr0iKFelq55+zXk0Pc+qsQDE0wee4X8CTLLJ9NAlv5/sIHebML XM4qqNGfwIvyNISBrwbu2jO5upgU0Vba1Fe4huz4GoJ5SYF6Od9RD7fT/oin1cm4wp tX9pv30snzlfgfpJbQRbdmDZFKArt91d+hqO+oXI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id D39BB3858C2C; Fri, 22 Apr 2022 08:53:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D39BB3858C2C Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8B3461F37B; Fri, 22 Apr 2022 08:53:30 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 69050131BD; Fri, 22 Apr 2022 08:53:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2r94GAptYmKwAQAAMHmgww (envelope-from ); Fri, 22 Apr 2022 08:53:30 +0000 Date: Fri, 22 Apr 2022 10:53:30 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH][v4] rtl-optimization/105231 - distribute_notes and REG_EH_REGION MIME-Version: 1.0 Message-Id: <20220422085330.69050131BD@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Cc: ebotcazou@adacore.com, segher@kernel.crashing.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" I was still unhappy with the previous patch and indeed, re-thinking all the special casing I put in there I found a hole with respect to externally throwing stmts which I totally forgot about and which might end up in must-not-throw regions after the previous patch. Fortunately all of the complicated situations only arise with non-call exceptions and when there's a REG_EH_REGION note to distribute. So first of all the new patch makes that explicit and does not affect the not non-call EH path (apart from the new assert in distribute_notes). It also does not affect the non-call EH path when there is no REG_EH_REGION on any of the insns. I resisted trying to be clever with lp_nr == 0 or INT_MIN (the nothrow notes that we could in theory just drop), but I put in an extra check in case we have a REG_EH_REGION note on an insn that cannot ever throw (and drop those at distribute_notes time). The patch preserves the main part of the very original patch, that we only ever place the REG_EH_REGION on i3. The split precondition should make sure that i2 never throws (but no assert since the trigger happy may_trap_p might be confused by some "optimization" done on the split part later). The hunk at the start of try_combine makes sure that we only have a single REG_EH_REGION note to distribute which ends up on i3 which accumulates all possibly throwing side-effects thanks to the split precondition. Bootstrapped and tested on x86_64-unknown-linux-gnu. OK for trunk? Thanks, Richard. --- The following mitigates a problem in combine distribute_notes which places an original REG_EH_REGION based on only may_trap_p which is good to test whether a non-call insn can possibly throw but not if actually it does or we care. That's something we decided at RTL expansion time where we possibly still know the insn evaluates to a constant. In fact, the REG_EH_REGION note with lp > 0 can only come from the original i3 and an assert is added to that effect. That means we only need to retain the note on i3 or, if that cannot trap, drop it but we should never move it to i2. The following places constraints on the insns to combine with non-call exceptions since we cannot handle the case where we have more than one EH side-effect in the IL. The patch also makes sure we can accumulate that on i3 and do not split a possible exception raising part of it to i2. As a special case we do not place any restriction on all externally throwing insns when there is no REG_EH_REGION present. 2022-04-22 Richard Biener PR rtl-optimization/105231 * combine.cc (distribute_notes): Assert that a REG_EH_REGION with landing pad > 0 is from i3. Put any REG_EH_REGION note on i3 or drop it if the insn can not trap. (try_combine): Ensure that we can merge REG_EH_REGION notes with non-call exceptions. Ensure we are not splitting a trapping part of an insn with non-call exceptions when there is any REG_EH_REGION note to preserve. * gcc.dg/torture/pr105231.c: New testcase. --- gcc/combine.cc | 81 +++++++++++++++++++------ gcc/testsuite/gcc.dg/torture/pr105231.c | 15 +++++ 2 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr105231.c diff --git a/gcc/combine.cc b/gcc/combine.cc index 53dcac92abc..dede573982a 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -2569,6 +2569,7 @@ try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0, rtx new_other_notes; int i; scalar_int_mode dest_mode, temp_mode; + bool nce_any_eh_region_note = false; /* Immediately return if any of I0,I1,I2 are the same insn (I3 can never be). */ @@ -2951,6 +2952,32 @@ try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0, return 0; } + /* With non-call exceptions we can end up trying to combine multiple + stmts with possible EH side effects. Make sure we can combine + that to a single stmt which means there must be at most one insn + in the combination with an EH side effect. */ + if (cfun->can_throw_non_call_exceptions) + { + if (find_reg_note (i3, REG_EH_REGION, NULL_RTX) + || find_reg_note (i2, REG_EH_REGION, NULL_RTX) + || (i1 && find_reg_note (i1, REG_EH_REGION, NULL_RTX)) + || (i0 && find_reg_note (i0, REG_EH_REGION, NULL_RTX))) + { + nce_any_eh_region_note = true; + if (insn_could_throw_p (i3) + + insn_could_throw_p (i2) + + (i1 ? insn_could_throw_p (i1) : 0) + + (i0 ? insn_could_throw_p (i0) : 0) > 1) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + fprintf (dump_file, "Can't combine multiple insns with EH " + "side-effects\n"); + undo_all (); + return 0; + } + } + } + /* Record whether i2 and i3 are trivial moves. */ i2_was_move = is_just_move (i2); i3_was_move = is_just_move (i3); @@ -3685,7 +3712,13 @@ try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0, || !modified_between_p (*split, i2, i3)) /* We can't overwrite I2DEST if its value is still used by NEWPAT. */ - && ! reg_referenced_p (i2dest, newpat)) + && ! reg_referenced_p (i2dest, newpat) + /* We should not split a possibly trapping part when we + care about non-call EH and have REG_EH_REGION notes + to distribute. */ + && (!cfun->can_throw_non_call_exceptions + || !nce_any_eh_region_note + || !may_trap_p (*split))) { rtx newdest = i2dest; enum rtx_code split_code = GET_CODE (*split); @@ -14175,23 +14208,35 @@ distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2, break; case REG_EH_REGION: - /* These notes must remain with the call or trapping instruction. */ - if (CALL_P (i3)) - place = i3; - else if (i2 && CALL_P (i2)) - place = i2; - else - { - gcc_assert (cfun->can_throw_non_call_exceptions); - if (may_trap_p (i3)) - place = i3; - else if (i2 && may_trap_p (i2)) - place = i2; - /* ??? Otherwise assume we've combined things such that we - can now prove that the instructions can't trap. Drop the - note in this case. */ - } - break; + { + /* This handling needs to be kept in sync with the + prerequisite checking in try_combine. */ + int lp_nr = INTVAL (XEXP (note, 0)); + /* A REG_EH_REGION note transfering control can only ever come + from i3. */ + if (lp_nr > 0) + gcc_assert (from_insn == i3); + /* We are making sure there is a single effective REG_EH_REGION + note and it's valid to put it on i3. */ + if (!insn_could_throw_p (from_insn)) + /* Deal with stray notes on insns that can never throw. */ + ; + else + { + if (CALL_P (i3)) + place = i3; + else + { + gcc_assert (cfun->can_throw_non_call_exceptions); + /* If i3 can still trap preserve the note, otherwise we've + combined things such that we can now prove that the + instructions can't trap. Drop the note in this case. */ + if (may_trap_p (i3)) + place = i3; + } + } + break; + } case REG_ARGS_SIZE: /* ??? How to distribute between i3-i1. Assume i3 contains the diff --git a/gcc/testsuite/gcc.dg/torture/pr105231.c b/gcc/testsuite/gcc.dg/torture/pr105231.c new file mode 100644 index 00000000000..50459219c08 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr105231.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target int32plus } */ +/* { dg-require-effective-target dfp } */ +/* { dg-additional-options "-fsanitize-coverage=trace-pc -fnon-call-exceptions --param=max-cse-insns=1 -frounding-math" } */ +/* { dg-additional-options "-mstack-arg-probe" { target x86_64-*-* i?86-*-* } } */ + +void baz (int *); +void bar (double, double, _Decimal64); + +void +foo (void) +{ + int s __attribute__((cleanup (baz))); + bar (0xfffffffffffffffe, 0xebf3fff2fbebaf7f, 0xffffffffffffff); +}