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.

160 lines
5.7 KiB

  1. //===-- llvm/OperandTraits.h - OperandTraits 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 file defines the traits classes that are handy for enforcing the correct
  11. // layout of various User subclasses. It also provides the means for accessing
  12. // the operands in the most efficient manner.
  13. //
  14. #ifndef LLVM_IR_OPERANDTRAITS_H
  15. #define LLVM_IR_OPERANDTRAITS_H
  16. #include "llvm/IR/User.h"
  17. namespace llvm {
  18. //===----------------------------------------------------------------------===//
  19. // FixedNumOperand Trait Class
  20. //===----------------------------------------------------------------------===//
  21. /// FixedNumOperandTraits - determine the allocation regime of the Use array
  22. /// when it is a prefix to the User object, and the number of Use objects is
  23. /// known at compile time.
  24. template <typename SubClass, unsigned ARITY>
  25. struct FixedNumOperandTraits {
  26. static Use *op_begin(SubClass* U) {
  27. return reinterpret_cast<Use*>(U) - ARITY;
  28. }
  29. static Use *op_end(SubClass* U) {
  30. return reinterpret_cast<Use*>(U);
  31. }
  32. static unsigned operands(const User*) {
  33. return ARITY;
  34. }
  35. };
  36. //===----------------------------------------------------------------------===//
  37. // OptionalOperand Trait Class
  38. //===----------------------------------------------------------------------===//
  39. /// OptionalOperandTraits - when the number of operands may change at runtime.
  40. /// Naturally it may only decrease, because the allocations may not change.
  41. template <typename SubClass, unsigned ARITY = 1>
  42. struct OptionalOperandTraits : public FixedNumOperandTraits<SubClass, ARITY> {
  43. static unsigned operands(const User *U) {
  44. return U->getNumOperands();
  45. }
  46. };
  47. //===----------------------------------------------------------------------===//
  48. // VariadicOperand Trait Class
  49. //===----------------------------------------------------------------------===//
  50. /// VariadicOperandTraits - determine the allocation regime of the Use array
  51. /// when it is a prefix to the User object, and the number of Use objects is
  52. /// only known at allocation time.
  53. template <typename SubClass, unsigned MINARITY = 0>
  54. struct VariadicOperandTraits {
  55. static Use *op_begin(SubClass* U) {
  56. return reinterpret_cast<Use*>(U) - static_cast<User*>(U)->getNumOperands();
  57. }
  58. static Use *op_end(SubClass* U) {
  59. return reinterpret_cast<Use*>(U);
  60. }
  61. static unsigned operands(const User *U) {
  62. return U->getNumOperands();
  63. }
  64. };
  65. //===----------------------------------------------------------------------===//
  66. // HungoffOperand Trait Class
  67. //===----------------------------------------------------------------------===//
  68. /// HungoffOperandTraits - determine the allocation regime of the Use array
  69. /// when it is not a prefix to the User object, but allocated at an unrelated
  70. /// heap address.
  71. /// Assumes that the User subclass that is determined by this traits class
  72. /// has an OperandList member of type User::op_iterator. [Note: this is now
  73. /// trivially satisfied, because User has that member for historic reasons.]
  74. ///
  75. /// This is the traits class that is needed when the Use array must be
  76. /// resizable.
  77. template <unsigned MINARITY = 1>
  78. struct HungoffOperandTraits {
  79. static Use *op_begin(User* U) {
  80. return U->OperandList;
  81. }
  82. static Use *op_end(User* U) {
  83. return U->OperandList + U->getNumOperands();
  84. }
  85. static unsigned operands(const User *U) {
  86. return U->getNumOperands();
  87. }
  88. };
  89. /// Macro for generating in-class operand accessor declarations.
  90. /// It should only be called in the public section of the interface.
  91. ///
  92. #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
  93. public: \
  94. inline VALUECLASS *getOperand(unsigned) const; \
  95. inline void setOperand(unsigned, VALUECLASS*); \
  96. inline op_iterator op_begin(); \
  97. inline const_op_iterator op_begin() const; \
  98. inline op_iterator op_end(); \
  99. inline const_op_iterator op_end() const; \
  100. protected: \
  101. template <int> inline Use &Op(); \
  102. template <int> inline const Use &Op() const; \
  103. public: \
  104. inline unsigned getNumOperands() const
  105. /// Macro for generating out-of-class operand accessor definitions
  106. #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
  107. CLASS::op_iterator CLASS::op_begin() { \
  108. return OperandTraits<CLASS>::op_begin(this); \
  109. } \
  110. CLASS::const_op_iterator CLASS::op_begin() const { \
  111. return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
  112. } \
  113. CLASS::op_iterator CLASS::op_end() { \
  114. return OperandTraits<CLASS>::op_end(this); \
  115. } \
  116. CLASS::const_op_iterator CLASS::op_end() const { \
  117. return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
  118. } \
  119. VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
  120. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  121. && "getOperand() out of range!"); \
  122. return cast_or_null<VALUECLASS>( \
  123. OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture].get()); \
  124. } \
  125. void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
  126. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  127. && "setOperand() out of range!"); \
  128. OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
  129. } \
  130. unsigned CLASS::getNumOperands() const { \
  131. return OperandTraits<CLASS>::operands(this); \
  132. } \
  133. template <int Idx_nocapture> Use &CLASS::Op() { \
  134. return this->OpFrom<Idx_nocapture>(this); \
  135. } \
  136. template <int Idx_nocapture> const Use &CLASS::Op() const { \
  137. return this->OpFrom<Idx_nocapture>(this); \
  138. }
  139. } // End llvm namespace
  140. #endif