<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 --- - Optimizer doesn't simplify obvious contradiction"
   href="https://llvm.org/bugs/show_bug.cgi?id=27869">27869</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimizer doesn't simplify obvious contradiction
          </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>Linux
          </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>me@manueljacob.de
          </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>The following C code contains an "obvious" contradiction.  An integer can't
both be 0 and negative, so this should be simplifies to false:

_Bool test(int i) {
  return (i == 0) & (i < 0);
}

The LLVM IR with SROA run on it:

define zeroext i1 @test(i32 %i) #0 {
entry:
  %cmp = icmp eq i32 %i, 0
  %conv = zext i1 %cmp to i32
  %cmp1 = icmp slt i32 %i, 0
  %conv2 = zext i1 %cmp1 to i32
  %and = and i32 %conv, %conv2
  %tobool = icmp ne i32 %and, 0
  ret i1 %tobool
}

However running all optimizations on it will result in the following code:

define zeroext i1 @test(i32 %i) #0 {
entry:
  %cmp = icmp eq i32 %i, 0
  %conv = zext i1 %cmp to i32
  %i.lobit = lshr i32 %i, 31
  %and = and i32 %conv, %i.lobit
  %tobool = icmp ne i32 %and, 0
  ret i1 %tobool
}

The problem is that InstCombine transforms the (zext (< i 0)) pattern into
clever bit shifting: (lshr i 31).  At first this saves one operation, but then
the obvious contradiction is obfuscated.

I see two options how to solve this:

1) Deferring the (zext (< i 0)) -> (lshr i 31) transformation, allowing
InstCombine to simplify the contradiction.

2) Making subsequent transformations more clever, so they can "look through"
the lshr instruction, seeing that it's actually a "less than" predicate.</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>