[PATCH] D32261: [LoopUnroll] Don't try to unroll non-rotated loops

Davide Italiano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 19 19:58:32 PDT 2017


davide updated this revision to Diff 95876.
davide added a comment.

Address Michael's comments.


https://reviews.llvm.org/D32261

Files:
  lib/Transforms/Utils/LoopUnroll.cpp
  test/Transforms/LoopUnroll/not-rotated.ll


Index: test/Transforms/LoopUnroll/not-rotated.ll
===================================================================
--- /dev/null
+++ test/Transforms/LoopUnroll/not-rotated.ll
@@ -0,0 +1,25 @@
+; PR28103
+; Don't unroll non-rotated loops. Those are not currently
+; properly handled by LoopUnroll.
+
+; RUN: opt -loop-unroll -verify-dom-info %s
+; REQUIRE: asserts
+
+define void @tinkywinky(i1 %patatino) {
+entry:
+  br label %header1
+header1:
+  %indvars.iv = phi i64 [ 1, %body2 ], [ 0, %entry ]
+  %exitcond = icmp ne i64 %indvars.iv, 1
+  br i1 %exitcond, label %body1, label %exit
+body1:
+  br i1 %patatino, label %body2, label %sink
+body2:
+  br i1 %patatino, label %header1, label %body3
+body3:
+  br label %sink
+sink:
+  br label %body2
+exit:
+  ret void
+}
Index: lib/Transforms/Utils/LoopUnroll.cpp
===================================================================
--- lib/Transforms/Utils/LoopUnroll.cpp
+++ lib/Transforms/Utils/LoopUnroll.cpp
@@ -321,10 +321,16 @@
   BasicBlock *Header = L->getHeader();
   BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
 
-  if (!BI || BI->isUnconditional()) {
-    // The loop-rotate pass can be helpful to avoid this in many cases.
-    DEBUG(dbgs() <<
-             "  Can't unroll; loop not terminated by a conditional branch.\n");
+  // If the loop is not rotated the latch is not one of the exiting blocks.
+  // Bail out on non-rotated loops.
+  SmallVector<BasicBlock *, 8> ExitingBlocks;
+  L->getExitingBlocks(ExitingBlocks);
+  bool NotRotated =
+      llvm::all_of(ExitingBlocks, [&LatchBlock](const BasicBlock *BB) {
+        return LatchBlock != BB;
+      });
+  if (NotRotated) {
+    DEBUG(dbgs() << "  Can't unroll; loop not rotated\n");
     return false;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32261.95876.patch
Type: text/x-patch
Size: 1761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170420/239d852d/attachment.bin>


More information about the llvm-commits mailing list