[llvm] r363964 - [LFTR] Fix a (latent?) bug related to nested loops

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 11:45:06 PDT 2019


Author: reames
Date: Thu Jun 20 11:45:06 2019
New Revision: 363964

URL: http://llvm.org/viewvc/llvm-project?rev=363964&view=rev
Log:
[LFTR] Fix a (latent?) bug related to nested loops

I can't actually come up with a test case this triggers on without an out of tree change, but in theory, it's a bug in the recently added multiple exit LFTR support.  The root issue is that an exiting block common to two loops can (in theory) have computable exit counts for both loops.  Rewriting the exit of an inner loop in terms of the outer loops IV would cause the inner loop to either a) run forever, or b) terminate on the first iteration.

In practice, we appear to get lucky and not have the exit count computable for the outer loop, except when it's trivially zero.  Given we bail on zero exit counts, we don't appear to ever trigger this.  But I can't come up with a reason we *can't* compute an exit count for the outer loop on the common exiting block, so this may very well be triggering in some cases.


Modified:
    llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=363964&r1=363963&r2=363964&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Thu Jun 20 11:45:06 2019
@@ -2710,6 +2710,12 @@ bool IndVarSimplify::run(Loop *L) {
       // Can't rewrite non-branch yet.
       if (!isa<BranchInst>(ExitingBB->getTerminator()))
         continue;
+
+      // If our exitting block exits multiple loops, we can only rewrite the
+      // innermost one.  Otherwise, we're changing how many times the innermost
+      // loop runs before it exits. 
+      if (LI->getLoopFor(ExitingBB) != L)
+        continue;
       
       if (!needsLFTR(L, ExitingBB))
         continue;




More information about the llvm-commits mailing list