<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 --- - LoopSimplify does not preserve LCSSA when separating nested loops."
   href="https://llvm.org/bugs/show_bug.cgi?id=28272">28272</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LoopSimplify does not preserve LCSSA when separating nested loops.
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>michael.v.zolotukhin@gmail.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>The following testcase fails with an assertion:
<span class="quote">> opt -lcssa -loop-unroll -o - -S < test.ll</span >
Assertion failed: (InLCSSA && "LCSSA is broken after loop-simplify."), function
runOnFunction, file
/Users/mvzolotu/devel/llvm.git/lib/Transforms/Utils/LoopSimplify.cpp, line 803.
Stack dump:
0.    Program arguments: bin/opt -lcssa -loop-unroll -o - -S
1.    Running pass 'Function Pass Manager' on module '<stdin>'.
2.    Running pass 'Canonicalize natural loops' on function '@func_10'

<span class="quote">> cat test.ll</span >
target triple = "x86_64-unknown-linux-gnu"

define void @func_10() {
entry:
  br label %bb29

bb29:
  br label %bb40

bb40:
  br i1 true, label %bb40, label %bb43

bb43:
  %a = phi i32 [ undef, %bb40 ], [ 0, %bb45 ], [ %a, %bb54 ]
  %b = phi i32 [ 0, %bb40 ], [ 1, %bb54 ], [ %c, %bb45 ]
  br i1 true, label %bb114, label %bb29

bb114:
  %c = add i32 0, 1
  br i1 true, label %bb45, label %bb54

bb45:
  br label %bb43

bb54:
  br label %bb43
}

This started to happen after r272224, where the assertion was added.

The bug seems to be in routine separateNestedLoop (in LoopSimplify.cpp). When
we split the loop header, we use SplitBlockPredecessors from
BasicBlockUtils.cpp. The latter function is supposed to preserve LCSSA, but in
our case splitting the block creates a new loop. Thus, loop info is no longer
valid, but SplitBlockPredecessors relies on it.

There are several possible fixes here:
1) Don't try to preserve LCSSA in the process and just recompute it when we're
finished separating the nested loops.
2) Don't pass LoopInfo to the basic blocks utils at all (thus not pretending
that we're preserving LCSSA). Then update LCSSA in separateNestedLoop manually.
Also, we might want to add an assert to SplitBlockPredecessors that we're not
splitting a loop header, since it breaks LCSSA.
3) Move logic for updating LoopInfo from separateNestedLoop to
SplitBlockPredecessors, teaching it to recognize cases where splitting a block
creates a loop and to handle such cases properly.

Option (3) looks the best to me, but if anyone is blocked, they can use (1) as
a workaround, as it's a one-line change.</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>