LLVM  14.0.0git
PassManager.cpp
Go to the documentation of this file.
1 //===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/PassManager.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/IR/LLVMContext.h"
13 
14 using namespace llvm;
15 
16 // Explicit template instantiations and specialization defininitions for core
17 // template typedefs.
18 namespace llvm {
19 template class AllAnalysesOn<Module>;
20 template class AllAnalysesOn<Function>;
21 template class PassManager<Module>;
22 template class PassManager<Function>;
23 template class AnalysisManager<Module>;
24 template class AnalysisManager<Function>;
27 
28 template <>
30  Module &M, const PreservedAnalyses &PA,
32  // If literally everything is preserved, we're done.
33  if (PA.areAllPreserved())
34  return false; // This is still a valid proxy.
35 
36  // If this proxy isn't marked as preserved, then even if the result remains
37  // valid, the key itself may no longer be valid, so we clear everything.
38  //
39  // Note that in order to preserve this proxy, a module pass must ensure that
40  // the FAM has been completely updated to handle the deletion of functions.
41  // Specifically, any FAM-cached results for those functions need to have been
42  // forcibly cleared. When preserved, this proxy will only invalidate results
43  // cached on functions *still in the module* at the end of the module pass.
45  if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
46  InnerAM->clear();
47  return true;
48  }
49 
50  // Directly check if the relevant set is preserved.
51  bool AreFunctionAnalysesPreserved =
53 
54  // Now walk all the functions to see if any inner analysis invalidation is
55  // necessary.
56  for (Function &F : M) {
57  Optional<PreservedAnalyses> FunctionPA;
58 
59  // Check to see whether the preserved set needs to be pruned based on
60  // module-level analysis invalidation that triggers deferred invalidation
61  // registered with the outer analysis manager proxy for this function.
62  if (auto *OuterProxy =
63  InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
64  for (const auto &OuterInvalidationPair :
65  OuterProxy->getOuterInvalidations()) {
66  AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
67  const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
68  if (Inv.invalidate(OuterAnalysisID, M, PA)) {
69  if (!FunctionPA)
70  FunctionPA = PA;
71  for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
72  FunctionPA->abandon(InnerAnalysisID);
73  }
74  }
75 
76  // Check if we needed a custom PA set, and if so we'll need to run the
77  // inner invalidation.
78  if (FunctionPA) {
79  InnerAM->invalidate(F, *FunctionPA);
80  continue;
81  }
82 
83  // Otherwise we only need to do invalidation if the original PA set didn't
84  // preserve all function analyses.
85  if (!AreFunctionAnalysesPreserved)
86  InnerAM->invalidate(F, PA);
87  }
88 
89  // Return false to indicate that this result is still a valid proxy.
90  return false;
91 }
92 } // namespace llvm
93 
95  raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
96  OS << "function(";
97  Pass->printPipeline(OS, MapClassName2PassName);
98  OS << ")";
99 }
100 
102  ModuleAnalysisManager &AM) {
105 
106  // Request PassInstrumentation from analysis manager, will use it to run
107  // instrumenting callbacks for the passes later.
109 
111  for (Function &F : M) {
112  if (F.isDeclaration())
113  continue;
114 
115  // Check the PassInstrumentation's BeforePass callbacks before running the
116  // pass, skip its execution completely if asked to (callback returns
117  // false).
118  if (!PI.runBeforePass<Function>(*Pass, F))
119  continue;
120 
121  PreservedAnalyses PassPA;
122  {
123  TimeTraceScope TimeScope(Pass->name(), F.getName());
124  PassPA = Pass->run(F, FAM);
125  }
126 
127  PI.runAfterPass(*Pass, F, PassPA);
128 
129  // We know that the function pass couldn't have invalidated any other
130  // function's analyses (that's the contract of a function pass), so
131  // directly handle the function analysis manager's invalidation here.
132  FAM.invalidate(F, PassPA);
133 
134  // Then intersect the preserved set so that invalidation of module
135  // analyses will eventually occur when the module pass completes.
136  PA.intersect(std::move(PassPA));
137  }
138 
139  // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
140  // the function passes we ran didn't add or remove any functions.
141  //
142  // We also preserve all analyses on Functions, because we did all the
143  // invalidation we needed to do above.
144  PA.preserveSet<AllAnalysesOn<Function>>();
145  PA.preserve<FunctionAnalysisManagerModuleProxy>();
146  return PA;
147 }
148 
149 AnalysisSetKey CFGAnalyses::SetKey;
150 
151 AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
llvm::ModuleToFunctionPassAdaptor::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
Definition: PassManager.cpp:101
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::OuterAnalysisManagerProxy
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1061
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::TimeTraceScope
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Definition: TimeProfiler.h:65
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:779
llvm::Function
Definition: Function.h:61
llvm::AnalysisManager::Invalidator::invalidate
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:684
llvm::AnalysisManager::invalidate
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
Definition: PassManagerImpl.h:89
llvm::AllAnalysesOn
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: PassManager.h:93
llvm::PreservedAnalyses::abandon
void abandon()
Mark an analysis as abandoned.
Definition: PassManager.h:209
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::Optional
Definition: APInt.h:33
STLExtras.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
PassManagerImpl.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:666
llvm::AnalysisSetKey
A special type used to provide an address that identifies a set of related analyses.
Definition: PassManager.h:82
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::PassInstrumentation::runAfterPass
void runAfterPass(const PassT &Pass, const IRUnitT &IR, const PreservedAnalyses &PA) const
AfterPass instrumentation point - takes Pass instance that has just been executed and constant refere...
Definition: PassInstrumentation.h:242
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::InnerAnalysisManagerProxy::Result::invalidate
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
llvm::PassInstrumentation
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
Definition: PassInstrumentation.h:180
llvm::PreservedAnalyses::allAnalysesInSetPreserved
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: PassManager.h:338
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::ModuleToFunctionPassAdaptor::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: PassManager.cpp:94
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::PreservedAnalyses::areAllPreserved
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition: PassManager.h:330
llvm::PassManager< Module >
llvm::PassInstrumentation::runBeforePass
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
Definition: PassInstrumentation.h:217
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
PassManager.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::PreservedAnalyses::getChecker
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:313
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::PassInstrumentationAnalysis
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
Definition: PassManager.h:599
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:936
LLVMContext.h