http://llvm.org/bugs/show_bug.cgi?id=21993

            Bug ID: 21993
           Summary: Compiler missing corner case of single bit test
                    optimization
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
    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;
  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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to