llvm.org GIT mirror llvm / 1bf3e6c
[LoopSimplify] Update LCSSA after separating nested loops. Summary: Usually LCSSA survives this transformation, but in some cases (see attached test) it doesn't: values from the original loop after separating might be used from the outer loop. Before the transformation it was the same loop, so LCSSA phis were not required. This fixes PR28272. Reviewers: sanjoy, hfinkel, chandlerc Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D21665 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275891 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Zolotukhin 3 years ago
2 changed file(s) with 126 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
326326 else
327327 NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));
328328
329 SmallVector OuterLoopBlocks;
330 OuterLoopBlocks.push_back(NewBB);
329331 // Now that we know which blocks are in L and which need to be moved to
330332 // OuterLoop, move any blocks that need it.
331333 for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
333335 if (!BlocksInL.count(BB)) {
334336 // Move this block to the parent, updating the exit blocks sets
335337 L->removeBlockFromLoop(BB);
336 if ((*LI)[BB] == L)
338 if ((*LI)[BB] == L) {
337339 LI->changeLoopFor(BB, NewOuter);
340 OuterLoopBlocks.push_back(BB);
341 }
338342 --i;
339343 }
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!");
340383 }
341384
342385 return NewOuter;
540583 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
541584 ExitBlocks.end());
542585 for (BasicBlock *ExitBlock : ExitBlockSet) {
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 }
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 }
554592 }
555593
556594 // If the header has more than two predecessors at this point (from the
0 ; 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 }