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 if (name.empty() && !hasName()) return;
123 if (getType() != Type::VoidTy && "Cannot assign a name to void values!");
126 // Get the symbol table to update for this object.
127 ValueSymbolTable *ST;
128 if (getSymTab(this, ST))
129 return; // Cannot set a name on this value (e.g. constant).
131 if (!ST) { // No symbol table to update? Just do the change.
133 // Free the name for this value.
138 // Name isn't changing.
139 if (name.size() == Name->getKeyLength() &&
140 !memcmp(Name->getKeyData(), &name[0], name.size()))
145 // Create the new name.
146 Name = ValueName::Create(&name[0], &name[name.size()]);
147 Name->setValue(this);
152 // NOTE: Could optimize for the case the name is shrinking to not deallocate
155 // Name isn't changing?
156 if (name.size() == Name->getKeyLength() &&
157 !memcmp(Name->getKeyData(), &name[0], name.size()))
161 ST->removeValueName(Name);
169 // Name is changing to something new.
170 Name = ST->createValueName(&name[0], name.size(), this);
173 /// takeName - transfer the name from V to this value, setting V's name to
174 /// empty. It is an error to call V->takeName(V).
175 void Value::takeName(Value *V) {
182 std::string Name = V->getName();
188 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
189 // except that it doesn't have all of the asserts. The asserts fail because we
190 // are half-way done resolving types, which causes some types to exist as two
191 // different Type*'s at the same time. This is a sledgehammer to work around
194 void Value::uncheckedReplaceAllUsesWith(Value *New) {
195 while (!use_empty()) {
197 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
199 if (Constant *C = dyn_cast<Constant>(U.getUser())) {
200 if (!isa<GlobalValue>(C))
201 C->replaceUsesOfWithOnConstant(this, New, &U);
210 void Value::replaceAllUsesWith(Value *New) {
211 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
212 assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
213 assert(New->getType() == getType() &&
214 "replaceAllUses of value with new value of different type!");
216 uncheckedReplaceAllUsesWith(New);
219 //===----------------------------------------------------------------------===//
221 //===----------------------------------------------------------------------===//
223 // replaceUsesOfWith - Replaces all references to the "From" definition with
224 // references to the "To" definition.
226 void User::replaceUsesOfWith(Value *From, Value *To) {
227 if (From == To) return; // Duh what?
229 assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
230 "Cannot call User::replaceUsesofWith on a constant!");
232 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
233 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
234 // The side effects of this setOperand call include linking to
235 // "To", adding "this" to the uses list of To, and
236 // most importantly, removing "this" from the use list of "From".
237 setOperand(i, To); // Fix it now...