`free` counterpart to `alloca`, or way to lift to function home

I'm unintentionally allocating too much space on the stack by using
`alloca` inside a loop.

To fix this I will do my `alloca` outside of the loop itself. I'm
wondering if there is a way for this to be automatically done: given
alloca a function scope, rather than loop scope.

I'm curious also, since this actually allocates each time in the loop,
is there a way to say the stack allocations are no longer required and
return to an early stack position? My `alloca` will always be in order,
in a tree, thus it'd be safe to return to an early allocation point.

As usual, Clang knows the answer:

$ cat tmp.c
void foo(int *);

void bar(int n) {
for (int i = 0; i < n; ++i) {
   int arr[i];
   foo(arr);
}
}
$ clang tmp.c -S -o- -emit-llvm -Os
[...]
define void @bar(i32) local_unnamed_addr #0 {
  [...]
  %8 = call i8* @llvm.stacksave()
  %9 = alloca i32, i64 %7, align 16
  call void @foo(i32* nonnull %9) #3
  call void @llvm.stackrestore(i8* %8)
  [...]
}

Cheers.

Tim.