<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - Compiler missing corner case of single bit test optimization"
   href="http://llvm.org/bugs/show_bug.cgi?id=21993">21993</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Compiler missing corner case of single bit test optimization
          </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>normal
          </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>douglas_yung@playstation.sony.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In r222871, an optimization was re-added to optimize sequences of the form:

(X & Y) == 0 ?     X : X ^ Y  --> X & ~Y
(X & Y) != 0 ? X ^ Y :     X  --> X & ~Y
(X & Y) == 0 ? X ^ Y :     X  --> X |  Y
(X & Y) != 0 ?     X : X ^ Y  --> X |  Y

It seems to work for most cases, but it seems if Y=0x80000000, the optimization
is not applied when it could be.

Consider the following code:
=====
unsigned int test0a(unsigned int x) {
  return (x & CONSTANT) == 0 ? x : (x ^ CONSTANT);
}

 -- or --

unsigned int test0b(unsigned int x) {
  if((x & CONSTANT) == 0)
    return x;
  else
    return (x ^ CONSTANT);
}
=====

Both represent the first pattern. If they are compiled targeting x64 linux with
-O1 and Y=0x8, clang (r224566) will generate the following code for both
functions:

        andl    $-9, %edi
        movl    %edi, %eax

which is what we expect.

However, if we use the same compiler and instead use Y=0x80000000, the compiler
generates the following for the functions:

        movl    %edi, %eax
        xorl    $-2147483648, %eax      # imm = 0xFFFFFFFF80000000
        testl   %edi, %edi
        cmovnsl %edi, %eax

The code is still correct, however, it could be better. The optimization
re-added with r222871 does not seem to be detecting this case as valid for
optimization when it should.</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>