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/SymbolTable.h" 00021 #include "llvm/Assembly/CachedWriter.h" 00022 #include "llvm/Support/InstIterator.h" 00023 using namespace llvm; 00024 00025 static RegisterAnalysis<FindUsedTypes> 00026 X("printusedtypes", "Find Used Types"); 00027 00028 // stub to help linkage 00029 void FindUsedTypes::stub() {} 00030 00031 // IncorporateType - Incorporate one type and all of its subtypes into the 00032 // collection of used types. 00033 // 00034 void FindUsedTypes::IncorporateType(const Type *Ty) { 00035 // If ty doesn't already exist in the used types map, add it now, otherwise 00036 // return. 00037 if (!UsedTypes.insert(Ty).second) return; // Already contain Ty. 00038 00039 // Make sure to add any types this type references now. 00040 // 00041 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); 00042 I != E; ++I) 00043 IncorporateType(*I); 00044 } 00045 00046 void FindUsedTypes::IncorporateValue(const Value *V) { 00047 IncorporateType(V->getType()); 00048 00049 // If this is a constant, it could be using other types... 00050 if (const Constant *C = dyn_cast<Constant>(V)) { 00051 if (!isa<GlobalValue>(C)) 00052 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); 00053 OI != OE; ++OI) 00054 IncorporateValue(*OI); 00055 } 00056 } 00057 00058 00059 // run - This incorporates all types used by the specified module 00060 // 00061 bool FindUsedTypes::runOnModule(Module &m) { 00062 UsedTypes.clear(); // reset if run multiple times... 00063 00064 // Loop over global variables, incorporating their types 00065 for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I) { 00066 IncorporateType(I->getType()); 00067 if (I->hasInitializer()) 00068 IncorporateValue(I->getInitializer()); 00069 } 00070 00071 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) { 00072 IncorporateType(MI->getType()); 00073 const Function &F = *MI; 00074 00075 // Loop over all of the instructions in the function, adding their return 00076 // type as well as the types of their operands. 00077 // 00078 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F); 00079 II != IE; ++II) { 00080 const Instruction &I = *II; 00081 00082 IncorporateType(I.getType()); // Incorporate the type of the instruction 00083 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); 00084 OI != OE; ++OI) 00085 IncorporateValue(*OI); // Insert inst operand types as well 00086 } 00087 } 00088 00089 return false; 00090 } 00091 00092 // Print the types found in the module. If the optional Module parameter is 00093 // passed in, then the types are printed symbolically if possible, using the 00094 // symbol table from the module. 00095 // 00096 void FindUsedTypes::print(std::ostream &o, const Module *M) const { 00097 o << "Types in use by this module:\n"; 00098 if (M) { 00099 CachedWriter CW(M, o); 00100 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 00101 E = UsedTypes.end(); I != E; ++I) 00102 CW << " " << **I << "\n"; 00103 } else 00104 for (std::set<const Type *>::const_iterator I = UsedTypes.begin(), 00105 E = UsedTypes.end(); I != E; ++I) 00106 o << " " << **I << "\n"; 00107 }