From 2a747bf1c566cbe9739f2a687bc093fdd5cfeda0 Mon Sep 17 00:00:00 2001 From: Dinesh Dwivedi Date: Thu, 29 May 2014 06:47:23 +0000 Subject: [PATCH] LCSSA should be performed on the outermost affected loop while unrolling loop. During loop-unroll, loop exits from the current loop may end up in in different outer loop. This requires to re-form LCSSA recursively for one level down from the outer most loop where loop exits are landed during unroll. This fixes PR18861. Differential Revision: http://reviews.llvm.org/D2976 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209796 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LoopUnroll.cpp | 9 ++++++++ test/Transforms/LoopUnroll/pr18861.ll | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 test/Transforms/LoopUnroll/pr18861.ll diff --git a/lib/Transforms/Utils/LoopUnroll.cpp b/lib/Transforms/Utils/LoopUnroll.cpp index d953e307310..16975b9e637 100644 --- a/lib/Transforms/Utils/LoopUnroll.cpp +++ b/lib/Transforms/Utils/LoopUnroll.cpp @@ -487,6 +487,15 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, if (OuterL) { ScalarEvolution *SE = PP->getAnalysisIfAvailable(); simplifyLoop(OuterL, DT, LI, PP, /*AliasAnalysis*/ nullptr, SE); + + // LCSSA must be performed on the outermost affected loop. The unrolled + // loop's last loop latch is guaranteed to be in the outermost loop after + // deleteLoopFromQueue updates LoopInfo. + Loop *LatchLoop = LI->getLoopFor(Latches.back()); + if (!OuterL->contains(LatchLoop)) + while (OuterL->getParentLoop() != LatchLoop) + OuterL = OuterL->getParentLoop(); + formLCSSARecursively(*OuterL, *DT, SE); } } diff --git a/test/Transforms/LoopUnroll/pr18861.ll b/test/Transforms/LoopUnroll/pr18861.ll new file mode 100644 index 00000000000..62f26106afb --- /dev/null +++ b/test/Transforms/LoopUnroll/pr18861.ll @@ -0,0 +1,43 @@ +; RUN: opt < %s -loop-unroll -indvars -disable-output + +@b = external global i32, align 4 + +; Function Attrs: nounwind uwtable +define void @fn1() #0 { +entry: + br label %for.cond1thread-pre-split + +for.cond1thread-pre-split: ; preds = %for.inc8, %entry + %storemerge1 = phi i32 [ 0, %entry ], [ %inc9, %for.inc8 ] + br i1 undef, label %for.inc8, label %for.cond2.preheader.lr.ph + +for.cond2.preheader.lr.ph: ; preds = %for.cond1thread-pre-split + br label %for.cond2.preheader + +for.cond2.preheader: ; preds = %for.inc5, %for.cond2.preheader.lr.ph + br label %for.cond2 + +for.cond2: ; preds = %for.body3, %for.cond2.preheader + %storemerge = phi i32 [ %add, %for.body3 ], [ 0, %for.cond2.preheader ] + %cmp = icmp slt i32 %storemerge, 1 + br i1 %cmp, label %for.body3, label %for.inc5 + +for.body3: ; preds = %for.cond2 + %tobool4 = icmp eq i32 %storemerge, 0 + %add = add nsw i32 %storemerge, 1 + br i1 %tobool4, label %for.cond2, label %if.then + +if.then: ; preds = %for.body3 + store i32 %storemerge1, i32* @b, align 4 + ret void + +for.inc5: ; preds = %for.cond2 + br i1 undef, label %for.cond1.for.inc8_crit_edge, label %for.cond2.preheader + +for.cond1.for.inc8_crit_edge: ; preds = %for.inc5 + br label %for.inc8 + +for.inc8: ; preds = %for.cond1.for.inc8_crit_edge, %for.cond1thread-pre-split + %inc9 = add nsw i32 %storemerge1, 1 + br label %for.cond1thread-pre-split +} -- 2.11.0