aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/IPA/FindUsedTypes.cpp
blob: 1d98983115cf4813b38acc7915a3749702afea24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===- FindUsedTypes.h - Find all Types used by a module --------------------=//
//
// This pass is used to seek out all of the types in use by the program.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Assembly/CachedWriter.h"
#include "llvm/SymbolTable.h"
#include "llvm/GlobalVariable.h"
#include "llvm/DerivedTypes.h"

// IncorporateType - Incorporate one type and all of its subtypes into the
// collection of used types.
//
void FindUsedTypes::IncorporateType(const Type *Ty) {
  if (UsedTypes.count(Ty)) return;  // Already contain Ty.
                             
  // If ty doesn't already exist in the used types map, add it now.
  //
  UsedTypes.insert(Ty);
  
  // Make sure to add any types this type references now.
  //
  for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
       I != E; ++I)
    IncorporateType(*I);
}

// IncorporateSymbolTable - Add all types referenced by the specified symtab
// into the collection of used types.
//
void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
  assert(0 && "Unimp");
}


// doPassInitialization - This loops over global constants defined in the
// module, converting them to their new type.
//
bool FindUsedTypes::doPassInitialization(Module *m) {
  const Module *M = m;
  if (IncludeSymbolTables && M->hasSymbolTable())
    IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...

  // Loop over global variables, incorporating their types
  for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
    IncorporateType((*I)->getType());
  return false;
}

// doPerMethodWork - This incorporates all types used by the specified method
//
bool FindUsedTypes::doPerMethodWork(Method *m) {
  const Method *M = m;
  if (IncludeSymbolTables && M->hasSymbolTable())
  IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
  
  // Loop over all of the instructions in the method, adding their return type
  // as well as the types of their operands.
  //
  for (Method::const_inst_iterator II = M->inst_begin(), IE = M->inst_end();
       II != IE; ++II) {
    const Instruction *I = *II;
    const Type *Ty = I->getType();
    
    IncorporateType(Ty);  // Incorporate the type of the instruction
    for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
         OI != OE; ++OI)
      if ((*OI)->getType() != Ty)          // Avoid set lookup in common case
        IncorporateType((*OI)->getType()); // Insert inst operand types as well
  }
  
  return false;
}

// Print the types found in the module.  If the optional Module parameter is
// passed in, then the types are printed symbolically if possible, using the
// symbol table from the module.
//
void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
  o << "Types in use by this module:\n";
  if (M) {
    CachedWriter CW(M, o);
    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
           E = UsedTypes.end(); I != E; ++I)
      CW << "  " << *I << "\n";
  } else
    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
           E = UsedTypes.end(); I != E; ++I)
      o << "  " << *I << "\n";
}