[llvm] r219241 - LoopUnroll: Create sub-loops in LoopInfo
Duncan P. N. Exon Smith
dexonsmith at apple.com
Tue Oct 7 14:19:00 PDT 2014
Author: dexonsmith
Date: Tue Oct 7 16:19:00 2014
New Revision: 219241
URL: http://llvm.org/viewvc/llvm-project?rev=219241&view=rev
Log:
LoopUnroll: Create sub-loops in LoopInfo
`LoopUnrollPass` says that it preserves `LoopInfo` -- make it so. In
particular, tell `LoopInfo` about copies of inner loops when unrolling
the outer loop.
Conservatively, also tell `ScalarEvolution` to forget about the original
versions of these loops, since their inputs may have changed.
Fixes PR20987.
Added:
llvm/trunk/test/Transforms/LoopUnroll/update-loop-info-in-subloops.ll
Modified:
llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp
Modified: llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp?rev=219241&r1=219240&r2=219241&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp Tue Oct 7 16:19:00 2014
@@ -301,13 +301,41 @@ bool llvm::UnrollLoop(Loop *L, unsigned
for (unsigned It = 1; It != Count; ++It) {
std::vector<BasicBlock*> NewBlocks;
+ SmallDenseMap<const Loop *, Loop *, 4> NewLoops;
+ NewLoops[L] = L;
for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
ValueToValueMapTy VMap;
BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
Header->getParent()->getBasicBlockList().push_back(New);
- L->addBasicBlockToLoop(New, LI->getBase());
+ // Tell LI about New.
+ if (*BB == Header) {
+ assert(LI->getLoopFor(*BB) == L && "Header should not be in a sub-loop");
+ L->addBasicBlockToLoop(New, LI->getBase());
+ } else {
+ // Figure out which loop New is in.
+ const Loop *OldLoop = LI->getLoopFor(*BB);
+ assert(OldLoop && "Should (at least) be in the loop being unrolled!");
+
+ Loop *&NewLoop = NewLoops[OldLoop];
+ if (!NewLoop) {
+ // Found a new sub-loop.
+ assert(*BB == OldLoop->getHeader() &&
+ "Header should be first in RPO");
+
+ Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
+ assert(NewLoopParent &&
+ "Expected parent loop before sub-loop in RPO");
+ NewLoop = new Loop;
+ NewLoopParent->addChildLoop(NewLoop);
+
+ // Forget the old loop, since its inputs may have changed.
+ if (SE)
+ SE->forgetLoop(OldLoop);
+ }
+ NewLoop->addBasicBlockToLoop(New, LI->getBase());
+ }
if (*BB == Header)
// Loop over all of the PHI nodes in the block, changing them to use
Added: llvm/trunk/test/Transforms/LoopUnroll/update-loop-info-in-subloops.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnroll/update-loop-info-in-subloops.ll?rev=219241&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LoopUnroll/update-loop-info-in-subloops.ll (added)
+++ llvm/trunk/test/Transforms/LoopUnroll/update-loop-info-in-subloops.ll Tue Oct 7 16:19:00 2014
@@ -0,0 +1,35 @@
+; RUN: opt -S < %s -loop-unroll -block-freq | FileCheck %s
+; Crasher from PR20987.
+
+; CHECK: define void @update_loop_info_in_subloops
+; CHECK: entry:
+; CHECK: L:
+; CHECK: L.inner:
+; CHECK: L.inner.latch:
+; CHECK: L.latch:
+; CHECK: L.inner.1:
+; CHECK: L.inner.latch.1:
+; CHECK: L.latch.1:
+
+define void @update_loop_info_in_subloops() {
+entry:
+ br label %L
+
+L:
+ %0 = phi i64 [ 1, %entry ], [ %1, %L.latch ]
+ br label %L.inner
+
+L.inner:
+ br label %L.inner.latch
+
+L.inner.latch:
+ br i1 false, label %L.latch, label %L.inner
+
+L.latch:
+ %1 = add i64 %0, 1
+ %2 = icmp eq i64 %1, 3
+ br i1 %2, label %exit, label %L
+
+exit:
+ ret void
+}
More information about the llvm-commits
mailing list