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(), E = Uses.end(); I != E; ++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);
50 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
51 // except that it doesn't have all of the asserts. The asserts fail because we
52 // are half-way done resolving types, which causes some types to exist as two
53 // different Type*'s at the same time. This is a sledgehammer to work around
56 void Value::uncheckedReplaceAllUsesWith(Value *New) {
57 while (!Uses.empty()) {
59 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
61 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
62 C->replaceUsesOfWithOnConstant(this, New);
69 void Value::replaceAllUsesWith(Value *New) {
70 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
71 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
72 assert(New->getType() == getType() &&
73 "replaceAllUses of value with new value of different type!");
75 uncheckedReplaceAllUsesWith(New);
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
82 User::User(const Type *Ty, ValueTy vty, const std::string &name)
83 : Value(Ty, vty, name) {
86 // replaceUsesOfWith - Replaces all references to the "From" definition with
87 // references to the "To" definition.
89 void User::replaceUsesOfWith(Value *From, Value *To) {
90 if (From == To) return; // Duh what?
92 assert(!isa<Constant>(this) &&
93 "Cannot call User::replaceUsesofWith on a constant!");
95 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
96 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
97 // The side effects of this setOperand call include linking to
98 // "To", adding "this" to the uses list of To, and
99 // most importantly, removing "this" from the use list of "From".
100 setOperand(i, To); // Fix it now...