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/Optional.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/IR/Attributes.h" 23 #include "llvm/IR/Comdat.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/DataLayout.h" 26 #include "llvm/IR/DebugInfoMetadata.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/GVMaterializer.h" 30 #include "llvm/IR/GlobalAlias.h" 31 #include "llvm/IR/GlobalIFunc.h" 32 #include "llvm/IR/GlobalValue.h" 33 #include "llvm/IR/GlobalVariable.h" 34 #include "llvm/IR/LLVMContext.h" 35 #include "llvm/IR/Metadata.h" 36 #include "llvm/IR/SymbolTableListTraits.h" 37 #include "llvm/IR/Type.h" 38 #include "llvm/IR/TypeFinder.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/IR/ValueSymbolTable.h" 41 #include "llvm/Pass.h" 42 #include "llvm/Support/Casting.h" 43 #include "llvm/Support/CodeGen.h" 44 #include "llvm/Support/Error.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/Path.h" 47 #include "llvm/Support/RandomNumberGenerator.h" 48 #include "llvm/Support/VersionTuple.h" 49 #include <algorithm> 50 #include <cassert> 51 #include <cstdint> 52 #include <memory> 53 #include <utility> 54 #include <vector> 55 56 using namespace llvm; 57 58 //===----------------------------------------------------------------------===// 59 // Methods to implement the globals and functions lists. 60 // 61 62 // Explicit instantiations of SymbolTableListTraits since some of the methods 63 // are not in the public header file. 64 template class llvm::SymbolTableListTraits<Function>; 65 template class llvm::SymbolTableListTraits<GlobalVariable>; 66 template class llvm::SymbolTableListTraits<GlobalAlias>; 67 template class llvm::SymbolTableListTraits<GlobalIFunc>; 68 69 //===----------------------------------------------------------------------===// 70 // Primitive Module methods. 71 // 72 73 Module::Module(StringRef MID, LLVMContext &C) 74 : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>()), 75 Materializer(), ModuleID(MID), SourceFileName(MID), DL("") { 76 Context.addModule(this); 77 } 78 79 Module::~Module() { 80 Context.removeModule(this); 81 dropAllReferences(); 82 GlobalList.clear(); 83 FunctionList.clear(); 84 AliasList.clear(); 85 IFuncList.clear(); 86 } 87 88 std::unique_ptr<RandomNumberGenerator> Module::createRNG(const Pass* P) const { 89 SmallString<32> Salt(P->getPassName()); 90 91 // This RNG is guaranteed to produce the same random stream only 92 // when the Module ID and thus the input filename is the same. This 93 // might be problematic if the input filename extension changes 94 // (e.g. from .c to .bc or .ll). 95 // 96 // We could store this salt in NamedMetadata, but this would make 97 // the parameter non-const. This would unfortunately make this 98 // interface unusable by any Machine passes, since they only have a 99 // const reference to their IR Module. Alternatively we can always 100 // store salt metadata from the Module constructor. 101 Salt += sys::path::filename(getModuleIdentifier()); 102 103 return std::unique_ptr<RandomNumberGenerator>(new RandomNumberGenerator(Salt)); 104 } 105 106 /// getNamedValue - Return the first global value in the module with 107 /// the specified name, of arbitrary type. This method returns null 108 /// if a global with the specified name is not found. 109 GlobalValue *Module::getNamedValue(StringRef Name) const { 110 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); 111 } 112 113 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 114 /// This ID is uniqued across modules in the current LLVMContext. 115 unsigned Module::getMDKindID(StringRef Name) const { 116 return Context.getMDKindID(Name); 117 } 118 119 /// getMDKindNames - Populate client supplied SmallVector with the name for 120 /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, 121 /// so it is filled in as an empty string. 122 void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { 123 return Context.getMDKindNames(Result); 124 } 125 126 void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const { 127 return Context.getOperandBundleTags(Result); 128 } 129 130 //===----------------------------------------------------------------------===// 131 // Methods for easy access to the functions in the module. 132 // 133 134 // getOrInsertFunction - Look up the specified function in the module symbol 135 // table. If it does not exist, add a prototype for the function and return 136 // it. This is nice because it allows most passes to get away with not handling 137 // the symbol table directly for this common task. 138 // 139 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty, 140 AttributeList AttributeList) { 141 // See if we have a definition for the specified function already. 142 GlobalValue *F = getNamedValue(Name); 143 if (!F) { 144 // Nope, add it 145 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, 146 DL.getProgramAddressSpace(), Name); 147 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction 148 New->setAttributes(AttributeList); 149 FunctionList.push_back(New); 150 return {Ty, New}; // Return the new prototype. 151 } 152 153 // If the function exists but has the wrong type, return a bitcast to the 154 // right type. 155 auto *PTy = PointerType::get(Ty, F->getAddressSpace()); 156 if (F->getType() != PTy) 157 return {Ty, ConstantExpr::getBitCast(F, PTy)}; 158 159 // Otherwise, we just found the existing function or a prototype. 160 return {Ty, F}; 161 } 162 163 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) { 164 return getOrInsertFunction(Name, Ty, AttributeList()); 165 } 166 167 // getFunction - Look up the specified function in the module symbol table. 168 // If it does not exist, return null. 169 // 170 Function *Module::getFunction(StringRef Name) const { 171 return dyn_cast_or_null<Function>(getNamedValue(Name)); 172 } 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 bool AllowLocal) const { 187 if (GlobalVariable *Result = 188 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) 189 if (AllowLocal || !Result->hasLocalLinkage()) 190 return Result; 191 return nullptr; 192 } 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 function_ref<GlobalVariable *()> CreateGlobalCallback) { 203 // See if we have a definition for the specified global already. 204 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); 205 if (!GV) 206 GV = CreateGlobalCallback(); 207 assert(GV && "The CreateGlobalCallback is expected to create a global"); 208 209 // If the variable exists but has the wrong type, return a bitcast to the 210 // right type. 211 Type *GVTy = GV->getType(); 212 PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace()); 213 if (GVTy != PTy) 214 return ConstantExpr::getBitCast(GV, PTy); 215 216 // Otherwise, we just found the existing function or a prototype. 217 return GV; 218 } 219 220 // Overload to construct a global variable using its constructor's defaults. 221 Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { 222 return getOrInsertGlobal(Name, Ty, [&] { 223 return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, 224 nullptr, Name); 225 }); 226 } 227 228 //===----------------------------------------------------------------------===// 229 // Methods for easy access to the global variables in the module. 230 // 231 232 // getNamedAlias - Look up the specified global in the module symbol table. 233 // If it does not exist, return null. 234 // 235 GlobalAlias *Module::getNamedAlias(StringRef Name) const { 236 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); 237 } 238 239 GlobalIFunc *Module::getNamedIFunc(StringRef Name) const { 240 return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name)); 241 } 242 243 /// getNamedMetadata - Return the first NamedMDNode in the module with the 244 /// specified name. This method returns null if a NamedMDNode with the 245 /// specified name is not found. 246 NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { 247 SmallString<256> NameData; 248 StringRef NameRef = Name.toStringRef(NameData); 249 return NamedMDSymTab.lookup(NameRef); 250 } 251 252 /// getOrInsertNamedMetadata - Return the first named MDNode in the module 253 /// with the specified name. This method returns a new NamedMDNode if a 254 /// NamedMDNode with the specified name is not found. 255 NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { 256 NamedMDNode *&NMD = NamedMDSymTab[Name]; 257 if (!NMD) { 258 NMD = new NamedMDNode(Name); 259 NMD->setParent(this); 260 NamedMDList.push_back(NMD); 261 } 262 return NMD; 263 } 264 265 /// eraseNamedMetadata - Remove the given NamedMDNode from this module and 266 /// delete it. 267 void Module::eraseNamedMetadata(NamedMDNode *NMD) { 268 NamedMDSymTab.erase(NMD->getName()); 269 NamedMDList.erase(NMD->getIterator()); 270 } 271 272 bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { 273 if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) { 274 uint64_t Val = Behavior->getLimitedValue(); 275 if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { 276 MFB = static_cast<ModFlagBehavior>(Val); 277 return true; 278 } 279 } 280 return false; 281 } 282 283 /// getModuleFlagsMetadata - Returns the module flags in the provided vector. 284 void Module:: 285 getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { 286 const NamedMDNode *ModFlags = getModuleFlagsMetadata(); 287 if (!ModFlags) return; 288 289 for (const MDNode *Flag : ModFlags->operands()) { 290 ModFlagBehavior MFB; 291 if (Flag->getNumOperands() >= 3 && 292 isValidModFlagBehavior(Flag->getOperand(0), MFB) && 293 dyn_cast_or_null<MDString>(Flag->getOperand(1))) { 294 // Check the operands of the MDNode before accessing the operands. 295 // The verifier will actually catch these failures. 296 MDString *Key = cast<MDString>(Flag->getOperand(1)); 297 Metadata *Val = Flag->getOperand(2); 298 Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); 299 } 300 } 301 } 302 303 /// Return the corresponding value if Key appears in module flags, otherwise 304 /// return null. 305 Metadata *Module::getModuleFlag(StringRef Key) const { 306 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 307 getModuleFlagsMetadata(ModuleFlags); 308 for (const ModuleFlagEntry &MFE : ModuleFlags) { 309 if (Key == MFE.Key->getString()) 310 return MFE.Val; 311 } 312 return nullptr; 313 } 314 315 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that 316 /// represents module-level flags. This method returns null if there are no 317 /// module-level flags. 318 NamedMDNode *Module::getModuleFlagsMetadata() const { 319 return getNamedMetadata("llvm.module.flags"); 320 } 321 322 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that 323 /// represents module-level flags. If module-level flags aren't found, it 324 /// creates the named metadata that contains them. 325 NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { 326 return getOrInsertNamedMetadata("llvm.module.flags"); 327 } 328 329 /// addModuleFlag - Add a module-level flag to the module-level flags 330 /// metadata. It will create the module-level flags named metadata if it doesn't 331 /// already exist. 332 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 333 Metadata *Val) { 334 Type *Int32Ty = Type::getInt32Ty(Context); 335 Metadata *Ops[3] = { 336 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), 337 MDString::get(Context, Key), Val}; 338 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); 339 } 340 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 341 Constant *Val) { 342 addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); 343 } 344 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 345 uint32_t Val) { 346 Type *Int32Ty = Type::getInt32Ty(Context); 347 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); 348 } 349 void Module::addModuleFlag(MDNode *Node) { 350 assert(Node->getNumOperands() == 3 && 351 "Invalid number of operands for module flag!"); 352 assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) && 353 isa<MDString>(Node->getOperand(1)) && 354 "Invalid operand types for module flag!"); 355 getOrInsertModuleFlagsMetadata()->addOperand(Node); 356 } 357 358 void Module::setDataLayout(StringRef Desc) { 359 DL.reset(Desc); 360 } 361 362 void Module::setDataLayout(const DataLayout &Other) { DL = Other; } 363 364 const DataLayout &Module::getDataLayout() const { return DL; } 365 366 DICompileUnit *Module::debug_compile_units_iterator::operator*() const { 367 return cast<DICompileUnit>(CUs->getOperand(Idx)); 368 } 369 DICompileUnit *Module::debug_compile_units_iterator::operator->() const { 370 return cast<DICompileUnit>(CUs->getOperand(Idx)); 371 } 372 373 void Module::debug_compile_units_iterator::SkipNoDebugCUs() { 374 while (CUs && (Idx < CUs->getNumOperands()) && 375 ((*this)->getEmissionKind() == DICompileUnit::NoDebug)) 376 ++Idx; 377 } 378 379 iterator_range<Module::global_object_iterator> Module::global_objects() { 380 return concat<GlobalObject>(functions(), globals()); 381 } 382 iterator_range<Module::const_global_object_iterator> 383 Module::global_objects() const { 384 return concat<const GlobalObject>(functions(), globals()); 385 } 386 387 iterator_range<Module::global_value_iterator> Module::global_values() { 388 return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs()); 389 } 390 iterator_range<Module::const_global_value_iterator> 391 Module::global_values() const { 392 return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs()); 393 } 394 395 //===----------------------------------------------------------------------===// 396 // Methods to control the materialization of GlobalValues in the Module. 397 // 398 void Module::setMaterializer(GVMaterializer *GVM) { 399 assert(!Materializer && 400 "Module already has a GVMaterializer. Call materializeAll" 401 " to clear it out before setting another one."); 402 Materializer.reset(GVM); 403 } 404 405 Error Module::materialize(GlobalValue *GV) { 406 if (!Materializer) 407 return Error::success(); 408 409 return Materializer->materialize(GV); 410 } 411 412 Error Module::materializeAll() { 413 if (!Materializer) 414 return Error::success(); 415 std::unique_ptr<GVMaterializer> M = std::move(Materializer); 416 return M->materializeModule(); 417 } 418 419 Error Module::materializeMetadata() { 420 if (!Materializer) 421 return Error::success(); 422 return Materializer->materializeMetadata(); 423 } 424 425 //===----------------------------------------------------------------------===// 426 // Other module related stuff. 427 // 428 429 std::vector<StructType *> Module::getIdentifiedStructTypes() const { 430 // If we have a materializer, it is possible that some unread function 431 // uses a type that is currently not visible to a TypeFinder, so ask 432 // the materializer which types it created. 433 if (Materializer) 434 return Materializer->getIdentifiedStructTypes(); 435 436 std::vector<StructType *> Ret; 437 TypeFinder SrcStructTypes; 438 SrcStructTypes.run(*this, true); 439 Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); 440 return Ret; 441 } 442 443 // dropAllReferences() - This function causes all the subelements to "let go" 444 // of all references that they are maintaining. This allows one to 'delete' a 445 // whole module at a time, even though there may be circular references... first 446 // all references are dropped, and all use counts go to zero. Then everything 447 // is deleted for real. Note that no operations are valid on an object that 448 // has "dropped all references", except operator delete. 449 // 450 void Module::dropAllReferences() { 451 for (Function &F : *this) 452 F.dropAllReferences(); 453 454 for (GlobalVariable &GV : globals()) 455 GV.dropAllReferences(); 456 457 for (GlobalAlias &GA : aliases()) 458 GA.dropAllReferences(); 459 460 for (GlobalIFunc &GIF : ifuncs()) 461 GIF.dropAllReferences(); 462 } 463 464 unsigned Module::getNumberRegisterParameters() const { 465 auto *Val = 466 cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters")); 467 if (!Val) 468 return 0; 469 return cast<ConstantInt>(Val->getValue())->getZExtValue(); 470 } 471 472 unsigned Module::getDwarfVersion() const { 473 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version")); 474 if (!Val) 475 return 0; 476 return cast<ConstantInt>(Val->getValue())->getZExtValue(); 477 } 478 479 unsigned Module::getCodeViewFlag() const { 480 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView")); 481 if (!Val) 482 return 0; 483 return cast<ConstantInt>(Val->getValue())->getZExtValue(); 484 } 485 486 unsigned Module::getInstructionCount() { 487 unsigned NumInstrs = 0; 488 for (Function &F : FunctionList) 489 NumInstrs += F.getInstructionCount(); 490 return NumInstrs; 491 } 492 493 Comdat *Module::getOrInsertComdat(StringRef Name) { 494 auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first; 495 Entry.second.Name = &Entry; 496 return &Entry.second; 497 } 498 499 PICLevel::Level Module::getPICLevel() const { 500 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level")); 501 502 if (!Val) 503 return PICLevel::NotPIC; 504 505 return static_cast<PICLevel::Level>( 506 cast<ConstantInt>(Val->getValue())->getZExtValue()); 507 } 508 509 void Module::setPICLevel(PICLevel::Level PL) { 510 addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL); 511 } 512 513 PIELevel::Level Module::getPIELevel() const { 514 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level")); 515 516 if (!Val) 517 return PIELevel::Default; 518 519 return static_cast<PIELevel::Level>( 520 cast<ConstantInt>(Val->getValue())->getZExtValue()); 521 } 522 523 void Module::setPIELevel(PIELevel::Level PL) { 524 addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL); 525 } 526 527 Optional<CodeModel::Model> Module::getCodeModel() const { 528 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model")); 529 530 if (!Val) 531 return None; 532 533 return static_cast<CodeModel::Model>( 534 cast<ConstantInt>(Val->getValue())->getZExtValue()); 535 } 536 537 void Module::setCodeModel(CodeModel::Model CL) { 538 // Linking object files with different code models is undefined behavior 539 // because the compiler would have to generate additional code (to span 540 // longer jumps) if a larger code model is used with a smaller one. 541 // Therefore we will treat attempts to mix code models as an error. 542 addModuleFlag(ModFlagBehavior::Error, "Code Model", CL); 543 } 544 545 void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) { 546 if (Kind == ProfileSummary::PSK_CSInstr) 547 addModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M); 548 else 549 addModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M); 550 } 551 552 Metadata *Module::getProfileSummary(bool IsCS) { 553 return (IsCS ? getModuleFlag("CSProfileSummary") 554 : getModuleFlag("ProfileSummary")); 555 } 556 557 void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) { 558 OwnedMemoryBuffer = std::move(MB); 559 } 560 561 bool Module::getRtLibUseGOT() const { 562 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT")); 563 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0); 564 } 565 566 void Module::setRtLibUseGOT() { 567 addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1); 568 } 569 570 void Module::setSDKVersion(const VersionTuple &V) { 571 SmallVector<unsigned, 3> Entries; 572 Entries.push_back(V.getMajor()); 573 if (auto Minor = V.getMinor()) { 574 Entries.push_back(*Minor); 575 if (auto Subminor = V.getSubminor()) 576 Entries.push_back(*Subminor); 577 // Ignore the 'build' component as it can't be represented in the object 578 // file. 579 } 580 addModuleFlag(ModFlagBehavior::Warning, "SDK Version", 581 ConstantDataArray::get(Context, Entries)); 582 } 583 584 VersionTuple Module::getSDKVersion() const { 585 auto *CM = dyn_cast_or_null<ConstantAsMetadata>(getModuleFlag("SDK Version")); 586 if (!CM) 587 return {}; 588 auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue()); 589 if (!Arr) 590 return {}; 591 auto getVersionComponent = [&](unsigned Index) -> Optional<unsigned> { 592 if (Index >= Arr->getNumElements()) 593 return None; 594 return (unsigned)Arr->getElementAsInteger(Index); 595 }; 596 auto Major = getVersionComponent(0); 597 if (!Major) 598 return {}; 599 VersionTuple Result = VersionTuple(*Major); 600 if (auto Minor = getVersionComponent(1)) { 601 Result = VersionTuple(*Major, *Minor); 602 if (auto Subminor = getVersionComponent(2)) { 603 Result = VersionTuple(*Major, *Minor, *Subminor); 604 } 605 } 606 return Result; 607 } 608 609 GlobalVariable *llvm::collectUsedGlobalVariables( 610 const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) { 611 const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used"; 612 GlobalVariable *GV = M.getGlobalVariable(Name); 613 if (!GV || !GV->hasInitializer()) 614 return GV; 615 616 const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); 617 for (Value *Op : Init->operands()) { 618 GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts()); 619 Set.insert(G); 620 } 621 return GV; 622 } 623