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"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 static inline const Type *checkType(const Type *Ty) {
27 assert(Ty && "Value defined with a null type: Error!");
31 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
32 : Name(name), Ty(checkType(ty)) {
37 #ifndef NDEBUG // Only in -g mode...
38 // Check to make sure that there are no uses of this value that are still
39 // around when the value is destroyed. If there are, then we have a dangling
40 // reference and something is wrong. This code is here to print out what is
41 // still being referenced. The value in question should be printed as
44 if (Uses.begin() != Uses.end()) {
45 std::cerr << "While deleting: " << *Ty << "%" << Name << "\n";
46 for (use_const_iterator I = Uses.begin(), E = Uses.end(); I != E; ++I)
47 std::cerr << "Use still stuck around after Def is destroyed:"
51 assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
53 // There should be no uses of this object anymore, remove it.
54 LeakDetector::removeGarbageObject(this);
58 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
59 // except that it doesn't have all of the asserts. The asserts fail because we
60 // are half-way done resolving types, which causes some types to exist as two
61 // different Type*'s at the same time. This is a sledgehammer to work around
64 void Value::uncheckedReplaceAllUsesWith(Value *New) {
65 while (!Uses.empty()) {
67 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
69 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
70 C->replaceUsesOfWithOnConstant(this, New, true);
77 void Value::replaceAllUsesWith(Value *New) {
78 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
79 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
80 assert(New->getType() == getType() &&
81 "replaceAllUses of value with new value of different type!");
83 uncheckedReplaceAllUsesWith(New);
86 //===----------------------------------------------------------------------===//
88 //===----------------------------------------------------------------------===//
90 User::User(const Type *Ty, ValueTy vty, const std::string &name)
91 : Value(Ty, vty, name) {
94 // replaceUsesOfWith - Replaces all references to the "From" definition with
95 // references to the "To" definition.
97 void User::replaceUsesOfWith(Value *From, Value *To) {
98 if (From == To) return; // Duh what?
100 assert(!isa<Constant>(this) &&
101 "Cannot call User::replaceUsesofWith on a constant!");
103 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
104 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
105 // The side effects of this setOperand call include linking to
106 // "To", adding "this" to the uses list of To, and
107 // most importantly, removing "this" from the use list of "From".
108 setOperand(i, To); // Fix it now...