xref: /llvm-project-15.0.7/llvm/lib/IR/Module.cpp (revision ec2375fb)
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"
20ef860a24SChandler Carruth #include "llvm/GVMaterializer.h"
219fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
229fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h"
239fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h"
249fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
25ef860a24SChandler Carruth #include "llvm/Support/LeakDetector.h"
26ef860a24SChandler Carruth #include <algorithm>
27ef860a24SChandler Carruth #include <cstdarg>
28ef860a24SChandler Carruth #include <cstdlib>
29ef860a24SChandler Carruth using namespace llvm;
30ef860a24SChandler Carruth 
31ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
32ef860a24SChandler Carruth // Methods to implement the globals and functions lists.
33ef860a24SChandler Carruth //
34ef860a24SChandler Carruth 
35ef860a24SChandler Carruth // Explicit instantiations of SymbolTableListTraits since some of the methods
36ef860a24SChandler Carruth // are not in the public header file.
37ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<Function, Module>;
38ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
39ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
40ef860a24SChandler Carruth 
41ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
42ef860a24SChandler Carruth // Primitive Module methods.
43ef860a24SChandler Carruth //
44ef860a24SChandler Carruth 
45ef860a24SChandler Carruth Module::Module(StringRef MID, LLVMContext& C)
46ef860a24SChandler Carruth   : Context(C), Materializer(NULL), ModuleID(MID) {
47ef860a24SChandler Carruth   ValSymTab = new ValueSymbolTable();
48ef860a24SChandler Carruth   NamedMDSymTab = new StringMap<NamedMDNode *>();
49ef860a24SChandler Carruth   Context.addModule(this);
50ef860a24SChandler Carruth }
51ef860a24SChandler Carruth 
52ef860a24SChandler Carruth Module::~Module() {
53ef860a24SChandler Carruth   Context.removeModule(this);
54ef860a24SChandler Carruth   dropAllReferences();
55ef860a24SChandler Carruth   GlobalList.clear();
56ef860a24SChandler Carruth   FunctionList.clear();
57ef860a24SChandler Carruth   AliasList.clear();
58ef860a24SChandler Carruth   NamedMDList.clear();
59ef860a24SChandler Carruth   delete ValSymTab;
60ef860a24SChandler Carruth   delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
61ef860a24SChandler Carruth }
62ef860a24SChandler Carruth 
63ef860a24SChandler Carruth /// Target endian information.
64ef860a24SChandler Carruth Module::Endianness Module::getEndianness() const {
65ef860a24SChandler Carruth   StringRef temp = DataLayout;
66ef860a24SChandler Carruth   Module::Endianness ret = AnyEndianness;
67ef860a24SChandler Carruth 
68ef860a24SChandler Carruth   while (!temp.empty()) {
69ef860a24SChandler Carruth     std::pair<StringRef, StringRef> P = getToken(temp, "-");
70ef860a24SChandler Carruth 
71ef860a24SChandler Carruth     StringRef token = P.first;
72ef860a24SChandler Carruth     temp = P.second;
73ef860a24SChandler Carruth 
74ef860a24SChandler Carruth     if (token[0] == 'e') {
75ef860a24SChandler Carruth       ret = LittleEndian;
76ef860a24SChandler Carruth     } else if (token[0] == 'E') {
77ef860a24SChandler Carruth       ret = BigEndian;
78ef860a24SChandler Carruth     }
79ef860a24SChandler Carruth   }
80ef860a24SChandler Carruth 
81ef860a24SChandler Carruth   return ret;
82ef860a24SChandler Carruth }
83ef860a24SChandler Carruth 
84ef860a24SChandler Carruth /// Target Pointer Size information.
85ef860a24SChandler Carruth Module::PointerSize Module::getPointerSize() const {
86ef860a24SChandler Carruth   StringRef temp = DataLayout;
87ef860a24SChandler Carruth   Module::PointerSize ret = AnyPointerSize;
88ef860a24SChandler Carruth 
89ef860a24SChandler Carruth   while (!temp.empty()) {
90ef860a24SChandler Carruth     std::pair<StringRef, StringRef> TmpP = getToken(temp, "-");
91ef860a24SChandler Carruth     temp = TmpP.second;
92ef860a24SChandler Carruth     TmpP = getToken(TmpP.first, ":");
93ef860a24SChandler Carruth     StringRef token = TmpP.second, signalToken = TmpP.first;
94ef860a24SChandler Carruth 
95ef860a24SChandler Carruth     if (signalToken[0] == 'p') {
96ef860a24SChandler Carruth       int size = 0;
97ef860a24SChandler Carruth       getToken(token, ":").first.getAsInteger(10, size);
98ef860a24SChandler Carruth       if (size == 32)
99ef860a24SChandler Carruth         ret = Pointer32;
100ef860a24SChandler Carruth       else if (size == 64)
101ef860a24SChandler Carruth         ret = Pointer64;
102ef860a24SChandler Carruth     }
103ef860a24SChandler Carruth   }
104ef860a24SChandler Carruth 
105ef860a24SChandler Carruth   return ret;
106ef860a24SChandler Carruth }
107ef860a24SChandler Carruth 
108ef860a24SChandler Carruth /// getNamedValue - Return the first global value in the module with
109ef860a24SChandler Carruth /// the specified name, of arbitrary type.  This method returns null
110ef860a24SChandler Carruth /// if a global with the specified name is not found.
111ef860a24SChandler Carruth GlobalValue *Module::getNamedValue(StringRef Name) const {
112ef860a24SChandler Carruth   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
113ef860a24SChandler Carruth }
114ef860a24SChandler Carruth 
115ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
116ef860a24SChandler Carruth /// This ID is uniqued across modules in the current LLVMContext.
117ef860a24SChandler Carruth unsigned Module::getMDKindID(StringRef Name) const {
118ef860a24SChandler Carruth   return Context.getMDKindID(Name);
119ef860a24SChandler Carruth }
120ef860a24SChandler Carruth 
121ef860a24SChandler Carruth /// getMDKindNames - Populate client supplied SmallVector with the name for
122ef860a24SChandler Carruth /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
123ef860a24SChandler Carruth /// so it is filled in as an empty string.
124ef860a24SChandler Carruth void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
125ef860a24SChandler Carruth   return Context.getMDKindNames(Result);
126ef860a24SChandler Carruth }
127ef860a24SChandler Carruth 
128ef860a24SChandler Carruth 
129ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
130ef860a24SChandler Carruth // Methods for easy access to the functions in the module.
131ef860a24SChandler Carruth //
132ef860a24SChandler Carruth 
133ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol
134ef860a24SChandler Carruth // table.  If it does not exist, add a prototype for the function and return
135ef860a24SChandler Carruth // it.  This is nice because it allows most passes to get away with not handling
136ef860a24SChandler Carruth // the symbol table directly for this common task.
137ef860a24SChandler Carruth //
138ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
139ef860a24SChandler Carruth                                       FunctionType *Ty,
140ef860a24SChandler Carruth                                       AttributeSet AttributeList) {
141ef860a24SChandler Carruth   // See if we have a definition for the specified function already.
142ef860a24SChandler Carruth   GlobalValue *F = getNamedValue(Name);
143ef860a24SChandler Carruth   if (F == 0) {
144ef860a24SChandler Carruth     // Nope, add it
145ef860a24SChandler Carruth     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
146ef860a24SChandler Carruth     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
147ef860a24SChandler Carruth       New->setAttributes(AttributeList);
148ef860a24SChandler Carruth     FunctionList.push_back(New);
149ef860a24SChandler Carruth     return New;                    // Return the new prototype.
150ef860a24SChandler Carruth   }
151ef860a24SChandler Carruth 
152ef860a24SChandler Carruth   // Okay, the function exists.  Does it have externally visible linkage?
153ef860a24SChandler Carruth   if (F->hasLocalLinkage()) {
154ef860a24SChandler Carruth     // Clear the function's name.
155ef860a24SChandler Carruth     F->setName("");
156ef860a24SChandler Carruth     // Retry, now there won't be a conflict.
157ef860a24SChandler Carruth     Constant *NewF = getOrInsertFunction(Name, Ty);
158ef860a24SChandler Carruth     F->setName(Name);
159ef860a24SChandler Carruth     return NewF;
160ef860a24SChandler Carruth   }
161ef860a24SChandler Carruth 
162ef860a24SChandler Carruth   // If the function exists but has the wrong type, return a bitcast to the
163ef860a24SChandler Carruth   // right type.
164ef860a24SChandler Carruth   if (F->getType() != PointerType::getUnqual(Ty))
165ef860a24SChandler Carruth     return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
166ef860a24SChandler Carruth 
167ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
168ef860a24SChandler Carruth   return F;
169ef860a24SChandler Carruth }
170ef860a24SChandler Carruth 
171ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
172ef860a24SChandler Carruth                                       FunctionType *Ty) {
173ef860a24SChandler Carruth   return getOrInsertFunction(Name, Ty, AttributeSet());
174ef860a24SChandler Carruth }
175ef860a24SChandler Carruth 
176ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol
177ef860a24SChandler Carruth // table.  If it does not exist, add a prototype for the function and return it.
178ef860a24SChandler Carruth // This version of the method takes a null terminated list of function
179ef860a24SChandler Carruth // arguments, which makes it easier for clients to use.
180ef860a24SChandler Carruth //
181ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
182ef860a24SChandler Carruth                                       AttributeSet AttributeList,
183ef860a24SChandler Carruth                                       Type *RetTy, ...) {
184ef860a24SChandler Carruth   va_list Args;
185ef860a24SChandler Carruth   va_start(Args, RetTy);
186ef860a24SChandler Carruth 
187ef860a24SChandler Carruth   // Build the list of argument types...
188ef860a24SChandler Carruth   std::vector<Type*> ArgTys;
189ef860a24SChandler Carruth   while (Type *ArgTy = va_arg(Args, Type*))
190ef860a24SChandler Carruth     ArgTys.push_back(ArgTy);
191ef860a24SChandler Carruth 
192ef860a24SChandler Carruth   va_end(Args);
193ef860a24SChandler Carruth 
194ef860a24SChandler Carruth   // Build the function type and chain to the other getOrInsertFunction...
195ef860a24SChandler Carruth   return getOrInsertFunction(Name,
196ef860a24SChandler Carruth                              FunctionType::get(RetTy, ArgTys, false),
197ef860a24SChandler Carruth                              AttributeList);
198ef860a24SChandler Carruth }
199ef860a24SChandler Carruth 
200ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
201ef860a24SChandler Carruth                                       Type *RetTy, ...) {
202ef860a24SChandler Carruth   va_list Args;
203ef860a24SChandler Carruth   va_start(Args, RetTy);
204ef860a24SChandler Carruth 
205ef860a24SChandler Carruth   // Build the list of argument types...
206ef860a24SChandler Carruth   std::vector<Type*> ArgTys;
207ef860a24SChandler Carruth   while (Type *ArgTy = va_arg(Args, Type*))
208ef860a24SChandler Carruth     ArgTys.push_back(ArgTy);
209ef860a24SChandler Carruth 
210ef860a24SChandler Carruth   va_end(Args);
211ef860a24SChandler Carruth 
212ef860a24SChandler Carruth   // Build the function type and chain to the other getOrInsertFunction...
213ef860a24SChandler Carruth   return getOrInsertFunction(Name,
214ef860a24SChandler Carruth                              FunctionType::get(RetTy, ArgTys, false),
215ef860a24SChandler Carruth                              AttributeSet());
216ef860a24SChandler Carruth }
217ef860a24SChandler Carruth 
218ef860a24SChandler Carruth // getFunction - Look up the specified function in the module symbol table.
219ef860a24SChandler Carruth // If it does not exist, return null.
220ef860a24SChandler Carruth //
221ef860a24SChandler Carruth Function *Module::getFunction(StringRef Name) const {
222ef860a24SChandler Carruth   return dyn_cast_or_null<Function>(getNamedValue(Name));
223ef860a24SChandler Carruth }
224ef860a24SChandler Carruth 
225ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
226ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
227ef860a24SChandler Carruth //
228ef860a24SChandler Carruth 
229ef860a24SChandler Carruth /// getGlobalVariable - Look up the specified global variable in the module
230ef860a24SChandler Carruth /// symbol table.  If it does not exist, return null.  The type argument
231ef860a24SChandler Carruth /// should be the underlying type of the global, i.e., it should not have
232ef860a24SChandler Carruth /// the top-level PointerType, which represents the address of the global.
233ef860a24SChandler Carruth /// If AllowLocal is set to true, this function will return types that
234ef860a24SChandler Carruth /// have an local. By default, these types are not returned.
235ef860a24SChandler Carruth ///
236*ec2375fbSRafael Espindola GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
237ef860a24SChandler Carruth   if (GlobalVariable *Result =
238ef860a24SChandler Carruth       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
239ef860a24SChandler Carruth     if (AllowLocal || !Result->hasLocalLinkage())
240ef860a24SChandler Carruth       return Result;
241ef860a24SChandler Carruth   return 0;
242ef860a24SChandler Carruth }
243ef860a24SChandler Carruth 
244ef860a24SChandler Carruth /// getOrInsertGlobal - Look up the specified global in the module symbol table.
245ef860a24SChandler Carruth ///   1. If it does not exist, add a declaration of the global and return it.
246ef860a24SChandler Carruth ///   2. Else, the global exists but has the wrong type: return the function
247ef860a24SChandler Carruth ///      with a constantexpr cast to the right type.
248ef860a24SChandler Carruth ///   3. Finally, if the existing global is the correct delclaration, return the
249ef860a24SChandler Carruth ///      existing global.
250ef860a24SChandler Carruth Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
251ef860a24SChandler Carruth   // See if we have a definition for the specified global already.
252ef860a24SChandler Carruth   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
253ef860a24SChandler Carruth   if (GV == 0) {
254ef860a24SChandler Carruth     // Nope, add it
255ef860a24SChandler Carruth     GlobalVariable *New =
256ef860a24SChandler Carruth       new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
257ef860a24SChandler Carruth                          0, Name);
258ef860a24SChandler Carruth      return New;                    // Return the new declaration.
259ef860a24SChandler Carruth   }
260ef860a24SChandler Carruth 
261ef860a24SChandler Carruth   // If the variable exists but has the wrong type, return a bitcast to the
262ef860a24SChandler Carruth   // right type.
263ef860a24SChandler Carruth   if (GV->getType() != PointerType::getUnqual(Ty))
264ef860a24SChandler Carruth     return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
265ef860a24SChandler Carruth 
266ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
267ef860a24SChandler Carruth   return GV;
268ef860a24SChandler Carruth }
269ef860a24SChandler Carruth 
270ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
271ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
272ef860a24SChandler Carruth //
273ef860a24SChandler Carruth 
274ef860a24SChandler Carruth // getNamedAlias - Look up the specified global in the module symbol table.
275ef860a24SChandler Carruth // If it does not exist, return null.
276ef860a24SChandler Carruth //
277ef860a24SChandler Carruth GlobalAlias *Module::getNamedAlias(StringRef Name) const {
278ef860a24SChandler Carruth   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
279ef860a24SChandler Carruth }
280ef860a24SChandler Carruth 
281ef860a24SChandler Carruth /// getNamedMetadata - Return the first NamedMDNode in the module with the
282ef860a24SChandler Carruth /// specified name. This method returns null if a NamedMDNode with the
283ef860a24SChandler Carruth /// specified name is not found.
284ef860a24SChandler Carruth NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
285ef860a24SChandler Carruth   SmallString<256> NameData;
286ef860a24SChandler Carruth   StringRef NameRef = Name.toStringRef(NameData);
287ef860a24SChandler Carruth   return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
288ef860a24SChandler Carruth }
289ef860a24SChandler Carruth 
290ef860a24SChandler Carruth /// getOrInsertNamedMetadata - Return the first named MDNode in the module
291ef860a24SChandler Carruth /// with the specified name. This method returns a new NamedMDNode if a
292ef860a24SChandler Carruth /// NamedMDNode with the specified name is not found.
293ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
294ef860a24SChandler Carruth   NamedMDNode *&NMD =
295ef860a24SChandler Carruth     (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
296ef860a24SChandler Carruth   if (!NMD) {
297ef860a24SChandler Carruth     NMD = new NamedMDNode(Name);
298ef860a24SChandler Carruth     NMD->setParent(this);
299ef860a24SChandler Carruth     NamedMDList.push_back(NMD);
300ef860a24SChandler Carruth   }
301ef860a24SChandler Carruth   return NMD;
302ef860a24SChandler Carruth }
303ef860a24SChandler Carruth 
304ef860a24SChandler Carruth /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
305ef860a24SChandler Carruth /// delete it.
306ef860a24SChandler Carruth void Module::eraseNamedMetadata(NamedMDNode *NMD) {
307ef860a24SChandler Carruth   static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
308ef860a24SChandler Carruth   NamedMDList.erase(NMD);
309ef860a24SChandler Carruth }
310ef860a24SChandler Carruth 
311ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
312ef860a24SChandler Carruth void Module::
313ef860a24SChandler Carruth getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
314ef860a24SChandler Carruth   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
315ef860a24SChandler Carruth   if (!ModFlags) return;
316ef860a24SChandler Carruth 
317ef860a24SChandler Carruth   for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
318ef860a24SChandler Carruth     MDNode *Flag = ModFlags->getOperand(i);
319ef860a24SChandler Carruth     ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
320ef860a24SChandler Carruth     MDString *Key = cast<MDString>(Flag->getOperand(1));
321ef860a24SChandler Carruth     Value *Val = Flag->getOperand(2);
322ef860a24SChandler Carruth     Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
323ef860a24SChandler Carruth                                     Key, Val));
324ef860a24SChandler Carruth   }
325ef860a24SChandler Carruth }
326ef860a24SChandler Carruth 
3278bfde891SManman Ren /// Return the corresponding value if Key appears in module flags, otherwise
3288bfde891SManman Ren /// return null.
3298bfde891SManman Ren Value *Module::getModuleFlag(StringRef Key) const {
3308bfde891SManman Ren   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
3318bfde891SManman Ren   getModuleFlagsMetadata(ModuleFlags);
3328bfde891SManman Ren   for (unsigned I = 0, E = ModuleFlags.size(); I < E; ++I) {
3338bfde891SManman Ren     const ModuleFlagEntry &MFE = ModuleFlags[I];
3348bfde891SManman Ren     if (Key == MFE.Key->getString())
3358bfde891SManman Ren       return MFE.Val;
3368bfde891SManman Ren   }
3378bfde891SManman Ren   return 0;
3388bfde891SManman Ren }
3398bfde891SManman Ren 
340ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
341ef860a24SChandler Carruth /// represents module-level flags. This method returns null if there are no
342ef860a24SChandler Carruth /// module-level flags.
343ef860a24SChandler Carruth NamedMDNode *Module::getModuleFlagsMetadata() const {
344ef860a24SChandler Carruth   return getNamedMetadata("llvm.module.flags");
345ef860a24SChandler Carruth }
346ef860a24SChandler Carruth 
347ef860a24SChandler Carruth /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
348ef860a24SChandler Carruth /// represents module-level flags. If module-level flags aren't found, it
349ef860a24SChandler Carruth /// creates the named metadata that contains them.
350ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
351ef860a24SChandler Carruth   return getOrInsertNamedMetadata("llvm.module.flags");
352ef860a24SChandler Carruth }
353ef860a24SChandler Carruth 
354ef860a24SChandler Carruth /// addModuleFlag - Add a module-level flag to the module-level flags
355ef860a24SChandler Carruth /// metadata. It will create the module-level flags named metadata if it doesn't
356ef860a24SChandler Carruth /// already exist.
357ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
358ef860a24SChandler Carruth                            Value *Val) {
359ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
360ef860a24SChandler Carruth   Value *Ops[3] = {
361ef860a24SChandler Carruth     ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
362ef860a24SChandler Carruth   };
363ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
364ef860a24SChandler Carruth }
365ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
366ef860a24SChandler Carruth                            uint32_t Val) {
367ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
368ef860a24SChandler Carruth   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
369ef860a24SChandler Carruth }
370ef860a24SChandler Carruth void Module::addModuleFlag(MDNode *Node) {
371ef860a24SChandler Carruth   assert(Node->getNumOperands() == 3 &&
372ef860a24SChandler Carruth          "Invalid number of operands for module flag!");
373ef860a24SChandler Carruth   assert(isa<ConstantInt>(Node->getOperand(0)) &&
374ef860a24SChandler Carruth          isa<MDString>(Node->getOperand(1)) &&
375ef860a24SChandler Carruth          "Invalid operand types for module flag!");
376ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(Node);
377ef860a24SChandler Carruth }
378ef860a24SChandler Carruth 
379ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
380ef860a24SChandler Carruth // Methods to control the materialization of GlobalValues in the Module.
381ef860a24SChandler Carruth //
382ef860a24SChandler Carruth void Module::setMaterializer(GVMaterializer *GVM) {
383ef860a24SChandler Carruth   assert(!Materializer &&
384ef860a24SChandler Carruth          "Module already has a GVMaterializer.  Call MaterializeAllPermanently"
385ef860a24SChandler Carruth          " to clear it out before setting another one.");
386ef860a24SChandler Carruth   Materializer.reset(GVM);
387ef860a24SChandler Carruth }
388ef860a24SChandler Carruth 
389ef860a24SChandler Carruth bool Module::isMaterializable(const GlobalValue *GV) const {
390ef860a24SChandler Carruth   if (Materializer)
391ef860a24SChandler Carruth     return Materializer->isMaterializable(GV);
392ef860a24SChandler Carruth   return false;
393ef860a24SChandler Carruth }
394ef860a24SChandler Carruth 
395ef860a24SChandler Carruth bool Module::isDematerializable(const GlobalValue *GV) const {
396ef860a24SChandler Carruth   if (Materializer)
397ef860a24SChandler Carruth     return Materializer->isDematerializable(GV);
398ef860a24SChandler Carruth   return false;
399ef860a24SChandler Carruth }
400ef860a24SChandler Carruth 
401ef860a24SChandler Carruth bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
402ef860a24SChandler Carruth   if (Materializer)
403ef860a24SChandler Carruth     return Materializer->Materialize(GV, ErrInfo);
404ef860a24SChandler Carruth   return false;
405ef860a24SChandler Carruth }
406ef860a24SChandler Carruth 
407ef860a24SChandler Carruth void Module::Dematerialize(GlobalValue *GV) {
408ef860a24SChandler Carruth   if (Materializer)
409ef860a24SChandler Carruth     return Materializer->Dematerialize(GV);
410ef860a24SChandler Carruth }
411ef860a24SChandler Carruth 
412ef860a24SChandler Carruth bool Module::MaterializeAll(std::string *ErrInfo) {
413ef860a24SChandler Carruth   if (!Materializer)
414ef860a24SChandler Carruth     return false;
415ef860a24SChandler Carruth   return Materializer->MaterializeModule(this, ErrInfo);
416ef860a24SChandler Carruth }
417ef860a24SChandler Carruth 
418ef860a24SChandler Carruth bool Module::MaterializeAllPermanently(std::string *ErrInfo) {
419ef860a24SChandler Carruth   if (MaterializeAll(ErrInfo))
420ef860a24SChandler Carruth     return true;
421ef860a24SChandler Carruth   Materializer.reset();
422ef860a24SChandler Carruth   return false;
423ef860a24SChandler Carruth }
424ef860a24SChandler Carruth 
425ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
426ef860a24SChandler Carruth // Other module related stuff.
427ef860a24SChandler Carruth //
428ef860a24SChandler Carruth 
429ef860a24SChandler Carruth 
430ef860a24SChandler Carruth // dropAllReferences() - This function causes all the subelements to "let go"
431ef860a24SChandler Carruth // of all references that they are maintaining.  This allows one to 'delete' a
432ef860a24SChandler Carruth // whole module at a time, even though there may be circular references... first
433ef860a24SChandler Carruth // all references are dropped, and all use counts go to zero.  Then everything
434ef860a24SChandler Carruth // is deleted for real.  Note that no operations are valid on an object that
435ef860a24SChandler Carruth // has "dropped all references", except operator delete.
436ef860a24SChandler Carruth //
437ef860a24SChandler Carruth void Module::dropAllReferences() {
438ef860a24SChandler Carruth   for(Module::iterator I = begin(), E = end(); I != E; ++I)
439ef860a24SChandler Carruth     I->dropAllReferences();
440ef860a24SChandler Carruth 
441ef860a24SChandler Carruth   for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
442ef860a24SChandler Carruth     I->dropAllReferences();
443ef860a24SChandler Carruth 
444ef860a24SChandler Carruth   for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
445ef860a24SChandler Carruth     I->dropAllReferences();
446ef860a24SChandler Carruth }
447