2002-07-23 22:03:41 +00:00
|
|
|
//===- DeadTypeElimination.cpp - Eliminate unused types for symbol table --===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
// 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.
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-23 22:03:41 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-01-31 00:45:11 +00:00
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-10-31 04:33:19 +00:00
|
|
|
#include "llvm/SymbolTable.h"
|
2001-10-31 06:35:59 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-07-23 22:03:41 +00:00
|
|
|
struct DTE : public Pass {
|
2002-02-26 21:46:54 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
bool run(Module &M);
|
2002-06-25 15:55:03 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// getAnalysisUsage - This function needs FindUsedTypes to do its job...
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<FindUsedTypes>();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
2002-07-26 21:12:44 +00:00
|
|
|
RegisterOpt<DTE> X("deadtypeelim", "Dead Type Elimination");
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<>
|
|
|
|
NumKilled("deadtypeelim", "Number of unused typenames removed from symtab");
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2003-11-21 21:54:22 +00:00
|
|
|
Pass *llvm::createDeadTypeEliminationPass() {
|
2002-07-23 22:03:41 +00:00
|
|
|
return new DTE();
|
2001-11-03 09:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-09 05:04:25 +00:00
|
|
|
// ShouldNukeSymtabEntry - Return true if this module level symbol table entry
|
2001-11-03 09:19:00 +00:00
|
|
|
// should be eliminated.
|
|
|
|
//
|
2004-02-26 20:02:23 +00:00
|
|
|
static inline bool ShouldNukeSymtabEntry(const Type *Ty){
|
2001-10-31 04:33:19 +00:00
|
|
|
// Nuke all names for primitive types!
|
2004-02-26 20:02:23 +00:00
|
|
|
if (Ty->isPrimitiveType()) return true;
|
2001-10-31 04:33:19 +00:00
|
|
|
|
2001-11-15 04:34:46 +00:00
|
|
|
// Nuke all pointers to primitive types as well...
|
2004-02-26 20:02:23 +00:00
|
|
|
if (const PointerType *PT = dyn_cast<PointerType>(Ty))
|
2001-12-04 00:03:30 +00:00
|
|
|
if (PT->getElementType()->isPrimitiveType()) return true;
|
2001-11-15 04:34:46 +00:00
|
|
|
|
2001-10-31 04:33:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-23 22:03:41 +00:00
|
|
|
// 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.
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
2002-07-23 22:03:41 +00:00
|
|
|
bool DTE::run(Module &M) {
|
2001-10-31 04:33:19 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-11-20 18:36:02 +00:00
|
|
|
SymbolTable &ST = M.getSymbolTable();
|
2004-02-26 20:02:23 +00:00
|
|
|
std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
|
2002-07-23 22:03:41 +00:00
|
|
|
|
2002-11-20 18:36:02 +00:00
|
|
|
// Check the symbol table for superfluous type entries...
|
|
|
|
//
|
|
|
|
// Grab the 'type' plane of the module symbol...
|
2004-05-25 08:53:40 +00:00
|
|
|
SymbolTable::type_iterator TI = ST.type_begin();
|
|
|
|
while ( TI != ST.type_end() ) {
|
|
|
|
// 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)) {
|
|
|
|
SymbolTable::type_iterator ToRemove = TI++;
|
2004-05-27 21:16:46 +00:00
|
|
|
ST.remove(ToRemove->second);
|
2004-05-25 08:53:40 +00:00
|
|
|
++NumKilled;
|
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
++TI;
|
|
|
|
// We only need to leave one name for each type.
|
|
|
|
UsedTypes.erase(RHS);
|
2004-02-26 20:02:23 +00:00
|
|
|
}
|
2001-11-15 04:34:46 +00:00
|
|
|
}
|
2002-07-23 22:03:41 +00:00
|
|
|
|
2001-10-31 04:33:19 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
2004-05-25 08:53:40 +00:00
|
|
|
|
|
|
|
// vim: sw=2
|