public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Do not spill variables/registers on the stack
@ 2011-02-02 17:14 Stefan Schulze Frielinghaus
  2011-02-02 17:34 ` Philip Herron
  2011-02-02 22:05 ` Ian Lance Taylor
  0 siblings, 2 replies; 30+ messages in thread
From: Stefan Schulze Frielinghaus @ 2011-02-02 17:14 UTC (permalink / raw)
  To: gcc-help

Hello all,

I would like to compile my code with optimization level zero (-O0) and
enable only one optimization which keeps most variables in registers if
possible, i.e. does not spill them on the stack.

At the moment I circumvent this problem by always stating that a
variable should be in a register if possible:

int main(void) {
	register int a = 1;
	register int b = -1;

	return a+b;
}

Compiling it with -O0 results in a binary where the variables a and b
are in registers and are not spilled on the stack. If I remove the
keyword "register", then the variables are spilled on the stack.
I had a look at the man page but couldn't find a suitable optimization
flag which prohibits this.

Does someone has an idea?

Kind regards,
Stefan

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 17:14 Do not spill variables/registers on the stack Stefan Schulze Frielinghaus
@ 2011-02-02 17:34 ` Philip Herron
  2011-02-02 17:55   ` Stefan Schulze Frielinghaus
  2011-02-02 18:09   ` Jeff Law
  2011-02-02 22:05 ` Ian Lance Taylor
  1 sibling, 2 replies; 30+ messages in thread
From: Philip Herron @ 2011-02-02 17:34 UTC (permalink / raw)
  To: Stefan Schulze Frielinghaus; +Cc: gcc-help

On 2 February 2011 17:14, Stefan Schulze Frielinghaus
<stefan@seekline.net> wrote:
> I would like to compile my code with optimization level zero (-O0) and
> enable only one optimization which keeps most variables in registers if
> possible, i.e. does not spill them on the stack.
>
> At the moment I circumvent this problem by always stating that a
> variable should be in a register if possible:
>
> int main(void) {
>        register int a = 1;
>        register int b = -1;
>
>        return a+b;
> }
>
> Compiling it with -O0 results in a binary where the variables a and b
> are in registers and are not spilled on the stack. If I remove the
> keyword "register", then the variables are spilled on the stack.
> I had a look at the man page but couldn't find a suitable optimization
> flag which prohibits this.
>

I am not quite sure what you are asking/trying to do but the register
keyword is used to tell the compiler keep this variable inside any
available registers which is useful if your going to use it alot in a
piece of code i would imagine it could speed up things alot.

If you take the keyword out the compiler doesnt assume anything and
goes for the safer option and put stuff in the stack which is just the
safe option of the compiler and since your compiling with -O0 i doubt
any optimizations are being passed and i dont know of any super
specific optimisation that would do what you want.

But have you tried comparing the outputs of what -O0 outputs to say -O2 ?

--Phil

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 17:34 ` Philip Herron
@ 2011-02-02 17:55   ` Stefan Schulze Frielinghaus
  2011-02-02 18:06     ` Drasko DRASKOVIC
  2011-02-02 18:09   ` Jeff Law
  1 sibling, 1 reply; 30+ messages in thread
From: Stefan Schulze Frielinghaus @ 2011-02-02 17:55 UTC (permalink / raw)
  To: Philip Herron; +Cc: gcc-help

On Mi, 2011-02-02 at 17:34 +0000, Philip Herron wrote:
> I am not quite sure what you are asking/trying to do

What I'm trying to achieve is to compile some piece of code without
"any" optimization but keep the variables in registers, i.e. do not
spill them on the stack. Optimization level 1 does that, i.e. it keeps
variables in registers and only spills them if there are to many, but it
also enables a lot of other optimization techniques which I do not want.

I don't want to run the application in the end. The binary file will be
analyzed afterwards by other applications and the CFG will be
reconstructed. Therefore, the binary is not used in real world
scenarios, it is more of a "scientific interest".

[...]
> But have you tried comparing the outputs of what -O0 outputs to say -O2 ?

The problem with using anything else then -O0 is that it enables other
optimization techniques, e.g. constant propagation (the example of the
first mail would be scaled down to a simple "return 0;") which I do not
want. Therefore, I would like to compile my code without any
optimizations except register allocation.

I hope it got more clear. Otherwise just let me know.

Any other ideas?

Kind regards,
Stefan

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 17:55   ` Stefan Schulze Frielinghaus
@ 2011-02-02 18:06     ` Drasko DRASKOVIC
  2011-02-02 18:39       ` Stefan Schulze Frielinghaus
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-02 18:06 UTC (permalink / raw)
  To: Stefan Schulze Frielinghaus; +Cc: Philip Herron, gcc-help

On Wed, Feb 2, 2011 at 6:55 PM, Stefan Schulze Frielinghaus
<stefan@seekline.net> wrote:
>> But have you tried comparing the outputs of what -O0 outputs to say -O2 ?
>
> The problem with using anything else then -O0 is that it enables other
> optimization techniques, e.g. constant propagation (the example of the
> first mail would be scaled down to a simple "return 0;") which I do not
> want. Therefore, I would like to compile my code without any
> optimizations except register allocation.

Hi Stefan,
keep in mind that "register" keyword is only a **hint** given to
compiler to do register optimization. Compiler is not obliged to
listen to your hints, and it probably does not do so without
optimization turned on (I am not sure if it can be forced).

BTW, you should also keep in mind that ANSI C does not allow for
taking the address of a register object; this restriction does not
apply to C++. However, if the address-of operator (&) is used on an
object, the compiler must put the object in a location for which an
address can be represented. In practice, this means in memory instead
of in a register. Because of this restriction GCC will ignore the
register keyword on variables whos address is taken at any point in
the program. So, ensure that somewhere in your code you are not using
the addresses operator on these variables.

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 17:34 ` Philip Herron
  2011-02-02 17:55   ` Stefan Schulze Frielinghaus
@ 2011-02-02 18:09   ` Jeff Law
  2011-02-02 18:14     ` Drasko DRASKOVIC
  2011-02-02 18:36     ` Stefan Schulze Frielinghaus
  1 sibling, 2 replies; 30+ messages in thread
