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/Instruction.h"
14 #include "llvm/Support/InstIterator.h"
16 AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
17 AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
19 // IncorporateType - Incorporate one type and all of its subtypes into the
20 // collection of used types.
22 void FindUsedTypes::IncorporateType(const Type *Ty) {
23 if (UsedTypes.count(Ty)) return; // Already contain Ty.
25 // If ty doesn't already exist in the used types map, add it now.
29 // Make sure to add any types this type references now.
31 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
36 // IncorporateSymbolTable - Add all types referenced by the specified symtab
37 // into the collection of used types.
39 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
43 // run - This incorporates all types used by the specified module
45 bool FindUsedTypes::run(Module *m) {
46 UsedTypes.clear(); // reset if run multiple times...
48 if (IncludeSymbolTables && m->hasSymbolTable())
49 IncorporateSymbolTable(m->getSymbolTable()); // Add symtab first...
51 // Loop over global variables, incorporating their types
52 for (Module::const_giterator I = m->gbegin(), E = m->gend(); I != E; ++I)
53 IncorporateType((*I)->getType());
55 for (Module::iterator MI = m->begin(), ME = m->end(); MI != ME; ++MI) {
56 const Function *M = *MI;
57 if (IncludeSymbolTables && M->hasSymbolTable())
58 IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
60 // Loop over all of the instructions in the function, adding their return
61 // type as well as the types of their operands.
63 for (const_inst_iterator II = inst_begin(M), IE = inst_end(M);
65 const Instruction *I = *II;
66 const Type *Ty = I->getType();
68 IncorporateType(Ty); // Incorporate the type of the instruction
69 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
71 if ((*OI)->getType() != Ty) // Avoid set lookup in common case
72 IncorporateType((*OI)->getType());// Insert inst operand types as well
79 // Print the types found in the module. If the optional Module parameter is
80 // passed in, then the types are printed symbolically if possible, using the
81 // symbol table from the module.
83 void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
84 o << "Types in use by this module:\n";
86 CachedWriter CW(M, o);
87 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
88 E = UsedTypes.end(); I != E; ++I)
89 CW << " " << *I << "\n";
91 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
92 E = UsedTypes.end(); I != E; ++I)
93 o << " " << *I << "\n";