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

            Bug ID: 98552
           Summary: Make more use of __builtin_undefined for assuring that
                    variables do not change
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoenig at gcc dot gnu.org
  Target Milestone: ---

Consider

void foo (int *);

void bar (int n)
{
  int i;
  for (i=0; i<n; i++)
    foo(&i);
}

void baz(int n)
{
  int i, j;
  for (i=0; i<n; i++)
    {
      j = i;
      foo (&i);
      if (j /= i)
        __builtin_unreachable();
    }
}

Assembly for bar and baz are identical, the loop is

.L9:
        leaq    12(%rsp), %rdi
        call    foo
        movl    12(%rsp), %eax
        addl    $1, %eax
        movl    %eax, 12(%rsp)
        cmpl    %ebx, %eax
        jl      .L9

In function bar, things are clear - the value of i has to be
reloaded from the stack, foo might have changed it.

However, this is not possible in baz.  j cannot be changed, and the
__builtin_unreachable ensures that i has the same value before and
after the call to foo. It need not be reloaded from the stack.

(The reason why I'm submitting this is another way to approach PR 31593 -
the Fortran front end could annotate code like this to inform
the middle end that DO loops variables are, in fact, invariant).

Reply via email to