blob: 38a51dfd5d388d341cf78750c246f57c268288ed [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)
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000061 : Context(C), Materializer(NULL), ModuleID(MID), DataLayout("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000062 ValSymTab = new ValueSymbolTable();
Reid Spencer32af9e82007-01-06 07:24:44 +000063 TypeSymTab = new TypeSymbolTable();
Devang Patelfcfee0f2010-01-07 19:39:36 +000064 NamedMDSymTab = new MDSymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000065}
66
67Module::~Module() {
68 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000069 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000070 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000071 AliasList.clear();
Reid Spencera0b05b32004-07-25 18:08:57 +000072 LibraryList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000073 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000074 delete ValSymTab;
75 delete TypeSymTab;
Devang Patel71ff5472010-01-09 01:44:59 +000076 delete NamedMDSymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000077}
78
Owen Anderson9eb1a262006-05-18 02:10:31 +000079/// Target endian information...
80Module::Endianness Module::getEndianness() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000081 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000082 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000083
Owen Anderson08aecf52006-05-18 05:46:08 +000084 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000085 StringRef token = DataLayout;
Chris Lattnerae12e352010-03-23 21:48:41 +000086 tie(token, temp) = getToken(temp, "-");
Owen Anderson9eb1a262006-05-18 02:10:31 +000087
88 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +000089 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000090 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +000091 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000092 }
93 }
94
Owen Anderson08aecf52006-05-18 05:46:08 +000095 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +000096}
97
Owen Anderson9eb1a262006-05-18 02:10:31 +000098/// Target Pointer Size information...
99Module::PointerSize Module::getPointerSize() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000100 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000101 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000102
Owen Anderson08aecf52006-05-18 05:46:08 +0000103 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000104 StringRef token, signalToken;
105 tie(token, temp) = getToken(temp, "-");
106 tie(signalToken, token) = getToken(token, ":");
Owen Anderson9eb1a262006-05-18 02:10:31 +0000107
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000108 if (signalToken[0] == 'p') {
109 int size = 0;
110 getToken(token, ":").first.getAsInteger(10, size);
Owen Anderson9eb1a262006-05-18 02:10:31 +0000111 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000112 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000114 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000115 }
116 }
117
Owen Anderson08aecf52006-05-18 05:46:08 +0000118 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000119}
120
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000121/// getNamedValue - Return the first global value in the module with
122/// the specified name, of arbitrary type. This method returns null
123/// if a global with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000124GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000125 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
126}
127
Chris Lattnera0566972009-12-29 09:01:33 +0000128/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
129/// This ID is uniqued across modules in the current LLVMContext.
130unsigned Module::getMDKindID(StringRef Name) const {
131 return Context.getMDKindID(Name);
132}
133
134/// getMDKindNames - Populate client supplied SmallVector with the name for
135/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
136/// so it is filled in as an empty string.
137void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
138 return Context.getMDKindNames(Result);
139}
140
141
Chris Lattner09bd1a02003-12-31 08:43:01 +0000142//===----------------------------------------------------------------------===//
143// Methods for easy access to the functions in the module.
144//
145
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000146// getOrInsertFunction - Look up the specified function in the module symbol
147// table. If it does not exist, add a prototype for the function and return
148// it. This is nice because it allows most passes to get away with not handling
149// the symbol table directly for this common task.
150//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000151Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000152 const FunctionType *Ty,
153 AttrListPtr AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000154 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000155 GlobalValue *F = getNamedValue(Name);
Chris Lattner505c06b2007-01-07 08:09:25 +0000156 if (F == 0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000157 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000158 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000159 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
160 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000161 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000162 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000163 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000164
165 // Okay, the function exists. Does it have externally visible linkage?
Rafael Espindola6de96a12009-01-15 20:18:42 +0000166 if (F->hasLocalLinkage()) {
Chris Lattner42e983e2008-06-27 21:25:24 +0000167 // Clear the function's name.
168 F->setName("");
Chris Lattner505c06b2007-01-07 08:09:25 +0000169 // Retry, now there won't be a conflict.
Chris Lattner42e983e2008-06-27 21:25:24 +0000170 Constant *NewF = getOrInsertFunction(Name, Ty);
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000171 F->setName(Name);
Chris Lattner42e983e2008-06-27 21:25:24 +0000172 return NewF;
Chris Lattner505c06b2007-01-07 08:09:25 +0000173 }
174
175 // If the function exists but has the wrong type, return a bitcast to the
176 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000177 if (F->getType() != PointerType::getUnqual(Ty))
178 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Chris Lattner505c06b2007-01-07 08:09:25 +0000179
180 // Otherwise, we just found the existing function or a prototype.
181 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000182}
183
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000184Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
Dale Johannesenb842d522009-02-05 01:49:45 +0000185 const FunctionType *Ty,
186 AttrListPtr AttributeList) {
Dale Johannesenb842d522009-02-05 01:49:45 +0000187 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000188 GlobalValue *F = getNamedValue(Name);
Dale Johannesenb842d522009-02-05 01:49:45 +0000189 if (F == 0) {
190 // Nope, add it
191 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
192 New->setAttributes(AttributeList);
193 FunctionList.push_back(New);
194 return New; // Return the new prototype.
195 }
196
197 // Otherwise, we just found the existing function or a prototype.
198 return F;
199}
200
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000201Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000202 const FunctionType *Ty) {
203 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
204 return getOrInsertFunction(Name, Ty, AttributeList);
205}
206
Chris Lattnerbd717d82003-08-31 00:19:28 +0000207// getOrInsertFunction - Look up the specified function in the module symbol
208// table. If it does not exist, add a prototype for the function and return it.
209// This version of the method takes a null terminated list of function
210// arguments, which makes it easier for clients to use.
211//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000212Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000213 AttrListPtr AttributeList,
214 const Type *RetTy, ...) {
215 va_list Args;
216 va_start(Args, RetTy);
217
218 // Build the list of argument types...
219 std::vector<const Type*> ArgTys;
220 while (const Type *ArgTy = va_arg(Args, const Type*))
221 ArgTys.push_back(ArgTy);
222
223 va_end(Args);
224
225 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000226 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000227 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000228 AttributeList);
229}
230
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000231Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerbd717d82003-08-31 00:19:28 +0000232 const Type *RetTy, ...) {
233 va_list Args;
234 va_start(Args, RetTy);
235
236 // Build the list of argument types...
237 std::vector<const Type*> ArgTys;
238 while (const Type *ArgTy = va_arg(Args, const Type*))
239 ArgTys.push_back(ArgTy);
240
241 va_end(Args);
242
243 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000244 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000245 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000246 AttrListPtr::get((AttributeWithIndex *)0, 0));
Chris Lattnerbd717d82003-08-31 00:19:28 +0000247}
248
Chris Lattnera483b062002-03-29 03:44:18 +0000249// getFunction - Look up the specified function in the module symbol table.
250// If it does not exist, return null.
251//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000252Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000253 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000254}
255
Chris Lattner09bd1a02003-12-31 08:43:01 +0000256//===----------------------------------------------------------------------===//
257// Methods for easy access to the global variables in the module.
258//
259
260/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000261/// symbol table. If it does not exist, return null. The type argument
262/// should be the underlying type of the global, i.e., it should not have
263/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000264/// If AllowLocal is set to true, this function will return types that
265/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000266///
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000267GlobalVariable *Module::getGlobalVariable(StringRef Name,
Rafael Espindola6de96a12009-01-15 20:18:42 +0000268 bool AllowLocal) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000269 if (GlobalVariable *Result =
270 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
271 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000272 return Result;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000273 return 0;
274}
275
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000276/// getOrInsertGlobal - Look up the specified global in the module symbol table.
277/// 1. If it does not exist, add a declaration of the global and return it.
278/// 2. Else, the global exists but has the wrong type: return the function
279/// with a constantexpr cast to the right type.
280/// 3. Finally, if the existing global is the correct delclaration, return the
281/// existing global.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000282Constant *Module::getOrInsertGlobal(StringRef Name, const Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000283 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000284 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Bill Wendling2f409562008-11-04 22:51:24 +0000285 if (GV == 0) {
286 // Nope, add it
287 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000288 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
289 0, Name);
290 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000291 }
292
293 // If the variable exists but has the wrong type, return a bitcast to the
294 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000295 if (GV->getType() != PointerType::getUnqual(Ty))
296 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
Bill Wendling2f409562008-11-04 22:51:24 +0000297
298 // Otherwise, we just found the existing function or a prototype.
299 return GV;
300}
301
Chris Lattner09bd1a02003-12-31 08:43:01 +0000302//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000303// Methods for easy access to the global variables in the module.
304//
305
306// getNamedAlias - Look up the specified global in the module symbol table.
307// If it does not exist, return null.
308//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000309GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000310 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000311}
312
Devang Patel98250792009-07-30 23:59:04 +0000313/// getNamedMetadata - Return the first NamedMDNode in the module with the
314/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson45814342010-06-19 05:33:57 +0000315/// specified name is not found.
Devang Patelb6e058d2010-06-22 01:19:38 +0000316NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patela6d20f42010-06-16 00:53:55 +0000317 SmallString<256> NameData;
318 StringRef NameRef = Name.toStringRef(NameData);
Bob Wilson45814342010-06-19 05:33:57 +0000319 return NamedMDSymTab->lookup(NameRef);
Devang Patela6d20f42010-06-16 00:53:55 +0000320}
321
Devang Patel98250792009-07-30 23:59:04 +0000322/// getOrInsertNamedMetadata - Return the first named MDNode in the module
323/// with the specified name. This method returns a new NamedMDNode if a
324/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000325NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Devang Patelfcfee0f2010-01-07 19:39:36 +0000326 NamedMDNode *NMD = NamedMDSymTab->lookup(Name);
Devang Patel98250792009-07-30 23:59:04 +0000327 if (!NMD)
Owen Anderson55f1c092009-08-13 21:58:54 +0000328 NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this);
Devang Patel98250792009-07-30 23:59:04 +0000329 return NMD;
330}
331
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000332//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000333// Methods for easy access to the types in the module.
334//
335
Chris Lattner1f985e02002-11-08 20:34:02 +0000336
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000337// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
338// there is already an entry for this name, true is returned and the symbol
339// table is not modified.
340//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000341bool Module::addTypeName(StringRef Name, const Type *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000342 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000343
Reid Spencer32af9e82007-01-06 07:24:44 +0000344 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000346 // Not in symbol table? Set the name with the Symtab as an argument so the
347 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000348 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000349
350 return false;
351}
352
353/// getTypeByName - Return the type with the specified name in this module, or
354/// null if there is none by that name.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000355const Type *Module::getTypeByName(StringRef Name) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000356 const TypeSymbolTable &ST = getTypeSymbolTable();
357 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000358}
Chris Lattner1f985e02002-11-08 20:34:02 +0000359
Chris Lattner10b7cb52002-04-13 18:58:33 +0000360// getTypeName - If there is at least one entry in the symbol table for the
361// specified type, return it.
362//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000363std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000364 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000365
Reid Spencer32af9e82007-01-06 07:24:44 +0000366 TypeSymbolTable::const_iterator TI = ST.begin();
367 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencerabb6f002004-05-25 08:52:20 +0000368 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000369
Reid Spencerabb6f002004-05-25 08:52:20 +0000370 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000371 ++TI;
372
373 if (TI != TE) // Must have found an entry!
374 return TI->first;
375 return ""; // Must not have found anything...
376}
377
Chris Lattner09bd1a02003-12-31 08:43:01 +0000378//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000379// Methods to control the materialization of GlobalValues in the Module.
380//
381void Module::setMaterializer(GVMaterializer *GVM) {
382 assert(!Materializer &&
383 "Module already has a GVMaterializer. Call MaterializeAllPermanently"
384 " to clear it out before setting another one.");
385 Materializer.reset(GVM);
386}
387
388bool Module::isMaterializable(const GlobalValue *GV) const {
389 if (Materializer)
390 return Materializer->isMaterializable(GV);
391 return false;
392}
393
394bool Module::isDematerializable(const GlobalValue *GV) const {
395 if (Materializer)
396 return Materializer->isDematerializable(GV);
397 return false;
398}
399
400bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
401 if (Materializer)
402 return Materializer->Materialize(GV, ErrInfo);
403 return false;
404}
405
406void Module::Dematerialize(GlobalValue *GV) {
407 if (Materializer)
408 return Materializer->Dematerialize(GV);
409}
410
411bool Module::MaterializeAll(std::string *ErrInfo) {
412 if (!Materializer)
413 return false;
414 return Materializer->MaterializeModule(this, ErrInfo);
415}
416
417bool Module::MaterializeAllPermanently(std::string *ErrInfo) {
418 if (MaterializeAll(ErrInfo))
419 return true;
420 Materializer.reset();
421 return false;
422}
423
424//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000425// Other module related stuff.
426//
427
428
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000429// dropAllReferences() - This function causes all the subelementss to "let go"
430// of all references that they are maintaining. This allows one to 'delete' a
431// whole module at a time, even though there may be circular references... first
432// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000433// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000434// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435//
436void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000437 for(Module::iterator I = begin(), E = end(); I != E; ++I)
438 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000439
Chris Lattner531f9e92005-03-15 04:54:21 +0000440 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000441 I->dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000442
443 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
444 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445}
Chris Lattner31cf9842001-06-30 04:35:40 +0000446
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000447void Module::addLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000448 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
449 if (*I == Lib)
450 return;
451 LibraryList.push_back(Lib);
452}
453
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000454void Module::removeLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000455 LibraryListType::iterator I = LibraryList.begin();
456 LibraryListType::iterator E = LibraryList.end();
457 for (;I != E; ++I)
458 if (*I == Lib) {
459 LibraryList.erase(I);
460 return;
461 }
462}