1 //===- PassManager.h - Pass management infrastructure -----------*- 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 defines various interfaces for pass management in LLVM. There 11 /// is no "pass" interface in LLVM per se. Instead, an instance of any class 12 /// which supports a method to 'run' it over a unit of IR can be used as 13 /// a pass. A pass manager is generally a tool to collect a sequence of passes 14 /// which run over a particular IR construct, and run each of them in sequence 15 /// over each such construct in the containing IR construct. As there is no 16 /// containing IR construct for a Module, a manager for passes over modules 17 /// forms the base case which runs its managed passes in sequence over the 18 /// single module provided. 19 /// 20 /// The core IR library provides managers for running passes over 21 /// modules and functions. 22 /// 23 /// * FunctionPassManager can run over a Module, runs each pass over 24 /// a Function. 25 /// * ModulePassManager must be directly run, runs each pass over the Module. 26 /// 27 /// Note that the implementations of the pass managers use concept-based 28 /// polymorphism as outlined in the "Value Semantics and Concept-based 29 /// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base 30 /// Class of Evil") by Sean Parent: 31 /// * http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations 32 /// * http://www.youtube.com/watch?v=_BpMYeUFXv8 33 /// * http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil 34 /// 35 //===----------------------------------------------------------------------===// 36 37 #ifndef LLVM_IR_PASSMANAGER_H 38 #define LLVM_IR_PASSMANAGER_H 39 40 #include "llvm/ADT/DenseMap.h" 41 #include "llvm/ADT/STLExtras.h" 42 #include "llvm/ADT/SmallPtrSet.h" 43 #include "llvm/ADT/StringRef.h" 44 #include "llvm/ADT/TinyPtrVector.h" 45 #include "llvm/IR/Function.h" 46 #include "llvm/IR/Module.h" 47 #include "llvm/IR/PassInstrumentation.h" 48 #include "llvm/IR/PassManagerInternal.h" 49 #include "llvm/Pass.h" 50 #include "llvm/Support/Debug.h" 51 #include "llvm/Support/TimeProfiler.h" 52 #include "llvm/Support/TypeName.h" 53 #include <algorithm> 54 #include <cassert> 55 #include <cstring> 56 #include <iterator> 57 #include <list> 58 #include <memory> 59 #include <tuple> 60 #include <type_traits> 61 #include <utility> 62 #include <vector> 63 64 namespace llvm { 65 66 /// A special type used by analysis passes to provide an address that 67 /// identifies that particular analysis pass type. 68 /// 69 /// Analysis passes should have a static data member of this type and derive 70 /// from the \c AnalysisInfoMixin to get a static ID method used to identify 71 /// the analysis in the pass management infrastructure. 72 struct alignas(8) AnalysisKey {}; 73 74 /// A special type used to provide an address that identifies a set of related 75 /// analyses. These sets are primarily used below to mark sets of analyses as 76 /// preserved. 77 /// 78 /// For example, a transformation can indicate that it preserves the CFG of a 79 /// function by preserving the appropriate AnalysisSetKey. An analysis that 80 /// depends only on the CFG can then check if that AnalysisSetKey is preserved; 81 /// if it is, the analysis knows that it itself is preserved. 82 struct alignas(8) AnalysisSetKey {}; 83 84 /// This templated class represents "all analyses that operate over \<a 85 /// particular IR unit\>" (e.g. a Function or a Module) in instances of 86 /// PreservedAnalysis. 87 /// 88 /// This lets a transformation say e.g. "I preserved all function analyses". 89 /// 90 /// Note that you must provide an explicit instantiation declaration and 91 /// definition for this template in order to get the correct behavior on 92 /// Windows. Otherwise, the address of SetKey will not be stable. 93 template <typename IRUnitT> class AllAnalysesOn { 94 public: 95 static AnalysisSetKey *ID() { return &SetKey; } 96 97 private: 98 static AnalysisSetKey SetKey; 99 }; 100 101 template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey; 102 103 extern template class AllAnalysesOn<Module>; 104 extern template class AllAnalysesOn<Function>; 105 106 /// Represents analyses that only rely on functions' control flow. 107 /// 108 /// This can be used with \c PreservedAnalyses to mark the CFG as preserved and 109 /// to query whether it has been preserved. 110 /// 111 /// The CFG of a function is defined as the set of basic blocks and the edges 112 /// between them. Changing the set of basic blocks in a function is enough to 113 /// mutate the CFG. Mutating the condition of a branch or argument of an 114 /// invoked function does not mutate the CFG, but changing the successor labels 115 /// of those instructions does. 116 class CFGAnalyses { 117 public: 118 static AnalysisSetKey *ID() { return &SetKey; } 119 120 private: 121 static AnalysisSetKey SetKey; 122 }; 123 124 /// A set of analyses that are preserved following a run of a transformation 125 /// pass. 126 /// 127 /// Transformation passes build and return these objects to communicate which 128 /// analyses are still valid after the transformation. For most passes this is 129 /// fairly simple: if they don't change anything all analyses are preserved, 130 /// otherwise only a short list of analyses that have been explicitly updated 131 /// are preserved. 132 /// 133 /// This class also lets transformation passes mark abstract *sets* of analyses 134 /// as preserved. A transformation that (say) does not alter the CFG can 135 /// indicate such by marking a particular AnalysisSetKey as preserved, and 136 /// then analyses can query whether that AnalysisSetKey is preserved. 137 /// 138 /// Finally, this class can represent an "abandoned" analysis, which is 139 /// not preserved even if it would be covered by some abstract set of analyses. 140 /// 141 /// Given a `PreservedAnalyses` object, an analysis will typically want to 142 /// figure out whether it is preserved. In the example below, MyAnalysisType is 143 /// preserved if it's not abandoned, and (a) it's explicitly marked as 144 /// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both 145 /// AnalysisSetA and AnalysisSetB are preserved. 146 /// 147 /// ``` 148 /// auto PAC = PA.getChecker<MyAnalysisType>(); 149 /// if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() || 150 /// (PAC.preservedSet<AnalysisSetA>() && 151 /// PAC.preservedSet<AnalysisSetB>())) { 152 /// // The analysis has been successfully preserved ... 153 /// } 154 /// ``` 155 class PreservedAnalyses { 156 public: 157 /// Convenience factory function for the empty preserved set. 158 static PreservedAnalyses none() { return PreservedAnalyses(); } 159 160 /// Construct a special preserved set that preserves all passes. 161 static PreservedAnalyses all() { 162 PreservedAnalyses PA; 163 PA.PreservedIDs.insert(&AllAnalysesKey); 164 return PA; 165 } 166 167 /// Construct a preserved analyses object with a single preserved set. 168 template <typename AnalysisSetT> 169 static PreservedAnalyses allInSet() { 170 PreservedAnalyses PA; 171 PA.preserveSet<AnalysisSetT>(); 172 return PA; 173 } 174 175 /// Mark an analysis as preserved. 176 template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); } 177 178 /// Given an analysis's ID, mark the analysis as preserved, adding it 179 /// to the set. 180 void preserve(AnalysisKey *ID) { 181 // Clear this ID from the explicit not-preserved set if present. 182 NotPreservedAnalysisIDs.erase(ID); 183 184 // If we're not already preserving all analyses (other than those in 185 // NotPreservedAnalysisIDs). 186 if (!areAllPreserved()) 187 PreservedIDs.insert(ID); 188 } 189 190 /// Mark an analysis set as preserved. 191 template <typename AnalysisSetT> void preserveSet() { 192 preserveSet(AnalysisSetT::ID()); 193 } 194 195 /// Mark an analysis set as preserved using its ID. 196 void preserveSet(AnalysisSetKey *ID) { 197 // If we're not already in the saturated 'all' state, add this set. 198 if (!areAllPreserved()) 199 PreservedIDs.insert(ID); 200 } 201 202 /// Mark an analysis as abandoned. 203 /// 204 /// An abandoned analysis is not preserved, even if it is nominally covered 205 /// by some other set or was previously explicitly marked as preserved. 206 /// 207 /// Note that you can only abandon a specific analysis, not a *set* of 208 /// analyses. 209 template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); } 210 211 /// Mark an analysis as abandoned using its ID. 212 /// 213 /// An abandoned analysis is not preserved, even if it is nominally covered 214 /// by some other set or was previously explicitly marked as preserved. 215 /// 216 /// Note that you can only abandon a specific analysis, not a *set* of 217 /// analyses. 218 void abandon(AnalysisKey *ID) { 219 PreservedIDs.erase(ID); 220 NotPreservedAnalysisIDs.insert(ID); 221 } 222 223 /// Intersect this set with another in place. 224 /// 225 /// This is a mutating operation on this preserved set, removing all 226 /// preserved passes which are not also preserved in the argument. 227 void intersect(const PreservedAnalyses &Arg) { 228 if (Arg.areAllPreserved()) 229 return; 230 if (areAllPreserved()) { 231 *this = Arg; 232 return; 233 } 234 // The intersection requires the *union* of the explicitly not-preserved 235 // IDs and the *intersection* of the preserved IDs. 236 for (auto ID : Arg.NotPreservedAnalysisIDs) { 237 PreservedIDs.erase(ID); 238 NotPreservedAnalysisIDs.insert(ID); 239 } 240 for (auto ID : PreservedIDs) 241 if (!Arg.PreservedIDs.count(ID)) 242 PreservedIDs.erase(ID); 243 } 244 245 /// Intersect this set with a temporary other set in place. 246 /// 247 /// This is a mutating operation on this preserved set, removing all 248 /// preserved passes which are not also preserved in the argument. 249 void intersect(PreservedAnalyses &&Arg) { 250 if (Arg.areAllPreserved()) 251 return; 252 if (areAllPreserved()) { 253 *this = std::move(Arg); 254 return; 255 } 256 // The intersection requires the *union* of the explicitly not-preserved 257 // IDs and the *intersection* of the preserved IDs. 258 for (auto ID : Arg.NotPreservedAnalysisIDs) { 259 PreservedIDs.erase(ID); 260 NotPreservedAnalysisIDs.insert(ID); 261 } 262 for (auto ID : PreservedIDs) 263 if (!Arg.PreservedIDs.count(ID)) 264 PreservedIDs.erase(ID); 265 } 266 267 /// A checker object that makes it easy to query for whether an analysis or 268 /// some set covering it is preserved. 269 class PreservedAnalysisChecker { 270 friend class PreservedAnalyses; 271 272 const PreservedAnalyses &PA; 273 AnalysisKey *const ID; 274 const bool IsAbandoned; 275 276 /// A PreservedAnalysisChecker is tied to a particular Analysis because 277 /// `preserved()` and `preservedSet()` both return false if the Analysis 278 /// was abandoned. 279 PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID) 280 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {} 281 282 public: 283 /// Returns true if the checker's analysis was not abandoned and either 284 /// - the analysis is explicitly preserved or 285 /// - all analyses are preserved. 286 bool preserved() { 287 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) || 288 PA.PreservedIDs.count(ID)); 289 } 290 291 /// Return true if the checker's analysis was not abandoned, i.e. it was not 292 /// explicitly invalidated. Even if the analysis is not explicitly 293 /// preserved, if the analysis is known stateless, then it is preserved. 294 bool preservedWhenStateless() { 295 return !IsAbandoned; 296 } 297 298 /// Returns true if the checker's analysis was not abandoned and either 299 /// - \p AnalysisSetT is explicitly preserved or 300 /// - all analyses are preserved. 301 template <typename AnalysisSetT> bool preservedSet() { 302 AnalysisSetKey *SetID = AnalysisSetT::ID(); 303 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) || 304 PA.PreservedIDs.count(SetID)); 305 } 306 }; 307 308 /// Build a checker for this `PreservedAnalyses` and the specified analysis 309 /// type. 310 /// 311 /// You can use the returned object to query whether an analysis was 312 /// preserved. See the example in the comment on `PreservedAnalysis`. 313 template <typename AnalysisT> PreservedAnalysisChecker getChecker() const { 314 return PreservedAnalysisChecker(*this, AnalysisT::ID()); 315 } 316 317 /// Build a checker for this `PreservedAnalyses` and the specified analysis 318 /// ID. 319 /// 320 /// You can use the returned object to query whether an analysis was 321 /// preserved. See the example in the comment on `PreservedAnalysis`. 322 PreservedAnalysisChecker getChecker(AnalysisKey *ID) const { 323 return PreservedAnalysisChecker(*this, ID); 324 } 325 326 /// Test whether all analyses are preserved (and none are abandoned). 327 /// 328 /// This is used primarily to optimize for the common case of a transformation 329 /// which makes no changes to the IR. 330 bool areAllPreserved() const { 331 return NotPreservedAnalysisIDs.empty() && 332 PreservedIDs.count(&AllAnalysesKey); 333 } 334 335 /// Directly test whether a set of analyses is preserved. 336 /// 337 /// This is only true when no analyses have been explicitly abandoned. 338 template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const { 339 return allAnalysesInSetPreserved(AnalysisSetT::ID()); 340 } 341 342 /// Directly test whether a set of analyses is preserved. 343 /// 344 /// This is only true when no analyses have been explicitly abandoned. 345 bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const { 346 return NotPreservedAnalysisIDs.empty() && 347 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID)); 348 } 349 350 private: 351 /// A special key used to indicate all analyses. 352 static AnalysisSetKey AllAnalysesKey; 353 354 /// The IDs of analyses and analysis sets that are preserved. 355 SmallPtrSet<void *, 2> PreservedIDs; 356 357 /// The IDs of explicitly not-preserved analyses. 358 /// 359 /// If an analysis in this set is covered by a set in `PreservedIDs`, we 360 /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always 361 /// "wins" over analysis sets in `PreservedIDs`. 362 /// 363 /// Also, a given ID should never occur both here and in `PreservedIDs`. 364 SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs; 365 }; 366 367 // Forward declare the analysis manager template. 368 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager; 369 370 /// A CRTP mix-in to automatically provide informational APIs needed for 371 /// passes. 372 /// 373 /// This provides some boilerplate for types that are passes. 374 template <typename DerivedT> struct PassInfoMixin { 375 /// Gets the name of the pass we are mixed into. 376 static StringRef name() { 377 static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value, 378 "Must pass the derived type as the template argument!"); 379 StringRef Name = getTypeName<DerivedT>(); 380 if (Name.startswith("llvm::")) 381 Name = Name.drop_front(strlen("llvm::")); 382 return Name; 383 } 384 }; 385 386 /// A CRTP mix-in that provides informational APIs needed for analysis passes. 387 /// 388 /// This provides some boilerplate for types that are analysis passes. It 389 /// automatically mixes in \c PassInfoMixin. 390 template <typename DerivedT> 391 struct AnalysisInfoMixin : PassInfoMixin<DerivedT> { 392 /// Returns an opaque, unique ID for this analysis type. 393 /// 394 /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus 395 /// suitable for use in sets, maps, and other data structures that use the low 396 /// bits of pointers. 397 /// 398 /// Note that this requires the derived type provide a static \c AnalysisKey 399 /// member called \c Key. 400 /// 401 /// FIXME: The only reason the mixin type itself can't declare the Key value 402 /// is that some compilers cannot correctly unique a templated static variable 403 /// so it has the same addresses in each instantiation. The only currently 404 /// known platform with this limitation is Windows DLL builds, specifically 405 /// building each part of LLVM as a DLL. If we ever remove that build 406 /// configuration, this mixin can provide the static key as well. 407 static AnalysisKey *ID() { 408 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value, 409 "Must pass the derived type as the template argument!"); 410 return &DerivedT::Key; 411 } 412 }; 413 414 namespace detail { 415 416 /// Actual unpacker of extra arguments in getAnalysisResult, 417 /// passes only those tuple arguments that are mentioned in index_sequence. 418 template <typename PassT, typename IRUnitT, typename AnalysisManagerT, 419 typename... ArgTs, size_t... Ns> 420 typename PassT::Result 421 getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR, 422 std::tuple<ArgTs...> Args, 423 std::index_sequence<Ns...>) { 424 (void)Args; 425 return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...); 426 } 427 428 /// Helper for *partial* unpacking of extra arguments in getAnalysisResult. 429 /// 430 /// Arguments passed in tuple come from PassManager, so they might have extra 431 /// arguments after those AnalysisManager's ExtraArgTs ones that we need to 432 /// pass to getResult. 433 template <typename PassT, typename IRUnitT, typename... AnalysisArgTs, 434 typename... MainArgTs> 435 typename PassT::Result 436 getAnalysisResult(AnalysisManager<IRUnitT, AnalysisArgTs...> &AM, IRUnitT &IR, 437 std::tuple<MainArgTs...> Args) { 438 return (getAnalysisResultUnpackTuple< 439 PassT, IRUnitT>)(AM, IR, Args, 440 std::index_sequence_for<AnalysisArgTs...>{}); 441 } 442 443 } // namespace detail 444 445 // Forward declare the pass instrumentation analysis explicitly queried in 446 // generic PassManager code. 447 // FIXME: figure out a way to move PassInstrumentationAnalysis into its own 448 // header. 449 class PassInstrumentationAnalysis; 450 451 /// Manages a sequence of passes over a particular unit of IR. 452 /// 453 /// A pass manager contains a sequence of passes to run over a particular unit 454 /// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of 455 /// IR, and when run over some given IR will run each of its contained passes in 456 /// sequence. Pass managers are the primary and most basic building block of a 457 /// pass pipeline. 458 /// 459 /// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT> 460 /// argument. The pass manager will propagate that analysis manager to each 461 /// pass it runs, and will call the analysis manager's invalidation routine with 462 /// the PreservedAnalyses of each pass it runs. 463 template <typename IRUnitT, 464 typename AnalysisManagerT = AnalysisManager<IRUnitT>, 465 typename... ExtraArgTs> 466 class PassManager : public PassInfoMixin< 467 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> { 468 public: 469 /// Construct a pass manager. 470 explicit PassManager() {} 471 472 // FIXME: These are equivalent to the default move constructor/move 473 // assignment. However, using = default triggers linker errors due to the 474 // explicit instantiations below. Find away to use the default and remove the 475 // duplicated code here. 476 PassManager(PassManager &&Arg) : Passes(std::move(Arg.Passes)) {} 477 478 PassManager &operator=(PassManager &&RHS) { 479 Passes = std::move(RHS.Passes); 480 return *this; 481 } 482 483 /// Run all of the passes in this manager over the given unit of IR. 484 /// ExtraArgs are passed to each pass. 485 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, 486 ExtraArgTs... ExtraArgs) { 487 PreservedAnalyses PA = PreservedAnalyses::all(); 488 489 // Request PassInstrumentation from analysis manager, will use it to run 490 // instrumenting callbacks for the passes later. 491 // Here we use std::tuple wrapper over getResult which helps to extract 492 // AnalysisManager's arguments out of the whole ExtraArgs set. 493 PassInstrumentation PI = 494 detail::getAnalysisResult<PassInstrumentationAnalysis>( 495 AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...)); 496 497 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { 498 auto *P = Passes[Idx].get(); 499 500 // Check the PassInstrumentation's BeforePass callbacks before running the 501 // pass, skip its execution completely if asked to (callback returns 502 // false). 503 if (!PI.runBeforePass<IRUnitT>(*P, IR)) 504 continue; 505 506 PreservedAnalyses PassPA; 507 { 508 TimeTraceScope TimeScope(P->name(), IR.getName()); 509 PassPA = P->run(IR, AM, ExtraArgs...); 510 } 511 512 // Call onto PassInstrumentation's AfterPass callbacks immediately after 513 // running the pass. 514 PI.runAfterPass<IRUnitT>(*P, IR, PassPA); 515 516 // Update the analysis manager as each pass runs and potentially 517 // invalidates analyses. 518 AM.invalidate(IR, PassPA); 519 520 // Finally, intersect the preserved analyses to compute the aggregate 521 // preserved set for this pass manager. 522 PA.intersect(std::move(PassPA)); 523 524 // FIXME: Historically, the pass managers all called the LLVM context's 525 // yield function here. We don't have a generic way to acquire the 526 // context and it isn't yet clear what the right pattern is for yielding 527 // in the new pass manager so it is currently omitted. 528 //IR.getContext().yield(); 529 } 530 531 // Invalidation was handled after each pass in the above loop for the 532 // current unit of IR. Therefore, the remaining analysis results in the 533 // AnalysisManager are preserved. We mark this with a set so that we don't 534 // need to inspect each one individually. 535 PA.preserveSet<AllAnalysesOn<IRUnitT>>(); 536 537 return PA; 538 } 539 540 template <typename PassT> 541 std::enable_if_t<!std::is_same<PassT, PassManager>::value> 542 addPass(PassT Pass) { 543 using PassModelT = 544 detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT, 545 ExtraArgTs...>; 546 547 Passes.emplace_back(new PassModelT(std::move(Pass))); 548 } 549 550 /// When adding a pass manager pass that has the same type as this pass 551 /// manager, simply move the passes over. This is because we don't have use 552 /// cases rely on executing nested pass managers. Doing this could reduce 553 /// implementation complexity and avoid potential invalidation issues that may 554 /// happen with nested pass managers of the same type. 555 template <typename PassT> 556 std::enable_if_t<std::is_same<PassT, PassManager>::value> 557 addPass(PassT &&Pass) { 558 for (auto &P : Pass.Passes) 559 Passes.emplace_back(std::move(P)); 560 } 561 562 /// Returns if the pass manager contains any passes. 563 bool isEmpty() const { return Passes.empty(); } 564 565 static bool isRequired() { return true; } 566 567 protected: 568 using PassConceptT = 569 detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>; 570 571 std::vector<std::unique_ptr<PassConceptT>> Passes; 572 }; 573 574 extern template class PassManager<Module>; 575 576 /// Convenience typedef for a pass manager over modules. 577 using ModulePassManager = PassManager<Module>; 578 579 extern template class PassManager<Function>; 580 581 /// Convenience typedef for a pass manager over functions. 582 using FunctionPassManager = PassManager<Function>; 583 584 /// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass 585 /// managers. Goes before AnalysisManager definition to provide its 586 /// internals (e.g PassInstrumentationAnalysis::ID) for use there if needed. 587 /// FIXME: figure out a way to move PassInstrumentationAnalysis into its own 588 /// header. 589 class PassInstrumentationAnalysis 590 : public AnalysisInfoMixin<PassInstrumentationAnalysis> { 591 friend AnalysisInfoMixin<PassInstrumentationAnalysis>; 592 static AnalysisKey Key; 593 594 PassInstrumentationCallbacks *Callbacks; 595 596 public: 597 /// PassInstrumentationCallbacks object is shared, owned by something else, 598 /// not this analysis. 599 PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr) 600 : Callbacks(Callbacks) {} 601 602 using Result = PassInstrumentation; 603 604 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs> 605 Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) { 606 return PassInstrumentation(Callbacks); 607 } 608 }; 609 610 /// A container for analyses that lazily runs them and caches their 611 /// results. 612 /// 613 /// This class can manage analyses for any IR unit where the address of the IR 614 /// unit sufficies as its identity. 615 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager { 616 public: 617 class Invalidator; 618 619 private: 620 // Now that we've defined our invalidator, we can define the concept types. 621 using ResultConceptT = 622 detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>; 623 using PassConceptT = 624 detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator, 625 ExtraArgTs...>; 626 627 /// List of analysis pass IDs and associated concept pointers. 628 /// 629 /// Requires iterators to be valid across appending new entries and arbitrary 630 /// erases. Provides the analysis ID to enable finding iterators to a given 631 /// entry in maps below, and provides the storage for the actual result 632 /// concept. 633 using AnalysisResultListT = 634 std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>; 635 636 /// Map type from IRUnitT pointer to our custom list type. 637 using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>; 638 639 /// Map type from a pair of analysis ID and IRUnitT pointer to an 640 /// iterator into a particular result list (which is where the actual analysis 641 /// result is stored). 642 using AnalysisResultMapT = 643 DenseMap<std::pair<AnalysisKey *, IRUnitT *>, 644 typename AnalysisResultListT::iterator>; 645 646 public: 647 /// API to communicate dependencies between analyses during invalidation. 648 /// 649 /// When an analysis result embeds handles to other analysis results, it 650 /// needs to be invalidated both when its own information isn't preserved and 651 /// when any of its embedded analysis results end up invalidated. We pass an 652 /// \c Invalidator object as an argument to \c invalidate() in order to let 653 /// the analysis results themselves define the dependency graph on the fly. 654 /// This lets us avoid building an explicit representation of the 655 /// dependencies between analysis results. 656 class Invalidator { 657 public: 658 /// Trigger the invalidation of some other analysis pass if not already 659 /// handled and return whether it was in fact invalidated. 660 /// 661 /// This is expected to be called from within a given analysis result's \c 662 /// invalidate method to trigger a depth-first walk of all inter-analysis 663 /// dependencies. The same \p IR unit and \p PA passed to that result's \c 664 /// invalidate method should in turn be provided to this routine. 665 /// 666 /// The first time this is called for a given analysis pass, it will call 667 /// the corresponding result's \c invalidate method. Subsequent calls will 668 /// use a cache of the results of that initial call. It is an error to form 669 /// cyclic dependencies between analysis results. 670 /// 671 /// This returns true if the given analysis's result is invalid. Any 672 /// dependecies on it will become invalid as a result. 673 template <typename PassT> 674 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) { 675 using ResultModelT = 676 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result, 677 PreservedAnalyses, Invalidator>; 678 679 return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA); 680 } 681 682 /// A type-erased variant of the above invalidate method with the same core 683 /// API other than passing an analysis ID rather than an analysis type 684 /// parameter. 685 /// 686 /// This is sadly less efficient than the above routine, which leverages 687 /// the type parameter to avoid the type erasure overhead. 688 bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) { 689 return invalidateImpl<>(ID, IR, PA); 690 } 691 692 private: 693 friend class AnalysisManager; 694 695 template <typename ResultT = ResultConceptT> 696 bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR, 697 const PreservedAnalyses &PA) { 698 // If we've already visited this pass, return true if it was invalidated 699 // and false otherwise. 700 auto IMapI = IsResultInvalidated.find(ID); 701 if (IMapI != IsResultInvalidated.end()) 702 return IMapI->second; 703 704 // Otherwise look up the result object. 705 auto RI = Results.find({ID, &IR}); 706 assert(RI != Results.end() && 707 "Trying to invalidate a dependent result that isn't in the " 708 "manager's cache is always an error, likely due to a stale result " 709 "handle!"); 710 711 auto &Result = static_cast<ResultT &>(*RI->second->second); 712 713 // Insert into the map whether the result should be invalidated and return 714 // that. Note that we cannot reuse IMapI and must do a fresh insert here, 715 // as calling invalidate could (recursively) insert things into the map, 716 // making any iterator or reference invalid. 717 bool Inserted; 718 std::tie(IMapI, Inserted) = 719 IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)}); 720 (void)Inserted; 721 assert(Inserted && "Should not have already inserted this ID, likely " 722 "indicates a dependency cycle!"); 723 return IMapI->second; 724 } 725 726 Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated, 727 const AnalysisResultMapT &Results) 728 : IsResultInvalidated(IsResultInvalidated), Results(Results) {} 729 730 SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated; 731 const AnalysisResultMapT &Results; 732 }; 733 734 /// Construct an empty analysis manager. 735 AnalysisManager(); 736 AnalysisManager(AnalysisManager &&); 737 AnalysisManager &operator=(AnalysisManager &&); 738 739 /// Returns true if the analysis manager has an empty results cache. 740 bool empty() const { 741 assert(AnalysisResults.empty() == AnalysisResultLists.empty() && 742 "The storage and index of analysis results disagree on how many " 743 "there are!"); 744 return AnalysisResults.empty(); 745 } 746 747 /// Clear any cached analysis results for a single unit of IR. 748 /// 749 /// This doesn't invalidate, but instead simply deletes, the relevant results. 750 /// It is useful when the IR is being removed and we want to clear out all the 751 /// memory pinned for it. 752 void clear(IRUnitT &IR, llvm::StringRef Name); 753 754 /// Clear all analysis results cached by this AnalysisManager. 755 /// 756 /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply 757 /// deletes them. This lets you clean up the AnalysisManager when the set of 758 /// IR units itself has potentially changed, and thus we can't even look up a 759 /// a result and invalidate/clear it directly. 760 void clear() { 761 AnalysisResults.clear(); 762 AnalysisResultLists.clear(); 763 } 764 765 /// Get the result of an analysis pass for a given IR unit. 766 /// 767 /// Runs the analysis if a cached result is not available. 768 template <typename PassT> 769 typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) { 770 assert(AnalysisPasses.count(PassT::ID()) && 771 "This analysis pass was not registered prior to being queried"); 772 ResultConceptT &ResultConcept = 773 getResultImpl(PassT::ID(), IR, ExtraArgs...); 774 775 using ResultModelT = 776 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result, 777 PreservedAnalyses, Invalidator>; 778 779 return static_cast<ResultModelT &>(ResultConcept).Result; 780 } 781 782 /// Get the cached result of an analysis pass for a given IR unit. 783 /// 784 /// This method never runs the analysis. 785 /// 786 /// \returns null if there is no cached result. 787 template <typename PassT> 788 typename PassT::Result *getCachedResult(IRUnitT &IR) const { 789 assert(AnalysisPasses.count(PassT::ID()) && 790 "This analysis pass was not registered prior to being queried"); 791 792 ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR); 793 if (!ResultConcept) 794 return nullptr; 795 796 using ResultModelT = 797 detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result, 798 PreservedAnalyses, Invalidator>; 799 800 return &static_cast<ResultModelT *>(ResultConcept)->Result; 801 } 802 803 /// Verify that the given Result cannot be invalidated, assert otherwise. 804 template <typename PassT> 805 void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const { 806 PreservedAnalyses PA = PreservedAnalyses::none(); 807 SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated; 808 Invalidator Inv(IsResultInvalidated, AnalysisResults); 809 assert(!Result->invalidate(IR, PA, Inv) && 810 "Cached result cannot be invalidated"); 811 } 812 813 /// Register an analysis pass with the manager. 814 /// 815 /// The parameter is a callable whose result is an analysis pass. This allows 816 /// passing in a lambda to construct the analysis. 817 /// 818 /// The analysis type to register is the type returned by calling the \c 819 /// PassBuilder argument. If that type has already been registered, then the 820 /// argument will not be called and this function will return false. 821 /// Otherwise, we register the analysis returned by calling \c PassBuilder(), 822 /// and this function returns true. 823 /// 824 /// (Note: Although the return value of this function indicates whether or not 825 /// an analysis was previously registered, there intentionally isn't a way to 826 /// query this directly. Instead, you should just register all the analyses 827 /// you might want and let this class run them lazily. This idiom lets us 828 /// minimize the number of times we have to look up analyses in our 829 /// hashtable.) 830 template <typename PassBuilderT> 831 bool registerPass(PassBuilderT &&PassBuilder) { 832 using PassT = decltype(PassBuilder()); 833 using PassModelT = 834 detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses, 835 Invalidator, ExtraArgTs...>; 836 837 auto &PassPtr = AnalysisPasses[PassT::ID()]; 838 if (PassPtr) 839 // Already registered this pass type! 840 return false; 841 842 // Construct a new model around the instance returned by the builder. 843 PassPtr.reset(new PassModelT(PassBuilder())); 844 return true; 845 } 846 847 /// Invalidate cached analyses for an IR unit. 848 /// 849 /// Walk through all of the analyses pertaining to this unit of IR and 850 /// invalidate them, unless they are preserved by the PreservedAnalyses set. 851 void invalidate(IRUnitT &IR, const PreservedAnalyses &PA); 852 853 private: 854 /// Look up a registered analysis pass. 855 PassConceptT &lookUpPass(AnalysisKey *ID) { 856 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID); 857 assert(PI != AnalysisPasses.end() && 858 "Analysis passes must be registered prior to being queried!"); 859 return *PI->second; 860 } 861 862 /// Look up a registered analysis pass. 863 const PassConceptT &lookUpPass(AnalysisKey *ID) const { 864 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID); 865 assert(PI != AnalysisPasses.end() && 866 "Analysis passes must be registered prior to being queried!"); 867 return *PI->second; 868 } 869 870 /// Get an analysis result, running the pass if necessary. 871 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR, 872 ExtraArgTs... ExtraArgs); 873 874 /// Get a cached analysis result or return null. 875 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const { 876 typename AnalysisResultMapT::const_iterator RI = 877 AnalysisResults.find({ID, &IR}); 878 return RI == AnalysisResults.end() ? nullptr : &*RI->second->second; 879 } 880 881 /// Map type from analysis pass ID to pass concept pointer. 882 using AnalysisPassMapT = 883 DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>; 884 885 /// Collection of analysis passes, indexed by ID. 886 AnalysisPassMapT AnalysisPasses; 887 888 /// Map from IR unit to a list of analysis results. 889 /// 890 /// Provides linear time removal of all analysis results for a IR unit and 891 /// the ultimate storage for a particular cached analysis result. 892 AnalysisResultListMapT AnalysisResultLists; 893 894 /// Map from an analysis ID and IR unit to a particular cached 895 /// analysis result. 896 AnalysisResultMapT AnalysisResults; 897 }; 898 899 extern template class AnalysisManager<Module>; 900 901 /// Convenience typedef for the Module analysis manager. 902 using ModuleAnalysisManager = AnalysisManager<Module>; 903 904 extern template class AnalysisManager<Function>; 905 906 /// Convenience typedef for the Function analysis manager. 907 using FunctionAnalysisManager = AnalysisManager<Function>; 908 909 /// An analysis over an "outer" IR unit that provides access to an 910 /// analysis manager over an "inner" IR unit. The inner unit must be contained 911 /// in the outer unit. 912 /// 913 /// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is 914 /// an analysis over Modules (the "outer" unit) that provides access to a 915 /// Function analysis manager. The FunctionAnalysisManager is the "inner" 916 /// manager being proxied, and Functions are the "inner" unit. The inner/outer 917 /// relationship is valid because each Function is contained in one Module. 918 /// 919 /// If you're (transitively) within a pass manager for an IR unit U that 920 /// contains IR unit V, you should never use an analysis manager over V, except 921 /// via one of these proxies. 922 /// 923 /// Note that the proxy's result is a move-only RAII object. The validity of 924 /// the analyses in the inner analysis manager is tied to its lifetime. 925 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs> 926 class InnerAnalysisManagerProxy 927 : public AnalysisInfoMixin< 928 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> { 929 public: 930 class Result { 931 public: 932 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {} 933 934 Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) { 935 // We have to null out the analysis manager in the moved-from state 936 // because we are taking ownership of the responsibilty to clear the 937 // analysis state. 938 Arg.InnerAM = nullptr; 939 } 940 941 ~Result() { 942 // InnerAM is cleared in a moved from state where there is nothing to do. 943 if (!InnerAM) 944 return; 945 946 // Clear out the analysis manager if we're being destroyed -- it means we 947 // didn't even see an invalidate call when we got invalidated. 948 InnerAM->clear(); 949 } 950 951 Result &operator=(Result &&RHS) { 952 InnerAM = RHS.InnerAM; 953 // We have to null out the analysis manager in the moved-from state 954 // because we are taking ownership of the responsibilty to clear the 955 // analysis state. 956 RHS.InnerAM = nullptr; 957 return *this; 958 } 959 960 /// Accessor for the analysis manager. 961 AnalysisManagerT &getManager() { return *InnerAM; } 962 963 /// Handler for invalidation of the outer IR unit, \c IRUnitT. 964 /// 965 /// If the proxy analysis itself is not preserved, we assume that the set of 966 /// inner IR objects contained in IRUnit may have changed. In this case, 967 /// we have to call \c clear() on the inner analysis manager, as it may now 968 /// have stale pointers to its inner IR objects. 969 /// 970 /// Regardless of whether the proxy analysis is marked as preserved, all of 971 /// the analyses in the inner analysis manager are potentially invalidated 972 /// based on the set of preserved analyses. 973 bool invalidate( 974 IRUnitT &IR, const PreservedAnalyses &PA, 975 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv); 976 977 private: 978 AnalysisManagerT *InnerAM; 979 }; 980 981 explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM) 982 : InnerAM(&InnerAM) {} 983 984 /// Run the analysis pass and create our proxy result object. 985 /// 986 /// This doesn't do any interesting work; it is primarily used to insert our 987 /// proxy result object into the outer analysis cache so that we can proxy 988 /// invalidation to the inner analysis manager. 989 Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM, 990 ExtraArgTs...) { 991 return Result(*InnerAM); 992 } 993 994 private: 995 friend AnalysisInfoMixin< 996 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>; 997 998 static AnalysisKey Key; 999 1000 AnalysisManagerT *InnerAM; 1001 }; 1002 1003 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs> 1004 AnalysisKey 1005 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key; 1006 1007 /// Provide the \c FunctionAnalysisManager to \c Module proxy. 1008 using FunctionAnalysisManagerModuleProxy = 1009 InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>; 1010 1011 /// Specialization of the invalidate method for the \c 1012 /// FunctionAnalysisManagerModuleProxy's result. 1013 template <> 1014 bool FunctionAnalysisManagerModuleProxy::Result::invalidate( 1015 Module &M, const PreservedAnalyses &PA, 1016 ModuleAnalysisManager::Invalidator &Inv); 1017 1018 // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern 1019 // template. 1020 extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager, 1021 Module>; 1022 1023 /// An analysis over an "inner" IR unit that provides access to an 1024 /// analysis manager over a "outer" IR unit. The inner unit must be contained 1025 /// in the outer unit. 1026 /// 1027 /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an 1028 /// analysis over Functions (the "inner" unit) which provides access to a Module 1029 /// analysis manager. The ModuleAnalysisManager is the "outer" manager being 1030 /// proxied, and Modules are the "outer" IR unit. The inner/outer relationship 1031 /// is valid because each Function is contained in one Module. 1032 /// 1033 /// This proxy only exposes the const interface of the outer analysis manager, 1034 /// to indicate that you cannot cause an outer analysis to run from within an 1035 /// inner pass. Instead, you must rely on the \c getCachedResult API. This is 1036 /// due to keeping potential future concurrency in mind. To give an example, 1037 /// running a module analysis before any function passes may give a different 1038 /// result than running it in a function pass. Both may be valid, but it would 1039 /// produce non-deterministic results. GlobalsAA is a good analysis example, 1040 /// because the cached information has the mod/ref info for all memory for each 1041 /// function at the time the analysis was computed. The information is still 1042 /// valid after a function transformation, but it may be *different* if 1043 /// recomputed after that transform. GlobalsAA is never invalidated. 1044 1045 /// 1046 /// This proxy doesn't manage invalidation in any way -- that is handled by the 1047 /// recursive return path of each layer of the pass manager. A consequence of 1048 /// this is the outer analyses may be stale. We invalidate the outer analyses 1049 /// only when we're done running passes over the inner IR units. 1050 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs> 1051 class OuterAnalysisManagerProxy 1052 : public AnalysisInfoMixin< 1053 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> { 1054 public: 1055 /// Result proxy object for \c OuterAnalysisManagerProxy. 1056 class Result { 1057 public: 1058 explicit Result(const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {} 1059 1060 /// Get a cached analysis. If the analysis can be invalidated, this will 1061 /// assert. 1062 template <typename PassT, typename IRUnitTParam> 1063 typename PassT::Result *getCachedResult(IRUnitTParam &IR) const { 1064 typename PassT::Result *Res = 1065 OuterAM->template getCachedResult<PassT>(IR); 1066 if (Res) 1067 OuterAM->template verifyNotInvalidated<PassT>(IR, Res); 1068 return Res; 1069 } 1070 1071 /// Method provided for unit testing, not intended for general use. 1072 template <typename PassT, typename IRUnitTParam> 1073 bool cachedResultExists(IRUnitTParam &IR) const { 1074 typename PassT::Result *Res = 1075 OuterAM->template getCachedResult<PassT>(IR); 1076 return Res != nullptr; 1077 } 1078 1079 /// When invalidation occurs, remove any registered invalidation events. 1080 bool invalidate( 1081 IRUnitT &IRUnit, const PreservedAnalyses &PA, 1082 typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) { 1083 // Loop over the set of registered outer invalidation mappings and if any 1084 // of them map to an analysis that is now invalid, clear it out. 1085 SmallVector<AnalysisKey *, 4> DeadKeys; 1086 for (auto &KeyValuePair : OuterAnalysisInvalidationMap) { 1087 AnalysisKey *OuterID = KeyValuePair.first; 1088 auto &InnerIDs = KeyValuePair.second; 1089 llvm::erase_if(InnerIDs, [&](AnalysisKey *InnerID) { 1090 return Inv.invalidate(InnerID, IRUnit, PA); 1091 }); 1092 if (InnerIDs.empty()) 1093 DeadKeys.push_back(OuterID); 1094 } 1095 1096 for (auto OuterID : DeadKeys) 1097 OuterAnalysisInvalidationMap.erase(OuterID); 1098 1099 // The proxy itself remains valid regardless of anything else. 1100 return false; 1101 } 1102 1103 /// Register a deferred invalidation event for when the outer analysis 1104 /// manager processes its invalidations. 1105 template <typename OuterAnalysisT, typename InvalidatedAnalysisT> 1106 void registerOuterAnalysisInvalidation() { 1107 AnalysisKey *OuterID = OuterAnalysisT::ID(); 1108 AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID(); 1109 1110 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID]; 1111 // Note, this is a linear scan. If we end up with large numbers of 1112 // analyses that all trigger invalidation on the same outer analysis, 1113 // this entire system should be changed to some other deterministic 1114 // data structure such as a `SetVector` of a pair of pointers. 1115 if (!llvm::is_contained(InvalidatedIDList, InvalidatedID)) 1116 InvalidatedIDList.push_back(InvalidatedID); 1117 } 1118 1119 /// Access the map from outer analyses to deferred invalidation requiring 1120 /// analyses. 1121 const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> & 1122 getOuterInvalidations() const { 1123 return OuterAnalysisInvalidationMap; 1124 } 1125 1126 private: 1127 const AnalysisManagerT *OuterAM; 1128 1129 /// A map from an outer analysis ID to the set of this IR-unit's analyses 1130 /// which need to be invalidated. 1131 SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> 1132 OuterAnalysisInvalidationMap; 1133 }; 1134 1135 OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM) 1136 : OuterAM(&OuterAM) {} 1137 1138 /// Run the analysis pass and create our proxy result object. 1139 /// Nothing to see here, it just forwards the \c OuterAM reference into the 1140 /// result. 1141 Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &, 1142 ExtraArgTs...) { 1143 return Result(*OuterAM); 1144 } 1145 1146 private: 1147 friend AnalysisInfoMixin< 1148 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>; 1149 1150 static AnalysisKey Key; 1151 1152 const AnalysisManagerT *OuterAM; 1153 }; 1154 1155 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs> 1156 AnalysisKey 1157 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key; 1158 1159 extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager, 1160 Function>; 1161 /// Provide the \c ModuleAnalysisManager to \c Function proxy. 1162 using ModuleAnalysisManagerFunctionProxy = 1163 OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>; 1164 1165 /// Trivial adaptor that maps from a module to its functions. 1166 /// 1167 /// Designed to allow composition of a FunctionPass(Manager) and 1168 /// a ModulePassManager, by running the FunctionPass(Manager) over every 1169 /// function in the module. 1170 /// 1171 /// Function passes run within this adaptor can rely on having exclusive access 1172 /// to the function they are run over. They should not read or modify any other 1173 /// functions! Other threads or systems may be manipulating other functions in 1174 /// the module, and so their state should never be relied on. 1175 /// FIXME: Make the above true for all of LLVM's actual passes, some still 1176 /// violate this principle. 1177 /// 1178 /// Function passes can also read the module containing the function, but they 1179 /// should not modify that module outside of the use lists of various globals. 1180 /// For example, a function pass is not permitted to add functions to the 1181 /// module. 1182 /// FIXME: Make the above true for all of LLVM's actual passes, some still 1183 /// violate this principle. 1184 /// 1185 /// Note that although function passes can access module analyses, module 1186 /// analyses are not invalidated while the function passes are running, so they 1187 /// may be stale. Function analyses will not be stale. 1188 class ModuleToFunctionPassAdaptor 1189 : public PassInfoMixin<ModuleToFunctionPassAdaptor> { 1190 public: 1191 using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>; 1192 1193 explicit ModuleToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass) 1194 : Pass(std::move(Pass)) {} 1195 1196 /// Runs the function pass across every function in the module. 1197 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 1198 1199 static bool isRequired() { return true; } 1200 1201 private: 1202 std::unique_ptr<PassConceptT> Pass; 1203 }; 1204 1205 /// A function to deduce a function pass type and wrap it in the 1206 /// templated adaptor. 1207 template <typename FunctionPassT> 1208 ModuleToFunctionPassAdaptor 1209 createModuleToFunctionPassAdaptor(FunctionPassT Pass) { 1210 using PassModelT = 1211 detail::PassModel<Function, FunctionPassT, PreservedAnalyses, 1212 FunctionAnalysisManager>; 1213 1214 return ModuleToFunctionPassAdaptor( 1215 std::make_unique<PassModelT>(std::move(Pass))); 1216 } 1217 1218 /// A utility pass template to force an analysis result to be available. 1219 /// 1220 /// If there are extra arguments at the pass's run level there may also be 1221 /// extra arguments to the analysis manager's \c getResult routine. We can't 1222 /// guess how to effectively map the arguments from one to the other, and so 1223 /// this specialization just ignores them. 1224 /// 1225 /// Specific patterns of run-method extra arguments and analysis manager extra 1226 /// arguments will have to be defined as appropriate specializations. 1227 template <typename AnalysisT, typename IRUnitT, 1228 typename AnalysisManagerT = AnalysisManager<IRUnitT>, 1229 typename... ExtraArgTs> 1230 struct RequireAnalysisPass 1231 : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT, 1232 ExtraArgTs...>> { 1233 /// Run this pass over some unit of IR. 1234 /// 1235 /// This pass can be run over any unit of IR and use any analysis manager 1236 /// provided they satisfy the basic API requirements. When this pass is 1237 /// created, these methods can be instantiated to satisfy whatever the 1238 /// context requires. 1239 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, 1240 ExtraArgTs &&... Args) { 1241 (void)AM.template getResult<AnalysisT>(Arg, 1242 std::forward<ExtraArgTs>(Args)...); 1243 1244 return PreservedAnalyses::all(); 1245 } 1246 static bool isRequired() { return true; } 1247 }; 1248 1249 /// A no-op pass template which simply forces a specific analysis result 1250 /// to be invalidated. 1251 template <typename AnalysisT> 1252 struct InvalidateAnalysisPass 1253 : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> { 1254 /// Run this pass over some unit of IR. 1255 /// 1256 /// This pass can be run over any unit of IR and use any analysis manager, 1257 /// provided they satisfy the basic API requirements. When this pass is 1258 /// created, these methods can be instantiated to satisfy whatever the 1259 /// context requires. 1260 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs> 1261 PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) { 1262 auto PA = PreservedAnalyses::all(); 1263 PA.abandon<AnalysisT>(); 1264 return PA; 1265 } 1266 }; 1267 1268 /// A utility pass that does nothing, but preserves no analyses. 1269 /// 1270 /// Because this preserves no analyses, any analysis passes queried after this 1271 /// pass runs will recompute fresh results. 1272 struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> { 1273 /// Run this pass over some unit of IR. 1274 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs> 1275 PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) { 1276 return PreservedAnalyses::none(); 1277 } 1278 }; 1279 1280 /// A utility pass template that simply runs another pass multiple times. 1281 /// 1282 /// This can be useful when debugging or testing passes. It also serves as an 1283 /// example of how to extend the pass manager in ways beyond composition. 1284 template <typename PassT> 1285 class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> { 1286 public: 1287 RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {} 1288 1289 template <typename IRUnitT, typename AnalysisManagerT, typename... Ts> 1290 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, Ts &&... Args) { 1291 1292 // Request PassInstrumentation from analysis manager, will use it to run 1293 // instrumenting callbacks for the passes later. 1294 // Here we use std::tuple wrapper over getResult which helps to extract 1295 // AnalysisManager's arguments out of the whole Args set. 1296 PassInstrumentation PI = 1297 detail::getAnalysisResult<PassInstrumentationAnalysis>( 1298 AM, IR, std::tuple<Ts...>(Args...)); 1299 1300 auto PA = PreservedAnalyses::all(); 1301 for (int i = 0; i < Count; ++i) { 1302 // Check the PassInstrumentation's BeforePass callbacks before running the 1303 // pass, skip its execution completely if asked to (callback returns 1304 // false). 1305 if (!PI.runBeforePass<IRUnitT>(P, IR)) 1306 continue; 1307 PreservedAnalyses IterPA = P.run(IR, AM, std::forward<Ts>(Args)...); 1308 PA.intersect(IterPA); 1309 PI.runAfterPass(P, IR, IterPA); 1310 } 1311 return PA; 1312 } 1313 1314 private: 1315 int Count; 1316 PassT P; 1317 }; 1318 1319 template <typename PassT> 1320 RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) { 1321 return RepeatedPass<PassT>(Count, std::move(P)); 1322 } 1323 1324 } // end namespace llvm 1325 1326 #endif // LLVM_IR_PASSMANAGER_H 1327