1] If an assembly output is optimised with -O3 [ heavy optimisation ], why is all the variables kept in the register?
2] Let say we have programmed with a bigger amount of variables, say 30 variables,
- when not optimised, its stored in the memory
- when optimised, its stored in the register
when will you know the register is full? Why is even 1 variable stored in the memory?
Keeping variables in registers ensures that program execution is as fast as possible, because no additional reads from (slower) cache/memory have to be made.
But I don't really see how you can have 30 variables in registers all time?
As far as i know standard ARM9 design has got 16 registers, and few of those are usually used and not really available.
if you have more variables then free registers, you will end up with cache reads/writes (or in worst case, memory reads/writes) which will considerable slow down program execution. That's why everyone is trying to use as few variables as possible in inner loops that get executed MANY times per sec.
Another approach is to pack 2/more variables into one register.
Like if you have 2 16bit variables, you can put them into one 32bit register (lower 16bits = first variable, higher 16bits= second variable) - now you can get either of those variables with only 1 or 2 shift operations. With ARM core shift operations are free...