<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 opportunity to simplify bitselect pattern"
   href="https://bugs.llvm.org/show_bug.cgi?id=50816">50816</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed opportunity to simplify bitselect pattern
          </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>Windows NT
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>lebedev.ri@gmail.com, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Pulled from bullet source code:

unsigned btSelect(unsigned condition, unsigned valueIfConditionNonZero,
unsigned valueIfConditionZero) 
{
    // Set testNz to 0xFFFFFFFF if condition is nonzero, 0x00000000 if
condition is zero
    // Rely on positive value or'ed with its negative having sign bit on
    // and zero value or'ed with its negative (which is still zero) having sign
bit off 
    // Use arithmetic shift right, shifting the sign bit through all 32 bits
    unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
    unsigned testEqz = ~testNz;
    return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero &
testEqz)); 
}

<a href="https://c.godbolt.org/z/rMf6Ta613">https://c.godbolt.org/z/rMf6Ta613</a>

The following code doesn't get simplified:

define i8 @src(i8 %a0, i8 %a1, i8 %a2) {
  %add = add i8 %a0, -1
  %xor = xor i8 %a0, -1
  %and = and i8 %add, %xor
  %cmp = icmp sgt i8 %and, -1
  %sel = select i1 %cmp, i8 %a1, i8 %a2
  ret i8 %sel
}

define i8 @tgt(i8 %a0, i8 %a1, i8 %a2) {
  %cmp = icmp ne i8 %a0, 0
  %sel = select i1 %cmp, i8 %a1, i8 %a2
  ret i8 %sel
}

Transformation seems to be correct!</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>