Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / lib / Transforms / IPO / DeadTypeElimination.cpp
1 //===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
21 using namespace llvm;
22
23 namespace {
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.
28     //
29     // Also, initialize instance variables.
30     //
31     bool run(Module &M);
32
33     // getAnalysisUsage - This function needs FindUsedTypes to do its job...
34     //
35     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36       AU.addRequired<FindUsedTypes>();
37     }
38   };
39   RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
40   Statistic<>
41   NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
42 }
43
44 Pass *llvm::createDeadTypeEliminationPass() {
45   return new DTE();
46 }
47
48
49 // ShouldNukeSymtabEntry - Return true if this module level symbol table entry
50 // should be eliminated.
51 //
52 static inline bool ShouldNukeSymtabEntry(const Type *Ty){
53   // Nuke all names for primitive types!
54   if (Ty->isPrimitiveType()) return true;
55
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;
59
60   return false;
61 }
62
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.
67 //
68 bool DTE::run(Module &M) {
69   bool Changed = false;
70
71   SymbolTable &ST = M.getSymbolTable();
72   std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
73
74   // Check the symbol table for superfluous type entries...
75   //
76   // Grab the 'type' plane of the module symbol...
77   SymbolTable::type_iterator TI = ST.type_begin();
78   while ( TI != ST.type_end() ) {
79     // If this entry should be unconditionally removed, or if we detect that
80     // the type is not used, remove it.
81     const Type *RHS = TI->second;
82     if (ShouldNukeSymtabEntry(RHS) || !UsedTypes.count(RHS)) {
83       SymbolTable::type_iterator ToRemove = TI++;
84       ST.remove(ToRemove->second);
85       ++NumKilled;
86       Changed = true;
87     } else {
88       ++TI;
89       // We only need to leave one name for each type.
90       UsedTypes.erase(RHS);
91     }
92   }
93
94   return Changed;
95 }
96
97 // vim: sw=2