blob: e0a6fb271ceabbe5d95959cc7d7b8c1ab49cb5a0 [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);
Chris Lattner379a8d22003-04-16 20:28:45 +000020 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000021 // 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 Lattner379a8d22003-04-16 20:28:45 +000026 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
27 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000028 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
30 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000031}
32
33iplist<Function> &ilist_traits<Function>::getList(Module *M) {
34 return M->getFunctionList();
35}
36iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
37 return M->getGlobalList();
38}
39
40// Explicit instantiations of SymbolTableListTraits since some of the methods
41// are not in the public header file...
42template SymbolTableListTraits<GlobalVariable, Module, Module>;
43template SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
Chris Lattner446ad502001-10-13 06:58:40 +000045// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
46// have Module.h depend on <map>
47//
Chris Lattner7d9a14d2002-08-12 20:23:29 +000048struct GlobalValueRefMap {
49 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
50 typedef MapTy::iterator iterator;
51 std::map<GlobalValue*, ConstantPointerRef*> Map;
Chris Lattner446ad502001-10-13 06:58:40 +000052};
53
54
Chris Lattner113f4f42002-06-25 16:13:24 +000055Module::Module() {
56 FunctionList.setItemParent(this);
57 FunctionList.setParent(this);
58 GlobalList.setItemParent(this);
59 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000060 GVRefMap = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +000061 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000062}
63
64Module::~Module() {
65 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000066 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000067 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000068 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000069 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000070 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000071}
72
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000073// Module::dump() - Allow printing from debugger
74void Module::dump() const {
75 print(std::cerr);
76}
77
Chris Lattnera483b062002-03-29 03:44:18 +000078// getOrInsertFunction - Look up the specified function in the module symbol
79// table. If it does not exist, add a prototype for the function and return
80// it. This is nice because it allows most passes to get away with not handling
81// the symbol table directly for this common task.
82//
83Function *Module::getOrInsertFunction(const std::string &Name,
84 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +000085 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +000086
87 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +000088 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +000089 return cast<Function>(V); // Yup, got it
90 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +000091 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +000092 FunctionList.push_back(New);
93 return New; // Return the new prototype...
94 }
95}
96
97// getFunction - Look up the specified function in the module symbol table.
98// If it does not exist, return null.
99//
100Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000101 SymbolTable &SymTab = getSymbolTable();
102 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000103}
104
Chris Lattner13ae72f2002-03-29 04:48:40 +0000105// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
106// there is already an entry for this name, true is returned and the symbol
107// table is not modified.
108//
109bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000110 SymbolTable &ST = getSymbolTable();
Chris Lattner13ae72f2002-03-29 04:48:40 +0000111
Chris Lattner98cf1f52002-11-20 18:36:02 +0000112 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
Chris Lattner13ae72f2002-03-29 04:48:40 +0000113
114 // Not in symbol table? Set the name with the Symtab as an argument so the
115 // type knows what to update...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000116 ((Value*)Ty)->setName(Name, &ST);
Chris Lattner13ae72f2002-03-29 04:48:40 +0000117
118 return false;
119}
Chris Lattnera483b062002-03-29 03:44:18 +0000120
Chris Lattner1f985e02002-11-08 20:34:02 +0000121/// getMainFunction - This function looks up main efficiently. This is such a
122/// common case, that it is a method in Module. If main cannot be found, a
123/// null pointer is returned.
124///
125Function *Module::getMainFunction() {
126 std::vector<const Type*> Params;
127
128 // int main(void)...
129 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
130 Params, false)))
131 return F;
132
133 // void main(void)...
134 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
135 Params, false)))
136 return F;
137
138 Params.push_back(Type::IntTy);
139
140 // int main(int argc)...
141 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
142 Params, false)))
143 return F;
144
145 // void main(int argc)...
146 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
147 Params, false)))
148 return F;
149
150 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
151 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
152
153 // int main(int argc, char **argv)...
154 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
155 Params, false)))
156 return F;
157
158 // void main(int argc, char **argv)...
159 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
160 Params, false)))
161 return F;
162 }
163
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000164 // Ok, try to find main the hard way...
165 return getNamedFunction("main");
166}
167
168/// getNamedFunction - Return the first function in the module with the
169/// specified name, of arbitrary type. This method returns null if a function
170/// with the specified name is not found.
171///
172Function *Module::getNamedFunction(const std::string &Name) {
173 // Loop over all of the functions, looking for the function desired
Chris Lattner1f985e02002-11-08 20:34:02 +0000174 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000175 if (I->getName() == Name)
Chris Lattner1f985e02002-11-08 20:34:02 +0000176 return I;
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000177 return 0; // function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000178}
179
180
181
Chris Lattner10b7cb52002-04-13 18:58:33 +0000182// getTypeName - If there is at least one entry in the symbol table for the
183// specified type, return it.
184//
185std::string Module::getTypeName(const Type *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000186 const SymbolTable &ST = getSymbolTable();
187 if (ST.find(Type::TypeTy) == ST.end())
Chris Lattner10b7cb52002-04-13 18:58:33 +0000188 return ""; // No names for types...
189
Chris Lattner98cf1f52002-11-20 18:36:02 +0000190 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
191 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
Chris Lattner10b7cb52002-04-13 18:58:33 +0000192
193 while (TI != TE && TI->second != (const Value*)Ty)
194 ++TI;
195
196 if (TI != TE) // Must have found an entry!
197 return TI->first;
198 return ""; // Must not have found anything...
199}
200
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000202// dropAllReferences() - This function causes all the subelementss to "let go"
203// of all references that they are maintaining. This allows one to 'delete' a
204// whole module at a time, even though there may be circular references... first
205// all references are dropped, and all use counts go to zero. Then everything
206// is delete'd for real. Note that no operations are valid on an object that
207// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//
209void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000210 for(Module::iterator I = begin(), E = end(); I != E; ++I)
211 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000212
Chris Lattner113f4f42002-06-25 16:13:24 +0000213 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
214 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000215
216 // If there are any GlobalVariable references still out there, nuke them now.
217 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000218 // them still. Note that destroying all of the constant pointer refs will
219 // eventually cause the GVRefMap field to be set to null (by
220 // destroyConstantPointerRef, below).
221 //
222 while (GVRefMap)
223 // Delete the ConstantPointerRef node...
224 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225}
Chris Lattner31cf9842001-06-30 04:35:40 +0000226
Chris Lattner446ad502001-10-13 06:58:40 +0000227// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000228ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000229 // Create ref map lazily on demand...
230 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
231
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000232 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
233 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000236 GVRefMap->Map[V] = Ref;
Chris Lattner446ad502001-10-13 06:58:40 +0000237 return Ref;
238}
239
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000240void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
241 assert(GVRefMap && "No map allocated, but we have a CPR?");
242 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
243 assert(0 && "ConstantPointerRef not found in module CPR map!");
244
245 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
246 delete GVRefMap;
247 GVRefMap = 0;
248 }
249}
250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000252 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
253 assert(I != GVRefMap->Map.end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000254 "mutateConstantPointerRef; OldGV not in table!");
255 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000256
257 // Remove the old entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000258 GVRefMap->Map.erase(I);
Chris Lattner446ad502001-10-13 06:58:40 +0000259
260 // Insert the new entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000261 GVRefMap->Map.insert(std::make_pair(NewGV, Ref));
Chris Lattner446ad502001-10-13 06:58:40 +0000262}