xref: /aosp_15_r20/external/llvm/docs/CodeGenerator.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker==========================================
2*9880d681SAndroid Build Coastguard WorkerThe LLVM Target-Independent Code Generator
3*9880d681SAndroid Build Coastguard Worker==========================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. role:: raw-html(raw)
6*9880d681SAndroid Build Coastguard Worker   :format: html
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker.. raw:: html
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker  <style>
11*9880d681SAndroid Build Coastguard Worker    .unknown { background-color: #C0C0C0; text-align: center; }
12*9880d681SAndroid Build Coastguard Worker    .unknown:before { content: "?" }
13*9880d681SAndroid Build Coastguard Worker    .no { background-color: #C11B17 }
14*9880d681SAndroid Build Coastguard Worker    .no:before { content: "N" }
15*9880d681SAndroid Build Coastguard Worker    .partial { background-color: #F88017 }
16*9880d681SAndroid Build Coastguard Worker    .yes { background-color: #0F0; }
17*9880d681SAndroid Build Coastguard Worker    .yes:before { content: "Y" }
18*9880d681SAndroid Build Coastguard Worker    .na { background-color: #6666FF; }
19*9880d681SAndroid Build Coastguard Worker    .na:before { content: "N/A" }
20*9880d681SAndroid Build Coastguard Worker  </style>
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker.. contents::
23*9880d681SAndroid Build Coastguard Worker   :local:
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker.. warning::
26*9880d681SAndroid Build Coastguard Worker  This is a work in progress.
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard WorkerIntroduction
29*9880d681SAndroid Build Coastguard Worker============
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard WorkerThe LLVM target-independent code generator is a framework that provides a suite
32*9880d681SAndroid Build Coastguard Workerof reusable components for translating the LLVM internal representation to the
33*9880d681SAndroid Build Coastguard Workermachine code for a specified target---either in assembly form (suitable for a
34*9880d681SAndroid Build Coastguard Workerstatic compiler) or in binary machine code format (usable for a JIT
35*9880d681SAndroid Build Coastguard Workercompiler). The LLVM target-independent code generator consists of six main
36*9880d681SAndroid Build Coastguard Workercomponents:
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker1. `Abstract target description`_ interfaces which capture important properties
39*9880d681SAndroid Build Coastguard Worker   about various aspects of the machine, independently of how they will be used.
40*9880d681SAndroid Build Coastguard Worker   These interfaces are defined in ``include/llvm/Target/``.
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker2. Classes used to represent the `code being generated`_ for a target.  These
43*9880d681SAndroid Build Coastguard Worker   classes are intended to be abstract enough to represent the machine code for
44*9880d681SAndroid Build Coastguard Worker   *any* target machine.  These classes are defined in
45*9880d681SAndroid Build Coastguard Worker   ``include/llvm/CodeGen/``. At this level, concepts like "constant pool
46*9880d681SAndroid Build Coastguard Worker   entries" and "jump tables" are explicitly exposed.
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker3. Classes and algorithms used to represent code at the object file level, the
49*9880d681SAndroid Build Coastguard Worker   `MC Layer`_.  These classes represent assembly level constructs like labels,
50*9880d681SAndroid Build Coastguard Worker   sections, and instructions.  At this level, concepts like "constant pool
51*9880d681SAndroid Build Coastguard Worker   entries" and "jump tables" don't exist.
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker4. `Target-independent algorithms`_ used to implement various phases of native
54*9880d681SAndroid Build Coastguard Worker   code generation (register allocation, scheduling, stack frame representation,
55*9880d681SAndroid Build Coastguard Worker   etc).  This code lives in ``lib/CodeGen/``.
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker5. `Implementations of the abstract target description interfaces`_ for
58*9880d681SAndroid Build Coastguard Worker   particular targets.  These machine descriptions make use of the components
59*9880d681SAndroid Build Coastguard Worker   provided by LLVM, and can optionally provide custom target-specific passes,
60*9880d681SAndroid Build Coastguard Worker   to build complete code generators for a specific target.  Target descriptions
61*9880d681SAndroid Build Coastguard Worker   live in ``lib/Target/``.
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker6. The target-independent JIT components.  The LLVM JIT is completely target
64*9880d681SAndroid Build Coastguard Worker   independent (it uses the ``TargetJITInfo`` structure to interface for
65*9880d681SAndroid Build Coastguard Worker   target-specific issues.  The code for the target-independent JIT lives in
66*9880d681SAndroid Build Coastguard Worker   ``lib/ExecutionEngine/JIT``.
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard WorkerDepending on which part of the code generator you are interested in working on,
69*9880d681SAndroid Build Coastguard Workerdifferent pieces of this will be useful to you.  In any case, you should be
70*9880d681SAndroid Build Coastguard Workerfamiliar with the `target description`_ and `machine code representation`_
71*9880d681SAndroid Build Coastguard Workerclasses.  If you want to add a backend for a new target, you will need to
72*9880d681SAndroid Build Coastguard Worker`implement the target description`_ classes for your new target and understand
73*9880d681SAndroid Build Coastguard Workerthe :doc:`LLVM code representation <LangRef>`.  If you are interested in
74*9880d681SAndroid Build Coastguard Workerimplementing a new `code generation algorithm`_, it should only depend on the
75*9880d681SAndroid Build Coastguard Workertarget-description and machine code representation classes, ensuring that it is
76*9880d681SAndroid Build Coastguard Workerportable.
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard WorkerRequired components in the code generator
79*9880d681SAndroid Build Coastguard Worker-----------------------------------------
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard WorkerThe two pieces of the LLVM code generator are the high-level interface to the
82*9880d681SAndroid Build Coastguard Workercode generator and the set of reusable components that can be used to build
83*9880d681SAndroid Build Coastguard Workertarget-specific backends.  The two most important interfaces (:raw-html:`<tt>`
84*9880d681SAndroid Build Coastguard Worker`TargetMachine`_ :raw-html:`</tt>` and :raw-html:`<tt>` `DataLayout`_
85*9880d681SAndroid Build Coastguard Worker:raw-html:`</tt>`) are the only ones that are required to be defined for a
86*9880d681SAndroid Build Coastguard Workerbackend to fit into the LLVM system, but the others must be defined if the
87*9880d681SAndroid Build Coastguard Workerreusable code generator components are going to be used.
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard WorkerThis design has two important implications.  The first is that LLVM can support
90*9880d681SAndroid Build Coastguard Workercompletely non-traditional code generation targets.  For example, the C backend
91*9880d681SAndroid Build Coastguard Workerdoes not require register allocation, instruction selection, or any of the other
92*9880d681SAndroid Build Coastguard Workerstandard components provided by the system.  As such, it only implements these
93*9880d681SAndroid Build Coastguard Workertwo interfaces, and does its own thing. Note that C backend was removed from the
94*9880d681SAndroid Build Coastguard Workertrunk since LLVM 3.1 release. Another example of a code generator like this is a
95*9880d681SAndroid Build Coastguard Worker(purely hypothetical) backend that converts LLVM to the GCC RTL form and uses
96*9880d681SAndroid Build Coastguard WorkerGCC to emit machine code for a target.
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard WorkerThis design also implies that it is possible to design and implement radically
99*9880d681SAndroid Build Coastguard Workerdifferent code generators in the LLVM system that do not make use of any of the
100*9880d681SAndroid Build Coastguard Workerbuilt-in components.  Doing so is not recommended at all, but could be required
101*9880d681SAndroid Build Coastguard Workerfor radically different targets that do not fit into the LLVM machine
102*9880d681SAndroid Build Coastguard Workerdescription model: FPGAs for example.
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker.. _high-level design of the code generator:
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard WorkerThe high-level design of the code generator
107*9880d681SAndroid Build Coastguard Worker-------------------------------------------
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard WorkerThe LLVM target-independent code generator is designed to support efficient and
110*9880d681SAndroid Build Coastguard Workerquality code generation for standard register-based microprocessors.  Code
111*9880d681SAndroid Build Coastguard Workergeneration in this model is divided into the following stages:
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker1. `Instruction Selection`_ --- This phase determines an efficient way to
114*9880d681SAndroid Build Coastguard Worker   express the input LLVM code in the target instruction set.  This stage
115*9880d681SAndroid Build Coastguard Worker   produces the initial code for the program in the target instruction set, then
116*9880d681SAndroid Build Coastguard Worker   makes use of virtual registers in SSA form and physical registers that
117*9880d681SAndroid Build Coastguard Worker   represent any required register assignments due to target constraints or
118*9880d681SAndroid Build Coastguard Worker   calling conventions.  This step turns the LLVM code into a DAG of target
119*9880d681SAndroid Build Coastguard Worker   instructions.
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker2. `Scheduling and Formation`_ --- This phase takes the DAG of target
122*9880d681SAndroid Build Coastguard Worker   instructions produced by the instruction selection phase, determines an
123*9880d681SAndroid Build Coastguard Worker   ordering of the instructions, then emits the instructions as :raw-html:`<tt>`
124*9880d681SAndroid Build Coastguard Worker   `MachineInstr`_\s :raw-html:`</tt>` with that ordering.  Note that we
125*9880d681SAndroid Build Coastguard Worker   describe this in the `instruction selection section`_ because it operates on
126*9880d681SAndroid Build Coastguard Worker   a `SelectionDAG`_.
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker3. `SSA-based Machine Code Optimizations`_ --- This optional stage consists of a
129*9880d681SAndroid Build Coastguard Worker   series of machine-code optimizations that operate on the SSA-form produced by
130*9880d681SAndroid Build Coastguard Worker   the instruction selector.  Optimizations like modulo-scheduling or peephole
131*9880d681SAndroid Build Coastguard Worker   optimization work here.
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker4. `Register Allocation`_ --- The target code is transformed from an infinite
134*9880d681SAndroid Build Coastguard Worker   virtual register file in SSA form to the concrete register file used by the
135*9880d681SAndroid Build Coastguard Worker   target.  This phase introduces spill code and eliminates all virtual register
136*9880d681SAndroid Build Coastguard Worker   references from the program.
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker5. `Prolog/Epilog Code Insertion`_ --- Once the machine code has been generated
139*9880d681SAndroid Build Coastguard Worker   for the function and the amount of stack space required is known (used for
140*9880d681SAndroid Build Coastguard Worker   LLVM alloca's and spill slots), the prolog and epilog code for the function
141*9880d681SAndroid Build Coastguard Worker   can be inserted and "abstract stack location references" can be eliminated.
142*9880d681SAndroid Build Coastguard Worker   This stage is responsible for implementing optimizations like frame-pointer
143*9880d681SAndroid Build Coastguard Worker   elimination and stack packing.
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker6. `Late Machine Code Optimizations`_ --- Optimizations that operate on "final"
146*9880d681SAndroid Build Coastguard Worker   machine code can go here, such as spill code scheduling and peephole
147*9880d681SAndroid Build Coastguard Worker   optimizations.
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker7. `Code Emission`_ --- The final stage actually puts out the code for the
150*9880d681SAndroid Build Coastguard Worker   current function, either in the target assembler format or in machine
151*9880d681SAndroid Build Coastguard Worker   code.
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard WorkerThe code generator is based on the assumption that the instruction selector will
154*9880d681SAndroid Build Coastguard Workeruse an optimal pattern matching selector to create high-quality sequences of
155*9880d681SAndroid Build Coastguard Workernative instructions.  Alternative code generator designs based on pattern
156*9880d681SAndroid Build Coastguard Workerexpansion and aggressive iterative peephole optimization are much slower.  This
157*9880d681SAndroid Build Coastguard Workerdesign permits efficient compilation (important for JIT environments) and
158*9880d681SAndroid Build Coastguard Workeraggressive optimization (used when generating code offline) by allowing
159*9880d681SAndroid Build Coastguard Workercomponents of varying levels of sophistication to be used for any step of
160*9880d681SAndroid Build Coastguard Workercompilation.
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard WorkerIn addition to these stages, target implementations can insert arbitrary
163*9880d681SAndroid Build Coastguard Workertarget-specific passes into the flow.  For example, the X86 target uses a
164*9880d681SAndroid Build Coastguard Workerspecial pass to handle the 80x87 floating point stack architecture.  Other
165*9880d681SAndroid Build Coastguard Workertargets with unusual requirements can be supported with custom passes as needed.
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard WorkerUsing TableGen for target description
168*9880d681SAndroid Build Coastguard Worker-------------------------------------
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard WorkerThe target description classes require a detailed description of the target
171*9880d681SAndroid Build Coastguard Workerarchitecture.  These target descriptions often have a large amount of common
172*9880d681SAndroid Build Coastguard Workerinformation (e.g., an ``add`` instruction is almost identical to a ``sub``
173*9880d681SAndroid Build Coastguard Workerinstruction).  In order to allow the maximum amount of commonality to be
174*9880d681SAndroid Build Coastguard Workerfactored out, the LLVM code generator uses the
175*9880d681SAndroid Build Coastguard Worker:doc:`TableGen/index` tool to describe big chunks of the
176*9880d681SAndroid Build Coastguard Workertarget machine, which allows the use of domain-specific and target-specific
177*9880d681SAndroid Build Coastguard Workerabstractions to reduce the amount of repetition.
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard WorkerAs LLVM continues to be developed and refined, we plan to move more and more of
180*9880d681SAndroid Build Coastguard Workerthe target description to the ``.td`` form.  Doing so gives us a number of
181*9880d681SAndroid Build Coastguard Workeradvantages.  The most important is that it makes it easier to port LLVM because
182*9880d681SAndroid Build Coastguard Workerit reduces the amount of C++ code that has to be written, and the surface area
183*9880d681SAndroid Build Coastguard Workerof the code generator that needs to be understood before someone can get
184*9880d681SAndroid Build Coastguard Workersomething working.  Second, it makes it easier to change things. In particular,
185*9880d681SAndroid Build Coastguard Workerif tables and other things are all emitted by ``tblgen``, we only need a change
186*9880d681SAndroid Build Coastguard Workerin one place (``tblgen``) to update all of the targets to a new interface.
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker.. _Abstract target description:
189*9880d681SAndroid Build Coastguard Worker.. _target description:
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard WorkerTarget description classes
192*9880d681SAndroid Build Coastguard Worker==========================
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard WorkerThe LLVM target description classes (located in the ``include/llvm/Target``
195*9880d681SAndroid Build Coastguard Workerdirectory) provide an abstract description of the target machine independent of
196*9880d681SAndroid Build Coastguard Workerany particular client.  These classes are designed to capture the *abstract*
197*9880d681SAndroid Build Coastguard Workerproperties of the target (such as the instructions and registers it has), and do
198*9880d681SAndroid Build Coastguard Workernot incorporate any particular pieces of code generation algorithms.
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard WorkerAll of the target description classes (except the :raw-html:`<tt>` `DataLayout`_
201*9880d681SAndroid Build Coastguard Worker:raw-html:`</tt>` class) are designed to be subclassed by the concrete target
202*9880d681SAndroid Build Coastguard Workerimplementation, and have virtual methods implemented.  To get to these
203*9880d681SAndroid Build Coastguard Workerimplementations, the :raw-html:`<tt>` `TargetMachine`_ :raw-html:`</tt>` class
204*9880d681SAndroid Build Coastguard Workerprovides accessors that should be implemented by the target.
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker.. _TargetMachine:
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard WorkerThe ``TargetMachine`` class
209*9880d681SAndroid Build Coastguard Worker---------------------------
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard WorkerThe ``TargetMachine`` class provides virtual methods that are used to access the
212*9880d681SAndroid Build Coastguard Workertarget-specific implementations of the various target description classes via
213*9880d681SAndroid Build Coastguard Workerthe ``get*Info`` methods (``getInstrInfo``, ``getRegisterInfo``,
214*9880d681SAndroid Build Coastguard Worker``getFrameInfo``, etc.).  This class is designed to be specialized by a concrete
215*9880d681SAndroid Build Coastguard Workertarget implementation (e.g., ``X86TargetMachine``) which implements the various
216*9880d681SAndroid Build Coastguard Workervirtual methods.  The only required target description class is the
217*9880d681SAndroid Build Coastguard Worker:raw-html:`<tt>` `DataLayout`_ :raw-html:`</tt>` class, but if the code
218*9880d681SAndroid Build Coastguard Workergenerator components are to be used, the other interfaces should be implemented
219*9880d681SAndroid Build Coastguard Workeras well.
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker.. _DataLayout:
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard WorkerThe ``DataLayout`` class
224*9880d681SAndroid Build Coastguard Worker------------------------
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard WorkerThe ``DataLayout`` class is the only required target description class, and it
227*9880d681SAndroid Build Coastguard Workeris the only class that is not extensible (you cannot derive a new class from
228*9880d681SAndroid Build Coastguard Workerit).  ``DataLayout`` specifies information about how the target lays out memory
229*9880d681SAndroid Build Coastguard Workerfor structures, the alignment requirements for various data types, the size of
230*9880d681SAndroid Build Coastguard Workerpointers in the target, and whether the target is little-endian or
231*9880d681SAndroid Build Coastguard Workerbig-endian.
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker.. _TargetLowering:
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard WorkerThe ``TargetLowering`` class
236*9880d681SAndroid Build Coastguard Worker----------------------------
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard WorkerThe ``TargetLowering`` class is used by SelectionDAG based instruction selectors
239*9880d681SAndroid Build Coastguard Workerprimarily to describe how LLVM code should be lowered to SelectionDAG
240*9880d681SAndroid Build Coastguard Workeroperations.  Among other things, this class indicates:
241*9880d681SAndroid Build Coastguard Worker
242*9880d681SAndroid Build Coastguard Worker* an initial register class to use for various ``ValueType``\s,
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker* which operations are natively supported by the target machine,
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker* the return type of ``setcc`` operations,
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker* the type to use for shift amounts, and
249*9880d681SAndroid Build Coastguard Worker
250*9880d681SAndroid Build Coastguard Worker* various high-level characteristics, like whether it is profitable to turn
251*9880d681SAndroid Build Coastguard Worker  division by a constant into a multiplication sequence.
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker.. _TargetRegisterInfo:
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard WorkerThe ``TargetRegisterInfo`` class
256*9880d681SAndroid Build Coastguard Worker--------------------------------
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard WorkerThe ``TargetRegisterInfo`` class is used to describe the register file of the
259*9880d681SAndroid Build Coastguard Workertarget and any interactions between the registers.
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard WorkerRegisters are represented in the code generator by unsigned integers.  Physical
262*9880d681SAndroid Build Coastguard Workerregisters (those that actually exist in the target description) are unique
263*9880d681SAndroid Build Coastguard Workersmall numbers, and virtual registers are generally large.  Note that
264*9880d681SAndroid Build Coastguard Workerregister ``#0`` is reserved as a flag value.
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard WorkerEach register in the processor description has an associated
267*9880d681SAndroid Build Coastguard Worker``TargetRegisterDesc`` entry, which provides a textual name for the register
268*9880d681SAndroid Build Coastguard Worker(used for assembly output and debugging dumps) and a set of aliases (used to
269*9880d681SAndroid Build Coastguard Workerindicate whether one register overlaps with another).
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard WorkerIn addition to the per-register description, the ``TargetRegisterInfo`` class
272*9880d681SAndroid Build Coastguard Workerexposes a set of processor specific register classes (instances of the
273*9880d681SAndroid Build Coastguard Worker``TargetRegisterClass`` class).  Each register class contains sets of registers
274*9880d681SAndroid Build Coastguard Workerthat have the same properties (for example, they are all 32-bit integer
275*9880d681SAndroid Build Coastguard Workerregisters).  Each SSA virtual register created by the instruction selector has
276*9880d681SAndroid Build Coastguard Workeran associated register class.  When the register allocator runs, it replaces
277*9880d681SAndroid Build Coastguard Workervirtual registers with a physical register in the set.
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard WorkerThe target-specific implementations of these classes is auto-generated from a
280*9880d681SAndroid Build Coastguard Worker:doc:`TableGen/index` description of the register file.
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker.. _TargetInstrInfo:
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard WorkerThe ``TargetInstrInfo`` class
285*9880d681SAndroid Build Coastguard Worker-----------------------------
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard WorkerThe ``TargetInstrInfo`` class is used to describe the machine instructions
288*9880d681SAndroid Build Coastguard Workersupported by the target.  Descriptions define things like the mnemonic for
289*9880d681SAndroid Build Coastguard Workerthe opcode, the number of operands, the list of implicit register uses and defs,
290*9880d681SAndroid Build Coastguard Workerwhether the instruction has certain target-independent properties (accesses
291*9880d681SAndroid Build Coastguard Workermemory, is commutable, etc), and holds any target-specific flags.
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard WorkerThe ``TargetFrameLowering`` class
294*9880d681SAndroid Build Coastguard Worker---------------------------------
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard WorkerThe ``TargetFrameLowering`` class is used to provide information about the stack
297*9880d681SAndroid Build Coastguard Workerframe layout of the target. It holds the direction of stack growth, the known
298*9880d681SAndroid Build Coastguard Workerstack alignment on entry to each function, and the offset to the local area.
299*9880d681SAndroid Build Coastguard WorkerThe offset to the local area is the offset from the stack pointer on function
300*9880d681SAndroid Build Coastguard Workerentry to the first location where function data (local variables, spill
301*9880d681SAndroid Build Coastguard Workerlocations) can be stored.
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard WorkerThe ``TargetSubtarget`` class
304*9880d681SAndroid Build Coastguard Worker-----------------------------
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard WorkerThe ``TargetSubtarget`` class is used to provide information about the specific
307*9880d681SAndroid Build Coastguard Workerchip set being targeted.  A sub-target informs code generation of which
308*9880d681SAndroid Build Coastguard Workerinstructions are supported, instruction latencies and instruction execution
309*9880d681SAndroid Build Coastguard Workeritinerary; i.e., which processing units are used, in what order, and for how
310*9880d681SAndroid Build Coastguard Workerlong.
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard WorkerThe ``TargetJITInfo`` class
313*9880d681SAndroid Build Coastguard Worker---------------------------
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard WorkerThe ``TargetJITInfo`` class exposes an abstract interface used by the
316*9880d681SAndroid Build Coastguard WorkerJust-In-Time code generator to perform target-specific activities, such as
317*9880d681SAndroid Build Coastguard Workeremitting stubs.  If a ``TargetMachine`` supports JIT code generation, it should
318*9880d681SAndroid Build Coastguard Workerprovide one of these objects through the ``getJITInfo`` method.
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker.. _code being generated:
321*9880d681SAndroid Build Coastguard Worker.. _machine code representation:
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard WorkerMachine code description classes
324*9880d681SAndroid Build Coastguard Worker================================
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard WorkerAt the high-level, LLVM code is translated to a machine specific representation
327*9880d681SAndroid Build Coastguard Workerformed out of :raw-html:`<tt>` `MachineFunction`_ :raw-html:`</tt>`,
328*9880d681SAndroid Build Coastguard Worker:raw-html:`<tt>` `MachineBasicBlock`_ :raw-html:`</tt>`, and :raw-html:`<tt>`
329*9880d681SAndroid Build Coastguard Worker`MachineInstr`_ :raw-html:`</tt>` instances (defined in
330*9880d681SAndroid Build Coastguard Worker``include/llvm/CodeGen``).  This representation is completely target agnostic,
331*9880d681SAndroid Build Coastguard Workerrepresenting instructions in their most abstract form: an opcode and a series of
332*9880d681SAndroid Build Coastguard Workeroperands.  This representation is designed to support both an SSA representation
333*9880d681SAndroid Build Coastguard Workerfor machine code, as well as a register allocated, non-SSA form.
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker.. _MachineInstr:
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard WorkerThe ``MachineInstr`` class
338*9880d681SAndroid Build Coastguard Worker--------------------------
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard WorkerTarget machine instructions are represented as instances of the ``MachineInstr``
341*9880d681SAndroid Build Coastguard Workerclass.  This class is an extremely abstract way of representing machine
342*9880d681SAndroid Build Coastguard Workerinstructions.  In particular, it only keeps track of an opcode number and a set
343*9880d681SAndroid Build Coastguard Workerof operands.
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard WorkerThe opcode number is a simple unsigned integer that only has meaning to a
346*9880d681SAndroid Build Coastguard Workerspecific backend.  All of the instructions for a target should be defined in the
347*9880d681SAndroid Build Coastguard Worker``*InstrInfo.td`` file for the target. The opcode enum values are auto-generated
348*9880d681SAndroid Build Coastguard Workerfrom this description.  The ``MachineInstr`` class does not have any information
349*9880d681SAndroid Build Coastguard Workerabout how to interpret the instruction (i.e., what the semantics of the
350*9880d681SAndroid Build Coastguard Workerinstruction are); for that you must refer to the :raw-html:`<tt>`
351*9880d681SAndroid Build Coastguard Worker`TargetInstrInfo`_ :raw-html:`</tt>` class.
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard WorkerThe operands of a machine instruction can be of several different types: a
354*9880d681SAndroid Build Coastguard Workerregister reference, a constant integer, a basic block reference, etc.  In
355*9880d681SAndroid Build Coastguard Workeraddition, a machine operand should be marked as a def or a use of the value
356*9880d681SAndroid Build Coastguard Worker(though only registers are allowed to be defs).
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard WorkerBy convention, the LLVM code generator orders instruction operands so that all
359*9880d681SAndroid Build Coastguard Workerregister definitions come before the register uses, even on architectures that
360*9880d681SAndroid Build Coastguard Workerare normally printed in other orders.  For example, the SPARC add instruction:
361*9880d681SAndroid Build Coastguard Worker"``add %i1, %i2, %i3``" adds the "%i1", and "%i2" registers and stores the
362*9880d681SAndroid Build Coastguard Workerresult into the "%i3" register.  In the LLVM code generator, the operands should
363*9880d681SAndroid Build Coastguard Workerbe stored as "``%i3, %i1, %i2``": with the destination first.
364*9880d681SAndroid Build Coastguard Worker
365*9880d681SAndroid Build Coastguard WorkerKeeping destination (definition) operands at the beginning of the operand list
366*9880d681SAndroid Build Coastguard Workerhas several advantages.  In particular, the debugging printer will print the
367*9880d681SAndroid Build Coastguard Workerinstruction like this:
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker  %r3 = add %i1, %i2
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard WorkerAlso if the first operand is a def, it is easier to `create instructions`_ whose
374*9880d681SAndroid Build Coastguard Workeronly def is the first operand.
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker.. _create instructions:
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard WorkerUsing the ``MachineInstrBuilder.h`` functions
379*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
380*9880d681SAndroid Build Coastguard Worker
381*9880d681SAndroid Build Coastguard WorkerMachine instructions are created by using the ``BuildMI`` functions, located in
382*9880d681SAndroid Build Coastguard Workerthe ``include/llvm/CodeGen/MachineInstrBuilder.h`` file.  The ``BuildMI``
383*9880d681SAndroid Build Coastguard Workerfunctions make it easy to build arbitrary machine instructions.  Usage of the
384*9880d681SAndroid Build Coastguard Worker``BuildMI`` functions look like this:
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker  // Create a 'DestReg = mov 42' (rendered in X86 assembly as 'mov DestReg, 42')
389*9880d681SAndroid Build Coastguard Worker  // instruction and insert it at the end of the given MachineBasicBlock.
390*9880d681SAndroid Build Coastguard Worker  const TargetInstrInfo &TII = ...
391*9880d681SAndroid Build Coastguard Worker  MachineBasicBlock &MBB = ...
392*9880d681SAndroid Build Coastguard Worker  DebugLoc DL;
393*9880d681SAndroid Build Coastguard Worker  MachineInstr *MI = BuildMI(MBB, DL, TII.get(X86::MOV32ri), DestReg).addImm(42);
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker  // Create the same instr, but insert it before a specified iterator point.
396*9880d681SAndroid Build Coastguard Worker  MachineBasicBlock::iterator MBBI = ...
397*9880d681SAndroid Build Coastguard Worker  BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), DestReg).addImm(42);
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker  // Create a 'cmp Reg, 0' instruction, no destination reg.
400*9880d681SAndroid Build Coastguard Worker  MI = BuildMI(MBB, DL, TII.get(X86::CMP32ri8)).addReg(Reg).addImm(42);
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard Worker  // Create an 'sahf' instruction which takes no operands and stores nothing.
403*9880d681SAndroid Build Coastguard Worker  MI = BuildMI(MBB, DL, TII.get(X86::SAHF));
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker  // Create a self looping branch instruction.
406*9880d681SAndroid Build Coastguard Worker  BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(&MBB);
407*9880d681SAndroid Build Coastguard Worker
408*9880d681SAndroid Build Coastguard WorkerIf you need to add a definition operand (other than the optional destination
409*9880d681SAndroid Build Coastguard Workerregister), you must explicitly mark it as such:
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker  MI.addReg(Reg, RegState::Define);
414*9880d681SAndroid Build Coastguard Worker
415*9880d681SAndroid Build Coastguard WorkerFixed (preassigned) registers
416*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard WorkerOne important issue that the code generator needs to be aware of is the presence
419*9880d681SAndroid Build Coastguard Workerof fixed registers.  In particular, there are often places in the instruction
420*9880d681SAndroid Build Coastguard Workerstream where the register allocator *must* arrange for a particular value to be
421*9880d681SAndroid Build Coastguard Workerin a particular register.  This can occur due to limitations of the instruction
422*9880d681SAndroid Build Coastguard Workerset (e.g., the X86 can only do a 32-bit divide with the ``EAX``/``EDX``
423*9880d681SAndroid Build Coastguard Workerregisters), or external factors like calling conventions.  In any case, the
424*9880d681SAndroid Build Coastguard Workerinstruction selector should emit code that copies a virtual register into or out
425*9880d681SAndroid Build Coastguard Workerof a physical register when needed.
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard WorkerFor example, consider this simple LLVM example:
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
430*9880d681SAndroid Build Coastguard Worker
431*9880d681SAndroid Build Coastguard Worker  define i32 @test(i32 %X, i32 %Y) {
432*9880d681SAndroid Build Coastguard Worker    %Z = sdiv i32 %X, %Y
433*9880d681SAndroid Build Coastguard Worker    ret i32 %Z
434*9880d681SAndroid Build Coastguard Worker  }
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard WorkerThe X86 instruction selector might produce this machine code for the ``div`` and
437*9880d681SAndroid Build Coastguard Worker``ret``:
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
440*9880d681SAndroid Build Coastguard Worker
441*9880d681SAndroid Build Coastguard Worker  ;; Start of div
442*9880d681SAndroid Build Coastguard Worker  %EAX = mov %reg1024           ;; Copy X (in reg1024) into EAX
443*9880d681SAndroid Build Coastguard Worker  %reg1027 = sar %reg1024, 31
444*9880d681SAndroid Build Coastguard Worker  %EDX = mov %reg1027           ;; Sign extend X into EDX
445*9880d681SAndroid Build Coastguard Worker  idiv %reg1025                 ;; Divide by Y (in reg1025)
446*9880d681SAndroid Build Coastguard Worker  %reg1026 = mov %EAX           ;; Read the result (Z) out of EAX
447*9880d681SAndroid Build Coastguard Worker
448*9880d681SAndroid Build Coastguard Worker  ;; Start of ret
449*9880d681SAndroid Build Coastguard Worker  %EAX = mov %reg1026           ;; 32-bit return value goes in EAX
450*9880d681SAndroid Build Coastguard Worker  ret
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard WorkerBy the end of code generation, the register allocator would coalesce the
453*9880d681SAndroid Build Coastguard Workerregisters and delete the resultant identity moves producing the following
454*9880d681SAndroid Build Coastguard Workercode:
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Worker  ;; X is in EAX, Y is in ECX
459*9880d681SAndroid Build Coastguard Worker  mov %EAX, %EDX
460*9880d681SAndroid Build Coastguard Worker  sar %EDX, 31
461*9880d681SAndroid Build Coastguard Worker  idiv %ECX
462*9880d681SAndroid Build Coastguard Worker  ret
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard WorkerThis approach is extremely general (if it can handle the X86 architecture, it
465*9880d681SAndroid Build Coastguard Workercan handle anything!) and allows all of the target specific knowledge about the
466*9880d681SAndroid Build Coastguard Workerinstruction stream to be isolated in the instruction selector.  Note that
467*9880d681SAndroid Build Coastguard Workerphysical registers should have a short lifetime for good code generation, and
468*9880d681SAndroid Build Coastguard Workerall physical registers are assumed dead on entry to and exit from basic blocks
469*9880d681SAndroid Build Coastguard Worker(before register allocation).  Thus, if you need a value to be live across basic
470*9880d681SAndroid Build Coastguard Workerblock boundaries, it *must* live in a virtual register.
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard WorkerCall-clobbered registers
473*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard WorkerSome machine instructions, like calls, clobber a large number of physical
476*9880d681SAndroid Build Coastguard Workerregisters.  Rather than adding ``<def,dead>`` operands for all of them, it is
477*9880d681SAndroid Build Coastguard Workerpossible to use an ``MO_RegisterMask`` operand instead.  The register mask
478*9880d681SAndroid Build Coastguard Workeroperand holds a bit mask of preserved registers, and everything else is
479*9880d681SAndroid Build Coastguard Workerconsidered to be clobbered by the instruction.
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard WorkerMachine code in SSA form
482*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker``MachineInstr``'s are initially selected in SSA-form, and are maintained in
485*9880d681SAndroid Build Coastguard WorkerSSA-form until register allocation happens.  For the most part, this is
486*9880d681SAndroid Build Coastguard Workertrivially simple since LLVM is already in SSA form; LLVM PHI nodes become
487*9880d681SAndroid Build Coastguard Workermachine code PHI nodes, and virtual registers are only allowed to have a single
488*9880d681SAndroid Build Coastguard Workerdefinition.
489*9880d681SAndroid Build Coastguard Worker
490*9880d681SAndroid Build Coastguard WorkerAfter register allocation, machine code is no longer in SSA-form because there
491*9880d681SAndroid Build Coastguard Workerare no virtual registers left in the code.
492*9880d681SAndroid Build Coastguard Worker
493*9880d681SAndroid Build Coastguard Worker.. _MachineBasicBlock:
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard WorkerThe ``MachineBasicBlock`` class
496*9880d681SAndroid Build Coastguard Worker-------------------------------
497*9880d681SAndroid Build Coastguard Worker
498*9880d681SAndroid Build Coastguard WorkerThe ``MachineBasicBlock`` class contains a list of machine instructions
499*9880d681SAndroid Build Coastguard Worker(:raw-html:`<tt>` `MachineInstr`_ :raw-html:`</tt>` instances).  It roughly
500*9880d681SAndroid Build Coastguard Workercorresponds to the LLVM code input to the instruction selector, but there can be
501*9880d681SAndroid Build Coastguard Workera one-to-many mapping (i.e. one LLVM basic block can map to multiple machine
502*9880d681SAndroid Build Coastguard Workerbasic blocks). The ``MachineBasicBlock`` class has a "``getBasicBlock``" method,
503*9880d681SAndroid Build Coastguard Workerwhich returns the LLVM basic block that it comes from.
504*9880d681SAndroid Build Coastguard Worker
505*9880d681SAndroid Build Coastguard Worker.. _MachineFunction:
506*9880d681SAndroid Build Coastguard Worker
507*9880d681SAndroid Build Coastguard WorkerThe ``MachineFunction`` class
508*9880d681SAndroid Build Coastguard Worker-----------------------------
509*9880d681SAndroid Build Coastguard Worker
510*9880d681SAndroid Build Coastguard WorkerThe ``MachineFunction`` class contains a list of machine basic blocks
511*9880d681SAndroid Build Coastguard Worker(:raw-html:`<tt>` `MachineBasicBlock`_ :raw-html:`</tt>` instances).  It
512*9880d681SAndroid Build Coastguard Workercorresponds one-to-one with the LLVM function input to the instruction selector.
513*9880d681SAndroid Build Coastguard WorkerIn addition to a list of basic blocks, the ``MachineFunction`` contains a a
514*9880d681SAndroid Build Coastguard Worker``MachineConstantPool``, a ``MachineFrameInfo``, a ``MachineFunctionInfo``, and
515*9880d681SAndroid Build Coastguard Workera ``MachineRegisterInfo``.  See ``include/llvm/CodeGen/MachineFunction.h`` for
516*9880d681SAndroid Build Coastguard Workermore information.
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker``MachineInstr Bundles``
519*9880d681SAndroid Build Coastguard Worker------------------------
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard WorkerLLVM code generator can model sequences of instructions as MachineInstr
522*9880d681SAndroid Build Coastguard Workerbundles. A MI bundle can model a VLIW group / pack which contains an arbitrary
523*9880d681SAndroid Build Coastguard Workernumber of parallel instructions. It can also be used to model a sequential list
524*9880d681SAndroid Build Coastguard Workerof instructions (potentially with data dependencies) that cannot be legally
525*9880d681SAndroid Build Coastguard Workerseparated (e.g. ARM Thumb2 IT blocks).
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard WorkerConceptually a MI bundle is a MI with a number of other MIs nested within:
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker::
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker  --------------
532*9880d681SAndroid Build Coastguard Worker  |   Bundle   | ---------
533*9880d681SAndroid Build Coastguard Worker  --------------          \
534*9880d681SAndroid Build Coastguard Worker         |           ----------------
535*9880d681SAndroid Build Coastguard Worker         |           |      MI      |
536*9880d681SAndroid Build Coastguard Worker         |           ----------------
537*9880d681SAndroid Build Coastguard Worker         |                   |
538*9880d681SAndroid Build Coastguard Worker         |           ----------------
539*9880d681SAndroid Build Coastguard Worker         |           |      MI      |
540*9880d681SAndroid Build Coastguard Worker         |           ----------------
541*9880d681SAndroid Build Coastguard Worker         |                   |
542*9880d681SAndroid Build Coastguard Worker         |           ----------------
543*9880d681SAndroid Build Coastguard Worker         |           |      MI      |
544*9880d681SAndroid Build Coastguard Worker         |           ----------------
545*9880d681SAndroid Build Coastguard Worker         |
546*9880d681SAndroid Build Coastguard Worker  --------------
547*9880d681SAndroid Build Coastguard Worker  |   Bundle   | --------
548*9880d681SAndroid Build Coastguard Worker  --------------         \
549*9880d681SAndroid Build Coastguard Worker         |           ----------------
550*9880d681SAndroid Build Coastguard Worker         |           |      MI      |
551*9880d681SAndroid Build Coastguard Worker         |           ----------------
552*9880d681SAndroid Build Coastguard Worker         |                   |
553*9880d681SAndroid Build Coastguard Worker         |           ----------------
554*9880d681SAndroid Build Coastguard Worker         |           |      MI      |
555*9880d681SAndroid Build Coastguard Worker         |           ----------------
556*9880d681SAndroid Build Coastguard Worker         |                   |
557*9880d681SAndroid Build Coastguard Worker         |                  ...
558*9880d681SAndroid Build Coastguard Worker         |
559*9880d681SAndroid Build Coastguard Worker  --------------
560*9880d681SAndroid Build Coastguard Worker  |   Bundle   | --------
561*9880d681SAndroid Build Coastguard Worker  --------------         \
562*9880d681SAndroid Build Coastguard Worker         |
563*9880d681SAndroid Build Coastguard Worker        ...
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard WorkerMI bundle support does not change the physical representations of
566*9880d681SAndroid Build Coastguard WorkerMachineBasicBlock and MachineInstr. All the MIs (including top level and nested
567*9880d681SAndroid Build Coastguard Workerones) are stored as sequential list of MIs. The "bundled" MIs are marked with
568*9880d681SAndroid Build Coastguard Workerthe 'InsideBundle' flag. A top level MI with the special BUNDLE opcode is used
569*9880d681SAndroid Build Coastguard Workerto represent the start of a bundle. It's legal to mix BUNDLE MIs with indiviual
570*9880d681SAndroid Build Coastguard WorkerMIs that are not inside bundles nor represent bundles.
571*9880d681SAndroid Build Coastguard Worker
572*9880d681SAndroid Build Coastguard WorkerMachineInstr passes should operate on a MI bundle as a single unit. Member
573*9880d681SAndroid Build Coastguard Workermethods have been taught to correctly handle bundles and MIs inside bundles.
574*9880d681SAndroid Build Coastguard WorkerThe MachineBasicBlock iterator has been modified to skip over bundled MIs to
575*9880d681SAndroid Build Coastguard Workerenforce the bundle-as-a-single-unit concept. An alternative iterator
576*9880d681SAndroid Build Coastguard Workerinstr_iterator has been added to MachineBasicBlock to allow passes to iterate
577*9880d681SAndroid Build Coastguard Workerover all of the MIs in a MachineBasicBlock, including those which are nested
578*9880d681SAndroid Build Coastguard Workerinside bundles. The top level BUNDLE instruction must have the correct set of
579*9880d681SAndroid Build Coastguard Workerregister MachineOperand's that represent the cumulative inputs and outputs of
580*9880d681SAndroid Build Coastguard Workerthe bundled MIs.
581*9880d681SAndroid Build Coastguard Worker
582*9880d681SAndroid Build Coastguard WorkerPacking / bundling of MachineInstr's should be done as part of the register
583*9880d681SAndroid Build Coastguard Workerallocation super-pass. More specifically, the pass which determines what MIs
584*9880d681SAndroid Build Coastguard Workershould be bundled together must be done after code generator exits SSA form
585*9880d681SAndroid Build Coastguard Worker(i.e. after two-address pass, PHI elimination, and copy coalescing).  Bundles
586*9880d681SAndroid Build Coastguard Workershould only be finalized (i.e. adding BUNDLE MIs and input and output register
587*9880d681SAndroid Build Coastguard WorkerMachineOperands) after virtual registers have been rewritten into physical
588*9880d681SAndroid Build Coastguard Workerregisters. This requirement eliminates the need to add virtual register operands
589*9880d681SAndroid Build Coastguard Workerto BUNDLE instructions which would effectively double the virtual register def
590*9880d681SAndroid Build Coastguard Workerand use lists.
591*9880d681SAndroid Build Coastguard Worker
592*9880d681SAndroid Build Coastguard Worker.. _MC Layer:
593*9880d681SAndroid Build Coastguard Worker
594*9880d681SAndroid Build Coastguard WorkerThe "MC" Layer
595*9880d681SAndroid Build Coastguard Worker==============
596*9880d681SAndroid Build Coastguard Worker
597*9880d681SAndroid Build Coastguard WorkerThe MC Layer is used to represent and process code at the raw machine code
598*9880d681SAndroid Build Coastguard Workerlevel, devoid of "high level" information like "constant pools", "jump tables",
599*9880d681SAndroid Build Coastguard Worker"global variables" or anything like that.  At this level, LLVM handles things
600*9880d681SAndroid Build Coastguard Workerlike label names, machine instructions, and sections in the object file.  The
601*9880d681SAndroid Build Coastguard Workercode in this layer is used for a number of important purposes: the tail end of
602*9880d681SAndroid Build Coastguard Workerthe code generator uses it to write a .s or .o file, and it is also used by the
603*9880d681SAndroid Build Coastguard Workerllvm-mc tool to implement standalone machine code assemblers and disassemblers.
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard WorkerThis section describes some of the important classes.  There are also a number
606*9880d681SAndroid Build Coastguard Workerof important subsystems that interact at this layer, they are described later in
607*9880d681SAndroid Build Coastguard Workerthis manual.
608*9880d681SAndroid Build Coastguard Worker
609*9880d681SAndroid Build Coastguard Worker.. _MCStreamer:
610*9880d681SAndroid Build Coastguard Worker
611*9880d681SAndroid Build Coastguard WorkerThe ``MCStreamer`` API
612*9880d681SAndroid Build Coastguard Worker----------------------
613*9880d681SAndroid Build Coastguard Worker
614*9880d681SAndroid Build Coastguard WorkerMCStreamer is best thought of as an assembler API.  It is an abstract API which
615*9880d681SAndroid Build Coastguard Workeris *implemented* in different ways (e.g. to output a .s file, output an ELF .o
616*9880d681SAndroid Build Coastguard Workerfile, etc) but whose API correspond directly to what you see in a .s file.
617*9880d681SAndroid Build Coastguard WorkerMCStreamer has one method per directive, such as EmitLabel, EmitSymbolAttribute,
618*9880d681SAndroid Build Coastguard WorkerSwitchSection, EmitValue (for .byte, .word), etc, which directly correspond to
619*9880d681SAndroid Build Coastguard Workerassembly level directives.  It also has an EmitInstruction method, which is used
620*9880d681SAndroid Build Coastguard Workerto output an MCInst to the streamer.
621*9880d681SAndroid Build Coastguard Worker
622*9880d681SAndroid Build Coastguard WorkerThis API is most important for two clients: the llvm-mc stand-alone assembler is
623*9880d681SAndroid Build Coastguard Workereffectively a parser that parses a line, then invokes a method on MCStreamer. In
624*9880d681SAndroid Build Coastguard Workerthe code generator, the `Code Emission`_ phase of the code generator lowers
625*9880d681SAndroid Build Coastguard Workerhigher level LLVM IR and Machine* constructs down to the MC layer, emitting
626*9880d681SAndroid Build Coastguard Workerdirectives through MCStreamer.
627*9880d681SAndroid Build Coastguard Worker
628*9880d681SAndroid Build Coastguard WorkerOn the implementation side of MCStreamer, there are two major implementations:
629*9880d681SAndroid Build Coastguard Workerone for writing out a .s file (MCAsmStreamer), and one for writing out a .o
630*9880d681SAndroid Build Coastguard Workerfile (MCObjectStreamer).  MCAsmStreamer is a straightforward implementation
631*9880d681SAndroid Build Coastguard Workerthat prints out a directive for each method (e.g. ``EmitValue -> .byte``), but
632*9880d681SAndroid Build Coastguard WorkerMCObjectStreamer implements a full assembler.
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard WorkerFor target specific directives, the MCStreamer has a MCTargetStreamer instance.
635*9880d681SAndroid Build Coastguard WorkerEach target that needs it defines a class that inherits from it and is a lot
636*9880d681SAndroid Build Coastguard Workerlike MCStreamer itself: It has one method per directive and two classes that
637*9880d681SAndroid Build Coastguard Workerinherit from it, a target object streamer and a target asm streamer. The target
638*9880d681SAndroid Build Coastguard Workerasm streamer just prints it (``emitFnStart -> .fnstart``), and the object
639*9880d681SAndroid Build Coastguard Workerstreamer implement the assembler logic for it.
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard WorkerTo make llvm use these classes, the target initialization must call
642*9880d681SAndroid Build Coastguard WorkerTargetRegistry::RegisterAsmStreamer and TargetRegistry::RegisterMCObjectStreamer
643*9880d681SAndroid Build Coastguard Workerpassing callbacks that allocate the corresponding target streamer and pass it
644*9880d681SAndroid Build Coastguard Workerto createAsmStreamer or to the appropriate object streamer constructor.
645*9880d681SAndroid Build Coastguard Worker
646*9880d681SAndroid Build Coastguard WorkerThe ``MCContext`` class
647*9880d681SAndroid Build Coastguard Worker-----------------------
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard WorkerThe MCContext class is the owner of a variety of uniqued data structures at the
650*9880d681SAndroid Build Coastguard WorkerMC layer, including symbols, sections, etc.  As such, this is the class that you
651*9880d681SAndroid Build Coastguard Workerinteract with to create symbols and sections.  This class can not be subclassed.
652*9880d681SAndroid Build Coastguard Worker
653*9880d681SAndroid Build Coastguard WorkerThe ``MCSymbol`` class
654*9880d681SAndroid Build Coastguard Worker----------------------
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard WorkerThe MCSymbol class represents a symbol (aka label) in the assembly file.  There
657*9880d681SAndroid Build Coastguard Workerare two interesting kinds of symbols: assembler temporary symbols, and normal
658*9880d681SAndroid Build Coastguard Workersymbols.  Assembler temporary symbols are used and processed by the assembler
659*9880d681SAndroid Build Coastguard Workerbut are discarded when the object file is produced.  The distinction is usually
660*9880d681SAndroid Build Coastguard Workerrepresented by adding a prefix to the label, for example "L" labels are
661*9880d681SAndroid Build Coastguard Workerassembler temporary labels in MachO.
662*9880d681SAndroid Build Coastguard Worker
663*9880d681SAndroid Build Coastguard WorkerMCSymbols are created by MCContext and uniqued there.  This means that MCSymbols
664*9880d681SAndroid Build Coastguard Workercan be compared for pointer equivalence to find out if they are the same symbol.
665*9880d681SAndroid Build Coastguard WorkerNote that pointer inequality does not guarantee the labels will end up at
666*9880d681SAndroid Build Coastguard Workerdifferent addresses though.  It's perfectly legal to output something like this
667*9880d681SAndroid Build Coastguard Workerto the .s file:
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker::
670*9880d681SAndroid Build Coastguard Worker
671*9880d681SAndroid Build Coastguard Worker  foo:
672*9880d681SAndroid Build Coastguard Worker  bar:
673*9880d681SAndroid Build Coastguard Worker    .byte 4
674*9880d681SAndroid Build Coastguard Worker
675*9880d681SAndroid Build Coastguard WorkerIn this case, both the foo and bar symbols will have the same address.
676*9880d681SAndroid Build Coastguard Worker
677*9880d681SAndroid Build Coastguard WorkerThe ``MCSection`` class
678*9880d681SAndroid Build Coastguard Worker-----------------------
679*9880d681SAndroid Build Coastguard Worker
680*9880d681SAndroid Build Coastguard WorkerThe ``MCSection`` class represents an object-file specific section. It is
681*9880d681SAndroid Build Coastguard Workersubclassed by object file specific implementations (e.g. ``MCSectionMachO``,
682*9880d681SAndroid Build Coastguard Worker``MCSectionCOFF``, ``MCSectionELF``) and these are created and uniqued by
683*9880d681SAndroid Build Coastguard WorkerMCContext.  The MCStreamer has a notion of the current section, which can be
684*9880d681SAndroid Build Coastguard Workerchanged with the SwitchToSection method (which corresponds to a ".section"
685*9880d681SAndroid Build Coastguard Workerdirective in a .s file).
686*9880d681SAndroid Build Coastguard Worker
687*9880d681SAndroid Build Coastguard Worker.. _MCInst:
688*9880d681SAndroid Build Coastguard Worker
689*9880d681SAndroid Build Coastguard WorkerThe ``MCInst`` class
690*9880d681SAndroid Build Coastguard Worker--------------------
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard WorkerThe ``MCInst`` class is a target-independent representation of an instruction.
693*9880d681SAndroid Build Coastguard WorkerIt is a simple class (much more so than `MachineInstr`_) that holds a
694*9880d681SAndroid Build Coastguard Workertarget-specific opcode and a vector of MCOperands.  MCOperand, in turn, is a
695*9880d681SAndroid Build Coastguard Workersimple discriminated union of three cases: 1) a simple immediate, 2) a target
696*9880d681SAndroid Build Coastguard Workerregister ID, 3) a symbolic expression (e.g. "``Lfoo-Lbar+42``") as an MCExpr.
697*9880d681SAndroid Build Coastguard Worker
698*9880d681SAndroid Build Coastguard WorkerMCInst is the common currency used to represent machine instructions at the MC
699*9880d681SAndroid Build Coastguard Workerlayer.  It is the type used by the instruction encoder, the instruction printer,
700*9880d681SAndroid Build Coastguard Workerand the type generated by the assembly parser and disassembler.
701*9880d681SAndroid Build Coastguard Worker
702*9880d681SAndroid Build Coastguard Worker.. _Target-independent algorithms:
703*9880d681SAndroid Build Coastguard Worker.. _code generation algorithm:
704*9880d681SAndroid Build Coastguard Worker
705*9880d681SAndroid Build Coastguard WorkerTarget-independent code generation algorithms
706*9880d681SAndroid Build Coastguard Worker=============================================
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard WorkerThis section documents the phases described in the `high-level design of the
709*9880d681SAndroid Build Coastguard Workercode generator`_.  It explains how they work and some of the rationale behind
710*9880d681SAndroid Build Coastguard Workertheir design.
711*9880d681SAndroid Build Coastguard Worker
712*9880d681SAndroid Build Coastguard Worker.. _Instruction Selection:
713*9880d681SAndroid Build Coastguard Worker.. _instruction selection section:
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard WorkerInstruction Selection
716*9880d681SAndroid Build Coastguard Worker---------------------
717*9880d681SAndroid Build Coastguard Worker
718*9880d681SAndroid Build Coastguard WorkerInstruction Selection is the process of translating LLVM code presented to the
719*9880d681SAndroid Build Coastguard Workercode generator into target-specific machine instructions.  There are several
720*9880d681SAndroid Build Coastguard Workerwell-known ways to do this in the literature.  LLVM uses a SelectionDAG based
721*9880d681SAndroid Build Coastguard Workerinstruction selector.
722*9880d681SAndroid Build Coastguard Worker
723*9880d681SAndroid Build Coastguard WorkerPortions of the DAG instruction selector are generated from the target
724*9880d681SAndroid Build Coastguard Workerdescription (``*.td``) files.  Our goal is for the entire instruction selector
725*9880d681SAndroid Build Coastguard Workerto be generated from these ``.td`` files, though currently there are still
726*9880d681SAndroid Build Coastguard Workerthings that require custom C++ code.
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker.. _SelectionDAG:
729*9880d681SAndroid Build Coastguard Worker
730*9880d681SAndroid Build Coastguard WorkerIntroduction to SelectionDAGs
731*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
732*9880d681SAndroid Build Coastguard Worker
733*9880d681SAndroid Build Coastguard WorkerThe SelectionDAG provides an abstraction for code representation in a way that
734*9880d681SAndroid Build Coastguard Workeris amenable to instruction selection using automatic techniques
735*9880d681SAndroid Build Coastguard Worker(e.g. dynamic-programming based optimal pattern matching selectors). It is also
736*9880d681SAndroid Build Coastguard Workerwell-suited to other phases of code generation; in particular, instruction
737*9880d681SAndroid Build Coastguard Workerscheduling (SelectionDAG's are very close to scheduling DAGs post-selection).
738*9880d681SAndroid Build Coastguard WorkerAdditionally, the SelectionDAG provides a host representation where a large
739*9880d681SAndroid Build Coastguard Workervariety of very-low-level (but target-independent) `optimizations`_ may be
740*9880d681SAndroid Build Coastguard Workerperformed; ones which require extensive information about the instructions
741*9880d681SAndroid Build Coastguard Workerefficiently supported by the target.
742*9880d681SAndroid Build Coastguard Worker
743*9880d681SAndroid Build Coastguard WorkerThe SelectionDAG is a Directed-Acyclic-Graph whose nodes are instances of the
744*9880d681SAndroid Build Coastguard Worker``SDNode`` class.  The primary payload of the ``SDNode`` is its operation code
745*9880d681SAndroid Build Coastguard Worker(Opcode) that indicates what operation the node performs and the operands to the
746*9880d681SAndroid Build Coastguard Workeroperation.  The various operation node types are described at the top of the
747*9880d681SAndroid Build Coastguard Worker``include/llvm/CodeGen/ISDOpcodes.h`` file.
748*9880d681SAndroid Build Coastguard Worker
749*9880d681SAndroid Build Coastguard WorkerAlthough most operations define a single value, each node in the graph may
750*9880d681SAndroid Build Coastguard Workerdefine multiple values.  For example, a combined div/rem operation will define
751*9880d681SAndroid Build Coastguard Workerboth the dividend and the remainder. Many other situations require multiple
752*9880d681SAndroid Build Coastguard Workervalues as well.  Each node also has some number of operands, which are edges to
753*9880d681SAndroid Build Coastguard Workerthe node defining the used value.  Because nodes may define multiple values,
754*9880d681SAndroid Build Coastguard Workeredges are represented by instances of the ``SDValue`` class, which is a
755*9880d681SAndroid Build Coastguard Worker``<SDNode, unsigned>`` pair, indicating the node and result value being used,
756*9880d681SAndroid Build Coastguard Workerrespectively.  Each value produced by an ``SDNode`` has an associated ``MVT``
757*9880d681SAndroid Build Coastguard Worker(Machine Value Type) indicating what the type of the value is.
758*9880d681SAndroid Build Coastguard Worker
759*9880d681SAndroid Build Coastguard WorkerSelectionDAGs contain two different kinds of values: those that represent data
760*9880d681SAndroid Build Coastguard Workerflow and those that represent control flow dependencies.  Data values are simple
761*9880d681SAndroid Build Coastguard Workeredges with an integer or floating point value type.  Control edges are
762*9880d681SAndroid Build Coastguard Workerrepresented as "chain" edges which are of type ``MVT::Other``.  These edges
763*9880d681SAndroid Build Coastguard Workerprovide an ordering between nodes that have side effects (such as loads, stores,
764*9880d681SAndroid Build Coastguard Workercalls, returns, etc).  All nodes that have side effects should take a token
765*9880d681SAndroid Build Coastguard Workerchain as input and produce a new one as output.  By convention, token chain
766*9880d681SAndroid Build Coastguard Workerinputs are always operand #0, and chain results are always the last value
767*9880d681SAndroid Build Coastguard Workerproduced by an operation. However, after instruction selection, the
768*9880d681SAndroid Build Coastguard Workermachine nodes have their chain after the instruction's operands, and
769*9880d681SAndroid Build Coastguard Workermay be followed by glue nodes.
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard WorkerA SelectionDAG has designated "Entry" and "Root" nodes.  The Entry node is
772*9880d681SAndroid Build Coastguard Workeralways a marker node with an Opcode of ``ISD::EntryToken``.  The Root node is
773*9880d681SAndroid Build Coastguard Workerthe final side-effecting node in the token chain. For example, in a single basic
774*9880d681SAndroid Build Coastguard Workerblock function it would be the return node.
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard WorkerOne important concept for SelectionDAGs is the notion of a "legal" vs.
777*9880d681SAndroid Build Coastguard Worker"illegal" DAG.  A legal DAG for a target is one that only uses supported
778*9880d681SAndroid Build Coastguard Workeroperations and supported types.  On a 32-bit PowerPC, for example, a DAG with a
779*9880d681SAndroid Build Coastguard Workervalue of type i1, i8, i16, or i64 would be illegal, as would a DAG that uses a
780*9880d681SAndroid Build Coastguard WorkerSREM or UREM operation.  The `legalize types`_ and `legalize operations`_ phases
781*9880d681SAndroid Build Coastguard Workerare responsible for turning an illegal DAG into a legal DAG.
782*9880d681SAndroid Build Coastguard Worker
783*9880d681SAndroid Build Coastguard Worker.. _SelectionDAG-Process:
784*9880d681SAndroid Build Coastguard Worker
785*9880d681SAndroid Build Coastguard WorkerSelectionDAG Instruction Selection Process
786*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
787*9880d681SAndroid Build Coastguard Worker
788*9880d681SAndroid Build Coastguard WorkerSelectionDAG-based instruction selection consists of the following steps:
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard Worker#. `Build initial DAG`_ --- This stage performs a simple translation from the
791*9880d681SAndroid Build Coastguard Worker   input LLVM code to an illegal SelectionDAG.
792*9880d681SAndroid Build Coastguard Worker
793*9880d681SAndroid Build Coastguard Worker#. `Optimize SelectionDAG`_ --- This stage performs simple optimizations on the
794*9880d681SAndroid Build Coastguard Worker   SelectionDAG to simplify it, and recognize meta instructions (like rotates
795*9880d681SAndroid Build Coastguard Worker   and ``div``/``rem`` pairs) for targets that support these meta operations.
796*9880d681SAndroid Build Coastguard Worker   This makes the resultant code more efficient and the `select instructions
797*9880d681SAndroid Build Coastguard Worker   from DAG`_ phase (below) simpler.
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker#. `Legalize SelectionDAG Types`_ --- This stage transforms SelectionDAG nodes
800*9880d681SAndroid Build Coastguard Worker   to eliminate any types that are unsupported on the target.
801*9880d681SAndroid Build Coastguard Worker
802*9880d681SAndroid Build Coastguard Worker#. `Optimize SelectionDAG`_ --- The SelectionDAG optimizer is run to clean up
803*9880d681SAndroid Build Coastguard Worker   redundancies exposed by type legalization.
804*9880d681SAndroid Build Coastguard Worker
805*9880d681SAndroid Build Coastguard Worker#. `Legalize SelectionDAG Ops`_ --- This stage transforms SelectionDAG nodes to
806*9880d681SAndroid Build Coastguard Worker   eliminate any operations that are unsupported on the target.
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker#. `Optimize SelectionDAG`_ --- The SelectionDAG optimizer is run to eliminate
809*9880d681SAndroid Build Coastguard Worker   inefficiencies introduced by operation legalization.
810*9880d681SAndroid Build Coastguard Worker
811*9880d681SAndroid Build Coastguard Worker#. `Select instructions from DAG`_ --- Finally, the target instruction selector
812*9880d681SAndroid Build Coastguard Worker   matches the DAG operations to target instructions.  This process translates
813*9880d681SAndroid Build Coastguard Worker   the target-independent input DAG into another DAG of target instructions.
814*9880d681SAndroid Build Coastguard Worker
815*9880d681SAndroid Build Coastguard Worker#. `SelectionDAG Scheduling and Formation`_ --- The last phase assigns a linear
816*9880d681SAndroid Build Coastguard Worker   order to the instructions in the target-instruction DAG and emits them into
817*9880d681SAndroid Build Coastguard Worker   the MachineFunction being compiled.  This step uses traditional prepass
818*9880d681SAndroid Build Coastguard Worker   scheduling techniques.
819*9880d681SAndroid Build Coastguard Worker
820*9880d681SAndroid Build Coastguard WorkerAfter all of these steps are complete, the SelectionDAG is destroyed and the
821*9880d681SAndroid Build Coastguard Workerrest of the code generation passes are run.
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard WorkerOne great way to visualize what is going on here is to take advantage of a few
824*9880d681SAndroid Build Coastguard WorkerLLC command line options.  The following options pop up a window displaying the
825*9880d681SAndroid Build Coastguard WorkerSelectionDAG at specific times (if you only get errors printed to the console
826*9880d681SAndroid Build Coastguard Workerwhile using this, you probably `need to configure your
827*9880d681SAndroid Build Coastguard Workersystem <ProgrammersManual.html#viewing-graphs-while-debugging-code>`_ to add support for it).
828*9880d681SAndroid Build Coastguard Worker
829*9880d681SAndroid Build Coastguard Worker* ``-view-dag-combine1-dags`` displays the DAG after being built, before the
830*9880d681SAndroid Build Coastguard Worker  first optimization pass.
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard Worker* ``-view-legalize-dags`` displays the DAG before Legalization.
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker* ``-view-dag-combine2-dags`` displays the DAG before the second optimization
835*9880d681SAndroid Build Coastguard Worker  pass.
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker* ``-view-isel-dags`` displays the DAG before the Select phase.
838*9880d681SAndroid Build Coastguard Worker
839*9880d681SAndroid Build Coastguard Worker* ``-view-sched-dags`` displays the DAG before Scheduling.
840*9880d681SAndroid Build Coastguard Worker
841*9880d681SAndroid Build Coastguard WorkerThe ``-view-sunit-dags`` displays the Scheduler's dependency graph.  This graph
842*9880d681SAndroid Build Coastguard Workeris based on the final SelectionDAG, with nodes that must be scheduled together
843*9880d681SAndroid Build Coastguard Workerbundled into a single scheduling-unit node, and with immediate operands and
844*9880d681SAndroid Build Coastguard Workerother nodes that aren't relevant for scheduling omitted.
845*9880d681SAndroid Build Coastguard Worker
846*9880d681SAndroid Build Coastguard WorkerThe option ``-filter-view-dags`` allows to select the name of the basic block
847*9880d681SAndroid Build Coastguard Workerthat you are interested to visualize and filters all the previous
848*9880d681SAndroid Build Coastguard Worker``view-*-dags`` options.
849*9880d681SAndroid Build Coastguard Worker
850*9880d681SAndroid Build Coastguard Worker.. _Build initial DAG:
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard WorkerInitial SelectionDAG Construction
853*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
854*9880d681SAndroid Build Coastguard Worker
855*9880d681SAndroid Build Coastguard WorkerThe initial SelectionDAG is na\ :raw-html:`&iuml;`\ vely peephole expanded from
856*9880d681SAndroid Build Coastguard Workerthe LLVM input by the ``SelectionDAGBuilder`` class.  The intent of this pass
857*9880d681SAndroid Build Coastguard Workeris to expose as much low-level, target-specific details to the SelectionDAG as
858*9880d681SAndroid Build Coastguard Workerpossible.  This pass is mostly hard-coded (e.g. an LLVM ``add`` turns into an
859*9880d681SAndroid Build Coastguard Worker``SDNode add`` while a ``getelementptr`` is expanded into the obvious
860*9880d681SAndroid Build Coastguard Workerarithmetic). This pass requires target-specific hooks to lower calls, returns,
861*9880d681SAndroid Build Coastguard Workervarargs, etc.  For these features, the :raw-html:`<tt>` `TargetLowering`_
862*9880d681SAndroid Build Coastguard Worker:raw-html:`</tt>` interface is used.
863*9880d681SAndroid Build Coastguard Worker
864*9880d681SAndroid Build Coastguard Worker.. _legalize types:
865*9880d681SAndroid Build Coastguard Worker.. _Legalize SelectionDAG Types:
866*9880d681SAndroid Build Coastguard Worker.. _Legalize SelectionDAG Ops:
867*9880d681SAndroid Build Coastguard Worker
868*9880d681SAndroid Build Coastguard WorkerSelectionDAG LegalizeTypes Phase
869*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
870*9880d681SAndroid Build Coastguard Worker
871*9880d681SAndroid Build Coastguard WorkerThe Legalize phase is in charge of converting a DAG to only use the types that
872*9880d681SAndroid Build Coastguard Workerare natively supported by the target.
873*9880d681SAndroid Build Coastguard Worker
874*9880d681SAndroid Build Coastguard WorkerThere are two main ways of converting values of unsupported scalar types to
875*9880d681SAndroid Build Coastguard Workervalues of supported types: converting small types to larger types ("promoting"),
876*9880d681SAndroid Build Coastguard Workerand breaking up large integer types into smaller ones ("expanding").  For
877*9880d681SAndroid Build Coastguard Workerexample, a target might require that all f32 values are promoted to f64 and that
878*9880d681SAndroid Build Coastguard Workerall i1/i8/i16 values are promoted to i32.  The same target might require that
879*9880d681SAndroid Build Coastguard Workerall i64 values be expanded into pairs of i32 values.  These changes can insert
880*9880d681SAndroid Build Coastguard Workersign and zero extensions as needed to make sure that the final code has the same
881*9880d681SAndroid Build Coastguard Workerbehavior as the input.
882*9880d681SAndroid Build Coastguard Worker
883*9880d681SAndroid Build Coastguard WorkerThere are two main ways of converting values of unsupported vector types to
884*9880d681SAndroid Build Coastguard Workervalue of supported types: splitting vector types, multiple times if necessary,
885*9880d681SAndroid Build Coastguard Workeruntil a legal type is found, and extending vector types by adding elements to
886*9880d681SAndroid Build Coastguard Workerthe end to round them out to legal types ("widening").  If a vector gets split
887*9880d681SAndroid Build Coastguard Workerall the way down to single-element parts with no supported vector type being
888*9880d681SAndroid Build Coastguard Workerfound, the elements are converted to scalars ("scalarizing").
889*9880d681SAndroid Build Coastguard Worker
890*9880d681SAndroid Build Coastguard WorkerA target implementation tells the legalizer which types are supported (and which
891*9880d681SAndroid Build Coastguard Workerregister class to use for them) by calling the ``addRegisterClass`` method in
892*9880d681SAndroid Build Coastguard Workerits ``TargetLowering`` constructor.
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker.. _legalize operations:
895*9880d681SAndroid Build Coastguard Worker.. _Legalizer:
896*9880d681SAndroid Build Coastguard Worker
897*9880d681SAndroid Build Coastguard WorkerSelectionDAG Legalize Phase
898*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^
899*9880d681SAndroid Build Coastguard Worker
900*9880d681SAndroid Build Coastguard WorkerThe Legalize phase is in charge of converting a DAG to only use the operations
901*9880d681SAndroid Build Coastguard Workerthat are natively supported by the target.
902*9880d681SAndroid Build Coastguard Worker
903*9880d681SAndroid Build Coastguard WorkerTargets often have weird constraints, such as not supporting every operation on
904*9880d681SAndroid Build Coastguard Workerevery supported datatype (e.g. X86 does not support byte conditional moves and
905*9880d681SAndroid Build Coastguard WorkerPowerPC does not support sign-extending loads from a 16-bit memory location).
906*9880d681SAndroid Build Coastguard WorkerLegalize takes care of this by open-coding another sequence of operations to
907*9880d681SAndroid Build Coastguard Workeremulate the operation ("expansion"), by promoting one type to a larger type that
908*9880d681SAndroid Build Coastguard Workersupports the operation ("promotion"), or by using a target-specific hook to
909*9880d681SAndroid Build Coastguard Workerimplement the legalization ("custom").
910*9880d681SAndroid Build Coastguard Worker
911*9880d681SAndroid Build Coastguard WorkerA target implementation tells the legalizer which operations are not supported
912*9880d681SAndroid Build Coastguard Worker(and which of the above three actions to take) by calling the
913*9880d681SAndroid Build Coastguard Worker``setOperationAction`` method in its ``TargetLowering`` constructor.
914*9880d681SAndroid Build Coastguard Worker
915*9880d681SAndroid Build Coastguard WorkerPrior to the existence of the Legalize passes, we required that every target
916*9880d681SAndroid Build Coastguard Worker`selector`_ supported and handled every operator and type even if they are not
917*9880d681SAndroid Build Coastguard Workernatively supported.  The introduction of the Legalize phases allows all of the
918*9880d681SAndroid Build Coastguard Workercanonicalization patterns to be shared across targets, and makes it very easy to
919*9880d681SAndroid Build Coastguard Workeroptimize the canonicalized code because it is still in the form of a DAG.
920*9880d681SAndroid Build Coastguard Worker
921*9880d681SAndroid Build Coastguard Worker.. _optimizations:
922*9880d681SAndroid Build Coastguard Worker.. _Optimize SelectionDAG:
923*9880d681SAndroid Build Coastguard Worker.. _selector:
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard WorkerSelectionDAG Optimization Phase: the DAG Combiner
926*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
927*9880d681SAndroid Build Coastguard Worker
928*9880d681SAndroid Build Coastguard WorkerThe SelectionDAG optimization phase is run multiple times for code generation,
929*9880d681SAndroid Build Coastguard Workerimmediately after the DAG is built and once after each legalization.  The first
930*9880d681SAndroid Build Coastguard Workerrun of the pass allows the initial code to be cleaned up (e.g. performing
931*9880d681SAndroid Build Coastguard Workeroptimizations that depend on knowing that the operators have restricted type
932*9880d681SAndroid Build Coastguard Workerinputs).  Subsequent runs of the pass clean up the messy code generated by the
933*9880d681SAndroid Build Coastguard WorkerLegalize passes, which allows Legalize to be very simple (it can focus on making
934*9880d681SAndroid Build Coastguard Workercode legal instead of focusing on generating *good* and legal code).
935*9880d681SAndroid Build Coastguard Worker
936*9880d681SAndroid Build Coastguard WorkerOne important class of optimizations performed is optimizing inserted sign and
937*9880d681SAndroid Build Coastguard Workerzero extension instructions.  We currently use ad-hoc techniques, but could move
938*9880d681SAndroid Build Coastguard Workerto more rigorous techniques in the future.  Here are some good papers on the
939*9880d681SAndroid Build Coastguard Workersubject:
940*9880d681SAndroid Build Coastguard Worker
941*9880d681SAndroid Build Coastguard Worker"`Widening integer arithmetic <http://www.eecs.harvard.edu/~nr/pubs/widen-abstract.html>`_" :raw-html:`<br>`
942*9880d681SAndroid Build Coastguard WorkerKevin Redwine and Norman Ramsey :raw-html:`<br>`
943*9880d681SAndroid Build Coastguard WorkerInternational Conference on Compiler Construction (CC) 2004
944*9880d681SAndroid Build Coastguard Worker
945*9880d681SAndroid Build Coastguard Worker"`Effective sign extension elimination <http://portal.acm.org/citation.cfm?doid=512529.512552>`_"  :raw-html:`<br>`
946*9880d681SAndroid Build Coastguard WorkerMotohiro Kawahito, Hideaki Komatsu, and Toshio Nakatani :raw-html:`<br>`
947*9880d681SAndroid Build Coastguard WorkerProceedings of the ACM SIGPLAN 2002 Conference on Programming Language Design
948*9880d681SAndroid Build Coastguard Workerand Implementation.
949*9880d681SAndroid Build Coastguard Worker
950*9880d681SAndroid Build Coastguard Worker.. _Select instructions from DAG:
951*9880d681SAndroid Build Coastguard Worker
952*9880d681SAndroid Build Coastguard WorkerSelectionDAG Select Phase
953*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^
954*9880d681SAndroid Build Coastguard Worker
955*9880d681SAndroid Build Coastguard WorkerThe Select phase is the bulk of the target-specific code for instruction
956*9880d681SAndroid Build Coastguard Workerselection.  This phase takes a legal SelectionDAG as input, pattern matches the
957*9880d681SAndroid Build Coastguard Workerinstructions supported by the target to this DAG, and produces a new DAG of
958*9880d681SAndroid Build Coastguard Workertarget code.  For example, consider the following LLVM fragment:
959*9880d681SAndroid Build Coastguard Worker
960*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
961*9880d681SAndroid Build Coastguard Worker
962*9880d681SAndroid Build Coastguard Worker  %t1 = fadd float %W, %X
963*9880d681SAndroid Build Coastguard Worker  %t2 = fmul float %t1, %Y
964*9880d681SAndroid Build Coastguard Worker  %t3 = fadd float %t2, %Z
965*9880d681SAndroid Build Coastguard Worker
966*9880d681SAndroid Build Coastguard WorkerThis LLVM code corresponds to a SelectionDAG that looks basically like this:
967*9880d681SAndroid Build Coastguard Worker
968*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
969*9880d681SAndroid Build Coastguard Worker
970*9880d681SAndroid Build Coastguard Worker  (fadd:f32 (fmul:f32 (fadd:f32 W, X), Y), Z)
971*9880d681SAndroid Build Coastguard Worker
972*9880d681SAndroid Build Coastguard WorkerIf a target supports floating point multiply-and-add (FMA) operations, one of
973*9880d681SAndroid Build Coastguard Workerthe adds can be merged with the multiply.  On the PowerPC, for example, the
974*9880d681SAndroid Build Coastguard Workeroutput of the instruction selector might look like this DAG:
975*9880d681SAndroid Build Coastguard Worker
976*9880d681SAndroid Build Coastguard Worker::
977*9880d681SAndroid Build Coastguard Worker
978*9880d681SAndroid Build Coastguard Worker  (FMADDS (FADDS W, X), Y, Z)
979*9880d681SAndroid Build Coastguard Worker
980*9880d681SAndroid Build Coastguard WorkerThe ``FMADDS`` instruction is a ternary instruction that multiplies its first
981*9880d681SAndroid Build Coastguard Workertwo operands and adds the third (as single-precision floating-point numbers).
982*9880d681SAndroid Build Coastguard WorkerThe ``FADDS`` instruction is a simple binary single-precision add instruction.
983*9880d681SAndroid Build Coastguard WorkerTo perform this pattern match, the PowerPC backend includes the following
984*9880d681SAndroid Build Coastguard Workerinstruction definitions:
985*9880d681SAndroid Build Coastguard Worker
986*9880d681SAndroid Build Coastguard Worker.. code-block:: text
987*9880d681SAndroid Build Coastguard Worker  :emphasize-lines: 4-5,9
988*9880d681SAndroid Build Coastguard Worker
989*9880d681SAndroid Build Coastguard Worker  def FMADDS : AForm_1<59, 29,
990*9880d681SAndroid Build Coastguard Worker                      (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRC, F4RC:$FRB),
991*9880d681SAndroid Build Coastguard Worker                      "fmadds $FRT, $FRA, $FRC, $FRB",
992*9880d681SAndroid Build Coastguard Worker                      [(set F4RC:$FRT, (fadd (fmul F4RC:$FRA, F4RC:$FRC),
993*9880d681SAndroid Build Coastguard Worker                                             F4RC:$FRB))]>;
994*9880d681SAndroid Build Coastguard Worker  def FADDS : AForm_2<59, 21,
995*9880d681SAndroid Build Coastguard Worker                      (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRB),
996*9880d681SAndroid Build Coastguard Worker                      "fadds $FRT, $FRA, $FRB",
997*9880d681SAndroid Build Coastguard Worker                      [(set F4RC:$FRT, (fadd F4RC:$FRA, F4RC:$FRB))]>;
998*9880d681SAndroid Build Coastguard Worker
999*9880d681SAndroid Build Coastguard WorkerThe highlighted portion of the instruction definitions indicates the pattern
1000*9880d681SAndroid Build Coastguard Workerused to match the instructions. The DAG operators (like ``fmul``/``fadd``)
1001*9880d681SAndroid Build Coastguard Workerare defined in the ``include/llvm/Target/TargetSelectionDAG.td`` file.
1002*9880d681SAndroid Build Coastguard Worker"``F4RC``" is the register class of the input and result values.
1003*9880d681SAndroid Build Coastguard Worker
1004*9880d681SAndroid Build Coastguard WorkerThe TableGen DAG instruction selector generator reads the instruction patterns
1005*9880d681SAndroid Build Coastguard Workerin the ``.td`` file and automatically builds parts of the pattern matching code
1006*9880d681SAndroid Build Coastguard Workerfor your target.  It has the following strengths:
1007*9880d681SAndroid Build Coastguard Worker
1008*9880d681SAndroid Build Coastguard Worker* At compiler-compiler time, it analyzes your instruction patterns and tells you
1009*9880d681SAndroid Build Coastguard Worker  if your patterns make sense or not.
1010*9880d681SAndroid Build Coastguard Worker
1011*9880d681SAndroid Build Coastguard Worker* It can handle arbitrary constraints on operands for the pattern match.  In
1012*9880d681SAndroid Build Coastguard Worker  particular, it is straight-forward to say things like "match any immediate
1013*9880d681SAndroid Build Coastguard Worker  that is a 13-bit sign-extended value".  For examples, see the ``immSExt16``
1014*9880d681SAndroid Build Coastguard Worker  and related ``tblgen`` classes in the PowerPC backend.
1015*9880d681SAndroid Build Coastguard Worker
1016*9880d681SAndroid Build Coastguard Worker* It knows several important identities for the patterns defined.  For example,
1017*9880d681SAndroid Build Coastguard Worker  it knows that addition is commutative, so it allows the ``FMADDS`` pattern
1018*9880d681SAndroid Build Coastguard Worker  above to match "``(fadd X, (fmul Y, Z))``" as well as "``(fadd (fmul X, Y),
1019*9880d681SAndroid Build Coastguard Worker  Z)``", without the target author having to specially handle this case.
1020*9880d681SAndroid Build Coastguard Worker
1021*9880d681SAndroid Build Coastguard Worker* It has a full-featured type-inferencing system.  In particular, you should
1022*9880d681SAndroid Build Coastguard Worker  rarely have to explicitly tell the system what type parts of your patterns
1023*9880d681SAndroid Build Coastguard Worker  are.  In the ``FMADDS`` case above, we didn't have to tell ``tblgen`` that all
1024*9880d681SAndroid Build Coastguard Worker  of the nodes in the pattern are of type 'f32'.  It was able to infer and
1025*9880d681SAndroid Build Coastguard Worker  propagate this knowledge from the fact that ``F4RC`` has type 'f32'.
1026*9880d681SAndroid Build Coastguard Worker
1027*9880d681SAndroid Build Coastguard Worker* Targets can define their own (and rely on built-in) "pattern fragments".
1028*9880d681SAndroid Build Coastguard Worker  Pattern fragments are chunks of reusable patterns that get inlined into your
1029*9880d681SAndroid Build Coastguard Worker  patterns during compiler-compiler time.  For example, the integer "``(not
1030*9880d681SAndroid Build Coastguard Worker  x)``" operation is actually defined as a pattern fragment that expands as
1031*9880d681SAndroid Build Coastguard Worker  "``(xor x, -1)``", since the SelectionDAG does not have a native '``not``'
1032*9880d681SAndroid Build Coastguard Worker  operation.  Targets can define their own short-hand fragments as they see fit.
1033*9880d681SAndroid Build Coastguard Worker  See the definition of '``not``' and '``ineg``' for examples.
1034*9880d681SAndroid Build Coastguard Worker
1035*9880d681SAndroid Build Coastguard Worker* In addition to instructions, targets can specify arbitrary patterns that map
1036*9880d681SAndroid Build Coastguard Worker  to one or more instructions using the 'Pat' class.  For example, the PowerPC
1037*9880d681SAndroid Build Coastguard Worker  has no way to load an arbitrary integer immediate into a register in one
1038*9880d681SAndroid Build Coastguard Worker  instruction. To tell tblgen how to do this, it defines:
1039*9880d681SAndroid Build Coastguard Worker
1040*9880d681SAndroid Build Coastguard Worker  ::
1041*9880d681SAndroid Build Coastguard Worker
1042*9880d681SAndroid Build Coastguard Worker    // Arbitrary immediate support.  Implement in terms of LIS/ORI.
1043*9880d681SAndroid Build Coastguard Worker    def : Pat<(i32 imm:$imm),
1044*9880d681SAndroid Build Coastguard Worker              (ORI (LIS (HI16 imm:$imm)), (LO16 imm:$imm))>;
1045*9880d681SAndroid Build Coastguard Worker
1046*9880d681SAndroid Build Coastguard Worker  If none of the single-instruction patterns for loading an immediate into a
1047*9880d681SAndroid Build Coastguard Worker  register match, this will be used.  This rule says "match an arbitrary i32
1048*9880d681SAndroid Build Coastguard Worker  immediate, turning it into an ``ORI`` ('or a 16-bit immediate') and an ``LIS``
1049*9880d681SAndroid Build Coastguard Worker  ('load 16-bit immediate, where the immediate is shifted to the left 16 bits')
1050*9880d681SAndroid Build Coastguard Worker  instruction".  To make this work, the ``LO16``/``HI16`` node transformations
1051*9880d681SAndroid Build Coastguard Worker  are used to manipulate the input immediate (in this case, take the high or low
1052*9880d681SAndroid Build Coastguard Worker  16-bits of the immediate).
1053*9880d681SAndroid Build Coastguard Worker
1054*9880d681SAndroid Build Coastguard Worker* When using the 'Pat' class to map a pattern to an instruction that has one
1055*9880d681SAndroid Build Coastguard Worker  or more complex operands (like e.g. `X86 addressing mode`_), the pattern may
1056*9880d681SAndroid Build Coastguard Worker  either specify the operand as a whole using a ``ComplexPattern``, or else it
1057*9880d681SAndroid Build Coastguard Worker  may specify the components of the complex operand separately.  The latter is
1058*9880d681SAndroid Build Coastguard Worker  done e.g. for pre-increment instructions by the PowerPC back end:
1059*9880d681SAndroid Build Coastguard Worker
1060*9880d681SAndroid Build Coastguard Worker  ::
1061*9880d681SAndroid Build Coastguard Worker
1062*9880d681SAndroid Build Coastguard Worker    def STWU  : DForm_1<37, (outs ptr_rc:$ea_res), (ins GPRC:$rS, memri:$dst),
1063*9880d681SAndroid Build Coastguard Worker                    "stwu $rS, $dst", LdStStoreUpd, []>,
1064*9880d681SAndroid Build Coastguard Worker                    RegConstraint<"$dst.reg = $ea_res">, NoEncode<"$ea_res">;
1065*9880d681SAndroid Build Coastguard Worker
1066*9880d681SAndroid Build Coastguard Worker    def : Pat<(pre_store GPRC:$rS, ptr_rc:$ptrreg, iaddroff:$ptroff),
1067*9880d681SAndroid Build Coastguard Worker              (STWU GPRC:$rS, iaddroff:$ptroff, ptr_rc:$ptrreg)>;
1068*9880d681SAndroid Build Coastguard Worker
1069*9880d681SAndroid Build Coastguard Worker  Here, the pair of ``ptroff`` and ``ptrreg`` operands is matched onto the
1070*9880d681SAndroid Build Coastguard Worker  complex operand ``dst`` of class ``memri`` in the ``STWU`` instruction.
1071*9880d681SAndroid Build Coastguard Worker
1072*9880d681SAndroid Build Coastguard Worker* While the system does automate a lot, it still allows you to write custom C++
1073*9880d681SAndroid Build Coastguard Worker  code to match special cases if there is something that is hard to
1074*9880d681SAndroid Build Coastguard Worker  express.
1075*9880d681SAndroid Build Coastguard Worker
1076*9880d681SAndroid Build Coastguard WorkerWhile it has many strengths, the system currently has some limitations,
1077*9880d681SAndroid Build Coastguard Workerprimarily because it is a work in progress and is not yet finished:
1078*9880d681SAndroid Build Coastguard Worker
1079*9880d681SAndroid Build Coastguard Worker* Overall, there is no way to define or match SelectionDAG nodes that define
1080*9880d681SAndroid Build Coastguard Worker  multiple values (e.g. ``SMUL_LOHI``, ``LOAD``, ``CALL``, etc).  This is the
1081*9880d681SAndroid Build Coastguard Worker  biggest reason that you currently still *have to* write custom C++ code
1082*9880d681SAndroid Build Coastguard Worker  for your instruction selector.
1083*9880d681SAndroid Build Coastguard Worker
1084*9880d681SAndroid Build Coastguard Worker* There is no great way to support matching complex addressing modes yet.  In
1085*9880d681SAndroid Build Coastguard Worker  the future, we will extend pattern fragments to allow them to define multiple
1086*9880d681SAndroid Build Coastguard Worker  values (e.g. the four operands of the `X86 addressing mode`_, which are
1087*9880d681SAndroid Build Coastguard Worker  currently matched with custom C++ code).  In addition, we'll extend fragments
1088*9880d681SAndroid Build Coastguard Worker  so that a fragment can match multiple different patterns.
1089*9880d681SAndroid Build Coastguard Worker
1090*9880d681SAndroid Build Coastguard Worker* We don't automatically infer flags like ``isStore``/``isLoad`` yet.
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard Worker* We don't automatically generate the set of supported registers and operations
1093*9880d681SAndroid Build Coastguard Worker  for the `Legalizer`_ yet.
1094*9880d681SAndroid Build Coastguard Worker
1095*9880d681SAndroid Build Coastguard Worker* We don't have a way of tying in custom legalized nodes yet.
1096*9880d681SAndroid Build Coastguard Worker
1097*9880d681SAndroid Build Coastguard WorkerDespite these limitations, the instruction selector generator is still quite
1098*9880d681SAndroid Build Coastguard Workeruseful for most of the binary and logical operations in typical instruction
1099*9880d681SAndroid Build Coastguard Workersets.  If you run into any problems or can't figure out how to do something,
1100*9880d681SAndroid Build Coastguard Workerplease let Chris know!
1101*9880d681SAndroid Build Coastguard Worker
1102*9880d681SAndroid Build Coastguard Worker.. _Scheduling and Formation:
1103*9880d681SAndroid Build Coastguard Worker.. _SelectionDAG Scheduling and Formation:
1104*9880d681SAndroid Build Coastguard Worker
1105*9880d681SAndroid Build Coastguard WorkerSelectionDAG Scheduling and Formation Phase
1106*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard WorkerThe scheduling phase takes the DAG of target instructions from the selection
1109*9880d681SAndroid Build Coastguard Workerphase and assigns an order.  The scheduler can pick an order depending on
1110*9880d681SAndroid Build Coastguard Workervarious constraints of the machines (i.e. order for minimal register pressure or
1111*9880d681SAndroid Build Coastguard Workertry to cover instruction latencies).  Once an order is established, the DAG is
1112*9880d681SAndroid Build Coastguard Workerconverted to a list of :raw-html:`<tt>` `MachineInstr`_\s :raw-html:`</tt>` and
1113*9880d681SAndroid Build Coastguard Workerthe SelectionDAG is destroyed.
1114*9880d681SAndroid Build Coastguard Worker
1115*9880d681SAndroid Build Coastguard WorkerNote that this phase is logically separate from the instruction selection phase,
1116*9880d681SAndroid Build Coastguard Workerbut is tied to it closely in the code because it operates on SelectionDAGs.
1117*9880d681SAndroid Build Coastguard Worker
1118*9880d681SAndroid Build Coastguard WorkerFuture directions for the SelectionDAG
1119*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1120*9880d681SAndroid Build Coastguard Worker
1121*9880d681SAndroid Build Coastguard Worker#. Optional function-at-a-time selection.
1122*9880d681SAndroid Build Coastguard Worker
1123*9880d681SAndroid Build Coastguard Worker#. Auto-generate entire selector from ``.td`` file.
1124*9880d681SAndroid Build Coastguard Worker
1125*9880d681SAndroid Build Coastguard Worker.. _SSA-based Machine Code Optimizations:
1126*9880d681SAndroid Build Coastguard Worker
1127*9880d681SAndroid Build Coastguard WorkerSSA-based Machine Code Optimizations
1128*9880d681SAndroid Build Coastguard Worker------------------------------------
1129*9880d681SAndroid Build Coastguard Worker
1130*9880d681SAndroid Build Coastguard WorkerTo Be Written
1131*9880d681SAndroid Build Coastguard Worker
1132*9880d681SAndroid Build Coastguard WorkerLive Intervals
1133*9880d681SAndroid Build Coastguard Worker--------------
1134*9880d681SAndroid Build Coastguard Worker
1135*9880d681SAndroid Build Coastguard WorkerLive Intervals are the ranges (intervals) where a variable is *live*.  They are
1136*9880d681SAndroid Build Coastguard Workerused by some `register allocator`_ passes to determine if two or more virtual
1137*9880d681SAndroid Build Coastguard Workerregisters which require the same physical register are live at the same point in
1138*9880d681SAndroid Build Coastguard Workerthe program (i.e., they conflict).  When this situation occurs, one virtual
1139*9880d681SAndroid Build Coastguard Workerregister must be *spilled*.
1140*9880d681SAndroid Build Coastguard Worker
1141*9880d681SAndroid Build Coastguard WorkerLive Variable Analysis
1142*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^
1143*9880d681SAndroid Build Coastguard Worker
1144*9880d681SAndroid Build Coastguard WorkerThe first step in determining the live intervals of variables is to calculate
1145*9880d681SAndroid Build Coastguard Workerthe set of registers that are immediately dead after the instruction (i.e., the
1146*9880d681SAndroid Build Coastguard Workerinstruction calculates the value, but it is never used) and the set of registers
1147*9880d681SAndroid Build Coastguard Workerthat are used by the instruction, but are never used after the instruction
1148*9880d681SAndroid Build Coastguard Worker(i.e., they are killed). Live variable information is computed for
1149*9880d681SAndroid Build Coastguard Workereach *virtual* register and *register allocatable* physical register
1150*9880d681SAndroid Build Coastguard Workerin the function.  This is done in a very efficient manner because it uses SSA to
1151*9880d681SAndroid Build Coastguard Workersparsely compute lifetime information for virtual registers (which are in SSA
1152*9880d681SAndroid Build Coastguard Workerform) and only has to track physical registers within a block.  Before register
1153*9880d681SAndroid Build Coastguard Workerallocation, LLVM can assume that physical registers are only live within a
1154*9880d681SAndroid Build Coastguard Workersingle basic block.  This allows it to do a single, local analysis to resolve
1155*9880d681SAndroid Build Coastguard Workerphysical register lifetimes within each basic block. If a physical register is
1156*9880d681SAndroid Build Coastguard Workernot register allocatable (e.g., a stack pointer or condition codes), it is not
1157*9880d681SAndroid Build Coastguard Workertracked.
1158*9880d681SAndroid Build Coastguard Worker
1159*9880d681SAndroid Build Coastguard WorkerPhysical registers may be live in to or out of a function. Live in values are
1160*9880d681SAndroid Build Coastguard Workertypically arguments in registers. Live out values are typically return values in
1161*9880d681SAndroid Build Coastguard Workerregisters. Live in values are marked as such, and are given a dummy "defining"
1162*9880d681SAndroid Build Coastguard Workerinstruction during live intervals analysis. If the last basic block of a
1163*9880d681SAndroid Build Coastguard Workerfunction is a ``return``, then it's marked as using all live out values in the
1164*9880d681SAndroid Build Coastguard Workerfunction.
1165*9880d681SAndroid Build Coastguard Worker
1166*9880d681SAndroid Build Coastguard Worker``PHI`` nodes need to be handled specially, because the calculation of the live
1167*9880d681SAndroid Build Coastguard Workervariable information from a depth first traversal of the CFG of the function
1168*9880d681SAndroid Build Coastguard Workerwon't guarantee that a virtual register used by the ``PHI`` node is defined
1169*9880d681SAndroid Build Coastguard Workerbefore it's used. When a ``PHI`` node is encountered, only the definition is
1170*9880d681SAndroid Build Coastguard Workerhandled, because the uses will be handled in other basic blocks.
1171*9880d681SAndroid Build Coastguard Worker
1172*9880d681SAndroid Build Coastguard WorkerFor each ``PHI`` node of the current basic block, we simulate an assignment at
1173*9880d681SAndroid Build Coastguard Workerthe end of the current basic block and traverse the successor basic blocks. If a
1174*9880d681SAndroid Build Coastguard Workersuccessor basic block has a ``PHI`` node and one of the ``PHI`` node's operands
1175*9880d681SAndroid Build Coastguard Workeris coming from the current basic block, then the variable is marked as *alive*
1176*9880d681SAndroid Build Coastguard Workerwithin the current basic block and all of its predecessor basic blocks, until
1177*9880d681SAndroid Build Coastguard Workerthe basic block with the defining instruction is encountered.
1178*9880d681SAndroid Build Coastguard Worker
1179*9880d681SAndroid Build Coastguard WorkerLive Intervals Analysis
1180*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^
1181*9880d681SAndroid Build Coastguard Worker
1182*9880d681SAndroid Build Coastguard WorkerWe now have the information available to perform the live intervals analysis and
1183*9880d681SAndroid Build Coastguard Workerbuild the live intervals themselves.  We start off by numbering the basic blocks
1184*9880d681SAndroid Build Coastguard Workerand machine instructions.  We then handle the "live-in" values.  These are in
1185*9880d681SAndroid Build Coastguard Workerphysical registers, so the physical register is assumed to be killed by the end
1186*9880d681SAndroid Build Coastguard Workerof the basic block.  Live intervals for virtual registers are computed for some
1187*9880d681SAndroid Build Coastguard Workerordering of the machine instructions ``[1, N]``.  A live interval is an interval
1188*9880d681SAndroid Build Coastguard Worker``[i, j)``, where ``1 >= i >= j > N``, for which a variable is live.
1189*9880d681SAndroid Build Coastguard Worker
1190*9880d681SAndroid Build Coastguard Worker.. note::
1191*9880d681SAndroid Build Coastguard Worker  More to come...
1192*9880d681SAndroid Build Coastguard Worker
1193*9880d681SAndroid Build Coastguard Worker.. _Register Allocation:
1194*9880d681SAndroid Build Coastguard Worker.. _register allocator:
1195*9880d681SAndroid Build Coastguard Worker
1196*9880d681SAndroid Build Coastguard WorkerRegister Allocation
1197*9880d681SAndroid Build Coastguard Worker-------------------
1198*9880d681SAndroid Build Coastguard Worker
1199*9880d681SAndroid Build Coastguard WorkerThe *Register Allocation problem* consists in mapping a program
1200*9880d681SAndroid Build Coastguard Worker:raw-html:`<b><tt>` P\ :sub:`v`\ :raw-html:`</tt></b>`, that can use an unbounded
1201*9880d681SAndroid Build Coastguard Workernumber of virtual registers, to a program :raw-html:`<b><tt>` P\ :sub:`p`\
1202*9880d681SAndroid Build Coastguard Worker:raw-html:`</tt></b>` that contains a finite (possibly small) number of physical
1203*9880d681SAndroid Build Coastguard Workerregisters. Each target architecture has a different number of physical
1204*9880d681SAndroid Build Coastguard Workerregisters. If the number of physical registers is not enough to accommodate all
1205*9880d681SAndroid Build Coastguard Workerthe virtual registers, some of them will have to be mapped into memory. These
1206*9880d681SAndroid Build Coastguard Workervirtuals are called *spilled virtuals*.
1207*9880d681SAndroid Build Coastguard Worker
1208*9880d681SAndroid Build Coastguard WorkerHow registers are represented in LLVM
1209*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1210*9880d681SAndroid Build Coastguard Worker
1211*9880d681SAndroid Build Coastguard WorkerIn LLVM, physical registers are denoted by integer numbers that normally range
1212*9880d681SAndroid Build Coastguard Workerfrom 1 to 1023. To see how this numbering is defined for a particular
1213*9880d681SAndroid Build Coastguard Workerarchitecture, you can read the ``GenRegisterNames.inc`` file for that
1214*9880d681SAndroid Build Coastguard Workerarchitecture. For instance, by inspecting
1215*9880d681SAndroid Build Coastguard Worker``lib/Target/X86/X86GenRegisterInfo.inc`` we see that the 32-bit register
1216*9880d681SAndroid Build Coastguard Worker``EAX`` is denoted by 43, and the MMX register ``MM0`` is mapped to 65.
1217*9880d681SAndroid Build Coastguard Worker
1218*9880d681SAndroid Build Coastguard WorkerSome architectures contain registers that share the same physical location. A
1219*9880d681SAndroid Build Coastguard Workernotable example is the X86 platform. For instance, in the X86 architecture, the
1220*9880d681SAndroid Build Coastguard Workerregisters ``EAX``, ``AX`` and ``AL`` share the first eight bits. These physical
1221*9880d681SAndroid Build Coastguard Workerregisters are marked as *aliased* in LLVM. Given a particular architecture, you
1222*9880d681SAndroid Build Coastguard Workercan check which registers are aliased by inspecting its ``RegisterInfo.td``
1223*9880d681SAndroid Build Coastguard Workerfile. Moreover, the class ``MCRegAliasIterator`` enumerates all the physical
1224*9880d681SAndroid Build Coastguard Workerregisters aliased to a register.
1225*9880d681SAndroid Build Coastguard Worker
1226*9880d681SAndroid Build Coastguard WorkerPhysical registers, in LLVM, are grouped in *Register Classes*.  Elements in the
1227*9880d681SAndroid Build Coastguard Workersame register class are functionally equivalent, and can be interchangeably
1228*9880d681SAndroid Build Coastguard Workerused. Each virtual register can only be mapped to physical registers of a
1229*9880d681SAndroid Build Coastguard Workerparticular class. For instance, in the X86 architecture, some virtuals can only
1230*9880d681SAndroid Build Coastguard Workerbe allocated to 8 bit registers.  A register class is described by
1231*9880d681SAndroid Build Coastguard Worker``TargetRegisterClass`` objects.  To discover if a virtual register is
1232*9880d681SAndroid Build Coastguard Workercompatible with a given physical, this code can be used:
1233*9880d681SAndroid Build Coastguard Worker
1234*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1235*9880d681SAndroid Build Coastguard Worker
1236*9880d681SAndroid Build Coastguard Worker  bool RegMapping_Fer::compatible_class(MachineFunction &mf,
1237*9880d681SAndroid Build Coastguard Worker                                        unsigned v_reg,
1238*9880d681SAndroid Build Coastguard Worker                                        unsigned p_reg) {
1239*9880d681SAndroid Build Coastguard Worker    assert(TargetRegisterInfo::isPhysicalRegister(p_reg) &&
1240*9880d681SAndroid Build Coastguard Worker           "Target register must be physical");
1241*9880d681SAndroid Build Coastguard Worker    const TargetRegisterClass *trc = mf.getRegInfo().getRegClass(v_reg);
1242*9880d681SAndroid Build Coastguard Worker    return trc->contains(p_reg);
1243*9880d681SAndroid Build Coastguard Worker  }
1244*9880d681SAndroid Build Coastguard Worker
1245*9880d681SAndroid Build Coastguard WorkerSometimes, mostly for debugging purposes, it is useful to change the number of
1246*9880d681SAndroid Build Coastguard Workerphysical registers available in the target architecture. This must be done
1247*9880d681SAndroid Build Coastguard Workerstatically, inside the ``TargetRegsterInfo.td`` file. Just ``grep`` for
1248*9880d681SAndroid Build Coastguard Worker``RegisterClass``, the last parameter of which is a list of registers. Just
1249*9880d681SAndroid Build Coastguard Workercommenting some out is one simple way to avoid them being used. A more polite
1250*9880d681SAndroid Build Coastguard Workerway is to explicitly exclude some registers from the *allocation order*. See the
1251*9880d681SAndroid Build Coastguard Workerdefinition of the ``GR8`` register class in
1252*9880d681SAndroid Build Coastguard Worker``lib/Target/X86/X86RegisterInfo.td`` for an example of this.
1253*9880d681SAndroid Build Coastguard Worker
1254*9880d681SAndroid Build Coastguard WorkerVirtual registers are also denoted by integer numbers. Contrary to physical
1255*9880d681SAndroid Build Coastguard Workerregisters, different virtual registers never share the same number. Whereas
1256*9880d681SAndroid Build Coastguard Workerphysical registers are statically defined in a ``TargetRegisterInfo.td`` file
1257*9880d681SAndroid Build Coastguard Workerand cannot be created by the application developer, that is not the case with
1258*9880d681SAndroid Build Coastguard Workervirtual registers. In order to create new virtual registers, use the method
1259*9880d681SAndroid Build Coastguard Worker``MachineRegisterInfo::createVirtualRegister()``. This method will return a new
1260*9880d681SAndroid Build Coastguard Workervirtual register. Use an ``IndexedMap<Foo, VirtReg2IndexFunctor>`` to hold
1261*9880d681SAndroid Build Coastguard Workerinformation per virtual register. If you need to enumerate all virtual
1262*9880d681SAndroid Build Coastguard Workerregisters, use the function ``TargetRegisterInfo::index2VirtReg()`` to find the
1263*9880d681SAndroid Build Coastguard Workervirtual register numbers:
1264*9880d681SAndroid Build Coastguard Worker
1265*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1266*9880d681SAndroid Build Coastguard Worker
1267*9880d681SAndroid Build Coastguard Worker    for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1268*9880d681SAndroid Build Coastguard Worker      unsigned VirtReg = TargetRegisterInfo::index2VirtReg(i);
1269*9880d681SAndroid Build Coastguard Worker      stuff(VirtReg);
1270*9880d681SAndroid Build Coastguard Worker    }
1271*9880d681SAndroid Build Coastguard Worker
1272*9880d681SAndroid Build Coastguard WorkerBefore register allocation, the operands of an instruction are mostly virtual
1273*9880d681SAndroid Build Coastguard Workerregisters, although physical registers may also be used. In order to check if a
1274*9880d681SAndroid Build Coastguard Workergiven machine operand is a register, use the boolean function
1275*9880d681SAndroid Build Coastguard Worker``MachineOperand::isRegister()``. To obtain the integer code of a register, use
1276*9880d681SAndroid Build Coastguard Worker``MachineOperand::getReg()``. An instruction may define or use a register. For
1277*9880d681SAndroid Build Coastguard Workerinstance, ``ADD reg:1026 := reg:1025 reg:1024`` defines the registers 1024, and
1278*9880d681SAndroid Build Coastguard Workeruses registers 1025 and 1026. Given a register operand, the method
1279*9880d681SAndroid Build Coastguard Worker``MachineOperand::isUse()`` informs if that register is being used by the
1280*9880d681SAndroid Build Coastguard Workerinstruction. The method ``MachineOperand::isDef()`` informs if that registers is
1281*9880d681SAndroid Build Coastguard Workerbeing defined.
1282*9880d681SAndroid Build Coastguard Worker
1283*9880d681SAndroid Build Coastguard WorkerWe will call physical registers present in the LLVM bitcode before register
1284*9880d681SAndroid Build Coastguard Workerallocation *pre-colored registers*. Pre-colored registers are used in many
1285*9880d681SAndroid Build Coastguard Workerdifferent situations, for instance, to pass parameters of functions calls, and
1286*9880d681SAndroid Build Coastguard Workerto store results of particular instructions. There are two types of pre-colored
1287*9880d681SAndroid Build Coastguard Workerregisters: the ones *implicitly* defined, and those *explicitly*
1288*9880d681SAndroid Build Coastguard Workerdefined. Explicitly defined registers are normal operands, and can be accessed
1289*9880d681SAndroid Build Coastguard Workerwith ``MachineInstr::getOperand(int)::getReg()``.  In order to check which
1290*9880d681SAndroid Build Coastguard Workerregisters are implicitly defined by an instruction, use the
1291*9880d681SAndroid Build Coastguard Worker``TargetInstrInfo::get(opcode)::ImplicitDefs``, where ``opcode`` is the opcode
1292*9880d681SAndroid Build Coastguard Workerof the target instruction. One important difference between explicit and
1293*9880d681SAndroid Build Coastguard Workerimplicit physical registers is that the latter are defined statically for each
1294*9880d681SAndroid Build Coastguard Workerinstruction, whereas the former may vary depending on the program being
1295*9880d681SAndroid Build Coastguard Workercompiled. For example, an instruction that represents a function call will
1296*9880d681SAndroid Build Coastguard Workeralways implicitly define or use the same set of physical registers. To read the
1297*9880d681SAndroid Build Coastguard Workerregisters implicitly used by an instruction, use
1298*9880d681SAndroid Build Coastguard Worker``TargetInstrInfo::get(opcode)::ImplicitUses``. Pre-colored registers impose
1299*9880d681SAndroid Build Coastguard Workerconstraints on any register allocation algorithm. The register allocator must
1300*9880d681SAndroid Build Coastguard Workermake sure that none of them are overwritten by the values of virtual registers
1301*9880d681SAndroid Build Coastguard Workerwhile still alive.
1302*9880d681SAndroid Build Coastguard Worker
1303*9880d681SAndroid Build Coastguard WorkerMapping virtual registers to physical registers
1304*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1305*9880d681SAndroid Build Coastguard Worker
1306*9880d681SAndroid Build Coastguard WorkerThere are two ways to map virtual registers to physical registers (or to memory
1307*9880d681SAndroid Build Coastguard Workerslots). The first way, that we will call *direct mapping*, is based on the use
1308*9880d681SAndroid Build Coastguard Workerof methods of the classes ``TargetRegisterInfo``, and ``MachineOperand``. The
1309*9880d681SAndroid Build Coastguard Workersecond way, that we will call *indirect mapping*, relies on the ``VirtRegMap``
1310*9880d681SAndroid Build Coastguard Workerclass in order to insert loads and stores sending and getting values to and from
1311*9880d681SAndroid Build Coastguard Workermemory.
1312*9880d681SAndroid Build Coastguard Worker
1313*9880d681SAndroid Build Coastguard WorkerThe direct mapping provides more flexibility to the developer of the register
1314*9880d681SAndroid Build Coastguard Workerallocator; however, it is more error prone, and demands more implementation
1315*9880d681SAndroid Build Coastguard Workerwork.  Basically, the programmer will have to specify where load and store
1316*9880d681SAndroid Build Coastguard Workerinstructions should be inserted in the target function being compiled in order
1317*9880d681SAndroid Build Coastguard Workerto get and store values in memory. To assign a physical register to a virtual
1318*9880d681SAndroid Build Coastguard Workerregister present in a given operand, use ``MachineOperand::setReg(p_reg)``. To
1319*9880d681SAndroid Build Coastguard Workerinsert a store instruction, use ``TargetInstrInfo::storeRegToStackSlot(...)``,
1320*9880d681SAndroid Build Coastguard Workerand to insert a load instruction, use ``TargetInstrInfo::loadRegFromStackSlot``.
1321*9880d681SAndroid Build Coastguard Worker
1322*9880d681SAndroid Build Coastguard WorkerThe indirect mapping shields the application developer from the complexities of
1323*9880d681SAndroid Build Coastguard Workerinserting load and store instructions. In order to map a virtual register to a
1324*9880d681SAndroid Build Coastguard Workerphysical one, use ``VirtRegMap::assignVirt2Phys(vreg, preg)``.  In order to map
1325*9880d681SAndroid Build Coastguard Workera certain virtual register to memory, use
1326*9880d681SAndroid Build Coastguard Worker``VirtRegMap::assignVirt2StackSlot(vreg)``. This method will return the stack
1327*9880d681SAndroid Build Coastguard Workerslot where ``vreg``'s value will be located.  If it is necessary to map another
1328*9880d681SAndroid Build Coastguard Workervirtual register to the same stack slot, use
1329*9880d681SAndroid Build Coastguard Worker``VirtRegMap::assignVirt2StackSlot(vreg, stack_location)``. One important point
1330*9880d681SAndroid Build Coastguard Workerto consider when using the indirect mapping, is that even if a virtual register
1331*9880d681SAndroid Build Coastguard Workeris mapped to memory, it still needs to be mapped to a physical register. This
1332*9880d681SAndroid Build Coastguard Workerphysical register is the location where the virtual register is supposed to be
1333*9880d681SAndroid Build Coastguard Workerfound before being stored or after being reloaded.
1334*9880d681SAndroid Build Coastguard Worker
1335*9880d681SAndroid Build Coastguard WorkerIf the indirect strategy is used, after all the virtual registers have been
1336*9880d681SAndroid Build Coastguard Workermapped to physical registers or stack slots, it is necessary to use a spiller
1337*9880d681SAndroid Build Coastguard Workerobject to place load and store instructions in the code. Every virtual that has
1338*9880d681SAndroid Build Coastguard Workerbeen mapped to a stack slot will be stored to memory after being defined and will
1339*9880d681SAndroid Build Coastguard Workerbe loaded before being used. The implementation of the spiller tries to recycle
1340*9880d681SAndroid Build Coastguard Workerload/store instructions, avoiding unnecessary instructions. For an example of
1341*9880d681SAndroid Build Coastguard Workerhow to invoke the spiller, see ``RegAllocLinearScan::runOnMachineFunction`` in
1342*9880d681SAndroid Build Coastguard Worker``lib/CodeGen/RegAllocLinearScan.cpp``.
1343*9880d681SAndroid Build Coastguard Worker
1344*9880d681SAndroid Build Coastguard WorkerHandling two address instructions
1345*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1346*9880d681SAndroid Build Coastguard Worker
1347*9880d681SAndroid Build Coastguard WorkerWith very rare exceptions (e.g., function calls), the LLVM machine code
1348*9880d681SAndroid Build Coastguard Workerinstructions are three address instructions. That is, each instruction is
1349*9880d681SAndroid Build Coastguard Workerexpected to define at most one register, and to use at most two registers.
1350*9880d681SAndroid Build Coastguard WorkerHowever, some architectures use two address instructions. In this case, the
1351*9880d681SAndroid Build Coastguard Workerdefined register is also one of the used registers. For instance, an instruction
1352*9880d681SAndroid Build Coastguard Workersuch as ``ADD %EAX, %EBX``, in X86 is actually equivalent to ``%EAX = %EAX +
1353*9880d681SAndroid Build Coastguard Worker%EBX``.
1354*9880d681SAndroid Build Coastguard Worker
1355*9880d681SAndroid Build Coastguard WorkerIn order to produce correct code, LLVM must convert three address instructions
1356*9880d681SAndroid Build Coastguard Workerthat represent two address instructions into true two address instructions. LLVM
1357*9880d681SAndroid Build Coastguard Workerprovides the pass ``TwoAddressInstructionPass`` for this specific purpose. It
1358*9880d681SAndroid Build Coastguard Workermust be run before register allocation takes place. After its execution, the
1359*9880d681SAndroid Build Coastguard Workerresulting code may no longer be in SSA form. This happens, for instance, in
1360*9880d681SAndroid Build Coastguard Workersituations where an instruction such as ``%a = ADD %b %c`` is converted to two
1361*9880d681SAndroid Build Coastguard Workerinstructions such as:
1362*9880d681SAndroid Build Coastguard Worker
1363*9880d681SAndroid Build Coastguard Worker::
1364*9880d681SAndroid Build Coastguard Worker
1365*9880d681SAndroid Build Coastguard Worker  %a = MOVE %b
1366*9880d681SAndroid Build Coastguard Worker  %a = ADD %a %c
1367*9880d681SAndroid Build Coastguard Worker
1368*9880d681SAndroid Build Coastguard WorkerNotice that, internally, the second instruction is represented as ``ADD
1369*9880d681SAndroid Build Coastguard Worker%a[def/use] %c``. I.e., the register operand ``%a`` is both used and defined by
1370*9880d681SAndroid Build Coastguard Workerthe instruction.
1371*9880d681SAndroid Build Coastguard Worker
1372*9880d681SAndroid Build Coastguard WorkerThe SSA deconstruction phase
1373*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1374*9880d681SAndroid Build Coastguard Worker
1375*9880d681SAndroid Build Coastguard WorkerAn important transformation that happens during register allocation is called
1376*9880d681SAndroid Build Coastguard Workerthe *SSA Deconstruction Phase*. The SSA form simplifies many analyses that are
1377*9880d681SAndroid Build Coastguard Workerperformed on the control flow graph of programs. However, traditional
1378*9880d681SAndroid Build Coastguard Workerinstruction sets do not implement PHI instructions. Thus, in order to generate
1379*9880d681SAndroid Build Coastguard Workerexecutable code, compilers must replace PHI instructions with other instructions
1380*9880d681SAndroid Build Coastguard Workerthat preserve their semantics.
1381*9880d681SAndroid Build Coastguard Worker
1382*9880d681SAndroid Build Coastguard WorkerThere are many ways in which PHI instructions can safely be removed from the
1383*9880d681SAndroid Build Coastguard Workertarget code. The most traditional PHI deconstruction algorithm replaces PHI
1384*9880d681SAndroid Build Coastguard Workerinstructions with copy instructions. That is the strategy adopted by LLVM. The
1385*9880d681SAndroid Build Coastguard WorkerSSA deconstruction algorithm is implemented in
1386*9880d681SAndroid Build Coastguard Worker``lib/CodeGen/PHIElimination.cpp``. In order to invoke this pass, the identifier
1387*9880d681SAndroid Build Coastguard Worker``PHIEliminationID`` must be marked as required in the code of the register
1388*9880d681SAndroid Build Coastguard Workerallocator.
1389*9880d681SAndroid Build Coastguard Worker
1390*9880d681SAndroid Build Coastguard WorkerInstruction folding
1391*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^
1392*9880d681SAndroid Build Coastguard Worker
1393*9880d681SAndroid Build Coastguard Worker*Instruction folding* is an optimization performed during register allocation
1394*9880d681SAndroid Build Coastguard Workerthat removes unnecessary copy instructions. For instance, a sequence of
1395*9880d681SAndroid Build Coastguard Workerinstructions such as:
1396*9880d681SAndroid Build Coastguard Worker
1397*9880d681SAndroid Build Coastguard Worker::
1398*9880d681SAndroid Build Coastguard Worker
1399*9880d681SAndroid Build Coastguard Worker  %EBX = LOAD %mem_address
1400*9880d681SAndroid Build Coastguard Worker  %EAX = COPY %EBX
1401*9880d681SAndroid Build Coastguard Worker
1402*9880d681SAndroid Build Coastguard Workercan be safely substituted by the single instruction:
1403*9880d681SAndroid Build Coastguard Worker
1404*9880d681SAndroid Build Coastguard Worker::
1405*9880d681SAndroid Build Coastguard Worker
1406*9880d681SAndroid Build Coastguard Worker  %EAX = LOAD %mem_address
1407*9880d681SAndroid Build Coastguard Worker
1408*9880d681SAndroid Build Coastguard WorkerInstructions can be folded with the
1409*9880d681SAndroid Build Coastguard Worker``TargetRegisterInfo::foldMemoryOperand(...)`` method. Care must be taken when
1410*9880d681SAndroid Build Coastguard Workerfolding instructions; a folded instruction can be quite different from the
1411*9880d681SAndroid Build Coastguard Workeroriginal instruction. See ``LiveIntervals::addIntervalsForSpills`` in
1412*9880d681SAndroid Build Coastguard Worker``lib/CodeGen/LiveIntervalAnalysis.cpp`` for an example of its use.
1413*9880d681SAndroid Build Coastguard Worker
1414*9880d681SAndroid Build Coastguard WorkerBuilt in register allocators
1415*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1416*9880d681SAndroid Build Coastguard Worker
1417*9880d681SAndroid Build Coastguard WorkerThe LLVM infrastructure provides the application developer with three different
1418*9880d681SAndroid Build Coastguard Workerregister allocators:
1419*9880d681SAndroid Build Coastguard Worker
1420*9880d681SAndroid Build Coastguard Worker* *Fast* --- This register allocator is the default for debug builds. It
1421*9880d681SAndroid Build Coastguard Worker  allocates registers on a basic block level, attempting to keep values in
1422*9880d681SAndroid Build Coastguard Worker  registers and reusing registers as appropriate.
1423*9880d681SAndroid Build Coastguard Worker
1424*9880d681SAndroid Build Coastguard Worker* *Basic* --- This is an incremental approach to register allocation. Live
1425*9880d681SAndroid Build Coastguard Worker  ranges are assigned to registers one at a time in an order that is driven by
1426*9880d681SAndroid Build Coastguard Worker  heuristics. Since code can be rewritten on-the-fly during allocation, this
1427*9880d681SAndroid Build Coastguard Worker  framework allows interesting allocators to be developed as extensions. It is
1428*9880d681SAndroid Build Coastguard Worker  not itself a production register allocator but is a potentially useful
1429*9880d681SAndroid Build Coastguard Worker  stand-alone mode for triaging bugs and as a performance baseline.
1430*9880d681SAndroid Build Coastguard Worker
1431*9880d681SAndroid Build Coastguard Worker* *Greedy* --- *The default allocator*. This is a highly tuned implementation of
1432*9880d681SAndroid Build Coastguard Worker  the *Basic* allocator that incorporates global live range splitting. This
1433*9880d681SAndroid Build Coastguard Worker  allocator works hard to minimize the cost of spill code.
1434*9880d681SAndroid Build Coastguard Worker
1435*9880d681SAndroid Build Coastguard Worker* *PBQP* --- A Partitioned Boolean Quadratic Programming (PBQP) based register
1436*9880d681SAndroid Build Coastguard Worker  allocator. This allocator works by constructing a PBQP problem representing
1437*9880d681SAndroid Build Coastguard Worker  the register allocation problem under consideration, solving this using a PBQP
1438*9880d681SAndroid Build Coastguard Worker  solver, and mapping the solution back to a register assignment.
1439*9880d681SAndroid Build Coastguard Worker
1440*9880d681SAndroid Build Coastguard WorkerThe type of register allocator used in ``llc`` can be chosen with the command
1441*9880d681SAndroid Build Coastguard Workerline option ``-regalloc=...``:
1442*9880d681SAndroid Build Coastguard Worker
1443*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
1444*9880d681SAndroid Build Coastguard Worker
1445*9880d681SAndroid Build Coastguard Worker  $ llc -regalloc=linearscan file.bc -o ln.s
1446*9880d681SAndroid Build Coastguard Worker  $ llc -regalloc=fast file.bc -o fa.s
1447*9880d681SAndroid Build Coastguard Worker  $ llc -regalloc=pbqp file.bc -o pbqp.s
1448*9880d681SAndroid Build Coastguard Worker
1449*9880d681SAndroid Build Coastguard Worker.. _Prolog/Epilog Code Insertion:
1450*9880d681SAndroid Build Coastguard Worker
1451*9880d681SAndroid Build Coastguard WorkerProlog/Epilog Code Insertion
1452*9880d681SAndroid Build Coastguard Worker----------------------------
1453*9880d681SAndroid Build Coastguard Worker
1454*9880d681SAndroid Build Coastguard WorkerCompact Unwind
1455*9880d681SAndroid Build Coastguard Worker
1456*9880d681SAndroid Build Coastguard WorkerThrowing an exception requires *unwinding* out of a function. The information on
1457*9880d681SAndroid Build Coastguard Workerhow to unwind a given function is traditionally expressed in DWARF unwind
1458*9880d681SAndroid Build Coastguard Worker(a.k.a. frame) info. But that format was originally developed for debuggers to
1459*9880d681SAndroid Build Coastguard Workerbacktrace, and each Frame Description Entry (FDE) requires ~20-30 bytes per
1460*9880d681SAndroid Build Coastguard Workerfunction. There is also the cost of mapping from an address in a function to the
1461*9880d681SAndroid Build Coastguard Workercorresponding FDE at runtime. An alternative unwind encoding is called *compact
1462*9880d681SAndroid Build Coastguard Workerunwind* and requires just 4-bytes per function.
1463*9880d681SAndroid Build Coastguard Worker
1464*9880d681SAndroid Build Coastguard WorkerThe compact unwind encoding is a 32-bit value, which is encoded in an
1465*9880d681SAndroid Build Coastguard Workerarchitecture-specific way. It specifies which registers to restore and from
1466*9880d681SAndroid Build Coastguard Workerwhere, and how to unwind out of the function. When the linker creates a final
1467*9880d681SAndroid Build Coastguard Workerlinked image, it will create a ``__TEXT,__unwind_info`` section. This section is
1468*9880d681SAndroid Build Coastguard Workera small and fast way for the runtime to access unwind info for any given
1469*9880d681SAndroid Build Coastguard Workerfunction. If we emit compact unwind info for the function, that compact unwind
1470*9880d681SAndroid Build Coastguard Workerinfo will be encoded in the ``__TEXT,__unwind_info`` section. If we emit DWARF
1471*9880d681SAndroid Build Coastguard Workerunwind info, the ``__TEXT,__unwind_info`` section will contain the offset of the
1472*9880d681SAndroid Build Coastguard WorkerFDE in the ``__TEXT,__eh_frame`` section in the final linked image.
1473*9880d681SAndroid Build Coastguard Worker
1474*9880d681SAndroid Build Coastguard WorkerFor X86, there are three modes for the compact unwind encoding:
1475*9880d681SAndroid Build Coastguard Worker
1476*9880d681SAndroid Build Coastguard Worker*Function with a Frame Pointer (``EBP`` or ``RBP``)*
1477*9880d681SAndroid Build Coastguard Worker  ``EBP/RBP``-based frame, where ``EBP/RBP`` is pushed onto the stack
1478*9880d681SAndroid Build Coastguard Worker  immediately after the return address, then ``ESP/RSP`` is moved to
1479*9880d681SAndroid Build Coastguard Worker  ``EBP/RBP``. Thus to unwind, ``ESP/RSP`` is restored with the current
1480*9880d681SAndroid Build Coastguard Worker  ``EBP/RBP`` value, then ``EBP/RBP`` is restored by popping the stack, and the
1481*9880d681SAndroid Build Coastguard Worker  return is done by popping the stack once more into the PC. All non-volatile
1482*9880d681SAndroid Build Coastguard Worker  registers that need to be restored must have been saved in a small range on
1483*9880d681SAndroid Build Coastguard Worker  the stack that starts ``EBP-4`` to ``EBP-1020`` (``RBP-8`` to
1484*9880d681SAndroid Build Coastguard Worker  ``RBP-1020``). The offset (divided by 4 in 32-bit mode and 8 in 64-bit mode)
1485*9880d681SAndroid Build Coastguard Worker  is encoded in bits 16-23 (mask: ``0x00FF0000``).  The registers saved are
1486*9880d681SAndroid Build Coastguard Worker  encoded in bits 0-14 (mask: ``0x00007FFF``) as five 3-bit entries from the
1487*9880d681SAndroid Build Coastguard Worker  following table:
1488*9880d681SAndroid Build Coastguard Worker
1489*9880d681SAndroid Build Coastguard Worker    ==============  =============  ===============
1490*9880d681SAndroid Build Coastguard Worker    Compact Number  i386 Register  x86-64 Register
1491*9880d681SAndroid Build Coastguard Worker    ==============  =============  ===============
1492*9880d681SAndroid Build Coastguard Worker    1               ``EBX``        ``RBX``
1493*9880d681SAndroid Build Coastguard Worker    2               ``ECX``        ``R12``
1494*9880d681SAndroid Build Coastguard Worker    3               ``EDX``        ``R13``
1495*9880d681SAndroid Build Coastguard Worker    4               ``EDI``        ``R14``
1496*9880d681SAndroid Build Coastguard Worker    5               ``ESI``        ``R15``
1497*9880d681SAndroid Build Coastguard Worker    6               ``EBP``        ``RBP``
1498*9880d681SAndroid Build Coastguard Worker    ==============  =============  ===============
1499*9880d681SAndroid Build Coastguard Worker
1500*9880d681SAndroid Build Coastguard Worker*Frameless with a Small Constant Stack Size (``EBP`` or ``RBP`` is not used as a frame pointer)*
1501*9880d681SAndroid Build Coastguard Worker  To return, a constant (encoded in the compact unwind encoding) is added to the
1502*9880d681SAndroid Build Coastguard Worker  ``ESP/RSP``.  Then the return is done by popping the stack into the PC. All
1503*9880d681SAndroid Build Coastguard Worker  non-volatile registers that need to be restored must have been saved on the
1504*9880d681SAndroid Build Coastguard Worker  stack immediately after the return address. The stack size (divided by 4 in
1505*9880d681SAndroid Build Coastguard Worker  32-bit mode and 8 in 64-bit mode) is encoded in bits 16-23 (mask:
1506*9880d681SAndroid Build Coastguard Worker  ``0x00FF0000``). There is a maximum stack size of 1024 bytes in 32-bit mode
1507*9880d681SAndroid Build Coastguard Worker  and 2048 in 64-bit mode. The number of registers saved is encoded in bits 9-12
1508*9880d681SAndroid Build Coastguard Worker  (mask: ``0x00001C00``). Bits 0-9 (mask: ``0x000003FF``) contain which
1509*9880d681SAndroid Build Coastguard Worker  registers were saved and their order. (See the
1510*9880d681SAndroid Build Coastguard Worker  ``encodeCompactUnwindRegistersWithoutFrame()`` function in
1511*9880d681SAndroid Build Coastguard Worker  ``lib/Target/X86FrameLowering.cpp`` for the encoding algorithm.)
1512*9880d681SAndroid Build Coastguard Worker
1513*9880d681SAndroid Build Coastguard Worker*Frameless with a Large Constant Stack Size (``EBP`` or ``RBP`` is not used as a frame pointer)*
1514*9880d681SAndroid Build Coastguard Worker  This case is like the "Frameless with a Small Constant Stack Size" case, but
1515*9880d681SAndroid Build Coastguard Worker  the stack size is too large to encode in the compact unwind encoding. Instead
1516*9880d681SAndroid Build Coastguard Worker  it requires that the function contains "``subl $nnnnnn, %esp``" in its
1517*9880d681SAndroid Build Coastguard Worker  prolog. The compact encoding contains the offset to the ``$nnnnnn`` value in
1518*9880d681SAndroid Build Coastguard Worker  the function in bits 9-12 (mask: ``0x00001C00``).
1519*9880d681SAndroid Build Coastguard Worker
1520*9880d681SAndroid Build Coastguard Worker.. _Late Machine Code Optimizations:
1521*9880d681SAndroid Build Coastguard Worker
1522*9880d681SAndroid Build Coastguard WorkerLate Machine Code Optimizations
1523*9880d681SAndroid Build Coastguard Worker-------------------------------
1524*9880d681SAndroid Build Coastguard Worker
1525*9880d681SAndroid Build Coastguard Worker.. note::
1526*9880d681SAndroid Build Coastguard Worker
1527*9880d681SAndroid Build Coastguard Worker  To Be Written
1528*9880d681SAndroid Build Coastguard Worker
1529*9880d681SAndroid Build Coastguard Worker.. _Code Emission:
1530*9880d681SAndroid Build Coastguard Worker
1531*9880d681SAndroid Build Coastguard WorkerCode Emission
1532*9880d681SAndroid Build Coastguard Worker-------------
1533*9880d681SAndroid Build Coastguard Worker
1534*9880d681SAndroid Build Coastguard WorkerThe code emission step of code generation is responsible for lowering from the
1535*9880d681SAndroid Build Coastguard Workercode generator abstractions (like `MachineFunction`_, `MachineInstr`_, etc) down
1536*9880d681SAndroid Build Coastguard Workerto the abstractions used by the MC layer (`MCInst`_, `MCStreamer`_, etc).  This
1537*9880d681SAndroid Build Coastguard Workeris done with a combination of several different classes: the (misnamed)
1538*9880d681SAndroid Build Coastguard Workertarget-independent AsmPrinter class, target-specific subclasses of AsmPrinter
1539*9880d681SAndroid Build Coastguard Worker(such as SparcAsmPrinter), and the TargetLoweringObjectFile class.
1540*9880d681SAndroid Build Coastguard Worker
1541*9880d681SAndroid Build Coastguard WorkerSince the MC layer works at the level of abstraction of object files, it doesn't
1542*9880d681SAndroid Build Coastguard Workerhave a notion of functions, global variables etc.  Instead, it thinks about
1543*9880d681SAndroid Build Coastguard Workerlabels, directives, and instructions.  A key class used at this time is the
1544*9880d681SAndroid Build Coastguard WorkerMCStreamer class.  This is an abstract API that is implemented in different ways
1545*9880d681SAndroid Build Coastguard Worker(e.g. to output a .s file, output an ELF .o file, etc) that is effectively an
1546*9880d681SAndroid Build Coastguard Worker"assembler API".  MCStreamer has one method per directive, such as EmitLabel,
1547*9880d681SAndroid Build Coastguard WorkerEmitSymbolAttribute, SwitchSection, etc, which directly correspond to assembly
1548*9880d681SAndroid Build Coastguard Workerlevel directives.
1549*9880d681SAndroid Build Coastguard Worker
1550*9880d681SAndroid Build Coastguard WorkerIf you are interested in implementing a code generator for a target, there are
1551*9880d681SAndroid Build Coastguard Workerthree important things that you have to implement for your target:
1552*9880d681SAndroid Build Coastguard Worker
1553*9880d681SAndroid Build Coastguard Worker#. First, you need a subclass of AsmPrinter for your target.  This class
1554*9880d681SAndroid Build Coastguard Worker   implements the general lowering process converting MachineFunction's into MC
1555*9880d681SAndroid Build Coastguard Worker   label constructs.  The AsmPrinter base class provides a number of useful
1556*9880d681SAndroid Build Coastguard Worker   methods and routines, and also allows you to override the lowering process in
1557*9880d681SAndroid Build Coastguard Worker   some important ways.  You should get much of the lowering for free if you are
1558*9880d681SAndroid Build Coastguard Worker   implementing an ELF, COFF, or MachO target, because the
1559*9880d681SAndroid Build Coastguard Worker   TargetLoweringObjectFile class implements much of the common logic.
1560*9880d681SAndroid Build Coastguard Worker
1561*9880d681SAndroid Build Coastguard Worker#. Second, you need to implement an instruction printer for your target.  The
1562*9880d681SAndroid Build Coastguard Worker   instruction printer takes an `MCInst`_ and renders it to a raw_ostream as
1563*9880d681SAndroid Build Coastguard Worker   text.  Most of this is automatically generated from the .td file (when you
1564*9880d681SAndroid Build Coastguard Worker   specify something like "``add $dst, $src1, $src2``" in the instructions), but
1565*9880d681SAndroid Build Coastguard Worker   you need to implement routines to print operands.
1566*9880d681SAndroid Build Coastguard Worker
1567*9880d681SAndroid Build Coastguard Worker#. Third, you need to implement code that lowers a `MachineInstr`_ to an MCInst,
1568*9880d681SAndroid Build Coastguard Worker   usually implemented in "<target>MCInstLower.cpp".  This lowering process is
1569*9880d681SAndroid Build Coastguard Worker   often target specific, and is responsible for turning jump table entries,
1570*9880d681SAndroid Build Coastguard Worker   constant pool indices, global variable addresses, etc into MCLabels as
1571*9880d681SAndroid Build Coastguard Worker   appropriate.  This translation layer is also responsible for expanding pseudo
1572*9880d681SAndroid Build Coastguard Worker   ops used by the code generator into the actual machine instructions they
1573*9880d681SAndroid Build Coastguard Worker   correspond to. The MCInsts that are generated by this are fed into the
1574*9880d681SAndroid Build Coastguard Worker   instruction printer or the encoder.
1575*9880d681SAndroid Build Coastguard Worker
1576*9880d681SAndroid Build Coastguard WorkerFinally, at your choosing, you can also implement a subclass of MCCodeEmitter
1577*9880d681SAndroid Build Coastguard Workerwhich lowers MCInst's into machine code bytes and relocations.  This is
1578*9880d681SAndroid Build Coastguard Workerimportant if you want to support direct .o file emission, or would like to
1579*9880d681SAndroid Build Coastguard Workerimplement an assembler for your target.
1580*9880d681SAndroid Build Coastguard Worker
1581*9880d681SAndroid Build Coastguard WorkerVLIW Packetizer
1582*9880d681SAndroid Build Coastguard Worker---------------
1583*9880d681SAndroid Build Coastguard Worker
1584*9880d681SAndroid Build Coastguard WorkerIn a Very Long Instruction Word (VLIW) architecture, the compiler is responsible
1585*9880d681SAndroid Build Coastguard Workerfor mapping instructions to functional-units available on the architecture. To
1586*9880d681SAndroid Build Coastguard Workerthat end, the compiler creates groups of instructions called *packets* or
1587*9880d681SAndroid Build Coastguard Worker*bundles*. The VLIW packetizer in LLVM is a target-independent mechanism to
1588*9880d681SAndroid Build Coastguard Workerenable the packetization of machine instructions.
1589*9880d681SAndroid Build Coastguard Worker
1590*9880d681SAndroid Build Coastguard WorkerMapping from instructions to functional units
1591*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1592*9880d681SAndroid Build Coastguard Worker
1593*9880d681SAndroid Build Coastguard WorkerInstructions in a VLIW target can typically be mapped to multiple functional
1594*9880d681SAndroid Build Coastguard Workerunits. During the process of packetizing, the compiler must be able to reason
1595*9880d681SAndroid Build Coastguard Workerabout whether an instruction can be added to a packet. This decision can be
1596*9880d681SAndroid Build Coastguard Workercomplex since the compiler has to examine all possible mappings of instructions
1597*9880d681SAndroid Build Coastguard Workerto functional units. Therefore to alleviate compilation-time complexity, the
1598*9880d681SAndroid Build Coastguard WorkerVLIW packetizer parses the instruction classes of a target and generates tables
1599*9880d681SAndroid Build Coastguard Workerat compiler build time. These tables can then be queried by the provided
1600*9880d681SAndroid Build Coastguard Workermachine-independent API to determine if an instruction can be accommodated in a
1601*9880d681SAndroid Build Coastguard Workerpacket.
1602*9880d681SAndroid Build Coastguard Worker
1603*9880d681SAndroid Build Coastguard WorkerHow the packetization tables are generated and used
1604*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1605*9880d681SAndroid Build Coastguard Worker
1606*9880d681SAndroid Build Coastguard WorkerThe packetizer reads instruction classes from a target's itineraries and creates
1607*9880d681SAndroid Build Coastguard Workera deterministic finite automaton (DFA) to represent the state of a packet. A DFA
1608*9880d681SAndroid Build Coastguard Workerconsists of three major elements: inputs, states, and transitions. The set of
1609*9880d681SAndroid Build Coastguard Workerinputs for the generated DFA represents the instruction being added to a
1610*9880d681SAndroid Build Coastguard Workerpacket. The states represent the possible consumption of functional units by
1611*9880d681SAndroid Build Coastguard Workerinstructions in a packet. In the DFA, transitions from one state to another
1612*9880d681SAndroid Build Coastguard Workeroccur on the addition of an instruction to an existing packet. If there is a
1613*9880d681SAndroid Build Coastguard Workerlegal mapping of functional units to instructions, then the DFA contains a
1614*9880d681SAndroid Build Coastguard Workercorresponding transition. The absence of a transition indicates that a legal
1615*9880d681SAndroid Build Coastguard Workermapping does not exist and that the instruction cannot be added to the packet.
1616*9880d681SAndroid Build Coastguard Worker
1617*9880d681SAndroid Build Coastguard WorkerTo generate tables for a VLIW target, add *Target*\ GenDFAPacketizer.inc as a
1618*9880d681SAndroid Build Coastguard Workertarget to the Makefile in the target directory. The exported API provides three
1619*9880d681SAndroid Build Coastguard Workerfunctions: ``DFAPacketizer::clearResources()``,
1620*9880d681SAndroid Build Coastguard Worker``DFAPacketizer::reserveResources(MachineInstr *MI)``, and
1621*9880d681SAndroid Build Coastguard Worker``DFAPacketizer::canReserveResources(MachineInstr *MI)``. These functions allow
1622*9880d681SAndroid Build Coastguard Workera target packetizer to add an instruction to an existing packet and to check
1623*9880d681SAndroid Build Coastguard Workerwhether an instruction can be added to a packet. See
1624*9880d681SAndroid Build Coastguard Worker``llvm/CodeGen/DFAPacketizer.h`` for more information.
1625*9880d681SAndroid Build Coastguard Worker
1626*9880d681SAndroid Build Coastguard WorkerImplementing a Native Assembler
1627*9880d681SAndroid Build Coastguard Worker===============================
1628*9880d681SAndroid Build Coastguard Worker
1629*9880d681SAndroid Build Coastguard WorkerThough you're probably reading this because you want to write or maintain a
1630*9880d681SAndroid Build Coastguard Workercompiler backend, LLVM also fully supports building a native assembler.
1631*9880d681SAndroid Build Coastguard WorkerWe've tried hard to automate the generation of the assembler from the .td files
1632*9880d681SAndroid Build Coastguard Worker(in particular the instruction syntax and encodings), which means that a large
1633*9880d681SAndroid Build Coastguard Workerpart of the manual and repetitive data entry can be factored and shared with the
1634*9880d681SAndroid Build Coastguard Workercompiler.
1635*9880d681SAndroid Build Coastguard Worker
1636*9880d681SAndroid Build Coastguard WorkerInstruction Parsing
1637*9880d681SAndroid Build Coastguard Worker-------------------
1638*9880d681SAndroid Build Coastguard Worker
1639*9880d681SAndroid Build Coastguard Worker.. note::
1640*9880d681SAndroid Build Coastguard Worker
1641*9880d681SAndroid Build Coastguard Worker  To Be Written
1642*9880d681SAndroid Build Coastguard Worker
1643*9880d681SAndroid Build Coastguard Worker
1644*9880d681SAndroid Build Coastguard WorkerInstruction Alias Processing
1645*9880d681SAndroid Build Coastguard Worker----------------------------
1646*9880d681SAndroid Build Coastguard Worker
1647*9880d681SAndroid Build Coastguard WorkerOnce the instruction is parsed, it enters the MatchInstructionImpl function.
1648*9880d681SAndroid Build Coastguard WorkerThe MatchInstructionImpl function performs alias processing and then does actual
1649*9880d681SAndroid Build Coastguard Workermatching.
1650*9880d681SAndroid Build Coastguard Worker
1651*9880d681SAndroid Build Coastguard WorkerAlias processing is the phase that canonicalizes different lexical forms of the
1652*9880d681SAndroid Build Coastguard Workersame instructions down to one representation.  There are several different kinds
1653*9880d681SAndroid Build Coastguard Workerof alias that are possible to implement and they are listed below in the order
1654*9880d681SAndroid Build Coastguard Workerthat they are processed (which is in order from simplest/weakest to most
1655*9880d681SAndroid Build Coastguard Workercomplex/powerful).  Generally you want to use the first alias mechanism that
1656*9880d681SAndroid Build Coastguard Workermeets the needs of your instruction, because it will allow a more concise
1657*9880d681SAndroid Build Coastguard Workerdescription.
1658*9880d681SAndroid Build Coastguard Worker
1659*9880d681SAndroid Build Coastguard WorkerMnemonic Aliases
1660*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^
1661*9880d681SAndroid Build Coastguard Worker
1662*9880d681SAndroid Build Coastguard WorkerThe first phase of alias processing is simple instruction mnemonic remapping for
1663*9880d681SAndroid Build Coastguard Workerclasses of instructions which are allowed with two different mnemonics.  This
1664*9880d681SAndroid Build Coastguard Workerphase is a simple and unconditionally remapping from one input mnemonic to one
1665*9880d681SAndroid Build Coastguard Workeroutput mnemonic.  It isn't possible for this form of alias to look at the
1666*9880d681SAndroid Build Coastguard Workeroperands at all, so the remapping must apply for all forms of a given mnemonic.
1667*9880d681SAndroid Build Coastguard WorkerMnemonic aliases are defined simply, for example X86 has:
1668*9880d681SAndroid Build Coastguard Worker
1669*9880d681SAndroid Build Coastguard Worker::
1670*9880d681SAndroid Build Coastguard Worker
1671*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"cbw",     "cbtw">;
1672*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"smovq",   "movsq">;
1673*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"fldcww",  "fldcw">;
1674*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"fucompi", "fucomip">;
1675*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"ud2a",    "ud2">;
1676*9880d681SAndroid Build Coastguard Worker
1677*9880d681SAndroid Build Coastguard Worker... and many others.  With a MnemonicAlias definition, the mnemonic is remapped
1678*9880d681SAndroid Build Coastguard Workersimply and directly.  Though MnemonicAlias's can't look at any aspect of the
1679*9880d681SAndroid Build Coastguard Workerinstruction (such as the operands) they can depend on global modes (the same
1680*9880d681SAndroid Build Coastguard Workerones supported by the matcher), through a Requires clause:
1681*9880d681SAndroid Build Coastguard Worker
1682*9880d681SAndroid Build Coastguard Worker::
1683*9880d681SAndroid Build Coastguard Worker
1684*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"pushf", "pushfq">, Requires<[In64BitMode]>;
1685*9880d681SAndroid Build Coastguard Worker  def : MnemonicAlias<"pushf", "pushfl">, Requires<[In32BitMode]>;
1686*9880d681SAndroid Build Coastguard Worker
1687*9880d681SAndroid Build Coastguard WorkerIn this example, the mnemonic gets mapped into a different one depending on
1688*9880d681SAndroid Build Coastguard Workerthe current instruction set.
1689*9880d681SAndroid Build Coastguard Worker
1690*9880d681SAndroid Build Coastguard WorkerInstruction Aliases
1691*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^
1692*9880d681SAndroid Build Coastguard Worker
1693*9880d681SAndroid Build Coastguard WorkerThe most general phase of alias processing occurs while matching is happening:
1694*9880d681SAndroid Build Coastguard Workerit provides new forms for the matcher to match along with a specific instruction
1695*9880d681SAndroid Build Coastguard Workerto generate.  An instruction alias has two parts: the string to match and the
1696*9880d681SAndroid Build Coastguard Workerinstruction to generate.  For example:
1697*9880d681SAndroid Build Coastguard Worker
1698*9880d681SAndroid Build Coastguard Worker::
1699*9880d681SAndroid Build Coastguard Worker
1700*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX16rr8W GR16:$dst, GR8  :$src)>;
1701*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX16rm8W GR16:$dst, i8mem:$src)>;
1702*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX32rr8  GR32:$dst, GR8  :$src)>;
1703*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX32rr16 GR32:$dst, GR16 :$src)>;
1704*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX64rr8  GR64:$dst, GR8  :$src)>;
1705*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX64rr16 GR64:$dst, GR16 :$src)>;
1706*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"movsx $src, $dst", (MOVSX64rr32 GR64:$dst, GR32 :$src)>;
1707*9880d681SAndroid Build Coastguard Worker
1708*9880d681SAndroid Build Coastguard WorkerThis shows a powerful example of the instruction aliases, matching the same
1709*9880d681SAndroid Build Coastguard Workermnemonic in multiple different ways depending on what operands are present in
1710*9880d681SAndroid Build Coastguard Workerthe assembly.  The result of instruction aliases can include operands in a
1711*9880d681SAndroid Build Coastguard Workerdifferent order than the destination instruction, and can use an input multiple
1712*9880d681SAndroid Build Coastguard Workertimes, for example:
1713*9880d681SAndroid Build Coastguard Worker
1714*9880d681SAndroid Build Coastguard Worker::
1715*9880d681SAndroid Build Coastguard Worker
1716*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"clrb $reg", (XOR8rr  GR8 :$reg, GR8 :$reg)>;
1717*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"clrw $reg", (XOR16rr GR16:$reg, GR16:$reg)>;
1718*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"clrl $reg", (XOR32rr GR32:$reg, GR32:$reg)>;
1719*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"clrq $reg", (XOR64rr GR64:$reg, GR64:$reg)>;
1720*9880d681SAndroid Build Coastguard Worker
1721*9880d681SAndroid Build Coastguard WorkerThis example also shows that tied operands are only listed once.  In the X86
1722*9880d681SAndroid Build Coastguard Workerbackend, XOR8rr has two input GR8's and one output GR8 (where an input is tied
1723*9880d681SAndroid Build Coastguard Workerto the output).  InstAliases take a flattened operand list without duplicates
1724*9880d681SAndroid Build Coastguard Workerfor tied operands.  The result of an instruction alias can also use immediates
1725*9880d681SAndroid Build Coastguard Workerand fixed physical registers which are added as simple immediate operands in the
1726*9880d681SAndroid Build Coastguard Workerresult, for example:
1727*9880d681SAndroid Build Coastguard Worker
1728*9880d681SAndroid Build Coastguard Worker::
1729*9880d681SAndroid Build Coastguard Worker
1730*9880d681SAndroid Build Coastguard Worker  // Fixed Immediate operand.
1731*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"aad", (AAD8i8 10)>;
1732*9880d681SAndroid Build Coastguard Worker
1733*9880d681SAndroid Build Coastguard Worker  // Fixed register operand.
1734*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"fcomi", (COM_FIr ST1)>;
1735*9880d681SAndroid Build Coastguard Worker
1736*9880d681SAndroid Build Coastguard Worker  // Simple alias.
1737*9880d681SAndroid Build Coastguard Worker  def : InstAlias<"fcomi $reg", (COM_FIr RST:$reg)>;
1738*9880d681SAndroid Build Coastguard Worker
1739*9880d681SAndroid Build Coastguard WorkerInstruction aliases can also have a Requires clause to make them subtarget
1740*9880d681SAndroid Build Coastguard Workerspecific.
1741*9880d681SAndroid Build Coastguard Worker
1742*9880d681SAndroid Build Coastguard WorkerIf the back-end supports it, the instruction printer can automatically emit the
1743*9880d681SAndroid Build Coastguard Workeralias rather than what's being aliased. It typically leads to better, more
1744*9880d681SAndroid Build Coastguard Workerreadable code. If it's better to print out what's being aliased, then pass a '0'
1745*9880d681SAndroid Build Coastguard Workeras the third parameter to the InstAlias definition.
1746*9880d681SAndroid Build Coastguard Worker
1747*9880d681SAndroid Build Coastguard WorkerInstruction Matching
1748*9880d681SAndroid Build Coastguard Worker--------------------
1749*9880d681SAndroid Build Coastguard Worker
1750*9880d681SAndroid Build Coastguard Worker.. note::
1751*9880d681SAndroid Build Coastguard Worker
1752*9880d681SAndroid Build Coastguard Worker  To Be Written
1753*9880d681SAndroid Build Coastguard Worker
1754*9880d681SAndroid Build Coastguard Worker.. _Implementations of the abstract target description interfaces:
1755*9880d681SAndroid Build Coastguard Worker.. _implement the target description:
1756*9880d681SAndroid Build Coastguard Worker
1757*9880d681SAndroid Build Coastguard WorkerTarget-specific Implementation Notes
1758*9880d681SAndroid Build Coastguard Worker====================================
1759*9880d681SAndroid Build Coastguard Worker
1760*9880d681SAndroid Build Coastguard WorkerThis section of the document explains features or design decisions that are
1761*9880d681SAndroid Build Coastguard Workerspecific to the code generator for a particular target.  First we start with a
1762*9880d681SAndroid Build Coastguard Workertable that summarizes what features are supported by each target.
1763*9880d681SAndroid Build Coastguard Worker
1764*9880d681SAndroid Build Coastguard Worker.. _target-feature-matrix:
1765*9880d681SAndroid Build Coastguard Worker
1766*9880d681SAndroid Build Coastguard WorkerTarget Feature Matrix
1767*9880d681SAndroid Build Coastguard Worker---------------------
1768*9880d681SAndroid Build Coastguard Worker
1769*9880d681SAndroid Build Coastguard WorkerNote that this table does not list features that are not supported fully by any
1770*9880d681SAndroid Build Coastguard Workertarget yet.  It considers a feature to be supported if at least one subtarget
1771*9880d681SAndroid Build Coastguard Workersupports it.  A feature being supported means that it is useful and works for
1772*9880d681SAndroid Build Coastguard Workermost cases, it does not indicate that there are zero known bugs in the
1773*9880d681SAndroid Build Coastguard Workerimplementation.  Here is the key:
1774*9880d681SAndroid Build Coastguard Worker
1775*9880d681SAndroid Build Coastguard Worker:raw-html:`<table border="1" cellspacing="0">`
1776*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1777*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Unknown</th>`
1778*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Not Applicable</th>`
1779*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>No support</th>`
1780*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Partial Support</th>`
1781*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Complete Support</th>`
1782*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1783*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1784*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td>`
1785*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="na"></td>`
1786*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td>`
1787*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="partial"></td>`
1788*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td>`
1789*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1790*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
1791*9880d681SAndroid Build Coastguard Worker
1792*9880d681SAndroid Build Coastguard WorkerHere is the table:
1793*9880d681SAndroid Build Coastguard Worker
1794*9880d681SAndroid Build Coastguard Worker:raw-html:`<table width="689" border="1" cellspacing="0">`
1795*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr><td></td>`
1796*9880d681SAndroid Build Coastguard Worker:raw-html:`<td colspan="13" align="center" style="background-color:#ffc">Target</td>`
1797*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1798*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1799*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Feature</th>`
1800*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>ARM</th>`
1801*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Hexagon</th>`
1802*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>MSP430</th>`
1803*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Mips</th>`
1804*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>NVPTX</th>`
1805*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>PowerPC</th>`
1806*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Sparc</th>`
1807*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>SystemZ</th>`
1808*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>X86</th>`
1809*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>XCore</th>`
1810*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>eBPF</th>`
1811*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1812*9880d681SAndroid Build Coastguard Worker
1813*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1814*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_reliable">is generally reliable</a></td>`
1815*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- ARM -->`
1816*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Hexagon -->`
1817*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- MSP430 -->`
1818*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Mips -->`
1819*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- NVPTX -->`
1820*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- PowerPC -->`
1821*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Sparc -->`
1822*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1823*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1824*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- XCore -->`
1825*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- eBPF -->`
1826*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1827*9880d681SAndroid Build Coastguard Worker
1828*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1829*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_asmparser">assembly parser</a></td>`
1830*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- ARM -->`
1831*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Hexagon -->`
1832*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- MSP430 -->`
1833*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1834*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- NVPTX -->`
1835*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- PowerPC -->`
1836*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Sparc -->`
1837*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1838*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1839*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- XCore -->`
1840*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- eBPF -->`
1841*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1842*9880d681SAndroid Build Coastguard Worker
1843*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1844*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_disassembler">disassembler</a></td>`
1845*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- ARM -->`
1846*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Hexagon -->`
1847*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- MSP430 -->`
1848*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1849*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="na"></td> <!-- NVPTX -->`
1850*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- PowerPC -->`
1851*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1852*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Sparc -->`
1853*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1854*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- XCore -->`
1855*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- eBPF -->`
1856*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1857*9880d681SAndroid Build Coastguard Worker
1858*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1859*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_inlineasm">inline asm</a></td>`
1860*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- ARM -->`
1861*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Hexagon -->`
1862*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- MSP430 -->`
1863*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1864*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- NVPTX -->`
1865*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- PowerPC -->`
1866*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- Sparc -->`
1867*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1868*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1869*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- XCore -->`
1870*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- eBPF -->`
1871*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1872*9880d681SAndroid Build Coastguard Worker
1873*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1874*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_jit">jit</a></td>`
1875*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="partial"><a href="#feat_jit_arm">*</a></td> <!-- ARM -->`
1876*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Hexagon -->`
1877*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- MSP430 -->`
1878*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Mips -->`
1879*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="na"></td> <!-- NVPTX -->`
1880*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- PowerPC -->`
1881*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- Sparc -->`
1882*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1883*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1884*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- XCore -->`
1885*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- eBPF -->`
1886*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1887*9880d681SAndroid Build Coastguard Worker
1888*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1889*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_objectwrite">.o&nbsp;file writing</a></td>`
1890*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- ARM -->`
1891*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Hexagon -->`
1892*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- MSP430 -->`
1893*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1894*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="na"></td> <!-- NVPTX -->`
1895*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- PowerPC -->`
1896*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Sparc -->`
1897*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- SystemZ -->`
1898*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1899*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- XCore -->`
1900*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- eBPF -->`
1901*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1902*9880d681SAndroid Build Coastguard Worker
1903*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1904*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a hr:raw-html:`ef="#feat_tailcall">tail calls</a></td>`
1905*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- ARM -->`
1906*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- Hexagon -->`
1907*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- MSP430 -->`
1908*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1909*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- NVPTX -->`
1910*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- PowerPC -->`
1911*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="unknown"></td> <!-- Sparc -->`
1912*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- SystemZ -->`
1913*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="yes"></td> <!-- X86 -->`
1914*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- XCore -->`
1915*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- eBPF -->`
1916*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1917*9880d681SAndroid Build Coastguard Worker
1918*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
1919*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><a href="#feat_segstacks">segmented stacks</a></td>`
1920*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- ARM -->`
1921*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Hexagon -->`
1922*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- MSP430 -->`
1923*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Mips -->`
1924*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- NVPTX -->`
1925*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- PowerPC -->`
1926*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- Sparc -->`
1927*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- SystemZ -->`
1928*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="partial"><a href="#feat_segstacks_x86">*</a></td> <!-- X86 -->`
1929*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- XCore -->`
1930*9880d681SAndroid Build Coastguard Worker:raw-html:`<td class="no"></td> <!-- eBPF -->`
1931*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
1932*9880d681SAndroid Build Coastguard Worker
1933*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
1934*9880d681SAndroid Build Coastguard Worker
1935*9880d681SAndroid Build Coastguard Worker.. _feat_reliable:
1936*9880d681SAndroid Build Coastguard Worker
1937*9880d681SAndroid Build Coastguard WorkerIs Generally Reliable
1938*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^
1939*9880d681SAndroid Build Coastguard Worker
1940*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target is considered to be production quality.
1941*9880d681SAndroid Build Coastguard WorkerThis indicates that the target has been used as a static compiler to compile
1942*9880d681SAndroid Build Coastguard Workerlarge amounts of code by a variety of different people and is in continuous use.
1943*9880d681SAndroid Build Coastguard Worker
1944*9880d681SAndroid Build Coastguard Worker.. _feat_asmparser:
1945*9880d681SAndroid Build Coastguard Worker
1946*9880d681SAndroid Build Coastguard WorkerAssembly Parser
1947*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^
1948*9880d681SAndroid Build Coastguard Worker
1949*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports parsing target specific .s files
1950*9880d681SAndroid Build Coastguard Workerby implementing the MCAsmParser interface.  This is required for llvm-mc to be
1951*9880d681SAndroid Build Coastguard Workerable to act as a native assembler and is required for inline assembly support in
1952*9880d681SAndroid Build Coastguard Workerthe native .o file writer.
1953*9880d681SAndroid Build Coastguard Worker
1954*9880d681SAndroid Build Coastguard Worker.. _feat_disassembler:
1955*9880d681SAndroid Build Coastguard Worker
1956*9880d681SAndroid Build Coastguard WorkerDisassembler
1957*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^
1958*9880d681SAndroid Build Coastguard Worker
1959*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports the MCDisassembler API for
1960*9880d681SAndroid Build Coastguard Workerdisassembling machine opcode bytes into MCInst's.
1961*9880d681SAndroid Build Coastguard Worker
1962*9880d681SAndroid Build Coastguard Worker.. _feat_inlineasm:
1963*9880d681SAndroid Build Coastguard Worker
1964*9880d681SAndroid Build Coastguard WorkerInline Asm
1965*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^
1966*9880d681SAndroid Build Coastguard Worker
1967*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports most popular inline assembly
1968*9880d681SAndroid Build Coastguard Workerconstraints and modifiers.
1969*9880d681SAndroid Build Coastguard Worker
1970*9880d681SAndroid Build Coastguard Worker.. _feat_jit:
1971*9880d681SAndroid Build Coastguard Worker
1972*9880d681SAndroid Build Coastguard WorkerJIT Support
1973*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^
1974*9880d681SAndroid Build Coastguard Worker
1975*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports the JIT compiler through the
1976*9880d681SAndroid Build Coastguard WorkerExecutionEngine interface.
1977*9880d681SAndroid Build Coastguard Worker
1978*9880d681SAndroid Build Coastguard Worker.. _feat_jit_arm:
1979*9880d681SAndroid Build Coastguard Worker
1980*9880d681SAndroid Build Coastguard WorkerThe ARM backend has basic support for integer code in ARM codegen mode, but
1981*9880d681SAndroid Build Coastguard Workerlacks NEON and full Thumb support.
1982*9880d681SAndroid Build Coastguard Worker
1983*9880d681SAndroid Build Coastguard Worker.. _feat_objectwrite:
1984*9880d681SAndroid Build Coastguard Worker
1985*9880d681SAndroid Build Coastguard Worker.o File Writing
1986*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^
1987*9880d681SAndroid Build Coastguard Worker
1988*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports writing .o files (e.g. MachO,
1989*9880d681SAndroid Build Coastguard WorkerELF, and/or COFF) files directly from the target.  Note that the target also
1990*9880d681SAndroid Build Coastguard Workermust include an assembly parser and general inline assembly support for full
1991*9880d681SAndroid Build Coastguard Workerinline assembly support in the .o writer.
1992*9880d681SAndroid Build Coastguard Worker
1993*9880d681SAndroid Build Coastguard WorkerTargets that don't support this feature can obviously still write out .o files,
1994*9880d681SAndroid Build Coastguard Workerthey just rely on having an external assembler to translate from a .s file to a
1995*9880d681SAndroid Build Coastguard Worker.o file (as is the case for many C compilers).
1996*9880d681SAndroid Build Coastguard Worker
1997*9880d681SAndroid Build Coastguard Worker.. _feat_tailcall:
1998*9880d681SAndroid Build Coastguard Worker
1999*9880d681SAndroid Build Coastguard WorkerTail Calls
2000*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^
2001*9880d681SAndroid Build Coastguard Worker
2002*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports guaranteed tail calls.  These are
2003*9880d681SAndroid Build Coastguard Workercalls marked "`tail <LangRef.html#i_call>`_" and use the fastcc calling
2004*9880d681SAndroid Build Coastguard Workerconvention.  Please see the `tail call section`_ for more details.
2005*9880d681SAndroid Build Coastguard Worker
2006*9880d681SAndroid Build Coastguard Worker.. _feat_segstacks:
2007*9880d681SAndroid Build Coastguard Worker
2008*9880d681SAndroid Build Coastguard WorkerSegmented Stacks
2009*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^
2010*9880d681SAndroid Build Coastguard Worker
2011*9880d681SAndroid Build Coastguard WorkerThis box indicates whether the target supports segmented stacks. This replaces
2012*9880d681SAndroid Build Coastguard Workerthe traditional large C stack with many linked segments. It is compatible with
2013*9880d681SAndroid Build Coastguard Workerthe `gcc implementation <http://gcc.gnu.org/wiki/SplitStacks>`_ used by the Go
2014*9880d681SAndroid Build Coastguard Workerfront end.
2015*9880d681SAndroid Build Coastguard Worker
2016*9880d681SAndroid Build Coastguard Worker.. _feat_segstacks_x86:
2017*9880d681SAndroid Build Coastguard Worker
2018*9880d681SAndroid Build Coastguard WorkerBasic support exists on the X86 backend. Currently vararg doesn't work and the
2019*9880d681SAndroid Build Coastguard Workerobject files are not marked the way the gold linker expects, but simple Go
2020*9880d681SAndroid Build Coastguard Workerprograms can be built by dragonegg.
2021*9880d681SAndroid Build Coastguard Worker
2022*9880d681SAndroid Build Coastguard Worker.. _tail call section:
2023*9880d681SAndroid Build Coastguard Worker
2024*9880d681SAndroid Build Coastguard WorkerTail call optimization
2025*9880d681SAndroid Build Coastguard Worker----------------------
2026*9880d681SAndroid Build Coastguard Worker
2027*9880d681SAndroid Build Coastguard WorkerTail call optimization, callee reusing the stack of the caller, is currently
2028*9880d681SAndroid Build Coastguard Workersupported on x86/x86-64 and PowerPC. It is performed if:
2029*9880d681SAndroid Build Coastguard Worker
2030*9880d681SAndroid Build Coastguard Worker* Caller and callee have the calling convention ``fastcc``, ``cc 10`` (GHC
2031*9880d681SAndroid Build Coastguard Worker  calling convention) or ``cc 11`` (HiPE calling convention).
2032*9880d681SAndroid Build Coastguard Worker
2033*9880d681SAndroid Build Coastguard Worker* The call is a tail call - in tail position (ret immediately follows call and
2034*9880d681SAndroid Build Coastguard Worker  ret uses value of call or is void).
2035*9880d681SAndroid Build Coastguard Worker
2036*9880d681SAndroid Build Coastguard Worker* Option ``-tailcallopt`` is enabled.
2037*9880d681SAndroid Build Coastguard Worker
2038*9880d681SAndroid Build Coastguard Worker* Platform-specific constraints are met.
2039*9880d681SAndroid Build Coastguard Worker
2040*9880d681SAndroid Build Coastguard Workerx86/x86-64 constraints:
2041*9880d681SAndroid Build Coastguard Worker
2042*9880d681SAndroid Build Coastguard Worker* No variable argument lists are used.
2043*9880d681SAndroid Build Coastguard Worker
2044*9880d681SAndroid Build Coastguard Worker* On x86-64 when generating GOT/PIC code only module-local calls (visibility =
2045*9880d681SAndroid Build Coastguard Worker  hidden or protected) are supported.
2046*9880d681SAndroid Build Coastguard Worker
2047*9880d681SAndroid Build Coastguard WorkerPowerPC constraints:
2048*9880d681SAndroid Build Coastguard Worker
2049*9880d681SAndroid Build Coastguard Worker* No variable argument lists are used.
2050*9880d681SAndroid Build Coastguard Worker
2051*9880d681SAndroid Build Coastguard Worker* No byval parameters are used.
2052*9880d681SAndroid Build Coastguard Worker
2053*9880d681SAndroid Build Coastguard Worker* On ppc32/64 GOT/PIC only module-local calls (visibility = hidden or protected)
2054*9880d681SAndroid Build Coastguard Worker  are supported.
2055*9880d681SAndroid Build Coastguard Worker
2056*9880d681SAndroid Build Coastguard WorkerExample:
2057*9880d681SAndroid Build Coastguard Worker
2058*9880d681SAndroid Build Coastguard WorkerCall as ``llc -tailcallopt test.ll``.
2059*9880d681SAndroid Build Coastguard Worker
2060*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
2061*9880d681SAndroid Build Coastguard Worker
2062*9880d681SAndroid Build Coastguard Worker  declare fastcc i32 @tailcallee(i32 inreg %a1, i32 inreg %a2, i32 %a3, i32 %a4)
2063*9880d681SAndroid Build Coastguard Worker
2064*9880d681SAndroid Build Coastguard Worker  define fastcc i32 @tailcaller(i32 %in1, i32 %in2) {
2065*9880d681SAndroid Build Coastguard Worker    %l1 = add i32 %in1, %in2
2066*9880d681SAndroid Build Coastguard Worker    %tmp = tail call fastcc i32 @tailcallee(i32 %in1 inreg, i32 %in2 inreg, i32 %in1, i32 %l1)
2067*9880d681SAndroid Build Coastguard Worker    ret i32 %tmp
2068*9880d681SAndroid Build Coastguard Worker  }
2069*9880d681SAndroid Build Coastguard Worker
2070*9880d681SAndroid Build Coastguard WorkerImplications of ``-tailcallopt``:
2071*9880d681SAndroid Build Coastguard Worker
2072*9880d681SAndroid Build Coastguard WorkerTo support tail call optimization in situations where the callee has more
2073*9880d681SAndroid Build Coastguard Workerarguments than the caller a 'callee pops arguments' convention is used. This
2074*9880d681SAndroid Build Coastguard Workercurrently causes each ``fastcc`` call that is not tail call optimized (because
2075*9880d681SAndroid Build Coastguard Workerone or more of above constraints are not met) to be followed by a readjustment
2076*9880d681SAndroid Build Coastguard Workerof the stack. So performance might be worse in such cases.
2077*9880d681SAndroid Build Coastguard Worker
2078*9880d681SAndroid Build Coastguard WorkerSibling call optimization
2079*9880d681SAndroid Build Coastguard Worker-------------------------
2080*9880d681SAndroid Build Coastguard Worker
2081*9880d681SAndroid Build Coastguard WorkerSibling call optimization is a restricted form of tail call optimization.
2082*9880d681SAndroid Build Coastguard WorkerUnlike tail call optimization described in the previous section, it can be
2083*9880d681SAndroid Build Coastguard Workerperformed automatically on any tail calls when ``-tailcallopt`` option is not
2084*9880d681SAndroid Build Coastguard Workerspecified.
2085*9880d681SAndroid Build Coastguard Worker
2086*9880d681SAndroid Build Coastguard WorkerSibling call optimization is currently performed on x86/x86-64 when the
2087*9880d681SAndroid Build Coastguard Workerfollowing constraints are met:
2088*9880d681SAndroid Build Coastguard Worker
2089*9880d681SAndroid Build Coastguard Worker* Caller and callee have the same calling convention. It can be either ``c`` or
2090*9880d681SAndroid Build Coastguard Worker  ``fastcc``.
2091*9880d681SAndroid Build Coastguard Worker
2092*9880d681SAndroid Build Coastguard Worker* The call is a tail call - in tail position (ret immediately follows call and
2093*9880d681SAndroid Build Coastguard Worker  ret uses value of call or is void).
2094*9880d681SAndroid Build Coastguard Worker
2095*9880d681SAndroid Build Coastguard Worker* Caller and callee have matching return type or the callee result is not used.
2096*9880d681SAndroid Build Coastguard Worker
2097*9880d681SAndroid Build Coastguard Worker* If any of the callee arguments are being passed in stack, they must be
2098*9880d681SAndroid Build Coastguard Worker  available in caller's own incoming argument stack and the frame offsets must
2099*9880d681SAndroid Build Coastguard Worker  be the same.
2100*9880d681SAndroid Build Coastguard Worker
2101*9880d681SAndroid Build Coastguard WorkerExample:
2102*9880d681SAndroid Build Coastguard Worker
2103*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
2104*9880d681SAndroid Build Coastguard Worker
2105*9880d681SAndroid Build Coastguard Worker  declare i32 @bar(i32, i32)
2106*9880d681SAndroid Build Coastguard Worker
2107*9880d681SAndroid Build Coastguard Worker  define i32 @foo(i32 %a, i32 %b, i32 %c) {
2108*9880d681SAndroid Build Coastguard Worker  entry:
2109*9880d681SAndroid Build Coastguard Worker    %0 = tail call i32 @bar(i32 %a, i32 %b)
2110*9880d681SAndroid Build Coastguard Worker    ret i32 %0
2111*9880d681SAndroid Build Coastguard Worker  }
2112*9880d681SAndroid Build Coastguard Worker
2113*9880d681SAndroid Build Coastguard WorkerThe X86 backend
2114*9880d681SAndroid Build Coastguard Worker---------------
2115*9880d681SAndroid Build Coastguard Worker
2116*9880d681SAndroid Build Coastguard WorkerThe X86 code generator lives in the ``lib/Target/X86`` directory.  This code
2117*9880d681SAndroid Build Coastguard Workergenerator is capable of targeting a variety of x86-32 and x86-64 processors, and
2118*9880d681SAndroid Build Coastguard Workerincludes support for ISA extensions such as MMX and SSE.
2119*9880d681SAndroid Build Coastguard Worker
2120*9880d681SAndroid Build Coastguard WorkerX86 Target Triples supported
2121*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2122*9880d681SAndroid Build Coastguard Worker
2123*9880d681SAndroid Build Coastguard WorkerThe following are the known target triples that are supported by the X86
2124*9880d681SAndroid Build Coastguard Workerbackend.  This is not an exhaustive list, and it would be useful to add those
2125*9880d681SAndroid Build Coastguard Workerthat people test.
2126*9880d681SAndroid Build Coastguard Worker
2127*9880d681SAndroid Build Coastguard Worker* **i686-pc-linux-gnu** --- Linux
2128*9880d681SAndroid Build Coastguard Worker
2129*9880d681SAndroid Build Coastguard Worker* **i386-unknown-freebsd5.3** --- FreeBSD 5.3
2130*9880d681SAndroid Build Coastguard Worker
2131*9880d681SAndroid Build Coastguard Worker* **i686-pc-cygwin** --- Cygwin on Win32
2132*9880d681SAndroid Build Coastguard Worker
2133*9880d681SAndroid Build Coastguard Worker* **i686-pc-mingw32** --- MingW on Win32
2134*9880d681SAndroid Build Coastguard Worker
2135*9880d681SAndroid Build Coastguard Worker* **i386-pc-mingw32msvc** --- MingW crosscompiler on Linux
2136*9880d681SAndroid Build Coastguard Worker
2137*9880d681SAndroid Build Coastguard Worker* **i686-apple-darwin*** --- Apple Darwin on X86
2138*9880d681SAndroid Build Coastguard Worker
2139*9880d681SAndroid Build Coastguard Worker* **x86_64-unknown-linux-gnu** --- Linux
2140*9880d681SAndroid Build Coastguard Worker
2141*9880d681SAndroid Build Coastguard WorkerX86 Calling Conventions supported
2142*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2143*9880d681SAndroid Build Coastguard Worker
2144*9880d681SAndroid Build Coastguard WorkerThe following target-specific calling conventions are known to backend:
2145*9880d681SAndroid Build Coastguard Worker
2146*9880d681SAndroid Build Coastguard Worker* **x86_StdCall** --- stdcall calling convention seen on Microsoft Windows
2147*9880d681SAndroid Build Coastguard Worker  platform (CC ID = 64).
2148*9880d681SAndroid Build Coastguard Worker
2149*9880d681SAndroid Build Coastguard Worker* **x86_FastCall** --- fastcall calling convention seen on Microsoft Windows
2150*9880d681SAndroid Build Coastguard Worker  platform (CC ID = 65).
2151*9880d681SAndroid Build Coastguard Worker
2152*9880d681SAndroid Build Coastguard Worker* **x86_ThisCall** --- Similar to X86_StdCall. Passes first argument in ECX,
2153*9880d681SAndroid Build Coastguard Worker  others via stack. Callee is responsible for stack cleaning. This convention is
2154*9880d681SAndroid Build Coastguard Worker  used by MSVC by default for methods in its ABI (CC ID = 70).
2155*9880d681SAndroid Build Coastguard Worker
2156*9880d681SAndroid Build Coastguard Worker.. _X86 addressing mode:
2157*9880d681SAndroid Build Coastguard Worker
2158*9880d681SAndroid Build Coastguard WorkerRepresenting X86 addressing modes in MachineInstrs
2159*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2160*9880d681SAndroid Build Coastguard Worker
2161*9880d681SAndroid Build Coastguard WorkerThe x86 has a very flexible way of accessing memory.  It is capable of forming
2162*9880d681SAndroid Build Coastguard Workermemory addresses of the following expression directly in integer instructions
2163*9880d681SAndroid Build Coastguard Worker(which use ModR/M addressing):
2164*9880d681SAndroid Build Coastguard Worker
2165*9880d681SAndroid Build Coastguard Worker::
2166*9880d681SAndroid Build Coastguard Worker
2167*9880d681SAndroid Build Coastguard Worker  SegmentReg: Base + [1,2,4,8] * IndexReg + Disp32
2168*9880d681SAndroid Build Coastguard Worker
2169*9880d681SAndroid Build Coastguard WorkerIn order to represent this, LLVM tracks no less than 5 operands for each memory
2170*9880d681SAndroid Build Coastguard Workeroperand of this form.  This means that the "load" form of '``mov``' has the
2171*9880d681SAndroid Build Coastguard Workerfollowing ``MachineOperand``\s in this order:
2172*9880d681SAndroid Build Coastguard Worker
2173*9880d681SAndroid Build Coastguard Worker::
2174*9880d681SAndroid Build Coastguard Worker
2175*9880d681SAndroid Build Coastguard Worker  Index:        0     |    1        2       3           4          5
2176*9880d681SAndroid Build Coastguard Worker  Meaning:   DestReg, | BaseReg,  Scale, IndexReg, Displacement Segment
2177*9880d681SAndroid Build Coastguard Worker  OperandTy: VirtReg, | VirtReg, UnsImm, VirtReg,   SignExtImm  PhysReg
2178*9880d681SAndroid Build Coastguard Worker
2179*9880d681SAndroid Build Coastguard WorkerStores, and all other instructions, treat the four memory operands in the same
2180*9880d681SAndroid Build Coastguard Workerway and in the same order.  If the segment register is unspecified (regno = 0),
2181*9880d681SAndroid Build Coastguard Workerthen no segment override is generated.  "Lea" operations do not have a segment
2182*9880d681SAndroid Build Coastguard Workerregister specified, so they only have 4 operands for their memory reference.
2183*9880d681SAndroid Build Coastguard Worker
2184*9880d681SAndroid Build Coastguard WorkerX86 address spaces supported
2185*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2186*9880d681SAndroid Build Coastguard Worker
2187*9880d681SAndroid Build Coastguard Workerx86 has a feature which provides the ability to perform loads and stores to
2188*9880d681SAndroid Build Coastguard Workerdifferent address spaces via the x86 segment registers.  A segment override
2189*9880d681SAndroid Build Coastguard Workerprefix byte on an instruction causes the instruction's memory access to go to
2190*9880d681SAndroid Build Coastguard Workerthe specified segment.  LLVM address space 0 is the default address space, which
2191*9880d681SAndroid Build Coastguard Workerincludes the stack, and any unqualified memory accesses in a program.  Address
2192*9880d681SAndroid Build Coastguard Workerspaces 1-255 are currently reserved for user-defined code.  The GS-segment is
2193*9880d681SAndroid Build Coastguard Workerrepresented by address space 256, the FS-segment is represented by address space
2194*9880d681SAndroid Build Coastguard Worker257, and the SS-segment is represented by address space 258. Other x86 segments
2195*9880d681SAndroid Build Coastguard Workerhave yet to be allocated address space numbers.
2196*9880d681SAndroid Build Coastguard Worker
2197*9880d681SAndroid Build Coastguard WorkerWhile these address spaces may seem similar to TLS via the ``thread_local``
2198*9880d681SAndroid Build Coastguard Workerkeyword, and often use the same underlying hardware, there are some fundamental
2199*9880d681SAndroid Build Coastguard Workerdifferences.
2200*9880d681SAndroid Build Coastguard Worker
2201*9880d681SAndroid Build Coastguard WorkerThe ``thread_local`` keyword applies to global variables and specifies that they
2202*9880d681SAndroid Build Coastguard Workerare to be allocated in thread-local memory. There are no type qualifiers
2203*9880d681SAndroid Build Coastguard Workerinvolved, and these variables can be pointed to with normal pointers and
2204*9880d681SAndroid Build Coastguard Workeraccessed with normal loads and stores.  The ``thread_local`` keyword is
2205*9880d681SAndroid Build Coastguard Workertarget-independent at the LLVM IR level (though LLVM doesn't yet have
2206*9880d681SAndroid Build Coastguard Workerimplementations of it for some configurations)
2207*9880d681SAndroid Build Coastguard Worker
2208*9880d681SAndroid Build Coastguard WorkerSpecial address spaces, in contrast, apply to static types. Every load and store
2209*9880d681SAndroid Build Coastguard Workerhas a particular address space in its address operand type, and this is what
2210*9880d681SAndroid Build Coastguard Workerdetermines which address space is accessed.  LLVM ignores these special address
2211*9880d681SAndroid Build Coastguard Workerspace qualifiers on global variables, and does not provide a way to directly
2212*9880d681SAndroid Build Coastguard Workerallocate storage in them.  At the LLVM IR level, the behavior of these special
2213*9880d681SAndroid Build Coastguard Workeraddress spaces depends in part on the underlying OS or runtime environment, and
2214*9880d681SAndroid Build Coastguard Workerthey are specific to x86 (and LLVM doesn't yet handle them correctly in some
2215*9880d681SAndroid Build Coastguard Workercases).
2216*9880d681SAndroid Build Coastguard Worker
2217*9880d681SAndroid Build Coastguard WorkerSome operating systems and runtime environments use (or may in the future use)
2218*9880d681SAndroid Build Coastguard Workerthe FS/GS-segment registers for various low-level purposes, so care should be
2219*9880d681SAndroid Build Coastguard Workertaken when considering them.
2220*9880d681SAndroid Build Coastguard Worker
2221*9880d681SAndroid Build Coastguard WorkerInstruction naming
2222*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^
2223*9880d681SAndroid Build Coastguard Worker
2224*9880d681SAndroid Build Coastguard WorkerAn instruction name consists of the base name, a default operand size, and a a
2225*9880d681SAndroid Build Coastguard Workercharacter per operand with an optional special size. For example:
2226*9880d681SAndroid Build Coastguard Worker
2227*9880d681SAndroid Build Coastguard Worker::
2228*9880d681SAndroid Build Coastguard Worker
2229*9880d681SAndroid Build Coastguard Worker  ADD8rr      -> add, 8-bit register, 8-bit register
2230*9880d681SAndroid Build Coastguard Worker  IMUL16rmi   -> imul, 16-bit register, 16-bit memory, 16-bit immediate
2231*9880d681SAndroid Build Coastguard Worker  IMUL16rmi8  -> imul, 16-bit register, 16-bit memory, 8-bit immediate
2232*9880d681SAndroid Build Coastguard Worker  MOVSX32rm16 -> movsx, 32-bit register, 16-bit memory
2233*9880d681SAndroid Build Coastguard Worker
2234*9880d681SAndroid Build Coastguard WorkerThe PowerPC backend
2235*9880d681SAndroid Build Coastguard Worker-------------------
2236*9880d681SAndroid Build Coastguard Worker
2237*9880d681SAndroid Build Coastguard WorkerThe PowerPC code generator lives in the lib/Target/PowerPC directory.  The code
2238*9880d681SAndroid Build Coastguard Workergeneration is retargetable to several variations or *subtargets* of the PowerPC
2239*9880d681SAndroid Build Coastguard WorkerISA; including ppc32, ppc64 and altivec.
2240*9880d681SAndroid Build Coastguard Worker
2241*9880d681SAndroid Build Coastguard WorkerLLVM PowerPC ABI
2242*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^
2243*9880d681SAndroid Build Coastguard Worker
2244*9880d681SAndroid Build Coastguard WorkerLLVM follows the AIX PowerPC ABI, with two deviations. LLVM uses a PC relative
2245*9880d681SAndroid Build Coastguard Worker(PIC) or static addressing for accessing global values, so no TOC (r2) is
2246*9880d681SAndroid Build Coastguard Workerused. Second, r31 is used as a frame pointer to allow dynamic growth of a stack
2247*9880d681SAndroid Build Coastguard Workerframe.  LLVM takes advantage of having no TOC to provide space to save the frame
2248*9880d681SAndroid Build Coastguard Workerpointer in the PowerPC linkage area of the caller frame.  Other details of
2249*9880d681SAndroid Build Coastguard WorkerPowerPC ABI can be found at `PowerPC ABI
2250*9880d681SAndroid Build Coastguard Worker<http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/32bitPowerPC.html>`_\
2251*9880d681SAndroid Build Coastguard Worker. Note: This link describes the 32 bit ABI.  The 64 bit ABI is similar except
2252*9880d681SAndroid Build Coastguard Workerspace for GPRs are 8 bytes wide (not 4) and r13 is reserved for system use.
2253*9880d681SAndroid Build Coastguard Worker
2254*9880d681SAndroid Build Coastguard WorkerFrame Layout
2255*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^
2256*9880d681SAndroid Build Coastguard Worker
2257*9880d681SAndroid Build Coastguard WorkerThe size of a PowerPC frame is usually fixed for the duration of a function's
2258*9880d681SAndroid Build Coastguard Workerinvocation.  Since the frame is fixed size, all references into the frame can be
2259*9880d681SAndroid Build Coastguard Workeraccessed via fixed offsets from the stack pointer.  The exception to this is
2260*9880d681SAndroid Build Coastguard Workerwhen dynamic alloca or variable sized arrays are present, then a base pointer
2261*9880d681SAndroid Build Coastguard Worker(r31) is used as a proxy for the stack pointer and stack pointer is free to grow
2262*9880d681SAndroid Build Coastguard Workeror shrink.  A base pointer is also used if llvm-gcc is not passed the
2263*9880d681SAndroid Build Coastguard Worker-fomit-frame-pointer flag. The stack pointer is always aligned to 16 bytes, so
2264*9880d681SAndroid Build Coastguard Workerthat space allocated for altivec vectors will be properly aligned.
2265*9880d681SAndroid Build Coastguard Worker
2266*9880d681SAndroid Build Coastguard WorkerAn invocation frame is laid out as follows (low memory at top):
2267*9880d681SAndroid Build Coastguard Worker
2268*9880d681SAndroid Build Coastguard Worker:raw-html:`<table border="1" cellspacing="0">`
2269*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2270*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Linkage<br><br></td>`
2271*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2272*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2273*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Parameter area<br><br></td>`
2274*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2275*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2276*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Dynamic area<br><br></td>`
2277*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2278*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2279*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Locals area<br><br></td>`
2280*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2281*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2282*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved registers area<br><br></td>`
2283*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2284*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr style="border-style: none hidden none hidden;">`
2285*9880d681SAndroid Build Coastguard Worker:raw-html:`<td><br></td>`
2286*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2287*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2288*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Previous Frame<br><br></td>`
2289*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2290*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
2291*9880d681SAndroid Build Coastguard Worker
2292*9880d681SAndroid Build Coastguard WorkerThe *linkage* area is used by a callee to save special registers prior to
2293*9880d681SAndroid Build Coastguard Workerallocating its own frame.  Only three entries are relevant to LLVM. The first
2294*9880d681SAndroid Build Coastguard Workerentry is the previous stack pointer (sp), aka link.  This allows probing tools
2295*9880d681SAndroid Build Coastguard Workerlike gdb or exception handlers to quickly scan the frames in the stack.  A
2296*9880d681SAndroid Build Coastguard Workerfunction epilog can also use the link to pop the frame from the stack.  The
2297*9880d681SAndroid Build Coastguard Workerthird entry in the linkage area is used to save the return address from the lr
2298*9880d681SAndroid Build Coastguard Workerregister. Finally, as mentioned above, the last entry is used to save the
2299*9880d681SAndroid Build Coastguard Workerprevious frame pointer (r31.)  The entries in the linkage area are the size of a
2300*9880d681SAndroid Build Coastguard WorkerGPR, thus the linkage area is 24 bytes long in 32 bit mode and 48 bytes in 64
2301*9880d681SAndroid Build Coastguard Workerbit mode.
2302*9880d681SAndroid Build Coastguard Worker
2303*9880d681SAndroid Build Coastguard Worker32 bit linkage area:
2304*9880d681SAndroid Build Coastguard Worker
2305*9880d681SAndroid Build Coastguard Worker:raw-html:`<table  border="1" cellspacing="0">`
2306*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2307*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>0</td>`
2308*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved SP (r1)</td>`
2309*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2310*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2311*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>4</td>`
2312*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved CR</td>`
2313*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2314*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2315*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>8</td>`
2316*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved LR</td>`
2317*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2318*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2319*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>12</td>`
2320*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Reserved</td>`
2321*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2322*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2323*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>16</td>`
2324*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Reserved</td>`
2325*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2326*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2327*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>20</td>`
2328*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved FP (r31)</td>`
2329*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2330*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
2331*9880d681SAndroid Build Coastguard Worker
2332*9880d681SAndroid Build Coastguard Worker64 bit linkage area:
2333*9880d681SAndroid Build Coastguard Worker
2334*9880d681SAndroid Build Coastguard Worker:raw-html:`<table border="1" cellspacing="0">`
2335*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2336*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>0</td>`
2337*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved SP (r1)</td>`
2338*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2339*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2340*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>8</td>`
2341*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved CR</td>`
2342*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2343*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2344*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>16</td>`
2345*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved LR</td>`
2346*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2347*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2348*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>24</td>`
2349*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Reserved</td>`
2350*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2351*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2352*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>32</td>`
2353*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Reserved</td>`
2354*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2355*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2356*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>40</td>`
2357*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>Saved FP (r31)</td>`
2358*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2359*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
2360*9880d681SAndroid Build Coastguard Worker
2361*9880d681SAndroid Build Coastguard WorkerThe *parameter area* is used to store arguments being passed to a callee
2362*9880d681SAndroid Build Coastguard Workerfunction.  Following the PowerPC ABI, the first few arguments are actually
2363*9880d681SAndroid Build Coastguard Workerpassed in registers, with the space in the parameter area unused.  However, if
2364*9880d681SAndroid Build Coastguard Workerthere are not enough registers or the callee is a thunk or vararg function,
2365*9880d681SAndroid Build Coastguard Workerthese register arguments can be spilled into the parameter area.  Thus, the
2366*9880d681SAndroid Build Coastguard Workerparameter area must be large enough to store all the parameters for the largest
2367*9880d681SAndroid Build Coastguard Workercall sequence made by the caller.  The size must also be minimally large enough
2368*9880d681SAndroid Build Coastguard Workerto spill registers r3-r10.  This allows callees blind to the call signature,
2369*9880d681SAndroid Build Coastguard Workersuch as thunks and vararg functions, enough space to cache the argument
2370*9880d681SAndroid Build Coastguard Workerregisters.  Therefore, the parameter area is minimally 32 bytes (64 bytes in 64
2371*9880d681SAndroid Build Coastguard Workerbit mode.)  Also note that since the parameter area is a fixed offset from the
2372*9880d681SAndroid Build Coastguard Workertop of the frame, that a callee can access its spilt arguments using fixed
2373*9880d681SAndroid Build Coastguard Workeroffsets from the stack pointer (or base pointer.)
2374*9880d681SAndroid Build Coastguard Worker
2375*9880d681SAndroid Build Coastguard WorkerCombining the information about the linkage, parameter areas and alignment. A
2376*9880d681SAndroid Build Coastguard Workerstack frame is minimally 64 bytes in 32 bit mode and 128 bytes in 64 bit mode.
2377*9880d681SAndroid Build Coastguard Worker
2378*9880d681SAndroid Build Coastguard WorkerThe *dynamic area* starts out as size zero.  If a function uses dynamic alloca
2379*9880d681SAndroid Build Coastguard Workerthen space is added to the stack, the linkage and parameter areas are shifted to
2380*9880d681SAndroid Build Coastguard Workertop of stack, and the new space is available immediately below the linkage and
2381*9880d681SAndroid Build Coastguard Workerparameter areas.  The cost of shifting the linkage and parameter areas is minor
2382*9880d681SAndroid Build Coastguard Workersince only the link value needs to be copied.  The link value can be easily
2383*9880d681SAndroid Build Coastguard Workerfetched by adding the original frame size to the base pointer.  Note that
2384*9880d681SAndroid Build Coastguard Workerallocations in the dynamic space need to observe 16 byte alignment.
2385*9880d681SAndroid Build Coastguard Worker
2386*9880d681SAndroid Build Coastguard WorkerThe *locals area* is where the llvm compiler reserves space for local variables.
2387*9880d681SAndroid Build Coastguard Worker
2388*9880d681SAndroid Build Coastguard WorkerThe *saved registers area* is where the llvm compiler spills callee saved
2389*9880d681SAndroid Build Coastguard Workerregisters on entry to the callee.
2390*9880d681SAndroid Build Coastguard Worker
2391*9880d681SAndroid Build Coastguard WorkerProlog/Epilog
2392*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^
2393*9880d681SAndroid Build Coastguard Worker
2394*9880d681SAndroid Build Coastguard WorkerThe llvm prolog and epilog are the same as described in the PowerPC ABI, with
2395*9880d681SAndroid Build Coastguard Workerthe following exceptions.  Callee saved registers are spilled after the frame is
2396*9880d681SAndroid Build Coastguard Workercreated.  This allows the llvm epilog/prolog support to be common with other
2397*9880d681SAndroid Build Coastguard Workertargets.  The base pointer callee saved register r31 is saved in the TOC slot of
2398*9880d681SAndroid Build Coastguard Workerlinkage area.  This simplifies allocation of space for the base pointer and
2399*9880d681SAndroid Build Coastguard Workermakes it convenient to locate programatically and during debugging.
2400*9880d681SAndroid Build Coastguard Worker
2401*9880d681SAndroid Build Coastguard WorkerDynamic Allocation
2402*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^
2403*9880d681SAndroid Build Coastguard Worker
2404*9880d681SAndroid Build Coastguard Worker.. note::
2405*9880d681SAndroid Build Coastguard Worker
2406*9880d681SAndroid Build Coastguard Worker  TODO - More to come.
2407*9880d681SAndroid Build Coastguard Worker
2408*9880d681SAndroid Build Coastguard WorkerThe NVPTX backend
2409*9880d681SAndroid Build Coastguard Worker-----------------
2410*9880d681SAndroid Build Coastguard Worker
2411*9880d681SAndroid Build Coastguard WorkerThe NVPTX code generator under lib/Target/NVPTX is an open-source version of
2412*9880d681SAndroid Build Coastguard Workerthe NVIDIA NVPTX code generator for LLVM.  It is contributed by NVIDIA and is
2413*9880d681SAndroid Build Coastguard Workera port of the code generator used in the CUDA compiler (nvcc).  It targets the
2414*9880d681SAndroid Build Coastguard WorkerPTX 3.0/3.1 ISA and can target any compute capability greater than or equal to
2415*9880d681SAndroid Build Coastguard Worker2.0 (Fermi).
2416*9880d681SAndroid Build Coastguard Worker
2417*9880d681SAndroid Build Coastguard WorkerThis target is of production quality and should be completely compatible with
2418*9880d681SAndroid Build Coastguard Workerthe official NVIDIA toolchain.
2419*9880d681SAndroid Build Coastguard Worker
2420*9880d681SAndroid Build Coastguard WorkerCode Generator Options:
2421*9880d681SAndroid Build Coastguard Worker
2422*9880d681SAndroid Build Coastguard Worker:raw-html:`<table border="1" cellspacing="0">`
2423*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2424*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Option</th>`
2425*9880d681SAndroid Build Coastguard Worker:raw-html:`<th>Description</th>`
2426*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2427*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2428*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>sm_20</td>`
2429*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Set shader model/compute capability to 2.0</td>`
2430*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2431*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2432*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>sm_21</td>`
2433*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Set shader model/compute capability to 2.1</td>`
2434*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2435*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2436*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>sm_30</td>`
2437*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Set shader model/compute capability to 3.0</td>`
2438*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2439*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2440*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>sm_35</td>`
2441*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Set shader model/compute capability to 3.5</td>`
2442*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2443*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2444*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>ptx30</td>`
2445*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Target PTX 3.0</td>`
2446*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2447*9880d681SAndroid Build Coastguard Worker:raw-html:`<tr>`
2448*9880d681SAndroid Build Coastguard Worker:raw-html:`<td>ptx31</td>`
2449*9880d681SAndroid Build Coastguard Worker:raw-html:`<td align="left">Target PTX 3.1</td>`
2450*9880d681SAndroid Build Coastguard Worker:raw-html:`</tr>`
2451*9880d681SAndroid Build Coastguard Worker:raw-html:`</table>`
2452*9880d681SAndroid Build Coastguard Worker
2453*9880d681SAndroid Build Coastguard WorkerThe extended Berkeley Packet Filter (eBPF) backend
2454*9880d681SAndroid Build Coastguard Worker--------------------------------------------------
2455*9880d681SAndroid Build Coastguard Worker
2456*9880d681SAndroid Build Coastguard WorkerExtended BPF (or eBPF) is similar to the original ("classic") BPF (cBPF) used
2457*9880d681SAndroid Build Coastguard Workerto filter network packets.  The
2458*9880d681SAndroid Build Coastguard Worker`bpf() system call <http://man7.org/linux/man-pages/man2/bpf.2.html>`_
2459*9880d681SAndroid Build Coastguard Workerperforms a range of operations related to eBPF.  For both cBPF and eBPF
2460*9880d681SAndroid Build Coastguard Workerprograms, the Linux kernel statically analyzes the programs before loading
2461*9880d681SAndroid Build Coastguard Workerthem, in order to ensure that they cannot harm the running system.  eBPF is
2462*9880d681SAndroid Build Coastguard Workera 64-bit RISC instruction set designed for one to one mapping to 64-bit CPUs.
2463*9880d681SAndroid Build Coastguard WorkerOpcodes are 8-bit encoded, and 87 instructions are defined.  There are 10
2464*9880d681SAndroid Build Coastguard Workerregisters, grouped by function as outlined below.
2465*9880d681SAndroid Build Coastguard Worker
2466*9880d681SAndroid Build Coastguard Worker::
2467*9880d681SAndroid Build Coastguard Worker
2468*9880d681SAndroid Build Coastguard Worker  R0        return value from in-kernel functions; exit value for eBPF program
2469*9880d681SAndroid Build Coastguard Worker  R1 - R5   function call arguments to in-kernel functions
2470*9880d681SAndroid Build Coastguard Worker  R6 - R9   callee-saved registers preserved by in-kernel functions
2471*9880d681SAndroid Build Coastguard Worker  R10       stack frame pointer (read only)
2472*9880d681SAndroid Build Coastguard Worker
2473*9880d681SAndroid Build Coastguard WorkerInstruction encoding (arithmetic and jump)
2474*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2475*9880d681SAndroid Build Coastguard WorkereBPF is reusing most of the opcode encoding from classic to simplify conversion
2476*9880d681SAndroid Build Coastguard Workerof classic BPF to eBPF.  For arithmetic and jump instructions the 8-bit 'code'
2477*9880d681SAndroid Build Coastguard Workerfield is divided into three parts:
2478*9880d681SAndroid Build Coastguard Worker
2479*9880d681SAndroid Build Coastguard Worker::
2480*9880d681SAndroid Build Coastguard Worker
2481*9880d681SAndroid Build Coastguard Worker  +----------------+--------+--------------------+
2482*9880d681SAndroid Build Coastguard Worker  |   4 bits       |  1 bit |   3 bits           |
2483*9880d681SAndroid Build Coastguard Worker  | operation code | source | instruction class  |
2484*9880d681SAndroid Build Coastguard Worker  +----------------+--------+--------------------+
2485*9880d681SAndroid Build Coastguard Worker  (MSB)                                      (LSB)
2486*9880d681SAndroid Build Coastguard Worker
2487*9880d681SAndroid Build Coastguard WorkerThree LSB bits store instruction class which is one of:
2488*9880d681SAndroid Build Coastguard Worker
2489*9880d681SAndroid Build Coastguard Worker::
2490*9880d681SAndroid Build Coastguard Worker
2491*9880d681SAndroid Build Coastguard Worker  BPF_LD     0x0
2492*9880d681SAndroid Build Coastguard Worker  BPF_LDX    0x1
2493*9880d681SAndroid Build Coastguard Worker  BPF_ST     0x2
2494*9880d681SAndroid Build Coastguard Worker  BPF_STX    0x3
2495*9880d681SAndroid Build Coastguard Worker  BPF_ALU    0x4
2496*9880d681SAndroid Build Coastguard Worker  BPF_JMP    0x5
2497*9880d681SAndroid Build Coastguard Worker  (unused)   0x6
2498*9880d681SAndroid Build Coastguard Worker  BPF_ALU64  0x7
2499*9880d681SAndroid Build Coastguard Worker
2500*9880d681SAndroid Build Coastguard WorkerWhen BPF_CLASS(code) == BPF_ALU or BPF_ALU64 or BPF_JMP,
2501*9880d681SAndroid Build Coastguard Worker4th bit encodes source operand
2502*9880d681SAndroid Build Coastguard Worker
2503*9880d681SAndroid Build Coastguard Worker::
2504*9880d681SAndroid Build Coastguard Worker
2505*9880d681SAndroid Build Coastguard Worker  BPF_X     0x0  use src_reg register as source operand
2506*9880d681SAndroid Build Coastguard Worker  BPF_K     0x1  use 32 bit immediate as source operand
2507*9880d681SAndroid Build Coastguard Worker
2508*9880d681SAndroid Build Coastguard Workerand four MSB bits store operation code
2509*9880d681SAndroid Build Coastguard Worker
2510*9880d681SAndroid Build Coastguard Worker::
2511*9880d681SAndroid Build Coastguard Worker
2512*9880d681SAndroid Build Coastguard Worker  BPF_ADD   0x0  add
2513*9880d681SAndroid Build Coastguard Worker  BPF_SUB   0x1  subtract
2514*9880d681SAndroid Build Coastguard Worker  BPF_MUL   0x2  multiply
2515*9880d681SAndroid Build Coastguard Worker  BPF_DIV   0x3  divide
2516*9880d681SAndroid Build Coastguard Worker  BPF_OR    0x4  bitwise logical OR
2517*9880d681SAndroid Build Coastguard Worker  BPF_AND   0x5  bitwise logical AND
2518*9880d681SAndroid Build Coastguard Worker  BPF_LSH   0x6  left shift
2519*9880d681SAndroid Build Coastguard Worker  BPF_RSH   0x7  right shift (zero extended)
2520*9880d681SAndroid Build Coastguard Worker  BPF_NEG   0x8  arithmetic negation
2521*9880d681SAndroid Build Coastguard Worker  BPF_MOD   0x9  modulo
2522*9880d681SAndroid Build Coastguard Worker  BPF_XOR   0xa  bitwise logical XOR
2523*9880d681SAndroid Build Coastguard Worker  BPF_MOV   0xb  move register to register
2524*9880d681SAndroid Build Coastguard Worker  BPF_ARSH  0xc  right shift (sign extended)
2525*9880d681SAndroid Build Coastguard Worker  BPF_END   0xd  endianness conversion
2526*9880d681SAndroid Build Coastguard Worker
2527*9880d681SAndroid Build Coastguard WorkerIf BPF_CLASS(code) == BPF_JMP, BPF_OP(code) is one of
2528*9880d681SAndroid Build Coastguard Worker
2529*9880d681SAndroid Build Coastguard Worker::
2530*9880d681SAndroid Build Coastguard Worker
2531*9880d681SAndroid Build Coastguard Worker  BPF_JA    0x0  unconditional jump
2532*9880d681SAndroid Build Coastguard Worker  BPF_JEQ   0x1  jump ==
2533*9880d681SAndroid Build Coastguard Worker  BPF_JGT   0x2  jump >
2534*9880d681SAndroid Build Coastguard Worker  BPF_JGE   0x3  jump >=
2535*9880d681SAndroid Build Coastguard Worker  BPF_JSET  0x4  jump if (DST & SRC)
2536*9880d681SAndroid Build Coastguard Worker  BPF_JNE   0x5  jump !=
2537*9880d681SAndroid Build Coastguard Worker  BPF_JSGT  0x6  jump signed >
2538*9880d681SAndroid Build Coastguard Worker  BPF_JSGE  0x7  jump signed >=
2539*9880d681SAndroid Build Coastguard Worker  BPF_CALL  0x8  function call
2540*9880d681SAndroid Build Coastguard Worker  BPF_EXIT  0x9  function return
2541*9880d681SAndroid Build Coastguard Worker
2542*9880d681SAndroid Build Coastguard WorkerInstruction encoding (load, store)
2543*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2544*9880d681SAndroid Build Coastguard WorkerFor load and store instructions the 8-bit 'code' field is divided as:
2545*9880d681SAndroid Build Coastguard Worker
2546*9880d681SAndroid Build Coastguard Worker::
2547*9880d681SAndroid Build Coastguard Worker
2548*9880d681SAndroid Build Coastguard Worker  +--------+--------+-------------------+
2549*9880d681SAndroid Build Coastguard Worker  | 3 bits | 2 bits |   3 bits          |
2550*9880d681SAndroid Build Coastguard Worker  |  mode  |  size  | instruction class |
2551*9880d681SAndroid Build Coastguard Worker  +--------+--------+-------------------+
2552*9880d681SAndroid Build Coastguard Worker  (MSB)                             (LSB)
2553*9880d681SAndroid Build Coastguard Worker
2554*9880d681SAndroid Build Coastguard WorkerSize modifier is one of
2555*9880d681SAndroid Build Coastguard Worker
2556*9880d681SAndroid Build Coastguard Worker::
2557*9880d681SAndroid Build Coastguard Worker
2558*9880d681SAndroid Build Coastguard Worker  BPF_W       0x0  word
2559*9880d681SAndroid Build Coastguard Worker  BPF_H       0x1  half word
2560*9880d681SAndroid Build Coastguard Worker  BPF_B       0x2  byte
2561*9880d681SAndroid Build Coastguard Worker  BPF_DW      0x3  double word
2562*9880d681SAndroid Build Coastguard Worker
2563*9880d681SAndroid Build Coastguard WorkerMode modifier is one of
2564*9880d681SAndroid Build Coastguard Worker
2565*9880d681SAndroid Build Coastguard Worker::
2566*9880d681SAndroid Build Coastguard Worker
2567*9880d681SAndroid Build Coastguard Worker  BPF_IMM     0x0  immediate
2568*9880d681SAndroid Build Coastguard Worker  BPF_ABS     0x1  used to access packet data
2569*9880d681SAndroid Build Coastguard Worker  BPF_IND     0x2  used to access packet data
2570*9880d681SAndroid Build Coastguard Worker  BPF_MEM     0x3  memory
2571*9880d681SAndroid Build Coastguard Worker  (reserved)  0x4
2572*9880d681SAndroid Build Coastguard Worker  (reserved)  0x5
2573*9880d681SAndroid Build Coastguard Worker  BPF_XADD    0x6  exclusive add
2574*9880d681SAndroid Build Coastguard Worker
2575*9880d681SAndroid Build Coastguard Worker
2576*9880d681SAndroid Build Coastguard WorkerPacket data access (BPF_ABS, BPF_IND)
2577*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2578*9880d681SAndroid Build Coastguard Worker
2579*9880d681SAndroid Build Coastguard WorkerTwo non-generic instructions: (BPF_ABS | <size> | BPF_LD) and
2580*9880d681SAndroid Build Coastguard Worker(BPF_IND | <size> | BPF_LD) which are used to access packet data.
2581*9880d681SAndroid Build Coastguard WorkerRegister R6 is an implicit input that must contain pointer to sk_buff.
2582*9880d681SAndroid Build Coastguard WorkerRegister R0 is an implicit output which contains the data fetched
2583*9880d681SAndroid Build Coastguard Workerfrom the packet.  Registers R1-R5 are scratch registers and must not
2584*9880d681SAndroid Build Coastguard Workerbe used to store the data across BPF_ABS | BPF_LD or BPF_IND | BPF_LD
2585*9880d681SAndroid Build Coastguard Workerinstructions.  These instructions have implicit program exit condition
2586*9880d681SAndroid Build Coastguard Workeras well.  When eBPF program is trying to access the data beyond
2587*9880d681SAndroid Build Coastguard Workerthe packet boundary, the interpreter will abort the execution of the program.
2588*9880d681SAndroid Build Coastguard Worker
2589*9880d681SAndroid Build Coastguard WorkerBPF_IND | BPF_W | BPF_LD is equivalent to:
2590*9880d681SAndroid Build Coastguard Worker  R0 = ntohl(\*(u32 \*) (((struct sk_buff \*) R6)->data + src_reg + imm32))
2591*9880d681SAndroid Build Coastguard Worker
2592*9880d681SAndroid Build Coastguard WorkereBPF maps
2593*9880d681SAndroid Build Coastguard Worker^^^^^^^^^
2594*9880d681SAndroid Build Coastguard Worker
2595*9880d681SAndroid Build Coastguard WorkereBPF maps are provided for sharing data between kernel and user-space.
2596*9880d681SAndroid Build Coastguard WorkerCurrently implemented types are hash and array, with potential extension to
2597*9880d681SAndroid Build Coastguard Workersupport bloom filters, radix trees, etc.  A map is defined by its type,
2598*9880d681SAndroid Build Coastguard Workermaximum number of elements, key size and value size in bytes.  eBPF syscall
2599*9880d681SAndroid Build Coastguard Workersupports create, update, find and delete functions on maps.
2600*9880d681SAndroid Build Coastguard Worker
2601*9880d681SAndroid Build Coastguard WorkerFunction calls
2602*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^
2603*9880d681SAndroid Build Coastguard Worker
2604*9880d681SAndroid Build Coastguard WorkerFunction call arguments are passed using up to five registers (R1 - R5).
2605*9880d681SAndroid Build Coastguard WorkerThe return value is passed in a dedicated register (R0).  Four additional
2606*9880d681SAndroid Build Coastguard Workerregisters (R6 - R9) are callee-saved, and the values in these registers
2607*9880d681SAndroid Build Coastguard Workerare preserved within kernel functions.  R0 - R5 are scratch registers within
2608*9880d681SAndroid Build Coastguard Workerkernel functions, and eBPF programs must therefor store/restore values in
2609*9880d681SAndroid Build Coastguard Workerthese registers if needed across function calls.  The stack can be accessed
2610*9880d681SAndroid Build Coastguard Workerusing the read-only frame pointer R10.  eBPF registers map 1:1 to hardware
2611*9880d681SAndroid Build Coastguard Workerregisters on x86_64 and other 64-bit architectures.  For example, x86_64
2612*9880d681SAndroid Build Coastguard Workerin-kernel JIT maps them as
2613*9880d681SAndroid Build Coastguard Worker
2614*9880d681SAndroid Build Coastguard Worker::
2615*9880d681SAndroid Build Coastguard Worker
2616*9880d681SAndroid Build Coastguard Worker  R0 - rax
2617*9880d681SAndroid Build Coastguard Worker  R1 - rdi
2618*9880d681SAndroid Build Coastguard Worker  R2 - rsi
2619*9880d681SAndroid Build Coastguard Worker  R3 - rdx
2620*9880d681SAndroid Build Coastguard Worker  R4 - rcx
2621*9880d681SAndroid Build Coastguard Worker  R5 - r8
2622*9880d681SAndroid Build Coastguard Worker  R6 - rbx
2623*9880d681SAndroid Build Coastguard Worker  R7 - r13
2624*9880d681SAndroid Build Coastguard Worker  R8 - r14
2625*9880d681SAndroid Build Coastguard Worker  R9 - r15
2626*9880d681SAndroid Build Coastguard Worker  R10 - rbp
2627*9880d681SAndroid Build Coastguard Worker
2628*9880d681SAndroid Build Coastguard Workersince x86_64 ABI mandates rdi, rsi, rdx, rcx, r8, r9 for argument passing
2629*9880d681SAndroid Build Coastguard Workerand rbx, r12 - r15 are callee saved.
2630*9880d681SAndroid Build Coastguard Worker
2631*9880d681SAndroid Build Coastguard WorkerProgram start
2632*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^
2633*9880d681SAndroid Build Coastguard Worker
2634*9880d681SAndroid Build Coastguard WorkerAn eBPF program receives a single argument and contains
2635*9880d681SAndroid Build Coastguard Workera single eBPF main routine; the program does not contain eBPF functions.
2636*9880d681SAndroid Build Coastguard WorkerFunction calls are limited to a predefined set of kernel functions.  The size
2637*9880d681SAndroid Build Coastguard Workerof a program is limited to 4K instructions:  this ensures fast termination and
2638*9880d681SAndroid Build Coastguard Workera limited number of kernel function calls.  Prior to running an eBPF program,
2639*9880d681SAndroid Build Coastguard Workera verifier performs static analysis to prevent loops in the code and
2640*9880d681SAndroid Build Coastguard Workerto ensure valid register usage and operand types.
2641*9880d681SAndroid Build Coastguard Worker
2642*9880d681SAndroid Build Coastguard WorkerThe AMDGPU backend
2643*9880d681SAndroid Build Coastguard Worker------------------
2644*9880d681SAndroid Build Coastguard Worker
2645*9880d681SAndroid Build Coastguard WorkerThe AMDGPU code generator lives in the lib/Target/AMDGPU directory, and is an
2646*9880d681SAndroid Build Coastguard Workeropen source native AMD GCN ISA code generator.
2647*9880d681SAndroid Build Coastguard Worker
2648*9880d681SAndroid Build Coastguard WorkerTarget triples supported
2649*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^
2650*9880d681SAndroid Build Coastguard Worker
2651*9880d681SAndroid Build Coastguard WorkerThe following are the known target triples that are supported by the AMDGPU
2652*9880d681SAndroid Build Coastguard Workerbackend.
2653*9880d681SAndroid Build Coastguard Worker
2654*9880d681SAndroid Build Coastguard Worker* **amdgcn--** --- AMD GCN GPUs (AMDGPU.7.0.0+)
2655*9880d681SAndroid Build Coastguard Worker* **amdgcn--amdhsa** --- AMD GCN GPUs (AMDGPU.7.0.0+) with HSA support
2656*9880d681SAndroid Build Coastguard Worker* **r600--** --- AMD GPUs HD2XXX-HD6XXX
2657*9880d681SAndroid Build Coastguard Worker
2658*9880d681SAndroid Build Coastguard WorkerRelocations
2659*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^
2660*9880d681SAndroid Build Coastguard Worker
2661*9880d681SAndroid Build Coastguard WorkerSupported relocatable fields are:
2662*9880d681SAndroid Build Coastguard Worker
2663*9880d681SAndroid Build Coastguard Worker* **word32** --- This specifies a 32-bit field occupying 4 bytes with arbitrary
2664*9880d681SAndroid Build Coastguard Worker  byte alignment. These values use the same byte order as other word values in
2665*9880d681SAndroid Build Coastguard Worker  the AMD GPU architecture
2666*9880d681SAndroid Build Coastguard Worker* **word64** --- This specifies a 64-bit field occupying 8 bytes with arbitrary
2667*9880d681SAndroid Build Coastguard Worker  byte alignment. These values use the same byte order as other word values in
2668*9880d681SAndroid Build Coastguard Worker  the AMD GPU architecture
2669*9880d681SAndroid Build Coastguard Worker
2670*9880d681SAndroid Build Coastguard WorkerFollowing notations are used for specifying relocation calculations:
2671*9880d681SAndroid Build Coastguard Worker
2672*9880d681SAndroid Build Coastguard Worker* **A** --- Represents the addend used to compute the value of the relocatable
2673*9880d681SAndroid Build Coastguard Worker  field
2674*9880d681SAndroid Build Coastguard Worker* **G** --- Represents the offset into the global offset table at which the
2675*9880d681SAndroid Build Coastguard Worker  relocation entry’s symbol will reside during execution.
2676*9880d681SAndroid Build Coastguard Worker* **GOT** --- Represents the address of the global offset table.
2677*9880d681SAndroid Build Coastguard Worker* **P** --- Represents the place (section offset or address) of the storage unit
2678*9880d681SAndroid Build Coastguard Worker  being relocated (computed using ``r_offset``)
2679*9880d681SAndroid Build Coastguard Worker* **S** --- Represents the value of the symbol whose index resides in the
2680*9880d681SAndroid Build Coastguard Worker  relocation entry
2681*9880d681SAndroid Build Coastguard Worker
2682*9880d681SAndroid Build Coastguard WorkerAMDGPU Backend generates *Elf64_Rela* relocation records with the following
2683*9880d681SAndroid Build Coastguard Workersupported relocation types:
2684*9880d681SAndroid Build Coastguard Worker
2685*9880d681SAndroid Build Coastguard Worker  =====================  =====  ==========  ====================
2686*9880d681SAndroid Build Coastguard Worker  Relocation type        Value  Field       Calculation
2687*9880d681SAndroid Build Coastguard Worker  =====================  =====  ==========  ====================
2688*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_NONE``      0      ``none``    ``none``
2689*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_ABS32_LO``  1      ``word32``  (S + A) & 0xFFFFFFFF
2690*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_ABS32_HI``  2      ``word32``  (S + A) >> 32
2691*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_ABS64``     3      ``word64``  S + A
2692*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_REL32``     4      ``word32``  S + A - P
2693*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_REL64``     5      ``word64``  S + A - P
2694*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_ABS32``     6      ``word32``  S + A
2695*9880d681SAndroid Build Coastguard Worker  ``R_AMDGPU_GOTPCREL``  7      ``word32``  G + GOT + A - P
2696*9880d681SAndroid Build Coastguard Worker  =====================  =====  ==========  ====================
2697