Skip to content

Commit aae168f

Browse files
author
Michael Zolotukhin
committedAug 9, 2016
[LoopSimplify] Rebuild LCSSA for the inner loop after separating nested loops.
Summary: This hopefully fixes PR28825. The problem now was that a value from the original loop was used in a subloop, which became a sibling after separation. While a subloop doesn't need an lcssa phi node, a sibling does, and that's where we broke LCSSA. The most natural way to fix this now is to simply call formLCSSA on the original loop: it'll do what we've been doing before plus it'll cover situations described above. I think we don't need to run formLCSSARecursively here, and we have an assert to verify this (I've tried testing it on LLVM testsuite + SPECs). I'd be happy to be corrected here though. I also changed a run line in the test from '-lcssa -loop-unroll' to '-lcssa -loop-simplify -indvars', because it exercises LCSSA preservation to the same extent, but also makes less unrelated transformation on the CFG, which makes it easier to verify. Reviewers: chandlerc, sanjoy, silvas Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D23288 llvm-svn: 278173
1 parent 3c05edf commit aae168f

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed
 

‎llvm/lib/Transforms/Utils/LoopSimplify.cpp

+4-32
Original file line numberDiff line numberDiff line change
@@ -361,39 +361,11 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
361361
// Fix LCSSA form for L. Some values, which previously were only used inside
362362
// L, can now be used in NewOuter loop. We need to insert phi-nodes for them
363363
// in corresponding exit blocks.
364+
// We don't need to form LCSSA recursively, because there cannot be uses
365+
// inside a newly created loop of defs from inner loops as those would
366+
// already be a use of an LCSSA phi node.
367+
formLCSSA(*L, *DT, LI, SE);
364368

365-
// Go through all instructions in OuterLoopBlocks and check if they are
366-
// using operands from the inner loop. In this case we'll need to fix LCSSA
367-
// for these instructions.
368-
SmallSetVector<Instruction *, 8> WorklistSet;
369-
for (BasicBlock *OuterBB: OuterLoopBlocks) {
370-
for (Instruction &I : *OuterBB) {
371-
for (Value *Op : I.operands()) {
372-
Instruction *OpI = dyn_cast<Instruction>(Op);
373-
if (!OpI || !L->contains(OpI))
374-
continue;
375-
WorklistSet.insert(OpI);
376-
}
377-
}
378-
}
379-
// We also need to check exit blocks of the outer loop - it might be using
380-
// values from what now became an inner loop.
381-
SmallVector<BasicBlock*, 8> ExitBlocks;
382-
NewOuter->getExitBlocks(ExitBlocks);
383-
for (BasicBlock *ExitBB: ExitBlocks) {
384-
for (Instruction &I : *ExitBB) {
385-
for (Value *Op : I.operands()) {
386-
Instruction *OpI = dyn_cast<Instruction>(Op);
387-
if (!OpI || !L->contains(OpI))
388-
continue;
389-
WorklistSet.insert(OpI);
390-
}
391-
}
392-
}
393-
394-
SmallVector<Instruction *, 8> Worklist(WorklistSet.begin(),
395-
WorklistSet.end());
396-
formLCSSAForInstructions(Worklist, *DT, *LI);
397369
assert(NewOuter->isRecursivelyLCSSAForm(*DT) &&
398370
"LCSSA is broken after separating nested loops!");
399371
}

‎llvm/test/Transforms/LoopSimplify/pr28272.ll

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt < %s -lcssa -loop-unroll -S | FileCheck %s
1+
; RUN: opt < %s -lcssa -loop-simplify -indvars -S | FileCheck %s
22
target triple = "x86_64-unknown-linux-gnu"
33

44
; PR28272, PR28825
@@ -106,3 +106,34 @@ bb_end:
106106
%x = getelementptr i32, i32* %b
107107
br label %bb_end
108108
}
109+
110+
; When LoopSimplify separates nested loops, it might break LCSSA form: values
111+
; from the original loop might occur in a loop, which is now a sibling of the
112+
; original loop (before separating it was a subloop of the original loop, and
113+
; thus didn't require an lcssa phi nodes).
114+
; CHECK-LABEL: @foo4
115+
define void @foo4() {
116+
bb1:
117+
br label %bb2
118+
119+
; CHECK: bb2.loopexit:
120+
bb2.loopexit: ; preds = %bb3
121+
%i.ph = phi i32 [ 0, %bb3 ]
122+
br label %bb2
123+
124+
; CHECK: bb2.outer:
125+
; CHECK: bb2:
126+
bb2: ; preds = %bb2.loopexit, %bb2, %bb1
127+
%i = phi i32 [ 0, %bb1 ], [ %i, %bb2 ], [ %i.ph, %bb2.loopexit ]
128+
%x = load i32, i32* undef, align 8
129+
br i1 undef, label %bb2, label %bb3.preheader
130+
131+
; CHECK: bb3.preheader:
132+
bb3.preheader: ; preds = %bb2
133+
; CHECK: %x.lcssa = phi i32 [ %x, %bb2 ]
134+
br label %bb3
135+
136+
bb3: ; preds = %bb3.preheader, %bb3
137+
%y = add i32 2, %x
138+
br i1 true, label %bb2.loopexit, label %bb3
139+
}

0 commit comments

Comments
 (0)
Please sign in to comment.