From: Jeff Law @ 2011-02-02 18:09 UTC (permalink / raw)
  To: Philip Herron; +Cc: Stefan Schulze Frielinghaus, gcc-help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/02/11 10:34, Philip Herron wrote:

> 
> I am not quite sure what you are asking/trying to do but the register
> keyword is used to tell the compiler keep this variable inside any
> available registers which is useful if your going to use it alot in a
> piece of code i would imagine it could speed up things alot.
GCC ignores the "register" keyword.

Jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJNSZ2pAAoJEBRtltQi2kC7C28H/jOxlMkn/whi5ALZ22CjRH7p
RK7lah+AseEdx6Fu4ZzKvn2kY6EnpP0OoyTmDl/4W6oBFQmQzGhbVRfRIWg3H+YB
JesHATyWXrq+Sf3SUf5dNqZ1lTIWE0fvFvFPPz28q03g7HvDPujFhM9Mi17DRe6W
g775i7jzZlDCUcUMz5BHWGUmUVGM7O/x/eU64XarGih26DJa/slUSYK2Wmh4j4YO
2R32AX7NJxGZ4Iup01OzZ5H3v1He14YqHA0g/xQe+aPQjEv2uaT1GLAjjY6jZZ1o
YceTXMmcDN0opprz/yEtcIR6+i/vLIiIPmTKp73DKgEwA0bOOHoyNkeNIzIpi1w=
=ihh3
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 18:09   ` Jeff Law
@ 2011-02-02 18:14     ` Drasko DRASKOVIC
  2011-02-02 18:36     ` Stefan Schulze Frielinghaus
  1 sibling, 0 replies; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-02 18:14 UTC (permalink / raw)
  To: Jeff Law; +Cc: Philip Herron, Stefan Schulze Frielinghaus, gcc-help

On Wed, Feb 2, 2011 at 7:08 PM, Jeff Law <law@redhat.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 02/02/11 10:34, Philip Herron wrote:
>
>>
>> I am not quite sure what you are asking/trying to do but the register
>> keyword is used to tell the compiler keep this variable inside any
>> available registers which is useful if your going to use it alot in a
>> piece of code i would imagine it could speed up things alot.
> GCC ignores the "register" keyword.

1) Why would it ignore it ?
2) When does it ignore it ? When -O0 ? In some other cases ?
3) Is there some way to force "register" keyword not to be ignored with -O0 ?

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 18:09   ` Jeff Law
  2011-02-02 18:14     ` Drasko DRASKOVIC
@ 2011-02-02 18:36     ` Stefan Schulze Frielinghaus
  2011-02-02 20:32       ` Drasko DRASKOVIC
  1 sibling, 1 reply; 30+ messages in thread
From: Stefan Schulze Frielinghaus @ 2011-02-02 18:36 UTC (permalink / raw)
  To: Jeff Law; +Cc: Philip Herron, gcc-help

On Mi, 2011-02-02 at 11:08 -0700, Jeff Law wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 02/02/11 10:34, Philip Herron wrote:
> 
> > 
> > I am not quite sure what you are asking/trying to do but the register
> > keyword is used to tell the compiler keep this variable inside any
> > available registers which is useful if your going to use it alot in a
> > piece of code i would imagine it could speed up things alot.
> GCC ignores the "register" keyword.

GCC does _not_ ignore the "register" keyword. The following code was
compiled with GCC version 4.4.5:

int main(void) {
        register int a = 1;
        register int b = -1;

        return a+b;
}

$ gcc -O0 -c test.c
$ objdump -d test.o

...
  20:	3b a0 00 01 	li      r29,1
  24:	3b c0 ff ff 	li      r30,-1
  28:	7c 1d f2 14 	add     r0,r29,r30
  2c:	7c 03 03 78 	mr      r3,r0

and if we remove the register keyword:

  18:	38 00 00 01 	li      r0,1
  1c:	90 1f 00 0c 	stw     r0,12(r31)
  20:	38 00 ff ff 	li      r0,-1
  24:	90 1f 00 08 	stw     r0,8(r31)
  28:	81 3f 00 0c 	lwz     r9,12(r31)
  2c:	80 1f 00 08 	lwz     r0,8(r31)
  30:	7c 09 02 14 	add     r0,r9,r0
  34:	7c 03 03 78 	mr      r3,r0

Kind regards,
Stefan

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 18:06     ` Drasko DRASKOVIC
@ 2011-02-02 18:39       ` Stefan Schulze Frielinghaus
  2011-02-02 20:01         ` kevin diggs
  0 siblings, 1 reply; 30+ messages in thread
From: Stefan Schulze Frielinghaus @ 2011-02-02 18:39 UTC (permalink / raw)
  To: Drasko DRASKOVIC; +Cc: Philip Herron, gcc-help

