<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div style="margin: 0;">Hi, Tim.</div><div style="margin: 0;"><br></div><div style="margin: 0;">I am also not quite clear about the convention of alloca. </div><div style="margin: 0;">when the size of a variable is known at compile-time, is it always that clang will put the alloca for the variable at the start of entry block?</div><div style="margin: 0;">when will clang put alloca at other block? And when will clang put alloca at entry block?</div><div style="margin: 0;">when clang put alloca at other block, does it mean that the size of the variable of alloca is not known at compile-time?</div><div style="margin: 0;">Thanks!</div><div style="margin: 0;"><br></div><div style="margin: 0;">Best Regards,</div><div style="margin: 0;">Jerry</div><p style="margin: 0;"><br></p><pre><br>At 2020-12-17 15:43:58, "Tim Northover" <t.p.northover@gmail.com> wrote:
>On Wed, 16 Dec 2020 at 13:52, ÁÖÕþ×Ú <jackie_linzz@126.com> wrote:
>> When there are dynamic allocas, is it always that the compiler will insert @llvm.stacksave() and @llvm.stackrestore()?
>
>Clang always emits them for variable length arrays as far as I know
>(e.g. "int arr[n]"), but C's actual "alloca" function
>(https://www.man7.org/linux/man-pages/man3/alloca.3.html) makes the
>memory available until the function returns so Clang can't use
>stackrestore anywhere there.
>
>> When there is a need to insert @llvm.stacksave() and @llvm.stackrestore(), how does the compiler decide where it should insert them?
>
>The stacksave should go just before the alloca you want to reclaim
>later, which is usually pretty easy to do.
>
>The stackrestore ideally goes just after the last instruction that
>uses the memory involved. That's difficult to determine though (and
>impossible to implement if it happens in another function). So in
>practice source languages have scoping rules that say when a variable
>is *allowed* to be accessed, and compilers put the stackrestore at the
>end of the scope.
>
>In C and C++ that's at the '}' of the block the variable is declared
>
>Cheers.
>
>Tim.
</pre></div>