<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 miscompiles with -O0."
   href="https://bugs.llvm.org/show_bug.cgi?id=37115">37115</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang miscompiles with -O0.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>Vsevolod.Livinskij@frtk.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Clang miscompiles testcase with -O0.

Reproducer:
<span class="quote">>$ cat repr.cpp</span >
#include <stdio.h>

struct struct_1 {
  int a : 17;
  bool b;
  static int stat_memb;
};
int struct_1::stat_memb;

struct_1 struct_obj_1 = {42, false};
struct_1 struct_obj_2 = {42, false};

int var;

void tf_3_foo() {
  struct_obj_2.stat_memb = unsigned(~0) > 0;
  var = -((-9223372036854775807L - 1) * struct_obj_1.stat_memb /
(-9223372036854775807L - 1)) * struct_obj_1.a;
}

int main() {
  tf_3_foo();
  printf("%d\n", var);
}

Error:
<span class="quote">>$ clang++ -O0 repr.cpp ; ./a.out</span >
42
<span class="quote">>$ clang++ -O1 repr.cpp ; ./a.out</span >
-42

GCC also gives -42 as an answer.

LLVM version:
<span class="quote">>$ clang++ -v</span >
clang version 7.0.0 (trunk 329789)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /llvm/bin-trunk/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64</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>