blob: b7fe5693fb239f996ed614145f204cccc71ce074 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
2//
3// This file implements the Module class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner00950542001-06-06 20:29:01 +00007#include "llvm/Module.h"
Chris Lattner28bf86a2001-06-30 04:35:40 +00008#include "llvm/InstrTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Chris Lattner6056c492002-03-29 03:44:18 +000010#include "llvm/DerivedTypes.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000011#include "Support/STLExtras.h"
Chris Lattnerd1e693f2002-09-08 18:59:35 +000012#include "Support/LeakDetector.h"
Chris Lattner7e708292002-06-25 16:13:24 +000013#include "SymbolTableListTraitsImpl.h"
14#include <algorithm>
Chris Lattner0ae8e872003-08-31 00:19:28 +000015#include <cstdarg>
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +000016#include <map>
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner7e708292002-06-25 16:13:24 +000018Function *ilist_traits<Function>::createNode() {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000019 FunctionType *FTy =
20 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner4ad02e72003-04-16 20:28:45 +000021 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattnerd1e693f2002-09-08 18:59:35 +000022 // This should not be garbage monitored.
23 LeakDetector::removeGarbageObject(Ret);
24 return Ret;
Chris Lattner7e708292002-06-25 16:13:24 +000025}
26GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
Chris Lattner4ad02e72003-04-16 20:28:45 +000027 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
28 GlobalValue::ExternalLinkage);
Chris Lattnerd1e693f2002-09-08 18:59:35 +000029 // This should not be garbage monitored.
30 LeakDetector::removeGarbageObject(Ret);
31 return Ret;
Chris Lattner7e708292002-06-25 16:13:24 +000032}
33
34iplist<Function> &ilist_traits<Function>::getList(Module *M) {
35 return M->getFunctionList();
36}
37iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
38 return M->getGlobalList();
39}
40
41// Explicit instantiations of SymbolTableListTraits since some of the methods
42// are not in the public header file...
43template SymbolTableListTraits<GlobalVariable, Module, Module>;
44template SymbolTableListTraits<Function, Module, Module>;
Chris Lattner00950542001-06-06 20:29:01 +000045
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +000046// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
47// have Module.h depend on <map>
48//
Chris Lattner4228b5a2002-08-12 20:23:29 +000049struct GlobalValueRefMap {
50 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
51 typedef MapTy::iterator iterator;
52 std::map<GlobalValue*, ConstantPointerRef*> Map;
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +000053};
54
55
Chris Lattner678c6a02003-04-22 18:02:04 +000056Module::Module(const std::string &MID)
Chris Lattnereb5d3a12003-08-24 13:48:48 +000057 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
Chris Lattner7e708292002-06-25 16:13:24 +000058 FunctionList.setItemParent(this);
59 FunctionList.setParent(this);
60 GlobalList.setItemParent(this);
61 GlobalList.setParent(this);
Chris Lattner11aa4772002-04-28 04:51:51 +000062 GVRefMap = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +000063 SymTab = new SymbolTable();
Chris Lattner00950542001-06-06 20:29:01 +000064}
65
66Module::~Module() {
67 dropAllReferences();
Chris Lattner7e708292002-06-25 16:13:24 +000068 GlobalList.clear();
Chris Lattner70cc3392001-09-10 07:58:01 +000069 GlobalList.setParent(0);
Chris Lattner7e708292002-06-25 16:13:24 +000070 FunctionList.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +000071 FunctionList.setParent(0);
Chris Lattner11aa4772002-04-28 04:51:51 +000072 delete SymTab;
Chris Lattner00950542001-06-06 20:29:01 +000073}
74
Chris Lattner9da07152002-08-17 23:32:47 +000075// Module::dump() - Allow printing from debugger
76void Module::dump() const {
77 print(std::cerr);
78}
79
Chris Lattner6056c492002-03-29 03:44:18 +000080// getOrInsertFunction - Look up the specified function in the module symbol
81// table. If it does not exist, add a prototype for the function and return
82// it. This is nice because it allows most passes to get away with not handling
83// the symbol table directly for this common task.
84//
85Function *Module::getOrInsertFunction(const std::string &Name,
86 const FunctionType *Ty) {
Chris Lattner6e6026b2002-11-20 18:36:02 +000087 SymbolTable &SymTab = getSymbolTable();
Chris Lattner6056c492002-03-29 03:44:18 +000088
89 // See if we have a definitions for the specified function already...
Chris Lattner6e6026b2002-11-20 18:36:02 +000090 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattner6056c492002-03-29 03:44:18 +000091 return cast<Function>(V); // Yup, got it
92 } else { // Nope, add one
Chris Lattner4ad02e72003-04-16 20:28:45 +000093 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattner6056c492002-03-29 03:44:18 +000094 FunctionList.push_back(New);
95 return New; // Return the new prototype...
96 }
97}
98
Chris Lattner0ae8e872003-08-31 00:19:28 +000099// getOrInsertFunction - Look up the specified function in the module symbol
100// table. If it does not exist, add a prototype for the function and return it.
101// This version of the method takes a null terminated list of function
102// arguments, which makes it easier for clients to use.
103//
104Function *Module::getOrInsertFunction(const std::string &Name,
105 const Type *RetTy, ...) {
106 va_list Args;
107 va_start(Args, RetTy);
108
109 // Build the list of argument types...
110 std::vector<const Type*> ArgTys;
111 while (const Type *ArgTy = va_arg(Args, const Type*))
112 ArgTys.push_back(ArgTy);
113
114 va_end(Args);
115
116 // Build the function type and chain to the other getOrInsertFunction...
117 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
118}
119
120
121
Chris Lattner6056c492002-03-29 03:44:18 +0000122// getFunction - Look up the specified function in the module symbol table.
123// If it does not exist, return null.
124//
125Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000126 SymbolTable &SymTab = getSymbolTable();
127 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattner6056c492002-03-29 03:44:18 +0000128}
129
Chris Lattner68e5ed82002-03-29 04:48:40 +0000130// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
131// there is already an entry for this name, true is returned and the symbol
132// table is not modified.
133//
134bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000135 SymbolTable &ST = getSymbolTable();
Chris Lattner68e5ed82002-03-29 04:48:40 +0000136
Chris Lattner6e6026b2002-11-20 18:36:02 +0000137 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
Chris Lattner68e5ed82002-03-29 04:48:40 +0000138
139 // Not in symbol table? Set the name with the Symtab as an argument so the
140 // type knows what to update...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000141 ((Value*)Ty)->setName(Name, &ST);
Chris Lattner68e5ed82002-03-29 04:48:40 +0000142
143 return false;
144}
Chris Lattner6056c492002-03-29 03:44:18 +0000145
Chris Lattnera9a93f32002-11-08 20:34:02 +0000146/// getMainFunction - This function looks up main efficiently. This is such a
147/// common case, that it is a method in Module. If main cannot be found, a
148/// null pointer is returned.
149///
150Function *Module::getMainFunction() {
151 std::vector<const Type*> Params;
152
153 // int main(void)...
154 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
155 Params, false)))
156 return F;
157
158 // void main(void)...
159 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
160 Params, false)))
161 return F;
162
163 Params.push_back(Type::IntTy);
164
165 // int main(int argc)...
166 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
167 Params, false)))
168 return F;
169
170 // void main(int argc)...
171 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
172 Params, false)))
173 return F;
174
175 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
176 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
177
178 // int main(int argc, char **argv)...
179 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
180 Params, false)))
181 return F;
182
183 // void main(int argc, char **argv)...
184 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
185 Params, false)))
186 return F;
187 }
188
Chris Lattner9a641b42002-11-19 18:41:44 +0000189 // Ok, try to find main the hard way...
190 return getNamedFunction("main");
191}
192
193/// getNamedFunction - Return the first function in the module with the
194/// specified name, of arbitrary type. This method returns null if a function
195/// with the specified name is not found.
196///
197Function *Module::getNamedFunction(const std::string &Name) {
198 // Loop over all of the functions, looking for the function desired
Chris Lattnerb6ede8a2003-07-23 20:21:30 +0000199 Function *Found = 0;
Chris Lattnera9a93f32002-11-08 20:34:02 +0000200 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner9a641b42002-11-19 18:41:44 +0000201 if (I->getName() == Name)
Chris Lattnerb6ede8a2003-07-23 20:21:30 +0000202 if (I->isExternal())
203 Found = I;
204 else
205 return I;
206 return Found; // Non-external function not found...
Chris Lattnera9a93f32002-11-08 20:34:02 +0000207}
208
209
210
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000211// getTypeName - If there is at least one entry in the symbol table for the
212// specified type, return it.
213//
214std::string Module::getTypeName(const Type *Ty) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000215 const SymbolTable &ST = getSymbolTable();
216 if (ST.find(Type::TypeTy) == ST.end())
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000217 return ""; // No names for types...
218
Chris Lattner6e6026b2002-11-20 18:36:02 +0000219 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
220 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000221
222 while (TI != TE && TI->second != (const Value*)Ty)
223 ++TI;
224
225 if (TI != TE) // Must have found an entry!
226 return TI->first;
227 return ""; // Must not have found anything...
228}
229
Chris Lattner00950542001-06-06 20:29:01 +0000230
Chris Lattner9da07152002-08-17 23:32:47 +0000231// dropAllReferences() - This function causes all the subelementss to "let go"
232// of all references that they are maintaining. This allows one to 'delete' a
233// whole module at a time, even though there may be circular references... first
234// all references are dropped, and all use counts go to zero. Then everything
235// is delete'd for real. Note that no operations are valid on an object that
236// has "dropped all references", except operator delete.
Chris Lattner00950542001-06-06 20:29:01 +0000237//
238void Module::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000239 for(Module::iterator I = begin(), E = end(); I != E; ++I)
240 I->dropAllReferences();
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000241
Chris Lattner7e708292002-06-25 16:13:24 +0000242 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
243 I->dropAllReferences();
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000244
245 // If there are any GlobalVariable references still out there, nuke them now.
246 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner41661fd2002-08-18 00:40:04 +0000247 // them still. Note that destroying all of the constant pointer refs will
248 // eventually cause the GVRefMap field to be set to null (by
249 // destroyConstantPointerRef, below).
250 //
251 while (GVRefMap)
252 // Delete the ConstantPointerRef node...
253 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner00950542001-06-06 20:29:01 +0000254}
Chris Lattner28bf86a2001-06-30 04:35:40 +0000255
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000256// Accessor for the underlying GlobalValRefMap...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000257ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000258 // Create ref map lazily on demand...
259 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
260
Chris Lattner4228b5a2002-08-12 20:23:29 +0000261 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
262 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000263
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000264 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner41661fd2002-08-18 00:40:04 +0000265 GVRefMap->Map[V] = Ref;
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000266 return Ref;
267}
268
Chris Lattner41661fd2002-08-18 00:40:04 +0000269void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
270 assert(GVRefMap && "No map allocated, but we have a CPR?");
271 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
272 assert(0 && "ConstantPointerRef not found in module CPR map!");
273
274 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
275 delete GVRefMap;
276 GVRefMap = 0;
277 }
278}
279
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000280void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattner608f4b02003-05-15 19:37:21 +0000281 assert(OldGV != NewGV && "Cannot mutate to the same global!");
Chris Lattner4228b5a2002-08-12 20:23:29 +0000282 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
283 assert(I != GVRefMap->Map.end() &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000284 "mutateConstantPointerRef; OldGV not in table!");
285 ConstantPointerRef *Ref = I->second;
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000286
287 // Remove the old entry...
Chris Lattner4228b5a2002-08-12 20:23:29 +0000288 GVRefMap->Map.erase(I);
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000289
Chris Lattner608f4b02003-05-15 19:37:21 +0000290 // Check to see if a CPR already exists for NewGV
291 I = GVRefMap->Map.lower_bound(NewGV);
292
293 if (I == GVRefMap->Map.end() || I->first != NewGV) {
294 // Insert the new entry...
295 GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
296 } else {
297 // Otherwise, an entry already exists for the current global value.
298 // Completely replace the old CPR with the existing one...
299 Ref->replaceAllUsesWith(I->second);
300 delete Ref;
301 }
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000302}