1193323Sed//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines stuff that is used to define and "use" Analysis Passes. 11193323Sed// This file is automatically #included by Pass.h, so: 12193323Sed// 13193323Sed// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 14193323Sed// 15193323Sed// Instead, #include Pass.h 16193323Sed// 17193323Sed//===----------------------------------------------------------------------===// 18193323Sed 19249423Sdim#ifndef LLVM_PASSANALYSISSUPPORT_H 20249423Sdim#define LLVM_PASSANALYSISSUPPORT_H 21193323Sed 22193323Sed#include "llvm/ADT/SmallVector.h" 23199481Srdivacky#include "llvm/ADT/StringRef.h" 24249423Sdim#include "llvm/Pass.h" 25202878Srdivacky#include <vector> 26193323Sed 27193323Sednamespace llvm { 28193323Sed 29193323Sed//===----------------------------------------------------------------------===// 30193323Sed// AnalysisUsage - Represent the analysis usage information of a pass. This 31193323Sed// tracks analyses that the pass REQUIRES (must be available when the pass 32193323Sed// runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the 33193323Sed// pass), and analyses that the pass PRESERVES (the pass does not invalidate the 34193323Sed// results of these analyses). This information is provided by a pass to the 35193323Sed// Pass infrastructure through the getAnalysisUsage virtual function. 36193323Sed// 37193323Sedclass AnalysisUsage { 38193323Sedpublic: 39193323Sed typedef SmallVector<AnalysisID, 32> VectorType; 40193323Sed 41193323Sedprivate: 42193323Sed // Sets of analyses required and preserved by a pass 43193323Sed VectorType Required, RequiredTransitive, Preserved; 44193323Sed bool PreservesAll; 45193323Sed 46193323Sedpublic: 47193323Sed AnalysisUsage() : PreservesAll(false) {} 48193323Sed 49193323Sed // addRequired - Add the specified ID to the required set of the usage info 50193323Sed // for a pass. 51193323Sed // 52212904Sdim AnalysisUsage &addRequiredID(const void *ID); 53212904Sdim AnalysisUsage &addRequiredID(char &ID); 54193323Sed template<class PassClass> 55193323Sed AnalysisUsage &addRequired() { 56212904Sdim return addRequiredID(PassClass::ID); 57193323Sed } 58193323Sed 59212904Sdim AnalysisUsage &addRequiredTransitiveID(char &ID); 60193323Sed template<class PassClass> 61193323Sed AnalysisUsage &addRequiredTransitive() { 62212904Sdim return addRequiredTransitiveID(PassClass::ID); 63193323Sed } 64193323Sed 65193323Sed // addPreserved - Add the specified ID to the set of analyses preserved by 66193323Sed // this pass 67193323Sed // 68212904Sdim AnalysisUsage &addPreservedID(const void *ID) { 69193323Sed Preserved.push_back(ID); 70193323Sed return *this; 71193323Sed } 72212904Sdim AnalysisUsage &addPreservedID(char &ID) { 73212904Sdim Preserved.push_back(&ID); 74212904Sdim return *this; 75212904Sdim } 76193323Sed 77198090Srdivacky // addPreserved - Add the specified Pass class to the set of analyses 78198090Srdivacky // preserved by this pass. 79198090Srdivacky // 80193323Sed template<class PassClass> 81193323Sed AnalysisUsage &addPreserved() { 82212904Sdim Preserved.push_back(&PassClass::ID); 83193323Sed return *this; 84193323Sed } 85193323Sed 86198090Srdivacky // addPreserved - Add the Pass with the specified argument string to the set 87198090Srdivacky // of analyses preserved by this pass. If no such Pass exists, do nothing. 88198090Srdivacky // This can be useful when a pass is trivially preserved, but may not be 89198090Srdivacky // linked in. Be careful about spelling! 90198090Srdivacky // 91212904Sdim AnalysisUsage &addPreserved(StringRef Arg); 92198090Srdivacky 93193323Sed // setPreservesAll - Set by analyses that do not transform their input at all 94193323Sed void setPreservesAll() { PreservesAll = true; } 95193323Sed bool getPreservesAll() const { return PreservesAll; } 96193323Sed 97193323Sed /// setPreservesCFG - This function should be called by the pass, iff they do 98193323Sed /// not: 99193323Sed /// 100193323Sed /// 1. Add or remove basic blocks from the function 101193323Sed /// 2. Modify terminator instructions in any way. 102193323Sed /// 103193323Sed /// This function annotates the AnalysisUsage info object to say that analyses 104193323Sed /// that only depend on the CFG are preserved by this pass. 105193323Sed /// 106193323Sed void setPreservesCFG(); 107193323Sed 108193323Sed const VectorType &getRequiredSet() const { return Required; } 109193323Sed const VectorType &getRequiredTransitiveSet() const { 110193323Sed return RequiredTransitive; 111193323Sed } 112193323Sed const VectorType &getPreservedSet() const { return Preserved; } 113193323Sed}; 114193323Sed 115193323Sed//===----------------------------------------------------------------------===// 116193323Sed// AnalysisResolver - Simple interface used by Pass objects to pull all 117193323Sed// analysis information out of pass manager that is responsible to manage 118193323Sed// the pass. 119193323Sed// 120193323Sedclass PMDataManager; 121193323Sedclass AnalysisResolver { 122193323Sedprivate: 123243830Sdim AnalysisResolver() LLVM_DELETED_FUNCTION; 124193323Sed 125193323Sedpublic: 126193323Sed explicit AnalysisResolver(PMDataManager &P) : PM(P) { } 127193323Sed 128193323Sed inline PMDataManager &getPMDataManager() { return PM; } 129193323Sed 130193323Sed // Find pass that is implementing PI. 131212904Sdim Pass *findImplPass(AnalysisID PI) { 132193323Sed Pass *ResultPass = 0; 133193323Sed for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) { 134193323Sed if (AnalysisImpls[i].first == PI) { 135193323Sed ResultPass = AnalysisImpls[i].second; 136193323Sed break; 137193323Sed } 138193323Sed } 139193323Sed return ResultPass; 140193323Sed } 141193323Sed 142193323Sed // Find pass that is implementing PI. Initialize pass for Function F. 143212904Sdim Pass *findImplPass(Pass *P, AnalysisID PI, Function &F); 144193323Sed 145212904Sdim void addAnalysisImplsPair(AnalysisID PI, Pass *P) { 146221345Sdim if (findImplPass(PI) == P) 147221345Sdim return; 148212904Sdim std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P); 149193323Sed AnalysisImpls.push_back(pir); 150193323Sed } 151193323Sed 152193323Sed /// clearAnalysisImpls - Clear cache that is used to connect a pass to the 153193323Sed /// the analysis (PassInfo). 154193323Sed void clearAnalysisImpls() { 155193323Sed AnalysisImpls.clear(); 156193323Sed } 157193323Sed 158193323Sed // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist 159193323Sed Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const; 160193323Sed 161212904Sdimprivate: 162193323Sed // AnalysisImpls - This keeps track of which passes implements the interfaces 163193323Sed // that are required by the current pass (to implement getAnalysis()). 164212904Sdim std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls; 165193323Sed 166193323Sed // PassManager that is used to resolve analysis info 167193323Sed PMDataManager &PM; 168193323Sed}; 169193323Sed 170193323Sed/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to 171193323Sed/// get analysis information that might be around, for example to update it. 172193323Sed/// This is different than getAnalysis in that it can fail (if the analysis 173193323Sed/// results haven't been computed), so should only be used if you can handle 174193323Sed/// the case when the analysis is not available. This method is often used by 175193323Sed/// transformation APIs to update analysis results for a pass automatically as 176193323Sed/// the transform is performed. 177193323Sed/// 178193323Sedtemplate<typename AnalysisType> 179193323SedAnalysisType *Pass::getAnalysisIfAvailable() const { 180193323Sed assert(Resolver && "Pass not resident in a PassManager object!"); 181193323Sed 182212904Sdim const void *PI = &AnalysisType::ID; 183202878Srdivacky 184202878Srdivacky Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true); 185202878Srdivacky if (ResultPass == 0) return 0; 186202878Srdivacky 187202878Srdivacky // Because the AnalysisType may not be a subclass of pass (for 188202878Srdivacky // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially 189202878Srdivacky // adjust the return pointer (because the class may multiply inherit, once 190202878Srdivacky // from pass, once from AnalysisType). 191202878Srdivacky return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); 192193323Sed} 193193323Sed 194193323Sed/// getAnalysis<AnalysisType>() - This function is used by subclasses to get 195193323Sed/// to the analysis information that they claim to use by overriding the 196193323Sed/// getAnalysisUsage function. 197193323Sed/// 198193323Sedtemplate<typename AnalysisType> 199193323SedAnalysisType &Pass::getAnalysis() const { 200202878Srdivacky assert(Resolver && "Pass has not been inserted into a PassManager object!"); 201212904Sdim return getAnalysisID<AnalysisType>(&AnalysisType::ID); 202193323Sed} 203193323Sed 204193323Sedtemplate<typename AnalysisType> 205212904SdimAnalysisType &Pass::getAnalysisID(AnalysisID PI) const { 206193323Sed assert(PI && "getAnalysis for unregistered pass!"); 207193323Sed assert(Resolver&&"Pass has not been inserted into a PassManager object!"); 208193323Sed // PI *must* appear in AnalysisImpls. Because the number of passes used 209193323Sed // should be a small number, we just do a linear search over a (dense) 210193323Sed // vector. 211193323Sed Pass *ResultPass = Resolver->findImplPass(PI); 212193323Sed assert (ResultPass && 213193323Sed "getAnalysis*() called on an analysis that was not " 214193323Sed "'required' by pass!"); 215193323Sed 216193323Sed // Because the AnalysisType may not be a subclass of pass (for 217202878Srdivacky // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially 218202878Srdivacky // adjust the return pointer (because the class may multiply inherit, once 219202878Srdivacky // from pass, once from AnalysisType). 220202878Srdivacky return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); 221193323Sed} 222193323Sed 223193323Sed/// getAnalysis<AnalysisType>() - This function is used by subclasses to get 224193323Sed/// to the analysis information that they claim to use by overriding the 225193323Sed/// getAnalysisUsage function. 226193323Sed/// 227193323Sedtemplate<typename AnalysisType> 228193323SedAnalysisType &Pass::getAnalysis(Function &F) { 229193323Sed assert(Resolver &&"Pass has not been inserted into a PassManager object!"); 230193323Sed 231212904Sdim return getAnalysisID<AnalysisType>(&AnalysisType::ID, F); 232193323Sed} 233193323Sed 234193323Sedtemplate<typename AnalysisType> 235212904SdimAnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) { 236193323Sed assert(PI && "getAnalysis for unregistered pass!"); 237193323Sed assert(Resolver && "Pass has not been inserted into a PassManager object!"); 238193323Sed // PI *must* appear in AnalysisImpls. Because the number of passes used 239193323Sed // should be a small number, we just do a linear search over a (dense) 240193323Sed // vector. 241193323Sed Pass *ResultPass = Resolver->findImplPass(this, PI, F); 242202878Srdivacky assert(ResultPass && "Unable to find requested analysis info"); 243193323Sed 244193323Sed // Because the AnalysisType may not be a subclass of pass (for 245202878Srdivacky // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially 246202878Srdivacky // adjust the return pointer (because the class may multiply inherit, once 247202878Srdivacky // from pass, once from AnalysisType). 248202878Srdivacky return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); 249193323Sed} 250193323Sed 251193323Sed} // End llvm namespace 252193323Sed 253193323Sed#endif 254