1 //===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to cleanup the output of GCC. It eliminate names for types
11 // that are unused in the entire translation unit, using the FindUsedTypes pass.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/Analysis/FindUsedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/DerivedTypes.h"
20 #include "Support/Statistic.h"
24 struct DTE : public Pass {
25 // doPassInitialization - For this pass, it removes global symbol table
26 // entries for primitive types. These are never used for linking in GCC and
27 // they make the output uglier to look at, so we nuke them.
29 // Also, initialize instance variables.
33 // getAnalysisUsage - This function needs FindUsedTypes to do its job...
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired<FindUsedTypes>();
39 RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
41 NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
44 Pass *llvm::createDeadTypeEliminationPass() {
49 // ShouldNukeSymtabEntry - Return true if this module level symbol table entry
50 // should be eliminated.
52 static inline bool ShouldNukeSymtabEntry(const Type *Ty){
53 // Nuke all names for primitive types!
54 if (Ty->isPrimitiveType()) return true;
56 // Nuke all pointers to primitive types as well...
57 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
58 if (PT->getElementType()->isPrimitiveType()) return true;
63 // run - For this pass, it removes global symbol table entries for primitive
64 // types. These are never used for linking in GCC and they make the output
65 // uglier to look at, so we nuke them. Also eliminate types that are never used
66 // in the entire program as indicated by FindUsedTypes.
68 bool DTE::run(Module &M) {
71 SymbolTable &ST = M.getSymbolTable();
72 std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
74 // Check the symbol table for superfluous type entries...
76 // Grab the 'type' plane of the module symbol...
77 SymbolTable::iterator STI = ST.find(Type::TypeTy);
78 if (STI != ST.end()) {
79 // Loop over all entries in the type plane...
80 SymbolTable::VarMap &Plane = STI->second;
81 for (SymbolTable::VarMap::iterator PI = Plane.begin(); PI != Plane.end();) {
82 // If this entry should be unconditionally removed, or if we detect that
83 // the type is not used, remove it.
84 const Type *RHS = cast<Type>(PI->second);
85 if (ShouldNukeSymtabEntry(RHS) || !UsedTypes.count(RHS)) {
91 // We only need to leave one name for each type.