2003-10-13 03:32:08 +00:00
|
|
|
//===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-09 05:27:21 +00:00
|
|
|
//
|
|
|
|
// This pass is used to seek out all of the types in use by the program.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
|
|
|
#include "llvm/Assembly/CachedWriter.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-01-31 00:42:27 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2002-01-31 00:42:27 +00:00
|
|
|
|
2002-07-26 21:12:46 +00:00
|
|
|
static RegisterAnalysis<FindUsedTypes>
|
|
|
|
X("printusedtypes", "Find Used Types");
|
2001-11-09 05:27:21 +00:00
|
|
|
|
2002-09-24 00:07:21 +00:00
|
|
|
// stub to help linkage
|
|
|
|
void FindUsedTypes::stub() {}
|
|
|
|
|
2001-11-09 05:27:21 +00:00
|
|
|
// IncorporateType - Incorporate one type and all of its subtypes into the
|
|
|
|
// collection of used types.
|
|
|
|
//
|
|
|
|
void FindUsedTypes::IncorporateType(const Type *Ty) {
|
|
|
|
if (UsedTypes.count(Ty)) return; // Already contain Ty.
|
|
|
|
|
|
|
|
// If ty doesn't already exist in the used types map, add it now.
|
|
|
|
//
|
|
|
|
UsedTypes.insert(Ty);
|
|
|
|
|
|
|
|
// Make sure to add any types this type references now.
|
|
|
|
//
|
|
|
|
for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
|
|
|
|
I != E; ++I)
|
|
|
|
IncorporateType(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// run - This incorporates all types used by the specified module
|
2001-11-09 05:27:21 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool FindUsedTypes::run(Module &m) {
|
2002-01-31 00:42:27 +00:00
|
|
|
UsedTypes.clear(); // reset if run multiple times...
|
|
|
|
|
2001-11-09 05:27:21 +00:00
|
|
|
// Loop over global variables, incorporating their types
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Module::const_giterator I = m.gbegin(), E = m.gend(); I != E; ++I)
|
|
|
|
IncorporateType(I->getType());
|
2001-11-09 05:27:21 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
|
2003-05-31 23:30:52 +00:00
|
|
|
IncorporateType(MI->getType());
|
2002-06-25 16:13:24 +00:00
|
|
|
const Function &F = *MI;
|
2001-11-09 05:27:21 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Loop over all of the instructions in the function, adding their return
|
|
|
|
// type as well as the types of their operands.
|
2002-01-31 00:42:27 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
|
2002-01-31 00:42:27 +00:00
|
|
|
II != IE; ++II) {
|
|
|
|
const Instruction *I = *II;
|
|
|
|
const Type *Ty = I->getType();
|
2001-11-09 05:27:21 +00:00
|
|
|
|
2002-01-31 00:42:27 +00:00
|
|
|
IncorporateType(Ty); // Incorporate the type of the instruction
|
|
|
|
for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if ((*OI)->getType() != Ty) // Avoid set lookup in common case
|
|
|
|
IncorporateType((*OI)->getType());// Insert inst operand types as well
|
|
|
|
}
|
2001-11-09 05:27:21 +00:00
|
|
|
}
|
2002-01-31 00:42:27 +00:00
|
|
|
|
2001-11-09 05:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the types found in the module. If the optional Module parameter is
|
|
|
|
// passed in, then the types are printed symbolically if possible, using the
|
|
|
|
// symbol table from the module.
|
|
|
|
//
|
2002-07-27 01:12:17 +00:00
|
|
|
void FindUsedTypes::print(std::ostream &o, const Module *M) const {
|
2001-11-09 05:27:21 +00:00
|
|
|
o << "Types in use by this module:\n";
|
|
|
|
if (M) {
|
|
|
|
CachedWriter CW(M, o);
|
2002-01-20 22:54:45 +00:00
|
|
|
for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
|
2001-11-09 05:27:21 +00:00
|
|
|
E = UsedTypes.end(); I != E; ++I)
|
2002-01-20 22:54:45 +00:00
|
|
|
CW << " " << *I << "\n";
|
2001-11-09 05:27:21 +00:00
|
|
|
} else
|
2002-01-20 22:54:45 +00:00
|
|
|
for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
|
2001-11-09 05:27:21 +00:00
|
|
|
E = UsedTypes.end(); I != E; ++I)
|
2002-01-20 22:54:45 +00:00
|
|
|
o << " " << *I << "\n";
|
2001-11-09 05:27:21 +00:00
|
|
|
}
|