Re: Do not spill variables/registers on the stack

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Feb 4, 2011 at 12:29 PM, David Brown <david@xxxxxxxxxxxxxxx> wrote:
> On 03/02/2011 23:36, Drasko DRASKOVIC wrote:
>>
>> Back to the topic, few things crossed my mind :
>>
>> 1) Register keyword is a "hint" to the compiler, and thus not
>> obligatory. We saw that for -O1 and above it is "useless hint", as it
>> is always ignored. Is it however always respected by GCC when -O0 ? If
>> yes - why is it a hint ? If no, in which cases it is not respected ?
>> Only when you assign more vars than you have reg, or GCC might ignore
>> it some other times even with -O0 ?
>>
>> 2) Is this behavior documented anywhere ? Can we find docs that note :
>>     a) that "regiser" keyword is not ignored only with -O0
>>     b) when -O0 in which cases it is taken into account and in which cases
>> not.
>>
>
> <http://gcc.gnu.org/onlinedocs/gcc/Hints-implementation.html>
>
>
> To quote:
>
> 4.8 Hints
>
>    * The extent to which suggestions made by using the register
> storage-class specifier are effective (C90 6.5.1, C99 6.7.1).
>
>      The register specifier affects code generation only in these ways:
>          o When used as part of the register variable extension, see
> Explicit Reg Vars.
>          o When -O0 is in use, the compiler allocates distinct stack memory
> for all variables that do not have the register storage-class specifier; if
> register is specified, the variable may have a shorter lifespan than the
> code would indicate and may never be placed in memory.
>          o On some rare x86 targets, setjmp doesn't save the registers in
> all circumstances. In those cases, GCC doesn't allocate any variables in
> registers unless they are marked register.

Hi David,
thanks for the llink.

>From what I see, for -O0 "register" does not look like a "hint" but
an obligation to the compiler (i.e. by this document it is promised
that variable will finish into the register).

Also, no mention what happens for -O1 and above - there is no promise
that variable will finish into the reg, but also no mention that it
will be ignored. Maybe in this case it is really a hint ?


What about global register variables ? Is it the same, ignored for -O1
and above ?

I am asking this because I know that for example U-Boot uses register
variable to hold some global information (pointer to global
structure), as you can see at the bottom of this file :
http://git.denx.de/?p=u-boot.git;a=blob;f=arch/arm/include/asm/global_data.h;h=2a84d27a4e15e10a86cc5842eae25e80d21981e1;hb=42d44f631c4e8e5359775bdc098f2fffde4e5c05

Then the code takes care not to clobber this reg in the .S files, and
in .c files it counts on GCC to to always allocate this reg to this
global variable, and keep it protected from clobbering during C
compilation. And U-Boot is surely compiled with highest possible
optimizations. But not only that it works, it is specified in the
official U-Boot documentation that it should always be done like this.

BR,
Drasko



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux