blob: ee4dc62f9d1c12bbd9c8764fa947b037cd4ee4d6 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the Module class for the IR library.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "SymbolTableListTraitsImpl.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringExtras.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000020#include "llvm/Constants.h"
Chris Lattner6056c492002-03-29 03:44:18 +000021#include "llvm/DerivedTypes.h"
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000022#include "llvm/GVMaterializer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/InstrTypes.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000024#include "llvm/LLVMContext.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/LeakDetector.h"
Chris Lattner7e708292002-06-25 16:13:24 +000026#include <algorithm>
Chris Lattner0ae8e872003-08-31 00:19:28 +000027#include <cstdarg>
Owen Anderson1d8b8532006-05-18 02:10:31 +000028#include <cstdlib>
Chris Lattner31f84992003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner60837822003-12-31 08:43:01 +000031//===----------------------------------------------------------------------===//
Misha Brukmanaf76cfb2004-04-21 18:27:56 +000032// Methods to implement the globals and functions lists.
Chris Lattner60837822003-12-31 08:43:01 +000033//
34
Chris Lattner7e708292002-06-25 16:13:24 +000035// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnercc041ba2006-01-24 04:13:11 +000036// are not in the public header file.
John McCallc63ca0a2009-12-19 00:55:12 +000037template class llvm::SymbolTableListTraits<Function, Module>;
Nick Lewycky53b2b732011-08-13 01:04:44 +000038template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
John McCallc63ca0a2009-12-19 00:55:12 +000039template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattner60837822003-12-31 08:43:01 +000041//===----------------------------------------------------------------------===//
42// Primitive Module methods.
43//
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +000044
Daniel Dunbar2928c832009-11-06 10:58:06 +000045Module::Module(StringRef MID, LLVMContext& C)
Dan Gohmand98af0a2010-08-04 01:39:08 +000046 : Context(C), Materializer(NULL), ModuleID(MID) {
Reid Spenceref9b9a72007-02-05 20:47:22 +000047 ValSymTab = new ValueSymbolTable();
Dan Gohman17aa92c2010-07-21 23:38:33 +000048 NamedMDSymTab = new StringMap<NamedMDNode *>();
Owen Anderson30268be2010-09-08 18:03:32 +000049 Context.addModule(this);
Chris Lattner00950542001-06-06 20:29:01 +000050}
51
52Module::~Module() {
Owen Anderson30268be2010-09-08 18:03:32 +000053 Context.removeModule(this);
Chris Lattner00950542001-06-06 20:29:01 +000054 dropAllReferences();
Chris Lattner7e708292002-06-25 16:13:24 +000055 GlobalList.clear();
Chris Lattner7e708292002-06-25 16:13:24 +000056 FunctionList.clear();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000057 AliasList.clear();
Devang Patel28bc9d82009-07-29 17:16:17 +000058 NamedMDList.clear();
Reid Spencer78d033e2007-01-06 07:24:44 +000059 delete ValSymTab;
Dan Gohman17aa92c2010-07-21 23:38:33 +000060 delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
Chris Lattner00950542001-06-06 20:29:01 +000061}
62
Chris Lattner1afcace2011-07-09 17:41:24 +000063/// Target endian information.
Owen Anderson1d8b8532006-05-18 02:10:31 +000064Module::Endianness Module::getEndianness() const {
Benjamin Kramerd4f19592010-01-11 18:03:24 +000065 StringRef temp = DataLayout;
Owen Andersonac4c75b2006-05-18 05:46:08 +000066 Module::Endianness ret = AnyEndianness;
Bill Wendling85d043d2012-04-23 00:23:33 +000067
Owen Andersonac4c75b2006-05-18 05:46:08 +000068 while (!temp.empty()) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +000069 std::pair<StringRef, StringRef> P = getToken(temp, "-");
Bill Wendling85d043d2012-04-23 00:23:33 +000070
Chris Lattnerc30a38f2011-07-21 06:21:31 +000071 StringRef token = P.first;
72 temp = P.second;
Bill Wendling85d043d2012-04-23 00:23:33 +000073
Owen Anderson1d8b8532006-05-18 02:10:31 +000074 if (token[0] == 'e') {
Owen Andersonac4c75b2006-05-18 05:46:08 +000075 ret = LittleEndian;
Owen Anderson1d8b8532006-05-18 02:10:31 +000076 } else if (token[0] == 'E') {
Owen Andersonac4c75b2006-05-18 05:46:08 +000077 ret = BigEndian;
Owen Anderson1d8b8532006-05-18 02:10:31 +000078 }
79 }
Bill Wendling85d043d2012-04-23 00:23:33 +000080
Owen Andersonac4c75b2006-05-18 05:46:08 +000081 return ret;
Owen Anderson1d8b8532006-05-18 02:10:31 +000082}
83
Chris Lattnerc30a38f2011-07-21 06:21:31 +000084/// Target Pointer Size information.
Owen Anderson1d8b8532006-05-18 02:10:31 +000085Module::PointerSize Module::getPointerSize() const {
Benjamin Kramerd4f19592010-01-11 18:03:24 +000086 StringRef temp = DataLayout;
Owen Andersonac4c75b2006-05-18 05:46:08 +000087 Module::PointerSize ret = AnyPointerSize;
Bill Wendling85d043d2012-04-23 00:23:33 +000088
Owen Andersonac4c75b2006-05-18 05:46:08 +000089 while (!temp.empty()) {
Chris Lattnerc30a38f2011-07-21 06:21:31 +000090 std::pair<StringRef, StringRef> TmpP = getToken(temp, "-");
91 temp = TmpP.second;
92 TmpP = getToken(TmpP.first, ":");
93 StringRef token = TmpP.second, signalToken = TmpP.first;
Bill Wendling85d043d2012-04-23 00:23:33 +000094
Benjamin Kramerd4f19592010-01-11 18:03:24 +000095 if (signalToken[0] == 'p') {
96 int size = 0;
97 getToken(token, ":").first.getAsInteger(10, size);
Owen Anderson1d8b8532006-05-18 02:10:31 +000098 if (size == 32)
Owen Andersonac4c75b2006-05-18 05:46:08 +000099 ret = Pointer32;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000100 else if (size == 64)
Owen Andersonac4c75b2006-05-18 05:46:08 +0000101 ret = Pointer64;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000102 }
103 }
Bill Wendling85d043d2012-04-23 00:23:33 +0000104
Owen Andersonac4c75b2006-05-18 05:46:08 +0000105 return ret;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000106}
107
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000108/// getNamedValue - Return the first global value in the module with
109/// the specified name, of arbitrary type. This method returns null
110/// if a global with the specified name is not found.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000111GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000112 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
113}
114
Chris Lattner08113472009-12-29 09:01:33 +0000115/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
116/// This ID is uniqued across modules in the current LLVMContext.
117unsigned Module::getMDKindID(StringRef Name) const {
118 return Context.getMDKindID(Name);
119}
120
121/// getMDKindNames - Populate client supplied SmallVector with the name for
122/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
123/// so it is filled in as an empty string.
124void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
125 return Context.getMDKindNames(Result);
126}
127
128
Chris Lattner60837822003-12-31 08:43:01 +0000129//===----------------------------------------------------------------------===//
130// Methods for easy access to the functions in the module.
131//
132
Reid Spenceref9b9a72007-02-05 20:47:22 +0000133// getOrInsertFunction - Look up the specified function in the module symbol
134// table. If it does not exist, add a prototype for the function and return
135// it. This is nice because it allows most passes to get away with not handling
136// the symbol table directly for this common task.
137//
Daniel Dunbar2928c832009-11-06 10:58:06 +0000138Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000139 FunctionType *Ty,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000140 AttributeSet AttributeList) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000141 // See if we have a definition for the specified function already.
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000142 GlobalValue *F = getNamedValue(Name);
Chris Lattner70d13052007-01-07 08:09:25 +0000143 if (F == 0) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000144 // Nope, add it
Gabor Greif051a9502008-04-06 20:25:17 +0000145 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky1186bf12009-01-04 22:54:40 +0000146 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
147 New->setAttributes(AttributeList);
Chris Lattner6056c492002-03-29 03:44:18 +0000148 FunctionList.push_back(New);
Chris Lattner70d13052007-01-07 08:09:25 +0000149 return New; // Return the new prototype.
Chris Lattner6056c492002-03-29 03:44:18 +0000150 }
Chris Lattner70d13052007-01-07 08:09:25 +0000151
152 // Okay, the function exists. Does it have externally visible linkage?
Rafael Espindolabb46f522009-01-15 20:18:42 +0000153 if (F->hasLocalLinkage()) {
Chris Lattner48cd7122008-06-27 21:25:24 +0000154 // Clear the function's name.
155 F->setName("");
Chris Lattner70d13052007-01-07 08:09:25 +0000156 // Retry, now there won't be a conflict.
Chris Lattner48cd7122008-06-27 21:25:24 +0000157 Constant *NewF = getOrInsertFunction(Name, Ty);
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000158 F->setName(Name);
Chris Lattner48cd7122008-06-27 21:25:24 +0000159 return NewF;
Chris Lattner70d13052007-01-07 08:09:25 +0000160 }
161
162 // If the function exists but has the wrong type, return a bitcast to the
163 // right type.
Owen Andersondebcb012009-07-29 22:17:13 +0000164 if (F->getType() != PointerType::getUnqual(Ty))
165 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Bill Wendling85d043d2012-04-23 00:23:33 +0000166
Chris Lattner70d13052007-01-07 08:09:25 +0000167 // Otherwise, we just found the existing function or a prototype.
Bill Wendling85d043d2012-04-23 00:23:33 +0000168 return F;
Chris Lattner6056c492002-03-29 03:44:18 +0000169}
170
Daniel Dunbar2928c832009-11-06 10:58:06 +0000171Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000172 FunctionType *Ty,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000173 AttributeSet AttributeList) {
Dale Johannesen49de9822009-02-05 01:49:45 +0000174 // See if we have a definition for the specified function already.
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000175 GlobalValue *F = getNamedValue(Name);
Dale Johannesen49de9822009-02-05 01:49:45 +0000176 if (F == 0) {
177 // Nope, add it
178 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
179 New->setAttributes(AttributeList);
180 FunctionList.push_back(New);
181 return New; // Return the new prototype.
182 }
183
184 // Otherwise, we just found the existing function or a prototype.
Bill Wendling85d043d2012-04-23 00:23:33 +0000185 return F;
Dale Johannesen49de9822009-02-05 01:49:45 +0000186}
187
Daniel Dunbar2928c832009-11-06 10:58:06 +0000188Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000189 FunctionType *Ty) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000190 return getOrInsertFunction(Name, Ty, AttributeSet());
Nick Lewycky1186bf12009-01-04 22:54:40 +0000191}
192
Chris Lattner0ae8e872003-08-31 00:19:28 +0000193// getOrInsertFunction - Look up the specified function in the module symbol
194// table. If it does not exist, add a prototype for the function and return it.
195// This version of the method takes a null terminated list of function
196// arguments, which makes it easier for clients to use.
197//
Daniel Dunbar2928c832009-11-06 10:58:06 +0000198Constant *Module::getOrInsertFunction(StringRef Name,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000199 AttributeSet AttributeList,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000200 Type *RetTy, ...) {
Nick Lewycky1186bf12009-01-04 22:54:40 +0000201 va_list Args;
202 va_start(Args, RetTy);
203
204 // Build the list of argument types...
Jay Foad5fdd6c82011-07-12 14:06:48 +0000205 std::vector<Type*> ArgTys;
206 while (Type *ArgTy = va_arg(Args, Type*))
Nick Lewycky1186bf12009-01-04 22:54:40 +0000207 ArgTys.push_back(ArgTy);
208
209 va_end(Args);
210
211 // Build the function type and chain to the other getOrInsertFunction...
Owen Andersonc341f1c2009-07-08 23:50:31 +0000212 return getOrInsertFunction(Name,
Owen Andersondebcb012009-07-29 22:17:13 +0000213 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky1186bf12009-01-04 22:54:40 +0000214 AttributeList);
215}
216
Daniel Dunbar2928c832009-11-06 10:58:06 +0000217Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000218 Type *RetTy, ...) {
Chris Lattner0ae8e872003-08-31 00:19:28 +0000219 va_list Args;
220 va_start(Args, RetTy);
221
222 // Build the list of argument types...
Jay Foad5fdd6c82011-07-12 14:06:48 +0000223 std::vector<Type*> ArgTys;
224 while (Type *ArgTy = va_arg(Args, Type*))
Chris Lattner0ae8e872003-08-31 00:19:28 +0000225 ArgTys.push_back(ArgTy);
226
227 va_end(Args);
228
229 // Build the function type and chain to the other getOrInsertFunction...
Bill Wendling85d043d2012-04-23 00:23:33 +0000230 return getOrInsertFunction(Name,
Owen Andersondebcb012009-07-29 22:17:13 +0000231 FunctionType::get(RetTy, ArgTys, false),
Bill Wendling99faa3b2012-12-07 23:16:57 +0000232 AttributeSet());
Chris Lattner0ae8e872003-08-31 00:19:28 +0000233}
234
Chris Lattner6056c492002-03-29 03:44:18 +0000235// getFunction - Look up the specified function in the module symbol table.
236// If it does not exist, return null.
237//
Daniel Dunbar2928c832009-11-06 10:58:06 +0000238Function *Module::getFunction(StringRef Name) const {
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000239 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnerbb0e2482008-06-27 21:09:10 +0000240}
241
Chris Lattner60837822003-12-31 08:43:01 +0000242//===----------------------------------------------------------------------===//
243// Methods for easy access to the global variables in the module.
244//
245
246/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner30614672005-12-05 05:30:21 +0000247/// symbol table. If it does not exist, return null. The type argument
248/// should be the underlying type of the global, i.e., it should not have
249/// the top-level PointerType, which represents the address of the global.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000250/// If AllowLocal is set to true, this function will return types that
251/// have an local. By default, these types are not returned.
Chris Lattner60837822003-12-31 08:43:01 +0000252///
Daniel Dunbar2928c832009-11-06 10:58:06 +0000253GlobalVariable *Module::getGlobalVariable(StringRef Name,
Rafael Espindolabb46f522009-01-15 20:18:42 +0000254 bool AllowLocal) const {
Bill Wendling85d043d2012-04-23 00:23:33 +0000255 if (GlobalVariable *Result =
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000256 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
257 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner60837822003-12-31 08:43:01 +0000258 return Result;
Chris Lattner60837822003-12-31 08:43:01 +0000259 return 0;
260}
261
Bill Wendlingb4b130f2008-11-05 23:42:27 +0000262/// getOrInsertGlobal - Look up the specified global in the module symbol table.
263/// 1. If it does not exist, add a declaration of the global and return it.
264/// 2. Else, the global exists but has the wrong type: return the function
265/// with a constantexpr cast to the right type.
266/// 3. Finally, if the existing global is the correct delclaration, return the
267/// existing global.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000268Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
Bill Wendlingb7c2c122008-11-04 22:51:24 +0000269 // See if we have a definition for the specified global already.
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000270 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Bill Wendlingb7c2c122008-11-04 22:51:24 +0000271 if (GV == 0) {
272 // Nope, add it
273 GlobalVariable *New =
Owen Andersone9b11b42009-07-08 19:03:57 +0000274 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
275 0, Name);
276 return New; // Return the new declaration.
Bill Wendlingb7c2c122008-11-04 22:51:24 +0000277 }
278
279 // If the variable exists but has the wrong type, return a bitcast to the
280 // right type.
Owen Andersondebcb012009-07-29 22:17:13 +0000281 if (GV->getType() != PointerType::getUnqual(Ty))
282 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
Bill Wendling85d043d2012-04-23 00:23:33 +0000283
Bill Wendlingb7c2c122008-11-04 22:51:24 +0000284 // Otherwise, we just found the existing function or a prototype.
285 return GV;
286}
287
Chris Lattner60837822003-12-31 08:43:01 +0000288//===----------------------------------------------------------------------===//
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000289// Methods for easy access to the global variables in the module.
290//
291
292// getNamedAlias - Look up the specified global in the module symbol table.
293// If it does not exist, return null.
294//
Daniel Dunbar2928c832009-11-06 10:58:06 +0000295GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbarf56ec642009-03-06 22:04:43 +0000296 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000297}
298
Devang Patel49fe6c92009-07-30 23:59:04 +0000299/// getNamedMetadata - Return the first NamedMDNode in the module with the
Bill Wendling85d043d2012-04-23 00:23:33 +0000300/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson54eee522010-06-19 05:33:57 +0000301/// specified name is not found.
Devang Patela762b092010-06-22 01:19:38 +0000302NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patel2f7d5292010-06-16 00:53:55 +0000303 SmallString<256> NameData;
304 StringRef NameRef = Name.toStringRef(NameData);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000305 return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
Devang Patel2f7d5292010-06-16 00:53:55 +0000306}
307
Bill Wendling85d043d2012-04-23 00:23:33 +0000308/// getOrInsertNamedMetadata - Return the first named MDNode in the module
309/// with the specified name. This method returns a new NamedMDNode if a
Devang Patel49fe6c92009-07-30 23:59:04 +0000310/// NamedMDNode with the specified name is not found.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000311NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Dan Gohman17aa92c2010-07-21 23:38:33 +0000312 NamedMDNode *&NMD =
313 (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
314 if (!NMD) {
315 NMD = new NamedMDNode(Name);
316 NMD->setParent(this);
317 NamedMDList.push_back(NMD);
318 }
Devang Patel49fe6c92009-07-30 23:59:04 +0000319 return NMD;
320}
321
Bill Wendlingd34cb1e2012-02-11 11:38:06 +0000322/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
323/// delete it.
Dan Gohman17aa92c2010-07-21 23:38:33 +0000324void Module::eraseNamedMetadata(NamedMDNode *NMD) {
325 static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
326 NamedMDList.erase(NMD);
327}
328
Bill Wendlingf20f2812012-02-15 22:34:20 +0000329/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
330void Module::
331getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
332 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
333 if (!ModFlags) return;
334
335 for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) {
336 MDNode *Flag = ModFlags->getOperand(i);
337 ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
338 MDString *Key = cast<MDString>(Flag->getOperand(1));
339 Value *Val = Flag->getOperand(2);
Bill Wendlinge6bd7a82012-02-16 10:28:10 +0000340 Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
Bill Wendling426f2152012-02-15 23:27:50 +0000341 Key, Val));
Bill Wendlingf20f2812012-02-15 22:34:20 +0000342 }
343}
344
Bill Wendlingd34cb1e2012-02-11 11:38:06 +0000345/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
346/// represents module-level flags. This method returns null if there are no
347/// module-level flags.
348NamedMDNode *Module::getModuleFlagsMetadata() const {
349 return getNamedMetadata("llvm.module.flags");
350}
351
352/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
353/// represents module-level flags. If module-level flags aren't found, it
354/// creates the named metadata that contains them.
355NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
356 return getOrInsertNamedMetadata("llvm.module.flags");
357}
358
359/// addModuleFlag - Add a module-level flag to the module-level flags
360/// metadata. It will create the module-level flags named metadata if it doesn't
361/// already exist.
Bill Wendlinge6bd7a82012-02-16 10:28:10 +0000362void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendlingd34cb1e2012-02-11 11:38:06 +0000363 Value *Val) {
364 Type *Int32Ty = Type::getInt32Ty(Context);
365 Value *Ops[3] = {
366 ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
367 };
368 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
369}
Bill Wendlinge6bd7a82012-02-16 10:28:10 +0000370void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendlingd34cb1e2012-02-11 11:38:06 +0000371 uint32_t Val) {
372 Type *Int32Ty = Type::getInt32Ty(Context);
373 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
374}
375void Module::addModuleFlag(MDNode *Node) {
376 assert(Node->getNumOperands() == 3 &&
377 "Invalid number of operands for module flag!");
378 assert(isa<ConstantInt>(Node->getOperand(0)) &&
379 isa<MDString>(Node->getOperand(1)) &&
380 "Invalid operand types for module flag!");
381 getOrInsertModuleFlagsMetadata()->addOperand(Node);
382}
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000383
Chris Lattner60837822003-12-31 08:43:01 +0000384//===----------------------------------------------------------------------===//
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000385// Methods to control the materialization of GlobalValues in the Module.
386//
387void Module::setMaterializer(GVMaterializer *GVM) {
388 assert(!Materializer &&
389 "Module already has a GVMaterializer. Call MaterializeAllPermanently"
390 " to clear it out before setting another one.");
391 Materializer.reset(GVM);
392}
393
394bool Module::isMaterializable(const GlobalValue *GV) const {
395 if (Materializer)
396 return Materializer->isMaterializable(GV);
397 return false;
398}
399
400bool Module::isDematerializable(const GlobalValue *GV) const {
401 if (Materializer)
402 return Materializer->isDematerializable(GV);
403 return false;
404}
405
406bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
407 if (Materializer)
408 return Materializer->Materialize(GV, ErrInfo);
409 return false;
410}
411
412void Module::Dematerialize(GlobalValue *GV) {
413 if (Materializer)
414 return Materializer->Dematerialize(GV);
415}
416
417bool Module::MaterializeAll(std::string *ErrInfo) {
418 if (!Materializer)
419 return false;
420 return Materializer->MaterializeModule(this, ErrInfo);
421}
422
423bool Module::MaterializeAllPermanently(std::string *ErrInfo) {
424 if (MaterializeAll(ErrInfo))
425 return true;
426 Materializer.reset();
427 return false;
428}
429
430//===----------------------------------------------------------------------===//
Chris Lattner60837822003-12-31 08:43:01 +0000431// Other module related stuff.
432//
433
434
Eric Christophercf84d862012-04-16 23:54:31 +0000435// dropAllReferences() - This function causes all the subelements to "let go"
Chris Lattner9da07152002-08-17 23:32:47 +0000436// of all references that they are maintaining. This allows one to 'delete' a
437// whole module at a time, even though there may be circular references... first
438// all references are dropped, and all use counts go to zero. Then everything
Misha Brukman6b634522003-10-10 17:54:14 +0000439// is deleted for real. Note that no operations are valid on an object that
Chris Lattner9da07152002-08-17 23:32:47 +0000440// has "dropped all references", except operator delete.
Chris Lattner00950542001-06-06 20:29:01 +0000441//
442void Module::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000443 for(Module::iterator I = begin(), E = end(); I != E; ++I)
444 I->dropAllReferences();
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000445
Chris Lattnere4d5c442005-03-15 04:54:21 +0000446 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000447 I->dropAllReferences();
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000448
449 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
450 I->dropAllReferences();
Chris Lattner00950542001-06-06 20:29:01 +0000451}