<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><p style="margin: 0;"><br></p><div style="margin: 0;">Hi, Tim,</div><div style="margin: 0;"><br></div><div style="margin: 0;">Thanks. Here comes another question. Given the code below.</div><div style="margin: 0;">----------------------------------------------------------------------</div><p style="margin: 0;">void notdead(int *);</p><p style="margin: 0;">int foo(int size, char * data) {</p><p style="margin: 0;">  int a = size;</p><p style="margin: 0;">  for (int i = 0; i < size; ++i) {</p><p style="margin: 0;">     int array[size];  // dynamic alloca</p><p style="margin: 0;">     notdead(array);</p><p style="margin: 0;">     a += size;</p><p style="margin: 0;">  }</p><div style="margin: 0;">  return size;</div><div style="margin: 0;">}</div><div style="margin: 0;">-------------------------------------------------------------------------</div><div style="margin: 0;">"a" is allocated in the stack of "foo".  and in "for" loop, "array" is dynamic allocated. new space for "array" is added to</div><div style="margin: 0;">current stack. SP is ajusted. we don't know how much SP is adjusted. After that, "a" is accessed. </div><div style="margin: 0;">How would the compiler compute the address of "a"?  Would the compiler access "a" through the frame pointer?</div><div style="margin: 0;">if that's true, when will the frame pointer be set? And when is a frame pointer is needed?</div><div style="margin: 0;"><br></div><div style="margin: 0;"><br></div><div style="margin: 0;">Best Regards,</div><div style="margin: 0;">Jerry</div><div><br></div><p style="margin: 0;"><br></p><p style="margin: 0;"><br></p><div style="position:relative;zoom:1"></div><div id="divNeteaseMailCard"></div><p style="margin: 0;"><br></p><pre><br>At 2020-12-09 18:16:47, "Tim Northover" <t.p.northover@gmail.com> wrote:
>On Wed, 9 Dec 2020 at 02:43, ÁÖÕþ×Ú via llvm-dev <llvm-dev@lists.llvm.org> wrote:
>> I saw the code fragment as above. Is it possible that alloca is not static?
>> And what is the behavior when alloca is not static?
>> When alloca is not in the entry block, it is not static. How would it behave?
>
>The compiler will modify sp in the middle of the function to allocate
>more memory on the stack. If your alloca is in a loop it might even do
>that repeatedly (@llvm.stacksave and @llvm.stackrestore can help
>here).
>
>> Would it adjust the stack for the current function?
>
>The memory would become part of the current function's stack frame,
>and still get freed when it returns.
>
>Cheers.
>
>Tim.
</pre></div>