1 //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/CGSCCPassManager.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/Optional.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/SetVector.h" 14 #include "llvm/ADT/SmallPtrSet.h" 15 #include "llvm/ADT/SmallVector.h" 16 #include "llvm/ADT/iterator_range.h" 17 #include "llvm/Analysis/LazyCallGraph.h" 18 #include "llvm/IR/Constant.h" 19 #include "llvm/IR/InstIterator.h" 20 #include "llvm/IR/Instruction.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/IR/PassManagerImpl.h" 23 #include "llvm/IR/ValueHandle.h" 24 #include "llvm/Support/Casting.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/TimeProfiler.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include <algorithm> 31 #include <cassert> 32 #include <iterator> 33 34 #define DEBUG_TYPE "cgscc" 35 36 using namespace llvm; 37 38 // Explicit template instantiations and specialization definitions for core 39 // template typedefs. 40 namespace llvm { 41 42 static cl::opt<bool> AbortOnMaxDevirtIterationsReached( 43 "abort-on-max-devirt-iterations-reached", 44 cl::desc("Abort when the max iterations for devirtualization CGSCC repeat " 45 "pass is reached")); 46 47 AnalysisKey FunctionStatusAnalysis::Key; 48 49 // Explicit instantiations for the core proxy templates. 50 template class AllAnalysesOn<LazyCallGraph::SCC>; 51 template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; 52 template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, 53 LazyCallGraph &, CGSCCUpdateResult &>; 54 template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; 55 template class OuterAnalysisManagerProxy<ModuleAnalysisManager, 56 LazyCallGraph::SCC, LazyCallGraph &>; 57 template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; 58 59 /// Explicitly specialize the pass manager run method to handle call graph 60 /// updates. 61 template <> 62 PreservedAnalyses 63 PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, 64 CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC, 65 CGSCCAnalysisManager &AM, 66 LazyCallGraph &G, CGSCCUpdateResult &UR) { 67 // Request PassInstrumentation from analysis manager, will use it to run 68 // instrumenting callbacks for the passes later. 69 PassInstrumentation PI = 70 AM.getResult<PassInstrumentationAnalysis>(InitialC, G); 71 72 PreservedAnalyses PA = PreservedAnalyses::all(); 73 74 if (DebugLogging) 75 dbgs() << "Starting CGSCC pass manager run.\n"; 76 77 // The SCC may be refined while we are running passes over it, so set up 78 // a pointer that we can update. 79 LazyCallGraph::SCC *C = &InitialC; 80 81 // Get Function analysis manager from its proxy. 82 FunctionAnalysisManager &FAM = 83 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*C)->getManager(); 84 85 for (auto &Pass : Passes) { 86 // Check the PassInstrumentation's BeforePass callbacks before running the 87 // pass, skip its execution completely if asked to (callback returns false). 88 if (!PI.runBeforePass(*Pass, *C)) 89 continue; 90 91 PreservedAnalyses PassPA; 92 { 93 TimeTraceScope TimeScope(Pass->name()); 94 PassPA = Pass->run(*C, AM, G, UR); 95 } 96 97 if (UR.InvalidatedSCCs.count(C)) 98 PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 99 else 100 PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 101 102 // Update the SCC if necessary. 103 C = UR.UpdatedC ? UR.UpdatedC : C; 104 if (UR.UpdatedC) { 105 // If C is updated, also create a proxy and update FAM inside the result. 106 auto *ResultFAMCP = 107 &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G); 108 ResultFAMCP->updateFAM(FAM); 109 } 110 111 // If the CGSCC pass wasn't able to provide a valid updated SCC, the 112 // current SCC may simply need to be skipped if invalid. 113 if (UR.InvalidatedSCCs.count(C)) { 114 LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n"); 115 break; 116 } 117 // Check that we didn't miss any update scenario. 118 assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 119 120 // Update the analysis manager as each pass runs and potentially 121 // invalidates analyses. 122 AM.invalidate(*C, PassPA); 123 124 // Finally, we intersect the final preserved analyses to compute the 125 // aggregate preserved set for this pass manager. 126 PA.intersect(std::move(PassPA)); 127 128 // FIXME: Historically, the pass managers all called the LLVM context's 129 // yield function here. We don't have a generic way to acquire the 130 // context and it isn't yet clear what the right pattern is for yielding 131 // in the new pass manager so it is currently omitted. 132 // ...getContext().yield(); 133 } 134 135 // Before we mark all of *this* SCC's analyses as preserved below, intersect 136 // this with the cross-SCC preserved analysis set. This is used to allow 137 // CGSCC passes to mutate ancestor SCCs and still trigger proper invalidation 138 // for them. 139 UR.CrossSCCPA.intersect(PA); 140 141 // Invalidation was handled after each pass in the above loop for the current 142 // SCC. Therefore, the remaining analysis results in the AnalysisManager are 143 // preserved. We mark this with a set so that we don't need to inspect each 144 // one individually. 145 PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); 146 147 if (DebugLogging) 148 dbgs() << "Finished CGSCC pass manager run.\n"; 149 150 return PA; 151 } 152 153 PreservedAnalyses 154 ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) { 155 // Setup the CGSCC analysis manager from its proxy. 156 CGSCCAnalysisManager &CGAM = 157 AM.getResult<CGSCCAnalysisManagerModuleProxy>(M).getManager(); 158 159 // Get the call graph for this module. 160 LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M); 161 162 // Get Function analysis manager from its proxy. 163 FunctionAnalysisManager &FAM = 164 AM.getCachedResult<FunctionAnalysisManagerModuleProxy>(M)->getManager(); 165 166 // We keep worklists to allow us to push more work onto the pass manager as 167 // the passes are run. 168 SmallPriorityWorklist<LazyCallGraph::RefSCC *, 1> RCWorklist; 169 SmallPriorityWorklist<LazyCallGraph::SCC *, 1> CWorklist; 170 171 // Keep sets for invalidated SCCs and RefSCCs that should be skipped when 172 // iterating off the worklists. 173 SmallPtrSet<LazyCallGraph::RefSCC *, 4> InvalidRefSCCSet; 174 SmallPtrSet<LazyCallGraph::SCC *, 4> InvalidSCCSet; 175 176 SmallDenseSet<std::pair<LazyCallGraph::Node *, LazyCallGraph::SCC *>, 4> 177 InlinedInternalEdges; 178 179 CGSCCUpdateResult UR = { 180 RCWorklist, CWorklist, InvalidRefSCCSet, InvalidSCCSet, 181 nullptr, nullptr, PreservedAnalyses::all(), InlinedInternalEdges, 182 {}}; 183 184 // Request PassInstrumentation from analysis manager, will use it to run 185 // instrumenting callbacks for the passes later. 186 PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M); 187 188 PreservedAnalyses PA = PreservedAnalyses::all(); 189 CG.buildRefSCCs(); 190 for (auto RCI = CG.postorder_ref_scc_begin(), 191 RCE = CG.postorder_ref_scc_end(); 192 RCI != RCE;) { 193 assert(RCWorklist.empty() && 194 "Should always start with an empty RefSCC worklist"); 195 // The postorder_ref_sccs range we are walking is lazily constructed, so 196 // we only push the first one onto the worklist. The worklist allows us 197 // to capture *new* RefSCCs created during transformations. 198 // 199 // We really want to form RefSCCs lazily because that makes them cheaper 200 // to update as the program is simplified and allows us to have greater 201 // cache locality as forming a RefSCC touches all the parts of all the 202 // functions within that RefSCC. 203 // 204 // We also eagerly increment the iterator to the next position because 205 // the CGSCC passes below may delete the current RefSCC. 206 RCWorklist.insert(&*RCI++); 207 208 do { 209 LazyCallGraph::RefSCC *RC = RCWorklist.pop_back_val(); 210 if (InvalidRefSCCSet.count(RC)) { 211 LLVM_DEBUG(dbgs() << "Skipping an invalid RefSCC...\n"); 212 continue; 213 } 214 215 assert(CWorklist.empty() && 216 "Should always start with an empty SCC worklist"); 217 218 LLVM_DEBUG(dbgs() << "Running an SCC pass across the RefSCC: " << *RC 219 << "\n"); 220 221 // The top of the worklist may *also* be the same SCC we just ran over 222 // (and invalidated for). Keep track of that last SCC we processed due 223 // to SCC update to avoid redundant processing when an SCC is both just 224 // updated itself and at the top of the worklist. 225 LazyCallGraph::SCC *LastUpdatedC = nullptr; 226 227 // Push the initial SCCs in reverse post-order as we'll pop off the 228 // back and so see this in post-order. 229 for (LazyCallGraph::SCC &C : llvm::reverse(*RC)) 230 CWorklist.insert(&C); 231 232 do { 233 LazyCallGraph::SCC *C = CWorklist.pop_back_val(); 234 // Due to call graph mutations, we may have invalid SCCs or SCCs from 235 // other RefSCCs in the worklist. The invalid ones are dead and the 236 // other RefSCCs should be queued above, so we just need to skip both 237 // scenarios here. 238 if (InvalidSCCSet.count(C)) { 239 LLVM_DEBUG(dbgs() << "Skipping an invalid SCC...\n"); 240 continue; 241 } 242 if (LastUpdatedC == C) { 243 LLVM_DEBUG(dbgs() << "Skipping redundant run on SCC: " << *C << "\n"); 244 continue; 245 } 246 if (&C->getOuterRefSCC() != RC) { 247 LLVM_DEBUG(dbgs() << "Skipping an SCC that is now part of some other " 248 "RefSCC...\n"); 249 continue; 250 } 251 252 // Ensure we can proxy analysis updates from the CGSCC analysis manager 253 // into the the Function analysis manager by getting a proxy here. 254 // This also needs to update the FunctionAnalysisManager, as this may be 255 // the first time we see this SCC. 256 CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM( 257 FAM); 258 259 // Each time we visit a new SCC pulled off the worklist, 260 // a transformation of a child SCC may have also modified this parent 261 // and invalidated analyses. So we invalidate using the update record's 262 // cross-SCC preserved set. This preserved set is intersected by any 263 // CGSCC pass that handles invalidation (primarily pass managers) prior 264 // to marking its SCC as preserved. That lets us track everything that 265 // might need invalidation across SCCs without excessive invalidations 266 // on a single SCC. 267 // 268 // This essentially allows SCC passes to freely invalidate analyses 269 // of any ancestor SCC. If this becomes detrimental to successfully 270 // caching analyses, we could force each SCC pass to manually 271 // invalidate the analyses for any SCCs other than themselves which 272 // are mutated. However, that seems to lose the robustness of the 273 // pass-manager driven invalidation scheme. 274 CGAM.invalidate(*C, UR.CrossSCCPA); 275 276 do { 277 // Check that we didn't miss any update scenario. 278 assert(!InvalidSCCSet.count(C) && "Processing an invalid SCC!"); 279 assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 280 assert(&C->getOuterRefSCC() == RC && 281 "Processing an SCC in a different RefSCC!"); 282 283 LastUpdatedC = UR.UpdatedC; 284 UR.UpdatedRC = nullptr; 285 UR.UpdatedC = nullptr; 286 287 // Check the PassInstrumentation's BeforePass callbacks before 288 // running the pass, skip its execution completely if asked to 289 // (callback returns false). 290 if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C)) 291 continue; 292 293 PreservedAnalyses PassPA; 294 { 295 TimeTraceScope TimeScope(Pass->name()); 296 PassPA = Pass->run(*C, CGAM, CG, UR); 297 } 298 299 if (UR.InvalidatedSCCs.count(C)) 300 PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 301 else 302 PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 303 304 // Update the SCC and RefSCC if necessary. 305 C = UR.UpdatedC ? UR.UpdatedC : C; 306 RC = UR.UpdatedRC ? UR.UpdatedRC : RC; 307 308 if (UR.UpdatedC) { 309 // If we're updating the SCC, also update the FAM inside the proxy's 310 // result. 311 CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM( 312 FAM); 313 } 314 315 // If the CGSCC pass wasn't able to provide a valid updated SCC, 316 // the current SCC may simply need to be skipped if invalid. 317 if (UR.InvalidatedSCCs.count(C)) { 318 LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n"); 319 break; 320 } 321 // Check that we didn't miss any update scenario. 322 assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 323 324 // We handle invalidating the CGSCC analysis manager's information 325 // for the (potentially updated) SCC here. Note that any other SCCs 326 // whose structure has changed should have been invalidated by 327 // whatever was updating the call graph. This SCC gets invalidated 328 // late as it contains the nodes that were actively being 329 // processed. 330 CGAM.invalidate(*C, PassPA); 331 332 // Then intersect the preserved set so that invalidation of module 333 // analyses will eventually occur when the module pass completes. 334 // Also intersect with the cross-SCC preserved set to capture any 335 // cross-SCC invalidation. 336 UR.CrossSCCPA.intersect(PassPA); 337 PA.intersect(std::move(PassPA)); 338 339 // The pass may have restructured the call graph and refined the 340 // current SCC and/or RefSCC. We need to update our current SCC and 341 // RefSCC pointers to follow these. Also, when the current SCC is 342 // refined, re-run the SCC pass over the newly refined SCC in order 343 // to observe the most precise SCC model available. This inherently 344 // cannot cycle excessively as it only happens when we split SCCs 345 // apart, at most converging on a DAG of single nodes. 346 // FIXME: If we ever start having RefSCC passes, we'll want to 347 // iterate there too. 348 if (UR.UpdatedC) 349 LLVM_DEBUG(dbgs() 350 << "Re-running SCC passes after a refinement of the " 351 "current SCC: " 352 << *UR.UpdatedC << "\n"); 353 354 // Note that both `C` and `RC` may at this point refer to deleted, 355 // invalid SCC and RefSCCs respectively. But we will short circuit 356 // the processing when we check them in the loop above. 357 } while (UR.UpdatedC); 358 } while (!CWorklist.empty()); 359 360 // We only need to keep internal inlined edge information within 361 // a RefSCC, clear it to save on space and let the next time we visit 362 // any of these functions have a fresh start. 363 InlinedInternalEdges.clear(); 364 } while (!RCWorklist.empty()); 365 } 366 367 // By definition we preserve the call garph, all SCC analyses, and the 368 // analysis proxies by handling them above and in any nested pass managers. 369 PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); 370 PA.preserve<LazyCallGraphAnalysis>(); 371 PA.preserve<CGSCCAnalysisManagerModuleProxy>(); 372 PA.preserve<FunctionAnalysisManagerModuleProxy>(); 373 return PA; 374 } 375 376 PreservedAnalyses DevirtSCCRepeatedPass::run(LazyCallGraph::SCC &InitialC, 377 CGSCCAnalysisManager &AM, 378 LazyCallGraph &CG, 379 CGSCCUpdateResult &UR) { 380 PreservedAnalyses PA = PreservedAnalyses::all(); 381 PassInstrumentation PI = 382 AM.getResult<PassInstrumentationAnalysis>(InitialC, CG); 383 384 // The SCC may be refined while we are running passes over it, so set up 385 // a pointer that we can update. 386 LazyCallGraph::SCC *C = &InitialC; 387 388 // Struct to track the counts of direct and indirect calls in each function 389 // of the SCC. 390 struct CallCount { 391 int Direct; 392 int Indirect; 393 }; 394 395 // Put value handles on all of the indirect calls and return the number of 396 // direct calls for each function in the SCC. 397 auto ScanSCC = [](LazyCallGraph::SCC &C, 398 SmallMapVector<Value *, WeakTrackingVH, 16> &CallHandles) { 399 assert(CallHandles.empty() && "Must start with a clear set of handles."); 400 401 SmallDenseMap<Function *, CallCount> CallCounts; 402 CallCount CountLocal = {0, 0}; 403 for (LazyCallGraph::Node &N : C) { 404 CallCount &Count = 405 CallCounts.insert(std::make_pair(&N.getFunction(), CountLocal)) 406 .first->second; 407 for (Instruction &I : instructions(N.getFunction())) 408 if (auto *CB = dyn_cast<CallBase>(&I)) { 409 if (CB->getCalledFunction()) { 410 ++Count.Direct; 411 } else { 412 ++Count.Indirect; 413 CallHandles.insert({CB, WeakTrackingVH(CB)}); 414 } 415 } 416 } 417 418 return CallCounts; 419 }; 420 421 UR.IndirectVHs.clear(); 422 // Populate the initial call handles and get the initial call counts. 423 auto CallCounts = ScanSCC(*C, UR.IndirectVHs); 424 425 for (int Iteration = 0;; ++Iteration) { 426 if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C)) 427 continue; 428 429 PreservedAnalyses PassPA = Pass->run(*C, AM, CG, UR); 430 431 if (UR.InvalidatedSCCs.count(C)) 432 PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 433 else 434 PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 435 436 // If the SCC structure has changed, bail immediately and let the outer 437 // CGSCC layer handle any iteration to reflect the refined structure. 438 if (UR.UpdatedC && UR.UpdatedC != C) { 439 PA.intersect(std::move(PassPA)); 440 break; 441 } 442 443 // Check that we didn't miss any update scenario. 444 assert(!UR.InvalidatedSCCs.count(C) && "Processing an invalid SCC!"); 445 assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 446 447 // Check whether any of the handles were devirtualized. 448 bool Devirt = llvm::any_of(UR.IndirectVHs, [](auto &P) -> bool { 449 if (P.second) { 450 if (CallBase *CB = dyn_cast<CallBase>(P.second)) { 451 if (CB->getCalledFunction()) { 452 LLVM_DEBUG(dbgs() << "Found devirtualized call: " << *CB << "\n"); 453 return true; 454 } 455 } 456 } 457 return false; 458 }); 459 460 // Rescan to build up a new set of handles and count how many direct 461 // calls remain. If we decide to iterate, this also sets up the input to 462 // the next iteration. 463 UR.IndirectVHs.clear(); 464 auto NewCallCounts = ScanSCC(*C, UR.IndirectVHs); 465 466 // If we haven't found an explicit devirtualization already see if we 467 // have decreased the number of indirect calls and increased the number 468 // of direct calls for any function in the SCC. This can be fooled by all 469 // manner of transformations such as DCE and other things, but seems to 470 // work well in practice. 471 if (!Devirt) 472 // Iterate over the keys in NewCallCounts, if Function also exists in 473 // CallCounts, make the check below. 474 for (auto &Pair : NewCallCounts) { 475 auto &CallCountNew = Pair.second; 476 auto CountIt = CallCounts.find(Pair.first); 477 if (CountIt != CallCounts.end()) { 478 const auto &CallCountOld = CountIt->second; 479 if (CallCountOld.Indirect > CallCountNew.Indirect && 480 CallCountOld.Direct < CallCountNew.Direct) { 481 Devirt = true; 482 break; 483 } 484 } 485 } 486 487 if (!Devirt) { 488 PA.intersect(std::move(PassPA)); 489 break; 490 } 491 492 // Otherwise, if we've already hit our max, we're done. 493 if (Iteration >= MaxIterations) { 494 if (AbortOnMaxDevirtIterationsReached) 495 report_fatal_error("Max devirtualization iterations reached"); 496 LLVM_DEBUG( 497 dbgs() << "Found another devirtualization after hitting the max " 498 "number of repetitions (" 499 << MaxIterations << ") on SCC: " << *C << "\n"); 500 PA.intersect(std::move(PassPA)); 501 break; 502 } 503 504 LLVM_DEBUG( 505 dbgs() << "Repeating an SCC pass after finding a devirtualization in: " 506 << *C << "\n"); 507 508 // Move over the new call counts in preparation for iterating. 509 CallCounts = std::move(NewCallCounts); 510 511 // Update the analysis manager with each run and intersect the total set 512 // of preserved analyses so we're ready to iterate. 513 AM.invalidate(*C, PassPA); 514 515 PA.intersect(std::move(PassPA)); 516 } 517 518 // Note that we don't add any preserved entries here unlike a more normal 519 // "pass manager" because we only handle invalidation *between* iterations, 520 // not after the last iteration. 521 return PA; 522 } 523 524 PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C, 525 CGSCCAnalysisManager &AM, 526 LazyCallGraph &CG, 527 CGSCCUpdateResult &UR) { 528 // Setup the function analysis manager from its proxy. 529 FunctionAnalysisManager &FAM = 530 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 531 532 SmallVector<LazyCallGraph::Node *, 4> Nodes; 533 for (LazyCallGraph::Node &N : C) 534 Nodes.push_back(&N); 535 536 // The SCC may get split while we are optimizing functions due to deleting 537 // edges. If this happens, the current SCC can shift, so keep track of 538 // a pointer we can overwrite. 539 LazyCallGraph::SCC *CurrentC = &C; 540 541 LLVM_DEBUG(dbgs() << "Running function passes across an SCC: " << C << "\n"); 542 543 PreservedAnalyses PA = PreservedAnalyses::all(); 544 for (LazyCallGraph::Node *N : Nodes) { 545 // Skip nodes from other SCCs. These may have been split out during 546 // processing. We'll eventually visit those SCCs and pick up the nodes 547 // there. 548 if (CG.lookupSCC(*N) != CurrentC) 549 continue; 550 551 Function &F = N->getFunction(); 552 // The expectation here is that FunctionStatusAnalysis was required at the 553 // end of the function passes pipeline managed by this adaptor. Then, if any 554 // CGSCC passes were re-run because CGSCCs changed (or devirtualization), 555 // and none changed F, then FunctionStatusAnalysis would still be cached 556 // here and we don't need to rerun the passes managed by this adaptor. 557 if (FAM.getCachedResult<FunctionStatusAnalysis>(F)) 558 continue; 559 560 PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F); 561 if (!PI.runBeforePass<Function>(*Pass, F)) 562 continue; 563 564 PreservedAnalyses PassPA; 565 { 566 TimeTraceScope TimeScope(Pass->name()); 567 PassPA = Pass->run(F, FAM); 568 } 569 570 PI.runAfterPass<Function>(*Pass, F, PassPA); 571 572 // We know that the function pass couldn't have invalidated any other 573 // function's analyses (that's the contract of a function pass), so 574 // directly handle the function analysis manager's invalidation here. 575 FAM.invalidate(F, PassPA); 576 577 // Then intersect the preserved set so that invalidation of module 578 // analyses will eventually occur when the module pass completes. 579 PA.intersect(std::move(PassPA)); 580 581 // If the call graph hasn't been preserved, update it based on this 582 // function pass. This may also update the current SCC to point to 583 // a smaller, more refined SCC. 584 auto PAC = PA.getChecker<LazyCallGraphAnalysis>(); 585 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) { 586 CurrentC = &updateCGAndAnalysisManagerForFunctionPass(CG, *CurrentC, *N, 587 AM, UR, FAM); 588 assert(CG.lookupSCC(*N) == CurrentC && 589 "Current SCC not updated to the SCC containing the current node!"); 590 } 591 } 592 593 // By definition we preserve the proxy. And we preserve all analyses on 594 // Functions. This precludes *any* invalidation of function analyses by the 595 // proxy, but that's OK because we've taken care to invalidate analyses in 596 // the function analysis manager incrementally above. 597 PA.preserveSet<AllAnalysesOn<Function>>(); 598 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 599 600 // We've also ensured that we updated the call graph along the way. 601 PA.preserve<LazyCallGraphAnalysis>(); 602 603 return PA; 604 } 605 606 bool CGSCCAnalysisManagerModuleProxy::Result::invalidate( 607 Module &M, const PreservedAnalyses &PA, 608 ModuleAnalysisManager::Invalidator &Inv) { 609 // If literally everything is preserved, we're done. 610 if (PA.areAllPreserved()) 611 return false; // This is still a valid proxy. 612 613 // If this proxy or the call graph is going to be invalidated, we also need 614 // to clear all the keys coming from that analysis. 615 // 616 // We also directly invalidate the FAM's module proxy if necessary, and if 617 // that proxy isn't preserved we can't preserve this proxy either. We rely on 618 // it to handle module -> function analysis invalidation in the face of 619 // structural changes and so if it's unavailable we conservatively clear the 620 // entire SCC layer as well rather than trying to do invalidation ourselves. 621 auto PAC = PA.getChecker<CGSCCAnalysisManagerModuleProxy>(); 622 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>()) || 623 Inv.invalidate<LazyCallGraphAnalysis>(M, PA) || 624 Inv.invalidate<FunctionAnalysisManagerModuleProxy>(M, PA)) { 625 InnerAM->clear(); 626 627 // And the proxy itself should be marked as invalid so that we can observe 628 // the new call graph. This isn't strictly necessary because we cheat 629 // above, but is still useful. 630 return true; 631 } 632 633 // Directly check if the relevant set is preserved so we can short circuit 634 // invalidating SCCs below. 635 bool AreSCCAnalysesPreserved = 636 PA.allAnalysesInSetPreserved<AllAnalysesOn<LazyCallGraph::SCC>>(); 637 638 // Ok, we have a graph, so we can propagate the invalidation down into it. 639 G->buildRefSCCs(); 640 for (auto &RC : G->postorder_ref_sccs()) 641 for (auto &C : RC) { 642 Optional<PreservedAnalyses> InnerPA; 643 644 // Check to see whether the preserved set needs to be adjusted based on 645 // module-level analysis invalidation triggering deferred invalidation 646 // for this SCC. 647 if (auto *OuterProxy = 648 InnerAM->getCachedResult<ModuleAnalysisManagerCGSCCProxy>(C)) 649 for (const auto &OuterInvalidationPair : 650 OuterProxy->getOuterInvalidations()) { 651 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 652 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 653 if (Inv.invalidate(OuterAnalysisID, M, PA)) { 654 if (!InnerPA) 655 InnerPA = PA; 656 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 657 InnerPA->abandon(InnerAnalysisID); 658 } 659 } 660 661 // Check if we needed a custom PA set. If so we'll need to run the inner 662 // invalidation. 663 if (InnerPA) { 664 InnerAM->invalidate(C, *InnerPA); 665 continue; 666 } 667 668 // Otherwise we only need to do invalidation if the original PA set didn't 669 // preserve all SCC analyses. 670 if (!AreSCCAnalysesPreserved) 671 InnerAM->invalidate(C, PA); 672 } 673 674 // Return false to indicate that this result is still a valid proxy. 675 return false; 676 } 677 678 template <> 679 CGSCCAnalysisManagerModuleProxy::Result 680 CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM) { 681 // Force the Function analysis manager to also be available so that it can 682 // be accessed in an SCC analysis and proxied onward to function passes. 683 // FIXME: It is pretty awkward to just drop the result here and assert that 684 // we can find it again later. 685 (void)AM.getResult<FunctionAnalysisManagerModuleProxy>(M); 686 687 return Result(*InnerAM, AM.getResult<LazyCallGraphAnalysis>(M)); 688 } 689 690 AnalysisKey FunctionAnalysisManagerCGSCCProxy::Key; 691 692 FunctionAnalysisManagerCGSCCProxy::Result 693 FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C, 694 CGSCCAnalysisManager &AM, 695 LazyCallGraph &CG) { 696 // Note: unconditionally getting checking that the proxy exists may get it at 697 // this point. There are cases when this is being run unnecessarily, but 698 // it is cheap and having the assertion in place is more valuable. 699 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG); 700 Module &M = *C.begin()->getFunction().getParent(); 701 bool ProxyExists = 702 MAMProxy.cachedResultExists<FunctionAnalysisManagerModuleProxy>(M); 703 assert(ProxyExists && 704 "The CGSCC pass manager requires that the FAM module proxy is run " 705 "on the module prior to entering the CGSCC walk"); 706 (void)ProxyExists; 707 708 // We just return an empty result. The caller will use the updateFAM interface 709 // to correctly register the relevant FunctionAnalysisManager based on the 710 // context in which this proxy is run. 711 return Result(); 712 } 713 714 bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate( 715 LazyCallGraph::SCC &C, const PreservedAnalyses &PA, 716 CGSCCAnalysisManager::Invalidator &Inv) { 717 // If literally everything is preserved, we're done. 718 if (PA.areAllPreserved()) 719 return false; // This is still a valid proxy. 720 721 // All updates to preserve valid results are done below, so we don't need to 722 // invalidate this proxy. 723 // 724 // Note that in order to preserve this proxy, a module pass must ensure that 725 // the FAM has been completely updated to handle the deletion of functions. 726 // Specifically, any FAM-cached results for those functions need to have been 727 // forcibly cleared. When preserved, this proxy will only invalidate results 728 // cached on functions *still in the module* at the end of the module pass. 729 auto PAC = PA.getChecker<FunctionAnalysisManagerCGSCCProxy>(); 730 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<LazyCallGraph::SCC>>()) { 731 for (LazyCallGraph::Node &N : C) 732 FAM->invalidate(N.getFunction(), PA); 733 734 return false; 735 } 736 737 // Directly check if the relevant set is preserved. 738 bool AreFunctionAnalysesPreserved = 739 PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); 740 741 // Now walk all the functions to see if any inner analysis invalidation is 742 // necessary. 743 for (LazyCallGraph::Node &N : C) { 744 Function &F = N.getFunction(); 745 Optional<PreservedAnalyses> FunctionPA; 746 747 // Check to see whether the preserved set needs to be pruned based on 748 // SCC-level analysis invalidation that triggers deferred invalidation 749 // registered with the outer analysis manager proxy for this function. 750 if (auto *OuterProxy = 751 FAM->getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F)) 752 for (const auto &OuterInvalidationPair : 753 OuterProxy->getOuterInvalidations()) { 754 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 755 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 756 if (Inv.invalidate(OuterAnalysisID, C, PA)) { 757 if (!FunctionPA) 758 FunctionPA = PA; 759 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 760 FunctionPA->abandon(InnerAnalysisID); 761 } 762 } 763 764 // Check if we needed a custom PA set, and if so we'll need to run the 765 // inner invalidation. 766 if (FunctionPA) { 767 FAM->invalidate(F, *FunctionPA); 768 continue; 769 } 770 771 // Otherwise we only need to do invalidation if the original PA set didn't 772 // preserve all function analyses. 773 if (!AreFunctionAnalysesPreserved) 774 FAM->invalidate(F, PA); 775 } 776 777 // Return false to indicate that this result is still a valid proxy. 778 return false; 779 } 780 781 } // end namespace llvm 782 783 /// When a new SCC is created for the graph we first update the 784 /// FunctionAnalysisManager in the Proxy's result. 785 /// As there might be function analysis results cached for the functions now in 786 /// that SCC, two forms of updates are required. 787 /// 788 /// First, a proxy from the SCC to the FunctionAnalysisManager needs to be 789 /// created so that any subsequent invalidation events to the SCC are 790 /// propagated to the function analysis results cached for functions within it. 791 /// 792 /// Second, if any of the functions within the SCC have analysis results with 793 /// outer analysis dependencies, then those dependencies would point to the 794 /// *wrong* SCC's analysis result. We forcibly invalidate the necessary 795 /// function analyses so that they don't retain stale handles. 796 static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C, 797 LazyCallGraph &G, 798 CGSCCAnalysisManager &AM, 799 FunctionAnalysisManager &FAM) { 800 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, G).updateFAM(FAM); 801 802 // Now walk the functions in this SCC and invalidate any function analysis 803 // results that might have outer dependencies on an SCC analysis. 804 for (LazyCallGraph::Node &N : C) { 805 Function &F = N.getFunction(); 806 807 auto *OuterProxy = 808 FAM.getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F); 809 if (!OuterProxy) 810 // No outer analyses were queried, nothing to do. 811 continue; 812 813 // Forcibly abandon all the inner analyses with dependencies, but 814 // invalidate nothing else. 815 auto PA = PreservedAnalyses::all(); 816 for (const auto &OuterInvalidationPair : 817 OuterProxy->getOuterInvalidations()) { 818 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 819 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 820 PA.abandon(InnerAnalysisID); 821 } 822 823 // Now invalidate anything we found. 824 FAM.invalidate(F, PA); 825 } 826 } 827 828 /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c 829 /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly 830 /// added SCCs. 831 /// 832 /// The range of new SCCs must be in postorder already. The SCC they were split 833 /// out of must be provided as \p C. The current node being mutated and 834 /// triggering updates must be passed as \p N. 835 /// 836 /// This function returns the SCC containing \p N. This will be either \p C if 837 /// no new SCCs have been split out, or it will be the new SCC containing \p N. 838 template <typename SCCRangeT> 839 static LazyCallGraph::SCC * 840 incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, 841 LazyCallGraph::Node &N, LazyCallGraph::SCC *C, 842 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { 843 using SCC = LazyCallGraph::SCC; 844 845 if (NewSCCRange.empty()) 846 return C; 847 848 // Add the current SCC to the worklist as its shape has changed. 849 UR.CWorklist.insert(C); 850 LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist:" << *C 851 << "\n"); 852 853 SCC *OldC = C; 854 855 // Update the current SCC. Note that if we have new SCCs, this must actually 856 // change the SCC. 857 assert(C != &*NewSCCRange.begin() && 858 "Cannot insert new SCCs without changing current SCC!"); 859 C = &*NewSCCRange.begin(); 860 assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 861 862 // If we had a cached FAM proxy originally, we will want to create more of 863 // them for each SCC that was split off. 864 FunctionAnalysisManager *FAM = nullptr; 865 if (auto *FAMProxy = 866 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*OldC)) 867 FAM = &FAMProxy->getManager(); 868 869 // We need to propagate an invalidation call to all but the newly current SCC 870 // because the outer pass manager won't do that for us after splitting them. 871 // FIXME: We should accept a PreservedAnalysis from the CG updater so that if 872 // there are preserved analysis we can avoid invalidating them here for 873 // split-off SCCs. 874 // We know however that this will preserve any FAM proxy so go ahead and mark 875 // that. 876 auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 877 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 878 AM.invalidate(*OldC, PA); 879 880 // Ensure the now-current SCC's function analyses are updated. 881 if (FAM) 882 updateNewSCCFunctionAnalyses(*C, G, AM, *FAM); 883 884 for (SCC &NewC : llvm::reverse(llvm::drop_begin(NewSCCRange))) { 885 assert(C != &NewC && "No need to re-visit the current SCC!"); 886 assert(OldC != &NewC && "Already handled the original SCC!"); 887 UR.CWorklist.insert(&NewC); 888 LLVM_DEBUG(dbgs() << "Enqueuing a newly formed SCC:" << NewC << "\n"); 889 890 // Ensure new SCCs' function analyses are updated. 891 if (FAM) 892 updateNewSCCFunctionAnalyses(NewC, G, AM, *FAM); 893 894 // Also propagate a normal invalidation to the new SCC as only the current 895 // will get one from the pass manager infrastructure. 896 AM.invalidate(NewC, PA); 897 } 898 return C; 899 } 900 901 static LazyCallGraph::SCC &updateCGAndAnalysisManagerForPass( 902 LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 903 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 904 FunctionAnalysisManager &FAM, bool FunctionPass) { 905 using Node = LazyCallGraph::Node; 906 using Edge = LazyCallGraph::Edge; 907 using SCC = LazyCallGraph::SCC; 908 using RefSCC = LazyCallGraph::RefSCC; 909 910 RefSCC &InitialRC = InitialC.getOuterRefSCC(); 911 SCC *C = &InitialC; 912 RefSCC *RC = &InitialRC; 913 Function &F = N.getFunction(); 914 915 // Walk the function body and build up the set of retained, promoted, and 916 // demoted edges. 917 SmallVector<Constant *, 16> Worklist; 918 SmallPtrSet<Constant *, 16> Visited; 919 SmallPtrSet<Node *, 16> RetainedEdges; 920 SmallSetVector<Node *, 4> PromotedRefTargets; 921 SmallSetVector<Node *, 4> DemotedCallTargets; 922 SmallSetVector<Node *, 4> NewCallEdges; 923 SmallSetVector<Node *, 4> NewRefEdges; 924 925 // First walk the function and handle all called functions. We do this first 926 // because if there is a single call edge, whether there are ref edges is 927 // irrelevant. 928 for (Instruction &I : instructions(F)) { 929 if (auto *CB = dyn_cast<CallBase>(&I)) { 930 if (Function *Callee = CB->getCalledFunction()) { 931 if (Visited.insert(Callee).second && !Callee->isDeclaration()) { 932 Node *CalleeN = G.lookup(*Callee); 933 assert(CalleeN && 934 "Visited function should already have an associated node"); 935 Edge *E = N->lookup(*CalleeN); 936 assert((E || !FunctionPass) && 937 "No function transformations should introduce *new* " 938 "call edges! Any new calls should be modeled as " 939 "promoted existing ref edges!"); 940 bool Inserted = RetainedEdges.insert(CalleeN).second; 941 (void)Inserted; 942 assert(Inserted && "We should never visit a function twice."); 943 if (!E) 944 NewCallEdges.insert(CalleeN); 945 else if (!E->isCall()) 946 PromotedRefTargets.insert(CalleeN); 947 } 948 } else { 949 // We can miss devirtualization if an indirect call is created then 950 // promoted before updateCGAndAnalysisManagerForPass runs. 951 auto *Entry = UR.IndirectVHs.find(CB); 952 if (Entry == UR.IndirectVHs.end()) 953 UR.IndirectVHs.insert({CB, WeakTrackingVH(CB)}); 954 else if (!Entry->second) 955 Entry->second = WeakTrackingVH(CB); 956 } 957 } 958 } 959 960 // Now walk all references. 961 for (Instruction &I : instructions(F)) 962 for (Value *Op : I.operand_values()) 963 if (auto *OpC = dyn_cast<Constant>(Op)) 964 if (Visited.insert(OpC).second) 965 Worklist.push_back(OpC); 966 967 auto VisitRef = [&](Function &Referee) { 968 Node *RefereeN = G.lookup(Referee); 969 assert(RefereeN && 970 "Visited function should already have an associated node"); 971 Edge *E = N->lookup(*RefereeN); 972 assert((E || !FunctionPass) && 973 "No function transformations should introduce *new* ref " 974 "edges! Any new ref edges would require IPO which " 975 "function passes aren't allowed to do!"); 976 bool Inserted = RetainedEdges.insert(RefereeN).second; 977 (void)Inserted; 978 assert(Inserted && "We should never visit a function twice."); 979 if (!E) 980 NewRefEdges.insert(RefereeN); 981 else if (E->isCall()) 982 DemotedCallTargets.insert(RefereeN); 983 }; 984 LazyCallGraph::visitReferences(Worklist, Visited, VisitRef); 985 986 // Handle new ref edges. 987 for (Node *RefTarget : NewRefEdges) { 988 SCC &TargetC = *G.lookupSCC(*RefTarget); 989 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 990 (void)TargetRC; 991 // TODO: This only allows trivial edges to be added for now. 992 #ifdef EXPENSIVE_CHECKS 993 assert((RC == &TargetRC || 994 RC->isAncestorOf(TargetRC)) && "New ref edge is not trivial!"); 995 #endif 996 RC->insertTrivialRefEdge(N, *RefTarget); 997 } 998 999 // Handle new call edges. 1000 for (Node *CallTarget : NewCallEdges) { 1001 SCC &TargetC = *G.lookupSCC(*CallTarget); 1002 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 1003 (void)TargetRC; 1004 // TODO: This only allows trivial edges to be added for now. 1005 #ifdef EXPENSIVE_CHECKS 1006 assert((RC == &TargetRC || 1007 RC->isAncestorOf(TargetRC)) && "New call edge is not trivial!"); 1008 #endif 1009 // Add a trivial ref edge to be promoted later on alongside 1010 // PromotedRefTargets. 1011 RC->insertTrivialRefEdge(N, *CallTarget); 1012 } 1013 1014 // Include synthetic reference edges to known, defined lib functions. 1015 for (auto *LibFn : G.getLibFunctions()) 1016 // While the list of lib functions doesn't have repeats, don't re-visit 1017 // anything handled above. 1018 if (!Visited.count(LibFn)) 1019 VisitRef(*LibFn); 1020 1021 // First remove all of the edges that are no longer present in this function. 1022 // The first step makes these edges uniformly ref edges and accumulates them 1023 // into a separate data structure so removal doesn't invalidate anything. 1024 SmallVector<Node *, 4> DeadTargets; 1025 for (Edge &E : *N) { 1026 if (RetainedEdges.count(&E.getNode())) 1027 continue; 1028 1029 SCC &TargetC = *G.lookupSCC(E.getNode()); 1030 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 1031 if (&TargetRC == RC && E.isCall()) { 1032 if (C != &TargetC) { 1033 // For separate SCCs this is trivial. 1034 RC->switchTrivialInternalEdgeToRef(N, E.getNode()); 1035 } else { 1036 // Now update the call graph. 1037 C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, E.getNode()), 1038 G, N, C, AM, UR); 1039 } 1040 } 1041 1042 // Now that this is ready for actual removal, put it into our list. 1043 DeadTargets.push_back(&E.getNode()); 1044 } 1045 // Remove the easy cases quickly and actually pull them out of our list. 1046 llvm::erase_if(DeadTargets, [&](Node *TargetN) { 1047 SCC &TargetC = *G.lookupSCC(*TargetN); 1048 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 1049 1050 // We can't trivially remove internal targets, so skip 1051 // those. 1052 if (&TargetRC == RC) 1053 return false; 1054 1055 LLVM_DEBUG(dbgs() << "Deleting outgoing edge from '" << N << "' to '" 1056 << *TargetN << "'\n"); 1057 RC->removeOutgoingEdge(N, *TargetN); 1058 return true; 1059 }); 1060 1061 // Now do a batch removal of the internal ref edges left. 1062 auto NewRefSCCs = RC->removeInternalRefEdge(N, DeadTargets); 1063 if (!NewRefSCCs.empty()) { 1064 // The old RefSCC is dead, mark it as such. 1065 UR.InvalidatedRefSCCs.insert(RC); 1066 1067 // Note that we don't bother to invalidate analyses as ref-edge 1068 // connectivity is not really observable in any way and is intended 1069 // exclusively to be used for ordering of transforms rather than for 1070 // analysis conclusions. 1071 1072 // Update RC to the "bottom". 1073 assert(G.lookupSCC(N) == C && "Changed the SCC when splitting RefSCCs!"); 1074 RC = &C->getOuterRefSCC(); 1075 assert(G.lookupRefSCC(N) == RC && "Failed to update current RefSCC!"); 1076 1077 // The RC worklist is in reverse postorder, so we enqueue the new ones in 1078 // RPO except for the one which contains the source node as that is the 1079 // "bottom" we will continue processing in the bottom-up walk. 1080 assert(NewRefSCCs.front() == RC && 1081 "New current RefSCC not first in the returned list!"); 1082 for (RefSCC *NewRC : llvm::reverse(llvm::drop_begin(NewRefSCCs))) { 1083 assert(NewRC != RC && "Should not encounter the current RefSCC further " 1084 "in the postorder list of new RefSCCs."); 1085 UR.RCWorklist.insert(NewRC); 1086 LLVM_DEBUG(dbgs() << "Enqueuing a new RefSCC in the update worklist: " 1087 << *NewRC << "\n"); 1088 } 1089 } 1090 1091 // Next demote all the call edges that are now ref edges. This helps make 1092 // the SCCs small which should minimize the work below as we don't want to 1093 // form cycles that this would break. 1094 for (Node *RefTarget : DemotedCallTargets) { 1095 SCC &TargetC = *G.lookupSCC(*RefTarget); 1096 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 1097 1098 // The easy case is when the target RefSCC is not this RefSCC. This is 1099 // only supported when the target RefSCC is a child of this RefSCC. 1100 if (&TargetRC != RC) { 1101 #ifdef EXPENSIVE_CHECKS 1102 assert(RC->isAncestorOf(TargetRC) && 1103 "Cannot potentially form RefSCC cycles here!"); 1104 #endif 1105 RC->switchOutgoingEdgeToRef(N, *RefTarget); 1106 LLVM_DEBUG(dbgs() << "Switch outgoing call edge to a ref edge from '" << N 1107 << "' to '" << *RefTarget << "'\n"); 1108 continue; 1109 } 1110 1111 // We are switching an internal call edge to a ref edge. This may split up 1112 // some SCCs. 1113 if (C != &TargetC) { 1114 // For separate SCCs this is trivial. 1115 RC->switchTrivialInternalEdgeToRef(N, *RefTarget); 1116 continue; 1117 } 1118 1119 // Now update the call graph. 1120 C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, *RefTarget), G, N, 1121 C, AM, UR); 1122 } 1123 1124 // We added a ref edge earlier for new call edges, promote those to call edges 1125 // alongside PromotedRefTargets. 1126 for (Node *E : NewCallEdges) 1127 PromotedRefTargets.insert(E); 1128 1129 // Now promote ref edges into call edges. 1130 for (Node *CallTarget : PromotedRefTargets) { 1131 SCC &TargetC = *G.lookupSCC(*CallTarget); 1132 RefSCC &TargetRC = TargetC.getOuterRefSCC(); 1133 1134 // The easy case is when the target RefSCC is not this RefSCC. This is 1135 // only supported when the target RefSCC is a child of this RefSCC. 1136 if (&TargetRC != RC) { 1137 #ifdef EXPENSIVE_CHECKS 1138 assert(RC->isAncestorOf(TargetRC) && 1139 "Cannot potentially form RefSCC cycles here!"); 1140 #endif 1141 RC->switchOutgoingEdgeToCall(N, *CallTarget); 1142 LLVM_DEBUG(dbgs() << "Switch outgoing ref edge to a call edge from '" << N 1143 << "' to '" << *CallTarget << "'\n"); 1144 continue; 1145 } 1146 LLVM_DEBUG(dbgs() << "Switch an internal ref edge to a call edge from '" 1147 << N << "' to '" << *CallTarget << "'\n"); 1148 1149 // Otherwise we are switching an internal ref edge to a call edge. This 1150 // may merge away some SCCs, and we add those to the UpdateResult. We also 1151 // need to make sure to update the worklist in the event SCCs have moved 1152 // before the current one in the post-order sequence 1153 bool HasFunctionAnalysisProxy = false; 1154 auto InitialSCCIndex = RC->find(*C) - RC->begin(); 1155 bool FormedCycle = RC->switchInternalEdgeToCall( 1156 N, *CallTarget, [&](ArrayRef<SCC *> MergedSCCs) { 1157 for (SCC *MergedC : MergedSCCs) { 1158 assert(MergedC != &TargetC && "Cannot merge away the target SCC!"); 1159 1160 HasFunctionAnalysisProxy |= 1161 AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>( 1162 *MergedC) != nullptr; 1163 1164 // Mark that this SCC will no longer be valid. 1165 UR.InvalidatedSCCs.insert(MergedC); 1166 1167 // FIXME: We should really do a 'clear' here to forcibly release 1168 // memory, but we don't have a good way of doing that and 1169 // preserving the function analyses. 1170 auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 1171 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 1172 AM.invalidate(*MergedC, PA); 1173 } 1174 }); 1175 1176 // If we formed a cycle by creating this call, we need to update more data 1177 // structures. 1178 if (FormedCycle) { 1179 C = &TargetC; 1180 assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 1181 1182 // If one of the invalidated SCCs had a cached proxy to a function 1183 // analysis manager, we need to create a proxy in the new current SCC as 1184 // the invalidated SCCs had their functions moved. 1185 if (HasFunctionAnalysisProxy) 1186 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G).updateFAM(FAM); 1187 1188 // Any analyses cached for this SCC are no longer precise as the shape 1189 // has changed by introducing this cycle. However, we have taken care to 1190 // update the proxies so it remains valide. 1191 auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 1192 PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 1193 AM.invalidate(*C, PA); 1194 } 1195 auto NewSCCIndex = RC->find(*C) - RC->begin(); 1196 // If we have actually moved an SCC to be topologically "below" the current 1197 // one due to merging, we will need to revisit the current SCC after 1198 // visiting those moved SCCs. 1199 // 1200 // It is critical that we *do not* revisit the current SCC unless we 1201 // actually move SCCs in the process of merging because otherwise we may 1202 // form a cycle where an SCC is split apart, merged, split, merged and so 1203 // on infinitely. 1204 if (InitialSCCIndex < NewSCCIndex) { 1205 // Put our current SCC back onto the worklist as we'll visit other SCCs 1206 // that are now definitively ordered prior to the current one in the 1207 // post-order sequence, and may end up observing more precise context to 1208 // optimize the current SCC. 1209 UR.CWorklist.insert(C); 1210 LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist: " << *C 1211 << "\n"); 1212 // Enqueue in reverse order as we pop off the back of the worklist. 1213 for (SCC &MovedC : llvm::reverse(make_range(RC->begin() + InitialSCCIndex, 1214 RC->begin() + NewSCCIndex))) { 1215 UR.CWorklist.insert(&MovedC); 1216 LLVM_DEBUG(dbgs() << "Enqueuing a newly earlier in post-order SCC: " 1217 << MovedC << "\n"); 1218 } 1219 } 1220 } 1221 1222 assert(!UR.InvalidatedSCCs.count(C) && "Invalidated the current SCC!"); 1223 assert(!UR.InvalidatedRefSCCs.count(RC) && "Invalidated the current RefSCC!"); 1224 assert(&C->getOuterRefSCC() == RC && "Current SCC not in current RefSCC!"); 1225 1226 // Record the current RefSCC and SCC for higher layers of the CGSCC pass 1227 // manager now that all the updates have been applied. 1228 if (RC != &InitialRC) 1229 UR.UpdatedRC = RC; 1230 if (C != &InitialC) 1231 UR.UpdatedC = C; 1232 1233 return *C; 1234 } 1235 1236 LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( 1237 LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 1238 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 1239 FunctionAnalysisManager &FAM) { 1240 return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM, 1241 /* FunctionPass */ true); 1242 } 1243 LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForCGSCCPass( 1244 LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 1245 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 1246 FunctionAnalysisManager &FAM) { 1247 return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM, 1248 /* FunctionPass */ false); 1249 } 1250