Using `const int' values to size stack arrays

Nathan Sidwell nathan@codesourcery.com
Wed Nov 17 13:51:00 GMT 2004


Daniel Towner wrote:

> 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?
sorry, I missed this point of your email.  I don't think we can arbitrarily
remove the dynamic allocation of a VLA, because I think the following
is required to work

static const int SIZE = SOME_HUGE_VALUE_THAT_CAUSES_STACK_OVERFLOW;

void Foo ()
{
   if (SOME_EXPR_THAT_IS_FALSE_AT_RUNTIME)
     {
	char ary[SIZE];
     }
}

If ary was promoted to a regular array, and therefore stack-frame
allocated at compile time, this function would fail when called, rather
than fail when the if block is entered.  (A pathelogical example)

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



> ---------------------
> 
> Example code. Note that after the initial load from memory all 
> subsequent operations could be removed through constant propogation:
what version of gcc is your port based on? Does the current development
tree do better?

I notice that MAX_VAL has been placed in .data, not .rodata -- is that
your port, or does GCC not think it constant?

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk




More information about the Gcc mailing list