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.

216 lines
7.1 KiB

  1. //===-- llvm/User.h - User class definition ---------------------*- 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 class defines the interface that one who uses a Value must implement.
  11. // Each instance of the Value class keeps track of what User's have handles
  12. // to it.
  13. //
  14. // * Instructions are the largest class of Users.
  15. // * Constants may be users of other constants (think arrays and stuff)
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_USER_H
  19. #define LLVM_USER_H
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Value.h"
  22. namespace llvm {
  23. /// OperandTraits - Compile-time customization of
  24. /// operand-related allocators and accessors
  25. /// for use of the User class
  26. template <class>
  27. struct OperandTraits;
  28. class User : public Value {
  29. User(const User &) LLVM_DELETED_FUNCTION;
  30. void *operator new(size_t) LLVM_DELETED_FUNCTION;
  31. template <unsigned>
  32. friend struct HungoffOperandTraits;
  33. virtual void anchor();
  34. protected:
  35. /// OperandList - This is a pointer to the array of Uses for this User.
  36. /// For nodes of fixed arity (e.g. a binary operator) this array will live
  37. /// prefixed to some derived class instance. For nodes of resizable variable
  38. /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
  39. /// allocated and should be destroyed by the classes' virtual dtor.
  40. Use *OperandList;
  41. /// NumOperands - The number of values used by this User.
  42. ///
  43. unsigned NumOperands;
  44. void *operator new(size_t s, unsigned Us);
  45. User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
  46. : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
  47. Use *allocHungoffUses(unsigned) const;
  48. void dropHungoffUses() {
  49. Use::zap(OperandList, OperandList + NumOperands, true);
  50. OperandList = 0;
  51. // Reset NumOperands so User::operator delete() does the right thing.
  52. NumOperands = 0;
  53. }
  54. public:
  55. ~User() {
  56. Use::zap(OperandList, OperandList + NumOperands);
  57. }
  58. /// operator delete - free memory allocated for User and Use objects
  59. void operator delete(void *Usr);
  60. /// placement delete - required by std, but never called.
  61. void operator delete(void*, unsigned) {
  62. llvm_unreachable("Constructor throws?");
  63. }
  64. /// placement delete - required by std, but never called.
  65. void operator delete(void*, unsigned, bool) {
  66. llvm_unreachable("Constructor throws?");
  67. }
  68. protected:
  69. template <int Idx, typename U> static Use &OpFrom(const U *that) {
  70. return Idx < 0
  71. ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
  72. : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
  73. }
  74. template <int Idx> Use &Op() {
  75. return OpFrom<Idx>(this);
  76. }
  77. template <int Idx> const Use &Op() const {
  78. return OpFrom<Idx>(this);
  79. }
  80. public:
  81. Value *getOperand(unsigned i) const {
  82. assert(i < NumOperands && "getOperand() out of range!");
  83. return OperandList[i];
  84. }
  85. void setOperand(unsigned i, Value *Val) {
  86. assert(i < NumOperands && "setOperand() out of range!");
  87. assert((!isa<Constant>((const Value*)this) ||
  88. isa<GlobalValue>((const Value*)this)) &&
  89. "Cannot mutate a constant with setOperand!");
  90. OperandList[i] = Val;
  91. }
  92. const Use &getOperandUse(unsigned i) const {
  93. assert(i < NumOperands && "getOperandUse() out of range!");
  94. return OperandList[i];
  95. }
  96. Use &getOperandUse(unsigned i) {
  97. assert(i < NumOperands && "getOperandUse() out of range!");
  98. return OperandList[i];
  99. }
  100. unsigned getNumOperands() const { return NumOperands; }
  101. // ---------------------------------------------------------------------------
  102. // Operand Iterator interface...
  103. //
  104. typedef Use* op_iterator;
  105. typedef const Use* const_op_iterator;
  106. inline op_iterator op_begin() { return OperandList; }
  107. inline const_op_iterator op_begin() const { return OperandList; }
  108. inline op_iterator op_end() { return OperandList+NumOperands; }
  109. inline const_op_iterator op_end() const { return OperandList+NumOperands; }
  110. /// Convenience iterator for directly iterating over the Values in the
  111. /// OperandList
  112. class value_op_iterator : public std::iterator<std::forward_iterator_tag,
  113. Value*> {
  114. op_iterator OI;
  115. public:
  116. explicit value_op_iterator(Use *U) : OI(U) {}
  117. bool operator==(const value_op_iterator &x) const {
  118. return OI == x.OI;
  119. }
  120. bool operator!=(const value_op_iterator &x) const {
  121. return !operator==(x);
  122. }
  123. /// Iterator traversal: forward iteration only
  124. value_op_iterator &operator++() { // Preincrement
  125. ++OI;
  126. return *this;
  127. }
  128. value_op_iterator operator++(int) { // Postincrement
  129. value_op_iterator tmp = *this; ++*this; return tmp;
  130. }
  131. /// Retrieve a pointer to the current Value.
  132. Value *operator*() const {
  133. return *OI;
  134. }
  135. Value *operator->() const { return operator*(); }
  136. };
  137. inline value_op_iterator value_op_begin() {
  138. return value_op_iterator(op_begin());
  139. }
  140. inline value_op_iterator value_op_end() {
  141. return value_op_iterator(op_end());
  142. }
  143. // dropAllReferences() - This function is in charge of "letting go" of all
  144. // objects that this User refers to. This allows one to
  145. // 'delete' a whole class at a time, even though there may be circular
  146. // references... First all references are dropped, and all use counts go to
  147. // zero. Then everything is deleted for real. Note that no operations are
  148. // valid on an object that has "dropped all references", except operator
  149. // delete.
  150. //
  151. void dropAllReferences() {
  152. for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
  153. i->set(0);
  154. }
  155. /// replaceUsesOfWith - Replaces all references to the "From" definition with
  156. /// references to the "To" definition.
  157. ///
  158. void replaceUsesOfWith(Value *From, Value *To);
  159. // Methods for support type inquiry through isa, cast, and dyn_cast:
  160. static inline bool classof(const User *) { return true; }
  161. static inline bool classof(const Value *V) {
  162. return isa<Instruction>(V) || isa<Constant>(V);
  163. }
  164. };
  165. template<> struct simplify_type<User::op_iterator> {
  166. typedef Value* SimpleType;
  167. static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
  168. return static_cast<SimpleType>(Val->get());
  169. }
  170. };
  171. template<> struct simplify_type<const User::op_iterator>
  172. : public simplify_type<User::op_iterator> {};
  173. template<> struct simplify_type<User::const_op_iterator> {
  174. typedef Value* SimpleType;
  175. static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
  176. return static_cast<SimpleType>(Val->get());
  177. }
  178. };
  179. template<> struct simplify_type<const User::const_op_iterator>
  180. : public simplify_type<User::const_op_iterator> {};
  181. // value_use_iterator::getOperandNo - Requires the definition of the User class.
  182. template<typename UserTy>
  183. unsigned value_use_iterator<UserTy>::getOperandNo() const {
  184. return U - U->getUser()->op_begin();
  185. }
  186. } // End llvm namespace
  187. #endif