blob: 7f28eb9d2875ba0675934e582db7e0ce6c332f2c [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
Chris Lattner2f7c9632001-06-06 20:29:01 +00007#include "llvm/InstrTypes.h"
8#include "llvm/SymbolTable.h"
9#include "llvm/SymTabValue.h"
Chris Lattner67e5c292002-01-25 03:45:27 +000010#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include <algorithm>
12
13//===----------------------------------------------------------------------===//
14// Value Class
15//===----------------------------------------------------------------------===//
16
Chris Lattner25450e32001-12-13 00:41:27 +000017static inline const Type *checkType(const Type *Ty) {
18 assert(Ty && "Value defined with a null type: Error!");
19 return Ty;
20}
21
Chris Lattner7f74a562002-01-20 22:54:45 +000022Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
Chris Lattner25450e32001-12-13 00:41:27 +000023 : Name(name), Ty(checkType(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 Lattner3d381bb2002-04-07 22:33:13 +000036 std::cerr << "While deleting: ";
37 dump();
38 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
39 std::cerr << "Use still stuck around after Def is destroyed:";
40 (*I)->dump();
41 std::cerr << "\n";
42 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000043 }
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 Lattner25c8f602002-03-21 05:38:15 +000051 assert(D->getType() == getType() &&
52 "replaceAllUses of value with new value of different type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000053 while (!Uses.empty()) {
Chris Lattner8d44b992001-09-14 16:56:32 +000054 User *Use = Uses.back();
Chris Lattner2f7c9632001-06-06 20:29:01 +000055#ifndef NDEBUG
56 unsigned NumUses = Uses.size();
57#endif
58 Use->replaceUsesOfWith(this, D);
59
60#ifndef NDEBUG // only in -g mode...
Chris Lattner3d381bb2002-04-07 22:33:13 +000061 if (Uses.size() == NumUses) {
62 std::cerr << "Use: ";
63 Use->dump();
64 std::cerr << "replace with: ";
65 D->dump();
66 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000067#endif
68 assert(Uses.size() != NumUses && "Didn't remove definition!");
69 }
70}
71
Chris Lattner97d79f12001-09-07 16:57:07 +000072// refineAbstractType - This function is implemented because we use
73// potentially abstract types, and these types may be resolved to more
74// concrete types after we are constructed. For the value class, we simply
75// change Ty to point to the right type. :)
76//
77void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner67e5c292002-01-25 03:45:27 +000078 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
79 if (OldTy == NewTy && !OldTy->isAbstract())
80 Ty.removeUserFromConcrete();
Chris Lattner97d79f12001-09-07 16:57:07 +000081 Ty = NewTy;
82}
83
Chris Lattner2f7c9632001-06-06 20:29:01 +000084void Value::killUse(User *i) {
85 if (i == 0) return;
86 use_iterator I = find(Uses.begin(), Uses.end(), i);
87
88 assert(I != Uses.end() && "Use not in uses list!!");
89 Uses.erase(I);
90}
91
92User *Value::use_remove(use_iterator &I) {
93 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
94 User *i = *I;
95 I = Uses.erase(I);
96 return i;
97}
98
Chris Lattner2f7c9632001-06-06 20:29:01 +000099//===----------------------------------------------------------------------===//
100// User Class
101//===----------------------------------------------------------------------===//
102
Chris Lattner7f74a562002-01-20 22:54:45 +0000103User::User(const Type *Ty, ValueTy vty, const std::string &name)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 : Value(Ty, vty, name) {
105}
106
107// replaceUsesOfWith - Replaces all references to the "From" definition with
108// references to the "To" definition.
109//
110void User::replaceUsesOfWith(Value *From, Value *To) {
111 if (From == To) return; // Duh what?
112
Chris Lattnera073acb2001-07-07 08:36:50 +0000113 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
114 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 // The side effects of this setOperand call include linking to
116 // "To", adding "this" to the uses list of To, and
117 // most importantly, removing "this" from the use list of "From".
Chris Lattnera073acb2001-07-07 08:36:50 +0000118 setOperand(i, To); // Fix it now...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120}
121
122
123//===----------------------------------------------------------------------===//
124// SymTabValue Class
125//===----------------------------------------------------------------------===//
126
Chris Lattner97d79f12001-09-07 16:57:07 +0000127SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
Chris Lattnerf2a738c2001-07-14 06:13:19 +0000128 assert(ValueParent && "SymTavValue without parent!?!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129 ParentSymTab = SymTab = 0;
130}
131
132
133SymTabValue::~SymTabValue() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 delete SymTab;
135}
136
137void SymTabValue::setParentSymTab(SymbolTable *ST) {
138 ParentSymTab = ST;
139 if (SymTab)
140 SymTab->setParentSymTab(ST);
141}
142
143SymbolTable *SymTabValue::getSymbolTableSure() {
144 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
145 return SymTab;
146}
147
148// hasSymbolTable() - Returns true if there is a symbol table allocated to
149// this object AND if there is at least one name in it!
150//
151bool SymTabValue::hasSymbolTable() const {
152 if (!SymTab) return false;
153
154 for (SymbolTable::const_iterator I = SymTab->begin();
Chris Lattner4cee8d82001-06-27 23:41:11 +0000155 I != SymTab->end(); ++I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156 if (I->second.begin() != I->second.end())
157 return true; // Found nonempty type plane!
158 }
159
160 return false;
161}