<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 --- - [InstCombine] recognize ub-safe negation pattern"
   href="https://llvm.org/bugs/show_bug.cgi?id=27817">27817</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[InstCombine] recognize ub-safe negation pattern
          </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>All
          </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>spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In D20434 ( <a href="http://reviews.llvm.org/D20434">http://reviews.llvm.org/D20434</a> ), 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
}</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>