<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: shld peephole missed with masked shift-counts"
   href="https://bugs.llvm.org/show_bug.cgi?id=34641">34641</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>x86: shld peephole missed with masked shift-counts
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@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 shld_safe(unsigned a, unsigned b, unsigned n){
        //n=13;
        a <<= n&31;
        b >>= (32-n)&31; // (32-n)&31
        return a|b;
}
<a href="https://godbolt.org/g/MxuAht">https://godbolt.org/g/MxuAht</a>


clang 6.0.0 (trunk 313348) -march=skylake -O3

        shlx    ecx, edi, edx
        neg     edx              # at least 32&31 optimized to 0
        shrx    eax, esi, edx
        or      eax, ecx
        ret

It takes even more instructions without BMI2, of course.

Without the `&31`, this optimizes to SHLD, so the peephole needs to learn that
SHLD masks the shift-count with &31 for 32-bit or smaller operand-size.  (Or
with &63 for 64-bit operand-size).  <a href="http://felixcloutier.com/x86/SHLD.html">http://felixcloutier.com/x86/SHLD.html</a>.

        mov     ecx, edx
        shld    edi, esi, cl
        mov     eax, edi
        ret

I didn't test with SHRD, but I assume it's the same.


BTW, this peephole activates even with `-march=znver1` or other AMD
architectures, where shld r,r,cl is 7 uops...  SHLD may still be worth it
without BMI2 for the variable-count case, but probably not when BMI2 is
available and tuning for AMD.

SHLD is probably still a good idea for tune=generic, especially with
variable-count; it's great on Intel, and only slightly worse than the amount of
instructions it takes to work around it on AMD, unless VectorPath instructions
are really bad in the front-end.)</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>