1 //===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
3 // This pass is used to cleanup the output of GCC. It eliminate names for types
4 // that are unused in the entire translation unit, using the FindUsedTypes pass.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/IPO.h"
9 #include "llvm/Analysis/FindUsedTypes.h"
10 #include "llvm/Module.h"
11 #include "llvm/SymbolTable.h"
12 #include "llvm/DerivedTypes.h"
13 #include "Support/Statistic.h"
18 struct DTE : public Pass {
19 // doPassInitialization - For this pass, it removes global symbol table
20 // entries for primitive types. These are never used for linking in GCC and
21 // they make the output uglier to look at, so we nuke them.
23 // Also, initialize instance variables.
27 // getAnalysisUsage - This function needs FindUsedTypes to do its job...
29 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
30 AU.addRequired<FindUsedTypes>();
33 RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
35 NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
38 Pass *createDeadTypeEliminationPass() {
44 // ShouldNukSymtabEntry - Return true if this module level symbol table entry
45 // should be eliminated.
47 static inline bool ShouldNukeSymtabEntry(const std::pair<std::string,Value*>&E){
48 // Nuke all names for primitive types!
49 if (cast<Type>(E.second)->isPrimitiveType()) return true;
51 // Nuke all pointers to primitive types as well...
52 if (const PointerType *PT = dyn_cast<PointerType>(E.second))
53 if (PT->getElementType()->isPrimitiveType()) return true;
58 // run - For this pass, it removes global symbol table entries for primitive
59 // types. These are never used for linking in GCC and they make the output
60 // uglier to look at, so we nuke them. Also eliminate types that are never used
61 // in the entire program as indicated by FindUsedTypes.
63 bool DTE::run(Module &M) {
66 if (SymbolTable *ST = M.getSymbolTable()) {
67 const std::set<const Type *> &UsedTypes =
68 getAnalysis<FindUsedTypes>().getTypes();
70 // Check the symbol table for superfluous type entries...
72 // Grab the 'type' plane of the module symbol...
73 SymbolTable::iterator STI = ST->find(Type::TypeTy);
74 if (STI != ST->end()) {
75 // Loop over all entries in the type plane...
76 SymbolTable::VarMap &Plane = STI->second;
77 for (SymbolTable::VarMap::iterator PI = Plane.begin(); PI != Plane.end();)
78 // If this entry should be unconditionally removed, or if we detect that
79 // the type is not used, remove it.
81 if (ShouldNukeSymtabEntry(*PI) ||
82 !UsedTypes.count(cast<Type>(PI->second))) {
83 #if MAP_IS_NOT_BRAINDEAD
84 PI = Plane.erase(PI); // STD C++ Map should support this!
86 Plane.erase(PI); // Alas, GCC 2.95.3 doesn't *SIGH*