2001-06-06 20:29:01 +00:00
|
|
|
//===-- Value.cpp - Implement the Value class -----------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2005-04-21 23:48:37 +00:00
|
|
|
// This file implements the Value and User classes.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-03-05 19:51:50 +00:00
|
|
|
#include "llvm/Constant.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/InstrTypes.h"
|
2005-03-05 19:51:50 +00:00
|
|
|
#include "llvm/Module.h"
|
2007-02-05 20:47:22 +00:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2006-11-17 08:03:48 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <algorithm>
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-12-13 00:41:27 +00:00
|
|
|
static inline const Type *checkType(const Type *Ty) {
|
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2007-02-12 05:18:08 +00:00
|
|
|
Value::Value(const Type *ty, unsigned scid)
|
2005-02-05 01:37:58 +00:00
|
|
|
: SubclassID(scid), SubclassData(0), Ty(checkType(ty)),
|
2007-02-12 05:18:08 +00:00
|
|
|
UseList(0), Name(0) {
|
2004-07-06 17:44:17 +00:00
|
|
|
if (!isa<Constant>(this) && !isa<BasicBlock>(this))
|
2005-04-21 23:48:37 +00:00
|
|
|
assert((Ty->isFirstClassType() || Ty == Type::VoidTy ||
|
2004-07-07 18:07:46 +00:00
|
|
|
isa<OpaqueType>(ty)) &&
|
2004-07-06 17:44:17 +00:00
|
|
|
"Cannot create non-first-class values except for constants!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::~Value() {
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-06-11 15:04:40 +00:00
|
|
|
// Check to make sure that there are no uses of this value that are still
|
|
|
|
// around when the value is destroyed. If there are, then we have a dangling
|
|
|
|
// reference and something is wrong. This code is here to print out what is
|
2005-04-21 23:48:37 +00:00
|
|
|
// still being referenced. The value in question should be printed as
|
2001-06-11 15:04:40 +00:00
|
|
|
// a <badref>
|
|
|
|
//
|
2007-10-03 19:26:29 +00:00
|
|
|
if (!use_empty()) {
|
2006-11-17 08:03:48 +00:00
|
|
|
DOUT << "While deleting: " << *Ty << " %" << Name << "\n";
|
2005-02-01 01:24:21 +00:00
|
|
|
for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
|
2006-11-17 08:03:48 +00:00
|
|
|
DOUT << "Use still stuck around after Def is destroyed:"
|
|
|
|
<< **I << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-10-03 19:26:29 +00:00
|
|
|
assert(use_empty() && "Uses remain when a value is destroyed!");
|
2002-09-08 18:59:35 +00:00
|
|
|
|
2007-03-20 00:18:10 +00:00
|
|
|
// If this value is named, destroy the name. This should not be in a symtab
|
|
|
|
// at this point.
|
|
|
|
if (Name)
|
|
|
|
Name->Destroy();
|
|
|
|
|
2002-09-08 18:59:35 +00:00
|
|
|
// There should be no uses of this object anymore, remove it.
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2005-02-01 01:24:21 +00:00
|
|
|
/// hasNUses - Return true if this Value has exactly N users.
|
|
|
|
///
|
|
|
|
bool Value::hasNUses(unsigned N) const {
|
|
|
|
use_const_iterator UI = use_begin(), E = use_end();
|
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
return UI == E;
|
|
|
|
}
|
|
|
|
|
2005-02-23 16:51:11 +00:00
|
|
|
/// hasNUsesOrMore - Return true if this value has N users or more. This is
|
|
|
|
/// logically equivalent to getNumUses() >= N.
|
|
|
|
///
|
|
|
|
bool Value::hasNUsesOrMore(unsigned N) const {
|
|
|
|
use_const_iterator UI = use_begin(), E = use_end();
|
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 01:24:21 +00:00
|
|
|
/// getNumUses - This method computes the number of uses of this Value. This
|
|
|
|
/// is a linear time operation. Use hasOneUse or hasNUses to check for specific
|
|
|
|
/// values.
|
|
|
|
unsigned Value::getNumUses() const {
|
|
|
|
return (unsigned)std::distance(use_begin(), use_end());
|
|
|
|
}
|
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
|
2007-02-11 19:12:18 +00:00
|
|
|
ST = 0;
|
2007-02-11 00:37:27 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
2005-03-05 19:51:50 +00:00
|
|
|
if (BasicBlock *P = I->getParent())
|
|
|
|
if (Function *PP = P->getParent())
|
2007-01-06 07:24:44 +00:00
|
|
|
ST = &PP->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
|
2007-02-05 20:47:22 +00:00
|
|
|
if (Function *P = BB->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2007-02-05 20:47:22 +00:00
|
|
|
if (Module *P = GV->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(V)) {
|
2007-02-05 20:47:22 +00:00
|
|
|
if (Function *P = A->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2005-03-05 19:51:50 +00:00
|
|
|
} else {
|
2007-02-11 00:37:27 +00:00
|
|
|
assert(isa<Constant>(V) && "Unknown value type!");
|
|
|
|
return true; // no name is setable for this.
|
2005-03-05 19:51:50 +00:00
|
|
|
}
|
2007-02-11 00:37:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-10 15:34:35 +00:00
|
|
|
/// getNameStart - Return a pointer to a null terminated string for this name.
|
|
|
|
/// Note that names can have null characters within the string as well as at
|
|
|
|
/// their end. This always returns a non-null pointer.
|
|
|
|
const char *Value::getNameStart() const {
|
|
|
|
if (Name == 0) return "";
|
|
|
|
return Name->getKeyData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getNameLen - Return the length of the string, correctly handling nul
|
|
|
|
/// characters embedded into them.
|
|
|
|
unsigned Value::getNameLen() const {
|
2007-09-28 20:09:40 +00:00
|
|
|
return Name ? Name->getKeyLength() : 0;
|
2007-08-10 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-15 18:53:54 +00:00
|
|
|
std::string Value::getNameStr() const {
|
2007-02-12 05:18:08 +00:00
|
|
|
if (Name == 0) return "";
|
|
|
|
return std::string(Name->getKeyData(),
|
|
|
|
Name->getKeyData()+Name->getKeyLength());
|
|
|
|
}
|
2007-02-11 00:37:27 +00:00
|
|
|
|
2007-02-12 05:18:08 +00:00
|
|
|
void Value::setName(const std::string &name) {
|
2007-02-12 18:52:59 +00:00
|
|
|
setName(&name[0], name.size());
|
|
|
|
}
|
|
|
|
|
2007-02-13 07:53:34 +00:00
|
|
|
void Value::setName(const char *Name) {
|
|
|
|
setName(Name, Name ? strlen(Name) : 0);
|
|
|
|
}
|
|
|
|
|
2007-02-12 18:52:59 +00:00
|
|
|
void Value::setName(const char *NameStr, unsigned NameLen) {
|
|
|
|
if (NameLen == 0 && !hasName()) return;
|
2007-03-05 00:00:42 +00:00
|
|
|
assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
|
2007-02-12 05:18:08 +00:00
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
// Get the symbol table to update for this object.
|
|
|
|
ValueSymbolTable *ST;
|
|
|
|
if (getSymTab(this, ST))
|
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
2005-03-05 19:51:50 +00:00
|
|
|
|
2007-02-12 05:18:08 +00:00
|
|
|
if (!ST) { // No symbol table to update? Just do the change.
|
2007-02-12 18:52:59 +00:00
|
|
|
if (NameLen == 0) {
|
2007-02-12 05:18:08 +00:00
|
|
|
// Free the name for this value.
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
2007-02-12 18:52:59 +00:00
|
|
|
return;
|
2005-03-06 02:14:28 +00:00
|
|
|
}
|
2007-02-12 18:52:59 +00:00
|
|
|
|
|
|
|
if (Name) {
|
|
|
|
// Name isn't changing?
|
|
|
|
if (NameLen == Name->getKeyLength() &&
|
|
|
|
!memcmp(Name->getKeyData(), NameStr, NameLen))
|
|
|
|
return;
|
|
|
|
Name->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
|
|
|
|
|
|
|
// Create the new name.
|
|
|
|
Name = ValueName::Create(NameStr, NameStr+NameLen);
|
|
|
|
Name->setValue(this);
|
2007-02-12 05:18:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
|
|
|
if (hasName()) {
|
|
|
|
// Name isn't changing?
|
2007-02-12 18:52:59 +00:00
|
|
|
if (NameLen == Name->getKeyLength() &&
|
|
|
|
!memcmp(Name->getKeyData(), NameStr, NameLen))
|
2007-02-12 05:18:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove old name.
|
|
|
|
ST->removeValueName(Name);
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
|
|
|
|
2007-02-12 18:52:59 +00:00
|
|
|
if (NameLen == 0)
|
|
|
|
return;
|
2005-03-06 02:14:28 +00:00
|
|
|
}
|
2007-02-12 05:18:08 +00:00
|
|
|
|
|
|
|
// Name is changing to something new.
|
2007-02-12 18:52:59 +00:00
|
|
|
Name = ST->createValueName(NameStr, NameLen, this);
|
2005-03-05 19:51:50 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 18:52:59 +00:00
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
/// takeName - transfer the name from V to this value, setting V's name to
|
|
|
|
/// empty. It is an error to call V->takeName(V).
|
|
|
|
void Value::takeName(Value *V) {
|
2007-02-15 20:01:43 +00:00
|
|
|
ValueSymbolTable *ST = 0;
|
|
|
|
// If this value has a name, drop it.
|
|
|
|
if (hasName()) {
|
|
|
|
// Get the symtab this is in.
|
|
|
|
if (getSymTab(this, ST)) {
|
|
|
|
// We can't set a name on this value, but we need to clear V's name if
|
|
|
|
// it has one.
|
|
|
|
if (V->hasName()) V->setName(0, 0);
|
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old name.
|
|
|
|
if (ST)
|
|
|
|
ST->removeValueName(Name);
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know that this has no name.
|
|
|
|
|
|
|
|
// If V has no name either, we're done.
|
|
|
|
if (!V->hasName()) return;
|
|
|
|
|
|
|
|
// Get this's symtab if we didn't before.
|
|
|
|
if (!ST) {
|
|
|
|
if (getSymTab(this, ST)) {
|
|
|
|
// Clear V's name.
|
|
|
|
V->setName(0, 0);
|
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get V's ST, this should always succed, because V has a name.
|
|
|
|
ValueSymbolTable *VST;
|
|
|
|
bool Failure = getSymTab(V, VST);
|
|
|
|
assert(!Failure && "V has a name, so it should have a ST!");
|
|
|
|
|
|
|
|
// If these values are both in the same symtab, we can do this very fast.
|
|
|
|
// This works even if both values have no symtab yet.
|
|
|
|
if (ST == VST) {
|
|
|
|
// Take the name!
|
|
|
|
Name = V->Name;
|
|
|
|
V->Name = 0;
|
|
|
|
Name->setValue(this);
|
2007-02-11 01:04:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Otherwise, things are slightly more complex. Remove V's name from VST and
|
|
|
|
// then reinsert it into ST.
|
|
|
|
|
|
|
|
if (VST)
|
|
|
|
VST->removeValueName(V->Name);
|
|
|
|
Name = V->Name;
|
|
|
|
V->Name = 0;
|
|
|
|
Name->setValue(this);
|
|
|
|
|
|
|
|
if (ST)
|
|
|
|
ST->reinsertValue(this);
|
2007-02-11 00:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 05:09:37 +00:00
|
|
|
// uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
|
|
|
|
// except that it doesn't have all of the asserts. The asserts fail because we
|
|
|
|
// are half-way done resolving types, which causes some types to exist as two
|
|
|
|
// different Type*'s at the same time. This is a sledgehammer to work around
|
|
|
|
// this problem.
|
|
|
|
//
|
|
|
|
void Value::uncheckedReplaceAllUsesWith(Value *New) {
|
2005-02-01 01:24:21 +00:00
|
|
|
while (!use_empty()) {
|
|
|
|
Use &U = *UseList;
|
2003-08-29 05:09:37 +00:00
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
2007-08-21 00:21:07 +00:00
|
|
|
// constant because they are uniqued.
|
2003-10-16 16:53:07 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(U.getUser())) {
|
2007-08-21 00:21:07 +00:00
|
|
|
if (!isa<GlobalValue>(C)) {
|
2005-10-04 18:13:04 +00:00
|
|
|
C->replaceUsesOfWithOnConstant(this, New, &U);
|
2007-08-21 00:21:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-08-29 05:09:37 +00:00
|
|
|
}
|
2007-08-21 00:21:07 +00:00
|
|
|
|
|
|
|
U.set(New);
|
2003-08-29 05:09:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-16 16:53:07 +00:00
|
|
|
void Value::replaceAllUsesWith(Value *New) {
|
|
|
|
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
|
|
|
|
assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
|
|
|
|
assert(New->getType() == getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
2003-08-29 05:09:37 +00:00
|
|
|
|
2003-10-16 16:53:07 +00:00
|
|
|
uncheckedReplaceAllUsesWith(New);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// User Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// replaceUsesOfWith - Replaces all references to the "From" definition with
|
|
|
|
// references to the "To" definition.
|
|
|
|
//
|
|
|
|
void User::replaceUsesOfWith(Value *From, Value *To) {
|
|
|
|
if (From == To) return; // Duh what?
|
|
|
|
|
2004-07-18 00:01:50 +00:00
|
|
|
assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
|
2002-10-09 23:12:59 +00:00
|
|
|
"Cannot call User::replaceUsesofWith on a constant!");
|
|
|
|
|
2001-07-07 08:36:50 +00:00
|
|
|
for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
|
|
|
|
if (getOperand(i) == From) { // Is This operand is pointing to oldval?
|
2001-06-06 20:29:01 +00:00
|
|
|
// The side effects of this setOperand call include linking to
|
|
|
|
// "To", adding "this" to the uses list of To, and
|
|
|
|
// most importantly, removing "this" from the use list of "From".
|
2001-07-07 08:36:50 +00:00
|
|
|
setOperand(i, To); // Fix it now...
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-18 00:01:50 +00:00
|
|
|
|