OperandTraits.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. ///////////////////////////////////////////////////////////////////////////////
  69. /// HungoffOperandTraits - determine the allocation regime of the Use array
  70. /// when it is not a prefix to the User object, but allocated at an unrelated
  71. /// heap address.
  72. /// Assumes that the User subclass that is determined by this traits class
  73. /// has an OperandList member of type User::op_iterator. [Note: this is now
  74. /// trivially satisfied, because User has that member for historic reasons.]
  75. ///
  76. /// This is the traits class that is needed when the Use array must be
  77. /// resizable.
  78. template <unsigned MINARITY = 1>
  79. struct HungoffOperandTraits {
  80. static Use *op_begin(User* U) {
  81. return U->getOperandList();
  82. }
  83. static Use *op_end(User* U) {
  84. return U->getOperandList() + U->getNumOperands();
  85. }
  86. static unsigned operands(const User *U) {
  87. return U->getNumOperands();
  88. }
  89. };
  90. /// Macro for generating in-class operand accessor declarations.
  91. /// It should only be called in the public section of the interface.
  92. ///
  93. #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
  94. public: \
  95. inline VALUECLASS *getOperand(unsigned) const; \
  96. inline void setOperand(unsigned, VALUECLASS*); \
  97. inline op_iterator op_begin(); \
  98. inline const_op_iterator op_begin() const; \
  99. inline op_iterator op_end(); \
  100. inline const_op_iterator op_end() const; \
  101. protected: \
  102. template <int> inline Use &Op(); \
  103. template <int> inline const Use &Op() const; \
  104. public: \
  105. inline unsigned getNumOperands() const
  106. /// Macro for generating out-of-class operand accessor definitions
  107. #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
  108. CLASS::op_iterator CLASS::op_begin() { \
  109. return OperandTraits<CLASS>::op_begin(this); \
  110. } \
  111. CLASS::const_op_iterator CLASS::op_begin() const { \
  112. return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
  113. } \
  114. CLASS::op_iterator CLASS::op_end() { \
  115. return OperandTraits<CLASS>::op_end(this); \
  116. } \
  117. CLASS::const_op_iterator CLASS::op_end() const { \
  118. return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
  119. } \
  120. VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
  121. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  122. && "getOperand() out of range!"); \
  123. return cast_or_null<VALUECLASS>( \
  124. OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture].get()); \
  125. } \
  126. void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
  127. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  128. && "setOperand() out of range!"); \
  129. OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
  130. } \
  131. unsigned CLASS::getNumOperands() const { \
  132. return OperandTraits<CLASS>::operands(this); \
  133. } \
  134. template <int Idx_nocapture> Use &CLASS::Op() { \
  135. return this->OpFrom<Idx_nocapture>(this); \
  136. } \
  137. template <int Idx_nocapture> const Use &CLASS::Op() const { \
  138. return this->OpFrom<Idx_nocapture>(this); \
  139. }
  140. } // End llvm namespace
  141. #endif