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 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/Assembly/Writer.h"
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DerivedTypes.h"
00020 #include "llvm/IR/Module.h"
00021 #include "llvm/Support/InstIterator.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 using namespace llvm;
00024 
00025 char FindUsedTypes::ID = 0;
00026 INITIALIZE_PASS(FindUsedTypes, "print-used-types",
00027                 "Find Used Types", false, true)
00028 
00029 // IncorporateType - Incorporate one type and all of its subtypes into the
00030 // collection of used types.
00031 //
00032 void FindUsedTypes::IncorporateType(Type *Ty) {
00033   // If ty doesn't already exist in the used types map, add it now, otherwise
00034   // return.
00035   if (!UsedTypes.insert(Ty)) return;  // Already contain Ty.
00036 
00037   // Make sure to add any types this type references now.
00038   //
00039   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
00040        I != E; ++I)
00041     IncorporateType(*I);
00042 }
00043 
00044 void FindUsedTypes::IncorporateValue(const Value *V) {
00045   IncorporateType(V->getType());
00046 
00047   // If this is a constant, it could be using other types...
00048   if (const Constant *C = dyn_cast<Constant>(V)) {
00049     if (!isa<GlobalValue>(C))
00050       for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
00051            OI != OE; ++OI)
00052         IncorporateValue(*OI);
00053   }
00054 }
00055 
00056 
00057 // run - This incorporates all types used by the specified module
00058 //
00059 bool FindUsedTypes::runOnModule(Module &m) {
00060   UsedTypes.clear();  // reset if run multiple times...
00061 
00062   // Loop over global variables, incorporating their types
00063   for (Module::const_global_iterator I = m.global_begin(), E = m.global_end();
00064        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(raw_ostream &OS, const Module *M) const {
00096   OS << "Types in use by this module:\n";
00097   for (SetVector<Type *>::const_iterator I = UsedTypes.begin(),
00098        E = UsedTypes.end(); I != E; ++I) {
00099     OS << "   " << **I << '\n';
00100   }
00101 }