1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass loops over all of the functions and variables in the input module.
10 // If the function or variable does not need to be preserved according to the
11 // client supplied callback, it is marked as internal.
13 // This transformation would not be legal in a regular compilation, but it gets
14 // extra information from the linker about what is safe.
16 // For example: Internalizing a function with external linkage. Only if we are
17 // told it is only used from within this module, it is safe to do it.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Transforms/IPO/Internalize.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/Analysis/CallGraph.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/LineIterator.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Transforms/IPO.h"
34 #include "llvm/Transforms/Utils/GlobalStatus.h"
37 #define DEBUG_TYPE "internalize"
39 STATISTIC(NumAliases
, "Number of aliases internalized");
40 STATISTIC(NumFunctions
, "Number of functions internalized");
41 STATISTIC(NumGlobals
, "Number of global vars internalized");
43 // APIFile - A file which contains a list of symbols that should not be marked
45 static cl::opt
<std::string
>
46 APIFile("internalize-public-api-file", cl::value_desc("filename"),
47 cl::desc("A file containing list of symbol names to preserve"));
49 // APIList - A list of symbols that should not be marked internal.
50 static cl::list
<std::string
>
51 APIList("internalize-public-api-list", cl::value_desc("list"),
52 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated
);
55 // Helper to load an API list to preserve from file and expose it as a functor
56 // for internalization.
57 class PreserveAPIList
{
62 ExternalNames
.insert(APIList
.begin(), APIList
.end());
65 bool operator()(const GlobalValue
&GV
) {
66 return ExternalNames
.count(GV
.getName());
70 // Contains the set of symbols loaded from file
71 StringSet
<> ExternalNames
;
73 void LoadFile(StringRef Filename
) {
74 // Load the APIFile...
75 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buf
=
76 MemoryBuffer::getFile(Filename
);
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
82 for (line_iterator
I(*Buf
->get(), true), E
; I
!= E
; ++I
)
83 ExternalNames
.insert(*I
);
86 } // end anonymous namespace
88 bool InternalizePass::shouldPreserveGV(const GlobalValue
&GV
) {
89 // Function must be defined here
90 if (GV
.isDeclaration())
93 // Available externally is really just a "declaration with a body".
94 if (GV
.hasAvailableExternallyLinkage())
97 // Assume that dllexported symbols are referenced elsewhere
98 if (GV
.hasDLLExportStorageClass())
101 // Already local, has nothing to do.
102 if (GV
.hasLocalLinkage())
105 // Check some special cases
106 if (AlwaysPreserved
.count(GV
.getName()))
109 return MustPreserveGV(GV
);
112 bool InternalizePass::maybeInternalize(
113 GlobalValue
&GV
, const DenseSet
<const Comdat
*> &ExternalComdats
) {
114 if (Comdat
*C
= GV
.getComdat()) {
115 if (ExternalComdats
.count(C
))
118 // If a comdat is not externally visible we can drop it.
119 if (auto GO
= dyn_cast
<GlobalObject
>(&GV
))
120 GO
->setComdat(nullptr);
122 if (GV
.hasLocalLinkage())
125 if (GV
.hasLocalLinkage())
128 if (shouldPreserveGV(GV
))
132 GV
.setVisibility(GlobalValue::DefaultVisibility
);
133 GV
.setLinkage(GlobalValue::InternalLinkage
);
137 // If GV is part of a comdat and is externally visible, keep track of its
138 // comdat so that we don't internalize any of its members.
139 void InternalizePass::checkComdatVisibility(
140 GlobalValue
&GV
, DenseSet
<const Comdat
*> &ExternalComdats
) {
141 Comdat
*C
= GV
.getComdat();
145 if (shouldPreserveGV(GV
))
146 ExternalComdats
.insert(C
);
149 bool InternalizePass::internalizeModule(Module
&M
, CallGraph
*CG
) {
150 bool Changed
= false;
151 CallGraphNode
*ExternalNode
= CG
? CG
->getExternalCallingNode() : nullptr;
153 SmallPtrSet
<GlobalValue
*, 8> Used
;
154 collectUsedGlobalVariables(M
, Used
, false);
156 // Collect comdat visiblity information for the module.
157 DenseSet
<const Comdat
*> ExternalComdats
;
158 if (!M
.getComdatSymbolTable().empty()) {
159 for (Function
&F
: M
)
160 checkComdatVisibility(F
, ExternalComdats
);
161 for (GlobalVariable
&GV
: M
.globals())
162 checkComdatVisibility(GV
, ExternalComdats
);
163 for (GlobalAlias
&GA
: M
.aliases())
164 checkComdatVisibility(GA
, ExternalComdats
);
167 // We must assume that globals in llvm.used have a reference that not even
168 // the linker can see, so we don't internalize them.
169 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
170 // linker can drop those symbols. If this pass is running as part of LTO,
171 // one might think that it could just drop llvm.compiler.used. The problem
172 // is that even in LTO llvm doesn't see every reference. For example,
173 // we don't see references from function local inline assembly. To be
174 // conservative, we internalize symbols in llvm.compiler.used, but we
175 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
176 for (GlobalValue
*V
: Used
) {
177 AlwaysPreserved
.insert(V
->getName());
180 // Mark all functions not in the api as internal.
181 for (Function
&I
: M
) {
182 if (!maybeInternalize(I
, ExternalComdats
))
187 // Remove a callgraph edge from the external node to this function.
188 ExternalNode
->removeOneAbstractEdgeTo((*CG
)[&I
]);
191 LLVM_DEBUG(dbgs() << "Internalizing func " << I
.getName() << "\n");
194 // Never internalize the llvm.used symbol. It is used to implement
195 // attribute((used)).
196 // FIXME: Shouldn't this just filter on llvm.metadata section??
197 AlwaysPreserved
.insert("llvm.used");
198 AlwaysPreserved
.insert("llvm.compiler.used");
200 // Never internalize anchors used by the machine module info, else the info
201 // won't find them. (see MachineModuleInfo.)
202 AlwaysPreserved
.insert("llvm.global_ctors");
203 AlwaysPreserved
.insert("llvm.global_dtors");
204 AlwaysPreserved
.insert("llvm.global.annotations");
206 // Never internalize symbols code-gen inserts.
207 // FIXME: We should probably add this (and the __stack_chk_guard) via some
208 // type of call-back in CodeGen.
209 AlwaysPreserved
.insert("__stack_chk_fail");
210 AlwaysPreserved
.insert("__stack_chk_guard");
212 // Mark all global variables with initializers that are not in the api as
214 for (auto &GV
: M
.globals()) {
215 if (!maybeInternalize(GV
, ExternalComdats
))
220 LLVM_DEBUG(dbgs() << "Internalized gvar " << GV
.getName() << "\n");
223 // Mark all aliases that are not in the api as internal as well.
224 for (auto &GA
: M
.aliases()) {
225 if (!maybeInternalize(GA
, ExternalComdats
))
230 LLVM_DEBUG(dbgs() << "Internalized alias " << GA
.getName() << "\n");
236 InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {}
238 PreservedAnalyses
InternalizePass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
239 if (!internalizeModule(M
, AM
.getCachedResult
<CallGraphAnalysis
>(M
)))
240 return PreservedAnalyses::all();
242 PreservedAnalyses PA
;
243 PA
.preserve
<CallGraphAnalysis
>();
248 class InternalizeLegacyPass
: public ModulePass
{
249 // Client supplied callback to control wheter a symbol must be preserved.
250 std::function
<bool(const GlobalValue
&)> MustPreserveGV
;
253 static char ID
; // Pass identification, replacement for typeid
255 InternalizeLegacyPass() : ModulePass(ID
), MustPreserveGV(PreserveAPIList()) {}
257 InternalizeLegacyPass(std::function
<bool(const GlobalValue
&)> MustPreserveGV
)
258 : ModulePass(ID
), MustPreserveGV(std::move(MustPreserveGV
)) {
259 initializeInternalizeLegacyPassPass(*PassRegistry::getPassRegistry());
262 bool runOnModule(Module
&M
) override
{
266 CallGraphWrapperPass
*CGPass
=
267 getAnalysisIfAvailable
<CallGraphWrapperPass
>();
268 CallGraph
*CG
= CGPass
? &CGPass
->getCallGraph() : nullptr;
269 return internalizeModule(M
, MustPreserveGV
, CG
);
272 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
273 AU
.setPreservesCFG();
274 AU
.addPreserved
<CallGraphWrapperPass
>();
279 char InternalizeLegacyPass::ID
= 0;
280 INITIALIZE_PASS(InternalizeLegacyPass
, "internalize",
281 "Internalize Global Symbols", false, false)
283 ModulePass
*llvm::createInternalizePass() {
284 return new InternalizeLegacyPass();
287 ModulePass
*llvm::createInternalizePass(
288 std::function
<bool(const GlobalValue
&)> MustPreserveGV
) {
289 return new InternalizeLegacyPass(std::move(MustPreserveGV
));