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