<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 of zext(cttz())"
   href="https://bugs.llvm.org/show_bug.cgi?id=45762">45762</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompile of zext(cttz())
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>meheff@google.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=23430" name="attach_23430" title="Repro .ll file">attachment 23430</a> <a href="attachment.cgi?id=23430&action=edit" title="Repro .ll file">[details]</a></span>
Repro .ll file

It looks like cttz(X, false) is unsafely relaxed to cttz(X, true) if the only
use of the cttz is a zext/trunc. Transformation happens here:

<a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L889">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L889</a>

It checks to see if the cttz has a single use (unsafely assuming it is the
select), but earlier the analysis can traverse through zext/trunc so the
assumption that the cttz's single use is the select is wrong:

<a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L864">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L864</a>

If the zext/trunc has other uses then the relaxed cttz(X, true) incorrectly
leaks out around the guarding select.

Repro attached.

$ lli bad.ll
$ echo $?
8
$ opt bad.ll --O2 | lli
$ echo $?
1</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>