From patchwork Tue Oct 1 09:43:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1991380 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=l8GJ2Uk8; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=3xkMyd+h; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=l8GJ2Uk8; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=3xkMyd+h; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XHtLW0Wwqz1xtg for ; Tue, 1 Oct 2024 19:44:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D3FC38449DA for ; Tue, 1 Oct 2024 09:44:01 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 8E94A384640D for ; Tue, 1 Oct 2024 09:43:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8E94A384640D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8E94A384640D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727775823; cv=none; b=XFnUmMXUzhfohIvqdeMHf4sP8iIBnzS3iNTBy2WSQdunSamD0SzFoDX8DfqfV88kywEIIGjjDbZA3bwHQ5m3MGqTjq15KWpcEsmVV3bbMLO/TpNJU0uKivrAgheYarmwz5qU6+//ASXCXaFN1S3ArLQx+6BckFWS9e0aNrVj3M4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727775823; c=relaxed/simple; bh=naI6jWGMGNT8EtLXjpknAtTaeGBrOpJiNa3w1YF5IUw=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=R295fzcEytNwQcKaWYrOYxy6EQs6aLYd2U5mMJVSwxEuLHmu75WHUHyJpXvpgh0rYUxW7dChqG9uwiyw5Asw0gJgMhXexuI+x8kPBov86hYakCXxIMZ/g734V5M9X6pLJQRER3JwhhkzMHi8WKophlq9BBitVQhMM+AApzHTuUo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from murzim.nue2.suse.org (unknown [10.168.4.243]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id 415A721B78 for ; Tue, 1 Oct 2024 09:43:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1727775820; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+f+nSCjh2co8yHu8R8NzRnQRWSgMI0YtE4N3RqhtBgs=; b=l8GJ2Uk8yvV+XXhbPeo9TtBJjxhC1edIXRwOXZz5MLr7fq9YWG7qV0lzeMByNXzs/dClPY P8auS24+SJ0ccKTVOOZmyaS9wB18gCzUqkUWpvYwW4sUEKVGfUsaqwBIfDaGSZXDtFC3Jw Ihoo++hdtelFaxrpxW8Lihvg0iNCpyQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1727775820; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+f+nSCjh2co8yHu8R8NzRnQRWSgMI0YtE4N3RqhtBgs=; b=3xkMyd+hQ1uQ3HFBDeLKwwdGIXBZ3QuMO5CSW2eripg3ZYGv3KJ0lgtV2TtxVOpCHBGqIo tdfwzfGZiBO8+/Dg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1727775820; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+f+nSCjh2co8yHu8R8NzRnQRWSgMI0YtE4N3RqhtBgs=; b=l8GJ2Uk8yvV+XXhbPeo9TtBJjxhC1edIXRwOXZz5MLr7fq9YWG7qV0lzeMByNXzs/dClPY P8auS24+SJ0ccKTVOOZmyaS9wB18gCzUqkUWpvYwW4sUEKVGfUsaqwBIfDaGSZXDtFC3Jw Ihoo++hdtelFaxrpxW8Lihvg0iNCpyQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1727775820; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=+f+nSCjh2co8yHu8R8NzRnQRWSgMI0YtE4N3RqhtBgs=; b=3xkMyd+hQ1uQ3HFBDeLKwwdGIXBZ3QuMO5CSW2eripg3ZYGv3KJ0lgtV2TtxVOpCHBGqIo tdfwzfGZiBO8+/Dg== Date: Tue, 1 Oct 2024 11:43:40 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/116906 - unsafe PRE with never executed edges MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-0.19 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_SPAM_SHORT(0.73)[0.245]; NEURAL_HAM_LONG(-0.32)[-0.322]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; RCPT_COUNT_ONE(0.00)[1]; FROM_HAS_DN(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[murzim.nue2.suse.org:helo] X-Spam-Score: -0.19 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Message-Id: <20241001094401.3D3FC38449DA@sourceware.org> When we're computing ANTIC for PRE we treat edges to not yet visited blocks as having a maximum ANTIC solution to get at an optimistic solution in the iteration. That assumes the edges visted eventually execute. This is a wrong assumption that can lead to wrong code (and not only non-optimality) when possibly trapping expressions are involved as the testcases in the PR show. The following mitigates this by pruning trapping expressions from ANTIC computed when maximum sets are involved. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/116906 * tree-ssa-pre.cc (prune_clobbered_mems): Add clean_traps argument. (compute_antic_aux): Direct prune_clobbered_mems to prune all traps when any MAX solution was involved in the ANTIC computation. (compute_partial_antic_aux): Adjust. * gcc.dg/pr116906-1.c: New testcase. * gcc.dg/pr116906-2.c: Likewise. --- gcc/testsuite/gcc.dg/pr116906-1.c | 43 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr116906-2.c | 40 ++++++++++++++++++++++++++++ gcc/tree-ssa-pre.cc | 16 +++++++----- 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr116906-1.c create mode 100644 gcc/testsuite/gcc.dg/pr116906-2.c diff --git a/gcc/testsuite/gcc.dg/pr116906-1.c b/gcc/testsuite/gcc.dg/pr116906-1.c new file mode 100644 index 00000000000..27b1fdae02b --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr116906-1.c @@ -0,0 +1,43 @@ +/* { dg-do run { target *-*-linux* *-*-gnu* *-*-uclinux* } } */ +/* { dg-options "-O2" } */ + +#include +#include +#include +#include + +int a = 1, b = 0; + +uint64_t safe_mod(uint64_t a, uint64_t b) +{ + if (b == 0) return a; + else return a % b; +} + +int __attribute__((noipa)) +f(uint64_t p) +{ + int c = 0; +j: + b = safe_mod( + (c = ((a &= (0 < p)) && 1), 1), p); + if (!c) + goto j; + return 0; +} + +void do_exit (int i) +{ + exit (0); +} + +int main() +{ + struct sigaction s; + sigemptyset (&s.sa_mask); + s.sa_handler = do_exit; + s.sa_flags = 0; + sigaction (SIGALRM, &s, NULL); + alarm (1); + f(b); +} diff --git a/gcc/testsuite/gcc.dg/pr116906-2.c b/gcc/testsuite/gcc.dg/pr116906-2.c new file mode 100644 index 00000000000..3478771678c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr116906-2.c @@ -0,0 +1,40 @@ +/* { dg-do run { target *-*-linux* *-*-gnu* *-*-uclinux* } } */ +/* { dg-options "-O2 -fno-tree-ch" } */ + +#include +#include +#include + +int x; + +void __attribute__((noipa)) +foo (int *p, unsigned n) +{ + unsigned i = 0; + do + { + if (i == n) + break; + if (p) + x = *p; + i += 2; + } + while (1); + x = *p; +} + +void do_exit (int i) +{ + exit (0); +} + +int main() +{ + struct sigaction s; + sigemptyset (&s.sa_mask); + s.sa_handler = do_exit; + s.sa_flags = 0; + sigaction (SIGALRM, &s, NULL); + alarm (1); + foo ((int *)0, 1); +} diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc index 6c0436b8b15..13f2c8a5420 100644 --- a/gcc/tree-ssa-pre.cc +++ b/gcc/tree-ssa-pre.cc @@ -2008,10 +2008,11 @@ clean (bitmap_set_t set1, bitmap_set_t set2 = NULL) } /* Clean the set of expressions that are no longer valid in SET because - they are clobbered in BLOCK or because they trap and may not be executed. */ + they are clobbered in BLOCK or because they trap and may not be executed. + When CLEAN_TRAPS is true remove all possibly trapping expressions. */ static void -prune_clobbered_mems (bitmap_set_t set, basic_block block) +prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps) { bitmap_iterator bi; unsigned i; @@ -2049,7 +2050,7 @@ prune_clobbered_mems (bitmap_set_t set, basic_block block) a possible exit point. ??? This is overly conservative if we translate AVAIL_OUT as the available expression might be after the exit point. */ - if (BB_MAY_NOTRETURN (block) + if ((BB_MAY_NOTRETURN (block) || clean_traps) && vn_reference_may_trap (ref)) to_remove = i; } @@ -2060,7 +2061,7 @@ prune_clobbered_mems (bitmap_set_t set, basic_block block) a possible exit point. ??? This is overly conservative if we translate AVAIL_OUT as the available expression might be after the exit point. */ - if (BB_MAY_NOTRETURN (block) + if ((BB_MAY_NOTRETURN (block) || clean_traps) && vn_nary_may_trap (nary)) to_remove = i; } @@ -2114,6 +2115,8 @@ compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge) bool was_visited = BB_VISITED (block); bool changed = ! BB_VISITED (block); + bool any_max_on_edge = false; + BB_VISITED (block) = 1; old = ANTIC_OUT = S = NULL; @@ -2158,6 +2161,7 @@ compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge) maximal set to arrive at a maximum ANTIC_IN solution. We can ignore them in the intersection operation and thus need not explicitely represent that maximum solution. */ + any_max_on_edge = true; if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n", e->src->index, e->dest->index); @@ -2223,7 +2227,7 @@ compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge) /* Prune expressions that are clobbered in block and thus become invalid if translated from ANTIC_OUT to ANTIC_IN. */ - prune_clobbered_mems (ANTIC_OUT, block); + prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge); /* Generate ANTIC_OUT - TMP_GEN. */ S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block)); @@ -2396,7 +2400,7 @@ compute_partial_antic_aux (basic_block block, /* Prune expressions that are clobbered in block and thus become invalid if translated from PA_OUT to PA_IN. */ - prune_clobbered_mems (PA_OUT, block); + prune_clobbered_mems (PA_OUT, block, false); /* PA_IN starts with PA_OUT - TMP_GEN. Then we subtract things from ANTIC_IN. */