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