30 #define DEBUG_TYPE "globaldce" 32 STATISTIC(NumAliases ,
"Number of global aliases removed");
33 STATISTIC(NumFunctions,
"Number of functions removed");
34 STATISTIC(NumIFuncs,
"Number of indirect functions removed");
35 STATISTIC(NumVariables,
"Number of global variables removed");
38 class GlobalDCELegacyPass :
public ModulePass {
48 bool runOnModule(
Module &M)
override {
60 auto PA = Impl.run(M, DummyMAM);
61 return !PA.areAllPreserved();
71 "Dead Global Elimination",
false,
false)
75 return new GlobalDCELegacyPass();
81 if (Entry.
size() != 1 || !isa<ReturnInst>(Entry.
front()))
89 void GlobalDCEPass::ComputeDependencies(
Value *V,
91 if (
auto *
I = dyn_cast<Instruction>(V)) {
92 Function *Parent =
I->getParent()->getParent();
94 }
else if (
auto *GV = dyn_cast<GlobalValue>(V)) {
96 }
else if (
auto *CE = dyn_cast<Constant>(V)) {
98 auto Where = ConstantDependenciesCache.find(CE);
99 if (Where != ConstantDependenciesCache.end()) {
100 auto const &K = Where->second;
101 Deps.
insert(K.begin(), K.end());
104 for (
User *CEUser : CE->users())
105 ComputeDependencies(CEUser, LocalDeps);
111 void GlobalDCEPass::UpdateGVDependencies(
GlobalValue &GV) {
114 ComputeDependencies(
User, Deps);
117 GVDependencies[GVU].insert(&GV);
124 auto const Ret = AliveGlobals.insert(&GV);
131 for (
auto &&CM :
make_range(ComdatMembers.equal_range(
C)))
132 MarkLive(*CM.second, Updates);
138 bool Changed =
false;
154 ComdatMembers.insert(std::make_pair(
C, &
F));
157 ComdatMembers.insert(std::make_pair(
C, &GV));
159 if (
Comdat *
C = GA.getComdat())
160 ComdatMembers.insert(std::make_pair(
C, &GA));
164 Changed |= RemoveUnusedGlobalValue(GO);
168 if (!GO.isDeclaration() && !GO.hasAvailableExternallyLinkage())
169 if (!GO.isDiscardableIfUnused())
172 UpdateGVDependencies(GO);
177 Changed |= RemoveUnusedGlobalValue(GA);
179 if (!GA.isDiscardableIfUnused())
182 UpdateGVDependencies(GA);
187 Changed |= RemoveUnusedGlobalValue(GIF);
189 if (!GIF.isDiscardableIfUnused())
192 UpdateGVDependencies(GIF);
199 while (!NewLiveGVs.empty()) {
201 for (
auto *GVD : GVDependencies[LGV])
202 MarkLive(*GVD, &NewLiveGVs);
210 std::vector<GlobalVariable *> DeadGlobalVars;
212 if (!AliveGlobals.count(&GV)) {
213 DeadGlobalVars.push_back(&GV);
214 if (GV.hasInitializer()) {
216 GV.setInitializer(
nullptr);
223 std::vector<Function *> DeadFunctions;
225 if (!AliveGlobals.count(&
F)) {
226 DeadFunctions.push_back(&
F);
227 if (!
F.isDeclaration())
232 std::vector<GlobalAlias*> DeadAliases;
234 if (!AliveGlobals.count(&GA)) {
235 DeadAliases.push_back(&GA);
236 GA.setAliasee(
nullptr);
240 std::vector<GlobalIFunc*> DeadIFuncs;
242 if (!AliveGlobals.count(&GIF)) {
243 DeadIFuncs.push_back(&GIF);
244 GIF.setResolver(
nullptr);
249 auto EraseUnusedGlobalValue = [&](
GlobalValue *GV) {
250 RemoveUnusedGlobalValue(*GV);
255 NumFunctions += DeadFunctions.size();
257 EraseUnusedGlobalValue(
F);
259 NumVariables += DeadGlobalVars.size();
261 EraseUnusedGlobalValue(GV);
263 NumAliases += DeadAliases.size();
265 EraseUnusedGlobalValue(GA);
267 NumIFuncs += DeadIFuncs.size();
269 EraseUnusedGlobalValue(GIF);
272 AliveGlobals.clear();
273 ConstantDependenciesCache.clear();
274 GVDependencies.clear();
275 ComdatMembers.clear();
288 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...
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
void initializeGlobalDCELegacyPassPass(PassRegistry &)
STATISTIC(NumFunctions, "Total number of functions")
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce", "Dead Global Elimination", false, false) ModulePass *llvm
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
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...
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
const BasicBlock & getEntryBlock() const
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const Instruction & front() const
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...
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 Comdat * getComdat() 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.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
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 retur...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)