On Mi, 2011-02-02 at 19:05 +0100, Drasko DRASKOVIC wrote:
> On Wed, Feb 2, 2011 at 6:55 PM, Stefan Schulze Frielinghaus
> <stefan@seekline.net> wrote:
> >> But have you tried comparing the outputs of what -O0 outputs to say -O2 ?
> >
> > The problem with using anything else then -O0 is that it enables other
> > optimization techniques, e.g. constant propagation (the example of the
> > first mail would be scaled down to a simple "return 0;") which I do not
> > want. Therefore, I would like to compile my code without any
> > optimizations except register allocation.
> 
> Hi Stefan,
> keep in mind that "register" keyword is only a **hint** given to
> compiler to do register optimization. Compiler is not obliged to
> listen to your hints, and it probably does not do so without
> optimization turned on (I am not sure if it can be forced).

Yes that's true. I only used it in my examples to demonstrate what
result I actually wanted.

> BTW, you should also keep in mind that ANSI C does not allow for
> taking the address of a register object; this restriction does not
> apply to C++. However, if the address-of operator (&) is used on an
> object, the compiler must put the object in a location for which an
> address can be represented. In practice, this means in memory instead
> of in a register. Because of this restriction GCC will ignore the
> register keyword on variables whos address is taken at any point in
> the program. So, ensure that somewhere in your code you are not using
> the addresses operator on these variables.

This wouldn't be a problem for me because in ANSI C, as you already
mentioned, the address-of operator is not allowed for variables which
reside in a register, ergo the variable cannot stay in a register and
needs to be spilled on the stack. That is perfectly fine for me. I'm
more interested in all other variables.

Kind regards,
Stefan

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 18:39       ` Stefan Schulze Frielinghaus
@ 2011-02-02 20:01         ` kevin diggs
  2011-02-02 20:27           ` Aubin LaBrosse
  0 siblings, 1 reply; 30+ messages in thread
From: kevin diggs @ 2011-02-02 20:01 UTC (permalink / raw)
  To: Stefan Schulze Frielinghaus; +Cc: Drasko DRASKOVIC, Philip Herron, gcc-help

Hi,

What if you try looking at the output of '-S -fverbose-asm'? Doesn't
that include all of the -f<thing> optimizery flags that are actually
passed to cc1? Maybe one of those will suggest something helpful?

Do the -O<x> flags only select sets of -f<thing>(s)? Or do they also
enable some things on their own? In other words if I did -O1 and then
did -fno-<thing> for everything that -O1 turned on would I effectively
disable -O1?

kevin

On Wed, Feb 2, 2011 at 12:39 PM, Stefan Schulze Frielinghaus
<stefan@seekline.net> wrote:
> On Mi, 2011-02-02 at 19:05 +0100, Drasko DRASKOVIC wrote:
>> On Wed, Feb 2, 2011 at 6:55 PM, Stefan Schulze Frielinghaus
>> <stefan@seekline.net> wrote:
>> >> But have you tried comparing the outputs of what -O0 outputs to say -O2 ?
>> >
>> > The problem with using anything else then -O0 is that it enables other
>> > optimization techniques, e.g. constant propagation (the example of the
>> > first mail would be scaled down to a simple "return 0;") which I do not
>> > want. Therefore, I would like to compile my code without any
>> > optimizations except register allocation.
>>
>> Hi Stefan,
>> keep in mind that "register" keyword is only a **hint** given to
>> compiler to do register optimization. Compiler is not obliged to
>> listen to your hints, and it probably does not do so without
>> optimization turned on (I am not sure if it can be forced).
>
> Yes that's true. I only used it in my examples to demonstrate what
> result I actually wanted.
>
>> BTW, you should also keep in mind that ANSI C does not allow for
>> taking the address of a register object; this restriction does not
>> apply to C++. However, if the address-of operator (&) is used on an
>> object, the compiler must put the object in a location for which an
>> address can be represented. In practice, this means in memory instead
>> of in a register. Because of this restriction GCC will ignore the
>> register keyword on variables whos address is taken at any point in
>> the program. So, ensure that somewhere in your code you are not using
>> the addresses operator on these variables.
>
> This wouldn't be a problem for me because in ANSI C, as you already
> mentioned, the address-of operator is not allowed for variables which
> reside in a register, ergo the variable cannot stay in a register and
> needs to be spilled on the stack. That is perfectly fine for me. I'm
> more interested in all other variables.
>
> Kind regards,
> Stefan
>
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 20:01         ` kevin diggs
@ 2011-02-02 20:27           ` Aubin LaBrosse
  0 siblings, 0 replies; 30+ messages in thread
From: Aubin LaBrosse @ 2011-02-02 20:27 UTC (permalink / raw)
  To: kevin diggs
  Cc: Stefan Schulze Frielinghaus, Drasko DRASKOVIC, Philip Herron, gcc-help

No. This has been mentioned often on this list: -O<x> is more than just combinations of -f<thing>(s). Details will need to come from someone more knowledgeable (or search the archives), but I know this much is true. 

-a

Sent from my iPhone

On Feb 2, 2011, at 12:01 PM, kevin diggs <diggskevin38@gmail.com> wrote:

> Hi,
> 
> What if you try looking at the output of '-S -fverbose-asm'? Doesn't
> that include all of the -f<thing> optimizery flags that are actually
> passed to cc1? Maybe one of those will suggest something helpful?
> 
> Do the -O<x> flags only select sets of -f<thing>(s)? Or do they also
> enable some things on their own? In other words if I did -O1 and then
> did -fno-<thing> for everything that -O1 turned on would I effectively
> disable -O1?
> 
> kevin
> 
> On Wed, Feb 2, 2011 at 12:39 PM, Stefan Schulze Frielinghaus
> <stefan@seekline.net> wrote:
>> On Mi, 2011-02-02 at 19:05 +0100, Drasko DRASKOVIC wrote:
>>> On Wed, Feb 2, 2011 at 6:55 PM, Stefan Schulze Frielinghaus
>>> <stefan@seekline.net> wrote:
>>>>> But have you tried comparing the outputs of what -O0 outputs to say -O2 ?
>>>> 
>>>> The problem with using anything else then -O0 is that it enables other
>>>> optimization techniques, e.g. constant propagation (the example of the
>>>> first mail would be scaled down to a simple "return 0;") which I do not
>>>> want. Therefore, I would like to compile my code without any
>>>> optimizations except register allocation.
>>> 
>>> Hi Stefan,
>>> keep in mind that "register" keyword is only a **hint** given to
>>> compiler to do register optimization. Compiler is not obliged to
>>> listen to your hints, and it probably does not do so without
>>> optimization turned on (I am not sure if it can be forced).
>> 
>> Yes that's true. I only used it in my examples to demonstrate what
>> result I actually wanted.
>> 
>>> BTW, you should also keep in mind that ANSI C does not allow for
>>> taking the address of a register object; this restriction does not
>>> apply to C++. However, if the address-of operator (&) is used on an
>>> object, the compiler must put the object in a location for which an
>>> address can be represented. In practice, this means in memory instead
>>> of in a register. Because of this restriction GCC will ignore the
>>> register keyword on variables whos address is taken at any point in
>>> the program. So, ensure that somewhere in your code you are not using
>>> the addresses operator on these variables.
>> 
>> This wouldn't be a problem for me because in ANSI C, as you already
>> mentioned, the address-of operator is not allowed for variables which
>> reside in a register, ergo the variable cannot stay in a register and
>> needs to be spilled on the stack. That is perfectly fine for me. I'm
>> more interested in all other variables.
>> 
>> Kind regards,
>> Stefan
>> 
>> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 18:36     ` Stefan Schulze Frielinghaus
@ 2011-02-02 20:32       ` Drasko DRASKOVIC
  2011-02-02 20:50         ` Stefan Schulze Frielinghaus
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-02 20:32 UTC (permalink / raw)
  To: Stefan Schulze Frielinghaus; +Cc: Jeff Law, Philip Herron, gcc-help

