blob: ac9f7012ef17ccbb5e5f2dab2415d8bd113cc7a7 [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 Lattner67e5c292002-01-25 03:45:27 +000011#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000012#include <algorithm>
13
14//===----------------------------------------------------------------------===//
15// Value Class
16//===----------------------------------------------------------------------===//
17
Chris Lattner25450e32001-12-13 00:41:27 +000018static inline const Type *checkType(const Type *Ty) {
19 assert(Ty && "Value defined with a null type: Error!");
20 return Ty;
21}
22
Chris Lattner7f74a562002-01-20 22:54:45 +000023Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
Chris Lattner25450e32001-12-13 00:41:27 +000024 : Name(name), Ty(checkType(ty), this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000025 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 Lattner3d381bb2002-04-07 22:33:13 +000037 std::cerr << "While deleting: ";
38 dump();
39 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
40 std::cerr << "Use still stuck around after Def is destroyed:";
41 (*I)->dump();
42 std::cerr << "\n";
43 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000044 }
45#endif
46 assert(Uses.begin() == Uses.end());
47}
48
49void Value::replaceAllUsesWith(Value *D) {
50 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000051 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner25c8f602002-03-21 05:38:15 +000052 assert(D->getType() == getType() &&
53 "replaceAllUses of value with new value of different type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000054 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000055 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000056#ifndef NDEBUG
57 unsigned NumUses = Uses.size();
58#endif
59 Use->replaceUsesOfWith(this, D);
60
61#ifndef NDEBUG // only in -g mode...
Chris Lattner3d381bb2002-04-07 22:33:13 +000062 if (Uses.size() == NumUses) {
63 std::cerr << "Use: ";
64 Use->dump();
65 std::cerr << "replace with: ";
66 D->dump();
67 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000068#endif
69 assert(Uses.size() != NumUses && "Didn't remove definition!");
70 }
71}
72
Chris Lattner97d79f12001-09-07 16:57:07 +000073// refineAbstractType - This function is implemented because we use
74// potentially abstract types, and these types may be resolved to more
75// concrete types after we are constructed. For the value class, we simply
76// change Ty to point to the right type. :)
77//
78void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner67e5c292002-01-25 03:45:27 +000079 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
80 if (OldTy == NewTy && !OldTy->isAbstract())
81 Ty.removeUserFromConcrete();
Chris Lattner97d79f12001-09-07 16:57:07 +000082 Ty = NewTy;
83}
84
Chris Lattner2f7c9632001-06-06 20:29:01 +000085void Value::killUse(User *i) {
86 if (i == 0) return;
87 use_iterator I = find(Uses.begin(), Uses.end(), i);
88
89 assert(I != Uses.end() && "Use not in uses list!!");
90 Uses.erase(I);
91}
92
93User *Value::use_remove(use_iterator &I) {
94 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
95 User *i = *I;
96 I = Uses.erase(I);
97 return i;
98}
99
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100//===----------------------------------------------------------------------===//
101// User Class
102//===----------------------------------------------------------------------===//
103
Chris Lattner7f74a562002-01-20 22:54:45 +0000104User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105 : Value(Ty, vty, name) {
106}
107
108// replaceUsesOfWith - Replaces all references to the "From" definition with
109// references to the "To" definition.
110//
111void User::replaceUsesOfWith(Value *From, Value *To) {
112 if (From == To) return; // Duh what?
113
Chris Lattnera073acb2001-07-07 08:36:50 +0000114 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
115 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 // The side effects of this setOperand call include linking to
117 // "To", adding "this" to the uses list of To, and
118 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000119 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121}
122
123
124//===----------------------------------------------------------------------===//
125// SymTabValue Class
126//===----------------------------------------------------------------------===//
127
Chris Lattner97d79f12001-09-07 16:57:07 +0000128SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000129 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130 ParentSymTab = SymTab = 0;
131}
132
133
134SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135 delete SymTab;
136}
137
138void SymTabValue::setParentSymTab(SymbolTable *ST) {
139 ParentSymTab = ST;
140 if (SymTab)
141 SymTab->setParentSymTab(ST);
142}
143
144SymbolTable *SymTabValue::getSymbolTableSure() {
145 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
146 return SymTab;
147}
148
149// hasSymbolTable() - Returns true if there is a symbol table allocated to
150// this object AND if there is at least one name in it!
151//
152bool SymTabValue::hasSymbolTable() const {
153 if (!SymTab) return false;
154
155 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000156 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157 if (I->second.begin() != I->second.end())
158 return true; // Found nonempty type plane!
159 }
160
161 return false;
162}