aboutsummaryrefslogtreecommitdiff
path: root/lib/IR/Value.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2015-01-18 16:17:27 +0000
committerDimitry Andric <dim@FreeBSD.org>2015-01-18 16:17:27 +0000
commit67c32a98315f785a9ec9d531c1f571a0196c7463 (patch)
tree4abb9cbeecc7901726dd0b4a37369596c852e9ef /lib/IR/Value.cpp
parent9f61947910e6ab40de38e6b4034751ef1513200f (diff)
downloadsrc-67c32a98315f785a9ec9d531c1f571a0196c7463.tar.gz
src-67c32a98315f785a9ec9d531c1f571a0196c7463.zip
Notes
Diffstat (limited to 'lib/IR/Value.cpp')
-rw-r--r--lib/IR/Value.cpp180
1 files changed, 98 insertions, 82 deletions
diff --git a/lib/IR/Value.cpp b/lib/IR/Value.cpp
index 1ab2183b6565..5f7e258441a8 100644
--- a/lib/IR/Value.cpp
+++ b/lib/IR/Value.cpp
@@ -23,7 +23,6 @@
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
-#include "llvm/IR/LeakDetector.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/ValueHandle.h"
@@ -44,8 +43,8 @@ static inline Type *checkType(Type *Ty) {
}
Value::Value(Type *ty, unsigned scid)
- : VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid),
- HasValueHandle(0), SubclassOptionalData(0), SubclassData(0) {
+ : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
+ SubclassOptionalData(0), SubclassData(0), NumOperands(0) {
// FIXME: Why isn't this in the subclass gunk??
// Note, we cannot call isa<CallInst> before the CallInst has been
// constructed.
@@ -62,6 +61,8 @@ Value::~Value() {
// Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle)
ValueHandleBase::ValueIsDeleted(this);
+ if (isUsedByMetadata())
+ ValueAsMetadata::handleDeletion(this);
#ifndef NDEBUG // Only in -g mode...
// Check to make sure that there are no uses of this value that are still
@@ -81,15 +82,16 @@ Value::~Value() {
// If this value is named, destroy the name. This should not be in a symtab
// at this point.
- if (Name && SubclassID != MDStringVal)
- Name->Destroy();
+ destroyValueName();
+}
- // There should be no uses of this object anymore, remove it.
- LeakDetector::removeGarbageObject(this);
+void Value::destroyValueName() {
+ ValueName *Name = getValueName();
+ if (Name)
+ Name->Destroy();
+ setValueName(nullptr);
}
-/// hasNUses - Return true if this Value has exactly N users.
-///
bool Value::hasNUses(unsigned N) const {
const_use_iterator UI = use_begin(), E = use_end();
@@ -98,9 +100,6 @@ bool Value::hasNUses(unsigned N) const {
return UI == E;
}
-/// hasNUsesOrMore - Return true if this value has N users or more. This is
-/// logically equivalent to getNumUses() >= N.
-///
bool Value::hasNUsesOrMore(unsigned N) const {
const_use_iterator UI = use_begin(), E = use_end();
@@ -110,8 +109,6 @@ bool Value::hasNUsesOrMore(unsigned N) const {
return true;
}
-/// isUsedInBasicBlock - Return true if this value is used in the specified
-/// basic block.
bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
// This can be computed either by scanning the instructions in BB, or by
// scanning the use list of this Value. Both lists can be very long, but
@@ -133,10 +130,6 @@ bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
return false;
}
-
-/// 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());
}
@@ -156,9 +149,7 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
} else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent())
ST = &P->getValueSymbolTable();
- } else if (isa<MDString>(V))
- return true;
- else {
+ } else {
assert(isa<Constant>(V) && "Unknown value type!");
return true; // no name is setable for this.
}
@@ -169,14 +160,12 @@ StringRef Value::getName() const {
// 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);
- return Name->getKey();
+ if (!getValueName())
+ return StringRef("", 0);
+ return getValueName()->getKey();
}
void Value::setName(const Twine &NewName) {
- assert(SubclassID != MDStringVal &&
- "Cannot set the name of MDString with this method!");
-
// Fast path for common IRBuilder case of setName("") when there is no name.
if (NewName.isTriviallyEmpty() && !hasName())
return;
@@ -203,20 +192,17 @@ void Value::setName(const Twine &NewName) {
if (!ST) { // No symbol table to update? Just do the change.
if (NameRef.empty()) {
// Free the name for this value.
- Name->Destroy();
- Name = nullptr;
+ destroyValueName();
return;
}
- if (Name)
- Name->Destroy();
-
// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
+ destroyValueName();
// Create the new name.
- Name = ValueName::Create(NameRef);
- Name->setValue(this);
+ setValueName(ValueName::Create(NameRef));
+ getValueName()->setValue(this);
return;
}
@@ -224,24 +210,18 @@ void Value::setName(const Twine &NewName) {
// then reallocated.
if (hasName()) {
// Remove old name.
- ST->removeValueName(Name);
- Name->Destroy();
- Name = nullptr;
+ ST->removeValueName(getValueName());
+ destroyValueName();
if (NameRef.empty())
return;
}
// Name is changing to something new.
- Name = ST->createValueName(NameRef, this);
+ setValueName(ST->createValueName(NameRef, this));
}
-
-/// 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) {
- assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
-
ValueSymbolTable *ST = nullptr;
// If this value has a name, drop it.
if (hasName()) {
@@ -255,9 +235,8 @@ void Value::takeName(Value *V) {
// Remove old name.
if (ST)
- ST->removeValueName(Name);
- Name->Destroy();
- Name = nullptr;
+ ST->removeValueName(getValueName());
+ destroyValueName();
}
// Now we know that this has no name.
@@ -283,9 +262,9 @@ void Value::takeName(Value *V) {
// This works even if both values have no symtab yet.
if (ST == VST) {
// Take the name!
- Name = V->Name;
- V->Name = nullptr;
- Name->setValue(this);
+ setValueName(V->getValueName());
+ V->setValueName(nullptr);
+ getValueName()->setValue(this);
return;
}
@@ -293,19 +272,19 @@ void Value::takeName(Value *V) {
// then reinsert it into ST.
if (VST)
- VST->removeValueName(V->Name);
- Name = V->Name;
- V->Name = nullptr;
- Name->setValue(this);
+ VST->removeValueName(V->getValueName());
+ setValueName(V->getValueName());
+ V->setValueName(nullptr);
+ getValueName()->setValue(this);
if (ST)
ST->reinsertValue(this);
}
#ifndef NDEBUG
-static bool contains(SmallPtrSet<ConstantExpr *, 4> &Cache, ConstantExpr *Expr,
+static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
Constant *C) {
- if (!Cache.insert(Expr))
+ if (!Cache.insert(Expr).second)
return false;
for (auto &O : Expr->operands()) {
@@ -347,6 +326,8 @@ void Value::replaceAllUsesWith(Value *New) {
// Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle)
ValueHandleBase::ValueIsRAUWd(this, New);
+ if (isUsedByMetadata())
+ ValueAsMetadata::handleRAUW(this, New);
while (!use_empty()) {
Use &U = *UseList;
@@ -366,6 +347,28 @@ void Value::replaceAllUsesWith(Value *New) {
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
}
+// Like replaceAllUsesWith except it does not handle constants or basic blocks.
+// This routine leaves uses within BB.
+void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
+ assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
+ assert(!contains(New, this) &&
+ "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
+ assert(New->getType() == getType() &&
+ "replaceUses of value with new value of different type!");
+ assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
+
+ use_iterator UI = use_begin(), E = use_end();
+ for (; UI != E;) {
+ Use &U = *UI;
+ ++UI;
+ auto *Usr = dyn_cast<Instruction>(U.getUser());
+ if (Usr && Usr->getParent() == BB)
+ continue;
+ U.set(New);
+ }
+ return;
+}
+
namespace {
// Various metrics for how much to strip off of pointers.
enum PointerStripKind {
@@ -414,7 +417,7 @@ static Value *stripPointerCastsAndOffsets(Value *V) {
return V;
}
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
- } while (Visited.insert(V));
+ } while (Visited.insert(V).second);
return V;
}
@@ -464,7 +467,7 @@ Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
return V;
}
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
- } while (Visited.insert(V));
+ } while (Visited.insert(V).second);
return V;
}
@@ -473,10 +476,12 @@ Value *Value::stripInBoundsOffsets() {
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
}
-/// isDereferenceablePointer - Test if this value is always a pointer to
-/// allocated and suitably aligned memory for a simple load or store.
+/// \brief Check if Value is always a dereferenceable pointer.
+///
+/// Test if V is always a pointer to allocated and suitably aligned memory for
+/// a simple load or store.
static bool isDereferenceablePointer(const Value *V, const DataLayout *DL,
- SmallPtrSet<const Value *, 32> &Visited) {
+ SmallPtrSetImpl<const Value *> &Visited) {
// Note that it is not safe to speculate into a malloc'd region because
// malloc may return null.
@@ -533,7 +538,7 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL,
// For GEPs, determine if the indexing lands within the allocated object.
if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
// Conservatively require that the base pointer be fully dereferenceable.
- if (!Visited.insert(GEP->getOperand(0)))
+ if (!Visited.insert(GEP->getOperand(0)).second)
return false;
if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited))
return false;
@@ -572,8 +577,6 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL,
return false;
}
-/// 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 DataLayout *DL) const {
// When dereferenceability information is provided by a dereferenceable
// attribute, we know exactly how many bytes are dereferenceable. If we can
@@ -600,10 +603,6 @@ bool Value::isDereferenceablePointer(const DataLayout *DL) const {
return ::isDereferenceablePointer(this, DL, Visited);
}
-/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
-/// 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);
@@ -614,12 +613,29 @@ Value *Value::DoPHITranslation(const BasicBlock *CurBB,
LLVMContext &Value::getContext() const { return VTy->getContext(); }
+void Value::reverseUseList() {
+ if (!UseList || !UseList->Next)
+ // No need to reverse 0 or 1 uses.
+ return;
+
+ Use *Head = UseList;
+ Use *Current = UseList->Next;
+ Head->Next = nullptr;
+ while (Current) {
+ Use *Next = Current->Next;
+ Current->Next = Head;
+ Head->setPrev(&Current->Next);
+ Head = Current;
+ Current = Next;
+ }
+ UseList = Head;
+ Head->setPrev(&UseList);
+}
+
//===----------------------------------------------------------------------===//
// ValueHandleBase Class
//===----------------------------------------------------------------------===//
-/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
-/// List is known to point into the existing use list.
void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
assert(List && "Handle list is null?");
@@ -629,7 +645,7 @@ void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
setPrevPtr(List);
if (Next) {
Next->setPrevPtr(&Next);
- assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
+ assert(V == Next->V && "Added to wrong list?");
}
}
@@ -643,16 +659,15 @@ void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
Next->setPrevPtr(&Next);
}
-/// AddToUseList - Add this ValueHandle to the use list for VP.
void ValueHandleBase::AddToUseList() {
- assert(VP.getPointer() && "Null pointer doesn't have a use list!");
+ assert(V && "Null pointer doesn't have a use list!");
- LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
+ LLVMContextImpl *pImpl = V->getContext().pImpl;
- if (VP.getPointer()->HasValueHandle) {
+ if (V->HasValueHandle) {
// If this value already has a ValueHandle, then it must be in the
// ValueHandles map already.
- ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
+ ValueHandleBase *&Entry = pImpl->ValueHandles[V];
assert(Entry && "Value doesn't have any handles?");
AddToExistingUseList(&Entry);
return;
@@ -666,10 +681,10 @@ void ValueHandleBase::AddToUseList() {
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
- ValueHandleBase *&Entry = Handles[VP.getPointer()];
+ ValueHandleBase *&Entry = Handles[V];
assert(!Entry && "Value really did already have handles?");
AddToExistingUseList(&Entry);
- VP.getPointer()->HasValueHandle = true;
+ V->HasValueHandle = true;
// If reallocation didn't happen or if this was the first insertion, don't
// walk the table.
@@ -681,15 +696,14 @@ void ValueHandleBase::AddToUseList() {
// Okay, reallocation did happen. Fix the Prev Pointers.
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
E = Handles.end(); I != E; ++I) {
- assert(I->second && I->first == I->second->VP.getPointer() &&
+ assert(I->second && I->first == I->second->V &&
"List invariant broken!");
I->second->setPrevPtr(&I->second);
}
}
-/// RemoveFromUseList - Remove this ValueHandle from its current use list.
void ValueHandleBase::RemoveFromUseList() {
- assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
+ assert(V && V->HasValueHandle &&
"Pointer doesn't have a use list!");
// Unlink this from its use list.
@@ -706,11 +720,11 @@ void ValueHandleBase::RemoveFromUseList() {
// 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.
- LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
+ LLVMContextImpl *pImpl = V->getContext().pImpl;
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
- Handles.erase(VP.getPointer());
- VP.getPointer()->HasValueHandle = false;
+ Handles.erase(V);
+ V->HasValueHandle = false;
}
}
@@ -775,6 +789,8 @@ void ValueHandleBase::ValueIsDeleted(Value *V) {
void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
assert(Old != New && "Changing value into itself!");
+ assert(Old->getType() == New->getType() &&
+ "replaceAllUses of value with new value of different type!");
// Get the linked list base, which is guaranteed to exist since the
// HasValueHandle flag is set.