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
|
|
|
|
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
|
2007-05-01 21:15:47 +00:00
|
|
|
GlobalDCE() : ModulePass((intptr_t)&ID) {}
|
|
|
|
|
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
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
/// MarkGlobalIsNeeded - the specific global value as needed, and
|
|
|
|
/// 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 SafeToDestroyConstant(Constant* C);
|
|
|
|
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;
|
|
|
|
// 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
|
2003-09-20 19:00:50 +00:00
|
|
|
if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
|
2007-01-30 20:08:39 +00:00
|
|
|
!I->isDeclaration())
|
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.
|
|
|
|
if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
|
2007-01-30 20:08:39 +00:00
|
|
|
!I->isDeclaration())
|
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) {
|
|
|
|
// Aliases are always needed even if they are not used.
|
2007-04-29 18:02:48 +00:00
|
|
|
MarkUsedGlobalsAsNeeded(I->getAliasee());
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DeadFunctions.empty()) {
|
|
|
|
// Now that all interreferences have been dropped, delete the actual objects
|
|
|
|
// 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
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
// Make sure that all memory is released
|
|
|
|
AliveGlobals.clear();
|
|
|
|
return Changed;
|
2002-08-18 01:28:30 +00:00
|
|
|
}
|
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
/// MarkGlobalIsNeeded - the specific global value as needed, and
|
|
|
|
/// 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());
|
2007-04-28 13:45:00 +00:00
|
|
|
} else if (!isa<GlobalAlias>(G)) {
|
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();
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-08-18 01:28:30 +00:00
|
|
|
// 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;
|
|
|
|
}
|