31 #define DEBUG_TYPE "globaldce"
33 STATISTIC(NumAliases ,
"Number of global aliases removed");
34 STATISTIC(NumFunctions,
"Number of functions removed");
35 STATISTIC(NumIFuncs,
"Number of indirect functions removed");
36 STATISTIC(NumVariables,
"Number of global variables removed");
39 class GlobalDCELegacyPass :
public ModulePass {
49 bool runOnModule(
Module &M)
override {
61 auto PA = Impl.run(M, DummyMAM);
62 return !PA.areAllPreserved();
72 "Dead Global Elimination",
false,
false)
76 return new GlobalDCELegacyPass();
82 if (Entry.
size() != 1 || !isa<ReturnInst>(Entry.
front()))
90 void GlobalDCEPass::ComputeDependencies(
Value *V,
92 if (
auto *
I = dyn_cast<Instruction>(V)) {
93 Function *Parent =
I->getParent()->getParent();
95 }
else if (
auto *GV = dyn_cast<GlobalValue>(V)) {
97 }
else if (
auto *CE = dyn_cast<Constant>(V)) {
99 auto Where = ConstantDependenciesCache.find(CE);
100 if (Where != ConstantDependenciesCache.end()) {
101 auto const &K = Where->second;
102 Deps.
insert(K.begin(), K.end());
105 for (
User *CEUser :
CE->users())
106 ComputeDependencies(CEUser, LocalDeps);
112 void GlobalDCEPass::UpdateGVDependencies(
GlobalValue &GV) {
115 ComputeDependencies(
User, Deps);
118 GVDependencies.insert(std::make_pair(GVU, &GV));
125 auto const Ret = AliveGlobals.insert(&GV);
132 for (
auto &&CM :
make_range(ComdatMembers.equal_range(
C)))
133 MarkLive(*CM.second, Updates);
139 bool Changed =
false;
155 ComdatMembers.insert(std::make_pair(
C, &
F));
158 ComdatMembers.insert(std::make_pair(
C, &GV));
160 if (
Comdat *
C = GA.getComdat())
161 ComdatMembers.insert(std::make_pair(
C, &GA));
165 Changed |= RemoveUnusedGlobalValue(GO);
169 if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
170 if (!GO.isDiscardableIfUnused())
173 UpdateGVDependencies(GO);
178 Changed |= RemoveUnusedGlobalValue(GA);
180 if (!GA.isDiscardableIfUnused())
183 UpdateGVDependencies(GA);
188 Changed |= RemoveUnusedGlobalValue(GIF);
190 if (!GIF.isDiscardableIfUnused())
193 UpdateGVDependencies(GIF);
200 while (!NewLiveGVs.empty()) {
202 for (
auto &&GVD :
make_range(GVDependencies.equal_range(LGV)))
203 MarkLive(*GVD.second, &NewLiveGVs);
211 std::vector<GlobalVariable *> DeadGlobalVars;
213 if (!AliveGlobals.count(&GV)) {
214 DeadGlobalVars.push_back(&GV);
215 if (GV.hasInitializer()) {
217 GV.setInitializer(
nullptr);
224 std::vector<Function *> DeadFunctions;
226 if (!AliveGlobals.count(&
F)) {
227 DeadFunctions.push_back(&
F);
228 if (!
F.isDeclaration())
233 std::vector<GlobalAlias*> DeadAliases;
235 if (!AliveGlobals.count(&GA)) {
236 DeadAliases.push_back(&GA);
237 GA.setAliasee(
nullptr);
241 std::vector<GlobalIFunc*> DeadIFuncs;
243 if (!AliveGlobals.count(&GIF)) {
244 DeadIFuncs.push_back(&GIF);
245 GIF.setResolver(
nullptr);
250 auto EraseUnusedGlobalValue = [&](
GlobalValue *GV) {
251 RemoveUnusedGlobalValue(*GV);
256 NumFunctions += DeadFunctions.size();
258 EraseUnusedGlobalValue(
F);
260 NumVariables += DeadGlobalVars.size();
262 EraseUnusedGlobalValue(GV);
264 NumAliases += DeadAliases.size();
266 EraseUnusedGlobalValue(GA);
268 NumIFuncs += DeadIFuncs.size();
270 EraseUnusedGlobalValue(GIF);
273 AliveGlobals.clear();
274 ConstantDependenciesCache.clear();
275 GVDependencies.clear();
276 ComdatMembers.clear();
289 bool GlobalDCEPass::RemoveUnusedGlobalValue(
GlobalValue &GV) {
Return a value (possibly void), from a function.
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
void initializeGlobalDCELegacyPassPass(PassRegistry &)
const Instruction & front() const
const Comdat * getComdat() const
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Windows NT (Windows on ARM)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
Pass to remove unused function declarations.
ModulePass * createGlobalDCEPass()
createGlobalDCEPass - This transform is designed to eliminate unreachable internal globals (functions...
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
This is an important base class in LLVM.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
INITIALIZE_PASS(GlobalDCELegacyPass,"globaldce","Dead Global Elimination", false, false) ModulePass *llvm
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static bool isEmptyFunction(Function *F)
Returns true if F contains only a single "ret" instruction.
const BasicBlock & getEntryBlock() const
iterator_range< user_iterator > users()
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
void destroyConstant()
Called if some element of this constant is no longer valid.
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
LLVM Value Representation.
A container for analyses that lazily runs them and caches their results.
bool optimizeGlobalCtorsList(Module &M, function_ref< bool(Function *)> ShouldRemove)
Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it ret...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)