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"
10 #include "llvm/Constant.h"
11 #include "Support/LeakDetector.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)) {
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: " << *Ty << "%" << Name << "\n";
38 for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
39 std::cerr << "Use still stuck around after Def is destroyed:"
43 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
45 // There should be no uses of this object anymore, remove it.
46 LeakDetector::removeGarbageObject(this);
52 void Value::replaceAllUsesWith(Value *New) {
53 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
54 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
55 assert(New->getType() == getType() &&
56 "replaceAllUses of value with new value of different type!");
57 while (!Uses.empty()) {
58 User *Use = Uses.back();
59 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
61 if (Constant *C = dyn_cast<Constant>(Use)) {
62 C->replaceUsesOfWithOnConstant(this, New);
64 Use->replaceUsesOfWith(this, New);
69 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
70 // except that it doesn't have all of the asserts. The asserts fail because we
71 // are half-way done resolving types, which causes some types to exist as two
72 // different Type*'s at the same time. This is a sledgehammer to work around
75 void Value::uncheckedReplaceAllUsesWith(Value *New) {
76 while (!Uses.empty()) {
77 User *Use = Uses.back();
78 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
80 if (Constant *C = dyn_cast<Constant>(Use)) {
81 C->replaceUsesOfWithOnConstant(this, New, true);
83 Use->replaceUsesOfWith(this, New);
89 void Value::killUse(User *U) {
90 assert(U != 0 && "Null users are not allowed!");
93 // Scan backwards through the uses list looking for the user. We do this
94 // because vectors like to be accessed on the end. This is incredibly
95 // important from a performance perspective.
96 for (i = Uses.size()-1; Uses[i] != U; --i)
99 assert(i < Uses.size() && "Use not in uses list!!");
100 Uses[i] = Uses.back();
104 //===----------------------------------------------------------------------===//
106 //===----------------------------------------------------------------------===//
108 User::User(const Type *Ty, ValueTy vty, const std::string &name)
109 : Value(Ty, vty, name) {
112 // replaceUsesOfWith - Replaces all references to the "From" definition with
113 // references to the "To" definition.
115 void User::replaceUsesOfWith(Value *From, Value *To) {
116 if (From == To) return; // Duh what?
118 assert(!isa<Constant>(this) &&
119 "Cannot call User::replaceUsesofWith on a constant!");
121 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
122 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
123 // The side effects of this setOperand call include linking to
124 // "To", adding "this" to the uses list of To, and
125 // most importantly, removing "this" from the use list of "From".
126 setOperand(i, To); // Fix it now...