[llvm-bugs] [Bug 27817] New: [InstCombine] recognize ub-safe negation pattern

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 19 15:27:37 PDT 2016


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

            Bug ID: 27817
           Summary: [InstCombine] recognize ub-safe negation pattern
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

In D20434 ( http://reviews.llvm.org/D20434 ), I learned a better way to avoid
undefined behavior due to signed integer overflow when negating INT_MIN:

$ cat ub.c
#include <limits.h>

int goo(int x) {
  if (x == INT_MIN)
    return x;
  return -x;
}

int foo(int x) {
  return -(unsigned)x;
}

----------------------------------------------------------------------------

Those should be equivalent in optimized IR, but:

$ ./clang -O1 ub.c -S -o - -emit-llvm

define i32 @goo(i32 %x) #0 {
  %cmp = icmp eq i32 %x, -2147483648
  %sub = sub nsw i32 0, %x
  %sel = select i1 %cmp, i32 -2147483648, i32 %sub
  ret i32 %sel
}

define i32 @foo(i32 %x) #0 {
  %sub = sub i32 0, %x
  ret i32 %sub
}

-- 
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/20160519/5a4d6efc/attachment.html>


More information about the llvm-bugs mailing list