10b57cec5SDimitry Andric //===- CGSCCPassManager.cpp - Managing & running CGSCC passes -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h" 100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 1181ad6265SDimitry Andric #include "llvm/ADT/PriorityWorklist.h" 120b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 130b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 140b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 160b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 170b57cec5SDimitry Andric #include "llvm/Analysis/LazyCallGraph.h" 180b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 190b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h" 200b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 210b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 225ffd83dbSDimitry Andric #include "llvm/IR/PassManagerImpl.h" 23e8d8bef9SDimitry Andric #include "llvm/IR/ValueHandle.h" 240b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 25e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h" 260b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 27e8d8bef9SDimitry Andric #include "llvm/Support/ErrorHandling.h" 285ffd83dbSDimitry Andric #include "llvm/Support/TimeProfiler.h" 29e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h" 300b57cec5SDimitry Andric #include <cassert> 310b57cec5SDimitry Andric #include <iterator> 32bdd1243dSDimitry Andric #include <optional> 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric #define DEBUG_TYPE "cgscc" 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric using namespace llvm; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // Explicit template instantiations and specialization definitions for core 390b57cec5SDimitry Andric // template typedefs. 400b57cec5SDimitry Andric namespace llvm { 41e8d8bef9SDimitry Andric static cl::opt<bool> AbortOnMaxDevirtIterationsReached( 42e8d8bef9SDimitry Andric "abort-on-max-devirt-iterations-reached", 43e8d8bef9SDimitry Andric cl::desc("Abort when the max iterations for devirtualization CGSCC repeat " 44e8d8bef9SDimitry Andric "pass is reached")); 45e8d8bef9SDimitry Andric 46349cc55cSDimitry Andric AnalysisKey ShouldNotRunFunctionPassesAnalysis::Key; 47349cc55cSDimitry Andric 480b57cec5SDimitry Andric // Explicit instantiations for the core proxy templates. 490b57cec5SDimitry Andric template class AllAnalysesOn<LazyCallGraph::SCC>; 500b57cec5SDimitry Andric template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; 510b57cec5SDimitry Andric template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, 520b57cec5SDimitry Andric LazyCallGraph &, CGSCCUpdateResult &>; 530b57cec5SDimitry Andric template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; 540b57cec5SDimitry Andric template class OuterAnalysisManagerProxy<ModuleAnalysisManager, 550b57cec5SDimitry Andric LazyCallGraph::SCC, LazyCallGraph &>; 560b57cec5SDimitry Andric template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric /// Explicitly specialize the pass manager run method to handle call graph 590b57cec5SDimitry Andric /// updates. 600b57cec5SDimitry Andric template <> 610b57cec5SDimitry Andric PreservedAnalyses 620b57cec5SDimitry Andric PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, 630b57cec5SDimitry Andric CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC, 640b57cec5SDimitry Andric CGSCCAnalysisManager &AM, 650b57cec5SDimitry Andric LazyCallGraph &G, CGSCCUpdateResult &UR) { 660b57cec5SDimitry Andric // Request PassInstrumentation from analysis manager, will use it to run 670b57cec5SDimitry Andric // instrumenting callbacks for the passes later. 680b57cec5SDimitry Andric PassInstrumentation PI = 690b57cec5SDimitry Andric AM.getResult<PassInstrumentationAnalysis>(InitialC, G); 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric PreservedAnalyses PA = PreservedAnalyses::all(); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // The SCC may be refined while we are running passes over it, so set up 740b57cec5SDimitry Andric // a pointer that we can update. 750b57cec5SDimitry Andric LazyCallGraph::SCC *C = &InitialC; 760b57cec5SDimitry Andric 775ffd83dbSDimitry Andric // Get Function analysis manager from its proxy. 785ffd83dbSDimitry Andric FunctionAnalysisManager &FAM = 795ffd83dbSDimitry Andric AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*C)->getManager(); 800b57cec5SDimitry Andric 815ffd83dbSDimitry Andric for (auto &Pass : Passes) { 820b57cec5SDimitry Andric // Check the PassInstrumentation's BeforePass callbacks before running the 830b57cec5SDimitry Andric // pass, skip its execution completely if asked to (callback returns false). 840b57cec5SDimitry Andric if (!PI.runBeforePass(*Pass, *C)) 850b57cec5SDimitry Andric continue; 860b57cec5SDimitry Andric 87bdd1243dSDimitry Andric PreservedAnalyses PassPA = Pass->run(*C, AM, G, UR); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // Update the SCC if necessary. 900b57cec5SDimitry Andric C = UR.UpdatedC ? UR.UpdatedC : C; 915ffd83dbSDimitry Andric if (UR.UpdatedC) { 925ffd83dbSDimitry Andric // If C is updated, also create a proxy and update FAM inside the result. 935ffd83dbSDimitry Andric auto *ResultFAMCP = 945ffd83dbSDimitry Andric &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G); 955ffd83dbSDimitry Andric ResultFAMCP->updateFAM(FAM); 965ffd83dbSDimitry Andric } 970b57cec5SDimitry Andric 98bdd1243dSDimitry Andric // Intersect the final preserved analyses to compute the aggregate 99bdd1243dSDimitry Andric // preserved set for this pass manager. 100bdd1243dSDimitry Andric PA.intersect(PassPA); 101bdd1243dSDimitry Andric 1020b57cec5SDimitry Andric // If the CGSCC pass wasn't able to provide a valid updated SCC, the 1030b57cec5SDimitry Andric // current SCC may simply need to be skipped if invalid. 1040b57cec5SDimitry Andric if (UR.InvalidatedSCCs.count(C)) { 10506c3fb27SDimitry Andric PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 1060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n"); 1070b57cec5SDimitry Andric break; 1080b57cec5SDimitry Andric } 109bdd1243dSDimitry Andric 1100b57cec5SDimitry Andric // Check that we didn't miss any update scenario. 1110b57cec5SDimitry Andric assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // Update the analysis manager as each pass runs and potentially 1140b57cec5SDimitry Andric // invalidates analyses. 1150b57cec5SDimitry Andric AM.invalidate(*C, PassPA); 11606c3fb27SDimitry Andric 11706c3fb27SDimitry Andric PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric // Before we mark all of *this* SCC's analyses as preserved below, intersect 1210b57cec5SDimitry Andric // this with the cross-SCC preserved analysis set. This is used to allow 1220b57cec5SDimitry Andric // CGSCC passes to mutate ancestor SCCs and still trigger proper invalidation 1230b57cec5SDimitry Andric // for them. 1240b57cec5SDimitry Andric UR.CrossSCCPA.intersect(PA); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // Invalidation was handled after each pass in the above loop for the current 1270b57cec5SDimitry Andric // SCC. Therefore, the remaining analysis results in the AnalysisManager are 1280b57cec5SDimitry Andric // preserved. We mark this with a set so that we don't need to inspect each 1290b57cec5SDimitry Andric // one individually. 1300b57cec5SDimitry Andric PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric return PA; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 135e8d8bef9SDimitry Andric PreservedAnalyses 136e8d8bef9SDimitry Andric ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) { 137e8d8bef9SDimitry Andric // Setup the CGSCC analysis manager from its proxy. 138e8d8bef9SDimitry Andric CGSCCAnalysisManager &CGAM = 139e8d8bef9SDimitry Andric AM.getResult<CGSCCAnalysisManagerModuleProxy>(M).getManager(); 140e8d8bef9SDimitry Andric 141e8d8bef9SDimitry Andric // Get the call graph for this module. 142e8d8bef9SDimitry Andric LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M); 143e8d8bef9SDimitry Andric 144e8d8bef9SDimitry Andric // Get Function analysis manager from its proxy. 145e8d8bef9SDimitry Andric FunctionAnalysisManager &FAM = 146e8d8bef9SDimitry Andric AM.getCachedResult<FunctionAnalysisManagerModuleProxy>(M)->getManager(); 147e8d8bef9SDimitry Andric 148e8d8bef9SDimitry Andric // We keep worklists to allow us to push more work onto the pass manager as 149e8d8bef9SDimitry Andric // the passes are run. 150e8d8bef9SDimitry Andric SmallPriorityWorklist<LazyCallGraph::RefSCC *, 1> RCWorklist; 151e8d8bef9SDimitry Andric SmallPriorityWorklist<LazyCallGraph::SCC *, 1> CWorklist; 152e8d8bef9SDimitry Andric 153*0fca6ea1SDimitry Andric // Keep sets for invalidated SCCs that should be skipped when 154e8d8bef9SDimitry Andric // iterating off the worklists. 155e8d8bef9SDimitry Andric SmallPtrSet<LazyCallGraph::SCC *, 4> InvalidSCCSet; 156e8d8bef9SDimitry Andric 157e8d8bef9SDimitry Andric SmallDenseSet<std::pair<LazyCallGraph::Node *, LazyCallGraph::SCC *>, 4> 158e8d8bef9SDimitry Andric InlinedInternalEdges; 159e8d8bef9SDimitry Andric 160*0fca6ea1SDimitry Andric SmallVector<Function *, 4> DeadFunctions; 161*0fca6ea1SDimitry Andric 162*0fca6ea1SDimitry Andric CGSCCUpdateResult UR = {CWorklist, 163*0fca6ea1SDimitry Andric InvalidSCCSet, 164*0fca6ea1SDimitry Andric nullptr, 165*0fca6ea1SDimitry Andric PreservedAnalyses::all(), 166*0fca6ea1SDimitry Andric InlinedInternalEdges, 167*0fca6ea1SDimitry Andric DeadFunctions, 168*0fca6ea1SDimitry Andric {}}; 169e8d8bef9SDimitry Andric 170e8d8bef9SDimitry Andric // Request PassInstrumentation from analysis manager, will use it to run 171e8d8bef9SDimitry Andric // instrumenting callbacks for the passes later. 172e8d8bef9SDimitry Andric PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M); 173e8d8bef9SDimitry Andric 174e8d8bef9SDimitry Andric PreservedAnalyses PA = PreservedAnalyses::all(); 175e8d8bef9SDimitry Andric CG.buildRefSCCs(); 17681ad6265SDimitry Andric for (LazyCallGraph::RefSCC &RC : 17781ad6265SDimitry Andric llvm::make_early_inc_range(CG.postorder_ref_sccs())) { 178e8d8bef9SDimitry Andric assert(RCWorklist.empty() && 179e8d8bef9SDimitry Andric "Should always start with an empty RefSCC worklist"); 180e8d8bef9SDimitry Andric // The postorder_ref_sccs range we are walking is lazily constructed, so 181e8d8bef9SDimitry Andric // we only push the first one onto the worklist. The worklist allows us 182e8d8bef9SDimitry Andric // to capture *new* RefSCCs created during transformations. 183e8d8bef9SDimitry Andric // 184e8d8bef9SDimitry Andric // We really want to form RefSCCs lazily because that makes them cheaper 185e8d8bef9SDimitry Andric // to update as the program is simplified and allows us to have greater 186e8d8bef9SDimitry Andric // cache locality as forming a RefSCC touches all the parts of all the 187e8d8bef9SDimitry Andric // functions within that RefSCC. 188e8d8bef9SDimitry Andric // 189e8d8bef9SDimitry Andric // We also eagerly increment the iterator to the next position because 190e8d8bef9SDimitry Andric // the CGSCC passes below may delete the current RefSCC. 19181ad6265SDimitry Andric RCWorklist.insert(&RC); 192e8d8bef9SDimitry Andric 193e8d8bef9SDimitry Andric do { 194e8d8bef9SDimitry Andric LazyCallGraph::RefSCC *RC = RCWorklist.pop_back_val(); 195e8d8bef9SDimitry Andric assert(CWorklist.empty() && 196e8d8bef9SDimitry Andric "Should always start with an empty SCC worklist"); 197e8d8bef9SDimitry Andric 198e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Running an SCC pass across the RefSCC: " << *RC 199e8d8bef9SDimitry Andric << "\n"); 200e8d8bef9SDimitry Andric 201e8d8bef9SDimitry Andric // The top of the worklist may *also* be the same SCC we just ran over 202e8d8bef9SDimitry Andric // (and invalidated for). Keep track of that last SCC we processed due 203e8d8bef9SDimitry Andric // to SCC update to avoid redundant processing when an SCC is both just 204e8d8bef9SDimitry Andric // updated itself and at the top of the worklist. 205e8d8bef9SDimitry Andric LazyCallGraph::SCC *LastUpdatedC = nullptr; 206e8d8bef9SDimitry Andric 207e8d8bef9SDimitry Andric // Push the initial SCCs in reverse post-order as we'll pop off the 208e8d8bef9SDimitry Andric // back and so see this in post-order. 209e8d8bef9SDimitry Andric for (LazyCallGraph::SCC &C : llvm::reverse(*RC)) 210e8d8bef9SDimitry Andric CWorklist.insert(&C); 211e8d8bef9SDimitry Andric 212e8d8bef9SDimitry Andric do { 213e8d8bef9SDimitry Andric LazyCallGraph::SCC *C = CWorklist.pop_back_val(); 214e8d8bef9SDimitry Andric // Due to call graph mutations, we may have invalid SCCs or SCCs from 215e8d8bef9SDimitry Andric // other RefSCCs in the worklist. The invalid ones are dead and the 216e8d8bef9SDimitry Andric // other RefSCCs should be queued above, so we just need to skip both 217e8d8bef9SDimitry Andric // scenarios here. 218e8d8bef9SDimitry Andric if (InvalidSCCSet.count(C)) { 219e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping an invalid SCC...\n"); 220e8d8bef9SDimitry Andric continue; 221e8d8bef9SDimitry Andric } 222e8d8bef9SDimitry Andric if (LastUpdatedC == C) { 223e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping redundant run on SCC: " << *C << "\n"); 224e8d8bef9SDimitry Andric continue; 225e8d8bef9SDimitry Andric } 22681ad6265SDimitry Andric // We used to also check if the current SCC is part of the current 22781ad6265SDimitry Andric // RefSCC and bail if it wasn't, since it should be in RCWorklist. 22881ad6265SDimitry Andric // However, this can cause compile time explosions in some cases on 22981ad6265SDimitry Andric // modules with a huge RefSCC. If a non-trivial amount of SCCs in the 23081ad6265SDimitry Andric // huge RefSCC can become their own child RefSCC, we create one child 23181ad6265SDimitry Andric // RefSCC, bail on the current RefSCC, visit the child RefSCC, revisit 23281ad6265SDimitry Andric // the huge RefSCC, and repeat. By visiting all SCCs in the original 23381ad6265SDimitry Andric // RefSCC we create all the child RefSCCs in one pass of the RefSCC, 23481ad6265SDimitry Andric // rather one pass of the RefSCC creating one child RefSCC at a time. 235e8d8bef9SDimitry Andric 236e8d8bef9SDimitry Andric // Ensure we can proxy analysis updates from the CGSCC analysis manager 2375f757f3fSDimitry Andric // into the Function analysis manager by getting a proxy here. 238e8d8bef9SDimitry Andric // This also needs to update the FunctionAnalysisManager, as this may be 239e8d8bef9SDimitry Andric // the first time we see this SCC. 240e8d8bef9SDimitry Andric CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM( 241e8d8bef9SDimitry Andric FAM); 242e8d8bef9SDimitry Andric 243e8d8bef9SDimitry Andric // Each time we visit a new SCC pulled off the worklist, 244e8d8bef9SDimitry Andric // a transformation of a child SCC may have also modified this parent 245e8d8bef9SDimitry Andric // and invalidated analyses. So we invalidate using the update record's 246e8d8bef9SDimitry Andric // cross-SCC preserved set. This preserved set is intersected by any 247e8d8bef9SDimitry Andric // CGSCC pass that handles invalidation (primarily pass managers) prior 248e8d8bef9SDimitry Andric // to marking its SCC as preserved. That lets us track everything that 249e8d8bef9SDimitry Andric // might need invalidation across SCCs without excessive invalidations 250e8d8bef9SDimitry Andric // on a single SCC. 251e8d8bef9SDimitry Andric // 252e8d8bef9SDimitry Andric // This essentially allows SCC passes to freely invalidate analyses 253e8d8bef9SDimitry Andric // of any ancestor SCC. If this becomes detrimental to successfully 254e8d8bef9SDimitry Andric // caching analyses, we could force each SCC pass to manually 255e8d8bef9SDimitry Andric // invalidate the analyses for any SCCs other than themselves which 256e8d8bef9SDimitry Andric // are mutated. However, that seems to lose the robustness of the 257e8d8bef9SDimitry Andric // pass-manager driven invalidation scheme. 258e8d8bef9SDimitry Andric CGAM.invalidate(*C, UR.CrossSCCPA); 259e8d8bef9SDimitry Andric 260e8d8bef9SDimitry Andric do { 261e8d8bef9SDimitry Andric // Check that we didn't miss any update scenario. 262e8d8bef9SDimitry Andric assert(!InvalidSCCSet.count(C) && "Processing an invalid SCC!"); 263e8d8bef9SDimitry Andric assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 264e8d8bef9SDimitry Andric 265e8d8bef9SDimitry Andric LastUpdatedC = UR.UpdatedC; 266e8d8bef9SDimitry Andric UR.UpdatedC = nullptr; 267e8d8bef9SDimitry Andric 268e8d8bef9SDimitry Andric // Check the PassInstrumentation's BeforePass callbacks before 269e8d8bef9SDimitry Andric // running the pass, skip its execution completely if asked to 270e8d8bef9SDimitry Andric // (callback returns false). 271e8d8bef9SDimitry Andric if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C)) 272e8d8bef9SDimitry Andric continue; 273e8d8bef9SDimitry Andric 274bdd1243dSDimitry Andric PreservedAnalyses PassPA = Pass->run(*C, CGAM, CG, UR); 275e8d8bef9SDimitry Andric 276e8d8bef9SDimitry Andric // Update the SCC and RefSCC if necessary. 277e8d8bef9SDimitry Andric C = UR.UpdatedC ? UR.UpdatedC : C; 278e8d8bef9SDimitry Andric 279e8d8bef9SDimitry Andric if (UR.UpdatedC) { 280e8d8bef9SDimitry Andric // If we're updating the SCC, also update the FAM inside the proxy's 281e8d8bef9SDimitry Andric // result. 282e8d8bef9SDimitry Andric CGAM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG).updateFAM( 283e8d8bef9SDimitry Andric FAM); 284e8d8bef9SDimitry Andric } 285e8d8bef9SDimitry Andric 286bdd1243dSDimitry Andric // Intersect with the cross-SCC preserved set to capture any 287bdd1243dSDimitry Andric // cross-SCC invalidation. 288bdd1243dSDimitry Andric UR.CrossSCCPA.intersect(PassPA); 289bdd1243dSDimitry Andric // Intersect the preserved set so that invalidation of module 290bdd1243dSDimitry Andric // analyses will eventually occur when the module pass completes. 291bdd1243dSDimitry Andric PA.intersect(PassPA); 292bdd1243dSDimitry Andric 293e8d8bef9SDimitry Andric // If the CGSCC pass wasn't able to provide a valid updated SCC, 294e8d8bef9SDimitry Andric // the current SCC may simply need to be skipped if invalid. 295e8d8bef9SDimitry Andric if (UR.InvalidatedSCCs.count(C)) { 29606c3fb27SDimitry Andric PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 297e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n"); 298e8d8bef9SDimitry Andric break; 299e8d8bef9SDimitry Andric } 300bdd1243dSDimitry Andric 301e8d8bef9SDimitry Andric // Check that we didn't miss any update scenario. 302e8d8bef9SDimitry Andric assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 303e8d8bef9SDimitry Andric 304e8d8bef9SDimitry Andric // We handle invalidating the CGSCC analysis manager's information 305e8d8bef9SDimitry Andric // for the (potentially updated) SCC here. Note that any other SCCs 306e8d8bef9SDimitry Andric // whose structure has changed should have been invalidated by 307e8d8bef9SDimitry Andric // whatever was updating the call graph. This SCC gets invalidated 308e8d8bef9SDimitry Andric // late as it contains the nodes that were actively being 309e8d8bef9SDimitry Andric // processed. 310e8d8bef9SDimitry Andric CGAM.invalidate(*C, PassPA); 311e8d8bef9SDimitry Andric 31206c3fb27SDimitry Andric PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 31306c3fb27SDimitry Andric 314e8d8bef9SDimitry Andric // The pass may have restructured the call graph and refined the 315e8d8bef9SDimitry Andric // current SCC and/or RefSCC. We need to update our current SCC and 316e8d8bef9SDimitry Andric // RefSCC pointers to follow these. Also, when the current SCC is 317e8d8bef9SDimitry Andric // refined, re-run the SCC pass over the newly refined SCC in order 318e8d8bef9SDimitry Andric // to observe the most precise SCC model available. This inherently 319e8d8bef9SDimitry Andric // cannot cycle excessively as it only happens when we split SCCs 320e8d8bef9SDimitry Andric // apart, at most converging on a DAG of single nodes. 321e8d8bef9SDimitry Andric // FIXME: If we ever start having RefSCC passes, we'll want to 322e8d8bef9SDimitry Andric // iterate there too. 323e8d8bef9SDimitry Andric if (UR.UpdatedC) 324e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() 325e8d8bef9SDimitry Andric << "Re-running SCC passes after a refinement of the " 326e8d8bef9SDimitry Andric "current SCC: " 327e8d8bef9SDimitry Andric << *UR.UpdatedC << "\n"); 328e8d8bef9SDimitry Andric 329e8d8bef9SDimitry Andric // Note that both `C` and `RC` may at this point refer to deleted, 330e8d8bef9SDimitry Andric // invalid SCC and RefSCCs respectively. But we will short circuit 331e8d8bef9SDimitry Andric // the processing when we check them in the loop above. 332e8d8bef9SDimitry Andric } while (UR.UpdatedC); 333e8d8bef9SDimitry Andric } while (!CWorklist.empty()); 334e8d8bef9SDimitry Andric 335e8d8bef9SDimitry Andric // We only need to keep internal inlined edge information within 336e8d8bef9SDimitry Andric // a RefSCC, clear it to save on space and let the next time we visit 337e8d8bef9SDimitry Andric // any of these functions have a fresh start. 338e8d8bef9SDimitry Andric InlinedInternalEdges.clear(); 339e8d8bef9SDimitry Andric } while (!RCWorklist.empty()); 340e8d8bef9SDimitry Andric } 341e8d8bef9SDimitry Andric 342*0fca6ea1SDimitry Andric CG.removeDeadFunctions(DeadFunctions); 343*0fca6ea1SDimitry Andric for (Function *DeadF : DeadFunctions) 344*0fca6ea1SDimitry Andric DeadF->eraseFromParent(); 345*0fca6ea1SDimitry Andric 346*0fca6ea1SDimitry Andric #if defined(EXPENSIVE_CHECKS) 347*0fca6ea1SDimitry Andric // Verify that the call graph is still valid. 348*0fca6ea1SDimitry Andric CG.verify(); 349*0fca6ea1SDimitry Andric #endif 350*0fca6ea1SDimitry Andric 351e8d8bef9SDimitry Andric // By definition we preserve the call garph, all SCC analyses, and the 352e8d8bef9SDimitry Andric // analysis proxies by handling them above and in any nested pass managers. 353e8d8bef9SDimitry Andric PA.preserveSet<AllAnalysesOn<LazyCallGraph::SCC>>(); 354e8d8bef9SDimitry Andric PA.preserve<LazyCallGraphAnalysis>(); 355e8d8bef9SDimitry Andric PA.preserve<CGSCCAnalysisManagerModuleProxy>(); 356e8d8bef9SDimitry Andric PA.preserve<FunctionAnalysisManagerModuleProxy>(); 357e8d8bef9SDimitry Andric return PA; 358e8d8bef9SDimitry Andric } 359e8d8bef9SDimitry Andric 360e8d8bef9SDimitry Andric PreservedAnalyses DevirtSCCRepeatedPass::run(LazyCallGraph::SCC &InitialC, 361e8d8bef9SDimitry Andric CGSCCAnalysisManager &AM, 362e8d8bef9SDimitry Andric LazyCallGraph &CG, 363e8d8bef9SDimitry Andric CGSCCUpdateResult &UR) { 364e8d8bef9SDimitry Andric PreservedAnalyses PA = PreservedAnalyses::all(); 365e8d8bef9SDimitry Andric PassInstrumentation PI = 366e8d8bef9SDimitry Andric AM.getResult<PassInstrumentationAnalysis>(InitialC, CG); 367e8d8bef9SDimitry Andric 368e8d8bef9SDimitry Andric // The SCC may be refined while we are running passes over it, so set up 369e8d8bef9SDimitry Andric // a pointer that we can update. 370e8d8bef9SDimitry Andric LazyCallGraph::SCC *C = &InitialC; 371e8d8bef9SDimitry Andric 372e8d8bef9SDimitry Andric // Struct to track the counts of direct and indirect calls in each function 373e8d8bef9SDimitry Andric // of the SCC. 374e8d8bef9SDimitry Andric struct CallCount { 375e8d8bef9SDimitry Andric int Direct; 376e8d8bef9SDimitry Andric int Indirect; 377e8d8bef9SDimitry Andric }; 378e8d8bef9SDimitry Andric 379e8d8bef9SDimitry Andric // Put value handles on all of the indirect calls and return the number of 380e8d8bef9SDimitry Andric // direct calls for each function in the SCC. 381e8d8bef9SDimitry Andric auto ScanSCC = [](LazyCallGraph::SCC &C, 382e8d8bef9SDimitry Andric SmallMapVector<Value *, WeakTrackingVH, 16> &CallHandles) { 383e8d8bef9SDimitry Andric assert(CallHandles.empty() && "Must start with a clear set of handles."); 384e8d8bef9SDimitry Andric 385e8d8bef9SDimitry Andric SmallDenseMap<Function *, CallCount> CallCounts; 386e8d8bef9SDimitry Andric CallCount CountLocal = {0, 0}; 387e8d8bef9SDimitry Andric for (LazyCallGraph::Node &N : C) { 388e8d8bef9SDimitry Andric CallCount &Count = 389e8d8bef9SDimitry Andric CallCounts.insert(std::make_pair(&N.getFunction(), CountLocal)) 390e8d8bef9SDimitry Andric .first->second; 391e8d8bef9SDimitry Andric for (Instruction &I : instructions(N.getFunction())) 392e8d8bef9SDimitry Andric if (auto *CB = dyn_cast<CallBase>(&I)) { 393e8d8bef9SDimitry Andric if (CB->getCalledFunction()) { 394e8d8bef9SDimitry Andric ++Count.Direct; 395e8d8bef9SDimitry Andric } else { 396e8d8bef9SDimitry Andric ++Count.Indirect; 397e8d8bef9SDimitry Andric CallHandles.insert({CB, WeakTrackingVH(CB)}); 398e8d8bef9SDimitry Andric } 399e8d8bef9SDimitry Andric } 400e8d8bef9SDimitry Andric } 401e8d8bef9SDimitry Andric 402e8d8bef9SDimitry Andric return CallCounts; 403e8d8bef9SDimitry Andric }; 404e8d8bef9SDimitry Andric 405e8d8bef9SDimitry Andric UR.IndirectVHs.clear(); 406e8d8bef9SDimitry Andric // Populate the initial call handles and get the initial call counts. 407e8d8bef9SDimitry Andric auto CallCounts = ScanSCC(*C, UR.IndirectVHs); 408e8d8bef9SDimitry Andric 409e8d8bef9SDimitry Andric for (int Iteration = 0;; ++Iteration) { 410e8d8bef9SDimitry Andric if (!PI.runBeforePass<LazyCallGraph::SCC>(*Pass, *C)) 411e8d8bef9SDimitry Andric continue; 412e8d8bef9SDimitry Andric 413e8d8bef9SDimitry Andric PreservedAnalyses PassPA = Pass->run(*C, AM, CG, UR); 414e8d8bef9SDimitry Andric 415bdd1243dSDimitry Andric PA.intersect(PassPA); 416bdd1243dSDimitry Andric 41706c3fb27SDimitry Andric // If the CGSCC pass wasn't able to provide a valid updated SCC, the 41806c3fb27SDimitry Andric // current SCC may simply need to be skipped if invalid. 41906c3fb27SDimitry Andric if (UR.InvalidatedSCCs.count(C)) { 42006c3fb27SDimitry Andric PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA); 42106c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n"); 42206c3fb27SDimitry Andric break; 42306c3fb27SDimitry Andric } 42406c3fb27SDimitry Andric 42506c3fb27SDimitry Andric // Update the analysis manager with each run and intersect the total set 42606c3fb27SDimitry Andric // of preserved analyses so we're ready to iterate. 42706c3fb27SDimitry Andric AM.invalidate(*C, PassPA); 42806c3fb27SDimitry Andric 42906c3fb27SDimitry Andric PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA); 43006c3fb27SDimitry Andric 431e8d8bef9SDimitry Andric // If the SCC structure has changed, bail immediately and let the outer 432e8d8bef9SDimitry Andric // CGSCC layer handle any iteration to reflect the refined structure. 433bdd1243dSDimitry Andric if (UR.UpdatedC && UR.UpdatedC != C) 434e8d8bef9SDimitry Andric break; 435e8d8bef9SDimitry Andric 436e8d8bef9SDimitry Andric assert(C->begin() != C->end() && "Cannot have an empty SCC!"); 437e8d8bef9SDimitry Andric 438e8d8bef9SDimitry Andric // Check whether any of the handles were devirtualized. 439e8d8bef9SDimitry Andric bool Devirt = llvm::any_of(UR.IndirectVHs, [](auto &P) -> bool { 440e8d8bef9SDimitry Andric if (P.second) { 441e8d8bef9SDimitry Andric if (CallBase *CB = dyn_cast<CallBase>(P.second)) { 442e8d8bef9SDimitry Andric if (CB->getCalledFunction()) { 443e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Found devirtualized call: " << *CB << "\n"); 444e8d8bef9SDimitry Andric return true; 445e8d8bef9SDimitry Andric } 446e8d8bef9SDimitry Andric } 447e8d8bef9SDimitry Andric } 448e8d8bef9SDimitry Andric return false; 449e8d8bef9SDimitry Andric }); 450e8d8bef9SDimitry Andric 451e8d8bef9SDimitry Andric // Rescan to build up a new set of handles and count how many direct 452e8d8bef9SDimitry Andric // calls remain. If we decide to iterate, this also sets up the input to 453e8d8bef9SDimitry Andric // the next iteration. 454e8d8bef9SDimitry Andric UR.IndirectVHs.clear(); 455e8d8bef9SDimitry Andric auto NewCallCounts = ScanSCC(*C, UR.IndirectVHs); 456e8d8bef9SDimitry Andric 457e8d8bef9SDimitry Andric // If we haven't found an explicit devirtualization already see if we 458e8d8bef9SDimitry Andric // have decreased the number of indirect calls and increased the number 459e8d8bef9SDimitry Andric // of direct calls for any function in the SCC. This can be fooled by all 460e8d8bef9SDimitry Andric // manner of transformations such as DCE and other things, but seems to 461e8d8bef9SDimitry Andric // work well in practice. 462e8d8bef9SDimitry Andric if (!Devirt) 463e8d8bef9SDimitry Andric // Iterate over the keys in NewCallCounts, if Function also exists in 464e8d8bef9SDimitry Andric // CallCounts, make the check below. 465e8d8bef9SDimitry Andric for (auto &Pair : NewCallCounts) { 466e8d8bef9SDimitry Andric auto &CallCountNew = Pair.second; 467e8d8bef9SDimitry Andric auto CountIt = CallCounts.find(Pair.first); 468e8d8bef9SDimitry Andric if (CountIt != CallCounts.end()) { 469e8d8bef9SDimitry Andric const auto &CallCountOld = CountIt->second; 470e8d8bef9SDimitry Andric if (CallCountOld.Indirect > CallCountNew.Indirect && 471e8d8bef9SDimitry Andric CallCountOld.Direct < CallCountNew.Direct) { 472e8d8bef9SDimitry Andric Devirt = true; 473e8d8bef9SDimitry Andric break; 474e8d8bef9SDimitry Andric } 475e8d8bef9SDimitry Andric } 476e8d8bef9SDimitry Andric } 477e8d8bef9SDimitry Andric 478e8d8bef9SDimitry Andric if (!Devirt) { 479e8d8bef9SDimitry Andric break; 480e8d8bef9SDimitry Andric } 481e8d8bef9SDimitry Andric 482e8d8bef9SDimitry Andric // Otherwise, if we've already hit our max, we're done. 483e8d8bef9SDimitry Andric if (Iteration >= MaxIterations) { 484e8d8bef9SDimitry Andric if (AbortOnMaxDevirtIterationsReached) 485e8d8bef9SDimitry Andric report_fatal_error("Max devirtualization iterations reached"); 486e8d8bef9SDimitry Andric LLVM_DEBUG( 487e8d8bef9SDimitry Andric dbgs() << "Found another devirtualization after hitting the max " 488e8d8bef9SDimitry Andric "number of repetitions (" 489e8d8bef9SDimitry Andric << MaxIterations << ") on SCC: " << *C << "\n"); 490e8d8bef9SDimitry Andric break; 491e8d8bef9SDimitry Andric } 492e8d8bef9SDimitry Andric 493e8d8bef9SDimitry Andric LLVM_DEBUG( 494e8d8bef9SDimitry Andric dbgs() << "Repeating an SCC pass after finding a devirtualization in: " 495e8d8bef9SDimitry Andric << *C << "\n"); 496e8d8bef9SDimitry Andric 497e8d8bef9SDimitry Andric // Move over the new call counts in preparation for iterating. 498e8d8bef9SDimitry Andric CallCounts = std::move(NewCallCounts); 499e8d8bef9SDimitry Andric } 500e8d8bef9SDimitry Andric 501e8d8bef9SDimitry Andric // Note that we don't add any preserved entries here unlike a more normal 502e8d8bef9SDimitry Andric // "pass manager" because we only handle invalidation *between* iterations, 503e8d8bef9SDimitry Andric // not after the last iteration. 504e8d8bef9SDimitry Andric return PA; 505e8d8bef9SDimitry Andric } 506e8d8bef9SDimitry Andric 507e8d8bef9SDimitry Andric PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C, 508e8d8bef9SDimitry Andric CGSCCAnalysisManager &AM, 509e8d8bef9SDimitry Andric LazyCallGraph &CG, 510e8d8bef9SDimitry Andric CGSCCUpdateResult &UR) { 511e8d8bef9SDimitry Andric // Setup the function analysis manager from its proxy. 512e8d8bef9SDimitry Andric FunctionAnalysisManager &FAM = 513e8d8bef9SDimitry Andric AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); 514e8d8bef9SDimitry Andric 515e8d8bef9SDimitry Andric SmallVector<LazyCallGraph::Node *, 4> Nodes; 516e8d8bef9SDimitry Andric for (LazyCallGraph::Node &N : C) 517e8d8bef9SDimitry Andric Nodes.push_back(&N); 518e8d8bef9SDimitry Andric 519e8d8bef9SDimitry Andric // The SCC may get split while we are optimizing functions due to deleting 520e8d8bef9SDimitry Andric // edges. If this happens, the current SCC can shift, so keep track of 521e8d8bef9SDimitry Andric // a pointer we can overwrite. 522e8d8bef9SDimitry Andric LazyCallGraph::SCC *CurrentC = &C; 523e8d8bef9SDimitry Andric 524e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Running function passes across an SCC: " << C << "\n"); 525e8d8bef9SDimitry Andric 526e8d8bef9SDimitry Andric PreservedAnalyses PA = PreservedAnalyses::all(); 527e8d8bef9SDimitry Andric for (LazyCallGraph::Node *N : Nodes) { 528e8d8bef9SDimitry Andric // Skip nodes from other SCCs. These may have been split out during 529e8d8bef9SDimitry Andric // processing. We'll eventually visit those SCCs and pick up the nodes 530e8d8bef9SDimitry Andric // there. 531e8d8bef9SDimitry Andric if (CG.lookupSCC(*N) != CurrentC) 532e8d8bef9SDimitry Andric continue; 533e8d8bef9SDimitry Andric 534e8d8bef9SDimitry Andric Function &F = N->getFunction(); 535e8d8bef9SDimitry Andric 536349cc55cSDimitry Andric if (NoRerun && FAM.getCachedResult<ShouldNotRunFunctionPassesAnalysis>(F)) 537349cc55cSDimitry Andric continue; 538349cc55cSDimitry Andric 539e8d8bef9SDimitry Andric PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F); 540e8d8bef9SDimitry Andric if (!PI.runBeforePass<Function>(*Pass, F)) 541e8d8bef9SDimitry Andric continue; 542e8d8bef9SDimitry Andric 543bdd1243dSDimitry Andric PreservedAnalyses PassPA = Pass->run(F, FAM); 544e8d8bef9SDimitry Andric 545e8d8bef9SDimitry Andric // We know that the function pass couldn't have invalidated any other 546e8d8bef9SDimitry Andric // function's analyses (that's the contract of a function pass), so 547e8d8bef9SDimitry Andric // directly handle the function analysis manager's invalidation here. 548349cc55cSDimitry Andric FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA); 54906c3fb27SDimitry Andric 55006c3fb27SDimitry Andric PI.runAfterPass<Function>(*Pass, F, PassPA); 551e8d8bef9SDimitry Andric 552e8d8bef9SDimitry Andric // Then intersect the preserved set so that invalidation of module 553e8d8bef9SDimitry Andric // analyses will eventually occur when the module pass completes. 554e8d8bef9SDimitry Andric PA.intersect(std::move(PassPA)); 555e8d8bef9SDimitry Andric 556e8d8bef9SDimitry Andric // If the call graph hasn't been preserved, update it based on this 557e8d8bef9SDimitry Andric // function pass. This may also update the current SCC to point to 558e8d8bef9SDimitry Andric // a smaller, more refined SCC. 559e8d8bef9SDimitry Andric auto PAC = PA.getChecker<LazyCallGraphAnalysis>(); 560e8d8bef9SDimitry Andric if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) { 561e8d8bef9SDimitry Andric CurrentC = &updateCGAndAnalysisManagerForFunctionPass(CG, *CurrentC, *N, 562e8d8bef9SDimitry Andric AM, UR, FAM); 563e8d8bef9SDimitry Andric assert(CG.lookupSCC(*N) == CurrentC && 564e8d8bef9SDimitry Andric "Current SCC not updated to the SCC containing the current node!"); 565e8d8bef9SDimitry Andric } 566e8d8bef9SDimitry Andric } 567e8d8bef9SDimitry Andric 568e8d8bef9SDimitry Andric // By definition we preserve the proxy. And we preserve all analyses on 569e8d8bef9SDimitry Andric // Functions. This precludes *any* invalidation of function analyses by the 570e8d8bef9SDimitry Andric // proxy, but that's OK because we've taken care to invalidate analyses in 571e8d8bef9SDimitry Andric // the function analysis manager incrementally above. 572e8d8bef9SDimitry Andric PA.preserveSet<AllAnalysesOn<Function>>(); 573e8d8bef9SDimitry Andric PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 574e8d8bef9SDimitry Andric 575e8d8bef9SDimitry Andric // We've also ensured that we updated the call graph along the way. 576e8d8bef9SDimitry Andric PA.preserve<LazyCallGraphAnalysis>(); 577e8d8bef9SDimitry Andric 578e8d8bef9SDimitry Andric return PA; 579e8d8bef9SDimitry Andric } 580e8d8bef9SDimitry Andric 5810b57cec5SDimitry Andric bool CGSCCAnalysisManagerModuleProxy::Result::invalidate( 5820b57cec5SDimitry Andric Module &M, const PreservedAnalyses &PA, 5830b57cec5SDimitry Andric ModuleAnalysisManager::Invalidator &Inv) { 5840b57cec5SDimitry Andric // If literally everything is preserved, we're done. 5850b57cec5SDimitry Andric if (PA.areAllPreserved()) 5860b57cec5SDimitry Andric return false; // This is still a valid proxy. 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric // If this proxy or the call graph is going to be invalidated, we also need 5890b57cec5SDimitry Andric // to clear all the keys coming from that analysis. 5900b57cec5SDimitry Andric // 5910b57cec5SDimitry Andric // We also directly invalidate the FAM's module proxy if necessary, and if 5920b57cec5SDimitry Andric // that proxy isn't preserved we can't preserve this proxy either. We rely on 5930b57cec5SDimitry Andric // it to handle module -> function analysis invalidation in the face of 5940b57cec5SDimitry Andric // structural changes and so if it's unavailable we conservatively clear the 5950b57cec5SDimitry Andric // entire SCC layer as well rather than trying to do invalidation ourselves. 5960b57cec5SDimitry Andric auto PAC = PA.getChecker<CGSCCAnalysisManagerModuleProxy>(); 5970b57cec5SDimitry Andric if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>()) || 5980b57cec5SDimitry Andric Inv.invalidate<LazyCallGraphAnalysis>(M, PA) || 5990b57cec5SDimitry Andric Inv.invalidate<FunctionAnalysisManagerModuleProxy>(M, PA)) { 6000b57cec5SDimitry Andric InnerAM->clear(); 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // And the proxy itself should be marked as invalid so that we can observe 6030b57cec5SDimitry Andric // the new call graph. This isn't strictly necessary because we cheat 6040b57cec5SDimitry Andric // above, but is still useful. 6050b57cec5SDimitry Andric return true; 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // Directly check if the relevant set is preserved so we can short circuit 6090b57cec5SDimitry Andric // invalidating SCCs below. 6100b57cec5SDimitry Andric bool AreSCCAnalysesPreserved = 6110b57cec5SDimitry Andric PA.allAnalysesInSetPreserved<AllAnalysesOn<LazyCallGraph::SCC>>(); 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric // Ok, we have a graph, so we can propagate the invalidation down into it. 6140b57cec5SDimitry Andric G->buildRefSCCs(); 6150b57cec5SDimitry Andric for (auto &RC : G->postorder_ref_sccs()) 6160b57cec5SDimitry Andric for (auto &C : RC) { 617bdd1243dSDimitry Andric std::optional<PreservedAnalyses> InnerPA; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric // Check to see whether the preserved set needs to be adjusted based on 6200b57cec5SDimitry Andric // module-level analysis invalidation triggering deferred invalidation 6210b57cec5SDimitry Andric // for this SCC. 6220b57cec5SDimitry Andric if (auto *OuterProxy = 6230b57cec5SDimitry Andric InnerAM->getCachedResult<ModuleAnalysisManagerCGSCCProxy>(C)) 6240b57cec5SDimitry Andric for (const auto &OuterInvalidationPair : 6250b57cec5SDimitry Andric OuterProxy->getOuterInvalidations()) { 6260b57cec5SDimitry Andric AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 6270b57cec5SDimitry Andric const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 6280b57cec5SDimitry Andric if (Inv.invalidate(OuterAnalysisID, M, PA)) { 6290b57cec5SDimitry Andric if (!InnerPA) 6300b57cec5SDimitry Andric InnerPA = PA; 6310b57cec5SDimitry Andric for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 6320b57cec5SDimitry Andric InnerPA->abandon(InnerAnalysisID); 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric } 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric // Check if we needed a custom PA set. If so we'll need to run the inner 6370b57cec5SDimitry Andric // invalidation. 6380b57cec5SDimitry Andric if (InnerPA) { 6390b57cec5SDimitry Andric InnerAM->invalidate(C, *InnerPA); 6400b57cec5SDimitry Andric continue; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric // Otherwise we only need to do invalidation if the original PA set didn't 6440b57cec5SDimitry Andric // preserve all SCC analyses. 6450b57cec5SDimitry Andric if (!AreSCCAnalysesPreserved) 6460b57cec5SDimitry Andric InnerAM->invalidate(C, PA); 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric // Return false to indicate that this result is still a valid proxy. 6500b57cec5SDimitry Andric return false; 6510b57cec5SDimitry Andric } 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric template <> 6540b57cec5SDimitry Andric CGSCCAnalysisManagerModuleProxy::Result 6550b57cec5SDimitry Andric CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM) { 6560b57cec5SDimitry Andric // Force the Function analysis manager to also be available so that it can 6570b57cec5SDimitry Andric // be accessed in an SCC analysis and proxied onward to function passes. 6580b57cec5SDimitry Andric // FIXME: It is pretty awkward to just drop the result here and assert that 6590b57cec5SDimitry Andric // we can find it again later. 6600b57cec5SDimitry Andric (void)AM.getResult<FunctionAnalysisManagerModuleProxy>(M); 6610b57cec5SDimitry Andric 6620b57cec5SDimitry Andric return Result(*InnerAM, AM.getResult<LazyCallGraphAnalysis>(M)); 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric AnalysisKey FunctionAnalysisManagerCGSCCProxy::Key; 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric FunctionAnalysisManagerCGSCCProxy::Result 6680b57cec5SDimitry Andric FunctionAnalysisManagerCGSCCProxy::run(LazyCallGraph::SCC &C, 6690b57cec5SDimitry Andric CGSCCAnalysisManager &AM, 6700b57cec5SDimitry Andric LazyCallGraph &CG) { 6715ffd83dbSDimitry Andric // Note: unconditionally getting checking that the proxy exists may get it at 6725ffd83dbSDimitry Andric // this point. There are cases when this is being run unnecessarily, but 6735ffd83dbSDimitry Andric // it is cheap and having the assertion in place is more valuable. 6745ffd83dbSDimitry Andric auto &MAMProxy = AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG); 6750b57cec5SDimitry Andric Module &M = *C.begin()->getFunction().getParent(); 6765ffd83dbSDimitry Andric bool ProxyExists = 6775ffd83dbSDimitry Andric MAMProxy.cachedResultExists<FunctionAnalysisManagerModuleProxy>(M); 6785ffd83dbSDimitry Andric assert(ProxyExists && 6795ffd83dbSDimitry Andric "The CGSCC pass manager requires that the FAM module proxy is run " 6805ffd83dbSDimitry Andric "on the module prior to entering the CGSCC walk"); 6815ffd83dbSDimitry Andric (void)ProxyExists; 6820b57cec5SDimitry Andric 6835ffd83dbSDimitry Andric // We just return an empty result. The caller will use the updateFAM interface 6845ffd83dbSDimitry Andric // to correctly register the relevant FunctionAnalysisManager based on the 6855ffd83dbSDimitry Andric // context in which this proxy is run. 6865ffd83dbSDimitry Andric return Result(); 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric bool FunctionAnalysisManagerCGSCCProxy::Result::invalidate( 6900b57cec5SDimitry Andric LazyCallGraph::SCC &C, const PreservedAnalyses &PA, 6910b57cec5SDimitry Andric CGSCCAnalysisManager::Invalidator &Inv) { 6920b57cec5SDimitry Andric // If literally everything is preserved, we're done. 6930b57cec5SDimitry Andric if (PA.areAllPreserved()) 6940b57cec5SDimitry Andric return false; // This is still a valid proxy. 6950b57cec5SDimitry Andric 6965ffd83dbSDimitry Andric // All updates to preserve valid results are done below, so we don't need to 6975ffd83dbSDimitry Andric // invalidate this proxy. 6980b57cec5SDimitry Andric // 6990b57cec5SDimitry Andric // Note that in order to preserve this proxy, a module pass must ensure that 7000b57cec5SDimitry Andric // the FAM has been completely updated to handle the deletion of functions. 7010b57cec5SDimitry Andric // Specifically, any FAM-cached results for those functions need to have been 7020b57cec5SDimitry Andric // forcibly cleared. When preserved, this proxy will only invalidate results 7030b57cec5SDimitry Andric // cached on functions *still in the module* at the end of the module pass. 7040b57cec5SDimitry Andric auto PAC = PA.getChecker<FunctionAnalysisManagerCGSCCProxy>(); 7050b57cec5SDimitry Andric if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<LazyCallGraph::SCC>>()) { 7060b57cec5SDimitry Andric for (LazyCallGraph::Node &N : C) 707fe6060f1SDimitry Andric FAM->invalidate(N.getFunction(), PA); 7080b57cec5SDimitry Andric 7095ffd83dbSDimitry Andric return false; 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric // Directly check if the relevant set is preserved. 7130b57cec5SDimitry Andric bool AreFunctionAnalysesPreserved = 7140b57cec5SDimitry Andric PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>(); 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric // Now walk all the functions to see if any inner analysis invalidation is 7170b57cec5SDimitry Andric // necessary. 7180b57cec5SDimitry Andric for (LazyCallGraph::Node &N : C) { 7190b57cec5SDimitry Andric Function &F = N.getFunction(); 720bdd1243dSDimitry Andric std::optional<PreservedAnalyses> FunctionPA; 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric // Check to see whether the preserved set needs to be pruned based on 7230b57cec5SDimitry Andric // SCC-level analysis invalidation that triggers deferred invalidation 7240b57cec5SDimitry Andric // registered with the outer analysis manager proxy for this function. 7250b57cec5SDimitry Andric if (auto *OuterProxy = 7260b57cec5SDimitry Andric FAM->getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F)) 7270b57cec5SDimitry Andric for (const auto &OuterInvalidationPair : 7280b57cec5SDimitry Andric OuterProxy->getOuterInvalidations()) { 7290b57cec5SDimitry Andric AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 7300b57cec5SDimitry Andric const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 7310b57cec5SDimitry Andric if (Inv.invalidate(OuterAnalysisID, C, PA)) { 7320b57cec5SDimitry Andric if (!FunctionPA) 7330b57cec5SDimitry Andric FunctionPA = PA; 7340b57cec5SDimitry Andric for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 7350b57cec5SDimitry Andric FunctionPA->abandon(InnerAnalysisID); 7360b57cec5SDimitry Andric } 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric // Check if we needed a custom PA set, and if so we'll need to run the 7400b57cec5SDimitry Andric // inner invalidation. 7410b57cec5SDimitry Andric if (FunctionPA) { 7420b57cec5SDimitry Andric FAM->invalidate(F, *FunctionPA); 7430b57cec5SDimitry Andric continue; 7440b57cec5SDimitry Andric } 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric // Otherwise we only need to do invalidation if the original PA set didn't 7470b57cec5SDimitry Andric // preserve all function analyses. 7480b57cec5SDimitry Andric if (!AreFunctionAnalysesPreserved) 7490b57cec5SDimitry Andric FAM->invalidate(F, PA); 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric // Return false to indicate that this result is still a valid proxy. 7530b57cec5SDimitry Andric return false; 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric } // end namespace llvm 7570b57cec5SDimitry Andric 7585ffd83dbSDimitry Andric /// When a new SCC is created for the graph we first update the 7595ffd83dbSDimitry Andric /// FunctionAnalysisManager in the Proxy's result. 7605ffd83dbSDimitry Andric /// As there might be function analysis results cached for the functions now in 7615ffd83dbSDimitry Andric /// that SCC, two forms of updates are required. 7620b57cec5SDimitry Andric /// 7630b57cec5SDimitry Andric /// First, a proxy from the SCC to the FunctionAnalysisManager needs to be 7640b57cec5SDimitry Andric /// created so that any subsequent invalidation events to the SCC are 7650b57cec5SDimitry Andric /// propagated to the function analysis results cached for functions within it. 7660b57cec5SDimitry Andric /// 7670b57cec5SDimitry Andric /// Second, if any of the functions within the SCC have analysis results with 7680b57cec5SDimitry Andric /// outer analysis dependencies, then those dependencies would point to the 7690b57cec5SDimitry Andric /// *wrong* SCC's analysis result. We forcibly invalidate the necessary 7700b57cec5SDimitry Andric /// function analyses so that they don't retain stale handles. 7710b57cec5SDimitry Andric static void updateNewSCCFunctionAnalyses(LazyCallGraph::SCC &C, 7720b57cec5SDimitry Andric LazyCallGraph &G, 7735ffd83dbSDimitry Andric CGSCCAnalysisManager &AM, 7745ffd83dbSDimitry Andric FunctionAnalysisManager &FAM) { 7755ffd83dbSDimitry Andric AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, G).updateFAM(FAM); 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric // Now walk the functions in this SCC and invalidate any function analysis 7780b57cec5SDimitry Andric // results that might have outer dependencies on an SCC analysis. 7790b57cec5SDimitry Andric for (LazyCallGraph::Node &N : C) { 7800b57cec5SDimitry Andric Function &F = N.getFunction(); 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric auto *OuterProxy = 7830b57cec5SDimitry Andric FAM.getCachedResult<CGSCCAnalysisManagerFunctionProxy>(F); 7840b57cec5SDimitry Andric if (!OuterProxy) 7850b57cec5SDimitry Andric // No outer analyses were queried, nothing to do. 7860b57cec5SDimitry Andric continue; 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric // Forcibly abandon all the inner analyses with dependencies, but 7890b57cec5SDimitry Andric // invalidate nothing else. 7900b57cec5SDimitry Andric auto PA = PreservedAnalyses::all(); 7910b57cec5SDimitry Andric for (const auto &OuterInvalidationPair : 7920b57cec5SDimitry Andric OuterProxy->getOuterInvalidations()) { 7930b57cec5SDimitry Andric const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 7940b57cec5SDimitry Andric for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 7950b57cec5SDimitry Andric PA.abandon(InnerAnalysisID); 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric // Now invalidate anything we found. 7990b57cec5SDimitry Andric FAM.invalidate(F, PA); 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric } 8020b57cec5SDimitry Andric 8030b57cec5SDimitry Andric /// Helper function to update both the \c CGSCCAnalysisManager \p AM and the \c 8040b57cec5SDimitry Andric /// CGSCCPassManager's \c CGSCCUpdateResult \p UR based on a range of newly 8050b57cec5SDimitry Andric /// added SCCs. 8060b57cec5SDimitry Andric /// 8070b57cec5SDimitry Andric /// The range of new SCCs must be in postorder already. The SCC they were split 8080b57cec5SDimitry Andric /// out of must be provided as \p C. The current node being mutated and 8090b57cec5SDimitry Andric /// triggering updates must be passed as \p N. 8100b57cec5SDimitry Andric /// 8110b57cec5SDimitry Andric /// This function returns the SCC containing \p N. This will be either \p C if 8120b57cec5SDimitry Andric /// no new SCCs have been split out, or it will be the new SCC containing \p N. 8130b57cec5SDimitry Andric template <typename SCCRangeT> 8140b57cec5SDimitry Andric static LazyCallGraph::SCC * 8150b57cec5SDimitry Andric incorporateNewSCCRange(const SCCRangeT &NewSCCRange, LazyCallGraph &G, 8160b57cec5SDimitry Andric LazyCallGraph::Node &N, LazyCallGraph::SCC *C, 8170b57cec5SDimitry Andric CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { 8180b57cec5SDimitry Andric using SCC = LazyCallGraph::SCC; 8190b57cec5SDimitry Andric 820e8d8bef9SDimitry Andric if (NewSCCRange.empty()) 8210b57cec5SDimitry Andric return C; 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric // Add the current SCC to the worklist as its shape has changed. 8240b57cec5SDimitry Andric UR.CWorklist.insert(C); 8250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist:" << *C 8260b57cec5SDimitry Andric << "\n"); 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric SCC *OldC = C; 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric // Update the current SCC. Note that if we have new SCCs, this must actually 8310b57cec5SDimitry Andric // change the SCC. 8320b57cec5SDimitry Andric assert(C != &*NewSCCRange.begin() && 8330b57cec5SDimitry Andric "Cannot insert new SCCs without changing current SCC!"); 8340b57cec5SDimitry Andric C = &*NewSCCRange.begin(); 8350b57cec5SDimitry Andric assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric // If we had a cached FAM proxy originally, we will want to create more of 8380b57cec5SDimitry Andric // them for each SCC that was split off. 8395ffd83dbSDimitry Andric FunctionAnalysisManager *FAM = nullptr; 8405ffd83dbSDimitry Andric if (auto *FAMProxy = 8415ffd83dbSDimitry Andric AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>(*OldC)) 8425ffd83dbSDimitry Andric FAM = &FAMProxy->getManager(); 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric // We need to propagate an invalidation call to all but the newly current SCC 8450b57cec5SDimitry Andric // because the outer pass manager won't do that for us after splitting them. 8460b57cec5SDimitry Andric // FIXME: We should accept a PreservedAnalysis from the CG updater so that if 8470b57cec5SDimitry Andric // there are preserved analysis we can avoid invalidating them here for 8480b57cec5SDimitry Andric // split-off SCCs. 8490b57cec5SDimitry Andric // We know however that this will preserve any FAM proxy so go ahead and mark 8500b57cec5SDimitry Andric // that. 851349cc55cSDimitry Andric auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 8520b57cec5SDimitry Andric PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 8530b57cec5SDimitry Andric AM.invalidate(*OldC, PA); 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric // Ensure the now-current SCC's function analyses are updated. 8565ffd83dbSDimitry Andric if (FAM) 8575ffd83dbSDimitry Andric updateNewSCCFunctionAnalyses(*C, G, AM, *FAM); 8580b57cec5SDimitry Andric 859fe6060f1SDimitry Andric for (SCC &NewC : llvm::reverse(llvm::drop_begin(NewSCCRange))) { 8600b57cec5SDimitry Andric assert(C != &NewC && "No need to re-visit the current SCC!"); 8610b57cec5SDimitry Andric assert(OldC != &NewC && "Already handled the original SCC!"); 8620b57cec5SDimitry Andric UR.CWorklist.insert(&NewC); 8630b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing a newly formed SCC:" << NewC << "\n"); 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric // Ensure new SCCs' function analyses are updated. 8665ffd83dbSDimitry Andric if (FAM) 8675ffd83dbSDimitry Andric updateNewSCCFunctionAnalyses(NewC, G, AM, *FAM); 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric // Also propagate a normal invalidation to the new SCC as only the current 8700b57cec5SDimitry Andric // will get one from the pass manager infrastructure. 8710b57cec5SDimitry Andric AM.invalidate(NewC, PA); 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric return C; 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8765ffd83dbSDimitry Andric static LazyCallGraph::SCC &updateCGAndAnalysisManagerForPass( 8770b57cec5SDimitry Andric LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 8785ffd83dbSDimitry Andric CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 8795ffd83dbSDimitry Andric FunctionAnalysisManager &FAM, bool FunctionPass) { 8800b57cec5SDimitry Andric using Node = LazyCallGraph::Node; 8810b57cec5SDimitry Andric using Edge = LazyCallGraph::Edge; 8820b57cec5SDimitry Andric using SCC = LazyCallGraph::SCC; 8830b57cec5SDimitry Andric using RefSCC = LazyCallGraph::RefSCC; 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric RefSCC &InitialRC = InitialC.getOuterRefSCC(); 8860b57cec5SDimitry Andric SCC *C = &InitialC; 8870b57cec5SDimitry Andric RefSCC *RC = &InitialRC; 8880b57cec5SDimitry Andric Function &F = N.getFunction(); 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric // Walk the function body and build up the set of retained, promoted, and 8910b57cec5SDimitry Andric // demoted edges. 8920b57cec5SDimitry Andric SmallVector<Constant *, 16> Worklist; 8930b57cec5SDimitry Andric SmallPtrSet<Constant *, 16> Visited; 8940b57cec5SDimitry Andric SmallPtrSet<Node *, 16> RetainedEdges; 8950b57cec5SDimitry Andric SmallSetVector<Node *, 4> PromotedRefTargets; 8960b57cec5SDimitry Andric SmallSetVector<Node *, 4> DemotedCallTargets; 8975ffd83dbSDimitry Andric SmallSetVector<Node *, 4> NewCallEdges; 8985ffd83dbSDimitry Andric SmallSetVector<Node *, 4> NewRefEdges; 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andric // First walk the function and handle all called functions. We do this first 9010b57cec5SDimitry Andric // because if there is a single call edge, whether there are ref edges is 9020b57cec5SDimitry Andric // irrelevant. 903e8d8bef9SDimitry Andric for (Instruction &I : instructions(F)) { 904e8d8bef9SDimitry Andric if (auto *CB = dyn_cast<CallBase>(&I)) { 905e8d8bef9SDimitry Andric if (Function *Callee = CB->getCalledFunction()) { 9060b57cec5SDimitry Andric if (Visited.insert(Callee).second && !Callee->isDeclaration()) { 907e8d8bef9SDimitry Andric Node *CalleeN = G.lookup(*Callee); 908e8d8bef9SDimitry Andric assert(CalleeN && 909e8d8bef9SDimitry Andric "Visited function should already have an associated node"); 910e8d8bef9SDimitry Andric Edge *E = N->lookup(*CalleeN); 9115ffd83dbSDimitry Andric assert((E || !FunctionPass) && 9125ffd83dbSDimitry Andric "No function transformations should introduce *new* " 9130b57cec5SDimitry Andric "call edges! Any new calls should be modeled as " 9140b57cec5SDimitry Andric "promoted existing ref edges!"); 915e8d8bef9SDimitry Andric bool Inserted = RetainedEdges.insert(CalleeN).second; 9160b57cec5SDimitry Andric (void)Inserted; 9170b57cec5SDimitry Andric assert(Inserted && "We should never visit a function twice."); 9185ffd83dbSDimitry Andric if (!E) 919e8d8bef9SDimitry Andric NewCallEdges.insert(CalleeN); 9205ffd83dbSDimitry Andric else if (!E->isCall()) 921e8d8bef9SDimitry Andric PromotedRefTargets.insert(CalleeN); 922e8d8bef9SDimitry Andric } 923e8d8bef9SDimitry Andric } else { 924e8d8bef9SDimitry Andric // We can miss devirtualization if an indirect call is created then 925e8d8bef9SDimitry Andric // promoted before updateCGAndAnalysisManagerForPass runs. 926e8d8bef9SDimitry Andric auto *Entry = UR.IndirectVHs.find(CB); 927e8d8bef9SDimitry Andric if (Entry == UR.IndirectVHs.end()) 928e8d8bef9SDimitry Andric UR.IndirectVHs.insert({CB, WeakTrackingVH(CB)}); 929e8d8bef9SDimitry Andric else if (!Entry->second) 930e8d8bef9SDimitry Andric Entry->second = WeakTrackingVH(CB); 931e8d8bef9SDimitry Andric } 932e8d8bef9SDimitry Andric } 9330b57cec5SDimitry Andric } 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric // Now walk all references. 9360b57cec5SDimitry Andric for (Instruction &I : instructions(F)) 9370b57cec5SDimitry Andric for (Value *Op : I.operand_values()) 938e8d8bef9SDimitry Andric if (auto *OpC = dyn_cast<Constant>(Op)) 939e8d8bef9SDimitry Andric if (Visited.insert(OpC).second) 940e8d8bef9SDimitry Andric Worklist.push_back(OpC); 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric auto VisitRef = [&](Function &Referee) { 943e8d8bef9SDimitry Andric Node *RefereeN = G.lookup(Referee); 944e8d8bef9SDimitry Andric assert(RefereeN && 945e8d8bef9SDimitry Andric "Visited function should already have an associated node"); 946e8d8bef9SDimitry Andric Edge *E = N->lookup(*RefereeN); 9475ffd83dbSDimitry Andric assert((E || !FunctionPass) && 9485ffd83dbSDimitry Andric "No function transformations should introduce *new* ref " 9490b57cec5SDimitry Andric "edges! Any new ref edges would require IPO which " 9500b57cec5SDimitry Andric "function passes aren't allowed to do!"); 951e8d8bef9SDimitry Andric bool Inserted = RetainedEdges.insert(RefereeN).second; 9520b57cec5SDimitry Andric (void)Inserted; 9530b57cec5SDimitry Andric assert(Inserted && "We should never visit a function twice."); 9545ffd83dbSDimitry Andric if (!E) 955e8d8bef9SDimitry Andric NewRefEdges.insert(RefereeN); 9565ffd83dbSDimitry Andric else if (E->isCall()) 957e8d8bef9SDimitry Andric DemotedCallTargets.insert(RefereeN); 9580b57cec5SDimitry Andric }; 9590b57cec5SDimitry Andric LazyCallGraph::visitReferences(Worklist, Visited, VisitRef); 9600b57cec5SDimitry Andric 9615ffd83dbSDimitry Andric // Handle new ref edges. 9625ffd83dbSDimitry Andric for (Node *RefTarget : NewRefEdges) { 9635ffd83dbSDimitry Andric SCC &TargetC = *G.lookupSCC(*RefTarget); 9645ffd83dbSDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 9655ffd83dbSDimitry Andric (void)TargetRC; 9665ffd83dbSDimitry Andric // TODO: This only allows trivial edges to be added for now. 967fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS 9685ffd83dbSDimitry Andric assert((RC == &TargetRC || 9695ffd83dbSDimitry Andric RC->isAncestorOf(TargetRC)) && "New ref edge is not trivial!"); 970fe6060f1SDimitry Andric #endif 9715ffd83dbSDimitry Andric RC->insertTrivialRefEdge(N, *RefTarget); 9725ffd83dbSDimitry Andric } 9735ffd83dbSDimitry Andric 9745ffd83dbSDimitry Andric // Handle new call edges. 9755ffd83dbSDimitry Andric for (Node *CallTarget : NewCallEdges) { 9765ffd83dbSDimitry Andric SCC &TargetC = *G.lookupSCC(*CallTarget); 9775ffd83dbSDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 9785ffd83dbSDimitry Andric (void)TargetRC; 9795ffd83dbSDimitry Andric // TODO: This only allows trivial edges to be added for now. 980fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS 9815ffd83dbSDimitry Andric assert((RC == &TargetRC || 9825ffd83dbSDimitry Andric RC->isAncestorOf(TargetRC)) && "New call edge is not trivial!"); 983fe6060f1SDimitry Andric #endif 984e8d8bef9SDimitry Andric // Add a trivial ref edge to be promoted later on alongside 985e8d8bef9SDimitry Andric // PromotedRefTargets. 986e8d8bef9SDimitry Andric RC->insertTrivialRefEdge(N, *CallTarget); 9875ffd83dbSDimitry Andric } 9885ffd83dbSDimitry Andric 9890b57cec5SDimitry Andric // Include synthetic reference edges to known, defined lib functions. 990e8d8bef9SDimitry Andric for (auto *LibFn : G.getLibFunctions()) 9910b57cec5SDimitry Andric // While the list of lib functions doesn't have repeats, don't re-visit 9920b57cec5SDimitry Andric // anything handled above. 993e8d8bef9SDimitry Andric if (!Visited.count(LibFn)) 994e8d8bef9SDimitry Andric VisitRef(*LibFn); 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric // First remove all of the edges that are no longer present in this function. 9970b57cec5SDimitry Andric // The first step makes these edges uniformly ref edges and accumulates them 9980b57cec5SDimitry Andric // into a separate data structure so removal doesn't invalidate anything. 9990b57cec5SDimitry Andric SmallVector<Node *, 4> DeadTargets; 10000b57cec5SDimitry Andric for (Edge &E : *N) { 10010b57cec5SDimitry Andric if (RetainedEdges.count(&E.getNode())) 10020b57cec5SDimitry Andric continue; 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric SCC &TargetC = *G.lookupSCC(E.getNode()); 10050b57cec5SDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 10060b57cec5SDimitry Andric if (&TargetRC == RC && E.isCall()) { 10070b57cec5SDimitry Andric if (C != &TargetC) { 10080b57cec5SDimitry Andric // For separate SCCs this is trivial. 10090b57cec5SDimitry Andric RC->switchTrivialInternalEdgeToRef(N, E.getNode()); 10100b57cec5SDimitry Andric } else { 10110b57cec5SDimitry Andric // Now update the call graph. 10120b57cec5SDimitry Andric C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, E.getNode()), 10130b57cec5SDimitry Andric G, N, C, AM, UR); 10140b57cec5SDimitry Andric } 10150b57cec5SDimitry Andric } 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric // Now that this is ready for actual removal, put it into our list. 10180b57cec5SDimitry Andric DeadTargets.push_back(&E.getNode()); 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric // Remove the easy cases quickly and actually pull them out of our list. 1021e8d8bef9SDimitry Andric llvm::erase_if(DeadTargets, [&](Node *TargetN) { 10220b57cec5SDimitry Andric SCC &TargetC = *G.lookupSCC(*TargetN); 10230b57cec5SDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric // We can't trivially remove internal targets, so skip 10260b57cec5SDimitry Andric // those. 10270b57cec5SDimitry Andric if (&TargetRC == RC) 10280b57cec5SDimitry Andric return false; 10290b57cec5SDimitry Andric 1030e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting outgoing edge from '" << N << "' to '" 1031fe6060f1SDimitry Andric << *TargetN << "'\n"); 1032fe6060f1SDimitry Andric RC->removeOutgoingEdge(N, *TargetN); 10330b57cec5SDimitry Andric return true; 1034e8d8bef9SDimitry Andric }); 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric // Next demote all the call edges that are now ref edges. This helps make 10370b57cec5SDimitry Andric // the SCCs small which should minimize the work below as we don't want to 10380b57cec5SDimitry Andric // form cycles that this would break. 10390b57cec5SDimitry Andric for (Node *RefTarget : DemotedCallTargets) { 10400b57cec5SDimitry Andric SCC &TargetC = *G.lookupSCC(*RefTarget); 10410b57cec5SDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 10420b57cec5SDimitry Andric 10430b57cec5SDimitry Andric // The easy case is when the target RefSCC is not this RefSCC. This is 10440b57cec5SDimitry Andric // only supported when the target RefSCC is a child of this RefSCC. 10450b57cec5SDimitry Andric if (&TargetRC != RC) { 1046fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS 10470b57cec5SDimitry Andric assert(RC->isAncestorOf(TargetRC) && 10480b57cec5SDimitry Andric "Cannot potentially form RefSCC cycles here!"); 1049fe6060f1SDimitry Andric #endif 10500b57cec5SDimitry Andric RC->switchOutgoingEdgeToRef(N, *RefTarget); 10510b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Switch outgoing call edge to a ref edge from '" << N 10520b57cec5SDimitry Andric << "' to '" << *RefTarget << "'\n"); 10530b57cec5SDimitry Andric continue; 10540b57cec5SDimitry Andric } 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric // We are switching an internal call edge to a ref edge. This may split up 10570b57cec5SDimitry Andric // some SCCs. 10580b57cec5SDimitry Andric if (C != &TargetC) { 10590b57cec5SDimitry Andric // For separate SCCs this is trivial. 10600b57cec5SDimitry Andric RC->switchTrivialInternalEdgeToRef(N, *RefTarget); 10610b57cec5SDimitry Andric continue; 10620b57cec5SDimitry Andric } 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric // Now update the call graph. 10650b57cec5SDimitry Andric C = incorporateNewSCCRange(RC->switchInternalEdgeToRef(N, *RefTarget), G, N, 10660b57cec5SDimitry Andric C, AM, UR); 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 1069e8d8bef9SDimitry Andric // We added a ref edge earlier for new call edges, promote those to call edges 1070e8d8bef9SDimitry Andric // alongside PromotedRefTargets. 1071e8d8bef9SDimitry Andric for (Node *E : NewCallEdges) 1072e8d8bef9SDimitry Andric PromotedRefTargets.insert(E); 1073e8d8bef9SDimitry Andric 10740b57cec5SDimitry Andric // Now promote ref edges into call edges. 10750b57cec5SDimitry Andric for (Node *CallTarget : PromotedRefTargets) { 10760b57cec5SDimitry Andric SCC &TargetC = *G.lookupSCC(*CallTarget); 10770b57cec5SDimitry Andric RefSCC &TargetRC = TargetC.getOuterRefSCC(); 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric // The easy case is when the target RefSCC is not this RefSCC. This is 10800b57cec5SDimitry Andric // only supported when the target RefSCC is a child of this RefSCC. 10810b57cec5SDimitry Andric if (&TargetRC != RC) { 1082fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS 10830b57cec5SDimitry Andric assert(RC->isAncestorOf(TargetRC) && 10840b57cec5SDimitry Andric "Cannot potentially form RefSCC cycles here!"); 1085fe6060f1SDimitry Andric #endif 10860b57cec5SDimitry Andric RC->switchOutgoingEdgeToCall(N, *CallTarget); 10870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Switch outgoing ref edge to a call edge from '" << N 10880b57cec5SDimitry Andric << "' to '" << *CallTarget << "'\n"); 10890b57cec5SDimitry Andric continue; 10900b57cec5SDimitry Andric } 10910b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Switch an internal ref edge to a call edge from '" 10920b57cec5SDimitry Andric << N << "' to '" << *CallTarget << "'\n"); 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric // Otherwise we are switching an internal ref edge to a call edge. This 10950b57cec5SDimitry Andric // may merge away some SCCs, and we add those to the UpdateResult. We also 10960b57cec5SDimitry Andric // need to make sure to update the worklist in the event SCCs have moved 10970b57cec5SDimitry Andric // before the current one in the post-order sequence 10980b57cec5SDimitry Andric bool HasFunctionAnalysisProxy = false; 10990b57cec5SDimitry Andric auto InitialSCCIndex = RC->find(*C) - RC->begin(); 11000b57cec5SDimitry Andric bool FormedCycle = RC->switchInternalEdgeToCall( 11010b57cec5SDimitry Andric N, *CallTarget, [&](ArrayRef<SCC *> MergedSCCs) { 11020b57cec5SDimitry Andric for (SCC *MergedC : MergedSCCs) { 11030b57cec5SDimitry Andric assert(MergedC != &TargetC && "Cannot merge away the target SCC!"); 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric HasFunctionAnalysisProxy |= 11060b57cec5SDimitry Andric AM.getCachedResult<FunctionAnalysisManagerCGSCCProxy>( 11070b57cec5SDimitry Andric *MergedC) != nullptr; 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric // Mark that this SCC will no longer be valid. 11100b57cec5SDimitry Andric UR.InvalidatedSCCs.insert(MergedC); 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric // FIXME: We should really do a 'clear' here to forcibly release 11130b57cec5SDimitry Andric // memory, but we don't have a good way of doing that and 11140b57cec5SDimitry Andric // preserving the function analyses. 11150b57cec5SDimitry Andric auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 11160b57cec5SDimitry Andric PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 11170b57cec5SDimitry Andric AM.invalidate(*MergedC, PA); 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric }); 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andric // If we formed a cycle by creating this call, we need to update more data 11220b57cec5SDimitry Andric // structures. 11230b57cec5SDimitry Andric if (FormedCycle) { 11240b57cec5SDimitry Andric C = &TargetC; 11250b57cec5SDimitry Andric assert(G.lookupSCC(N) == C && "Failed to update current SCC!"); 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric // If one of the invalidated SCCs had a cached proxy to a function 11280b57cec5SDimitry Andric // analysis manager, we need to create a proxy in the new current SCC as 11290b57cec5SDimitry Andric // the invalidated SCCs had their functions moved. 11300b57cec5SDimitry Andric if (HasFunctionAnalysisProxy) 11315ffd83dbSDimitry Andric AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, G).updateFAM(FAM); 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric // Any analyses cached for this SCC are no longer precise as the shape 11340b57cec5SDimitry Andric // has changed by introducing this cycle. However, we have taken care to 11350b57cec5SDimitry Andric // update the proxies so it remains valide. 11360b57cec5SDimitry Andric auto PA = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); 11370b57cec5SDimitry Andric PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); 11380b57cec5SDimitry Andric AM.invalidate(*C, PA); 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric auto NewSCCIndex = RC->find(*C) - RC->begin(); 11410b57cec5SDimitry Andric // If we have actually moved an SCC to be topologically "below" the current 11420b57cec5SDimitry Andric // one due to merging, we will need to revisit the current SCC after 11430b57cec5SDimitry Andric // visiting those moved SCCs. 11440b57cec5SDimitry Andric // 11450b57cec5SDimitry Andric // It is critical that we *do not* revisit the current SCC unless we 11460b57cec5SDimitry Andric // actually move SCCs in the process of merging because otherwise we may 11470b57cec5SDimitry Andric // form a cycle where an SCC is split apart, merged, split, merged and so 11480b57cec5SDimitry Andric // on infinitely. 11490b57cec5SDimitry Andric if (InitialSCCIndex < NewSCCIndex) { 11500b57cec5SDimitry Andric // Put our current SCC back onto the worklist as we'll visit other SCCs 11510b57cec5SDimitry Andric // that are now definitively ordered prior to the current one in the 11520b57cec5SDimitry Andric // post-order sequence, and may end up observing more precise context to 11530b57cec5SDimitry Andric // optimize the current SCC. 11540b57cec5SDimitry Andric UR.CWorklist.insert(C); 11550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing the existing SCC in the worklist: " << *C 11560b57cec5SDimitry Andric << "\n"); 11570b57cec5SDimitry Andric // Enqueue in reverse order as we pop off the back of the worklist. 11580b57cec5SDimitry Andric for (SCC &MovedC : llvm::reverse(make_range(RC->begin() + InitialSCCIndex, 11590b57cec5SDimitry Andric RC->begin() + NewSCCIndex))) { 11600b57cec5SDimitry Andric UR.CWorklist.insert(&MovedC); 11610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Enqueuing a newly earlier in post-order SCC: " 11620b57cec5SDimitry Andric << MovedC << "\n"); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric } 11660b57cec5SDimitry Andric 11670b57cec5SDimitry Andric assert(!UR.InvalidatedSCCs.count(C) && "Invalidated the current SCC!"); 11680b57cec5SDimitry Andric assert(&C->getOuterRefSCC() == RC && "Current SCC not in current RefSCC!"); 11690b57cec5SDimitry Andric 117081ad6265SDimitry Andric // Record the current SCC for higher layers of the CGSCC pass manager now that 117181ad6265SDimitry Andric // all the updates have been applied. 11720b57cec5SDimitry Andric if (C != &InitialC) 11730b57cec5SDimitry Andric UR.UpdatedC = C; 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric return *C; 11760b57cec5SDimitry Andric } 11775ffd83dbSDimitry Andric 11785ffd83dbSDimitry Andric LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForFunctionPass( 11795ffd83dbSDimitry Andric LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 11805ffd83dbSDimitry Andric CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 11815ffd83dbSDimitry Andric FunctionAnalysisManager &FAM) { 11825ffd83dbSDimitry Andric return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM, 11835ffd83dbSDimitry Andric /* FunctionPass */ true); 11845ffd83dbSDimitry Andric } 11855ffd83dbSDimitry Andric LazyCallGraph::SCC &llvm::updateCGAndAnalysisManagerForCGSCCPass( 11865ffd83dbSDimitry Andric LazyCallGraph &G, LazyCallGraph::SCC &InitialC, LazyCallGraph::Node &N, 11875ffd83dbSDimitry Andric CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 11885ffd83dbSDimitry Andric FunctionAnalysisManager &FAM) { 11895ffd83dbSDimitry Andric return updateCGAndAnalysisManagerForPass(G, InitialC, N, AM, UR, FAM, 11905ffd83dbSDimitry Andric /* FunctionPass */ false); 11915ffd83dbSDimitry Andric } 1192