GlobalDCE.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This transform is designed to eliminate unreachable internal globals from the
  10. // program. It uses an aggressive algorithm, searching out globals that are
  11. // known to be alive. After it finds all of the globals which are needed, it
  12. // deletes whatever is left over. This allows it to delete recursive chunks of
  13. // the program which are unreachable.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/IPO/GlobalDCE.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/Analysis/TypeMetadataUtils.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/IntrinsicInst.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/Operator.h"
  24. #include "llvm/InitializePasses.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Transforms/IPO.h"
  28. #include "llvm/Transforms/Utils/CtorUtils.h"
  29. #include "llvm/Transforms/Utils/GlobalStatus.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "globaldce"
  32. static cl::opt<bool>
  33. ClEnableVFE("enable-vfe", cl::Hidden, cl::init(true), cl::ZeroOrMore,
  34. cl::desc("Enable virtual function elimination"));
  35. STATISTIC(NumAliases , "Number of global aliases removed");
  36. STATISTIC(NumFunctions, "Number of functions removed");
  37. STATISTIC(NumIFuncs, "Number of indirect functions removed");
  38. STATISTIC(NumVariables, "Number of global variables removed");
  39. STATISTIC(NumVFuncs, "Number of virtual functions removed");
  40. namespace {
  41. class GlobalDCELegacyPass : public ModulePass {
  42. public:
  43. static char ID; // Pass identification, replacement for typeid
  44. GlobalDCELegacyPass() : ModulePass(ID) {
  45. initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
  46. }
  47. // run - Do the GlobalDCE pass on the specified module, optionally updating
  48. // the specified callgraph to reflect the changes.
  49. //
  50. bool runOnModule(Module &M) override {
  51. if (skipModule(M))
  52. return false;
  53. // We need a minimally functional dummy module analysis manager. It needs
  54. // to at least know about the possibility of proxying a function analysis
  55. // manager.
  56. FunctionAnalysisManager DummyFAM;
  57. ModuleAnalysisManager DummyMAM;
  58. DummyMAM.registerPass(
  59. [&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); });
  60. auto PA = Impl.run(M, DummyMAM);
  61. return !PA.areAllPreserved();
  62. }
  63. private:
  64. GlobalDCEPass Impl;
  65. };
  66. }
  67. char GlobalDCELegacyPass::ID = 0;
  68. INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
  69. "Dead Global Elimination", false, false)
  70. // Public interface to the GlobalDCEPass.
  71. ModulePass *llvm::createGlobalDCEPass() {
  72. return new GlobalDCELegacyPass();
  73. }
  74. /// Returns true if F is effectively empty.
  75. static bool isEmptyFunction(Function *F) {
  76. BasicBlock &Entry = F->getEntryBlock();
  77. for (auto &I : Entry) {
  78. if (I.isDebugOrPseudoInst())
  79. continue;
  80. if (auto *RI = dyn_cast<ReturnInst>(&I))
  81. return !RI->getReturnValue();
  82. break;
  83. }
  84. return false;
  85. }
  86. /// Compute the set of GlobalValue that depends from V.
  87. /// The recursion stops as soon as a GlobalValue is met.
  88. void GlobalDCEPass::ComputeDependencies(Value *V,
  89. SmallPtrSetImpl<GlobalValue *> &Deps) {
  90. if (auto *I = dyn_cast<Instruction>(V)) {
  91. Function *Parent = I->getParent()->getParent();
  92. Deps.insert(Parent);
  93. } else if (auto *GV = dyn_cast<GlobalValue>(V)) {
  94. Deps.insert(GV);
  95. } else if (auto *CE = dyn_cast<Constant>(V)) {
  96. // Avoid walking the whole tree of a big ConstantExprs multiple times.
  97. auto Where = ConstantDependenciesCache.find(CE);
  98. if (Where != ConstantDependenciesCache.end()) {
  99. auto const &K = Where->second;
  100. Deps.insert(K.begin(), K.end());
  101. } else {
  102. SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE];
  103. for (User *CEUser : CE->users())
  104. ComputeDependencies(CEUser, LocalDeps);
  105. Deps.insert(LocalDeps.begin(), LocalDeps.end());
  106. }
  107. }
  108. }
  109. void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) {
  110. SmallPtrSet<GlobalValue *, 8> Deps;
  111. for (User *User : GV.users())
  112. ComputeDependencies(User, Deps);
  113. Deps.erase(&GV); // Remove self-reference.
  114. for (GlobalValue *GVU : Deps) {
  115. // If this is a dep from a vtable to a virtual function, and we have
  116. // complete information about all virtual call sites which could call
  117. // though this vtable, then skip it, because the call site information will
  118. // be more precise.
  119. if (VFESafeVTables.count(GVU) && isa<Function>(&GV)) {
  120. LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU->getName() << " -> "
  121. << GV.getName() << "\n");
  122. continue;
  123. }
  124. GVDependencies[GVU].insert(&GV);
  125. }
  126. }
  127. /// Mark Global value as Live
  128. void GlobalDCEPass::MarkLive(GlobalValue &GV,
  129. SmallVectorImpl<GlobalValue *> *Updates) {
  130. auto const Ret = AliveGlobals.insert(&GV);
  131. if (!Ret.second)
  132. return;
  133. if (Updates)
  134. Updates->push_back(&GV);
  135. if (Comdat *C = GV.getComdat()) {
  136. for (auto &&CM : make_range(ComdatMembers.equal_range(C))) {
  137. MarkLive(*CM.second, Updates); // Recursion depth is only two because only
  138. // globals in the same comdat are visited.
  139. }
  140. }
  141. }
  142. void GlobalDCEPass::ScanVTables(Module &M) {
  143. SmallVector<MDNode *, 2> Types;
  144. LLVM_DEBUG(dbgs() << "Building type info -> vtable map\n");
  145. auto *LTOPostLinkMD =
  146. cast_or_null<ConstantAsMetadata>(M.getModuleFlag("LTOPostLink"));
  147. bool LTOPostLink =
  148. LTOPostLinkMD &&
  149. (cast<ConstantInt>(LTOPostLinkMD->getValue())->getZExtValue() != 0);
  150. for (GlobalVariable &GV : M.globals()) {
  151. Types.clear();
  152. GV.getMetadata(LLVMContext::MD_type, Types);
  153. if (GV.isDeclaration() || Types.empty())
  154. continue;
  155. // Use the typeid metadata on the vtable to build a mapping from typeids to
  156. // the list of (GV, offset) pairs which are the possible vtables for that
  157. // typeid.
  158. for (MDNode *Type : Types) {
  159. Metadata *TypeID = Type->getOperand(1).get();
  160. uint64_t Offset =
  161. cast<ConstantInt>(
  162. cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
  163. ->getZExtValue();
  164. TypeIdMap[TypeID].insert(std::make_pair(&GV, Offset));
  165. }
  166. // If the type corresponding to the vtable is private to this translation
  167. // unit, we know that we can see all virtual functions which might use it,
  168. // so VFE is safe.
  169. if (auto GO = dyn_cast<GlobalObject>(&GV)) {
  170. GlobalObject::VCallVisibility TypeVis = GO->getVCallVisibility();
  171. if (TypeVis == GlobalObject::VCallVisibilityTranslationUnit ||
  172. (LTOPostLink &&
  173. TypeVis == GlobalObject::VCallVisibilityLinkageUnit)) {
  174. LLVM_DEBUG(dbgs() << GV.getName() << " is safe for VFE\n");
  175. VFESafeVTables.insert(&GV);
  176. }
  177. }
  178. }
  179. }
  180. void GlobalDCEPass::ScanVTableLoad(Function *Caller, Metadata *TypeId,
  181. uint64_t CallOffset) {
  182. for (auto &VTableInfo : TypeIdMap[TypeId]) {
  183. GlobalVariable *VTable = VTableInfo.first;
  184. uint64_t VTableOffset = VTableInfo.second;
  185. Constant *Ptr =
  186. getPointerAtOffset(VTable->getInitializer(), VTableOffset + CallOffset,
  187. *Caller->getParent(), VTable);
  188. if (!Ptr) {
  189. LLVM_DEBUG(dbgs() << "can't find pointer in vtable!\n");
  190. VFESafeVTables.erase(VTable);
  191. return;
  192. }
  193. auto Callee = dyn_cast<Function>(Ptr->stripPointerCasts());
  194. if (!Callee) {
  195. LLVM_DEBUG(dbgs() << "vtable entry is not function pointer!\n");
  196. VFESafeVTables.erase(VTable);
  197. return;
  198. }
  199. LLVM_DEBUG(dbgs() << "vfunc dep " << Caller->getName() << " -> "
  200. << Callee->getName() << "\n");
  201. GVDependencies[Caller].insert(Callee);
  202. }
  203. }
  204. void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module &M) {
  205. LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n");
  206. Function *TypeCheckedLoadFunc =
  207. M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
  208. if (!TypeCheckedLoadFunc)
  209. return;
  210. for (auto U : TypeCheckedLoadFunc->users()) {
  211. auto CI = dyn_cast<CallInst>(U);
  212. if (!CI)
  213. continue;
  214. auto *Offset = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  215. Value *TypeIdValue = CI->getArgOperand(2);
  216. auto *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
  217. if (Offset) {
  218. ScanVTableLoad(CI->getFunction(), TypeId, Offset->getZExtValue());
  219. } else {
  220. // type.checked.load with a non-constant offset, so assume every entry in
  221. // every matching vtable is used.
  222. for (auto &VTableInfo : TypeIdMap[TypeId]) {
  223. VFESafeVTables.erase(VTableInfo.first);
  224. }
  225. }
  226. }
  227. }
  228. void GlobalDCEPass::AddVirtualFunctionDependencies(Module &M) {
  229. if (!ClEnableVFE)
  230. return;
  231. // If the Virtual Function Elim module flag is present and set to zero, then
  232. // the vcall_visibility metadata was inserted for another optimization (WPD)
  233. // and we may not have type checked loads on all accesses to the vtable.
  234. // Don't attempt VFE in that case.
  235. auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
  236. M.getModuleFlag("Virtual Function Elim"));
  237. if (!Val || Val->getZExtValue() == 0)
  238. return;
  239. ScanVTables(M);
  240. if (VFESafeVTables.empty())
  241. return;
  242. ScanTypeCheckedLoadIntrinsics(M);
  243. LLVM_DEBUG(
  244. dbgs() << "VFE safe vtables:\n";
  245. for (auto *VTable : VFESafeVTables)
  246. dbgs() << " " << VTable->getName() << "\n";
  247. );
  248. }
  249. PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) {
  250. bool Changed = false;
  251. // The algorithm first computes the set L of global variables that are
  252. // trivially live. Then it walks the initialization of these variables to
  253. // compute the globals used to initialize them, which effectively builds a
  254. // directed graph where nodes are global variables, and an edge from A to B
  255. // means B is used to initialize A. Finally, it propagates the liveness
  256. // information through the graph starting from the nodes in L. Nodes note
  257. // marked as alive are discarded.
  258. // Remove empty functions from the global ctors list.
  259. Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
  260. // Collect the set of members for each comdat.
  261. for (Function &F : M)
  262. if (Comdat *C = F.getComdat())
  263. ComdatMembers.insert(std::make_pair(C, &F));
  264. for (GlobalVariable &GV : M.globals())
  265. if (Comdat *C = GV.getComdat())
  266. ComdatMembers.insert(std::make_pair(C, &GV));
  267. for (GlobalAlias &GA : M.aliases())
  268. if (Comdat *C = GA.getComdat())
  269. ComdatMembers.insert(std::make_pair(C, &GA));
  270. // Add dependencies between virtual call sites and the virtual functions they
  271. // might call, if we have that information.
  272. AddVirtualFunctionDependencies(M);
  273. // Loop over the module, adding globals which are obviously necessary.
  274. for (GlobalObject &GO : M.global_objects()) {
  275. Changed |= RemoveUnusedGlobalValue(GO);
  276. // Functions with external linkage are needed if they have a body.
  277. // Externally visible & appending globals are needed, if they have an
  278. // initializer.
  279. if (!GO.isDeclaration())
  280. if (!GO.isDiscardableIfUnused())
  281. MarkLive(GO);
  282. UpdateGVDependencies(GO);
  283. }
  284. // Compute direct dependencies of aliases.
  285. for (GlobalAlias &GA : M.aliases()) {
  286. Changed |= RemoveUnusedGlobalValue(GA);
  287. // Externally visible aliases are needed.
  288. if (!GA.isDiscardableIfUnused())
  289. MarkLive(GA);
  290. UpdateGVDependencies(GA);
  291. }
  292. // Compute direct dependencies of ifuncs.
  293. for (GlobalIFunc &GIF : M.ifuncs()) {
  294. Changed |= RemoveUnusedGlobalValue(GIF);
  295. // Externally visible ifuncs are needed.
  296. if (!GIF.isDiscardableIfUnused())
  297. MarkLive(GIF);
  298. UpdateGVDependencies(GIF);
  299. }
  300. // Propagate liveness from collected Global Values through the computed
  301. // dependencies.
  302. SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(),
  303. AliveGlobals.end()};
  304. while (!NewLiveGVs.empty()) {
  305. GlobalValue *LGV = NewLiveGVs.pop_back_val();
  306. for (auto *GVD : GVDependencies[LGV])
  307. MarkLive(*GVD, &NewLiveGVs);
  308. }
  309. // Now that all globals which are needed are in the AliveGlobals set, we loop
  310. // through the program, deleting those which are not alive.
  311. //
  312. // The first pass is to drop initializers of global variables which are dead.
  313. std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
  314. for (GlobalVariable &GV : M.globals())
  315. if (!AliveGlobals.count(&GV)) {
  316. DeadGlobalVars.push_back(&GV); // Keep track of dead globals
  317. if (GV.hasInitializer()) {
  318. Constant *Init = GV.getInitializer();
  319. GV.setInitializer(nullptr);
  320. if (isSafeToDestroyConstant(Init))
  321. Init->destroyConstant();
  322. }
  323. }
  324. // The second pass drops the bodies of functions which are dead...
  325. std::vector<Function *> DeadFunctions;
  326. for (Function &F : M)
  327. if (!AliveGlobals.count(&F)) {
  328. DeadFunctions.push_back(&F); // Keep track of dead globals
  329. if (!F.isDeclaration())
  330. F.deleteBody();
  331. }
  332. // The third pass drops targets of aliases which are dead...
  333. std::vector<GlobalAlias*> DeadAliases;
  334. for (GlobalAlias &GA : M.aliases())
  335. if (!AliveGlobals.count(&GA)) {
  336. DeadAliases.push_back(&GA);
  337. GA.setAliasee(nullptr);
  338. }
  339. // The fourth pass drops targets of ifuncs which are dead...
  340. std::vector<GlobalIFunc*> DeadIFuncs;
  341. for (GlobalIFunc &GIF : M.ifuncs())
  342. if (!AliveGlobals.count(&GIF)) {
  343. DeadIFuncs.push_back(&GIF);
  344. GIF.setResolver(nullptr);
  345. }
  346. // Now that all interferences have been dropped, delete the actual objects
  347. // themselves.
  348. auto EraseUnusedGlobalValue = [&](GlobalValue *GV) {
  349. RemoveUnusedGlobalValue(*GV);
  350. GV->eraseFromParent();
  351. Changed = true;
  352. };
  353. NumFunctions += DeadFunctions.size();
  354. for (Function *F : DeadFunctions) {
  355. if (!F->use_empty()) {
  356. // Virtual functions might still be referenced by one or more vtables,
  357. // but if we've proven them to be unused then it's safe to replace the
  358. // virtual function pointers with null, allowing us to remove the
  359. // function itself.
  360. ++NumVFuncs;
  361. // Detect vfuncs that are referenced as "relative pointers" which are used
  362. // in Swift vtables, i.e. entries in the form of:
  363. //
  364. // i32 trunc (i64 sub (i64 ptrtoint @f, i64 ptrtoint ...)) to i32)
  365. //
  366. // In this case, replace the whole "sub" expression with constant 0 to
  367. // avoid leaving a weird sub(0, symbol) expression behind.
  368. replaceRelativePointerUsersWithZero(F);
  369. F->replaceNonMetadataUsesWith(ConstantPointerNull::get(F->getType()));
  370. }
  371. EraseUnusedGlobalValue(F);
  372. }
  373. NumVariables += DeadGlobalVars.size();
  374. for (GlobalVariable *GV : DeadGlobalVars)
  375. EraseUnusedGlobalValue(GV);
  376. NumAliases += DeadAliases.size();
  377. for (GlobalAlias *GA : DeadAliases)
  378. EraseUnusedGlobalValue(GA);
  379. NumIFuncs += DeadIFuncs.size();
  380. for (GlobalIFunc *GIF : DeadIFuncs)
  381. EraseUnusedGlobalValue(GIF);
  382. // Make sure that all memory is released
  383. AliveGlobals.clear();
  384. ConstantDependenciesCache.clear();
  385. GVDependencies.clear();
  386. ComdatMembers.clear();
  387. TypeIdMap.clear();
  388. VFESafeVTables.clear();
  389. if (Changed)
  390. return PreservedAnalyses::none();
  391. return PreservedAnalyses::all();
  392. }
  393. // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
  394. // GlobalValue, looking for the constant pointer ref that may be pointing to it.
  395. // If found, check to see if the constant pointer ref is safe to destroy, and if
  396. // so, nuke it. This will reduce the reference count on the global value, which
  397. // might make it deader.
  398. //
  399. bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) {
  400. if (GV.use_empty())
  401. return false;
  402. GV.removeDeadConstantUsers();
  403. return GV.use_empty();
  404. }