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)
34 : SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
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!");
43 #ifndef NDEBUG // Only in -g mode...
44 // Check to make sure that there are no uses of this value that are still
45 // around when the value is destroyed. If there are, then we have a dangling
46 // reference and something is wrong. This code is here to print out what is
47 // still being referenced. The value in question should be printed as
50 if (use_begin() != use_end()) {
51 DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
52 for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
53 DOUT << "Use still stuck around after Def is destroyed:"
57 assert(use_begin() == use_end() && "Uses remain when a value is destroyed!");
59 // There should be no uses of this object anymore, remove it.
60 LeakDetector::removeGarbageObject(this);
63 /// hasNUses - Return true if this Value has exactly N users.
65 bool Value::hasNUses(unsigned N) const {
66 use_const_iterator UI = use_begin(), E = use_end();
69 if (UI == E) return false; // Too few.
73 /// hasNUsesOrMore - Return true if this value has N users or more. This is
74 /// logically equivalent to getNumUses() >= N.
76 bool Value::hasNUsesOrMore(unsigned N) const {
77 use_const_iterator UI = use_begin(), E = use_end();
80 if (UI == E) return false; // Too few.
86 /// getNumUses - This method computes the number of uses of this Value. This
87 /// is a linear time operation. Use hasOneUse or hasNUses to check for specific
89 unsigned Value::getNumUses() const {
90 return (unsigned)std::distance(use_begin(), use_end());
93 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
95 if (Instruction *I = dyn_cast<Instruction>(V)) {
96 if (BasicBlock *P = I->getParent())
97 if (Function *PP = P->getParent())
98 ST = &PP->getValueSymbolTable();
99 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
100 if (Function *P = BB->getParent())
101 ST = &P->getValueSymbolTable();
102 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
103 if (Module *P = GV->getParent())
104 ST = &P->getValueSymbolTable();
105 } else if (Argument *A = dyn_cast<Argument>(V)) {
106 if (Function *P = A->getParent())
107 ST = &P->getValueSymbolTable();
109 assert(isa<Constant>(V) && "Unknown value type!");
110 return true; // no name is setable for this.
115 std::string Value::getNameStr() const {
116 if (Name == 0) return "";
117 return std::string(Name->getKeyData(),
118 Name->getKeyData()+Name->getKeyLength());
121 void Value::setName(const std::string &name) {
122 setName(&name[0], name.size());
125 void Value::setName(const char *Name) {
126 setName(Name, Name ? strlen(Name) : 0);
129 void Value::setName(const char *NameStr, unsigned NameLen) {
130 if (NameLen == 0 && !hasName()) return;
131 if (getType() != Type::VoidTy && "Cannot assign a name to void values!");
133 // Get the symbol table to update for this object.
134 ValueSymbolTable *ST;
135 if (getSymTab(this, ST))
136 return; // Cannot set a name on this value (e.g. constant).
138 if (!ST) { // No symbol table to update? Just do the change.
140 // Free the name for this value.
147 // Name isn't changing?
148 if (NameLen == Name->getKeyLength() &&
149 !memcmp(Name->getKeyData(), NameStr, NameLen))
154 // NOTE: Could optimize for the case the name is shrinking to not deallocate
157 // Create the new name.
158 Name = ValueName::Create(NameStr, NameStr+NameLen);
159 Name->setValue(this);
163 // NOTE: Could optimize for the case the name is shrinking to not deallocate
166 // Name isn't changing?
167 if (NameLen == Name->getKeyLength() &&
168 !memcmp(Name->getKeyData(), NameStr, NameLen))
172 ST->removeValueName(Name);
180 // Name is changing to something new.
181 Name = ST->createValueName(NameStr, NameLen, this);
185 /// takeName - transfer the name from V to this value, setting V's name to
186 /// empty. It is an error to call V->takeName(V).
187 void Value::takeName(Value *V) {
188 ValueSymbolTable *ST = 0;
189 // If this value has a name, drop it.
191 // Get the symtab this is in.
192 if (getSymTab(this, ST)) {
193 // We can't set a name on this value, but we need to clear V's name if
195 if (V->hasName()) V->setName(0, 0);
196 return; // Cannot set a name on this value (e.g. constant).
201 ST->removeValueName(Name);
206 // Now we know that this has no name.
208 // If V has no name either, we're done.
209 if (!V->hasName()) return;
211 // Get this's symtab if we didn't before.
213 if (getSymTab(this, ST)) {
216 return; // Cannot set a name on this value (e.g. constant).
220 // Get V's ST, this should always succed, because V has a name.
221 ValueSymbolTable *VST;
222 bool Failure = getSymTab(V, VST);
223 assert(!Failure && "V has a name, so it should have a ST!");
225 // If these values are both in the same symtab, we can do this very fast.
226 // This works even if both values have no symtab yet.
231 Name->setValue(this);
235 // Otherwise, things are slightly more complex. Remove V's name from VST and
236 // then reinsert it into ST.
239 VST->removeValueName(V->Name);
242 Name->setValue(this);
245 ST->reinsertValue(this);
249 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
250 // except that it doesn't have all of the asserts. The asserts fail because we
251 // are half-way done resolving types, which causes some types to exist as two
252 // different Type*'s at the same time. This is a sledgehammer to work around
255 void Value::uncheckedReplaceAllUsesWith(Value *New) {
256 while (!use_empty()) {
258 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
260 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
261 if (!isa<GlobalValue>(C))
262 C->replaceUsesOfWithOnConstant(this, New, &U);
271 void Value::replaceAllUsesWith(Value *New) {
272 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
273 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
274 assert(New->getType() == getType() &&
275 "replaceAllUses of value with new value of different type!");
277 uncheckedReplaceAllUsesWith(New);
280 //===----------------------------------------------------------------------===//
282 //===----------------------------------------------------------------------===//
284 // replaceUsesOfWith - Replaces all references to the "From" definition with
285 // references to the "To" definition.
287 void User::replaceUsesOfWith(Value *From, Value *To) {
288 if (From == To) return; // Duh what?
290 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
291 "Cannot call User::replaceUsesofWith on a constant!");
293 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
294 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
295 // The side effects of this setOperand call include linking to
296 // "To", adding "this" to the uses list of To, and
297 // most importantly, removing "this" from the use list of "From".
298 setOperand(i, To); // Fix it now...