<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 --- - incorrect comparison result between INT_MAX and INT_MIN with optimizations" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D23757&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=cGe0r1aYLC31EEOwanT2mjzG0wcbkE54Y4piDsBQd4c&s=-87fU5bBE66PK-eC8uhxw8KhtJUmt7H0ZmsuQZp7U7o&e=">23757</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>incorrect comparison result between INT_MAX and INT_MIN with optimizations
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>todd@cloudera.com
          </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 gives different results with -O1 vs -O0:

#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <iostream>
#include <string.h>

using namespace std;

bool Increment32(int32_t* cell_ptr) {
  int32_t orig = *cell_ptr;
  int32_t inc;
  // Signed overflow is undefined in C. So, we'll use a branch here
  // instead of counting on undefined behavior.
  if (orig == INT32_MAX) {
    inc = INT32_MIN;
  } else {
    inc = orig + 1;
  }
  *cell_ptr = inc;
  return inc > orig;
}

int main(int argc, char** argv) {
  int32_t input = INT32_MAX;
  cout << "input: " << input << endl;
  bool ret = Increment32(&input);
  cout << "output: " << input << endl;
  if (ret) {
    cout << "BAD ANSWER" << endl;
  }
  return 0;
}

I narrowed it down to the following set of passes on llvm 3.4:
todd@todd-ThinkPad-T540p:/tmp$ $I/clang++ -O0 -c -emit-llvm test.cc
todd@todd-ThinkPad-T540p:/tmp$ $I/opt -sroa -simplifycfg -instcombine -o
test-opt.bc test.bc ; $I/clang++ -o test test-opt.bc ; ./test
input: 2147483647
output: -2147483648
WRONG ANSWER

I also verified that a 3.7 build (from the chromium toolchain) has the same
bug.

Compiling with -fwrapv or -fsanitize=undefined makes the error go away. gcc
doesn't have the same issue.</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>