Bug 81556 - [7/8 Regression] Wrong code at -O2
Summary: [7/8 Regression] Wrong code at -O2
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 7.2
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks: yarpgen
  Show dependency treegraph
 
Reported: 2017-07-26 01:43 UTC by Dmitry Babokin
Modified: 2021-11-01 23:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-26 00:00:00


Attachments
gcc8-pr81556.patch (1.23 KB, patch)
2017-07-26 10:07 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Babokin 2017-07-26 01:43:47 UTC
gcc trunk, rev250545, x86_64.

This one seems to be different from previously reported bugs. Minimum opt level to trigger bug is -O2, switching slsr doesn't help (-fno-tree-slsr).

> cat f.cpp
#include <stdio.h>

unsigned long int var_0 = 13272098465497875865UL;
unsigned long int var_1 = 15341539099603541390UL;
unsigned long int var_2 = 2;
unsigned long int var_3 = 17471558040813171499UL;
unsigned long int var_4 = 1;
unsigned long int var_5 = 0;
unsigned long int var_6 = 0;

void foo() {
  bool a = var_2 > 1;
  var_5 = var_4 % ((var_2 > 1) << 9);
  var_6 = a & (var_3 & (a & var_0 & var_1));
}

int main() {
  foo();
  printf("0x%llx, 0x%llx\n", var_5, var_6);
  return 0;
}

> g++ -O0 f.cpp -o out; ./out
0x1, 0x0

> g++ -O2 f.cpp -o out; ./out
0x1, 0x9020100000000108
Comment 1 Marc Glisse 2017-07-26 06:27:37 UTC
Reassoc not clearing the VRP info?
Comment 2 Jakub Jelinek 2017-07-26 08:25:01 UTC
Started with r242075.
Comment 3 Jakub Jelinek 2017-07-26 08:35:39 UTC
But it is indeed true that this seems to be reassoc.  That pass shouldn't be resetting the range info, instead it shouldn't reuse SSA_NAMEs unless they must have the same value as before.  It has code to avoid the reusing, but most likely this is a corner case that isn't handled well.
I'll have a look.
Comment 4 Jakub Jelinek 2017-07-26 10:07:00 UTC
Created attachment 41832 [details]
gcc8-pr81556.patch

Untested fix.  The current code whether a SSA_NAME can be reused or not is based on whether all the rhs2 ops are the same from the toplevel stmt (and whether there is powi/negate).  That should work fine as long as the number of ops is the same after optimization, but in this case there is a removal of reduncant op, at which point that doesn't work anymore.
Comment 5 Jakub Jelinek 2017-07-27 08:49:54 UTC
Author: jakub
Date: Thu Jul 27 08:49:16 2017
New Revision: 250609

URL: https://gcc.gnu.org/viewcvs?rev=250609&root=gcc&view=rev
Log:
	PR tree-optimization/81555
	PR tree-optimization/81556
	* tree-ssa-reassoc.c (rewrite_expr_tree): Add NEXT_CHANGED argument,
	if true, force CHANGED for the recursive invocation.
	(reassociate_bb): Remember original length of ops array, pass
	len != orig_len as NEXT_CHANGED in rewrite_expr_tree call.

	* gcc.c-torture/execute/pr81555.c: New test.
	* gcc.c-torture/execute/pr81556.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr81555.c
    trunk/gcc/testsuite/gcc.c-torture/execute/pr81556.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-reassoc.c
Comment 6 Jakub Jelinek 2017-07-27 09:06:41 UTC
Author: jakub
Date: Thu Jul 27 09:06:08 2017
New Revision: 250610

URL: https://gcc.gnu.org/viewcvs?rev=250610&root=gcc&view=rev
Log:
	PR tree-optimization/81555
	PR tree-optimization/81556
	* tree-ssa-reassoc.c (rewrite_expr_tree): Add NEXT_CHANGED argument,
	if true, force CHANGED for the recursive invocation.
	(reassociate_bb): Remember original length of ops array, pass
	len != orig_len as NEXT_CHANGED in rewrite_expr_tree call.

	* gcc.c-torture/execute/pr81555.c: New test.
	* gcc.c-torture/execute/pr81556.c: New test.

