2003-10-13 03:32:08 +00:00
|
|
|
//===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-09 05:27:21 +00:00
|
|
|
//
|
2003-11-02 01:28:41 +00:00
|
|
|
// This pass is used to seek out all of the types in use by the program. Note
|
|
|
|
// that this analysis explicitly does not include types only used by the symbol
|
|
|
|
// table.
|
2001-11-09 05:27:21 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
2003-10-28 23:09:45 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-11-09 05:27:21 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-01-31 00:42:27 +00:00
|
|
|
#include "llvm/Module.h"
|
2006-12-06 06:35:25 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2004-05-09 06:22:29 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char FindUsedTypes::ID = 0;
|
2006-08-27 22:30:17 +00:00
|
|
|
static RegisterPass<FindUsedTypes>
|
2008-03-20 02:25:21 +00:00
|
|
|
X("printusedtypes", "Find Used Types", false, true);
|
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) {
|
2005-04-21 21:13:18 +00:00
|
|
|
// If ty doesn't already exist in the used types map, add it now, otherwise
|
2004-05-28 05:36:49 +00:00
|
|
|
// return.
|
|
|
|
if (!UsedTypes.insert(Ty).second) return; // Already contain Ty.
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2001-11-09 05:27:21 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2003-10-28 23:09:45 +00:00
|
|
|
void FindUsedTypes::IncorporateValue(const Value *V) {
|
|
|
|
IncorporateType(V->getType());
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-10-28 23:09:45 +00:00
|
|
|
// If this is a constant, it could be using other types...
|
|
|
|
if (const Constant *C = dyn_cast<Constant>(V)) {
|
2004-07-18 00:18:30 +00:00
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
|
2005-04-22 04:01:18 +00:00
|
|
|
OI != OE; ++OI)
|
|
|
|
IncorporateValue(*OI);
|
2003-10-28 23:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool FindUsedTypes::runOnModule(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
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); I != E; ++I) {
|
2002-06-25 16:13:24 +00:00
|
|
|
IncorporateType(I->getType());
|
2003-10-28 23:09:45 +00:00
|
|
|
if (I->hasInitializer())
|
|
|
|
IncorporateValue(I->getInitializer());
|
|
|
|
}
|
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;
|
2005-04-21 21:13:18 +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) {
|
2004-04-27 15:13:33 +00:00
|
|
|
const Instruction &I = *II;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-28 05:36:49 +00:00
|
|
|
IncorporateType(I.getType()); // Incorporate the type of the instruction
|
2004-04-27 15:13:33 +00:00
|
|
|
for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
|
2002-01-31 00:42:27 +00:00
|
|
|
OI != OE; ++OI)
|
2003-10-28 23:09:45 +00:00
|
|
|
IncorporateValue(*OI); // Insert inst operand types as well
|
2002-01-31 00:42:27 +00:00
|
|
|
}
|
2001-11-09 05:27:21 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +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";
|
2006-12-06 06:35:25 +00:00
|
|
|
for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
|
|
|
|
E = UsedTypes.end(); I != E; ++I)
|
|
|
|
WriteTypeSymbolic(o << " ", *I, M) << "\n";
|
2001-11-09 05:27:21 +00:00
|
|
|
}
|