Re: const versus __attribute__((const))

Jamie Lokier
2003-12-09T03:00:03Z

H. Peter Anvin wrote:
> Actually, the reason it doesn't use it for the inlines is because it
> doesn't need to -- it already has full visibility, so it doesn't need it
> to be spelled out.

Until a few minutes ago, I disagreed. I thought GCC wouldn't
eliminate calls to inline functions which contain an inline asm, such
as current_thread_info().

But having just looked, multiple calls to current_thread_info() are
eliminated. GCC inspects the arguments of the inline asm which is the
body of this function, and concludes that the asm itself is to be
optimised like a const function, depending only on its input operands.

In fact to get GCC to _not_ optimise away inline asms, or even to
behave like "pure" (allowing memory to be read) instead of "const",
they must be declared volatile, or have no outputs. Putting "memory"
in the clobber list says only that the asm clobbers memory, not that
it reads memory.

It would be nice to have a way to declare an asm like "pure" not
"const", so that it's allowed to read memory but multiple calls can be
eliminated; I don't know of a way to express that.

-- Jamie