1 //===- FindUsedTypes.h - Find all Types used by a module --------------------=//
3 // This pass is used to seek out all of the types in use by the program.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/FindUsedTypes.h"
8 #include "llvm/Assembly/CachedWriter.h"
9 #include "llvm/SymbolTable.h"
10 #include "llvm/GlobalVariable.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Module.h"
13 #include "llvm/Method.h"
15 AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
16 AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
18 // IncorporateType - Incorporate one type and all of its subtypes into the
19 // collection of used types.
21 void FindUsedTypes::IncorporateType(const Type *Ty) {
22 if (UsedTypes.count(Ty)) return; // Already contain Ty.
24 // If ty doesn't already exist in the used types map, add it now.
28 // Make sure to add any types this type references now.
30 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
35 // IncorporateSymbolTable - Add all types referenced by the specified symtab
36 // into the collection of used types.
38 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
42 // doPerMethodWork - This incorporates all types used by the specified method
44 bool FindUsedTypes::run(Module *m) {
45 UsedTypes.clear(); // reset if run multiple times...
47 if (IncludeSymbolTables && m->hasSymbolTable())
48 IncorporateSymbolTable(m->getSymbolTable()); // Add symtab first...
50 // Loop over global variables, incorporating their types
51 for (Module::const_giterator I = m->gbegin(), E = m->gend(); I != E; ++I)
52 IncorporateType((*I)->getType());
54 for (Module::iterator MI = m->begin(), ME = m->end(); MI != ME; ++MI) {
55 const Method *M = *MI;
56 if (IncludeSymbolTables && M->hasSymbolTable())
57 IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
59 // Loop over all of the instructions in the method, adding their return type
60 // as well as the types of their operands.
62 for (Method::const_inst_iterator II = M->inst_begin(), IE = M->inst_end();
64 const Instruction *I = *II;
65 const Type *Ty = I->getType();
67 IncorporateType(Ty); // Incorporate the type of the instruction
68 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
70 if ((*OI)->getType() != Ty) // Avoid set lookup in common case
71 IncorporateType((*OI)->getType());// Insert inst operand types as well
78 // Print the types found in the module. If the optional Module parameter is
79 // passed in, then the types are printed symbolically if possible, using the
80 // symbol table from the module.
82 void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
83 o << "Types in use by this module:\n";
85 CachedWriter CW(M, o);
86 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
87 E = UsedTypes.end(); I != E; ++I)
88 CW << " " << *I << "\n";
90 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
91 E = UsedTypes.end(); I != E; ++I)
92 o << " " << *I << "\n";
95 // getAnalysisUsageInfo - Of course, we provide ourself...
97 void FindUsedTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
98 Pass::AnalysisSet &Destroyed,
99 Pass::AnalysisSet &Provided) {
100 Provided.push_back(FindUsedTypes::ID);