1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // This file implements the Value, User, and SymTabValue classes.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/InstrTypes.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/SymTabValue.h"
10 #include "llvm/DerivedTypes.h"
13 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
17 static inline const Type *checkType(const Type *Ty) {
18 assert(Ty && "Value defined with a null type: Error!");
22 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
23 : Name(name), Ty(checkType(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 std::cerr << "While deleting: ";
38 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
39 std::cerr << "Use still stuck around after Def is destroyed:";
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 assert(D->getType() == getType() &&
52 "replaceAllUses of value with new value of different type!");
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) {
64 std::cerr << "replace with: ";
68 assert(Uses.size() != NumUses && "Didn't remove definition!");
72 // refineAbstractType - This function is implemented because we use
73 // potentially abstract types, and these types may be resolved to more
74 // concrete types after we are constructed. For the value class, we simply
75 // change Ty to point to the right type. :)
77 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
78 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
79 if (OldTy == NewTy && !OldTy->isAbstract())
80 Ty.removeUserFromConcrete();
84 void Value::killUse(User *i) {
86 use_iterator I = find(Uses.begin(), Uses.end(), i);
88 assert(I != Uses.end() && "Use not in uses list!!");
92 User *Value::use_remove(use_iterator &I) {
93 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
99 //===----------------------------------------------------------------------===//
101 //===----------------------------------------------------------------------===//
103 User::User(const Type *Ty, ValueTy vty, const std::string &name)
104 : Value(Ty, vty, name) {
107 // replaceUsesOfWith - Replaces all references to the "From" definition with
108 // references to the "To" definition.
110 void User::replaceUsesOfWith(Value *From, Value *To) {
111 if (From == To) return; // Duh what?
113 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
114 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
115 // The side effects of this setOperand call include linking to
116 // "To", adding "this" to the uses list of To, and
117 // most importantly, removing "this" from the use list of "From".
118 setOperand(i, To); // Fix it now...
123 //===----------------------------------------------------------------------===//
125 //===----------------------------------------------------------------------===//
127 SymTabValue::SymTabValue(Value *p) : ValueParent(p) {
128 assert(ValueParent && "SymTavValue without parent!?!");
129 ParentSymTab = SymTab = 0;
133 SymTabValue::~SymTabValue() {
137 void SymTabValue::setParentSymTab(SymbolTable *ST) {
140 SymTab->setParentSymTab(ST);
143 SymbolTable *SymTabValue::getSymbolTableSure() {
144 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
148 // hasSymbolTable() - Returns true if there is a symbol table allocated to
149 // this object AND if there is at least one name in it!
151 bool SymTabValue::hasSymbolTable() const {
152 if (!SymTab) return false;
154 for (SymbolTable::const_iterator I = SymTab->begin();
155 I != SymTab->end(); ++I) {
156 if (I->second.begin() != I->second.end())
157 return true; // Found nonempty type plane!