<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 - optimize smax(x, -1) with bit-hack"
   href="https://bugs.llvm.org/show_bug.cgi?id=49895">49895</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>optimize smax(x, -1) with bit-hack
          </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>Severity</th>
          <td>enhancement
          </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>spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Noticed while investigating idioms for signum functions...

If we have an smax with -1:

declare i32 @llvm.smax.i32(i32, i32)
define i32 @smax(i32 %x) {
  %m = call i32 @llvm.smax.i32(i32 %x, i32 -1)
  ret i32 %m
}

or:

define i32 @cmpsel(i32 %x) {
  %a = icmp sgt i32 %x, -1
  %m = select i1 %a, i32 %x, i32 -1
  ret i32 %m
}

It seems most targets would do better to convert to arithmetic shift + logic:

define i32 @tgt(i32 %x) {
  %a = ashr i32 %x, 31
  %m = or i32 %x, %a
  ret i32 %m
}

<a href="https://alive2.llvm.org/ce/z/AJYAmp">https://alive2.llvm.org/ce/z/AJYAmp</a>

AArch64:
        cmp     w0, #0                     
        csinv   w0, w0, wzr, ge
vs.
        orr     w0, w0, w0, asr #31

PowerPC64:
        li 4, -1
        cmpwi   3, -1
        rldic 4, 4, 0, 32
        iselgt  3, 3, 4
vs.
        srawi 4, 3, 31
        or 3, 3, 4

x86:
        testl   %edi, %edi
        movl    $-1, %eax
        cmovnsl %edi, %eax

vs.
        movl    %edi, %eax
        sarl    $31, %eax
        orl     %edi, %eax</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>