#include <set>
using namespace llvm;
+STATISTIC(NumAliases , "Number of global aliases removed");
STATISTIC(NumFunctions, "Number of functions removed");
STATISTIC(NumVariables, "Number of global variables removed");
struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
static char ID; // Pass identification, replacement for typeid
GlobalDCE() : ModulePass(&ID) {}
-
+
// run - Do the GlobalDCE pass on the specified module, optionally updating
// the specified callgraph to reflect the changes.
//
private:
std::set<GlobalValue*> AliveGlobals;
- /// GlobalIsNeeded - the specific global value as needed, and
+ /// GlobalIsNeeded - mark the specific global value as needed, and
/// recursively mark anything that it uses as also needed.
void GlobalIsNeeded(GlobalValue *GV);
void MarkUsedGlobalsAsNeeded(Constant *C);
- bool SafeToDestroyConstant(Constant* C);
bool RemoveUnusedGlobalValue(GlobalValue &GV);
};
}
bool GlobalDCE::runOnModule(Module &M) {
bool Changed = false;
+
// Loop over the module, adding globals which are obviously necessary.
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Changed |= RemoveUnusedGlobalValue(*I);
// Functions with external linkage are needed if they have a body
- if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
- !I->isDeclaration())
+ if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
+ !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
GlobalIsNeeded(I);
}
Changed |= RemoveUnusedGlobalValue(*I);
// Externally visible & appending globals are needed, if they have an
// initializer.
- if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
- !I->isDeclaration())
+ if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
+ !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
GlobalIsNeeded(I);
}
-
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
- // Aliases are always needed even if they are not used.
- MarkUsedGlobalsAsNeeded(I->getAliasee());
+ Changed |= RemoveUnusedGlobalValue(*I);
+ // Externally visible aliases are needed.
+ if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
+ GlobalIsNeeded(I);
}
// Now that all globals which are needed are in the AliveGlobals set, we loop
I->setInitializer(0);
}
-
// The second pass drops the bodies of functions which are dead...
std::vector<Function*> DeadFunctions;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
I->deleteBody();
}
+ // The third pass drops targets of aliases which are dead...
+ std::vector<GlobalAlias*> DeadAliases;
+ for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
+ ++I)
+ if (!AliveGlobals.count(I)) {
+ DeadAliases.push_back(I);
+ I->setAliasee(0);
+ }
+
if (!DeadFunctions.empty()) {
- // Now that all interreferences have been dropped, delete the actual objects
+ // Now that all interferences have been dropped, delete the actual objects
// themselves.
for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
RemoveUnusedGlobalValue(*DeadFunctions[i]);
Changed = true;
}
+ // Now delete any dead aliases.
+ if (!DeadAliases.empty()) {
+ for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
+ RemoveUnusedGlobalValue(*DeadAliases[i]);
+ M.getAliasList().erase(DeadAliases[i]);
+ }
+ NumAliases += DeadAliases.size();
+ Changed = true;
+ }
+
// Make sure that all memory is released
AliveGlobals.clear();
+
+ // Remove dead metadata.
+ Changed |= M.getContext().RemoveDeadMetadata();
return Changed;
}
// referenced by the initializer to the alive set.
if (GV->hasInitializer())
MarkUsedGlobalsAsNeeded(GV->getInitializer());
- } else if (!isa<GlobalAlias>(G)) {
+ } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
+ // The target of a global alias is needed.
+ MarkUsedGlobalsAsNeeded(GA->getAliasee());
+ } else {
// Otherwise this must be a function object. We have to scan the body of
// the function looking for constants and global values which are used as
// operands. Any operands of these types must be processed to ensure that
GV.removeDeadConstantUsers();
return GV.use_empty();
}
-
-// SafeToDestroyConstant - It is safe to destroy a constant iff it is only used
-// by constants itself. Note that constants cannot be cyclic, so this test is
-// pretty easy to implement recursively.
-//
-bool GlobalDCE::SafeToDestroyConstant(Constant *C) {
- for (Value::use_iterator I = C->use_begin(), E = C->use_end(); I != E; ++I)
- if (Constant *User = dyn_cast<Constant>(*I)) {
- if (!SafeToDestroyConstant(User)) return false;
- } else {
- return false;
- }
- return true;
-}