blob: 0ac7205104326a666ef3025fa44afbc9244f4c4c [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/Type.h"
12#ifndef NDEBUG // Only in -g mode...
13#include "llvm/Assembly/Writer.h"
14#endif
15#include <algorithm>
16
17//===----------------------------------------------------------------------===//
18// Value Class
19//===----------------------------------------------------------------------===//
20
Chris Lattner25450e32001-12-13 00:41:27 +000021static inline const Type *checkType(const Type *Ty) {
22 assert(Ty && "Value defined with a null type: Error!");
23 return Ty;
24}
25
Chris Lattner97d79f12001-09-07 16:57:07 +000026Value::Value(const Type *ty, ValueTy vty, const string &name = "")
Chris Lattner25450e32001-12-13 00:41:27 +000027 : Name(name), Ty(checkType(ty), this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000028 VTy = vty;
29}
30
31Value::~Value() {
32#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000033 // Check to make sure that there are no uses of this value that are still
34 // around when the value is destroyed. If there are, then we have a dangling
35 // reference and something is wrong. This code is here to print out what is
36 // still being referenced. The value in question should be printed as
37 // a <badref>
38 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 if (Uses.begin() != Uses.end()) {
Chris Lattnerd6953052001-09-28 00:06:52 +000040 cerr << "While deleting: " << this;
Chris Lattner4cee8d82001-06-27 23:41:11 +000041 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +000042 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
43 }
44#endif
45 assert(Uses.begin() == Uses.end());
46}
47
48void Value::replaceAllUsesWith(Value *D) {
49 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000050 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000051 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000052 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000053#ifndef NDEBUG
54 unsigned NumUses = Uses.size();
55#endif
56 Use->replaceUsesOfWith(this, D);
57
58#ifndef NDEBUG // only in -g mode...
59 if (Uses.size() == NumUses)
60 cerr << "Use: " << Use << "replace with: " << D;
61#endif
62 assert(Uses.size() != NumUses && "Didn't remove definition!");
63 }
64}
65
Chris Lattner97d79f12001-09-07 16:57:07 +000066// refineAbstractType - This function is implemented because we use
67// potentially abstract types, and these types may be resolved to more
68// concrete types after we are constructed. For the value class, we simply
69// change Ty to point to the right type. :)
70//
71void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
72 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
73 Ty = NewTy;
74}
75
Chris Lattner2f7c9632001-06-06 20:29:01 +000076void Value::killUse(User *i) {
77 if (i == 0) return;
78 use_iterator I = find(Uses.begin(), Uses.end(), i);
79
80 assert(I != Uses.end() && "Use not in uses list!!");
81 Uses.erase(I);
82}
83
84User *Value::use_remove(use_iterator &I) {
85 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
86 User *i = *I;
87 I = Uses.erase(I);
88 return i;
89}
90
Chris Lattnerac128cd2001-09-18 17:03:59 +000091#ifndef NDEBUG // Only in -g mode...
Chris Lattnerac128cd2001-09-18 17:03:59 +000092void Value::dump() const {
Chris Lattner92c15742001-09-19 14:09:25 +000093 cerr << this;
Chris Lattnerac128cd2001-09-18 17:03:59 +000094}
95#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +000096
97//===----------------------------------------------------------------------===//
98// User Class
99//===----------------------------------------------------------------------===//
100
101User::User(const Type *Ty, ValueTy vty, const string &name)
102 : Value(Ty, vty, name) {
103}
104
105// replaceUsesOfWith - Replaces all references to the "From" definition with
106// references to the "To" definition.
107//
108void User::replaceUsesOfWith(Value *From, Value *To) {
109 if (From == To) return; // Duh what?
110
Chris Lattnera073acb2001-07-07 08:36:50 +0000111 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
112 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 // The side effects of this setOperand call include linking to
114 // "To", adding "this" to the uses list of To, and
115 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000116 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118}
119
120
121//===----------------------------------------------------------------------===//
122// SymTabValue Class
123//===----------------------------------------------------------------------===//
124
Chris Lattner97d79f12001-09-07 16:57:07 +0000125SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000126 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 ParentSymTab = SymTab = 0;
128}
129
130
131SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 delete SymTab;
133}
134
135void SymTabValue::setParentSymTab(SymbolTable *ST) {
136 ParentSymTab = ST;
137 if (SymTab)
138 SymTab->setParentSymTab(ST);
139}
140
141SymbolTable *SymTabValue::getSymbolTableSure() {
142 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
143 return SymTab;
144}
145
146// hasSymbolTable() - Returns true if there is a symbol table allocated to
147// this object AND if there is at least one name in it!
148//
149bool SymTabValue::hasSymbolTable() const {
150 if (!SymTab) return false;
151
152 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000153 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000154 if (I->second.begin() != I->second.end())
155 return true; // Found nonempty type plane!
156 }
157
158 return false;
159}