FindUsedTypes.cpp revision 221d688a5ef21a22c2368c9fff0e92d7966c95e5
1//===- FindUsedTypes.h - Find all Types used by a module --------------------=//
2//
3// This pass is used to seek out all of the types in use by the program.
4//
5//===----------------------------------------------------------------------===//
6
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"
14#include "llvm/Support/InstIterator.h"
15
16AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
17AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
18
19// IncorporateType - Incorporate one type and all of its subtypes into the
20// collection of used types.
21//
22void FindUsedTypes::IncorporateType(const Type *Ty) {
23  if (UsedTypes.count(Ty)) return;  // Already contain Ty.
24
25  // If ty doesn't already exist in the used types map, add it now.
26  //
27  UsedTypes.insert(Ty);
28
29  // Make sure to add any types this type references now.
30  //
31  for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
32       I != E; ++I)
33    IncorporateType(*I);
34}
35
36// IncorporateSymbolTable - Add all types referenced by the specified symtab
37// into the collection of used types.
38//
39void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
40  assert(0 && "Unimp");
41}
42
43// doPerMethodWork - This incorporates all types used by the specified method
44//
45bool FindUsedTypes::run(Module *m) {
46  UsedTypes.clear();  // reset if run multiple times...
47
48  if (IncludeSymbolTables && m->hasSymbolTable())
49    IncorporateSymbolTable(m->getSymbolTable()); // Add symtab first...
50
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());
54
55  for (Module::iterator MI = m->begin(), ME = m->end(); MI != ME; ++MI) {
56    const Method *M = *MI;
57    if (IncludeSymbolTables && M->hasSymbolTable())
58      IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
59
60    // Loop over all of the instructions in the method, adding their return type
61    // as well as the types of their operands.
62    //
63    for (const_inst_iterator II = inst_begin(M), IE = inst_end(M);
64         II != IE; ++II) {
65      const Instruction *I = *II;
66      const Type *Ty = I->getType();
67
68      IncorporateType(Ty);  // Incorporate the type of the instruction
69      for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
70           OI != OE; ++OI)
71        if ((*OI)->getType() != Ty)         // Avoid set lookup in common case
72          IncorporateType((*OI)->getType());// Insert inst operand types as well
73    }
74  }
75
76  return false;
77}
78
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.
82//
83void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
84  o << "Types in use by this module:\n";
85  if (M) {
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";
90  } else
91    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
92           E = UsedTypes.end(); I != E; ++I)
93      o << "  " << *I << "\n";
94}
95
96// getAnalysisUsageInfo - Of course, we provide ourself...
97//
98void FindUsedTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
99                                         Pass::AnalysisSet &Destroyed,
100                                         Pass::AnalysisSet &Provided) {
101  Provided.push_back(FindUsedTypes::ID);
102}
103