https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93411

            Bug ID: 93411
           Summary: variable reference to constant array with same
                    elements not folded
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

This was prompted by pr86912 (not sure if the problem there would be solved by
solving this one).  The following test case shows that while constant
references to constant arrays are folded (both in-bounds and out-of-bounds),
non-constant references are not even though they could be.

$ cat u.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout u.c
const int a[] = { 0, 0, 0 };

int idx_cst_in_bounds (void)
{
  return a[1];   // valid, folded to 0 (good)
}

int idx_cst_out_of_bounds (void)
{
  return a[-123];   // invalid, folded to 0 without a warning (see pr86691)
}

int idx_rng_in_bounds (int i)
{
  if (i < 0 || 3 < i) i = 0;
  return a[i];   // valid, not folded but could/should be
}

int idx_unknown (int i)
{
  return a[i];   // valid, not folded but could/should be
}

int idx_rng_out_of_bounds (int i)
{
  if (i >= 0) i = -1;
  return a[i];   // invalid, not folded with warning; should it be?
}

;; Function idx_cst_in_bounds (idx_cst_in_bounds, funcdef_no=0, decl_uid=1931,
cgraph_uid=1, symbol_order=1)

idx_cst_in_bounds ()
{
  <bb 2> [local count: 1073741824]:
  return 0;

}



;; Function idx_cst_out_of_bounds (idx_cst_out_of_bounds, funcdef_no=6,
decl_uid=1934, cgraph_uid=2, symbol_order=2)

idx_cst_out_of_bounds ()
{
  <bb 2> [local count: 1073741824]:
  return 0;

}



;; Function idx_rng_in_bounds (idx_rng_in_bounds, funcdef_no=2, decl_uid=1937,
cgraph_uid=3, symbol_order=3)

Removing basic block 5
idx_rng_in_bounds (int i)
{
  unsigned int i.0_1;
  int pretmp_6;
  int prephitmp_7;

  <bb 2> [local count: 1073741824]:
  i.0_1 = (unsigned int) i_3(D);
  if (i.0_1 > 3)
    goto <bb 4>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 536870912]:
  pretmp_6 = a[i_3(D)];

  <bb 4> [local count: 1073741824]:
  # prephitmp_7 = PHI <pretmp_6(3), 0(2)>
  return prephitmp_7;

}



;; Function idx_unknown (idx_unknown, funcdef_no=3, decl_uid=1940,
cgraph_uid=4, symbol_order=4)

idx_unknown (int i)
{
  int _3;

  <bb 2> [local count: 1073741824]:
  _3 = a[i_2(D)];
  return _3;

}


u.c: In function ‘idx_rng_out_of_bounds’:
u.c:27:11: warning: array subscript -1 is below array bounds of ‘const int[3]’
[-Warray-bounds]
   27 |   return a[i];   // not folded with warning; should it be?
      |          ~^~~
u.c:1:11: note: while referencing ‘a’
    1 | const int a[] = { 0, 0, 0 };
      |           ^

;; Function idx_rng_out_of_bounds (idx_rng_out_of_bounds, funcdef_no=4,
decl_uid=1943, cgraph_uid=5, symbol_order=5)

idx_rng_out_of_bounds (int i)
{
  int _4;

  <bb 2> [local count: 1073741824]:
  i_2 = MIN_EXPR <i_1(D), -1>;
  _4 = a[i_2];
  return _4;

}

Reply via email to