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