1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // This file implements the Value class.
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 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.front();
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!!!");
87 //===----------------------------------------------------------------------===//
89 //===----------------------------------------------------------------------===//
91 User::User(const Type *Ty, ValueTy vty, const string &name)
92 : Value(Ty, vty, name) {
95 // replaceUsesOfWith - Replaces all references to the "From" definition with
96 // references to the "To" definition.
98 void User::replaceUsesOfWith(Value *From, Value *To) {
99 if (From == To) return; // Duh what?
101 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
102 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
103 // The side effects of this setOperand call include linking to
104 // "To", adding "this" to the uses list of To, and
105 // most importantly, removing "this" from the use list of "From".
106 setOperand(i, To); // Fix it now...
111 //===----------------------------------------------------------------------===//
113 //===----------------------------------------------------------------------===//
115 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
116 assert(ValueParent && "SymTavValue without parent!?!");
117 ParentSymTab = SymTab = 0;
121 SymTabValue::~SymTabValue() {
125 void SymTabValue::setParentSymTab(SymbolTable *ST) {
128 SymTab->setParentSymTab(ST);
131 SymbolTable *SymTabValue::getSymbolTableSure() {
132 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
136 // hasSymbolTable() - Returns true if there is a symbol table allocated to
137 // this object AND if there is at least one name in it!
139 bool SymTabValue::hasSymbolTable() const {
140 if (!SymTab) return false;
142 for (SymbolTable::const_iterator I = SymTab->begin();
143 I != SymTab->end(); ++I) {
144 if (I->second.begin() != I->second.end())
145 return true; // Found nonempty type plane!