On Wed, Feb 2, 2011 at 7:35 PM, Stefan Schulze Frielinghaus
<stefan@seekline.net> wrote:
> On Mi, 2011-02-02 at 11:08 -0700, Jeff Law wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> On 02/02/11 10:34, Philip Herron wrote:
>>
>> >
>> > I am not quite sure what you are asking/trying to do but the register
>> > keyword is used to tell the compiler keep this variable inside any
>> > available registers which is useful if your going to use it alot in a
>> > piece of code i would imagine it could speed up things alot.
>> GCC ignores the "register" keyword.
>
> GCC does _not_ ignore the "register" keyword.

Are you sure ? I mean, are you sure what Jeff was suggesting with his
short and not elaborated answer ?
I mean, what if he wanted to say : " GCC ignores the 'register'
keyword if no-optimization (-O0) flag was passed." ?

That's why I asked for clarification, for in that case he might be right.

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 20:32       ` Drasko DRASKOVIC
@ 2011-02-02 20:50         ` Stefan Schulze Frielinghaus
  2011-02-03  9:44           ` David Brown
  0 siblings, 1 reply; 30+ messages in thread
From: Stefan Schulze Frielinghaus @ 2011-02-02 20:50 UTC (permalink / raw)
  To: Drasko DRASKOVIC; +Cc: Jeff Law, Philip Herron, gcc-help

On Mi, 2011-02-02 at 21:32 +0100, Drasko DRASKOVIC wrote:
> On Wed, Feb 2, 2011 at 7:35 PM, Stefan Schulze Frielinghaus
> <stefan@seekline.net> wrote:
> > On Mi, 2011-02-02 at 11:08 -0700, Jeff Law wrote:
> >> -----BEGIN PGP SIGNED MESSAGE-----
> >> Hash: SHA1
> >>
> >> On 02/02/11 10:34, Philip Herron wrote:
> >>
> >> >
> >> > I am not quite sure what you are asking/trying to do but the register
> >> > keyword is used to tell the compiler keep this variable inside any
> >> > available registers which is useful if your going to use it alot in a
> >> > piece of code i would imagine it could speed up things alot.
> >> GCC ignores the "register" keyword.
> >
> > GCC does _not_ ignore the "register" keyword.
> 
> Are you sure ? I mean, are you sure what Jeff was suggesting with his
> short and not elaborated answer ?
> I mean, what if he wanted to say : " GCC ignores the 'register'
> keyword if no-optimization (-O0) flag was passed." ?
> 
> That's why I asked for clarification, for in that case he might be right.

I took his words as a "for all" statement and proved by a counter
example that at least in one case it is not true ;-)

No seriously, just wait for his respond. There might be situations where
the keyword is ignored. I can only guess ...

Kind regards,
Stefan

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 17:14 Do not spill variables/registers on the stack Stefan Schulze Frielinghaus
  2011-02-02 17:34 ` Philip Herron
@ 2011-02-02 22:05 ` Ian Lance Taylor
  2011-02-02 22:10   ` Drasko DRASKOVIC
  1 sibling, 1 reply; 30+ messages in thread
