llvm.org GIT mirror llvm / 82cad6f
[IPO/GlobalDCE] Port to the new pass manager. Differential Revision: http://reviews.llvm.org/D19782 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268425 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 3 years ago
8 changed file(s) with 81 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
141141 void initializeGCMachineCodeAnalysisPass(PassRegistry&);
142142 void initializeGCModuleInfoPass(PassRegistry&);
143143 void initializeGVNLegacyPassPass(PassRegistry&);
144 void initializeGlobalDCEPass(PassRegistry&);
144 void initializeGlobalDCELegacyPassPass(PassRegistry&);
145145 void initializeGlobalOptLegacyPassPass(PassRegistry&);
146146 void initializeGlobalsAAWrapperPassPass(PassRegistry&);
147147 void initializeIPCPPass(PassRegistry&);
0 //===-- GlobalDCE.h - DCE unreachable internal functions ------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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
17 #ifndef LLVM_TRANSFORMS_IPO_GLOBALDCE_H
18 #define LLVM_TRANSFORMS_IPO_GLOBALDCE_H
19
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/PassManager.h"
22 #include
23
24 namespace llvm {
25
26 /// Pass to remove unused function declarations.
27 class GlobalDCEPass : public PassInfoMixin {
28 public:
29 PreservedAnalyses run(Module &M);
30
31 private:
32 SmallPtrSet AliveGlobals;
33 SmallPtrSet SeenConstants;
34 std::unordered_multimap ComdatMembers;
35
36 /// Mark the specific global value as needed, and
37 /// recursively mark anything that it uses as also needed.
38 void GlobalIsNeeded(GlobalValue *GV);
39 void MarkUsedGlobalsAsNeeded(Constant *C);
40 bool RemoveUnusedGlobalValue(GlobalValue &GV);
41 };
42
43 }
44
45 #endif // LLVM_TRANSFORMS_IPO_GLOBALDCE_H
103103 initializeInstructionCombiningPassPass(R);
104104 initializeSimpleInlinerPass(R);
105105 initializePruneEHPass(R);
106 initializeGlobalDCEPass(R);
106 initializeGlobalDCELegacyPassPass(R);
107107 initializeArgPromotionPass(R);
108108 initializeJumpThreadingPass(R);
109109 initializeSROALegacyPassPass(R);
4646 #include "llvm/Target/TargetMachine.h"
4747 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
4848 #include "llvm/Transforms/IPO/FunctionAttrs.h"
49 #include "llvm/Transforms/IPO/GlobalDCE.h"
4950 #include "llvm/Transforms/IPO/GlobalOpt.h"
5051 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
5152 #include "llvm/Transforms/IPO/Internalize.h"
3535 #define MODULE_PASS(NAME, CREATE_PASS)
3636 #endif
3737 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
38 MODULE_PASS("globaldce", GlobalDCEPass())
3839 MODULE_PASS("globalopt", GlobalOptPass())
3940 MODULE_PASS("inferattrs", InferFunctionAttrsPass())
4041 MODULE_PASS("internalize", InternalizePass())
1414 //
1515 //===----------------------------------------------------------------------===//
1616
17 #include "llvm/Transforms/IPO/GlobalDCE.h"
1718 #include "llvm/ADT/SmallPtrSet.h"
1819 #include "llvm/ADT/Statistic.h"
1920 #include "llvm/IR/Constants.h"
3334 STATISTIC(NumVariables, "Number of global variables removed");
3435
3536 namespace {
36 struct GlobalDCE : public ModulePass {
37 class GlobalDCELegacyPass : public ModulePass {
38 public:
3739 static char ID; // Pass identification, replacement for typeid
38 GlobalDCE() : ModulePass(ID) {
39 initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
40 GlobalDCELegacyPass() : ModulePass(ID) {
41 initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
4042 }
4143
4244 // run - Do the GlobalDCE pass on the specified module, optionally updating
4345 // the specified callgraph to reflect the changes.
4446 //
45 bool runOnModule(Module &M) override;
47 bool runOnModule(Module &M) override {
48 if (skipModule(M))
49 return false;
50
51 auto PA = Impl.run(M);
52 return !PA.areAllPreserved();
53 }
4654
4755 private:
48 SmallPtrSet AliveGlobals;
49 SmallPtrSet SeenConstants;
50 std::unordered_multimap ComdatMembers;
51
52 /// GlobalIsNeeded - mark the specific global value as needed, and
53 /// recursively mark anything that it uses as also needed.
54 void GlobalIsNeeded(GlobalValue *GV);
55 void MarkUsedGlobalsAsNeeded(Constant *C);
56
57 bool RemoveUnusedGlobalValue(GlobalValue &GV);
56 GlobalDCEPass Impl;
5857 };
58 }
59
60 char GlobalDCELegacyPass::ID = 0;
61 INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
62 "Dead Global Elimination", false, false)
63
64 // Public interface to the GlobalDCEPass.
65 ModulePass *llvm::createGlobalDCEPass() {
66 return new GlobalDCELegacyPass();
5967 }
6068
6169 /// Returns true if F contains only a single "ret" instruction.
6775 return RI.getReturnValue() == nullptr;
6876 }
6977
70 char GlobalDCE::ID = 0;
71 INITIALIZE_PASS(GlobalDCE, "globaldce",
72 "Dead Global Elimination", false, false)
73
74 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
75
76 bool GlobalDCE::runOnModule(Module &M) {
77 if (skipModule(M))
78 return false;
79
78 PreservedAnalyses GlobalDCEPass::run(Module &M) {
8079 bool Changed = false;
8180
8281 // Remove empty functions from the global ctors list.
187186 SeenConstants.clear();
188187 ComdatMembers.clear();
189188
190 return Changed;
189 if (Changed)
190 return PreservedAnalyses::none();
191 return PreservedAnalyses::all();
191192 }
192193
193194 /// GlobalIsNeeded - the specific global value as needed, and
194195 /// recursively mark anything that it uses as also needed.
195 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
196 void GlobalDCEPass::GlobalIsNeeded(GlobalValue *G) {
196197 // If the global is already in the set, no need to reprocess it.
197198 if (!AliveGlobals.insert(G).second)
198199 return;
230231 }
231232 }
232233
233 void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
234 void GlobalDCEPass::MarkUsedGlobalsAsNeeded(Constant *C) {
234235 if (GlobalValue *GV = dyn_cast(C))
235236 return GlobalIsNeeded(GV);
236237
250251 // so, nuke it. This will reduce the reference count on the global value, which
251252 // might make it deader.
252253 //
253 bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
254 bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) {
254255 if (GV.use_empty())
255256 return false;
256257 GV.removeDeadConstantUsers();
2828 initializeDAEPass(Registry);
2929 initializeDAHPass(Registry);
3030 initializeForceFunctionAttrsLegacyPassPass(Registry);
31 initializeGlobalDCEPass(Registry);
31 initializeGlobalDCELegacyPassPass(Registry);
3232 initializeGlobalOptLegacyPassPass(Registry);
3333 initializeIPCPPass(Registry);
3434 initializeAlwaysInlinerPass(Registry);
None ; RUN: opt < %s -globaldce -S | FileCheck %s
0 ; RUN: opt < %s -passes=globaldce -S | FileCheck %s
11
22 ; CHECK-NOT: global
33 @X = external global i32