Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit fc9c4d6

Browse files
committed
Merging r276077:
------------------------------------------------------------------------ r276077 | mzolotukhin | 2016-07-19 18:55:27 -0700 (Tue, 19 Jul 2016) | 4 lines Revert "Revert r275883 and r275891. They seem to cause PR28608." This reverts commit r276064, and thus reapplies r275891 and r275883 with a fix for PR28608. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_39@276665 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8830176 commit fc9c4d6

File tree

5 files changed

+267
-15
lines changed

5 files changed

+267
-15
lines changed

lib/Transforms/Utils/LCSSA.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
6464
DominatorTree &DT, LoopInfo &LI) {
6565
SmallVector<Use *, 16> UsesToRewrite;
6666
SmallVector<BasicBlock *, 8> ExitBlocks;
67+
SmallSetVector<PHINode *, 16> PHIsToRemove;
6768
PredIteratorCache PredCache;
6869
bool Changed = false;
6970

@@ -115,7 +116,8 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
115116
SmallVector<PHINode *, 16> AddedPHIs;
116117
SmallVector<PHINode *, 8> PostProcessPHIs;
117118

118-
SSAUpdater SSAUpdate;
119+
SmallVector<PHINode *, 4> InsertedPHIs;
120+
SSAUpdater SSAUpdate(&InsertedPHIs);
119121
SSAUpdate.Initialize(I->getType(), I->getName());
120122

121123
// Insert the LCSSA phi's into all of the exit blocks dominated by the
@@ -184,6 +186,14 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
184186

185187
// Otherwise, do full PHI insertion.
186188
SSAUpdate.RewriteUse(*UseToRewrite);
189+
190+
// SSAUpdater might have inserted phi-nodes inside other loops. We'll need
191+
// to post-process them to keep LCSSA form.
192+
for (PHINode *InsertedPN : InsertedPHIs) {
193+
if (auto *OtherLoop = LI.getLoopFor(InsertedPN->getParent()))
194+
if (!L->contains(OtherLoop))
195+
PostProcessPHIs.push_back(InsertedPN);
196+
}
187197
}
188198

189199
// Post process PHI instructions that were inserted into another disjoint
@@ -196,13 +206,19 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
196206
Worklist.push_back(PostProcessPN);
197207
}
198208

199-
// Remove PHI nodes that did not have any uses rewritten.
209+
// Keep track of PHI nodes that we want to remove because they did not have
210+
// any uses rewritten.
200211
for (PHINode *PN : AddedPHIs)
201212
if (PN->use_empty())
202-
PN->eraseFromParent();
213+
PHIsToRemove.insert(PN);
203214

204215
Changed = true;
205216
}
217+
// Remove PHI nodes that did not have any uses rewritten.
218+
for (PHINode *PN : PHIsToRemove) {
219+
assert (PN->use_empty() && "Trying to remove a phi with uses.");
220+
PN->eraseFromParent();
221+
}
206222
return Changed;
207223
}
208224

lib/Transforms/Utils/LoopSimplify.cpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,19 +327,62 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
327327
else
328328
NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));
329329

330+
SmallVector<BasicBlock *, 8> OuterLoopBlocks;
331+
OuterLoopBlocks.push_back(NewBB);
330332
// Now that we know which blocks are in L and which need to be moved to
331333
// OuterLoop, move any blocks that need it.
332334
for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
333335
BasicBlock *BB = L->getBlocks()[i];
334336
if (!BlocksInL.count(BB)) {
335337
// Move this block to the parent, updating the exit blocks sets
336338
L->removeBlockFromLoop(BB);
337-
if ((*LI)[BB] == L)
339+
if ((*LI)[BB] == L) {
338340
LI->changeLoopFor(BB, NewOuter);
341+
OuterLoopBlocks.push_back(BB);
342+
}
339343
--i;
340344
}
341345
}
342346

