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/Assembly/CachedWriter.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/InstIterator.h"
21 static RegisterAnalysis<FindUsedTypes>
22 X("printusedtypes", "Find Used Types");
24 // stub to help linkage
25 void FindUsedTypes::stub() {}
27 // IncorporateType - Incorporate one type and all of its subtypes into the
28 // collection of used types.
30 void FindUsedTypes::IncorporateType(const Type *Ty) {
31 if (UsedTypes.count(Ty)) return; // Already contain Ty.
33 // If ty doesn't already exist in the used types map, add it now.
37 // Make sure to add any types this type references now.
39 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
44 void FindUsedTypes::IncorporateSymbolTable(const SymbolTable &ST) {
45 SymbolTable::const_iterator TI = ST.find(Type::TypeTy);
46 if (TI == ST.end()) return; // No named types
48 for (SymbolTable::type_const_iterator I = TI->second.begin(),
49 E = TI->second.end(); I != E; ++I)
50 IncorporateType(cast<Type>(I->second));
53 // run - This incorporates all types used by the specified module
55 bool FindUsedTypes::run(Module &m) {
56 UsedTypes.clear(); // reset if run multiple times...
58 IncorporateSymbolTable(m.getSymbolTable());
60 // Loop over global variables, incorporating their types
61 for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I)
62 IncorporateType(I->getType());
64 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
65 IncorporateType(MI->getType());
66 const Function &F = *MI;
67 IncorporateSymbolTable(F.getSymbolTable());
69 // Loop over all of the instructions in the function, adding their return
70 // type as well as the types of their operands.
72 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
74 const Instruction *I = *II;
75 const Type *Ty = I->getType();
77 IncorporateType(Ty); // Incorporate the type of the instruction
78 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
80 if ((*OI)->getType() != Ty) // Avoid set lookup in common case
81 IncorporateType((*OI)->getType());// Insert inst operand types as well
88 // Print the types found in the module. If the optional Module parameter is
89 // passed in, then the types are printed symbolically if possible, using the
90 // symbol table from the module.
92 void FindUsedTypes::print(std::ostream &o, const Module *M) const {
93 o << "Types in use by this module:\n";
95 CachedWriter CW(M, o);
96 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
97 E = UsedTypes.end(); I != E; ++I)
98 CW << " " << *I << "\n";
100 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
101 E = UsedTypes.end(); I != E; ++I)
102 o << " " << *I << "\n";