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"
13 // IncorporateType - Incorporate one type and all of its subtypes into the
14 // collection of used types.
16 void FindUsedTypes::IncorporateType(const Type *Ty) {
17 if (UsedTypes.count(Ty)) return; // Already contain Ty.
19 // If ty doesn't already exist in the used types map, add it now.
23 // Make sure to add any types this type references now.
25 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
30 // IncorporateSymbolTable - Add all types referenced by the specified symtab
31 // into the collection of used types.
33 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
38 // doPassInitialization - This loops over global constants defined in the
39 // module, converting them to their new type.
41 bool FindUsedTypes::doPassInitialization(Module *m) {
43 if (IncludeSymbolTables && M->hasSymbolTable())
44 IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
46 // Loop over global variables, incorporating their types
47 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
48 IncorporateType((*I)->getType());
52 // doPerMethodWork - This incorporates all types used by the specified method
54 bool FindUsedTypes::doPerMethodWork(Method *m) {
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::inst_const_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::op_const_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
77 // Print the types found in the module. If the optional Module parameter is
78 // passed in, then the types are printed symbolically if possible, using the
79 // symbol table from the module.
81 void FindUsedTypes::printTypes(ostream &o, const Module *M = 0) const {
82 o << "Types in use by this module:\n";
84 CachedWriter CW(M, o);
85 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
86 E = UsedTypes.end(); I != E; ++I)
87 CW << " " << *I << endl;
89 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
90 E = UsedTypes.end(); I != E; ++I)
91 o << " " << *I << endl;