*** empty log message ***
[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 AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
15
16 // IncorporateType - Incorporate one type and all of its subtypes into the
17 // collection of used types.
18 //
19 void FindUsedTypes::IncorporateType(const Type *Ty) {
20   if (UsedTypes.count(Ty)) return;  // Already contain Ty.
21                              
22   // If ty doesn't already exist in the used types map, add it now.
23   //
24   UsedTypes.insert(Ty);
25   
26   // Make sure to add any types this type references now.
27   //
28   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
29        I != E; ++I)
30     IncorporateType(*I);
31 }
32
33
34 // run - This incorporates all types used by the specified module
35 //
36 bool FindUsedTypes::run(Module &m) {
37   UsedTypes.clear();  // reset if run multiple times...
38
39   // Loop over global variables, incorporating their types
40   for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I)
41     IncorporateType(I->getType());
42
43   for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
44     const Function &F = *MI;
45   
46     // Loop over all of the instructions in the function, adding their return
47     // type as well as the types of their operands.
48     //
49     for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
50          II != IE; ++II) {
51       const Instruction *I = *II;
52       const Type *Ty = I->getType();
53     
54       IncorporateType(Ty);  // Incorporate the type of the instruction
55       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
56            OI != OE; ++OI)
57         if ((*OI)->getType() != Ty)         // Avoid set lookup in common case
58           IncorporateType((*OI)->getType());// Insert inst operand types as well
59     }
60   }
61  
62   return false;
63 }
64
65 // Print the types found in the module.  If the optional Module parameter is
66 // passed in, then the types are printed symbolically if possible, using the
67 // symbol table from the module.
68 //
69 void FindUsedTypes::printTypes(std::ostream &o, const Module *M) const {
70   o << "Types in use by this module:\n";
71   if (M) {
72     CachedWriter CW(M, o);
73     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
74            E = UsedTypes.end(); I != E; ++I)
75       CW << "  " << *I << "\n";
76   } else
77     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
78            E = UsedTypes.end(); I != E; ++I)
79       o << "  " << *I << "\n";
80 }