blob: f481a0d8c88f333a6778d21723d7bcc2d7ae7fa6 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include "Support/STLExtras.h"
Chris Lattner184b2982002-09-08 18:59:35 +000019#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
21#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000022#include <cstdarg>
Chris Lattner446ad502001-10-13 06:58:40 +000023#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner09bd1a02003-12-31 08:43:01 +000026//===----------------------------------------------------------------------===//
27// Stuff to implement the globals and functions lists.
28//
29
Chris Lattner113f4f42002-06-25 16:13:24 +000030Function *ilist_traits<Function>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000031 FunctionType *FTy =
32 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner379a8d22003-04-16 20:28:45 +000033 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000034 // This should not be garbage monitored.
35 LeakDetector::removeGarbageObject(Ret);
36 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000037}
38GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
Chris Lattner379a8d22003-04-16 20:28:45 +000039 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
40 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000041 // This should not be garbage monitored.
42 LeakDetector::removeGarbageObject(Ret);
43 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000044}
45
46iplist<Function> &ilist_traits<Function>::getList(Module *M) {
47 return M->getFunctionList();
48}
49iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
50 return M->getGlobalList();
51}
52
53// Explicit instantiations of SymbolTableListTraits since some of the methods
54// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000055template class SymbolTableListTraits<GlobalVariable, Module, Module>;
56template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000057
Chris Lattner446ad502001-10-13 06:58:40 +000058// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
59// have Module.h depend on <map>
60//
Chris Lattner189d19f2003-11-21 20:23:48 +000061namespace llvm {
62 struct GlobalValueRefMap {
63 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
64 typedef MapTy::iterator iterator;
65 std::map<GlobalValue*, ConstantPointerRef*> Map;
66 };
67}
Chris Lattner446ad502001-10-13 06:58:40 +000068
Chris Lattner09bd1a02003-12-31 08:43:01 +000069//===----------------------------------------------------------------------===//
70// Primitive Module methods.
71//
Chris Lattner446ad502001-10-13 06:58:40 +000072
Chris Lattnerc3f6e002003-04-22 18:02:04 +000073Module::Module(const std::string &MID)
Chris Lattner8068e0c2003-08-24 13:48:48 +000074 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
Chris Lattner113f4f42002-06-25 16:13:24 +000075 FunctionList.setItemParent(this);
76 FunctionList.setParent(this);
77 GlobalList.setItemParent(this);
78 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000079 GVRefMap = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +000080 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000081}
82
83Module::~Module() {
84 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000085 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000086 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000087 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000088 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000089 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000090}
91
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000092// Module::dump() - Allow printing from debugger
93void Module::dump() const {
94 print(std::cerr);
95}
96
Chris Lattner09bd1a02003-12-31 08:43:01 +000097//===----------------------------------------------------------------------===//
98// Methods for easy access to the functions in the module.
99//
100
Chris Lattnera483b062002-03-29 03:44:18 +0000101// getOrInsertFunction - Look up the specified function in the module symbol
102// table. If it does not exist, add a prototype for the function and return
103// it. This is nice because it allows most passes to get away with not handling
104// the symbol table directly for this common task.
105//
106Function *Module::getOrInsertFunction(const std::string &Name,
107 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000108 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +0000109
110 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000111 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000112 return cast<Function>(V); // Yup, got it
113 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000114 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000115 FunctionList.push_back(New);
116 return New; // Return the new prototype...
117 }
118}
119
Chris Lattnerbd717d82003-08-31 00:19:28 +0000120// getOrInsertFunction - Look up the specified function in the module symbol
121// table. If it does not exist, add a prototype for the function and return it.
122// This version of the method takes a null terminated list of function
123// arguments, which makes it easier for clients to use.
124//
125Function *Module::getOrInsertFunction(const std::string &Name,
126 const Type *RetTy, ...) {
127 va_list Args;
128 va_start(Args, RetTy);
129
130 // Build the list of argument types...
131 std::vector<const Type*> ArgTys;
132 while (const Type *ArgTy = va_arg(Args, const Type*))
133 ArgTys.push_back(ArgTy);
134
135 va_end(Args);
136
137 // Build the function type and chain to the other getOrInsertFunction...
138 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
139}
140
141
Chris Lattnera483b062002-03-29 03:44:18 +0000142// getFunction - Look up the specified function in the module symbol table.
143// If it does not exist, return null.
144//
145Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000146 SymbolTable &SymTab = getSymbolTable();
147 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000148}
149
150
Chris Lattner1f985e02002-11-08 20:34:02 +0000151/// getMainFunction - This function looks up main efficiently. This is such a
152/// common case, that it is a method in Module. If main cannot be found, a
153/// null pointer is returned.
154///
155Function *Module::getMainFunction() {
156 std::vector<const Type*> Params;
157
158 // int main(void)...
159 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
160 Params, false)))
161 return F;
162
163 // void main(void)...
164 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
165 Params, false)))
166 return F;
167
168 Params.push_back(Type::IntTy);
169
170 // int main(int argc)...
171 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
172 Params, false)))
173 return F;
174
175 // void main(int argc)...
176 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
177 Params, false)))
178 return F;
179
180 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
181 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
182
183 // int main(int argc, char **argv)...
184 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
185 Params, false)))
186 return F;
187
188 // void main(int argc, char **argv)...
189 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
190 Params, false)))
191 return F;
192 }
193
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000194 // Ok, try to find main the hard way...
195 return getNamedFunction("main");
196}
197
198/// getNamedFunction - Return the first function in the module with the
199/// specified name, of arbitrary type. This method returns null if a function
200/// with the specified name is not found.
201///
202Function *Module::getNamedFunction(const std::string &Name) {
203 // Loop over all of the functions, looking for the function desired
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000204 Function *Found = 0;
Chris Lattner1f985e02002-11-08 20:34:02 +0000205 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000206 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000207 if (I->isExternal())
208 Found = I;
209 else
210 return I;
211 return Found; // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000212}
213
Chris Lattner09bd1a02003-12-31 08:43:01 +0000214//===----------------------------------------------------------------------===//
215// Methods for easy access to the global variables in the module.
216//
217
218/// getGlobalVariable - Look up the specified global variable in the module
219/// symbol table. If it does not exist, return null. Note that this only
220/// returns a global variable if it does not have internal linkage. The type
221/// argument should be the underlying type of the global, ie, it should not
222/// have the top-level PointerType, which represents the address of the
223/// global.
224///
225GlobalVariable *Module::getGlobalVariable(const std::string &Name,
226 const Type *Ty) {
227 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
228 GlobalVariable *Result = cast<GlobalVariable>(V);
229 if (!Result->hasInternalLinkage())
230 return Result;
231 }
232 return 0;
233}
234
235
236
237//===----------------------------------------------------------------------===//
238// Methods for easy access to the types in the module.
239//
240
Chris Lattner1f985e02002-11-08 20:34:02 +0000241
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000242// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
243// there is already an entry for this name, true is returned and the symbol
244// table is not modified.
245//
246bool Module::addTypeName(const std::string &Name, const Type *Ty) {
247 SymbolTable &ST = getSymbolTable();
248
249 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
250
251 // Not in symbol table? Set the name with the Symtab as an argument so the
252 // type knows what to update...
253 ((Value*)Ty)->setName(Name, &ST);
254
255 return false;
256}
257
258/// getTypeByName - Return the type with the specified name in this module, or
259/// null if there is none by that name.
260const Type *Module::getTypeByName(const std::string &Name) const {
261 const SymbolTable &ST = getSymbolTable();
262 return cast_or_null<Type>(ST.lookup(Type::TypeTy, Name));
263}
Chris Lattner1f985e02002-11-08 20:34:02 +0000264
Chris Lattner10b7cb52002-04-13 18:58:33 +0000265// getTypeName - If there is at least one entry in the symbol table for the
266// specified type, return it.
267//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000268std::string Module::getTypeName(const Type *Ty) const {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000269 const SymbolTable &ST = getSymbolTable();
270 if (ST.find(Type::TypeTy) == ST.end())
Chris Lattner10b7cb52002-04-13 18:58:33 +0000271 return ""; // No names for types...
272
Chris Lattner98cf1f52002-11-20 18:36:02 +0000273 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
274 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
Chris Lattner10b7cb52002-04-13 18:58:33 +0000275
276 while (TI != TE && TI->second != (const Value*)Ty)
277 ++TI;
278
279 if (TI != TE) // Must have found an entry!
280 return TI->first;
281 return ""; // Must not have found anything...
282}
283
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284
Chris Lattner09bd1a02003-12-31 08:43:01 +0000285//===----------------------------------------------------------------------===//
286// Other module related stuff.
287//
288
289
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000290// dropAllReferences() - This function causes all the subelementss to "let go"
291// of all references that they are maintaining. This allows one to 'delete' a
292// whole module at a time, even though there may be circular references... first
293// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000294// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000295// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296//
297void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000298 for(Module::iterator I = begin(), E = end(); I != E; ++I)
299 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000300
Chris Lattner113f4f42002-06-25 16:13:24 +0000301 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
302 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000303
304 // If there are any GlobalVariable references still out there, nuke them now.
305 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000306 // them still. Note that destroying all of the constant pointer refs will
307 // eventually cause the GVRefMap field to be set to null (by
308 // destroyConstantPointerRef, below).
309 //
310 while (GVRefMap)
311 // Delete the ConstantPointerRef node...
312 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313}
Chris Lattner31cf9842001-06-30 04:35:40 +0000314
Chris Lattner446ad502001-10-13 06:58:40 +0000315// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000316ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000317 // Create ref map lazily on demand...
318 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
319
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000320 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
321 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000322
Chris Lattner3462ae32001-12-03 22:26:30 +0000323 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000324 GVRefMap->Map[V] = Ref;
Chris Lattner446ad502001-10-13 06:58:40 +0000325 return Ref;
326}
327
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000328void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
329 assert(GVRefMap && "No map allocated, but we have a CPR?");
330 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
331 assert(0 && "ConstantPointerRef not found in module CPR map!");
332
333 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
334 delete GVRefMap;
335 GVRefMap = 0;
336 }
337}
338
Chris Lattner3462ae32001-12-03 22:26:30 +0000339void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000340 assert(OldGV != NewGV && "Cannot mutate to the same global!");
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000341 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
342 assert(I != GVRefMap->Map.end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000343 "mutateConstantPointerRef; OldGV not in table!");
344 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000345
346 // Remove the old entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000347 GVRefMap->Map.erase(I);
Chris Lattner446ad502001-10-13 06:58:40 +0000348
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000349 // Check to see if a CPR already exists for NewGV
350 I = GVRefMap->Map.lower_bound(NewGV);
351
352 if (I == GVRefMap->Map.end() || I->first != NewGV) {
353 // Insert the new entry...
354 GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
355 } else {
356 // Otherwise, an entry already exists for the current global value.
357 // Completely replace the old CPR with the existing one...
358 Ref->replaceAllUsesWith(I->second);
359 delete Ref;
360 }
Chris Lattner446ad502001-10-13 06:58:40 +0000361}
Brian Gaeke960707c2003-11-11 22:41:34 +0000362