LCOV - code coverage report
Current view: top level - lib/IR - Module.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 200 205 97.6 %
Date: 2018-09-23 13:06:45 Functions: 53 55 96.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Module.cpp - Implement the Module class ----------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the Module class for the IR library.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/Module.h"
      15             : #include "SymbolTableListTraitsImpl.h"
      16             : #include "llvm/ADT/Optional.h"
      17             : #include "llvm/ADT/SmallPtrSet.h"
      18             : #include "llvm/ADT/SmallString.h"
      19             : #include "llvm/ADT/SmallVector.h"
      20             : #include "llvm/ADT/StringMap.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/ADT/Twine.h"
      23             : #include "llvm/IR/Attributes.h"
      24             : #include "llvm/IR/Comdat.h"
      25             : #include "llvm/IR/Constants.h"
      26             : #include "llvm/IR/DataLayout.h"
      27             : #include "llvm/IR/DebugInfoMetadata.h"
      28             : #include "llvm/IR/DerivedTypes.h"
      29             : #include "llvm/IR/Function.h"
      30             : #include "llvm/IR/GVMaterializer.h"
      31             : #include "llvm/IR/GlobalAlias.h"
      32             : #include "llvm/IR/GlobalIFunc.h"
      33             : #include "llvm/IR/GlobalValue.h"
      34             : #include "llvm/IR/GlobalVariable.h"
      35             : #include "llvm/IR/LLVMContext.h"
      36             : #include "llvm/IR/Metadata.h"
      37             : #include "llvm/IR/SymbolTableListTraits.h"
      38             : #include "llvm/IR/Type.h"
      39             : #include "llvm/IR/TypeFinder.h"
      40             : #include "llvm/IR/Value.h"
      41             : #include "llvm/IR/ValueSymbolTable.h"
      42             : #include "llvm/Pass.h"
      43             : #include "llvm/Support/Casting.h"
      44             : #include "llvm/Support/CodeGen.h"
      45             : #include "llvm/Support/Error.h"
      46             : #include "llvm/Support/MemoryBuffer.h"
      47             : #include "llvm/Support/Path.h"
      48             : #include "llvm/Support/RandomNumberGenerator.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       55694 : Module::Module(StringRef MID, LLVMContext &C)
      74      167086 :     : Context(C), Materializer(), ModuleID(MID), SourceFileName(MID), DL("") {
      75       55695 :   ValSymTab = new ValueSymbolTable();
      76       55696 :   NamedMDSymTab = new StringMap<NamedMDNode *>();
      77       55696 :   Context.addModule(this);
      78       55696 : }
      79             : 
      80       95212 : Module::~Module() {
      81       47605 :   Context.removeModule(this);
      82       47606 :   dropAllReferences();
      83       47605 :   GlobalList.clear();
      84       47605 :   FunctionList.clear();
      85       47606 :   AliasList.clear();
      86       47606 :   IFuncList.clear();
      87             :   NamedMDList.clear();
      88       47606 :   delete ValSymTab;
      89       47606 :   delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
      90       47606 : }
      91             : 
      92           2 : std::unique_ptr<RandomNumberGenerator> Module::createRNG(const Pass* P) const {
      93           2 :   SmallString<32> Salt(P->getPassName());
      94             : 
      95             :   // This RNG is guaranteed to produce the same random stream only
      96             :   // when the Module ID and thus the input filename is the same. This
      97             :   // might be problematic if the input filename extension changes
      98             :   // (e.g. from .c to .bc or .ll).
      99             :   //
     100             :   // We could store this salt in NamedMetadata, but this would make
     101             :   // the parameter non-const. This would unfortunately make this
     102             :   // interface unusable by any Machine passes, since they only have a
     103             :   // const reference to their IR Module. Alternatively we can always
     104             :   // store salt metadata from the Module constructor.
     105           4 :   Salt += sys::path::filename(getModuleIdentifier());
     106             : 
     107           2 :   return std::unique_ptr<RandomNumberGenerator>(new RandomNumberGenerator(Salt));
     108             : }
     109             : 
     110             : /// getNamedValue - Return the first global value in the module with
     111             : /// the specified name, of arbitrary type.  This method returns null
     112             : /// if a global with the specified name is not found.
     113    28203625 : GlobalValue *Module::getNamedValue(StringRef Name) const {
     114    28203625 :   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
     115             : }
     116             : 
     117             : /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
     118             : /// This ID is uniqued across modules in the current LLVMContext.
     119      124459 : unsigned Module::getMDKindID(StringRef Name) const {
     120      124459 :   return Context.getMDKindID(Name);
     121             : }
     122             : 
     123             : /// getMDKindNames - Populate client supplied SmallVector with the name for
     124             : /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
     125             : /// so it is filled in as an empty string.
     126        4673 : void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
     127        4673 :   return Context.getMDKindNames(Result);
     128             : }
     129             : 
     130        4673 : void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
     131        4673 :   return Context.getOperandBundleTags(Result);
     132             : }
     133             : 
     134             : //===----------------------------------------------------------------------===//
     135             : // Methods for easy access to the functions in the module.
     136             : //
     137             : 
     138             : // getOrInsertFunction - Look up the specified function in the module symbol
     139             : // table.  If it does not exist, add a prototype for the function and return
     140             : // it.  This is nice because it allows most passes to get away with not handling
     141             : // the symbol table directly for this common task.
     142             : //
     143     3015028 : Constant *Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
     144             :                                       AttributeList AttributeList) {
     145             :   // See if we have a definition for the specified function already.
     146     3015028 :   GlobalValue *F = getNamedValue(Name);
     147     3015028 :   if (!F) {
     148             :     // Nope, add it
     149      180850 :     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
     150             :                                      DL.getProgramAddressSpace(), Name);
     151       90425 :     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
     152             :       New->setAttributes(AttributeList);
     153       90425 :     FunctionList.push_back(New);
     154       90425 :     return New;                    // Return the new prototype.
     155             :   }
     156             : 
     157             :   // If the function exists but has the wrong type, return a bitcast to the
     158             :   // right type.
     159     2924603 :   auto *PTy = PointerType::get(Ty, F->getAddressSpace());
     160     2924603 :   if (F->getType() != PTy)
     161          11 :     return ConstantExpr::getBitCast(F, PTy);
     162             : 
     163             :   // Otherwise, we just found the existing function or a prototype.
     164             :   return F;
     165             : }
     166             : 
     167     2967508 : Constant *Module::getOrInsertFunction(StringRef Name,
     168             :                                       FunctionType *Ty) {
     169     2967508 :   return getOrInsertFunction(Name, Ty, AttributeList());
     170             : }
     171             : 
     172             : // getFunction - Look up the specified function in the module symbol table.
     173             : // If it does not exist, return null.
     174             : //
     175     3905591 : Function *Module::getFunction(StringRef Name) const {
     176     3905591 :   return dyn_cast_or_null<Function>(getNamedValue(Name));
     177             : }
     178             : 
     179             : //===----------------------------------------------------------------------===//
     180             : // Methods for easy access to the global variables in the module.
     181             : //
     182             : 
     183             : /// getGlobalVariable - Look up the specified global variable in the module
     184             : /// symbol table.  If it does not exist, return null.  The type argument
     185             : /// should be the underlying type of the global, i.e., it should not have
     186             : /// the top-level PointerType, which represents the address of the global.
     187             : /// If AllowLocal is set to true, this function will return types that
     188             : /// have an local. By default, these types are not returned.
     189             : ///
     190      134462 : GlobalVariable *Module::getGlobalVariable(StringRef Name,
     191             :                                           bool AllowLocal) const {
     192             :   if (GlobalVariable *Result =
     193      134462 :       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
     194       45172 :     if (AllowLocal || !Result->hasLocalLinkage())
     195       45171 :       return Result;
     196             :   return nullptr;
     197             : }
     198             : 
     199             : /// getOrInsertGlobal - Look up the specified global in the module symbol table.
     200             : ///   1. If it does not exist, add a declaration of the global and return it.
     201             : ///   2. Else, the global exists but has the wrong type: return the function
     202             : ///      with a constantexpr cast to the right type.
     203             : ///   3. Finally, if the existing global is the correct declaration, return the
     204             : ///      existing global.
     205        8899 : Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
     206             :   // See if we have a definition for the specified global already.
     207        8899 :   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
     208             :   if (!GV) {
     209             :     // Nope, add it
     210             :     GlobalVariable *New =
     211             :       new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
     212        2860 :                          nullptr, Name);
     213        2860 :      return New;                    // Return the new declaration.
     214             :   }
     215             : 
     216             :   // If the variable exists but has the wrong type, return a bitcast to the
     217             :   // right type.
     218             :   Type *GVTy = GV->getType();
     219        6039 :   PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
     220        6039 :   if (GVTy != PTy)
     221         128 :     return ConstantExpr::getBitCast(GV, PTy);
     222             : 
     223             :   // Otherwise, we just found the existing function or a prototype.
     224             :   return GV;
     225             : }
     226             : 
     227             : //===----------------------------------------------------------------------===//
     228             : // Methods for easy access to the global variables in the module.
     229             : //
     230             : 
     231             : // getNamedAlias - Look up the specified global in the module symbol table.
     232             : // If it does not exist, return null.
     233             : //
     234          19 : GlobalAlias *Module::getNamedAlias(StringRef Name) const {
     235          19 :   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
     236             : }
     237             : 
     238           0 : GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
     239           0 :   return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
     240             : }
     241             : 
     242             : /// getNamedMetadata - Return the first NamedMDNode in the module with the
     243             : /// specified name. This method returns null if a NamedMDNode with the
     244             : /// specified name is not found.
     245     4183786 : NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
     246             :   SmallString<256> NameData;
     247     4183786 :   StringRef NameRef = Name.toStringRef(NameData);
     248     4183790 :   return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
     249             : }
     250             : 
     251             : /// getOrInsertNamedMetadata - Return the first named MDNode in the module
     252             : /// with the specified name. This method returns a new NamedMDNode if a
     253             : /// NamedMDNode with the specified name is not found.
     254       57903 : NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
     255             :   NamedMDNode *&NMD =
     256       57903 :     (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
     257       57903 :   if (!NMD) {
     258       45986 :     NMD = new NamedMDNode(Name);
     259             :     NMD->setParent(this);
     260             :     NamedMDList.push_back(NMD);
     261             :   }
     262       57903 :   return NMD;
     263             : }
     264             : 
     265             : /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
     266             : /// delete it.
     267        1388 : void Module::eraseNamedMetadata(NamedMDNode *NMD) {
     268        1388 :   static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
     269        1388 :   NamedMDList.erase(NMD->getIterator());
     270        1388 : }
     271             : 
     272     7826804 : 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     7826800 :     if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
     276     7826798 :       MFB = static_cast<ModFlagBehavior>(Val);
     277     7826798 :       return true;
     278             :     }
     279             :   }
     280             :   return false;
     281             : }
     282             : 
     283             : /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
     284     3723055 : void Module::
     285             : getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
     286     3723055 :   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
     287     3723056 :   if (!ModFlags) return;
     288             : 
     289    10552490 :   for (const MDNode *Flag : ModFlags->operands()) {
     290             :     ModFlagBehavior MFB;
     291     7796224 :     if (Flag->getNumOperands() >= 3 &&
     292     7796225 :         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    15592438 :       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     3671015 : Metadata *Module::getModuleFlag(StringRef Key) const {
     306             :   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
     307     3671015 :   getModuleFlagsMetadata(ModuleFlags);
     308    11425314 :   for (const ModuleFlagEntry &MFE : ModuleFlags) {
     309     7761829 :     if (Key == MFE.Key->getString())
     310        7532 :       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     3860217 : NamedMDNode *Module::getModuleFlagsMetadata() const {
     319     3860217 :   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       27267 : NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
     326       27267 :   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       27104 : void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
     333             :                            Metadata *Val) {
     334       27104 :   Type *Int32Ty = Type::getInt32Ty(Context);
     335             :   Metadata *Ops[3] = {
     336       27104 :       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
     337       27104 :       MDString::get(Context, Key), Val};
     338       27104 :   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
     339       27104 : }
     340       26215 : void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
     341             :                            Constant *Val) {
     342       26215 :   addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
     343       26215 : }
     344       26215 : void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
     345             :                            uint32_t Val) {
     346       26215 :   Type *Int32Ty = Type::getInt32Ty(Context);
     347       26215 :   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
     348       26215 : }
     349           0 : 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           0 :   getOrInsertModuleFlagsMetadata()->addOperand(Node);
     356           0 : }
     357             : 
     358        9826 : void Module::setDataLayout(StringRef Desc) {
     359        9826 :   DL.reset(Desc);
     360        9798 : }
     361             : 
     362       54421 : void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
     363             : 
     364   345583978 : const DataLayout &Module::getDataLayout() const { return DL; }
     365             : 
     366        5801 : DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
     367        5801 :   return cast<DICompileUnit>(CUs->getOperand(Idx));
     368             : }
     369        8777 : DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
     370        8777 :   return cast<DICompileUnit>(CUs->getOperand(Idx));
     371             : }
     372             : 
     373      125986 : void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
     374      126069 :   while (CUs && (Idx < CUs->getNumOperands()) &&
     375        8777 :          ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
     376          83 :     ++Idx;
     377      125987 : }
     378             : 
     379             : //===----------------------------------------------------------------------===//
     380             : // Methods to control the materialization of GlobalValues in the Module.
     381             : //
     382        3472 : void Module::setMaterializer(GVMaterializer *GVM) {
     383             :   assert(!Materializer &&
     384             :          "Module already has a GVMaterializer.  Call materializeAll"
     385             :          " to clear it out before setting another one.");
     386             :   Materializer.reset(GVM);
     387        3472 : }
     388             : 
     389      859761 : Error Module::materialize(GlobalValue *GV) {
     390      859761 :   if (!Materializer)
     391             :     return Error::success();
     392             : 
     393        1058 :   return Materializer->materialize(GV);
     394             : }
     395             : 
     396        2846 : Error Module::materializeAll() {
     397        2846 :   if (!Materializer)
     398             :     return Error::success();
     399             :   std::unique_ptr<GVMaterializer> M = std::move(Materializer);
     400        2660 :   return M->materializeModule();
     401             : }
     402             : 
     403        1149 : Error Module::materializeMetadata() {
     404        1149 :   if (!Materializer)
     405             :     return Error::success();
     406         691 :   return Materializer->materializeMetadata();
     407             : }
     408             : 
     409             : //===----------------------------------------------------------------------===//
     410             : // Other module related stuff.
     411             : //
     412             : 
     413        1098 : std::vector<StructType *> Module::getIdentifiedStructTypes() const {
     414             :   // If we have a materializer, it is possible that some unread function
     415             :   // uses a type that is currently not visible to a TypeFinder, so ask
     416             :   // the materializer which types it created.
     417        1098 :   if (Materializer)
     418         699 :     return Materializer->getIdentifiedStructTypes();
     419             : 
     420             :   std::vector<StructType *> Ret;
     421         798 :   TypeFinder SrcStructTypes;
     422         399 :   SrcStructTypes.run(*this, true);
     423             :   Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
     424             :   return Ret;
     425             : }
     426             : 
     427             : // dropAllReferences() - This function causes all the subelements to "let go"
     428             : // of all references that they are maintaining.  This allows one to 'delete' a
     429             : // whole module at a time, even though there may be circular references... first
     430             : // all references are dropped, and all use counts go to zero.  Then everything
     431             : // is deleted for real.  Note that no operations are valid on an object that
     432             : // has "dropped all references", except operator delete.
     433             : //
     434       47605 : void Module::dropAllReferences() {
     435      569235 :   for (Function &F : *this)
     436      521629 :     F.dropAllReferences();
     437             : 
     438      175433 :   for (GlobalVariable &GV : globals())
     439      127827 :     GV.dropAllReferences();
     440             : 
     441       50106 :   for (GlobalAlias &GA : aliases())
     442        2500 :     GA.dropAllReferences();
     443             : 
     444       47733 :   for (GlobalIFunc &GIF : ifuncs())
     445         128 :     GIF.dropAllReferences();
     446       47605 : }
     447             : 
     448         954 : unsigned Module::getNumberRegisterParameters() const {
     449             :   auto *Val =
     450        1339 :       cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
     451             :   if (!Val)
     452         569 :     return 0;
     453         385 :   return cast<ConstantInt>(Val->getValue())->getZExtValue();
     454             : }
     455             : 
     456       28086 : unsigned Module::getDwarfVersion() const {
     457       30396 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
     458             :   if (!Val)
     459       25776 :     return 0;
     460        2310 :   return cast<ConstantInt>(Val->getValue())->getZExtValue();
     461             : }
     462             : 
     463       30384 : unsigned Module::getCodeViewFlag() const {
     464       30734 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
     465             :   if (!Val)
     466       30035 :     return 0;
     467         350 :   return cast<ConstantInt>(Val->getValue())->getZExtValue();
     468             : }
     469             : 
     470          29 : unsigned Module::getInstructionCount() {
     471             :   unsigned NumInstrs = 0;
     472         124 :   for (Function &F : FunctionList)
     473          95 :     NumInstrs += F.getInstructionCount();
     474          29 :   return NumInstrs;
     475             : }
     476             : 
     477      740498 : Comdat *Module::getOrInsertComdat(StringRef Name) {
     478      740498 :   auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
     479      740498 :   Entry.second.Name = &Entry;
     480      740498 :   return &Entry.second;
     481             : }
     482             : 
     483       77295 : PICLevel::Level Module::getPICLevel() const {
     484       78188 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
     485             : 
     486             :   if (!Val)
     487       76402 :     return PICLevel::NotPIC;
     488             : 
     489             :   return static_cast<PICLevel::Level>(
     490         893 :       cast<ConstantInt>(Val->getValue())->getZExtValue());
     491             : }
     492             : 
     493         351 : void Module::setPICLevel(PICLevel::Level PL) {
     494         351 :   addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL);
     495         351 : }
     496             : 
     497       98660 : PIELevel::Level Module::getPIELevel() const {
     498       98951 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
     499             : 
     500             :   if (!Val)
     501       98369 :     return PIELevel::Default;
     502             : 
     503             :   return static_cast<PIELevel::Level>(
     504         291 :       cast<ConstantInt>(Val->getValue())->getZExtValue());
     505             : }
     506             : 
     507          17 : void Module::setPIELevel(PIELevel::Level PL) {
     508          17 :   addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
     509          17 : }
     510             : 
     511         696 : Optional<CodeModel::Model> Module::getCodeModel() const {
     512        1392 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
     513             : 
     514             :   if (!Val)
     515             :     return None;
     516             : 
     517           2 :   return static_cast<CodeModel::Model>(
     518             :       cast<ConstantInt>(Val->getValue())->getZExtValue());
     519             : }
     520             : 
     521           5 : void Module::setCodeModel(CodeModel::Model CL) {
     522             :   // Linking object files with different code models is undefined behavior
     523             :   // because the compiler would have to generate additional code (to span
     524             :   // longer jumps) if a larger code model is used with a smaller one.
     525             :   // Therefore we will treat attempts to mix code models as an error.
     526           5 :   addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
     527           5 : }
     528             : 
     529         153 : void Module::setProfileSummary(Metadata *M) {
     530         153 :   addModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
     531         153 : }
     532             : 
     533     1723208 : Metadata *Module::getProfileSummary() {
     534     1723208 :   return getModuleFlag("ProfileSummary");
     535             : }
     536             : 
     537         196 : void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
     538             :   OwnedMemoryBuffer = std::move(MB);
     539         196 : }
     540             : 
     541      245745 : bool Module::getRtLibUseGOT() const {
     542      491490 :   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
     543          17 :   return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
     544             : }
     545             : 
     546           5 : void Module::setRtLibUseGOT() {
     547           5 :   addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
     548           5 : }
     549             : 
     550       15796 : GlobalVariable *llvm::collectUsedGlobalVariables(
     551             :     const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
     552       15796 :   const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
     553       15797 :   GlobalVariable *GV = M.getGlobalVariable(Name);
     554       15970 :   if (!GV || !GV->hasInitializer())
     555       15624 :     return GV;
     556             : 
     557             :   const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
     558         714 :   for (Value *Op : Init->operands()) {
     559             :     GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
     560         368 :     Set.insert(G);
     561             :   }
     562             :   return GV;
     563             : }

Generated by: LCOV version 1.13