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::getName() 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) {
194 std::string Name = V->getName();
200 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
201 // except that it doesn't have all of the asserts. The asserts fail because we
202 // are half-way done resolving types, which causes some types to exist as two
203 // different Type*'s at the same time. This is a sledgehammer to work around
206 void Value::uncheckedReplaceAllUsesWith(Value *New) {
207 while (!use_empty()) {
209 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
211 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
212 if (!isa<GlobalValue>(C))
213 C->replaceUsesOfWithOnConstant(this, New, &U);
222 void Value::replaceAllUsesWith(Value *New) {
223 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
224 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
225 assert(New->getType() == getType() &&
226 "replaceAllUses of value with new value of different type!");
228 uncheckedReplaceAllUsesWith(New);
231 //===----------------------------------------------------------------------===//
233 //===----------------------------------------------------------------------===//
235 // replaceUsesOfWith - Replaces all references to the "From" definition with
236 // references to the "To" definition.
238 void User::replaceUsesOfWith(Value *From, Value *To) {
239 if (From == To) return; // Duh what?
241 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
242 "Cannot call User::replaceUsesofWith on a constant!");
244 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
245 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
246 // The side effects of this setOperand call include linking to
247 // "To", adding "this" to the uses list of To, and
248 // most importantly, removing "this" from the use list of "From".
249 setOperand(i, To); // Fix it now...