1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Value and User classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/InstrTypes.h"
15 #include "llvm/SymbolTable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Constant.h"
18 #include "Support/LeakDetector.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 static inline const Type *checkType(const Type *Ty) {
28 assert(Ty && "Value defined with a null type: Error!");
32 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
33 : Name(name), Ty(checkType(ty)) {
38 #ifndef NDEBUG // Only in -g mode...
39 // Check to make sure that there are no uses of this value that are still
40 // around when the value is destroyed. If there are, then we have a dangling
41 // reference and something is wrong. This code is here to print out what is
42 // still being referenced. The value in question should be printed as
45 if (Uses.begin() != Uses.end()) {
46 std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
47 for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
48 std::cerr << "Use still stuck around after Def is destroyed:"
52 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
54 // There should be no uses of this object anymore, remove it.
55 LeakDetector::removeGarbageObject(this);
59 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
60 // except that it doesn't have all of the asserts. The asserts fail because we
61 // are half-way done resolving types, which causes some types to exist as two
62 // different Type*'s at the same time. This is a sledgehammer to work around
65 void Value::uncheckedReplaceAllUsesWith(Value *New) {
66 while (!Uses.empty()) {
68 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
70 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
71 C->replaceUsesOfWithOnConstant(this, New, true);
78 void Value::replaceAllUsesWith(Value *New) {
79 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
80 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
81 assert(New->getType() == getType() &&
82 "replaceAllUses of value with new value of different type!");
84 uncheckedReplaceAllUsesWith(New);
87 //===----------------------------------------------------------------------===//
89 //===----------------------------------------------------------------------===//
91 User::User(const Type *Ty, ValueTy vty, const std::string &name)
92 : Value(Ty, vty, name) {
95 // replaceUsesOfWith - Replaces all references to the "From" definition with
96 // references to the "To" definition.
98 void User::replaceUsesOfWith(Value *From, Value *To) {
99 if (From == To) return; // Duh what?
101 assert(!isa<Constant>(this) &&
102 "Cannot call User::replaceUsesofWith on a constant!");
104 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
105 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
106 // The side effects of this setOperand call include linking to
107 // "To", adding "this" to the uses list of To, and
108 // most importantly, removing "this" from the use list of "From".
109 setOperand(i, To); // Fix it now...
113 } // End llvm namespace