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/ConstPoolVals.h"
12 #include "llvm/Type.h"
13 #ifndef NDEBUG // Only in -g mode...
14 #include "llvm/Assembly/Writer.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 Value::Value(const Type *ty, ValueTy vty, const string &name = "")
23 : Name(name), Ty(ty, this) {
28 #ifndef NDEBUG // Only in -g mode...
29 // 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
35 if (Uses.begin() != Uses.end()) {
36 cerr << "While deleting: " << this;
37 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
38 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
41 assert(Uses.begin() == Uses.end());
44 void Value::replaceAllUsesWith(Value *D) {
45 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
46 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
47 while (!Uses.empty()) {
48 User *Use = Uses.back();
50 unsigned NumUses = Uses.size();
52 Use->replaceUsesOfWith(this, D);
54 #ifndef NDEBUG // only in -g mode...
55 if (Uses.size() == NumUses)
56 cerr << "Use: " << Use << "replace with: " << D;
58 assert(Uses.size() != NumUses && "Didn't remove definition!");
62 // 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. :)
67 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
68 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
72 void Value::killUse(User *i) {
74 use_iterator I = find(Uses.begin(), Uses.end(), i);
76 assert(I != Uses.end() && "Use not in uses list!!");
80 User *Value::use_remove(use_iterator &I) {
81 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
87 #ifndef NDEBUG // Only in -g mode...
88 void Value::dump() const {
93 //===----------------------------------------------------------------------===//
95 //===----------------------------------------------------------------------===//
97 User::User(const Type *Ty, ValueTy vty, const string &name)
98 : Value(Ty, vty, name) {
101 // replaceUsesOfWith - Replaces all references to the "From" definition with
102 // references to the "To" definition.
104 void User::replaceUsesOfWith(Value *From, Value *To) {
105 if (From == To) return; // Duh what?
107 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
108 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
109 // 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".
112 setOperand(i, To); // Fix it now...
117 //===----------------------------------------------------------------------===//
119 //===----------------------------------------------------------------------===//
121 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
122 assert(ValueParent && "SymTavValue without parent!?!");
123 ParentSymTab = SymTab = 0;
127 SymTabValue::~SymTabValue() {
131 void SymTabValue::setParentSymTab(SymbolTable *ST) {
134 SymTab->setParentSymTab(ST);
137 SymbolTable *SymTabValue::getSymbolTableSure() {
138 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
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!
145 bool SymTabValue::hasSymbolTable() const {
146 if (!SymTab) return false;
148 for (SymbolTable::const_iterator I = SymTab->begin();
149 I != SymTab->end(); ++I) {
150 if (I->second.begin() != I->second.end())
151 return true; // Found nonempty type plane!