Bug 107293 - Wrong code at -Os on x86_64-linux-gnu since r13-2876-g2460f7cdef7ef9c9
Summary: Wrong code at -Os on x86_64-linux-gnu since r13-2876-g2460f7cdef7ef9c9
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 13.0
: P1 normal
Target Milestone: 13.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
: 107196 (view as bug list)
Depends on:
Blocks:
 
Reported: 2022-10-17 12:06 UTC by Shaohua Li
Modified: 2022-10-21 06:28 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-10-17 00:00:00


Attachments
patch in testing (1.13 KB, patch)
2022-10-17 16:59 UTC, Aldy Hernandez
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Shaohua Li 2022-10-17 12:06:09 UTC
% gcc-tk -v
Using built-in specs.
COLLECT_GCC=gcc-tk
COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-acdb24166d13d87c374e578d2ad5d58249171930/libexec/gcc/x86_64-pc-linux-gnu/13.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap --enable-languages=c,c++ --prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-acdb24166d13d87c374e578d2ad5d58249171930
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.0.0 20221017 (experimental) (GCC)
%
% gcc-tk -w -O0 a.c && ./a.out
1
% gcc-tk -w -Os a.c && ./a.out
0
% cat a.c
short a;
int b[1];

int c(int p) {
  return (p < 0) ? 0 : 10 + ((p / 100 - 16) / 4);
}

void f(int n) {
  while (1) {
    int m = n;
    while ((m ) )
      m /= 2;
    break;
  }
}

void g() {
  int h = a = 0;
  for (; h + a <= 0; a++) {
    if (b[c(a - 6)])
      break;
    f(a);
  }
}
int main() {
  g();
  __builtin_printf("%d\n", a);
}

%

Compiler explorer: https://godbolt.org/z/e7sssT7cq
Comment 1 Martin Liška 2022-10-17 12:11:39 UTC
Started with r13-2876-g2460f7cdef7ef9c9.
Comment 2 Aldy Hernandez 2022-10-17 16:59:00 UTC
Created attachment 53717 [details]
patch in testing
Comment 3 GCC Commits 2022-10-17 18:56:11 UTC
The master branch has been updated by Aldy Hernandez <aldyh@gcc.gnu.org>:

https://gcc.gnu.org/g:c9b840db504d15db01f06fe1fec38282dbafee07

commit r13-3341-gc9b840db504d15db01f06fe1fec38282dbafee07
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Mon Oct 17 18:56:24 2022 +0200

    Make sure exported range for SSA post-dominates the DEF in set_global_ranges_from_unreachable_edges.
    
    The problem here is that we're exporting a range for an SSA range that
    happens on the other side of a __builtin_unreachable, but the SSA does
    not post-dominate the definition point.  This is causing ivcanon to
    unroll things incorrectly.
    
    This was a snafu when converting the code from evrp.
    
            PR tree-optimization/107293
    
    gcc/ChangeLog:
    
            * tree-ssa-dom.cc
            (dom_opt_dom_walker::set_global_ranges_from_unreachable_edges):
            Check that condition post-dominates the definition point.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr107293.c: New test.
Comment 4 Aldy Hernandez 2022-10-17 18:57:50 UTC
fixed
Comment 5 Andrew Pinski 2022-10-21 06:28:42 UTC
*** Bug 107196 has been marked as a duplicate of this bug. ***