GCC optimization: killing variables or memory locations

Viewed 91

Is there a way to tell the optimizer of recent versions of GCC that a particular variable or memory location is dead at a particular point? Is there a way to tell it that a memory location won't change across a particular call?

For example, such knowledge can help the register allocator and will allow the compiler not to emit unneeded save/restore operations for memory locations (and/or variables).

So, I am looking for something like

x = __builtin_undefined ();

which overrides x with a conceptually undefined value, effectively killing its lifetime.

For the other problem of declaring a location as unmodified, I tried the following sample code

void f (int);

int g (int *p)
{
  int t = *p;
  f (t);
  if (t != *p)
    __builtin_unreachable ();
  return *p;
}

but at -O3, the compiler still accesses the memory twice:

g:
        pushq   %rbx
        movq    %rdi, %rbx
        movl    (%rdi), %edi
        call    f
        movl    (%rbx), %eax
        popq    %rbx
        ret

An example for unnecessary stores/loads is given as follows:

static void
g (int *p)
{
  /* frobnicates *p */
}

/* Returns the frobnicated value of *p and clobbers *p.  */
int
f (int *p)
{
  g (p);
  return *p;
}

The compiler cannot read the comment and so would have to write back the value that g calculates to *p even when g is fully inlined.

0 Answers
Related