blob: 83e0a6e1ff5a2e6efe956c7713b8569861130b0d [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 Lattner113f4f42002-06-25 16:13:24 +000026Function *ilist_traits<Function>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000027 FunctionType *FTy =
28 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner379a8d22003-04-16 20:28:45 +000029 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000030 // This should not be garbage monitored.
31 LeakDetector::removeGarbageObject(Ret);
32 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000033}
34GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
Chris Lattner379a8d22003-04-16 20:28:45 +000035 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
36 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000037 // This should not be garbage monitored.
38 LeakDetector::removeGarbageObject(Ret);
39 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000040}
41
42iplist<Function> &ilist_traits<Function>::getList(Module *M) {
43 return M->getFunctionList();
44}
45iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
46 return M->getGlobalList();
47}
48
49// Explicit instantiations of SymbolTableListTraits since some of the methods
50// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000051template class SymbolTableListTraits<GlobalVariable, Module, Module>;
52template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Chris Lattner446ad502001-10-13 06:58:40 +000054// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
55// have Module.h depend on <map>
56//
Chris Lattner189d19f2003-11-21 20:23:48 +000057namespace llvm {
58 struct GlobalValueRefMap {
59 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
60 typedef MapTy::iterator iterator;
61 std::map<GlobalValue*, ConstantPointerRef*> Map;
62 };
63}
Chris Lattner446ad502001-10-13 06:58:40 +000064
65
Chris Lattnerc3f6e002003-04-22 18:02:04 +000066Module::Module(const std::string &MID)
Chris Lattner8068e0c2003-08-24 13:48:48 +000067 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
Chris Lattner113f4f42002-06-25 16:13:24 +000068 FunctionList.setItemParent(this);
69 FunctionList.setParent(this);
70 GlobalList.setItemParent(this);
71 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000072 GVRefMap = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +000073 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000074}
75
76Module::~Module() {
77 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000078 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000079 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000080 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000081 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000082 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000083}
84
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000085// Module::dump() - Allow printing from debugger
86void Module::dump() const {
87 print(std::cerr);
88}
89
Chris Lattnera483b062002-03-29 03:44:18 +000090// getOrInsertFunction - Look up the specified function in the module symbol
91// table. If it does not exist, add a prototype for the function and return
92// it. This is nice because it allows most passes to get away with not handling
93// the symbol table directly for this common task.
94//
95Function *Module::getOrInsertFunction(const std::string &Name,
96 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +000097 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +000098
99 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000100 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000101 return cast<Function>(V); // Yup, got it
102 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000103 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000104 FunctionList.push_back(New);
105 return New; // Return the new prototype...
106 }
107}
108
Chris Lattnerbd717d82003-08-31 00:19:28 +0000109// getOrInsertFunction - Look up the specified function in the module symbol
110// table. If it does not exist, add a prototype for the function and return it.
111// This version of the method takes a null terminated list of function
112// arguments, which makes it easier for clients to use.
113//
114Function *Module::getOrInsertFunction(const std::string &Name,
115 const Type *RetTy, ...) {
116 va_list Args;
117 va_start(Args, RetTy);
118
119 // Build the list of argument types...
120 std::vector<const Type*> ArgTys;
121 while (const Type *ArgTy = va_arg(Args, const Type*))
122 ArgTys.push_back(ArgTy);
123
124 va_end(Args);
125
126 // Build the function type and chain to the other getOrInsertFunction...
127 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
128}
129
130
131
Chris Lattnera483b062002-03-29 03:44:18 +0000132// getFunction - Look up the specified function in the module symbol table.
133// If it does not exist, return null.
134//
135Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000136 SymbolTable &SymTab = getSymbolTable();
137 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000138}
139
140
Chris Lattner1f985e02002-11-08 20:34:02 +0000141/// getMainFunction - This function looks up main efficiently. This is such a
142/// common case, that it is a method in Module. If main cannot be found, a
143/// null pointer is returned.
144///
145Function *Module::getMainFunction() {
146 std::vector<const Type*> Params;
147
148 // int main(void)...
149 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
150 Params, false)))
151 return F;
152
153 // void main(void)...
154 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
155 Params, false)))
156 return F;
157
158 Params.push_back(Type::IntTy);
159
160 // int main(int argc)...
161 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
162 Params, false)))
163 return F;
164
165 // void main(int argc)...
166 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
167 Params, false)))
168 return F;
169
170 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
171 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
172
173 // int main(int argc, char **argv)...
174 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
175 Params, false)))
176 return F;
177
178 // void main(int argc, char **argv)...
179 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
180 Params, false)))
181 return F;
182 }
183
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000184 // Ok, try to find main the hard way...
185 return getNamedFunction("main");
186}
187
188/// getNamedFunction - Return the first function in the module with the
189/// specified name, of arbitrary type. This method returns null if a function
190/// with the specified name is not found.
191///
192Function *Module::getNamedFunction(const std::string &Name) {
193 // Loop over all of the functions, looking for the function desired
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000194 Function *Found = 0;
Chris Lattner1f985e02002-11-08 20:34:02 +0000195 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000196 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000197 if (I->isExternal())
198 Found = I;
199 else
200 return I;
201 return Found; // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000202}
203
204
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000205// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
206// there is already an entry for this name, true is returned and the symbol
207// table is not modified.
208//
209bool Module::addTypeName(const std::string &Name, const Type *Ty) {
210 SymbolTable &ST = getSymbolTable();
211
212 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
213
214 // Not in symbol table? Set the name with the Symtab as an argument so the
215 // type knows what to update...
216 ((Value*)Ty)->setName(Name, &ST);
217
218 return false;
219}
220
221/// getTypeByName - Return the type with the specified name in this module, or
222/// null if there is none by that name.
223const Type *Module::getTypeByName(const std::string &Name) const {
224 const SymbolTable &ST = getSymbolTable();
225 return cast_or_null<Type>(ST.lookup(Type::TypeTy, Name));
226}
Chris Lattner1f985e02002-11-08 20:34:02 +0000227
Chris Lattner10b7cb52002-04-13 18:58:33 +0000228// getTypeName - If there is at least one entry in the symbol table for the
229// specified type, return it.
230//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000231std::string Module::getTypeName(const Type *Ty) const {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000232 const SymbolTable &ST = getSymbolTable();
233 if (ST.find(Type::TypeTy) == ST.end())
Chris Lattner10b7cb52002-04-13 18:58:33 +0000234 return ""; // No names for types...
235
Chris Lattner98cf1f52002-11-20 18:36:02 +0000236 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
237 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
Chris Lattner10b7cb52002-04-13 18:58:33 +0000238
239 while (TI != TE && TI->second != (const Value*)Ty)
240 ++TI;
241
242 if (TI != TE) // Must have found an entry!
243 return TI->first;
244 return ""; // Must not have found anything...
245}
246
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000248// dropAllReferences() - This function causes all the subelementss to "let go"
249// of all references that they are maintaining. This allows one to 'delete' a
250// whole module at a time, even though there may be circular references... first
251// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000252// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000253// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254//
255void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000256 for(Module::iterator I = begin(), E = end(); I != E; ++I)
257 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000258
Chris Lattner113f4f42002-06-25 16:13:24 +0000259 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
260 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000261
262 // If there are any GlobalVariable references still out there, nuke them now.
263 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000264 // them still. Note that destroying all of the constant pointer refs will
265 // eventually cause the GVRefMap field to be set to null (by
266 // destroyConstantPointerRef, below).
267 //
268 while (GVRefMap)
269 // Delete the ConstantPointerRef node...
270 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271}
Chris Lattner31cf9842001-06-30 04:35:40 +0000272
Chris Lattner446ad502001-10-13 06:58:40 +0000273// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000274ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000275 // Create ref map lazily on demand...
276 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
277
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000278 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
279 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000280
Chris Lattner3462ae32001-12-03 22:26:30 +0000281 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000282 GVRefMap->Map[V] = Ref;
Chris Lattner446ad502001-10-13 06:58:40 +0000283 return Ref;
284}
285
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000286void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
287 assert(GVRefMap && "No map allocated, but we have a CPR?");
288 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
289 assert(0 && "ConstantPointerRef not found in module CPR map!");
290
291 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
292 delete GVRefMap;
293 GVRefMap = 0;
294 }
295}
296
Chris Lattner3462ae32001-12-03 22:26:30 +0000297void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000298 assert(OldGV != NewGV && "Cannot mutate to the same global!");
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000299 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
300 assert(I != GVRefMap->Map.end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000301 "mutateConstantPointerRef; OldGV not in table!");
302 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000303
304 // Remove the old entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000305 GVRefMap->Map.erase(I);
Chris Lattner446ad502001-10-13 06:58:40 +0000306
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000307 // Check to see if a CPR already exists for NewGV
308 I = GVRefMap->Map.lower_bound(NewGV);
309
310 if (I == GVRefMap->Map.end() || I->first != NewGV) {
311 // Insert the new entry...
312 GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
313 } else {
314 // Otherwise, an entry already exists for the current global value.
315 // Completely replace the old CPR with the existing one...
316 Ref->replaceAllUsesWith(I->second);
317 delete Ref;
318 }
Chris Lattner446ad502001-10-13 06:58:40 +0000319}
Brian Gaeke960707c2003-11-11 22:41:34 +0000320