blob: b137c62dd735b76d96be5fec0a61d2f2358a5742 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Value.cpp - Implement the Value class -----------------------------===//
2//
Chris Lattner8d44b992001-09-14 16:56:32 +00003// This file implements the Value, User, and SymTabValue classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/InstrTypes.h"
9#include "llvm/SymbolTable.h"
10#include "llvm/SymTabValue.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/ConstPoolVals.h"
12#include "llvm/Type.h"
13#ifndef NDEBUG // Only in -g mode...
14#include "llvm/Assembly/Writer.h"
15#endif
16#include <algorithm>
17
18//===----------------------------------------------------------------------===//
19// Value Class
20//===----------------------------------------------------------------------===//
21
Chris Lattner97d79f12001-09-07 16:57:07 +000022Value::Value(const Type *ty, ValueTy vty, const string &name = "")
23 : Name(name), Ty(ty, this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000024 VTy = vty;
25}
26
27Value::~Value() {
28#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000029 // Check to make sure that there are no uses of this value that are still
30 // around when the value is destroyed. If there are, then we have a dangling
31 // reference and something is wrong. This code is here to print out what is
32 // still being referenced. The value in question should be printed as
33 // a <badref>
34 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000035 if (Uses.begin() != Uses.end()) {
Chris Lattner4cee8d82001-06-27 23:41:11 +000036 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +000037 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
38 }
39#endif
40 assert(Uses.begin() == Uses.end());
41}
42
43void Value::replaceAllUsesWith(Value *D) {
44 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000045 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000046 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000047 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000048#ifndef NDEBUG
49 unsigned NumUses = Uses.size();
50#endif
51 Use->replaceUsesOfWith(this, D);
52
53#ifndef NDEBUG // only in -g mode...
54 if (Uses.size() == NumUses)
55 cerr << "Use: " << Use << "replace with: " << D;
56#endif
57 assert(Uses.size() != NumUses && "Didn't remove definition!");
58 }
59}
60
Chris Lattner97d79f12001-09-07 16:57:07 +000061// refineAbstractType - This function is implemented because we use
62// potentially abstract types, and these types may be resolved to more
63// concrete types after we are constructed. For the value class, we simply
64// change Ty to point to the right type. :)
65//
66void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
67 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
68 Ty = NewTy;
69}
70
Chris Lattner2f7c9632001-06-06 20:29:01 +000071void Value::killUse(User *i) {
72 if (i == 0) return;
73 use_iterator I = find(Uses.begin(), Uses.end(), i);
74
75 assert(I != Uses.end() && "Use not in uses list!!");
76 Uses.erase(I);
77}
78
79User *Value::use_remove(use_iterator &I) {
80 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
81 User *i = *I;
82 I = Uses.erase(I);
83 return i;
84}
85
Chris Lattnerac128cd2001-09-18 17:03:59 +000086#ifndef NDEBUG // Only in -g mode...
87void DebugValue(const Value *V) {
Vikram S. Advedf6bbeb2001-09-18 12:44:41 +000088 if (V)
89 cerr << *V << endl;
90 else
91 cerr << "<NULL value>" << endl;
92}
93
Chris Lattnerac128cd2001-09-18 17:03:59 +000094void DebugValue(const Value &V) {
Vikram S. Advedf6bbeb2001-09-18 12:44:41 +000095 cerr << V << endl;
96}
97
Chris Lattnerac128cd2001-09-18 17:03:59 +000098void Value::dump() const {
99 DebugValue(*this);
100}
101#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102
103//===----------------------------------------------------------------------===//
104// User Class
105//===----------------------------------------------------------------------===//
106
107User::User(const Type *Ty, ValueTy vty, const string &name)
108 : Value(Ty, vty, name) {
109}
110
111// replaceUsesOfWith - Replaces all references to the "From" definition with
112// references to the "To" definition.
113//
114void User::replaceUsesOfWith(Value *From, Value *To) {
115 if (From == To) return; // Duh what?
116
Chris Lattnera073acb2001-07-07 08:36:50 +0000117 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
118 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 // The side effects of this setOperand call include linking to
120 // "To", adding "this" to the uses list of To, and
121 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000122 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124}
125
126
127//===----------------------------------------------------------------------===//
128// SymTabValue Class
129//===----------------------------------------------------------------------===//
130
Chris Lattner97d79f12001-09-07 16:57:07 +0000131SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000132 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 ParentSymTab = SymTab = 0;
134}
135
136
137SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138 delete SymTab;
139}
140
141void SymTabValue::setParentSymTab(SymbolTable *ST) {
142 ParentSymTab = ST;
143 if (SymTab)
144 SymTab->setParentSymTab(ST);
145}
146
147SymbolTable *SymTabValue::getSymbolTableSure() {
148 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
149 return SymTab;
150}
151
152// hasSymbolTable() - Returns true if there is a symbol table allocated to
153// this object AND if there is at least one name in it!
154//
155bool SymTabValue::hasSymbolTable() const {
156 if (!SymTab) return false;
157
158 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000159 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 if (I->second.begin() != I->second.end())
161 return true; // Found nonempty type plane!
162 }
163
164 return false;
165}