On Sat, Oct 15, 2011 at 11:52 PM, Ulrich Drepper <drep...@gmail.com> wrote:
> I think gcc should allow the programmer to tell it something about a
> function return value even if the function is inlined and the compiler
> can see all the code.  Consider the code below.
>
> If NOINLINE is defined the compiler will call g once.  No need to do
> anything after the h() call since the function is marked const.
>
> If NOINLINE is not defined and the compiler sees the asm statement it
> will expand the function body twice.  Don't worry about the content of
> the asm, this is correct in the case I care about.  What I expect is
> that gcc still respects that the function is marked const and performs
> the same optimization as in the case when the function is not inlined.
>
> Is there anything I miss how to achieve this?  I don't think so in
> which case, do people agree that this should be changed?

It's difficult to do this in general given that we give up for
statements in most optimization passes that would make
the function not auto-discovered as const (that is, a volatile
asm such as yours).

So the case boils down to a optimization phase ordering issue.
While we do CSE before inlining the function the CSE opportunity
is only discovered after doing loop invariant motion - which is done
way after we inlined the function (and you can probably see that
the asm() is not considered loop invariant either).

The question is now, of course why you need to emit calls
from an asm() statement, something which isn't guaranteed
to work anyway (IIRC).

Richard.

>
> extern int **g(void) __attribute__((const, nothrow));
> #ifndef NOINLINE
> extern inline int ** __attribute__((always_inline, const, nothrow,
> gnu_inline, artificial)) g(void) {
>  int **p;
>  asm ("call g@plt" : "=a" (p));
>  return p;
> }
> #endif
>
> #define pr(c) ((*g())[c] & 0x80)
>
> extern void h(void);
>
> int
> f(const char *s)
> {
>  int c = 0;
>  for (int i = 0; i < 20; ++i)
>    c |= pr(s[i]);
>
>  h();
>
>  for (int i = 20; i < 40; ++i)
>    c |= pr(s[i]);
>
>  return c;
> }
>

Reply via email to