83b994b53e9964fb6583b9ea6839b8aff092c6e2
[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.  Note
11 // that this analysis explicitly does not include types only used by the symbol
12 // table.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/FindUsedTypes.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/SymbolTable.h"
21 #include "llvm/Assembly/CachedWriter.h"
22 #include "llvm/Support/InstIterator.h"
23 using namespace llvm;
24
25 static RegisterAnalysis<FindUsedTypes>
26 X("printusedtypes", "Find Used Types");
27
28 // stub to help linkage
29 void FindUsedTypes::stub() {}
30
31 // IncorporateType - Incorporate one type and all of its subtypes into the
32 // collection of used types.
33 //
34 void FindUsedTypes::IncorporateType(const Type *Ty) {
35   // If ty doesn't already exist in the used types map, add it now, otherwise
36   // return.
37   if (!UsedTypes.insert(Ty).second) return;  // Already contain Ty.
38
39   // Make sure to add any types this type references now.
40   //
41   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
42        I != E; ++I)
43     IncorporateType(*I);
44 }
45
46 void FindUsedTypes::IncorporateValue(const Value *V) {
47   IncorporateType(V->getType());
48
49   // If this is a constant, it could be using other types...
50   if (const Constant *C = dyn_cast<Constant>(V)) {
51     if (!isa<GlobalValue>(C))
52       for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
53            OI != OE; ++OI)
54         IncorporateValue(*OI);
55   }
56 }
57
58
59 // run - This incorporates all types used by the specified module
60 //
61 bool FindUsedTypes::runOnModule(Module &m) {
62   UsedTypes.clear();  // reset if run multiple times...
63
64   // Loop over global variables, incorporating their types
65   for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); I != E; ++I) {
66     IncorporateType(I->getType());
67     if (I->hasInitializer())
68       IncorporateValue(I->getInitializer());
69   }
70
71   for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
72     IncorporateType(MI->getType());
73     const Function &F = *MI;
74
75     // Loop over all of the instructions in the function, adding their return
76     // type as well as the types of their operands.
77     //
78     for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
79          II != IE; ++II) {
80       const Instruction &I = *II;
81
82       IncorporateType(I.getType());  // Incorporate the type of the instruction
83       for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
84            OI != OE; ++OI)
85         IncorporateValue(*OI);  // Insert inst operand types as well
86     }
87   }
88
89   return false;
90 }
91
92 // Print the types found in the module.  If the optional Module parameter is
93 // passed in, then the types are printed symbolically if possible, using the
94 // symbol table from the module.
95 //
96 void FindUsedTypes::print(std::ostream &o, const Module *M) const {
97   o << "Types in use by this module:\n";
98   if (M) {
99     CachedWriter CW(M, o);
100     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
101            E = UsedTypes.end(); I != E; ++I)
102       CW << "  " << **I << "\n";
103   } else
104     for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
105            E = UsedTypes.end(); I != E; ++I)
106       o << "  " << **I << "\n";
107 }