<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 - [x86] missed optimizations for shift-counts: (32-n) can be (-n)"
   href="https://bugs.llvm.org/show_bug.cgi?id=34642">34642</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] missed optimizations for shift-counts: (32-n) can be (-n)
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>peter@cordes.ca
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>unsigned shift(unsigned a, unsigned n){
        n = (32-n);
        // n &= 31;
        return a << n;
}

// <a href="https://godbolt.org/g/YFKxQw">https://godbolt.org/g/YFKxQw</a>
// clang version 6.0.0 (trunk 313348) -march=skylake -O3

        mov     eax, 32           # -mno-bmi2 version is essentially the same
        sub     eax, esi
        shlx    eax, edi, eax
        ret

But with n &= 31; we get the obviously-better

        neg     esi
        shlx    eax, edi, esi
        ret

(shlx masks the shift count identically to how shl r,cl does, with &31 for
32-bit operand size.  -mno-bmi2 code-gen is equivalent.)

So clang / llvm doesn't take full advantage of the masking behaviour of x86
shift counts for shlx,  shl r,cl, or presumably  bt / bts (which do also wrap
around at the destination register size with a register destination.)</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>