<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 - missed DCE optimization after loop unrolling"
   href="https://bugs.llvm.org/show_bug.cgi?id=43550">43550</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>missed DCE optimization after loop unrolling
          </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>All
          </td>
        </tr>

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

        <tr>
          <th>Keywords</th>
          <td>code-quality
          </td>
        </tr>

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

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

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>trass3r@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>#include <cstdint>
#include <utility>

const int N = 4;
void bitreverse(int* data)
{
    uint32_t j = 0;
    for (uint32_t i = 0; i < N; ++i)
    {
        if (j > i)
            std::swap(data[i], data[j]);

        uint32_t k = N/2;
        while (k <= j)
        {
            j -= k;
            k /= 2;
        }
        j += k;
    }
}

Even with -O3 after everything is unrolled the while loop unnecessarily
remains: <a href="https://godbolt.org/z/cA-S8J">https://godbolt.org/z/cA-S8J</a>
Of course it's even more severe with N=8: <a href="https://godbolt.org/z/niZPeS">https://godbolt.org/z/niZPeS</a>

Interestingly if you change the swap line slightly it suddenly starts using
pshufd which seems quite clever: <a href="https://godbolt.org/z/5YJnJ1">https://godbolt.org/z/5YJnJ1</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>