<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Clang generates stacksave/stackrestore incorrectly when a loop has VLA and alloca"
   href="https://llvm.org/bugs/show_bug.cgi?id=30229">30229</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang generates stacksave/stackrestore incorrectly when a loop has VLA and alloca
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bluechristlove@163.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following case:

#include <alloca.h>
#include <string.h>
#include <new>
int main(void) {
   char *cp = 0;
   for (int i = 1; i <= 100; ++i) {
      char vla[i];
      memset(vla, 0, static_cast<size_t>(i));
      if (!cp) cp = new (alloca(1)) char('Q');
   }
   return *cp == 'Q' ? 55 : 66;
}

Expected return code 55.

Clang emit LLVM IR:
we sill generate @llvm.stackstore and @llvm.stacksave. However, from GCC 6, the
VLA scope is changed when the block has alloca function:
see: <a href="https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html">https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html</a>

Space allocated with alloca exists until the containing function returns. The
space for a variable-length array is deallocated as soon as the array name's
scope ends, unless you also use alloca in this scope.

Clang should consider this condition. GCC 6 passes this case but Clang failed.

Similar bug: <a href="https://llvm.org/bugs/show_bug.cgi?format=multiple&id=16099">https://llvm.org/bugs/show_bug.cgi?format=multiple&id=16099</a></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>