LLVM API Documentation

OperandTraits.h

Go to the documentation of this file.
00001 //===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the traits classes that are handy for enforcing the correct
00011 // layout of various User subclasses. It also provides the means for accessing
00012 // the operands in the most efficient manner.
00013 //
00014 
00015 #ifndef LLVM_OPERAND_TRAITS_H
00016 #define LLVM_OPERAND_TRAITS_H
00017 
00018 #include "llvm/User.h"
00019 
00020 namespace llvm {
00021 
00022 //===----------------------------------------------------------------------===//
00023 //                          FixedNumOperands Trait Class
00024 //===----------------------------------------------------------------------===//
00025 
00026 /// FixedNumOperandTraits - determine the allocation regime of the Use array
00027 /// when it is a prefix to the User object, and the number of Use objects is
00028 /// known at compile time.
00029 
00030 template <unsigned ARITY>
00031 struct FixedNumOperandTraits {
00032   static Use *op_begin(User* U) {
00033     return reinterpret_cast<Use*>(U) - ARITY;
00034   }
00035   static Use *op_end(User* U) {
00036     return reinterpret_cast<Use*>(U);
00037   }
00038   static unsigned operands(const User*) {
00039     return ARITY;
00040   }
00041   struct prefix {
00042     Use Ops[ARITY];
00043     prefix(); // DO NOT IMPLEMENT
00044   };
00045   template <class U>
00046   struct Layout {
00047     struct overlay : prefix, U {
00048       overlay(); // DO NOT IMPLEMENT
00049     };
00050   };
00051   static inline void *allocate(unsigned); // FIXME
00052 };
00053 
00054 //===----------------------------------------------------------------------===//
00055 //                          OptionalOperands Trait Class
00056 //===----------------------------------------------------------------------===//
00057 
00058 template <unsigned ARITY = 1>
00059 struct OptionalOperandTraits : FixedNumOperandTraits<ARITY> {
00060   static unsigned operands(const User *U) {
00061     return U->getNumOperands();
00062   }
00063 };
00064 
00065 //===----------------------------------------------------------------------===//
00066 //                          VariadicOperand Trait Class
00067 //===----------------------------------------------------------------------===//
00068 
00069 /// VariadicOperandTraits - determine the allocation regime of the Use array
00070 /// when it is a prefix to the User object, and the number of Use objects is
00071 /// only known at allocation time.
00072 
00073 template <unsigned MINARITY = 0>
00074 struct VariadicOperandTraits {
00075   static Use *op_begin(User* U) {
00076     return reinterpret_cast<Use*>(U) - U->getNumOperands();
00077   }
00078   static Use *op_end(User* U) {
00079     return reinterpret_cast<Use*>(U);
00080   }
00081   static unsigned operands(const User *U) {
00082     return U->getNumOperands();
00083   }
00084   static inline void *allocate(unsigned); // FIXME
00085 };
00086 
00087 //===----------------------------------------------------------------------===//
00088 //                          HungoffOperand Trait Class
00089 //===----------------------------------------------------------------------===//
00090 
00091 /// HungoffOperandTraits - determine the allocation regime of the Use array
00092 /// when it is not a prefix to the User object, but allocated at an unrelated
00093 /// heap address.
00094 /// Assumes that the User subclass that is determined by this traits class
00095 /// has an OperandList member of type User::op_iterator. [Note: this is now
00096 /// trivially satisfied, because User has that member for historic reasons.]
00097 ///
00098 /// This is the traits class that is needed when the Use array must be
00099 /// resizable.
00100 
00101 template <unsigned MINARITY = 1>
00102 struct HungoffOperandTraits {
00103   static Use *op_begin(User* U) {
00104     return U->OperandList;
00105   }
00106   static Use *op_end(User* U) {
00107     return U->OperandList + U->getNumOperands();
00108   }
00109   static unsigned operands(const User *U) {
00110     return U->getNumOperands();
00111   }
00112   static inline void *allocate(unsigned); // FIXME
00113 };
00114 
00115 /// Macro for generating in-class operand accessor declarations.
00116 /// It should only be called in the public section of the interface.
00117 ///
00118 #define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
00119   public: \
00120   inline VALUECLASS *getOperand(unsigned) const; \
00121   inline void setOperand(unsigned, VALUECLASS*); \
00122   protected: \
00123   template <unsigned> inline Use &Op(); \
00124   template <unsigned> inline const Use &Op() const; \
00125   public: \
00126   inline unsigned getNumOperands() const
00127 
00128 /// Macro for generating out-of-class operand accessor definitions
00129 #define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
00130 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
00131   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
00132          && "getOperand() out of range!"); \
00133   return static_cast<VALUECLASS*>( \
00134     OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
00135 } \
00136 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
00137   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
00138          && "setOperand() out of range!"); \
00139   OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
00140 } \
00141 unsigned CLASS::getNumOperands() const { \
00142   return OperandTraits<CLASS>::operands(this);  \
00143 } \
00144 template <unsigned Idx_nocapture> Use &CLASS::Op() { \
00145   return OperandTraits<CLASS>::op_begin(this)[Idx_nocapture]; \
00146 } \
00147 template <unsigned Idx_nocapture> const Use &CLASS::Op() const { \
00148   return OperandTraits<CLASS>::op_begin( \
00149     const_cast<CLASS*>(this))[Idx_nocapture]; \
00150 }
00151 
00152 
00153 /// Macro for generating out-of-class operand accessor
00154 /// definitions with casted result
00155 #define DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
00156 VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
00157   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
00158          && "getOperand() out of range!"); \
00159   return cast<VALUECLASS>( \
00160     OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture]); \
00161 } \
00162 void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
00163   assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
00164          && "setOperand() out of range!"); \
00165   OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
00166 } \
00167 unsigned CLASS::getNumOperands() const { \
00168   return OperandTraits<CLASS>::operands(this); \
00169 } \
00170 template <unsigned Idx_nocapture> Use &CLASS::Op() { \
00171   return OperandTraits<CLASS>::op_begin(this)[Idx_nocapture]; \
00172 } \
00173 template <unsigned Idx_nocapture> const Use &CLASS::Op() const { \
00174   return OperandTraits<CLASS>::op_begin( \
00175     const_cast<CLASS*>(this))[Idx_nocapture]; \
00176 }
00177 
00178 
00179 } // End llvm namespace
00180 
00181 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.