#define LLVM_ADT_VALUEMAP_H
#include "llvm/ADT/DenseMap.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Mutex.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/type_traits.h"
#include <iterator>
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include <vector>
namespace llvm {
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/IncludeFile.h"
-#include "llvm/Support/ValueHandle.h"
#include <map>
namespace llvm {
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
namespace llvm {
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/InstVisitor.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/TargetFolder.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
class CallInst;
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
class Function;
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/DataTypes.h"
-#include "llvm/Support/ValueHandle.h"
#include <map>
namespace llvm {
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/TargetFolder.h"
-#include "llvm/Support/ValueHandle.h"
#include <set>
namespace llvm {
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Metadata.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/DebugLoc.h"
-#include "llvm/Support/ValueHandle.h"
#include <utility>
namespace llvm {
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Metadata.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MachineLocation.h"
#include "llvm/Pass.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/DebugLoc.h"
#include "llvm/Support/Dwarf.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/DebugInfo.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/DataTypes.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
class BasicBlock;
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/ValueMap.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/MC/MCCodeGenInfo.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Mutex.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <map>
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/ConstantFolder.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
class MDNode;
/// Every value has a "use list" that keeps track of which other Values are
/// using this Value. A Value can also have an arbitrary number of ValueHandle
/// objects that watch it and listen to RAUW and Destroy events. See
-/// llvm/Support/ValueHandle.h for details.
+/// llvm/IR/ValueHandle.h for details.
///
/// @brief LLVM Value Representation
class Value {
--- /dev/null
+//===- ValueHandle.h - Value Smart Pointer classes --------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the ValueHandle class and its sub-classes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_IR_VALUEHANDLE_H
+#define LLVM_IR_VALUEHANDLE_H
+
+#include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/PointerIntPair.h"
+#include "llvm/IR/Value.h"
+
+namespace llvm {
+class ValueHandleBase;
+template<typename From> struct simplify_type;
+
+// ValueHandleBase** is only 4-byte aligned.
+template<>
+class PointerLikeTypeTraits<ValueHandleBase**> {
+public:
+ static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
+ static inline ValueHandleBase **getFromVoidPointer(void *P) {
+ return static_cast<ValueHandleBase**>(P);
+ }
+ enum { NumLowBitsAvailable = 2 };
+};
+
+/// ValueHandleBase - This is the common base class of value handles.
+/// ValueHandle's are smart pointers to Value's that have special behavior when
+/// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
+/// below for details.
+///
+class ValueHandleBase {
+ friend class Value;
+protected:
+ /// HandleBaseKind - This indicates what sub class the handle actually is.
+ /// This is to avoid having a vtable for the light-weight handle pointers. The
+ /// fully general Callback version does have a vtable.
+ enum HandleBaseKind {
+ Assert,
+ Callback,
+ Tracking,
+ Weak
+ };
+
+private:
+ PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
+ ValueHandleBase *Next;
+
+ // A subclass may want to store some information along with the value
+ // pointer. Allow them to do this by making the value pointer a pointer-int
+ // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
+ // access.
+ PointerIntPair<Value*, 2> VP;
+
+ ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
+public:
+ explicit ValueHandleBase(HandleBaseKind Kind)
+ : PrevPair(0, Kind), Next(0), VP(0, 0) {}
+ ValueHandleBase(HandleBaseKind Kind, Value *V)
+ : PrevPair(0, Kind), Next(0), VP(V, 0) {
+ if (isValid(VP.getPointer()))
+ AddToUseList();
+ }
+ ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
+ : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
+ if (isValid(VP.getPointer()))
+ AddToExistingUseList(RHS.getPrevPtr());
+ }
+ ~ValueHandleBase() {
+ if (isValid(VP.getPointer()))
+ RemoveFromUseList();
+ }
+
+ Value *operator=(Value *RHS) {
+ if (VP.getPointer() == RHS) return RHS;
+ if (isValid(VP.getPointer())) RemoveFromUseList();
+ VP.setPointer(RHS);
+ if (isValid(VP.getPointer())) AddToUseList();
+ return RHS;
+ }
+
+ Value *operator=(const ValueHandleBase &RHS) {
+ if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
+ if (isValid(VP.getPointer())) RemoveFromUseList();
+ VP.setPointer(RHS.VP.getPointer());
+ if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
+ return VP.getPointer();
+ }
+
+ Value *operator->() const { return getValPtr(); }
+ Value &operator*() const { return *getValPtr(); }
+
+protected:
+ Value *getValPtr() const { return VP.getPointer(); }
+
+ void setValPtrInt(unsigned K) { VP.setInt(K); }
+ unsigned getValPtrInt() const { return VP.getInt(); }
+
+ static bool isValid(Value *V) {
+ return V &&
+ V != DenseMapInfo<Value *>::getEmptyKey() &&
+ V != DenseMapInfo<Value *>::getTombstoneKey();
+ }
+
+public:
+ // Callbacks made from Value.
+ static void ValueIsDeleted(Value *V);
+ static void ValueIsRAUWd(Value *Old, Value *New);
+
+private:
+ // Internal implementation details.
+ ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
+ HandleBaseKind getKind() const { return PrevPair.getInt(); }
+ void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
+
+ /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
+ /// List is the address of either the head of the list or a Next node within
+ /// the existing use list.
+ void AddToExistingUseList(ValueHandleBase **List);
+
+ /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
+ /// Node.
+ void AddToExistingUseListAfter(ValueHandleBase *Node);
+
+ /// AddToUseList - Add this ValueHandle to the use list for VP.
+ void AddToUseList();
+ /// RemoveFromUseList - Remove this ValueHandle from its current use list.
+ void RemoveFromUseList();
+};
+
+/// WeakVH - This is a value handle that tries hard to point to a Value, even
+/// across RAUW operations, but will null itself out if the value is destroyed.
+/// this is useful for advisory sorts of information, but should not be used as
+/// the key of a map (since the map would have to rearrange itself when the
+/// pointer changes).
+class WeakVH : public ValueHandleBase {
+public:
+ WeakVH() : ValueHandleBase(Weak) {}
+ WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
+ WeakVH(const WeakVH &RHS)
+ : ValueHandleBase(Weak, RHS) {}
+
+ Value *operator=(Value *RHS) {
+ return ValueHandleBase::operator=(RHS);
+ }
+ Value *operator=(const ValueHandleBase &RHS) {
+ return ValueHandleBase::operator=(RHS);
+ }
+
+ operator Value*() const {
+ return getValPtr();
+ }
+};
+
+// Specialize simplify_type to allow WeakVH to participate in
+// dyn_cast, isa, etc.
+template<> struct simplify_type<WeakVH> {
+ typedef Value* SimpleType;
+ static SimpleType getSimplifiedValue(WeakVH &WVH) {
+ return WVH;
+ }
+};
+
+/// AssertingVH - This is a Value Handle that points to a value and asserts out
+/// if the value is destroyed while the handle is still live. This is very
+/// useful for catching dangling pointer bugs and other things which can be
+/// non-obvious. One particularly useful place to use this is as the Key of a
+/// map. Dangling pointer bugs often lead to really subtle bugs that only occur
+/// if another object happens to get allocated to the same address as the old
+/// one. Using an AssertingVH ensures that an assert is triggered as soon as
+/// the bad delete occurs.
+///
+/// Note that an AssertingVH handle does *not* follow values across RAUW
+/// operations. This means that RAUW's need to explicitly update the
+/// AssertingVH's as it moves. This is required because in non-assert mode this
+/// class turns into a trivial wrapper around a pointer.
+template <typename ValueTy>
+class AssertingVH
+#ifndef NDEBUG
+ : public ValueHandleBase
+#endif
+ {
+
+#ifndef NDEBUG
+ ValueTy *getValPtr() const {
+ return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
+ }
+ void setValPtr(ValueTy *P) {
+ ValueHandleBase::operator=(GetAsValue(P));
+ }
+#else
+ ValueTy *ThePtr;
+ ValueTy *getValPtr() const { return ThePtr; }
+ void setValPtr(ValueTy *P) { ThePtr = P; }
+#endif
+
+ // Convert a ValueTy*, which may be const, to the type the base
+ // class expects.
+ static Value *GetAsValue(Value *V) { return V; }
+ static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
+
+public:
+#ifndef NDEBUG
+ AssertingVH() : ValueHandleBase(Assert) {}
+ AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
+ AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
+#else
+ AssertingVH() : ThePtr(0) {}
+ AssertingVH(ValueTy *P) : ThePtr(P) {}
+#endif
+
+ operator ValueTy*() const {
+ return getValPtr();
+ }
+
+ ValueTy *operator=(ValueTy *RHS) {
+ setValPtr(RHS);
+ return getValPtr();
+ }
+ ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
+ setValPtr(RHS.getValPtr());
+ return getValPtr();
+ }
+
+ ValueTy *operator->() const { return getValPtr(); }
+ ValueTy &operator*() const { return *getValPtr(); }
+};
+
+// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
+template<typename T>
+struct DenseMapInfo<AssertingVH<T> > {
+ typedef DenseMapInfo<T*> PointerInfo;
+ static inline AssertingVH<T> getEmptyKey() {
+ return AssertingVH<T>(PointerInfo::getEmptyKey());
+ }
+ static inline T* getTombstoneKey() {
+ return AssertingVH<T>(PointerInfo::getTombstoneKey());
+ }
+ static unsigned getHashValue(const AssertingVH<T> &Val) {
+ return PointerInfo::getHashValue(Val);
+ }
+ static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
+ return LHS == RHS;
+ }
+};
+
+template <typename T>
+struct isPodLike<AssertingVH<T> > {
+#ifdef NDEBUG
+ static const bool value = true;
+#else
+ static const bool value = false;
+#endif
+};
+
+
+/// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
+/// even across RAUW operations.
+///
+/// TrackingVH is designed for situations where a client needs to hold a handle
+/// to a Value (or subclass) across some operations which may move that value,
+/// but should never destroy it or replace it with some unacceptable type.
+///
+/// It is an error to do anything with a TrackingVH whose value has been
+/// destroyed, except to destruct it.
+///
+/// It is an error to attempt to replace a value with one of a type which is
+/// incompatible with any of its outstanding TrackingVHs.
+template<typename ValueTy>
+class TrackingVH : public ValueHandleBase {
+ void CheckValidity() const {
+ Value *VP = ValueHandleBase::getValPtr();
+
+ // Null is always ok.
+ if (!VP) return;
+
+ // Check that this value is valid (i.e., it hasn't been deleted). We
+ // explicitly delay this check until access to avoid requiring clients to be
+ // unnecessarily careful w.r.t. destruction.
+ assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
+
+ // Check that the value is a member of the correct subclass. We would like
+ // to check this property on assignment for better debugging, but we don't
+ // want to require a virtual interface on this VH. Instead we allow RAUW to
+ // replace this value with a value of an invalid type, and check it here.
+ assert(isa<ValueTy>(VP) &&
+ "Tracked Value was replaced by one with an invalid type!");
+ }
+
+ ValueTy *getValPtr() const {
+ CheckValidity();
+ return (ValueTy*)ValueHandleBase::getValPtr();
+ }
+ void setValPtr(ValueTy *P) {
+ CheckValidity();
+ ValueHandleBase::operator=(GetAsValue(P));
+ }
+
+ // Convert a ValueTy*, which may be const, to the type the base
+ // class expects.
+ static Value *GetAsValue(Value *V) { return V; }
+ static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
+
+public:
+ TrackingVH() : ValueHandleBase(Tracking) {}
+ TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
+ TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
+
+ operator ValueTy*() const {
+ return getValPtr();
+ }
+
+ ValueTy *operator=(ValueTy *RHS) {
+ setValPtr(RHS);
+ return getValPtr();
+ }
+ ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
+ setValPtr(RHS.getValPtr());
+ return getValPtr();
+ }
+
+ ValueTy *operator->() const { return getValPtr(); }
+ ValueTy &operator*() const { return *getValPtr(); }
+};
+
+/// CallbackVH - This is a value handle that allows subclasses to define
+/// callbacks that run when the underlying Value has RAUW called on it or is
+/// destroyed. This class can be used as the key of a map, as long as the user
+/// takes it out of the map before calling setValPtr() (since the map has to
+/// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
+/// class has a vtable and a virtual destructor.
+class CallbackVH : public ValueHandleBase {
+ virtual void anchor();
+protected:
+ CallbackVH(const CallbackVH &RHS)
+ : ValueHandleBase(Callback, RHS) {}
+
+ virtual ~CallbackVH() {}
+
+ void setValPtr(Value *P) {
+ ValueHandleBase::operator=(P);
+ }
+
+public:
+ CallbackVH() : ValueHandleBase(Callback) {}
+ CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
+
+ operator Value*() const {
+ return getValPtr();
+ }
+
+ /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
+ /// call any non-virtual Value method on getValPtr(), but no subclass methods.
+ /// If WeakVH were implemented as a CallbackVH, it would use this method to
+ /// call setValPtr(NULL). AssertingVH would use this method to cause an
+ /// assertion failure.
+ ///
+ /// All implementations must remove the reference from this object to the
+ /// Value that's being destroyed.
+ virtual void deleted() { setValPtr(NULL); }
+
+ /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
+ /// _before_ any of the uses have actually been replaced. If WeakVH were
+ /// implemented as a CallbackVH, it would use this method to call
+ /// setValPtr(new_value). AssertingVH would do nothing in this method.
+ virtual void allUsesReplacedWith(Value *) {}
+};
+
+} // End llvm namespace
+
+#endif
+++ /dev/null
-//===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file declares the ValueHandle class and its sub-classes.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_SUPPORT_VALUEHANDLE_H
-#define LLVM_SUPPORT_VALUEHANDLE_H
-
-#include "llvm/ADT/DenseMapInfo.h"
-#include "llvm/ADT/PointerIntPair.h"
-#include "llvm/IR/Value.h"
-
-namespace llvm {
-class ValueHandleBase;
-template<typename From> struct simplify_type;
-
-// ValueHandleBase** is only 4-byte aligned.
-template<>
-class PointerLikeTypeTraits<ValueHandleBase**> {
-public:
- static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
- static inline ValueHandleBase **getFromVoidPointer(void *P) {
- return static_cast<ValueHandleBase**>(P);
- }
- enum { NumLowBitsAvailable = 2 };
-};
-
-/// ValueHandleBase - This is the common base class of value handles.
-/// ValueHandle's are smart pointers to Value's that have special behavior when
-/// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
-/// below for details.
-///
-class ValueHandleBase {
- friend class Value;
-protected:
- /// HandleBaseKind - This indicates what sub class the handle actually is.
- /// This is to avoid having a vtable for the light-weight handle pointers. The
- /// fully general Callback version does have a vtable.
- enum HandleBaseKind {
- Assert,
- Callback,
- Tracking,
- Weak
- };
-
-private:
- PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
- ValueHandleBase *Next;
-
- // A subclass may want to store some information along with the value
- // pointer. Allow them to do this by making the value pointer a pointer-int
- // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
- // access.
- PointerIntPair<Value*, 2> VP;
-
- ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
-public:
- explicit ValueHandleBase(HandleBaseKind Kind)
- : PrevPair(0, Kind), Next(0), VP(0, 0) {}
- ValueHandleBase(HandleBaseKind Kind, Value *V)
- : PrevPair(0, Kind), Next(0), VP(V, 0) {
- if (isValid(VP.getPointer()))
- AddToUseList();
- }
- ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
- : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
- if (isValid(VP.getPointer()))
- AddToExistingUseList(RHS.getPrevPtr());
- }
- ~ValueHandleBase() {
- if (isValid(VP.getPointer()))
- RemoveFromUseList();
- }
-
- Value *operator=(Value *RHS) {
- if (VP.getPointer() == RHS) return RHS;
- if (isValid(VP.getPointer())) RemoveFromUseList();
- VP.setPointer(RHS);
- if (isValid(VP.getPointer())) AddToUseList();
- return RHS;
- }
-
- Value *operator=(const ValueHandleBase &RHS) {
- if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
- if (isValid(VP.getPointer())) RemoveFromUseList();
- VP.setPointer(RHS.VP.getPointer());
- if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
- return VP.getPointer();
- }
-
- Value *operator->() const { return getValPtr(); }
- Value &operator*() const { return *getValPtr(); }
-
-protected:
- Value *getValPtr() const { return VP.getPointer(); }
-
- void setValPtrInt(unsigned K) { VP.setInt(K); }
- unsigned getValPtrInt() const { return VP.getInt(); }
-
- static bool isValid(Value *V) {
- return V &&
- V != DenseMapInfo<Value *>::getEmptyKey() &&
- V != DenseMapInfo<Value *>::getTombstoneKey();
- }
-
-public:
- // Callbacks made from Value.
- static void ValueIsDeleted(Value *V);
- static void ValueIsRAUWd(Value *Old, Value *New);
-
-private:
- // Internal implementation details.
- ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
- HandleBaseKind getKind() const { return PrevPair.getInt(); }
- void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
-
- /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
- /// List is the address of either the head of the list or a Next node within
- /// the existing use list.
- void AddToExistingUseList(ValueHandleBase **List);
-
- /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
- /// Node.
- void AddToExistingUseListAfter(ValueHandleBase *Node);
-
- /// AddToUseList - Add this ValueHandle to the use list for VP.
- void AddToUseList();
- /// RemoveFromUseList - Remove this ValueHandle from its current use list.
- void RemoveFromUseList();
-};
-
-/// WeakVH - This is a value handle that tries hard to point to a Value, even
-/// across RAUW operations, but will null itself out if the value is destroyed.
-/// this is useful for advisory sorts of information, but should not be used as
-/// the key of a map (since the map would have to rearrange itself when the
-/// pointer changes).
-class WeakVH : public ValueHandleBase {
-public:
- WeakVH() : ValueHandleBase(Weak) {}
- WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
- WeakVH(const WeakVH &RHS)
- : ValueHandleBase(Weak, RHS) {}
-
- Value *operator=(Value *RHS) {
- return ValueHandleBase::operator=(RHS);
- }
- Value *operator=(const ValueHandleBase &RHS) {
- return ValueHandleBase::operator=(RHS);
- }
-
- operator Value*() const {
- return getValPtr();
- }
-};
-
-// Specialize simplify_type to allow WeakVH to participate in
-// dyn_cast, isa, etc.
-template<> struct simplify_type<WeakVH> {
- typedef Value* SimpleType;
- static SimpleType getSimplifiedValue(WeakVH &WVH) {
- return WVH;
- }
-};
-
-/// AssertingVH - This is a Value Handle that points to a value and asserts out
-/// if the value is destroyed while the handle is still live. This is very
-/// useful for catching dangling pointer bugs and other things which can be
-/// non-obvious. One particularly useful place to use this is as the Key of a
-/// map. Dangling pointer bugs often lead to really subtle bugs that only occur
-/// if another object happens to get allocated to the same address as the old
-/// one. Using an AssertingVH ensures that an assert is triggered as soon as
-/// the bad delete occurs.
-///
-/// Note that an AssertingVH handle does *not* follow values across RAUW
-/// operations. This means that RAUW's need to explicitly update the
-/// AssertingVH's as it moves. This is required because in non-assert mode this
-/// class turns into a trivial wrapper around a pointer.
-template <typename ValueTy>
-class AssertingVH
-#ifndef NDEBUG
- : public ValueHandleBase
-#endif
- {
-
-#ifndef NDEBUG
- ValueTy *getValPtr() const {
- return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
- }
- void setValPtr(ValueTy *P) {
- ValueHandleBase::operator=(GetAsValue(P));
- }
-#else
- ValueTy *ThePtr;
- ValueTy *getValPtr() const { return ThePtr; }
- void setValPtr(ValueTy *P) { ThePtr = P; }
-#endif
-
- // Convert a ValueTy*, which may be const, to the type the base
- // class expects.
- static Value *GetAsValue(Value *V) { return V; }
- static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
-
-public:
-#ifndef NDEBUG
- AssertingVH() : ValueHandleBase(Assert) {}
- AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
- AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
-#else
- AssertingVH() : ThePtr(0) {}
- AssertingVH(ValueTy *P) : ThePtr(P) {}
-#endif
-
- operator ValueTy*() const {
- return getValPtr();
- }
-
- ValueTy *operator=(ValueTy *RHS) {
- setValPtr(RHS);
- return getValPtr();
- }
- ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
- setValPtr(RHS.getValPtr());
- return getValPtr();
- }
-
- ValueTy *operator->() const { return getValPtr(); }
- ValueTy &operator*() const { return *getValPtr(); }
-};
-
-// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
-template<typename T>
-struct DenseMapInfo<AssertingVH<T> > {
- typedef DenseMapInfo<T*> PointerInfo;
- static inline AssertingVH<T> getEmptyKey() {
- return AssertingVH<T>(PointerInfo::getEmptyKey());
- }
- static inline T* getTombstoneKey() {
- return AssertingVH<T>(PointerInfo::getTombstoneKey());
- }
- static unsigned getHashValue(const AssertingVH<T> &Val) {
- return PointerInfo::getHashValue(Val);
- }
- static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
- return LHS == RHS;
- }
-};
-
-template <typename T>
-struct isPodLike<AssertingVH<T> > {
-#ifdef NDEBUG
- static const bool value = true;
-#else
- static const bool value = false;
-#endif
-};
-
-
-/// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
-/// even across RAUW operations.
-///
-/// TrackingVH is designed for situations where a client needs to hold a handle
-/// to a Value (or subclass) across some operations which may move that value,
-/// but should never destroy it or replace it with some unacceptable type.
-///
-/// It is an error to do anything with a TrackingVH whose value has been
-/// destroyed, except to destruct it.
-///
-/// It is an error to attempt to replace a value with one of a type which is
-/// incompatible with any of its outstanding TrackingVHs.
-template<typename ValueTy>
-class TrackingVH : public ValueHandleBase {
- void CheckValidity() const {
- Value *VP = ValueHandleBase::getValPtr();
-
- // Null is always ok.
- if (!VP) return;
-
- // Check that this value is valid (i.e., it hasn't been deleted). We
- // explicitly delay this check until access to avoid requiring clients to be
- // unnecessarily careful w.r.t. destruction.
- assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
-
- // Check that the value is a member of the correct subclass. We would like
- // to check this property on assignment for better debugging, but we don't
- // want to require a virtual interface on this VH. Instead we allow RAUW to
- // replace this value with a value of an invalid type, and check it here.
- assert(isa<ValueTy>(VP) &&
- "Tracked Value was replaced by one with an invalid type!");
- }
-
- ValueTy *getValPtr() const {
- CheckValidity();
- return (ValueTy*)ValueHandleBase::getValPtr();
- }
- void setValPtr(ValueTy *P) {
- CheckValidity();
- ValueHandleBase::operator=(GetAsValue(P));
- }
-
- // Convert a ValueTy*, which may be const, to the type the base
- // class expects.
- static Value *GetAsValue(Value *V) { return V; }
- static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
-
-public:
- TrackingVH() : ValueHandleBase(Tracking) {}
- TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
- TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
-
- operator ValueTy*() const {
- return getValPtr();
- }
-
- ValueTy *operator=(ValueTy *RHS) {
- setValPtr(RHS);
- return getValPtr();
- }
- ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
- setValPtr(RHS.getValPtr());
- return getValPtr();
- }
-
- ValueTy *operator->() const { return getValPtr(); }
- ValueTy &operator*() const { return *getValPtr(); }
-};
-
-/// CallbackVH - This is a value handle that allows subclasses to define
-/// callbacks that run when the underlying Value has RAUW called on it or is
-/// destroyed. This class can be used as the key of a map, as long as the user
-/// takes it out of the map before calling setValPtr() (since the map has to
-/// rearrange itself when the pointer changes). Unlike ValueHandleBase, this
-/// class has a vtable and a virtual destructor.
-class CallbackVH : public ValueHandleBase {
- virtual void anchor();
-protected:
- CallbackVH(const CallbackVH &RHS)
- : ValueHandleBase(Callback, RHS) {}
-
- virtual ~CallbackVH() {}
-
- void setValPtr(Value *P) {
- ValueHandleBase::operator=(P);
- }
-
-public:
- CallbackVH() : ValueHandleBase(Callback) {}
- CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
-
- operator Value*() const {
- return getValPtr();
- }
-
- /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
- /// call any non-virtual Value method on getValPtr(), but no subclass methods.
- /// If WeakVH were implemented as a CallbackVH, it would use this method to
- /// call setValPtr(NULL). AssertingVH would use this method to cause an
- /// assertion failure.
- ///
- /// All implementations must remove the reference from this object to the
- /// Value that's being destroyed.
- virtual void deleted() { setValPtr(NULL); }
-
- /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
- /// _before_ any of the uses have actually been replaced. If WeakVH were
- /// implemented as a CallbackVH, it would use this method to call
- /// setValPtr(new_value). AssertingVH would do nothing in this method.
- virtual void allUsesReplacedWith(Value *) {}
-};
-
-} // End llvm namespace
-
-#endif
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/ValueMap.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
namespace llvm {
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H
#define LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/ConstantRange.h"
-#include "llvm/Support/ValueHandle.h"
using namespace llvm;
using namespace llvm::PatternMatch;
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include <map>
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include <map>
namespace llvm {
#include "llvm/IR/Attributes.h"
#include "llvm/IR/OperandTraits.h"
#include "llvm/IR/Type.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/system_error.h"
#include <vector>
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Timer.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Target/TargetFrameLowering.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/DebugInfo.h"
#include "llvm/IR/Metadata.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Path.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MutexGuard.h"
#include "llvm/Support/TargetRegistry.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <cmath>
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Errno.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include "EventListenerCommon.h"
#include "IntelJITEventsWrapper.h"
#define JIT_H
#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/PassManager.h"
-#include "llvm/Support/ValueHandle.h"
namespace llvm {
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Disassembler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MutexGuard.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetJITInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Dwarf.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::dwarf;
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include <vector>
namespace llvm {
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/LeakDetector.h"
-#include "llvm/Support/ValueHandle.h"
using namespace llvm;
//===----------------------------------------------------------------------===//
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LeakDetector.h"
#include "llvm/Support/ManagedStatic.h"
-#include "llvm/Support/ValueHandle.h"
#include <algorithm>
using namespace llvm;
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/PassSupport.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/NoFolder.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#define DEBUG_TYPE "xcore-lower-thread-local"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
using namespace llvm;
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Type.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Operator.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Type.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
-#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Scalar.h"
PatternMatch.cpp
TypeBuilderTest.cpp
TypesTest.cpp
+ ValueHandleTest.cpp
ValueMapTest.cpp
ValueTest.cpp
VerifierTest.cpp
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
-#include "llvm/Support/ValueHandle.h"
+#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
--- /dev/null
+//===- ValueHandleTest.cpp - ValueHandle tests ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/ValueHandle.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+using namespace llvm;
+
+namespace {
+
+class ValueHandle : public testing::Test {
+protected:
+ Constant *ConstantV;
+ std::auto_ptr<BitCastInst> BitcastV;
+
+ ValueHandle() :
+ ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
+ BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) {
+ }
+};
+
+class ConcreteCallbackVH : public CallbackVH {
+public:
+ ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
+};
+
+TEST_F(ValueHandle, WeakVH_BasicOperation) {
+ WeakVH WVH(BitcastV.get());
+ EXPECT_EQ(BitcastV.get(), WVH);
+ WVH = ConstantV;
+ EXPECT_EQ(ConstantV, WVH);
+
+ // Make sure I can call a method on the underlying Value. It
+ // doesn't matter which method.
+ EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType());
+ EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
+}
+
+TEST_F(ValueHandle, WeakVH_Comparisons) {
+ WeakVH BitcastWVH(BitcastV.get());
+ WeakVH ConstantWVH(ConstantV);
+
+ EXPECT_TRUE(BitcastWVH == BitcastWVH);
+ EXPECT_TRUE(BitcastV.get() == BitcastWVH);
+ EXPECT_TRUE(BitcastWVH == BitcastV.get());
+ EXPECT_FALSE(BitcastWVH == ConstantWVH);
+
+ EXPECT_TRUE(BitcastWVH != ConstantWVH);
+ EXPECT_TRUE(BitcastV.get() != ConstantWVH);
+ EXPECT_TRUE(BitcastWVH != ConstantV);
+ EXPECT_FALSE(BitcastWVH != BitcastWVH);
+
+ // Cast to Value* so comparisons work.
+ Value *BV = BitcastV.get();
+ Value *CV = ConstantV;
+ EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
+ EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
+ EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
+ EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
+
+ EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
+ EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
+ EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
+ EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
+
+ EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
+ EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
+ EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
+ EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
+}
+
+TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
+ WeakVH WVH(BitcastV.get());
+ WeakVH WVH_Copy(WVH);
+ WeakVH WVH_Recreated(BitcastV.get());
+ BitcastV->replaceAllUsesWith(ConstantV);
+ EXPECT_EQ(ConstantV, WVH);
+ EXPECT_EQ(ConstantV, WVH_Copy);
+ EXPECT_EQ(ConstantV, WVH_Recreated);
+}
+
+TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
+ WeakVH WVH(BitcastV.get());
+ WeakVH WVH_Copy(WVH);
+ WeakVH WVH_Recreated(BitcastV.get());
+ BitcastV.reset();
+ Value *null_value = NULL;
+ EXPECT_EQ(null_value, WVH);
+ EXPECT_EQ(null_value, WVH_Copy);
+ EXPECT_EQ(null_value, WVH_Recreated);
+}
+
+
+TEST_F(ValueHandle, AssertingVH_BasicOperation) {
+ AssertingVH<CastInst> AVH(BitcastV.get());
+ CastInst *implicit_to_exact_type = AVH;
+ (void)implicit_to_exact_type; // Avoid warning.
+
+ AssertingVH<Value> GenericAVH(BitcastV.get());
+ EXPECT_EQ(BitcastV.get(), GenericAVH);
+ GenericAVH = ConstantV;
+ EXPECT_EQ(ConstantV, GenericAVH);
+
+ // Make sure I can call a method on the underlying CastInst. It
+ // doesn't matter which method.
+ EXPECT_FALSE(AVH->mayWriteToMemory());
+ EXPECT_FALSE((*AVH).mayWriteToMemory());
+}
+
+TEST_F(ValueHandle, AssertingVH_Const) {
+ const CastInst *ConstBitcast = BitcastV.get();
+ AssertingVH<const CastInst> AVH(ConstBitcast);
+ const CastInst *implicit_to_exact_type = AVH;
+ (void)implicit_to_exact_type; // Avoid warning.
+}
+
+TEST_F(ValueHandle, AssertingVH_Comparisons) {
+ AssertingVH<Value> BitcastAVH(BitcastV.get());
+ AssertingVH<Value> ConstantAVH(ConstantV);
+
+ EXPECT_TRUE(BitcastAVH == BitcastAVH);
+ EXPECT_TRUE(BitcastV.get() == BitcastAVH);
+ EXPECT_TRUE(BitcastAVH == BitcastV.get());
+ EXPECT_FALSE(BitcastAVH == ConstantAVH);
+
+ EXPECT_TRUE(BitcastAVH != ConstantAVH);
+ EXPECT_TRUE(BitcastV.get() != ConstantAVH);
+ EXPECT_TRUE(BitcastAVH != ConstantV);
+ EXPECT_FALSE(BitcastAVH != BitcastAVH);
+
+ // Cast to Value* so comparisons work.
+ Value *BV = BitcastV.get();
+ Value *CV = ConstantV;
+ EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
+ EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
+ EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
+ EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
+
+ EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
+ EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
+ EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
+ EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
+
+ EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
+ EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
+ EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
+ EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
+}
+
+TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
+ AssertingVH<Value> AVH(BitcastV.get());
+ BitcastV->replaceAllUsesWith(ConstantV);
+ EXPECT_EQ(BitcastV.get(), AVH);
+}
+
+#ifdef NDEBUG
+
+TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
+ EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
+}
+
+#else // !NDEBUG
+
+#ifdef GTEST_HAS_DEATH_TEST
+
+TEST_F(ValueHandle, AssertingVH_Asserts) {
+ AssertingVH<Value> AVH(BitcastV.get());
+ EXPECT_DEATH({BitcastV.reset();},
+ "An asserting value handle still pointed to this value!");
+ AssertingVH<Value> Copy(AVH);
+ AVH = NULL;
+ EXPECT_DEATH({BitcastV.reset();},
+ "An asserting value handle still pointed to this value!");
+ Copy = NULL;
+ BitcastV.reset();
+}
+
+#endif // GTEST_HAS_DEATH_TEST
+
+#endif // NDEBUG
+
+TEST_F(ValueHandle, CallbackVH_BasicOperation) {
+ ConcreteCallbackVH CVH(BitcastV.get());
+ EXPECT_EQ(BitcastV.get(), CVH);
+ CVH = ConstantV;
+ EXPECT_EQ(ConstantV, CVH);
+
+ // Make sure I can call a method on the underlying Value. It
+ // doesn't matter which method.
+ EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType());
+ EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType());
+}
+
+TEST_F(ValueHandle, CallbackVH_Comparisons) {
+ ConcreteCallbackVH BitcastCVH(BitcastV.get());
+ ConcreteCallbackVH ConstantCVH(ConstantV);
+
+ EXPECT_TRUE(BitcastCVH == BitcastCVH);
+ EXPECT_TRUE(BitcastV.get() == BitcastCVH);
+ EXPECT_TRUE(BitcastCVH == BitcastV.get());
+ EXPECT_FALSE(BitcastCVH == ConstantCVH);
+
+ EXPECT_TRUE(BitcastCVH != ConstantCVH);
+ EXPECT_TRUE(BitcastV.get() != ConstantCVH);
+ EXPECT_TRUE(BitcastCVH != ConstantV);
+ EXPECT_FALSE(BitcastCVH != BitcastCVH);
+
+ // Cast to Value* so comparisons work.
+ Value *BV = BitcastV.get();
+ Value *CV = ConstantV;
+ EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
+ EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
+ EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
+ EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
+
+ EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
+ EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
+ EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
+ EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
+
+ EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
+ EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
+ EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
+ EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
+}
+
+TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
+ class RecordingVH : public CallbackVH {
+ public:
+ int DeletedCalls;
+ int AURWCalls;
+
+ RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
+ RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
+
+ private:
+ virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
+ virtual void allUsesReplacedWith(Value *) { AURWCalls++; }
+ };
+
+ RecordingVH RVH;
+ RVH = BitcastV.get();
+ EXPECT_EQ(0, RVH.DeletedCalls);
+ EXPECT_EQ(0, RVH.AURWCalls);
+ BitcastV.reset();
+ EXPECT_EQ(1, RVH.DeletedCalls);
+ EXPECT_EQ(0, RVH.AURWCalls);
+}
+
+TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
+ class RecordingVH : public CallbackVH {
+ public:
+ int DeletedCalls;
+ Value *AURWArgument;
+
+ RecordingVH() : DeletedCalls(0), AURWArgument(NULL) {}
+ RecordingVH(Value *V)
+ : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {}
+
+ private:
+ virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
+ virtual void allUsesReplacedWith(Value *new_value) {
+ EXPECT_EQ(NULL, AURWArgument);
+ AURWArgument = new_value;
+ }
+ };
+
+ RecordingVH RVH;
+ RVH = BitcastV.get();
+ EXPECT_EQ(0, RVH.DeletedCalls);
+ EXPECT_EQ(NULL, RVH.AURWArgument);
+ BitcastV->replaceAllUsesWith(ConstantV);
+ EXPECT_EQ(0, RVH.DeletedCalls);
+ EXPECT_EQ(ConstantV, RVH.AURWArgument);
+}
+
+TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
+ class RecoveringVH : public CallbackVH {
+ public:
+ int DeletedCalls;
+ Value *AURWArgument;
+ LLVMContext *Context;
+
+ RecoveringVH() : DeletedCalls(0), AURWArgument(NULL),
+ Context(&getGlobalContext()) {}
+ RecoveringVH(Value *V)
+ : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL),
+ Context(&getGlobalContext()) {}
+
+ private:
+ virtual void deleted() {
+ getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())));
+ setValPtr(NULL);
+ }
+ virtual void allUsesReplacedWith(Value *new_value) {
+ ASSERT_TRUE(NULL != getValPtr());
+ EXPECT_EQ(1U, getValPtr()->getNumUses());
+ EXPECT_EQ(NULL, AURWArgument);
+ AURWArgument = new_value;
+ }
+ };
+
+ // Normally, if a value has uses, deleting it will crash. However, we can use
+ // a CallbackVH to remove the uses before the check for no uses.
+ RecoveringVH RVH;
+ RVH = BitcastV.get();
+ std::auto_ptr<BinaryOperator> BitcastUser(
+ BinaryOperator::CreateAdd(RVH,
+ Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))));
+ EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
+ BitcastV.reset(); // Would crash without the ValueHandler.
+ EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument);
+ EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())),
+ BitcastUser->getOperand(0));
+}
+
+TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
+ // When a CallbackVH modifies other ValueHandles in its callbacks,
+ // that shouldn't interfere with non-modified ValueHandles receiving
+ // their appropriate callbacks.
+ //
+ // We create the active CallbackVH in the middle of a palindromic
+ // arrangement of other VHs so that the bad behavior would be
+ // triggered in whichever order callbacks run.
+
+ class DestroyingVH : public CallbackVH {
+ public:
+ OwningPtr<WeakVH> ToClear[2];
+ DestroyingVH(Value *V) {
+ ToClear[0].reset(new WeakVH(V));
+ setValPtr(V);
+ ToClear[1].reset(new WeakVH(V));
+ }
+ virtual void deleted() {
+ ToClear[0].reset();
+ ToClear[1].reset();
+ CallbackVH::deleted();
+ }
+ virtual void allUsesReplacedWith(Value *) {
+ ToClear[0].reset();
+ ToClear[1].reset();
+ }
+ };
+
+ {
+ WeakVH ShouldBeVisited1(BitcastV.get());
+ DestroyingVH C(BitcastV.get());
+ WeakVH ShouldBeVisited2(BitcastV.get());
+
+ BitcastV->replaceAllUsesWith(ConstantV);
+ EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
+ EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
+ }
+
+ {
+ WeakVH ShouldBeVisited1(BitcastV.get());
+ DestroyingVH C(BitcastV.get());
+ WeakVH ShouldBeVisited2(BitcastV.get());
+
+ BitcastV.reset();
+ EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited1));
+ EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited2));
+ }
+}
+
+TEST_F(ValueHandle, AssertingVHCheckedLast) {
+ // If a CallbackVH exists to clear out a group of AssertingVHs on
+ // Value deletion, the CallbackVH should get a chance to do so
+ // before the AssertingVHs assert.
+
+ class ClearingVH : public CallbackVH {
+ public:
+ AssertingVH<Value> *ToClear[2];
+ ClearingVH(Value *V,
+ AssertingVH<Value> &A0, AssertingVH<Value> &A1)
+ : CallbackVH(V) {
+ ToClear[0] = &A0;
+ ToClear[1] = &A1;
+ }
+
+ virtual void deleted() {
+ *ToClear[0] = 0;
+ *ToClear[1] = 0;
+ CallbackVH::deleted();
+ }
+ };
+
+ AssertingVH<Value> A1, A2;
+ A1 = BitcastV.get();
+ ClearingVH C(BitcastV.get(), A1, A2);
+ A2 = BitcastV.get();
+ // C.deleted() should run first, clearing the two AssertingVHs,
+ // which should prevent them from asserting.
+ BitcastV.reset();
+}
+
+}
ThreadLocalTest.cpp
TimeValueTest.cpp
UnicodeTest.cpp
- ValueHandleTest.cpp
YAMLIOTest.cpp
YAMLParserTest.cpp
formatted_raw_ostream_test.cpp
+++ /dev/null
-//===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Support/ValueHandle.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/IR/Constants.h"
-#include "llvm/IR/Instructions.h"
-#include "llvm/IR/LLVMContext.h"
-#include "gtest/gtest.h"
-#include <memory>
-
-using namespace llvm;
-
-namespace {
-
-class ValueHandle : public testing::Test {
-protected:
- Constant *ConstantV;
- std::auto_ptr<BitCastInst> BitcastV;
-
- ValueHandle() :
- ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
- BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) {
- }
-};
-
-class ConcreteCallbackVH : public CallbackVH {
-public:
- ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
-};
-
-TEST_F(ValueHandle, WeakVH_BasicOperation) {
- WeakVH WVH(BitcastV.get());
- EXPECT_EQ(BitcastV.get(), WVH);
- WVH = ConstantV;
- EXPECT_EQ(ConstantV, WVH);
-
- // Make sure I can call a method on the underlying Value. It
- // doesn't matter which method.
- EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType());
- EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
-}
-
-TEST_F(ValueHandle, WeakVH_Comparisons) {
- WeakVH BitcastWVH(BitcastV.get());
- WeakVH ConstantWVH(ConstantV);
-
- EXPECT_TRUE(BitcastWVH == BitcastWVH);
- EXPECT_TRUE(BitcastV.get() == BitcastWVH);
- EXPECT_TRUE(BitcastWVH == BitcastV.get());
- EXPECT_FALSE(BitcastWVH == ConstantWVH);
-
- EXPECT_TRUE(BitcastWVH != ConstantWVH);
- EXPECT_TRUE(BitcastV.get() != ConstantWVH);
- EXPECT_TRUE(BitcastWVH != ConstantV);
- EXPECT_FALSE(BitcastWVH != BitcastWVH);
-
- // Cast to Value* so comparisons work.
- Value *BV = BitcastV.get();
- Value *CV = ConstantV;
- EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
- EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
- EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
- EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
-
- EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
- EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
- EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
- EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
-
- EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
- EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
- EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
- EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
-}
-
-TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
- WeakVH WVH(BitcastV.get());
- WeakVH WVH_Copy(WVH);
- WeakVH WVH_Recreated(BitcastV.get());
- BitcastV->replaceAllUsesWith(ConstantV);
- EXPECT_EQ(ConstantV, WVH);
- EXPECT_EQ(ConstantV, WVH_Copy);
- EXPECT_EQ(ConstantV, WVH_Recreated);
-}
-
-TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
- WeakVH WVH(BitcastV.get());
- WeakVH WVH_Copy(WVH);
- WeakVH WVH_Recreated(BitcastV.get());
- BitcastV.reset();
- Value *null_value = NULL;
- EXPECT_EQ(null_value, WVH);
- EXPECT_EQ(null_value, WVH_Copy);
- EXPECT_EQ(null_value, WVH_Recreated);
-}
-
-
-TEST_F(ValueHandle, AssertingVH_BasicOperation) {
- AssertingVH<CastInst> AVH(BitcastV.get());
- CastInst *implicit_to_exact_type = AVH;
- (void)implicit_to_exact_type; // Avoid warning.
-
- AssertingVH<Value> GenericAVH(BitcastV.get());
- EXPECT_EQ(BitcastV.get(), GenericAVH);
- GenericAVH = ConstantV;
- EXPECT_EQ(ConstantV, GenericAVH);
-
- // Make sure I can call a method on the underlying CastInst. It
- // doesn't matter which method.
- EXPECT_FALSE(AVH->mayWriteToMemory());
- EXPECT_FALSE((*AVH).mayWriteToMemory());
-}
-
-TEST_F(ValueHandle, AssertingVH_Const) {
- const CastInst *ConstBitcast = BitcastV.get();
- AssertingVH<const CastInst> AVH(ConstBitcast);
- const CastInst *implicit_to_exact_type = AVH;
- (void)implicit_to_exact_type; // Avoid warning.
-}
-
-TEST_F(ValueHandle, AssertingVH_Comparisons) {
- AssertingVH<Value> BitcastAVH(BitcastV.get());
- AssertingVH<Value> ConstantAVH(ConstantV);
-
- EXPECT_TRUE(BitcastAVH == BitcastAVH);
- EXPECT_TRUE(BitcastV.get() == BitcastAVH);
- EXPECT_TRUE(BitcastAVH == BitcastV.get());
- EXPECT_FALSE(BitcastAVH == ConstantAVH);
-
- EXPECT_TRUE(BitcastAVH != ConstantAVH);
- EXPECT_TRUE(BitcastV.get() != ConstantAVH);
- EXPECT_TRUE(BitcastAVH != ConstantV);
- EXPECT_FALSE(BitcastAVH != BitcastAVH);
-
- // Cast to Value* so comparisons work.
- Value *BV = BitcastV.get();
- Value *CV = ConstantV;
- EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
- EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
- EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
- EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
-
- EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
- EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
- EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
- EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
-
- EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
- EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
- EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
- EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
-}
-
-TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
- AssertingVH<Value> AVH(BitcastV.get());
- BitcastV->replaceAllUsesWith(ConstantV);
- EXPECT_EQ(BitcastV.get(), AVH);
-}
-
-#ifdef NDEBUG
-
-TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
- EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
-}
-
-#else // !NDEBUG
-
-#ifdef GTEST_HAS_DEATH_TEST
-
-TEST_F(ValueHandle, AssertingVH_Asserts) {
- AssertingVH<Value> AVH(BitcastV.get());
- EXPECT_DEATH({BitcastV.reset();},
- "An asserting value handle still pointed to this value!");
- AssertingVH<Value> Copy(AVH);
- AVH = NULL;
- EXPECT_DEATH({BitcastV.reset();},
- "An asserting value handle still pointed to this value!");
- Copy = NULL;
- BitcastV.reset();
-}
-
-#endif // GTEST_HAS_DEATH_TEST
-
-#endif // NDEBUG
-
-TEST_F(ValueHandle, CallbackVH_BasicOperation) {
- ConcreteCallbackVH CVH(BitcastV.get());
- EXPECT_EQ(BitcastV.get(), CVH);
- CVH = ConstantV;
- EXPECT_EQ(ConstantV, CVH);
-
- // Make sure I can call a method on the underlying Value. It
- // doesn't matter which method.
- EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType());
- EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType());
-}
-
-TEST_F(ValueHandle, CallbackVH_Comparisons) {
- ConcreteCallbackVH BitcastCVH(BitcastV.get());
- ConcreteCallbackVH ConstantCVH(ConstantV);
-
- EXPECT_TRUE(BitcastCVH == BitcastCVH);
- EXPECT_TRUE(BitcastV.get() == BitcastCVH);
- EXPECT_TRUE(BitcastCVH == BitcastV.get());
- EXPECT_FALSE(BitcastCVH == ConstantCVH);
-
- EXPECT_TRUE(BitcastCVH != ConstantCVH);
- EXPECT_TRUE(BitcastV.get() != ConstantCVH);
- EXPECT_TRUE(BitcastCVH != ConstantV);
- EXPECT_FALSE(BitcastCVH != BitcastCVH);
-
- // Cast to Value* so comparisons work.
- Value *BV = BitcastV.get();
- Value *CV = ConstantV;
- EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
- EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
- EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
- EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
-
- EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
- EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
- EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
- EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
-
- EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
- EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
- EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
- EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
-}
-
-TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
- class RecordingVH : public CallbackVH {
- public:
- int DeletedCalls;
- int AURWCalls;
-
- RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
- RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
-
- private:
- virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
- virtual void allUsesReplacedWith(Value *) { AURWCalls++; }
- };
-
- RecordingVH RVH;
- RVH = BitcastV.get();
- EXPECT_EQ(0, RVH.DeletedCalls);
- EXPECT_EQ(0, RVH.AURWCalls);
- BitcastV.reset();
- EXPECT_EQ(1, RVH.DeletedCalls);
- EXPECT_EQ(0, RVH.AURWCalls);
-}
-
-TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
- class RecordingVH : public CallbackVH {
- public:
- int DeletedCalls;
- Value *AURWArgument;
-
- RecordingVH() : DeletedCalls(0), AURWArgument(NULL) {}
- RecordingVH(Value *V)
- : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {}
-
- private:
- virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
- virtual void allUsesReplacedWith(Value *new_value) {
- EXPECT_EQ(NULL, AURWArgument);
- AURWArgument = new_value;
- }
- };
-
- RecordingVH RVH;
- RVH = BitcastV.get();
- EXPECT_EQ(0, RVH.DeletedCalls);
- EXPECT_EQ(NULL, RVH.AURWArgument);
- BitcastV->replaceAllUsesWith(ConstantV);
- EXPECT_EQ(0, RVH.DeletedCalls);
- EXPECT_EQ(ConstantV, RVH.AURWArgument);
-}
-
-TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
- class RecoveringVH : public CallbackVH {
- public:
- int DeletedCalls;
- Value *AURWArgument;
- LLVMContext *Context;
-
- RecoveringVH() : DeletedCalls(0), AURWArgument(NULL),
- Context(&getGlobalContext()) {}
- RecoveringVH(Value *V)
- : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL),
- Context(&getGlobalContext()) {}
-
- private:
- virtual void deleted() {
- getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())));
- setValPtr(NULL);
- }
- virtual void allUsesReplacedWith(Value *new_value) {
- ASSERT_TRUE(NULL != getValPtr());
- EXPECT_EQ(1U, getValPtr()->getNumUses());
- EXPECT_EQ(NULL, AURWArgument);
- AURWArgument = new_value;
- }
- };
-
- // Normally, if a value has uses, deleting it will crash. However, we can use
- // a CallbackVH to remove the uses before the check for no uses.
- RecoveringVH RVH;
- RVH = BitcastV.get();
- std::auto_ptr<BinaryOperator> BitcastUser(
- BinaryOperator::CreateAdd(RVH,
- Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))));
- EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
- BitcastV.reset(); // Would crash without the ValueHandler.
- EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument);
- EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())),
- BitcastUser->getOperand(0));
-}
-
-TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
- // When a CallbackVH modifies other ValueHandles in its callbacks,
- // that shouldn't interfere with non-modified ValueHandles receiving
- // their appropriate callbacks.
- //
- // We create the active CallbackVH in the middle of a palindromic
- // arrangement of other VHs so that the bad behavior would be
- // triggered in whichever order callbacks run.
-
- class DestroyingVH : public CallbackVH {
- public:
- OwningPtr<WeakVH> ToClear[2];
- DestroyingVH(Value *V) {
- ToClear[0].reset(new WeakVH(V));
- setValPtr(V);
- ToClear[1].reset(new WeakVH(V));
- }
- virtual void deleted() {
- ToClear[0].reset();
- ToClear[1].reset();
- CallbackVH::deleted();
- }
- virtual void allUsesReplacedWith(Value *) {
- ToClear[0].reset();
- ToClear[1].reset();
- }
- };
-
- {
- WeakVH ShouldBeVisited1(BitcastV.get());
- DestroyingVH C(BitcastV.get());
- WeakVH ShouldBeVisited2(BitcastV.get());
-
- BitcastV->replaceAllUsesWith(ConstantV);
- EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
- EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
- }
-
- {
- WeakVH ShouldBeVisited1(BitcastV.get());
- DestroyingVH C(BitcastV.get());
- WeakVH ShouldBeVisited2(BitcastV.get());
-
- BitcastV.reset();
- EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited1));
- EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited2));
- }
-}
-
-TEST_F(ValueHandle, AssertingVHCheckedLast) {
- // If a CallbackVH exists to clear out a group of AssertingVHs on
- // Value deletion, the CallbackVH should get a chance to do so
- // before the AssertingVHs assert.
-
- class ClearingVH : public CallbackVH {
- public:
- AssertingVH<Value> *ToClear[2];
- ClearingVH(Value *V,
- AssertingVH<Value> &A0, AssertingVH<Value> &A1)
- : CallbackVH(V) {
- ToClear[0] = &A0;
- ToClear[1] = &A1;
- }
-
- virtual void deleted() {
- *ToClear[0] = 0;
- *ToClear[1] = 0;
- CallbackVH::deleted();
- }
- };
-
- AssertingVH<Value> A1, A2;
- A1 = BitcastV.get();
- ClearingVH C(BitcastV.get(), A1, A2);
- A2 = BitcastV.get();
- // C.deleted() should run first, clearing the two AssertingVHs,
- // which should prevent them from asserting.
- BitcastV.reset();
-}
-
-}