blob: a42535580bff176c3aa86d97ef6ecf4f67557b7a [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 Lattnerf50b7232001-09-07 16:47:42 +000013#include "llvm/Type.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000014#include "llvm/ConstantVals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
Chris Lattner446ad502001-10-13 06:58:40 +000016#include <map>
Chris Lattner2f7c9632001-06-06 20:29:01 +000017
18// Instantiate Templates - This ugliness is the price we have to pay
19// for having a DefHolderImpl.h file seperate from DefHolder.h! :(
20//
Chris Lattnerda975502001-09-10 07:58:01 +000021template class ValueHolder<GlobalVariable, Module, Module>;
Chris Lattnerf2a738c2001-07-14 06:13:19 +000022template class ValueHolder<Method, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000023
Chris Lattner446ad502001-10-13 06:58:40 +000024// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
25// have Module.h depend on <map>
26//
Chris Lattner3462ae32001-12-03 22:26:30 +000027struct GlobalValueRefMap : public map<GlobalValue*, ConstantPointerRef*>{
Chris Lattner446ad502001-10-13 06:58:40 +000028};
29
30
Chris Lattner2f7c9632001-06-06 20:29:01 +000031Module::Module()
Chris Lattnerf50b7232001-09-07 16:47:42 +000032 : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
Chris Lattner446ad502001-10-13 06:58:40 +000033 GlobalList(this, this), MethodList(this, this), GVRefMap(0) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000034}
35
36Module::~Module() {
37 dropAllReferences();
Chris Lattnerda975502001-09-10 07:58:01 +000038 GlobalList.delete_all();
39 GlobalList.setParent(0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 MethodList.delete_all();
41 MethodList.setParent(0);
42}
43
44
45// dropAllReferences() - This function causes all the subinstructions to "let
46// go" of all references that they are maintaining. This allows one to
47// 'delete' a whole class at a time, even though there may be circular
48// references... first all references are dropped, and all use counts go to
49// zero. Then everything is delete'd for real. Note that no operations are
50// valid on an object that has "dropped all references", except operator
51// delete.
52//
53void Module::dropAllReferences() {
Chris Lattner446ad502001-10-13 06:58:40 +000054 for_each(MethodList.begin(), MethodList.end(),
55 std::mem_fun(&Method::dropAllReferences));
56
57 for_each(GlobalList.begin(), GlobalList.end(),
58 std::mem_fun(&GlobalVariable::dropAllReferences));
59
60 // If there are any GlobalVariable references still out there, nuke them now.
61 // Since all references are hereby dropped, nothing could possibly reference
62 // them still.
63 if (GVRefMap) {
64 for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
65 I != E; ++I) {
Chris Lattner3462ae32001-12-03 22:26:30 +000066 // Delete the ConstantPointerRef node...
Chris Lattner446ad502001-10-13 06:58:40 +000067 I->second->destroyConstant();
68 }
69
70 // Since the table is empty, we can now delete it...
71 delete GVRefMap;
72 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000073}
Chris Lattner31cf9842001-06-30 04:35:40 +000074
75// reduceApply - Apply the specified function to all of the methods in this
76// module. The result values are or'd together and the result is returned.
77//
Chris Lattnerda975502001-09-10 07:58:01 +000078bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
79 return reduce_apply_bool(gbegin(), gend(), Func);
80}
81bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
82 return reduce_apply_bool(gbegin(), gend(), Func);
83}
Chris Lattner31cf9842001-06-30 04:35:40 +000084bool Module::reduceApply(bool (*Func)(Method*)) {
85 return reduce_apply_bool(begin(), end(), Func);
86}
87bool Module::reduceApply(bool (*Func)(const Method*)) const {
88 return reduce_apply_bool(begin(), end(), Func);
89}
90
Chris Lattner446ad502001-10-13 06:58:40 +000091// Accessor for the underlying GlobalValRefMap...
Chris Lattner3462ae32001-12-03 22:26:30 +000092ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
Chris Lattner446ad502001-10-13 06:58:40 +000093 // Create ref map lazily on demand...
94 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
95
96 GlobalValueRefMap::iterator I = GVRefMap->find(V);
97 if (I != GVRefMap->end()) return I->second;
98
Chris Lattner3462ae32001-12-03 22:26:30 +000099 ConstantPointerRef *Ref = new ConstantPointerRef(V);
Chris Lattner446ad502001-10-13 06:58:40 +0000100 GVRefMap->insert(make_pair(V, Ref));
101
102 return Ref;
103}
104
Chris Lattner3462ae32001-12-03 22:26:30 +0000105void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
Chris Lattner446ad502001-10-13 06:58:40 +0000106 GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
107 assert(I != GVRefMap->end() &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000108 "mutateConstantPointerRef; OldGV not in table!");
109 ConstantPointerRef *Ref = I->second;
Chris Lattner446ad502001-10-13 06:58:40 +0000110
111 // Remove the old entry...
112 GVRefMap->erase(I);
113
114 // Insert the new entry...
115 GVRefMap->insert(make_pair(NewGV, Ref));
116}