<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 rule for ICmp+Add causes extra instructions"
   href="https://bugs.llvm.org/show_bug.cgi?id=44899">44899</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstCombine rule for ICmp+Add causes extra instructions
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </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>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>opt
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>anna@azul.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>lebedev.ri@gmail.com, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com, t.p.northover@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Recently, there was a change (0f22e783a038b6983f0fe161eef6cf2add3a4156) landed
in foldICmpAddConstant which was a revert of another change (checking for
multiple uses of Add much earlier).

With this change we are having regressions on an internal benchmark.

I've narrowed it down to this IR snippet:
define i1 @test(i64 %0) {  
  %c1 = ashr i64 %0, 1
  %c2 = add i64 %c1, 31083597720000
  %c3 = icmp eq i64 %c2, 0
  %c4 = icmp slt i64 %c2, 0
  %c6 = select i1 %c4, i32 -1, i32 1
  %result = select i1 %c3, i32 0, i32 %c6
  %c5 = icmp sge i32 %result, 0
  br i1 %c5, label %bci_28, label %unreached

bci_28:                                           ; preds = %bci_0
  ret i1 true

unreached:
  ret i1 false
}

Running with instCombine produces:
define i1 @test(i64 %0) {
  %c1.mask = and i64 %0, -2
  %c3 = icmp eq i64 %c1.mask, -62167195440000
  %c4 = icmp sgt i64 %0, -62167195440001
  %c5 = or i1 %c3, %c4
  br i1 %c5, label %bci_28, label %unreached

bci_28:                                           ; preds = %1
  ret i1 true

unreached:                                        ; preds = %1
  ret i1 false
}


We can clearly see from the original snippet, the whole sequence can be
narrowed to:
define i1 @test(i64 %0) {
  %2 = icmp sgt i64 %0, -62167195440001
  br i1 %2, label %bci_28, label %unreached

bci_28:                                           ; preds = %1
  ret i1 true

unreached:                                        ; preds = %1
  ret i1 false
}

This is the case if I run instCombine with the change
0f22e783a038b6983f0fe161eef6cf2add3a4156 reverted.

I noticed that this was done to fix PR44100. We will need a stronger check for
Adds that have more than one use. This is a proper case of more instructions
generated even if we break the dep chain between add and compare.</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>