1 //===- CGSCCPassManager.h - Call graph pass management ----------*- C++ -*-===// 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 /// \file 9 /// 10 /// This header provides classes for managing passes over SCCs of the call 11 /// graph. These passes form an important component of LLVM's interprocedural 12 /// optimizations. Because they operate on the SCCs of the call graph, and they 13 /// traverse the graph in post-order, they can effectively do pair-wise 14 /// interprocedural optimizations for all call edges in the program while 15 /// incrementally refining it and improving the context of these pair-wise 16 /// optimizations. At each call site edge, the callee has already been 17 /// optimized as much as is possible. This in turn allows very accurate 18 /// analysis of it for IPO. 19 /// 20 /// A secondary more general goal is to be able to isolate optimization on 21 /// unrelated parts of the IR module. This is useful to ensure our 22 /// optimizations are principled and don't miss oportunities where refinement 23 /// of one part of the module influences transformations in another part of the 24 /// module. But this is also useful if we want to parallelize the optimizations 25 /// across common large module graph shapes which tend to be very wide and have 26 /// large regions of unrelated cliques. 27 /// 28 /// To satisfy these goals, we use the LazyCallGraph which provides two graphs 29 /// nested inside each other (and built lazily from the bottom-up): the call 30 /// graph proper, and a reference graph. The reference graph is super set of 31 /// the call graph and is a conservative approximation of what could through 32 /// scalar or CGSCC transforms *become* the call graph. Using this allows us to 33 /// ensure we optimize functions prior to them being introduced into the call 34 /// graph by devirtualization or other technique, and thus ensures that 35 /// subsequent pair-wise interprocedural optimizations observe the optimized 36 /// form of these functions. The (potentially transitive) reference 37 /// reachability used by the reference graph is a conservative approximation 38 /// that still allows us to have independent regions of the graph. 39 /// 40 /// FIXME: There is one major drawback of the reference graph: in its naive 41 /// form it is quadratic because it contains a distinct edge for each 42 /// (potentially indirect) reference, even if are all through some common 43 /// global table of function pointers. This can be fixed in a number of ways 44 /// that essentially preserve enough of the normalization. While it isn't 45 /// expected to completely preclude the usability of this, it will need to be 46 /// addressed. 47 /// 48 /// 49 /// All of these issues are made substantially more complex in the face of 50 /// mutations to the call graph while optimization passes are being run. When 51 /// mutations to the call graph occur we want to achieve two different things: 52 /// 53 /// - We need to update the call graph in-flight and invalidate analyses 54 /// cached on entities in the graph. Because of the cache-based analysis 55 /// design of the pass manager, it is essential to have stable identities for 56 /// the elements of the IR that passes traverse, and to invalidate any 57 /// analyses cached on these elements as the mutations take place. 58 /// 59 /// - We want to preserve the incremental and post-order traversal of the 60 /// graph even as it is refined and mutated. This means we want optimization 61 /// to observe the most refined form of the call graph and to do so in 62 /// post-order. 63 /// 64 /// To address this, the CGSCC manager uses both worklists that can be expanded 65 /// by passes which transform the IR, and provides invalidation tests to skip 66 /// entries that become dead. This extra data is provided to every SCC pass so 67 /// that it can carefully update the manager's traversal as the call graph 68 /// mutates. 69 /// 70 /// We also provide support for running function passes within the CGSCC walk, 71 /// and there we provide automatic update of the call graph including of the 72 /// pass manager to reflect call graph changes that fall out naturally as part 73 /// of scalar transformations. 74 /// 75 /// The patterns used to ensure the goals of post-order visitation of the fully 76 /// refined graph: 77 /// 78 /// 1) Sink toward the "bottom" as the graph is refined. This means that any 79 /// iteration continues in some valid post-order sequence after the mutation 80 /// has altered the structure. 81 /// 82 /// 2) Enqueue in post-order, including the current entity. If the current 83 /// entity's shape changes, it and everything after it in post-order needs 84 /// to be visited to observe that shape. 85 /// 86 //===----------------------------------------------------------------------===// 87 88 #ifndef LLVM_ANALYSIS_CGSCCPASSMANAGER_H 89 #define LLVM_ANALYSIS_CGSCCPASSMANAGER_H 90 91 #include "llvm/ADT/MapVector.h" 92 #include "llvm/Analysis/LazyCallGraph.h" 93 #include "llvm/IR/PassManager.h" 94 #include "llvm/IR/ValueHandle.h" 95 #include "llvm/Support/raw_ostream.h" 96 #include <cassert> 97 #include <utility> 98 99 namespace llvm { 100 101 class Function; 102 template <typename T, unsigned int N> class SmallPriorityWorklist; 103 struct CGSCCUpdateResult; 104 105 class Module; 106 107 // Allow debug logging in this inline function. 108 #define DEBUG_TYPE "cgscc" 109 110 /// Extern template declaration for the analysis set for this IR unit. 111 extern template class AllAnalysesOn<LazyCallGraph::SCC>; 112 113 extern template class AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; 114 115 /// The CGSCC analysis manager. 116 /// 117 /// See the documentation for the AnalysisManager template for detail 118 /// documentation. This type serves as a convenient way to refer to this 119 /// construct in the adaptors and proxies used to integrate this into the larger 120 /// pass manager infrastructure. 121 using CGSCCAnalysisManager = 122 AnalysisManager<LazyCallGraph::SCC, LazyCallGraph &>; 123 124 // Explicit specialization and instantiation declarations for the pass manager. 125 // See the comments on the definition of the specialization for details on how 126 // it differs from the primary template. 127 template <> 128 PreservedAnalyses 129 PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, 130 CGSCCUpdateResult &>::run(LazyCallGraph::SCC &InitialC, 131 CGSCCAnalysisManager &AM, 132 LazyCallGraph &G, CGSCCUpdateResult &UR); 133 extern template class PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, 134 LazyCallGraph &, CGSCCUpdateResult &>; 135 136 /// The CGSCC pass manager. 137 /// 138 /// See the documentation for the PassManager template for details. It runs 139 /// a sequence of SCC passes over each SCC that the manager is run over. This 140 /// type serves as a convenient way to refer to this construct. 141 using CGSCCPassManager = 142 PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, 143 CGSCCUpdateResult &>; 144 145 /// An explicit specialization of the require analysis template pass. 146 template <typename AnalysisT> 147 struct RequireAnalysisPass<AnalysisT, LazyCallGraph::SCC, CGSCCAnalysisManager, 148 LazyCallGraph &, CGSCCUpdateResult &> 149 : PassInfoMixin<RequireAnalysisPass<AnalysisT, LazyCallGraph::SCC, 150 CGSCCAnalysisManager, LazyCallGraph &, 151 CGSCCUpdateResult &>> { 152 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, 153 LazyCallGraph &CG, CGSCCUpdateResult &) { 154 (void)AM.template getResult<AnalysisT>(C, CG); 155 return PreservedAnalyses::all(); 156 } 157 void printPipeline(raw_ostream &OS, 158 function_ref<StringRef(StringRef)> MapClassName2PassName) { 159 auto ClassName = AnalysisT::name(); 160 auto PassName = MapClassName2PassName(ClassName); 161 OS << "require<" << PassName << '>'; 162 } 163 }; 164 165 /// A proxy from a \c CGSCCAnalysisManager to a \c Module. 166 using CGSCCAnalysisManagerModuleProxy = 167 InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; 168 169 /// We need a specialized result for the \c CGSCCAnalysisManagerModuleProxy so 170 /// it can have access to the call graph in order to walk all the SCCs when 171 /// invalidating things. 172 template <> class CGSCCAnalysisManagerModuleProxy::Result { 173 public: 174 explicit Result(CGSCCAnalysisManager &InnerAM, LazyCallGraph &G) 175 : InnerAM(&InnerAM), G(&G) {} 176 177 /// Accessor for the analysis manager. 178 CGSCCAnalysisManager &getManager() { return *InnerAM; } 179 180 /// Handler for invalidation of the Module. 181 /// 182 /// If the proxy analysis itself is preserved, then we assume that the set of 183 /// SCCs in the Module hasn't changed. Thus any pointers to SCCs in the 184 /// CGSCCAnalysisManager are still valid, and we don't need to call \c clear 185 /// on the CGSCCAnalysisManager. 186 /// 187 /// Regardless of whether this analysis is marked as preserved, all of the 188 /// analyses in the \c CGSCCAnalysisManager are potentially invalidated based 189 /// on the set of preserved analyses. 190 bool invalidate(Module &M, const PreservedAnalyses &PA, 191 ModuleAnalysisManager::Invalidator &Inv); 192 193 private: 194 CGSCCAnalysisManager *InnerAM; 195 LazyCallGraph *G; 196 }; 197 198 /// Provide a specialized run method for the \c CGSCCAnalysisManagerModuleProxy 199 /// so it can pass the lazy call graph to the result. 200 template <> 201 CGSCCAnalysisManagerModuleProxy::Result 202 CGSCCAnalysisManagerModuleProxy::run(Module &M, ModuleAnalysisManager &AM); 203 204 // Ensure the \c CGSCCAnalysisManagerModuleProxy is provided as an extern 205 // template. 206 extern template class InnerAnalysisManagerProxy<CGSCCAnalysisManager, Module>; 207 208 extern template class OuterAnalysisManagerProxy< 209 ModuleAnalysisManager, LazyCallGraph::SCC, LazyCallGraph &>; 210 211 /// A proxy from a \c ModuleAnalysisManager to an \c SCC. 212 using ModuleAnalysisManagerCGSCCProxy = 213 OuterAnalysisManagerProxy<ModuleAnalysisManager, LazyCallGraph::SCC, 214 LazyCallGraph &>; 215 216 /// Support structure for SCC passes to communicate updates the call graph back 217 /// to the CGSCC pass manager infrastructure. 218 /// 219 /// The CGSCC pass manager runs SCC passes which are allowed to update the call 220 /// graph and SCC structures. This means the structure the pass manager works 221 /// on is mutating underneath it. In order to support that, there needs to be 222 /// careful communication about the precise nature and ramifications of these 223 /// updates to the pass management infrastructure. 224 /// 225 /// All SCC passes will have to accept a reference to the management layer's 226 /// update result struct and use it to reflect the results of any CG updates 227 /// performed. 228 /// 229 /// Passes which do not change the call graph structure in any way can just 230 /// ignore this argument to their run method. 231 struct CGSCCUpdateResult { 232 /// Worklist of the SCCs queued for processing. 233 /// 234 /// When a pass refines the graph and creates new SCCs or causes them to have 235 /// a different shape or set of component functions it should add the SCCs to 236 /// this worklist so that we visit them in the refined form. 237 /// 238 /// Note that if the SCCs are part of a RefSCC that is added to the \c 239 /// RCWorklist, they don't need to be added here as visiting the RefSCC will 240 /// be sufficient to re-visit the SCCs within it. 241 /// 242 /// This worklist is in reverse post-order, as we pop off the back in order 243 /// to observe SCCs in post-order. When adding SCCs, clients should add them 244 /// in reverse post-order. 245 SmallPriorityWorklist<LazyCallGraph::SCC *, 1> &CWorklist; 246 247 /// The set of invalidated SCCs which should be skipped if they are found 248 /// in \c CWorklist. 249 /// 250 /// This is used to quickly prune out SCCs when they get deleted and happen 251 /// to already be on the worklist. We use this primarily to avoid scanning 252 /// the list and removing entries from it. 253 SmallPtrSetImpl<LazyCallGraph::SCC *> &InvalidatedSCCs; 254 255 /// If non-null, the updated current \c SCC being processed. 256 /// 257 /// This is set when a graph refinement takes place and the "current" point 258 /// in the graph moves "down" or earlier in the post-order walk. This will 259 /// often cause the "current" SCC to be a newly created SCC object and the 260 /// old one to be added to the above worklist. When that happens, this 261 /// pointer is non-null and can be used to continue processing the "top" of 262 /// the post-order walk. 263 LazyCallGraph::SCC *UpdatedC; 264 265 /// Preserved analyses across SCCs. 266 /// 267 /// We specifically want to allow CGSCC passes to mutate ancestor IR 268 /// (changing both the CG structure and the function IR itself). However, 269 /// this means we need to take special care to correctly mark what analyses 270 /// are preserved *across* SCCs. We have to track this out-of-band here 271 /// because within the main `PassManager` infrastructure we need to mark 272 /// everything within an SCC as preserved in order to avoid repeatedly 273 /// invalidating the same analyses as we unnest pass managers and adaptors. 274 /// So we track the cross-SCC version of the preserved analyses here from any 275 /// code that does direct invalidation of SCC analyses, and then use it 276 /// whenever we move forward in the post-order walk of SCCs before running 277 /// passes over the new SCC. 278 PreservedAnalyses CrossSCCPA; 279 280 /// A hacky area where the inliner can retain history about inlining 281 /// decisions that mutated the call graph's SCC structure in order to avoid 282 /// infinite inlining. See the comments in the inliner's CG update logic. 283 /// 284 /// FIXME: Keeping this here seems like a big layering issue, we should look 285 /// for a better technique. 286 SmallDenseSet<std::pair<LazyCallGraph::Node *, LazyCallGraph::SCC *>, 4> 287 &InlinedInternalEdges; 288 289 /// Functions that a pass has considered to be dead to be removed at the end 290 /// of the call graph walk in batch. 291 SmallVector<Function *, 4> &DeadFunctions; 292 293 /// Weak VHs to keep track of indirect calls for the purposes of detecting 294 /// devirtualization. 295 /// 296 /// This is a map to avoid having duplicate entries. If a Value is 297 /// deallocated, its corresponding WeakTrackingVH will be nulled out. When 298 /// checking if a Value is in the map or not, also check if the corresponding 299 /// WeakTrackingVH is null to avoid issues with a new Value sharing the same 300 /// address as a deallocated one. 301 SmallMapVector<Value *, WeakTrackingVH, 16> IndirectVHs; 302 }; 303 304 /// The core module pass which does a post-order walk of the SCCs and 305 /// runs a CGSCC pass over each one. 306 /// 307 /// Designed to allow composition of a CGSCCPass(Manager) and 308 /// a ModulePassManager. Note that this pass must be run with a module analysis 309 /// manager as it uses the LazyCallGraph analysis. It will also run the 310 /// \c CGSCCAnalysisManagerModuleProxy analysis prior to running the CGSCC 311 /// pass over the module to enable a \c FunctionAnalysisManager to be used 312 /// within this run safely. 313 class ModuleToPostOrderCGSCCPassAdaptor 314 : public PassInfoMixin<ModuleToPostOrderCGSCCPassAdaptor> { 315 public: 316 using PassConceptT = 317 detail::PassConcept<LazyCallGraph::SCC, CGSCCAnalysisManager, 318 LazyCallGraph &, CGSCCUpdateResult &>; 319 320 explicit ModuleToPostOrderCGSCCPassAdaptor(std::unique_ptr<PassConceptT> Pass) 321 : Pass(std::move(Pass)) {} 322 323 ModuleToPostOrderCGSCCPassAdaptor(ModuleToPostOrderCGSCCPassAdaptor &&Arg) 324 : Pass(std::move(Arg.Pass)) {} 325 326 friend void swap(ModuleToPostOrderCGSCCPassAdaptor &LHS, 327 ModuleToPostOrderCGSCCPassAdaptor &RHS) { 328 std::swap(LHS.Pass, RHS.Pass); 329 } 330 331 ModuleToPostOrderCGSCCPassAdaptor & 332 operator=(ModuleToPostOrderCGSCCPassAdaptor RHS) { 333 swap(*this, RHS); 334 return *this; 335 } 336 337 /// Runs the CGSCC pass across every SCC in the module. 338 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 339 340 void printPipeline(raw_ostream &OS, 341 function_ref<StringRef(StringRef)> MapClassName2PassName) { 342 OS << "cgscc("; 343 Pass->printPipeline(OS, MapClassName2PassName); 344 OS << ')'; 345 } 346 347 static bool isRequired() { return true; } 348 349 private: 350 std::unique_ptr<PassConceptT> Pass; 351 }; 352 353 /// A function to deduce a function pass type and wrap it in the 354 /// templated adaptor. 355 template <typename CGSCCPassT> 356 ModuleToPostOrderCGSCCPassAdaptor 357 createModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT &&Pass) { 358 using PassModelT = 359 detail::PassModel<LazyCallGraph::SCC, CGSCCPassT, CGSCCAnalysisManager, 360 LazyCallGraph &, CGSCCUpdateResult &>; 361 // Do not use make_unique, it causes too many template instantiations, 362 // causing terrible compile times. 363 return ModuleToPostOrderCGSCCPassAdaptor( 364 std::unique_ptr<ModuleToPostOrderCGSCCPassAdaptor::PassConceptT>( 365 new PassModelT(std::forward<CGSCCPassT>(Pass)))); 366 } 367 368 /// A proxy from a \c FunctionAnalysisManager to an \c SCC. 369 /// 370 /// When a module pass runs and triggers invalidation, both the CGSCC and 371 /// Function analysis manager proxies on the module get an invalidation event. 372 /// We don't want to fully duplicate responsibility for most of the 373 /// invalidation logic. Instead, this layer is only responsible for SCC-local 374 /// invalidation events. We work with the module's FunctionAnalysisManager to 375 /// invalidate function analyses. 376 class FunctionAnalysisManagerCGSCCProxy 377 : public AnalysisInfoMixin<FunctionAnalysisManagerCGSCCProxy> { 378 public: 379 class Result { 380 public: 381 explicit Result() : FAM(nullptr) {} 382 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {} 383 384 void updateFAM(FunctionAnalysisManager &FAM) { this->FAM = &FAM; } 385 /// Accessor for the analysis manager. 386 FunctionAnalysisManager &getManager() { 387 assert(FAM); 388 return *FAM; 389 } 390 391 bool invalidate(LazyCallGraph::SCC &C, const PreservedAnalyses &PA, 392 CGSCCAnalysisManager::Invalidator &Inv); 393 394 private: 395 FunctionAnalysisManager *FAM; 396 }; 397 398 /// Computes the \c FunctionAnalysisManager and stores it in the result proxy. 399 Result run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &); 400 401 private: 402 friend AnalysisInfoMixin<FunctionAnalysisManagerCGSCCProxy>; 403 404 static AnalysisKey Key; 405 }; 406 407 extern template class OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; 408 409 /// A proxy from a \c CGSCCAnalysisManager to a \c Function. 410 using CGSCCAnalysisManagerFunctionProxy = 411 OuterAnalysisManagerProxy<CGSCCAnalysisManager, Function>; 412 413 /// Helper to update the call graph after running a function pass. 414 /// 415 /// Function passes can only mutate the call graph in specific ways. This 416 /// routine provides a helper that updates the call graph in those ways 417 /// including returning whether any changes were made and populating a CG 418 /// update result struct for the overall CGSCC walk. 419 LazyCallGraph::SCC &updateCGAndAnalysisManagerForFunctionPass( 420 LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, 421 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 422 FunctionAnalysisManager &FAM); 423 424 /// Helper to update the call graph after running a CGSCC pass. 425 /// 426 /// CGSCC passes can only mutate the call graph in specific ways. This 427 /// routine provides a helper that updates the call graph in those ways 428 /// including returning whether any changes were made and populating a CG 429 /// update result struct for the overall CGSCC walk. 430 LazyCallGraph::SCC &updateCGAndAnalysisManagerForCGSCCPass( 431 LazyCallGraph &G, LazyCallGraph::SCC &C, LazyCallGraph::Node &N, 432 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR, 433 FunctionAnalysisManager &FAM); 434 435 /// Adaptor that maps from a SCC to its functions. 436 /// 437 /// Designed to allow composition of a FunctionPass(Manager) and 438 /// a CGSCCPassManager. Note that if this pass is constructed with a pointer 439 /// to a \c CGSCCAnalysisManager it will run the 440 /// \c FunctionAnalysisManagerCGSCCProxy analysis prior to running the function 441 /// pass over the SCC to enable a \c FunctionAnalysisManager to be used 442 /// within this run safely. 443 class CGSCCToFunctionPassAdaptor 444 : public PassInfoMixin<CGSCCToFunctionPassAdaptor> { 445 public: 446 using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>; 447 448 explicit CGSCCToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass, 449 bool EagerlyInvalidate, bool NoRerun) 450 : Pass(std::move(Pass)), EagerlyInvalidate(EagerlyInvalidate), 451 NoRerun(NoRerun) {} 452 453 CGSCCToFunctionPassAdaptor(CGSCCToFunctionPassAdaptor &&Arg) 454 : Pass(std::move(Arg.Pass)), EagerlyInvalidate(Arg.EagerlyInvalidate), 455 NoRerun(Arg.NoRerun) {} 456 457 friend void swap(CGSCCToFunctionPassAdaptor &LHS, 458 CGSCCToFunctionPassAdaptor &RHS) { 459 std::swap(LHS.Pass, RHS.Pass); 460 } 461 462 CGSCCToFunctionPassAdaptor &operator=(CGSCCToFunctionPassAdaptor RHS) { 463 swap(*this, RHS); 464 return *this; 465 } 466 467 /// Runs the function pass across every function in the module. 468 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, 469 LazyCallGraph &CG, CGSCCUpdateResult &UR); 470 471 void printPipeline(raw_ostream &OS, 472 function_ref<StringRef(StringRef)> MapClassName2PassName) { 473 OS << "function"; 474 if (EagerlyInvalidate || NoRerun) { 475 OS << "<"; 476 if (EagerlyInvalidate) 477 OS << "eager-inv"; 478 if (EagerlyInvalidate && NoRerun) 479 OS << ";"; 480 if (NoRerun) 481 OS << "no-rerun"; 482 OS << ">"; 483 } 484 OS << '('; 485 Pass->printPipeline(OS, MapClassName2PassName); 486 OS << ')'; 487 } 488 489 static bool isRequired() { return true; } 490 491 private: 492 std::unique_ptr<PassConceptT> Pass; 493 bool EagerlyInvalidate; 494 bool NoRerun; 495 }; 496 497 /// A function to deduce a function pass type and wrap it in the 498 /// templated adaptor. 499 template <typename FunctionPassT> 500 CGSCCToFunctionPassAdaptor 501 createCGSCCToFunctionPassAdaptor(FunctionPassT &&Pass, 502 bool EagerlyInvalidate = false, 503 bool NoRerun = false) { 504 using PassModelT = 505 detail::PassModel<Function, FunctionPassT, FunctionAnalysisManager>; 506 // Do not use make_unique, it causes too many template instantiations, 507 // causing terrible compile times. 508 return CGSCCToFunctionPassAdaptor( 509 std::unique_ptr<CGSCCToFunctionPassAdaptor::PassConceptT>( 510 new PassModelT(std::forward<FunctionPassT>(Pass))), 511 EagerlyInvalidate, NoRerun); 512 } 513 514 // A marker to determine if function passes should be run on a function within a 515 // CGSCCToFunctionPassAdaptor. This is used to prevent running an expensive 516 // function pass (manager) on a function multiple times if SCC mutations cause a 517 // function to be visited multiple times and the function is not modified by 518 // other SCC passes. 519 class ShouldNotRunFunctionPassesAnalysis 520 : public AnalysisInfoMixin<ShouldNotRunFunctionPassesAnalysis> { 521 public: 522 static AnalysisKey Key; 523 struct Result {}; 524 525 Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); } 526 }; 527 528 /// A helper that repeats an SCC pass each time an indirect call is refined to 529 /// a direct call by that pass. 530 /// 531 /// While the CGSCC pass manager works to re-visit SCCs and RefSCCs as they 532 /// change shape, we may also want to repeat an SCC pass if it simply refines 533 /// an indirect call to a direct call, even if doing so does not alter the 534 /// shape of the graph. Note that this only pertains to direct calls to 535 /// functions where IPO across the SCC may be able to compute more precise 536 /// results. For intrinsics, we assume scalar optimizations already can fully 537 /// reason about them. 538 /// 539 /// This repetition has the potential to be very large however, as each one 540 /// might refine a single call site. As a consequence, in practice we use an 541 /// upper bound on the number of repetitions to limit things. 542 class DevirtSCCRepeatedPass : public PassInfoMixin<DevirtSCCRepeatedPass> { 543 public: 544 using PassConceptT = 545 detail::PassConcept<LazyCallGraph::SCC, CGSCCAnalysisManager, 546 LazyCallGraph &, CGSCCUpdateResult &>; 547 548 explicit DevirtSCCRepeatedPass(std::unique_ptr<PassConceptT> Pass, 549 int MaxIterations) 550 : Pass(std::move(Pass)), MaxIterations(MaxIterations) {} 551 552 /// Runs the wrapped pass up to \c MaxIterations on the SCC, iterating 553 /// whenever an indirect call is refined. 554 PreservedAnalyses run(LazyCallGraph::SCC &InitialC, CGSCCAnalysisManager &AM, 555 LazyCallGraph &CG, CGSCCUpdateResult &UR); 556 557 void printPipeline(raw_ostream &OS, 558 function_ref<StringRef(StringRef)> MapClassName2PassName) { 559 OS << "devirt<" << MaxIterations << ">("; 560 Pass->printPipeline(OS, MapClassName2PassName); 561 OS << ')'; 562 } 563 564 private: 565 std::unique_ptr<PassConceptT> Pass; 566 int MaxIterations; 567 }; 568 569 /// A function to deduce a function pass type and wrap it in the 570 /// templated adaptor. 571 template <typename CGSCCPassT> 572 DevirtSCCRepeatedPass createDevirtSCCRepeatedPass(CGSCCPassT &&Pass, 573 int MaxIterations) { 574 using PassModelT = 575 detail::PassModel<LazyCallGraph::SCC, CGSCCPassT, CGSCCAnalysisManager, 576 LazyCallGraph &, CGSCCUpdateResult &>; 577 // Do not use make_unique, it causes too many template instantiations, 578 // causing terrible compile times. 579 return DevirtSCCRepeatedPass( 580 std::unique_ptr<DevirtSCCRepeatedPass::PassConceptT>( 581 new PassModelT(std::forward<CGSCCPassT>(Pass))), 582 MaxIterations); 583 } 584 585 // Clear out the debug logging macro. 586 #undef DEBUG_TYPE 587 588 } // end namespace llvm 589 590 #endif // LLVM_ANALYSIS_CGSCCPASSMANAGER_H 591