347+
// Split edges to exit blocks from the inner loop, if they emerged in the
348+
// process of separating the outer one.
349+
SmallVector<BasicBlock *, 8> ExitBlocks;
350+
L->getExitBlocks(ExitBlocks);
351+
SmallSetVector<BasicBlock *, 8> ExitBlockSet(ExitBlocks.begin(),
352+
ExitBlocks.end());
353+
for (BasicBlock *ExitBlock : ExitBlockSet) {
354+
if (any_of(predecessors(ExitBlock),
355+
[L](BasicBlock *BB) { return !L->contains(BB); })) {
356+
rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
357+
}
358+
}
359+
360+
if (PreserveLCSSA) {
361+
// Fix LCSSA form for L. Some values, which previously were only used inside
362+
// L, can now be used in NewOuter loop. We need to insert phi-nodes for them
363+
// in corresponding exit blocks.
364+
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+
SmallVector<Instruction *, 8> Worklist(WorklistSet.begin(),
380+
WorklistSet.end());
381+
formLCSSAForInstructions(Worklist, *DT, *LI);
382+
assert(NewOuter->isRecursivelyLCSSAForm(*DT) &&
383+
"LCSSA is broken after separating nested loops!");
384+
}
385+
343386
return NewOuter;
344387
}
345388

@@ -541,17 +584,12 @@ static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,
541584
SmallSetVector<BasicBlock *, 8> ExitBlockSet(ExitBlocks.begin(),
542585
ExitBlocks.end());
543586
for (BasicBlock *ExitBlock : ExitBlockSet) {
544-
for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
545-
PI != PE; ++PI)
546-
// Must be exactly this loop: no subloops, parent loops, or non-loop preds
547-
// allowed.
548-
if (!L->contains(*PI)) {
549-
if (rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA)) {
550-
++NumInserted;
551-
Changed = true;
552-
}
553-
break;
554-
}
587+
if (any_of(predecessors(ExitBlock),
588+
[L](BasicBlock *BB) { return !L->contains(BB); })) {
589+
rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
590+
++NumInserted;
591+
Changed = true;
592+
}
555593
}
556594

557595
// If the header has more than two predecessors at this point (from the

test/Transforms/LCSSA/pr28424.ll

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; RUN: opt < %s -lcssa -S -o - | FileCheck %s
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
; PR28424
5+
; Here LCSSA adds phi-nodes for %x into the loop exits. Then, SSAUpdater needs
6+
; to insert phi-nodes to merge these values. That creates a new def, which in
7+
; its turn needs another LCCSA phi-node, and this test ensures that we insert
8+
; it.
9+
10+
; CHECK-LABEL: @foo1
11+
define internal i32 @foo1() {
12+
entry:
13+
br label %header
14+
15+
header:
16+
%x = add i32 0, 1
17+
br i1 undef, label %if, label %loopexit1
18+
19+
if:
20+
br i1 undef, label %latch, label %loopexit2
21+
22+
latch:
23+
br i1 undef, label %header, label %loopexit3
24+
25+
; CHECK: loopexit1:
26+
; CHECK: %x.lcssa = phi i32 [ %x, %header ]
27+
loopexit1:
28+
br label %loop_with_insert_point
29+
30+
; CHECK: loopexit2:
31+
; CHECK: %x.lcssa1 = phi i32 [ %x, %if ]
32+
loopexit2:
33+
br label %exit
34+
35+
; CHECK: loopexit3:
36+
; CHECK: %x.lcssa2 = phi i32 [ %x, %latch ]
37+
loopexit3:
38+
br label %loop_with_insert_point
39+
40+
; CHECK: loop_with_insert_point:
41+
; CHECK: %x4 = phi i32 [ %x4, %loop_with_insert_point ], [ %x.lcssa2, %loopexit3 ], [ %x.lcssa, %loopexit1 ]
42+
loop_with_insert_point:
43+
br i1 undef, label %loop_with_insert_point, label %bb
44+
45+
; CHECK: bb:
46+
; CHECK: %x4.lcssa = phi i32 [ %x4, %loop_with_insert_point ]
47+
bb:
48+
br label %exit
49+
50+
; CHECK: exit:
51+
; CHECK: %x3 = phi i32 [ %x4.lcssa, %bb ], [ %x.lcssa1, %loopexit2 ]
52+
exit:
53+
ret i32 %x
54+
}
55+
56+
; CHECK-LABEL: @foo2
57+
define internal i32 @foo2() {
58+
entry:
59+
br label %header
60+
61+
header:
62+
%x = add i32 0, 1
63+
br i1 undef, label %latch, label %loopexit1
64+
65+
latch:
66+
br i1 undef, label %header, label %loopexit2
67+
68+
; CHECK: loopexit1:
69+
; CHECK: %x.lcssa = phi i32 [ %x, %header ]
70+
loopexit1:
71+
br label %loop_with_insert_point
72+
73+
; CHECK: loopexit2:
74+
; CHECK: %x.lcssa1 = phi i32 [ %x, %latch ]
75+
loopexit2:
76+
br label %loop_with_insert_point
77+
78+
; CHECK: loop_with_insert_point:
79+
; CHECK: %x2 = phi i32 [ %x2, %loop_with_insert_point ], [ %x.lcssa1, %loopexit2 ], [ %x.lcssa, %loopexit1 ]
80+
loop_with_insert_point:
81+
br i1 undef, label %loop_with_insert_point, label %exit
82+
83+
; CHECK: exit:
84+
; CHECK: %x2.lcssa = phi i32 [ %x2, %loop_with_insert_point ]
85+
exit:
86+
ret i32 %x
87+
}

