Fix problem where subpasses would invalidate all analyses outstanding
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
2 //
3 // This file implements the Function & GlobalVariable classes for the VMCore
4 // library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Function.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Module.h"
11 #include "llvm/GlobalVariable.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Argument.h"
15 #include "ValueHolderImpl.h"
16
17 //===----------------------------------------------------------------------===//
18 // Argument Implementation
19 //===----------------------------------------------------------------------===//
20
21 // Specialize setName to take care of symbol table majik
22 void Argument::setName(const std::string &name, SymbolTable *ST) {
23   Function *P;
24   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
25          "Invalid symtab argument!");
26   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
27   Value::setName(name);
28   if (P && hasName()) P->getSymbolTable()->insert(this);
29 }
30
31
32
33 //===----------------------------------------------------------------------===//
34 // Function Implementation
35 //===----------------------------------------------------------------------===//
36
37
38 // Instantiate Templates - This ugliness is the price we have to pay
39 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
40 //
41 template class ValueHolder<Argument  , Function, Function>;
42 template class ValueHolder<BasicBlock, Function, Function>;
43
44 Function::Function(const FunctionType *Ty, bool isInternal,
45                    const std::string &name)
46   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
47     BasicBlocks(this), ArgumentList(this, this) {
48   ParentSymTab = SymTab = 0;
49 }
50
51 Function::~Function() {
52   dropAllReferences();    // After this it is safe to delete instructions.
53
54   // TODO: Should remove from the end, not the beginning of vector!
55   iterator BI = begin();
56   while ((BI = begin()) != end())
57     delete BasicBlocks.remove(BI);
58
59   // Delete all of the method arguments and unlink from symbol table...
60   ArgumentList.delete_all();
61   ArgumentList.setParent(0);
62   delete SymTab;
63 }
64
65 // Specialize setName to take care of symbol table majik
66 void Function::setName(const std::string &name, SymbolTable *ST) {
67   Module *P;
68   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
69          "Invalid symtab argument!");
70   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
71   Value::setName(name);
72   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
73 }
74
75 void Function::setParent(Module *parent) {
76   Parent = parent;
77
78   // Relink symbol tables together...
79   ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
80   if (SymTab) SymTab->setParentSymTab(ParentSymTab);
81 }
82
83 const FunctionType *Function::getFunctionType() const {
84   return cast<FunctionType>(getType()->getElementType());
85 }
86
87 const Type *Function::getReturnType() const { 
88   return getFunctionType()->getReturnType();
89 }
90
91 SymbolTable *Function::getSymbolTableSure() {
92   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
93   return SymTab;
94 }
95
96 // hasSymbolTable() - Returns true if there is a symbol table allocated to
97 // this object AND if there is at least one name in it!
98 //
99 bool Function::hasSymbolTable() const {
100   if (!SymTab) return false;
101
102   for (SymbolTable::const_iterator I = SymTab->begin(); 
103        I != SymTab->end(); ++I) {
104     if (I->second.begin() != I->second.end())
105       return true;                                // Found nonempty type plane!
106   }
107   
108   return false;
109 }
110
111
112 // dropAllReferences() - This function causes all the subinstructions to "let
113 // go" of all references that they are maintaining.  This allows one to
114 // 'delete' a whole class at a time, even though there may be circular
115 // references... first all references are dropped, and all use counts go to
116 // zero.  Then everything is delete'd for real.  Note that no operations are
117 // valid on an object that has "dropped all references", except operator 
118 // delete.
119 //
120 void Function::dropAllReferences() {
121   for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
122 }
123
124 //===----------------------------------------------------------------------===//
125 // GlobalVariable Implementation
126 //===----------------------------------------------------------------------===//
127
128 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
129                                Constant *Initializer = 0,
130                                const std::string &Name = "")
131   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
132     isConstantGlobal(constant) {
133   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
134 }
135
136 // Specialize setName to take care of symbol table majik
137 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
138   Module *P;
139   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
140          "Invalid symtab argument!");
141   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
142   Value::setName(name);
143   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
144 }