llvm.org GIT mirror llvm / 98e46ef
Revert r275883 and r275891. They seem to cause PR28608. Revert "[LoopSimplify] Update LCSSA after separating nested loops." This reverts commit r275891. Revert "[LCSSA] Post-process PHI-nodes created by SSAUpdate when constructing LCSSA form." This reverts commit r275883. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@276064 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 3 years ago
4 changed file(s) with 13 addition(s) and 223 deletion(s). Raw diff Collapse all Expand all
114114 SmallVector AddedPHIs;
115115 SmallVector PostProcessPHIs;
116116
117 SmallVector InsertedPHIs;
118 SSAUpdater SSAUpdate(&InsertedPHIs);
117 SSAUpdater SSAUpdate;
119118 SSAUpdate.Initialize(I->getType(), I->getName());
120119
121120 // Insert the LCSSA phi's into all of the exit blocks dominated by the
184183
185184 // Otherwise, do full PHI insertion.
186185 SSAUpdate.RewriteUse(*UseToRewrite);
187
188 // SSAUpdater might have inserted phi-nodes inside other loops. We'll need
189 // to post-process them to keep LCSSA form.
190 for (PHINode *InsertedPN : InsertedPHIs) {
191 if (auto *OtherLoop = LI.getLoopFor(InsertedPN->getParent()))
192 if (!L->contains(OtherLoop))
193 PostProcessPHIs.push_back(InsertedPN);
194 }
195186 }
196187
197188 // Post process PHI instructions that were inserted into another disjoint
326326 else
327327 NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));
328328
329 SmallVector OuterLoopBlocks;
330 OuterLoopBlocks.push_back(NewBB);
331329 // Now that we know which blocks are in L and which need to be moved to
332330 // OuterLoop, move any blocks that need it.
333331 for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
335333 if (!BlocksInL.count(BB)) {
336334 // Move this block to the parent, updating the exit blocks sets
337335 L->removeBlockFromLoop(BB);
338 if ((*LI)[BB] == L) {
336 if ((*LI)[BB] == L)
339337 LI->changeLoopFor(BB, NewOuter);
340 OuterLoopBlocks.push_back(BB);
341 }
342338 --i;
343339 }
344 }
345
346 // Split edges to exit blocks from the inner loop, if they emerged in the
347 // process of separating the outer one.
348 SmallVector ExitBlocks;
349 L->getExitBlocks(ExitBlocks);
350 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
351 ExitBlocks.end());
352 for (BasicBlock *ExitBlock : ExitBlockSet) {
353 if (any_of(predecessors(ExitBlock),
354 [L](BasicBlock *BB) { return !L->contains(BB); })) {
355 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
356 }
357 }
358
359 if (PreserveLCSSA) {
360 // Fix LCSSA form for L. Some values, which previously were only used inside
361 // L, can now be used in NewOuter loop. We need to insert phi-nodes for them
362 // in corresponding exit blocks.
363
364 // Go through all instructions in OuterLoopBlocks and check if they are
365 // using operands from the inner loop. In this case we'll need to fix LCSSA
366 // for these instructions.
367 SmallSetVector WorklistSet;
368 for (BasicBlock *OuterBB: OuterLoopBlocks) {
369 for (Instruction &I : *OuterBB) {
370 for (Value *Op : I.operands()) {
371 Instruction *OpI = dyn_cast(Op);
372 if (!OpI || !L->contains(OpI))
373 continue;
374 WorklistSet.insert(OpI);
375 }
376 }
377 }
378 SmallVector Worklist(WorklistSet.begin(),
379 WorklistSet.end());
380 formLCSSAForInstructions(Worklist, *DT, *LI);
381 assert(NewOuter->isRecursivelyLCSSAForm(*DT) &&
382 "LCSSA is broken after separating nested loops!");
383340 }
384341
385342 return NewOuter;
583540 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
584541 ExitBlocks.end());
585542 for (BasicBlock *ExitBlock : ExitBlockSet) {
586 if (any_of(predecessors(ExitBlock),
587 [L](BasicBlock *BB) { return !L->contains(BB); })) {
588 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
589 ++NumInserted;
590 Changed = true;
591 }
543 for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
544 PI != PE; ++PI)
545 // Must be exactly this loop: no subloops, parent loops, or non-loop preds
546 // allowed.
547 if (!L->contains(*PI)) {
548 if (rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA)) {
549 ++NumInserted;
550 Changed = true;
551 }
552 break;
553 }
592554 }
593555
594556 // If the header has more than two predecessors at this point (from the
+0
-87
test/Transforms/LCSSA/pr28424.ll less more
None ; RUN: opt < %s -lcssa -S -o - | FileCheck %s
1 target triple = "x86_64-unknown-linux-gnu"
2
3 ; PR28424
4 ; Here LCSSA adds phi-nodes for %x into the loop exits. Then, SSAUpdater needs
5 ; to insert phi-nodes to merge these values. That creates a new def, which in
6 ; its turn needs another LCCSA phi-node, and this test ensures that we insert
7 ; it.
8
9 ; CHECK-LABEL: @foo1
10 define internal i32 @foo1() {
11 entry:
12 br label %header
13
14 header:
15 %x = add i32 0, 1
16 br i1 undef, label %if, label %loopexit1
17
18 if:
19 br i1 undef, label %latch, label %loopexit2
20
21 latch:
22 br i1 undef, label %header, label %loopexit3
23
24 ; CHECK: loopexit1:
25 ; CHECK: %x.lcssa = phi i32 [ %x, %header ]
26 loopexit1:
27 br label %loop_with_insert_point
28
29 ; CHECK: loopexit2:
30 ; CHECK: %x.lcssa1 = phi i32 [ %x, %if ]
31 loopexit2:
32 br label %exit
33
34 ; CHECK: loopexit3:
35 ; CHECK: %x.lcssa2 = phi i32 [ %x, %latch ]
36 loopexit3:
37 br label %loop_with_insert_point
38
39 ; CHECK: loop_with_insert_point:
40 ; CHECK: %x4 = phi i32 [ %x4, %loop_with_insert_point ], [ %x.lcssa2, %loopexit3 ], [ %x.lcssa, %loopexit1 ]
41 loop_with_insert_point:
42 br i1 undef, label %loop_with_insert_point, label %bb
43
44 ; CHECK: bb:
45 ; CHECK: %x4.lcssa = phi i32 [ %x4, %loop_with_insert_point ]
46 bb:
47 br label %exit
48
49 ; CHECK: exit:
50 ; CHECK: %x3 = phi i32 [ %x4.lcssa, %bb ], [ %x.lcssa1, %loopexit2 ]
51 exit:
52 ret i32 %x
53 }
54
55 ; CHECK-LABEL: @foo2
56 define internal i32 @foo2() {
57 entry:
58 br label %header
59
60 header:
61 %x = add i32 0, 1
62 br i1 undef, label %latch, label %loopexit1
63
64 latch:
65 br i1 undef, label %header, label %loopexit2
66
67 ; CHECK: loopexit1:
68 ; CHECK: %x.lcssa = phi i32 [ %x, %header ]
69 loopexit1:
70 br label %loop_with_insert_point
71
72 ; CHECK: loopexit2:
73 ; CHECK: %x.lcssa1 = phi i32 [ %x, %latch ]
74 loopexit2:
75 br label %loop_with_insert_point
76
77 ; CHECK: loop_with_insert_point:
78 ; CHECK: %x2 = phi i32 [ %x2, %loop_with_insert_point ], [ %x.lcssa1, %loopexit2 ], [ %x.lcssa, %loopexit1 ]
79 loop_with_insert_point:
80 br i1 undef, label %loop_with_insert_point, label %exit
81
82 ; CHECK: exit:
83 ; CHECK: %x2.lcssa = phi i32 [ %x2, %loop_with_insert_point ]
84 exit:
85 ret i32 %x
86 }
+0
-76
test/Transforms/LoopSimplify/pr28272.ll less more
None ; RUN: opt < %s -lcssa -loop-unroll -S | FileCheck %s
1 target triple = "x86_64-unknown-linux-gnu"
2
3 ; PR28272
4 ; When LoopSimplify separates nested loops, it might break LCSSA form: values
5 ; from the original loop might be used in the outer loop. This test invokes
6 ; loop-unroll, which calls loop-simplify before itself. If LCSSA is broken
7 ; after loop-simplify, we crash on assertion.
8
9 ; CHECK-LABEL: @foo
10 define void @foo() {
11 entry:
12 br label %header
13
14 header:
15 br label %loop1
16
17 loop1:
18 br i1 true, label %loop1, label %bb43
19
20 bb43:
21 %a = phi i32 [ undef, %loop1 ], [ 0, %bb45 ], [ %a, %bb54 ]
22 %b = phi i32 [ 0, %loop1 ], [ 1, %bb54 ], [ %c, %bb45 ]
23 br i1 true, label %bb114, label %header
24
25 bb114:
26 %c = add i32 0, 1
27 %d = add i32 0, 1
28 br i1 true, label %bb45, label %bb54
29
30 bb45:
31 %x = add i32 %d, 0
32 br label %bb43
33
34 bb54:
35 br label %bb43
36 }
37
38 ; CHECK-LABEL: @foo2
39 define void @foo2() {
40 entry:
41 br label %outer
42
43 outer.loopexit:
44 br label %outer
45
46 outer:
47 br label %loop1
48
49 loop1:
50 br i1 true, label %loop1, label %loop2.preheader
51
52 loop2.preheader:
53 %a.ph = phi i32 [ undef, %loop1 ]
54 %b.ph = phi i32 [ 0, %loop1 ]
55 br label %loop2
56
57 loop2:
58 %a = phi i32 [ 0, %loop2.if.true ], [ %a, %loop2.if.false ], [ %a.ph, %loop2.preheader ], [0, %bb]
59 %b = phi i32 [ 1, %loop2.if.false ], [ %c, %loop2.if.true ], [ %b.ph, %loop2.preheader ], [%c, %bb]
60 br i1 true, label %loop2.if, label %outer.loopexit
61
62 loop2.if:
63 %c = add i32 0, 1
64 switch i32 undef, label %loop2.if.false [i32 0, label %loop2.if.true
65 i32 1, label %bb]
66
67 loop2.if.true:
68 br i1 undef, label %loop2, label %bb
69
70 loop2.if.false:
71 br label %loop2
72
73 bb:
74 br label %loop2
75 }