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/DerivedTypes.h"
14 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
18 static inline const Type *checkType(const Type *Ty) {
19 assert(Ty && "Value defined with a null type: Error!");
23 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
24 : Name(name), Ty(checkType(ty), this) {
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 std::cerr << "While deleting: ";
39 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
40 std::cerr << "Use still stuck around after Def is destroyed:";
46 assert(Uses.begin() == Uses.end());
49 void Value::replaceAllUsesWith(Value *D) {
50 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
51 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
52 assert(D->getType() == getType() &&
53 "replaceAllUses of value with new value of different type!");
54 while (!Uses.empty()) {
55 User *Use = Uses.back();
57 unsigned NumUses = Uses.size();
59 Use->replaceUsesOfWith(this, D);
61 #ifndef NDEBUG // only in -g mode...
62 if (Uses.size() == NumUses) {
65 std::cerr << "replace with: ";
69 assert(Uses.size() != NumUses && "Didn't remove definition!");
73 // refineAbstractType - This function is implemented because we use
74 // potentially abstract types, and these types may be resolved to more
75 // concrete types after we are constructed. For the value class, we simply
76 // change Ty to point to the right type. :)
78 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
79 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
80 if (OldTy == NewTy && !OldTy->isAbstract())
81 Ty.removeUserFromConcrete();
85 void Value::killUse(User *i) {
87 use_iterator I = find(Uses.begin(), Uses.end(), i);
89 assert(I != Uses.end() && "Use not in uses list!!");
93 User *Value::use_remove(use_iterator &I) {
94 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
100 //===----------------------------------------------------------------------===//
102 //===----------------------------------------------------------------------===//
104 User::User(const Type *Ty, ValueTy vty, const std::string &name)
105 : Value(Ty, vty, name) {
108 // replaceUsesOfWith - Replaces all references to the "From" definition with
109 // references to the "To" definition.
111 void User::replaceUsesOfWith(Value *From, Value *To) {
112 if (From == To) return; // Duh what?
114 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
115 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
116 // The side effects of this setOperand call include linking to
117 // "To", adding "this" to the uses list of To, and
118 // most importantly, removing "this" from the use list of "From".
119 setOperand(i, To); // Fix it now...
124 //===----------------------------------------------------------------------===//
126 //===----------------------------------------------------------------------===//
128 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
129 assert(ValueParent && "SymTavValue without parent!?!");
130 ParentSymTab = SymTab = 0;
134 SymTabValue::~SymTabValue() {
138 void SymTabValue::setParentSymTab(SymbolTable *ST) {
141 SymTab->setParentSymTab(ST);
144 SymbolTable *SymTabValue::getSymbolTableSure() {
145 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
149 // hasSymbolTable() - Returns true if there is a symbol table allocated to
150 // this object AND if there is at least one name in it!
152 bool SymTabValue::hasSymbolTable() const {
153 if (!SymTab) return false;
155 for (SymbolTable::const_iterator I = SymTab->begin();
156 I != SymTab->end(); ++I) {
157 if (I->second.begin() != I->second.end())
158 return true; // Found nonempty type plane!