[LLVMbugs] [Bug 22050] New: clang 3.5.0 generates invalid code for the conditional/ternary operator with -O1 or above in some cases

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Dec 29 07:26:56 PST 2014


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

            Bug ID: 22050
           Summary: clang 3.5.0 generates invalid code for the
                    conditional/ternary operator with -O1 or above in some
                    cases
           Product: clang
           Version: 3.5
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: david at qore.org
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 13602
  --> http://llvm.org/bugs/attachment.cgi?id=13602&action=edit
example code

the following code when built with clang 3.5.0 with -O1 or above is compiled
incorrectly:

#include <stdio.h>

union u {
   size_t _refptr;

   void setPtr(int* refptr, bool readonly = false) {
      _refptr = (size_t)refptr;
      if (readonly)
         _refptr |= 1;
   }

   int* getPtr() const {
      // the line below is compiled incorrectly with clang++ 3.5.0 -O1 or above
      return (int*)((_refptr & 1L) ? (_refptr ^ 1L) : _refptr);
   }
};

int main(int argc, char *argv[]) {
   u u;
   int i = 100;

   u.setPtr(&i);
   printf("i: %p p(i): %p: %s\n", &i, u.getPtr(), &i == u.getPtr() ? "OK" :
"ERROR");

   return 0;
}

-- example output is:
i: 0x7fff6adf3dac p(i): 0x7fff6adf3dad: ERROR

basically the conditional operator is evaluated with the opposite arguments
according to its boolean operand expression in this case.

I tried some other simple cases and it was OK - no clue why this is happening,
but in my case it's a big bug (I store values in the low bits of some pointers
using this trick in some cases to optimize memory usage).

Note that compiling without optimizations results in correct code generation. 
Maybe there's something awry with the optimizer with this case.

-- 
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/20141229/db3f1961/attachment.html>


More information about the llvm-bugs mailing list