test/Transforms/LCSSA/pr28608.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: opt < %s -lcssa -disable-output
2+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
; PR28608
6+
; Check that we don't crash on this test.
7+
8+
define void @foo() {
9+
entry:
10+
br label %bb1
11+
12+
bb1:
13+
br label %bb2
14+
15+
bb2:
16+
%x = phi i32 [ undef, %bb5 ], [ undef, %bb1 ]
17+
br i1 undef, label %bb3, label %bb6
18+
19+
bb3:
20+
br i1 undef, label %bb5, label %bb4
21+
22+
bb4:
23+
br label %bb6
24+
25+
bb5:
26+
br label %bb2
27+
28+
bb6:
29+
br label %bb1
30+
31+
exit:
32+
%y = add i32 0, %x
33+
ret void
34+
}
35+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
; RUN: opt < %s -lcssa -loop-unroll -S | FileCheck %s
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
; PR28272
5+
; When LoopSimplify separates nested loops, it might break LCSSA form: values
6+
; from the original loop might be used in the outer loop. This test invokes
7+
; loop-unroll, which calls loop-simplify before itself. If LCSSA is broken
8+
; after loop-simplify, we crash on assertion.
9+
10+
; CHECK-LABEL: @foo
11+
define void @foo() {
12+
entry:
13+
br label %header
14+
15+
header:
16+
br label %loop1
17+
18+
loop1:
19+
br i1 true, label %loop1, label %bb43
20+
21+
bb43:
22+
%a = phi i32 [ undef, %loop1 ], [ 0, %bb45 ], [ %a, %bb54 ]
23+
%b = phi i32 [ 0, %loop1 ], [ 1, %bb54 ], [ %c, %bb45 ]
24+
br i1 true, label %bb114, label %header
25+
26+
bb114:
27+
%c = add i32 0, 1
28+
%d = add i32 0, 1
29+
br i1 true, label %bb45, label %bb54
30+
31+
bb45:
32+
%x = add i32 %d, 0
33+
br label %bb43
34+
35+
bb54:
36+
br label %bb43
37+
}
38+
39+
; CHECK-LABEL: @foo2
40+
define void @foo2() {
41+
entry:
42+
br label %outer
43+
44+
outer.loopexit:
45+
br label %outer
46+
47+
outer:
48+
br label %loop1
49+
50+
loop1:
51+
br i1 true, label %loop1, label %loop2.preheader
52+
53+
loop2.preheader:
54+
%a.ph = phi i32 [ undef, %loop1 ]
55+
%b.ph = phi i32 [ 0, %loop1 ]
56+
br label %loop2
57+
58+
loop2:
59+
%a = phi i32 [ 0, %loop2.if.true ], [ %a, %loop2.if.false ], [ %a.ph, %loop2.preheader ], [0, %bb]
60+
%b = phi i32 [ 1, %loop2.if.false ], [ %c, %loop2.if.true ], [ %b.ph, %loop2.preheader ], [%c, %bb]
61+
br i1 true, label %loop2.if, label %outer.loopexit
62+
63+
loop2.if:
64+
%c = add i32 0, 1
65+
switch i32 undef, label %loop2.if.false [i32 0, label %loop2.if.true
66+
i32 1, label %bb]
67+
68+
loop2.if.true:
69+
br i1 undef, label %loop2, label %bb
70+
71+
loop2.if.false:
72+
br label %loop2
73+
74+
bb:
75+
br label %loop2
76+
}

0 commit comments

Comments
 (0)