blob: 1d05e238587406f4d2aca5729ddc70fc4de5ad79 [file] [log] [blame]
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +00001=====================
Joe Abbey0013a5d2013-02-12 11:45:22 +00002How To Use Attributes
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +00003=====================
Joe Abbey0013a5d2013-02-12 11:45:22 +00004
5.. contents::
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +00006 :local:
Joe Abbey0013a5d2013-02-12 11:45:22 +00007
8Introduction
9============
10
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000011Attributes in LLVM have changed in some fundamental ways. It was necessary to
12do this to support expanding the attributes to encompass more than a handful of
13attributes --- e.g. command line options. The old way of handling attributes
14consisted of representing them as a bit mask of values. This bit mask was
15stored in a "list" structure that was reference counted. The advantage of this
16was that attributes could be manipulated with 'or's and 'and's. The
17disadvantage of this was that there was limited room for expansion, and
18virtually no support for attribute-value pairs other than alignment.
Joe Abbey0013a5d2013-02-12 11:45:22 +000019
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000020In the new scheme, an ``Attribute`` object represents a single attribute that's
21uniqued. You use the ``Attribute::get`` methods to create a new ``Attribute``
22object. An attribute can be a single "enum" value (the enum being the
23``Attribute::AttrKind`` enum), a string representing a target-dependent
24attribute, or an attribute-value pair. Some examples:
Joe Abbey0013a5d2013-02-12 11:45:22 +000025
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000026* Target-independent: ``noinline``, ``zext``
27* Target-dependent: ``"no-sse"``, ``"thumb2"``
28* Attribute-value pair: ``"cpu" = "cortex-a8"``, ``align = 4``
Joe Abbey0013a5d2013-02-12 11:45:22 +000029
30Note: for an attribute value pair, we expect a target-dependent attribute to
31have a string for the value.
32
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000033``Attribute``
34=============
35An ``Attribute`` object is designed to be passed around by value.
Joe Abbey0013a5d2013-02-12 11:45:22 +000036
37Because attributes are no longer represented as a bit mask, you will need to
38convert any code which does treat them as a bit mask to use the new query
39methods on the Attribute class.
40
Reid Kleckner67077702017-03-21 16:57:19 +000041``AttributeList``
Reid Kleckner3b530722017-03-21 17:05:00 +000042=================
Joe Abbey0013a5d2013-02-12 11:45:22 +000043
Reid Kleckner67077702017-03-21 16:57:19 +000044The ``AttributeList`` stores a collection of Attribute objects for each kind of
45object that may have an attribute associated with it: the function as a whole,
46the return type, or the function's parameters. A function's attributes are at
47index ``AttributeList::FunctionIndex``; the return type's attributes are at
48index ``AttributeList::ReturnIndex``; and the function's parameters' attributes
49are at indices 1, ..., n (where 'n' is the number of parameters). Most methods
50on the ``AttributeList`` class take an index parameter.
Joe Abbey0013a5d2013-02-12 11:45:22 +000051
Reid Kleckner67077702017-03-21 16:57:19 +000052An ``AttributeList`` is also a uniqued and immutable object. You create an
53``AttributeList`` through the ``AttributeList::get`` methods. You can add and
54remove attributes, which result in the creation of a new ``AttributeList``.
Joe Abbey0013a5d2013-02-12 11:45:22 +000055
Reid Kleckner67077702017-03-21 16:57:19 +000056An ``AttributeList`` object is designed to be passed around by value.
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000057
Reid Kleckner67077702017-03-21 16:57:19 +000058Note: It is advised that you do *not* use the ``AttributeList`` "introspection"
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000059methods (e.g. ``Raw``, ``getRawPointer``, etc.). These methods break
60encapsulation, and may be removed in a future release (i.e. LLVM 4.0).
61
62``AttrBuilder``
63===============
64
Reid Kleckner67077702017-03-21 16:57:19 +000065Lastly, we have a "builder" class to help create the ``AttributeList`` object
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000066without having to create several different intermediate uniqued
Reid Kleckner67077702017-03-21 16:57:19 +000067``AttributeList`` objects. The ``AttrBuilder`` class allows you to add and
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000068remove attributes at will. The attributes won't be uniqued until you call the
Reid Kleckner67077702017-03-21 16:57:19 +000069appropriate ``AttributeList::get`` method.
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000070
71An ``AttrBuilder`` object is *not* designed to be passed around by value. It
72should be passed by reference.
73
74Note: It is advised that you do *not* use the ``AttrBuilder::addRawValue()``
75method or the ``AttrBuilder(uint64_t Val)`` constructor. These are for
76backwards compatibility and may be removed in a future release (i.e. LLVM 4.0).
Joe Abbey0013a5d2013-02-12 11:45:22 +000077
78And that's basically it! A lot of functionality is hidden behind these classes,
79but the interfaces are pretty straight forward.
Dmitri Gribenko1cb058f2013-02-12 18:26:08 +000080