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/SmallString.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/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/GlobPattern.h"
30 #include "llvm/Support/LineIterator.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/TargetParser/Triple.h"
34 #include "llvm/Transforms/IPO.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 symbol glob patterns that should
44 // not be marked external.
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 symbol glob patterns 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 for (StringRef Pattern
: APIList
)
66 bool operator()(const GlobalValue
&GV
) {
68 ExternalNames
, [&](GlobPattern
&GP
) { return GP
.match(GV
.getName()); });
72 // Contains the set of symbols loaded from file
73 SmallVector
<GlobPattern
> ExternalNames
;
75 void addGlob(StringRef Pattern
) {
76 auto GlobOrErr
= GlobPattern::create(Pattern
);
78 errs() << "WARNING: when loading pattern: '"
79 << toString(GlobOrErr
.takeError()) << "' ignoring";
82 ExternalNames
.emplace_back(std::move(*GlobOrErr
));
85 void LoadFile(StringRef Filename
) {
86 // Load the APIFile...
87 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufOrErr
=
88 MemoryBuffer::getFile(Filename
);
90 errs() << "WARNING: Internalize couldn't load file '" << Filename
91 << "'! Continuing as if it's empty.\n";
92 return; // Just continue as if the file were empty
94 Buf
= std::move(*BufOrErr
);
95 for (line_iterator
I(*Buf
, true), E
; I
!= E
; ++I
)
99 std::shared_ptr
<MemoryBuffer
> Buf
;
101 } // end anonymous namespace
103 bool InternalizePass::shouldPreserveGV(const GlobalValue
&GV
) {
104 // Function must be defined here
105 if (GV
.isDeclaration())
108 // Available externally is really just a "declaration with a body".
109 if (GV
.hasAvailableExternallyLinkage())
112 // Assume that dllexported symbols are referenced elsewhere
113 if (GV
.hasDLLExportStorageClass())
116 // As the name suggests, externally initialized variables need preserving as
117 // they would be initialized elsewhere externally.
118 if (const auto *G
= dyn_cast
<GlobalVariable
>(&GV
))
119 if (G
->isExternallyInitialized())
122 // Already local, has nothing to do.
123 if (GV
.hasLocalLinkage())
126 // Check some special cases
127 if (AlwaysPreserved
.count(GV
.getName()))
130 return MustPreserveGV(GV
);
133 bool InternalizePass::maybeInternalize(
134 GlobalValue
&GV
, DenseMap
<const Comdat
*, ComdatInfo
> &ComdatMap
) {
135 SmallString
<0> ComdatName
;
136 if (Comdat
*C
= GV
.getComdat()) {
137 // For GlobalAlias, C is the aliasee object's comdat which may have been
138 // redirected. So ComdatMap may not contain C.
139 if (ComdatMap
.lookup(C
).External
)
142 if (auto *GO
= dyn_cast
<GlobalObject
>(&GV
)) {
143 // If a comdat with one member is not externally visible, we can drop it.
144 // Otherwise, the comdat can be used to establish dependencies among the
145 // group of sections. Thus we have to keep the comdat but switch it to
147 // Note: nodeduplicate is not necessary for COFF. wasm doesn't support
149 ComdatInfo
&Info
= ComdatMap
.find(C
)->second
;
151 GO
->setComdat(nullptr);
153 C
->setSelectionKind(Comdat::NoDeduplicate
);
156 if (GV
.hasLocalLinkage())
159 if (GV
.hasLocalLinkage())
162 if (shouldPreserveGV(GV
))
166 GV
.setVisibility(GlobalValue::DefaultVisibility
);
167 GV
.setLinkage(GlobalValue::InternalLinkage
);
171 // If GV is part of a comdat and is externally visible, update the comdat size
172 // and keep track of its comdat so that we don't internalize any of its members.
173 void InternalizePass::checkComdat(
174 GlobalValue
&GV
, DenseMap
<const Comdat
*, ComdatInfo
> &ComdatMap
) {
175 Comdat
*C
= GV
.getComdat();
179 ComdatInfo
&Info
= ComdatMap
.try_emplace(C
).first
->second
;
181 if (shouldPreserveGV(GV
))
182 Info
.External
= true;
185 bool InternalizePass::internalizeModule(Module
&M
) {
186 bool Changed
= false;
188 SmallVector
<GlobalValue
*, 4> Used
;
189 collectUsedGlobalVariables(M
, Used
, false);
191 // Collect comdat size and visiblity information for the module.
192 DenseMap
<const Comdat
*, ComdatInfo
> ComdatMap
;
193 if (!M
.getComdatSymbolTable().empty()) {
194 for (Function
&F
: M
)
195 checkComdat(F
, ComdatMap
);
196 for (GlobalVariable
&GV
: M
.globals())
197 checkComdat(GV
, ComdatMap
);
198 for (GlobalAlias
&GA
: M
.aliases())
199 checkComdat(GA
, ComdatMap
);
202 // We must assume that globals in llvm.used have a reference that not even
203 // the linker can see, so we don't internalize them.
204 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
205 // linker can drop those symbols. If this pass is running as part of LTO,
206 // one might think that it could just drop llvm.compiler.used. The problem
207 // is that even in LTO llvm doesn't see every reference. For example,
208 // we don't see references from function local inline assembly. To be
209 // conservative, we internalize symbols in llvm.compiler.used, but we
210 // keep llvm.compiler.used so that the symbol is not deleted by llvm.
211 for (GlobalValue
*V
: Used
) {
212 AlwaysPreserved
.insert(V
->getName());
215 // Never internalize the llvm.used symbol. It is used to implement
216 // attribute((used)).
217 // FIXME: Shouldn't this just filter on llvm.metadata section??
218 AlwaysPreserved
.insert("llvm.used");
219 AlwaysPreserved
.insert("llvm.compiler.used");
221 // Never internalize anchors used by the machine module info, else the info
222 // won't find them. (see MachineModuleInfo.)
223 AlwaysPreserved
.insert("llvm.global_ctors");
224 AlwaysPreserved
.insert("llvm.global_dtors");
225 AlwaysPreserved
.insert("llvm.global.annotations");
227 // Never internalize symbols code-gen inserts.
228 // FIXME: We should probably add this (and the __stack_chk_guard) via some
229 // type of call-back in CodeGen.
230 AlwaysPreserved
.insert("__stack_chk_fail");
231 if (Triple(M
.getTargetTriple()).isOSAIX())
232 AlwaysPreserved
.insert("__ssp_canary_word");
234 AlwaysPreserved
.insert("__stack_chk_guard");
236 // Mark all functions not in the api as internal.
237 IsWasm
= Triple(M
.getTargetTriple()).isOSBinFormatWasm();
238 for (Function
&I
: M
) {
239 if (!maybeInternalize(I
, ComdatMap
))
244 LLVM_DEBUG(dbgs() << "Internalizing func " << I
.getName() << "\n");
247 // Mark all global variables with initializers that are not in the api as
249 for (auto &GV
: M
.globals()) {
250 if (!maybeInternalize(GV
, ComdatMap
))
255 LLVM_DEBUG(dbgs() << "Internalized gvar " << GV
.getName() << "\n");
258 // Mark all aliases that are not in the api as internal as well.
259 for (auto &GA
: M
.aliases()) {
260 if (!maybeInternalize(GA
, ComdatMap
))
265 LLVM_DEBUG(dbgs() << "Internalized alias " << GA
.getName() << "\n");
271 InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {}
273 PreservedAnalyses
InternalizePass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
274 if (!internalizeModule(M
))
275 return PreservedAnalyses::all();
277 return PreservedAnalyses::none();