<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 - [LSR] Rewrite misses some fixup locations if it splits critical edge"
   href="https://bugs.llvm.org/show_bug.cgi?id=41445">41445</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LSR] Rewrite misses some fixup locations if it splits critical edge
          </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>denis.bakhvalov@intel.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21760" name="attach_21760" title="Reproducing behavior">attachment 21760</a> <a href="attachment.cgi?id=21760&action=edit" title="Reproducing behavior">[details]</a></span>
Reproducing behavior

If LSR split critical edge during rewriting operand in a phi node which has
other pending fixup locations, those pending locations will not be rewritten.
Because not all locations were rewritten formulae was not implemented
completely and some instructions are not eliminated.

Reproducer attached.
$ opt repro.ll -loop-reduce -S -o -
If you compare IR it's visible that LSR does something non-profitable. For
example, shl instruction was not eliminated, but it's possible to eliminate it.

We have this phi node where all input operands (%1, %2, %3, %4) need to be
rewritten:
%indvars.iv.lcssa.ph = phi i64 [ %1, %then.34 ], [ %2, %ifmerge.34 ], [ %3,
%ifmerge.38 ], [ %4, %ifmerge.42 ]

Then, when we try to rewrite %1, we first split the critical edge. All the
other PHI inputs besides %1 go to a new phi node. Like this:

loop.exit.loopexitsplit:                          ; preds = %ifmerge.42,
%ifmerge.38, %ifmerge.34
  %indvars.iv.lcssa.ph.ph = phi i64 [ %4, %ifmerge.42 ], [ %3, %ifmerge.38 ], [
%2, %ifmerge.34 ]
  br label %loop.exit.loopexit

%indvars.iv.lcssa.ph = phi i64 [ %0, %then.34.loop.exit.loopexit_crit_edge ], [
%indvars.iv.lcssa.ph.ph, %loop.exit.loopexitsplit ]

After that when we try to fix other operands (%2, %3, %4) in the original phi
node (%indvars.iv.lcssa.ph), there are no such operands. And algorithm just
skips those fixup sites, because it can't find them.

At the moment I'm considering 4 possible ways to fix this:
1. Forbid splitting critical edge if there are pending fixup sites in current
PHI.
2. Delay splitting critical edge if there are pending fixup sites in current
PHI, until there will be only one operand to fixup.
3. After splitting critical edge, update all the fixup sites with pointer to
the new PHI node.
4. While searching for operand to rewrite, go down through the PHI chain.</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>