38 #ifndef LLVM_IR_PASSMANAGER_H
39 #define LLVM_IR_PASSMANAGER_H
58 #include <type_traits>
99 template <
typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
101 extern template class AllAnalysesOn<Module>;
102 extern template class AllAnalysesOn<Function>;
161 PA.PreservedIDs.
insert(&AllAnalysesKey);
166 template <
typename AnalysisSetT>
180 NotPreservedAnalysisIDs.erase(ID);
184 if (!areAllPreserved())
185 PreservedIDs.insert(ID);
196 if (!areAllPreserved())
197 PreservedIDs.insert(ID);
217 PreservedIDs.erase(ID);
218 NotPreservedAnalysisIDs.insert(ID);
228 if (areAllPreserved()) {
234 for (
auto ID : Arg.NotPreservedAnalysisIDs) {
235 PreservedIDs.erase(
ID);
236 NotPreservedAnalysisIDs.insert(
ID);
238 for (
auto ID : PreservedIDs)
239 if (!Arg.PreservedIDs.
count(
ID))
240 PreservedIDs.erase(
ID);
248 if (Arg.areAllPreserved())
250 if (areAllPreserved()) {
251 *
this = std::move(Arg);
256 for (
auto ID : Arg.NotPreservedAnalysisIDs) {
257 PreservedIDs.erase(
ID);
258 NotPreservedAnalysisIDs.insert(
ID);
260 for (
auto ID : PreservedIDs)
261 if (!Arg.PreservedIDs.count(
ID))
262 PreservedIDs.erase(
ID);
272 const bool IsAbandoned;
278 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.
count(ID)) {}
285 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
286 PA.PreservedIDs.count(
ID));
294 return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
295 PA.PreservedIDs.count(SetID));
322 return NotPreservedAnalysisIDs.empty() &&
323 PreservedIDs.count(&AllAnalysesKey);
337 return NotPreservedAnalysisIDs.empty() &&
338 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
359 template <
typename IRUnitT,
typename... ExtraArgTs>
class AnalysisManager;
368 static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
369 "Must pass the derived type as the template argument!");
381 template <
typename DerivedT>
399 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
400 "Must pass the derived type as the template argument!");
401 return &DerivedT::Key;
417 template <
typename IRUnitT,
418 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
419 typename... ExtraArgTs>
421 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
426 explicit PassManager(
bool DebugLogging =
false) : DebugLogging(DebugLogging) {}
433 : Passes(std::move(Arg.Passes)),
434 DebugLogging(std::move(Arg.DebugLogging)) {}
437 Passes = std::move(RHS.Passes);
438 DebugLogging = std::move(RHS.DebugLogging);
445 ExtraArgTs... ExtraArgs) {
449 dbgs() <<
"Starting " << getTypeName<IRUnitT>() <<
" pass manager run.\n";
451 for (
unsigned Idx = 0,
Size = Passes.size(); Idx !=
Size; ++Idx) {
453 dbgs() <<
"Running pass: " << Passes[Idx]->name() <<
" on "
454 << IR.getName() <<
"\n";
460 AM.invalidate(IR, PassPA);
480 dbgs() <<
"Finished " << getTypeName<IRUnitT>() <<
" pass manager run.\n";
490 Passes.emplace_back(
new PassModelT(std::move(Pass)));
497 std::vector<std::unique_ptr<PassConceptT>> Passes;
503 extern template class PassManager<Module>;
518 template <
typename IRUnitT,
typename... ExtraArgTs>
class AnalysisManager {
524 using ResultConceptT =
536 using AnalysisResultListT =
537 std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
545 using AnalysisResultMapT =
547 typename AnalysisResultListT::iterator>;
576 template <
typename PassT>
582 return invalidateImpl<ResultModelT>(
PassT::ID(),
IR, PA);
592 return invalidateImpl<>(
ID,
IR, PA);
598 template <
typename ResultT = ResultConceptT>
603 auto IMapI = IsResultInvalidated.find(ID);
604 if (IMapI != IsResultInvalidated.end())
605 return IMapI->second;
608 auto RI =
Results.find({ID, &IR});
610 "Trying to invalidate a dependent result that isn't in the "
611 "manager's cache is always an error, likely due to a stale result "
614 auto &Result =
static_cast<ResultT &
>(*RI->second->second);
621 std::tie(IMapI, Inserted) =
622 IsResultInvalidated.insert({
ID, Result.invalidate(IR, PA, *
this)});
624 assert(Inserted &&
"Should not have already inserted this ID, likely "
625 "indicates a dependency cycle!");
626 return IMapI->second;
629 Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
630 const AnalysisResultMapT &
Results)
631 : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
633 SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
634 const AnalysisResultMapT &
Results;
646 assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
647 "The storage and index of analysis results disagree on how many "
649 return AnalysisResults.empty();
659 dbgs() <<
"Clearing all analysis results for: " << IR.getName() <<
"\n";
661 auto ResultsListI = AnalysisResultLists.find(&IR);
662 if (ResultsListI == AnalysisResultLists.end())
665 for (
auto &IDAndResult : ResultsListI->second)
666 AnalysisResults.erase({IDAndResult.first, &IR});
669 AnalysisResultLists.erase(ResultsListI);
679 AnalysisResults.clear();
680 AnalysisResultLists.clear();
686 template <
typename PassT>
687 typename PassT::Result &
getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
689 "This analysis pass was not registered prior to being queried");
691 getResultImpl(
PassT::ID(), IR, ExtraArgs...);
697 return static_cast<ResultModelT &
>(ResultConcept).Result;
705 template <
typename PassT>
708 "This analysis pass was not registered prior to being queried");
718 return &
static_cast<ResultModelT *
>(ResultConcept)->Result;
738 template <
typename PassBuilderT>
745 auto &PassPtr = AnalysisPasses[
PassT::ID()];
761 "This analysis pass was not registered prior to being invalidated");
775 dbgs() <<
"Invalidating all non-preserved analyses for: " << IR.getName()
781 Invalidator Inv(IsResultInvalidated, AnalysisResults);
782 AnalysisResultListT &ResultsList = AnalysisResultLists[&
IR];
783 for (
auto &AnalysisResultPair : ResultsList) {
789 auto &Result = *AnalysisResultPair.second;
791 auto IMapI = IsResultInvalidated.
find(ID);
792 if (IMapI != IsResultInvalidated.
end())
802 IsResultInvalidated.
insert({
ID, Result.invalidate(IR, PA, Inv)})
805 assert(Inserted &&
"Should never have already inserted this ID, likely "
806 "indicates a cycle!");
810 if (!IsResultInvalidated.
empty()) {
811 for (
auto I = ResultsList.begin(),
E = ResultsList.end();
I !=
E;) {
813 if (!IsResultInvalidated.
lookup(ID)) {
819 dbgs() <<
"Invalidating analysis: " << this->lookUpPass(ID).name()
820 <<
" on " << IR.getName() <<
"\n";
822 I = ResultsList.erase(
I);
823 AnalysisResults.erase({
ID, &IR});
827 if (ResultsList.empty())
828 AnalysisResultLists.erase(&IR);
834 typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
835 assert(PI != AnalysisPasses.end() &&
836 "Analysis passes must be registered prior to being queried!");
841 const PassConceptT &lookUpPass(AnalysisKey *ID)
const {
842 typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
843 assert(PI != AnalysisPasses.end() &&
844 "Analysis passes must be registered prior to being queried!");
849 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
850 ExtraArgTs... ExtraArgs) {
851 typename AnalysisResultMapT::iterator RI;
853 std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
854 std::make_pair(ID, &IR),
typename AnalysisResultListT::iterator()));
859 auto &
P = this->lookUpPass(ID);
861 dbgs() <<
"Running analysis: " <<
P.name() <<
" on " << IR.getName()
863 AnalysisResultListT &ResultList = AnalysisResultLists[&
IR];
864 ResultList.emplace_back(ID,
P.run(IR, *
this, ExtraArgs...));
868 RI = AnalysisResults.find({
ID, &IR});
869 assert(RI != AnalysisResults.end() &&
"we just inserted it!");
871 RI->second = std::prev(ResultList.end());
874 return *RI->second->second;
878 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR)
const {
879 typename AnalysisResultMapT::const_iterator RI =
880 AnalysisResults.find({
ID, &IR});
881 return RI == AnalysisResults.end() ?
nullptr : &*RI->second->second;
885 void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
886 typename AnalysisResultMapT::iterator RI =
887 AnalysisResults.find({
ID, &IR});
888 if (RI == AnalysisResults.end())
892 dbgs() <<
"Invalidating analysis: " << this->lookUpPass(ID).name()
893 <<
" on " << IR.getName() <<
"\n";
894 AnalysisResultLists[&
IR].erase(RI->second);
895 AnalysisResults.erase(RI);
899 using AnalysisPassMapT =
900 DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
903 AnalysisPassMapT AnalysisPasses;
909 AnalysisResultListMapT AnalysisResultLists;
913 AnalysisResultMapT AnalysisResults;
919 extern template class AnalysisManager<Module>;
945 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
948 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
952 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
958 Arg.InnerAM =
nullptr;
972 InnerAM = RHS.InnerAM;
976 RHS.InnerAM =
nullptr;
998 AnalysisManagerT *InnerAM;
1002 : InnerAM(&InnerAM) {}
1020 AnalysisManagerT *InnerAM;
1023 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1025 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1034 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
1061 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1064 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1069 explicit Result(
const AnalysisManagerT &AM) : AM(&AM) {}
1080 for (
auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1082 auto &InnerIDs = KeyValuePair.second;
1084 return Inv.
invalidate(InnerID, IRUnit, PA); }),
1086 if (InnerIDs.empty())
1090 for (
auto OuterID : DeadKeys)
1091 OuterAnalysisInvalidationMap.erase(OuterID);
1099 template <
typename OuterAnalysisT,
typename Inval
idatedAnalysisT>
1104 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1109 auto InvalidatedIt =
std::find(InvalidatedIDList.begin(),
1110 InvalidatedIDList.end(), InvalidatedID);
1111 if (InvalidatedIt == InvalidatedIDList.end())
1112 InvalidatedIDList.push_back(InvalidatedID);
1119 return OuterAnalysisInvalidationMap;
1123 const AnalysisManagerT *AM;
1128 OuterAnalysisInvalidationMap;
1147 const AnalysisManagerT *AM;
1150 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1152 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
1183 template <
typename FunctionPassT>
1185 :
public PassInfoMixin<ModuleToFunctionPassAdaptor<FunctionPassT>> {
1188 : Pass(std::move(Pass)) {}
1192 FunctionAnalysisManager &FAM =
1196 for (Function &
F : M) {
1197 if (
F.isDeclaration())
1228 template <
typename FunctionPassT>
1229 ModuleToFunctionPassAdaptor<FunctionPassT>
1243 template <
typename AnalysisT,
typename IRUnitT,
1244 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
1245 typename... ExtraArgTs>
1247 :
PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1256 ExtraArgTs &&...
Args) {
1257 (void)AM.template getResult<AnalysisT>(Arg,
1258 std::forward<ExtraArgTs>(
Args)...);
1260 return PreservedAnalyses::all();
1266 template <
typename AnalysisT>
1275 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
1277 auto PA = PreservedAnalyses::all();
1289 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
1291 return PreservedAnalyses::none();
1299 template <
typename PassT>
1304 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... Ts>
1306 auto PA = PreservedAnalyses::all();
1307 for (
int i = 0; i < Count; ++i)
1317 template <
typename PassT>
1324 #endif // LLVM_IR_PASSMANAGER_H
Pass interface - Implemented by all 'passes'.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
void abandon()
Mark an analysis as abandoned.
void push_back(const T &Elt)
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs...ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Wrapper to model the analysis pass concept.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
const SmallDenseMap< AnalysisKey *, TinyPtrVector< AnalysisKey * >, 2 > & getOuterInvalidations() const
Access the map from outer analyses to deferred invalidation requiring analyses.
void intersect(const PreservedAnalyses &Arg)
Intersect this set with another in place.
Trivial adaptor that maps from a module to its functions.
A Module instance is used to store all the information related to an LLVM module. ...
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassManager(bool DebugLogging=false)
Construct a pass manager.
RepeatedPass< PassT > createRepeatedPass(int Count, PassT P)
RepeatedPass(int Count, PassT P)
Function Alias Analysis Results
Template for the abstract base class used to dispatch polymorphically over pass objects.
A utility pass template that simply runs another pass multiple times.
A utility pass template to force an analysis result to be available.
bool empty() const
Returns true if the analysis manager has an empty results cache.
Abstract concept of an analysis pass.
AnalysisManager(bool DebugLogging=false)
Construct an empty analysis manager.
OuterAnalysisManagerProxy(const AnalysisManagerT &AM)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
This class provides access to building LLVM's passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
AnalysisManagerT & getManager()
Accessor for the analysis manager.
bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const
Directly test whether a set of analyses is preserved.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
PassManager(PassManager &&Arg)
void invalidate(IRUnitT &IR)
Invalidate a specific analysis pass for an IR module.
A CRTP mix-in to automatically provide informational APIs needed for passes.
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, Ts &&...Args)
Result run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs...> &AM, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Result run(IRUnitT &, AnalysisManager< IRUnitT, ExtraArgTs...> &, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
void registerOuterAnalysisInvalidation()
Register a deferred invalidation event for when the outer analysis manager processes its invalidation...
static PreservedAnalyses allInSet()
Construct a preserved analyses object with a single preserved set.
const AnalysisManagerT & getManager() const
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...Args)
Run this pass over some unit of IR.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
iterator find(const_arg_type_t< KeyT > Val)
A checker object that makes it easy to query for whether an analysis or some set covering it is prese...
A set of analyses that are preserved following a run of a transformation pass.
InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
ModuleToFunctionPassAdaptor< FunctionPassT > createModuleToFunctionPassAdaptor(FunctionPassT Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
bool invalidate(IRUnitT &IRUnit, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs...>::Invalidator &Inv)
When invalidation occurs, remove any registered invalidation events.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Wrapper to model the analysis result concept.
bool preservedSet()
Returns true if the checker's analysis was not abandoned and either.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...)
Run this pass over some unit of IR.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
static StringRef name()
Gets the name of the pass we are mixed into.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void intersect(PreservedAnalyses &&Arg)
Intersect this set with a temporary other set in place.
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
PassManager & operator=(PassManager &&RHS)
void clear()
Clear all analysis results cached by this AnalysisManager.
Result proxy object for OuterAnalysisManagerProxy.
ModuleToFunctionPassAdaptor(FunctionPassT Pass)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
print lazy value Lazy Value Info Printer Pass
static AnalysisSetKey * ID()
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA)
A type-erased variant of the above invalidate method with the same core API other than passing an ana...
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
A template wrapper used to implement the polymorphic API.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Abstract concept of an analysis result.
A utility pass that does nothing, but preserves no analyses.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Result(AnalysisManagerT &InnerAM)
static AnalysisSetKey * ID()
This header provides internal APIs and implementation details used by the pass management interfaces ...
Represents analyses that only rely on functions' control flow.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
void abandon(AnalysisKey *ID)
Mark an analysis as abandoned using its ID.
Result & operator=(Result &&RHS)
void preserveSet(AnalysisSetKey *ID)
Mark an analysis set as preserved using its ID.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
A special type used to provide an address that identifies a set of related analyses.
void clear(IRUnitT &IR)
Clear any cached analysis results for a single unit of IR.
Manages a sequence of passes over a particular unit of IR.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
void preserveSet()
Mark an analysis set as preserved.
void preserve()
Mark an analysis as preserved.
A no-op pass template which simply forces a specific analysis result to be invalidated.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_NODISCARD bool empty() const
API to communicate dependencies between analyses during invalidation.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over \<a particular IR unit\>" (e...
void preserve(AnalysisKey *ID)
Given an analysis's ID, mark the analysis as preserved, adding it to the set.
bool preserved()
Returns true if the checker's analysis was not abandoned and either.
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Result(const AnalysisManagerT &AM)
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
PreservedAnalysisChecker getChecker(AnalysisKey *ID) const
Build a checker for this PreservedAnalyses and the specified analysis ID.
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...)
Run this pass over some unit of IR.
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
A special type used by analysis passes to provide an address that identifies that particular analysis...
Statically lint checks LLVM IR
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...