2002-04-08 04:49:59 +08:00
|
|
|
//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-27 02:42:17 +08:00
|
|
|
//
|
2003-09-17 03:27:31 +08: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-27 02:42:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
#define DEBUG_TYPE "globaldce"
|
2002-07-25 01:12:05 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-08-18 09:28:30 +08:00
|
|
|
#include "llvm/Constants.h"
|
2003-09-17 03:27:31 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2009-11-02 03:03:42 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2009-01-06 04:39:50 +08:00
|
|
|
STATISTIC(NumAliases , "Number of global aliases removed");
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumFunctions, "Number of functions removed");
|
|
|
|
STATISTIC(NumVariables, "Number of global variables removed");
|
2002-07-18 12:43:20 +08:00
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
struct GlobalDCE : public ModulePass {
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-08-07 02:33:48 +08:00
|
|
|
GlobalDCE() : ModulePass(ID) {}
|
2009-01-06 04:37:33 +08:00
|
|
|
|
2002-02-27 05:46:54 +08:00
|
|
|
// run - Do the GlobalDCE pass on the specified module, optionally updating
|
|
|
|
// the specified callgraph to reflect the changes.
|
|
|
|
//
|
2004-09-20 12:48:05 +08:00
|
|
|
bool runOnModule(Module &M);
|
2002-08-18 09:28:30 +08:00
|
|
|
|
|
|
|
private:
|
2009-11-02 03:03:42 +08:00
|
|
|
SmallPtrSet<GlobalValue*, 32> AliveGlobals;
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2009-01-06 04:37:33 +08:00
|
|
|
/// GlobalIsNeeded - mark the specific global value as needed, and
|
2003-09-17 03:27:31 +08:00
|
|
|
/// recursively mark anything that it uses as also needed.
|
|
|
|
void GlobalIsNeeded(GlobalValue *GV);
|
|
|
|
void MarkUsedGlobalsAsNeeded(Constant *C);
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2004-07-18 08:25:04 +08:00
|
|
|
bool RemoveUnusedGlobalValue(GlobalValue &GV);
|
2002-02-27 05:46:54 +08:00
|
|
|
};
|
2001-11-27 02:42:17 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char GlobalDCE::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(GlobalDCE, "globaldce",
|
|
|
|
"Dead Global Elimination", false, false);
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
bool GlobalDCE::runOnModule(Module &M) {
|
2003-09-17 03:27:31 +08:00
|
|
|
bool Changed = false;
|
2009-07-17 02:04:31 +08:00
|
|
|
|
2003-09-17 03:27:31 +08: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 08:25:04 +08:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
2003-09-17 03:27:31 +08:00
|
|
|
// Functions with external linkage are needed if they have a body
|
2009-04-13 13:38:23 +08:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
2009-05-23 22:06:57 +08:00
|
|
|
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
2003-09-17 03:27:31 +08:00
|
|
|
GlobalIsNeeded(I);
|
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2007-04-25 22:27:10 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2004-07-18 08:25:04 +08:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
2003-09-21 03:00:50 +08:00
|
|
|
// Externally visible & appending globals are needed, if they have an
|
|
|
|
// initializer.
|
2009-04-13 13:38:23 +08:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
2009-05-23 22:06:57 +08:00
|
|
|
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
2003-09-17 03:27:31 +08:00
|
|
|
GlobalIsNeeded(I);
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
|
|
|
|
2007-04-25 22:27:10 +08:00
|
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
|
|
|
I != E; ++I) {
|
2009-01-06 04:37:33 +08:00
|
|
|
Changed |= RemoveUnusedGlobalValue(*I);
|
|
|
|
// Externally visible aliases are needed.
|
2009-02-18 07:05:26 +08:00
|
|
|
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
|
2009-01-06 04:37:33 +08:00
|
|
|
GlobalIsNeeded(I);
|
2007-04-25 22:27:10 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 03:27:31 +08: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 09:28:30 +08:00
|
|
|
//
|
|
|
|
|
2003-09-17 03:27:31 +08: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-02 03:03:42 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
2003-09-17 03:27:31 +08:00
|
|
|
if (!AliveGlobals.count(I)) {
|
|
|
|
DeadGlobalVars.push_back(I); // Keep track of dead globals
|
|
|
|
I->setInitializer(0);
|
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2003-09-17 03:27:31 +08: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-31 04:08:39 +08:00
|
|
|
if (!I->isDeclaration())
|
2003-09-17 03:27:31 +08:00
|
|
|
I->deleteBody();
|
|
|
|
}
|
|
|
|
|
2009-02-18 07:05:26 +08: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-17 03:27:31 +08:00
|
|
|
if (!DeadFunctions.empty()) {
|
2009-01-06 04:37:33 +08:00
|
|
|
// Now that all interferences have been dropped, delete the actual objects
|
2003-09-17 03:27:31 +08:00
|
|
|
// themselves.
|
|
|
|
for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
|
2004-07-18 08:25:04 +08:00
|
|
|
RemoveUnusedGlobalValue(*DeadFunctions[i]);
|
2003-09-17 03:27:31 +08:00
|
|
|
M.getFunctionList().erase(DeadFunctions[i]);
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
2003-09-17 03:27:31 +08:00
|
|
|
NumFunctions += DeadFunctions.size();
|
|
|
|
Changed = true;
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
2003-09-17 03:27:31 +08:00
|
|
|
|
|
|
|
if (!DeadGlobalVars.empty()) {
|
|
|
|
for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
|
2004-07-18 08:25:04 +08:00
|
|
|
RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
|
2003-09-17 03:27:31 +08:00
|
|
|
M.getGlobalList().erase(DeadGlobalVars[i]);
|
|
|
|
}
|
|
|
|
NumVariables += DeadGlobalVars.size();
|
|
|
|
Changed = true;
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2009-01-06 04:37:33 +08:00
|
|
|
// Now delete any dead aliases.
|
2009-02-18 07:05:26 +08:00
|
|
|
if (!DeadAliases.empty()) {
|
|
|
|
for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
|
|
|
|
RemoveUnusedGlobalValue(*DeadAliases[i]);
|
|
|
|
M.getAliasList().erase(DeadAliases[i]);
|
2009-01-06 04:37:33 +08:00
|
|
|
}
|
2009-02-18 07:05:26 +08:00
|
|
|
NumAliases += DeadAliases.size();
|
|
|
|
Changed = true;
|
2009-01-06 04:37:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 03:27:31 +08:00
|
|
|
// Make sure that all memory is released
|
|
|
|
AliveGlobals.clear();
|
2009-08-11 14:31:57 +08:00
|
|
|
|
2003-09-17 03:27:31 +08:00
|
|
|
return Changed;
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 03:16:41 +08:00
|
|
|
/// GlobalIsNeeded - the specific global value as needed, and
|
2003-09-17 03:27:31 +08: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-02 03:03:42 +08:00
|
|
|
if (!AliveGlobals.insert(G))
|
|
|
|
return;
|
|
|
|
|
2003-09-17 03:27:31 +08: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-06 04:37:33 +08:00
|
|
|
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
|
|
|
|
// The target of a global alias is needed.
|
2009-01-08 02:45:53 +08:00
|
|
|
MarkUsedGlobalsAsNeeded(GA->getAliasee());
|
2009-01-06 04:37:33 +08:00
|
|
|
} else {
|
2003-09-17 03:27:31 +08: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-02 03:03:42 +08:00
|
|
|
|
2003-09-17 03:27:31 +08: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-22 07:48:37 +08:00
|
|
|
MarkUsedGlobalsAsNeeded(C);
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-17 03:27:31 +08:00
|
|
|
void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
|
2004-07-18 08:25:04 +08:00
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
|
2009-10-29 09:21:20 +08: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-17 03:27:31 +08:00
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2004-07-18 08:25:04 +08:00
|
|
|
// RemoveUnusedGlobalValue - Loop over all of the uses of the specified
|
2002-08-18 09:28:30 +08: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 08:25:04 +08:00
|
|
|
bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
|
2004-07-18 15:22:58 +08:00
|
|
|
if (GV.use_empty()) return false;
|
|
|
|
GV.removeDeadConstantUsers();
|
|
|
|
return GV.use_empty();
|
|
|
|
}
|