From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id D14583858D1E for ; Tue, 29 Nov 2022 13:30:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D14583858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de 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-out1.suse.de (Postfix) with ESMTPS id AD2B221BBE for ; Tue, 29 Nov 2022 13:30:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1669728622; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=K5uNyjwCnwv0CuaP895NNVCzPRRYqCVmwbOgj15c3Mo=; b=qOxoMFQXq8yZPl8KHPDejP75v+3rJedvE4dSZDuTypiCSjXDX1OffyFA7gErum69rrVCzJ uxBtnV9+z+lrXdRpBq0S97x+jigzgHDiIqy/CrLQc81IV2EKbl1oTZ4d2HIXNiy19jRtNf n7NmQNKDHu68gA8oy2EZE53Phba7oFY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1669728622; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=K5uNyjwCnwv0CuaP895NNVCzPRRYqCVmwbOgj15c3Mo=; b=eC9AP282neXwMuN6z3lzXAWs+2pNec4yjuJ5pEGUPSyojped2gZavWuXfJ56wEV69nY5Cb ueTEeugoZEKH/zDQ== 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 99C0C13AF6 for ; Tue, 29 Nov 2022 13:30:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id jV2GJG4JhmMMMQAAMHmgww (envelope-from ) for ; Tue, 29 Nov 2022 13:30:22 +0000 Date: Tue, 29 Nov 2022 14:30:22 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107852 - missed optimization with PHIs MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20221129133022.99C0C13AF6@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The following deals with the situation where we have [local count: 1073741824]: _5 = bytes.D.25336._M_impl.D.24643._M_start; _6 = bytes.D.25336._M_impl.D.24643._M_finish; pretmp_66 = bytes.D.25336._M_impl.D.24643._M_end_of_storage; if (_5 != _6) goto ; [70.00%] else goto ; [30.00%] ... [local count: 329045359]: _89 = operator new (4); _43 = bytes.D.25336._M_impl.D.24643._M_start; _Num_44 = _137 - _43; if (_Num_44 != 0) but fail to see that _137 is equal to _5 and thus eventually _Num_44 is zero if not operator new would possibly clobber the global bytes variable. The following resolves this in value-numbering by using the predicated values for _5 == _6 recorded for the dominating condition. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/107852 * tree-ssa-sccvn.cc (visit_phi): Use equivalences recorded as predicated values to elide more redundant PHIs. * gcc.dg/tree-ssa/ssa-fre-101.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c | 47 +++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 51 ++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c new file mode 100644 index 00000000000..c67f211dcf6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-fre1-details" } */ + +int test1 (int i, int j) +{ + int k; + if (i != j) + k = i; + else + k = j; + return k; +} + +int test2 (int i, int j) +{ + int k; + if (i != j) + k = j; + else + k = i; + return k; +} + +int test3 (int i, int j) +{ + int k; + if (i == j) + k = j; + else + k = i; + return k; +} + +int test4 (int i, int j) +{ + int k; + if (i == j) + k = i; + else + k = j; + return k; +} + +/* We'd expect 4 hits but since we only keep one forwarder the + VN predication machinery cannot record something for the entry + block since it doesn't work on edges but on their source. */ +/* { dg-final { scan-tree-dump-times "equal on edge" 2 "fre1" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 1f9c6c53b52..6895ae84d13 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -5814,7 +5814,8 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p) /* See if all non-TOP arguments have the same value. TOP is equivalent to everything, so we can ignore it. */ - FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) + basic_block bb = gimple_bb (phi); + FOR_EACH_EDGE (e, ei, bb->preds) if (e->flags & EDGE_EXECUTABLE) { tree def = PHI_ARG_DEF_FROM_EDGE (phi, e); @@ -5859,6 +5860,54 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p) && known_eq (soff, doff)) continue; } + /* There's also the possibility to use equivalences. */ + if (!FLOAT_TYPE_P (TREE_TYPE (def))) + { + vn_nary_op_t vnresult; + tree ops[2]; + ops[0] = def; + ops[1] = sameval; + tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR, + boolean_type_node, + ops, &vnresult); + if (! val && vnresult && vnresult->predicated_values) + { + val = vn_nary_op_get_predicated_value (vnresult, e->src); + if (val && integer_truep (val)) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Predication says "); + print_generic_expr (dump_file, def, TDF_NONE); + fprintf (dump_file, " and "); + print_generic_expr (dump_file, sameval, TDF_NONE); + fprintf (dump_file, " are equal on edge %d -> %d\n", + e->src->index, e->dest->index); + } + continue; + } + /* If on all previous edges the value was equal to def + we can change sameval to def. */ + if (EDGE_COUNT (bb->preds) == 2 + && (val = vn_nary_op_get_predicated_value + (vnresult, EDGE_PRED (bb, 0)->src)) + && integer_truep (val)) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Predication says "); + print_generic_expr (dump_file, def, TDF_NONE); + fprintf (dump_file, " and "); + print_generic_expr (dump_file, sameval, TDF_NONE); + fprintf (dump_file, " are equal on edge %d -> %d\n", + EDGE_PRED (bb, 0)->src->index, + EDGE_PRED (bb, 0)->dest->index); + } + sameval = def; + continue; + } + } + } sameval = NULL_TREE; break; } -- 2.35.3