37#ifndef LLVM_IR_PASSMANAGER_H
38#define LLVM_IR_PASSMANAGER_H
65 static_assert(std::is_base_of<InfoMixin, DerivedT>::value,
66 "Must pass the derived type as the template argument!");
68 Name.consume_front(
"llvm::");
81template <
typename DerivedT>
86 auto PassName = MapClassName2PassName(ClassName);
99template <
typename IRUnitT,
typename... ExtraArgTs>
class AnalysisManager;
102template <
typename DerivedT>
113template <
typename DerivedT>
122template <
typename DerivedT>
140 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
141 "Must pass the derived type as the template argument!");
142 return &DerivedT::Key;
150template <
typename PassT,
typename IRUnitT,
typename AnalysisManagerT,
151 typename... ArgTs,
size_t... Ns>
152typename PassT::Result
154 std::tuple<ArgTs...> Args,
155 std::index_sequence<Ns...>) {
157 return AM.template getResult<PassT>(
IR, std::get<Ns>(Args)...);
165template <
typename PassT,
typename IRUnitT,
typename... AnalysisArgTs,
166 typename... MainArgTs>
167typename PassT::Result
169 std::tuple<MainArgTs...> Args) {
171 PassT, IRUnitT>)(AM,
IR, Args,
172 std::index_sequence_for<AnalysisArgTs...>{});
189template <
typename IRUnitT,
190 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
191 typename... ExtraArgTs>
193 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
214 P->printPipeline(OS, MapClassName2PassName);
221 ExtraArgTs... ExtraArgs);
223 template <
typename PassT>
228 Passes.push_back(PassModelT::create(std::move(
Pass)));
236 template <
typename PassT>
239 for (
auto &
P :
Pass.Passes)
240 Passes.push_back(std::move(
P));
250 std::vector<typename PassConceptT::unique_ptr>
Passes;
253template <
typename IRUnitT>
295 using AnalysisResultListT =
305 using AnalysisResultMapT =
335 template <
typename PassT>
341 return invalidateImpl<ResultModelT>(PassT::ID(),
IR, PA);
351 return invalidateImpl<>(ID,
IR, PA);
358 enum class ResultState :
uint8_t { Unknown, InProgress, Valid, Invalid };
360 template <
typename ResultT = ResultConceptT>
363 assert(&
IR == &IRUnit &&
"Invalidating a result of another IR unit");
364 auto RI =
llvm::find_if(ResultsList, [ID](
const auto &IDAndResult) {
365 return IDAndResult.first == ID;
368 "Trying to invalidate a dependent result that isn't in the "
369 "manager's cache is always an error, likely due to a stale result "
371 return invalidateResult<ResultT>(RI - ResultsList.
begin(), PA);
376 template <
typename ResultT = ResultConceptT>
377 bool invalidateResult(
size_t Idx,
const PreservedAnalyses &PA) {
378 if (States[Idx] != ResultState::Unknown) {
379 assert(States[Idx] != ResultState::InProgress &&
380 "Dependency cycle between analysis results");
381 return States[Idx] == ResultState::Invalid;
384 States[
Idx] = ResultState::InProgress;
385 auto &
Result =
static_cast<ResultT &
>(*ResultsList[
Idx].second);
386 bool Invalidated =
Result.invalidate(IRUnit, PA, *
this);
387 States[
Idx] = Invalidated ? ResultState::Invalid : ResultState::Valid;
391 Invalidator(IRUnitT &
IR,
const AnalysisResultListT &ResultsList,
392 SmallVectorImpl<ResultState> &States)
393 : IRUnit(
IR), ResultsList(ResultsList), States(States) {}
396 const AnalysisResultListT &ResultsList;
397 SmallVectorImpl<ResultState> &States;
407 assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
408 "The storage and index of analysis results disagree on how many "
410 return AnalysisResults.empty();
427 AnalysisResults.clear();
428 AnalysisResultLists.clear();
433 return AnalysisPasses.count(PassT::ID());
439 template <
typename PassT>
440 typename PassT::Result &
getResult(IRUnitT &
IR, ExtraArgTs... ExtraArgs) {
441 assert(AnalysisPasses.count(PassT::ID()) &&
442 "This analysis pass was not registered prior to being queried");
443 ResultConceptT &ResultConcept =
444 getResultImpl(PassT::ID(),
IR, ExtraArgs...);
450 return static_cast<ResultModelT &
>(ResultConcept).Result;
458 template <
typename PassT>
460 assert(AnalysisPasses.count(PassT::ID()) &&
461 "This analysis pass was not registered prior to being queried");
463 ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(),
IR);
471 return &
static_cast<ResultModelT *
>(ResultConcept)->Result;
475 template <
typename PassT>
478 auto ResultsListI = AnalysisResultLists.find(&
IR);
479 assert(ResultsListI != AnalysisResultLists.end() &&
480 "Result is not cached for this IR unit");
481 const AnalysisResultListT &ResultsList = ResultsListI->second;
483 ResultsList.
size(), Invalidator::ResultState::Unknown);
484 Invalidator Inv(
IR, ResultsList, States);
485 assert(!Result->invalidate(
IR, PA, Inv) &&
486 "Cached result cannot be invalidated");
505 template <
typename PassBuilderT>
511 auto &PassPtr = AnalysisPasses[PassT::ID()];
533 auto ResultsListI = AnalysisResultLists.find(&
IR);
534 assert(ResultsListI != AnalysisResultLists.end() &&
535 "Analysis must be available");
536 AnalysisResultListT &ResultsList = ResultsListI->second;
541 assert(
I != ResultsList.
end() &&
"Analysis must be available");
543 AnalysisResults.erase({ID, &
IR});
551 "Analysis passes must be registered prior to being queried!");
556 const PassConceptT &lookUpPass(AnalysisKey *ID)
const {
559 "Analysis passes must be registered prior to being queried!");
564 ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &
IR,
565 ExtraArgTs... ExtraArgs);
568 ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &
IR)
const {
569 typename AnalysisResultMapT::const_iterator RI =
570 AnalysisResults.find({
ID, &
IR});
571 return RI == AnalysisResults.end() ? nullptr : RI->second;
575 using AnalysisPassMapT =
576 DenseMap<AnalysisKey *, typename PassConceptT::unique_ptr>;
579 AnalysisPassMapT AnalysisPasses;
585 AnalysisResultListMapT AnalysisResultLists;
589 AnalysisResultMapT AnalysisResults;
618template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
621 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
625 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
631 Arg.InnerAM =
nullptr;
645 InnerAM =
RHS.InnerAM;
649 RHS.InnerAM =
nullptr;
671 AnalysisManagerT *InnerAM;
675 : InnerAM(&InnerAM) {}
684 return Result(*InnerAM);
693 AnalysisManagerT *InnerAM;
702template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
713LLVM_ABI bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
715 ModuleAnalysisManager::Invalidator &Inv);
749template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
752 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
757 explicit Result(
const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {}
761 template <
typename PassT,
typename IRUnitTParam>
763 typename PassT::Result *Res =
766 OuterAM->template verifyNotInvalidated<PassT>(
IR, Res);
771 template <
typename PassT,
typename IRUnitTParam>
773 typename PassT::Result *Res =
775 return Res !=
nullptr;
785 for (
auto &KeyValuePair : OuterAnalysisInvalidationMap) {
787 auto &InnerIDs = KeyValuePair.second;
791 if (InnerIDs.empty())
795 for (
auto *OuterID : DeadKeys)
796 OuterAnalysisInvalidationMap.
erase(OuterID);
804 template <
typename OuterAnalysisT,
typename Inval
idatedAnalysisT>
807 AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
809 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
815 InvalidatedIDList.push_back(InvalidatedID);
822 return OuterAnalysisInvalidationMap;
826 const AnalysisManagerT *OuterAM;
831 OuterAnalysisInvalidationMap;
835 : OuterAM(&OuterAM) {}
842 return Result(*OuterAM);
851 const AnalysisManagerT *OuterAM;
854template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
893 bool EagerlyInvalidate)
894 : Pass(
std::
move(Pass)), EagerlyInvalidate(EagerlyInvalidate) {}
903 PassConceptT::unique_ptr
Pass;
904 bool EagerlyInvalidate;
909template <
typename FunctionPassT>
910ModuleToFunctionPassAdaptor
912 bool EagerlyInvalidate =
false) {
928template <
typename AnalysisT,
typename IRUnitT,
929 typename AnalysisManagerT = AnalysisManager<IRUnitT>,
930 typename... ExtraArgTs>
933 AnalysisT, IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
941 ExtraArgTs &&... Args) {
942 (void)AM.template getResult<AnalysisT>(Arg,
943 std::forward<ExtraArgTs>(Args)...);
949 auto ClassName = AnalysisT::name();
950 auto PassName = MapClassName2PassName(ClassName);
951 OS <<
"require<" <<
PassName <<
'>';
957template <
typename AnalysisT>
966 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
969 PA.abandon<AnalysisT>();
974 auto ClassName = AnalysisT::name();
975 auto PassName = MapClassName2PassName(ClassName);
976 OS <<
"invalidate<" <<
PassName <<
'>';
987 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_TEMPLATE_ABI
#define LLVM_ATTRIBUTE_MINSIZE
#define LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
This file defines the DenseMap class.
This file defines IRUnitRef, a type-erased reference to the IR unit a pass or analysis is running on,...
Legalize the Machine IR a function s Machine IR
Machine Check Debug Module
This header provides internal APIs and implementation details used by the pass management interfaces ...
This file defines the SmallVector class.
static const char PassName[]
API to communicate dependencies between analyses during invalidation.
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...
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
friend class AnalysisManager
A container for analyses that lazily runs them and caches their results.
bool isPassRegistered() const
Returns true if the specified analysis pass is registered.
AnalysisManager()
Construct an empty analysis manager.
void clear()
Clear all analysis results cached by this AnalysisManager.
AnalysisManager(AnalysisManager &&)
void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const
Verify that the given Result cannot be invalidated, assert otherwise.
AnalysisManager & operator=(AnalysisManager &&)
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
void clearAnalysis(IRUnitT &IR)
Directly clear a cached analysis for an IR unit.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
bool empty() const
Returns true if the analysis manager has an empty results cache.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
iterator find(const_arg_type_t< KeyT > Val)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
Result(AnalysisManagerT &InnerAM)
Result & operator=(Result &&RHS)
AnalysisManagerT & getManager()
Accessor for the analysis manager.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Result run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
A helper class to return the specified delimiter string after the first invocation of operator String...
Trivial adaptor that maps from a module to its functions.
detail::PassConcept< Function, FunctionAnalysisManager > PassConceptT
ModuleToFunctionPassAdaptor(PassConceptT::unique_ptr Pass, bool EagerlyInvalidate)
A Module instance is used to store all the information related to an LLVM module.
Result(const AnalysisManagerT &OuterAM)
PassT::Result * getCachedResult(IRUnitTParam &IR) const
Get a cached analysis.
bool invalidate(IRUnitT &IRUnit, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
When invalidation occurs, remove any registered invalidation events.
bool cachedResultExists(IRUnitTParam &IR) const
Method provided for unit testing, not intended for general use.
const SmallDenseMap< AnalysisKey *, TinyPtrVector< AnalysisKey * >, 2 > & getOuterInvalidations() const
Access the map from outer analyses to deferred invalidation requiring analyses.
void registerOuterAnalysisInvalidation()
Register a deferred invalidation event for when the outer analysis manager processes its invalidation...
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Result run(IRUnitT &, AnalysisManager< IRUnitT, ExtraArgTs... > &, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
This class provides access to building LLVM's passes.
Manages a sequence of passes over a particular unit of IR.
PassManager(PassManager &&Arg)
PassManager & operator=(PassManager &&RHS)
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PassManager()=default
Construct a pass manager.
detail::PassConcept< LazyCallGraph::SCC, CGSCCAnalysisManager, ExtraArgTs... > PassConceptT
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t< std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
When adding a pass manager pass that has the same type as this pass manager, simply move the passes o...
std::vector< typename PassConceptT::unique_ptr > Passes
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
bool isEmpty() const
Returns if the pass manager contains any passes.
Pass interface - Implemented by all 'passes'.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Abstract concept of an analysis pass.
Wrapper to model the analysis pass concept.
Template for the abstract base class used to dispatch over pass objects.
std::unique_ptr< PassConcept, Deleter > unique_ptr
A template wrapper used to implement PassConcept.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Pass manager infrastructure for declaring and invalidating analyses.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
PassT::Result getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR, std::tuple< ArgTs... > Args, std::index_sequence< Ns... >)
Actual unpacker of extra arguments in getAnalysisResult, passes only those tuple arguments that are m...
LLVM_ABI bool shouldSkipOptimizationForOptBisect(IRUnitRef IR, StringRef PassName)
PassT::Result getAnalysisResult(AnalysisManager< IRUnitT, AnalysisArgTs... > &AM, IRUnitT &IR, std::tuple< MainArgTs... > Args)
Helper for partial unpacking of extra arguments in getAnalysisResult.
This is an optimization pass for GlobalISel generic memory operations.
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
LLVM_ABI void printIRUnitNameForStackTrace< Function >(raw_ostream &OS, const Function &IR)
void printIRUnitNameForStackTrace(raw_ostream &OS, const IRUnitT &IR)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI void printIRUnitNameForStackTrace< Module >(raw_ostream &OS, const Module &IR)
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
A CRTP mix-in that provides informational APIs needed for analysis passes.
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
A special type used by analysis passes to provide an address that identifies that particular analysis...
A utility pass that does nothing, but preserves no analyses.
PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...)
Run this pass over some unit of IR.
A no-op pass template which simply forces a specific analysis result to be invalidated.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...)
Run this pass over some unit of IR.
A CRTP mix-in for passes that can be skipped.
A utility pass template to force an analysis result to be available.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&... Args)
Run this pass over some unit of IR.
A CRTP mix-in for passes that should not be skipped.
bool shouldSkipOptimizationForOptBisect(IRUnitRef IR)
Abstract concept of an analysis result.
Wrapper to model the analysis result concept.
static StringRef name()
Gets the name of the pass we are mixed into.
A CRTP mix-in to automatically provide informational APIs needed for passes.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)