PassManager.h 54 KB

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