Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
12 KiB

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