1 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to seek out all of the types in use by the program.
12 //===----------------------------------------------------------------------===//
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"
22 static RegisterAnalysis<FindUsedTypes>
23 X("printusedtypes", "Find Used Types");
25 // stub to help linkage
26 void FindUsedTypes::stub() {}
28 // IncorporateType - Incorporate one type and all of its subtypes into the
29 // collection of used types.
31 void FindUsedTypes::IncorporateType(const Type *Ty) {
32 if (UsedTypes.count(Ty)) return; // Already contain Ty.
34 // If ty doesn't already exist in the used types map, add it now.
38 // Make sure to add any types this type references now.
40 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
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
49 for (SymbolTable::type_const_iterator I = TI->second.begin(),
50 E = TI->second.end(); I != E; ++I)
51 IncorporateType(cast<Type>(I->second));
54 void FindUsedTypes::IncorporateValue(const Value *V) {
55 IncorporateType(V->getType());
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();
61 IncorporateValue(*OI);
66 // run - This incorporates all types used by the specified module
68 bool FindUsedTypes::run(Module &m) {
69 UsedTypes.clear(); // reset if run multiple times...
71 IncorporateSymbolTable(m.getSymbolTable());
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());
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());
85 // Loop over all of the instructions in the function, adding their return
86 // type as well as the types of their operands.
88 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
90 const Instruction *I = *II;
91 const Type *Ty = I->getType();
93 IncorporateType(Ty); // Incorporate the type of the instruction
94 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
96 IncorporateValue(*OI); // Insert inst operand types as well
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.
107 void FindUsedTypes::print(std::ostream &o, const Module *M) const {
108 o << "Types in use by this module:\n";
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";
115 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
116 E = UsedTypes.end(); I != E; ++I)
117 o << " " << *I << "\n";