<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 --- - ExitValue rewrite in IndVarSimplify introduced high cost operations in loop preheader"
   href="https://llvm.org/bugs/show_bug.cgi?id=23538">23538</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ExitValue rewrite in IndVarSimplify introduced high cost operations in loop preheader
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>wmi@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=14333" name="attach_14333" title="testcase 1.cc">attachment 14333</a> <a href="attachment.cgi?id=14333&action=edit" title="testcase 1.cc">[details]</a></span>
testcase 1.cc

ExitValue rewrite in IndVarSimplify may introduce stmt (like div) with high
expansion cost in loop preheader. Existing implementation doesn't consider the
cost. If the loop is not hot enough or ExitValue rewrite doesn't reduce the
amount of IV processing in loop substantially, the high expansion cost in loop
preheader will be a problem.

The simple testcase 1.cc attached shows the problem.
~/workarea/llvm-r236609/build/bin/clang++ -O2 -S 1.cc

# BB#1:                                 # %while.body.lr.ph
-----------
        leal    -12(%rsi), %r8d
        movl    $2863311531, %ecx       # imm = 0xAAAAAAAB
        imulq   %r8, %rcx
        shrq    $35, %rcx
        shll    $2, %ecx
        leal    (%rcx,%rcx,2), %ecx
        subl    %ecx, %r8d
-----------
        movl    $-1640531527, %ecx      # imm = 0xFFFFFFFF9E3779B9
        .align  16, 0x90
.LBB0_2:                                # %while.body
                                        # =>This Inner Loop Header: Depth=1
        movl    (%rdi), %edx
        leal    (%rdx,%rdx), %eax
        andl    $16843008, %eax         # imm = 0x1010100
        addl    %ecx, %edx
        subl    %eax, %edx
        movl    %edx, 4(%rsp)
        addq    $12, %rdi
        addl    $-12, %esi
        cmpl    $11, %esi
        movl    %edx, %ecx
        ja      .LBB0_2

The insns between the two dash lines are the extra insns generated for ExitVal
rewrite in IndVarSimplify. 

It is not easy to evaluate whether ExitValue rewrite will or will not reduce
the amount of IV processing in loop in IndVarSimplify pass. Without profiling
information support, it is also difficult to know for sure whether a loop is
hot enough. So it may be good to be a little conservative here at least in
none-FDO mode (Feedback Optimization mode). 

I think ExitValue rewrite should use the similar logic as
LinearFunctionTestReplace which rewrites ICMP to ICMPZero only when expansion
cost is low.

The problem degraded one of our internal benchmark by 10% and another one by
1.5%. We didn't see >1% regression by simply turning off ExitValue rewrite.</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>