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

            Bug ID: 81009
           Summary: missing aliasing optimization for const restrict
                    pointers
           Product: gcc
           Version: 7.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: ---

One of the constraints on the use of restrict-qualified pointers is that the
const qualifier effectively imposes a binding requirement on such a pointer
preventing it from being used to modify the object (this is specified in
6.7.3.1, p4: "If L is used to access the value of the object X that it
designates, and X is also modified (by any means), then the following
requirements apply: T shall not be const-qualified.")

This constraint doesn't exist for ordinary (not restrict-qualified) const
pointers where the constness can be cast away and the result used to modify the
pointed-to object (provided the object itself isn't const).

GCC could take advantage of this constraint on programs by assuming that the
call to f() in g() below doesn't modify *p, thus eliminating the subsequent
test for x != *p.

$ cat t.c && gcc -O3 -S -Wall -Wrestrict -fdump-tree-optimized=/dev/stdout t.c

void f (const int*);

static void g (const int* restrict p)
{
  int x = *p;
  f (p);         // f() cannot modify *p here
  if (x != *p)   // cannot be true
    __builtin_abort ();
}

void h (void)
{
  int i = 0;
  g (&i);
}

;; Function h (h, funcdef_no=1, decl_uid=1799, cgraph_uid=1, symbol_order=1)

h ()
{
  int i;
  int _4;

  <bb 2> [100.00%]:
  i = 0;
  f (&i);
  _4 = MEM[(const int *)&i];
  if (_4 != 0)
    goto <bb 3>; [0.04%]
  else
    goto <bb 4>; [99.96%]

  <bb 3> [0.04%]:
  __builtin_abort ();

  <bb 4> [99.96%]:
  i ={v} {CLOBBER};
  return;

}

Reply via email to