blob: 341e527acb5b4150770ca2b59e6d223d9211cea1 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the Module class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner31cf9842001-06-30 04:35:40 +000015#include "llvm/InstrTypes.h"
Chris Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattnera483b062002-03-29 03:44:18 +000017#include "llvm/DerivedTypes.h"
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000018#include "llvm/GVMaterializer.h"
Owen Anderson6773d382009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Devang Patela6d20f42010-06-16 00:53:55 +000020#include "llvm/ADT/SmallString.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Owen Anderson9eb1a262006-05-18 02:10:31 +000022#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000024#include "SymbolTableListTraitsImpl.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000025#include "llvm/TypeSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000026#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000027#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000028#include <cstdlib>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner09bd1a02003-12-31 08:43:01 +000031//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000032// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000033//
34
Chris Lattnerf6c93e32005-01-30 00:09:23 +000035GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Chris Lattner46b5c642009-11-06 04:27:31 +000036 GlobalVariable *Ret = new GlobalVariable(Type::getInt32Ty(getGlobalContext()),
Owen Andersonb17f3292009-07-08 19:03:57 +000037 false, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000038 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
40 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000041}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000042GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
Owen Anderson55f1c092009-08-13 21:58:54 +000043 GlobalAlias *Ret = new GlobalAlias(Type::getInt32Ty(getGlobalContext()),
Dan Gohman929391a2008-01-29 12:09:55 +000044 GlobalValue::ExternalLinkage);
Anton Korobeynikova97b6942007-04-25 14:27:10 +000045 // This should not be garbage monitored.
46 LeakDetector::removeGarbageObject(Ret);
47 return Ret;
48}
Chris Lattner113f4f42002-06-25 16:13:24 +000049
Chris Lattner113f4f42002-06-25 16:13:24 +000050// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000051// are not in the public header file.
John McCall086bb4e2009-12-19 00:55:12 +000052template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
53template class llvm::SymbolTableListTraits<Function, Module>;
54template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000055
Chris Lattner09bd1a02003-12-31 08:43:01 +000056//===----------------------------------------------------------------------===//
57// Primitive Module methods.
58//
Chris Lattner446ad502001-10-13 06:58:40 +000059
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000060Module::Module(StringRef MID, LLVMContext& C)
Dan Gohman5cae1032010-08-04 01:39:08 +000061 : Context(C), Materializer(NULL), ModuleID(MID) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000062 ValSymTab = new ValueSymbolTable();
Reid Spencer32af9e82007-01-06 07:24:44 +000063 TypeSymTab = new TypeSymbolTable();
Dan Gohman2637cc12010-07-21 23:38:33 +000064 NamedMDSymTab = new StringMap<NamedMDNode *>();
Owen Anderson8e89e412010-09-08 18:03:32 +000065 Context.addModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000066}
67
68Module::~Module() {
Owen Anderson8e89e412010-09-08 18:03:32 +000069 Context.removeModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000070 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000071 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000072 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000073 AliasList.clear();
Reid Spencera0b05b32004-07-25 18:08:57 +000074 LibraryList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000075 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000076 delete ValSymTab;
77 delete TypeSymTab;
Dan Gohman2637cc12010-07-21 23:38:33 +000078 delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +000079}
80
Owen Anderson9eb1a262006-05-18 02:10:31 +000081/// Target endian information...
82Module::Endianness Module::getEndianness() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000083 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000084 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000085
Owen Anderson08aecf52006-05-18 05:46:08 +000086 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000087 StringRef token = DataLayout;
Chris Lattnerae12e352010-03-23 21:48:41 +000088 tie(token, temp) = getToken(temp, "-");
Owen Anderson9eb1a262006-05-18 02:10:31 +000089
90 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +000091 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000092 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +000093 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000094 }
95 }
96
Owen Anderson08aecf52006-05-18 05:46:08 +000097 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +000098}
99
Owen Anderson9eb1a262006-05-18 02:10:31 +0000100/// Target Pointer Size information...
101Module::PointerSize Module::getPointerSize() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000102 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000103 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000104
Owen Anderson08aecf52006-05-18 05:46:08 +0000105 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000106 StringRef token, signalToken;
107 tie(token, temp) = getToken(temp, "-");
108 tie(signalToken, token) = getToken(token, ":");
Owen Anderson9eb1a262006-05-18 02:10:31 +0000109
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000110 if (signalToken[0] == 'p') {
111 int size = 0;
112 getToken(token, ":").first.getAsInteger(10, size);
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000114 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000115 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000116 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000117 }
118 }
119
Owen Anderson08aecf52006-05-18 05:46:08 +0000120 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000121}
122
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000123/// getNamedValue - Return the first global value in the module with
124/// the specified name, of arbitrary type. This method returns null
125/// if a global with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000126GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000127 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
128}
129
Chris Lattnera0566972009-12-29 09:01:33 +0000130/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
131/// This ID is uniqued across modules in the current LLVMContext.
132unsigned Module::getMDKindID(StringRef Name) const {
133 return Context.getMDKindID(Name);
134}
135
136/// getMDKindNames - Populate client supplied SmallVector with the name for
137/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
138/// so it is filled in as an empty string.
139void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
140 return Context.getMDKindNames(Result);
141}
142
143
Chris Lattner09bd1a02003-12-31 08:43:01 +0000144//===----------------------------------------------------------------------===//
145// Methods for easy access to the functions in the module.
146//
147
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000148// getOrInsertFunction - Look up the specified function in the module symbol
149// table. If it does not exist, add a prototype for the function and return
150// it. This is nice because it allows most passes to get away with not handling
151// the symbol table directly for this common task.
152//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000153Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000154 const FunctionType *Ty,
155 AttrListPtr AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000156 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000157 GlobalValue *F = getNamedValue(Name);
Chris Lattner505c06b2007-01-07 08:09:25 +0000158 if (F == 0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000159 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000160 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000161 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
162 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000163 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000164 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000165 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000166
167 // Okay, the function exists. Does it have externally visible linkage?
Rafael Espindola6de96a12009-01-15 20:18:42 +0000168 if (F->hasLocalLinkage()) {
Chris Lattner42e983e2008-06-27 21:25:24 +0000169 // Clear the function's name.
170 F->setName("");
Chris Lattner505c06b2007-01-07 08:09:25 +0000171 // Retry, now there won't be a conflict.
Chris Lattner42e983e2008-06-27 21:25:24 +0000172 Constant *NewF = getOrInsertFunction(Name, Ty);
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000173 F->setName(Name);
Chris Lattner42e983e2008-06-27 21:25:24 +0000174 return NewF;
Chris Lattner505c06b2007-01-07 08:09:25 +0000175 }
176
177 // If the function exists but has the wrong type, return a bitcast to the
178 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000179 if (F->getType() != PointerType::getUnqual(Ty))
180 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Chris Lattner505c06b2007-01-07 08:09:25 +0000181
182 // Otherwise, we just found the existing function or a prototype.
183 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000184}
185
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000186Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
Dale Johannesenb842d522009-02-05 01:49:45 +0000187 const FunctionType *Ty,
188 AttrListPtr AttributeList) {
Dale Johannesenb842d522009-02-05 01:49:45 +0000189 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000190 GlobalValue *F = getNamedValue(Name);
Dale Johannesenb842d522009-02-05 01:49:45 +0000191 if (F == 0) {
192 // Nope, add it
193 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
194 New->setAttributes(AttributeList);
195 FunctionList.push_back(New);
196 return New; // Return the new prototype.
197 }
198
199 // Otherwise, we just found the existing function or a prototype.
200 return F;
201}
202
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000203Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000204 const FunctionType *Ty) {
205 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
206 return getOrInsertFunction(Name, Ty, AttributeList);
207}
208
Chris Lattnerbd717d82003-08-31 00:19:28 +0000209// getOrInsertFunction - Look up the specified function in the module symbol
210// table. If it does not exist, add a prototype for the function and return it.
211// This version of the method takes a null terminated list of function
212// arguments, which makes it easier for clients to use.
213//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000214Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000215 AttrListPtr AttributeList,
216 const Type *RetTy, ...) {
217 va_list Args;
218 va_start(Args, RetTy);
219
220 // Build the list of argument types...
221 std::vector<const Type*> ArgTys;
222 while (const Type *ArgTy = va_arg(Args, const Type*))
223 ArgTys.push_back(ArgTy);
224
225 va_end(Args);
226
227 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000228 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000229 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000230 AttributeList);
231}
232
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000233Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerbd717d82003-08-31 00:19:28 +0000234 const Type *RetTy, ...) {
235 va_list Args;
236 va_start(Args, RetTy);
237
238 // Build the list of argument types...
239 std::vector<const Type*> ArgTys;
240 while (const Type *ArgTy = va_arg(Args, const Type*))
241 ArgTys.push_back(ArgTy);
242
243 va_end(Args);
244
245 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000246 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000247 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000248 AttrListPtr::get((AttributeWithIndex *)0, 0));
Chris Lattnerbd717d82003-08-31 00:19:28 +0000249}
250
Chris Lattnera483b062002-03-29 03:44:18 +0000251// getFunction - Look up the specified function in the module symbol table.
252// If it does not exist, return null.
253//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000254Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000255 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000256}
257
Chris Lattner09bd1a02003-12-31 08:43:01 +0000258//===----------------------------------------------------------------------===//
259// Methods for easy access to the global variables in the module.
260//
261
262/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000263/// symbol table. If it does not exist, return null. The type argument
264/// should be the underlying type of the global, i.e., it should not have
265/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000266/// If AllowLocal is set to true, this function will return types that
267/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000268///
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000269GlobalVariable *Module::getGlobalVariable(StringRef Name,
Rafael Espindola6de96a12009-01-15 20:18:42 +0000270 bool AllowLocal) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000271 if (GlobalVariable *Result =
272 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
273 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000274 return Result;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000275 return 0;
276}
277
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000278/// getOrInsertGlobal - Look up the specified global in the module symbol table.
279/// 1. If it does not exist, add a declaration of the global and return it.
280/// 2. Else, the global exists but has the wrong type: return the function
281/// with a constantexpr cast to the right type.
282/// 3. Finally, if the existing global is the correct delclaration, return the
283/// existing global.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000284Constant *Module::getOrInsertGlobal(StringRef Name, const Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000285 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000286 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Bill Wendling2f409562008-11-04 22:51:24 +0000287 if (GV == 0) {
288 // Nope, add it
289 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000290 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
291 0, Name);
292 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000293 }
294
295 // If the variable exists but has the wrong type, return a bitcast to the
296 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000297 if (GV->getType() != PointerType::getUnqual(Ty))
298 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
Bill Wendling2f409562008-11-04 22:51:24 +0000299
300 // Otherwise, we just found the existing function or a prototype.
301 return GV;
302}
303
Chris Lattner09bd1a02003-12-31 08:43:01 +0000304//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000305// Methods for easy access to the global variables in the module.
306//
307
308// getNamedAlias - Look up the specified global in the module symbol table.
309// If it does not exist, return null.
310//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000311GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000312 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000313}
314
Devang Patel98250792009-07-30 23:59:04 +0000315/// getNamedMetadata - Return the first NamedMDNode in the module with the
316/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson45814342010-06-19 05:33:57 +0000317/// specified name is not found.
Devang Patelb6e058d2010-06-22 01:19:38 +0000318NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patela6d20f42010-06-16 00:53:55 +0000319 SmallString<256> NameData;
320 StringRef NameRef = Name.toStringRef(NameData);
Dan Gohman2637cc12010-07-21 23:38:33 +0000321 return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
Devang Patela6d20f42010-06-16 00:53:55 +0000322}
323
Devang Patel98250792009-07-30 23:59:04 +0000324/// getOrInsertNamedMetadata - Return the first named MDNode in the module
325/// with the specified name. This method returns a new NamedMDNode if a
326/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000327NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Dan Gohman2637cc12010-07-21 23:38:33 +0000328 NamedMDNode *&NMD =
329 (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
330 if (!NMD) {
331 NMD = new NamedMDNode(Name);
332 NMD->setParent(this);
333 NamedMDList.push_back(NMD);
334 }
Devang Patel98250792009-07-30 23:59:04 +0000335 return NMD;
336}
337
Dan Gohman2637cc12010-07-21 23:38:33 +0000338void Module::eraseNamedMetadata(NamedMDNode *NMD) {
339 static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
340 NamedMDList.erase(NMD);
341}
342
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000343//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000344// Methods for easy access to the types in the module.
345//
346
Chris Lattner1f985e02002-11-08 20:34:02 +0000347
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000348// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
349// there is already an entry for this name, true is returned and the symbol
350// table is not modified.
351//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000352bool Module::addTypeName(StringRef Name, const Type *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000353 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000354
Reid Spencer32af9e82007-01-06 07:24:44 +0000355 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000356
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000357 // Not in symbol table? Set the name with the Symtab as an argument so the
358 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000359 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000360
361 return false;
362}
363
364/// getTypeByName - Return the type with the specified name in this module, or
365/// null if there is none by that name.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000366const Type *Module::getTypeByName(StringRef Name) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000367 const TypeSymbolTable &ST = getTypeSymbolTable();
368 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000369}
Chris Lattner1f985e02002-11-08 20:34:02 +0000370
Chris Lattner10b7cb52002-04-13 18:58:33 +0000371// getTypeName - If there is at least one entry in the symbol table for the
372// specified type, return it.
373//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000374std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000375 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000376
Reid Spencer32af9e82007-01-06 07:24:44 +0000377 TypeSymbolTable::const_iterator TI = ST.begin();
378 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencerabb6f002004-05-25 08:52:20 +0000379 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000380
Reid Spencerabb6f002004-05-25 08:52:20 +0000381 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000382 ++TI;
383
384 if (TI != TE) // Must have found an entry!
385 return TI->first;
386 return ""; // Must not have found anything...
387}
388
Chris Lattner09bd1a02003-12-31 08:43:01 +0000389//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000390// Methods to control the materialization of GlobalValues in the Module.
391//
392void Module::setMaterializer(GVMaterializer *GVM) {
393 assert(!Materializer &&
394 "Module already has a GVMaterializer. Call MaterializeAllPermanently"
395 " to clear it out before setting another one.");
396 Materializer.reset(GVM);
397}
398
399bool Module::isMaterializable(const GlobalValue *GV) const {
400 if (Materializer)
401 return Materializer->isMaterializable(GV);
402 return false;
403}
404
405bool Module::isDematerializable(const GlobalValue *GV) const {
406 if (Materializer)
407 return Materializer->isDematerializable(GV);
408 return false;
409}
410
411bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
412 if (Materializer)
413 return Materializer->Materialize(GV, ErrInfo);
414 return false;
415}
416
417void Module::Dematerialize(GlobalValue *GV) {
418 if (Materializer)
419 return Materializer->Dematerialize(GV);
420}
421
422bool Module::MaterializeAll(std::string *ErrInfo) {
423 if (!Materializer)
424 return false;
425 return Materializer->MaterializeModule(this, ErrInfo);
426}
427
428bool Module::MaterializeAllPermanently(std::string *ErrInfo) {
429 if (MaterializeAll(ErrInfo))
430 return true;
431 Materializer.reset();
432 return false;
433}
434
435//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000436// Other module related stuff.
437//
438
439
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000440// dropAllReferences() - This function causes all the subelementss to "let go"
441// of all references that they are maintaining. This allows one to 'delete' a
442// whole module at a time, even though there may be circular references... first
443// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000444// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000445// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000446//
447void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000448 for(Module::iterator I = begin(), E = end(); I != E; ++I)
449 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000450
Chris Lattner531f9e92005-03-15 04:54:21 +0000451 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000452 I->dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000453
454 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
455 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456}
Chris Lattner31cf9842001-06-30 04:35:40 +0000457
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000458void Module::addLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000459 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
460 if (*I == Lib)
461 return;
462 LibraryList.push_back(Lib);
463}
464
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000465void Module::removeLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000466 LibraryListType::iterator I = LibraryList.begin();
467 LibraryListType::iterator E = LibraryList.end();
468 for (;I != E; ++I)
469 if (*I == Lib) {
470 LibraryList.erase(I);
471 return;
472 }
473}