* Add support for different "PassType's"
[oota-llvm.git] / lib / Analysis / IPA / FindUsedTypes.cpp
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
14 static RegisterAnalysis<FindUsedTypes>
15 X("printusedtypes", "Find Used Types");
16 AnalysisID FindUsedTypes::ID(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
36 // run - This incorporates all types used by the specified module
37 //
38 bool FindUsedTypes::run(Module &m) {
39   UsedTypes.clear();  // reset if run multiple times...
40
41   // Loop over global variables, incorporating their types
42   for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I)
43     IncorporateType(I->getType());
44
45   for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
46     const Function &F = *MI;
47   
48     // Loop over all of the instructions in the function, adding their return
49     // type as well as the types of their operands.
50     //
51     for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
52          II != IE; ++II) {
53       const Instruction *I = *II;
54       const Type *Ty = I->getType();
55     
56       IncorporateType(Ty);  // Incorporate the type of the instruction
57       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
58            OI != OE; ++OI)
59         if ((*OI)->getType() != Ty)         // Avoid set lookup in common case
60           IncorporateType((*OI)->getType());// Insert inst operand types as well
61     }
62   }
63  
64   return false;
65 }
66
67 // Print the types found in the module.  If the optional Module parameter is
68 // passed in, then the types are printed symbolically if possible, using the
69 // symbol table from the module.
70 //
71 void FindUsedTypes::printTypes(std::ostream &o, const Module *M) const {
72   o << "Types in use by this module:\n";
73   if (M) {
74     CachedWriter CW(M, o);
75     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
76            E = UsedTypes.end(); I != E; ++I)
77       CW << "  " << *I << "\n";
78   } else
79     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
80            E = UsedTypes.end(); I != E; ++I)
81       o << "  " << *I << "\n";
82 }