From: Ian Lance Taylor @ 2011-02-02 22:05 UTC (permalink / raw)
  To: Stefan Schulze Frielinghaus; +Cc: gcc-help

Stefan Schulze Frielinghaus <stefan@seekline.net> writes:

> I would like to compile my code with optimization level zero (-O0) and
> enable only one optimization which keeps most variables in registers if
> possible, i.e. does not spill them on the stack.
>
> At the moment I circumvent this problem by always stating that a
> variable should be in a register if possible:
>
> int main(void) {
> 	register int a = 1;
> 	register int b = -1;
>
> 	return a+b;
> }
>
> Compiling it with -O0 results in a binary where the variables a and b
> are in registers and are not spilled on the stack. If I remove the
> keyword "register", then the variables are spilled on the stack.
> I had a look at the man page but couldn't find a suitable optimization
> flag which prohibits this.
>
> Does someone has an idea?

What you want is to disable all optimizations other than register
allocation.  That does not sound like a particularly useful feature for
gcc to provide, and in fact it does not provide it.  You could do it by
changing gcc's source code.

Ian

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 22:05 ` Ian Lance Taylor
@ 2011-02-02 22:10   ` Drasko DRASKOVIC
  2011-02-02 22:27     ` Ian Lance Taylor
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-02 22:10 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Stefan Schulze Frielinghaus, gcc-help

On Wed, Feb 2, 2011 at 11:04 PM, Ian Lance Taylor <iant@google.com> wrote:
> What you want is to disable all optimizations other than register
> allocation.  That does not sound like a particularly useful feature for
> gcc to provide, and in fact it does not provide it.  You could do it by
> changing gcc's source code.

Ian,
would that actually say that with -O0 you can say goodbye to
"register" keywords (gcc will start ignoring them for -O0, while for
other -Ox it will **not** ignore them) ?

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 22:10   ` Drasko DRASKOVIC
@ 2011-02-02 22:27     ` Ian Lance Taylor
  2011-02-02 22:56       ` Drasko DRASKOVIC
  0 siblings, 1 reply; 30+ messages in thread
From: Ian Lance Taylor @ 2011-02-02 22:27 UTC (permalink / raw)
  To: Drasko DRASKOVIC; +Cc: Stefan Schulze Frielinghaus, gcc-help

Drasko DRASKOVIC <drasko.draskovic@gmail.com> writes:

> On Wed, Feb 2, 2011 at 11:04 PM, Ian Lance Taylor <iant@google.com> wrote:
>> What you want is to disable all optimizations other than register
>> allocation.  That does not sound like a particularly useful feature for
>> gcc to provide, and in fact it does not provide it.  You could do it by
>> changing gcc's source code.
>
> would that actually say that with -O0 you can say goodbye to
> "register" keywords (gcc will start ignoring them for -O0, while for
> other -Ox it will **not** ignore them) ?

My understanding was that the OP did not want to put register keywords
everywhere.

As far as I know gcc does ignore the register keyword at optimization
levels about -O0, except for register asm and forbidding taking the
address of a variable marked as register.

Ian

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 22:27     ` Ian Lance Taylor
@ 2011-02-02 22:56       ` Drasko DRASKOVIC
  0 siblings, 0 replies; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-02 22:56 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Stefan Schulze Frielinghaus, gcc-help

On Wed, Feb 2, 2011 at 11:27 PM, Ian Lance Taylor <iant@google.com> wrote:
> As far as I know gcc does ignore the register keyword at optimization
> levels about -O0
OK, thanks. That completes Jeffs statement from before, I guess...

> except for register asm
Stefan, I guess embedded asm would be your only solution...

>  and forbidding taking the
> address of a variable marked as register.
This one should be forbidden at all optimozetion level, as C standard
forbids it (C++ does not, though).


BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-02 20:50         ` Stefan Schulze Frielinghaus
@ 2011-02-03  9:44           ` David Brown
  2011-02-03  9:56             ` Drasko DRASKOVIC
  0 siblings, 1 reply; 30+ messages in thread
From: David Brown @ 2011-02-03  9:44 UTC (permalink / raw)
  To: gcc-help

On 02/02/2011 21:49, Stefan Schulze Frielinghaus wrote:
> On Mi, 2011-02-02 at 21:32 +0100, Drasko DRASKOVIC wrote:
>> On Wed, Feb 2, 2011 at 7:35 PM, Stefan Schulze Frielinghaus
>> <stefan@seekline.net>  wrote:
>>> On Mi, 2011-02-02 at 11:08 -0700, Jeff Law wrote:
>>>> -----BEGIN PGP SIGNED MESSAGE-----
>>>> Hash: SHA1
>>>>
>>>> On 02/02/11 10:34, Philip Herron wrote:
>>>>
>>>>>
>>>>> I am not quite sure what you are asking/trying to do but the register
>>>>> keyword is used to tell the compiler keep this variable inside any
>>>>> available registers which is useful if your going to use it alot in a
>>>>> piece of code i would imagine it could speed up things alot.
>>>> GCC ignores the "register" keyword.
>>>
>>> GCC does _not_ ignore the "register" keyword.
>>
>> Are you sure ? I mean, are you sure what Jeff was suggesting with his
>> short and not elaborated answer ?
>> I mean, what if he wanted to say : " GCC ignores the 'register'
>> keyword if no-optimization (-O0) flag was passed." ?
>>
>> That's why I asked for clarification, for in that case he might be right.
>
> I took his words as a "for all" statement and proved by a counter
> example that at least in one case it is not true ;-)
>
> No seriously, just wait for his respond. There might be situations where
> the keyword is ignored. I can only guess ...
>

My understanding is that "register" is ignored at -O1 and above, which 
would seem to match your experience.


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03  9:44           ` David Brown
@ 2011-02-03  9:56             ` Drasko DRASKOVIC
  2011-02-03 12:00               ` David Brown
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-03  9:56 UTC (permalink / raw)
  To: David Brown; +Cc: gcc-help

