Bug 87926 - bad array-index warning breaks --disable-checking bootstrap
Summary: bad array-index warning breaks --disable-checking bootstrap
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: build, diagnostic, missed-optimization
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2018-11-07 22:38 UTC by Nathan Sidwell
Modified: 2019-11-21 09:03 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-21 00:00:00


Attachments
cc1plus bug.ii -O2 -W -Wall -quiet (221 bytes, text/plain)
2018-11-07 22:38 UTC, Nathan Sidwell
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Sidwell 2018-11-07 22:38:23 UTC
Created attachment 44967 [details]
cc1plus bug.ii -O2 -W -Wall -quiet

x86_64-linux --disable-checking bootstrap fails with:
../../../src/gcc/bitmap.c: In function ‘unsigned int bitmap_last_set_bit(const_bitmap)’:
../../../src/gcc/bitmap.c:1191:26: error: array subscript -1 is below array bounds of ‘const BITMAP_WORD [2]’ {aka ‘const long unsigned int [2]’} [-Werror=array-bounds]
  1191 |       word = elt->bits[ix];
       |              ~~~~~~~~~~~~^

Attached is a reduced testcase

Adding:
bitmap.o-warn = -Wno-error
to gcc/Makefile.in works around the problem.
Comment 1 Nathan Sidwell 2018-11-07 22:50:52 UTC
Author: nathan
Date: Wed Nov  7 22:50:20 2018
New Revision: 265899

URL: https://gcc.gnu.org/viewcvs?rev=265899&root=gcc&view=rev
Log:
[PR/87936] --disable-checking bootstrap break

https://gcc.gnu.org/ml/gcc-patches/2018-11/msg00502.html
	PR 87926
	* Makefile.in (bitmap.o-warn): Add -Wno-error to unbreak
	--disable-checking bootstrap.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/Makefile.in
Comment 2 Richard Biener 2018-11-08 09:13:45 UTC
Please use -Wno-error=array-bounds instead of disabling _all_ errors.
Comment 3 Martin Liška 2018-11-20 08:51:47 UTC
Nathan: Can the bug be marked as resolved?
Comment 4 Nathan Sidwell 2018-11-20 15:05:17 UTC
Marxin, the underlying problem is still there, I think
Comment 5 Nathan Sidwell 2018-11-20 15:54:44 UTC
Author: nathan
Date: Tue Nov 20 15:54:12 2018
New Revision: 266319

URL: https://gcc.gnu.org/viewcvs?rev=266319&root=gcc&view=rev
Log:
	PR 87926
	* Makefile.in (bitmap.o-warn): Use -Wno-error=array-bounds.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/Makefile.in
Comment 6 Richard Biener 2018-11-21 11:42:15 UTC
Confirmed.  I _think_ I've seen a duplicate at some point.  Given

  for (ix = 2 - 1; ix >= 0; ix--)
    {
      BITMAP_WORD word = elt->bits[ix];
      if (word)
        goto found_bit;
    }
  __builtin_unreachable ();

we compute that the loop exit test is never true (ix < 0) and thus elide it.
That causes us to unroll the loop based on the size of elt->bits[] given
that constrains the number of iterations now which is where we hit an
older complete unrolling issue that it doesn't mark all unreachable bits
properly unreachable.  That is, remove_exits_and_undefined_stmts isn't
doing it's job properly.  Hmm, because a bound is recorded for the IV
decrement (range info!) but not the load.  Ah - trailing array.  So
the issue is indeed a dup of some PR because the IV already goes out of
bound for the latch of the previous loop copy.  Now go and find the dup...

We can avoid the warning by refactoring the code to

  for (ix = 2 - 1; ix >= 1; ix--)
    {
      BITMAP_WORD word = elt->bits[ix];
      if (word)
        goto found_bit;
    }
  gcc_assert (elt->bits[ix]);
found_bit:
  return ix;

which should be even faster eliding the read of elt->bits[0] for --disable-checking.