<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 misscompiles “|=” if the right side is a int{char{T}}"
   href="http://llvm.org/bugs/show_bug.cgi?id=16903">16903</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang misscompiles “|=” if the right side is a int{char{T}}
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.3
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>florian.weber@sfz-bw.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, 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=11041" name="attach_11041" title="the preprocessed source">attachment 11041</a> <a href="attachment.cgi?id=11041&action=edit" title="the preprocessed source">[details]</a></span>
the preprocessed source

The following program causes clang to create wrong code (plattform:
x86_64-unknown-linux-gnu):

#include <iostream>

template<typename T>
void fun (T it) {
    int m = 0;
    for(int i = 0; i < 4; ++i, ++it){
        m |= int{char{*it}};
        std::cout << int(*it) << ", " << m << ", " << i << std::endl;
    }
}

int main () {
    char in[4] = {0,0,0,0};
    fun(in);
}

Expected output:
0, 0, 0
0, 0, 1
0, 0, 2
0, 0, 3

The real output depends on the optimization-level, eg with -O0:
0, 256, 0
0, 110578432, 1
0, 110578432, 2
0, 110578432, 3

(110578432 is a number unique to every run if adress-space-randomisation is
enabled, otherwise it is always -7936)

with -O3 the output is always:
0, 224, 0
0, 224, 1
0, 224, 2
0, 224, 3

Additional information:

My commandline is:
clang++ -O0 -std=c++11 main.cpp / clang++ -O3 -std=c++11 main.cpp

The preprocessed source is attached.

As the Bug has some (though small) similarities, it may be related to #16894.

A look into the assembler showed that the only “or”-instruction in the TU
involves the register %ecx which is not used in any other place of the
object-file (I don't known details of the x64-assembler, so this might be
unrelated but it took long enough to find and looks suspicious enough that I
note it here).

clang++ --version:
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

plattform: Arch Linux</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>