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