Added:
    branches/gcc-7-branch/gcc/testsuite/gcc.c-torture/execute/pr81555.c
    branches/gcc-7-branch/gcc/testsuite/gcc.c-torture/execute/pr81556.c
Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
    branches/gcc-7-branch/gcc/tree-ssa-reassoc.c
Comment 7 Jakub Jelinek 2017-07-27 09:33:11 UTC
Author: jakub
Date: Thu Jul 27 09:32:33 2017
New Revision: 250612

URL: https://gcc.gnu.org/viewcvs?rev=250612&root=gcc&view=rev
Log:
	PR tree-optimization/81555
	PR tree-optimization/81556
	* tree-ssa-reassoc.c (rewrite_expr_tree): Add NEXT_CHANGED argument,
	if true, force CHANGED for the recursive invocation.
	(reassociate_bb): Remember original length of ops array, pass
	len != orig_len as NEXT_CHANGED in rewrite_expr_tree call.

	* gcc.c-torture/execute/pr81555.c: New test.
	* gcc.c-torture/execute/pr81556.c: New test.

Added:
    branches/gcc-6-branch/gcc/testsuite/gcc.c-torture/execute/pr81555.c
    branches/gcc-6-branch/gcc/testsuite/gcc.c-torture/execute/pr81556.c
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/tree-ssa-reassoc.c
Comment 8 Jakub Jelinek 2017-07-27 09:43:34 UTC
Author: jakub
Date: Thu Jul 27 09:43:01 2017
New Revision: 250616

URL: https://gcc.gnu.org/viewcvs?rev=250616&root=gcc&view=rev
Log:
	PR tree-optimization/81555
	PR tree-optimization/81556
	* tree-ssa-reassoc.c (rewrite_expr_tree): Add NEXT_CHANGED argument,
	if true, force CHANGED for the recursive invocation.
	(reassociate_bb): Remember original length of ops array, pass
	len != orig_len as NEXT_CHANGED in rewrite_expr_tree call.

	* gcc.c-torture/execute/pr81555.c: New test.
	* gcc.c-torture/execute/pr81556.c: New test.

Added:
    branches/gcc-5-branch/gcc/testsuite/gcc.c-torture/execute/pr81555.c
    branches/gcc-5-branch/gcc/testsuite/gcc.c-torture/execute/pr81556.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
    branches/gcc-5-branch/gcc/tree-ssa-reassoc.c
Comment 9 Jakub Jelinek 2017-07-28 07:16:31 UTC
Fixed.
Comment 10 Aldy Hernandez 2017-09-13 15:51:25 UTC
Author: aldyh
Date: Wed Sep 13 15:50:53 2017
New Revision: 252110

URL: https://gcc.gnu.org/viewcvs?rev=252110&root=gcc&view=rev
Log:
	PR tree-optimization/81555
	PR tree-optimization/81556
	* tree-ssa-reassoc.c (rewrite_expr_tree): Add NEXT_CHANGED argument,
	if true, force CHANGED for the recursive invocation.
	(reassociate_bb): Remember original length of ops array, pass
	len != orig_len as NEXT_CHANGED in rewrite_expr_tree call.

	* gcc.c-torture/execute/pr81555.c: New test.
	* gcc.c-torture/execute/pr81556.c: New test.

Added:
    branches/range-gen2/gcc/testsuite/gcc.c-torture/execute/pr81555.c
    branches/range-gen2/gcc/testsuite/gcc.c-torture/execute/pr81556.c
Modified:
    branches/range-gen2/gcc/ChangeLog
    branches/range-gen2/gcc/testsuite/ChangeLog
    branches/range-gen2/gcc/tree-ssa-reassoc.c