2002-07-23 22:03:41 +00:00
|
|
|
//===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
// This pass is used to cleanup the output of GCC. It eliminate names for types
|
|
|
|
// that are unused in the entire translation unit, using the FindUsedTypes pass.
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-23 22:03:41 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-01-31 00:45:11 +00:00
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-10-31 04:33:19 +00:00
|
|
|
#include "llvm/SymbolTable.h"
|
2001-10-31 06:35:59 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-10 15:38:35 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::vector;
|
2001-10-31 06:35:59 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-07-23 22:03:41 +00:00
|
|
|
struct DTE : public Pass {
|
2002-02-26 21:46:54 +00:00
|
|
|
// doPassInitialization - For this pass, it removes global symbol table
|
|
|
|
// entries for primitive types. These are never used for linking in GCC and
|
|
|
|
// they make the output uglier to look at, so we nuke them.
|
|
|
|
//
|
|
|
|
// Also, initialize instance variables.
|
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
bool run(Module &M);
|
2002-06-25 15:55:03 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// getAnalysisUsage - This function needs FindUsedTypes to do its job...
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<FindUsedTypes>();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
2002-07-26 21:12:44 +00:00
|
|
|
RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<>
|
|
|
|
NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-23 22:03:41 +00:00
|
|
|
Pass *createDeadTypeEliminationPass() {
|
|
|
|
return new DTE();
|
2001-11-03 09:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ShouldNukSymtabEntry - Return true if this module level symbol table entry
|
|
|
|
// should be eliminated.
|
|
|
|
//
|
2002-05-24 20:42:13 +00:00
|
|
|
static inline bool ShouldNukeSymtabEntry(const std::pair<std::string,Value*>&E){
|
2001-10-31 04:33:19 +00:00
|
|
|
// Nuke all names for primitive types!
|
|
|
|
if (cast<Type>(E.second)->isPrimitiveType()) return true;
|
|
|
|
|
2001-11-15 04:34:46 +00:00
|
|
|
// Nuke all pointers to primitive types as well...
|
|
|
|
if (const PointerType *PT = dyn_cast<PointerType>(E.second))
|
2001-12-04 00:03:30 +00:00
|
|
|
if (PT->getElementType()->isPrimitiveType()) return true;
|
2001-11-15 04:34:46 +00:00
|
|
|
|
2001-10-31 04:33:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-23 22:03:41 +00:00
|
|
|
// run - For this pass, it removes global symbol table entries for primitive
|
|
|
|
// types. These are never used for linking in GCC and they make the output
|
|
|
|
// uglier to look at, so we nuke them. Also eliminate types that are never used
|
|
|
|
// in the entire program as indicated by FindUsedTypes.
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
bool DTE::run(Module &M) {
|
2001-10-31 04:33:19 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-06-25 15:55:03 +00:00
|
|
|
if (SymbolTable *ST = M.getSymbolTable()) {
|
2002-07-23 22:03:41 +00:00
|
|
|
const std::set<const Type *> &UsedTypes =
|
|
|
|
getAnalysis<FindUsedTypes>().getTypes();
|
|
|
|
|
2001-10-31 06:35:59 +00:00
|
|
|
// Check the symbol table for superfluous type entries...
|
|
|
|
//
|
|
|
|
// Grab the 'type' plane of the module symbol...
|
2001-10-31 04:33:19 +00:00
|
|
|
SymbolTable::iterator STI = ST->find(Type::TypeTy);
|
|
|
|
if (STI != ST->end()) {
|
|
|
|
// Loop over all entries in the type plane...
|
|
|
|
SymbolTable::VarMap &Plane = STI->second;
|
|
|
|
for (SymbolTable::VarMap::iterator PI = Plane.begin(); PI != Plane.end();)
|
2002-09-08 21:47:54 +00:00
|
|
|
// If this entry should be unconditionally removed, or if we detect that
|
|
|
|
// the type is not used, remove it.
|
|
|
|
//
|
|
|
|
if (ShouldNukeSymtabEntry(*PI) ||
|
|
|
|
!UsedTypes.count(cast<Type>(PI->second))) {
|
2001-10-31 04:33:19 +00:00
|
|
|
#if MAP_IS_NOT_BRAINDEAD
|
|
|
|
PI = Plane.erase(PI); // STD C++ Map should support this!
|
|
|
|
#else
|
|
|
|
Plane.erase(PI); // Alas, GCC 2.95.3 doesn't *SIGH*
|
|
|
|
PI = Plane.begin();
|
2001-11-15 04:34:46 +00:00
|
|
|
#endif
|
2002-07-23 22:03:41 +00:00
|
|
|
++NumKilled;
|
2001-11-15 04:34:46 +00:00
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
++PI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-23 22:03:41 +00:00
|
|
|
|
2001-10-31 04:33:19 +00:00
|
|
|
return Changed;
|
|
|
|
}
|