//
// 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.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#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");
namespace {
struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
static char ID; // Pass identification, replacement for typeid
- GlobalDCE() : ModulePass((intptr_t)&ID) {}
-
+ 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;
- /// MarkGlobalIsNeeded - 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);
};
- char GlobalDCE::ID = 0;
- RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
}
+char GlobalDCE::ID = 0;
+static RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
+
ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
bool GlobalDCE::runOnModule(Module &M) {
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()) &&
+ if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
!I->isDeclaration())
GlobalIsNeeded(I);
}
Changed |= RemoveUnusedGlobalValue(*I);
// Externally visible & appending globals are needed, if they have an
// initializer.
- if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
+ if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
!I->isDeclaration())
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();
return Changed;
}
-/// MarkGlobalIsNeeded - the specific global value as needed, and
+/// GlobalIsNeeded - the specific global value as needed, and
/// recursively mark anything that it uses as also needed.
void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
- std::set<GlobalValue*>::iterator I = AliveGlobals.lower_bound(G);
+ std::set<GlobalValue*>::iterator I = AliveGlobals.find(G);
// If the global is already in the set, no need to reprocess it.
- if (I != AliveGlobals.end() && *I == G) return;
+ if (I != AliveGlobals.end()) return;
// Otherwise insert it now, so we do not infinitely recurse
AliveGlobals.insert(I, G);
// 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