PassManager.h 52 KB

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