1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // This file implements the Value and User classes.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/InstrTypes.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/DerivedTypes.h"
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
16 static inline const Type *checkType(const Type *Ty) {
17 assert(Ty && "Value defined with a null type: Error!");
21 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
22 : Name(name), Ty(checkType(ty), this) {
27 #ifndef NDEBUG // Only in -g mode...
28 // Check to make sure that there are no uses of this value that are still
29 // around when the value is destroyed. If there are, then we have a dangling
30 // reference and something is wrong. This code is here to print out what is
31 // still being referenced. The value in question should be printed as
34 if (Uses.begin() != Uses.end()) {
35 std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
36 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
37 std::cerr << "Use still stuck around after Def is destroyed:";
43 assert(Uses.begin() == Uses.end());
46 void Value::replaceAllUsesWith(Value *D) {
47 assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
48 assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
49 assert(D->getType() == getType() &&
50 "replaceAllUses of value with new value of different type!");
51 while (!Uses.empty()) {
52 User *Use = Uses.back();
54 unsigned NumUses = Uses.size();
56 Use->replaceUsesOfWith(this, D);
58 #ifndef NDEBUG // only in -g mode...
59 if (Uses.size() == NumUses) {
62 std::cerr << "replace with: ";
66 assert(Uses.size() != NumUses && "Didn't remove definition!");
70 // refineAbstractType - This function is implemented because we use
71 // potentially abstract types, and these types may be resolved to more
72 // concrete types after we are constructed. For the value class, we simply
73 // change Ty to point to the right type. :)
75 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
76 assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
77 if (OldTy == NewTy && !OldTy->isAbstract())
78 Ty.removeUserFromConcrete();
82 void Value::killUse(User *i) {
84 use_iterator I = find(Uses.begin(), Uses.end(), i);
86 assert(I != Uses.end() && "Use not in uses list!!");
90 User *Value::use_remove(use_iterator &I) {
91 assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 User::User(const Type *Ty, ValueTy vty, const std::string &name)
102 : Value(Ty, vty, name) {
105 // replaceUsesOfWith - Replaces all references to the "From" definition with
106 // references to the "To" definition.
108 void User::replaceUsesOfWith(Value *From, Value *To) {
109 if (From == To) return; // Duh what?
111 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
112 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
113 // The side effects of this setOperand call include linking to
114 // "To", adding "this" to the uses list of To, and
115 // most importantly, removing "this" from the use list of "From".
116 setOperand(i, To); // Fix it now...