blob: d40aaca2773b67bbaa208fa1bc06347c566c3a2a [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 Lattnerd6953052001-09-28 00:06:52 +000036 cerr << "While deleting: " << this;
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()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000048 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000049#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
Chris Lattner97d79f12001-09-07 16:57:07 +000062// refineAbstractType - This function is implemented because we use
63// potentially abstract types, and these types may be resolved to more
64// concrete types after we are constructed. For the value class, we simply
65// change Ty to point to the right type. :)
66//
67void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
68 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
69 Ty = NewTy;
70}
71
Chris Lattner2f7c9632001-06-06 20:29:01 +000072void Value::killUse(User *i) {
73 if (i == 0) return;
74 use_iterator I = find(Uses.begin(), Uses.end(), i);
75
76 assert(I != Uses.end() && "Use not in uses list!!");
77 Uses.erase(I);
78}
79
80User *Value::use_remove(use_iterator &I) {
81 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
82 User *i = *I;
83 I = Uses.erase(I);
84 return i;
85}
86
Chris Lattnerac128cd2001-09-18 17:03:59 +000087#ifndef NDEBUG // Only in -g mode...
Chris Lattnerac128cd2001-09-18 17:03:59 +000088void Value::dump() const {
Chris Lattner92c15742001-09-19 14:09:25 +000089 cerr << this;
Chris Lattnerac128cd2001-09-18 17:03:59 +000090}
91#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +000092
93//===----------------------------------------------------------------------===//
94// User Class
95//===----------------------------------------------------------------------===//
96
97User::User(const Type *Ty, ValueTy vty, const string &name)
98 : Value(Ty, vty, name) {
99}
100
101// replaceUsesOfWith - Replaces all references to the "From" definition with
102// references to the "To" definition.
103//
104void User::replaceUsesOfWith(Value *From, Value *To) {
105 if (From == To) return; // Duh what?
106
Chris Lattnera073acb2001-07-07 08:36:50 +0000107 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
108 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109 // The side effects of this setOperand call include linking to
110 // "To", adding "this" to the uses list of To, and
111 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000112 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114}
115
116
117//===----------------------------------------------------------------------===//
118// SymTabValue Class
119//===----------------------------------------------------------------------===//
120
Chris Lattner97d79f12001-09-07 16:57:07 +0000121SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000122 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 ParentSymTab = SymTab = 0;
124}
125
126
127SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 delete SymTab;
129}
130
131void SymTabValue::setParentSymTab(SymbolTable *ST) {
132 ParentSymTab = ST;
133 if (SymTab)
134 SymTab->setParentSymTab(ST);
135}
136
137SymbolTable *SymTabValue::getSymbolTableSure() {
138 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
139 return SymTab;
140}
141
142// hasSymbolTable() - Returns true if there is a symbol table allocated to
143// this object AND if there is at least one name in it!
144//
145bool SymTabValue::hasSymbolTable() const {
146 if (!SymTab) return false;
147
148 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000149 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150 if (I->second.begin() != I->second.end())
151 return true; // Found nonempty type plane!
152 }
153
154 return false;
155}