1ef860a24SChandler Carruth //===-- Module.cpp - Implement the Module class ---------------------------===// 2ef860a24SChandler Carruth // 3ef860a24SChandler Carruth // The LLVM Compiler Infrastructure 4ef860a24SChandler Carruth // 5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source 6ef860a24SChandler Carruth // License. See LICENSE.TXT for details. 7ef860a24SChandler Carruth // 8ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 9ef860a24SChandler Carruth // 10ef860a24SChandler Carruth // This file implements the Module class for the IR library. 11ef860a24SChandler Carruth // 12ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 13ef860a24SChandler Carruth 149fb823bbSChandler Carruth #include "llvm/IR/Module.h" 15ef860a24SChandler Carruth #include "SymbolTableListTraitsImpl.h" 16ef860a24SChandler Carruth #include "llvm/ADT/DenseSet.h" 17ef860a24SChandler Carruth #include "llvm/ADT/STLExtras.h" 18ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h" 19ef860a24SChandler Carruth #include "llvm/ADT/StringExtras.h" 209fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 219fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h" 22d1163aa5SChandler Carruth #include "llvm/IR/GVMaterializer.h" 239fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h" 249fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 254b6845c7SChandler Carruth #include "llvm/IR/LeakDetector.h" 262fa1e43aSRafael Espindola #include "llvm/IR/TypeFinder.h" 270915c047SDiego Novillo #include "llvm/Support/Dwarf.h" 28144829d3SJF Bastien #include "llvm/Support/Path.h" 29144829d3SJF Bastien #include "llvm/Support/RandomNumberGenerator.h" 30ef860a24SChandler Carruth #include <algorithm> 31ef860a24SChandler Carruth #include <cstdarg> 32ef860a24SChandler Carruth #include <cstdlib> 33ef860a24SChandler Carruth using namespace llvm; 34ef860a24SChandler Carruth 35ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 36ef860a24SChandler Carruth // Methods to implement the globals and functions lists. 37ef860a24SChandler Carruth // 38ef860a24SChandler Carruth 39ef860a24SChandler Carruth // Explicit instantiations of SymbolTableListTraits since some of the methods 40ef860a24SChandler Carruth // are not in the public header file. 41ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<Function, Module>; 42ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalVariable, Module>; 43ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalAlias, Module>; 44ef860a24SChandler Carruth 45ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 46ef860a24SChandler Carruth // Primitive Module methods. 47ef860a24SChandler Carruth // 48ef860a24SChandler Carruth 49ef860a24SChandler Carruth Module::Module(StringRef MID, LLVMContext &C) 50144829d3SJF Bastien : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") { 51ef860a24SChandler Carruth ValSymTab = new ValueSymbolTable(); 52ef860a24SChandler Carruth NamedMDSymTab = new StringMap<NamedMDNode *>(); 53ef860a24SChandler Carruth Context.addModule(this); 54ef860a24SChandler Carruth } 55ef860a24SChandler Carruth 56ef860a24SChandler Carruth Module::~Module() { 57ef860a24SChandler Carruth Context.removeModule(this); 58ef860a24SChandler Carruth dropAllReferences(); 59ef860a24SChandler Carruth GlobalList.clear(); 60ef860a24SChandler Carruth FunctionList.clear(); 61ef860a24SChandler Carruth AliasList.clear(); 62ef860a24SChandler Carruth NamedMDList.clear(); 63ef860a24SChandler Carruth delete ValSymTab; 64ef860a24SChandler Carruth delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); 65144829d3SJF Bastien delete RNG; 66ef860a24SChandler Carruth } 67ef860a24SChandler Carruth 68ef860a24SChandler Carruth /// getNamedValue - Return the first global value in the module with 69ef860a24SChandler Carruth /// the specified name, of arbitrary type. This method returns null 70ef860a24SChandler Carruth /// if a global with the specified name is not found. 71ef860a24SChandler Carruth GlobalValue *Module::getNamedValue(StringRef Name) const { 72ef860a24SChandler Carruth return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); 73ef860a24SChandler Carruth } 74ef860a24SChandler Carruth 75ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 76ef860a24SChandler Carruth /// This ID is uniqued across modules in the current LLVMContext. 77ef860a24SChandler Carruth unsigned Module::getMDKindID(StringRef Name) const { 78ef860a24SChandler Carruth return Context.getMDKindID(Name); 79ef860a24SChandler Carruth } 80ef860a24SChandler Carruth 81ef860a24SChandler Carruth /// getMDKindNames - Populate client supplied SmallVector with the name for 82ef860a24SChandler Carruth /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, 83ef860a24SChandler Carruth /// so it is filled in as an empty string. 84ef860a24SChandler Carruth void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { 85ef860a24SChandler Carruth return Context.getMDKindNames(Result); 86ef860a24SChandler Carruth } 87ef860a24SChandler Carruth 88ef860a24SChandler Carruth 89ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 90ef860a24SChandler Carruth // Methods for easy access to the functions in the module. 91ef860a24SChandler Carruth // 92ef860a24SChandler Carruth 93ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol 94ef860a24SChandler Carruth // table. If it does not exist, add a prototype for the function and return 95ef860a24SChandler Carruth // it. This is nice because it allows most passes to get away with not handling 96ef860a24SChandler Carruth // the symbol table directly for this common task. 97ef860a24SChandler Carruth // 98ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name, 99ef860a24SChandler Carruth FunctionType *Ty, 100ef860a24SChandler Carruth AttributeSet AttributeList) { 101ef860a24SChandler Carruth // See if we have a definition for the specified function already. 102ef860a24SChandler Carruth GlobalValue *F = getNamedValue(Name); 103c620761cSCraig Topper if (!F) { 104ef860a24SChandler Carruth // Nope, add it 105ef860a24SChandler Carruth Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); 106ef860a24SChandler Carruth if (!New->isIntrinsic()) // Intrinsics get attrs set on construction 107ef860a24SChandler Carruth New->setAttributes(AttributeList); 108ef860a24SChandler Carruth FunctionList.push_back(New); 109ef860a24SChandler Carruth return New; // Return the new prototype. 110ef860a24SChandler Carruth } 111ef860a24SChandler Carruth 112ef860a24SChandler Carruth // If the function exists but has the wrong type, return a bitcast to the 113ef860a24SChandler Carruth // right type. 114ef860a24SChandler Carruth if (F->getType() != PointerType::getUnqual(Ty)) 115ef860a24SChandler Carruth return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); 116ef860a24SChandler Carruth 117ef860a24SChandler Carruth // Otherwise, we just found the existing function or a prototype. 118ef860a24SChandler Carruth return F; 119ef860a24SChandler Carruth } 120ef860a24SChandler Carruth 121ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name, 122ef860a24SChandler Carruth FunctionType *Ty) { 123ef860a24SChandler Carruth return getOrInsertFunction(Name, Ty, AttributeSet()); 124ef860a24SChandler Carruth } 125ef860a24SChandler Carruth 126ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol 127ef860a24SChandler Carruth // table. If it does not exist, add a prototype for the function and return it. 128ef860a24SChandler Carruth // This version of the method takes a null terminated list of function 129ef860a24SChandler Carruth // arguments, which makes it easier for clients to use. 130ef860a24SChandler Carruth // 131ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name, 132ef860a24SChandler Carruth AttributeSet AttributeList, 133ef860a24SChandler Carruth Type *RetTy, ...) { 134ef860a24SChandler Carruth va_list Args; 135ef860a24SChandler Carruth va_start(Args, RetTy); 136ef860a24SChandler Carruth 137ef860a24SChandler Carruth // Build the list of argument types... 138ef860a24SChandler Carruth std::vector<Type*> ArgTys; 139ef860a24SChandler Carruth while (Type *ArgTy = va_arg(Args, Type*)) 140ef860a24SChandler Carruth ArgTys.push_back(ArgTy); 141ef860a24SChandler Carruth 142ef860a24SChandler Carruth va_end(Args); 143ef860a24SChandler Carruth 144ef860a24SChandler Carruth // Build the function type and chain to the other getOrInsertFunction... 145ef860a24SChandler Carruth return getOrInsertFunction(Name, 146ef860a24SChandler Carruth FunctionType::get(RetTy, ArgTys, false), 147ef860a24SChandler Carruth AttributeList); 148ef860a24SChandler Carruth } 149ef860a24SChandler Carruth 150ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name, 151ef860a24SChandler Carruth Type *RetTy, ...) { 152ef860a24SChandler Carruth va_list Args; 153ef860a24SChandler Carruth va_start(Args, RetTy); 154ef860a24SChandler Carruth 155ef860a24SChandler Carruth // Build the list of argument types... 156ef860a24SChandler Carruth std::vector<Type*> ArgTys; 157ef860a24SChandler Carruth while (Type *ArgTy = va_arg(Args, Type*)) 158ef860a24SChandler Carruth ArgTys.push_back(ArgTy); 159ef860a24SChandler Carruth 160ef860a24SChandler Carruth va_end(Args); 161ef860a24SChandler Carruth 162ef860a24SChandler Carruth // Build the function type and chain to the other getOrInsertFunction... 163ef860a24SChandler Carruth return getOrInsertFunction(Name, 164ef860a24SChandler Carruth FunctionType::get(RetTy, ArgTys, false), 165ef860a24SChandler Carruth AttributeSet()); 166ef860a24SChandler Carruth } 167ef860a24SChandler Carruth 168ef860a24SChandler Carruth // getFunction - Look up the specified function in the module symbol table. 169ef860a24SChandler Carruth // If it does not exist, return null. 170ef860a24SChandler Carruth // 171ef860a24SChandler Carruth Function *Module::getFunction(StringRef Name) const { 172ef860a24SChandler Carruth return dyn_cast_or_null<Function>(getNamedValue(Name)); 173ef860a24SChandler Carruth } 174ef860a24SChandler Carruth 175ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 176ef860a24SChandler Carruth // Methods for easy access to the global variables in the module. 177ef860a24SChandler Carruth // 178ef860a24SChandler Carruth 179ef860a24SChandler Carruth /// getGlobalVariable - Look up the specified global variable in the module 180ef860a24SChandler Carruth /// symbol table. If it does not exist, return null. The type argument 181ef860a24SChandler Carruth /// should be the underlying type of the global, i.e., it should not have 182ef860a24SChandler Carruth /// the top-level PointerType, which represents the address of the global. 183ef860a24SChandler Carruth /// If AllowLocal is set to true, this function will return types that 184ef860a24SChandler Carruth /// have an local. By default, these types are not returned. 185ef860a24SChandler Carruth /// 186ec2375fbSRafael Espindola GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) { 187ef860a24SChandler Carruth if (GlobalVariable *Result = 188ef860a24SChandler Carruth dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) 189ef860a24SChandler Carruth if (AllowLocal || !Result->hasLocalLinkage()) 190ef860a24SChandler Carruth return Result; 191c620761cSCraig Topper return nullptr; 192ef860a24SChandler Carruth } 193ef860a24SChandler Carruth 194ef860a24SChandler Carruth /// getOrInsertGlobal - Look up the specified global in the module symbol table. 195ef860a24SChandler Carruth /// 1. If it does not exist, add a declaration of the global and return it. 196ef860a24SChandler Carruth /// 2. Else, the global exists but has the wrong type: return the function 197ef860a24SChandler Carruth /// with a constantexpr cast to the right type. 1985200fdf0SMatt Arsenault /// 3. Finally, if the existing global is the correct declaration, return the 199ef860a24SChandler Carruth /// existing global. 200ef860a24SChandler Carruth Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { 201ef860a24SChandler Carruth // See if we have a definition for the specified global already. 202ef860a24SChandler Carruth GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); 203c620761cSCraig Topper if (!GV) { 204ef860a24SChandler Carruth // Nope, add it 205ef860a24SChandler Carruth GlobalVariable *New = 206ef860a24SChandler Carruth new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, 207c620761cSCraig Topper nullptr, Name); 208ef860a24SChandler Carruth return New; // Return the new declaration. 209ef860a24SChandler Carruth } 210ef860a24SChandler Carruth 211ef860a24SChandler Carruth // If the variable exists but has the wrong type, return a bitcast to the 212ef860a24SChandler Carruth // right type. 21327e783e9SMatt Arsenault Type *GVTy = GV->getType(); 21427e783e9SMatt Arsenault PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace()); 215a90a340fSMatt Arsenault if (GVTy != PTy) 21627e783e9SMatt Arsenault return ConstantExpr::getBitCast(GV, PTy); 217ef860a24SChandler Carruth 218ef860a24SChandler Carruth // Otherwise, we just found the existing function or a prototype. 219ef860a24SChandler Carruth return GV; 220ef860a24SChandler Carruth } 221ef860a24SChandler Carruth 222ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 223ef860a24SChandler Carruth // Methods for easy access to the global variables in the module. 224ef860a24SChandler Carruth // 225ef860a24SChandler Carruth 226ef860a24SChandler Carruth // getNamedAlias - Look up the specified global in the module symbol table. 227ef860a24SChandler Carruth // If it does not exist, return null. 228ef860a24SChandler Carruth // 229ef860a24SChandler Carruth GlobalAlias *Module::getNamedAlias(StringRef Name) const { 230ef860a24SChandler Carruth return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); 231ef860a24SChandler Carruth } 232ef860a24SChandler Carruth 233ef860a24SChandler Carruth /// getNamedMetadata - Return the first NamedMDNode in the module with the 234ef860a24SChandler Carruth /// specified name. This method returns null if a NamedMDNode with the 235ef860a24SChandler Carruth /// specified name is not found. 236ef860a24SChandler Carruth NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { 237ef860a24SChandler Carruth SmallString<256> NameData; 238ef860a24SChandler Carruth StringRef NameRef = Name.toStringRef(NameData); 239ef860a24SChandler Carruth return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef); 240ef860a24SChandler Carruth } 241ef860a24SChandler Carruth 242ef860a24SChandler Carruth /// getOrInsertNamedMetadata - Return the first named MDNode in the module 243ef860a24SChandler Carruth /// with the specified name. This method returns a new NamedMDNode if a 244ef860a24SChandler Carruth /// NamedMDNode with the specified name is not found. 245ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { 246ef860a24SChandler Carruth NamedMDNode *&NMD = 247ef860a24SChandler Carruth (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name]; 248ef860a24SChandler Carruth if (!NMD) { 249ef860a24SChandler Carruth NMD = new NamedMDNode(Name); 250ef860a24SChandler Carruth NMD->setParent(this); 251ef860a24SChandler Carruth NamedMDList.push_back(NMD); 252ef860a24SChandler Carruth } 253ef860a24SChandler Carruth return NMD; 254ef860a24SChandler Carruth } 255ef860a24SChandler Carruth 256ef860a24SChandler Carruth /// eraseNamedMetadata - Remove the given NamedMDNode from this module and 257ef860a24SChandler Carruth /// delete it. 258ef860a24SChandler Carruth void Module::eraseNamedMetadata(NamedMDNode *NMD) { 259ef860a24SChandler Carruth static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); 260ef860a24SChandler Carruth NamedMDList.erase(NMD); 261ef860a24SChandler Carruth } 262ef860a24SChandler Carruth 263*5bf8fef5SDuncan P. N. Exon Smith bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { 264*5bf8fef5SDuncan P. N. Exon Smith if (ConstantInt *Behavior = mdconst::dyn_extract<ConstantInt>(MD)) { 265af023adbSAlexey Samsonov uint64_t Val = Behavior->getLimitedValue(); 266af023adbSAlexey Samsonov if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { 267af023adbSAlexey Samsonov MFB = static_cast<ModFlagBehavior>(Val); 268af023adbSAlexey Samsonov return true; 269af023adbSAlexey Samsonov } 270af023adbSAlexey Samsonov } 271af023adbSAlexey Samsonov return false; 272af023adbSAlexey Samsonov } 273af023adbSAlexey Samsonov 274ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the module flags in the provided vector. 275ef860a24SChandler Carruth void Module:: 276ef860a24SChandler Carruth getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { 277ef860a24SChandler Carruth const NamedMDNode *ModFlags = getModuleFlagsMetadata(); 278ef860a24SChandler Carruth if (!ModFlags) return; 279ef860a24SChandler Carruth 280de36e804SDuncan P. N. Exon Smith for (const MDNode *Flag : ModFlags->operands()) { 281af023adbSAlexey Samsonov ModFlagBehavior MFB; 282af023adbSAlexey Samsonov if (Flag->getNumOperands() >= 3 && 283af023adbSAlexey Samsonov isValidModFlagBehavior(Flag->getOperand(0), MFB) && 2848b4306ceSManman Ren isa<MDString>(Flag->getOperand(1))) { 2858b4306ceSManman Ren // Check the operands of the MDNode before accessing the operands. 2868b4306ceSManman Ren // The verifier will actually catch these failures. 287ef860a24SChandler Carruth MDString *Key = cast<MDString>(Flag->getOperand(1)); 288*5bf8fef5SDuncan P. N. Exon Smith Metadata *Val = Flag->getOperand(2); 289af023adbSAlexey Samsonov Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); 290ef860a24SChandler Carruth } 291ef860a24SChandler Carruth } 2928b4306ceSManman Ren } 293ef860a24SChandler Carruth 2948bfde891SManman Ren /// Return the corresponding value if Key appears in module flags, otherwise 2958bfde891SManman Ren /// return null. 296*5bf8fef5SDuncan P. N. Exon Smith Metadata *Module::getModuleFlag(StringRef Key) const { 2978bfde891SManman Ren SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; 2988bfde891SManman Ren getModuleFlagsMetadata(ModuleFlags); 2993ad5c962SBenjamin Kramer for (const ModuleFlagEntry &MFE : ModuleFlags) { 3008bfde891SManman Ren if (Key == MFE.Key->getString()) 3018bfde891SManman Ren return MFE.Val; 3028bfde891SManman Ren } 303c620761cSCraig Topper return nullptr; 3048bfde891SManman Ren } 3058bfde891SManman Ren 306ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that 307ef860a24SChandler Carruth /// represents module-level flags. This method returns null if there are no 308ef860a24SChandler Carruth /// module-level flags. 309ef860a24SChandler Carruth NamedMDNode *Module::getModuleFlagsMetadata() const { 310ef860a24SChandler Carruth return getNamedMetadata("llvm.module.flags"); 311ef860a24SChandler Carruth } 312ef860a24SChandler Carruth 313ef860a24SChandler Carruth /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that 314ef860a24SChandler Carruth /// represents module-level flags. If module-level flags aren't found, it 315ef860a24SChandler Carruth /// creates the named metadata that contains them. 316ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { 317ef860a24SChandler Carruth return getOrInsertNamedMetadata("llvm.module.flags"); 318ef860a24SChandler Carruth } 319ef860a24SChandler Carruth 320ef860a24SChandler Carruth /// addModuleFlag - Add a module-level flag to the module-level flags 321ef860a24SChandler Carruth /// metadata. It will create the module-level flags named metadata if it doesn't 322ef860a24SChandler Carruth /// already exist. 323ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 324*5bf8fef5SDuncan P. N. Exon Smith Metadata *Val) { 325ef860a24SChandler Carruth Type *Int32Ty = Type::getInt32Ty(Context); 326*5bf8fef5SDuncan P. N. Exon Smith Metadata *Ops[3] = { 327*5bf8fef5SDuncan P. N. Exon Smith ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), 328*5bf8fef5SDuncan P. N. Exon Smith MDString::get(Context, Key), Val}; 329ef860a24SChandler Carruth getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); 330ef860a24SChandler Carruth } 331ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 332*5bf8fef5SDuncan P. N. Exon Smith Constant *Val) { 333*5bf8fef5SDuncan P. N. Exon Smith addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); 334*5bf8fef5SDuncan P. N. Exon Smith } 335*5bf8fef5SDuncan P. N. Exon Smith void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, 336ef860a24SChandler Carruth uint32_t Val) { 337ef860a24SChandler Carruth Type *Int32Ty = Type::getInt32Ty(Context); 338ef860a24SChandler Carruth addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); 339ef860a24SChandler Carruth } 340ef860a24SChandler Carruth void Module::addModuleFlag(MDNode *Node) { 341ef860a24SChandler Carruth assert(Node->getNumOperands() == 3 && 342ef860a24SChandler Carruth "Invalid number of operands for module flag!"); 343*5bf8fef5SDuncan P. N. Exon Smith assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) && 344ef860a24SChandler Carruth isa<MDString>(Node->getOperand(1)) && 345ef860a24SChandler Carruth "Invalid operand types for module flag!"); 346ef860a24SChandler Carruth getOrInsertModuleFlagsMetadata()->addOperand(Node); 347ef860a24SChandler Carruth } 348ef860a24SChandler Carruth 349f863ee29SRafael Espindola void Module::setDataLayout(StringRef Desc) { 350248ac139SRafael Espindola DL.reset(Desc); 351248ac139SRafael Espindola 352f863ee29SRafael Espindola if (Desc.empty()) { 353f863ee29SRafael Espindola DataLayoutStr = ""; 354f863ee29SRafael Espindola } else { 355f863ee29SRafael Espindola DataLayoutStr = DL.getStringRepresentation(); 356248ac139SRafael Espindola // DataLayoutStr is now equivalent to Desc, but since the representation 357248ac139SRafael Espindola // is not unique, they may not be identical. 358f863ee29SRafael Espindola } 359f863ee29SRafael Espindola } 360f863ee29SRafael Espindola 361f863ee29SRafael Espindola void Module::setDataLayout(const DataLayout *Other) { 362f863ee29SRafael Espindola if (!Other) { 363f863ee29SRafael Espindola DataLayoutStr = ""; 364248ac139SRafael Espindola DL.reset(""); 365f863ee29SRafael Espindola } else { 366f863ee29SRafael Espindola DL = *Other; 367f863ee29SRafael Espindola DataLayoutStr = DL.getStringRepresentation(); 368f863ee29SRafael Espindola } 369f863ee29SRafael Espindola } 370f863ee29SRafael Espindola 371f863ee29SRafael Espindola const DataLayout *Module::getDataLayout() const { 372f863ee29SRafael Espindola if (DataLayoutStr.empty()) 373c620761cSCraig Topper return nullptr; 374f863ee29SRafael Espindola return &DL; 375f863ee29SRafael Espindola } 376f863ee29SRafael Espindola 377144829d3SJF Bastien // We want reproducible builds, but ModuleID may be a full path so we just use 378144829d3SJF Bastien // the filename to salt the RNG (although it is not guaranteed to be unique). 379144829d3SJF Bastien RandomNumberGenerator &Module::getRNG() const { 380144829d3SJF Bastien if (RNG == nullptr) { 381144829d3SJF Bastien StringRef Salt = sys::path::filename(ModuleID); 382144829d3SJF Bastien RNG = new RandomNumberGenerator(Salt); 383144829d3SJF Bastien } 384144829d3SJF Bastien return *RNG; 385144829d3SJF Bastien } 386144829d3SJF Bastien 387ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 388ef860a24SChandler Carruth // Methods to control the materialization of GlobalValues in the Module. 389ef860a24SChandler Carruth // 390ef860a24SChandler Carruth void Module::setMaterializer(GVMaterializer *GVM) { 391ef860a24SChandler Carruth assert(!Materializer && 392ef860a24SChandler Carruth "Module already has a GVMaterializer. Call MaterializeAllPermanently" 393ef860a24SChandler Carruth " to clear it out before setting another one."); 394ef860a24SChandler Carruth Materializer.reset(GVM); 395ef860a24SChandler Carruth } 396ef860a24SChandler Carruth 397ef860a24SChandler Carruth bool Module::isDematerializable(const GlobalValue *GV) const { 398ef860a24SChandler Carruth if (Materializer) 399ef860a24SChandler Carruth return Materializer->isDematerializable(GV); 400ef860a24SChandler Carruth return false; 401ef860a24SChandler Carruth } 402ef860a24SChandler Carruth 4035a52e6dcSRafael Espindola std::error_code Module::materialize(GlobalValue *GV) { 4042b11ad4fSRafael Espindola if (!Materializer) 4055a52e6dcSRafael Espindola return std::error_code(); 4062b11ad4fSRafael Espindola 4075a52e6dcSRafael Espindola return Materializer->materialize(GV); 408ef860a24SChandler Carruth } 409ef860a24SChandler Carruth 410ef860a24SChandler Carruth void Module::Dematerialize(GlobalValue *GV) { 411ef860a24SChandler Carruth if (Materializer) 412ef860a24SChandler Carruth return Materializer->Dematerialize(GV); 413ef860a24SChandler Carruth } 414ef860a24SChandler Carruth 415db4ed0bdSRafael Espindola std::error_code Module::materializeAll() { 416ef860a24SChandler Carruth if (!Materializer) 417db4ed0bdSRafael Espindola return std::error_code(); 4181d06f720SRafael Espindola return Materializer->MaterializeModule(this); 4191d06f720SRafael Espindola } 4201d06f720SRafael Espindola 421d96d553dSRafael Espindola std::error_code Module::materializeAllPermanently() { 422db4ed0bdSRafael Espindola if (std::error_code EC = materializeAll()) 423e9fab9b0SRafael Espindola return EC; 424e9fab9b0SRafael Espindola 425ef860a24SChandler Carruth Materializer.reset(); 426db4ed0bdSRafael Espindola return std::error_code(); 427ef860a24SChandler Carruth } 428ef860a24SChandler Carruth 429ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 430ef860a24SChandler Carruth // Other module related stuff. 431ef860a24SChandler Carruth // 432ef860a24SChandler Carruth 4332fa1e43aSRafael Espindola std::vector<StructType *> Module::getIdentifiedStructTypes() const { 4342fa1e43aSRafael Espindola // If we have a materializer, it is possible that some unread function 4352fa1e43aSRafael Espindola // uses a type that is currently not visible to a TypeFinder, so ask 4362fa1e43aSRafael Espindola // the materializer which types it created. 4372fa1e43aSRafael Espindola if (Materializer) 4382fa1e43aSRafael Espindola return Materializer->getIdentifiedStructTypes(); 4392fa1e43aSRafael Espindola 4402fa1e43aSRafael Espindola std::vector<StructType *> Ret; 4412fa1e43aSRafael Espindola TypeFinder SrcStructTypes; 4422fa1e43aSRafael Espindola SrcStructTypes.run(*this, true); 4432fa1e43aSRafael Espindola Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); 4442fa1e43aSRafael Espindola return Ret; 4452fa1e43aSRafael Espindola } 446ef860a24SChandler Carruth 447ef860a24SChandler Carruth // dropAllReferences() - This function causes all the subelements to "let go" 448ef860a24SChandler Carruth // of all references that they are maintaining. This allows one to 'delete' a 449ef860a24SChandler Carruth // whole module at a time, even though there may be circular references... first 450ef860a24SChandler Carruth // all references are dropped, and all use counts go to zero. Then everything 451ef860a24SChandler Carruth // is deleted for real. Note that no operations are valid on an object that 452ef860a24SChandler Carruth // has "dropped all references", except operator delete. 453ef860a24SChandler Carruth // 454ef860a24SChandler Carruth void Module::dropAllReferences() { 4553374910fSDavid Majnemer for (Function &F : *this) 4563374910fSDavid Majnemer F.dropAllReferences(); 457ef860a24SChandler Carruth 4583374910fSDavid Majnemer for (GlobalVariable &GV : globals()) 4593374910fSDavid Majnemer GV.dropAllReferences(); 460ef860a24SChandler Carruth 4613374910fSDavid Majnemer for (GlobalAlias &GA : aliases()) 4623374910fSDavid Majnemer GA.dropAllReferences(); 463ef860a24SChandler Carruth } 4640915c047SDiego Novillo 4650915c047SDiego Novillo unsigned Module::getDwarfVersion() const { 466*5bf8fef5SDuncan P. N. Exon Smith auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version")); 4670915c047SDiego Novillo if (!Val) 4680915c047SDiego Novillo return dwarf::DWARF_VERSION; 469*5bf8fef5SDuncan P. N. Exon Smith return cast<ConstantInt>(Val->getValue())->getZExtValue(); 4700915c047SDiego Novillo } 471dad0a645SDavid Majnemer 472dad0a645SDavid Majnemer Comdat *Module::getOrInsertComdat(StringRef Name) { 4735106ce78SDavid Blaikie auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first; 474dad0a645SDavid Majnemer Entry.second.Name = &Entry; 475dad0a645SDavid Majnemer return &Entry.second; 476dad0a645SDavid Majnemer } 477771c132eSJustin Hibbits 478771c132eSJustin Hibbits PICLevel::Level Module::getPICLevel() const { 479*5bf8fef5SDuncan P. N. Exon Smith auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level")); 480771c132eSJustin Hibbits 481771c132eSJustin Hibbits if (Val == NULL) 482771c132eSJustin Hibbits return PICLevel::Default; 483771c132eSJustin Hibbits 484*5bf8fef5SDuncan P. N. Exon Smith return static_cast<PICLevel::Level>( 485*5bf8fef5SDuncan P. N. Exon Smith cast<ConstantInt>(Val->getValue())->getZExtValue()); 486771c132eSJustin Hibbits } 487771c132eSJustin Hibbits 488771c132eSJustin Hibbits void Module::setPICLevel(PICLevel::Level PL) { 489771c132eSJustin Hibbits addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL); 490771c132eSJustin Hibbits } 491