Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Module class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 7 | #include "llvm/Module.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 8 | #include "llvm/Method.h" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 9 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 10 | #include "llvm/BasicBlock.h" |
| 11 | #include "llvm/InstrTypes.h" |
| 12 | #include "llvm/ValueHolderImpl.h" |
Chris Lattner | e2472bb | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 13 | #include "llvm/Support/STLExtras.h" |
Chris Lattner | f50b723 | 2001-09-07 16:47:42 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame^] | 15 | #include <map> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 20 | template class ValueHolder<GlobalVariable, Module, Module>; |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 21 | template class ValueHolder<Method, Module, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame^] | 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 map<GlobalValue*, ConstPoolPointerReference*>{ |
| 27 | }; |
| 28 | |
| 29 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | Module::Module() |
Chris Lattner | f50b723 | 2001-09-07 16:47:42 +0000 | [diff] [blame] | 31 | : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this), |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame^] | 32 | GlobalList(this, this), MethodList(this, this), GVRefMap(0) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Module::~Module() { |
| 36 | dropAllReferences(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 37 | GlobalList.delete_all(); |
| 38 | GlobalList.setParent(0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | 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 | // |
| 52 | void Module::dropAllReferences() { |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame^] | 53 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 73 | |
| 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 Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 77 | bool Module::reduceApply(bool (*Func)(GlobalVariable*)) { |
| 78 | return reduce_apply_bool(gbegin(), gend(), Func); |
| 79 | } |
| 80 | bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const { |
| 81 | return reduce_apply_bool(gbegin(), gend(), Func); |
| 82 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 83 | bool Module::reduceApply(bool (*Func)(Method*)) { |
| 84 | return reduce_apply_bool(begin(), end(), Func); |
| 85 | } |
| 86 | bool Module::reduceApply(bool (*Func)(const Method*)) const { |
| 87 | return reduce_apply_bool(begin(), end(), Func); |
| 88 | } |
| 89 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame^] | 90 | // Accessor for the underlying GlobalValRefMap... |
| 91 | ConstPoolPointerReference *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 | |
| 104 | void 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 | } |