db5d64c0a0ecb463094388a4cd611ebe8b0e164f
[oota-llvm.git] / lib / Analysis / IPA / FindUsedTypes.cpp
1 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to seek out all of the types in use by the program.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/FindUsedTypes.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/Assembly/CachedWriter.h"
20 #include "llvm/Support/InstIterator.h"
21
22 static RegisterAnalysis<FindUsedTypes>
23 X("printusedtypes", "Find Used Types");
24
25 // stub to help linkage
26 void FindUsedTypes::stub() {}
27
28 // IncorporateType - Incorporate one type and all of its subtypes into the
29 // collection of used types.
30 //
31 void FindUsedTypes::IncorporateType(const Type *Ty) {
32   if (UsedTypes.count(Ty)) return;  // Already contain Ty.
33                              
34   // If ty doesn't already exist in the used types map, add it now.
35   //
36   UsedTypes.insert(Ty);
37   
38   // Make sure to add any types this type references now.
39   //
40   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
41        I != E; ++I)
42     IncorporateType(*I);
43 }
44
45 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable &ST) {
46   SymbolTable::const_iterator TI = ST.find(Type::TypeTy);
47   if (TI == ST.end()) return;  // No named types
48
49   for (SymbolTable::type_const_iterator I = TI->second.begin(),
50          E = TI->second.end(); I != E; ++I)
51     IncorporateType(cast<Type>(I->second));
52 }
53
54 void FindUsedTypes::IncorporateValue(const Value *V) {
55   IncorporateType(V->getType());
56   
57   // If this is a constant, it could be using other types...
58   if (const Constant *C = dyn_cast<Constant>(V)) {
59     for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
60          OI != OE; ++OI)
61       IncorporateValue(*OI);
62   }
63 }
64
65
66 // run - This incorporates all types used by the specified module
67 //
68 bool FindUsedTypes::run(Module &m) {
69   UsedTypes.clear();  // reset if run multiple times...
70
71   IncorporateSymbolTable(m.getSymbolTable());
72
73   // Loop over global variables, incorporating their types
74   for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I) {
75     IncorporateType(I->getType());
76     if (I->hasInitializer())
77       IncorporateValue(I->getInitializer());
78   }
79
80   for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
81     IncorporateType(MI->getType());
82     const Function &F = *MI;
83     IncorporateSymbolTable(F.getSymbolTable());
84   
85     // Loop over all of the instructions in the function, adding their return
86     // type as well as the types of their operands.
87     //
88     for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
89          II != IE; ++II) {
90       const Instruction *I = *II;
91       const Type *Ty = I->getType();
92     
93       IncorporateType(Ty);  // Incorporate the type of the instruction
94       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
95            OI != OE; ++OI)
96         IncorporateValue(*OI);  // Insert inst operand types as well
97     }
98   }
99  
100   return false;
101 }
102
103 // Print the types found in the module.  If the optional Module parameter is
104 // passed in, then the types are printed symbolically if possible, using the
105 // symbol table from the module.
106 //
107 void FindUsedTypes::print(std::ostream &o, const Module *M) const {
108   o << "Types in use by this module:\n";
109   if (M) {
110     CachedWriter CW(M, o);
111     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
112            E = UsedTypes.end(); I != E; ++I)
113       CW << "  " << *I << "\n";
114   } else
115     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
116            E = UsedTypes.end(); I != E; ++I)
117       o << "  " << *I << "\n";
118 }