blob: fec6fec77d376428d6c23d10b85675bb8b3d2af9 [file] [log] [blame]
Chris Lattner2f7c9632001-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 Lattner2f7c9632001-06-06 20:29:01 +00007#include "llvm/Module.h"
Chris Lattner31cf9842001-06-30 04:35:40 +00008#include "llvm/InstrTypes.h"
Chris Lattnerca142372002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Chris Lattnera483b062002-03-29 03:44:18 +000010#include "llvm/DerivedTypes.h"
Chris Lattner5de22042001-11-27 00:03:19 +000011#include "Support/STLExtras.h"
Chris Lattner184b2982002-09-08 18:59:35 +000012#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000013#include "SymbolTableListTraitsImpl.h"
14#include <algorithm>
Chris Lattner446ad502001-10-13 06:58:40 +000015#include <map>
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
Chris Lattner113f4f42002-06-25 16:13:24 +000017Function *ilist_traits<Function>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000018 FunctionType *FTy =
19 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
20 Function *Ret = new Function(FTy, false);
21 // This should not be garbage monitored.
22 LeakDetector::removeGarbageObject(Ret);
23 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000024}
25GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000026 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
29 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000030}
31
32iplist<Function> &ilist_traits<Function>::getList(Module *M) {
33 return M->getFunctionList();
34}
35iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
36 return M->getGlobalList();
37}
38
39// Explicit instantiations of SymbolTableListTraits since some of the methods
40// are not in the public header file...
41template SymbolTableListTraits<GlobalVariable, Module, Module>;
42template SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000043
Chris Lattner446ad502001-10-13 06:58:40 +000044// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
45// have Module.h depend on <map>
46//
Chris Lattner7d9a14d2002-08-12 20:23:29 +000047struct GlobalValueRefMap {
48 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
49 typedef MapTy::iterator iterator;
50 std::map<GlobalValue*, ConstantPointerRef*> Map;
Chris Lattner446ad502001-10-13 06:58:40 +000051};
52
53
Chris Lattner113f4f42002-06-25 16:13:24 +000054Module::Module() {
55 FunctionList.setItemParent(this);
56 FunctionList.setParent(this);
57 GlobalList.setItemParent(this);
58 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000059 GVRefMap = 0;
60 SymTab = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000061}
62
63Module::~Module() {
64 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000065 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000066 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000067 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000068 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000069 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000070}
71
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000072// Module::dump() - Allow printing from debugger
73void Module::dump() const {
74 print(std::cerr);
75}
76
Chris Lattner2c8ff632002-04-28 04:51:51 +000077SymbolTable *Module::getSymbolTableSure() {
Chris Lattner33100ed2002-10-15 21:26:29 +000078 if (!SymTab) SymTab = new SymbolTable();
Chris Lattner2c8ff632002-04-28 04:51:51 +000079 return SymTab;
80}
81
82// hasSymbolTable() - Returns true if there is a symbol table allocated to
83// this object AND if there is at least one name in it!
84//
85bool Module::hasSymbolTable() const {
86 if (!SymTab) return false;
87
88 for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
89 I != E; ++I)
90 if (I->second.begin() != I->second.end())
91 return true; // Found nonempty type plane!
92
93 return false;
94}
95
96
Chris Lattnera483b062002-03-29 03:44:18 +000097// getOrInsertFunction - Look up the specified function in the module symbol
98// table. If it does not exist, add a prototype for the function and return
99// it. This is nice because it allows most passes to get away with not handling
100// the symbol table directly for this common task.
101//
102Function *Module::getOrInsertFunction(const std::string &Name,
103 const FunctionType *Ty) {
104 SymbolTable *SymTab = getSymbolTableSure();
105
106 // See if we have a definitions for the specified function already...
107 if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
108 return cast<Function>(V); // Yup, got it
109 } else { // Nope, add one
110 Function *New = new Function(Ty, false, Name);
111 FunctionList.push_back(New);
112 return New; // Return the new prototype...
113 }
114}
115
116// getFunction - Look up the specified function in the module symbol table.
117// If it does not exist, return null.
118//
119Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
120 SymbolTable *SymTab = getSymbolTable();
121 if (SymTab == 0) return 0; // No symtab, no symbols...
122
123 return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
124}
125
Chris Lattner13ae72f2002-03-29 04:48:40 +0000126// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
127// there is already an entry for this name, true is returned and the symbol
128// table is not modified.
129//
130bool Module::addTypeName(const std::string &Name, const Type *Ty) {
131 SymbolTable *ST = getSymbolTableSure();
132
133 if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab...
134
135 // Not in symbol table? Set the name with the Symtab as an argument so the
136 // type knows what to update...
137 ((Value*)Ty)->setName(Name, ST);
138
139 return false;
140}
Chris Lattnera483b062002-03-29 03:44:18 +0000141
Chris Lattner1f985e02002-11-08 20:34:02 +0000142/// getMainFunction - This function looks up main efficiently. This is such a
143/// common case, that it is a method in Module. If main cannot be found, a
144/// null pointer is returned.
145///
146Function *Module::getMainFunction() {
147 std::vector<const Type*> Params;
148
149 // int main(void)...
150 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
151 Params, false)))
152 return F;
153
154 // void main(void)...
155 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
156 Params, false)))
157 return F;
158
159 Params.push_back(Type::IntTy);
160
161 // int main(int argc)...
162 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
163 Params, false)))
164 return F;
165
166 // void main(int argc)...
167 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
168 Params, false)))
169 return F;
170
171 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
172 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
173
174 // int main(int argc, char **argv)...
175 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
176 Params, false)))
177 return F;
178
179 // void main(int argc, char **argv)...
180 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
181 Params, false)))
182 return F;
183 }
184
185 // Loop over all of the methods, trying to find main the hard way...
186 for (iterator I = begin(), E = end(); I != E; ++I)
187 if (I->getName() == "main")
188 return I;
189 return 0; // Main not found...
190}
191
192
193
Chris Lattner10b7cb52002-04-13 18:58:33 +0000194// getTypeName - If there is at least one entry in the symbol table for the
195// specified type, return it.
196//
197std::string Module::getTypeName(const Type *Ty) {
198 const SymbolTable *ST = getSymbolTable();
199 if (ST == 0) return ""; // No symbol table, must not have an entry...
200 if (ST->find(Type::TypeTy) == ST->end())
201 return ""; // No names for types...
202
203 SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
204 SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
205
206 while (TI != TE && TI->second != (const Value*)Ty)
207 ++TI;
208
209 if (TI != TE) // Must have found an entry!
210 return TI->first;
211 return ""; // Must not have found anything...
212}
213
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000215// dropAllReferences() - This function causes all the subelementss to "let go"
216// of all references that they are maintaining. This allows one to 'delete' a
217// whole module at a time, even though there may be circular references... first
218// all references are dropped, and all use counts go to zero. Then everything
219// is delete'd for real. Note that no operations are valid on an object that
220// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221//
222void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000223 for(Module::iterator I = begin(), E = end(); I != E; ++I)
224 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000225
Chris Lattner113f4f42002-06-25 16:13:24 +0000226 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
227 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000228
229 // If there are any GlobalVariable references still out there, nuke them now.
230 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000231 // them still. Note that destroying all of the constant pointer refs will
232 // eventually cause the GVRefMap field to be set to null (by
233 // destroyConstantPointerRef, below).
234 //
235 while (GVRefMap)
236 // Delete the ConstantPointerRef node...
237 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
Chris Lattner31cf9842001-06-30 04:35:40 +0000239
Chris Lattner446ad502001-10-13 06:58:40 +0000240// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000241ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000242 // Create ref map lazily on demand...
243 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
244
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000245 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
246 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000247
Chris Lattner3462ae32001-12-03 22:26:30 +0000248 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000249 GVRefMap->Map[V] = Ref;
Chris Lattner446ad502001-10-13 06:58:40 +0000250 return Ref;
251}
252
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000253void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
254 assert(GVRefMap && "No map allocated, but we have a CPR?");
255 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
256 assert(0 && "ConstantPointerRef not found in module CPR map!");
257
258 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
259 delete GVRefMap;
260 GVRefMap = 0;
261 }
262}
263
Chris Lattner3462ae32001-12-03 22:26:30 +0000264void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000265 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
266 assert(I != GVRefMap->Map.end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000267 "mutateConstantPointerRef; OldGV not in table!");
268 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000269
270 // Remove the old entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000271 GVRefMap->Map.erase(I);
Chris Lattner446ad502001-10-13 06:58:40 +0000272
273 // Insert the new entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000274 GVRefMap->Map.insert(std::make_pair(NewGV, Ref));
Chris Lattner446ad502001-10-13 06:58:40 +0000275}