LLVM API Documentation

Attributes.h

Go to the documentation of this file.
00001 //===-- llvm/Attributes.h - Container for Attributes ---*---------- 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 contains the simple types necessary to represent the
00011 // attributes associated with functions and their calls.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ATTRIBUTES_H
00016 #define LLVM_ATTRIBUTES_H
00017 
00018 #include "llvm/Support/MathExtras.h"
00019 #include <cassert>
00020 #include <string>
00021 
00022 namespace llvm {
00023 class Type;
00024 
00025 /// Attributes - A bitset of attributes.
00026 typedef unsigned Attributes;
00027 
00028 namespace Attribute {
00029 
00030 /// Function parameters and results can have attributes to indicate how they 
00031 /// should be treated by optimizations and code generation. This enumeration 
00032 /// lists the attributes that can be associated with parameters, function 
00033 /// results or the function itself.
00034 /// @brief Function attributes.
00035 
00036 const Attributes None      = 0;     ///< No attributes have been set
00037 const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
00038 const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
00039 const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
00040 const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
00041 const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
00042 const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
00043 const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
00044 const Attributes ByVal     = 1<<7;  ///< Pass structure by value
00045 const Attributes Nest      = 1<<8;  ///< Nested function static chain
00046 const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
00047 const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
00048 const Attributes NoInline        = 1<<11; ///< inline=never 
00049 const Attributes AlwaysInline    = 1<<12; ///< inline=always
00050 const Attributes OptimizeForSize = 1<<13; ///< opt_size
00051 const Attributes StackProtect    = 1<<14; ///< Stack protection.
00052 const Attributes StackProtectReq = 1<<15; ///< Stack protection required.
00053 const Attributes Alignment = 31<<16; ///< Alignment of parameter (5 bits)
00054                                      // stored as log2 of alignment with +1 bias
00055                                      // 0 means unaligned different from align 1
00056 const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer
00057 const Attributes NoRedZone = 1<<22; /// disable redzone
00058 const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point
00059                                           /// instructions.
00060 const Attributes Naked     = 1<<24; ///< Naked function
00061 
00062 /// @brief Attributes that only apply to function parameters.
00063 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
00064 
00065 /// @brief Attributes that may be applied to the function itself.  These cannot
00066 /// be used on return values or function parameters.
00067 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | 
00068   NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
00069   NoRedZone | NoImplicitFloat | Naked;
00070 
00071 /// @brief Parameter attributes that do not apply to vararg call arguments.
00072 const Attributes VarArgsIncompatible = StructRet;
00073 
00074 /// @brief Attributes that are mutually incompatible.
00075 const Attributes MutuallyIncompatible[4] = {
00076   ByVal | InReg | Nest | StructRet,
00077   ZExt  | SExt,
00078   ReadNone | ReadOnly,
00079   NoInline | AlwaysInline
00080 };
00081 
00082 /// @brief Which attributes cannot be applied to a type.
00083 Attributes typeIncompatible(const Type *Ty);
00084 
00085 /// This turns an int alignment (a power of 2, normally) into the
00086 /// form used internally in Attributes.
00087 inline Attributes constructAlignmentFromInt(unsigned i) {
00088   // Default alignment, allow the target to define how to align it.
00089   if (i == 0)
00090     return 0;
00091 
00092   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
00093   assert(i <= 0x40000000 && "Alignment too large.");
00094   return (Log2_32(i)+1) << 16;
00095 }
00096 
00097 /// This returns the alignment field of an attribute as a byte alignment value.
00098 inline unsigned getAlignmentFromAttrs(Attributes A) {
00099   Attributes Align = A & Attribute::Alignment;
00100   if (Align == 0)
00101     return 0;
00102   
00103   return 1U << ((Align >> 16) - 1);
00104 }
00105   
00106   
00107 /// The set of Attributes set in Attributes is converted to a
00108 /// string of equivalent mnemonics. This is, presumably, for writing out
00109 /// the mnemonics for the assembly writer. 
00110 /// @brief Convert attribute bits to text
00111 std::string getAsString(Attributes Attrs);
00112 } // end namespace Attribute
00113 
00114 /// This is just a pair of values to associate a set of attributes
00115 /// with an index. 
00116 struct AttributeWithIndex {
00117   Attributes Attrs; ///< The attributes that are set, or'd together.
00118   unsigned Index; ///< Index of the parameter for which the attributes apply.
00119                   ///< Index 0 is used for return value attributes.
00120                   ///< Index ~0U is used for function attributes.
00121   
00122   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
00123     AttributeWithIndex P;
00124     P.Index = Idx;
00125     P.Attrs = Attrs;
00126     return P;
00127   }
00128 };
00129   
00130 //===----------------------------------------------------------------------===//
00131 // AttrListPtr Smart Pointer
00132 //===----------------------------------------------------------------------===//
00133 
00134 class AttributeListImpl;
00135   
00136 /// AttrListPtr - This class manages the ref count for the opaque 
00137 /// AttributeListImpl object and provides accessors for it.
00138 class AttrListPtr {
00139   /// AttrList - The attributes that we are managing.  This can be null
00140   /// to represent the empty attributes list.
00141   AttributeListImpl *AttrList;
00142 public:
00143   AttrListPtr() : AttrList(0) {}
00144   AttrListPtr(const AttrListPtr &P);
00145   const AttrListPtr &operator=(const AttrListPtr &RHS);
00146   ~AttrListPtr();
00147   
00148   //===--------------------------------------------------------------------===//
00149   // Attribute List Construction and Mutation
00150   //===--------------------------------------------------------------------===//
00151   
00152   /// get - Return a Attributes list with the specified parameter in it.
00153   static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
00154   
00155   /// get - Return a Attribute list with the parameters specified by the
00156   /// consecutive random access iterator range.
00157   template <typename Iter>
00158   static AttrListPtr get(const Iter &I, const Iter &E) {
00159     if (I == E) return AttrListPtr();  // Empty list.
00160     return get(&*I, static_cast<unsigned>(E-I));
00161   }
00162 
00163   /// addAttr - Add the specified attribute at the specified index to this
00164   /// attribute list.  Since attribute lists are immutable, this
00165   /// returns the new list.
00166   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
00167   
00168   /// removeAttr - Remove the specified attribute at the specified index from
00169   /// this attribute list.  Since attribute lists are immutable, this
00170   /// returns the new list.
00171   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
00172   
00173   //===--------------------------------------------------------------------===//
00174   // Attribute List Accessors
00175   //===--------------------------------------------------------------------===//
00176   /// getParamAttributes - The attributes for the specified index are
00177   /// returned. 
00178   Attributes getParamAttributes(unsigned Idx) const {
00179     assert (Idx && Idx != ~0U && "Invalid parameter index!");
00180     return getAttributes(Idx);
00181   }
00182 
00183   /// getRetAttributes - The attributes for the ret value are
00184   /// returned. 
00185   Attributes getRetAttributes() const {
00186     return getAttributes(0);
00187   }
00188 
00189   /// getFnAttributes - The function attributes are returned.
00190   Attributes getFnAttributes() const {
00191     return getAttributes(~0U);
00192   }
00193   
00194   /// paramHasAttr - Return true if the specified parameter index has the
00195   /// specified attribute set.
00196   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
00197     return getAttributes(Idx) & Attr;
00198   }
00199   
00200   /// getParamAlignment - Return the alignment for the specified function
00201   /// parameter.
00202   unsigned getParamAlignment(unsigned Idx) const {
00203     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
00204   }
00205   
00206   /// hasAttrSomewhere - Return true if the specified attribute is set for at
00207   /// least one parameter or for the return value.
00208   bool hasAttrSomewhere(Attributes Attr) const;
00209 
00210   /// operator==/!= - Provide equality predicates.
00211   bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
00212   bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
00213   
00214   void dump() const;
00215 
00216   //===--------------------------------------------------------------------===//
00217   // Attribute List Introspection
00218   //===--------------------------------------------------------------------===//
00219   
00220   /// getRawPointer - Return a raw pointer that uniquely identifies this
00221   /// attribute list. 
00222   void *getRawPointer() const {
00223     return AttrList;
00224   }
00225   
00226   // Attributes are stored as a dense set of slots, where there is one
00227   // slot for each argument that has an attribute.  This allows walking over the
00228   // dense set instead of walking the sparse list of attributes.
00229   
00230   /// isEmpty - Return true if there are no attributes.
00231   ///
00232   bool isEmpty() const {
00233     return AttrList == 0;
00234   }
00235   
00236   /// getNumSlots - Return the number of slots used in this attribute list. 
00237   /// This is the number of arguments that have an attribute set on them
00238   /// (including the function itself).
00239   unsigned getNumSlots() const;
00240   
00241   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
00242   /// holds a index number plus a set of attributes.
00243   const AttributeWithIndex &getSlot(unsigned Slot) const;
00244   
00245 private:
00246   explicit AttrListPtr(AttributeListImpl *L);
00247 
00248   /// getAttributes - The attributes for the specified index are
00249   /// returned.  Attributes for the result are denoted with Idx = 0.
00250   Attributes getAttributes(unsigned Idx) const;
00251 
00252 };
00253 
00254 } // End llvm namespace
00255 
00256 #endif