blob: 48f9347f962e6394ef8c0593973066cbd4e435ba [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
2//
3// This file implements the Value class.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/InstrTypes.h"
9#include "llvm/SymbolTable.h"
10#include "llvm/SymTabValue.h"
11#include "llvm/ConstantPool.h"
12#include "llvm/ConstPoolVals.h"
13#include "llvm/Type.h"
14#ifndef NDEBUG // Only in -g mode...
15#include "llvm/Assembly/Writer.h"
16#endif
17#include <algorithm>
18
19//===----------------------------------------------------------------------===//
20// Value Class
21//===----------------------------------------------------------------------===//
22
23Value::Value(const Type *ty, ValueTy vty, const string &name = "") : Name(name){
24 Ty = ty;
25 VTy = vty;
26}
27
28Value::~Value() {
29#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000030 // Check to make sure that there are no uses of this value that are still
31 // around when the value is destroyed. If there are, then we have a dangling
32 // reference and something is wrong. This code is here to print out what is
33 // still being referenced. The value in question should be printed as
34 // a <badref>
35 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000036 if (Uses.begin() != Uses.end()) {
Chris Lattner4cee8d82001-06-27 23:41:11 +000037 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +000038 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
39 }
40#endif
41 assert(Uses.begin() == Uses.end());
42}
43
44void Value::replaceAllUsesWith(Value *D) {
45 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000046 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000047 while (!Uses.empty()) {
48 User *Use = Uses.front();
49#ifndef NDEBUG
50 unsigned NumUses = Uses.size();
51#endif
52 Use->replaceUsesOfWith(this, D);
53
54#ifndef NDEBUG // only in -g mode...
55 if (Uses.size() == NumUses)
56 cerr << "Use: " << Use << "replace with: " << D;
57#endif
58 assert(Uses.size() != NumUses && "Didn't remove definition!");
59 }
60}
61
62void Value::killUse(User *i) {
63 if (i == 0) return;
64 use_iterator I = find(Uses.begin(), Uses.end(), i);
65
66 assert(I != Uses.end() && "Use not in uses list!!");
67 Uses.erase(I);
68}
69
70User *Value::use_remove(use_iterator &I) {
71 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
72 User *i = *I;
73 I = Uses.erase(I);
74 return i;
75}
76
77
78//===----------------------------------------------------------------------===//
79// User Class
80//===----------------------------------------------------------------------===//
81
82User::User(const Type *Ty, ValueTy vty, const string &name)
83 : Value(Ty, vty, name) {
84}
85
86// replaceUsesOfWith - Replaces all references to the "From" definition with
87// references to the "To" definition.
88//
89void User::replaceUsesOfWith(Value *From, Value *To) {
90 if (From == To) return; // Duh what?
91
Chris Lattnera073acb2001-07-07 08:36:50 +000092 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
93 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 // The side effects of this setOperand call include linking to
95 // "To", adding "this" to the uses list of To, and
96 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +000097 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +000098 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000099}
100
101
102//===----------------------------------------------------------------------===//
103// SymTabValue Class
104//===----------------------------------------------------------------------===//
105
106// Instantiate Templates - This ugliness is the price we have to pay
107// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
108//
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000109template class ValueHolder<ConstPoolVal, SymTabValue, SymTabValue>;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000111SymTabValue::SymTabValue(Value *p) : ConstPool(this), ValueParent(p) {
112 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 ParentSymTab = SymTab = 0;
114}
115
116
117SymTabValue::~SymTabValue() {
118 ConstPool.dropAllReferences();
119 ConstPool.delete_all();
120 ConstPool.setParent(0);
121
122 delete SymTab;
123}
124
125void SymTabValue::setParentSymTab(SymbolTable *ST) {
126 ParentSymTab = ST;
127 if (SymTab)
128 SymTab->setParentSymTab(ST);
129}
130
131SymbolTable *SymTabValue::getSymbolTableSure() {
132 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
133 return SymTab;
134}
135
136// hasSymbolTable() - Returns true if there is a symbol table allocated to
137// this object AND if there is at least one name in it!
138//
139bool SymTabValue::hasSymbolTable() const {
140 if (!SymTab) return false;
141
142 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000143 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144 if (I->second.begin() != I->second.end())
145 return true; // Found nonempty type plane!
146 }
147
148 return false;
149}