Attributes.h revision 22bd64173981bf1251c4b3bfc684207340534ba3
1710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov//===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
26091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//
36091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//                     The LLVM Compiler Infrastructure
46091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
76091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//
86091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//===----------------------------------------------------------------------===//
927107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling///
1027107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \file
1127107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \brief This file contains the simple types necessary to represent the
1227107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// attributes associated with functions and their calls.
1327107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling///
146091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer//===----------------------------------------------------------------------===//
156091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer
160598866c052147c31b808391f58434ce3dbfb838Devang Patel#ifndef LLVM_ATTRIBUTES_H
170598866c052147c31b808391f58434ce3dbfb838Devang Patel#define LLVM_ATTRIBUTES_H
186091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer
19d509d0b532ec2358b3f341d4a4cd1411cb8b5db2Chris Lattner#include "llvm/ADT/ArrayRef.h"
2022bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling#include "llvm/Support/MathExtras.h"
2122bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling#include <cassert>
2258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner#include <string>
236091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer
246091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencernamespace llvm {
25d426a642a23a234547cbc7061f5bfec157673249Bill Wendling
26702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendlingclass AttrBuilder;
27f6670729aabc1fab85238d2b306a1c1767a807bbBill Wendlingclass AttributeImpl;
282c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendlingclass LLVMContext;
29ad9a9e15595bc9d5ba1ed752caf8572957f77a3dDuncan Sandsclass Type;
30ad9a9e15595bc9d5ba1ed752caf8572957f77a3dDuncan Sands
311d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling//===----------------------------------------------------------------------===//
3227107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \class
3327107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \brief Functions, function parameters, and return types can have attributes
341d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling/// to indicate how they should be treated by optimizations and code
351d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling/// generation. This class represents one of those attributes. It's light-weight
361d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling/// and should be passed around by-value.
37034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendlingclass Attribute {
386765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendlingpublic:
391d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// This enumeration lists the attributes that can be associated with
401d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// parameters, function results or the function itself.
41f3d1500ab2c7364d3d0fb73a7e1b8c6339ab48b1Bill Wendling  ///
421d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// Note: uwtable is about the ABI or the user mandating an entry in the
4311d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// unwind table. The nounwind attribute is about an exception passing by the
4411d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// function.
45f3d1500ab2c7364d3d0fb73a7e1b8c6339ab48b1Bill Wendling  ///
4611d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// In a theoretical system that uses tables for profiling and sjlj for
4711d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// exceptions, they would be fully independent. In a normal system that uses
4811d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// tables for both, the semantics are:
49f3d1500ab2c7364d3d0fb73a7e1b8c6339ab48b1Bill Wendling  ///
5011d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// nil                = Needs an entry because an exception might pass by.
5111d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// nounwind           = No need for an entry
5211d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// uwtable            = Needs an entry because the ABI says so and because
5311d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  ///                      an exception might pass by.
5411d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  /// uwtable + nounwind = Needs an entry because the ABI says so.
5511d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling
56629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  enum AttrKind {
575a0eeb5a9d727940b1dbe8dff6e9aa292ada0f6aBill Wendling    // IR-Level Attributes
58480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    None,                  ///< No attributes have been set
59480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    AddressSafety,         ///< Address safety checking is on.
60480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    Alignment,             ///< Alignment of parameter (5 bits)
616765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling                           ///< stored as log2 of alignment with +1 bias
62f6670729aabc1fab85238d2b306a1c1767a807bbBill Wendling                           ///< 0 means unaligned (different from align(1))
63480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    AlwaysInline,          ///< inline=always
64480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    ByVal,                 ///< Pass structure by value
65480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    InlineHint,            ///< Source said inlining was desirable
66480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    InReg,                 ///< Force argument to be passed in register
679a419f656e278b96e9dfe739cd63c7bff9a4e1fdQuentin Colombet    MinSize,               ///< Function must be optimized for size first
68480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    Naked,                 ///< Naked function
69480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    Nest,                  ///< Nested function static chain
70480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoAlias,               ///< Considered to not alias after call
71480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoCapture,             ///< Function creates no aliases of pointer
7267ae13575900e8efd056672987249fd0adbf5e73James Molloy    NoDuplicate,           ///< Call cannot be duplicated
73480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoImplicitFloat,       ///< Disable implicit floating point insts
74480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoInline,              ///< inline=never
75480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NonLazyBind,           ///< Function is called early and/or
763a106e60366a51b4594ec303ff8dbbc58913227fBill Wendling                           ///< often, so lazy binding isn't worthwhile
77480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoRedZone,             ///< Disable redzone
78480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoReturn,              ///< Mark the function as not returning
79480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    NoUnwind,              ///< Function doesn't unwind stack
80480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    OptimizeForSize,       ///< opt_size
81480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    ReadNone,              ///< Function does not access memory
82480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    ReadOnly,              ///< Function only reads from memory
83480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    ReturnsTwice,          ///< Function can return twice
84480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    SExt,                  ///< Sign extended before/after call
85480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    StackAlignment,        ///< Alignment of stack for function (3 bits)
866765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling                           ///< stored as log2 of alignment with +1 bias 0
876765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling                           ///< means unaligned (different from
88f6670729aabc1fab85238d2b306a1c1767a807bbBill Wendling                           ///< alignstack=(1))
89480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    StackProtect,          ///< Stack protection.
90480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    StackProtectReq,       ///< Stack protection required.
91480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    StructRet,             ///< Hidden pointer to structure to return
92480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    UWTable,               ///< Function must be in a unwind table
93480b1b28ea6fc1bb5c78d99472df624cfd3fce47Bill Wendling    ZExt                   ///< Zero extended before/after call
946765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling  };
956765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendlingprivate:
9627107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling  AttributeImpl *pImpl;
9727107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling  Attribute(AttributeImpl *A) : pImpl(A) {}
98d426a642a23a234547cbc7061f5bfec157673249Bill Wendlingpublic:
9927107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling  Attribute() : pImpl(0) {}
1002c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
1011d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Return a uniquified Attribute object. This takes the uniquified
102034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  /// value from the Builder and wraps it in the Attribute class.
103629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals);
104034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  static Attribute get(LLVMContext &Context, AttrBuilder &B);
1052c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
1061d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Return true if the attribute is present.
107629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  bool hasAttribute(AttrKind Val) const;
1082e879bcd52583335c753c005d203bf2ffe8b67b5Bill Wendling
1091d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Return true if attributes exist
11005cc40d20c0f3b2f1bd5cb86ceb9f32d07cae110Bill Wendling  bool hasAttributes() const;
1112e879bcd52583335c753c005d203bf2ffe8b67b5Bill Wendling
1121d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Return true if the attributes are a non-null intersection.
113034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  bool hasAttributes(const Attribute &A) const;
114e853d2e2508e21b5c3156c7d8b6e6902a7d2604aBill Wendling
1151d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Returns the alignment field of an attribute as a byte alignment
116ef99fe8efaa6cb74c66e570a6ef467debca92911Bill Wendling  /// value.
117e66f3d3ba0ea9f82f65a29c47fc37e997cbf0aceBill Wendling  unsigned getAlignment() const;
118ef99fe8efaa6cb74c66e570a6ef467debca92911Bill Wendling
1191d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Returns the stack alignment field of an attribute as a byte
120943c29135e03e55f9a5dab393786171a4a536482Bill Wendling  /// alignment value.
121e66f3d3ba0ea9f82f65a29c47fc37e997cbf0aceBill Wendling  unsigned getStackAlignment() const;
12230b483c94001927b3593ed200e823104bab51660Bill Wendling
12392e287f5bde8d34af9c3f2979afb6cd05bfb452cBill Wendling  bool operator==(AttrKind K) const;
12492e287f5bde8d34af9c3f2979afb6cd05bfb452cBill Wendling  bool operator!=(AttrKind K) const;
12592e287f5bde8d34af9c3f2979afb6cd05bfb452cBill Wendling
12692e287f5bde8d34af9c3f2979afb6cd05bfb452cBill Wendling  // FIXME: Remove these 'operator' methods.
127034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  bool operator==(const Attribute &A) const {
12827107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling    return pImpl == A.pImpl;
129d426a642a23a234547cbc7061f5bfec157673249Bill Wendling  }
130034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  bool operator!=(const Attribute &A) const {
13127107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling    return pImpl != A.pImpl;
132d426a642a23a234547cbc7061f5bfec157673249Bill Wendling  }
133e66f3d3ba0ea9f82f65a29c47fc37e997cbf0aceBill Wendling
134c966e08a9895a39f575d917a8390c8e359f042f5Bill Wendling  uint64_t getBitMask() const;
1358ce1e432d1c942d101617b3c623970a4f763f93dBill Wendling
1361d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief Which attributes cannot be applied to a type.
137034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  static Attribute typeIncompatible(Type *Ty);
138623a389f623dfef44f07211bd12fbd2a93db09e3Duncan Sands
1391d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief This returns an integer containing an encoding of all the LLVM
1401d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// attributes found in the given attribute bitset.  Any change to this
1411d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// encoding is a breaking change to bitcode compatibility.
142034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
1432039a8f83464e900a71f3e846c7749b2d0d8cde2Bill Wendling
1441d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// \brief This returns an attribute bitset containing the LLVM attributes
1451d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// that have been decoded from the given integer.  This function must stay in
1461d3dcfe4246b4d45fa78a8dfd0a11c7fff842c15Bill Wendling  /// sync with 'encodeLLVMAttributesForBitcode'.
147034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
14892e287f5bde8d34af9c3f2979afb6cd05bfb452cBill Wendling                                                  uint64_t EncodedAttrs);
1492c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling
15094e94b350652d3a71993bbc7d44afbe3b304605eBill Wendling  /// \brief The Attribute is converted to a string of equivalent mnemonic. This
15194e94b350652d3a71993bbc7d44afbe3b304605eBill Wendling  /// is, presumably, for writing out the mnemonics for the assembly writer.
1522c79ecbd704c656178ffa43d5a58ebe3ca188b40Bill Wendling  std::string getAsString() const;
153827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands};
154827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands
155e66f3d3ba0ea9f82f65a29c47fc37e997cbf0aceBill Wendling//===----------------------------------------------------------------------===//
15627107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \class
15727107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \brief This class is used in conjunction with the Attribute::get method to
15827107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// create an Attribute object. The object itself is uniquified. The Builder's
15927107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// value, however, is not. So this can be used as a quick way to test for
16027107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// equality, presence of attributes, etc.
161702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendlingclass AttrBuilder {
16222bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  uint64_t Bits;
163702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendlingpublic:
16422bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  AttrBuilder() : Bits(0) {}
165702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  explicit AttrBuilder(uint64_t B) : Bits(B) {}
166c966e08a9895a39f575d917a8390c8e359f042f5Bill Wendling  AttrBuilder(const Attribute &A) : Bits(A.getBitMask()) {}
167702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
16822bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  void clear() { Bits = 0; }
169702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
17022bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// addAttribute - Add an attribute to the builder.
171629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  AttrBuilder &addAttribute(Attribute::AttrKind Val);
172702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
17322bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// removeAttribute - Remove an attribute from the builder.
174629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  AttrBuilder &removeAttribute(Attribute::AttrKind Val);
175702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
17622bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// addAttribute - Add the attributes from A to the builder.
177034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  AttrBuilder &addAttributes(const Attribute &A);
178702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
17922bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// removeAttribute - Remove the attributes from A from the builder.
180034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  AttrBuilder &removeAttributes(const Attribute &A);
181702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
18222bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// \brief Return true if the builder has the specified attribute.
18322bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  bool contains(Attribute::AttrKind A) const;
18422bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling
18522bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// hasAttributes - Return true if the builder has IR-level attributes.
186702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  bool hasAttributes() const;
187702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
18822bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// hasAttributes - Return true if the builder has any attribute that's in the
189702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  /// specified attribute.
190034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  bool hasAttributes(const Attribute &A) const;
191702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
19222bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
193702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  bool hasAlignmentAttr() const;
194702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
19522bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// getAlignment - Retrieve the alignment attribute, if it exists.
196702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  uint64_t getAlignment() const;
197702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
19822bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
199702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  uint64_t getStackAlignment() const;
200702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
20122bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// addAlignmentAttr - This turns an int alignment (which must be a power of
20222bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// 2) into the form used internally in Attribute.
203702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  AttrBuilder &addAlignmentAttr(unsigned Align);
204702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
20522bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
20622bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// power of 2) into the form used internally in Attribute.
207702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  AttrBuilder &addStackAlignmentAttr(unsigned Align);
208702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
20922bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// addRawValue - Add the raw value to the internal representation.
210702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  /// N.B. This should be used ONLY for decoding LLVM bitcode!
211702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  AttrBuilder &addRawValue(uint64_t Val);
212702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
21322bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  /// @brief Remove attributes that are used on functions only.
214702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  void removeFunctionOnlyAttrs() {
215034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling    removeAttribute(Attribute::NoReturn)
216034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::NoUnwind)
217034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::ReadNone)
218034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::ReadOnly)
219034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::NoInline)
220034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::AlwaysInline)
221034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::OptimizeForSize)
222034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::StackProtect)
223034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::StackProtectReq)
224034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::NoRedZone)
225034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::NoImplicitFloat)
226034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::Naked)
227034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::InlineHint)
228034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::StackAlignment)
229034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::UWTable)
230034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::NonLazyBind)
231034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::ReturnsTwice)
232034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling      .removeAttribute(Attribute::AddressSafety)
23367ae13575900e8efd056672987249fd0adbf5e73James Molloy      .removeAttribute(Attribute::MinSize)
23467ae13575900e8efd056672987249fd0adbf5e73James Molloy      .removeAttribute(Attribute::NoDuplicate);
235702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  }
236702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
237c966e08a9895a39f575d917a8390c8e359f042f5Bill Wendling  uint64_t getBitMask() const { return Bits; }
238a5c699d5f7de7eb88b7f549539b6550653dea74eBill Wendling
23922bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  bool operator==(const AttrBuilder &B) {
240702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling    return Bits == B.Bits;
241702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  }
24222bd64173981bf1251c4b3bfc684207340534ba3Bill Wendling  bool operator!=(const AttrBuilder &B) {
243702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling    return Bits != B.Bits;
244702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling  }
245702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling};
246702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling
247702cc91aa1bd41540e8674921ae7ac89a4ff061fBill Wendling//===----------------------------------------------------------------------===//
24827107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \class
24927107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \brief This is just a pair of values to associate a set of attributes with
25018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling/// an index.
2510598866c052147c31b808391f58434ce3dbfb838Devang Patelstruct AttributeWithIndex {
252034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  Attribute Attrs;  ///< The attributes that are set, or'd together.
25318e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  unsigned Index;   ///< Index of the parameter for which the attributes apply.
25418e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling                    ///< Index 0 is used for return value attributes.
25518e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling                    ///< Index ~0U is used for function attributes.
256710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
257cb3de0bc800d7920087b19bb12a545d4cc84114eBill Wendling  static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
258629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling                                ArrayRef<Attribute::AttrKind> Attrs) {
259034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling    return get(Idx, Attribute::get(C, Attrs));
26011d00420e42ba88c3b48cab997965a7be79315e2Bill Wendling  }
261034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
2620598866c052147c31b808391f58434ce3dbfb838Devang Patel    AttributeWithIndex P;
26358d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    P.Index = Idx;
26458d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner    P.Attrs = Attrs;
26566250e97eff4f9e7dd097ccc50a78446bf013a4cChris Lattner    return P;
26666250e97eff4f9e7dd097ccc50a78446bf013a4cChris Lattner  }
2674f859aa532dbf061736f9c23e0d0882b5cdfe566Reid Spencer};
268710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
26958d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner//===----------------------------------------------------------------------===//
27099faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling// AttributeSet Smart Pointer
27158d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner//===----------------------------------------------------------------------===//
27258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner
27318e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendlingclass AttributeSetImpl;
274710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
27518e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling//===----------------------------------------------------------------------===//
27627107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \class
27727107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling/// \brief This class manages the ref count for the opaque AttributeSetImpl
27818e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling/// object and provides accessors for it.
27999faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendlingclass AttributeSet {
28007aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendlingpublic:
28107aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling  enum AttrIndex {
28207aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling    ReturnIndex = 0U,
28307aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling    FunctionIndex = ~0U
28407aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling  };
28507aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendlingprivate:
28618e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief The attributes that we are managing.  This can be null to represent
2870976e00fd1cbf4128daeb72efd8957d00383fda9Bill Wendling  /// the empty attributes list.
28818e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  AttributeSetImpl *AttrList;
2890976e00fd1cbf4128daeb72efd8957d00383fda9Bill Wendling
29018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief The attributes for the specified index are returned.  Attributes
2910976e00fd1cbf4128daeb72efd8957d00383fda9Bill Wendling  /// for the result are denoted with Idx = 0.
292034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  Attribute getAttributes(unsigned Idx) const;
2930976e00fd1cbf4128daeb72efd8957d00383fda9Bill Wendling
29418e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
29558d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattnerpublic:
29699faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  AttributeSet() : AttrList(0) {}
29799faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
29899faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  const AttributeSet &operator=(const AttributeSet &RHS);
299710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
30058d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
3010598866c052147c31b808391f58434ce3dbfb838Devang Patel  // Attribute List Construction and Mutation
30258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
303710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
30418e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return an AttributeSet with the specified parameters in it.
30599faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
30658d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner
30718e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Add the specified attribute at the specified index to this
30818e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// attribute list.  Since attribute lists are immutable, this returns the new
30918e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// list.
310034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
311710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
31218e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Remove the specified attribute at the specified index from this
31318e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// attribute list.  Since attribute lists are immutable, this returns the new
31418e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// list.
315034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
316710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
31758d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
3180598866c052147c31b808391f58434ce3dbfb838Devang Patel  // Attribute List Accessors
31958d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
32018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling
32118e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief The attributes for the specified index are returned.
322034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  Attribute getParamAttributes(unsigned Idx) const {
32319c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    return getAttributes(Idx);
32419c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  }
32519c874638d9478a5d5028854817a5ee72293bb2bDevang Patel
32618e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief The attributes for the ret value are returned.
327034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  Attribute getRetAttributes() const {
32807aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling    return getAttributes(ReturnIndex);
32919c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  }
33019c874638d9478a5d5028854817a5ee72293bb2bDevang Patel
33118e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief The function attributes are returned.
332034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  Attribute getFnAttributes() const {
33307aae2e7d58fe23e370e0cbb9e1a3def99434c36Bill Wendling    return getAttributes(FunctionIndex);
33419c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  }
335710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
33618e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return the alignment for the specified function parameter.
33758d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  unsigned getParamAlignment(unsigned Idx) const {
338ef99fe8efaa6cb74c66e570a6ef467debca92911Bill Wendling    return getAttributes(Idx).getAlignment();
33958d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  }
340710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
341831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  /// \brief Return true if the attribute exists at the given index.
342831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
343831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling
344831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  /// \brief Return true if attribute exists at the given index.
345831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  bool hasAttributes(unsigned Index) const;
346831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling
347831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  /// \brief Get the stack alignment.
348831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  unsigned getStackAlignment(unsigned Index) const;
349831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling
350831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  /// \brief Return the attributes at the index as a string.
351831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  std::string getAsString(unsigned Index) const;
352831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling
353831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling  uint64_t getBitMask(unsigned Index) const;
354831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling
35518e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return true if the specified attribute is set for at least one
35618e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// parameter or for the return value.
357629fb82419d9bfff6ae475363bcce66192dfcc8eBill Wendling  bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
35858d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner
359041221c0972ff575b07f76808c504833d629ae1fChris Lattner  /// operator==/!= - Provide equality predicates.
36018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  bool operator==(const AttributeSet &RHS) const {
36118e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling    return AttrList == RHS.AttrList;
36218e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  }
36318e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  bool operator!=(const AttributeSet &RHS) const {
36418e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling    return AttrList != RHS.AttrList;
36518e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  }
366710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
36758d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
3680598866c052147c31b808391f58434ce3dbfb838Devang Patel  // Attribute List Introspection
36958d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  //===--------------------------------------------------------------------===//
370710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
37118e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return a raw pointer that uniquely identifies this attribute list.
37258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  void *getRawPointer() const {
3730598866c052147c31b808391f58434ce3dbfb838Devang Patel    return AttrList;
37458d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  }
375710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
376034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  // Attributes are stored as a dense set of slots, where there is one slot for
377034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  // each argument that has an attribute.  This allows walking over the dense
378034b94b17006f51722886b0f2283fb6fb19aca1fBill Wendling  // set instead of walking the sparse list of attributes.
379710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
38018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return true if there are no attributes.
38158d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  bool isEmpty() const {
3820598866c052147c31b808391f58434ce3dbfb838Devang Patel    return AttrList == 0;
38358d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  }
384710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
38518e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return the number of slots used in this attribute list.  This is
38618e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// the number of arguments that have an attribute set on them (including the
38718e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// function itself).
38858d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner  unsigned getNumSlots() const;
389710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
39018e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// \brief Return the AttributeWithIndex at the specified slot.  This holds a
39118e7211068c9d2c6204512f9c468ee179818a4b6Bill Wendling  /// index number plus a set of attributes.
3920598866c052147c31b808391f58434ce3dbfb838Devang Patel  const AttributeWithIndex &getSlot(unsigned Slot) const;
393710632d07b13609444626367bebd34c0af3acb6aMikhail Glushenkov
394f3d1500ab2c7364d3d0fb73a7e1b8c6339ab48b1Bill Wendling  void dump() const;
39558d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner};
3964f859aa532dbf061736f9c23e0d0882b5cdfe566Reid Spencer
39727107f6ab4627fa38bcacad6757ed6d52910f880Bill Wendling} // end llvm namespace
3986091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer
3996091ebd172a16a10f1ea66061a5fa7cbf5139e56Reid Spencer#endif
400