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

            Bug ID: 94374
           Summary: modification of constant scalars sometimes eliminated,
                    sometimes not
           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: ---

Similar to pr94313, the test case below shows that GCC silently eliminates some
statements that modify const scalars while emitting others.  As in the other
report, while this isn't strictly wrong (the modification is undefined), doing
so silently, without a warning, is unhelpful.  It prevents the bugs from
getting fixed, and leads to inconsistent results at runtime.

At a minimum, GCC should diagnose all these cases before removing them. 
Preferably, however, it should treat all of them the same (i.e., either remove
all of them or none), ideally leaving the choice of which up to the user.  The
three alternative strategies that have been suggested as most useful are:
either a) removal, or b) insertion of __builtin_trap, or c) insertion of
__builtin_unreachable.

$ cat d.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout d.c
extern const int a;

int fa (void)
{ 
  int *p = (int*)&a;
  ++*p;                // not eliminated
  return *p;           // not folded
}

static const int b;

int fb (void)
{
  int *p = (int*)&b;
  ++*p;                // eliminated
  return *p;           // folded to zero
}

int fc (void)
{ 
  static const int c;
  int *p = (int*)&c;
  ++*p;                // eliminated
  return *p;           // folded to zero
}

int fd (void)
{
  const int d = 0;
  int *p = (int*)&d;
  ++*p;                // not eliminated
  return *p;           // folded to 1
}


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

fa ()
{
  int _1;
  int _2;

  <bb 2> [local count: 1073741824]:
  _1 = a;
  _2 = _1 + 1;
  a = _2;
  return _2;

}



;; Function fb (fb, funcdef_no=1, decl_uid=1936, cgraph_uid=2, symbol_order=2)

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

}



;; Function fc (fc, funcdef_no=5, decl_uid=1940, cgraph_uid=3, symbol_order=3)

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

}



;; Function fd (fd, funcdef_no=3, decl_uid=1945, cgraph_uid=4, symbol_order=4)

fd ()
{
  <bb 2> [local count: 1073741824]:
  return 1;

}

Reply via email to