FindUsedTypes.cpp revision d31d81c7aef66d4e3c5758e07a1a430963253d5a
1//===- FindUsedTypes.cpp - 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/DerivedTypes.h"
11#include "llvm/Module.h"
12#include "llvm/Support/InstIterator.h"
13
14AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
15AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
16
17// IncorporateType - Incorporate one type and all of its subtypes into the
18// collection of used types.
19//
20void FindUsedTypes::IncorporateType(const Type *Ty) {
21  if (UsedTypes.count(Ty)) return;  // Already contain Ty.
22
23  // If ty doesn't already exist in the used types map, add it now.
24  //
25  UsedTypes.insert(Ty);
26
27  // Make sure to add any types this type references now.
28  //
29  for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
30       I != E; ++I)
31    IncorporateType(*I);
32}
33
34// IncorporateSymbolTable - Add all types referenced by the specified symtab
35// into the collection of used types.
36//
37void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
38  assert(0 && "Unimp");
39}
40
41// run - This incorporates all types used by the specified module
42//
43bool FindUsedTypes::run(Module &m) {
44  UsedTypes.clear();  // reset if run multiple times...
45
46  if (IncludeSymbolTables && m.hasSymbolTable())
47    IncorporateSymbolTable(m.getSymbolTable()); // Add symtab first...
48
49  // Loop over global variables, incorporating their types
50  for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I)
51    IncorporateType(I->getType());
52
53  for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
54    const Function &F = *MI;
55    if (IncludeSymbolTables && F.hasSymbolTable())
56      IncorporateSymbolTable(F.getSymbolTable()); // Add symtab first...
57
58    // Loop over all of the instructions in the function, adding their return
59    // type as well as the types of their operands.
60    //
61    for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
62         II != IE; ++II) {
63      const Instruction *I = *II;
64      const Type *Ty = I->getType();
65
66      IncorporateType(Ty);  // Incorporate the type of the instruction
67      for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
68           OI != OE; ++OI)
69        if ((*OI)->getType() != Ty)         // Avoid set lookup in common case
70          IncorporateType((*OI)->getType());// Insert inst operand types as well
71    }
72  }
73
74  return false;
75}
76
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.
80//
81void FindUsedTypes::printTypes(std::ostream &o, const Module *M) const {
82  o << "Types in use by this module:\n";
83  if (M) {
84    CachedWriter CW(M, o);
85    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
86           E = UsedTypes.end(); I != E; ++I)
87      CW << "  " << *I << "\n";
88  } else
89    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
90           E = UsedTypes.end(); I != E; ++I)
91      o << "  " << *I << "\n";
92}
93