Index: include/llvm/Transforms/IPO/GlobalDCE.h =================================================================== --- include/llvm/Transforms/IPO/GlobalDCE.h +++ include/llvm/Transforms/IPO/GlobalDCE.h @@ -18,10 +18,10 @@ #ifndef LLVM_TRANSFORMS_IPO_GLOBALDCE_H #define LLVM_TRANSFORMS_IPO_GLOBALDCE_H +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/IR/Module.h" #include "llvm/IR/PassManager.h" -#include "llvm/ADT/SmallSet.h" -#include "llvm/ADT/DenseMap.h" #include namespace llvm { @@ -33,15 +33,20 @@ private: SmallPtrSet AliveGlobals; - DenseMap> GVDependencies; // Global -> Global that uses this global. - std::unordered_map> ConstantDependenciesCache; // Constant -> Globals that use this global cache. - std::unordered_multimap ComdatMembers; // Comdat -> Globals in that Comdat section. + DenseMap> + GVDependencies; // Global -> Global that uses this global. + std::unordered_map> + ConstantDependenciesCache; // Constant -> Globals that use this global + // cache. + std::unordered_multimap + ComdatMembers; // Comdat -> Globals in that Comdat section. void UpdateGVDependencies(GlobalValue &GV); - void MarkLive(GlobalValue &GV, SmallVectorImpl* Updates=nullptr); + void MarkLive(GlobalValue &GV, + SmallVectorImpl *Updates = nullptr); bool RemoveUnusedGlobalValue(GlobalValue &GV); - void ComputeDependencies(Value *V, SmallPtrSetImpl& U); + void ComputeDependencies(Value *V, SmallPtrSetImpl &U); }; } Index: lib/Transforms/IPO/GlobalDCE.cpp =================================================================== --- lib/Transforms/IPO/GlobalDCE.cpp +++ lib/Transforms/IPO/GlobalDCE.cpp @@ -80,24 +80,22 @@ // Compute the set of GlobalValue that depends from V // The recursion stops as soon as a GlobalValue is met. -void GlobalDCEPass::ComputeDependencies(Value* V, SmallPtrSetImpl &Deps) { - if(auto *I = dyn_cast(V)) { - Function* Parent = I->getParent()->getParent(); +void GlobalDCEPass::ComputeDependencies(Value *V, + SmallPtrSetImpl &Deps) { + if (auto *I = dyn_cast(V)) { + Function *Parent = I->getParent()->getParent(); Deps.insert(Parent); - } - else if(auto* GV = dyn_cast(V)) { + } else if (auto *GV = dyn_cast(V)) { Deps.insert(GV); - } - else if(auto* CE = dyn_cast(V)) { + } else if (auto *CE = dyn_cast(V)) { // Avoid walking the whole tree of a big ConstantExprs multiple times. auto Where = ConstantDependenciesCache.find(CE); - if(Where != ConstantDependenciesCache.end()) { + if (Where != ConstantDependenciesCache.end()) { auto const &K = Where->second; Deps.insert(K.begin(), K.end()); - } - else { + } else { auto &LocalDeps = ConstantDependenciesCache[CE]; - for(auto* CEUser : CE->users()) + for (auto *CEUser : CE->users()) ComputeDependencies(CEUser, LocalDeps); Deps.insert(LocalDeps.begin(), LocalDeps.end()); } @@ -105,26 +103,28 @@ } void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) { - SmallPtrSet Deps; - for(auto* User: GV.users()) + SmallPtrSet Deps; + for (auto *User : GV.users()) ComputeDependencies(User, Deps); Deps.erase(&GV); // Remove self-reference. - for(auto* GVU : Deps) { + for (auto *GVU : Deps) { GVDependencies[GVU].push_back(&GV); } } // Mark Global value as Live -void GlobalDCEPass::MarkLive(GlobalValue &GV, SmallVectorImpl* Updates) { +void GlobalDCEPass::MarkLive(GlobalValue &GV, + SmallVectorImpl *Updates) { auto const Ret = AliveGlobals.insert(&GV); - if(!Ret.second) + if (!Ret.second) return; - if(Updates) + if (Updates) Updates->push_back(&GV); if (Comdat *C = GV.getComdat()) { for (auto &&CM : make_range(ComdatMembers.equal_range(C))) - MarkLive(*CM.second, Updates); // Recursion depth is only two because only globals in the same comdat are visited. + MarkLive(*CM.second, Updates); // Recursion depth is only two because only + // globals in the same comdat are visited. } } @@ -159,7 +159,6 @@ UpdateGVDependencies(GO); } - // Compute direct dependencies of aliases. for (GlobalAlias &GA : M.aliases()) { Changed |= RemoveUnusedGlobalValue(GA); @@ -180,15 +179,16 @@ UpdateGVDependencies(GIF); } - // Propagate liveness from collected Global Values through the computed dependencies. - SmallVector NewLiveGVs{AliveGlobals.begin(), AliveGlobals.end()}; - while(!NewLiveGVs.empty()) { - auto * LGV = NewLiveGVs.pop_back_val(); - for(auto* GV : GVDependencies[LGV]) + // Propagate liveness from collected Global Values through the computed + // dependencies. + SmallVector NewLiveGVs{AliveGlobals.begin(), + AliveGlobals.end()}; + while (!NewLiveGVs.empty()) { + auto *LGV = NewLiveGVs.pop_back_val(); + for (auto *GV : GVDependencies[LGV]) MarkLive(*GV, &NewLiveGVs); } - // Now that all globals which are needed are in the AliveGlobals set, we loop // through the program, deleting those which are not alive. //