2003-09-30 18:37:50 +00:00
|
|
|
//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-23 17:59:55 +00:00
|
|
|
//
|
|
|
|
// This file defines stuff that is used to define and "use" Analysis Passes.
|
|
|
|
// This file is automatically #included by Pass.h, so:
|
|
|
|
//
|
|
|
|
// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
|
|
|
|
//
|
|
|
|
// Instead, #include Pass.h
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
|
|
|
|
#define LLVM_PASS_ANALYSIS_SUPPORT_H
|
|
|
|
|
2006-02-22 16:23:43 +00:00
|
|
|
#include <vector>
|
2008-08-08 05:33:04 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2006-02-22 16:23:43 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-07-23 17:59:55 +00:00
|
|
|
// No need to include Pass.h, we are being included by it!
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisUsage - Represent the analysis usage information of a pass. This
|
2005-04-21 20:19:05 +00:00
|
|
|
// tracks analyses that the pass REQUIRES (must be available when the pass
|
2004-03-12 06:13:15 +00:00
|
|
|
// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
|
|
|
|
// pass), and analyses that the pass PRESERVES (the pass does not invalidate the
|
2005-04-21 20:19:05 +00:00
|
|
|
// results of these analyses). This information is provided by a pass to the
|
2002-07-23 17:59:55 +00:00
|
|
|
// Pass infrastructure through the getAnalysisUsage virtual function.
|
|
|
|
//
|
|
|
|
class AnalysisUsage {
|
2008-08-08 05:33:04 +00:00
|
|
|
public:
|
|
|
|
typedef SmallVector<AnalysisID, 32> VectorType;
|
|
|
|
|
|
|
|
private:
|
2002-07-23 17:59:55 +00:00
|
|
|
// Sets of analyses required and preserved by a pass
|
2008-08-08 05:33:04 +00:00
|
|
|
VectorType Required, RequiredTransitive, Preserved;
|
2002-07-23 17:59:55 +00:00
|
|
|
bool PreservesAll;
|
2008-08-08 05:33:04 +00:00
|
|
|
|
2002-07-23 17:59:55 +00:00
|
|
|
public:
|
|
|
|
AnalysisUsage() : PreservesAll(false) {}
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2002-08-08 19:01:11 +00:00
|
|
|
// addRequired - Add the specified ID to the required set of the usage info
|
2002-07-23 17:59:55 +00:00
|
|
|
// for a pass.
|
|
|
|
//
|
2002-08-08 19:01:11 +00:00
|
|
|
AnalysisUsage &addRequiredID(AnalysisID ID) {
|
2007-05-14 14:21:46 +00:00
|
|
|
assert(ID && "Pass class not registered!");
|
2002-07-23 17:59:55 +00:00
|
|
|
Required.push_back(ID);
|
|
|
|
return *this;
|
|
|
|
}
|
2002-08-08 19:01:11 +00:00
|
|
|
template<class PassClass>
|
|
|
|
AnalysisUsage &addRequired() {
|
2007-05-14 14:21:46 +00:00
|
|
|
return addRequiredID(Pass::getClassPassInfo<PassClass>());
|
2002-08-08 19:01:11 +00:00
|
|
|
}
|
2002-07-23 17:59:55 +00:00
|
|
|
|
2007-05-14 14:21:46 +00:00
|
|
|
AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
|
2004-03-12 06:13:15 +00:00
|
|
|
assert(ID && "Pass class not registered!");
|
|
|
|
Required.push_back(ID);
|
|
|
|
RequiredTransitive.push_back(ID);
|
|
|
|
return *this;
|
|
|
|
}
|
2007-05-14 14:21:46 +00:00
|
|
|
template<class PassClass>
|
|
|
|
AnalysisUsage &addRequiredTransitive() {
|
|
|
|
AnalysisID ID = Pass::getClassPassInfo<PassClass>();
|
|
|
|
return addRequiredTransitiveID(ID);
|
|
|
|
}
|
2004-03-12 06:13:15 +00:00
|
|
|
|
2002-08-08 19:01:11 +00:00
|
|
|
// addPreserved - Add the specified ID to the set of analyses preserved by
|
2002-07-23 17:59:55 +00:00
|
|
|
// this pass
|
|
|
|
//
|
2002-08-08 19:01:11 +00:00
|
|
|
AnalysisUsage &addPreservedID(AnalysisID ID) {
|
2002-07-23 17:59:55 +00:00
|
|
|
Preserved.push_back(ID);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2002-08-08 19:01:11 +00:00
|
|
|
template<class PassClass>
|
|
|
|
AnalysisUsage &addPreserved() {
|
2002-08-21 22:14:28 +00:00
|
|
|
assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
|
2002-08-21 17:08:37 +00:00
|
|
|
Preserved.push_back(Pass::getClassPassInfo<PassClass>());
|
2002-08-08 19:01:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setPreservesAll - Set by analyses that do not transform their input at all
|
2002-07-23 17:59:55 +00:00
|
|
|
void setPreservesAll() { PreservesAll = true; }
|
2002-10-21 20:00:28 +00:00
|
|
|
bool getPreservesAll() const { return PreservesAll; }
|
2002-07-23 17:59:55 +00:00
|
|
|
|
2002-10-21 20:00:28 +00:00
|
|
|
/// setPreservesCFG - This function should be called by the pass, iff they do
|
|
|
|
/// not:
|
|
|
|
///
|
|
|
|
/// 1. Add or remove basic blocks from the function
|
|
|
|
/// 2. Modify terminator instructions in any way.
|
|
|
|
///
|
|
|
|
/// This function annotates the AnalysisUsage info object to say that analyses
|
|
|
|
/// that only depend on the CFG are preserved by this pass.
|
|
|
|
///
|
|
|
|
void setPreservesCFG();
|
2002-07-23 17:59:55 +00:00
|
|
|
|
2008-08-08 05:33:04 +00:00
|
|
|
const VectorType &getRequiredSet() const { return Required; }
|
|
|
|
const VectorType &getRequiredTransitiveSet() const {
|
2004-03-12 06:13:15 +00:00
|
|
|
return RequiredTransitive;
|
|
|
|
}
|
2008-08-08 05:33:04 +00:00
|
|
|
const VectorType &getPreservedSet() const { return Preserved; }
|
2002-07-23 17:59:55 +00:00
|
|
|
};
|
|
|
|
|
2006-12-08 23:28:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalysisResolver - Simple interface used by Pass objects to pull all
|
|
|
|
// analysis information out of pass manager that is responsible to manage
|
|
|
|
// the pass.
|
|
|
|
//
|
|
|
|
class PMDataManager;
|
2007-01-05 22:47:07 +00:00
|
|
|
class AnalysisResolver {
|
2006-12-08 23:28:54 +00:00
|
|
|
private:
|
2007-01-05 22:47:07 +00:00
|
|
|
AnalysisResolver(); // DO NOT IMPLEMENT
|
2006-12-08 23:28:54 +00:00
|
|
|
|
|
|
|
public:
|
2007-03-23 18:44:11 +00:00
|
|
|
explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
|
2006-12-08 23:28:54 +00:00
|
|
|
|
|
|
|
inline PMDataManager &getPMDataManager() { return PM; }
|
|
|
|
|
|
|
|
// Find pass that is implementing PI.
|
|
|
|
Pass *findImplPass(const PassInfo *PI) {
|
|
|
|
Pass *ResultPass = 0;
|
|
|
|
for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
|
|
|
|
if (AnalysisImpls[i].first == PI) {
|
|
|
|
ResultPass = AnalysisImpls[i].second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ResultPass;
|
|
|
|
}
|
2002-07-23 17:59:55 +00:00
|
|
|
|
2007-04-16 20:56:24 +00:00
|
|
|
// Find pass that is implementing PI. Initialize pass for Function F.
|
|
|
|
Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
|
|
|
|
|
2006-12-08 23:28:54 +00:00
|
|
|
void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
|
|
|
|
std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
|
|
|
|
AnalysisImpls.push_back(pir);
|
|
|
|
}
|
|
|
|
|
2009-04-01 22:34:41 +00:00
|
|
|
/// clearAnalysisImpls - Clear cache that is used to connect a pass to the
|
|
|
|
/// the analysis (PassInfo).
|
|
|
|
void clearAnalysisImpls() {
|
|
|
|
AnalysisImpls.clear();
|
|
|
|
}
|
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
|
|
|
|
Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
|
2006-12-08 23:28:54 +00:00
|
|
|
|
|
|
|
// AnalysisImpls - This keeps track of which passes implements the interfaces
|
|
|
|
// that are required by the current pass (to implement getAnalysis()).
|
|
|
|
std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// PassManager that is used to resolve analysis info
|
|
|
|
PMDataManager &PM;
|
|
|
|
};
|
2002-07-23 17:59:55 +00:00
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
|
|
|
|
/// get analysis information that might be around, for example to update it.
|
|
|
|
/// This is different than getAnalysis in that it can fail (if the analysis
|
|
|
|
/// results haven't been computed), so should only be used if you can handle
|
|
|
|
/// the case when the analysis is not available. This method is often used by
|
|
|
|
/// transformation APIs to update analysis results for a pass automatically as
|
|
|
|
/// the transform is performed.
|
2003-08-29 14:26:51 +00:00
|
|
|
///
|
|
|
|
template<typename AnalysisType>
|
2009-01-28 13:14:17 +00:00
|
|
|
AnalysisType *Pass::getAnalysisIfAvailable() const {
|
2007-01-05 22:47:07 +00:00
|
|
|
assert(Resolver && "Pass not resident in a PassManager object!");
|
2007-01-05 20:16:23 +00:00
|
|
|
|
2003-08-29 14:26:51 +00:00
|
|
|
const PassInfo *PI = getClassPassInfo<AnalysisType>();
|
|
|
|
if (PI == 0) return 0;
|
2006-12-13 02:36:01 +00:00
|
|
|
return dynamic_cast<AnalysisType*>
|
2009-01-28 13:14:17 +00:00
|
|
|
(Resolver->getAnalysisIfAvailable(PI, true));
|
2003-08-29 14:26:51 +00:00
|
|
|
}
|
|
|
|
|
2006-12-13 00:23:44 +00:00
|
|
|
/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
|
|
|
|
/// to the analysis information that they claim to use by overriding the
|
|
|
|
/// getAnalysisUsage function.
|
|
|
|
///
|
|
|
|
template<typename AnalysisType>
|
|
|
|
AnalysisType &Pass::getAnalysis() const {
|
2007-01-05 22:47:07 +00:00
|
|
|
assert(Resolver &&"Pass has not been inserted into a PassManager object!");
|
2007-01-05 20:16:23 +00:00
|
|
|
|
2006-12-13 21:56:10 +00:00
|
|
|
return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
|
2006-12-13 00:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename AnalysisType>
|
|
|
|
AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
|
|
|
|
assert(PI && "getAnalysis for unregistered pass!");
|
2007-01-05 22:47:07 +00:00
|
|
|
assert(Resolver&&"Pass has not been inserted into a PassManager object!");
|
2006-12-13 02:36:01 +00:00
|
|
|
// PI *must* appear in AnalysisImpls. Because the number of passes used
|
|
|
|
// should be a small number, we just do a linear search over a (dense)
|
|
|
|
// vector.
|
2007-01-05 22:47:07 +00:00
|
|
|
Pass *ResultPass = Resolver->findImplPass(PI);
|
2006-12-13 02:36:01 +00:00
|
|
|
assert (ResultPass &&
|
|
|
|
"getAnalysis*() called on an analysis that was not "
|
|
|
|
"'required' by pass!");
|
|
|
|
|
2006-12-13 00:23:44 +00:00
|
|
|
// Because the AnalysisType may not be a subclass of pass (for
|
|
|
|
// AnalysisGroups), we must use dynamic_cast here to potentially adjust the
|
|
|
|
// return pointer (because the class may multiply inherit, once from pass,
|
|
|
|
// once from AnalysisType).
|
|
|
|
//
|
|
|
|
AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
|
|
|
|
assert(Result && "Pass does not implement interface required!");
|
|
|
|
return *Result;
|
|
|
|
}
|
|
|
|
|
2007-04-16 20:56:24 +00:00
|
|
|
/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
|
|
|
|
/// to the analysis information that they claim to use by overriding the
|
|
|
|
/// getAnalysisUsage function.
|
|
|
|
///
|
|
|
|
template<typename AnalysisType>
|
|
|
|
AnalysisType &Pass::getAnalysis(Function &F) {
|
|
|
|
assert(Resolver &&"Pass has not been inserted into a PassManager object!");
|
|
|
|
|
|
|
|
return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename AnalysisType>
|
|
|
|
AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
|
|
|
|
assert(PI && "getAnalysis for unregistered pass!");
|
|
|
|
assert(Resolver&&"Pass has not been inserted into a PassManager object!");
|
|
|
|
// PI *must* appear in AnalysisImpls. Because the number of passes used
|
|
|
|
// should be a small number, we just do a linear search over a (dense)
|
|
|
|
// vector.
|
|
|
|
Pass *ResultPass = Resolver->findImplPass(this, PI, F);
|
|
|
|
assert (ResultPass &&
|
|
|
|
"getAnalysis*() called on an analysis that was not "
|
|
|
|
"'required' by pass!");
|
|
|
|
|
|
|
|
// Because the AnalysisType may not be a subclass of pass (for
|
|
|
|
// AnalysisGroups), we must use dynamic_cast here to potentially adjust the
|
|
|
|
// return pointer (because the class may multiply inherit, once from pass,
|
|
|
|
// once from AnalysisType).
|
|
|
|
//
|
|
|
|
AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
|
|
|
|
assert(Result && "Pass does not implement interface required!");
|
|
|
|
return *Result;
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-07-23 17:59:55 +00:00
|
|
|
#endif
|