[llvm-bugs] [Bug 50816] New: Missed opportunity to simplify bitselect pattern

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jun 23 05:44:18 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50816

            Bug ID: 50816
           Summary: Missed opportunity to simplify bitselect pattern
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm-dev at redking.me.uk
                CC: lebedev.ri at gmail.com, llvm-bugs at lists.llvm.org,
                    spatel+llvm at rotateright.com

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)); 
}

https://c.godbolt.org/z/rMf6Ta613

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!

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210623/a08b0743/attachment.html>


More information about the llvm-bugs mailing list