1 //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 #ifndef LLVM_OPERAND_TRAITS_H
16 #define LLVM_OPERAND_TRAITS_H
18 #include "llvm/User.h"
22 //===----------------------------------------------------------------------===//
23 // FixedNumOperand Trait Class
24 //===----------------------------------------------------------------------===//
26 /// FixedNumOperandTraits - determine the allocation regime of the Use array
27 /// when it is a prefix to the User object, and the number of Use objects is
28 /// known at compile time.
30 template <unsigned ARITY>
31 struct FixedNumOperandTraits {
32 static Use *op_begin(User* U) {
33 return reinterpret_cast<Use*>(U) - ARITY;
35 static Use *op_end(User* U) {
36 return reinterpret_cast<Use*>(U);
38 static unsigned operands(const User*) {
43 prefix(); // DO NOT IMPLEMENT
47 struct overlay : public prefix, public U {
48 overlay(); // DO NOT IMPLEMENT
53 //===----------------------------------------------------------------------===//
54 // OptionalOperand Trait Class
55 //===----------------------------------------------------------------------===//
57 /// OptionalOperandTraits - when the number of operands may change at runtime.
58 /// Naturally it may only decrease, because the allocations may not change.
60 template <unsigned ARITY = 1>
61 struct OptionalOperandTraits : public FixedNumOperandTraits<ARITY> {
62 static unsigned operands(const User *U) {
63 return U->getNumOperands();
67 //===----------------------------------------------------------------------===//
68 // VariadicOperand Trait Class
69 //===----------------------------------------------------------------------===//
71 /// VariadicOperandTraits - determine the allocation regime of the Use array
72 /// when it is a prefix to the User object, and the number of Use objects is
73 /// only known at allocation time.
75 template <unsigned MINARITY = 0>
76 struct VariadicOperandTraits {
77 static Use *op_begin(User* U) {
78 return reinterpret_cast<Use*>(U) - U->getNumOperands();
80 static Use *op_end(User* U) {
81 return reinterpret_cast<Use*>(U);
83 static unsigned operands(const User *U) {
84 return U->getNumOperands();
88 //===----------------------------------------------------------------------===//
89 // HungoffOperand Trait Class
90 //===----------------------------------------------------------------------===//
92 /// HungoffOperandTraits - determine the allocation regime of the Use array
93 /// when it is not a prefix to the User object, but allocated at an unrelated
95 /// Assumes that the User subclass that is determined by this traits class
96 /// has an OperandList member of type User::op_iterator. [Note: this is now
97 /// trivially satisfied, because User has that member for historic reasons.]
99 /// This is the traits class that is needed when the Use array must be
102 template <unsigned MINARITY = 1>
103 struct HungoffOperandTraits {
104 static Use *op_begin(User* U) {
105 return U->OperandList;
107 static Use *op_end(User* U) {
108 return U->OperandList + U->getNumOperands();
110 static unsigned operands(const User *U) {
111 return U->getNumOperands();
115 /// Macro for generating in-class operand accessor declarations.
116 /// It should only be called in the public section of the interface.
118 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
120 inline VALUECLASS *getOperand(unsigned) const; \
121 inline void setOperand(unsigned, VALUECLASS*); \
122 inline op_iterator op_begin(); \
123 inline const_op_iterator op_begin() const; \
124 inline op_iterator op_end(); \
125 inline const_op_iterator op_end() const; \
127 template <int> inline Use &Op(); \
128 template <int> inline const Use &Op() const; \
130 inline unsigned getNumOperands() const
132 /// Macro for generating out-of-class operand accessor definitions
133 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
134 CLASS::op_iterator CLASS::op_begin() { \
135 return OperandTraits<CLASS>::op_begin(this); \
137 CLASS::const_op_iterator CLASS::op_begin() const { \
138 return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
140 CLASS::op_iterator CLASS::op_end() { \
141 return OperandTraits<CLASS>::op_end(this); \
143 CLASS::const_op_iterator CLASS::op_end() const { \
144 return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
146 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
147 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
148 && "getOperand() out of range!"); \
149 return static_cast<VALUECLASS*>( \
150 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
152 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
153 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
154 && "setOperand() out of range!"); \
155 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
157 unsigned CLASS::getNumOperands() const { \
158 return OperandTraits<CLASS>::operands(this); \
160 template <int Idx_nocapture> Use &CLASS::Op() { \
161 return this->OpFrom<Idx_nocapture>(this); \
163 template <int Idx_nocapture> const Use &CLASS::Op() const { \
164 return this->OpFrom<Idx_nocapture>(this); \
168 /// Macro for generating out-of-class operand accessor
169 /// definitions with casted result
170 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
171 CLASS::op_iterator CLASS::op_begin() { \
172 return OperandTraits<CLASS>::op_begin(this); \
174 CLASS::const_op_iterator CLASS::op_begin() const { \
175 return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
177 CLASS::op_iterator CLASS::op_end() { \
178 return OperandTraits<CLASS>::op_end(this); \
180 CLASS::const_op_iterator CLASS::op_end() const { \
181 return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
183 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
184 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
185 && "getOperand() out of range!"); \
186 return cast<VALUECLASS>( \
187 OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
189 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
190 assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
191 && "setOperand() out of range!"); \
192 OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
194 unsigned CLASS::getNumOperands() const { \
195 return OperandTraits<CLASS>::operands(this); \
197 template <int Idx_nocapture> Use &CLASS::Op() { \
198 return this->OpFrom<Idx_nocapture>(this); \
200 template <int Idx_nocapture> const Use &CLASS::Op() const { \
201 return this->OpFrom<Idx_nocapture>(this); \
205 } // End llvm namespace