<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 - Miscompile with opt -licm with memory-ssa enabled"
   href="https://bugs.llvm.org/show_bug.cgi?id=43540">43540</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompile with opt -licm with memory-ssa enabled
          </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>mikael.holmen@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=22622" name="attach_22622" title="bbi-33452.ll reproducer">attachment 22622</a> <a href="attachment.cgi?id=22622&action=edit" title="bbi-33452.ll reproducer">[details]</a></span>
bbi-33452.ll reproducer

With 

 opt -licm -S -o - bbi-33452.ll

the store to @v_2 is erroneously hoisted out of the loop, so it's done before
the memcpy reading from @v_2.

The loop is only run one round, so the hoisting looks wrong to me.

Input:

define void @foo() {
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.body, %entry
  %0 = phi i16 [ %inc, %for.body ], [ 0, %entry ]
  %cmp = icmp slt i16 %0, 1
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* @v_1, i8 * @v_2, i64 1, i1 false)
  store i8 1, i8 * @v_2, align 1
  %inc = add nsw i16 %0, 1
  br label %for.cond

for.end:                                          ; preds = %for.cond
  ret void
}

Output:

@v_1 = global i8 0, align 1
@v_2 = global i8 0, align 1

define void @foo() {
entry:
  store i8 1, i8* @v_2, align 1
  br label %for.cond

for.cond:                                         ; preds = %for.body, %entry
  %0 = phi i16 [ %inc, %for.body ], [ 0, %entry ]
  %cmp = icmp slt i16 %0, 1
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* @v_1, i8* @v_2, i64 1, i1 false)
  %inc = add nsw i16 %0, 1
  br label %for.cond

for.end:                                          ; preds = %for.cond
  ret void
}

; Function Attrs: argmemonly nounwind willreturn
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8*
noalias nocapture readonly, i64, i1 immarg) #0

attributes #0 = { argmemonly nounwind willreturn }</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>