11c3e3c14SDmitri Gribenko=====================
2ba3e901eSJoe AbbeyHow To Use Attributes
31c3e3c14SDmitri Gribenko=====================
4ba3e901eSJoe Abbey
5ba3e901eSJoe Abbey.. contents::
6ba3e901eSJoe Abbey  :local:
7ba3e901eSJoe Abbey
8ba3e901eSJoe AbbeyIntroduction
9ba3e901eSJoe Abbey============
10ba3e901eSJoe Abbey
111c3e3c14SDmitri GribenkoAttributes in LLVM have changed in some fundamental ways.  It was necessary to
121c3e3c14SDmitri Gribenkodo this to support expanding the attributes to encompass more than a handful of
13ba3e901eSJoe Abbeyattributes --- e.g. command line options.  The old way of handling attributes
141c3e3c14SDmitri Gribenkoconsisted of representing them as a bit mask of values.  This bit mask was
151c3e3c14SDmitri Gribenkostored in a "list" structure that was reference counted.  The advantage of this
161c3e3c14SDmitri Gribenkowas that attributes could be manipulated with 'or's and 'and's.  The
171c3e3c14SDmitri Gribenkodisadvantage of this was that there was limited room for expansion, and
181c3e3c14SDmitri Gribenkovirtually no support for attribute-value pairs other than alignment.
19ba3e901eSJoe Abbey
201c3e3c14SDmitri GribenkoIn the new scheme, an ``Attribute`` object represents a single attribute that's
211c3e3c14SDmitri Gribenkouniqued.  You use the ``Attribute::get`` methods to create a new ``Attribute``
22ba3e901eSJoe Abbeyobject.  An attribute can be a single "enum" value (the enum being the
231c3e3c14SDmitri Gribenko``Attribute::AttrKind`` enum), a string representing a target-dependent
241c3e3c14SDmitri Gribenkoattribute, or an attribute-value pair.  Some examples:
25ba3e901eSJoe Abbey
261c3e3c14SDmitri Gribenko* Target-independent: ``noinline``, ``zext``
271c3e3c14SDmitri Gribenko* Target-dependent: ``"no-sse"``, ``"thumb2"``
281c3e3c14SDmitri Gribenko* Attribute-value pair: ``"cpu" = "cortex-a8"``, ``align = 4``
29ba3e901eSJoe Abbey
30ba3e901eSJoe AbbeyNote: for an attribute value pair, we expect a target-dependent attribute to
31ba3e901eSJoe Abbeyhave a string for the value.
32ba3e901eSJoe Abbey
331c3e3c14SDmitri Gribenko``Attribute``
341c3e3c14SDmitri Gribenko=============
351c3e3c14SDmitri GribenkoAn ``Attribute`` object is designed to be passed around by value.
36ba3e901eSJoe Abbey
37ba3e901eSJoe AbbeyBecause attributes are no longer represented as a bit mask, you will need to
38ba3e901eSJoe Abbeyconvert any code which does treat them as a bit mask to use the new query
39ba3e901eSJoe Abbeymethods on the Attribute class.
40ba3e901eSJoe Abbey
41b518054bSReid Kleckner``AttributeList``
42974a349bSReid Kleckner=================
43ba3e901eSJoe Abbey
44b518054bSReid KlecknerThe ``AttributeList`` stores a collection of Attribute objects for each kind of
45b518054bSReid Klecknerobject that may have an attribute associated with it: the function as a whole,
46b518054bSReid Klecknerthe return type, or the function's parameters.  A function's attributes are at
47b518054bSReid Klecknerindex ``AttributeList::FunctionIndex``; the return type's attributes are at
48b518054bSReid Klecknerindex ``AttributeList::ReturnIndex``; and the function's parameters' attributes
49b518054bSReid Klecknerare at indices 1, ..., n (where 'n' is the number of parameters).  Most methods
50b518054bSReid Kleckneron the ``AttributeList`` class take an index parameter.
51ba3e901eSJoe Abbey
52b518054bSReid KlecknerAn ``AttributeList`` is also a uniqued and immutable object.  You create an
53b518054bSReid Kleckner``AttributeList`` through the ``AttributeList::get`` methods.  You can add and
54b518054bSReid Klecknerremove attributes, which result in the creation of a new ``AttributeList``.
55ba3e901eSJoe Abbey
56b518054bSReid KlecknerAn ``AttributeList`` object is designed to be passed around by value.
571c3e3c14SDmitri Gribenko
58b518054bSReid KlecknerNote: It is advised that you do *not* use the ``AttributeList`` "introspection"
591c3e3c14SDmitri Gribenkomethods (e.g. ``Raw``, ``getRawPointer``, etc.).  These methods break
60*4abf0243SMehdi Aminiencapsulation, and may be removed in a future release.
611c3e3c14SDmitri Gribenko
621c3e3c14SDmitri Gribenko``AttrBuilder``
631c3e3c14SDmitri Gribenko===============
641c3e3c14SDmitri Gribenko
65b518054bSReid KlecknerLastly, we have a "builder" class to help create the ``AttributeList`` object
661c3e3c14SDmitri Gribenkowithout having to create several different intermediate uniqued
67b518054bSReid Kleckner``AttributeList`` objects.  The ``AttrBuilder`` class allows you to add and
681c3e3c14SDmitri Gribenkoremove attributes at will.  The attributes won't be uniqued until you call the
69b518054bSReid Klecknerappropriate ``AttributeList::get`` method.
701c3e3c14SDmitri Gribenko
711c3e3c14SDmitri GribenkoAn ``AttrBuilder`` object is *not* designed to be passed around by value.  It
721c3e3c14SDmitri Gribenkoshould be passed by reference.
731c3e3c14SDmitri Gribenko
741c3e3c14SDmitri GribenkoNote: It is advised that you do *not* use the ``AttrBuilder::addRawValue()``
751c3e3c14SDmitri Gribenkomethod or the ``AttrBuilder(uint64_t Val)`` constructor.  These are for
76*4abf0243SMehdi Aminibackwards compatibility and may be removed in a future release.
77ba3e901eSJoe Abbey
78ba3e901eSJoe AbbeyAnd that's basically it! A lot of functionality is hidden behind these classes,
79ba3e901eSJoe Abbeybut the interfaces are pretty straight forward.
801c3e3c14SDmitri Gribenko
81