1 //===- FindUsedTypes.cpp - 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/Function.h"
14 #include "llvm/Instruction.h"
15 #include "llvm/Support/InstIterator.h"
17 AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
18 AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
20 // IncorporateType - Incorporate one type and all of its subtypes into the
21 // collection of used types.
23 void FindUsedTypes::IncorporateType(const Type *Ty) {
24 if (UsedTypes.count(Ty)) return; // Already contain Ty.
26 // If ty doesn't already exist in the used types map, add it now.
30 // Make sure to add any types this type references now.
32 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
37 // IncorporateSymbolTable - Add all types referenced by the specified symtab
38 // into the collection of used types.
40 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
44 // run - This incorporates all types used by the specified module
46 bool FindUsedTypes::run(Module *m) {
47 UsedTypes.clear(); // reset if run multiple times...
49 if (IncludeSymbolTables && m->hasSymbolTable())
50 IncorporateSymbolTable(m->getSymbolTable()); // Add symtab first...
52 // Loop over global variables, incorporating their types
53 for (Module::const_giterator I = m->gbegin(), E = m->gend(); I != E; ++I)
54 IncorporateType((*I)->getType());
56 for (Module::iterator MI = m->begin(), ME = m->end(); MI != ME; ++MI) {
57 const Function *M = *MI;
58 if (IncludeSymbolTables && M->hasSymbolTable())
59 IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
61 // Loop over all of the instructions in the function, adding their return
62 // type as well as the types of their operands.
64 for (const_inst_iterator II = inst_begin(M), IE = inst_end(M);
66 const Instruction *I = *II;
67 const Type *Ty = I->getType();
69 IncorporateType(Ty); // Incorporate the type of the instruction
70 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
72 if ((*OI)->getType() != Ty) // Avoid set lookup in common case
73 IncorporateType((*OI)->getType());// Insert inst operand types as well
80 // Print the types found in the module. If the optional Module parameter is
81 // passed in, then the types are printed symbolically if possible, using the
82 // symbol table from the module.
84 void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
85 o << "Types in use by this module:\n";
87 CachedWriter CW(M, o);
88 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
89 E = UsedTypes.end(); I != E; ++I)
90 CW << " " << *I << "\n";
92 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
93 E = UsedTypes.end(); I != E; ++I)
94 o << " " << *I << "\n";
97 // getAnalysisUsageInfo - Of course, we provide ourself...
99 void FindUsedTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
100 Pass::AnalysisSet &Destroyed,
101 Pass::AnalysisSet &Provided) {
102 Provided.push_back(FindUsedTypes::ID);