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 static inline const Type *checkType(const Type *Ty) {
22 assert(Ty && "Value defined with a null type: Error!");
26 Value::Value(const Type *ty, ValueTy vty, const string &name = "")
27 : Name(name), Ty(checkType(ty), this) {
32 #ifndef NDEBUG // Only in -g mode...
33 // Check to make sure that there are no uses of this value that are still
34 // around when the value is destroyed. If there are, then we have a dangling
35 // reference and something is wrong. This code is here to print out what is
36 // still being referenced. The value in question should be printed as
39 if (Uses.begin() != Uses.end()) {
40 cerr << "While deleting: " << this;
41 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
42 cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
45 assert(Uses.begin() == Uses.end());
48 void Value::replaceAllUsesWith(Value *D) {
49 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
50 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
51 while (!Uses.empty()) {
52 User *Use = Uses.back();
54 unsigned NumUses = Uses.size();
56 Use->replaceUsesOfWith(this, D);
58 #ifndef NDEBUG // only in -g mode...
59 if (Uses.size() == NumUses)
60 cerr << "Use: " << Use << "replace with: " << D;
62 assert(Uses.size() != NumUses && "Didn't remove definition!");
66 // refineAbstractType - This function is implemented because we use
67 // potentially abstract types, and these types may be resolved to more
68 // concrete types after we are constructed. For the value class, we simply
69 // change Ty to point to the right type. :)
71 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
72 assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
76 void Value::killUse(User *i) {
78 use_iterator I = find(Uses.begin(), Uses.end(), i);
80 assert(I != Uses.end() && "Use not in uses list!!");
84 User *Value::use_remove(use_iterator &I) {
85 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
91 #ifndef NDEBUG // Only in -g mode...
92 void Value::dump() const {
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 User::User(const Type *Ty, ValueTy vty, const string &name)
102 : Value(Ty, vty, name) {
105 // replaceUsesOfWith - Replaces all references to the "From" definition with
106 // references to the "To" definition.
108 void User::replaceUsesOfWith(Value *From, Value *To) {
109 if (From == To) return; // Duh what?
111 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
112 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
113 // The side effects of this setOperand call include linking to
114 // "To", adding "this" to the uses list of To, and
115 // most importantly, removing "this" from the use list of "From".
116 setOperand(i, To); // Fix it now...
121 //===----------------------------------------------------------------------===//
123 //===----------------------------------------------------------------------===//
125 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
126 assert(ValueParent && "SymTavValue without parent!?!");
127 ParentSymTab = SymTab = 0;
131 SymTabValue::~SymTabValue() {
135 void SymTabValue::setParentSymTab(SymbolTable *ST) {
138 SymTab->setParentSymTab(ST);
141 SymbolTable *SymTabValue::getSymbolTableSure() {
142 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
146 // hasSymbolTable() - Returns true if there is a symbol table allocated to
147 // this object AND if there is at least one name in it!
149 bool SymTabValue::hasSymbolTable() const {
150 if (!SymTab) return false;
152 for (SymbolTable::const_iterator I = SymTab->begin();
153 I != SymTab->end(); ++I) {
154 if (I->second.begin() != I->second.end())
155 return true; // Found nonempty type plane!