ValueHandle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //===- ValueHandle.h - Value Smart Pointer classes --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the ValueHandle class and its sub-classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_VALUEHANDLE_H
  14. #define LLVM_IR_VALUEHANDLE_H
  15. #include "llvm/ADT/DenseMapInfo.h"
  16. #include "llvm/ADT/PointerIntPair.h"
  17. #include "llvm/IR/Value.h"
  18. namespace llvm {
  19. class ValueHandleBase;
  20. template<typename From> struct simplify_type;
  21. // ValueHandleBase** is only 4-byte aligned.
  22. template<>
  23. class PointerLikeTypeTraits<ValueHandleBase**> {
  24. public:
  25. static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
  26. static inline ValueHandleBase **getFromVoidPointer(void *P) {
  27. return static_cast<ValueHandleBase**>(P);
  28. }
  29. enum { NumLowBitsAvailable = 2 };
  30. };
  31. /// \brief This is the common base class of value handles.
  32. ///
  33. /// ValueHandle's are smart pointers to Value's that have special behavior when
  34. /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
  35. /// below for details.
  36. class ValueHandleBase {
  37. friend class Value;
  38. protected:
  39. /// \brief This indicates what sub class the handle actually is.
  40. ///
  41. /// This is to avoid having a vtable for the light-weight handle pointers. The
  42. /// fully general Callback version does have a vtable.
  43. enum HandleBaseKind {
  44. Assert,
  45. Callback,
  46. Tracking,
  47. Weak
  48. };
  49. private:
  50. PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
  51. ValueHandleBase *Next;
  52. Value* V;
  53. ValueHandleBase(const ValueHandleBase&) = delete;
  54. public:
  55. explicit ValueHandleBase(HandleBaseKind Kind)
  56. : PrevPair(nullptr, Kind), Next(nullptr), V(nullptr) {}
  57. ValueHandleBase(HandleBaseKind Kind, Value *V)
  58. : PrevPair(nullptr, Kind), Next(nullptr), V(V) {
  59. if (isValid(V))
  60. AddToUseList();
  61. }
  62. ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
  63. : PrevPair(nullptr, Kind), Next(nullptr), V(RHS.V) {
  64. if (isValid(V))
  65. AddToExistingUseList(RHS.getPrevPtr());
  66. }
  67. ~ValueHandleBase() {
  68. if (isValid(V))
  69. RemoveFromUseList();
  70. }
  71. Value *operator=(Value *RHS) {
  72. if (V == RHS) return RHS;
  73. if (isValid(V)) RemoveFromUseList();
  74. V = RHS;
  75. if (isValid(V)) AddToUseList();
  76. return RHS;
  77. }
  78. Value *operator=(const ValueHandleBase &RHS) {
  79. if (V == RHS.V) return RHS.V;
  80. if (isValid(V)) RemoveFromUseList();
  81. V = RHS.V;
  82. if (isValid(V)) AddToExistingUseList(RHS.getPrevPtr());
  83. return V;
  84. }
  85. Value *operator->() const { return V; }
  86. Value &operator*() const { return *V; }
  87. protected:
  88. Value *getValPtr() const { return V; }
  89. static bool isValid(Value *V) {
  90. return V &&
  91. V != DenseMapInfo<Value *>::getEmptyKey() &&
  92. V != DenseMapInfo<Value *>::getTombstoneKey();
  93. }
  94. public:
  95. // Callbacks made from Value.
  96. static void ValueIsDeleted(Value *V);
  97. static void ValueIsRAUWd(Value *Old, Value *New);
  98. private:
  99. // Internal implementation details.
  100. ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
  101. HandleBaseKind getKind() const { return PrevPair.getInt(); }
  102. void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
  103. /// \brief Add this ValueHandle to the use list for V.
  104. ///
  105. /// List is the address of either the head of the list or a Next node within
  106. /// the existing use list.
  107. void AddToExistingUseList(ValueHandleBase **List);
  108. /// \brief Add this ValueHandle to the use list after Node.
  109. void AddToExistingUseListAfter(ValueHandleBase *Node);
  110. /// \brief Add this ValueHandle to the use list for V.
  111. void AddToUseList();
  112. /// \brief Remove this ValueHandle from its current use list.
  113. void RemoveFromUseList();
  114. };
  115. /// \brief Value handle that is nullable, but tries to track the Value.
  116. ///
  117. /// This is a value handle that tries hard to point to a Value, even across
  118. /// RAUW operations, but will null itself out if the value is destroyed. this
  119. /// is useful for advisory sorts of information, but should not be used as the
  120. /// key of a map (since the map would have to rearrange itself when the pointer
  121. /// changes).
  122. class WeakVH : public ValueHandleBase {
  123. public:
  124. WeakVH() : ValueHandleBase(Weak) {}
  125. WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
  126. WeakVH(const WeakVH &RHS)
  127. : ValueHandleBase(Weak, RHS) {}
  128. Value *operator=(Value *RHS) {
  129. return ValueHandleBase::operator=(RHS);
  130. }
  131. Value *operator=(const ValueHandleBase &RHS) {
  132. return ValueHandleBase::operator=(RHS);
  133. }
  134. operator Value*() const {
  135. return getValPtr();
  136. }
  137. };
  138. // Specialize simplify_type to allow WeakVH to participate in
  139. // dyn_cast, isa, etc.
  140. template <> struct simplify_type<WeakVH> {
  141. typedef Value *SimpleType;
  142. static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
  143. };
  144. template <> struct simplify_type<const WeakVH> {
  145. typedef Value *SimpleType;
  146. static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
  147. };
  148. /// \brief Value handle that asserts if the Value is deleted.
  149. ///
  150. /// This is a Value Handle that points to a value and asserts out if the value
  151. /// is destroyed while the handle is still live. This is very useful for
  152. /// catching dangling pointer bugs and other things which can be non-obvious.
  153. /// One particularly useful place to use this is as the Key of a map. Dangling
  154. /// pointer bugs often lead to really subtle bugs that only occur if another
  155. /// object happens to get allocated to the same address as the old one. Using
  156. /// an AssertingVH ensures that an assert is triggered as soon as the bad
  157. /// delete occurs.
  158. ///
  159. /// Note that an AssertingVH handle does *not* follow values across RAUW
  160. /// operations. This means that RAUW's need to explicitly update the
  161. /// AssertingVH's as it moves. This is required because in non-assert mode this
  162. /// class turns into a trivial wrapper around a pointer.
  163. template <typename ValueTy>
  164. class AssertingVH
  165. #ifndef NDEBUG
  166. : public ValueHandleBase
  167. #endif
  168. {
  169. friend struct DenseMapInfo<AssertingVH<ValueTy> >;
  170. #ifndef NDEBUG
  171. Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
  172. void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
  173. #else
  174. Value *ThePtr;
  175. Value *getRawValPtr() const { return ThePtr; }
  176. void setRawValPtr(Value *P) { ThePtr = P; }
  177. #endif
  178. // Convert a ValueTy*, which may be const, to the raw Value*.
  179. static Value *GetAsValue(Value *V) { return V; }
  180. static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
  181. ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
  182. void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
  183. public:
  184. #ifndef NDEBUG
  185. AssertingVH() : ValueHandleBase(Assert) {}
  186. AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
  187. AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
  188. #else
  189. AssertingVH() : ThePtr(nullptr) {}
  190. AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
  191. #endif
  192. operator ValueTy*() const {
  193. return getValPtr();
  194. }
  195. ValueTy *operator=(ValueTy *RHS) {
  196. setValPtr(RHS);
  197. return getValPtr();
  198. }
  199. ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
  200. setValPtr(RHS.getValPtr());
  201. return getValPtr();
  202. }
  203. ValueTy *operator->() const { return getValPtr(); }
  204. ValueTy &operator*() const { return *getValPtr(); }
  205. };
  206. // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
  207. template<typename T>
  208. struct DenseMapInfo<AssertingVH<T> > {
  209. static inline AssertingVH<T> getEmptyKey() {
  210. AssertingVH<T> Res;
  211. Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
  212. return Res;
  213. }
  214. static inline AssertingVH<T> getTombstoneKey() {
  215. AssertingVH<T> Res;
  216. Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
  217. return Res;
  218. }
  219. static unsigned getHashValue(const AssertingVH<T> &Val) {
  220. return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
  221. }
  222. static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
  223. return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
  224. RHS.getRawValPtr());
  225. }
  226. };
  227. template <typename T>
  228. struct isPodLike<AssertingVH<T> > {
  229. #ifdef NDEBUG
  230. static const bool value = true;
  231. #else
  232. static const bool value = false;
  233. #endif
  234. };
  235. /// \brief Value handle that tracks a Value across RAUW.
  236. ///
  237. /// TrackingVH is designed for situations where a client needs to hold a handle
  238. /// to a Value (or subclass) across some operations which may move that value,
  239. /// but should never destroy it or replace it with some unacceptable type.
  240. ///
  241. /// It is an error to do anything with a TrackingVH whose value has been
  242. /// destroyed, except to destruct it.
  243. ///
  244. /// It is an error to attempt to replace a value with one of a type which is
  245. /// incompatible with any of its outstanding TrackingVHs.
  246. template<typename ValueTy>
  247. class TrackingVH : public ValueHandleBase {
  248. void CheckValidity() const {
  249. Value *VP = ValueHandleBase::getValPtr();
  250. // Null is always ok.
  251. if (!VP) return;
  252. // Check that this value is valid (i.e., it hasn't been deleted). We
  253. // explicitly delay this check until access to avoid requiring clients to be
  254. // unnecessarily careful w.r.t. destruction.
  255. assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
  256. // Check that the value is a member of the correct subclass. We would like
  257. // to check this property on assignment for better debugging, but we don't
  258. // want to require a virtual interface on this VH. Instead we allow RAUW to
  259. // replace this value with a value of an invalid type, and check it here.
  260. assert(isa<ValueTy>(VP) &&
  261. "Tracked Value was replaced by one with an invalid type!");
  262. }
  263. ValueTy *getValPtr() const {
  264. CheckValidity();
  265. return (ValueTy*)ValueHandleBase::getValPtr();
  266. }
  267. void setValPtr(ValueTy *P) {
  268. CheckValidity();
  269. ValueHandleBase::operator=(GetAsValue(P));
  270. }
  271. // Convert a ValueTy*, which may be const, to the type the base
  272. // class expects.
  273. static Value *GetAsValue(Value *V) { return V; }
  274. static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
  275. public:
  276. TrackingVH() : ValueHandleBase(Tracking) {}
  277. TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
  278. TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
  279. operator ValueTy*() const {
  280. return getValPtr();
  281. }
  282. ValueTy *operator=(ValueTy *RHS) {
  283. setValPtr(RHS);
  284. return getValPtr();
  285. }
  286. ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
  287. setValPtr(RHS.getValPtr());
  288. return getValPtr();
  289. }
  290. ValueTy *operator->() const { return getValPtr(); }
  291. ValueTy &operator*() const { return *getValPtr(); }
  292. };
  293. /// \brief Value handle with callbacks on RAUW and destruction.
  294. ///
  295. /// This is a value handle that allows subclasses to define callbacks that run
  296. /// when the underlying Value has RAUW called on it or is destroyed. This
  297. /// class can be used as the key of a map, as long as the user takes it out of
  298. /// the map before calling setValPtr() (since the map has to rearrange itself
  299. /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable
  300. /// and a virtual destructor.
  301. class CallbackVH : public ValueHandleBase {
  302. virtual void anchor();
  303. protected:
  304. CallbackVH(const CallbackVH &RHS)
  305. : ValueHandleBase(Callback, RHS) {}
  306. virtual ~CallbackVH() {}
  307. void setValPtr(Value *P) {
  308. ValueHandleBase::operator=(P);
  309. }
  310. public:
  311. CallbackVH() : ValueHandleBase(Callback) {}
  312. CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
  313. operator Value*() const {
  314. return getValPtr();
  315. }
  316. /// \brief Callback for Value destruction.
  317. ///
  318. /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
  319. /// may call any non-virtual Value method on getValPtr(), but no subclass
  320. /// methods. If WeakVH were implemented as a CallbackVH, it would use this
  321. /// method to call setValPtr(NULL). AssertingVH would use this method to
  322. /// cause an assertion failure.
  323. ///
  324. /// All implementations must remove the reference from this object to the
  325. /// Value that's being destroyed.
  326. virtual void deleted() { setValPtr(nullptr); }
  327. /// \brief Callback for Value RAUW.
  328. ///
  329. /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
  330. /// _before_ any of the uses have actually been replaced. If WeakVH were
  331. /// implemented as a CallbackVH, it would use this method to call
  332. /// setValPtr(new_value). AssertingVH would do nothing in this method.
  333. virtual void allUsesReplacedWith(Value *) {}
  334. };
  335. } // End llvm namespace
  336. #endif