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"
|
2009-11-01 19:03:42 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
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 {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct GlobalDCE : public ModulePass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
GlobalDCE() : ModulePass(ID) {
|
|
|
|
initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
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:
|
2009-11-01 19:03:42 +00:00
|
|
|
SmallPtrSet<GlobalValue*, 32> 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;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(GlobalDCE, "globaldce",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Dead Global Elimination", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
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
|
2012-06-14 22:48:13 +00:00
|
|
|
if (!I->isDiscardableIfUnused() &&
|
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.
|
2012-06-14 22:48:13 +00:00
|
|
|
if (!I->isDiscardableIfUnused() &&
|
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.
|
2012-06-14 22:48:13 +00:00
|
|
|
if (!I->isDiscardableIfUnused())
|
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
|
2009-11-01 19:03:42 +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
|
|
|
|
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) {
|
|
|
|
// If the global is already in the set, no need to reprocess it.
|
2009-11-01 19:03:42 +00:00
|
|
|
if (!AliveGlobals.insert(G))
|
|
|
|
return;
|
|
|
|
|
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);
|
2009-11-01 19:03:42 +00:00
|
|
|
|
2003-09-16 19:27:31 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
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))
|
2009-10-29 01:21:20 +00:00
|
|
|
return GlobalIsNeeded(GV);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if (Constant *OpC = dyn_cast<Constant>(*I))
|
|
|
|
MarkUsedGlobalsAsNeeded(OpC);
|
2003-09-16 19:27:31 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|