On Mon, Jan 4, 2010 at 12:19 AM, Joshua Haberman <jhaber...@gmail.com> wrote:
> By the way, here is one case I tested where I was surprised GCC was not
> more aggressive:
>
>  extern void bar();
>  int foo(int *i) {
>    if(*i) bar();
>    return *i;
>  }
>
> With GCC 4.4.1 -O3 (Ubuntu, x86-64) this reloads *i if bar is called.  I
> suppose you have to allow that either "i" or "*i" is accessible as a
> global.  But this is a pretty big bummer, because it means that any
> function call forces reloads of any data that was read from a pointer.
> "restrict" doesn't help here, nor does "const".  The only way to prevent
> these reloads is to manually read the data into stack variable(s):
>
>  extern void bar();
>  int foo(int *i) {
>    int i_val = *i;
>    if(i_val) bar();
>    return i_val;
>  }

Well, i may point to any global integer variable and bar() might change it.
There's no way for a compiler to know that it does not unless it has access
to its function body.  You can mark the function with the pure or the const
attribute though.

Richard.


> Josh
>
>

Reply via email to