<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] forming subo intrinsic thwarts codegen"
   href="https://bugs.llvm.org/show_bug.cgi?id=42571">42571</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] forming subo intrinsic thwarts codegen
          </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>Backend: X86
          </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>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Forking this example from <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Missed BLSR for x == (x & -x) && x"
   href="show_bug.cgi?id=42314">bug 42314</a> because it's not exactly an "isPowerOf2"
pattern:

int foo(int x, int y) {
  return x ? (x & (x - 1)) : y;
}

Or as optimized IR (if we should create ctpop from this, then this isn't
strictly an x86 bug):

define i32 @foo(i32 %x, i32 %y) {
  %tobool = icmp eq i32 %x, 0
  %sub = add nsw i32 %x, -1
  %and = and i32 %sub, %x
  %cond = select i1 %tobool, i32 %y, i32 %and
  ret i32 %cond
}

CGP will transform that to use llvm.usub.with.overflow.i32, and we get:

  movl %edi, %eax
  subl $1, %eax
  andl %edi, %eax
  cmpl $1, %edi
  cmovbl %esi, %eax
  retq

For generic x86, this seems better
        leal    -1(%rdi), %eax
        andl    %edi, %eax
        testl   %edi, %edi
        cmove   %esi, %eax

And with BMI ('blsr'):
        blsr    %edi, %eax
        testl   %edi, %edi
        cmove   %esi, %eax


<a href="https://godbolt.org/z/Wmgyy9">https://godbolt.org/z/Wmgyy9</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>