llvm.org GIT mirror llvm / 8fbafc9
Refactor the InternalizePass into a helper class, and expose it through a public free function (NFC) There is really no reason to require to instanciate a pass manager to internalize. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266167 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
4 changed file(s) with 124 addition(s) and 79 deletion(s). Raw diff Collapse all Expand all
0 //====- llvm/Transforms/IPO/Internalize.h - Internalization API -*- C++ -*-===//
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 #ifndef LLVM_INTERNALIZE_H
10 #define LLVM_INTERNALIZE_H
11
12 #include "llvm/IR/GlobalValue.h"
13
14 #include
15
16 namespace llvm {
17 class Module;
18 class CallGraph;
19
20 bool internalizeModule(
21 Module &TheModule,
22 const std::function &MustPreserveGV,
23 CallGraph *CG = nullptr);
24 }
25
26 #endif // LLVM_INTERNALIZE_H
1717 #include "llvm/IR/Mangler.h"
1818 #include "llvm/Target/TargetLowering.h"
1919 #include "llvm/Target/TargetSubtargetInfo.h"
20 #include "llvm/Transforms/IPO.h"
20 #include "llvm/Transforms/IPO/Internalize.h"
2121
2222 using namespace llvm;
2323
169169 LLVMCompilerUsed->setSection("llvm.metadata");
170170 }
171171
172 legacy::PassManager passes;
173 passes.add(createInternalizePass(MustPreserveSymbols));
174
175 // apply scope restrictions
176 passes.run(TheModule);
172 internalizeModule(TheModule, MustPreserveSymbols);
177173 }
1818 //
1919 //===----------------------------------------------------------------------===//
2020
21 #include "llvm/Transforms/IPO/Internalize.h"
2122 #include "llvm/Transforms/IPO.h"
2223 #include "llvm/ADT/SmallPtrSet.h"
2324 #include "llvm/ADT/Statistic.h"
3637
3738 #define DEBUG_TYPE "internalize"
3839
39 STATISTIC(NumAliases , "Number of aliases internalized");
40 STATISTIC(NumAliases, "Number of aliases internalized");
4041 STATISTIC(NumFunctions, "Number of functions internalized");
41 STATISTIC(NumGlobals , "Number of global vars internalized");
42 STATISTIC(NumGlobals, "Number of global vars internalized");
4243
4344 // APIFile - A file which contains a list of symbols that should not be marked
4445 // external.
4546 static cl::opt
46 APIFile("internalize-public-api-file", cl::value_desc("filename"),
47 cl::desc("A file containing list of symbol names to preserve"));
47 APIFile("internalize-public-api-file", cl::value_desc("filename"),
48 cl::desc("A file containing list of symbol names to preserve"));
4849
4950 // APIList - A list of symbols that should not be marked internal.
5051 static cl::list
51 APIList("internalize-public-api-list", cl::value_desc("list"),
52 cl::desc("A list of symbol names to preserve"),
53 cl::CommaSeparated);
52 APIList("internalize-public-api-list", cl::value_desc("list"),
53 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated);
5454
5555 namespace {
56
5657 // Helper to load an API list to preserve from file and expose it as a functor
57 // to the Internalize Pass
58 // for internalization.
5859 class PreserveAPIList {
5960 public:
6061 PreserveAPIList() {
6970
7071 private:
7172 // Contains the set of symbols loaded from file
72 StringSet<> ExternalNames;
73
74 void LoadFile(StringRef Filename) {
75 // Load the APIFile...
76 std::ifstream In(Filename.data());
77 if (!In.good()) {
78 errs() << "WARNING: Internalize couldn't load file '" << Filename
79 << "'! Continuing as if it's empty.\n";
80 return; // Just continue as if the file were empty
81 }
82 while (In) {
83 std::string Symbol;
84 In >> Symbol;
85 if (!Symbol.empty())
86 ExternalNames.insert(Symbol);
87 }
73 StringSet<> ExternalNames;
74
75 void LoadFile(StringRef Filename) {
76 // Load the APIFile...
77 std::ifstream In(Filename.data());
78 if (!In.good()) {
79 errs() << "WARNING: Internalize couldn't load file '" << Filename
80 << "'! Continuing as if it's empty.\n";
81 return; // Just continue as if the file were empty
8882 }
83 while (In) {
84 std::string Symbol;
85 In >> Symbol;
86 if (!Symbol.empty())
87 ExternalNames.insert(Symbol);
88 }
89 }
8990 };
90 }
91
92 namespace {
91
92 // Internalization exposed as a pass
9393 class InternalizePass : public ModulePass {
94 // Client supply callback to control wheter a symbol must be preserved.
94 // Client supplied callback to control wheter a symbol must be preserved.
9595 std::function MustPreserveGV;
96
97 public:
98 static char ID; // Pass identification, replacement for typeid
99
100 InternalizePass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
101
102 InternalizePass(std::function MustPreserveGV)
103 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
104 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
105 }
106
107 bool runOnModule(Module &M) override {
108 CallGraphWrapperPass *CGPass =
109 getAnalysisIfAvailable();
110 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
111 return internalizeModule(M, MustPreserveGV, CG);
112 }
113
114 void getAnalysisUsage(AnalysisUsage &AU) const override {
115 AU.setPreservesCFG();
116 AU.addPreserved();
117 }
118 };
119 } // end anonymous namespace
120
121 char InternalizePass::ID = 0;
122 INITIALIZE_PASS(InternalizePass, "internalize", "Internalize Global Symbols",
123 false, false)
124
125 // Helper class to perform internalization.
126 class Internalizer {
127 // Client supplied callback to control wheter a symbol must be preserved.
128 const std::function &MustPreserveGV;
96129
97130 // Set of symbols private to the compiler that this pass should not touch.
98131 StringSet<> AlwaysPreserved;
122155 return MustPreserveGV(GV);
123156 }
124157
125 bool maybeInternalize(GlobalValue &GV,
126 const std::set &ExternalComdats);
127 void checkComdatVisibility(GlobalValue &GV,
128 std::set &ExternalComdats);
129
130 public:
131 static char ID; // Pass identification, replacement for typeid
132 explicit InternalizePass();
133 InternalizePass(std::function MustPreserveGV);
134 bool runOnModule(Module &M) override;
135
136 void getAnalysisUsage(AnalysisUsage &AU) const override {
137 AU.setPreservesCFG();
138 AU.addPreserved();
139 }
140 };
141 } // end anonymous namespace
142
143 char InternalizePass::ID = 0;
144 INITIALIZE_PASS(InternalizePass, "internalize",
145 "Internalize Global Symbols", false, false)
146
147 InternalizePass::InternalizePass()
148 : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {}
149
150 InternalizePass::InternalizePass(
151 std::function MustPreserveGV)
152 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) {
153 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
154 }
158 bool maybeInternalize(GlobalValue &GV,
159 const std::set &ExternalComdats);
160 void checkComdatVisibility(GlobalValue &GV,
161 std::set &ExternalComdats);
162
163 public:
164 Internalizer(const std::function &MustPreserveGV)
165 : MustPreserveGV(MustPreserveGV) {}
166
167 /// Run the internalizer on \p TheModule, returns true if any changes was
168 /// made.
169 ///
170 /// If the CallGraph \p CG is supplied, it will be updated when
171 /// internalizing a function (by removing any edge from the "external node")
172 bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr);
173 };
155174
156175 // Internalize GV if it is possible to do so, i.e. it is not externally visible
157176 // and is not a member of an externally visible comdat.
158 bool InternalizePass::maybeInternalize(
177 bool Internalizer::maybeInternalize(
159178 GlobalValue &GV, const std::set &ExternalComdats) {
160179 if (Comdat *C = GV.getComdat()) {
161180 if (ExternalComdats.count(C))
182201
183202 // If GV is part of a comdat and is externally visible, keep track of its
184203 // comdat so that we don't internalize any of its members.
185 void InternalizePass::checkComdatVisibility(
204 void Internalizer::checkComdatVisibility(
186205 GlobalValue &GV, std::set &ExternalComdats) {
187206 Comdat *C = GV.getComdat();
188207 if (!C)
192211 ExternalComdats.insert(C);
193212 }
194213
195 bool InternalizePass::runOnModule(Module &M) {
196 CallGraphWrapperPass *CGPass = getAnalysisIfAvailable();
197 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr;
214 bool Internalizer::internalizeModule(Module &M, CallGraph *CG) {
198215 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
199216
200217 SmallPtrSet Used;
257274
258275 // Mark all global variables with initializers that are not in the api as
259276 // internal as well.
260 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
261 I != E; ++I) {
277 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;
278 ++I) {
262279 if (!maybeInternalize(*I, ExternalComdats))
263280 continue;
264281
267284 }
268285
269286 // Mark all aliases that are not in the api as internal as well.
270 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
271 I != E; ++I) {
287 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
288 ++I) {
272289 if (!maybeInternalize(*I, ExternalComdats))
273290 continue;
274291
285302 return true;
286303 }
287304
288 ModulePass *llvm::createInternalizePass() {
289 return new InternalizePass(PreserveAPIList());
290 }
305 /// Public API below
306
307 bool llvm::internalizeModule(
308 Module &TheModule,
309 const std::function &MustPreserveGV,
310 CallGraph *CG) {
311 return Internalizer(MustPreserveGV).internalizeModule(TheModule, CG);
312 }
313
314 ModulePass *llvm::createInternalizePass() { return new InternalizePass(); }
291315
292316 ModulePass *llvm::createInternalizePass(
293317 std::function MustPreserveGV) {
294318 return new InternalizePass(std::move(MustPreserveGV));
295319 }
296
55 target triple = "x86_64-apple-macosx10.10.0"
66
77 ; -disable-verify should disable verification from the optimization pipeline.
8 ; CHECK: Pass Arguments: -internalize
8 ; CHECK: Pass Arguments:
99 ; CHECK-NOT: -verify
1010
11 ; VERIFY: Pass Arguments: -internalize
1211 ; VERIFY: Pass Arguments: {{.*}} -verify {{.*}} -verify
1312
1413 define void @f() {