Bug 57521 - [4.7 Regression] wrong code for expressions in loop at -O3
Summary: [4.7 Regression] wrong code for expressions in loop at -O3
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 4.7.4
Assignee: Richard Biener
URL:
Keywords:
: 57608 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-06-04 00:47 UTC by Dara Hazeghi
Modified: 2014-03-18 08:47 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.2, 4.9.0
Known to fail: 4.8.1
Last reconfirmed: 2013-06-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dara Hazeghi 2013-06-04 00:47:43 UTC
The following code is miscompiled with current gcc trunk at -O3 optimization on x86_64-linux.  I've tried to reduce it somewhat from the original form.  Note that 'e' is only touched in one place.  This also fails on 4.7 and 4.8 and is a regression from gcc 4.6.


$ gcc-trunk -v
gcc version 4.9.0 20130603 (experimental) [trunk revision 199601] (GCC) 
$ gcc-trunk -O2 wrong.c 
$ ./a.out 
1
$ gcc-4.6 -O3 wrong.c 
$ ./a.out 
1
$ gcc-trunk -O3 wrong.c 
$ ./a.out 
0
$
--------------------------------------

int printf (const char *, ...);

int a, b, c, d, o = 1, p;
short e;

int
fn1 (int * p1)
{
    int f, g, h, j = 0, k = 0, l = 0;
    unsigned int i;
    int *m[1] = { &l };
    for (; b >= 0; b--)
    {
        if (*p1)
            if (j >= 0)
            {
                int n = 1;
                e = 1;
                h = a ? a : 1 % n;
                g = h > 0 ? 0 : h + 1;
                k = c + g;
            }
            else
                continue;
        else
        {
            
            f = d > 0 ? 0 : d + 1;
            i = f;
            j = 1 + i;
        }
        l++;
    }
    return k;
}

int
main ()
{
    for (;; p++)
    {
        fn1 (&o);
        break;
    }
    printf ("%d\n", e);
    return 0;
}
Comment 1 Jakub Jelinek 2013-06-04 05:33:13 UTC
Started with r175704 aka PR46787.
Comment 2 Richard Biener 2013-06-04 12:19:45 UTC
Hmhm, mine.
Comment 3 Richard Biener 2013-06-24 13:48:08 UTC
It's if-conversion (we do not vectorize anything here).  The predicates
are inserted correctly but the wrong ones are being used for the
predication.  That is because the predecessor edge we chose in find_phi_replacement_condition is not fallthru and thus the predicate
we chose is bogus.  It seems to assume that critical edges are split.

I have a patch.
Comment 4 Richard Biener 2013-08-27 10:12:09 UTC
Author: rguenth
Date: Tue Aug 27 10:10:34 2013
New Revision: 202019

URL: http://gcc.gnu.org/viewcvs?rev=202019&root=gcc&view=rev
Log:
2013-08-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/57521
	* tree-if-conv.c (if_convertible_bb_p): Verify that at least
	one edge is non-critical.
	(find_phi_replacement_condition): Make sure to use a non-critical
	edge.  Cleanup and remove old bug workarounds.
	(bb_postdominates_preds): Remove.
	(if_convertible_loop_p_1): Do not compute post-dominators.
	(combine_blocks): Do not free post-dominators.
	(main_tree_if_conversion): Likewise.
	(pass_data_if_conversion): Add TODO_verify_ssa.

	* gcc.dg/torture/pr57521.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr57521.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-if-conv.c
Comment 5 Richard Biener 2013-09-09 09:51:20 UTC
Author: rguenth
Date: Mon Sep  9 09:51:18 2013
New Revision: 202388

URL: http://gcc.gnu.org/viewcvs?rev=202388&root=gcc&view=rev
Log:
2013-09-09  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2013-08-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/57521
	* tree-if-conv.c (if_convertible_bb_p): Verify that at least
	one edge is non-critical.
	(find_phi_replacement_condition): Make sure to use a non-critical
	edge.  Cleanup and remove old bug workarounds.
	(bb_postdominates_preds): Remove.
	(if_convertible_loop_p_1): Do not compute post-dominators.
	(combine_blocks): Do not free post-dominators.
	(main_tree_if_conversion): Likewise.

	* gcc.dg/torture/pr57521.c: New testcase.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr57521.c
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_8-branch/gcc/tree-if-conv.c
Comment 6 Richard Biener 2013-10-25 10:30:28 UTC
*** Bug 57608 has been marked as a duplicate of this bug. ***
Comment 7 Richard Biener 2014-03-18 08:46:53 UTC
Author: rguenth
Date: Tue Mar 18 08:46:21 2014
New Revision: 208632

URL: http://gcc.gnu.org/viewcvs?rev=208632&root=gcc&view=rev
Log:
2014-03-18  Richard Biener  <rguenther@suse.de>
 
	Backport from mainline
	2013-08-27  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/57521
	* tree-if-conv.c (if_convertible_bb_p): Verify that at least
	one edge is non-critical.
	(find_phi_replacement_condition): Make sure to use a non-critical
	edge.  Cleanup and remove old bug workarounds.
	(bb_postdominates_preds): Remove.
	(if_convertible_loop_p_1): Do not compute post-dominators.
	(combine_blocks): Do not free post-dominators.
	(main_tree_if_conversion): Likewise.

	* gcc.dg/torture/pr57521.c: New testcase.

	2013-09-03  Richard Biener  <rguenther@suse.de>

	PR middle-end/57656
	* fold-const.c (negate_expr_p): Fix division case.
	(negate_expr): Likewise.

	* gcc.dg/torture/pr57656.c: New testcase.

	2013-11-19  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/57517
	* tree-predcom.c (combinable_refs_p): Verify the combination
	is always executed when the refs are.

	* gfortran.fortran-torture/compile/pr57517.f90: New testcase.
	* gcc.dg/torture/pr57517.c: Likewise.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gcc.dg/torture/pr57517.c
    branches/gcc-4_7-branch/gcc/testsuite/gcc.dg/torture/pr57521.c
    branches/gcc-4_7-branch/gcc/testsuite/gcc.dg/torture/pr57656.c
    branches/gcc-4_7-branch/gcc/testsuite/gfortran.fortran-torture/compile/pr57517.f90
Modified:
    branches/gcc-4_7-branch/gcc/ChangeLog
    branches/gcc-4_7-branch/gcc/fold-const.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_7-branch/gcc/tree-if-conv.c
    branches/gcc-4_7-branch/gcc/tree-predcom.c
Comment 8 Richard Biener 2014-03-18 08:47:30 UTC
Fixed.