2001-06-07 04:29:01 +08:00
|
|
|
//===-- Value.cpp - Implement the Value class -----------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
2009-04-01 06:11:05 +08:00
|
|
|
// This file implements the Value, ValueHandle, and User classes.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-03-06 03:51:50 +08:00
|
|
|
#include "llvm/Constant.h"
|
2008-05-07 06:52:30 +08:00
|
|
|
#include "llvm/Constants.h"
|
2005-03-06 03:51:50 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/InstrTypes.h"
|
2008-02-21 09:54:02 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2009-07-18 06:25:10 +08:00
|
|
|
#include "llvm/Operator.h"
|
2005-03-06 03:51:50 +08:00
|
|
|
#include "llvm/Module.h"
|
2009-07-29 05:49:47 +08:00
|
|
|
#include "llvm/Metadata.h"
|
2007-02-06 04:47:22 +08:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2009-07-25 12:41:11 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2006-11-17 16:03:48 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-09 02:01:40 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2009-04-01 06:11:05 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-09 04:53:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-06-18 01:36:57 +08:00
|
|
|
#include "llvm/System/RWMutex.h"
|
2009-06-19 00:54:52 +08:00
|
|
|
#include "llvm/System/Threading.h"
|
2009-04-01 06:11:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include <algorithm>
|
2003-11-22 04:23:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-12-13 08:41:27 +08:00
|
|
|
static inline const Type *checkType(const Type *Ty) {
|
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2007-02-12 13:18:08 +08:00
|
|
|
Value::Value(const Type *ty, unsigned scid)
|
2009-07-18 01:16:59 +08:00
|
|
|
: SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
|
|
|
|
SubclassData(0), VTy(checkType(ty)),
|
2008-09-19 23:13:20 +08:00
|
|
|
UseList(0), Name(0) {
|
2008-02-21 10:14:01 +08:00
|
|
|
if (isa<CallInst>(this) || isa<InvokeInst>(this))
|
2008-07-24 08:08:56 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
|
|
|
|
isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) &&
|
2008-02-21 09:54:02 +08:00
|
|
|
"invalid CallInst type!");
|
|
|
|
else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
|
2008-07-24 08:08:56 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
|
2004-07-08 02:07:46 +08:00
|
|
|
isa<OpaqueType>(ty)) &&
|
2004-07-07 01:44:17 +08:00
|
|
|
"Cannot create non-first-class values except for constants!");
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2007-12-10 10:14:30 +08:00
|
|
|
Value::~Value() {
|
2009-04-01 06:11:05 +08:00
|
|
|
// Notify all ValueHandles (if present) that this value is going away.
|
|
|
|
if (HasValueHandle)
|
|
|
|
ValueHandleBase::ValueIsDeleted(this);
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-06-11 23:04:40 +08: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-22 07:48:37 +08:00
|
|
|
// still being referenced. The value in question should be printed as
|
2001-06-11 23:04:40 +08:00
|
|
|
// a <badref>
|
|
|
|
//
|
2007-12-10 10:14:30 +08:00
|
|
|
if (!use_empty()) {
|
2009-07-24 18:05:20 +08:00
|
|
|
errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
|
2007-12-10 10:14:30 +08:00
|
|
|
for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
|
2009-07-24 18:05:20 +08:00
|
|
|
errs() << "Use still stuck around after Def is destroyed:"
|
2006-11-17 16:03:48 +08:00
|
|
|
<< **I << "\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
#endif
|
2007-12-10 10:14:30 +08:00
|
|
|
assert(use_empty() && "Uses remain when a value is destroyed!");
|
2009-08-05 07:07:12 +08:00
|
|
|
|
|
|
|
// If this value is named, destroy the name. This should not be in a symtab
|
|
|
|
// at this point.
|
|
|
|
if (Name)
|
|
|
|
Name->Destroy();
|
|
|
|
|
|
|
|
// There should be no uses of this object anymore, remove it.
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2005-02-01 09:24:21 +08: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-24 00:51:11 +08: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;
|
|
|
|
}
|
|
|
|
|
2008-06-13 05:15:59 +08:00
|
|
|
/// isUsedInBasicBlock - Return true if this value is used in the specified
|
|
|
|
/// basic block.
|
2008-09-26 06:42:01 +08:00
|
|
|
bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
|
2008-06-13 05:15:59 +08:00
|
|
|
for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
|
|
|
|
const Instruction *User = dyn_cast<Instruction>(*I);
|
|
|
|
if (User && User->getParent() == BB)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-24 00:51:11 +08:00
|
|
|
|
2005-02-01 09:24:21 +08: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 08:37:27 +08:00
|
|
|
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
|
2007-02-12 03:12:18 +08:00
|
|
|
ST = 0;
|
2007-02-11 08:37:27 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
2005-03-06 03:51:50 +08:00
|
|
|
if (BasicBlock *P = I->getParent())
|
|
|
|
if (Function *PP = P->getParent())
|
2007-01-06 15:24:44 +08:00
|
|
|
ST = &PP->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
|
2007-02-06 04:47:22 +08:00
|
|
|
if (Function *P = BB->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2007-02-06 04:47:22 +08:00
|
|
|
if (Module *P = GV->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(V)) {
|
2007-02-06 04:47:22 +08:00
|
|
|
if (Function *P = A->getParent())
|
|
|
|
ST = &P->getValueSymbolTable();
|
2009-07-30 01:16:17 +08:00
|
|
|
} else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
|
|
|
|
if (Module *P = N->getParent()) {
|
|
|
|
ST = &P->getValueSymbolTable();
|
|
|
|
}
|
2009-07-23 01:43:22 +08:00
|
|
|
} else if (isa<MDString>(V))
|
|
|
|
return true;
|
|
|
|
else {
|
2007-02-11 08:37:27 +08:00
|
|
|
assert(isa<Constant>(V) && "Unknown value type!");
|
|
|
|
return true; // no name is setable for this.
|
2005-03-06 03:51:50 +08:00
|
|
|
}
|
2007-02-11 08:37:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-26 08:51:56 +08:00
|
|
|
StringRef Value::getName() const {
|
2009-07-26 17:22:02 +08:00
|
|
|
// Make sure the empty string is still a C string. For historical reasons,
|
|
|
|
// some clients want to call .data() on the result and expect it to be null
|
|
|
|
// terminated.
|
|
|
|
if (!Name) return StringRef("", 0);
|
2009-07-26 08:51:56 +08:00
|
|
|
return Name->getKey();
|
2007-08-10 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
2007-02-16 02:53:54 +08:00
|
|
|
std::string Value::getNameStr() const {
|
2009-07-26 08:17:14 +08:00
|
|
|
return getName().str();
|
2007-02-12 13:18:08 +08:00
|
|
|
}
|
2007-02-11 08:37:27 +08:00
|
|
|
|
2009-07-26 08:34:27 +08:00
|
|
|
void Value::setName(const Twine &NewName) {
|
2009-07-25 12:41:11 +08:00
|
|
|
SmallString<32> NameData;
|
2009-07-26 08:34:27 +08:00
|
|
|
NewName.toVector(NameData);
|
|
|
|
|
|
|
|
const char *NameStr = NameData.data();
|
|
|
|
unsigned NameLen = NameData.size();
|
2007-02-13 02:52:59 +08:00
|
|
|
|
2009-07-26 08:42:33 +08:00
|
|
|
// Name isn't changing?
|
|
|
|
if (getName() == StringRef(NameStr, NameLen))
|
|
|
|
return;
|
|
|
|
|
2007-03-05 08:00:42 +08:00
|
|
|
assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
|
2007-02-12 13:18:08 +08:00
|
|
|
|
2007-02-11 08:37:27 +08: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-06 03:51:50 +08:00
|
|
|
|
2007-02-12 13:18:08 +08:00
|
|
|
if (!ST) { // No symbol table to update? Just do the change.
|
2007-02-13 02:52:59 +08:00
|
|
|
if (NameLen == 0) {
|
2007-02-12 13:18:08 +08:00
|
|
|
// Free the name for this value.
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
2007-02-13 02:52:59 +08:00
|
|
|
return;
|
2005-03-06 10:14:28 +08:00
|
|
|
}
|
2007-02-13 02:52:59 +08:00
|
|
|
|
2009-07-26 08:42:33 +08:00
|
|
|
if (Name)
|
2007-02-13 02:52:59 +08:00
|
|
|
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 13:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
|
|
|
if (hasName()) {
|
|
|
|
// Remove old name.
|
|
|
|
ST->removeValueName(Name);
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
|
|
|
|
2007-02-13 02:52:59 +08:00
|
|
|
if (NameLen == 0)
|
|
|
|
return;
|
2005-03-06 10:14:28 +08:00
|
|
|
}
|
2007-02-12 13:18:08 +08:00
|
|
|
|
|
|
|
// Name is changing to something new.
|
2009-07-24 02:50:53 +08:00
|
|
|
Name = ST->createValueName(StringRef(NameStr, NameLen), this);
|
2005-03-06 03:51:50 +08:00
|
|
|
}
|
|
|
|
|
2007-02-13 02:52:59 +08:00
|
|
|
|
2007-02-11 08:37:27 +08: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-16 04:01:43 +08: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.
|
2009-07-26 08:34:27 +08:00
|
|
|
if (V->hasName()) V->setName("");
|
2007-02-16 04:01:43 +08:00
|
|
|
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.
|
2009-07-26 08:34:27 +08:00
|
|
|
V->setName("");
|
2007-02-16 04:01:43 +08:00
|
|
|
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);
|
2008-06-22 03:47:03 +08:00
|
|
|
assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
|
2007-02-16 04:01:43 +08:00
|
|
|
|
|
|
|
// 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 09:04:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-16 04:01:43 +08: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 08:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 13:09:37 +08: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) {
|
2009-04-01 06:11:05 +08:00
|
|
|
// Notify all ValueHandles (if present) that this value is going away.
|
|
|
|
if (HasValueHandle)
|
|
|
|
ValueHandleBase::ValueIsRAUWd(this, New);
|
|
|
|
|
2005-02-01 09:24:21 +08:00
|
|
|
while (!use_empty()) {
|
2008-09-19 23:13:20 +08:00
|
|
|
Use &U = *UseList;
|
2003-08-29 13:09:37 +08:00
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
2007-08-21 08:21:07 +08:00
|
|
|
// constant because they are uniqued.
|
2003-10-17 00:53:07 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(U.getUser())) {
|
2007-08-21 08:21:07 +08:00
|
|
|
if (!isa<GlobalValue>(C)) {
|
2005-10-05 02:13:04 +08:00
|
|
|
C->replaceUsesOfWithOnConstant(this, New, &U);
|
2007-08-21 08:21:07 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
2007-08-21 08:21:07 +08:00
|
|
|
|
|
|
|
U.set(New);
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-17 00:53:07 +08: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 13:09:37 +08:00
|
|
|
|
2003-10-17 00:53:07 +08:00
|
|
|
uncheckedReplaceAllUsesWith(New);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2008-05-08 06:54:15 +08:00
|
|
|
Value *Value::stripPointerCasts() {
|
2008-10-01 23:25:41 +08:00
|
|
|
if (!isa<PointerType>(getType()))
|
|
|
|
return this;
|
2008-12-30 05:06:19 +08:00
|
|
|
Value *V = this;
|
|
|
|
do {
|
2009-07-18 07:55:56 +08:00
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
2008-12-30 05:06:19 +08:00
|
|
|
if (!GEP->hasAllZeroIndices())
|
|
|
|
return V;
|
2009-07-18 07:55:56 +08:00
|
|
|
V = GEP->getPointerOperand();
|
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
|
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2008-12-30 05:06:19 +08:00
|
|
|
} else {
|
|
|
|
return V;
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
2008-12-30 05:06:19 +08:00
|
|
|
assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
|
|
|
|
} while (1);
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
|
|
|
|
2008-10-01 23:25:41 +08:00
|
|
|
Value *Value::getUnderlyingObject() {
|
|
|
|
if (!isa<PointerType>(getType()))
|
|
|
|
return this;
|
2008-12-30 05:06:19 +08:00
|
|
|
Value *V = this;
|
2009-04-15 14:23:41 +08:00
|
|
|
unsigned MaxLookup = 6;
|
2008-12-30 05:06:19 +08:00
|
|
|
do {
|
2009-07-18 07:55:56 +08:00
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
|
|
|
V = GEP->getPointerOperand();
|
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
|
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2008-12-30 05:06:19 +08:00
|
|
|
} else {
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
|
2009-04-15 14:23:41 +08:00
|
|
|
} while (--MaxLookup);
|
|
|
|
return V;
|
2008-10-01 23:25:41 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 02:33:11 +08:00
|
|
|
/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
|
2008-12-02 15:16:45 +08:00
|
|
|
/// return the value in the PHI node corresponding to PredBB. If not, return
|
|
|
|
/// ourself. This is useful if you want to know the value something has in a
|
|
|
|
/// predecessor block.
|
|
|
|
Value *Value::DoPHITranslation(const BasicBlock *CurBB,
|
|
|
|
const BasicBlock *PredBB) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(this);
|
|
|
|
if (PN && PN->getParent() == CurBB)
|
|
|
|
return PN->getIncomingValueForBlock(PredBB);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2009-07-22 08:24:57 +08:00
|
|
|
LLVMContext &Value::getContext() const { return VTy->getContext(); }
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueHandleBase Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ValueHandles - This map keeps track of all of the value handles that are
|
|
|
|
/// watching a Value*. The Value::HasValueHandle bit is used to know whether or
|
|
|
|
/// not a value has an entry in this map.
|
|
|
|
typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
|
|
|
|
static ManagedStatic<ValueHandlesTy> ValueHandles;
|
2009-06-19 04:36:21 +08:00
|
|
|
static ManagedStatic<sys::SmartRWMutex<true> > ValueHandlesLock;
|
2009-04-01 06:11:05 +08:00
|
|
|
|
2009-05-05 01:25:21 +08:00
|
|
|
/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
|
|
|
|
/// List is known to point into the existing use list.
|
2009-04-01 06:11:05 +08:00
|
|
|
void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
|
|
|
|
assert(List && "Handle list is null?");
|
|
|
|
|
|
|
|
// Splice ourselves into the list.
|
|
|
|
Next = *List;
|
|
|
|
*List = this;
|
|
|
|
setPrevPtr(List);
|
|
|
|
if (Next) {
|
|
|
|
Next->setPrevPtr(&Next);
|
|
|
|
assert(VP == Next->VP && "Added to wrong list?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddToUseList - Add this ValueHandle to the use list for VP.
|
|
|
|
void ValueHandleBase::AddToUseList() {
|
|
|
|
assert(VP && "Null pointer doesn't have a use list!");
|
|
|
|
if (VP->HasValueHandle) {
|
|
|
|
// If this value already has a ValueHandle, then it must be in the
|
|
|
|
// ValueHandles map already.
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
|
2009-04-01 06:11:05 +08:00
|
|
|
ValueHandleBase *&Entry = (*ValueHandles)[VP];
|
|
|
|
assert(Entry != 0 && "Value doesn't have any handles?");
|
2009-06-18 01:36:57 +08:00
|
|
|
AddToExistingUseList(&Entry);
|
|
|
|
return;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, it doesn't have any handles yet, so we must insert it into the
|
|
|
|
// DenseMap. However, doing this insertion could cause the DenseMap to
|
|
|
|
// reallocate itself, which would invalidate all of the PrevP pointers that
|
|
|
|
// point into the old table. Handle this by checking for reallocation and
|
|
|
|
// updating the stale pointers only if needed.
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
|
2009-04-01 06:11:05 +08:00
|
|
|
ValueHandlesTy &Handles = *ValueHandles;
|
|
|
|
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
|
|
|
|
|
|
|
|
ValueHandleBase *&Entry = Handles[VP];
|
|
|
|
assert(Entry == 0 && "Value really did already have handles?");
|
|
|
|
AddToExistingUseList(&Entry);
|
2009-05-05 01:25:21 +08:00
|
|
|
VP->HasValueHandle = true;
|
2009-04-01 06:11:05 +08:00
|
|
|
|
|
|
|
// If reallocation didn't happen or if this was the first insertion, don't
|
|
|
|
// walk the table.
|
|
|
|
if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
|
2009-06-18 01:36:57 +08:00
|
|
|
Handles.size() == 1) {
|
2009-04-01 06:11:05 +08:00
|
|
|
return;
|
2009-06-18 01:36:57 +08:00
|
|
|
}
|
2009-04-01 06:11:05 +08:00
|
|
|
|
|
|
|
// Okay, reallocation did happen. Fix the Prev Pointers.
|
|
|
|
for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
assert(I->second && I->first == I->second->VP && "List invariant broken!");
|
|
|
|
I->second->setPrevPtr(&I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveFromUseList - Remove this ValueHandle from its current use list.
|
|
|
|
void ValueHandleBase::RemoveFromUseList() {
|
|
|
|
assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
|
|
|
|
|
|
|
|
// Unlink this from its use list.
|
|
|
|
ValueHandleBase **PrevPtr = getPrevPtr();
|
|
|
|
assert(*PrevPtr == this && "List invariant broken");
|
|
|
|
|
|
|
|
*PrevPtr = Next;
|
|
|
|
if (Next) {
|
|
|
|
assert(Next->getPrevPtr() == &Next && "List invariant broken");
|
|
|
|
Next->setPrevPtr(PrevPtr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Next pointer was null, then it is possible that this was the last
|
|
|
|
// ValueHandle watching VP. If so, delete its entry from the ValueHandles
|
|
|
|
// map.
|
2009-07-08 02:33:04 +08:00
|
|
|
sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
|
2009-04-01 06:11:05 +08:00
|
|
|
ValueHandlesTy &Handles = *ValueHandles;
|
|
|
|
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
|
|
|
|
Handles.erase(VP);
|
|
|
|
VP->HasValueHandle = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValueHandleBase::ValueIsDeleted(Value *V) {
|
|
|
|
assert(V->HasValueHandle && "Should only be called if ValueHandles present");
|
|
|
|
|
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-06-19 04:36:21 +08:00
|
|
|
ValueHandlesLock->reader_acquire();
|
2009-04-01 06:11:05 +08:00
|
|
|
ValueHandleBase *Entry = (*ValueHandles)[V];
|
2009-06-19 04:36:21 +08:00
|
|
|
ValueHandlesLock->reader_release();
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
|
|
|
|
|
|
|
while (Entry) {
|
|
|
|
// Advance pointer to avoid invalidation.
|
|
|
|
ValueHandleBase *ThisNode = Entry;
|
|
|
|
Entry = Entry->Next;
|
|
|
|
|
|
|
|
switch (ThisNode->getKind()) {
|
|
|
|
case Assert:
|
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2009-07-24 18:05:20 +08:00
|
|
|
errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
|
|
|
|
<< "\n";
|
2009-04-01 06:11:05 +08:00
|
|
|
#endif
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("An asserting value handle still pointed to this"
|
2009-07-09 06:09:00 +08:00
|
|
|
" value!");
|
2009-04-01 06:11:05 +08:00
|
|
|
case Weak:
|
|
|
|
// Weak just goes to null, which will unlink it from the list.
|
|
|
|
ThisNode->operator=(0);
|
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
|
|
|
static_cast<CallbackVH*>(ThisNode)->deleted();
|
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All callbacks and weak references should be dropped by now.
|
|
|
|
assert(!V->HasValueHandle && "All references to V were not removed?");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
|
|
|
|
assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
|
|
|
|
assert(Old != New && "Changing value into itself!");
|
|
|
|
|
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-06-19 04:36:21 +08:00
|
|
|
ValueHandlesLock->reader_acquire();
|
2009-04-01 06:11:05 +08:00
|
|
|
ValueHandleBase *Entry = (*ValueHandles)[Old];
|
2009-06-19 04:36:21 +08:00
|
|
|
ValueHandlesLock->reader_release();
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
|
|
|
|
|
|
|
while (Entry) {
|
|
|
|
// Advance pointer to avoid invalidation.
|
|
|
|
ValueHandleBase *ThisNode = Entry;
|
|
|
|
Entry = Entry->Next;
|
|
|
|
|
|
|
|
switch (ThisNode->getKind()) {
|
|
|
|
case Assert:
|
|
|
|
// Asserting handle does not follow RAUW implicitly.
|
|
|
|
break;
|
|
|
|
case Weak:
|
|
|
|
// Weak goes to the new value, which will unlink it from Old's list.
|
|
|
|
ThisNode->operator=(New);
|
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
|
|
|
static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New);
|
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-03 05:10:48 +08:00
|
|
|
/// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
|
|
|
|
/// more than once.
|
|
|
|
CallbackVH::~CallbackVH() {}
|
|
|
|
|
2008-12-02 15:16:45 +08:00
|
|
|
|
2001-06-07 04:29:01 +08: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?
|
|
|
|
|
2008-02-20 19:08:44 +08:00
|
|
|
assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
|
2009-08-11 23:53:15 +08:00
|
|
|
"Cannot call User::replaceUsesOfWith on a constant!");
|
2002-10-10 07:12:59 +08:00
|
|
|
|
2001-07-07 16:36:50 +08:00
|
|
|
for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
|
|
|
|
if (getOperand(i) == From) { // Is This operand is pointing to oldval?
|
2001-06-07 04:29:01 +08: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 16:36:50 +08:00
|
|
|
setOperand(i, To); // Fix it now...
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|