<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 --- - clang 3.5.0 generates invalid code for the conditional/ternary operator with -O1 or above in some cases"
   href="http://llvm.org/bugs/show_bug.cgi?id=22050">22050</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang 3.5.0 generates invalid code for the conditional/ternary operator with -O1 or above in some cases
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.5
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david@qore.org
          </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>Created <span class=""><a href="attachment.cgi?id=13602" name="attach_13602" title="example code">attachment 13602</a> <a href="attachment.cgi?id=13602&action=edit" title="example code">[details]</a></span>
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.</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>