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 namespace Attribute { 26 /// We use this proxy POD type to allow constructing Attributes constants 27 /// using initializer lists. Do not use this class directly. 28 struct AttrConst { 29 uint64_t v; 30 AttrConst operator | (const AttrConst Attrs) const { 31 AttrConst Res = {v | Attrs.v}; 32 return Res; 33 } 34 AttrConst operator ~ () const { 35 AttrConst Res = {~v}; 36 return Res; 37 } 38 }; 39 } // namespace Attribute 40 41 42 /// Attributes - A bitset of attributes. 43 class Attributes { 44 public: 45 Attributes() : Bits(0) { } 46 explicit Attributes(uint64_t Val) : Bits(Val) { } 47 /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { } 48 Attributes(const Attributes &Attrs) : Bits(Attrs.Bits) { } 49 // This is a "safe bool() operator". 50 operator const void *() const { return Bits ? this : 0; } 51 bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; } 52 Attributes &operator = (const Attributes &Attrs) { 53 Bits = Attrs.Bits; 54 return *this; 55 } 56 bool operator == (const Attributes &Attrs) const { 57 return Bits == Attrs.Bits; 58 } 59 bool operator != (const Attributes &Attrs) const { 60 return Bits != Attrs.Bits; 61 } 62 Attributes operator | (const Attributes &Attrs) const { 63 return Attributes(Bits | Attrs.Bits); 64 } 65 Attributes operator & (const Attributes &Attrs) const { 66 return Attributes(Bits & Attrs.Bits); 67 } 68 Attributes operator ^ (const Attributes &Attrs) const { 69 return Attributes(Bits ^ Attrs.Bits); 70 } 71 Attributes &operator |= (const Attributes &Attrs) { 72 Bits |= Attrs.Bits; 73 return *this; 74 } 75 Attributes &operator &= (const Attributes &Attrs) { 76 Bits &= Attrs.Bits; 77 return *this; 78 } 79 Attributes operator ~ () const { return Attributes(~Bits); } 80 uint64_t Raw() const { return Bits; } 81 private: 82 // Currently, we need less than 64 bits. 83 uint64_t Bits; 84 }; 85 86 namespace Attribute { 87 88 /// Function parameters and results can have attributes to indicate how they 89 /// should be treated by optimizations and code generation. This enumeration 90 /// lists the attributes that can be associated with parameters, function 91 /// results or the function itself. 92 /// @brief Function attributes. 93 94 // We declare AttrConst objects that will be used throughout the code 95 // and also raw uint64_t objects with _i suffix to be used below for other 96 // constant declarations. This is done to avoid static CTORs and at the same 97 // time to keep type-safety of Attributes. 98 #define DECLARE_LLVM_ATTRIBUTE(name, value) \ 99 const uint64_t name##_i = value; \ 100 const AttrConst name = {value}; 101 102 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set 103 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call 104 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call 105 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning 106 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register 107 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return 108 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack 109 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call 110 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value 111 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain 112 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory 113 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory 114 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never 115 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always 116 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size 117 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection. 118 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required. 119 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits) 120 // stored as log2 of alignment with +1 bias 121 // 0 means unaligned different from align 1 122 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer 123 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone 124 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point 125 /// instructions. 126 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function 127 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was 128 ///desirable 129 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for 130 ///function (3 bits) stored as log2 131 ///of alignment with +1 bias 132 ///0 means unaligned (different from 133 ///alignstack= {1)) 134 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice 135 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind 136 ///table 137 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or 138 /// often, so lazy binding isn't 139 /// worthwhile. 140 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on. 141 142 #undef DECLARE_LLVM_ATTRIBUTE 143 144 /// Note that uwtable is about the ABI or the user mandating an entry in the 145 /// unwind table. The nounwind attribute is about an exception passing by the 146 /// function. 147 /// In a theoretical system that uses tables for profiling and sjlj for 148 /// exceptions, they would be fully independent. In a normal system that 149 /// uses tables for both, the semantics are: 150 /// nil = Needs an entry because an exception might pass by. 151 /// nounwind = No need for an entry 152 /// uwtable = Needs an entry because the ABI says so and because 153 /// an exception might pass by. 154 /// uwtable + nounwind = Needs an entry because the ABI says so. 155 156 /// @brief Attributes that only apply to function parameters. 157 const AttrConst ParameterOnly = {ByVal_i | Nest_i | 158 StructRet_i | NoCapture_i}; 159 160 /// @brief Attributes that may be applied to the function itself. These cannot 161 /// be used on return values or function parameters. 162 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i | 163 ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i | 164 StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i | 165 Naked_i | InlineHint_i | StackAlignment_i | 166 UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i}; 167 168 /// @brief Parameter attributes that do not apply to vararg call arguments. 169 const AttrConst VarArgsIncompatible = {StructRet_i}; 170 171 /// @brief Attributes that are mutually incompatible. 172 const AttrConst MutuallyIncompatible[4] = { 173 {ByVal_i | InReg_i | Nest_i | StructRet_i}, 174 {ZExt_i | SExt_i}, 175 {ReadNone_i | ReadOnly_i}, 176 {NoInline_i | AlwaysInline_i} 177 }; 178 179 /// @brief Which attributes cannot be applied to a type. 180 Attributes typeIncompatible(Type *Ty); 181 182 /// This turns an int alignment (a power of 2, normally) into the 183 /// form used internally in Attributes. 184 inline Attributes constructAlignmentFromInt(unsigned i) { 185 // Default alignment, allow the target to define how to align it. 186 if (i == 0) 187 return None; 188 189 assert(isPowerOf2_32(i) && "Alignment must be a power of two."); 190 assert(i <= 0x40000000 && "Alignment too large."); 191 return Attributes((Log2_32(i)+1) << 16); 192 } 193 194 /// This returns the alignment field of an attribute as a byte alignment value. 195 inline unsigned getAlignmentFromAttrs(Attributes A) { 196 Attributes Align = A & Attribute::Alignment; 197 if (!Align) 198 return 0; 199 200 return 1U << ((Align.Raw() >> 16) - 1); 201 } 202 203 /// This turns an int stack alignment (which must be a power of 2) into 204 /// the form used internally in Attributes. 205 inline Attributes constructStackAlignmentFromInt(unsigned i) { 206 // Default alignment, allow the target to define how to align it. 207 if (i == 0) 208 return None; 209 210 assert(isPowerOf2_32(i) && "Alignment must be a power of two."); 211 assert(i <= 0x100 && "Alignment too large."); 212 return Attributes((Log2_32(i)+1) << 26); 213 } 214 215 /// This returns the stack alignment field of an attribute as a byte alignment 216 /// value. 217 inline unsigned getStackAlignmentFromAttrs(Attributes A) { 218 Attributes StackAlign = A & Attribute::StackAlignment; 219 if (!StackAlign) 220 return 0; 221 222 return 1U << ((StackAlign.Raw() >> 26) - 1); 223 } 224 225 226 /// The set of Attributes set in Attributes is converted to a 227 /// string of equivalent mnemonics. This is, presumably, for writing out 228 /// the mnemonics for the assembly writer. 229 /// @brief Convert attribute bits to text 230 std::string getAsString(Attributes Attrs); 231 } // end namespace Attribute 232 233 /// This is just a pair of values to associate a set of attributes 234 /// with an index. 235 struct AttributeWithIndex { 236 Attributes Attrs; ///< The attributes that are set, or'd together. 237 unsigned Index; ///< Index of the parameter for which the attributes apply. 238 ///< Index 0 is used for return value attributes. 239 ///< Index ~0U is used for function attributes. 240 241 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) { 242 AttributeWithIndex P; 243 P.Index = Idx; 244 P.Attrs = Attrs; 245 return P; 246 } 247 }; 248 249 //===----------------------------------------------------------------------===// 250 // AttrListPtr Smart Pointer 251 //===----------------------------------------------------------------------===// 252 253 class AttributeListImpl; 254 255 /// AttrListPtr - This class manages the ref count for the opaque 256 /// AttributeListImpl object and provides accessors for it. 257 class AttrListPtr { 258 /// AttrList - The attributes that we are managing. This can be null 259 /// to represent the empty attributes list. 260 AttributeListImpl *AttrList; 261 public: 262 AttrListPtr() : AttrList(0) {} 263 AttrListPtr(const AttrListPtr &P); 264 const AttrListPtr &operator=(const AttrListPtr &RHS); 265 ~AttrListPtr(); 266 267 //===--------------------------------------------------------------------===// 268 // Attribute List Construction and Mutation 269 //===--------------------------------------------------------------------===// 270 271 /// get - Return a Attributes list with the specified parameter in it. 272 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs); 273 274 /// get - Return a Attribute list with the parameters specified by the 275 /// consecutive random access iterator range. 276 template <typename Iter> 277 static AttrListPtr get(const Iter &I, const Iter &E) { 278 if (I == E) return AttrListPtr(); // Empty list. 279 return get(&*I, static_cast<unsigned>(E-I)); 280 } 281 282 /// addAttr - Add the specified attribute at the specified index to this 283 /// attribute list. Since attribute lists are immutable, this 284 /// returns the new list. 285 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const; 286 287 /// removeAttr - Remove the specified attribute at the specified index from 288 /// this attribute list. Since attribute lists are immutable, this 289 /// returns the new list. 290 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const; 291 292 //===--------------------------------------------------------------------===// 293 // Attribute List Accessors 294 //===--------------------------------------------------------------------===// 295 /// getParamAttributes - The attributes for the specified index are 296 /// returned. 297 Attributes getParamAttributes(unsigned Idx) const { 298 assert (Idx && Idx != ~0U && "Invalid parameter index!"); 299 return getAttributes(Idx); 300 } 301 302 /// getRetAttributes - The attributes for the ret value are 303 /// returned. 304 Attributes getRetAttributes() const { 305 return getAttributes(0); 306 } 307 308 /// getFnAttributes - The function attributes are returned. 309 Attributes getFnAttributes() const { 310 return getAttributes(~0U); 311 } 312 313 /// paramHasAttr - Return true if the specified parameter index has the 314 /// specified attribute set. 315 bool paramHasAttr(unsigned Idx, Attributes Attr) const { 316 return getAttributes(Idx) & Attr; 317 } 318 319 /// getParamAlignment - Return the alignment for the specified function 320 /// parameter. 321 unsigned getParamAlignment(unsigned Idx) const { 322 return Attribute::getAlignmentFromAttrs(getAttributes(Idx)); 323 } 324 325 /// hasAttrSomewhere - Return true if the specified attribute is set for at 326 /// least one parameter or for the return value. 327 bool hasAttrSomewhere(Attributes Attr) const; 328 329 /// operator==/!= - Provide equality predicates. 330 bool operator==(const AttrListPtr &RHS) const 331 { return AttrList == RHS.AttrList; } 332 bool operator!=(const AttrListPtr &RHS) const 333 { return AttrList != RHS.AttrList; } 334 335 void dump() const; 336 337 //===--------------------------------------------------------------------===// 338 // Attribute List Introspection 339 //===--------------------------------------------------------------------===// 340 341 /// getRawPointer - Return a raw pointer that uniquely identifies this 342 /// attribute list. 343 void *getRawPointer() const { 344 return AttrList; 345 } 346 347 // Attributes are stored as a dense set of slots, where there is one 348 // slot for each argument that has an attribute. This allows walking over the 349 // dense set instead of walking the sparse list of attributes. 350 351 /// isEmpty - Return true if there are no attributes. 352 /// 353 bool isEmpty() const { 354 return AttrList == 0; 355 } 356 357 /// getNumSlots - Return the number of slots used in this attribute list. 358 /// This is the number of arguments that have an attribute set on them 359 /// (including the function itself). 360 unsigned getNumSlots() const; 361 362 /// getSlot - Return the AttributeWithIndex at the specified slot. This 363 /// holds a index number plus a set of attributes. 364 const AttributeWithIndex &getSlot(unsigned Slot) const; 365 366 private: 367 explicit AttrListPtr(AttributeListImpl *L); 368 369 /// getAttributes - The attributes for the specified index are 370 /// returned. Attributes for the result are denoted with Idx = 0. 371 Attributes getAttributes(unsigned Idx) const; 372 373 }; 374 375 } // End llvm namespace 376 377 #endif 378