blob: abc4654da1d1b4760fe0e2d112ce9fddfb741bc4 [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 Lattnerc3f6e002003-04-22 18:02:04 +000055Module::Module(const std::string &MID)
56 : ModuleID(MID), Endian(BigEndian), PtrSize(Pointer64) {
Chris Lattner113f4f42002-06-25 16:13:24 +000057 FunctionList.setItemParent(this);
58 FunctionList.setParent(this);
59 GlobalList.setItemParent(this);
60 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000061 GVRefMap = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +000062 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000063}
64
65Module::~Module() {
66 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000067 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000068 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000069 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000070 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000071 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000072}
73
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000074// Module::dump() - Allow printing from debugger
75void Module::dump() const {
76 print(std::cerr);
77}
78
Chris Lattnera483b062002-03-29 03:44:18 +000079// getOrInsertFunction - Look up the specified function in the module symbol
80// table. If it does not exist, add a prototype for the function and return
81// it. This is nice because it allows most passes to get away with not handling
82// the symbol table directly for this common task.
83//
84Function *Module::getOrInsertFunction(const std::string &Name,
85 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +000086 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +000087
88 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +000089 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +000090 return cast<Function>(V); // Yup, got it
91 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +000092 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +000093 FunctionList.push_back(New);
94 return New; // Return the new prototype...
95 }
96}
97
98// getFunction - Look up the specified function in the module symbol table.
99// If it does not exist, return null.
100//
101Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000102 SymbolTable &SymTab = getSymbolTable();
103 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000104}
105
Chris Lattner13ae72f2002-03-29 04:48:40 +0000106// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
107// there is already an entry for this name, true is returned and the symbol
108// table is not modified.
109//
110bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000111 SymbolTable &ST = getSymbolTable();
Chris Lattner13ae72f2002-03-29 04:48:40 +0000112
Chris Lattner98cf1f52002-11-20 18:36:02 +0000113 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
Chris Lattner13ae72f2002-03-29 04:48:40 +0000114
115 // Not in symbol table? Set the name with the Symtab as an argument so the
116 // type knows what to update...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000117 ((Value*)Ty)->setName(Name, &ST);
Chris Lattner13ae72f2002-03-29 04:48:40 +0000118
119 return false;
120}
Chris Lattnera483b062002-03-29 03:44:18 +0000121
Chris Lattner1f985e02002-11-08 20:34:02 +0000122/// getMainFunction - This function looks up main efficiently. This is such a
123/// common case, that it is a method in Module. If main cannot be found, a
124/// null pointer is returned.
125///
126Function *Module::getMainFunction() {
127 std::vector<const Type*> Params;
128
129 // int main(void)...
130 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
131 Params, false)))
132 return F;
133
134 // void main(void)...
135 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
136 Params, false)))
137 return F;
138
139 Params.push_back(Type::IntTy);
140
141 // int main(int argc)...
142 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
143 Params, false)))
144 return F;
145
146 // void main(int argc)...
147 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
148 Params, false)))
149 return F;
150
151 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
152 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
153
154 // int main(int argc, char **argv)...
155 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
156 Params, false)))
157 return F;
158
159 // void main(int argc, char **argv)...
160 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
161 Params, false)))
162 return F;
163 }
164
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000165 // Ok, try to find main the hard way...
166 return getNamedFunction("main");
167}
168
169/// getNamedFunction - Return the first function in the module with the
170/// specified name, of arbitrary type. This method returns null if a function
171/// with the specified name is not found.
172///
173Function *Module::getNamedFunction(const std::string &Name) {
174 // Loop over all of the functions, looking for the function desired
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000175 Function *Found = 0;
Chris Lattner1f985e02002-11-08 20:34:02 +0000176 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000177 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000178 if (I->isExternal())
179 Found = I;
180 else
181 return I;
182 return Found; // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000183}
184
185
186
Chris Lattner10b7cb52002-04-13 18:58:33 +0000187// getTypeName - If there is at least one entry in the symbol table for the
188// specified type, return it.
189//
190std::string Module::getTypeName(const Type *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000191 const SymbolTable &ST = getSymbolTable();
192 if (ST.find(Type::TypeTy) == ST.end())
Chris Lattner10b7cb52002-04-13 18:58:33 +0000193 return ""; // No names for types...
194
Chris Lattner98cf1f52002-11-20 18:36:02 +0000195 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
196 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
Chris Lattner10b7cb52002-04-13 18:58:33 +0000197
198 while (TI != TE && TI->second != (const Value*)Ty)
199 ++TI;
200
201 if (TI != TE) // Must have found an entry!
202 return TI->first;
203 return ""; // Must not have found anything...
204}
205
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000207// dropAllReferences() - This function causes all the subelementss to "let go"
208// of all references that they are maintaining. This allows one to 'delete' a
209// whole module at a time, even though there may be circular references... first
210// all references are dropped, and all use counts go to zero. Then everything
211// is delete'd for real. Note that no operations are valid on an object that
212// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213//
214void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000215 for(Module::iterator I = begin(), E = end(); I != E; ++I)
216 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000217
Chris Lattner113f4f42002-06-25 16:13:24 +0000218 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
219 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000220
221 // If there are any GlobalVariable references still out there, nuke them now.
222 // Since all references are hereby dropped, nothing could possibly reference
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000223 // them still. Note that destroying all of the constant pointer refs will
224 // eventually cause the GVRefMap field to be set to null (by
225 // destroyConstantPointerRef, below).
226 //
227 while (GVRefMap)
228 // Delete the ConstantPointerRef node...
229 GVRefMap->Map.begin()->second->destroyConstant();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
Chris Lattner31cf9842001-06-30 04:35:40 +0000231
Chris Lattner446ad502001-10-13 06:58:40 +0000232// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000234 // Create ref map lazily on demand...
235 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
236
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000237 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
238 if (I != GVRefMap->Map.end()) return I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000241 GVRefMap->Map[V] = Ref;
Chris Lattner446ad502001-10-13 06:58:40 +0000242 return Ref;
243}
244
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000245void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
246 assert(GVRefMap && "No map allocated, but we have a CPR?");
247 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
248 assert(0 && "ConstantPointerRef not found in module CPR map!");
249
250 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
251 delete GVRefMap;
252 GVRefMap = 0;
253 }
254}
255
Chris Lattner3462ae32001-12-03 22:26:30 +0000256void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000257 assert(OldGV != NewGV && "Cannot mutate to the same global!");
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000258 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
259 assert(I != GVRefMap->Map.end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000260 "mutateConstantPointerRef; OldGV not in table!");
261 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000262
263 // Remove the old entry...
Chris Lattner7d9a14d2002-08-12 20:23:29 +0000264 GVRefMap->Map.erase(I);
Chris Lattner446ad502001-10-13 06:58:40 +0000265
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000266 // Check to see if a CPR already exists for NewGV
267 I = GVRefMap->Map.lower_bound(NewGV);
268
269 if (I == GVRefMap->Map.end() || I->first != NewGV) {
270 // Insert the new entry...
271 GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
272 } else {
273 // Otherwise, an entry already exists for the current global value.
274 // Completely replace the old CPR with the existing one...
275 Ref->replaceAllUsesWith(I->second);
276 delete Ref;
277 }
Chris Lattner446ad502001-10-13 06:58:40 +0000278}