[llvm-dev] why is llvm.stacksave() necessary?

David Chisnall via llvm-dev llvm-dev at lists.llvm.org
Tue Jul 25 04:27:01 PDT 2017

On 25 Jul 2017, at 12:17, alex via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> Hi David,
> good example, thank you.
> This means than, when generating VLA's, one would need to check for the
> 'minimal' part which needs to be enclosed by safe/remove (in this
> example the related calls need to enclose the inner part of the loop).

In C, the ‘minimal’ part is called the ‘scope’.  Variables are always destroyed in the inverse order to the order in which they are created and so it’s always trivial to translate each into a stack save followed by an alloca when the variable comes into scope and a stack restore when the variable goes out of scope.

> what would happen if stack safe/remove would be neglected?
> at the end of the function everything should get cleaned-up - right?;
> the only problem could be that one consumes much more stack than
> necessary - not?

Exactly.  Each alloca would allocate more memory.  gcc used to have a bug that did this, which bit me in the past (about 15 years ago), where using a VLA in the form from my example would consume stack space in proportion to the sum of all values of i, rather than in proportion to the maximum value of i.  


More information about the llvm-dev mailing list