Split ConstantVals.h into Constant.h and Constants.h
[oota-llvm.git] / lib / VMCore / Module.cpp
1 //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
2 //
3 // This file implements the Module class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Module.h"
8 #include "llvm/Function.h"
9 #include "llvm/GlobalVariable.h"
10 #include "llvm/InstrTypes.h"
11 #include "llvm/Type.h"
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "Support/STLExtras.h"
15 #include "ValueHolderImpl.h"
16 #include <map>
17
18 // Instantiate Templates - This ugliness is the price we have to pay
19 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
20 //
21 template class ValueHolder<GlobalVariable, Module, Module>;
22 template class ValueHolder<Function, Module, Module>;
23
24 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
25 // have Module.h depend on <map>
26 //
27 struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
28 };
29
30
31 Module::Module() : GlobalList(this, this), FunctionList(this, this) {
32   GVRefMap = 0;
33   SymTab = 0;
34 }
35
36 Module::~Module() {
37   dropAllReferences();
38   GlobalList.delete_all();
39   GlobalList.setParent(0);
40   FunctionList.delete_all();
41   FunctionList.setParent(0);
42   delete SymTab;
43 }
44
45 SymbolTable *Module::getSymbolTableSure() {
46   if (!SymTab) SymTab = new SymbolTable(0);
47   return SymTab;
48 }
49
50 // hasSymbolTable() - Returns true if there is a symbol table allocated to
51 // this object AND if there is at least one name in it!
52 //
53 bool Module::hasSymbolTable() const {
54   if (!SymTab) return false;
55
56   for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
57        I != E; ++I)
58     if (I->second.begin() != I->second.end())
59       return true;                                // Found nonempty type plane!
60   
61   return false;
62 }
63
64
65 // getOrInsertFunction - Look up the specified function in the module symbol
66 // table.  If it does not exist, add a prototype for the function and return
67 // it.  This is nice because it allows most passes to get away with not handling
68 // the symbol table directly for this common task.
69 //
70 Function *Module::getOrInsertFunction(const std::string &Name,
71                                       const FunctionType *Ty) {
72   SymbolTable *SymTab = getSymbolTableSure();
73
74   // See if we have a definitions for the specified function already...
75   if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
76     return cast<Function>(V);      // Yup, got it
77   } else {                         // Nope, add one
78     Function *New = new Function(Ty, false, Name);
79     FunctionList.push_back(New);
80     return New;                    // Return the new prototype...
81   }
82 }
83
84 // getFunction - Look up the specified function in the module symbol table.
85 // If it does not exist, return null.
86 //
87 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
88   SymbolTable *SymTab = getSymbolTable();
89   if (SymTab == 0) return 0;  // No symtab, no symbols...
90
91   return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
92 }
93
94 // addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
95 // there is already an entry for this name, true is returned and the symbol
96 // table is not modified.
97 //
98 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
99   SymbolTable *ST = getSymbolTableSure();
100
101   if (ST->lookup(Type::TypeTy, Name)) return true;  // Already in symtab...
102   
103   // Not in symbol table?  Set the name with the Symtab as an argument so the
104   // type knows what to update...
105   ((Value*)Ty)->setName(Name, ST);
106
107   return false;
108 }
109
110 // getTypeName - If there is at least one entry in the symbol table for the
111 // specified type, return it.
112 //
113 std::string Module::getTypeName(const Type *Ty) {
114   const SymbolTable *ST = getSymbolTable();
115   if (ST == 0) return "";  // No symbol table, must not have an entry...
116   if (ST->find(Type::TypeTy) == ST->end())
117     return ""; // No names for types...
118
119   SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
120   SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
121
122   while (TI != TE && TI->second != (const Value*)Ty)
123     ++TI;
124
125   if (TI != TE)  // Must have found an entry!
126     return TI->first;
127   return "";     // Must not have found anything...
128 }
129
130
131 // dropAllReferences() - This function causes all the subinstructions to "let
132 // go" of all references that they are maintaining.  This allows one to
133 // 'delete' a whole class at a time, even though there may be circular
134 // references... first all references are dropped, and all use counts go to
135 // zero.  Then everything is delete'd for real.  Note that no operations are
136 // valid on an object that has "dropped all references", except operator 
137 // delete.
138 //
139 void Module::dropAllReferences() {
140   for_each(FunctionList.begin(), FunctionList.end(),
141            std::mem_fun(&Function::dropAllReferences));
142
143   for_each(GlobalList.begin(), GlobalList.end(),
144            std::mem_fun(&GlobalVariable::dropAllReferences));
145
146   // If there are any GlobalVariable references still out there, nuke them now.
147   // Since all references are hereby dropped, nothing could possibly reference
148   // them still.
149   if (GVRefMap) {
150     for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
151          I != E; ++I) {
152       // Delete the ConstantPointerRef node...
153       I->second->destroyConstant();
154     }
155
156     // Since the table is empty, we can now delete it...
157     delete GVRefMap;
158   }
159 }
160
161 // Accessor for the underlying GlobalValRefMap...
162 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
163   // Create ref map lazily on demand...
164   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
165
166   GlobalValueRefMap::iterator I = GVRefMap->find(V);
167   if (I != GVRefMap->end()) return I->second;
168
169   ConstantPointerRef *Ref = new ConstantPointerRef(V);
170   GVRefMap->insert(std::make_pair(V, Ref));
171
172   return Ref;
173 }
174
175 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
176   GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
177   assert(I != GVRefMap->end() && 
178          "mutateConstantPointerRef; OldGV not in table!");
179   ConstantPointerRef *Ref = I->second;
180
181   // Remove the old entry...
182   GVRefMap->erase(I);
183
184   // Insert the new entry...
185   GVRefMap->insert(std::make_pair(NewGV, Ref));
186 }