From 7695cb6da8021ef610ffbcf3b296a16688c7930d Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 25 Oct 2016 01:58:26 +0000 Subject: [PATCH] GlobalDCE: Deduplicate code. NFCI. llvm-svn: 285048 --- llvm/lib/Transforms/IPO/GlobalDCE.cpp | 53 +++++++++------------------ 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/llvm/lib/Transforms/IPO/GlobalDCE.cpp b/llvm/lib/Transforms/IPO/GlobalDCE.cpp index 4c74698a1b61..aaa9f8991442 100644 --- a/llvm/lib/Transforms/IPO/GlobalDCE.cpp +++ b/llvm/lib/Transforms/IPO/GlobalDCE.cpp @@ -162,45 +162,28 @@ PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &) { GIF.setResolver(nullptr); } - if (!DeadFunctions.empty()) { - // Now that all interferences have been dropped, delete the actual objects - // themselves. - for (Function *F : DeadFunctions) { - RemoveUnusedGlobalValue(*F); - M.getFunctionList().erase(F); - } - NumFunctions += DeadFunctions.size(); + // Now that all interferences have been dropped, delete the actual objects + // themselves. + auto EraseUnusedGlobalValue = [&](GlobalValue *GV) { + RemoveUnusedGlobalValue(*GV); + GV->eraseFromParent(); Changed = true; - } + }; - if (!DeadGlobalVars.empty()) { - for (GlobalVariable *GV : DeadGlobalVars) { - RemoveUnusedGlobalValue(*GV); - M.getGlobalList().erase(GV); - } - NumVariables += DeadGlobalVars.size(); - Changed = true; - } + NumFunctions += DeadFunctions.size(); + for (Function *F : DeadFunctions) + EraseUnusedGlobalValue(F); - // Now delete any dead aliases. - if (!DeadAliases.empty()) { - for (GlobalAlias *GA : DeadAliases) { - RemoveUnusedGlobalValue(*GA); - M.getAliasList().erase(GA); - } - NumAliases += DeadAliases.size(); - Changed = true; - } + NumVariables += DeadGlobalVars.size(); + for (GlobalVariable *GV : DeadGlobalVars) + EraseUnusedGlobalValue(GV); - // Now delete any dead aliases. - if (!DeadIFuncs.empty()) { - for (GlobalIFunc *GIF : DeadIFuncs) { - RemoveUnusedGlobalValue(*GIF); - M.getIFuncList().erase(GIF); - } - NumIFuncs += DeadIFuncs.size(); - Changed = true; - } + NumAliases += DeadAliases.size(); + for (GlobalAlias *GA : DeadAliases) + EraseUnusedGlobalValue(GA); + + for (GlobalIFunc *GIF : DeadIFuncs) + EraseUnusedGlobalValue(GIF); // Make sure that all memory is released AliveGlobals.clear();