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