<html>
    <head>
      <base href="https://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 --- - Regression: Miscompile with -O1 and -fsanitize=signed-integer-overflow"
   href="https://llvm.org/bugs/show_bug.cgi?id=23212">23212</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Regression: Miscompile with -O1 and -fsanitize=signed-integer-overflow
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jonathan.sauer@gmx.de
          </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>The following program should print 1, and it does so when compiled with clang
r234732 and either -O1 or -fsanitize=signed-integer-overflow. However, when
compiled with both -O1 *and* -fsanitize=signed-integer-overflow, it prints 0.

#include <cstdio>

int flipSign(unsigned int t)
{
  return -int(t);
}

int main()
{
  std::printf("%d\n", -2 == flipSign(2));
}


% ~/LLVM/build/Release+Asserts/bin/clang++ -O1 clang.cpp && ./a.out
1

% ~/LLVM/build/Release+Asserts/bin/clang++ -fsanitize=signed-integer-overflow
clang.cpp && ./a.out
1

% ~/LLVM/build/Release+Asserts/bin/clang++ -O1
-fsanitize=signed-integer-overflow clang.cpp && ./a.out
0

When compiled with clang r234147, it always prints 1. So does removing the
explicit cast to "int" in "flipSign".

The miscompile also happens with higher optimization levels, e.g. -O2, -O3 and
-Os.


Detailed clang version:
clang version 3.7.0 (trunk 234732)
Target: x86_64-apple-darwin13.4.0
Thread model: posix</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>