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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
2009-08-19 02:28:58 +08:00
|
|
|
#include "LLVMContextImpl.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
|
|
|
#include "llvm/IR/ValueSymbolTable.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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.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"
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
static inline Type *checkType(Type *Ty) {
|
2001-12-13 08:41:27 +08:00
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
return const_cast<Type*>(Ty);
|
2001-12-13 08:41:27 +08:00
|
|
|
}
|
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
Value::Value(Type *ty, unsigned scid)
|
2009-12-29 07:41:32 +08:00
|
|
|
: SubclassID(scid), HasValueHandle(0),
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)),
|
2008-09-19 23:13:20 +08:00
|
|
|
UseList(0), Name(0) {
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
// FIXME: Why isn't this in the subclass gunk??
|
2012-12-20 12:11:02 +08:00
|
|
|
// Note, we cannot call isa<CallInst> before the CallInst has been
|
|
|
|
// constructed.
|
|
|
|
if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
|
|
|
|
"invalid CallInst type!");
|
2012-12-20 12:11:02 +08:00
|
|
|
else if (SubclassID != BasicBlockVal &&
|
|
|
|
(SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
|
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);
|
2009-09-20 12:03:34 +08:00
|
|
|
|
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()) {
|
2011-11-16 00:27:03 +08:00
|
|
|
dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
|
2007-12-10 10:14:30 +08:00
|
|
|
for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
|
2010-01-05 09:30:09 +08:00
|
|
|
dbgs() << "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.
|
2012-04-11 04:12:16 +08:00
|
|
|
if (Name && SubclassID != MDStringVal)
|
2009-08-05 07:07:12 +08:00
|
|
|
Name->Destroy();
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-08-05 07:07:12 +08:00
|
|
|
// 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 {
|
2010-03-26 07:06:16 +08:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-01 09:24:21 +08:00
|
|
|
|
|
|
|
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 {
|
2010-03-26 07:06:16 +08:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-24 00:51:11 +08:00
|
|
|
|
|
|
|
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 {
|
2011-12-06 01:23:27 +08:00
|
|
|
// Start by scanning over the instructions looking for a use before we start
|
|
|
|
// the expensive use iteration.
|
|
|
|
unsigned MaxBlockSize = 3;
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
|
|
|
if (std::find(I->op_begin(), I->op_end(), this) != I->op_end())
|
|
|
|
return true;
|
2013-04-12 16:33:11 +08:00
|
|
|
if (--MaxBlockSize == 0) // If the block is larger fall back to use_iterator
|
2011-12-06 01:23:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxBlockSize != 0) // We scanned the entire block and found no use.
|
|
|
|
return false;
|
|
|
|
|
2010-03-26 07:06:16 +08:00
|
|
|
for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
|
2008-06-13 05:15:59 +08:00
|
|
|
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)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Function *P = BB->getParent())
|
2007-02-06 04:47:22 +08:00
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Module *P = GV->getParent())
|
2007-02-06 04:47:22 +08:00
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(V)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Function *P = A->getParent())
|
2007-02-06 04:47:22 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-07-26 08:34:27 +08:00
|
|
|
void Value::setName(const Twine &NewName) {
|
2012-04-11 04:12:16 +08:00
|
|
|
assert(SubclassID != MDStringVal &&
|
|
|
|
"Cannot set the name of MDString with this method!");
|
|
|
|
|
2009-08-20 07:37:23 +08:00
|
|
|
// Fast path for common IRBuilder case of setName("") when there is no name.
|
|
|
|
if (NewName.isTriviallyEmpty() && !hasName())
|
|
|
|
return;
|
|
|
|
|
2009-08-19 13:08:06 +08:00
|
|
|
SmallString<256> NameData;
|
2010-01-13 20:45:23 +08:00
|
|
|
StringRef NameRef = NewName.toStringRef(NameData);
|
2007-02-13 02:52:59 +08:00
|
|
|
|
2009-07-26 08:42:33 +08:00
|
|
|
// Name isn't changing?
|
2010-01-13 20:45:23 +08:00
|
|
|
if (getName() == NameRef)
|
2009-07-26 08:42:33 +08:00
|
|
|
return;
|
|
|
|
|
2010-01-05 21:12:22 +08:00
|
|
|
assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
|
2009-09-20 12:03:34 +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
|
|
|
|
2013-03-02 02:48:54 +08:00
|
|
|
if (Function *F = dyn_cast<Function>(this))
|
|
|
|
getContext().pImpl->IntrinsicIDCache.erase(F);
|
|
|
|
|
2007-02-12 13:18:08 +08:00
|
|
|
if (!ST) { // No symbol table to update? Just do the change.
|
2010-01-13 20:45:23 +08:00
|
|
|
if (NameRef.empty()) {
|
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
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-07-26 08:42:33 +08:00
|
|
|
if (Name)
|
2007-02-13 02:52:59 +08:00
|
|
|
Name->Destroy();
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-13 02:52:59 +08:00
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-13 02:52:59 +08:00
|
|
|
// Create the new name.
|
2010-01-13 20:45:23 +08:00
|
|
|
Name = ValueName::Create(NameRef.begin(), NameRef.end());
|
2007-02-13 02:52:59 +08:00
|
|
|
Name->setValue(this);
|
2007-02-12 13:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-12 13:18:08 +08:00
|
|
|
// 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;
|
|
|
|
|
2010-01-13 20:45:23 +08:00
|
|
|
if (NameRef.empty())
|
2007-02-13 02:52:59 +08:00
|
|
|
return;
|
2005-03-06 10:14:28 +08:00
|
|
|
}
|
2007-02-12 13:18:08 +08:00
|
|
|
|
|
|
|
// Name is changing to something new.
|
2010-01-13 20:45:23 +08:00
|
|
|
Name = ST->createValueName(NameRef, 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
|
2009-09-20 12:03:34 +08:00
|
|
|
/// empty. It is an error to call V->takeName(V).
|
2007-02-11 08:37:27 +08:00
|
|
|
void Value::takeName(Value *V) {
|
2012-04-11 04:12:16 +08:00
|
|
|
assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
|
|
|
|
|
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).
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Remove old name.
|
|
|
|
if (ST)
|
|
|
|
ST->removeValueName(Name);
|
|
|
|
Name->Destroy();
|
|
|
|
Name = 0;
|
2009-09-20 12:03:34 +08:00
|
|
|
}
|
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Now we know that this has no name.
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// If V has no name either, we're done.
|
|
|
|
if (!V->hasName()) return;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// 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).
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Get V's ST, this should always succed, because V has a name.
|
|
|
|
ValueSymbolTable *VST;
|
|
|
|
bool Failure = getSymTab(V, VST);
|
2010-12-23 08:58:24 +08:00
|
|
|
assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
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;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
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.
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
if (VST)
|
|
|
|
VST->removeValueName(V->Name);
|
|
|
|
Name = V->Name;
|
|
|
|
V->Name = 0;
|
|
|
|
Name->setValue(this);
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
if (ST)
|
|
|
|
ST->reinsertValue(this);
|
2007-02-11 08:37:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-15 14:18:52 +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!");
|
|
|
|
|
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);
|
2013-03-02 02:48:54 +08:00
|
|
|
|
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
|
|
|
}
|
2013-03-02 02:48:54 +08:00
|
|
|
|
2007-08-21 08:21:07 +08:00
|
|
|
U.set(New);
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
2013-03-02 02:48:54 +08:00
|
|
|
|
2011-06-23 17:09:15 +08:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
|
|
|
|
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 16:39:09 +08:00
|
|
|
namespace {
|
|
|
|
// Various metrics for how much to strip off of pointers.
|
|
|
|
enum PointerStripKind {
|
|
|
|
PSK_ZeroIndices,
|
2012-03-15 07:19:53 +08:00
|
|
|
PSK_InBoundsConstantIndices,
|
|
|
|
PSK_InBounds
|
2012-03-10 16:39:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <PointerStripKind StripKind>
|
|
|
|
static Value *stripPointerCastsAndOffsets(Value *V) {
|
|
|
|
if (!V->getType()->isPointerTy())
|
|
|
|
return V;
|
2010-06-29 05:16:52 +08:00
|
|
|
|
|
|
|
// Even though we don't look through PHI nodes, we could be called on an
|
|
|
|
// instruction in an unreachable block, which may be on a cycle.
|
|
|
|
SmallPtrSet<Value *, 4> Visited;
|
|
|
|
|
|
|
|
Visited.insert(V);
|
2008-12-30 05:06:19 +08:00
|
|
|
do {
|
2009-07-18 07:55:56 +08:00
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
2012-03-10 16:39:09 +08:00
|
|
|
switch (StripKind) {
|
|
|
|
case PSK_ZeroIndices:
|
|
|
|
if (!GEP->hasAllZeroIndices())
|
|
|
|
return V;
|
|
|
|
break;
|
2012-03-15 07:19:53 +08:00
|
|
|
case PSK_InBoundsConstantIndices:
|
2012-03-10 16:39:09 +08:00
|
|
|
if (!GEP->hasAllConstantIndices())
|
|
|
|
return V;
|
2012-03-15 07:19:53 +08:00
|
|
|
// fallthrough
|
2012-03-10 16:39:09 +08:00
|
|
|
case PSK_InBounds:
|
|
|
|
if (!GEP->isInBounds())
|
|
|
|
return V;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-18 07:55:56 +08:00
|
|
|
V = GEP->getPointerOperand();
|
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
|
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2009-08-28 01:55:13 +08:00
|
|
|
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
|
|
|
|
if (GA->mayBeOverridden())
|
|
|
|
return V;
|
|
|
|
V = GA->getAliasee();
|
2008-12-30 05:06:19 +08:00
|
|
|
} else {
|
|
|
|
return V;
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
2010-02-16 19:11:14 +08:00
|
|
|
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
|
2010-06-29 05:16:52 +08:00
|
|
|
} while (Visited.insert(V));
|
|
|
|
|
|
|
|
return V;
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
2012-03-10 16:39:09 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Value *Value::stripPointerCasts() {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
|
|
|
|
}
|
|
|
|
|
2012-03-15 07:19:53 +08:00
|
|
|
Value *Value::stripInBoundsConstantOffsets() {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
|
2012-03-10 16:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *Value::stripInBoundsOffsets() {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
|
|
|
|
}
|
2008-05-08 06:54:15 +08:00
|
|
|
|
2010-11-12 05:23:25 +08:00
|
|
|
/// isDereferenceablePointer - Test if this value is always a pointer to
|
2010-11-12 05:51:44 +08:00
|
|
|
/// allocated and suitably aligned memory for a simple load or store.
|
2012-01-23 08:05:17 +08:00
|
|
|
static bool isDereferenceablePointer(const Value *V,
|
|
|
|
SmallPtrSet<const Value *, 32> &Visited) {
|
2010-11-12 05:23:25 +08:00
|
|
|
// Note that it is not safe to speculate into a malloc'd region because
|
|
|
|
// malloc may return null.
|
|
|
|
// It's also not always safe to follow a bitcast, for example:
|
|
|
|
// bitcast i8* (alloca i8) to i32*
|
|
|
|
// would result in a 4-byte load from a 1-byte alloca. Some cases could
|
2012-10-09 00:38:25 +08:00
|
|
|
// be handled using DataLayout to check sizes and alignments though.
|
2010-11-12 05:23:25 +08:00
|
|
|
|
|
|
|
// These are obviously ok.
|
2012-01-23 08:05:17 +08:00
|
|
|
if (isa<AllocaInst>(V)) return true;
|
2010-11-12 05:23:25 +08:00
|
|
|
|
|
|
|
// Global variables which can't collapse to null are ok.
|
2012-01-23 08:05:17 +08:00
|
|
|
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
|
2010-11-12 05:23:25 +08:00
|
|
|
return !GV->hasExternalWeakLinkage();
|
|
|
|
|
2011-01-24 05:15:29 +08:00
|
|
|
// byval arguments are ok.
|
2012-01-23 08:05:17 +08:00
|
|
|
if (const Argument *A = dyn_cast<Argument>(V))
|
2011-01-24 05:15:29 +08:00
|
|
|
return A->hasByValAttr();
|
2012-01-23 08:05:17 +08:00
|
|
|
|
2010-11-12 05:23:25 +08:00
|
|
|
// For GEPs, determine if the indexing lands within the allocated object.
|
2012-01-23 08:05:17 +08:00
|
|
|
if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
2010-11-12 05:23:25 +08:00
|
|
|
// Conservatively require that the base pointer be fully dereferenceable.
|
2012-01-23 08:05:17 +08:00
|
|
|
if (!Visited.insert(GEP->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
if (!isDereferenceablePointer(GEP->getOperand(0), Visited))
|
2010-11-12 05:23:25 +08:00
|
|
|
return false;
|
|
|
|
// Check the indices.
|
|
|
|
gep_type_iterator GTI = gep_type_begin(GEP);
|
|
|
|
for (User::const_op_iterator I = GEP->op_begin()+1,
|
|
|
|
E = GEP->op_end(); I != E; ++I) {
|
|
|
|
Value *Index = *I;
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty = *GTI++;
|
2010-11-12 05:23:25 +08:00
|
|
|
// Struct indices can't be out of bounds.
|
|
|
|
if (isa<StructType>(Ty))
|
|
|
|
continue;
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(Index);
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
// Zero is always ok.
|
|
|
|
if (CI->isZero())
|
|
|
|
continue;
|
|
|
|
// Check to see that it's within the bounds of an array.
|
2011-07-18 12:54:35 +08:00
|
|
|
ArrayType *ATy = dyn_cast<ArrayType>(Ty);
|
2010-11-12 05:23:25 +08:00
|
|
|
if (!ATy)
|
|
|
|
return false;
|
|
|
|
if (CI->getValue().getActiveBits() > 64)
|
|
|
|
return false;
|
|
|
|
if (CI->getZExtValue() >= ATy->getNumElements())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Indices check out; this is dereferenceable.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't know, assume the worst.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-23 08:05:17 +08:00
|
|
|
/// isDereferenceablePointer - Test if this value is always a pointer to
|
|
|
|
/// allocated and suitably aligned memory for a simple load or store.
|
|
|
|
bool Value::isDereferenceablePointer() const {
|
|
|
|
SmallPtrSet<const Value *, 32> Visited;
|
|
|
|
return ::isDereferenceablePointer(this, Visited);
|
|
|
|
}
|
|
|
|
|
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.
|
2009-09-20 12:03:34 +08:00
|
|
|
Value *Value::DoPHITranslation(const BasicBlock *CurBB,
|
2008-12-02 15:16:45 +08:00
|
|
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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?");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Splice ourselves into the list.
|
|
|
|
Next = *List;
|
|
|
|
*List = this;
|
|
|
|
setPrevPtr(List);
|
|
|
|
if (Next) {
|
|
|
|
Next->setPrevPtr(&Next);
|
2012-04-08 18:16:43 +08:00
|
|
|
assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
|
|
|
|
assert(List && "Must insert after existing node");
|
|
|
|
|
|
|
|
Next = List->Next;
|
|
|
|
setPrevPtr(&List->Next);
|
|
|
|
List->Next = this;
|
|
|
|
if (Next)
|
|
|
|
Next->setPrevPtr(&Next);
|
|
|
|
}
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
/// AddToUseList - Add this ValueHandle to the use list for VP.
|
|
|
|
void ValueHandleBase::AddToUseList() {
|
2012-04-08 18:16:43 +08:00
|
|
|
assert(VP.getPointer() && "Null pointer doesn't have a use list!");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2012-04-08 18:16:43 +08:00
|
|
|
LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2012-04-08 18:16:43 +08:00
|
|
|
if (VP.getPointer()->HasValueHandle) {
|
2009-04-01 06:11:05 +08:00
|
|
|
// If this value already has a ValueHandle, then it must be in the
|
|
|
|
// ValueHandles map already.
|
2012-04-08 18:16:43 +08:00
|
|
|
ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
|
2009-04-01 06:11:05 +08:00
|
|
|
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
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
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-08-19 02:28:58 +08:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-04-01 06:11:05 +08:00
|
|
|
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2012-04-08 18:16:43 +08:00
|
|
|
ValueHandleBase *&Entry = Handles[VP.getPointer()];
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry == 0 && "Value really did already have handles?");
|
|
|
|
AddToExistingUseList(&Entry);
|
2012-04-08 18:16:43 +08:00
|
|
|
VP.getPointer()->HasValueHandle = true;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
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.
|
2009-09-20 12:03:34 +08:00
|
|
|
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-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Okay, reallocation did happen. Fix the Prev Pointers.
|
2009-08-19 02:28:58 +08:00
|
|
|
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
|
|
|
|
E = Handles.end(); I != E; ++I) {
|
2012-04-08 18:16:43 +08:00
|
|
|
assert(I->second && I->first == I->second->VP.getPointer() &&
|
|
|
|
"List invariant broken!");
|
2009-04-01 06:11:05 +08:00
|
|
|
I->second->setPrevPtr(&I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveFromUseList - Remove this ValueHandle from its current use list.
|
|
|
|
void ValueHandleBase::RemoveFromUseList() {
|
2012-04-08 18:16:43 +08:00
|
|
|
assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
|
|
|
|
"Pointer doesn't have a use list!");
|
2009-04-01 06:11:05 +08:00
|
|
|
|
|
|
|
// Unlink this from its use list.
|
|
|
|
ValueHandleBase **PrevPtr = getPrevPtr();
|
|
|
|
assert(*PrevPtr == this && "List invariant broken");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
*PrevPtr = Next;
|
|
|
|
if (Next) {
|
|
|
|
assert(Next->getPrevPtr() == &Next && "List invariant broken");
|
|
|
|
Next->setPrevPtr(PrevPtr);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// 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.
|
2012-04-08 18:16:43 +08:00
|
|
|
LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
|
2009-08-19 02:28:58 +08:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-04-01 06:11:05 +08:00
|
|
|
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
|
2012-04-08 18:16:43 +08:00
|
|
|
Handles.erase(VP.getPointer());
|
|
|
|
VP.getPointer()->HasValueHandle = false;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-08-19 02:28:58 +08:00
|
|
|
LLVMContextImpl *pImpl = V->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[V];
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2010-07-24 20:09:22 +08:00
|
|
|
// We use a local ValueHandleBase as an iterator so that ValueHandles can add
|
|
|
|
// and remove themselves from the list without breaking our iteration. This
|
|
|
|
// is not really an AssertingVH; we just have to give ValueHandleBase a kind.
|
|
|
|
// Note that we deliberately do not the support the case when dropping a value
|
|
|
|
// handle results in a new value handle being permanently added to the list
|
|
|
|
// (as might occur in theory for CallbackVH's): the new value handle will not
|
2010-07-27 14:53:14 +08:00
|
|
|
// be processed and the checking code will mete out righteous punishment if
|
2010-07-24 20:09:22 +08:00
|
|
|
// the handle is still present once we have finished processing all the other
|
|
|
|
// value handles (it is fine to momentarily add then remove a value handle).
|
2009-10-13 01:43:32 +08:00
|
|
|
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
|
|
|
|
Iterator.RemoveFromUseList();
|
|
|
|
Iterator.AddToExistingUseListAfter(Entry);
|
|
|
|
assert(Entry->Next == &Iterator && "Loop invariant broken.");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
switch (Entry->getKind()) {
|
2009-04-01 06:11:05 +08:00
|
|
|
case Assert:
|
2009-10-13 01:43:32 +08:00
|
|
|
break;
|
2009-09-22 10:02:33 +08:00
|
|
|
case Tracking:
|
|
|
|
// Mark that this value has been deleted by setting it to an invalid Value
|
|
|
|
// pointer.
|
2009-10-13 01:43:32 +08:00
|
|
|
Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
|
2009-09-22 10:02:33 +08:00
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
case Weak:
|
|
|
|
// Weak just goes to null, which will unlink it from the list.
|
2009-10-13 01:43:32 +08:00
|
|
|
Entry->operator=(0);
|
2009-04-01 06:11:05 +08:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-13 01:43:32 +08:00
|
|
|
static_cast<CallbackVH*>(Entry)->deleted();
|
2009-05-03 05:10:48 +08:00
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
// All callbacks, weak references, and assertingVHs should be dropped by now.
|
|
|
|
if (V->HasValueHandle) {
|
|
|
|
#ifndef NDEBUG // Only in +Asserts mode...
|
2011-11-16 00:27:03 +08:00
|
|
|
dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
|
2009-10-13 01:43:32 +08:00
|
|
|
<< "\n";
|
|
|
|
if (pImpl->ValueHandles[V]->getKind() == Assert)
|
|
|
|
llvm_unreachable("An asserting value handle still pointed to this"
|
|
|
|
" value!");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
llvm_unreachable("All references to V were not removed?");
|
|
|
|
}
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
|
|
|
|
assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
|
|
|
|
assert(Old != New && "Changing value into itself!");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-08-19 02:28:58 +08:00
|
|
|
LLVMContextImpl *pImpl = Old->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[Old];
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
// We use a local ValueHandleBase as an iterator so that
|
|
|
|
// ValueHandles can add and remove themselves from the list without
|
|
|
|
// breaking our iteration. This is not really an AssertingVH; we
|
|
|
|
// just have to give ValueHandleBase some kind.
|
|
|
|
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
|
|
|
|
Iterator.RemoveFromUseList();
|
|
|
|
Iterator.AddToExistingUseListAfter(Entry);
|
|
|
|
assert(Entry->Next == &Iterator && "Loop invariant broken.");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
switch (Entry->getKind()) {
|
2009-04-01 06:11:05 +08:00
|
|
|
case Assert:
|
|
|
|
// Asserting handle does not follow RAUW implicitly.
|
|
|
|
break;
|
2009-09-22 10:02:33 +08:00
|
|
|
case Tracking:
|
|
|
|
// Tracking goes to new value like a WeakVH. Note that this may make it
|
|
|
|
// something incompatible with its templated type. We don't want to have a
|
|
|
|
// virtual (or inline) interface to handle this though, so instead we make
|
2009-09-22 18:30:34 +08:00
|
|
|
// the TrackingVH accessors guarantee that a client never sees this value.
|
2009-09-22 10:02:33 +08:00
|
|
|
|
|
|
|
// FALLTHROUGH
|
2009-04-01 06:11:05 +08:00
|
|
|
case Weak:
|
|
|
|
// Weak goes to the new value, which will unlink it from Old's list.
|
2009-10-13 01:43:32 +08:00
|
|
|
Entry->operator=(New);
|
2009-04-01 06:11:05 +08:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-13 01:43:32 +08:00
|
|
|
static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
|
2009-05-03 05:10:48 +08:00
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-27 14:53:14 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// If any new tracking or weak value handles were added while processing the
|
|
|
|
// list, then complain about it now.
|
|
|
|
if (Old->HasValueHandle)
|
|
|
|
for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
|
|
|
|
switch (Entry->getKind()) {
|
|
|
|
case Tracking:
|
|
|
|
case Weak:
|
|
|
|
dbgs() << "After RAUW from " << *Old->getType() << " %"
|
2011-11-16 00:27:03 +08:00
|
|
|
<< Old->getName() << " to " << *New->getType() << " %"
|
|
|
|
<< New->getName() << "\n";
|
2010-07-27 14:53:14 +08:00
|
|
|
llvm_unreachable("A tracking or weak value handle still pointed to the"
|
|
|
|
" old value!\n");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
|
2012-05-20 03:15:25 +08:00
|
|
|
// Default implementation for CallbackVH.
|
|
|
|
void CallbackVH::allUsesReplacedWith(Value *) {}
|
|
|
|
|
|
|
|
void CallbackVH::deleted() {
|
|
|
|
setValPtr(NULL);
|
|
|
|
}
|