blob: 9dc19dffb027a8484ef09edec682e77070440792 [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#ifndef NDEBUG // Only in -g mode...
13#include "llvm/Assembly/Writer.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000014#include <iostream>
15using std::cerr;
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#endif
17#include <algorithm>
18
19//===----------------------------------------------------------------------===//
20// Value Class
21//===----------------------------------------------------------------------===//
22
Chris Lattner25450e32001-12-13 00:41:27 +000023static inline const Type *checkType(const Type *Ty) {
24 assert(Ty && "Value defined with a null type: Error!");
25 return Ty;
26}
27
Chris Lattner7f74a562002-01-20 22:54:45 +000028Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
Chris Lattner25450e32001-12-13 00:41:27 +000029 : Name(name), Ty(checkType(ty), this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000030 VTy = vty;
31}
32
33Value::~Value() {
34#ifndef NDEBUG // Only in -g mode...
Chris Lattner874ddad2001-06-11 15:04:40 +000035 // Check to make sure that there are no uses of this value that are still
36 // around when the value is destroyed. If there are, then we have a dangling
37 // reference and something is wrong. This code is here to print out what is
38 // still being referenced. The value in question should be printed as
39 // a <badref>
40 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 if (Uses.begin() != Uses.end()) {
Chris Lattnerd6953052001-09-28 00:06:52 +000042 cerr << "While deleting: " << this;
Chris Lattner4cee8d82001-06-27 23:41:11 +000043 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
Chris Lattner7f74a562002-01-20 22:54:45 +000044 cerr << "Use still stuck around after Def is destroyed:" << *I << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000045 }
46#endif
47 assert(Uses.begin() == Uses.end());
48}
49
50void Value::replaceAllUsesWith(Value *D) {
51 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
Chris Lattner2dd58ae2001-06-29 05:25:51 +000052 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
Chris Lattner25c8f602002-03-21 05:38:15 +000053 assert(D->getType() == getType() &&
54 "replaceAllUses of value with new value of different type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000056 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000057#ifndef NDEBUG
58 unsigned NumUses = Uses.size();
59#endif
60 Use->replaceUsesOfWith(this, D);
61
62#ifndef NDEBUG // only in -g mode...
63 if (Uses.size() == NumUses)
64 cerr << "Use: " << Use << "replace with: " << D;
65#endif
66 assert(Uses.size() != NumUses && "Didn't remove definition!");
67 }
68}
69
Chris Lattner97d79f12001-09-07 16:57:07 +000070// refineAbstractType - This function is implemented because we use
71// potentially abstract types, and these types may be resolved to more
72// concrete types after we are constructed. For the value class, we simply
73// change Ty to point to the right type. :)
74//
75void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner67e5c292002-01-25 03:45:27 +000076 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
77 if (OldTy == NewTy && !OldTy->isAbstract())
78 Ty.removeUserFromConcrete();
Chris Lattner97d79f12001-09-07 16:57:07 +000079 Ty = NewTy;
80}
81
Chris Lattner2f7c9632001-06-06 20:29:01 +000082void Value::killUse(User *i) {
83 if (i == 0) return;
84 use_iterator I = find(Uses.begin(), Uses.end(), i);
85
86 assert(I != Uses.end() && "Use not in uses list!!");
87 Uses.erase(I);
88}
89
90User *Value::use_remove(use_iterator &I) {
91 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
92 User *i = *I;
93 I = Uses.erase(I);
94 return i;
95}
96
Chris Lattnerac128cd2001-09-18 17:03:59 +000097void Value::dump() const {
Chris Lattner92c15742001-09-19 14:09:25 +000098 cerr << this;
Chris Lattnerac128cd2001-09-18 17:03:59 +000099}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100
101//===----------------------------------------------------------------------===//
102// User Class
103//===----------------------------------------------------------------------===//
104
Chris Lattner7f74a562002-01-20 22:54:45 +0000105User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106 : Value(Ty, vty, name) {
107}
108
109// replaceUsesOfWith - Replaces all references to the "From" definition with
110// references to the "To" definition.
111//
112void User::replaceUsesOfWith(Value *From, Value *To) {
113 if (From == To) return; // Duh what?
114
Chris Lattnera073acb2001-07-07 08:36:50 +0000115 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
116 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117 // The side effects of this setOperand call include linking to
118 // "To", adding "this" to the uses list of To, and
119 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000120 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122}
123
124
125//===----------------------------------------------------------------------===//
126// SymTabValue Class
127//===----------------------------------------------------------------------===//
128
Chris Lattner97d79f12001-09-07 16:57:07 +0000129SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000130 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 ParentSymTab = SymTab = 0;
132}
133
134
135SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 delete SymTab;
137}
138
139void SymTabValue::setParentSymTab(SymbolTable *ST) {
140 ParentSymTab = ST;
141 if (SymTab)
142 SymTab->setParentSymTab(ST);
143}
144
145SymbolTable *SymTabValue::getSymbolTableSure() {
146 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
147 return SymTab;
148}
149
150// hasSymbolTable() - Returns true if there is a symbol table allocated to
151// this object AND if there is at least one name in it!
152//
153bool SymTabValue::hasSymbolTable() const {
154 if (!SymTab) return false;
155
156 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000157 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158 if (I->second.begin() != I->second.end())
159 return true; // Found nonempty type plane!
160 }
161
162 return false;
163}