From patchwork Sat May 28 05:51:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 54477 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 BB621384A876 for ; Sat, 28 May 2022 05:51:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB621384A876 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653717108; bh=3FlTOYvIXfP2QBmB8pEmBS6ZPz0smFQBmiSZdaAS2Qs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=cf46zpi2Ye0vvot2xt8RDQUD9+QOM4AMvdFMZC5ZhIA2lBjGeLBPr2b2xCnxJBI6k u2epS3vonnqVjhIZTDVYuUPXtE9K6jt95fcw/dKv4rF/9qBKR/5sbg5SYHP0tKWONv EIWV6MH8/OuBiq1xf7HFy4axDIki4YuE8jWWEjsc= 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 BDD70385608E for ; Sat, 28 May 2022 05:51:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BDD70385608E Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id DD805116307; Sat, 28 May 2022 01:51:16 -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 4hAJcBfdiCot; Sat, 28 May 2022 01:51:16 -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 69036116215; Sat, 28 May 2022 01:51:16 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 24S5p3Pj630268 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 28 May 2022 02:51:04 -0300 To: gcc-patches@gcc.gnu.org Subject: [PATCH] [PR105665] ivopts: check defs of names in base for undefs Organization: Free thinker, does not speak for AdaCore Date: Sat, 28 May 2022 02:51:02 -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 Cc: Bin Cheng Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The patch for PR 100810 tested for undefined SSA_NAMEs appearing directly in the base expression of the potential IV candidate, but that's not enough. The testcase for PR105665 shows an undefined SSA_NAME has the same ill effect if it's referenced as an PHI_NODE arg in the referenced SSA_NAME. The variant of that test shows it can be further removed from the referenced SSA_NAME. To avoid deep recursion, precompute SSA_NAMEs deemed unsuitable candidates, so that we can skip them with a flag test. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog PR tree-optimization/105665 PR tree-optimization/100810 * tree-ssa-loop-ivopts.cc (mark_ssa_undefs): Precompute unsuitability of SSA_NAMEs in TREE_VISITED. (find_ssa_undef): Check the precomputed flag. (tree_ssa_iv_optimize): Call mark_ssa_undefs. for gcc/testsuite/ChangeLog PR tree-optimization/105665 PR tree-optimization/100810 * gcc.dg/torture/pr105665.c: New. --- gcc/testsuite/gcc.dg/torture/pr105665.c | 20 ++++++++++ gcc/tree-ssa-loop-ivopts.cc | 62 ++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr105665.c diff --git a/gcc/testsuite/gcc.dg/torture/pr105665.c b/gcc/testsuite/gcc.dg/torture/pr105665.c new file mode 100644 index 0000000000000..34cfc65843495 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr105665.c @@ -0,0 +1,20 @@ +/* { dg-do run } */ + +int a, b, c[1], d[2], *e = c; +int main() { + int f = 0; + for (; b < 2; b++) { + int g; + if (f) + g++, b = 40; + a = d[b * b]; + for (f = 0; f < 3; f++) { + if (e) + break; + g--; + if (a) + a = g; + } + } + return 0; +} diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc index 81b536f930415..d8200f2a53b21 100644 --- a/gcc/tree-ssa-loop-ivopts.cc +++ b/gcc/tree-ssa-loop-ivopts.cc @@ -3071,13 +3071,70 @@ get_loop_invariant_expr (struct ivopts_data *data, tree inv_expr) return *slot; } -/* Find the first undefined SSA name in *TP. */ +/* Mark as TREe_VISITED any SSA_NAMEs that are unsuitable as ivopts + candidates for potentially involving undefined behavior. */ + +static void +mark_ssa_undefs (void) +{ + auto_vec queue; + + unsigned int i; + tree var; + FOR_EACH_SSA_NAME (i, var, cfun) + { + if (SSA_NAME_IS_VIRTUAL_OPERAND (var) + || ssa_defined_default_def_p (var) + || !ssa_undefined_value_p (var, false)) + TREE_VISITED (var) = false; + else + { + TREE_VISITED (var) = true; + queue.safe_push (var); + if (dump_file) + fprintf (dump_file, "marking _%i as undef\n", + SSA_NAME_VERSION (var)); + } + } + + while (!queue.is_empty ()) + { + var = queue.pop (); + gimple *stmt; + imm_use_iterator iter; + FOR_EACH_IMM_USE_STMT (stmt, iter, var) + { + if (is_gimple_call (stmt) || is_a (stmt)) + continue; + + def_operand_p defvar; + ssa_op_iter diter; + FOR_EACH_PHI_OR_STMT_DEF (defvar, stmt, diter, SSA_OP_DEF) + { + gcc_checking_assert (is_gimple_assign (stmt) + || is_a (stmt)); + tree def = DEF_FROM_PTR (defvar); + if (TREE_VISITED (def)) + continue; + TREE_VISITED (def) = true; + queue.safe_push (def); + if (dump_file) + fprintf (dump_file, "Marking _%i as undef because of _%i\n", + SSA_NAME_VERSION (def), SSA_NAME_VERSION (var)); + } + } + } +} + +/* Return *TP if it is an SSA_NAME marked with TREE_VISITED, i.e., as + unsuitable as ivopts candidates for potentially involving undefined + behavior. */ static tree find_ssa_undef (tree *tp, int *walk_subtrees, void *) { if (TREE_CODE (*tp) == SSA_NAME - && ssa_undefined_value_p (*tp, false)) + && TREE_VISITED (*tp)) return *tp; if (!EXPR_P (*tp)) *walk_subtrees = 0; @@ -8192,6 +8249,7 @@ tree_ssa_iv_optimize (void) auto_bitmap toremove; tree_ssa_iv_optimize_init (&data); + mark_ssa_undefs (); /* Optimize the loops starting with the innermost ones. */ for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))