LLVM API Documentation

FindUsedTypes.cpp

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