blob: b8cffdb56f5abeebc73f127ed8d9efb8850151a5 [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 Lattner57698e22002-03-26 18:01:55 +00008#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +00009#include "llvm/GlobalVariable.h"
Chris Lattner31cf9842001-06-30 04:35:40 +000010#include "llvm/InstrTypes.h"
Chris Lattnerca142372002-04-28 19:55:58 +000011#include "llvm/Constants.h"
Chris Lattnera483b062002-03-29 03:44:18 +000012#include "llvm/DerivedTypes.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/STLExtras.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000014#include "SymbolTableListTraitsImpl.h"
15#include <algorithm>
Chris Lattner446ad502001-10-13 06:58:40 +000016#include <map>
Chris Lattner2f7c9632001-06-06 20:29:01 +000017
Chris Lattner113f4f42002-06-25 16:13:24 +000018Function *ilist_traits<Function>::createNode() {
19 return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
20 false), false);
21}
22GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
23 return new GlobalVariable(Type::IntTy, false, false);
24}
25
26iplist<Function> &ilist_traits<Function>::getList(Module *M) {
27 return M->getFunctionList();
28}
29iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
30 return M->getGlobalList();
31}
32
33// Explicit instantiations of SymbolTableListTraits since some of the methods
34// are not in the public header file...
35template SymbolTableListTraits<GlobalVariable, Module, Module>;
36template SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000037
Chris Lattner446ad502001-10-13 06:58:40 +000038// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
39// have Module.h depend on <map>
40//
Chris Lattner7f74a562002-01-20 22:54:45 +000041struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
Chris Lattner446ad502001-10-13 06:58:40 +000042};
43
44
Chris Lattner113f4f42002-06-25 16:13:24 +000045Module::Module() {
46 FunctionList.setItemParent(this);
47 FunctionList.setParent(this);
48 GlobalList.setItemParent(this);
49 GlobalList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000050 GVRefMap = 0;
51 SymTab = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000052}
53
54Module::~Module() {
55 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000056 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000057 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000058 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000059 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000060 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000061}
62
Chris Lattner2c8ff632002-04-28 04:51:51 +000063SymbolTable *Module::getSymbolTableSure() {
64 if (!SymTab) SymTab = new SymbolTable(0);
65 return SymTab;
66}
67
68// hasSymbolTable() - Returns true if there is a symbol table allocated to
69// this object AND if there is at least one name in it!
70//
71bool Module::hasSymbolTable() const {
72 if (!SymTab) return false;
73
74 for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
75 I != E; ++I)
76 if (I->second.begin() != I->second.end())
77 return true; // Found nonempty type plane!
78
79 return false;
80}
81
82
Chris Lattnera483b062002-03-29 03:44:18 +000083// getOrInsertFunction - Look up the specified function in the module symbol
84// table. If it does not exist, add a prototype for the function and return
85// it. This is nice because it allows most passes to get away with not handling
86// the symbol table directly for this common task.
87//
88Function *Module::getOrInsertFunction(const std::string &Name,
89 const FunctionType *Ty) {
90 SymbolTable *SymTab = getSymbolTableSure();
91
92 // See if we have a definitions for the specified function already...
93 if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
94 return cast<Function>(V); // Yup, got it
95 } else { // Nope, add one
96 Function *New = new Function(Ty, false, Name);
97 FunctionList.push_back(New);
98 return New; // Return the new prototype...
99 }
100}
101
102// getFunction - Look up the specified function in the module symbol table.
103// If it does not exist, return null.
104//
105Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
106 SymbolTable *SymTab = getSymbolTable();
107 if (SymTab == 0) return 0; // No symtab, no symbols...
108
109 return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
110}
111
Chris Lattner13ae72f2002-03-29 04:48:40 +0000112// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
113// there is already an entry for this name, true is returned and the symbol
114// table is not modified.
115//
116bool Module::addTypeName(const std::string &Name, const Type *Ty) {
117 SymbolTable *ST = getSymbolTableSure();
118
119 if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab...
120
121 // Not in symbol table? Set the name with the Symtab as an argument so the
122 // type knows what to update...
123 ((Value*)Ty)->setName(Name, ST);
124
125 return false;
126}
Chris Lattnera483b062002-03-29 03:44:18 +0000127
Chris Lattner10b7cb52002-04-13 18:58:33 +0000128// getTypeName - If there is at least one entry in the symbol table for the
129// specified type, return it.
130//
131std::string Module::getTypeName(const Type *Ty) {
132 const SymbolTable *ST = getSymbolTable();
133 if (ST == 0) return ""; // No symbol table, must not have an entry...
134 if (ST->find(Type::TypeTy) == ST->end())
135 return ""; // No names for types...
136
137 SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
138 SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
139
140 while (TI != TE && TI->second != (const Value*)Ty)
141 ++TI;
142
143 if (TI != TE) // Must have found an entry!
144 return TI->first;
145 return ""; // Must not have found anything...
146}
147
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148
149// dropAllReferences() - This function causes all the subinstructions to "let
150// go" of all references that they are maintaining. This allows one to
151// 'delete' a whole class at a time, even though there may be circular
152// references... first all references are dropped, and all use counts go to
153// zero. Then everything is delete'd for real. Note that no operations are
154// valid on an object that has "dropped all references", except operator
155// delete.
156//
157void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000158 for(Module::iterator I = begin(), E = end(); I != E; ++I)
159 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000160
Chris Lattner113f4f42002-06-25 16:13:24 +0000161 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
162 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000163
164 // If there are any GlobalVariable references still out there, nuke them now.
165 // Since all references are hereby dropped, nothing could possibly reference
166 // them still.
167 if (GVRefMap) {
168 for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
169 I != E; ++I) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000170 // Delete the ConstantPointerRef node...
Chris Lattner446ad502001-10-13 06:58:40 +0000171 I->second->destroyConstant();
172 }
173
174 // Since the table is empty, we can now delete it...
175 delete GVRefMap;
176 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177}
Chris Lattner31cf9842001-06-30 04:35:40 +0000178
Chris Lattner446ad502001-10-13 06:58:40 +0000179// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +0000180ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +0000181 // Create ref map lazily on demand...
182 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
183
184 GlobalValueRefMap::iterator I = GVRefMap->find(V);
185 if (I != GVRefMap->end()) return I->second;
186
Chris Lattner3462ae32001-12-03 22:26:30 +0000187 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner7f74a562002-01-20 22:54:45 +0000188 GVRefMap->insert(std::make_pair(V, Ref));
Chris Lattner446ad502001-10-13 06:58:40 +0000189
190 return Ref;
191}
192
Chris Lattner3462ae32001-12-03 22:26:30 +0000193void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattner446ad502001-10-13 06:58:40 +0000194 GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
195 assert(I != GVRefMap->end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000196 "mutateConstantPointerRef; OldGV not in table!");
197 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000198
199 // Remove the old entry...
200 GVRefMap->erase(I);
201
202 // Insert the new entry...
Chris Lattner7f74a562002-01-20 22:54:45 +0000203 GVRefMap->insert(std::make_pair(NewGV, Ref));
Chris Lattner446ad502001-10-13 06:58:40 +0000204}