On Thu, Feb 3, 2011 at 10:42 AM, David Brown <david@westcontrol.com> wrote:
> My understanding is that "register" is ignored at -O1 and above, which would
> seem to match your experience.

You wanted to say "-O1 and below", right ?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03  9:56             ` Drasko DRASKOVIC
@ 2011-02-03 12:00               ` David Brown
  2011-02-03 14:15                 ` Drasko DRASKOVIC
  2011-02-03 18:22                 ` Bob Plantz
  0 siblings, 2 replies; 30+ messages in thread
From: David Brown @ 2011-02-03 12:00 UTC (permalink / raw)
  To: gcc-help

On 03/02/2011 10:56, Drasko DRASKOVIC wrote:
> On Thu, Feb 3, 2011 at 10:42 AM, David Brown<david@westcontrol.com>  wrote:
>> My understanding is that "register" is ignored at -O1 and above, which would
>> seem to match your experience.
>
> You wanted to say "-O1 and below", right ?
>

No, "register" is ignored at -O1 and above.  When the optimiser is 
enabled, /it/ decides whether data should go in a register, or has to go 
on the stack, or can be eliminated entirely.  It is only at -O0 that the 
compiler puts everything on the stack by default, and puts "register" 
data in registers.  That's why the "register" keyword has been 
considered redundant for many years - the compiler almost always does a 
much better job than any human at deciding what data to put in registers.



^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03 12:00               ` David Brown
@ 2011-02-03 14:15                 ` Drasko DRASKOVIC
  2011-02-03 18:22                 ` Bob Plantz
  1 sibling, 0 replies; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-03 14:15 UTC (permalink / raw)
  To: David Brown; +Cc: gcc-help

On Thu, Feb 3, 2011 at 12:58 PM, David Brown <david@westcontrol.com> wrote:
> On 03/02/2011 10:56, Drasko DRASKOVIC wrote:
>>
>> On Thu, Feb 3, 2011 at 10:42 AM, David Brown<david@westcontrol.com>
>>  wrote:
>>>
>>> My understanding is that "register" is ignored at -O1 and above, which
>>> would
>>> seem to match your experience.
>>
>> You wanted to say "-O1 and below", right ?
>>
>
> No, "register" is ignored at -O1 and above.  When the optimiser is enabled,
> /it/ decides whether data should go in a register, or has to go on the
> stack, or can be eliminated entirely.  It is only at -O0 that the compiler
> puts everything on the stack by default, and puts "register" data in
> registers.  That's why the "register" keyword has been considered redundant
> for many years - the compiler almost always does a much better job than any
> human at deciding what data to put in registers.

OK, thanks.

This would say that Stefan has no problem at all, because he wanted to
compile his program without optimization (thus -O0) and put "register"
variables in registers (which will be done, because compiler will not
ignore "register keyword at this level, as the code he pasted before
proves).

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03 12:00               ` David Brown
  2011-02-03 14:15                 ` Drasko DRASKOVIC
@ 2011-02-03 18:22                 ` Bob Plantz
  2011-02-03 20:47                   ` kevin diggs
  1 sibling, 1 reply; 30+ messages in thread
From: Bob Plantz @ 2011-02-03 18:22 UTC (permalink / raw)
  To: David Brown; +Cc: gcc-help

On 2/3/2011 3:58 AM, David Brown wrote:
> On 03/02/2011 10:56, Drasko DRASKOVIC wrote:
>> On Thu, Feb 3, 2011 at 10:42 AM, David Brown<david@westcontrol.com>  
>> wrote:
>>> My understanding is that "register" is ignored at -O1 and above, 
>>> which would
>>> seem to match your experience.
>>
>> You wanted to say "-O1 and below", right ?
>>
>
> No, "register" is ignored at -O1 and above.  When the optimiser is 
> enabled, /it/ decides whether data should go in a register, or has to 
> go on the stack, or can be eliminated entirely.  It is only at -O0 
> that the compiler puts everything on the stack by default, and puts 
> "register" data in registers.  That's why the "register" keyword has 
> been considered redundant for many years - the compiler almost always 
> does a much better job than any human at deciding what data to put in 
> registers.
>
Let us not forget that a human wrote the compiler. Ultimately, it is a human who makes these decisions. At least, I hope so. :-)

A little off topic here, but it bugs me when "customer service" tells me there was a "computer error." No, there was a programming error, which was made by a human.

--Bob


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03 18:22                 ` Bob Plantz
@ 2011-02-03 20:47                   ` kevin diggs
  2011-02-03 22:36                     ` Drasko DRASKOVIC
  0 siblings, 1 reply; 30+ messages in thread
From: kevin diggs @ 2011-02-03 20:47 UTC (permalink / raw)
  To: Bob Plantz; +Cc: David Brown, gcc-help

Hi,

Even more off topic but in a similar vein is when the sports anchors
talk about 'how the computers' are deciding who plays in the ncaa
football bowl games.

kevin

