LLVM: lib/Transforms/IPO/Internalize.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
34using namespace llvm;
35
36#define DEBUG_TYPE "internalize"
37
38STATISTIC(NumAliases, "Number of aliases internalized");
39STATISTIC(NumFunctions, "Number of functions internalized");
40STATISTIC(NumGlobals, "Number of global vars internalized");
41
42
43
46 cl::desc("A file containing list of symbol names to preserve"));
47
48
52
53namespace {
54
55
56class PreserveAPIList {
57public:
58 PreserveAPIList() {
63 }
64
65 bool operator()(const GlobalValue &GV) {
67 ExternalNames, [&](GlobPattern &GP) { return GP.match(GV.getName()); });
68 }
69
70private:
71
73
74 void addGlob(StringRef Pattern) {
76 if (!GlobOrErr) {
77 errs() << "WARNING: when loading pattern: '"
78 << toString(GlobOrErr.takeError()) << "' ignoring";
79 return;
80 }
81 ExternalNames.emplace_back(std::move(*GlobOrErr));
82 }
83
84 void LoadFile(StringRef Filename) {
85
86 ErrorOr<std::unique_ptr> BufOrErr =
88 if (!BufOrErr) {
89 errs() << "WARNING: Internalize couldn't load file '" << Filename
90 << "'! Continuing as if it's empty.\n";
91 return;
92 }
93 Buf = std::move(*BufOrErr);
94 for (line_iterator I(*Buf, true), E; I != E; ++I)
95 addGlob(*I);
96 }
97
98 std::shared_ptr Buf;
99};
100}
101
102bool InternalizePass::shouldPreserveGV(const GlobalValue &GV) {
103
105 return true;
106
107
109 return true;
110
111
113 return true;
114
115
116
118 if (G->isExternallyInitialized())
119 return true;
120
121
123 return false;
124
125
126 if (AlwaysPreserved.count(GV.getName()))
127 return true;
128
129 return MustPreserveGV(GV);
130}
131
132bool InternalizePass::maybeInternalize(
135
136
137 if (ComdatMap.lookup(C).External)
138 return false;
139
141
142
143
144
145
146
147 ComdatInfo &Info = ComdatMap.find(C)->second;
148 if (Info.Size == 1)
149 GO->setComdat(nullptr);
150 else if (!IsWasm)
152 }
153
155 return false;
156 } else {
158 return false;
159
160 if (shouldPreserveGV(GV))
161 return false;
162 }
163
166 return true;
167}
168
169
170
171void InternalizePass::checkComdat(
174 if ()
175 return;
176
177 ComdatInfo &Info = ComdatMap[C];
179 if (shouldPreserveGV(GV))
180 Info.External = true;
181}
182
185
188
189
191 if (!M.getComdatSymbolTable().empty()) {
193 checkComdat(F, ComdatMap);
195 checkComdat(GV, ComdatMap);
197 checkComdat(GA, ComdatMap);
198 }
199
200
201
202
203
204
205
206
207
208
210 AlwaysPreserved.insert(V->getName());
211 }
212
213
214
215
216 AlwaysPreserved.insert("llvm.used");
217 AlwaysPreserved.insert("llvm.compiler.used");
218
219
220
221 AlwaysPreserved.insert("llvm.global_ctors");
222 AlwaysPreserved.insert("llvm.global_dtors");
223 AlwaysPreserved.insert("llvm.global.annotations");
224
225
226
227
228 AlwaysPreserved.insert("__stack_chk_fail");
229 if (M.getTargetTriple().isOSAIX())
230 AlwaysPreserved.insert("__ssp_canary_word");
231 else
232 AlwaysPreserved.insert("__stack_chk_guard");
233
234
235 if (M.getTargetTriple().isNVPTX())
236 AlwaysPreserved.insert("__llvm_rpc_client");
237
238
239 IsWasm = M.getTargetTriple().isOSBinFormatWasm();
241 if (!maybeInternalize(I, ComdatMap))
242 continue;
244
245 ++NumFunctions;
246 LLVM_DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n");
247 }
248
249
250
251 for (auto &GV : M.globals()) {
252 if (!maybeInternalize(GV, ComdatMap))
253 continue;
255
256 ++NumGlobals;
258 }
259
260
261 for (auto &GA : M.aliases()) {
262 if (!maybeInternalize(GA, ComdatMap))
263 continue;
265
266 ++NumAliases;
267 LLVM_DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n");
268 }
269
271}
272
274
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
Module.h This file contains the declarations for the Module class.
static cl::list< std::string > APIList("internalize-public-api-list", cl::value_desc("list"), cl::desc("A list of symbol names to preserve"), cl::CommaSeparated)
static cl::opt< std::string > APIFile("internalize-public-api-file", cl::value_desc("filename"), cl::desc("A file containing list of symbol names to preserve"))
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
StringSet - A set-like wrapper for the StringMap.
@ NoDeduplicate
No deduplication is performed.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
LLVM_ABI bool match(StringRef S) const
static LLVM_ABI Expected< GlobPattern > create(StringRef Pat, std::optional< size_t > MaxSubPatterns={})
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
bool hasLocalLinkage() const
LLVM_ABI const Comdat * getComdat() const
void setLinkage(LinkageTypes LT)
bool hasDLLExportStorageClass() const
@ DefaultVisibility
The GV is visible.
void setVisibility(VisibilityTypes V)
bool hasAvailableExternallyLinkage() const
@ InternalLinkage
Rename collisions when linking (static functions).
LLVM_ABI InternalizePass()
Definition Internalize.cpp:273
LLVM_ABI bool internalizeModule(Module &TheModule)
Run the internalizer on TheModule, returns true if any changes was made.
Definition Internalize.cpp:183
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition Internalize.cpp:275
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...