llvm.org GIT mirror llvm / a559a23
[LCSSA] Handle PHI insertion in disjoint loops Take two disjoint Loops L1 and L2. LoopSimplify fails to simplify some loops (e.g. when indirect branches are involved). In such situations, it can happen that an exit for L1 is the header of L2. Thus, when we create PHIs in one of such exits we are also inserting PHIs in L2 header. This could break LCSSA form for L2 because these inserted PHIs can also have uses in L2 exits, which are never handled in the current implementation. Provide a fix for this corner case and test that we don't assert/crash on that. Differential Revision: http://reviews.llvm.org/D6624 rdar://problem/19166231 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224740 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 4 years ago
5 changed file(s) with 84 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
4848 /// If ScalarEvolution is passed in, it will be preserved.
4949 ///
5050 /// Returns true if any modifications are made to the loop.
51 bool formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE = nullptr);
51 bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
52 ScalarEvolution *SE = nullptr);
5253
5354 /// \brief Put a loop nest into LCSSA form.
5455 ///
5960 /// If ScalarEvolution is passed in, it will be preserved.
6061 ///
6162 /// Returns true if any modifications are made to the loop.
62 bool formLCSSARecursively(Loop &L, DominatorTree &DT,
63 bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
6364 ScalarEvolution *SE = nullptr);
6465 }
6566
315315 // SSAUpdater strategy during promotion that was LCSSA aware and reformed
316316 // it as it went.
317317 if (Changed)
318 formLCSSARecursively(*L, *DT, getAnalysisIfAvailable());
318 formLCSSARecursively(*L, *DT, LI,
319 getAnalysisIfAvailable());
319320 }
320321
321322 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
6060 /// uses.
6161 static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
6262 const SmallVectorImpl &ExitBlocks,
63 PredIteratorCache &PredCache) {
63 PredIteratorCache &PredCache, LoopInfo *LI) {
6464 SmallVector UsesToRewrite;
6565
6666 BasicBlock *InstBB = Inst.getParent();
9393 DomTreeNode *DomNode = DT.getNode(DomBB);
9494
9595 SmallVector AddedPHIs;
96 SmallVector PostProcessPHIs;
9697
9798 SSAUpdater SSAUpdate;
9899 SSAUpdate.Initialize(Inst.getType(), Inst.getName());
130131
131132 // Remember that this phi makes the value alive in this block.
132133 SSAUpdate.AddAvailableValue(ExitBB, PN);
134
135 // LoopSimplify might fail to simplify some loops (e.g. when indirect
136 // branches are involved). In such situations, it might happen that an exit
137 // for Loop L1 is the header of a disjoint Loop L2. Thus, when we create
138 // PHIs in such an exit block, we are also inserting PHIs into L2's header.
139 // This could break LCSSA form for L2 because these inserted PHIs can also
140 // have uses outside of L2. Remember all PHIs in such situation as to
141 // revisit than later on. FIXME: Remove this if indirectbr support into
142 // LoopSimplify gets improved.
143 if (auto *OtherLoop = LI->getLoopFor(ExitBB))
144 if (!L.contains(OtherLoop))
145 PostProcessPHIs.push_back(PN);
133146 }
134147
135148 // Rewrite all uses outside the loop in terms of the new PHIs we just
156169 SSAUpdate.RewriteUse(*UsesToRewrite[i]);
157170 }
158171
172 // Post process PHI instructions that were inserted into another disjoint loop
173 // and update their exits properly.
174 for (auto *I : PostProcessPHIs) {
175 if (I->use_empty())
176 continue;
177
178 BasicBlock *PHIBB = I->getParent();
179 Loop *OtherLoop = LI->getLoopFor(PHIBB);
180 SmallVector EBs;
181 OtherLoop->getExitBlocks(EBs);
182 if (EBs.empty())
183 continue;
184
185 // Recurse and re-process each PHI instruction. FIXME: we should really
186 // convert this entire thing to a worklist approach where we process a
187 // vector of instructions...
188 processInstruction(*OtherLoop, *I, DT, EBs, PredCache, LI);
189 }
190
159191 // Remove PHI nodes that did not have any uses rewritten.
160192 for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
161193 if (AddedPHIs[i]->use_empty())
179211 return false;
180212 }
181213
182 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE) {
214 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
215 ScalarEvolution *SE) {
183216 bool Changed = false;
184217
185218 // Get the set of exiting blocks.
211244 !isa(I->user_back())))
212245 continue;
213246
214 Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache);
247 Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache, LI);
215248 }
216249 }
217250
227260 }
228261
229262 /// Process a loop nest depth first.
230 bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT,
263 bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
231264 ScalarEvolution *SE) {
232265 bool Changed = false;
233266
234267 // Recurse depth-first through inner loops.
235 for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
236 Changed |= formLCSSARecursively(**LI, DT, SE);
237
238 Changed |= formLCSSA(L, DT, SE);
268 for (Loop::iterator I = L.begin(), E = L.end(); I != E; ++I)
269 Changed |= formLCSSARecursively(**I, DT, LI, SE);
270
271 Changed |= formLCSSA(L, DT, LI, SE);
239272 return Changed;
240273 }
241274
290323
291324 // Simplify each loop nest in the function.
292325 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
293 Changed |= formLCSSARecursively(**I, *DT, SE);
326 Changed |= formLCSSARecursively(**I, *DT, LI, SE);
294327
295328 return Changed;
296329 }
543543 while (OuterL->getParentLoop() != LatchLoop)
544544 OuterL = OuterL->getParentLoop();
545545
546 formLCSSARecursively(*OuterL, *DT, SE);
546 formLCSSARecursively(*OuterL, *DT, LI, SE);
547547 }
548548 }
549549
None ; RUN: opt < %s -lcssa -verify-loop-info -verify-dom-info -disable-output
1 ; PR5437
0 ; RUN: opt < %s -loop-simplify -lcssa -verify-loop-info -verify-dom-info -S | FileCheck %s
21
32 ; LCSSA should work correctly in the case of an indirectbr that exits
43 ; the loop, and the loop has exits with predecessors not within the loop
54 ; (and btw these edges are unsplittable due to the indirectbr).
6
7 define i32 @js_Interpret() nounwind {
5 ; PR5437
6 define i32 @test0() nounwind {
7 ; CHECK-LABEL: @test0
88 entry:
99 br i1 undef, label %"4", label %"3"
1010
539539 "1862": ; preds = %"1836", %"692"
540540 unreachable
541541 }
542
543 ; An exit for Loop L1 may be the header of a disjoint Loop L2. Thus, when we
544 ; create PHIs in one of such exits we are also inserting PHIs in L2 header. This
545 ; could break LCSSA form for L2 because these inserted PHIs can also have uses
546 ; in L2 exits. Test that we don't assert/crash on that.
547 define void @test1() {
548 ; CHECK-LABEL: @test1
549 br label %lab1
550
551 lab1:
552 %tmp21 = add i32 undef, 677038203
553 br i1 undef, label %lab2, label %exit
554
555 lab2:
556 indirectbr i8* undef, [label %lab1, label %lab3]
557
558 lab3:
559 ; CHECK: %tmp21.lcssa1 = phi i32 [ %tmp21.lcssa1, %lab4 ], [ %tmp21, %lab2 ]
560 %tmp12 = phi i32 [ %tmp21, %lab2 ], [ %tmp12, %lab4 ]
561 br i1 undef, label %lab5, label %lab4
562
563 lab4:
564 br label %lab3
565
566 lab5:
567 ; CHECK: %tmp21.lcssa1.lcssa = phi i32 [ %tmp21.lcssa1, %lab3 ]
568 %tmp15 = add i32 %tmp12, undef
569 br label %exit
570
571 exit:
572 ret void
573 }