Rockbox mail archive
Subject: Re: time to sleep?
From: Sven Karlsson (svenka_at_it.kth.se)
Just coming back from some quality time with the relatives.
> "register" is no use with gcc since itself will try to put the first local
> variables in registers (not always a good thing by the way...)
>To rely upon registers to
>keep a data is hazardous due to the way gcc uses them (not enough registers
>to avoid stack saving/restoring for reusing registers).
Hmm, I'm quite sure GCC always do register allocation with at least graph
coloring when optimizing. Doesn't it? I did a backend for GCC a couple of
years ago and it did AFAICR decent register allocation. The other
optimizations it did weren't that good though and sadly it looks like this
is true even today. It looks like GCC is way behind other compilers on quite
a few targets. :-(
However, most often you will see the following senario:
1. load values/variables into registers
2. do something
3. vrite results back, "invalidate" all registers
Here you will see that the "first" registers are very heavily used. This is
a consequence of the fact that the compiler cannot see which variables are
being updated through pointers. It must simply take the safe road and assume
that any variable that can be altered though variables will be. An
optimizing compiler usually asumes that any variable that have the same type
as what a pointer points. Most programmers use ints and int *s. The int *
will simply force the compiler never to allocate the int variables to
registers. At least not for long!
Using register can help out here!
Rember that "register" does _not_ mean "put this in register". It means
"this need not be in the memory" ot rather
"you cannot take the address of this". See the ANSI 9899 (C99) specification
paragrapgh 6.7.1. I quote:
"A declaration of an identifier for an object with storage-class specifier
register suggests that access to the object to be as fast as possible. The
extent to which such suggestions are effective is implementation-defined"
This section is unfortunately often misunderstood. However, there is an
"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, ether explicitly ... or implicitly ...."
It is safe to use register. You will get compiler warnings if you have too
many "register" just like "const".
register makes it possible for the optimizer to deduce that there are no
aliasing going on for a variable and that is a very good thing as the
compiler would otherwise have to spill registers to memory and later reload
them. In other words, a "register" variable can never be changed via a
pointer and can safelt be allocated to a register!
This is important since SH is a load-store architecture and you will have to
use extra load and store instructions, i.e., moves to and from memory, to
spill and reload memory each of these add 2 unnesserary memory operations
and several wasted clockcycles and the power associated to that.
Some early compilers used the register keyword to do register allocation,
i.e., they put all "register" variables in registers. This, of course, led
to disaster. The good news is, though, that I haven't seen this naughty
behaivour for several years and the compilers I use for production, i.e.,
MIPS', SUN's and Microsoft's compilers, do sensible things with the keyword.
I only use GCC on i386, where I don't really care as there are other
bottlenecks, and to some extent on PowerPC where I have so much inline
assembly that I don't really bother what the compiler do. I have run simple
tests a year ago on GCC and found that it did what I expected with it and
the code became more efficient.
I'd gladly accept any reports on experiences with the "register" keyword.
Furthermore, I do think register makes code more readable. In fact, this is
one of the many things that are broken in C and C++. Aliasing problems in
both languages are severe! I think the default for variables should be
"register" and there should be a keyword "memory" (or
whatever) that forces a variable into memory. Fortran, which do not have
pointers, often yield much more efficient optimized code than C (unless the
algorithms rely heavily on pointers). This is one of the reasons Fortran is
sadly the laguage of choice for high performance scientific computing.
Finally, register is actually much stronger than const which can be
overridden. register cannot.
Spread the word! :-) Use the register keyword. Speaking as one who has
implemented optimizing compilers I must say I really like "register".
>So I'm quite dubitative about a success to reduce power since executing
>instructions involves DRAM access anyway. In fact, load/store actions with
>data in DRAM take much more cycles, since we must both load instruction and
>load/store data in DRAM.
Judging from all the different phenomenons you are referring to, i.e., code
size, use of registers etc, it looks to me that you have had problems with
the register allocation in the optimizer . I think you would be surprised if
you tried register. I know I was when I started using it. I was coding an
end game solver for Othello and by using register whenever possible and not
changing anything else the solver got IIRC 30% faster.
Also, see above. register normally helps you remove redundant code and of
course the assiciated power consomption.
>Just a precision, you were speaking about sleep mode, it is a mistake, you
>probably speaking about standby mode. Don't use sleep mode but standby mode
>to reduce the power consumption because sleep mode shuts down all, i.e,
>peripheral controlers like timers, external interrupts would be reset,
>is probably not what you want. So you must use standby mode so timers and
Eh?!? See the SH hardware manual pp. 459-464. Sleep mode is interrupted by
interrupts, end of DMA and other things. Suspend mode can only be
interrupted with reset or NMI.
BTW sleep and suspend are quite common names for different power down
states. The processors I know of also have the same or similar meanings to
sleep and suspend. (Just think about what the words mean. ;-) )
You can use the sleep instruction to enter both modes though.
Page was last modified "Jan 10 2012" The Rockbox Crew