[LLVMbugs] [Bug 20186] New: Instcombine turns -(x/INT_MIN) to x/INT_MIN

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Jul 1 14:10:04 PDT 2014


            Bug ID: 20186
           Summary: Instcombine turns -(x/INT_MIN) to x/INT_MIN
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Transformation Utilities
          Assignee: unassignedbugs at nondot.org
          Reporter: davemm at cs.rutgers.edu
                CC: llvmbugs at cs.uiuc.edu, regehr at cs.utah.edu
    Classification: Unclassified

The transformation of -(X/C) to X/(-C) is invalid if C == INT_MIN.

Specifically, if I have

define i32 @foo(i32 %x) #0 {
%div = sdiv i32 %x, -2147483648
%sub = sub nsw i32 0, %div
ret i32 %sub

then opt -instcombine will produce

define i32 @foo(i32 %x) #0 {
%sub = sdiv i32 %x, -2147483648
ret i32 %sub

You can observe this with the following test case:

#include <stdio.h>
#include <limits.h>

int foo(int x)
return -(x/INT_MIN);

int main (void)
printf ("%d\n", foo(INT_MIN));

This will print -1 or 1, depending on the optimization level.

This appears to be the relevant code:

   // 0 - (X sdiv C)  -> (X sdiv -C)
   if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) &&
       match(Op0, m_Zero()))
     return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));

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/20140701/280b3814/attachment.html>

More information about the llvm-bugs mailing list