1
//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines stuff that is used to define and "use" Analysis Passes.
11
// This file is automatically #included by Pass.h, so:
13
// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
15
// Instead, #include Pass.h
17
//===----------------------------------------------------------------------===//
19
#ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
20
#define LLVM_PASS_ANALYSIS_SUPPORT_H
22
#include "llvm/Pass.h"
23
#include "llvm/ADT/SmallVector.h"
24
#include "llvm/ADT/StringRef.h"
29
//===----------------------------------------------------------------------===//
30
// AnalysisUsage - Represent the analysis usage information of a pass. This
31
// tracks analyses that the pass REQUIRES (must be available when the pass
32
// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
33
// pass), and analyses that the pass PRESERVES (the pass does not invalidate the
34
// results of these analyses). This information is provided by a pass to the
35
// Pass infrastructure through the getAnalysisUsage virtual function.
39
typedef SmallVector<AnalysisID, 32> VectorType;
42
// Sets of analyses required and preserved by a pass
43
VectorType Required, RequiredTransitive, Preserved;
47
AnalysisUsage() : PreservesAll(false) {}
49
// addRequired - Add the specified ID to the required set of the usage info
52
AnalysisUsage &addRequiredID(AnalysisID ID) {
53
assert(ID && "Pass class not registered!");
54
Required.push_back(ID);
57
template<class PassClass>
58
AnalysisUsage &addRequired() {
59
return addRequiredID(Pass::getClassPassInfo<PassClass>());
62
AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
63
assert(ID && "Pass class not registered!");
64
Required.push_back(ID);
65
RequiredTransitive.push_back(ID);
68
template<class PassClass>
69
AnalysisUsage &addRequiredTransitive() {
70
AnalysisID ID = Pass::getClassPassInfo<PassClass>();
71
return addRequiredTransitiveID(ID);
74
// addPreserved - Add the specified ID to the set of analyses preserved by
77
AnalysisUsage &addPreservedID(AnalysisID ID) {
78
Preserved.push_back(ID);
82
// addPreserved - Add the specified Pass class to the set of analyses
83
// preserved by this pass.
85
template<class PassClass>
86
AnalysisUsage &addPreserved() {
87
assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
88
Preserved.push_back(Pass::getClassPassInfo<PassClass>());
92
// addPreserved - Add the Pass with the specified argument string to the set
93
// of analyses preserved by this pass. If no such Pass exists, do nothing.
94
// This can be useful when a pass is trivially preserved, but may not be
95
// linked in. Be careful about spelling!
97
AnalysisUsage &addPreserved(StringRef Arg) {
98
const PassInfo *PI = Pass::lookupPassInfo(Arg);
99
// If the pass exists, preserve it. Otherwise silently do nothing.
100
if (PI) Preserved.push_back(PI);
104
// setPreservesAll - Set by analyses that do not transform their input at all
105
void setPreservesAll() { PreservesAll = true; }
106
bool getPreservesAll() const { return PreservesAll; }
108
/// setPreservesCFG - This function should be called by the pass, iff they do
111
/// 1. Add or remove basic blocks from the function
112
/// 2. Modify terminator instructions in any way.
114
/// This function annotates the AnalysisUsage info object to say that analyses
115
/// that only depend on the CFG are preserved by this pass.
117
void setPreservesCFG();
119
const VectorType &getRequiredSet() const { return Required; }
120
const VectorType &getRequiredTransitiveSet() const {
121
return RequiredTransitive;
123
const VectorType &getPreservedSet() const { return Preserved; }
126
//===----------------------------------------------------------------------===//
127
// AnalysisResolver - Simple interface used by Pass objects to pull all
128
// analysis information out of pass manager that is responsible to manage
132
class AnalysisResolver {
134
AnalysisResolver(); // DO NOT IMPLEMENT
137
explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
139
inline PMDataManager &getPMDataManager() { return PM; }
141
// Find pass that is implementing PI.
142
Pass *findImplPass(const PassInfo *PI) {
143
Pass *ResultPass = 0;
144
for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
145
if (AnalysisImpls[i].first == PI) {
146
ResultPass = AnalysisImpls[i].second;
153
// Find pass that is implementing PI. Initialize pass for Function F.
154
Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
156
void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
157
std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
158
AnalysisImpls.push_back(pir);
161
/// clearAnalysisImpls - Clear cache that is used to connect a pass to the
162
/// the analysis (PassInfo).
163
void clearAnalysisImpls() {
164
AnalysisImpls.clear();
167
// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
168
Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
170
// AnalysisImpls - This keeps track of which passes implements the interfaces
171
// that are required by the current pass (to implement getAnalysis()).
172
std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
175
// PassManager that is used to resolve analysis info
179
/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
180
/// get analysis information that might be around, for example to update it.
181
/// This is different than getAnalysis in that it can fail (if the analysis
182
/// results haven't been computed), so should only be used if you can handle
183
/// the case when the analysis is not available. This method is often used by
184
/// transformation APIs to update analysis results for a pass automatically as
185
/// the transform is performed.
187
template<typename AnalysisType>
188
AnalysisType *Pass::getAnalysisIfAvailable() const {
189
assert(Resolver && "Pass not resident in a PassManager object!");
191
const PassInfo *PI = getClassPassInfo<AnalysisType>();
192
if (PI == 0) return 0;
194
Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
195
if (ResultPass == 0) return 0;
197
// Because the AnalysisType may not be a subclass of pass (for
198
// AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
199
// adjust the return pointer (because the class may multiply inherit, once
200
// from pass, once from AnalysisType).
201
return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
204
/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
205
/// to the analysis information that they claim to use by overriding the
206
/// getAnalysisUsage function.
208
template<typename AnalysisType>
209
AnalysisType &Pass::getAnalysis() const {
210
assert(Resolver && "Pass has not been inserted into a PassManager object!");
211
return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
214
template<typename AnalysisType>
215
AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
216
assert(PI && "getAnalysis for unregistered pass!");
217
assert(Resolver&&"Pass has not been inserted into a PassManager object!");
218
// PI *must* appear in AnalysisImpls. Because the number of passes used
219
// should be a small number, we just do a linear search over a (dense)
221
Pass *ResultPass = Resolver->findImplPass(PI);
222
assert (ResultPass &&
223
"getAnalysis*() called on an analysis that was not "
224
"'required' by pass!");
226
// Because the AnalysisType may not be a subclass of pass (for
227
// AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
228
// adjust the return pointer (because the class may multiply inherit, once
229
// from pass, once from AnalysisType).
230
return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
233
/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
234
/// to the analysis information that they claim to use by overriding the
235
/// getAnalysisUsage function.
237
template<typename AnalysisType>
238
AnalysisType &Pass::getAnalysis(Function &F) {
239
assert(Resolver &&"Pass has not been inserted into a PassManager object!");
241
return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
244
template<typename AnalysisType>
245
AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
246
assert(PI && "getAnalysis for unregistered pass!");
247
assert(Resolver && "Pass has not been inserted into a PassManager object!");
248
// PI *must* appear in AnalysisImpls. Because the number of passes used
249
// should be a small number, we just do a linear search over a (dense)
251
Pass *ResultPass = Resolver->findImplPass(this, PI, F);
252
assert(ResultPass && "Unable to find requested analysis info");
254
// Because the AnalysisType may not be a subclass of pass (for
255
// AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
256
// adjust the return pointer (because the class may multiply inherit, once
257
// from pass, once from AnalysisType).
258
return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
261
} // End llvm namespace