[LLVMbugs] [Bug 21993] New: Compiler missing corner case of single bit test optimization

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Dec 19 16:00:38 PST 2014


            Bug ID: 21993
           Summary: Compiler missing corner case of single bit test
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: douglas_yung at playstation.sony.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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

        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.

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/20141220/94232cc0/attachment.html>

More information about the llvm-bugs mailing list