Remove the reduceApply functions they are obsolete things from the days before
[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/ValueHolderImpl.h"
12 #include "llvm/Type.h"
13 #include "llvm/ConstantVals.h"
14 #include "Support/STLExtras.h"
15 #include <map>
16
17 // Instantiate Templates - This ugliness is the price we have to pay
18 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
19 //
20 template class ValueHolder<GlobalVariable, Module, Module>;
21 template class ValueHolder<Function, Module, Module>;
22
23 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
24 // have Module.h depend on <map>
25 //
26 struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
27 };
28
29
30 Module::Module()
31   : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
32     GlobalList(this, this), FunctionList(this, this), GVRefMap(0) {
33 }
34
35 Module::~Module() {
36   dropAllReferences();
37   GlobalList.delete_all();
38   GlobalList.setParent(0);
39   FunctionList.delete_all();
40   FunctionList.setParent(0);
41 }
42
43
44 // dropAllReferences() - This function causes all the subinstructions to "let
45 // go" of all references that they are maintaining.  This allows one to
46 // 'delete' a whole class at a time, even though there may be circular
47 // references... first all references are dropped, and all use counts go to
48 // zero.  Then everything is delete'd for real.  Note that no operations are
49 // valid on an object that has "dropped all references", except operator 
50 // delete.
51 //
52 void Module::dropAllReferences() {
53   for_each(FunctionList.begin(), FunctionList.end(),
54            std::mem_fun(&Function::dropAllReferences));
55
56   for_each(GlobalList.begin(), GlobalList.end(),
57            std::mem_fun(&GlobalVariable::dropAllReferences));
58
59   // If there are any GlobalVariable references still out there, nuke them now.
60   // Since all references are hereby dropped, nothing could possibly reference
61   // them still.
62   if (GVRefMap) {
63     for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
64          I != E; ++I) {
65       // Delete the ConstantPointerRef node...
66       I->second->destroyConstant();
67     }
68
69     // Since the table is empty, we can now delete it...
70     delete GVRefMap;
71   }
72 }
73
74 // Accessor for the underlying GlobalValRefMap...
75 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
76   // Create ref map lazily on demand...
77   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
78
79   GlobalValueRefMap::iterator I = GVRefMap->find(V);
80   if (I != GVRefMap->end()) return I->second;
81
82   ConstantPointerRef *Ref = new ConstantPointerRef(V);
83   GVRefMap->insert(std::make_pair(V, Ref));
84
85   return Ref;
86 }
87
88 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
89   GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
90   assert(I != GVRefMap->end() && 
91          "mutateConstantPointerRef; OldGV not in table!");
92   ConstantPointerRef *Ref = I->second;
93
94   // Remove the old entry...
95   GVRefMap->erase(I);
96
97   // Insert the new entry...
98   GVRefMap->insert(std::make_pair(NewGV, Ref));
99 }