LLVM API Documentation
00001 //===- FindUsedTypes.cpp - Find all Types used by a module ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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/IR/Constants.h" 00018 #include "llvm/IR/DerivedTypes.h" 00019 #include "llvm/IR/InstIterator.h" 00020 #include "llvm/IR/Module.h" 00021 #include "llvm/Support/raw_ostream.h" 00022 using namespace llvm; 00023 00024 char FindUsedTypes::ID = 0; 00025 INITIALIZE_PASS(FindUsedTypes, "print-used-types", 00026 "Find Used Types", false, true) 00027 00028 // IncorporateType - Incorporate one type and all of its subtypes into the 00029 // collection of used types. 00030 // 00031 void FindUsedTypes::IncorporateType(Type *Ty) { 00032 // If ty doesn't already exist in the used types map, add it now, otherwise 00033 // return. 00034 if (!UsedTypes.insert(Ty)) return; // Already contain Ty. 00035 00036 // Make sure to add any types this type references now. 00037 // 00038 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); 00039 I != E; ++I) 00040 IncorporateType(*I); 00041 } 00042 00043 void FindUsedTypes::IncorporateValue(const Value *V) { 00044 IncorporateType(V->getType()); 00045 00046 // If this is a constant, it could be using other types... 00047 if (const Constant *C = dyn_cast<Constant>(V)) { 00048 if (!isa<GlobalValue>(C)) 00049 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end(); 00050 OI != OE; ++OI) 00051 IncorporateValue(*OI); 00052 } 00053 } 00054 00055 00056 // run - This incorporates all types used by the specified module 00057 // 00058 bool FindUsedTypes::runOnModule(Module &m) { 00059 UsedTypes.clear(); // reset if run multiple times... 00060 00061 // Loop over global variables, incorporating their types 00062 for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); 00063 I != E; ++I) { 00064 IncorporateType(I->getType()); 00065 if (I->hasInitializer()) 00066 IncorporateValue(I->getInitializer()); 00067 } 00068 00069 for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) { 00070 IncorporateType(MI->getType()); 00071 const Function &F = *MI; 00072 00073 // Loop over all of the instructions in the function, adding their return 00074 // type as well as the types of their operands. 00075 // 00076 for (const_inst_iterator II = inst_begin(F), IE = inst_end(F); 00077 II != IE; ++II) { 00078 const Instruction &I = *II; 00079 00080 IncorporateType(I.getType()); // Incorporate the type of the instruction 00081 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); 00082 OI != OE; ++OI) 00083 IncorporateValue(*OI); // Insert inst operand types as well 00084 } 00085 } 00086 00087 return false; 00088 } 00089 00090 // Print the types found in the module. If the optional Module parameter is 00091 // passed in, then the types are printed symbolically if possible, using the 00092 // symbol table from the module. 00093 // 00094 void FindUsedTypes::print(raw_ostream &OS, const Module *M) const { 00095 OS << "Types in use by this module:\n"; 00096 for (SetVector<Type *>::const_iterator I = UsedTypes.begin(), 00097 E = UsedTypes.end(); I != E; ++I) { 00098 OS << " " << **I << '\n'; 00099 } 00100 }