On Thu, Feb 3, 2011 at 10:20 AM, Bob Plantz <plantz@cds1.net> wrote:
>
> A little off topic here, but it bugs me when "customer service" tells me
> there was a "computer error." No, there was a programming error, which was
> made by a human.
>
> --Bob
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03 20:47                   ` kevin diggs
@ 2011-02-03 22:36                     ` Drasko DRASKOVIC
  2011-02-04 11:45                       ` David Brown
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-03 22:36 UTC (permalink / raw)
  To: kevin diggs; +Cc: Bob Plantz, David Brown, gcc-help

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.


BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-03 22:36                     ` Drasko DRASKOVIC
@ 2011-02-04 11:45                       ` David Brown
  2011-02-04 12:34                         ` Drasko DRASKOVIC
  0 siblings, 1 reply; 30+ messages in thread
From: David Brown @ 2011-02-04 11:45 UTC (permalink / raw)
  To: gcc-help

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.


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 11:45                       ` David Brown
@ 2011-02-04 12:34                         ` Drasko DRASKOVIC
  2011-02-04 15:11                           ` David Brown
  0 siblings, 1 reply; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-04 12:34 UTC (permalink / raw)
  To: David Brown; +Cc: gcc-help

On Fri, Feb 4, 2011 at 12:29 PM, David Brown <david@westcontrol.com> 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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 12:34                         ` Drasko DRASKOVIC
@ 2011-02-04 15:11                           ` David Brown
  2011-02-04 15:50                             ` Drasko DRASKOVIC
  2011-02-04 19:55                             ` Enrico Weigelt
  0 siblings, 2 replies; 30+ messages in thread
From: David Brown @ 2011-02-04 15:11 UTC (permalink / raw)
  To: gcc-help

On 04/02/2011 13:19, Drasko DRASKOVIC wrote:
> On Fri, Feb 4, 2011 at 12:29 PM, David Brown<david@westcontrol.com>
> 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).
>

No, it's a hint - but one that gcc will take seriously and do its best 
to honour (at -O0).  The compiler cannot guarantee that it will be able 
to put the data into a register - there are many things that could keep 
it out of the register, including register pressure and the way the data 
is used.  The text says that with -O0 gcc will put all non-register 
variables on the stack - that does /not/ imply that it will put all 
register variables in registers.

> 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 ?
>

No, it is ignored for -O1 and above, except in odd combinations of 
particular x86 targets when using setjmp - as detailed on that page. 
The page says "The register specifier affects code generation /only/ in 
these ways..." - if these don't apply, the hint is ignored.

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

Variables allocated to a specific named register are a different matter 
entirely - that is a gcc extension that is related to its inline 
assembler, and is not related to the "register" storage specifier.  It's 
an example of C's bad habit of overloading keywords - albeit a fairly 
natural one.

> 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.
>

That's correct - you can put data into specified dedicated named 
registers like this, if you are /very/ careful.  But it is unrelated to 
the register storage specifier, and unaffected by any optimisation levels.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 15:11                           ` David Brown
@ 2011-02-04 15:50                             ` Drasko DRASKOVIC
  2011-02-04 19:55                             ` Enrico Weigelt
  1 sibling, 0 replies; 30+ messages in thread
From: Drasko DRASKOVIC @ 2011-02-04 15:50 UTC (permalink / raw)
  To: David Brown; +Cc: gcc-help

David,
thanks a lot for these expert clarifications.

BR,
Drasko

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 15:11                           ` David Brown
  2011-02-04 15:50                             ` Drasko DRASKOVIC
@ 2011-02-04 19:55                             ` Enrico Weigelt
  2011-02-04 22:17                               ` Ian Lance Taylor
  1 sibling, 1 reply; 30+ messages in thread
From: Enrico Weigelt @ 2011-02-04 19:55 UTC (permalink / raw)
  To: gcc-help

* David Brown <david@westcontrol.com> wrote:

> No, it's a hint - but one that gcc will take seriously and do its best 
> to honour (at -O0).  The compiler cannot guarantee that it will be able 
> to put the data into a register - there are many things that could keep 
> it out of the register, including register pressure and the way the data 
> is used.  The text says that with -O0 gcc will put all non-register 
> variables on the stack - that does /not/ imply that it will put all 
> register variables in registers.

What does the ANSI C spec say about the "register" keyword ?

Does GCC issue some warning when it fails to put such a variable
directly into a register ?

Is it that keyword still useful at all on todays compilers ?


