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