1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // This file implements the Value, User, and SymTabValue classes.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/ValueHolderImpl.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/SymbolTable.h"
10 #include "llvm/SymTabValue.h"
11 #include "llvm/Type.h"
12 #ifndef NDEBUG // Only in -g mode...
13 #include "llvm/Assembly/Writer.h"
17 //===----------------------------------------------------------------------===//
19 //===----------------------------------------------------------------------===//
21 Value::Value(const Type *ty, ValueTy vty, const string &name = "")
22 : Name(name), Ty(ty, this) {
27 #ifndef NDEBUG // Only in -g mode...
28 // Check to make sure that there are no uses of this value that are still
29 // around when the value is destroyed. If there are, then we have a dangling
30 // reference and something is wrong. This code is here to print out what is
31 // still being referenced. The value in question should be printed as
34 if (Uses.begin() != Uses.end()) {
35 cerr << "While deleting: " << this;
36 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
37 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
40 assert(Uses.begin() == Uses.end());
43 void Value::replaceAllUsesWith(Value *D) {
44 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
45 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
46 while (!Uses.empty()) {
47 User *Use = Uses.back();
49 unsigned NumUses = Uses.size();
51 Use->replaceUsesOfWith(this, D);
53 #ifndef NDEBUG // only in -g mode...
54 if (Uses.size() == NumUses)
55 cerr << "Use: " << Use << "replace with: " << D;
57 assert(Uses.size() != NumUses && "Didn't remove definition!");
61 // refineAbstractType - This function is implemented because we use
62 // potentially abstract types, and these types may be resolved to more
63 // concrete types after we are constructed. For the value class, we simply
64 // change Ty to point to the right type. :)
66 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
67 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
71 void Value::killUse(User *i) {
73 use_iterator I = find(Uses.begin(), Uses.end(), i);
75 assert(I != Uses.end() && "Use not in uses list!!");
79 User *Value::use_remove(use_iterator &I) {
80 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
86 #ifndef NDEBUG // Only in -g mode...
87 void Value::dump() const {
92 //===----------------------------------------------------------------------===//
94 //===----------------------------------------------------------------------===//
96 User::User(const Type *Ty, ValueTy vty, const string &name)
97 : Value(Ty, vty, name) {
100 // replaceUsesOfWith - Replaces all references to the "From" definition with
101 // references to the "To" definition.
103 void User::replaceUsesOfWith(Value *From, Value *To) {
104 if (From == To) return; // Duh what?
106 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
107 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
108 // The side effects of this setOperand call include linking to
109 // "To", adding "this" to the uses list of To, and
110 // most importantly, removing "this" from the use list of "From".
111 setOperand(i, To); // Fix it now...
116 //===----------------------------------------------------------------------===//
118 //===----------------------------------------------------------------------===//
120 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
121 assert(ValueParent && "SymTavValue without parent!?!");
122 ParentSymTab = SymTab = 0;
126 SymTabValue::~SymTabValue() {
130 void SymTabValue::setParentSymTab(SymbolTable *ST) {
133 SymTab->setParentSymTab(ST);
136 SymbolTable *SymTabValue::getSymbolTableSure() {
137 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
141 // hasSymbolTable() - Returns true if there is a symbol table allocated to
142 // this object AND if there is at least one name in it!
144 bool SymTabValue::hasSymbolTable() const {
145 if (!SymTab) return false;
147 for (SymbolTable::const_iterator I = SymTab->begin();
148 I != SymTab->end(); ++I) {
149 if (I->second.begin() != I->second.end())
150 return true; // Found nonempty type plane!