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