<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] functionally-equivalent comparison code produces different IR"
   href="https://llvm.org/bugs/show_bug.cgi?id=28476">28476</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[InstCombine] functionally-equivalent comparison code produces different IR
          </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>int good(int x, int y) {
  return (x == 0 || y == 0) ? 1 : 0;
}
int bad(int x, int y) {
  return (x != 0 && y != 0) ? 0 : 1;
}

These are equivalent (DeMorgan's Law), but they generate different IR (and
ultimately different asm):

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

define i32 @good(i32 %x, i32 %y) local_unnamed_addr #0 {
entry:
  %cmp = icmp eq i32 %x, 0
  %cmp1 = icmp eq i32 %y, 0
  %0 = or i1 %cmp, %cmp1
  %cond = zext i1 %0 to i32
  ret i32 %cond
}


define i32 @bad(i32 %x, i32 %y) local_unnamed_addr #0 {
entry:
  %cmp = icmp ne i32 %x, 0
  %cmp1 = icmp ne i32 %y, 0
  %0 = and i1 %cmp, %cmp1
  %1 = zext i1 %0 to i32
  %cond = xor i32 %1, 1
  ret i32 %cond
}

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

I think this can be fixed by removing a canonicalization in
InstCombiner::visitZExt() that causes us to miss 'not' logic:

  // zext (xor i1 X, true) to i32  --> xor (zext i1 X to i32), 1

This issue also came up in:
<a href="http://reviews.llvm.org/D21899">http://reviews.llvm.org/D21899</a></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>