2002-04-07 20:49:59 +00:00
|
|
|
//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-26 18:42:17 +00:00
|
|
|
//
|
2003-09-16 19:27:31 +00:00
|
|
|
// This transform is designed to eliminate unreachable internal globals from the
|
|
|
|
// program. It uses an aggressive algorithm, searching out globals that are
|
|
|
|
// known to be alive. After it finds all of the globals which are needed, it
|
|
|
|
// deletes whatever is left over. This allows it to delete recursive chunks of
|
|
|
|
// the program which are unreachable.
|
2001-11-26 18:42:17 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
#define DEBUG_TYPE "globaldce"
|
2002-07-24 17:12:05 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-08-18 01:28:30 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-09-16 19:27:31 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2003-09-16 19:27:31 +00:00
|
|
|
#include <set>
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2009-01-05 20:39:50 +00:00
|
|
|
STATISTIC(NumAliases , "Number of global aliases removed");
|
2006-12-19 22:09:18 +00:00
|
|
|
STATISTIC(NumFunctions, "Number of functions removed");
|
|
|
|
STATISTIC(NumVariables, "Number of global variables removed");
|
2002-07-18 04:43:20 +00:00
|
|
|
|
2006-12-19 22:09:18 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
GlobalDCE() : ModulePass(&ID) {}
|
2009-01-05 20:37:33 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
// run - Do the GlobalDCE pass on the specified module, optionally updating
|
|
|
|
// the specified callgraph to reflect the changes.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M);
|
2002-08-18 01:28:30 +00:00
|
|
|
|
|
|
|
private:
|
2003-09-16 19:27:31 +00:00
|
|
|
std::set<GlobalValue*> AliveGlobals;
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2009-01-05 20:37:33 +00:00
|
|
|
/// GlobalIsNeeded - mark the specific global value as needed, and
|
2003-09-16 19:27:31 +00:00
|
|
|
/// recursively mark anything that it uses as also needed.
|
|
|
|
void GlobalIsNeeded(GlobalValue *GV);
|
|
|
|
void MarkUsedGlobalsAsNeeded(Constant *C);
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2004-07-18 00:25:04 +00:00
|
|
|
bool RemoveUnusedGlobalValue(GlobalValue &GV);
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
2001-11-26 18:42:17 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char GlobalDCE::ID = 0;
|
|
|
|
static RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool GlobalDCE::runOnModule(Module &M) {
|
2003-09-16 19:27:31 +00:00
|
|
|
bool Changed = false;
|
2009-07-16 18:04:31 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// Loop over the module, adding globals which are obviously necessary.
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2004-07-18 00:25:04 +00:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
2003-09-16 19:27:31 +00:00
|
|
|
// Functions with external linkage are needed if they have a body
|
2009-04-13 05:38:23 +00:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
2009-05-23 14:06:57 +00:00
|
|
|
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
2003-09-16 19:27:31 +00:00
|
|
|
GlobalIsNeeded(I);
|
|
|
|
}
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2007-04-25 14:27:10 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2004-07-18 00:25:04 +00:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
2003-09-20 19:00:50 +00:00
|
|
|
// Externally visible & appending globals are needed, if they have an
|
|
|
|
// initializer.
|
2009-04-13 05:38:23 +00:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
2009-05-23 14:06:57 +00:00
|
|
|
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
2003-09-16 19:27:31 +00:00
|
|
|
GlobalIsNeeded(I);
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
|
|
|
|
2007-04-25 14:27:10 +00:00
|
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
|
|
|
I != E; ++I) {
|
2009-01-05 20:37:33 +00:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
|
|
|
// Externally visible aliases are needed.
|
2009-02-17 23:05:26 +00:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
|
2009-01-05 20:37:33 +00:00
|
|
|
GlobalIsNeeded(I);
|
2007-04-25 14:27:10 +00:00
|
|
|
}
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// Now that all globals which are needed are in the AliveGlobals set, we loop
|
|
|
|
// through the program, deleting those which are not alive.
|
2002-08-18 01:28:30 +00:00
|
|
|
//
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// The first pass is to drop initializers of global variables which are dead.
|
|
|
|
std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2003-09-16 19:27:31 +00:00
|
|
|
if (!AliveGlobals.count(I)) {
|
|
|
|
DeadGlobalVars.push_back(I); // Keep track of dead globals
|
|
|
|
I->setInitializer(0);
|
|
|
|
}
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// 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)
|
|
|
|
if (!AliveGlobals.count(I)) {
|
|
|
|
DeadFunctions.push_back(I); // Keep track of dead globals
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration())
|
2003-09-16 19:27:31 +00:00
|
|
|
I->deleteBody();
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:05:26 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
if (!DeadFunctions.empty()) {
|
2009-01-05 20:37:33 +00:00
|
|
|
// Now that all interferences have been dropped, delete the actual objects
|
2003-09-16 19:27:31 +00:00
|
|
|
// themselves.
|
|
|
|
for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
|
2004-07-18 00:25:04 +00:00
|
|
|
RemoveUnusedGlobalValue(*DeadFunctions[i]);
|
2003-09-16 19:27:31 +00:00
|
|
|
M.getFunctionList().erase(DeadFunctions[i]);
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
2003-09-16 19:27:31 +00:00
|
|
|
NumFunctions += DeadFunctions.size();
|
|
|
|
Changed = true;
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
2003-09-16 19:27:31 +00:00
|
|
|
|
|
|
|
if (!DeadGlobalVars.empty()) {
|
|
|
|
for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
|
2004-07-18 00:25:04 +00:00
|
|
|
RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
|
2003-09-16 19:27:31 +00:00
|
|
|
M.getGlobalList().erase(DeadGlobalVars[i]);
|
|
|
|
}
|
|
|
|
NumVariables += DeadGlobalVars.size();
|
|
|
|
Changed = true;
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2009-01-05 20:37:33 +00:00
|
|
|
// Now delete any dead aliases.
|
2009-02-17 23:05:26 +00:00
|
|
|
if (!DeadAliases.empty()) {
|
|
|
|
for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
|
|
|
|
RemoveUnusedGlobalValue(*DeadAliases[i]);
|
|
|
|
M.getAliasList().erase(DeadAliases[i]);
|
2009-01-05 20:37:33 +00:00
|
|
|
}
|
2009-02-17 23:05:26 +00:00
|
|
|
NumAliases += DeadAliases.size();
|
|
|
|
Changed = true;
|
2009-01-05 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// Make sure that all memory is released
|
|
|
|
AliveGlobals.clear();
|
2009-08-11 06:31:57 +00:00
|
|
|
|
|
|
|
// Remove dead metadata.
|
|
|
|
Changed |= M.getContext().RemoveDeadMetadata();
|
2003-09-16 19:27:31 +00:00
|
|
|
return Changed;
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
|
|
|
|
2008-11-11 19:16:41 +00:00
|
|
|
/// GlobalIsNeeded - the specific global value as needed, and
|
2003-09-16 19:27:31 +00:00
|
|
|
/// recursively mark anything that it uses as also needed.
|
|
|
|
void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
|
2008-07-11 20:58:19 +00:00
|
|
|
std::set<GlobalValue*>::iterator I = AliveGlobals.find(G);
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// If the global is already in the set, no need to reprocess it.
|
2008-07-11 20:58:19 +00:00
|
|
|
if (I != AliveGlobals.end()) return;
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// Otherwise insert it now, so we do not infinitely recurse
|
|
|
|
AliveGlobals.insert(I, G);
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
|
|
|
|
// If this is a global variable, we must make sure to add any global values
|
|
|
|
// referenced by the initializer to the alive set.
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
MarkUsedGlobalsAsNeeded(GV->getInitializer());
|
2009-01-05 20:37:33 +00:00
|
|
|
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
|
|
|
|
// The target of a global alias is needed.
|
2009-01-07 18:45:53 +00:00
|
|
|
MarkUsedGlobalsAsNeeded(GA->getAliasee());
|
2009-01-05 20:37:33 +00:00
|
|
|
} else {
|
2003-09-16 19:27:31 +00:00
|
|
|
// 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
|
|
|
|
// any globals used will be marked as needed.
|
|
|
|
Function *F = cast<Function>(G);
|
|
|
|
// For all basic blocks...
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
// For all instructions...
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
// For all operands...
|
|
|
|
for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
|
|
|
|
GlobalIsNeeded(GV);
|
|
|
|
else if (Constant *C = dyn_cast<Constant>(*U))
|
2005-04-21 23:48:37 +00:00
|
|
|
MarkUsedGlobalsAsNeeded(C);
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
|
2004-07-18 00:25:04 +00:00
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
|
|
|
|
GlobalIsNeeded(GV);
|
2003-09-16 19:27:31 +00:00
|
|
|
else {
|
|
|
|
// Loop over all of the operands of the constant, adding any globals they
|
|
|
|
// use to the list of needed globals.
|
|
|
|
for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I)
|
|
|
|
MarkUsedGlobalsAsNeeded(cast<Constant>(*I));
|
|
|
|
}
|
|
|
|
}
|
2002-08-18 01:28:30 +00:00
|
|
|
|
2004-07-18 00:25:04 +00:00
|
|
|
// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
|
2002-08-18 01:28:30 +00:00
|
|
|
// GlobalValue, looking for the constant pointer ref that may be pointing to it.
|
|
|
|
// If found, check to see if the constant pointer ref is safe to destroy, and if
|
|
|
|
// so, nuke it. This will reduce the reference count on the global value, which
|
|
|
|
// might make it deader.
|
|
|
|
//
|
2004-07-18 00:25:04 +00:00
|
|
|
bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
|
2004-07-18 07:22:58 +00:00
|
|
|
if (GV.use_empty()) return false;
|
|
|
|
GV.removeDeadConstantUsers();
|
|
|
|
return GV.use_empty();
|
|
|
|
}
|