This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Using `const int' values to size stack arrays
Robert Dewar wrote:
I presume we're talking C here, and gnu C at that.
MAX_VAL is not an integral constant expression, so int ary[MAX_VAL]
must be a VLA, if it is valid.
C89 disallows both uses,
C99 disallows VLAs at file scope, but allows them at function scope.
GNU C is similar to C99.
Of course, since GNU C allows dynamic arrays, it does allow this
construct, and given that it is allowed, it is perfectly reasonable
to ask that the compiler do this efficiently and not actually treat
it as dynamic at the implementation level (even if it is dynamic at
the semantic level).
Absolutely. My port of gcc generates code which initialises a memory
variable with the value 12, and then in the body of the code which
handles the variable length array, loads that value into a register, and
does all the appropriate arithmetic on it (some example code is shown
below). Can the code not be optimised at the instruction level, so that
it would recognise that the memory value will always be 12, so the load
from memory will always be 12, and hence copy propogation could remove
large chunks of code?
The main requirement for such an optimisation seems to be whether the
optimiser can recognise that the load from memory will always be a
constant value. If the original symbol was declared as `const int', or
`static const int', and that int is written to memory, can its value
ever be different to 12? I think that it is allowed to change if the
variable was marked as volatile, but in the other cases I would have
thought it could have been optimised?
thanks,
dan
---------------------
Example code. Note that after the initial load from memory all
subsequent operations could be removed through constant propogation:
.section .data
_MAX_VAL:
.word 12 // Can this value ever change?
.section .text
_main:
COPY.0 &_MAX_VAL,R5 // Get address of MAX_VAL
LDW (R5)0,R5 // R5 now contains constant `12'
COPY.0 0,R0
STW R14,(FP)-4
SUB.0 FP,-(-8),FP
ADD.0 R5,R5,R5 // Multiply by 2
STW R3,(FP)1
ADD.0 R5,6,R5 // Add 6
COPY.1 FP,R4
AND.0 R5, -4, R5 // Mask
ADD.0 FP,R5,FP // Change FP by constant value.
etc...
============================================================================
Daniel Towner
picoChip Designs Ltd., Riverside Buildings, 108, Walcot Street, BATH,
BA1 5BG
daniel.towner@picochip.com
07786 702589