<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 - Assert in SCEV: LHS is not available at Loop Entry"
   href="https://bugs.llvm.org/show_bug.cgi?id=36633">36633</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Assert in SCEV: LHS is not available at Loop Entry
          </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>enhancement
          </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>nemanja.i.ibm@gmail.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=20015" name="attach_20015" title="Failing test case reduced by bugpoint">attachment 20015</a> <a href="attachment.cgi?id=20015&action=edit" title="Failing test case reduced by bugpoint">[details]</a></span>
Failing test case reduced by bugpoint

Compiling the attached test case with either:
$ clang -O2 test.ll -S --target=x86_64-unknown-linux-gnu
or
$ clang -O2 test.ll -S --target=powerpc64le-unknown-linux-gnu

(and presumably other targets that I haven't checked)

Causes the following assert:
ScalarEvolution.cpp:9077: bool
llvm::ScalarEvolution::isLoopEntryGuardedByCond(const llvm::Loop*,
llvm::CmpInst::Predicate, const llvm::SCEV*, const llvm::SCEV*): Assertion
`isAv
ailableAtLoopEntry(LHS, L) && "LHS is not available at Loop Entry"' failed.

The following patch fixes it and seems like a rather logical fix to someone
like me with no real understanding of how SCEV works. Mind you I only say it
seems to make sense because of the names of the function and the SCEV type
(i.e. if the SCEV is of type "unknown" it should imply that
isKnownOnEveryIteration() should return false).
However, the patch breaks lit test cases which leads me to believe that it is
overly conservative:

Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp    (revision 326779)
+++ lib/Analysis/ScalarEvolution.cpp    (working copy)
@@ -8771,6 +8771,9 @@ bool ScalarEvolution::isKnownOnEveryIteration(ICmp
                                               const SCEVAddRecExpr *LHS,
                                               const SCEV *RHS) {
   const Loop *L = LHS->getLoop();
+  if (static_cast<SCEVTypes>(LHS->getStart()->getSCEVType()) == scUnknown ||
+      static_cast<SCEVTypes>(RHS->getSCEVType()) == scUnknown)
+    return false;
   return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) &&
          isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this),
RHS);
 }</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>