llvm.org GIT mirror llvm / 4b47096
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278173 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Zolotukhin 3 years ago
2 changed file(s) with 37 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
360360 // Fix LCSSA form for L. Some values, which previously were only used inside
361361 // L, can now be used in NewOuter loop. We need to insert phi-nodes for them
362362 // 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 // We also need to check exit blocks of the outer loop - it might be using
379 // values from what now became an inner loop.
380 SmallVector ExitBlocks;
381 NewOuter->getExitBlocks(ExitBlocks);
382 for (BasicBlock *ExitBB: ExitBlocks) {
383 for (Instruction &I : *ExitBB) {
384 for (Value *Op : I.operands()) {
385 Instruction *OpI = dyn_cast(Op);
386 if (!OpI || !L->contains(OpI))
387 continue;
388 WorklistSet.insert(OpI);
389 }
390 }
391 }
392
393 SmallVector Worklist(WorklistSet.begin(),
394 WorklistSet.end());
395 formLCSSAForInstructions(Worklist, *DT, *LI);
363 // We don't need to form LCSSA recursively, because there cannot be uses
364 // inside a newly created loop of defs from inner loops as those would
365 // already be a use of an LCSSA phi node.
366 formLCSSA(*L, *DT, LI, SE);
367
396368 assert(NewOuter->isRecursivelyLCSSAForm(*DT) &&
397369 "LCSSA is broken after separating nested loops!");
398370 }
None ; RUN: opt < %s -lcssa -loop-unroll -S | FileCheck %s
0 ; RUN: opt < %s -lcssa -loop-simplify -indvars -S | FileCheck %s
11 target triple = "x86_64-unknown-linux-gnu"
22
33 ; PR28272, PR28825
105105 %x = getelementptr i32, i32* %b
106106 br label %bb_end
107107 }
108
109 ; When LoopSimplify separates nested loops, it might break LCSSA form: values
110 ; from the original loop might occur in a loop, which is now a sibling of the
111 ; original loop (before separating it was a subloop of the original loop, and
112 ; thus didn't require an lcssa phi nodes).
113 ; CHECK-LABEL: @foo4
114 define void @foo4() {
115 bb1:
116 br label %bb2
117
118 ; CHECK: bb2.loopexit:
119 bb2.loopexit: ; preds = %bb3
120 %i.ph = phi i32 [ 0, %bb3 ]
121 br label %bb2
122
123 ; CHECK: bb2.outer:
124 ; CHECK: bb2:
125 bb2: ; preds = %bb2.loopexit, %bb2, %bb1
126 %i = phi i32 [ 0, %bb1 ], [ %i, %bb2 ], [ %i.ph, %bb2.loopexit ]
127 %x = load i32, i32* undef, align 8
128 br i1 undef, label %bb2, label %bb3.preheader
129
130 ; CHECK: bb3.preheader:
131 bb3.preheader: ; preds = %bb2
132 ; CHECK: %x.lcssa = phi i32 [ %x, %bb2 ]
133 br label %bb3
134
135 bb3: ; preds = %bb3.preheader, %bb3
136 %y = add i32 2, %x
137 br i1 true, label %bb2.loopexit, label %bb3
138 }