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"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-10-31 06:35:59 +00:00
|
|
|
#include "llvm/iMemory.h"
|
Add new cleanup pass:
// 1. PHI nodes with multiple entries for the same predecessor. GCC sometimes
// generates code that looks like this:
//
// bb7: br bool %cond1004, label %bb8, label %bb8
// bb8: %reg119 = phi uint [ 0, %bb7 ], [ 1, %bb7 ]
//
// which is completely illegal LLVM code. To compensate for this, we insert
// an extra basic block, and convert the code to look like this:
//
// bb7: br bool %cond1004, label %bbX, label %bb8
// bbX: br label bb8
// bb8: %reg119 = phi uint [ 0, %bbX ], [ 1, %bb7 ]
//
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1114 91177308-0d34-0410-b5e6-96231b3b80d8
2001-11-03 21:08:59 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iOther.h"
|
2002-02-12 22:39:50 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2002-05-07 18:12:18 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2002-05-24 20:42:13 +00:00
|
|
|
#include "Support/StatisticReporter.h"
|
2001-11-03 09:19:00 +00:00
|
|
|
#include <algorithm>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
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-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Cleanup GCC Output"; }
|
|
|
|
|
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-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;
|
|
|
|
}
|