Module.cpp revision dd6dfbc8cc008faca03b6ac9b904f263205a418a
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/Method.h" 9#include "llvm/GlobalVariable.h" 10#include "llvm/BasicBlock.h" 11#include "llvm/InstrTypes.h" 12#include "llvm/ValueHolderImpl.h" 13#include "llvm/Support/STLExtras.h" 14#include "llvm/Type.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// 20template class ValueHolder<GlobalVariable, Module, Module>; 21template class ValueHolder<Method, 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// 26struct GlobalValueRefMap : public map<GlobalValue*, ConstPoolPointerReference*>{ 27}; 28 29 30Module::Module() 31 : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this), 32 GlobalList(this, this), MethodList(this, this), GVRefMap(0) { 33} 34 35Module::~Module() { 36 dropAllReferences(); 37 GlobalList.delete_all(); 38 GlobalList.setParent(0); 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// 52void Module::dropAllReferences() { 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 } 72} 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// 77bool 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} 83bool 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 90// 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} 117