[LLVMbugs] [Bug 23757] New: incorrect comparison result between INT_MAX and INT_MIN with optimizations

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Jun 4 09:08:16 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23757

            Bug ID: 23757
           Summary: incorrect comparison result between INT_MAX and
                    INT_MIN with optimizations
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: todd at cloudera.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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 at todd-ThinkPad-T540p:/tmp$ $I/clang++ -O0 -c -emit-llvm test.cc
todd at 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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150604/9667738e/attachment.html>


More information about the llvm-bugs mailing list