<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - instcombine: doesn't remove redundant allocation / memory access with aligned_alloc"
   href="https://bugs.llvm.org/show_bug.cgi?id=44062">44062</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>instcombine: doesn't remove redundant allocation / memory access with aligned_alloc
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>opt
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>uday@polymagelabs.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the snippet below, the allocated storage is optimized away when a malloc is
used, but not when an aligned_alloc is used. (aligned_alloc has effectively
similar semantics for the purpose of relevant opts here, and it has been in
glibc since 2.16 and in the C11 standard.)  When a malloc is used, once the
load/stores are hoisted out by LICM, -instcombine gets rid of the heap
allocation and *s is effectively in a virtual register. But the same thing
doesn't happen with aligned_alloc; the allocation and the store to it remain. 

<a href="https://godbolt.org/z/WCUF9n">https://godbolt.org/z/WCUF9n</a>
------------------------------------------------
#include <stdlib.h>
#define N 100

void foo(int Y[N], int A[N][N], int X[N]) {
  for (int i = 0; i < N; ++i) {
    // Allocated storage optimized away when malloc is used.
    // int *s = (int*)malloc((sizeof(int)));
    int *s = (int *)aligned_alloc(sizeof(int), sizeof(int));
    *s = Y[i];
    for (int j = 0; j < N; ++j) {
      *s += A[i][j] * X[j];
    }
    Y[i] += *s;
    free(s);
  }
}
--------------

On this note, this issue exists with posix_memalign as well. With
posix_memalign, in fact, the load/stores to *s in the loop aren't replaced by a
scalar. Although this is an artificial C/C++ example to reproduce this issue,
the more interesting/real use case is for example when a pass/utility has to
generate an allocation for a buffer of vector element types -- as such, an
alignment to vector size boundaries is desired, which malloc doesn't provide
for > 16 bytes.</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>