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/ConstantPool.h"
12 #include "llvm/ConstPoolVals.h"
13 #include "llvm/Type.h"
14 #ifndef NDEBUG // Only in -g mode...
15 #include "llvm/Assembly/Writer.h"
19 //===----------------------------------------------------------------------===//
21 //===----------------------------------------------------------------------===//
23 Value::Value(const Type *ty, ValueTy vty, const string &name = "") : Name(name){
29 #ifndef NDEBUG // Only in -g mode...
30 // Check to make sure that there are no uses of this value that are still
31 // around when the value is destroyed. If there are, then we have a dangling
32 // reference and something is wrong. This code is here to print out what is
33 // still being referenced. The value in question should be printed as
36 if (Uses.begin() != Uses.end()) {
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.front();
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 void Value::killUse(User *i) {
64 use_iterator I = find(Uses.begin(), Uses.end(), i);
66 assert(I != Uses.end() && "Use not in uses list!!");
70 User *Value::use_remove(use_iterator &I) {
71 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
82 User::User(const Type *Ty, ValueTy vty, const string &name)
83 : Value(Ty, vty, name) {
86 // replaceUsesOfWith - Replaces all references to the "From" definition with
87 // references to the "To" definition.
89 void User::replaceUsesOfWith(Value *From, Value *To) {
90 if (From == To) return; // Duh what?
92 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
93 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
94 // The side effects of this setOperand call include linking to
95 // "To", adding "this" to the uses list of To, and
96 // most importantly, removing "this" from the use list of "From".
97 setOperand(i, To); // Fix it now...
102 //===----------------------------------------------------------------------===//
104 //===----------------------------------------------------------------------===//
106 // Instantiate Templates - This ugliness is the price we have to pay
107 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
109 template class ValueHolder<ConstPoolVal, SymTabValue, SymTabValue>;
111 SymTabValue::SymTabValue(Value *p) : ConstPool(this), ValueParent(p) {
112 assert(ValueParent && "SymTavValue without parent!?!");
113 ParentSymTab = SymTab = 0;
117 SymTabValue::~SymTabValue() {
118 ConstPool.dropAllReferences();
119 ConstPool.delete_all();
120 ConstPool.setParent(0);
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!