-//===- CleanupGCCOutput.cpp - Cleanup GCC Output --------------------------===//
+//===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
//
-// This pass is used to cleanup the output of GCC. GCC's output is
-// unneccessarily gross for a couple of reasons. This pass does the following
-// things to try to clean it up:
+// The LLVM Compiler Infrastructure
//
-// * Eliminate names for GCC types that we know can't be needed by the user.
-// * Eliminate names for types that are unused in the entire translation unit
-// * Fix various problems that we might have in PHI nodes and casts
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
-// Note: This code produces dead declarations, it is a good idea to run DCE
-// after this pass.
+//===----------------------------------------------------------------------===//
+//
+// 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.
//
//===----------------------------------------------------------------------===//
-#include "llvm/Transforms/CleanupGCCOutput.h"
+#define DEBUG_TYPE "deadtypeelim"
+#include "llvm/Transforms/IPO.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
+#include "llvm/TypeSymbolTable.h"
#include "llvm/DerivedTypes.h"
-#include "Support/StatisticReporter.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Compiler.h"
+using namespace llvm;
-static Statistic<> NumTypeSymtabEntriesKilled("cleangcc\t- Number of unused typenames removed from symtab");
-
-using std::vector;
+STATISTIC(NumKilled, "Number of unused typenames removed from symtab");
namespace {
- struct CleanupGCCOutput : public FunctionPass {
+ struct VISIBILITY_HIDDEN DTE : public ModulePass {
+ static char ID; // Pass identification, replacement for typeid
+ DTE() : ModulePass((intptr_t)&ID) {}
+
// 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.
//
- bool doInitialization(Module &M);
+ bool runOnModule(Module &M);
- // FIXME:
- // FIXME: This FunctionPass should be a PASS!
- // FIXME:
- bool runOnFunction(Function &F) { return false; }
-
- // doPassFinalization - Strip out type names that are unused by the program
- bool doFinalization(Module &M);
-
// getAnalysisUsage - This function needs FindUsedTypes to do its job...
//
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired(FindUsedTypes::ID);
+ AU.addRequired<FindUsedTypes>();
}
};
- RegisterPass<CleanupGCCOutput> X("cleangcc", "Cleanup GCC Output");
+ char DTE::ID = 0;
+ RegisterPass<DTE> X("deadtypeelim", "Dead Type Elimination");
}
-Pass *createCleanupGCCOutputPass() {
- return new CleanupGCCOutput();
+ModulePass *llvm::createDeadTypeEliminationPass() {
+ return new DTE();
}
-
-// ShouldNukSymtabEntry - Return true if this module level symbol table entry
+// ShouldNukeSymtabEntry - Return true if this module level symbol table entry
// should be eliminated.
//
-static inline bool ShouldNukeSymtabEntry(const std::pair<std::string,Value*>&E){
+static inline bool ShouldNukeSymtabEntry(const Type *Ty){
// Nuke all names for primitive types!
- if (cast<Type>(E.second)->isPrimitiveType()) return true;
+ if (Ty->isPrimitiveType() || Ty->isInteger())
+ return true;
// Nuke all pointers to primitive types as well...
- if (const PointerType *PT = dyn_cast<PointerType>(E.second))
- if (PT->getElementType()->isPrimitiveType()) return true;
+ if (const PointerType *PT = dyn_cast<PointerType>(Ty))
+ if (PT->getElementType()->isPrimitiveType() ||
+ PT->getElementType()->isInteger())
+ return true;
return false;
}
-// doInitialization - 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.
+// 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.
//
-bool CleanupGCCOutput::doInitialization(Module &M) {
+bool DTE::runOnModule(Module &M) {
bool Changed = false;
- if (SymbolTable *ST = M.getSymbolTable()) {
- // Check the symbol table for superfluous type entries...
- //
- // Grab the 'type' plane of the module symbol...
- 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();)
- if (ShouldNukeSymtabEntry(*PI)) { // Should we remove this entry?
-#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();
-#endif
- ++NumTypeSymtabEntriesKilled;
- Changed = true;
- } else {
- ++PI;
- }
+ TypeSymbolTable &ST = M.getTypeSymbolTable();
+ std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
+
+ // Check the symbol table for superfluous type entries...
+ //
+ // Grab the 'type' plane of the module symbol...
+ TypeSymbolTable::iterator TI = ST.begin();
+ TypeSymbolTable::iterator TE = ST.end();
+ while ( TI != TE ) {
+ // If this entry should be unconditionally removed, or if we detect that
+ // the type is not used, remove it.
+ const Type *RHS = TI->second;
+ if (ShouldNukeSymtabEntry(RHS) || !UsedTypes.count(RHS)) {
+ ST.remove(TI++);
+ ++NumKilled;
+ Changed = true;
+ } else {
+ ++TI;
+ // We only need to leave one name for each type.
+ UsedTypes.erase(RHS);
}
}
return Changed;
}
-
-bool CleanupGCCOutput::doFinalization(Module &M) {
- bool Changed = false;
-
- if (SymbolTable *ST = M.getSymbolTable()) {
- const std::set<const Type *> &UsedTypes =
- getAnalysis<FindUsedTypes>().getTypes();
-
- // Check the symbol table for superfluous type entries that aren't used in
- // the program
- //
- // Grab the 'type' plane of the module symbol...
- 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();)
- if (!UsedTypes.count(cast<Type>(PI->second))) {
-#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(); // N^2 algorithms are fun. :(
-#endif
- Changed = true;
- } else {
- ++PI;
- }
- }
- }
- return Changed;
-}
+// vim: sw=2