llvm.org GIT mirror
[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).
 48 48 /// If ScalarEvolution is passed in, it will be preserved. 49 49 /// 50 50 /// 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); 52 53 53 54 /// \brief Put a loop nest into LCSSA form. 54 55 /// 59 60 /// If ScalarEvolution is passed in, it will be preserved. 60 61 /// 61 62 /// 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,⏎ 63 64 ScalarEvolution *SE = nullptr); 64 65 } 65 66
 315 315 // SSAUpdater strategy during promotion that was LCSSA aware and reformed 316 316 // it as it went. 317 317 if (Changed) 318 formLCSSARecursively(*L, *DT, getAnalysisIfAvailable());⏎ 318 formLCSSARecursively(*L, *DT, LI,⏎ 319 getAnalysisIfAvailable()); 319 320 } 320 321 321 322 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
 60 60 /// uses. 61 61 static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT, 62 62 const SmallVectorImpl &ExitBlocks, 63 PredIteratorCache &PredCache) {⏎ 63 PredIteratorCache &PredCache, LoopInfo *LI) {⏎ 64 64 SmallVector UsesToRewrite; 65 65 66 66 BasicBlock *InstBB = Inst.getParent(); 93 93 DomTreeNode *DomNode = DT.getNode(DomBB); 94 94 95 95 SmallVector AddedPHIs; 96 SmallVector PostProcessPHIs; 96 97 97 98 SSAUpdater SSAUpdate; 98 99 SSAUpdate.Initialize(Inst.getType(), Inst.getName()); 130 131 131 132 // Remember that this phi makes the value alive in this block. 132 133 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); 133 146 } 134 147 135 148 // Rewrite all uses outside the loop in terms of the new PHIs we just 156 169 SSAUpdate.RewriteUse(*UsesToRewrite[i]); 157 170 } 158 171 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 159 191 // Remove PHI nodes that did not have any uses rewritten. 160 192 for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) { 161 193 if (AddedPHIs[i]->use_empty()) 179 211 return false; 180 212 } 181 213 182 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE) {⏎ 214 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,⏎ 215 ScalarEvolution *SE) { 183 216 bool Changed = false; 184 217 185 218 // Get the set of exiting blocks. 211 244 !isa(I->user_back()))) 212 245 continue; 213 246 214 Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache);⏎ 247 Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache, LI);⏎ 215 248 } 216 249 } 217 250 227 260 } 228 261 229 262 /// Process a loop nest depth first. 230 bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT,⏎ 263 bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,⏎ 231 264 ScalarEvolution *SE) { 232 265 bool Changed = false; 233 266 234 267 // 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); 239 272 return Changed; 240 273 } 241 274 290 323 291 324 // Simplify each loop nest in the function. 292 325 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);⏎ 294 327 295 328 return Changed; 296 329 }
 543 543 while (OuterL->getParentLoop() != LatchLoop) 544 544 OuterL = OuterL->getParentLoop(); 545 545 546 formLCSSARecursively(*OuterL, *DT, SE);⏎ 546 formLCSSARecursively(*OuterL, *DT, LI, SE);⏎ 547 547 } 548 548 } 549 549
 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⏎ 2 1 3 2 ; LCSSA should work correctly in the case of an indirectbr that exits 4 3 ; the loop, and the loop has exits with predecessors not within the loop 5 4 ; (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 8 8 entry: 9 9 br i1 undef, label %"4", label %"3" 10 10 539 539 "1862": ; preds = %"1836", %"692" 540 540 unreachable 541 541 } 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 }