/src/llvm-project/llvm/lib/IR/Module.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Module.cpp - Implement the Module class ----------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the Module class for the IR library. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/IR/Module.h" |
14 | | #include "SymbolTableListTraitsImpl.h" |
15 | | #include "llvm/ADT/SmallString.h" |
16 | | #include "llvm/ADT/SmallVector.h" |
17 | | #include "llvm/ADT/StringMap.h" |
18 | | #include "llvm/ADT/StringRef.h" |
19 | | #include "llvm/ADT/Twine.h" |
20 | | #include "llvm/IR/Attributes.h" |
21 | | #include "llvm/IR/Comdat.h" |
22 | | #include "llvm/IR/Constants.h" |
23 | | #include "llvm/IR/DataLayout.h" |
24 | | #include "llvm/IR/DebugInfoMetadata.h" |
25 | | #include "llvm/IR/DerivedTypes.h" |
26 | | #include "llvm/IR/Function.h" |
27 | | #include "llvm/IR/GVMaterializer.h" |
28 | | #include "llvm/IR/GlobalAlias.h" |
29 | | #include "llvm/IR/GlobalIFunc.h" |
30 | | #include "llvm/IR/GlobalValue.h" |
31 | | #include "llvm/IR/GlobalVariable.h" |
32 | | #include "llvm/IR/LLVMContext.h" |
33 | | #include "llvm/IR/Metadata.h" |
34 | | #include "llvm/IR/ModuleSummaryIndex.h" |
35 | | #include "llvm/IR/SymbolTableListTraits.h" |
36 | | #include "llvm/IR/Type.h" |
37 | | #include "llvm/IR/TypeFinder.h" |
38 | | #include "llvm/IR/Value.h" |
39 | | #include "llvm/IR/ValueSymbolTable.h" |
40 | | #include "llvm/Support/Casting.h" |
41 | | #include "llvm/Support/CodeGen.h" |
42 | | #include "llvm/Support/Error.h" |
43 | | #include "llvm/Support/MemoryBuffer.h" |
44 | | #include "llvm/Support/Path.h" |
45 | | #include "llvm/Support/RandomNumberGenerator.h" |
46 | | #include "llvm/Support/VersionTuple.h" |
47 | | #include <algorithm> |
48 | | #include <cassert> |
49 | | #include <cstdint> |
50 | | #include <memory> |
51 | | #include <optional> |
52 | | #include <utility> |
53 | | #include <vector> |
54 | | |
55 | | using namespace llvm; |
56 | | |
57 | | //===----------------------------------------------------------------------===// |
58 | | // Methods to implement the globals and functions lists. |
59 | | // |
60 | | |
61 | | // Explicit instantiations of SymbolTableListTraits since some of the methods |
62 | | // are not in the public header file. |
63 | | template class llvm::SymbolTableListTraits<Function>; |
64 | | template class llvm::SymbolTableListTraits<GlobalVariable>; |
65 | | template class llvm::SymbolTableListTraits<GlobalAlias>; |
66 | | template class llvm::SymbolTableListTraits<GlobalIFunc>; |
67 | | |
68 | | //===----------------------------------------------------------------------===// |
69 | | // Primitive Module methods. |
70 | | // |
71 | | |
72 | | Module::Module(StringRef MID, LLVMContext &C) |
73 | | : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)), |
74 | | ModuleID(std::string(MID)), SourceFileName(std::string(MID)), DL(""), |
75 | 178k | IsNewDbgInfoFormat(false) { |
76 | 178k | Context.addModule(this); |
77 | 178k | } |
78 | | |
79 | 178k | Module::~Module() { |
80 | 178k | Context.removeModule(this); |
81 | 178k | dropAllReferences(); |
82 | 178k | GlobalList.clear(); |
83 | 178k | FunctionList.clear(); |
84 | 178k | AliasList.clear(); |
85 | 178k | IFuncList.clear(); |
86 | 178k | } |
87 | | |
88 | | std::unique_ptr<RandomNumberGenerator> |
89 | 0 | Module::createRNG(const StringRef Name) const { |
90 | 0 | SmallString<32> Salt(Name); |
91 | | |
92 | | // This RNG is guaranteed to produce the same random stream only |
93 | | // when the Module ID and thus the input filename is the same. This |
94 | | // might be problematic if the input filename extension changes |
95 | | // (e.g. from .c to .bc or .ll). |
96 | | // |
97 | | // We could store this salt in NamedMetadata, but this would make |
98 | | // the parameter non-const. This would unfortunately make this |
99 | | // interface unusable by any Machine passes, since they only have a |
100 | | // const reference to their IR Module. Alternatively we can always |
101 | | // store salt metadata from the Module constructor. |
102 | 0 | Salt += sys::path::filename(getModuleIdentifier()); |
103 | |
|
104 | 0 | return std::unique_ptr<RandomNumberGenerator>( |
105 | 0 | new RandomNumberGenerator(Salt)); |
106 | 0 | } |
107 | | |
108 | | /// getNamedValue - Return the first global value in the module with |
109 | | /// the specified name, of arbitrary type. This method returns null |
110 | | /// if a global with the specified name is not found. |
111 | 1.09M | GlobalValue *Module::getNamedValue(StringRef Name) const { |
112 | 1.09M | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
113 | 1.09M | } |
114 | | |
115 | 0 | unsigned Module::getNumNamedValues() const { |
116 | 0 | return getValueSymbolTable().size(); |
117 | 0 | } |
118 | | |
119 | | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
120 | | /// This ID is uniqued across modules in the current LLVMContext. |
121 | 5.85M | unsigned Module::getMDKindID(StringRef Name) const { |
122 | 5.85M | return Context.getMDKindID(Name); |
123 | 5.85M | } |
124 | | |
125 | | /// getMDKindNames - Populate client supplied SmallVector with the name for |
126 | | /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, |
127 | | /// so it is filled in as an empty string. |
128 | 0 | void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { |
129 | 0 | return Context.getMDKindNames(Result); |
130 | 0 | } |
131 | | |
132 | 0 | void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const { |
133 | 0 | return Context.getOperandBundleTags(Result); |
134 | 0 | } |
135 | | |
136 | | //===----------------------------------------------------------------------===// |
137 | | // Methods for easy access to the functions in the module. |
138 | | // |
139 | | |
140 | | // getOrInsertFunction - Look up the specified function in the module symbol |
141 | | // table. If it does not exist, add a prototype for the function and return |
142 | | // it. This is nice because it allows most passes to get away with not handling |
143 | | // the symbol table directly for this common task. |
144 | | // |
145 | | FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, |
146 | 65.3k | AttributeList AttributeList) { |
147 | | // See if we have a definition for the specified function already. |
148 | 65.3k | GlobalValue *F = getNamedValue(Name); |
149 | 65.3k | if (!F) { |
150 | | // Nope, add it |
151 | 27.2k | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, |
152 | 27.2k | DL.getProgramAddressSpace(), Name); |
153 | 27.2k | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
154 | 573 | New->setAttributes(AttributeList); |
155 | 27.2k | FunctionList.push_back(New); |
156 | 27.2k | return {Ty, New}; // Return the new prototype. |
157 | 27.2k | } |
158 | | |
159 | | // Otherwise, we just found the existing function or a prototype. |
160 | 38.1k | return {Ty, F}; |
161 | 65.3k | } |
162 | | |
163 | 64.3k | FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) { |
164 | 64.3k | return getOrInsertFunction(Name, Ty, AttributeList()); |
165 | 64.3k | } |
166 | | |
167 | | // getFunction - Look up the specified function in the module symbol table. |
168 | | // If it does not exist, return null. |
169 | | // |
170 | 920k | Function *Module::getFunction(StringRef Name) const { |
171 | 920k | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
172 | 920k | } |
173 | | |
174 | | //===----------------------------------------------------------------------===// |
175 | | // Methods for easy access to the global variables in the module. |
176 | | // |
177 | | |
178 | | /// getGlobalVariable - Look up the specified global variable in the module |
179 | | /// symbol table. If it does not exist, return null. The type argument |
180 | | /// should be the underlying type of the global, i.e., it should not have |
181 | | /// the top-level PointerType, which represents the address of the global. |
182 | | /// If AllowLocal is set to true, this function will return types that |
183 | | /// have an local. By default, these types are not returned. |
184 | | /// |
185 | | GlobalVariable *Module::getGlobalVariable(StringRef Name, |
186 | 66.2k | bool AllowLocal) const { |
187 | 66.2k | if (GlobalVariable *Result = |
188 | 66.2k | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
189 | 0 | if (AllowLocal || !Result->hasLocalLinkage()) |
190 | 0 | return Result; |
191 | 66.2k | return nullptr; |
192 | 66.2k | } |
193 | | |
194 | | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
195 | | /// 1. If it does not exist, add a declaration of the global and return it. |
196 | | /// 2. Else, the global exists but has the wrong type: return the function |
197 | | /// with a constantexpr cast to the right type. |
198 | | /// 3. Finally, if the existing global is the correct declaration, return the |
199 | | /// existing global. |
200 | | Constant *Module::getOrInsertGlobal( |
201 | | StringRef Name, Type *Ty, |
202 | 0 | function_ref<GlobalVariable *()> CreateGlobalCallback) { |
203 | | // See if we have a definition for the specified global already. |
204 | 0 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
205 | 0 | if (!GV) |
206 | 0 | GV = CreateGlobalCallback(); |
207 | 0 | assert(GV && "The CreateGlobalCallback is expected to create a global"); |
208 | | |
209 | | // Otherwise, we just found the existing function or a prototype. |
210 | 0 | return GV; |
211 | 0 | } |
212 | | |
213 | | // Overload to construct a global variable using its constructor's defaults. |
214 | 0 | Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { |
215 | 0 | return getOrInsertGlobal(Name, Ty, [&] { |
216 | 0 | return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
217 | 0 | nullptr, Name); |
218 | 0 | }); |
219 | 0 | } |
220 | | |
221 | | //===----------------------------------------------------------------------===// |
222 | | // Methods for easy access to the global variables in the module. |
223 | | // |
224 | | |
225 | | // getNamedAlias - Look up the specified global in the module symbol table. |
226 | | // If it does not exist, return null. |
227 | | // |
228 | 0 | GlobalAlias *Module::getNamedAlias(StringRef Name) const { |
229 | 0 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
230 | 0 | } |
231 | | |
232 | 0 | GlobalIFunc *Module::getNamedIFunc(StringRef Name) const { |
233 | 0 | return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name)); |
234 | 0 | } |
235 | | |
236 | | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
237 | | /// specified name. This method returns null if a NamedMDNode with the |
238 | | /// specified name is not found. |
239 | 7.72M | NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { |
240 | 7.72M | SmallString<256> NameData; |
241 | 7.72M | StringRef NameRef = Name.toStringRef(NameData); |
242 | 7.72M | return NamedMDSymTab.lookup(NameRef); |
243 | 7.72M | } |
244 | | |
245 | | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
246 | | /// with the specified name. This method returns a new NamedMDNode if a |
247 | | /// NamedMDNode with the specified name is not found. |
248 | 45.7k | NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { |
249 | 45.7k | NamedMDNode *&NMD = NamedMDSymTab[Name]; |
250 | 45.7k | if (!NMD) { |
251 | 18.6k | NMD = new NamedMDNode(Name); |
252 | 18.6k | NMD->setParent(this); |
253 | 18.6k | insertNamedMDNode(NMD); |
254 | 18.6k | } |
255 | 45.7k | return NMD; |
256 | 45.7k | } |
257 | | |
258 | | /// eraseNamedMetadata - Remove the given NamedMDNode from this module and |
259 | | /// delete it. |
260 | 600 | void Module::eraseNamedMetadata(NamedMDNode *NMD) { |
261 | 600 | NamedMDSymTab.erase(NMD->getName()); |
262 | 600 | eraseNamedMDNode(NMD); |
263 | 600 | } |
264 | | |
265 | 374k | bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { |
266 | 374k | if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) { |
267 | 374k | uint64_t Val = Behavior->getLimitedValue(); |
268 | 374k | if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { |
269 | 374k | MFB = static_cast<ModFlagBehavior>(Val); |
270 | 374k | return true; |
271 | 374k | } |
272 | 374k | } |
273 | 379 | return false; |
274 | 374k | } |
275 | | |
276 | | bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB, |
277 | 338k | MDString *&Key, Metadata *&Val) { |
278 | 338k | if (ModFlag.getNumOperands() < 3) |
279 | 0 | return false; |
280 | 338k | if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB)) |
281 | 316 | return false; |
282 | 337k | MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1)); |
283 | 337k | if (!K) |
284 | 21 | return false; |
285 | 337k | Key = K; |
286 | 337k | Val = ModFlag.getOperand(2); |
287 | 337k | return true; |
288 | 337k | } |
289 | | |
290 | | /// getModuleFlagsMetadata - Returns the module flags in the provided vector. |
291 | | void Module:: |
292 | 4.97M | getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { |
293 | 4.97M | const NamedMDNode *ModFlags = getModuleFlagsMetadata(); |
294 | 4.97M | if (!ModFlags) return; |
295 | | |
296 | 338k | for (const MDNode *Flag : ModFlags->operands()) { |
297 | 338k | ModFlagBehavior MFB; |
298 | 338k | MDString *Key = nullptr; |
299 | 338k | Metadata *Val = nullptr; |
300 | 338k | if (isValidModuleFlag(*Flag, MFB, Key, Val)) { |
301 | | // Check the operands of the MDNode before accessing the operands. |
302 | | // The verifier will actually catch these failures. |
303 | 337k | Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); |
304 | 337k | } |
305 | 338k | } |
306 | 163k | } |
307 | | |
308 | | /// Return the corresponding value if Key appears in module flags, otherwise |
309 | | /// return null. |
310 | 4.90M | Metadata *Module::getModuleFlag(StringRef Key) const { |
311 | 4.90M | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
312 | 4.90M | getModuleFlagsMetadata(ModuleFlags); |
313 | 4.90M | for (const ModuleFlagEntry &MFE : ModuleFlags) { |
314 | 327k | if (Key == MFE.Key->getString()) |
315 | 10.8k | return MFE.Val; |
316 | 327k | } |
317 | 4.89M | return nullptr; |
318 | 4.90M | } |
319 | | |
320 | | /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that |
321 | | /// represents module-level flags. This method returns null if there are no |
322 | | /// module-level flags. |
323 | 5.39M | NamedMDNode *Module::getModuleFlagsMetadata() const { |
324 | 5.39M | return getNamedMetadata("llvm.module.flags"); |
325 | 5.39M | } |
326 | | |
327 | | /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that |
328 | | /// represents module-level flags. If module-level flags aren't found, it |
329 | | /// creates the named metadata that contains them. |
330 | 212 | NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { |
331 | 212 | return getOrInsertNamedMetadata("llvm.module.flags"); |
332 | 212 | } |
333 | | |
334 | | /// addModuleFlag - Add a module-level flag to the module-level flags |
335 | | /// metadata. It will create the module-level flags named metadata if it doesn't |
336 | | /// already exist. |
337 | | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
338 | 212 | Metadata *Val) { |
339 | 212 | Type *Int32Ty = Type::getInt32Ty(Context); |
340 | 212 | Metadata *Ops[3] = { |
341 | 212 | ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), |
342 | 212 | MDString::get(Context, Key), Val}; |
343 | 212 | getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); |
344 | 212 | } |
345 | | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
346 | 212 | Constant *Val) { |
347 | 212 | addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); |
348 | 212 | } |
349 | | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
350 | 212 | uint32_t Val) { |
351 | 212 | Type *Int32Ty = Type::getInt32Ty(Context); |
352 | 212 | addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); |
353 | 212 | } |
354 | 0 | void Module::addModuleFlag(MDNode *Node) { |
355 | 0 | assert(Node->getNumOperands() == 3 && |
356 | 0 | "Invalid number of operands for module flag!"); |
357 | 0 | assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) && |
358 | 0 | isa<MDString>(Node->getOperand(1)) && |
359 | 0 | "Invalid operand types for module flag!"); |
360 | 0 | getOrInsertModuleFlagsMetadata()->addOperand(Node); |
361 | 0 | } |
362 | | |
363 | | void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
364 | 0 | Metadata *Val) { |
365 | 0 | NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata(); |
366 | | // Replace the flag if it already exists. |
367 | 0 | for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { |
368 | 0 | MDNode *Flag = ModFlags->getOperand(I); |
369 | 0 | ModFlagBehavior MFB; |
370 | 0 | MDString *K = nullptr; |
371 | 0 | Metadata *V = nullptr; |
372 | 0 | if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) { |
373 | 0 | Flag->replaceOperandWith(2, Val); |
374 | 0 | return; |
375 | 0 | } |
376 | 0 | } |
377 | 0 | addModuleFlag(Behavior, Key, Val); |
378 | 0 | } |
379 | | |
380 | 46 | void Module::setDataLayout(StringRef Desc) { |
381 | 46 | DL.reset(Desc); |
382 | 46 | } |
383 | | |
384 | 471k | void Module::setDataLayout(const DataLayout &Other) { DL = Other; } |
385 | | |
386 | 1.49k | DICompileUnit *Module::debug_compile_units_iterator::operator*() const { |
387 | 1.49k | return cast<DICompileUnit>(CUs->getOperand(Idx)); |
388 | 1.49k | } |
389 | 3.86k | DICompileUnit *Module::debug_compile_units_iterator::operator->() const { |
390 | 3.86k | return cast<DICompileUnit>(CUs->getOperand(Idx)); |
391 | 3.86k | } |
392 | | |
393 | 79.3k | void Module::debug_compile_units_iterator::SkipNoDebugCUs() { |
394 | 79.4k | while (CUs && (Idx < CUs->getNumOperands()) && |
395 | 79.4k | ((*this)->getEmissionKind() == DICompileUnit::NoDebug)) |
396 | 57 | ++Idx; |
397 | 79.3k | } |
398 | | |
399 | 33.6k | iterator_range<Module::global_object_iterator> Module::global_objects() { |
400 | 33.6k | return concat<GlobalObject>(functions(), globals()); |
401 | 33.6k | } |
402 | | iterator_range<Module::const_global_object_iterator> |
403 | 10.9k | Module::global_objects() const { |
404 | 10.9k | return concat<const GlobalObject>(functions(), globals()); |
405 | 10.9k | } |
406 | | |
407 | 34.3k | iterator_range<Module::global_value_iterator> Module::global_values() { |
408 | 34.3k | return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs()); |
409 | 34.3k | } |
410 | | iterator_range<Module::const_global_value_iterator> |
411 | 0 | Module::global_values() const { |
412 | 0 | return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs()); |
413 | 0 | } |
414 | | |
415 | | //===----------------------------------------------------------------------===// |
416 | | // Methods to control the materialization of GlobalValues in the Module. |
417 | | // |
418 | 163k | void Module::setMaterializer(GVMaterializer *GVM) { |
419 | 163k | assert(!Materializer && |
420 | 163k | "Module already has a GVMaterializer. Call materializeAll" |
421 | 163k | " to clear it out before setting another one."); |
422 | 0 | Materializer.reset(GVM); |
423 | 163k | } |
424 | | |
425 | 0 | Error Module::materialize(GlobalValue *GV) { |
426 | 0 | if (!Materializer) |
427 | 0 | return Error::success(); |
428 | | |
429 | 0 | return Materializer->materialize(GV); |
430 | 0 | } |
431 | | |
432 | 156k | Error Module::materializeAll() { |
433 | 156k | if (!Materializer) |
434 | 0 | return Error::success(); |
435 | 156k | std::unique_ptr<GVMaterializer> M = std::move(Materializer); |
436 | 156k | return M->materializeModule(); |
437 | 156k | } |
438 | | |
439 | 0 | Error Module::materializeMetadata() { |
440 | 0 | if (!Materializer) |
441 | 0 | return Error::success(); |
442 | 0 | return Materializer->materializeMetadata(); |
443 | 0 | } |
444 | | |
445 | | //===----------------------------------------------------------------------===// |
446 | | // Other module related stuff. |
447 | | // |
448 | | |
449 | 0 | std::vector<StructType *> Module::getIdentifiedStructTypes() const { |
450 | | // If we have a materializer, it is possible that some unread function |
451 | | // uses a type that is currently not visible to a TypeFinder, so ask |
452 | | // the materializer which types it created. |
453 | 0 | if (Materializer) |
454 | 0 | return Materializer->getIdentifiedStructTypes(); |
455 | | |
456 | 0 | std::vector<StructType *> Ret; |
457 | 0 | TypeFinder SrcStructTypes; |
458 | 0 | SrcStructTypes.run(*this, true); |
459 | 0 | Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); |
460 | 0 | return Ret; |
461 | 0 | } |
462 | | |
463 | | std::string Module::getUniqueIntrinsicName(StringRef BaseName, Intrinsic::ID Id, |
464 | 2 | const FunctionType *Proto) { |
465 | 2 | auto Encode = [&BaseName](unsigned Suffix) { |
466 | 2 | return (Twine(BaseName) + "." + Twine(Suffix)).str(); |
467 | 2 | }; |
468 | | |
469 | 2 | { |
470 | | // fast path - the prototype is already known |
471 | 2 | auto UinItInserted = UniquedIntrinsicNames.insert({{Id, Proto}, 0}); |
472 | 2 | if (!UinItInserted.second) |
473 | 1 | return Encode(UinItInserted.first->second); |
474 | 2 | } |
475 | | |
476 | | // Not known yet. A new entry was created with index 0. Check if there already |
477 | | // exists a matching declaration, or select a new entry. |
478 | | |
479 | | // Start looking for names with the current known maximum count (or 0). |
480 | 1 | auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0}); |
481 | 1 | unsigned Count = NiidItInserted.first->second; |
482 | | |
483 | | // This might be slow if a whole population of intrinsics already existed, but |
484 | | // we cache the values for later usage. |
485 | 1 | std::string NewName; |
486 | 1 | while (true) { |
487 | 1 | NewName = Encode(Count); |
488 | 1 | GlobalValue *F = getNamedValue(NewName); |
489 | 1 | if (!F) { |
490 | | // Reserve this entry for the new proto |
491 | 1 | UniquedIntrinsicNames[{Id, Proto}] = Count; |
492 | 1 | break; |
493 | 1 | } |
494 | | |
495 | | // A declaration with this name already exists. Remember it. |
496 | 0 | FunctionType *FT = dyn_cast<FunctionType>(F->getValueType()); |
497 | 0 | auto UinItInserted = UniquedIntrinsicNames.insert({{Id, FT}, Count}); |
498 | 0 | if (FT == Proto) { |
499 | | // It was a declaration for our prototype. This entry was allocated in the |
500 | | // beginning. Update the count to match the existing declaration. |
501 | 0 | UinItInserted.first->second = Count; |
502 | 0 | break; |
503 | 0 | } |
504 | | |
505 | 0 | ++Count; |
506 | 0 | } |
507 | | |
508 | 1 | NiidItInserted.first->second = Count + 1; |
509 | | |
510 | 1 | return NewName; |
511 | 2 | } |
512 | | |
513 | | // dropAllReferences() - This function causes all the subelements to "let go" |
514 | | // of all references that they are maintaining. This allows one to 'delete' a |
515 | | // whole module at a time, even though there may be circular references... first |
516 | | // all references are dropped, and all use counts go to zero. Then everything |
517 | | // is deleted for real. Note that no operations are valid on an object that |
518 | | // has "dropped all references", except operator delete. |
519 | | // |
520 | 178k | void Module::dropAllReferences() { |
521 | 178k | for (Function &F : *this) |
522 | 1.22M | F.dropAllReferences(); |
523 | | |
524 | 178k | for (GlobalVariable &GV : globals()) |
525 | 983k | GV.dropAllReferences(); |
526 | | |
527 | 178k | for (GlobalAlias &GA : aliases()) |
528 | 557 | GA.dropAllReferences(); |
529 | | |
530 | 178k | for (GlobalIFunc &GIF : ifuncs()) |
531 | 10 | GIF.dropAllReferences(); |
532 | 178k | } |
533 | | |
534 | 1.02k | unsigned Module::getNumberRegisterParameters() const { |
535 | 1.02k | auto *Val = |
536 | 1.02k | cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters")); |
537 | 1.02k | if (!Val) |
538 | 1.02k | return 0; |
539 | 0 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
540 | 1.02k | } |
541 | | |
542 | 816 | unsigned Module::getDwarfVersion() const { |
543 | 816 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version")); |
544 | 816 | if (!Val) |
545 | 503 | return 0; |
546 | 313 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
547 | 816 | } |
548 | | |
549 | 748 | bool Module::isDwarf64() const { |
550 | 748 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("DWARF64")); |
551 | 748 | return Val && cast<ConstantInt>(Val->getValue())->isOne(); |
552 | 748 | } |
553 | | |
554 | 34.4k | unsigned Module::getCodeViewFlag() const { |
555 | 34.4k | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView")); |
556 | 34.4k | if (!Val) |
557 | 34.3k | return 0; |
558 | 68 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
559 | 34.4k | } |
560 | | |
561 | 0 | unsigned Module::getInstructionCount() const { |
562 | 0 | unsigned NumInstrs = 0; |
563 | 0 | for (const Function &F : FunctionList) |
564 | 0 | NumInstrs += F.getInstructionCount(); |
565 | 0 | return NumInstrs; |
566 | 0 | } |
567 | | |
568 | 1.10k | Comdat *Module::getOrInsertComdat(StringRef Name) { |
569 | 1.10k | auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first; |
570 | 1.10k | Entry.second.Name = &Entry; |
571 | 1.10k | return &Entry.second; |
572 | 1.10k | } |
573 | | |
574 | 1.34M | PICLevel::Level Module::getPICLevel() const { |
575 | 1.34M | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level")); |
576 | | |
577 | 1.34M | if (!Val) |
578 | 1.33M | return PICLevel::NotPIC; |
579 | | |
580 | 4.87k | return static_cast<PICLevel::Level>( |
581 | 4.87k | cast<ConstantInt>(Val->getValue())->getZExtValue()); |
582 | 1.34M | } |
583 | | |
584 | 0 | void Module::setPICLevel(PICLevel::Level PL) { |
585 | | // The merge result of a non-PIC object and a PIC object can only be reliably |
586 | | // used as a non-PIC object, so use the Min merge behavior. |
587 | 0 | addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL); |
588 | 0 | } |
589 | | |
590 | 26.7k | PIELevel::Level Module::getPIELevel() const { |
591 | 26.7k | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level")); |
592 | | |
593 | 26.7k | if (!Val) |
594 | 26.7k | return PIELevel::Default; |
595 | | |
596 | 0 | return static_cast<PIELevel::Level>( |
597 | 0 | cast<ConstantInt>(Val->getValue())->getZExtValue()); |
598 | 26.7k | } |
599 | | |
600 | 0 | void Module::setPIELevel(PIELevel::Level PL) { |
601 | 0 | addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL); |
602 | 0 | } |
603 | | |
604 | 0 | std::optional<CodeModel::Model> Module::getCodeModel() const { |
605 | 0 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model")); |
606 | |
|
607 | 0 | if (!Val) |
608 | 0 | return std::nullopt; |
609 | | |
610 | 0 | return static_cast<CodeModel::Model>( |
611 | 0 | cast<ConstantInt>(Val->getValue())->getZExtValue()); |
612 | 0 | } |
613 | | |
614 | 0 | void Module::setCodeModel(CodeModel::Model CL) { |
615 | | // Linking object files with different code models is undefined behavior |
616 | | // because the compiler would have to generate additional code (to span |
617 | | // longer jumps) if a larger code model is used with a smaller one. |
618 | | // Therefore we will treat attempts to mix code models as an error. |
619 | 0 | addModuleFlag(ModFlagBehavior::Error, "Code Model", CL); |
620 | 0 | } |
621 | | |
622 | 0 | std::optional<uint64_t> Module::getLargeDataThreshold() const { |
623 | 0 | auto *Val = |
624 | 0 | cast_or_null<ConstantAsMetadata>(getModuleFlag("Large Data Threshold")); |
625 | |
|
626 | 0 | if (!Val) |
627 | 0 | return std::nullopt; |
628 | | |
629 | 0 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
630 | 0 | } |
631 | | |
632 | 0 | void Module::setLargeDataThreshold(uint64_t Threshold) { |
633 | | // Since the large data threshold goes along with the code model, the merge |
634 | | // behavior is the same. |
635 | 0 | addModuleFlag(ModFlagBehavior::Error, "Large Data Threshold", |
636 | 0 | ConstantInt::get(Type::getInt64Ty(Context), Threshold)); |
637 | 0 | } |
638 | | |
639 | 0 | void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) { |
640 | 0 | if (Kind == ProfileSummary::PSK_CSInstr) |
641 | 0 | setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M); |
642 | 0 | else |
643 | 0 | setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M); |
644 | 0 | } |
645 | | |
646 | 68.8k | Metadata *Module::getProfileSummary(bool IsCS) const { |
647 | 68.8k | return (IsCS ? getModuleFlag("CSProfileSummary") |
648 | 68.8k | : getModuleFlag("ProfileSummary")); |
649 | 68.8k | } |
650 | | |
651 | 751k | bool Module::getSemanticInterposition() const { |
652 | 751k | Metadata *MF = getModuleFlag("SemanticInterposition"); |
653 | | |
654 | 751k | auto *Val = cast_or_null<ConstantAsMetadata>(MF); |
655 | 751k | if (!Val) |
656 | 751k | return false; |
657 | | |
658 | 0 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
659 | 751k | } |
660 | | |
661 | 0 | void Module::setSemanticInterposition(bool SI) { |
662 | 0 | addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI); |
663 | 0 | } |
664 | | |
665 | 0 | void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) { |
666 | 0 | OwnedMemoryBuffer = std::move(MB); |
667 | 0 | } |
668 | | |
669 | 2.08k | bool Module::getRtLibUseGOT() const { |
670 | 2.08k | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT")); |
671 | 2.08k | return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0); |
672 | 2.08k | } |
673 | | |
674 | 0 | void Module::setRtLibUseGOT() { |
675 | 0 | addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1); |
676 | 0 | } |
677 | | |
678 | 157 | bool Module::getDirectAccessExternalData() const { |
679 | 157 | auto *Val = cast_or_null<ConstantAsMetadata>( |
680 | 157 | getModuleFlag("direct-access-external-data")); |
681 | 157 | if (Val) |
682 | 0 | return cast<ConstantInt>(Val->getValue())->getZExtValue() > 0; |
683 | 157 | return getPICLevel() == PICLevel::NotPIC; |
684 | 157 | } |
685 | | |
686 | 0 | void Module::setDirectAccessExternalData(bool Value) { |
687 | 0 | addModuleFlag(ModFlagBehavior::Max, "direct-access-external-data", Value); |
688 | 0 | } |
689 | | |
690 | 0 | UWTableKind Module::getUwtable() const { |
691 | 0 | if (auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable"))) |
692 | 0 | return UWTableKind(cast<ConstantInt>(Val->getValue())->getZExtValue()); |
693 | 0 | return UWTableKind::None; |
694 | 0 | } |
695 | | |
696 | 0 | void Module::setUwtable(UWTableKind Kind) { |
697 | 0 | addModuleFlag(ModFlagBehavior::Max, "uwtable", uint32_t(Kind)); |
698 | 0 | } |
699 | | |
700 | 0 | FramePointerKind Module::getFramePointer() const { |
701 | 0 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("frame-pointer")); |
702 | 0 | return static_cast<FramePointerKind>( |
703 | 0 | Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0); |
704 | 0 | } |
705 | | |
706 | 0 | void Module::setFramePointer(FramePointerKind Kind) { |
707 | 0 | addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind)); |
708 | 0 | } |
709 | | |
710 | 286 | StringRef Module::getStackProtectorGuard() const { |
711 | 286 | Metadata *MD = getModuleFlag("stack-protector-guard"); |
712 | 286 | if (auto *MDS = dyn_cast_or_null<MDString>(MD)) |
713 | 0 | return MDS->getString(); |
714 | 286 | return {}; |
715 | 286 | } |
716 | | |
717 | 0 | void Module::setStackProtectorGuard(StringRef Kind) { |
718 | 0 | MDString *ID = MDString::get(getContext(), Kind); |
719 | 0 | addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard", ID); |
720 | 0 | } |
721 | | |
722 | 0 | StringRef Module::getStackProtectorGuardReg() const { |
723 | 0 | Metadata *MD = getModuleFlag("stack-protector-guard-reg"); |
724 | 0 | if (auto *MDS = dyn_cast_or_null<MDString>(MD)) |
725 | 0 | return MDS->getString(); |
726 | 0 | return {}; |
727 | 0 | } |
728 | | |
729 | 0 | void Module::setStackProtectorGuardReg(StringRef Reg) { |
730 | 0 | MDString *ID = MDString::get(getContext(), Reg); |
731 | 0 | addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-reg", ID); |
732 | 0 | } |
733 | | |
734 | 0 | StringRef Module::getStackProtectorGuardSymbol() const { |
735 | 0 | Metadata *MD = getModuleFlag("stack-protector-guard-symbol"); |
736 | 0 | if (auto *MDS = dyn_cast_or_null<MDString>(MD)) |
737 | 0 | return MDS->getString(); |
738 | 0 | return {}; |
739 | 0 | } |
740 | | |
741 | 0 | void Module::setStackProtectorGuardSymbol(StringRef Symbol) { |
742 | 0 | MDString *ID = MDString::get(getContext(), Symbol); |
743 | 0 | addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-symbol", ID); |
744 | 0 | } |
745 | | |
746 | 0 | int Module::getStackProtectorGuardOffset() const { |
747 | 0 | Metadata *MD = getModuleFlag("stack-protector-guard-offset"); |
748 | 0 | if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD)) |
749 | 0 | return CI->getSExtValue(); |
750 | 0 | return INT_MAX; |
751 | 0 | } |
752 | | |
753 | 0 | void Module::setStackProtectorGuardOffset(int Offset) { |
754 | 0 | addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-offset", Offset); |
755 | 0 | } |
756 | | |
757 | 160 | unsigned Module::getOverrideStackAlignment() const { |
758 | 160 | Metadata *MD = getModuleFlag("override-stack-alignment"); |
759 | 160 | if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD)) |
760 | 0 | return CI->getZExtValue(); |
761 | 160 | return 0; |
762 | 160 | } |
763 | | |
764 | 0 | unsigned Module::getMaxTLSAlignment() const { |
765 | 0 | Metadata *MD = getModuleFlag("MaxTLSAlign"); |
766 | 0 | if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD)) |
767 | 0 | return CI->getZExtValue(); |
768 | 0 | return 0; |
769 | 0 | } |
770 | | |
771 | 0 | void Module::setOverrideStackAlignment(unsigned Align) { |
772 | 0 | addModuleFlag(ModFlagBehavior::Error, "override-stack-alignment", Align); |
773 | 0 | } |
774 | | |
775 | 0 | static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name) { |
776 | 0 | SmallVector<unsigned, 3> Entries; |
777 | 0 | Entries.push_back(V.getMajor()); |
778 | 0 | if (auto Minor = V.getMinor()) { |
779 | 0 | Entries.push_back(*Minor); |
780 | 0 | if (auto Subminor = V.getSubminor()) |
781 | 0 | Entries.push_back(*Subminor); |
782 | | // Ignore the 'build' component as it can't be represented in the object |
783 | | // file. |
784 | 0 | } |
785 | 0 | M.addModuleFlag(Module::ModFlagBehavior::Warning, Name, |
786 | 0 | ConstantDataArray::get(M.getContext(), Entries)); |
787 | 0 | } |
788 | | |
789 | 0 | void Module::setSDKVersion(const VersionTuple &V) { |
790 | 0 | addSDKVersionMD(V, *this, "SDK Version"); |
791 | 0 | } |
792 | | |
793 | 68.8k | static VersionTuple getSDKVersionMD(Metadata *MD) { |
794 | 68.8k | auto *CM = dyn_cast_or_null<ConstantAsMetadata>(MD); |
795 | 68.8k | if (!CM) |
796 | 68.8k | return {}; |
797 | 1 | auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue()); |
798 | 1 | if (!Arr) |
799 | 0 | return {}; |
800 | 3 | auto getVersionComponent = [&](unsigned Index) -> std::optional<unsigned> { |
801 | 3 | if (Index >= Arr->getNumElements()) |
802 | 1 | return std::nullopt; |
803 | 2 | return (unsigned)Arr->getElementAsInteger(Index); |
804 | 3 | }; |
805 | 1 | auto Major = getVersionComponent(0); |
806 | 1 | if (!Major) |
807 | 0 | return {}; |
808 | 1 | VersionTuple Result = VersionTuple(*Major); |
809 | 1 | if (auto Minor = getVersionComponent(1)) { |
810 | 1 | Result = VersionTuple(*Major, *Minor); |
811 | 1 | if (auto Subminor = getVersionComponent(2)) { |
812 | 0 | Result = VersionTuple(*Major, *Minor, *Subminor); |
813 | 0 | } |
814 | 1 | } |
815 | 1 | return Result; |
816 | 1 | } |
817 | | |
818 | 34.4k | VersionTuple Module::getSDKVersion() const { |
819 | 34.4k | return getSDKVersionMD(getModuleFlag("SDK Version")); |
820 | 34.4k | } |
821 | | |
822 | | GlobalVariable *llvm::collectUsedGlobalVariables( |
823 | 50.1k | const Module &M, SmallVectorImpl<GlobalValue *> &Vec, bool CompilerUsed) { |
824 | 50.1k | const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used"; |
825 | 50.1k | GlobalVariable *GV = M.getGlobalVariable(Name); |
826 | 50.1k | if (!GV || !GV->hasInitializer()) |
827 | 50.1k | return GV; |
828 | | |
829 | 0 | const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
830 | 0 | for (Value *Op : Init->operands()) { |
831 | 0 | GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts()); |
832 | 0 | Vec.push_back(G); |
833 | 0 | } |
834 | 0 | return GV; |
835 | 50.1k | } |
836 | | |
837 | 0 | void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) { |
838 | 0 | if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) { |
839 | 0 | std::unique_ptr<ProfileSummary> ProfileSummary( |
840 | 0 | ProfileSummary::getFromMD(SummaryMD)); |
841 | 0 | if (ProfileSummary) { |
842 | 0 | if (ProfileSummary->getKind() != ProfileSummary::PSK_Sample || |
843 | 0 | !ProfileSummary->isPartialProfile()) |
844 | 0 | return; |
845 | 0 | uint64_t BlockCount = Index.getBlockCount(); |
846 | 0 | uint32_t NumCounts = ProfileSummary->getNumCounts(); |
847 | 0 | if (!NumCounts) |
848 | 0 | return; |
849 | 0 | double Ratio = (double)BlockCount / NumCounts; |
850 | 0 | ProfileSummary->setPartialProfileRatio(Ratio); |
851 | 0 | setProfileSummary(ProfileSummary->getMD(getContext()), |
852 | 0 | ProfileSummary::PSK_Sample); |
853 | 0 | } |
854 | 0 | } |
855 | 0 | } |
856 | | |
857 | 68.8k | StringRef Module::getDarwinTargetVariantTriple() const { |
858 | 68.8k | if (const auto *MD = getModuleFlag("darwin.target_variant.triple")) |
859 | 0 | return cast<MDString>(MD)->getString(); |
860 | 68.8k | return ""; |
861 | 68.8k | } |
862 | | |
863 | 0 | void Module::setDarwinTargetVariantTriple(StringRef T) { |
864 | 0 | addModuleFlag(ModFlagBehavior::Override, "darwin.target_variant.triple", |
865 | 0 | MDString::get(getContext(), T)); |
866 | 0 | } |
867 | | |
868 | 34.4k | VersionTuple Module::getDarwinTargetVariantSDKVersion() const { |
869 | 34.4k | return getSDKVersionMD(getModuleFlag("darwin.target_variant.SDK Version")); |
870 | 34.4k | } |
871 | | |
872 | 0 | void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) { |
873 | 0 | addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version"); |
874 | 0 | } |