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"
12 #ifndef NDEBUG // Only in -g mode...
13 #include "llvm/Assembly/Writer.h"
19 //===----------------------------------------------------------------------===//
21 //===----------------------------------------------------------------------===//
23 static inline const Type *checkType(const Type *Ty) {
24 assert(Ty && "Value defined with a null type: Error!");
28 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
29 : Name(name), Ty(checkType(ty), this) {
34 #ifndef NDEBUG // Only in -g mode...
35 // Check to make sure that there are no uses of this value that are still
36 // around when the value is destroyed. If there are, then we have a dangling
37 // reference and something is wrong. This code is here to print out what is
38 // still being referenced. The value in question should be printed as
41 if (Uses.begin() != Uses.end()) {
42 cerr << "While deleting: " << this;
43 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
44 cerr << "Use still stuck around after Def is destroyed:" << *I << "\n";
47 assert(Uses.begin() == Uses.end());
50 void Value::replaceAllUsesWith(Value *D) {
51 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
52 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
53 while (!Uses.empty()) {
54 User *Use = Uses.back();
56 unsigned NumUses = Uses.size();
58 Use->replaceUsesOfWith(this, D);
60 #ifndef NDEBUG // only in -g mode...
61 if (Uses.size() == NumUses)
62 cerr << "Use: " << Use << "replace with: " << D;
64 assert(Uses.size() != NumUses && "Didn't remove definition!");
68 // refineAbstractType - This function is implemented because we use
69 // potentially abstract types, and these types may be resolved to more
70 // concrete types after we are constructed. For the value class, we simply
71 // change Ty to point to the right type. :)
73 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
74 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
75 if (OldTy == NewTy && !OldTy->isAbstract())
76 Ty.removeUserFromConcrete();
80 void Value::killUse(User *i) {
82 use_iterator I = find(Uses.begin(), Uses.end(), i);
84 assert(I != Uses.end() && "Use not in uses list!!");
88 User *Value::use_remove(use_iterator &I) {
89 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
95 #ifndef NDEBUG // Only in -g mode...
96 void Value::dump() const {
101 //===----------------------------------------------------------------------===//
103 //===----------------------------------------------------------------------===//
105 User::User(const Type *Ty, ValueTy vty, const std::string &name)
106 : Value(Ty, vty, name) {
109 // replaceUsesOfWith - Replaces all references to the "From" definition with
110 // references to the "To" definition.
112 void User::replaceUsesOfWith(Value *From, Value *To) {
113 if (From == To) return; // Duh what?
115 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
116 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
117 // The side effects of this setOperand call include linking to
118 // "To", adding "this" to the uses list of To, and
119 // most importantly, removing "this" from the use list of "From".
120 setOperand(i, To); // Fix it now...
125 //===----------------------------------------------------------------------===//
127 //===----------------------------------------------------------------------===//
129 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
130 assert(ValueParent && "SymTavValue without parent!?!");
131 ParentSymTab = SymTab = 0;
135 SymTabValue::~SymTabValue() {
139 void SymTabValue::setParentSymTab(SymbolTable *ST) {
142 SymTab->setParentSymTab(ST);
145 SymbolTable *SymTabValue::getSymbolTableSure() {
146 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
150 // hasSymbolTable() - Returns true if there is a symbol table allocated to
151 // this object AND if there is at least one name in it!
153 bool SymTabValue::hasSymbolTable() const {
154 if (!SymTab) return false;
156 for (SymbolTable::const_iterator I = SymTab->begin();
157 I != SymTab->end(); ++I) {
158 if (I->second.begin() != I->second.end())
159 return true; // Found nonempty type plane!