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 
00058 /// @brief Attributes that only apply to function parameters.
00059 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
00060 
00061 /// @brief Attributes that only apply to function.
00062 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | 
00063   NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq;
00064 
00065 /// @brief Parameter attributes that do not apply to vararg call arguments.
00066 const Attributes VarArgsIncompatible = StructRet;
00067 
00068 /// @brief Attributes that are mutually incompatible.
00069 const Attributes MutuallyIncompatible[4] = {
00070   ByVal | InReg | Nest | StructRet,
00071   ZExt  | SExt,
00072   ReadNone | ReadOnly,
00073   NoInline | AlwaysInline
00074 };
00075 
00076 /// @brief Which attributes cannot be applied to a type.
00077 Attributes typeIncompatible(const Type *Ty);
00078 
00079 /// This turns an int alignment (a power of 2, normally) into the
00080 /// form used internally in Attributes.
00081 inline Attributes constructAlignmentFromInt(unsigned i) {
00082   // Default alignment, allow the target to define how to align it.
00083   if (i == 0)
00084     return 0;
00085 
00086   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
00087   assert(i <= 0x40000000 && "Alignment too large.");
00088   return (Log2_32(i)+1) << 16;
00089 }
00090 
00091 /// This returns the alignment field of an attribute as a byte alignment value.
00092 inline unsigned getAlignmentFromAttrs(Attributes A) {
00093   Attributes Align = A & Attribute::Alignment;
00094   if (Align == 0)
00095     return 0;
00096   
00097   return 1U << ((Align >> 16) - 1);
00098 }
00099   
00100   
00101 /// The set of Attributes set in Attributes is converted to a
00102 /// string of equivalent mnemonics. This is, presumably, for writing out
00103 /// the mnemonics for the assembly writer. 
00104 /// @brief Convert attribute bits to text
00105 std::string getAsString(Attributes Attrs);
00106 } // end namespace Attribute
00107 
00108 /// This is just a pair of values to associate a set of attributes
00109 /// with an index. 
00110 struct AttributeWithIndex {
00111   Attributes Attrs; ///< The attributes that are set, or'd together.
00112   unsigned Index; ///< Index of the parameter for which the attributes apply.
00113                   ///< Index 0 is used for return value attributes.
00114                   ///< Index ~0U is used for function attributes.
00115   
00116   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
00117     AttributeWithIndex P;
00118     P.Index = Idx;
00119     P.Attrs = Attrs;
00120     return P;
00121   }
00122 };
00123   
00124 //===----------------------------------------------------------------------===//
00125 // AttrListPtr Smart Pointer
00126 //===----------------------------------------------------------------------===//
00127 
00128 class AttributeListImpl;
00129   
00130 /// AttrListPtr - This class manages the ref count for the opaque 
00131 /// AttributeListImpl object and provides accessors for it.
00132 class AttrListPtr {
00133   /// AttrList - The attributes that we are managing.  This can be null
00134   /// to represent the empty attributes list.
00135   AttributeListImpl *AttrList;
00136 public:
00137   AttrListPtr() : AttrList(0) {}
00138   AttrListPtr(const AttrListPtr &P);
00139   const AttrListPtr &operator=(const AttrListPtr &RHS);
00140   ~AttrListPtr();
00141   
00142   //===--------------------------------------------------------------------===//
00143   // Attribute List Construction and Mutation
00144   //===--------------------------------------------------------------------===//
00145   
00146   /// get - Return a Attributes list with the specified parameter in it.
00147   static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
00148   
00149   /// get - Return a Attribute list with the parameters specified by the
00150   /// consecutive random access iterator range.
00151   template <typename Iter>
00152   static AttrListPtr get(const Iter &I, const Iter &E) {
00153     if (I == E) return AttrListPtr();  // Empty list.
00154     return get(&*I, static_cast<unsigned>(E-I));
00155   }
00156 
00157   /// addAttr - Add the specified attribute at the specified index to this
00158   /// attribute list.  Since attribute lists are immutable, this
00159   /// returns the new list.
00160   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
00161   
00162   /// removeAttr - Remove the specified attribute at the specified index from
00163   /// this attribute list.  Since attribute lists are immutable, this
00164   /// returns the new list.
00165   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
00166   
00167   //===--------------------------------------------------------------------===//
00168   // Attribute List Accessors
00169   //===--------------------------------------------------------------------===//
00170   /// getParamAttributes - The attributes for the specified index are
00171   /// returned. 
00172   Attributes getParamAttributes(unsigned Idx) const {
00173     assert (Idx && Idx != ~0U && "Invalid parameter index!");
00174     return getAttributes(Idx);
00175   }
00176 
00177   /// getRetAttributes - The attributes for the ret value are
00178   /// returned. 
00179   Attributes getRetAttributes() const {
00180     return getAttributes(0);
00181   }
00182 
00183   /// getFnAttributes - The function attributes are returned.
00184   Attributes getFnAttributes() const {
00185     return getAttributes(~0);
00186   }
00187   
00188   /// paramHasAttr - Return true if the specified parameter index has the
00189   /// specified attribute set.
00190   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
00191     return getAttributes(Idx) & Attr;
00192   }
00193   
00194   /// getParamAlignment - Return the alignment for the specified function
00195   /// parameter.
00196   unsigned getParamAlignment(unsigned Idx) const {
00197     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
00198   }
00199   
00200   /// hasAttrSomewhere - Return true if the specified attribute is set for at
00201   /// least one parameter or for the return value.
00202   bool hasAttrSomewhere(Attributes Attr) const;
00203 
00204   /// operator==/!= - Provide equality predicates.
00205   bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
00206   bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
00207   
00208   void dump() const;
00209 
00210   //===--------------------------------------------------------------------===//
00211   // Attribute List Introspection
00212   //===--------------------------------------------------------------------===//
00213   
00214   /// getRawPointer - Return a raw pointer that uniquely identifies this
00215   /// attribute list. 
00216   void *getRawPointer() const {
00217     return AttrList;
00218   }
00219   
00220   // Attributes are stored as a dense set of slots, where there is one
00221   // slot for each argument that has an attribute.  This allows walking over the
00222   // dense set instead of walking the sparse list of attributes.
00223   
00224   /// isEmpty - Return true if there are no attributes.
00225   ///
00226   bool isEmpty() const {
00227     return AttrList == 0;
00228   }
00229   
00230   /// getNumSlots - Return the number of slots used in this attribute list. 
00231   /// This is the number of arguments that have an attribute set on them
00232   /// (including the function itself).
00233   unsigned getNumSlots() const;
00234   
00235   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
00236   /// holds a index number plus a set of attributes.
00237   const AttributeWithIndex &getSlot(unsigned Slot) const;
00238   
00239 private:
00240   explicit AttrListPtr(AttributeListImpl *L);
00241 
00242   /// getAttributes - The attributes for the specified index are
00243   /// returned.  Attributes for the result are denoted with Idx = 0.
00244   Attributes getAttributes(unsigned Idx) const;
00245 
00246 };
00247 
00248 } // End llvm namespace
00249 
00250 #endif