Attributes.h revision 3ac03815e195a598fce50abdda440c9d2fae815e
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/// \file 11/// \brief This file contains the simple types necessary to represent the 12/// attributes associated with functions and their calls. 13/// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_ATTRIBUTES_H 17#define LLVM_ATTRIBUTES_H 18 19#include "llvm/ADT/ArrayRef.h" 20#include "llvm/Support/MathExtras.h" 21#include <cassert> 22#include <string> 23 24namespace llvm { 25 26class AttrBuilder; 27class AttributeImpl; 28class LLVMContext; 29class Type; 30 31//===----------------------------------------------------------------------===// 32/// \class 33/// \brief Functions, function parameters, and return types can have attributes 34/// to indicate how they should be treated by optimizations and code 35/// generation. This class represents one of those attributes. It's light-weight 36/// and should be passed around by-value. 37class Attribute { 38public: 39 /// This enumeration lists the attributes that can be associated with 40 /// parameters, function results or the function itself. 41 /// 42 /// Note: uwtable is about the ABI or the user mandating an entry in the 43 /// unwind table. The nounwind attribute is about an exception passing by the 44 /// function. 45 /// 46 /// In a theoretical system that uses tables for profiling and sjlj for 47 /// exceptions, they would be fully independent. In a normal system that uses 48 /// tables for both, the semantics are: 49 /// 50 /// nil = Needs an entry because an exception might pass by. 51 /// nounwind = No need for an entry 52 /// uwtable = Needs an entry because the ABI says so and because 53 /// an exception might pass by. 54 /// uwtable + nounwind = Needs an entry because the ABI says so. 55 56 enum AttrKind { 57 // IR-Level Attributes 58 None, ///< No attributes have been set 59 AddressSafety, ///< Address safety checking is on. 60 Alignment, ///< Alignment of parameter (5 bits) 61 ///< stored as log2 of alignment with +1 bias 62 ///< 0 means unaligned (different from align(1)) 63 AlwaysInline, ///< inline=always 64 ByVal, ///< Pass structure by value 65 InlineHint, ///< Source said inlining was desirable 66 InReg, ///< Force argument to be passed in register 67 MinSize, ///< Function must be optimized for size first 68 Naked, ///< Naked function 69 Nest, ///< Nested function static chain 70 NoAlias, ///< Considered to not alias after call 71 NoCapture, ///< Function creates no aliases of pointer 72 NoDuplicate, ///< Call cannot be duplicated 73 NoImplicitFloat, ///< Disable implicit floating point insts 74 NoInline, ///< inline=never 75 NonLazyBind, ///< Function is called early and/or 76 ///< often, so lazy binding isn't worthwhile 77 NoRedZone, ///< Disable redzone 78 NoReturn, ///< Mark the function as not returning 79 NoUnwind, ///< Function doesn't unwind stack 80 OptimizeForSize, ///< opt_size 81 ReadNone, ///< Function does not access memory 82 ReadOnly, ///< Function only reads from memory 83 ReturnsTwice, ///< Function can return twice 84 SExt, ///< Sign extended before/after call 85 StackAlignment, ///< Alignment of stack for function (3 bits) 86 ///< stored as log2 of alignment with +1 bias 0 87 ///< means unaligned (different from 88 ///< alignstack=(1)) 89 StackProtect, ///< Stack protection. 90 StackProtectReq, ///< Stack protection required. 91 StructRet, ///< Hidden pointer to structure to return 92 UWTable, ///< Function must be in a unwind table 93 ZExt ///< Zero extended before/after call 94 }; 95private: 96 AttributeImpl *pImpl; 97 Attribute(AttributeImpl *A) : pImpl(A) {} 98public: 99 Attribute() : pImpl(0) {} 100 101 /// \brief Return a uniquified Attribute object. This takes the uniquified 102 /// value from the Builder and wraps it in the Attribute class. 103 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals); 104 static Attribute get(LLVMContext &Context, AttrBuilder &B); 105 106 /// \brief Return true if the attribute is present. 107 bool hasAttribute(AttrKind Val) const; 108 109 /// \brief Return true if attributes exist 110 bool hasAttributes() const; 111 112 /// \brief Return true if the attributes are a non-null intersection. 113 bool hasAttributes(const Attribute &A) const; 114 115 /// \brief Returns the alignment field of an attribute as a byte alignment 116 /// value. 117 unsigned getAlignment() const; 118 119 /// \brief Returns the stack alignment field of an attribute as a byte 120 /// alignment value. 121 unsigned getStackAlignment() const; 122 123 bool operator==(AttrKind K) const; 124 bool operator!=(AttrKind K) const; 125 126 // FIXME: Remove these 'operator' methods. 127 bool operator==(const Attribute &A) const { 128 return pImpl == A.pImpl; 129 } 130 bool operator!=(const Attribute &A) const { 131 return pImpl != A.pImpl; 132 } 133 134 uint64_t getBitMask() const; 135 136 /// \brief Which attributes cannot be applied to a type. 137 static Attribute typeIncompatible(Type *Ty); 138 139 /// \brief This returns an integer containing an encoding of all the LLVM 140 /// attributes found in the given attribute bitset. Any change to this 141 /// encoding is a breaking change to bitcode compatibility. 142 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs); 143 144 /// \brief This returns an attribute bitset containing the LLVM attributes 145 /// that have been decoded from the given integer. This function must stay in 146 /// sync with 'encodeLLVMAttributesForBitcode'. 147 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C, 148 uint64_t EncodedAttrs); 149 150 /// \brief The Attribute is converted to a string of equivalent mnemonic. This 151 /// is, presumably, for writing out the mnemonics for the assembly writer. 152 std::string getAsString() const; 153}; 154 155//===----------------------------------------------------------------------===// 156/// \class 157/// \brief This class is used in conjunction with the Attribute::get method to 158/// create an Attribute object. The object itself is uniquified. The Builder's 159/// value, however, is not. So this can be used as a quick way to test for 160/// equality, presence of attributes, etc. 161class AttrBuilder { 162 uint64_t Bits; 163public: 164 AttrBuilder() : Bits(0) {} 165 explicit AttrBuilder(uint64_t B) : Bits(B) {} 166 AttrBuilder(const Attribute &A) : Bits(A.getBitMask()) {} 167 168 void clear() { Bits = 0; } 169 170 /// addAttribute - Add an attribute to the builder. 171 AttrBuilder &addAttribute(Attribute::AttrKind Val); 172 173 /// removeAttribute - Remove an attribute from the builder. 174 AttrBuilder &removeAttribute(Attribute::AttrKind Val); 175 176 /// addAttribute - Add the attributes from A to the builder. 177 AttrBuilder &addAttributes(const Attribute &A); 178 179 /// removeAttribute - Remove the attributes from A from the builder. 180 AttrBuilder &removeAttributes(const Attribute &A); 181 182 /// \brief Return true if the builder has the specified attribute. 183 bool contains(Attribute::AttrKind A) const; 184 185 /// hasAttributes - Return true if the builder has IR-level attributes. 186 bool hasAttributes() const; 187 188 /// hasAttributes - Return true if the builder has any attribute that's in the 189 /// specified attribute. 190 bool hasAttributes(const Attribute &A) const; 191 192 /// hasAlignmentAttr - Return true if the builder has an alignment attribute. 193 bool hasAlignmentAttr() const; 194 195 /// getAlignment - Retrieve the alignment attribute, if it exists. 196 uint64_t getAlignment() const; 197 198 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists. 199 uint64_t getStackAlignment() const; 200 201 /// addAlignmentAttr - This turns an int alignment (which must be a power of 202 /// 2) into the form used internally in Attribute. 203 AttrBuilder &addAlignmentAttr(unsigned Align); 204 205 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a 206 /// power of 2) into the form used internally in Attribute. 207 AttrBuilder &addStackAlignmentAttr(unsigned Align); 208 209 /// addRawValue - Add the raw value to the internal representation. 210 /// N.B. This should be used ONLY for decoding LLVM bitcode! 211 AttrBuilder &addRawValue(uint64_t Val); 212 213 /// @brief Remove attributes that are used on functions only. 214 void removeFunctionOnlyAttrs() { 215 removeAttribute(Attribute::NoReturn) 216 .removeAttribute(Attribute::NoUnwind) 217 .removeAttribute(Attribute::ReadNone) 218 .removeAttribute(Attribute::ReadOnly) 219 .removeAttribute(Attribute::NoInline) 220 .removeAttribute(Attribute::AlwaysInline) 221 .removeAttribute(Attribute::OptimizeForSize) 222 .removeAttribute(Attribute::StackProtect) 223 .removeAttribute(Attribute::StackProtectReq) 224 .removeAttribute(Attribute::NoRedZone) 225 .removeAttribute(Attribute::NoImplicitFloat) 226 .removeAttribute(Attribute::Naked) 227 .removeAttribute(Attribute::InlineHint) 228 .removeAttribute(Attribute::StackAlignment) 229 .removeAttribute(Attribute::UWTable) 230 .removeAttribute(Attribute::NonLazyBind) 231 .removeAttribute(Attribute::ReturnsTwice) 232 .removeAttribute(Attribute::AddressSafety) 233 .removeAttribute(Attribute::MinSize) 234 .removeAttribute(Attribute::NoDuplicate); 235 } 236 237 uint64_t getBitMask() const { return Bits; } 238 239 bool operator==(const AttrBuilder &B) { 240 return Bits == B.Bits; 241 } 242 bool operator!=(const AttrBuilder &B) { 243 return Bits != B.Bits; 244 } 245}; 246 247//===----------------------------------------------------------------------===// 248/// \class 249/// \brief This is just a pair of values to associate a set of attributes with 250/// an index. 251struct AttributeWithIndex { 252 Attribute Attrs; ///< The attributes that are set, or'd together. 253 unsigned Index; ///< Index of the parameter for which the attributes apply. 254 ///< Index 0 is used for return value attributes. 255 ///< Index ~0U is used for function attributes. 256 257 static AttributeWithIndex get(LLVMContext &C, unsigned Idx, 258 ArrayRef<Attribute::AttrKind> Attrs) { 259 return get(Idx, Attribute::get(C, Attrs)); 260 } 261 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) { 262 AttributeWithIndex P; 263 P.Index = Idx; 264 P.Attrs = Attrs; 265 return P; 266 } 267}; 268 269//===----------------------------------------------------------------------===// 270// AttributeSet Smart Pointer 271//===----------------------------------------------------------------------===// 272 273class AttributeSetImpl; 274 275//===----------------------------------------------------------------------===// 276/// \class 277/// \brief This class manages the ref count for the opaque AttributeSetImpl 278/// object and provides accessors for it. 279class AttributeSet { 280public: 281 enum AttrIndex { 282 ReturnIndex = 0U, 283 FunctionIndex = ~0U 284 }; 285private: 286 /// \brief The attributes that we are managing. This can be null to represent 287 /// the empty attributes list. 288 AttributeSetImpl *AttrList; 289 290 /// \brief The attributes for the specified index are returned. Attributes 291 /// for the result are denoted with Idx = 0. 292 Attribute getAttributes(unsigned Idx) const; 293 294 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {} 295public: 296 AttributeSet() : AttrList(0) {} 297 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {} 298 const AttributeSet &operator=(const AttributeSet &RHS); 299 300 //===--------------------------------------------------------------------===// 301 // Attribute List Construction and Mutation 302 //===--------------------------------------------------------------------===// 303 304 /// \brief Return an AttributeSet with the specified parameters in it. 305 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs); 306 307 /// \brief Add the specified attribute at the specified index to this 308 /// attribute list. Since attribute lists are immutable, this returns the new 309 /// list. 310 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const; 311 312 /// \brief Remove the specified attribute at the specified index from this 313 /// attribute list. Since attribute lists are immutable, this returns the new 314 /// list. 315 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const; 316 317 //===--------------------------------------------------------------------===// 318 // Attribute List Accessors 319 //===--------------------------------------------------------------------===// 320 321 /// \brief The attributes for the specified index are returned. 322 Attribute getParamAttributes(unsigned Idx) const { 323 return getAttributes(Idx); 324 } 325 326 /// \brief The attributes for the ret value are returned. 327 Attribute getRetAttributes() const { 328 return getAttributes(ReturnIndex); 329 } 330 331 /// \brief The function attributes are returned. 332 Attribute getFnAttributes() const { 333 return getAttributes(FunctionIndex); 334 } 335 336 /// \brief Return the alignment for the specified function parameter. 337 unsigned getParamAlignment(unsigned Idx) const { 338 return getAttributes(Idx).getAlignment(); 339 } 340 341 /// \brief Return true if the attribute exists at the given index. 342 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const; 343 344 /// \brief Return true if attribute exists at the given index. 345 bool hasAttributes(unsigned Index) const; 346 347 /// \brief Get the stack alignment. 348 unsigned getStackAlignment(unsigned Index) const; 349 350 /// \brief Return the attributes at the index as a string. 351 std::string getAsString(unsigned Index) const; 352 353 uint64_t getBitMask(unsigned Index) const; 354 355 /// \brief Return true if the specified attribute is set for at least one 356 /// parameter or for the return value. 357 bool hasAttrSomewhere(Attribute::AttrKind Attr) const; 358 359 /// operator==/!= - Provide equality predicates. 360 bool operator==(const AttributeSet &RHS) const { 361 return AttrList == RHS.AttrList; 362 } 363 bool operator!=(const AttributeSet &RHS) const { 364 return AttrList != RHS.AttrList; 365 } 366 367 //===--------------------------------------------------------------------===// 368 // Attribute List Introspection 369 //===--------------------------------------------------------------------===// 370 371 /// \brief Return a raw pointer that uniquely identifies this attribute list. 372 void *getRawPointer() const { 373 return AttrList; 374 } 375 376 // Attributes are stored as a dense set of slots, where there is one slot for 377 // each argument that has an attribute. This allows walking over the dense 378 // set instead of walking the sparse list of attributes. 379 380 /// \brief Return true if there are no attributes. 381 bool isEmpty() const { 382 return AttrList == 0; 383 } 384 385 /// \brief Return the number of slots used in this attribute list. This is 386 /// the number of arguments that have an attribute set on them (including the 387 /// function itself). 388 unsigned getNumSlots() const; 389 390 /// \brief Return the AttributeWithIndex at the specified slot. This holds a 391 /// index number plus a set of attributes. 392 const AttributeWithIndex &getSlot(unsigned Slot) const; 393 394 void dump() const; 395}; 396 397} // end llvm namespace 398 399#endif 400