Added LLVM project notice to the top of every C++ source file.
[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
22 namespace {
23   struct DTE : public Pass {
24     // doPassInitialization - For this pass, it removes global symbol table
25     // entries for primitive types.  These are never used for linking in GCC and
26     // they make the output uglier to look at, so we nuke them.
27     //
28     // Also, initialize instance variables.
29     //
30     bool run(Module &M);
31
32     // getAnalysisUsage - This function needs FindUsedTypes to do its job...
33     //
34     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
35       AU.addRequired<FindUsedTypes>();
36     }
37   };
38   RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
39   Statistic<>
40   NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
41 }
42
43 Pass *createDeadTypeEliminationPass() {
44   return new DTE();
45 }
46
47
48
49 // ShouldNukSymtabEntry - Return true if this module level symbol table entry
50 // should be eliminated.
51 //
52 static inline bool ShouldNukeSymtabEntry(const std::pair<std::string,Value*>&E){
53   // Nuke all names for primitive types!
54   if (cast<Type>(E.second)->isPrimitiveType()) return true;
55
56   // Nuke all pointers to primitive types as well...
57   if (const PointerType *PT = dyn_cast<PointerType>(E.second))
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   const std::set<const Type *> &UsedTypes =
73     getAnalysis<FindUsedTypes>().getTypes();
74
75   // Check the symbol table for superfluous type entries...
76   //
77   // Grab the 'type' plane of the module symbol...
78   SymbolTable::iterator STI = ST.find(Type::TypeTy);
79   if (STI != ST.end()) {
80     // Loop over all entries in the type plane...
81     SymbolTable::VarMap &Plane = STI->second;
82     for (SymbolTable::VarMap::iterator PI = Plane.begin(); PI != Plane.end();)
83       // If this entry should be unconditionally removed, or if we detect that
84       // the type is not used, remove it.
85       if (ShouldNukeSymtabEntry(*PI) ||
86           !UsedTypes.count(cast<Type>(PI->second))) {
87         SymbolTable::VarMap::iterator PJ = PI++;
88         Plane.erase(PJ);
89         ++NumKilled;
90         Changed = true;
91       } else {
92         ++PI;
93       }
94   }
95
96   return Changed;
97 }