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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-05-03 02:35:25 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2014-05-03 02:35:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/CtorUtils.h"
|
2014-08-26 01:51:14 +08:00
|
|
|
#include "llvm/Transforms/Utils/GlobalStatus.h"
|
2015-03-20 02:23:29 +08:00
|
|
|
#include <unordered_map>
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "globaldce"
|
|
|
|
|
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");
|
2016-05-04 08:20:48 +08:00
|
|
|
STATISTIC(NumIFuncs, "Number of indirect functions removed");
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumVariables, "Number of global variables removed");
|
2002-07-18 12:43:20 +08:00
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
namespace {
|
2016-05-04 03:39:15 +08:00
|
|
|
class GlobalDCELegacyPass : public ModulePass {
|
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2016-05-04 03:39:15 +08:00
|
|
|
GlobalDCELegacyPass() : ModulePass(ID) {
|
|
|
|
initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
|
2010-10-20 01:21:58 +08:00
|
|
|
}
|
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.
|
|
|
|
//
|
2016-05-04 03:39:15 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto PA = Impl.run(M);
|
|
|
|
return !PA.areAllPreserved();
|
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
|
|
|
private:
|
2016-05-04 03:39:15 +08:00
|
|
|
GlobalDCEPass Impl;
|
|
|
|
};
|
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
char GlobalDCELegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
|
|
|
|
"Dead Global Elimination", false, false)
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
// Public interface to the GlobalDCEPass.
|
|
|
|
ModulePass *llvm::createGlobalDCEPass() {
|
|
|
|
return new GlobalDCELegacyPass();
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-05-03 02:35:25 +08:00
|
|
|
|
|
|
|
/// Returns true if F contains only a single "ret" instruction.
|
2014-05-06 09:44:26 +08:00
|
|
|
static bool isEmptyFunction(Function *F) {
|
2014-05-03 02:35:25 +08:00
|
|
|
BasicBlock &Entry = F->getEntryBlock();
|
|
|
|
if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
|
|
|
|
return false;
|
|
|
|
ReturnInst &RI = cast<ReturnInst>(Entry.front());
|
2014-06-09 06:29:17 +08:00
|
|
|
return RI.getReturnValue() == nullptr;
|
2014-05-03 02:35:25 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
PreservedAnalyses GlobalDCEPass::run(Module &M) {
|
2003-09-17 03:27:31 +08:00
|
|
|
bool Changed = false;
|
2014-05-03 02:35:25 +08:00
|
|
|
|
|
|
|
// Remove empty functions from the global ctors list.
|
2014-05-06 09:44:26 +08:00
|
|
|
Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
|
2014-05-03 02:35:25 +08:00
|
|
|
|
2015-03-20 02:23:29 +08:00
|
|
|
// Collect the set of members for each comdat.
|
|
|
|
for (Function &F : M)
|
|
|
|
if (Comdat *C = F.getComdat())
|
|
|
|
ComdatMembers.insert(std::make_pair(C, &F));
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
if (Comdat *C = GV.getComdat())
|
|
|
|
ComdatMembers.insert(std::make_pair(C, &GV));
|
|
|
|
for (GlobalAlias &GA : M.aliases())
|
|
|
|
if (Comdat *C = GA.getComdat())
|
|
|
|
ComdatMembers.insert(std::make_pair(C, &GA));
|
|
|
|
|
2003-09-17 03:27:31 +08:00
|
|
|
// Loop over the module, adding globals which are obviously necessary.
|
2015-07-19 03:57:34 +08:00
|
|
|
for (Function &F : M) {
|
|
|
|
Changed |= RemoveUnusedGlobalValue(F);
|
2003-09-17 03:27:31 +08:00
|
|
|
// Functions with external linkage are needed if they have a body
|
2015-07-19 03:57:34 +08:00
|
|
|
if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage())
|
|
|
|
if (!F.isDiscardableIfUnused())
|
|
|
|
GlobalIsNeeded(&F);
|
2003-09-17 03:27:31 +08:00
|
|
|
}
|
2002-08-18 09:28:30 +08:00
|
|
|
|
2015-07-19 03:57:34 +08:00
|
|
|
for (GlobalVariable &GV : M.globals()) {
|
|
|
|
Changed |= RemoveUnusedGlobalValue(GV);
|
2003-09-21 03:00:50 +08:00
|
|
|
// Externally visible & appending globals are needed, if they have an
|
|
|
|
// initializer.
|
2015-07-19 03:57:34 +08:00
|
|
|
if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
|
|
|
|
if (!GV.isDiscardableIfUnused())
|
|
|
|
GlobalIsNeeded(&GV);
|
2002-08-18 09:28:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-19 03:57:34 +08:00
|
|
|
for (GlobalAlias &GA : M.aliases()) {
|
|
|
|
Changed |= RemoveUnusedGlobalValue(GA);
|
2009-01-06 04:37:33 +08:00
|
|
|
// Externally visible aliases are needed.
|
2015-07-19 03:57:34 +08:00
|
|
|
if (!GA.isDiscardableIfUnused())
|
|
|
|
GlobalIsNeeded(&GA);
|
2007-04-25 22:27:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:20:48 +08:00
|
|
|
for (GlobalIFunc &GIF : M.ifuncs()) {
|
|
|
|
Changed |= RemoveUnusedGlobalValue(GIF);
|
|
|
|
// Externally visible ifuncs are needed.
|
|
|
|
if (!GIF.isDiscardableIfUnused())
|
|
|
|
GlobalIsNeeded(&GIF);
|
|
|
|
}
|
|
|
|
|
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.
|
2015-07-19 03:57:34 +08:00
|
|
|
std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
if (!AliveGlobals.count(&GV)) {
|
|
|
|
DeadGlobalVars.push_back(&GV); // Keep track of dead globals
|
|
|
|
if (GV.hasInitializer()) {
|
|
|
|
Constant *Init = GV.getInitializer();
|
|
|
|
GV.setInitializer(nullptr);
|
2014-08-26 01:51:14 +08:00
|
|
|
if (isSafeToDestroyConstant(Init))
|
|
|
|
Init->destroyConstant();
|
|
|
|
}
|
2003-09-17 03:27:31 +08:00
|
|
|
}
|
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...
|
2015-07-19 03:57:34 +08:00
|
|
|
std::vector<Function *> DeadFunctions;
|
|
|
|
for (Function &F : M)
|
|
|
|
if (!AliveGlobals.count(&F)) {
|
|
|
|
DeadFunctions.push_back(&F); // Keep track of dead globals
|
|
|
|
if (!F.isDeclaration())
|
|
|
|
F.deleteBody();
|
2003-09-17 03:27:31 +08:00
|
|
|
}
|
|
|
|
|
2009-02-18 07:05:26 +08:00
|
|
|
// The third pass drops targets of aliases which are dead...
|
|
|
|
std::vector<GlobalAlias*> DeadAliases;
|
2015-07-19 03:57:34 +08:00
|
|
|
for (GlobalAlias &GA : M.aliases())
|
|
|
|
if (!AliveGlobals.count(&GA)) {
|
|
|
|
DeadAliases.push_back(&GA);
|
|
|
|
GA.setAliasee(nullptr);
|
2009-02-18 07:05:26 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:20:48 +08:00
|
|
|
// The third pass drops targets of ifuncs which are dead...
|
|
|
|
std::vector<GlobalIFunc*> DeadIFuncs;
|
|
|
|
for (GlobalIFunc &GIF : M.ifuncs())
|
|
|
|
if (!AliveGlobals.count(&GIF)) {
|
|
|
|
DeadIFuncs.push_back(&GIF);
|
|
|
|
GIF.setResolver(nullptr);
|
|
|
|
}
|
|
|
|
|
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.
|
2015-07-19 03:57:34 +08:00
|
|
|
for (Function *F : DeadFunctions) {
|
|
|
|
RemoveUnusedGlobalValue(*F);
|
|
|
|
M.getFunctionList().erase(F);
|
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()) {
|
2015-07-19 03:57:34 +08:00
|
|
|
for (GlobalVariable *GV : DeadGlobalVars) {
|
|
|
|
RemoveUnusedGlobalValue(*GV);
|
|
|
|
M.getGlobalList().erase(GV);
|
2003-09-17 03:27:31 +08:00
|
|
|
}
|
|
|
|
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()) {
|
2015-07-19 03:57:34 +08:00
|
|
|
for (GlobalAlias *GA : DeadAliases) {
|
|
|
|
RemoveUnusedGlobalValue(*GA);
|
|
|
|
M.getAliasList().erase(GA);
|
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
|
|
|
}
|
|
|
|
|
2016-05-04 08:20:48 +08:00
|
|
|
// Now delete any dead aliases.
|
|
|
|
if (!DeadIFuncs.empty()) {
|
|
|
|
for (GlobalIFunc *GIF : DeadIFuncs) {
|
|
|
|
RemoveUnusedGlobalValue(*GIF);
|
|
|
|
M.getIFuncList().erase(GIF);
|
|
|
|
}
|
|
|
|
NumIFuncs += DeadIFuncs.size();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2003-09-17 03:27:31 +08:00
|
|
|
// Make sure that all memory is released
|
|
|
|
AliveGlobals.clear();
|
2013-04-13 20:53:18 +08:00
|
|
|
SeenConstants.clear();
|
2015-03-20 02:23:29 +08:00
|
|
|
ComdatMembers.clear();
|
2009-08-11 14:31:57 +08:00
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
if (Changed)
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
return PreservedAnalyses::all();
|
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.
|
2016-05-04 03:39:15 +08:00
|
|
|
void GlobalDCEPass::GlobalIsNeeded(GlobalValue *G) {
|
2003-09-17 03:27:31 +08:00
|
|
|
// If the global is already in the set, no need to reprocess it.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!AliveGlobals.insert(G).second)
|
2009-11-02 03:03:42 +08:00
|
|
|
return;
|
2014-10-07 15:07:19 +08:00
|
|
|
|
|
|
|
if (Comdat *C = G->getComdat()) {
|
2015-03-20 02:23:29 +08:00
|
|
|
for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
|
|
|
|
GlobalIsNeeded(CM.second);
|
2014-10-07 15:07:19 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2014-07-09 01:06:03 +08:00
|
|
|
if (GV->hasInitializer())
|
|
|
|
MarkUsedGlobalsAsNeeded(GV->getInitializer());
|
2016-04-05 16:47:51 +08:00
|
|
|
} else if (GlobalIndirectSymbol *GIS = dyn_cast<GlobalIndirectSymbol>(G)) {
|
|
|
|
// The target of a global alias or ifunc is needed.
|
|
|
|
MarkUsedGlobalsAsNeeded(GIS->getIndirectSymbol());
|
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
|
|
|
|
2015-12-19 16:52:49 +08:00
|
|
|
for (Use &U : F->operands())
|
|
|
|
MarkUsedGlobalsAsNeeded(cast<Constant>(U.get()));
|
2015-06-18 04:52:32 +08:00
|
|
|
|
2015-07-19 03:57:34 +08:00
|
|
|
for (BasicBlock &BB : *F)
|
|
|
|
for (Instruction &I : BB)
|
|
|
|
for (Use &U : I.operands())
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(U))
|
2003-09-17 03:27:31 +08:00
|
|
|
GlobalIsNeeded(GV);
|
2015-07-19 03:57:34 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 03:39:15 +08:00
|
|
|
void GlobalDCEPass::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);
|
2013-04-13 20:53:18 +08:00
|
|
|
|
2009-10-29 09:21:20 +08:00
|
|
|
// Loop over all of the operands of the constant, adding any globals they
|
|
|
|
// use to the list of needed globals.
|
2015-07-19 03:57:34 +08:00
|
|
|
for (Use &U : C->operands()) {
|
2013-04-13 20:53:18 +08:00
|
|
|
// If we've already processed this constant there's no need to do it again.
|
2015-07-19 03:57:34 +08:00
|
|
|
Constant *Op = dyn_cast<Constant>(U);
|
2014-11-19 15:49:26 +08:00
|
|
|
if (Op && SeenConstants.insert(Op).second)
|
2013-04-13 20:53:18 +08:00
|
|
|
MarkUsedGlobalsAsNeeded(Op);
|
|
|
|
}
|
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.
|
|
|
|
//
|
2016-05-04 03:39:15 +08:00
|
|
|
bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) {
|
2015-07-19 03:57:34 +08:00
|
|
|
if (GV.use_empty())
|
|
|
|
return false;
|
2004-07-18 15:22:58 +08:00
|
|
|
GV.removeDeadConstantUsers();
|
|
|
|
return GV.use_empty();
|
|
|
|
}
|