FindUsedTypes.cpp revision a92dc193971daec428337dad437f08b8b2e5ceaf
1//===- FindUsedTypes.cpp - Find all Types used by a module ------------------=// 2// 3// This pass is used to seek out all of the types in use by the program. 4// 5//===----------------------------------------------------------------------===// 6 7#include "llvm/Analysis/FindUsedTypes.h" 8#include "llvm/Assembly/CachedWriter.h" 9#include "llvm/SymbolTable.h" 10#include "llvm/DerivedTypes.h" 11#include "llvm/Module.h" 12#include "llvm/Support/InstIterator.h" 13 14static RegisterAnalysis<FindUsedTypes> 15X("printusedtypes", "Find Used Types"); 16 17// stub to help linkage 18void FindUsedTypes::stub() {} 19 20// IncorporateType - Incorporate one type and all of its subtypes into the 21// collection of used types. 22// 23void FindUsedTypes::IncorporateType(const Type *Ty) { 24 if (UsedTypes.count(Ty)) return; // Already contain Ty. 25 26 // If ty doesn't already exist in the used types map, add it now. 27 // 28 UsedTypes.insert(Ty); 29 30 // Make sure to add any types this type references now. 31 // 32 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); 33 I != E; ++I) 34 IncorporateType(*I); 35} 36 37 38// run - This incorporates all types used by the specified module 39// 40bool FindUsedTypes::run(Module &m) { 41 UsedTypes.clear(); // reset if run multiple times... 42 43 // Loop over global variables, incorporating their types 44 for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I) 45 IncorporateType(I->getType()); 46 47 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) { 48 IncorporateType(MI->getType()); 49 const Function &F = *MI; 50 51 // Loop over all of the instructions in the function, adding their return 52 // type as well as the types of their operands. 53 // 54 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F); 55 II != IE; ++II) { 56 const Instruction *I = *II; 57 const Type *Ty = I->getType(); 58 59 IncorporateType(Ty); // Incorporate the type of the instruction 60 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end(); 61 OI != OE; ++OI) 62 if ((*OI)->getType() != Ty) // Avoid set lookup in common case 63 IncorporateType((*OI)->getType());// Insert inst operand types as well 64 } 65 } 66 67 return false; 68} 69 70// Print the types found in the module. If the optional Module parameter is 71// passed in, then the types are printed symbolically if possible, using the 72// symbol table from the module. 73// 74void FindUsedTypes::print(std::ostream &o, const Module *M) const { 75 o << "Types in use by this module:\n"; 76 if (M) { 77 CachedWriter CW(M, o); 78 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 79 E = UsedTypes.end(); I != E; ++I) 80 CW << " " << *I << "\n"; 81 } else 82 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 83 E = UsedTypes.end(); I != E; ++I) 84 o << " " << *I << "\n"; 85} 86