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/Constant.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/ValueSymbolTable.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/LeakDetector.h"
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 static inline const Type *checkType(const Type *Ty) {
29 assert(Ty && "Value defined with a null type: Error!");
33 Value::Value(const Type *ty, unsigned scid, const std::string &name)
34 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
35 UseList(0), Name(name) {
36 if (!isa<Constant>(this) && !isa<BasicBlock>(this))
37 assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
38 isa<OpaqueType>(ty)) &&
39 "Cannot create non-first-class values except for constants!");
40 if (ty == Type::VoidTy)
41 assert(name.empty() && "Cannot have named void values!");
45 #ifndef NDEBUG // Only in -g mode...
46 // Check to make sure that there are no uses of this value that are still
47 // around when the value is destroyed. If there are, then we have a dangling
48 // reference and something is wrong. This code is here to print out what is
49 // still being referenced. The value in question should be printed as
52 if (use_begin() != use_end()) {
53 DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
54 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
55 DOUT << "Use still stuck around after Def is destroyed:"
59 assert(use_begin() == use_end() && "Uses remain when a value is destroyed!");
61 // There should be no uses of this object anymore, remove it.
62 LeakDetector::removeGarbageObject(this);
65 /// hasNUses - Return true if this Value has exactly N users.
67 bool Value::hasNUses(unsigned N) const {
68 use_const_iterator UI = use_begin(), E = use_end();
71 if (UI == E) return false; // Too few.
75 /// hasNUsesOrMore - Return true if this value has N users or more. This is
76 /// logically equivalent to getNumUses() >= N.
78 bool Value::hasNUsesOrMore(unsigned N) const {
79 use_const_iterator UI = use_begin(), E = use_end();
82 if (UI == E) return false; // Too few.
88 /// getNumUses - This method computes the number of uses of this Value. This
89 /// is a linear time operation. Use hasOneUse or hasNUses to check for specific
91 unsigned Value::getNumUses() const {
92 return (unsigned)std::distance(use_begin(), use_end());
95 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
97 if (Instruction *I = dyn_cast<Instruction>(V)) {
98 if (BasicBlock *P = I->getParent())
99 if (Function *PP = P->getParent())
100 ST = &PP->getValueSymbolTable();
101 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
102 if (Function *P = BB->getParent())
103 ST = &P->getValueSymbolTable();
104 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
105 if (Module *P = GV->getParent())
106 ST = &P->getValueSymbolTable();
107 } else if (Argument *A = dyn_cast<Argument>(V)) {
108 if (Function *P = A->getParent())
109 ST = &P->getValueSymbolTable();
111 assert(isa<Constant>(V) && "Unknown value type!");
112 return true; // no name is setable for this.
117 void Value::setName(const std::string &name) {
118 if (Name == name) return; // Name is already set.
120 // Get the symbol table to update for this object.
121 ValueSymbolTable *ST;
122 if (getSymTab(this, ST))
123 return; // Cannot set a name on this value (e.g. constant).
125 if (!ST) // No symbol table to update? Just do the change.
127 else if (hasName()) {
128 if (!name.empty()) { // Replacing name.
132 } else { // Transitioning from hasName -> noname.
136 } else { // Transitioning from noname -> hasName.
142 /// takeName - transfer the name from V to this value, setting V's name to
143 /// empty. It is an error to call V->takeName(V).
144 void Value::takeName(Value *V) {
151 std::string Name = V->getName();
157 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
158 // except that it doesn't have all of the asserts. The asserts fail because we
159 // are half-way done resolving types, which causes some types to exist as two
160 // different Type*'s at the same time. This is a sledgehammer to work around
163 void Value::uncheckedReplaceAllUsesWith(Value *New) {
164 while (!use_empty()) {
166 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
168 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
169 if (!isa<GlobalValue>(C))
170 C->replaceUsesOfWithOnConstant(this, New, &U);
179 void Value::replaceAllUsesWith(Value *New) {
180 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
181 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
182 assert(New->getType() == getType() &&
183 "replaceAllUses of value with new value of different type!");
185 uncheckedReplaceAllUsesWith(New);
188 //===----------------------------------------------------------------------===//
190 //===----------------------------------------------------------------------===//
192 // replaceUsesOfWith - Replaces all references to the "From" definition with
193 // references to the "To" definition.
195 void User::replaceUsesOfWith(Value *From, Value *To) {
196 if (From == To) return; // Duh what?
198 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
199 "Cannot call User::replaceUsesofWith on a constant!");
201 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
202 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
203 // The side effects of this setOperand call include linking to
204 // "To", adding "this" to the uses list of To, and
205 // most importantly, removing "this" from the use list of "From".
206 setOperand(i, To); // Fix it now...