From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id D9878385828D; Fri, 3 Mar 2023 11:06:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D9878385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677841618; bh=Z/kXe9a17YPW8eZUQAXZ6PE1phN7cOK/v89AifBKcs0=; h=From:To:Subject:Date:From; b=kXz23ybwNympjqNKp9e8zrEnDEqodV8PbQ6YoKsZGung7mwsN7r26mipIvoDCdO4H vezlp91Yi7v8Hh3+Rgbw/Vg7q2Nq2o5tq4Vac07yLcP43t+ORyfFh9qzsvAK3ANuTj FYqq5Qe8yq6+zIpuqz1tTt1HMOYt47NLIonSsdgc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6443] tree-optimization/109002 - partial PRE miscompilation X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: 59bc2b68de8041adf5eeb5bd18e5921f8a1f9567 X-Git-Newrev: 0132acc03cada2c3b47c48a205e821563153fc80 Message-Id: <20230303110658.D9878385828D@sourceware.org> Date: Fri, 3 Mar 2023 11:06:58 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0132acc03cada2c3b47c48a205e821563153fc80 commit r13-6443-g0132acc03cada2c3b47c48a205e821563153fc80 Author: Richard Biener Date: Fri Mar 3 10:41:29 2023 +0100 tree-optimization/109002 - partial PRE miscompilation Partial PRE ends up miscompiling the testcase in PR109002, likely involving a corner case when inifinite loops are involved. The following avoids the miscompilation by addressing a long-standing oddity that manifests in odd partial partial redundancies eliminated that are full redundancies. The oddity is that while we properly PHI translate the PA_IN set from the successors when computing PA_OUT but we fail to do the same for ANTIC_IN which is supposed to be unioned. That results in expressions with wrong virtual operands being placed in the PA_OUT/IN sets and the pruning machinery to go wrong because it assumes the expressions in the sets have virtual operands that are valid in the respective blocks. PR tree-optimization/109002 * tree-ssa-pre.cc (compute_partial_antic_aux): Properly PHI-translate ANTIC_IN. * gcc.dg/torture/pr109002.c: New testcase. Diff: --- gcc/testsuite/gcc.dg/torture/pr109002.c | 27 +++++++++++++++++++++++++++ gcc/tree-ssa-pre.cc | 20 ++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr109002.c b/gcc/testsuite/gcc.dg/torture/pr109002.c new file mode 100644 index 00000000000..5575a4b9edc --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr109002.c @@ -0,0 +1,27 @@ +/* { dg-do run } */ +/* { dg-additional-options "-ftree-pre -ftree-partial-pre" } */ + +extern void exit (int); + +int g; +int h; + +void __attribute__((noipa)) bar () +{ + if (g) + exit (0); +} + +int main(void) +{ + for (int i = 0; ; i++) { + for (int j = 0; j < g; j++); + if (i & 1) { + if (h) + continue; + if (g) + bar (); + g = 1; + } + } +} diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc index f77732d75c3..37cad36f2de 100644 --- a/gcc/tree-ssa-pre.cc +++ b/gcc/tree-ssa-pre.cc @@ -2364,11 +2364,14 @@ compute_partial_antic_aux (basic_block block, unsigned int i; bitmap_iterator bi; - FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi) - bitmap_value_insert_into_set (PA_OUT, - expression_for_id (i)); if (!gimple_seq_empty_p (phi_nodes (e->dest))) { + bitmap_set_t antic_in = bitmap_set_new (); + phi_translate_set (antic_in, ANTIC_IN (e->dest), e); + FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi) + bitmap_value_insert_into_set (PA_OUT, + expression_for_id (i)); + bitmap_set_free (antic_in); bitmap_set_t pa_in = bitmap_set_new (); phi_translate_set (pa_in, PA_IN (e->dest), e); FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi) @@ -2377,9 +2380,14 @@ compute_partial_antic_aux (basic_block block, bitmap_set_free (pa_in); } else - FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi) - bitmap_value_insert_into_set (PA_OUT, - expression_for_id (i)); + { + FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi) + bitmap_value_insert_into_set (PA_OUT, + expression_for_id (i)); + FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi) + bitmap_value_insert_into_set (PA_OUT, + expression_for_id (i)); + } } } }