I'm not really experienced at that low level, but i can imagine 
scenarios where it's essential to keep certain things in registers
(eg. when there's no valid stack), so the compiler should issue
a warning (which can be made fatal w/ -Werror) in those cases.



cu
-- 
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt@metux.de
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 19:55                             ` Enrico Weigelt
@ 2011-02-04 22:17                               ` Ian Lance Taylor
  2011-02-07 10:48                                 ` David Brown
  0 siblings, 1 reply; 30+ messages in thread
From: Ian Lance Taylor @ 2011-02-04 22:17 UTC (permalink / raw)
  To: gcc-help

Enrico Weigelt <weigelt@metux.de> writes:

> What does the ANSI C spec say about the "register" keyword ?

6.7.1.  "A declaration of an identifier for an object with storage-class
specifier register suggests that access to the object be as fast as
possible.  The extent to which such suggestions are effective is
implementation-defined."

Then a footnote to that text says "The implementation may treat any
register declaration simply as an auto declaration.  However, whether or
not addressable storage is actually used, the address of any part of an
object declared with storage-class specifier register cannot be
computed, either explicitly (by use of the unary & operator as discussed
in 6.5.3.2) or implicitly (by converting an array name to a pointer as
discussed in 6.3.2.1).  Thus, the only operator that can be applied to
an array declared with storage-class specifier register is sizeof."


> Does GCC issue some warning when it fails to put such a variable
> directly into a register ?

No.

> Is it that keyword still useful at all on todays compilers ?

No, except for things like gcc's extension to specify the hardware
register where a variable should live.

> I'm not really experienced at that low level, but i can imagine 
> scenarios where it's essential to keep certain things in registers
> (eg. when there's no valid stack), so the compiler should issue
> a warning (which can be made fatal w/ -Werror) in those cases.

Such code can not be written in standard C.  It always requires
extensions.

Ian

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Do not spill variables/registers on the stack
  2011-02-04 22:17                               ` Ian Lance Taylor
@ 2011-02-07 10:48                                 ` David Brown
  0 siblings, 0 replies; 30+ messages in thread
From: David Brown @ 2011-02-07 10:48 UTC (permalink / raw)
  To: gcc-help

On 04/02/2011 23:13, Ian Lance Taylor wrote:
> Enrico Weigelt<weigelt@metux.de>  writes:
>
>> What does the ANSI C spec say about the "register" keyword ?
>
> 6.7.1.  "A declaration of an identifier for an object with storage-class
> specifier register suggests that access to the object be as fast as
> possible.  The extent to which such suggestions are effective is
> implementation-defined."
>
> Then a footnote to that text says "The implementation may treat any
> register declaration simply as an auto declaration.  However, whether or
> not addressable storage is actually used, the address of any part of an
> object declared with storage-class specifier register cannot be
> computed, either explicitly (by use of the unary&  operator as discussed
> in 6.5.3.2) or implicitly (by converting an array name to a pointer as
> discussed in 6.3.2.1).  Thus, the only operator that can be applied to
> an array declared with storage-class specifier register is sizeof."
>
>

Technically, that means gcc can ignore the "register" qualifier except 
to give a warning or error if you try to take the address of the data.

>> Does GCC issue some warning when it fails to put such a variable
>> directly into a register ?
>
> No.
>
>> Is it that keyword still useful at all on todays compilers ?
>
> No, except for things like gcc's extension to specify the hardware
> register where a variable should live.
>

I suppose the OP's situation where he specifically wants to use -O0 but 
have some data in registers is another case, but it's certainly quite 
unusual.

>> I'm not really experienced at that low level, but i can imagine
>> scenarios where it's essential to keep certain things in registers
>> (eg. when there's no valid stack), so the compiler should issue
>> a warning (which can be made fatal w/ -Werror) in those cases.
>
> Such code can not be written in standard C.  It always requires
> extensions.
>

Fortunately, gcc has such extensions - the "register asm" extension you 
mentioned does exactly that.

Actually, it is not necessary to use such extensions to write code that 
runs before the stack exists.  You just have to be very careful, compile 
with optimisations (so that data does go in registers), and obviously 
limit the features you use.  You also want to check the generated 
assembly carefully.

I work with embedded systems on a variety of processors, and sometimes 
need to write pre-main startup code.  I invariably write it in straight 
C with only a minimum of inline assembly.

I also once had a project for a microcontroller with no RAM at all - 
only 32 8-bit registers.  I wrote that with gcc without too many 
problems (obviously it was a small program).

The real use of the "register asm" extension is not for pre-main code, 
but for things like RTOS's where you might have some sort of "current 
task" pointer or variable in a fixed register.  It's not that common, 
however, and very expensive unless the cpu has lots of registers.  It is 
also occasionally used locally for things like system calls.


^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2011-02-07 10:29 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-02 17:14 Do not spill variables/registers on the stack Stefan Schulze Frielinghaus
2011-02-02 17:34 ` Philip Herron
2011-02-02 17:55   ` Stefan Schulze Frielinghaus
2011-02-02 18:06     ` Drasko DRASKOVIC
2011-02-02 18:39       ` Stefan Schulze Frielinghaus
2011-02-02 20:01         ` kevin diggs
2011-02-02 20:27           ` Aubin LaBrosse
2011-02-02 18:09   ` Jeff Law
2011-02-02 18:14     ` Drasko DRASKOVIC
2011-02-02 18:36     ` Stefan Schulze Frielinghaus
2011-02-02 20:32       ` Drasko DRASKOVIC
2011-02-02 20:50         ` Stefan Schulze Frielinghaus
2011-02-03  9:44           ` David Brown
2011-02-03  9:56             ` Drasko DRASKOVIC
2011-02-03 12:00               ` David Brown
2011-02-03 14:15                 ` Drasko DRASKOVIC
2011-02-03 18:22                 ` Bob Plantz
2011-02-03 20:47                   ` kevin diggs
2011-02-03 22:36                     ` Drasko DRASKOVIC
2011-02-04 11:45                       ` David Brown
2011-02-04 12:34                         ` Drasko DRASKOVIC
2011-02-04 15:11                           ` David Brown
2011-02-04 15:50                             ` Drasko DRASKOVIC
2011-02-04 19:55                             ` Enrico Weigelt
2011-02-04 22:17                               ` Ian Lance Taylor
2011-02-07 10:48                                 ` David Brown
2011-02-02 22:05 ` Ian Lance Taylor
2011-02-02 22:10   ` Drasko DRASKOVIC
2011-02-02 22:27     ` Ian Lance Taylor
2011-02-02 22:56       ` Drasko DRASKOVIC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).