blob: fbe85867146b38d4d407d778154cdab6479d99f6 [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/Method.h"
Chris Lattnerda975502001-09-10 07:58:01 +00009#include "llvm/GlobalVariable.h"
Chris Lattner31cf9842001-06-30 04:35:40 +000010#include "llvm/BasicBlock.h"
11#include "llvm/InstrTypes.h"
12#include "llvm/ValueHolderImpl.h"
Chris Lattnere2472bb2001-07-23 17:46:59 +000013#include "llvm/Support/STLExtras.h"
Chris Lattnerf50b7232001-09-07 16:47:42 +000014#include "llvm/Type.h"
Chris Lattner446ad502001-10-13 06:58:40 +000015#include <map>
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
17// Instantiate Templates - This ugliness is the price we have to pay
18// for having a DefHolderImpl.h file seperate from DefHolder.h! :(
19//
Chris Lattnerda975502001-09-10 07:58:01 +000020template class ValueHolder<GlobalVariable, Module, Module>;
Chris Lattnerf2a738c2001-07-14 06:13:19 +000021template class ValueHolder<Method, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000022
Chris Lattner446ad502001-10-13 06:58:40 +000023// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
24// have Module.h depend on <map>
25//
26struct GlobalValueRefMap : public map<GlobalValue*, ConstPoolPointerReference*>{
27};
28
29
Chris Lattner2f7c9632001-06-06 20:29:01 +000030Module::Module()
Chris Lattnerf50b7232001-09-07 16:47:42 +000031 : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
Chris Lattner446ad502001-10-13 06:58:40 +000032 GlobalList(this, this), MethodList(this, this), GVRefMap(0) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
35Module::~Module() {
36 dropAllReferences();
Chris Lattnerda975502001-09-10 07:58:01 +000037 GlobalList.delete_all();
38 GlobalList.setParent(0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 MethodList.delete_all();
40 MethodList.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//
52void Module::dropAllReferences() {
Chris Lattner446ad502001-10-13 06:58:40 +000053 for_each(MethodList.begin(), MethodList.end(),
54 std::mem_fun(&Method::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 ConstPoolPointerReference node...
66 I->second->destroyConstant();
67 }
68
69 // Since the table is empty, we can now delete it...
70 delete GVRefMap;
71 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000072}
Chris Lattner31cf9842001-06-30 04:35:40 +000073
74// reduceApply - Apply the specified function to all of the methods in this
75// module. The result values are or'd together and the result is returned.
76//
Chris Lattnerda975502001-09-10 07:58:01 +000077bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
78 return reduce_apply_bool(gbegin(), gend(), Func);
79}
80bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
81 return reduce_apply_bool(gbegin(), gend(), Func);
82}
Chris Lattner31cf9842001-06-30 04:35:40 +000083bool Module::reduceApply(bool (*Func)(Method*)) {
84 return reduce_apply_bool(begin(), end(), Func);
85}
86bool Module::reduceApply(bool (*Func)(const Method*)) const {
87 return reduce_apply_bool(begin(), end(), Func);
88}
89
Chris Lattner446ad502001-10-13 06:58:40 +000090// Accessor for the underlying GlobalValRefMap...
91ConstPoolPointerReference *Module::getConstPoolPointerReference(GlobalValue *V){
92 // Create ref map lazily on demand...
93 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
94
95 GlobalValueRefMap::iterator I = GVRefMap->find(V);
96 if (I != GVRefMap->end()) return I->second;
97
98 ConstPoolPointerReference *Ref = new ConstPoolPointerReference(V);
99 GVRefMap->insert(make_pair(V, Ref));
100
101 return Ref;
102}
103
104void Module::mutateConstPoolPointerReference(GlobalValue *OldGV,
105 GlobalValue *NewGV) {
106 GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
107 assert(I != GVRefMap->end() &&
108 "mutateConstPoolPointerReference; OldGV not in table!");
109 ConstPoolPointerReference *Ref = I->second;
110
111 // Remove the old entry...
112 GVRefMap->erase(I);
113
114 // Insert the new entry...
115 GVRefMap->insert(make_pair(NewGV, Ref));
116}