2002-04-10 20:33:11 +00:00
|
|
|
//===- CleanupGCCOutput.cpp - Cleanup GCC Output --------------------------===//
|
2001-10-31 04:33:19 +00:00
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2001-10-31 06:35:59 +00:00
|
|
|
// * Eliminate names for GCC types that we know can't be needed by the user.
|
2001-11-15 04:34:46 +00:00
|
|
|
// * Eliminate names for types that are unused in the entire translation unit
|
2002-04-10 20:33:11 +00:00
|
|
|
// * Fix various problems that we might have in PHI nodes and casts
|
2001-10-31 06:35:59 +00:00
|
|
|
//
|
2001-10-31 04:33:19 +00:00
|
|
|
// Note: This code produces dead declarations, it is a good idea to run DCE
|
|
|
|
// after this pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.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-05-24 20:42:13 +00:00
|
|
|
#include "Support/StatisticReporter.h"
|
2002-05-10 15:38:35 +00:00
|
|
|
|
|
|
|
static Statistic<> NumTypeSymtabEntriesKilled("cleangcc\t- Number of unused typenames removed from symtab");
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::vector;
|
2001-10-31 06:35:59 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct CleanupGCCOutput : public FunctionPass {
|
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-06-25 15:55:03 +00:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// FIXME: This FunctionPass should be a PASS!
|
|
|
|
// FIXME:
|
|
|
|
bool runOnFunction(Function &F) { return false; }
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
// doPassFinalization - Strip out type names that are unused by the program
|
2002-06-25 15:55:03 +00:00
|
|
|
bool doFinalization(Module &M);
|
2002-02-26 21:46:54 +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 {
|
|
|
|
AU.addRequired(FindUsedTypes::ID);
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
2002-07-23 18:01:39 +00:00
|
|
|
RegisterPass<CleanupGCCOutput> X("cleangcc", "Cleanup GCC Output");
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-10 20:33:11 +00:00
|
|
|
Pass *createCleanupGCCOutputPass() {
|
|
|
|
return new CleanupGCCOutput();
|
2001-11-03 09:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ShouldNukSymtabEntry - Return true if this module level symbol table entry
|
|
|
|
// should be eliminated.
|
|
|
|
//
|
2002-05-24 20:42:13 +00:00
|
|
|
static inline bool ShouldNukeSymtabEntry(const std::pair<std::string,Value*>&E){
|
2001-10-31 04:33:19 +00:00
|
|
|
// Nuke all names for primitive types!
|
|
|
|
if (cast<Type>(E.second)->isPrimitiveType()) return true;
|
|
|
|
|
2001-11-15 04:34:46 +00:00
|
|
|
// Nuke all pointers to primitive types as well...
|
|
|
|
if (const PointerType *PT = dyn_cast<PointerType>(E.second))
|
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-01-21 07:31:50 +00:00
|
|
|
// doInitialization - For this pass, it removes global symbol table
|
2001-10-31 04:33:19 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2002-06-25 15:55:03 +00:00
|
|
|
bool CleanupGCCOutput::doInitialization(Module &M) {
|
2001-10-31 04:33:19 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-06-25 15:55:03 +00:00
|
|
|
if (SymbolTable *ST = M.getSymbolTable()) {
|
2001-10-31 06:35:59 +00:00
|
|
|
// Check the symbol table for superfluous type entries...
|
|
|
|
//
|
|
|
|
// Grab the 'type' plane of the module symbol...
|
2001-10-31 04:33:19 +00:00
|
|
|
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
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumTypeSymtabEntriesKilled;
|
2001-10-31 04:33:19 +00:00
|
|
|
Changed = true;
|
|
|
|
} else {
|
|
|
|
++PI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2001-10-31 06:35:59 +00:00
|
|
|
|
2002-06-25 15:55:03 +00:00
|
|
|
bool CleanupGCCOutput::doFinalization(Module &M) {
|
2001-11-15 04:34:46 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2002-06-25 15:55:03 +00:00
|
|
|
if (SymbolTable *ST = M.getSymbolTable()) {
|
2002-01-31 00:45:11 +00:00
|
|
|
const std::set<const Type *> &UsedTypes =
|
|
|
|
getAnalysis<FindUsedTypes>().getTypes();
|
2001-11-15 04:34:46 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-31 04:33:19 +00:00
|
|
|
return Changed;
|
|
|
|
}
|