1 //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Analysis/CGSCCPassManager.h" 11 #include "llvm/IR/CallSite.h" 12 #include "llvm/IR/InstIterator.h" 13 14 #define DEBUG_TYPE "cgscc" 15 16 using namespace llvm; 17 18 // Explicit template instantiations and specialization defininitions for core 19 // template typedefs. 20 namespace llvm { 21 22 // Explicit instantiations for the core proxy templates. 23 template class AllAnalysesOn<LazyCallGraph::SCC>; 24 template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; 25 template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, 26 LazyCallGraph &, CGSCCUpdateResult &>; 27 template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; 28 template class OuterAnalysisManagerProxy<ModuleAnalysisManager, 29 LazyCallGraph::SCC, LazyCallGraph &>; 30 template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; 31 32 /// Explicitly specialize the pass manager run method to handle call graph 33 /// updates. 34 template <> 35 PreservedAnalyses 36 PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, 37 CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC, 38 CGSCCAnalysisManager &AM, 39 LazyCallGraph &G, CGSCCUpdateResult &UR) { 40 PreservedAnalyses PA = PreservedAnalyses::all(); 41 42 if (DebugLogging) 43 dbgs() << "Starting CGSCC pass manager run.\n"; 44 45 // The SCC may be refined while we are running passes over it, so set up 46 // a pointer that we can update. 47 LazyCallGraph::SCC *C = &InitialC; 48 49 for (auto &Pass : Passes) { 50 if (DebugLogging) 51 dbgs() << "Running pass: " << Pass->name() << " on " << *C << "\n"; 52 53 PreservedAnalyses PassPA = Pass->run(*C, AM, G, UR); 54 55 // Update the SCC if necessary. 56 C = UR.UpdatedC ? UR.UpdatedC : C; 57 58 // Check that we didn't miss any update scenario. 59 assert(!UR.InvalidatedSCCs.count(C) && "Processing an invalid SCC!"); 60 assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 61 62 // Update the analysis manager as each pass runs and potentially 63 // invalidates analyses. 64 AM.invalidate(*C, PassPA); 65 66 // Finally, we intersect the final preserved analyses to compute the 67 // aggregate preserved set for this pass manager. 68 PA.intersect(std::move(PassPA)); 69 70 // FIXME: Historically, the pass managers all called the LLVM context's 71 // yield function here. We don't have a generic way to acquire the 72 // context and it isn't yet clear what the right pattern is for yielding 73 // in the new pass manager so it is currently omitted. 74 // ...getContext().yield(); 75 } 76 77 // Invaliadtion was handled after each pass in the above loop for the current 78 // SCC. Therefore, the remaining analysis results in the AnalysisManager are 79 // preserved. We mark this with a set so that we don't need to inspect each 80 // one individually. 81 PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); 82 83 if (DebugLogging) 84 dbgs() << "Finished CGSCC pass manager run.\n"; 85 86 return PA; 87 } 88 89 bool CGSCCAnalysisManagerModuleProxy::Result::invalidate( 90 Module &M, const PreservedAnalyses &PA, 91 ModuleAnalysisManager::Invalidator &Inv) { 92 // If literally everything is preserved, we're done. 93 if (PA.areAllPreserved()) 94 return false; // This is still a valid proxy. 95 96 // If this proxy or the call graph is going to be invalidated, we also need 97 // to clear all the keys coming from that analysis. 98 // 99 // We also directly invalidate the FAM's module proxy if necessary, and if 100 // that proxy isn't preserved we can't preserve this proxy either. We rely on 101 // it to handle module -> function analysis invalidation in the face of 102 // structural changes and so if it's unavailable we conservatively clear the 103 // entire SCC layer as well rather than trying to do invalidation ourselves. 104 auto PAC = PA.getChecker<CGSCCAnalysisManagerModuleProxy>(); 105 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>()) || 106 Inv.invalidate<LazyCallGraphAnalysis>(M, PA) || 107 Inv.invalidate<FunctionAnalysisManagerModuleProxy>(M, PA)) { 108 InnerAM->clear(); 109 110 // And the proxy itself should be marked as invalid so that we can observe 111 // the new call graph. This isn't strictly necessary because we cheat 112 // above, but is still useful. 113 return true; 114 } 115 116 // Directly check if the relevant set is preserved so we can short circuit 117 // invalidating SCCs below. 118 bool AreSCCAnalysesPreserved = 119 PA.allAnalysesInSetPreserved<AllAnalysesOn<LazyCallGraph::SCC>>(); 120 121 // Ok, we have a graph, so we can propagate the invalidation down into it. 122 G->buildRefSCCs(); 123 for (auto &RC : G->postorder_ref_sccs()) 124 for (auto &C : RC) { 125 Optional<PreservedAnalyses> InnerPA; 126 127 // Check to see whether the preserved set needs to be adjusted based on 128 // module-level analysis invalidation triggering deferred invalidation 129 // for this SCC. 130 if (auto *OuterProxy = 131 InnerAM->getCachedResult<ModuleAnalysisManagerCGSCCProxy>(C)) 132 for (const auto &OuterInvalidationPair : 133 OuterProxy->getOuterInvalidations()) { 134 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 135 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 136 if (Inv.invalidate(OuterAnalysisID, M, PA)) { 137 if (!InnerPA) 138 InnerPA = PA; 139 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 140 InnerPA->abandon(InnerAnalysisID); 141 } 142 } 143 144 // Check if we needed a custom PA set. If so we'll need to run the inner 145 // invalidation. 146 if (InnerPA) { 147 InnerAM->invalidate(C, *InnerPA); 148 continue; 149 } 150 151 // Otherwise we only need to do invalidation if the original PA set didn't 152 // preserve all SCC analyses. 153 if (!AreSCCAnalysesPreserved) 154 InnerAM->invalidate(C, PA); 155 } 156 157 // Return false to indicate that this result is still a valid proxy. 158 return false; 159 } 160 161 template <> 162 CGSCCAnalysisManagerModuleProxy::Result 163 CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM) { 164 // Force the Function analysis manager to also be available so that it can 165 // be accessed in an SCC analysis and proxied onward to function passes. 166 // FIXME: It is pretty awkward to just drop the result here and assert that 167 // we can find it again later. 168 (void)AM.getResult<FunctionAnalysisManagerModuleProxy>(M); 169 170 return Result(*InnerAM, AM.getResult<LazyCallGraphAnalysis>(M)); 171 } 172 173 AnalysisKey FunctionAnalysisManagerCGSCCProxy::Key; 174 175 FunctionAnalysisManagerCGSCCProxy::Result 176 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C, 177 CGSCCAnalysisManager &AM, 178 LazyCallGraph &CG) { 179 // Collect the FunctionAnalysisManager from the Module layer and use that to 180 // build the proxy result. 181 // 182 // This allows us to rely on the FunctionAnalysisMangaerModuleProxy to 183 // invalidate the function analyses. 184 auto &MAM = AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG).getManager(); 185 Module &M = *C.begin()->getFunction().getParent(); 186 auto *FAMProxy = MAM.getCachedResult<FunctionAnalysisManagerModuleProxy>(M); 187 assert(FAMProxy && "The CGSCC pass manager requires that the FAM module " 188 "proxy is run on the module prior to entering the CGSCC " 189 "walk."); 190 191 // Note that we special-case invalidation handling of this proxy in the CGSCC 192 // analysis manager's Module proxy. This avoids the need to do anything 193 // special here to recompute all of this if ever the FAM's module proxy goes 194 // away. 195 return Result(FAMProxy->getManager()); 196 } 197 198 bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate( 199 LazyCallGraph::SCC &C, const PreservedAnalyses &PA, 200 CGSCCAnalysisManager::Invalidator &Inv) { 201 // If literally everything is preserved, we're done. 202 if (PA.areAllPreserved()) 203 return false; // This is still a valid proxy. 204 205 // If this proxy isn't marked as preserved, then even if the result remains 206 // valid, the key itself may no longer be valid, so we clear everything. 207 // 208 // Note that in order to preserve this proxy, a module pass must ensure that 209 // the FAM has been completely updated to handle the deletion of functions. 210 // Specifically, any FAM-cached results for those functions need to have been 211 // forcibly cleared. When preserved, this proxy will only invalidate results 212 // cached on functions *still in the module* at the end of the module pass. 213 auto PAC = PA.getChecker<FunctionAnalysisManagerCGSCCProxy>(); 214 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<LazyCallGraph::SCC>>()) { 215 for (LazyCallGraph::Node &N : C) 216 FAM->clear(N.getFunction()); 217 218 return true; 219 } 220 221 // Directly check if the relevant set is preserved. 222 bool AreFunctionAnalysesPreserved = 223 PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); 224 225 // Now walk all the functions to see if any inner analysis invalidation is 226 // necessary. 227 for (LazyCallGraph::Node &N : C) { 228 Function &F = N.getFunction(); 229 Optional<PreservedAnalyses> FunctionPA; 230 231 // Check to see whether the preserved set needs to be pruned based on 232 // SCC-level analysis invalidation that triggers deferred invalidation 233 // registered with the outer analysis manager proxy for this function. 234 if (auto *OuterProxy = 235 FAM->getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F)) 236 for (const auto &OuterInvalidationPair : 237 OuterProxy->getOuterInvalidations()) { 238 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 239 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 240 if (Inv.invalidate(OuterAnalysisID, C, PA)) { 241 if (!FunctionPA) 242 FunctionPA = PA; 243 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 244 FunctionPA->abandon(InnerAnalysisID); 245 } 246 } 247 248 // Check if we needed a custom PA set, and if so we'll need to run the 249 // inner invalidation. 250 if (FunctionPA) { 251 FAM->invalidate(F, *FunctionPA); 252 continue; 253 } 254 255 // Otherwise we only need to do invalidation if the original PA set didn't 256 // preserve all function analyses. 257 if (!AreFunctionAnalysesPreserved) 258 FAM->invalidate(F, PA); 259 } 260 261 // Return false to indicate that this result is still a valid proxy. 262 return false; 263 } 264 265 } // End llvm namespace 266 267 /// When a new SCC is created for the graph and there might be function 268 /// analysis results cached for the functions now in that SCC two forms of 269 /// updates are required. 270 /// 271 /// First, a proxy from the SCC to the FunctionAnalysisManager needs to be 272 /// created so that any subsequent invalidation events to the SCC are 273 /// propagated to the function analysis results cached for functions within it. 274 /// 275 /// Second, if any of the functions within the SCC have analysis results with 276 /// outer analysis dependencies, then those dependencies would point to the 277 /// *wrong* SCC's analysis result. We forcibly invalidate the necessary 278 /// function analyses so that they don't retain stale handles. 279 static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C, 280 LazyCallGraph &G, 281 CGSCCAnalysisManager &AM) { 282 // Get the relevant function analysis manager. 283 auto &FAM = 284 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, G).getManager(); 285 286 // Now walk the functions in this SCC and invalidate any function analysis 287 // results that might have outer dependencies on an SCC analysis. 288 for (LazyCallGraph::Node &N : C) { 289 Function &F = N.getFunction(); 290 291 auto *OuterProxy = 292 FAM.getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F); 293 if (!OuterProxy) 294 // No outer analyses were queried, nothing to do. 295 continue; 296 297 // Forcibly abandon all the inner analyses with dependencies, but 298 // invalidate nothing else. 299 auto PA = PreservedAnalyses::all(); 300 for (const auto &OuterInvalidationPair : 301 OuterProxy->getOuterInvalidations()) { 302 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 303 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 304 PA.abandon(InnerAnalysisID); 305 } 306 307 // Now invalidate anything we found. 308 FAM.invalidate(F, PA); 309 } 310 } 311 312 namespace { 313 /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c 314 /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly 315 /// added SCCs. 316 /// 317 /// The range of new SCCs must be in postorder already. The SCC they were split 318 /// out of must be provided as \p C. The current node being mutated and 319 /// triggering updates must be passed as \p N. 320 /// 321 /// This function returns the SCC containing \p N. This will be either \p C if 322 /// no new SCCs have been split out, or it will be the new SCC containing \p N. 323 template <typename SCCRangeT> 324 LazyCallGraph::SCC * 325 incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, 326 LazyCallGraph::Node &N, LazyCallGraph::SCC *C, 327 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { 328 typedef LazyCallGraph::SCC SCC; 329 330 if (NewSCCRange.begin() == NewSCCRange.end()) 331 return C; 332 333 // Add the current SCC to the worklist as its shape has changed. 334 UR.CWorklist.insert(C); 335 DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist:" << *C << "\n"); 336 337 SCC *OldC = C; 338 339 // Update the current SCC. Note that if we have new SCCs, this must actually 340 // change the SCC. 341 assert(C != &*NewSCCRange.begin() && 342 "Cannot insert new SCCs without changing current SCC!"); 343 C = &*NewSCCRange.begin(); 344 assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 345 346 // If we had a cached FAM proxy originally, we will want to create more of 347 // them for each SCC that was split off. 348 bool NeedFAMProxy = 349 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*OldC) != nullptr; 350 351 // We need to propagate an invalidation call to all but the newly current SCC 352 // because the outer pass manager won't do that for us after splitting them. 353 // FIXME: We should accept a PreservedAnalysis from the CG updater so that if 354 // there are preserved ananalyses we can avoid invalidating them here for 355 // split-off SCCs. 356 // We know however that this will preserve any FAM proxy so go ahead and mark 357 // that. 358 PreservedAnalyses PA; 359 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 360 AM.invalidate(*OldC, PA); 361 362 // Ensure the now-current SCC's function analyses are updated. 363 if (NeedFAMProxy) 364 updateNewSCCFunctionAnalyses(*C, G, AM); 365 366 for (SCC &NewC : 367 reverse(make_range(std::next(NewSCCRange.begin()), NewSCCRange.end()))) { 368 assert(C != &NewC && "No need to re-visit the current SCC!"); 369 assert(OldC != &NewC && "Already handled the original SCC!"); 370 UR.CWorklist.insert(&NewC); 371 DEBUG(dbgs() << "Enqueuing a newly formed SCC:" << NewC << "\n"); 372 373 // Ensure new SCCs' function analyses are updated. 374 if (NeedFAMProxy) 375 updateNewSCCFunctionAnalyses(NewC, G, AM); 376 377 // Also propagate a normal invalidation to the new SCC as only the current 378 // will get one from the pass manager infrastructure. 379 AM.invalidate(NewC, PA); 380 } 381 return C; 382 } 383 } 384 385 LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( 386 LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 387 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { 388 typedef LazyCallGraph::Node Node; 389 typedef LazyCallGraph::Edge Edge; 390 typedef LazyCallGraph::SCC SCC; 391 typedef LazyCallGraph::RefSCC RefSCC; 392 393 RefSCC &InitialRC = InitialC.getOuterRefSCC(); 394 SCC *C = &InitialC; 395 RefSCC *RC = &InitialRC; 396 Function &F = N.getFunction(); 397 398 // Walk the function body and build up the set of retained, promoted, and 399 // demoted edges. 400 SmallVector<Constant *, 16> Worklist; 401 SmallPtrSet<Constant *, 16> Visited; 402 SmallPtrSet<Node *, 16> RetainedEdges; 403 SmallSetVector<Node *, 4> PromotedRefTargets; 404 SmallSetVector<Node *, 4> DemotedCallTargets; 405 406 // First walk the function and handle all called functions. We do this first 407 // because if there is a single call edge, whether there are ref edges is 408 // irrelevant. 409 for (Instruction &I : instructions(F)) 410 if (auto CS = CallSite(&I)) 411 if (Function *Callee = CS.getCalledFunction()) 412 if (Visited.insert(Callee).second && !Callee->isDeclaration()) { 413 Node &CalleeN = *G.lookup(*Callee); 414 Edge *E = N->lookup(CalleeN); 415 // FIXME: We should really handle adding new calls. While it will 416 // make downstream usage more complex, there is no fundamental 417 // limitation and it will allow passes within the CGSCC to be a bit 418 // more flexible in what transforms they can do. Until then, we 419 // verify that new calls haven't been introduced. 420 assert(E && "No function transformations should introduce *new* " 421 "call edges! Any new calls should be modeled as " 422 "promoted existing ref edges!"); 423 bool Inserted = RetainedEdges.insert(&CalleeN).second; 424 (void)Inserted; 425 assert(Inserted && "We should never visit a function twice."); 426 if (!E->isCall()) 427 PromotedRefTargets.insert(&CalleeN); 428 } 429 430 // Now walk all references. 431 for (Instruction &I : instructions(F)) 432 for (Value *Op : I.operand_values()) 433 if (Constant *C = dyn_cast<Constant>(Op)) 434 if (Visited.insert(C).second) 435 Worklist.push_back(C); 436 437 auto VisitRef = [&](Function &Referee) { 438 Node &RefereeN = *G.lookup(Referee); 439 Edge *E = N->lookup(RefereeN); 440 // FIXME: Similarly to new calls, we also currently preclude 441 // introducing new references. See above for details. 442 assert(E && "No function transformations should introduce *new* ref " 443 "edges! Any new ref edges would require IPO which " 444 "function passes aren't allowed to do!"); 445 bool Inserted = RetainedEdges.insert(&RefereeN).second; 446 (void)Inserted; 447 assert(Inserted && "We should never visit a function twice."); 448 if (E->isCall()) 449 DemotedCallTargets.insert(&RefereeN); 450 }; 451 LazyCallGraph::visitReferences(Worklist, Visited, VisitRef); 452 453 // Include synthetic reference edges to known, defined lib functions. 454 for (auto *F : G.getLibFunctions()) 455 // While the list of lib functions doesn't have repeats, don't re-visit 456 // anything handled above. 457 if (!Visited.count(F)) 458 VisitRef(*F); 459 460 // First remove all of the edges that are no longer present in this function. 461 // The first step makes these edges uniformly ref edges and accumulates them 462 // into a separate data structure so removal doesn't invalidate anything. 463 SmallVector<Node *, 4> DeadTargets; 464 for (Edge &E : *N) { 465 if (RetainedEdges.count(&E.getNode())) 466 continue; 467 468 SCC &TargetC = *G.lookupSCC(E.getNode()); 469 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 470 if (&TargetRC == RC && E.isCall()) { 471 if (C != &TargetC) { 472 // For separate SCCs this is trivial. 473 RC->switchTrivialInternalEdgeToRef(N, E.getNode()); 474 } else { 475 // Now update the call graph. 476 C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, E.getNode()), 477 G, N, C, AM, UR); 478 } 479 } 480 481 // Now that this is ready for actual removal, put it into our list. 482 DeadTargets.push_back(&E.getNode()); 483 } 484 // Remove the easy cases quickly and actually pull them out of our list. 485 DeadTargets.erase( 486 llvm::remove_if(DeadTargets, 487 [&](Node *TargetN) { 488 SCC &TargetC = *G.lookupSCC(*TargetN); 489 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 490 491 // We can't trivially remove internal targets, so skip 492 // those. 493 if (&TargetRC == RC) 494 return false; 495 496 RC->removeOutgoingEdge(N, *TargetN); 497 DEBUG(dbgs() << "Deleting outgoing edge from '" << N 498 << "' to '" << TargetN << "'\n"); 499 return true; 500 }), 501 DeadTargets.end()); 502 503 // Now do a batch removal of the internal ref edges left. 504 auto NewRefSCCs = RC->removeInternalRefEdge(N, DeadTargets); 505 if (!NewRefSCCs.empty()) { 506 // The old RefSCC is dead, mark it as such. 507 UR.InvalidatedRefSCCs.insert(RC); 508 509 // Note that we don't bother to invalidate analyses as ref-edge 510 // connectivity is not really observable in any way and is intended 511 // exclusively to be used for ordering of transforms rather than for 512 // analysis conclusions. 513 514 // Update RC to the "bottom". 515 assert(G.lookupSCC(N) == C && "Changed the SCC when splitting RefSCCs!"); 516 RC = &C->getOuterRefSCC(); 517 assert(G.lookupRefSCC(N) == RC && "Failed to update current RefSCC!"); 518 519 // The RC worklist is in reverse postorder, so we enqueue the new ones in 520 // RPO except for the one which contains the source node as that is the 521 // "bottom" we will continue processing in the bottom-up walk. 522 assert(NewRefSCCs.front() == RC && 523 "New current RefSCC not first in the returned list!"); 524 for (RefSCC *NewRC : 525 reverse(make_range(std::next(NewRefSCCs.begin()), NewRefSCCs.end()))) { 526 assert(NewRC != RC && "Should not encounter the current RefSCC further " 527 "in the postorder list of new RefSCCs."); 528 UR.RCWorklist.insert(NewRC); 529 DEBUG(dbgs() << "Enqueuing a new RefSCC in the update worklist: " 530 << *NewRC << "\n"); 531 } 532 } 533 534 // Next demote all the call edges that are now ref edges. This helps make 535 // the SCCs small which should minimize the work below as we don't want to 536 // form cycles that this would break. 537 for (Node *RefTarget : DemotedCallTargets) { 538 SCC &TargetC = *G.lookupSCC(*RefTarget); 539 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 540 541 // The easy case is when the target RefSCC is not this RefSCC. This is 542 // only supported when the target RefSCC is a child of this RefSCC. 543 if (&TargetRC != RC) { 544 assert(RC->isAncestorOf(TargetRC) && 545 "Cannot potentially form RefSCC cycles here!"); 546 RC->switchOutgoingEdgeToRef(N, *RefTarget); 547 DEBUG(dbgs() << "Switch outgoing call edge to a ref edge from '" << N 548 << "' to '" << *RefTarget << "'\n"); 549 continue; 550 } 551 552 // We are switching an internal call edge to a ref edge. This may split up 553 // some SCCs. 554 if (C != &TargetC) { 555 // For separate SCCs this is trivial. 556 RC->switchTrivialInternalEdgeToRef(N, *RefTarget); 557 continue; 558 } 559 560 // Now update the call graph. 561 C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, *RefTarget), G, N, 562 C, AM, UR); 563 } 564 565 // Now promote ref edges into call edges. 566 for (Node *CallTarget : PromotedRefTargets) { 567 SCC &TargetC = *G.lookupSCC(*CallTarget); 568 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 569 570 // The easy case is when the target RefSCC is not this RefSCC. This is 571 // only supported when the target RefSCC is a child of this RefSCC. 572 if (&TargetRC != RC) { 573 assert(RC->isAncestorOf(TargetRC) && 574 "Cannot potentially form RefSCC cycles here!"); 575 RC->switchOutgoingEdgeToCall(N, *CallTarget); 576 DEBUG(dbgs() << "Switch outgoing ref edge to a call edge from '" << N 577 << "' to '" << *CallTarget << "'\n"); 578 continue; 579 } 580 DEBUG(dbgs() << "Switch an internal ref edge to a call edge from '" << N 581 << "' to '" << *CallTarget << "'\n"); 582 583 // Otherwise we are switching an internal ref edge to a call edge. This 584 // may merge away some SCCs, and we add those to the UpdateResult. We also 585 // need to make sure to update the worklist in the event SCCs have moved 586 // before the current one in the post-order sequence 587 bool HasFunctionAnalysisProxy = false; 588 auto InitialSCCIndex = RC->find(*C) - RC->begin(); 589 bool FormedCycle = RC->switchInternalEdgeToCall( 590 N, *CallTarget, [&](ArrayRef<SCC *> MergedSCCs) { 591 for (SCC *MergedC : MergedSCCs) { 592 assert(MergedC != &TargetC && "Cannot merge away the target SCC!"); 593 594 HasFunctionAnalysisProxy |= 595 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>( 596 *MergedC) != nullptr; 597 598 // Mark that this SCC will no longer be valid. 599 UR.InvalidatedSCCs.insert(MergedC); 600 601 // FIXME: We should really do a 'clear' here to forcibly release 602 // memory, but we don't have a good way of doing that and 603 // preserving the function analyses. 604 auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 605 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 606 AM.invalidate(*MergedC, PA); 607 } 608 }); 609 610 // If we formed a cycle by creating this call, we need to update more data 611 // structures. 612 if (FormedCycle) { 613 C = &TargetC; 614 assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 615 616 // If one of the invalidated SCCs had a cached proxy to a function 617 // analysis manager, we need to create a proxy in the new current SCC as 618 // the invaliadted SCCs had their functions moved. 619 if (HasFunctionAnalysisProxy) 620 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G); 621 622 // Any analyses cached for this SCC are no longer precise as the shape 623 // has changed by introducing this cycle. However, we have taken care to 624 // update the proxies so it remains valide. 625 auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 626 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 627 AM.invalidate(*C, PA); 628 } 629 auto NewSCCIndex = RC->find(*C) - RC->begin(); 630 // If we have actually moved an SCC to be topologically "below" the current 631 // one due to merging, we will need to revisit the current SCC after 632 // visiting those moved SCCs. 633 // 634 // It is critical that we *do not* revisit the current SCC unless we 635 // actually move SCCs in the process of merging because otherwise we may 636 // form a cycle where an SCC is split apart, merged, split, merged and so 637 // on infinitely. 638 if (InitialSCCIndex < NewSCCIndex) { 639 // Put our current SCC back onto the worklist as we'll visit other SCCs 640 // that are now definitively ordered prior to the current one in the 641 // post-order sequence, and may end up observing more precise context to 642 // optimize the current SCC. 643 UR.CWorklist.insert(C); 644 DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist: " << *C 645 << "\n"); 646 // Enqueue in reverse order as we pop off the back of the worklist. 647 for (SCC &MovedC : reverse(make_range(RC->begin() + InitialSCCIndex, 648 RC->begin() + NewSCCIndex))) { 649 UR.CWorklist.insert(&MovedC); 650 DEBUG(dbgs() << "Enqueuing a newly earlier in post-order SCC: " 651 << MovedC << "\n"); 652 } 653 } 654 } 655 656 assert(!UR.InvalidatedSCCs.count(C) && "Invalidated the current SCC!"); 657 assert(!UR.InvalidatedRefSCCs.count(RC) && "Invalidated the current RefSCC!"); 658 assert(&C->getOuterRefSCC() == RC && "Current SCC not in current RefSCC!"); 659 660 // Record the current RefSCC and SCC for higher layers of the CGSCC pass 661 // manager now that all the updates have been applied. 662 if (RC != &InitialRC) 663 UR.UpdatedRC = RC; 664 if (C != &InitialC) 665 UR.UpdatedC = C; 666 667 return *C; 668 } 669