<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 - Avoid reloads after vectorizer's runtime alias check"
   href="https://bugs.llvm.org/show_bug.cgi?id=43111">43111</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Avoid reloads after vectorizer's runtime alias check
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider loop:
int foo(int *p, int *q) {
    int s = 0;
    for (int i = 0; i < 256; ++i) {
        int k = p[i];
        q[i] = 2*k;
        s+= k;
        int o = p[i];
        s+= o;
    }
    return s;
}

Flags:  clang -fno-unroll-loops -O3 loop.c -mavx2

In entry block there is a runtime check to check whether pointers do not alias.
entry:
  %scevgep = getelementptr i32, i32* %q, i64 256
  %scevgep23 = getelementptr i32, i32* %p, i64 256
  %bound0 = icmp ugt i32* %scevgep23, %q
  %bound1 = icmp ugt i32* %scevgep, %p
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %for.body, label %vector.body


Then, in loop body, we know there is no aliasing.

vector.body:                                      ; preds = %entry,
%vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %entry ]
  %vec.phi = phi <8 x i32> [ %7, %vector.body ], [ zeroinitializer, %entry ]
  %0 = getelementptr inbounds i32, i32* %p, i64 %index
  %1 = bitcast i32* %0 to <8 x i32>*
  %wide.load = load <8 x i32>, <8 x i32>* %1, align 4, !tbaa !2, !alias.scope
!6
  %2 = shl nsw <8 x i32> %wide.load, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1,
i32 1, i32 1>
  %3 = getelementptr inbounds i32, i32* %q, i64 %index
  %4 = bitcast i32* %3 to <8 x i32>*
  store <8 x i32> %2, <8 x i32>* %4, align 4, !tbaa !2, !alias.scope !9,
!noalias !6
  %5 = add nsw <8 x i32> %wide.load, %vec.phi
  %6 = bitcast i32* %0 to <8 x i32>*
  %wide.load25 = load <8 x i32>, <8 x i32>* %6, align 4, !tbaa !2, !alias.scope
!6
...

But we still reload p[i] '%wide.load25'..

Expected IR:
vector.body:                                      ; preds = %entry,
%vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %entry ]
  %vec.phi = phi <8 x i32> [ %5, %vector.body ], [ zeroinitializer, %entry ]
  %0 = getelementptr inbounds i32, i32* %p, i64 %index
  %1 = bitcast i32* %0 to <8 x i32>*
  %wide.load = load <8 x i32>, <8 x i32>* %1, align 4, !tbaa !2, !alias.scope
!6
  %2 = shl <8 x i32> %wide.load, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32
1, i32 1>
  %3 = getelementptr inbounds i32, i32* %q, i64 %index
  %4 = bitcast i32* %3 to <8 x i32>*
  store <8 x i32> %2, <8 x i32>* %4, align 4, !tbaa !2, !alias.scope !9,
!noalias !6
...</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>