ValueHandle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. WeakVH &operator=(const WeakVH &RHS) = default;
  129. Value *operator=(Value *RHS) {
  130. return ValueHandleBase::operator=(RHS);
  131. }
  132. Value *operator=(const ValueHandleBase &RHS) {
  133. return ValueHandleBase::operator=(RHS);
  134. }
  135. operator Value*() const {
  136. return getValPtr();
  137. }
  138. };
  139. // Specialize simplify_type to allow WeakVH to participate in
  140. // dyn_cast, isa, etc.
  141. template <> struct simplify_type<WeakVH> {
  142. typedef Value *SimpleType;
  143. static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
  144. };
  145. template <> struct simplify_type<const WeakVH> {
  146. typedef Value *SimpleType;
  147. static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
  148. };
  149. /// \brief Value handle that asserts if the Value is deleted.
  150. ///
  151. /// This is a Value Handle that points to a value and asserts out if the value
  152. /// is destroyed while the handle is still live. This is very useful for
  153. /// catching dangling pointer bugs and other things which can be non-obvious.
  154. /// One particularly useful place to use this is as the Key of a map. Dangling
  155. /// pointer bugs often lead to really subtle bugs that only occur if another
  156. /// object happens to get allocated to the same address as the old one. Using
  157. /// an AssertingVH ensures that an assert is triggered as soon as the bad
  158. /// delete occurs.
  159. ///
  160. /// Note that an AssertingVH handle does *not* follow values across RAUW
  161. /// operations. This means that RAUW's need to explicitly update the
  162. /// AssertingVH's as it moves. This is required because in non-assert mode this
  163. /// class turns into a trivial wrapper around a pointer.
  164. template <typename ValueTy>
  165. class AssertingVH
  166. #ifndef NDEBUG
  167. : public ValueHandleBase
  168. #endif
  169. {
  170. friend struct DenseMapInfo<AssertingVH<ValueTy> >;
  171. #ifndef NDEBUG
  172. Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
  173. void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
  174. #else
  175. Value *ThePtr;
  176. Value *getRawValPtr() const { return ThePtr; }
  177. void setRawValPtr(Value *P) { ThePtr = P; }
  178. #endif
  179. // Convert a ValueTy*, which may be const, to the raw Value*.
  180. static Value *GetAsValue(Value *V) { return V; }
  181. static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
  182. ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
  183. void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
  184. public:
  185. #ifndef NDEBUG
  186. AssertingVH() : ValueHandleBase(Assert) {}
  187. AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
  188. AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
  189. #else
  190. AssertingVH() : ThePtr(nullptr) {}
  191. AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
  192. AssertingVH(const AssertingVH<ValueTy> &) = default;
  193. #endif
  194. operator ValueTy*() const {
  195. return getValPtr();
  196. }
  197. ValueTy *operator=(ValueTy *RHS) {
  198. setValPtr(RHS);
  199. return getValPtr();
  200. }
  201. ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
  202. setValPtr(RHS.getValPtr());
  203. return getValPtr();
  204. }
  205. ValueTy *operator->() const { return getValPtr(); }
  206. ValueTy &operator*() const { return *getValPtr(); }
  207. };
  208. // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
  209. template<typename T>
  210. struct DenseMapInfo<AssertingVH<T> > {
  211. static inline AssertingVH<T> getEmptyKey() {
  212. AssertingVH<T> Res;
  213. Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
  214. return Res;
  215. }
  216. static inline AssertingVH<T> getTombstoneKey() {
  217. AssertingVH<T> Res;
  218. Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
  219. return Res;
  220. }
  221. static unsigned getHashValue(const AssertingVH<T> &Val) {
  222. return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
  223. }
  224. static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
  225. return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
  226. RHS.getRawValPtr());
  227. }
  228. };
  229. template <typename T>
  230. struct isPodLike<AssertingVH<T> > {
  231. #ifdef NDEBUG
  232. static const bool value = true;
  233. #else
  234. static const bool value = false;
  235. #endif
  236. };
  237. /// \brief Value handle that tracks a Value across RAUW.
  238. ///
  239. /// TrackingVH is designed for situations where a client needs to hold a handle
  240. /// to a Value (or subclass) across some operations which may move that value,
  241. /// but should never destroy it or replace it with some unacceptable type.
  242. ///
  243. /// It is an error to do anything with a TrackingVH whose value has been
  244. /// destroyed, except to destruct it.
  245. ///
  246. /// It is an error to attempt to replace a value with one of a type which is
  247. /// incompatible with any of its outstanding TrackingVHs.
  248. template<typename ValueTy>
  249. class TrackingVH : public ValueHandleBase {
  250. void CheckValidity() const {
  251. Value *VP = ValueHandleBase::getValPtr();
  252. // Null is always ok.
  253. if (!VP) return;
  254. // Check that this value is valid (i.e., it hasn't been deleted). We
  255. // explicitly delay this check until access to avoid requiring clients to be
  256. // unnecessarily careful w.r.t. destruction.
  257. assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
  258. // Check that the value is a member of the correct subclass. We would like
  259. // to check this property on assignment for better debugging, but we don't
  260. // want to require a virtual interface on this VH. Instead we allow RAUW to
  261. // replace this value with a value of an invalid type, and check it here.
  262. assert(isa<ValueTy>(VP) &&
  263. "Tracked Value was replaced by one with an invalid type!");
  264. }
  265. ValueTy *getValPtr() const {
  266. CheckValidity();
  267. return (ValueTy*)ValueHandleBase::getValPtr();
  268. }
  269. void setValPtr(ValueTy *P) {
  270. CheckValidity();
  271. ValueHandleBase::operator=(GetAsValue(P));
  272. }
  273. // Convert a ValueTy*, which may be const, to the type the base
  274. // class expects.
  275. static Value *GetAsValue(Value *V) { return V; }
  276. static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
  277. public:
  278. TrackingVH() : ValueHandleBase(Tracking) {}
  279. TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
  280. TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
  281. operator ValueTy*() const {
  282. return getValPtr();
  283. }
  284. ValueTy *operator=(ValueTy *RHS) {
  285. setValPtr(RHS);
  286. return getValPtr();
  287. }
  288. ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
  289. setValPtr(RHS.getValPtr());
  290. return getValPtr();
  291. }
  292. ValueTy *operator->() const { return getValPtr(); }
  293. ValueTy &operator*() const { return *getValPtr(); }
  294. };
  295. /// \brief Value handle with callbacks on RAUW and destruction.
  296. ///
  297. /// This is a value handle that allows subclasses to define callbacks that run
  298. /// when the underlying Value has RAUW called on it or is destroyed. This
  299. /// class can be used as the key of a map, as long as the user takes it out of
  300. /// the map before calling setValPtr() (since the map has to rearrange itself
  301. /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable
  302. /// and a virtual destructor.
  303. class CallbackVH : public ValueHandleBase {
  304. virtual void anchor();
  305. protected:
  306. CallbackVH(const CallbackVH &RHS)
  307. : ValueHandleBase(Callback, RHS) {}
  308. virtual ~CallbackVH() {}
  309. void setValPtr(Value *P) {
  310. ValueHandleBase::operator=(P);
  311. }
  312. public:
  313. CallbackVH() : ValueHandleBase(Callback) {}
  314. CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
  315. operator Value*() const {
  316. return getValPtr();
  317. }
  318. /// \brief Callback for Value destruction.
  319. ///
  320. /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
  321. /// may call any non-virtual Value method on getValPtr(), but no subclass
  322. /// methods. If WeakVH were implemented as a CallbackVH, it would use this
  323. /// method to call setValPtr(NULL). AssertingVH would use this method to
  324. /// cause an assertion failure.
  325. ///
  326. /// All implementations must remove the reference from this object to the
  327. /// Value that's being destroyed.
  328. virtual void deleted() { setValPtr(nullptr); }
  329. /// \brief Callback for Value RAUW.
  330. ///
  331. /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
  332. /// _before_ any of the uses have actually been replaced. If WeakVH were
  333. /// implemented as a CallbackVH, it would use this method to call
  334. /// setValPtr(new_value). AssertingVH would do nothing in this method.
  335. virtual void allUsesReplacedWith(Value *) {}
  336. };
  337. } // End llvm namespace
  338. #endif