blob: ef94796b24285df3b317bbf876300709ecae0a48 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/ADT/STLExtras.h"
Owen Anderson9eb1a262006-05-18 02:10:31 +000019#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000022#include "llvm/TypeSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000023#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000024#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000025#include <cstdlib>
Chris Lattner189d19f2003-11-21 20:23:48 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner09bd1a02003-12-31 08:43:01 +000028//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000029// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000030//
31
Chris Lattnerf6c93e32005-01-30 00:09:23 +000032Function *ilist_traits<Function>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000033 FunctionType *FTy =
Reid Spencere76289b2007-04-09 06:12:07 +000034 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Gabor Greife9ecc682008-04-06 20:25:17 +000035 Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000036 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000039}
Chris Lattnerf6c93e32005-01-30 00:09:23 +000040GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000041 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false,
Chris Lattner379a8d22003-04-16 20:28:45 +000042 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000043 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
45 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000046}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000047GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
Dan Gohman929391a2008-01-29 12:09:55 +000048 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
49 GlobalValue::ExternalLinkage);
Anton Korobeynikova97b6942007-04-25 14:27:10 +000050 // This should not be garbage monitored.
51 LeakDetector::removeGarbageObject(Ret);
52 return Ret;
53}
Chris Lattner113f4f42002-06-25 16:13:24 +000054
55iplist<Function> &ilist_traits<Function>::getList(Module *M) {
56 return M->getFunctionList();
57}
58iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
59 return M->getGlobalList();
60}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000061iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) {
62 return M->getAliasList();
63}
Chris Lattner113f4f42002-06-25 16:13:24 +000064
65// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000066// are not in the public header file.
Chris Lattnerb47aa542007-04-17 03:26:42 +000067template class SymbolTableListTraits<GlobalVariable, Module>;
68template class SymbolTableListTraits<Function, Module>;
Anton Korobeynikova97b6942007-04-25 14:27:10 +000069template class SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000070
Chris Lattner09bd1a02003-12-31 08:43:01 +000071//===----------------------------------------------------------------------===//
72// Primitive Module methods.
73//
Chris Lattner446ad502001-10-13 06:58:40 +000074
Chris Lattnerc3f6e002003-04-22 18:02:04 +000075Module::Module(const std::string &MID)
Owen Anderson9eb1a262006-05-18 02:10:31 +000076 : ModuleID(MID), DataLayout("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000077 ValSymTab = new ValueSymbolTable();
Reid Spencer32af9e82007-01-06 07:24:44 +000078 TypeSymTab = new TypeSymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000079}
80
81Module::~Module() {
82 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000083 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000084 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000085 AliasList.clear();
Reid Spencera0b05b32004-07-25 18:08:57 +000086 LibraryList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000087 delete ValSymTab;
88 delete TypeSymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000089}
90
Owen Anderson9eb1a262006-05-18 02:10:31 +000091/// Target endian information...
92Module::Endianness Module::getEndianness() const {
93 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000094 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000095
Owen Anderson08aecf52006-05-18 05:46:08 +000096 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +000097 std::string token = getToken(temp, "-");
98
99 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +0000100 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000101 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +0000102 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000103 }
104 }
105
Owen Anderson08aecf52006-05-18 05:46:08 +0000106 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000107}
108
Owen Anderson9eb1a262006-05-18 02:10:31 +0000109/// Target Pointer Size information...
110Module::PointerSize Module::getPointerSize() const {
111 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000112 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113
Owen Anderson08aecf52006-05-18 05:46:08 +0000114 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +0000115 std::string token = getToken(temp, "-");
116 char signal = getToken(token, ":")[0];
117
118 if (signal == 'p') {
119 int size = atoi(getToken(token, ":").c_str());
120 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000121 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000122 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000123 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000124 }
125 }
126
Owen Anderson08aecf52006-05-18 05:46:08 +0000127 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000128}
129
Chris Lattner09bd1a02003-12-31 08:43:01 +0000130//===----------------------------------------------------------------------===//
131// Methods for easy access to the functions in the module.
132//
133
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000134// getOrInsertFunction - Look up the specified function in the module symbol
135// table. If it does not exist, add a prototype for the function and return
136// it. This is nice because it allows most passes to get away with not handling
137// the symbol table directly for this common task.
138//
Chris Lattner505c06b2007-01-07 08:09:25 +0000139Constant *Module::getOrInsertFunction(const std::string &Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000140 const FunctionType *Ty,
141 AttrListPtr AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000142 ValueSymbolTable &SymTab = getValueSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +0000143
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000144 // See if we have a definition for the specified function already.
Reid Spencer1241d6d2007-02-05 21:19:13 +0000145 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name));
Chris Lattner505c06b2007-01-07 08:09:25 +0000146 if (F == 0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000147 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000148 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000149 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
150 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000151 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000152 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000153 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000154
155 // Okay, the function exists. Does it have externally visible linkage?
Rafael Espindola6de96a12009-01-15 20:18:42 +0000156 if (F->hasLocalLinkage()) {
Chris Lattner42e983e2008-06-27 21:25:24 +0000157 // Clear the function's name.
158 F->setName("");
Chris Lattner505c06b2007-01-07 08:09:25 +0000159 // Retry, now there won't be a conflict.
Chris Lattner42e983e2008-06-27 21:25:24 +0000160 Constant *NewF = getOrInsertFunction(Name, Ty);
161 F->setName(&Name[0], Name.size());
162 return NewF;
Chris Lattner505c06b2007-01-07 08:09:25 +0000163 }
164
165 // If the function exists but has the wrong type, return a bitcast to the
166 // right type.
Christopher Lambedf07882007-12-17 01:12:55 +0000167 if (F->getType() != PointerType::getUnqual(Ty))
168 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Chris Lattner505c06b2007-01-07 08:09:25 +0000169
170 // Otherwise, we just found the existing function or a prototype.
171 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000172}
173
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000174Constant *Module::getOrInsertFunction(const std::string &Name,
175 const FunctionType *Ty) {
176 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
177 return getOrInsertFunction(Name, Ty, AttributeList);
178}
179
Chris Lattnerbd717d82003-08-31 00:19:28 +0000180// getOrInsertFunction - Look up the specified function in the module symbol
181// table. If it does not exist, add a prototype for the function and return it.
182// This version of the method takes a null terminated list of function
183// arguments, which makes it easier for clients to use.
184//
Chris Lattner505c06b2007-01-07 08:09:25 +0000185Constant *Module::getOrInsertFunction(const std::string &Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000186 AttrListPtr AttributeList,
187 const Type *RetTy, ...) {
188 va_list Args;
189 va_start(Args, RetTy);
190
191 // Build the list of argument types...
192 std::vector<const Type*> ArgTys;
193 while (const Type *ArgTy = va_arg(Args, const Type*))
194 ArgTys.push_back(ArgTy);
195
196 va_end(Args);
197
198 // Build the function type and chain to the other getOrInsertFunction...
199 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
200 AttributeList);
201}
202
203Constant *Module::getOrInsertFunction(const std::string &Name,
Chris Lattnerbd717d82003-08-31 00:19:28 +0000204 const Type *RetTy, ...) {
205 va_list Args;
206 va_start(Args, RetTy);
207
208 // Build the list of argument types...
209 std::vector<const Type*> ArgTys;
210 while (const Type *ArgTy = va_arg(Args, const Type*))
211 ArgTys.push_back(ArgTy);
212
213 va_end(Args);
214
215 // Build the function type and chain to the other getOrInsertFunction...
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000216 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
217 AttrListPtr::get((AttributeWithIndex *)0, 0));
Chris Lattnerbd717d82003-08-31 00:19:28 +0000218}
219
Chris Lattnera483b062002-03-29 03:44:18 +0000220// getFunction - Look up the specified function in the module symbol table.
221// If it does not exist, return null.
222//
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000223Function *Module::getFunction(const std::string &Name) const {
224 const ValueSymbolTable &SymTab = getValueSymbolTable();
225 return dyn_cast_or_null<Function>(SymTab.lookup(Name));
Chris Lattner1f985e02002-11-08 20:34:02 +0000226}
227
Chris Lattnere43649f2008-06-27 21:09:10 +0000228Function *Module::getFunction(const char *Name) const {
229 const ValueSymbolTable &SymTab = getValueSymbolTable();
230 return dyn_cast_or_null<Function>(SymTab.lookup(Name, Name+strlen(Name)));
231}
232
Chris Lattner09bd1a02003-12-31 08:43:01 +0000233//===----------------------------------------------------------------------===//
234// Methods for easy access to the global variables in the module.
235//
236
237/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000238/// symbol table. If it does not exist, return null. The type argument
239/// should be the underlying type of the global, i.e., it should not have
240/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000241/// If AllowLocal is set to true, this function will return types that
242/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000243///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000244GlobalVariable *Module::getGlobalVariable(const std::string &Name,
Rafael Espindola6de96a12009-01-15 20:18:42 +0000245 bool AllowLocal) const {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000246 if (Value *V = ValSymTab->lookup(Name)) {
247 GlobalVariable *Result = dyn_cast<GlobalVariable>(V);
Rafael Espindola6de96a12009-01-15 20:18:42 +0000248 if (Result && (AllowLocal || !Result->hasLocalLinkage()))
Chris Lattner09bd1a02003-12-31 08:43:01 +0000249 return Result;
250 }
251 return 0;
252}
253
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000254/// getOrInsertGlobal - Look up the specified global in the module symbol table.
255/// 1. If it does not exist, add a declaration of the global and return it.
256/// 2. Else, the global exists but has the wrong type: return the function
257/// with a constantexpr cast to the right type.
258/// 3. Finally, if the existing global is the correct delclaration, return the
259/// existing global.
Bill Wendling2f409562008-11-04 22:51:24 +0000260Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) {
261 ValueSymbolTable &SymTab = getValueSymbolTable();
262
263 // See if we have a definition for the specified global already.
264 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(SymTab.lookup(Name));
265 if (GV == 0) {
266 // Nope, add it
267 GlobalVariable *New =
268 new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name);
269 GlobalList.push_back(New);
270 return New; // Return the new declaration.
271 }
272
273 // If the variable exists but has the wrong type, return a bitcast to the
274 // right type.
275 if (GV->getType() != PointerType::getUnqual(Ty))
276 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
277
278 // Otherwise, we just found the existing function or a prototype.
279 return GV;
280}
281
Chris Lattner09bd1a02003-12-31 08:43:01 +0000282//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000283// Methods for easy access to the global variables in the module.
284//
285
286// getNamedAlias - Look up the specified global in the module symbol table.
287// If it does not exist, return null.
288//
289GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
290 const ValueSymbolTable &SymTab = getValueSymbolTable();
291 return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
292}
293
294//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000295// Methods for easy access to the types in the module.
296//
297
Chris Lattner1f985e02002-11-08 20:34:02 +0000298
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000299// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
300// there is already an entry for this name, true is returned and the symbol
301// table is not modified.
302//
303bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000304 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000305
Reid Spencer32af9e82007-01-06 07:24:44 +0000306 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000307
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000308 // Not in symbol table? Set the name with the Symtab as an argument so the
309 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000310 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000311
312 return false;
313}
314
315/// getTypeByName - Return the type with the specified name in this module, or
316/// null if there is none by that name.
317const Type *Module::getTypeByName(const std::string &Name) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000318 const TypeSymbolTable &ST = getTypeSymbolTable();
319 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000320}
Chris Lattner1f985e02002-11-08 20:34:02 +0000321
Chris Lattner10b7cb52002-04-13 18:58:33 +0000322// getTypeName - If there is at least one entry in the symbol table for the
323// specified type, return it.
324//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000325std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000326 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000327
Reid Spencer32af9e82007-01-06 07:24:44 +0000328 TypeSymbolTable::const_iterator TI = ST.begin();
329 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencerabb6f002004-05-25 08:52:20 +0000330 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000331
Reid Spencerabb6f002004-05-25 08:52:20 +0000332 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000333 ++TI;
334
335 if (TI != TE) // Must have found an entry!
336 return TI->first;
337 return ""; // Must not have found anything...
338}
339
Chris Lattner09bd1a02003-12-31 08:43:01 +0000340//===----------------------------------------------------------------------===//
341// Other module related stuff.
342//
343
344
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000345// dropAllReferences() - This function causes all the subelementss to "let go"
346// of all references that they are maintaining. This allows one to 'delete' a
347// whole module at a time, even though there may be circular references... first
348// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000349// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000350// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000351//
352void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000353 for(Module::iterator I = begin(), E = end(); I != E; ++I)
354 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000355
Chris Lattner531f9e92005-03-15 04:54:21 +0000356 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000357 I->dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000358
359 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
360 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361}
Chris Lattner31cf9842001-06-30 04:35:40 +0000362
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000363void Module::addLibrary(const std::string& Lib) {
364 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
365 if (*I == Lib)
366 return;
367 LibraryList.push_back(Lib);
368}
369
370void Module::removeLibrary(const std::string& Lib) {
371 LibraryListType::iterator I = LibraryList.begin();
372 LibraryListType::iterator E = LibraryList.end();
373 for (;I != E; ++I)
374 if (*I == Lib) {
375 LibraryList.erase(I);
376 return;
377 }
378}