LLVM API Documentation
00001 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass is used to seek out all of the types in use by the program. Note 00011 // that this analysis explicitly does not include types only used by the symbol 00012 // table. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "llvm/Analysis/FindUsedTypes.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/Assembly/CachedWriter.h" 00021 #include "llvm/Support/InstIterator.h" 00022 using namespace llvm; 00023 00024 static RegisterAnalysis<FindUsedTypes> 00025 X("printusedtypes", "Find Used Types"); 00026 00027 // IncorporateType - Incorporate one type and all of its subtypes into the 00028 // collection of used types. 00029 // 00030 void FindUsedTypes::IncorporateType(const Type *Ty) { 00031 // If ty doesn't already exist in the used types map, add it now, otherwise 00032 // return. 00033 if (!UsedTypes.insert(Ty).second) return; // Already contain Ty. 00034 00035 // Make sure to add any types this type references now. 00036 // 00037 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); 00038 I != E; ++I) 00039 IncorporateType(*I); 00040 } 00041 00042 void FindUsedTypes::IncorporateValue(const Value *V) { 00043 IncorporateType(V->getType()); 00044 00045 // If this is a constant, it could be using other types... 00046 if (const Constant *C = dyn_cast<Constant>(V)) { 00047 if (!isa<GlobalValue>(C)) 00048 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); 00049 OI != OE; ++OI) 00050 IncorporateValue(*OI); 00051 } 00052 } 00053 00054 00055 // run - This incorporates all types used by the specified module 00056 // 00057 bool FindUsedTypes::runOnModule(Module &m) { 00058 UsedTypes.clear(); // reset if run multiple times... 00059 00060 // Loop over global variables, incorporating their types 00061 for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); I != E; ++I) { 00062 IncorporateType(I->getType()); 00063 if (I->hasInitializer()) 00064 IncorporateValue(I->getInitializer()); 00065 } 00066 00067 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) { 00068 IncorporateType(MI->getType()); 00069 const Function &F = *MI; 00070 00071 // Loop over all of the instructions in the function, adding their return 00072 // type as well as the types of their operands. 00073 // 00074 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F); 00075 II != IE; ++II) { 00076 const Instruction &I = *II; 00077 00078 IncorporateType(I.getType()); // Incorporate the type of the instruction 00079 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); 00080 OI != OE; ++OI) 00081 IncorporateValue(*OI); // Insert inst operand types as well 00082 } 00083 } 00084 00085 return false; 00086 } 00087 00088 // Print the types found in the module. If the optional Module parameter is 00089 // passed in, then the types are printed symbolically if possible, using the 00090 // symbol table from the module. 00091 // 00092 void FindUsedTypes::print(std::ostream &o, const Module *M) const { 00093 o << "Types in use by this module:\n"; 00094 if (M) { 00095 CachedWriter CW(M, o); 00096 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 00097 E = UsedTypes.end(); I != E; ++I) 00098 CW << " " << **I << "\n"; 00099 } else 00100 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 00101 E = UsedTypes.end(); I != E; ++I) 00102 o << " " << **I << "\n"; 00103 } 00104 00105 // Ensure that this file gets linked in when FindUsedTypes.h is used. 00106 DEFINING_FILE_FOR(FindUsedTypes)