OperandTraits.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the traits classes that are handy for enforcing the correct
  15. // layout of various User subclasses. It also provides the means for accessing
  16. // the operands in the most efficient manner.
  17. //
  18. #ifndef LLVM_IR_OPERANDTRAITS_H
  19. #define LLVM_IR_OPERANDTRAITS_H
  20. #include "llvm/IR/User.h"
  21. namespace llvm {
  22. //===----------------------------------------------------------------------===//
  23. // FixedNumOperand Trait Class
  24. //===----------------------------------------------------------------------===//
  25. /// FixedNumOperandTraits - determine the allocation regime of the Use array
  26. /// when it is a prefix to the User object, and the number of Use objects is
  27. /// known at compile time.
  28. template <typename SubClass, unsigned ARITY>
  29. struct FixedNumOperandTraits {
  30. static Use *op_begin(SubClass* U) {
  31. static_assert(
  32. !std::is_polymorphic<SubClass>::value,
  33. "adding virtual methods to subclasses of User breaks use lists");
  34. return reinterpret_cast<Use*>(U) - ARITY;
  35. }
  36. static Use *op_end(SubClass* U) {
  37. return reinterpret_cast<Use*>(U);
  38. }
  39. static unsigned operands(const User*) {
  40. return ARITY;
  41. }
  42. };
  43. //===----------------------------------------------------------------------===//
  44. // OptionalOperand Trait Class
  45. //===----------------------------------------------------------------------===//
  46. /// OptionalOperandTraits - when the number of operands may change at runtime.
  47. /// Naturally it may only decrease, because the allocations may not change.
  48. template <typename SubClass, unsigned ARITY = 1>
  49. struct OptionalOperandTraits : public FixedNumOperandTraits<SubClass, ARITY> {
  50. static unsigned operands(const User *U) {
  51. return U->getNumOperands();
  52. }
  53. };
  54. //===----------------------------------------------------------------------===//
  55. // VariadicOperand Trait Class
  56. //===----------------------------------------------------------------------===//
  57. /// VariadicOperandTraits - determine the allocation regime of the Use array
  58. /// when it is a prefix to the User object, and the number of Use objects is
  59. /// only known at allocation time.
  60. template <typename SubClass, unsigned MINARITY = 0>
  61. struct VariadicOperandTraits {
  62. static Use *op_begin(SubClass* U) {
  63. static_assert(
  64. !std::is_polymorphic<SubClass>::value,
  65. "adding virtual methods to subclasses of User breaks use lists");
  66. return reinterpret_cast<Use*>(U) - static_cast<User*>(U)->getNumOperands();
  67. }
  68. static Use *op_end(SubClass* U) {
  69. return reinterpret_cast<Use*>(U);
  70. }
  71. static unsigned operands(const User *U) {
  72. return U->getNumOperands();
  73. }
  74. };
  75. //===----------------------------------------------------------------------===//
  76. // HungoffOperand Trait Class
  77. //===----------------------------------------------------------------------===//
  78. /// HungoffOperandTraits - determine the allocation regime of the Use array
  79. /// when it is not a prefix to the User object, but allocated at an unrelated
  80. /// heap address.
  81. ///
  82. /// This is the traits class that is needed when the Use array must be
  83. /// resizable.
  84. template <unsigned MINARITY = 1>
  85. struct HungoffOperandTraits {
  86. static Use *op_begin(User* U) {
  87. return U->getOperandList();
  88. }
  89. static Use *op_end(User* U) {
  90. return U->getOperandList() + U->getNumOperands();
  91. }
  92. static unsigned operands(const User *U) {
  93. return U->getNumOperands();
  94. }
  95. };
  96. /// Macro for generating in-class operand accessor declarations.
  97. /// It should only be called in the public section of the interface.
  98. ///
  99. #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
  100. public: \
  101. inline VALUECLASS *getOperand(unsigned) const; \
  102. inline void setOperand(unsigned, VALUECLASS*); \
  103. inline op_iterator op_begin(); \
  104. inline const_op_iterator op_begin() const; \
  105. inline op_iterator op_end(); \
  106. inline const_op_iterator op_end() const; \
  107. protected: \
  108. template <int> inline Use &Op(); \
  109. template <int> inline const Use &Op() const; \
  110. public: \
  111. inline unsigned getNumOperands() const
  112. /// Macro for generating out-of-class operand accessor definitions
  113. #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
  114. CLASS::op_iterator CLASS::op_begin() { \
  115. return OperandTraits<CLASS>::op_begin(this); \
  116. } \
  117. CLASS::const_op_iterator CLASS::op_begin() const { \
  118. return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
  119. } \
  120. CLASS::op_iterator CLASS::op_end() { \
  121. return OperandTraits<CLASS>::op_end(this); \
  122. } \
  123. CLASS::const_op_iterator CLASS::op_end() const { \
  124. return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
  125. } \
  126. VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
  127. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  128. && "getOperand() out of range!"); \
  129. return cast_or_null<VALUECLASS>( \
  130. OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture].get()); \
  131. } \
  132. void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
  133. assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
  134. && "setOperand() out of range!"); \
  135. OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
  136. } \
  137. unsigned CLASS::getNumOperands() const { \
  138. return OperandTraits<CLASS>::operands(this); \
  139. } \
  140. template <int Idx_nocapture> Use &CLASS::Op() { \
  141. return this->OpFrom<Idx_nocapture>(this); \
  142. } \
  143. template <int Idx_nocapture> const Use &CLASS::Op() const { \
  144. return this->OpFrom<Idx_nocapture>(this); \
  145. }
  146. } // End llvm namespace
  147. #endif
  148. #ifdef __GNUC__
  149. #pragma GCC diagnostic pop
  150. #endif