1 //===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the simple types necessary to represent the 11 // attributes associated with functions and their calls. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_ATTRIBUTES_H 16 #define LLVM_ATTRIBUTES_H 17 18 #include "llvm/Support/MathExtras.h" 19 #include <cassert> 20 #include <string> 21 22 namespace llvm { 23 class Type; 24 25 /// Attributes - A bitset of attributes. 26 typedef unsigned Attributes; 27 28 namespace Attribute { 29 30 /// Function parameters and results can have attributes to indicate how they 31 /// should be treated by optimizations and code generation. This enumeration 32 /// lists the attributes that can be associated with parameters, function 33 /// results or the function itself. 34 /// @brief Function attributes. 35 36 const Attributes None = 0; ///< No attributes have been set 37 const Attributes ZExt = 1<<0; ///< Zero extended before/after call 38 const Attributes SExt = 1<<1; ///< Sign extended before/after call 39 const Attributes NoReturn = 1<<2; ///< Mark the function as not returning 40 const Attributes InReg = 1<<3; ///< Force argument to be passed in register 41 const Attributes StructRet = 1<<4; ///< Hidden pointer to structure to return 42 const Attributes NoUnwind = 1<<5; ///< Function doesn't unwind stack 43 const Attributes NoAlias = 1<<6; ///< Considered to not alias after call 44 const Attributes ByVal = 1<<7; ///< Pass structure by value 45 const Attributes Nest = 1<<8; ///< Nested function static chain 46 const Attributes ReadNone = 1<<9; ///< Function does not access memory 47 const Attributes ReadOnly = 1<<10; ///< Function only reads from memory 48 const Attributes NoInline = 1<<11; ///< inline=never 49 const Attributes AlwaysInline = 1<<12; ///< inline=always 50 const Attributes OptimizeForSize = 1<<13; ///< opt_size 51 const Attributes StackProtect = 1<<14; ///< Stack protection. 52 const Attributes StackProtectReq = 1<<15; ///< Stack protection required. 53 const Attributes Alignment = 31<<16; ///< Alignment of parameter (5 bits) 54 // stored as log2 of alignment with +1 bias 55 // 0 means unaligned different from align 1 56 const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer 57 const Attributes NoRedZone = 1<<22; /// disable redzone 58 const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point 59 /// instructions. 60 const Attributes Naked = 1<<24; ///< Naked function 61 const Attributes InlineHint = 1<<25; ///< source said inlining was 62 ///desirable 63 const Attributes StackAlignment = 7<<26; ///< Alignment of stack for 64 ///function (3 bits) stored as log2 65 ///of alignment with +1 bias 66 ///0 means unaligned (different from 67 ///alignstack(1)) 68 const Attributes Hotpatch = 1<<29; ///< Function should have special 69 ///'hotpatch' sequence in prologue 70 const Attributes UWTable = 1<<30; ///< Function must be in a unwind 71 ///table 72 const Attributes NonLazyBind = 1U<<31; ///< Function is called early and/or 73 /// often, so lazy binding isn't 74 /// worthwhile. 75 76 /// Note that uwtable is about the ABI or the user mandating an entry in the 77 /// unwind table. The nounwind attribute is about an exception passing by the 78 /// function. 79 /// In a theoretical system that uses tables for profiling and sjlj for 80 /// exceptions, they would be fully independent. In a normal system that 81 /// uses tables for both, the semantics are: 82 /// nil = Needs an entry because an exception might pass by. 83 /// nounwind = No need for an entry 84 /// uwtable = Needs an entry because the ABI says so and because 85 /// an exception might pass by. 86 /// uwtable + nounwind = Needs an entry because the ABI says so. 87 88 /// @brief Attributes that only apply to function parameters. 89 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture; 90 91 /// @brief Attributes that may be applied to the function itself. These cannot 92 /// be used on return values or function parameters. 93 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | 94 NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq | 95 NoRedZone | NoImplicitFloat | Naked | InlineHint | StackAlignment | 96 Hotpatch | UWTable | NonLazyBind; 97 98 /// @brief Parameter attributes that do not apply to vararg call arguments. 99 const Attributes VarArgsIncompatible = StructRet; 100 101 /// @brief Attributes that are mutually incompatible. 102 const Attributes MutuallyIncompatible[4] = { 103 ByVal | InReg | Nest | StructRet, 104 ZExt | SExt, 105 ReadNone | ReadOnly, 106 NoInline | AlwaysInline 107 }; 108 109 /// @brief Which attributes cannot be applied to a type. 110 Attributes typeIncompatible(Type *Ty); 111 112 /// This turns an int alignment (a power of 2, normally) into the 113 /// form used internally in Attributes. 114 inline Attributes constructAlignmentFromInt(unsigned i) { 115 // Default alignment, allow the target to define how to align it. 116 if (i == 0) 117 return 0; 118 119 assert(isPowerOf2_32(i) && "Alignment must be a power of two."); 120 assert(i <= 0x40000000 && "Alignment too large."); 121 return (Log2_32(i)+1) << 16; 122 } 123 124 /// This returns the alignment field of an attribute as a byte alignment value. 125 inline unsigned getAlignmentFromAttrs(Attributes A) { 126 Attributes Align = A & Attribute::Alignment; 127 if (Align == 0) 128 return 0; 129 130 return 1U << ((Align >> 16) - 1); 131 } 132 133 /// This turns an int stack alignment (which must be a power of 2) into 134 /// the form used internally in Attributes. 135 inline Attributes constructStackAlignmentFromInt(unsigned i) { 136 // Default alignment, allow the target to define how to align it. 137 if (i == 0) 138 return 0; 139 140 assert(isPowerOf2_32(i) && "Alignment must be a power of two."); 141 assert(i <= 0x100 && "Alignment too large."); 142 return (Log2_32(i)+1) << 26; 143 } 144 145 /// This returns the stack alignment field of an attribute as a byte alignment 146 /// value. 147 inline unsigned getStackAlignmentFromAttrs(Attributes A) { 148 Attributes StackAlign = A & Attribute::StackAlignment; 149 if (StackAlign == 0) 150 return 0; 151 152 return 1U << ((StackAlign >> 26) - 1); 153 } 154 155 156 /// The set of Attributes set in Attributes is converted to a 157 /// string of equivalent mnemonics. This is, presumably, for writing out 158 /// the mnemonics for the assembly writer. 159 /// @brief Convert attribute bits to text 160 std::string getAsString(Attributes Attrs); 161 } // end namespace Attribute 162 163 /// This is just a pair of values to associate a set of attributes 164 /// with an index. 165 struct AttributeWithIndex { 166 Attributes Attrs; ///< The attributes that are set, or'd together. 167 unsigned Index; ///< Index of the parameter for which the attributes apply. 168 ///< Index 0 is used for return value attributes. 169 ///< Index ~0U is used for function attributes. 170 171 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) { 172 AttributeWithIndex P; 173 P.Index = Idx; 174 P.Attrs = Attrs; 175 return P; 176 } 177 }; 178 179 //===----------------------------------------------------------------------===// 180 // AttrListPtr Smart Pointer 181 //===----------------------------------------------------------------------===// 182 183 class AttributeListImpl; 184 185 /// AttrListPtr - This class manages the ref count for the opaque 186 /// AttributeListImpl object and provides accessors for it. 187 class AttrListPtr { 188 /// AttrList - The attributes that we are managing. This can be null 189 /// to represent the empty attributes list. 190 AttributeListImpl *AttrList; 191 public: 192 AttrListPtr() : AttrList(0) {} 193 AttrListPtr(const AttrListPtr &P); 194 const AttrListPtr &operator=(const AttrListPtr &RHS); 195 ~AttrListPtr(); 196 197 //===--------------------------------------------------------------------===// 198 // Attribute List Construction and Mutation 199 //===--------------------------------------------------------------------===// 200 201 /// get - Return a Attributes list with the specified parameter in it. 202 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs); 203 204 /// get - Return a Attribute list with the parameters specified by the 205 /// consecutive random access iterator range. 206 template <typename Iter> 207 static AttrListPtr get(const Iter &I, const Iter &E) { 208 if (I == E) return AttrListPtr(); // Empty list. 209 return get(&*I, static_cast<unsigned>(E-I)); 210 } 211 212 /// addAttr - Add the specified attribute at the specified index to this 213 /// attribute list. Since attribute lists are immutable, this 214 /// returns the new list. 215 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const; 216 217 /// removeAttr - Remove the specified attribute at the specified index from 218 /// this attribute list. Since attribute lists are immutable, this 219 /// returns the new list. 220 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const; 221 222 //===--------------------------------------------------------------------===// 223 // Attribute List Accessors 224 //===--------------------------------------------------------------------===// 225 /// getParamAttributes - The attributes for the specified index are 226 /// returned. 227 Attributes getParamAttributes(unsigned Idx) const { 228 assert (Idx && Idx != ~0U && "Invalid parameter index!"); 229 return getAttributes(Idx); 230 } 231 232 /// getRetAttributes - The attributes for the ret value are 233 /// returned. 234 Attributes getRetAttributes() const { 235 return getAttributes(0); 236 } 237 238 /// getFnAttributes - The function attributes are returned. 239 Attributes getFnAttributes() const { 240 return getAttributes(~0U); 241 } 242 243 /// paramHasAttr - Return true if the specified parameter index has the 244 /// specified attribute set. 245 bool paramHasAttr(unsigned Idx, Attributes Attr) const { 246 return (getAttributes(Idx) & Attr) != 0; 247 } 248 249 /// getParamAlignment - Return the alignment for the specified function 250 /// parameter. 251 unsigned getParamAlignment(unsigned Idx) const { 252 return Attribute::getAlignmentFromAttrs(getAttributes(Idx)); 253 } 254 255 /// hasAttrSomewhere - Return true if the specified attribute is set for at 256 /// least one parameter or for the return value. 257 bool hasAttrSomewhere(Attributes Attr) const; 258 259 /// operator==/!= - Provide equality predicates. 260 bool operator==(const AttrListPtr &RHS) const 261 { return AttrList == RHS.AttrList; } 262 bool operator!=(const AttrListPtr &RHS) const 263 { return AttrList != RHS.AttrList; } 264 265 void dump() const; 266 267 //===--------------------------------------------------------------------===// 268 // Attribute List Introspection 269 //===--------------------------------------------------------------------===// 270 271 /// getRawPointer - Return a raw pointer that uniquely identifies this 272 /// attribute list. 273 void *getRawPointer() const { 274 return AttrList; 275 } 276 277 // Attributes are stored as a dense set of slots, where there is one 278 // slot for each argument that has an attribute. This allows walking over the 279 // dense set instead of walking the sparse list of attributes. 280 281 /// isEmpty - Return true if there are no attributes. 282 /// 283 bool isEmpty() const { 284 return AttrList == 0; 285 } 286 287 /// getNumSlots - Return the number of slots used in this attribute list. 288 /// This is the number of arguments that have an attribute set on them 289 /// (including the function itself). 290 unsigned getNumSlots() const; 291 292 /// getSlot - Return the AttributeWithIndex at the specified slot. This 293 /// holds a index number plus a set of attributes. 294 const AttributeWithIndex &getSlot(unsigned Slot) const; 295 296 private: 297 explicit AttrListPtr(AttributeListImpl *L); 298 299 /// getAttributes - The attributes for the specified index are 300 /// returned. Attributes for the result are denoted with Idx = 0. 301 Attributes getAttributes(unsigned Idx) const; 302 303 }; 304 305 } // End llvm namespace 306 307 #endif 308