xref: /aosp_15_r20/external/llvm/docs/WritingAnLLVMBackend.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker=======================
2*9880d681SAndroid Build Coastguard WorkerWriting an LLVM Backend
3*9880d681SAndroid Build Coastguard Worker=======================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. toctree::
6*9880d681SAndroid Build Coastguard Worker   :hidden:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker   HowToUseInstrMappings
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker.. contents::
11*9880d681SAndroid Build Coastguard Worker   :local:
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard WorkerIntroduction
14*9880d681SAndroid Build Coastguard Worker============
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard WorkerThis document describes techniques for writing compiler backends that convert
17*9880d681SAndroid Build Coastguard Workerthe LLVM Intermediate Representation (IR) to code for a specified machine or
18*9880d681SAndroid Build Coastguard Workerother languages.  Code intended for a specific machine can take the form of
19*9880d681SAndroid Build Coastguard Workereither assembly code or binary code (usable for a JIT compiler).
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard WorkerThe backend of LLVM features a target-independent code generator that may
22*9880d681SAndroid Build Coastguard Workercreate output for several types of target CPUs --- including X86, PowerPC,
23*9880d681SAndroid Build Coastguard WorkerARM, and SPARC.  The backend may also be used to generate code targeted at SPUs
24*9880d681SAndroid Build Coastguard Workerof the Cell processor or GPUs to support the execution of compute kernels.
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard WorkerThe document focuses on existing examples found in subdirectories of
27*9880d681SAndroid Build Coastguard Worker``llvm/lib/Target`` in a downloaded LLVM release.  In particular, this document
28*9880d681SAndroid Build Coastguard Workerfocuses on the example of creating a static compiler (one that emits text
29*9880d681SAndroid Build Coastguard Workerassembly) for a SPARC target, because SPARC has fairly standard
30*9880d681SAndroid Build Coastguard Workercharacteristics, such as a RISC instruction set and straightforward calling
31*9880d681SAndroid Build Coastguard Workerconventions.
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard WorkerAudience
34*9880d681SAndroid Build Coastguard Worker--------
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard WorkerThe audience for this document is anyone who needs to write an LLVM backend to
37*9880d681SAndroid Build Coastguard Workergenerate code for a specific hardware or software target.
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard WorkerPrerequisite Reading
40*9880d681SAndroid Build Coastguard Worker--------------------
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard WorkerThese essential documents must be read before reading this document:
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker* `LLVM Language Reference Manual <LangRef.html>`_ --- a reference manual for
45*9880d681SAndroid Build Coastguard Worker  the LLVM assembly language.
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker* :doc:`CodeGenerator` --- a guide to the components (classes and code
48*9880d681SAndroid Build Coastguard Worker  generation algorithms) for translating the LLVM internal representation into
49*9880d681SAndroid Build Coastguard Worker  machine code for a specified target.  Pay particular attention to the
50*9880d681SAndroid Build Coastguard Worker  descriptions of code generation stages: Instruction Selection, Scheduling and
51*9880d681SAndroid Build Coastguard Worker  Formation, SSA-based Optimization, Register Allocation, Prolog/Epilog Code
52*9880d681SAndroid Build Coastguard Worker  Insertion, Late Machine Code Optimizations, and Code Emission.
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker* :doc:`TableGen/index` --- a document that describes the TableGen
55*9880d681SAndroid Build Coastguard Worker  (``tblgen``) application that manages domain-specific information to support
56*9880d681SAndroid Build Coastguard Worker  LLVM code generation.  TableGen processes input from a target description
57*9880d681SAndroid Build Coastguard Worker  file (``.td`` suffix) and generates C++ code that can be used for code
58*9880d681SAndroid Build Coastguard Worker  generation.
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker* :doc:`WritingAnLLVMPass` --- The assembly printer is a ``FunctionPass``, as
61*9880d681SAndroid Build Coastguard Worker  are several ``SelectionDAG`` processing steps.
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard WorkerTo follow the SPARC examples in this document, have a copy of `The SPARC
64*9880d681SAndroid Build Coastguard WorkerArchitecture Manual, Version 8 <http://www.sparc.org/standards/V8.pdf>`_ for
65*9880d681SAndroid Build Coastguard Workerreference.  For details about the ARM instruction set, refer to the `ARM
66*9880d681SAndroid Build Coastguard WorkerArchitecture Reference Manual <http://infocenter.arm.com/>`_.  For more about
67*9880d681SAndroid Build Coastguard Workerthe GNU Assembler format (``GAS``), see `Using As
68*9880d681SAndroid Build Coastguard Worker<http://sourceware.org/binutils/docs/as/index.html>`_, especially for the
69*9880d681SAndroid Build Coastguard Workerassembly printer.  "Using As" contains a list of target machine dependent
70*9880d681SAndroid Build Coastguard Workerfeatures.
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard WorkerBasic Steps
73*9880d681SAndroid Build Coastguard Worker-----------
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard WorkerTo write a compiler backend for LLVM that converts the LLVM IR to code for a
76*9880d681SAndroid Build Coastguard Workerspecified target (machine or other language), follow these steps:
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker* Create a subclass of the ``TargetMachine`` class that describes
79*9880d681SAndroid Build Coastguard Worker  characteristics of your target machine.  Copy existing examples of specific
80*9880d681SAndroid Build Coastguard Worker  ``TargetMachine`` class and header files; for example, start with
81*9880d681SAndroid Build Coastguard Worker  ``SparcTargetMachine.cpp`` and ``SparcTargetMachine.h``, but change the file
82*9880d681SAndroid Build Coastguard Worker  names for your target.  Similarly, change code that references "``Sparc``" to
83*9880d681SAndroid Build Coastguard Worker  reference your target.
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker* Describe the register set of the target.  Use TableGen to generate code for
86*9880d681SAndroid Build Coastguard Worker  register definition, register aliases, and register classes from a
87*9880d681SAndroid Build Coastguard Worker  target-specific ``RegisterInfo.td`` input file.  You should also write
88*9880d681SAndroid Build Coastguard Worker  additional code for a subclass of the ``TargetRegisterInfo`` class that
89*9880d681SAndroid Build Coastguard Worker  represents the class register file data used for register allocation and also
90*9880d681SAndroid Build Coastguard Worker  describes the interactions between registers.
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker* Describe the instruction set of the target.  Use TableGen to generate code
93*9880d681SAndroid Build Coastguard Worker  for target-specific instructions from target-specific versions of
94*9880d681SAndroid Build Coastguard Worker  ``TargetInstrFormats.td`` and ``TargetInstrInfo.td``.  You should write
95*9880d681SAndroid Build Coastguard Worker  additional code for a subclass of the ``TargetInstrInfo`` class to represent
96*9880d681SAndroid Build Coastguard Worker  machine instructions supported by the target machine.
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker* Describe the selection and conversion of the LLVM IR from a Directed Acyclic
99*9880d681SAndroid Build Coastguard Worker  Graph (DAG) representation of instructions to native target-specific
100*9880d681SAndroid Build Coastguard Worker  instructions.  Use TableGen to generate code that matches patterns and
101*9880d681SAndroid Build Coastguard Worker  selects instructions based on additional information in a target-specific
102*9880d681SAndroid Build Coastguard Worker  version of ``TargetInstrInfo.td``.  Write code for ``XXXISelDAGToDAG.cpp``,
103*9880d681SAndroid Build Coastguard Worker  where ``XXX`` identifies the specific target, to perform pattern matching and
104*9880d681SAndroid Build Coastguard Worker  DAG-to-DAG instruction selection.  Also write code in ``XXXISelLowering.cpp``
105*9880d681SAndroid Build Coastguard Worker  to replace or remove operations and data types that are not supported
106*9880d681SAndroid Build Coastguard Worker  natively in a SelectionDAG.
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker* Write code for an assembly printer that converts LLVM IR to a GAS format for
109*9880d681SAndroid Build Coastguard Worker  your target machine.  You should add assembly strings to the instructions
110*9880d681SAndroid Build Coastguard Worker  defined in your target-specific version of ``TargetInstrInfo.td``.  You
111*9880d681SAndroid Build Coastguard Worker  should also write code for a subclass of ``AsmPrinter`` that performs the
112*9880d681SAndroid Build Coastguard Worker  LLVM-to-assembly conversion and a trivial subclass of ``TargetAsmInfo``.
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker* Optionally, add support for subtargets (i.e., variants with different
115*9880d681SAndroid Build Coastguard Worker  capabilities).  You should also write code for a subclass of the
116*9880d681SAndroid Build Coastguard Worker  ``TargetSubtarget`` class, which allows you to use the ``-mcpu=`` and
117*9880d681SAndroid Build Coastguard Worker  ``-mattr=`` command-line options.
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker* Optionally, add JIT support and create a machine code emitter (subclass of
120*9880d681SAndroid Build Coastguard Worker  ``TargetJITInfo``) that is used to emit binary code directly into memory.
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard WorkerIn the ``.cpp`` and ``.h``. files, initially stub up these methods and then
123*9880d681SAndroid Build Coastguard Workerimplement them later.  Initially, you may not know which private members that
124*9880d681SAndroid Build Coastguard Workerthe class will need and which components will need to be subclassed.
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard WorkerPreliminaries
127*9880d681SAndroid Build Coastguard Worker-------------
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard WorkerTo actually create your compiler backend, you need to create and modify a few
130*9880d681SAndroid Build Coastguard Workerfiles.  The absolute minimum is discussed here.  But to actually use the LLVM
131*9880d681SAndroid Build Coastguard Workertarget-independent code generator, you must perform the steps described in the
132*9880d681SAndroid Build Coastguard Worker:doc:`LLVM Target-Independent Code Generator <CodeGenerator>` document.
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard WorkerFirst, you should create a subdirectory under ``lib/Target`` to hold all the
135*9880d681SAndroid Build Coastguard Workerfiles related to your target.  If your target is called "Dummy", create the
136*9880d681SAndroid Build Coastguard Workerdirectory ``lib/Target/Dummy``.
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard WorkerIn this new directory, create a ``CMakeLists.txt``.  It is easiest to copy a
139*9880d681SAndroid Build Coastguard Worker``CMakeLists.txt`` of another target and modify it.  It should at least contain
140*9880d681SAndroid Build Coastguard Workerthe ``LLVM_TARGET_DEFINITIONS`` variable. The library can be named ``LLVMDummy``
141*9880d681SAndroid Build Coastguard Worker(for example, see the MIPS target).  Alternatively, you can split the library
142*9880d681SAndroid Build Coastguard Workerinto ``LLVMDummyCodeGen`` and ``LLVMDummyAsmPrinter``, the latter of which
143*9880d681SAndroid Build Coastguard Workershould be implemented in a subdirectory below ``lib/Target/Dummy`` (for example,
144*9880d681SAndroid Build Coastguard Workersee the PowerPC target).
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard WorkerNote that these two naming schemes are hardcoded into ``llvm-config``.  Using
147*9880d681SAndroid Build Coastguard Workerany other naming scheme will confuse ``llvm-config`` and produce a lot of
148*9880d681SAndroid Build Coastguard Worker(seemingly unrelated) linker errors when linking ``llc``.
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard WorkerTo make your target actually do something, you need to implement a subclass of
151*9880d681SAndroid Build Coastguard Worker``TargetMachine``.  This implementation should typically be in the file
152*9880d681SAndroid Build Coastguard Worker``lib/Target/DummyTargetMachine.cpp``, but any file in the ``lib/Target``
153*9880d681SAndroid Build Coastguard Workerdirectory will be built and should work.  To use LLVM's target independent code
154*9880d681SAndroid Build Coastguard Workergenerator, you should do what all current machine backends do: create a
155*9880d681SAndroid Build Coastguard Workersubclass of ``LLVMTargetMachine``.  (To create a target from scratch, create a
156*9880d681SAndroid Build Coastguard Workersubclass of ``TargetMachine``.)
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard WorkerTo get LLVM to actually build and link your target, you need to run ``cmake``
159*9880d681SAndroid Build Coastguard Workerwith ``-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=Dummy``. This will build your
160*9880d681SAndroid Build Coastguard Workertarget without needing to add it to the list of all the targets.
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard WorkerOnce your target is stable, you can add it to the ``LLVM_ALL_TARGETS`` variable
163*9880d681SAndroid Build Coastguard Workerlocated in the main ``CMakeLists.txt``.
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard WorkerTarget Machine
166*9880d681SAndroid Build Coastguard Worker==============
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker``LLVMTargetMachine`` is designed as a base class for targets implemented with
169*9880d681SAndroid Build Coastguard Workerthe LLVM target-independent code generator.  The ``LLVMTargetMachine`` class
170*9880d681SAndroid Build Coastguard Workershould be specialized by a concrete target class that implements the various
171*9880d681SAndroid Build Coastguard Workervirtual methods.  ``LLVMTargetMachine`` is defined as a subclass of
172*9880d681SAndroid Build Coastguard Worker``TargetMachine`` in ``include/llvm/Target/TargetMachine.h``.  The
173*9880d681SAndroid Build Coastguard Worker``TargetMachine`` class implementation (``TargetMachine.cpp``) also processes
174*9880d681SAndroid Build Coastguard Workernumerous command-line options.
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard WorkerTo create a concrete target-specific subclass of ``LLVMTargetMachine``, start
177*9880d681SAndroid Build Coastguard Workerby copying an existing ``TargetMachine`` class and header.  You should name the
178*9880d681SAndroid Build Coastguard Workerfiles that you create to reflect your specific target.  For instance, for the
179*9880d681SAndroid Build Coastguard WorkerSPARC target, name the files ``SparcTargetMachine.h`` and
180*9880d681SAndroid Build Coastguard Worker``SparcTargetMachine.cpp``.
181*9880d681SAndroid Build Coastguard Worker
182*9880d681SAndroid Build Coastguard WorkerFor a target machine ``XXX``, the implementation of ``XXXTargetMachine`` must
183*9880d681SAndroid Build Coastguard Workerhave access methods to obtain objects that represent target components.  These
184*9880d681SAndroid Build Coastguard Workermethods are named ``get*Info``, and are intended to obtain the instruction set
185*9880d681SAndroid Build Coastguard Worker(``getInstrInfo``), register set (``getRegisterInfo``), stack frame layout
186*9880d681SAndroid Build Coastguard Worker(``getFrameInfo``), and similar information.  ``XXXTargetMachine`` must also
187*9880d681SAndroid Build Coastguard Workerimplement the ``getDataLayout`` method to access an object with target-specific
188*9880d681SAndroid Build Coastguard Workerdata characteristics, such as data type size and alignment requirements.
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard WorkerFor instance, for the SPARC target, the header file ``SparcTargetMachine.h``
191*9880d681SAndroid Build Coastguard Workerdeclares prototypes for several ``get*Info`` and ``getDataLayout`` methods that
192*9880d681SAndroid Build Coastguard Workersimply return a class member.
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker  namespace llvm {
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker  class Module;
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker  class SparcTargetMachine : public LLVMTargetMachine {
201*9880d681SAndroid Build Coastguard Worker    const DataLayout DataLayout;       // Calculates type size & alignment
202*9880d681SAndroid Build Coastguard Worker    SparcSubtarget Subtarget;
203*9880d681SAndroid Build Coastguard Worker    SparcInstrInfo InstrInfo;
204*9880d681SAndroid Build Coastguard Worker    TargetFrameInfo FrameInfo;
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker  protected:
207*9880d681SAndroid Build Coastguard Worker    virtual const TargetAsmInfo *createTargetAsmInfo() const;
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker  public:
210*9880d681SAndroid Build Coastguard Worker    SparcTargetMachine(const Module &M, const std::string &FS);
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker    virtual const SparcInstrInfo *getInstrInfo() const {return &InstrInfo; }
213*9880d681SAndroid Build Coastguard Worker    virtual const TargetFrameInfo *getFrameInfo() const {return &FrameInfo; }
214*9880d681SAndroid Build Coastguard Worker    virtual const TargetSubtarget *getSubtargetImpl() const{return &Subtarget; }
215*9880d681SAndroid Build Coastguard Worker    virtual const TargetRegisterInfo *getRegisterInfo() const {
216*9880d681SAndroid Build Coastguard Worker      return &InstrInfo.getRegisterInfo();
217*9880d681SAndroid Build Coastguard Worker    }
218*9880d681SAndroid Build Coastguard Worker    virtual const DataLayout *getDataLayout() const { return &DataLayout; }
219*9880d681SAndroid Build Coastguard Worker    static unsigned getModuleMatchQuality(const Module &M);
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker    // Pass Pipeline Configuration
222*9880d681SAndroid Build Coastguard Worker    virtual bool addInstSelector(PassManagerBase &PM, bool Fast);
223*9880d681SAndroid Build Coastguard Worker    virtual bool addPreEmitPass(PassManagerBase &PM, bool Fast);
224*9880d681SAndroid Build Coastguard Worker  };
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker  } // end namespace llvm
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker* ``getInstrInfo()``
229*9880d681SAndroid Build Coastguard Worker* ``getRegisterInfo()``
230*9880d681SAndroid Build Coastguard Worker* ``getFrameInfo()``
231*9880d681SAndroid Build Coastguard Worker* ``getDataLayout()``
232*9880d681SAndroid Build Coastguard Worker* ``getSubtargetImpl()``
233*9880d681SAndroid Build Coastguard Worker
234*9880d681SAndroid Build Coastguard WorkerFor some targets, you also need to support the following methods:
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker* ``getTargetLowering()``
237*9880d681SAndroid Build Coastguard Worker* ``getJITInfo()``
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard WorkerSome architectures, such as GPUs, do not support jumping to an arbitrary
240*9880d681SAndroid Build Coastguard Workerprogram location and implement branching using masked execution and loop using
241*9880d681SAndroid Build Coastguard Workerspecial instructions around the loop body. In order to avoid CFG modifications
242*9880d681SAndroid Build Coastguard Workerthat introduce irreducible control flow not handled by such hardware, a target
243*9880d681SAndroid Build Coastguard Workermust call `setRequiresStructuredCFG(true)` when being initialized.
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard WorkerIn addition, the ``XXXTargetMachine`` constructor should specify a
246*9880d681SAndroid Build Coastguard Worker``TargetDescription`` string that determines the data layout for the target
247*9880d681SAndroid Build Coastguard Workermachine, including characteristics such as pointer size, alignment, and
248*9880d681SAndroid Build Coastguard Workerendianness.  For example, the constructor for ``SparcTargetMachine`` contains
249*9880d681SAndroid Build Coastguard Workerthe following:
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker  SparcTargetMachine::SparcTargetMachine(const Module &M, const std::string &FS)
254*9880d681SAndroid Build Coastguard Worker    : DataLayout("E-p:32:32-f128:128:128"),
255*9880d681SAndroid Build Coastguard Worker      Subtarget(M, FS), InstrInfo(Subtarget),
256*9880d681SAndroid Build Coastguard Worker      FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) {
257*9880d681SAndroid Build Coastguard Worker  }
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard WorkerHyphens separate portions of the ``TargetDescription`` string.
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker* An upper-case "``E``" in the string indicates a big-endian target data model.
262*9880d681SAndroid Build Coastguard Worker  A lower-case "``e``" indicates little-endian.
263*9880d681SAndroid Build Coastguard Worker
264*9880d681SAndroid Build Coastguard Worker* "``p:``" is followed by pointer information: size, ABI alignment, and
265*9880d681SAndroid Build Coastguard Worker  preferred alignment.  If only two figures follow "``p:``", then the first
266*9880d681SAndroid Build Coastguard Worker  value is pointer size, and the second value is both ABI and preferred
267*9880d681SAndroid Build Coastguard Worker  alignment.
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker* Then a letter for numeric type alignment: "``i``", "``f``", "``v``", or
270*9880d681SAndroid Build Coastguard Worker  "``a``" (corresponding to integer, floating point, vector, or aggregate).
271*9880d681SAndroid Build Coastguard Worker  "``i``", "``v``", or "``a``" are followed by ABI alignment and preferred
272*9880d681SAndroid Build Coastguard Worker  alignment. "``f``" is followed by three values: the first indicates the size
273*9880d681SAndroid Build Coastguard Worker  of a long double, then ABI alignment, and then ABI preferred alignment.
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard WorkerTarget Registration
276*9880d681SAndroid Build Coastguard Worker===================
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard WorkerYou must also register your target with the ``TargetRegistry``, which is what
279*9880d681SAndroid Build Coastguard Workerother LLVM tools use to be able to lookup and use your target at runtime.  The
280*9880d681SAndroid Build Coastguard Worker``TargetRegistry`` can be used directly, but for most targets there are helper
281*9880d681SAndroid Build Coastguard Workertemplates which should take care of the work for you.
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard WorkerAll targets should declare a global ``Target`` object which is used to
284*9880d681SAndroid Build Coastguard Workerrepresent the target during registration.  Then, in the target's ``TargetInfo``
285*9880d681SAndroid Build Coastguard Workerlibrary, the target should define that object and use the ``RegisterTarget``
286*9880d681SAndroid Build Coastguard Workertemplate to register the target.  For example, the Sparc registration code
287*9880d681SAndroid Build Coastguard Workerlooks like this:
288*9880d681SAndroid Build Coastguard Worker
289*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
290*9880d681SAndroid Build Coastguard Worker
291*9880d681SAndroid Build Coastguard Worker  Target llvm::TheSparcTarget;
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker  extern "C" void LLVMInitializeSparcTargetInfo() {
294*9880d681SAndroid Build Coastguard Worker    RegisterTarget<Triple::sparc, /*HasJIT=*/false>
295*9880d681SAndroid Build Coastguard Worker      X(TheSparcTarget, "sparc", "Sparc");
296*9880d681SAndroid Build Coastguard Worker  }
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard WorkerThis allows the ``TargetRegistry`` to look up the target by name or by target
299*9880d681SAndroid Build Coastguard Workertriple.  In addition, most targets will also register additional features which
300*9880d681SAndroid Build Coastguard Workerare available in separate libraries.  These registration steps are separate,
301*9880d681SAndroid Build Coastguard Workerbecause some clients may wish to only link in some parts of the target --- the
302*9880d681SAndroid Build Coastguard WorkerJIT code generator does not require the use of the assembler printer, for
303*9880d681SAndroid Build Coastguard Workerexample.  Here is an example of registering the Sparc assembly printer:
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker  extern "C" void LLVMInitializeSparcAsmPrinter() {
308*9880d681SAndroid Build Coastguard Worker    RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
309*9880d681SAndroid Build Coastguard Worker  }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard WorkerFor more information, see "`llvm/Target/TargetRegistry.h
312*9880d681SAndroid Build Coastguard Worker</doxygen/TargetRegistry_8h-source.html>`_".
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard WorkerRegister Set and Register Classes
315*9880d681SAndroid Build Coastguard Worker=================================
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard WorkerYou should describe a concrete target-specific class that represents the
318*9880d681SAndroid Build Coastguard Workerregister file of a target machine.  This class is called ``XXXRegisterInfo``
319*9880d681SAndroid Build Coastguard Worker(where ``XXX`` identifies the target) and represents the class register file
320*9880d681SAndroid Build Coastguard Workerdata that is used for register allocation.  It also describes the interactions
321*9880d681SAndroid Build Coastguard Workerbetween registers.
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard WorkerYou also need to define register classes to categorize related registers.  A
324*9880d681SAndroid Build Coastguard Workerregister class should be added for groups of registers that are all treated the
325*9880d681SAndroid Build Coastguard Workersame way for some instruction.  Typical examples are register classes for
326*9880d681SAndroid Build Coastguard Workerinteger, floating-point, or vector registers.  A register allocator allows an
327*9880d681SAndroid Build Coastguard Workerinstruction to use any register in a specified register class to perform the
328*9880d681SAndroid Build Coastguard Workerinstruction in a similar manner.  Register classes allocate virtual registers
329*9880d681SAndroid Build Coastguard Workerto instructions from these sets, and register classes let the
330*9880d681SAndroid Build Coastguard Workertarget-independent register allocator automatically choose the actual
331*9880d681SAndroid Build Coastguard Workerregisters.
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard WorkerMuch of the code for registers, including register definition, register
334*9880d681SAndroid Build Coastguard Workeraliases, and register classes, is generated by TableGen from
335*9880d681SAndroid Build Coastguard Worker``XXXRegisterInfo.td`` input files and placed in ``XXXGenRegisterInfo.h.inc``
336*9880d681SAndroid Build Coastguard Workerand ``XXXGenRegisterInfo.inc`` output files.  Some of the code in the
337*9880d681SAndroid Build Coastguard Workerimplementation of ``XXXRegisterInfo`` requires hand-coding.
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard WorkerDefining a Register
340*9880d681SAndroid Build Coastguard Worker-------------------
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard WorkerThe ``XXXRegisterInfo.td`` file typically starts with register definitions for
343*9880d681SAndroid Build Coastguard Workera target machine.  The ``Register`` class (specified in ``Target.td``) is used
344*9880d681SAndroid Build Coastguard Workerto define an object for each register.  The specified string ``n`` becomes the
345*9880d681SAndroid Build Coastguard Worker``Name`` of the register.  The basic ``Register`` object does not have any
346*9880d681SAndroid Build Coastguard Workersubregisters and does not specify any aliases.
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker  class Register<string n> {
351*9880d681SAndroid Build Coastguard Worker    string Namespace = "";
352*9880d681SAndroid Build Coastguard Worker    string AsmName = n;
353*9880d681SAndroid Build Coastguard Worker    string Name = n;
354*9880d681SAndroid Build Coastguard Worker    int SpillSize = 0;
355*9880d681SAndroid Build Coastguard Worker    int SpillAlignment = 0;
356*9880d681SAndroid Build Coastguard Worker    list<Register> Aliases = [];
357*9880d681SAndroid Build Coastguard Worker    list<Register> SubRegs = [];
358*9880d681SAndroid Build Coastguard Worker    list<int> DwarfNumbers = [];
359*9880d681SAndroid Build Coastguard Worker  }
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard WorkerFor example, in the ``X86RegisterInfo.td`` file, there are register definitions
362*9880d681SAndroid Build Coastguard Workerthat utilize the ``Register`` class, such as:
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Worker  def AL : Register<"AL">, DwarfRegNum<[0, 0, 0]>;
367*9880d681SAndroid Build Coastguard Worker
368*9880d681SAndroid Build Coastguard WorkerThis defines the register ``AL`` and assigns it values (with ``DwarfRegNum``)
369*9880d681SAndroid Build Coastguard Workerthat are used by ``gcc``, ``gdb``, or a debug information writer to identify a
370*9880d681SAndroid Build Coastguard Workerregister.  For register ``AL``, ``DwarfRegNum`` takes an array of 3 values
371*9880d681SAndroid Build Coastguard Workerrepresenting 3 different modes: the first element is for X86-64, the second for
372*9880d681SAndroid Build Coastguard Workerexception handling (EH) on X86-32, and the third is generic. -1 is a special
373*9880d681SAndroid Build Coastguard WorkerDwarf number that indicates the gcc number is undefined, and -2 indicates the
374*9880d681SAndroid Build Coastguard Workerregister number is invalid for this mode.
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard WorkerFrom the previously described line in the ``X86RegisterInfo.td`` file, TableGen
377*9880d681SAndroid Build Coastguard Workergenerates this code in the ``X86GenRegisterInfo.inc`` file:
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
380*9880d681SAndroid Build Coastguard Worker
381*9880d681SAndroid Build Coastguard Worker  static const unsigned GR8[] = { X86::AL, ... };
382*9880d681SAndroid Build Coastguard Worker
383*9880d681SAndroid Build Coastguard Worker  const unsigned AL_AliasSet[] = { X86::AX, X86::EAX, X86::RAX, 0 };
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker  const TargetRegisterDesc RegisterDescriptors[] = {
386*9880d681SAndroid Build Coastguard Worker    ...
387*9880d681SAndroid Build Coastguard Worker  { "AL", "AL", AL_AliasSet, Empty_SubRegsSet, Empty_SubRegsSet, AL_SuperRegsSet }, ...
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard WorkerFrom the register info file, TableGen generates a ``TargetRegisterDesc`` object
390*9880d681SAndroid Build Coastguard Workerfor each register.  ``TargetRegisterDesc`` is defined in
391*9880d681SAndroid Build Coastguard Worker``include/llvm/Target/TargetRegisterInfo.h`` with the following fields:
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker  struct TargetRegisterDesc {
396*9880d681SAndroid Build Coastguard Worker    const char     *AsmName;      // Assembly language name for the register
397*9880d681SAndroid Build Coastguard Worker    const char     *Name;         // Printable name for the reg (for debugging)
398*9880d681SAndroid Build Coastguard Worker    const unsigned *AliasSet;     // Register Alias Set
399*9880d681SAndroid Build Coastguard Worker    const unsigned *SubRegs;      // Sub-register set
400*9880d681SAndroid Build Coastguard Worker    const unsigned *ImmSubRegs;   // Immediate sub-register set
401*9880d681SAndroid Build Coastguard Worker    const unsigned *SuperRegs;    // Super-register set
402*9880d681SAndroid Build Coastguard Worker  };
403*9880d681SAndroid Build Coastguard Worker
404*9880d681SAndroid Build Coastguard WorkerTableGen uses the entire target description file (``.td``) to determine text
405*9880d681SAndroid Build Coastguard Workernames for the register (in the ``AsmName`` and ``Name`` fields of
406*9880d681SAndroid Build Coastguard Worker``TargetRegisterDesc``) and the relationships of other registers to the defined
407*9880d681SAndroid Build Coastguard Workerregister (in the other ``TargetRegisterDesc`` fields).  In this example, other
408*9880d681SAndroid Build Coastguard Workerdefinitions establish the registers "``AX``", "``EAX``", and "``RAX``" as
409*9880d681SAndroid Build Coastguard Workeraliases for one another, so TableGen generates a null-terminated array
410*9880d681SAndroid Build Coastguard Worker(``AL_AliasSet``) for this register alias set.
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard WorkerThe ``Register`` class is commonly used as a base class for more complex
413*9880d681SAndroid Build Coastguard Workerclasses.  In ``Target.td``, the ``Register`` class is the base for the
414*9880d681SAndroid Build Coastguard Worker``RegisterWithSubRegs`` class that is used to define registers that need to
415*9880d681SAndroid Build Coastguard Workerspecify subregisters in the ``SubRegs`` list, as shown here:
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker  class RegisterWithSubRegs<string n, list<Register> subregs> : Register<n> {
420*9880d681SAndroid Build Coastguard Worker    let SubRegs = subregs;
421*9880d681SAndroid Build Coastguard Worker  }
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard WorkerIn ``SparcRegisterInfo.td``, additional register classes are defined for SPARC:
424*9880d681SAndroid Build Coastguard Workera ``Register`` subclass, ``SparcReg``, and further subclasses: ``Ri``, ``Rf``,
425*9880d681SAndroid Build Coastguard Workerand ``Rd``.  SPARC registers are identified by 5-bit ID numbers, which is a
426*9880d681SAndroid Build Coastguard Workerfeature common to these subclasses.  Note the use of "``let``" expressions to
427*9880d681SAndroid Build Coastguard Workeroverride values that are initially defined in a superclass (such as ``SubRegs``
428*9880d681SAndroid Build Coastguard Workerfield in the ``Rd`` class).
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker  class SparcReg<string n> : Register<n> {
433*9880d681SAndroid Build Coastguard Worker    field bits<5> Num;
434*9880d681SAndroid Build Coastguard Worker    let Namespace = "SP";
435*9880d681SAndroid Build Coastguard Worker  }
436*9880d681SAndroid Build Coastguard Worker  // Ri - 32-bit integer registers
437*9880d681SAndroid Build Coastguard Worker  class Ri<bits<5> num, string n> :
438*9880d681SAndroid Build Coastguard Worker  SparcReg<n> {
439*9880d681SAndroid Build Coastguard Worker    let Num = num;
440*9880d681SAndroid Build Coastguard Worker  }
441*9880d681SAndroid Build Coastguard Worker  // Rf - 32-bit floating-point registers
442*9880d681SAndroid Build Coastguard Worker  class Rf<bits<5> num, string n> :
443*9880d681SAndroid Build Coastguard Worker  SparcReg<n> {
444*9880d681SAndroid Build Coastguard Worker    let Num = num;
445*9880d681SAndroid Build Coastguard Worker  }
446*9880d681SAndroid Build Coastguard Worker  // Rd - Slots in the FP register file for 64-bit floating-point values.
447*9880d681SAndroid Build Coastguard Worker  class Rd<bits<5> num, string n, list<Register> subregs> : SparcReg<n> {
448*9880d681SAndroid Build Coastguard Worker    let Num = num;
449*9880d681SAndroid Build Coastguard Worker    let SubRegs = subregs;
450*9880d681SAndroid Build Coastguard Worker  }
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard WorkerIn the ``SparcRegisterInfo.td`` file, there are register definitions that
453*9880d681SAndroid Build Coastguard Workerutilize these subclasses of ``Register``, such as:
454*9880d681SAndroid Build Coastguard Worker
455*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker  def G0 : Ri< 0, "G0">, DwarfRegNum<[0]>;
458*9880d681SAndroid Build Coastguard Worker  def G1 : Ri< 1, "G1">, DwarfRegNum<[1]>;
459*9880d681SAndroid Build Coastguard Worker  ...
460*9880d681SAndroid Build Coastguard Worker  def F0 : Rf< 0, "F0">, DwarfRegNum<[32]>;
461*9880d681SAndroid Build Coastguard Worker  def F1 : Rf< 1, "F1">, DwarfRegNum<[33]>;
462*9880d681SAndroid Build Coastguard Worker  ...
463*9880d681SAndroid Build Coastguard Worker  def D0 : Rd< 0, "F0", [F0, F1]>, DwarfRegNum<[32]>;
464*9880d681SAndroid Build Coastguard Worker  def D1 : Rd< 2, "F2", [F2, F3]>, DwarfRegNum<[34]>;
465*9880d681SAndroid Build Coastguard Worker
466*9880d681SAndroid Build Coastguard WorkerThe last two registers shown above (``D0`` and ``D1``) are double-precision
467*9880d681SAndroid Build Coastguard Workerfloating-point registers that are aliases for pairs of single-precision
468*9880d681SAndroid Build Coastguard Workerfloating-point sub-registers.  In addition to aliases, the sub-register and
469*9880d681SAndroid Build Coastguard Workersuper-register relationships of the defined register are in fields of a
470*9880d681SAndroid Build Coastguard Workerregister's ``TargetRegisterDesc``.
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard WorkerDefining a Register Class
473*9880d681SAndroid Build Coastguard Worker-------------------------
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard WorkerThe ``RegisterClass`` class (specified in ``Target.td``) is used to define an
476*9880d681SAndroid Build Coastguard Workerobject that represents a group of related registers and also defines the
477*9880d681SAndroid Build Coastguard Workerdefault allocation order of the registers.  A target description file
478*9880d681SAndroid Build Coastguard Worker``XXXRegisterInfo.td`` that uses ``Target.td`` can construct register classes
479*9880d681SAndroid Build Coastguard Workerusing the following class:
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
482*9880d681SAndroid Build Coastguard Worker
483*9880d681SAndroid Build Coastguard Worker  class RegisterClass<string namespace,
484*9880d681SAndroid Build Coastguard Worker  list<ValueType> regTypes, int alignment, dag regList> {
485*9880d681SAndroid Build Coastguard Worker    string Namespace = namespace;
486*9880d681SAndroid Build Coastguard Worker    list<ValueType> RegTypes = regTypes;
487*9880d681SAndroid Build Coastguard Worker    int Size = 0;  // spill size, in bits; zero lets tblgen pick the size
488*9880d681SAndroid Build Coastguard Worker    int Alignment = alignment;
489*9880d681SAndroid Build Coastguard Worker
490*9880d681SAndroid Build Coastguard Worker    // CopyCost is the cost of copying a value between two registers
491*9880d681SAndroid Build Coastguard Worker    // default value 1 means a single instruction
492*9880d681SAndroid Build Coastguard Worker    // A negative value means copying is extremely expensive or impossible
493*9880d681SAndroid Build Coastguard Worker    int CopyCost = 1;
494*9880d681SAndroid Build Coastguard Worker    dag MemberList = regList;
495*9880d681SAndroid Build Coastguard Worker
496*9880d681SAndroid Build Coastguard Worker    // for register classes that are subregisters of this class
497*9880d681SAndroid Build Coastguard Worker    list<RegisterClass> SubRegClassList = [];
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker    code MethodProtos = [{}];  // to insert arbitrary code
500*9880d681SAndroid Build Coastguard Worker    code MethodBodies = [{}];
501*9880d681SAndroid Build Coastguard Worker  }
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard WorkerTo define a ``RegisterClass``, use the following 4 arguments:
504*9880d681SAndroid Build Coastguard Worker
505*9880d681SAndroid Build Coastguard Worker* The first argument of the definition is the name of the namespace.
506*9880d681SAndroid Build Coastguard Worker
507*9880d681SAndroid Build Coastguard Worker* The second argument is a list of ``ValueType`` register type values that are
508*9880d681SAndroid Build Coastguard Worker  defined in ``include/llvm/CodeGen/ValueTypes.td``.  Defined values include
509*9880d681SAndroid Build Coastguard Worker  integer types (such as ``i16``, ``i32``, and ``i1`` for Boolean),
510*9880d681SAndroid Build Coastguard Worker  floating-point types (``f32``, ``f64``), and vector types (for example,
511*9880d681SAndroid Build Coastguard Worker  ``v8i16`` for an ``8 x i16`` vector).  All registers in a ``RegisterClass``
512*9880d681SAndroid Build Coastguard Worker  must have the same ``ValueType``, but some registers may store vector data in
513*9880d681SAndroid Build Coastguard Worker  different configurations.  For example a register that can process a 128-bit
514*9880d681SAndroid Build Coastguard Worker  vector may be able to handle 16 8-bit integer elements, 8 16-bit integers, 4
515*9880d681SAndroid Build Coastguard Worker  32-bit integers, and so on.
516*9880d681SAndroid Build Coastguard Worker
517*9880d681SAndroid Build Coastguard Worker* The third argument of the ``RegisterClass`` definition specifies the
518*9880d681SAndroid Build Coastguard Worker  alignment required of the registers when they are stored or loaded to
519*9880d681SAndroid Build Coastguard Worker  memory.
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard Worker* The final argument, ``regList``, specifies which registers are in this class.
522*9880d681SAndroid Build Coastguard Worker  If an alternative allocation order method is not specified, then ``regList``
523*9880d681SAndroid Build Coastguard Worker  also defines the order of allocation used by the register allocator.  Besides
524*9880d681SAndroid Build Coastguard Worker  simply listing registers with ``(add R0, R1, ...)``, more advanced set
525*9880d681SAndroid Build Coastguard Worker  operators are available.  See ``include/llvm/Target/Target.td`` for more
526*9880d681SAndroid Build Coastguard Worker  information.
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard WorkerIn ``SparcRegisterInfo.td``, three ``RegisterClass`` objects are defined:
529*9880d681SAndroid Build Coastguard Worker``FPRegs``, ``DFPRegs``, and ``IntRegs``.  For all three register classes, the
530*9880d681SAndroid Build Coastguard Workerfirst argument defines the namespace with the string "``SP``".  ``FPRegs``
531*9880d681SAndroid Build Coastguard Workerdefines a group of 32 single-precision floating-point registers (``F0`` to
532*9880d681SAndroid Build Coastguard Worker``F31``); ``DFPRegs`` defines a group of 16 double-precision registers
533*9880d681SAndroid Build Coastguard Worker(``D0-D15``).
534*9880d681SAndroid Build Coastguard Worker
535*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker  // F0, F1, F2, ..., F31
538*9880d681SAndroid Build Coastguard Worker  def FPRegs : RegisterClass<"SP", [f32], 32, (sequence "F%u", 0, 31)>;
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker  def DFPRegs : RegisterClass<"SP", [f64], 64,
541*9880d681SAndroid Build Coastguard Worker                              (add D0, D1, D2, D3, D4, D5, D6, D7, D8,
542*9880d681SAndroid Build Coastguard Worker                                   D9, D10, D11, D12, D13, D14, D15)>;
543*9880d681SAndroid Build Coastguard Worker
544*9880d681SAndroid Build Coastguard Worker  def IntRegs : RegisterClass<"SP", [i32], 32,
545*9880d681SAndroid Build Coastguard Worker      (add L0, L1, L2, L3, L4, L5, L6, L7,
546*9880d681SAndroid Build Coastguard Worker           I0, I1, I2, I3, I4, I5,
547*9880d681SAndroid Build Coastguard Worker           O0, O1, O2, O3, O4, O5, O7,
548*9880d681SAndroid Build Coastguard Worker           G1,
549*9880d681SAndroid Build Coastguard Worker           // Non-allocatable regs:
550*9880d681SAndroid Build Coastguard Worker           G2, G3, G4,
551*9880d681SAndroid Build Coastguard Worker           O6,        // stack ptr
552*9880d681SAndroid Build Coastguard Worker           I6,        // frame ptr
553*9880d681SAndroid Build Coastguard Worker           I7,        // return address
554*9880d681SAndroid Build Coastguard Worker           G0,        // constant zero
555*9880d681SAndroid Build Coastguard Worker           G5, G6, G7 // reserved for kernel
556*9880d681SAndroid Build Coastguard Worker      )>;
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard WorkerUsing ``SparcRegisterInfo.td`` with TableGen generates several output files
559*9880d681SAndroid Build Coastguard Workerthat are intended for inclusion in other source code that you write.
560*9880d681SAndroid Build Coastguard Worker``SparcRegisterInfo.td`` generates ``SparcGenRegisterInfo.h.inc``, which should
561*9880d681SAndroid Build Coastguard Workerbe included in the header file for the implementation of the SPARC register
562*9880d681SAndroid Build Coastguard Workerimplementation that you write (``SparcRegisterInfo.h``).  In
563*9880d681SAndroid Build Coastguard Worker``SparcGenRegisterInfo.h.inc`` a new structure is defined called
564*9880d681SAndroid Build Coastguard Worker``SparcGenRegisterInfo`` that uses ``TargetRegisterInfo`` as its base.  It also
565*9880d681SAndroid Build Coastguard Workerspecifies types, based upon the defined register classes: ``DFPRegsClass``,
566*9880d681SAndroid Build Coastguard Worker``FPRegsClass``, and ``IntRegsClass``.
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker``SparcRegisterInfo.td`` also generates ``SparcGenRegisterInfo.inc``, which is
569*9880d681SAndroid Build Coastguard Workerincluded at the bottom of ``SparcRegisterInfo.cpp``, the SPARC register
570*9880d681SAndroid Build Coastguard Workerimplementation.  The code below shows only the generated integer registers and
571*9880d681SAndroid Build Coastguard Workerassociated register classes.  The order of registers in ``IntRegs`` reflects
572*9880d681SAndroid Build Coastguard Workerthe order in the definition of ``IntRegs`` in the target description file.
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker  // IntRegs Register Class...
577*9880d681SAndroid Build Coastguard Worker  static const unsigned IntRegs[] = {
578*9880d681SAndroid Build Coastguard Worker    SP::L0, SP::L1, SP::L2, SP::L3, SP::L4, SP::L5,
579*9880d681SAndroid Build Coastguard Worker    SP::L6, SP::L7, SP::I0, SP::I1, SP::I2, SP::I3,
580*9880d681SAndroid Build Coastguard Worker    SP::I4, SP::I5, SP::O0, SP::O1, SP::O2, SP::O3,
581*9880d681SAndroid Build Coastguard Worker    SP::O4, SP::O5, SP::O7, SP::G1, SP::G2, SP::G3,
582*9880d681SAndroid Build Coastguard Worker    SP::G4, SP::O6, SP::I6, SP::I7, SP::G0, SP::G5,
583*9880d681SAndroid Build Coastguard Worker    SP::G6, SP::G7,
584*9880d681SAndroid Build Coastguard Worker  };
585*9880d681SAndroid Build Coastguard Worker
586*9880d681SAndroid Build Coastguard Worker  // IntRegsVTs Register Class Value Types...
587*9880d681SAndroid Build Coastguard Worker  static const MVT::ValueType IntRegsVTs[] = {
588*9880d681SAndroid Build Coastguard Worker    MVT::i32, MVT::Other
589*9880d681SAndroid Build Coastguard Worker  };
590*9880d681SAndroid Build Coastguard Worker
591*9880d681SAndroid Build Coastguard Worker  namespace SP {   // Register class instances
592*9880d681SAndroid Build Coastguard Worker    DFPRegsClass    DFPRegsRegClass;
593*9880d681SAndroid Build Coastguard Worker    FPRegsClass     FPRegsRegClass;
594*9880d681SAndroid Build Coastguard Worker    IntRegsClass    IntRegsRegClass;
595*9880d681SAndroid Build Coastguard Worker  ...
596*9880d681SAndroid Build Coastguard Worker    // IntRegs Sub-register Classess...
597*9880d681SAndroid Build Coastguard Worker    static const TargetRegisterClass* const IntRegsSubRegClasses [] = {
598*9880d681SAndroid Build Coastguard Worker      NULL
599*9880d681SAndroid Build Coastguard Worker    };
600*9880d681SAndroid Build Coastguard Worker  ...
601*9880d681SAndroid Build Coastguard Worker    // IntRegs Super-register Classess...
602*9880d681SAndroid Build Coastguard Worker    static const TargetRegisterClass* const IntRegsSuperRegClasses [] = {
603*9880d681SAndroid Build Coastguard Worker      NULL
604*9880d681SAndroid Build Coastguard Worker    };
605*9880d681SAndroid Build Coastguard Worker  ...
606*9880d681SAndroid Build Coastguard Worker    // IntRegs Register Class sub-classes...
607*9880d681SAndroid Build Coastguard Worker    static const TargetRegisterClass* const IntRegsSubclasses [] = {
608*9880d681SAndroid Build Coastguard Worker      NULL
609*9880d681SAndroid Build Coastguard Worker    };
610*9880d681SAndroid Build Coastguard Worker  ...
611*9880d681SAndroid Build Coastguard Worker    // IntRegs Register Class super-classes...
612*9880d681SAndroid Build Coastguard Worker    static const TargetRegisterClass* const IntRegsSuperclasses [] = {
613*9880d681SAndroid Build Coastguard Worker      NULL
614*9880d681SAndroid Build Coastguard Worker    };
615*9880d681SAndroid Build Coastguard Worker
616*9880d681SAndroid Build Coastguard Worker    IntRegsClass::IntRegsClass() : TargetRegisterClass(IntRegsRegClassID,
617*9880d681SAndroid Build Coastguard Worker      IntRegsVTs, IntRegsSubclasses, IntRegsSuperclasses, IntRegsSubRegClasses,
618*9880d681SAndroid Build Coastguard Worker      IntRegsSuperRegClasses, 4, 4, 1, IntRegs, IntRegs + 32) {}
619*9880d681SAndroid Build Coastguard Worker  }
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard WorkerThe register allocators will avoid using reserved registers, and callee saved
622*9880d681SAndroid Build Coastguard Workerregisters are not used until all the volatile registers have been used.  That
623*9880d681SAndroid Build Coastguard Workeris usually good enough, but in some cases it may be necessary to provide custom
624*9880d681SAndroid Build Coastguard Workerallocation orders.
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard WorkerImplement a subclass of ``TargetRegisterInfo``
627*9880d681SAndroid Build Coastguard Worker----------------------------------------------
628*9880d681SAndroid Build Coastguard Worker
629*9880d681SAndroid Build Coastguard WorkerThe final step is to hand code portions of ``XXXRegisterInfo``, which
630*9880d681SAndroid Build Coastguard Workerimplements the interface described in ``TargetRegisterInfo.h`` (see
631*9880d681SAndroid Build Coastguard Worker:ref:`TargetRegisterInfo`).  These functions return ``0``, ``NULL``, or
632*9880d681SAndroid Build Coastguard Worker``false``, unless overridden.  Here is a list of functions that are overridden
633*9880d681SAndroid Build Coastguard Workerfor the SPARC implementation in ``SparcRegisterInfo.cpp``:
634*9880d681SAndroid Build Coastguard Worker
635*9880d681SAndroid Build Coastguard Worker* ``getCalleeSavedRegs`` --- Returns a list of callee-saved registers in the
636*9880d681SAndroid Build Coastguard Worker  order of the desired callee-save stack frame offset.
637*9880d681SAndroid Build Coastguard Worker
638*9880d681SAndroid Build Coastguard Worker* ``getReservedRegs`` --- Returns a bitset indexed by physical register
639*9880d681SAndroid Build Coastguard Worker  numbers, indicating if a particular register is unavailable.
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard Worker* ``hasFP`` --- Return a Boolean indicating if a function should have a
642*9880d681SAndroid Build Coastguard Worker  dedicated frame pointer register.
643*9880d681SAndroid Build Coastguard Worker
644*9880d681SAndroid Build Coastguard Worker* ``eliminateCallFramePseudoInstr`` --- If call frame setup or destroy pseudo
645*9880d681SAndroid Build Coastguard Worker  instructions are used, this can be called to eliminate them.
646*9880d681SAndroid Build Coastguard Worker
647*9880d681SAndroid Build Coastguard Worker* ``eliminateFrameIndex`` --- Eliminate abstract frame indices from
648*9880d681SAndroid Build Coastguard Worker  instructions that may use them.
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker* ``emitPrologue`` --- Insert prologue code into the function.
651*9880d681SAndroid Build Coastguard Worker
652*9880d681SAndroid Build Coastguard Worker* ``emitEpilogue`` --- Insert epilogue code into the function.
653*9880d681SAndroid Build Coastguard Worker
654*9880d681SAndroid Build Coastguard Worker.. _instruction-set:
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard WorkerInstruction Set
657*9880d681SAndroid Build Coastguard Worker===============
658*9880d681SAndroid Build Coastguard Worker
659*9880d681SAndroid Build Coastguard WorkerDuring the early stages of code generation, the LLVM IR code is converted to a
660*9880d681SAndroid Build Coastguard Worker``SelectionDAG`` with nodes that are instances of the ``SDNode`` class
661*9880d681SAndroid Build Coastguard Workercontaining target instructions.  An ``SDNode`` has an opcode, operands, type
662*9880d681SAndroid Build Coastguard Workerrequirements, and operation properties.  For example, is an operation
663*9880d681SAndroid Build Coastguard Workercommutative, does an operation load from memory.  The various operation node
664*9880d681SAndroid Build Coastguard Workertypes are described in the ``include/llvm/CodeGen/SelectionDAGNodes.h`` file
665*9880d681SAndroid Build Coastguard Worker(values of the ``NodeType`` enum in the ``ISD`` namespace).
666*9880d681SAndroid Build Coastguard Worker
667*9880d681SAndroid Build Coastguard WorkerTableGen uses the following target description (``.td``) input files to
668*9880d681SAndroid Build Coastguard Workergenerate much of the code for instruction definition:
669*9880d681SAndroid Build Coastguard Worker
670*9880d681SAndroid Build Coastguard Worker* ``Target.td`` --- Where the ``Instruction``, ``Operand``, ``InstrInfo``, and
671*9880d681SAndroid Build Coastguard Worker  other fundamental classes are defined.
672*9880d681SAndroid Build Coastguard Worker
673*9880d681SAndroid Build Coastguard Worker* ``TargetSelectionDAG.td`` --- Used by ``SelectionDAG`` instruction selection
674*9880d681SAndroid Build Coastguard Worker  generators, contains ``SDTC*`` classes (selection DAG type constraint),
675*9880d681SAndroid Build Coastguard Worker  definitions of ``SelectionDAG`` nodes (such as ``imm``, ``cond``, ``bb``,
676*9880d681SAndroid Build Coastguard Worker  ``add``, ``fadd``, ``sub``), and pattern support (``Pattern``, ``Pat``,
677*9880d681SAndroid Build Coastguard Worker  ``PatFrag``, ``PatLeaf``, ``ComplexPattern``.
678*9880d681SAndroid Build Coastguard Worker
679*9880d681SAndroid Build Coastguard Worker* ``XXXInstrFormats.td`` --- Patterns for definitions of target-specific
680*9880d681SAndroid Build Coastguard Worker  instructions.
681*9880d681SAndroid Build Coastguard Worker
682*9880d681SAndroid Build Coastguard Worker* ``XXXInstrInfo.td`` --- Target-specific definitions of instruction templates,
683*9880d681SAndroid Build Coastguard Worker  condition codes, and instructions of an instruction set.  For architecture
684*9880d681SAndroid Build Coastguard Worker  modifications, a different file name may be used.  For example, for Pentium
685*9880d681SAndroid Build Coastguard Worker  with SSE instruction, this file is ``X86InstrSSE.td``, and for Pentium with
686*9880d681SAndroid Build Coastguard Worker  MMX, this file is ``X86InstrMMX.td``.
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard WorkerThere is also a target-specific ``XXX.td`` file, where ``XXX`` is the name of
689*9880d681SAndroid Build Coastguard Workerthe target.  The ``XXX.td`` file includes the other ``.td`` input files, but
690*9880d681SAndroid Build Coastguard Workerits contents are only directly important for subtargets.
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard WorkerYou should describe a concrete target-specific class ``XXXInstrInfo`` that
693*9880d681SAndroid Build Coastguard Workerrepresents machine instructions supported by a target machine.
694*9880d681SAndroid Build Coastguard Worker``XXXInstrInfo`` contains an array of ``XXXInstrDescriptor`` objects, each of
695*9880d681SAndroid Build Coastguard Workerwhich describes one instruction.  An instruction descriptor defines:
696*9880d681SAndroid Build Coastguard Worker
697*9880d681SAndroid Build Coastguard Worker* Opcode mnemonic
698*9880d681SAndroid Build Coastguard Worker* Number of operands
699*9880d681SAndroid Build Coastguard Worker* List of implicit register definitions and uses
700*9880d681SAndroid Build Coastguard Worker* Target-independent properties (such as memory access, is commutable)
701*9880d681SAndroid Build Coastguard Worker* Target-specific flags
702*9880d681SAndroid Build Coastguard Worker
703*9880d681SAndroid Build Coastguard WorkerThe Instruction class (defined in ``Target.td``) is mostly used as a base for
704*9880d681SAndroid Build Coastguard Workermore complex instruction classes.
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker  class Instruction {
709*9880d681SAndroid Build Coastguard Worker    string Namespace = "";
710*9880d681SAndroid Build Coastguard Worker    dag OutOperandList;    // A dag containing the MI def operand list.
711*9880d681SAndroid Build Coastguard Worker    dag InOperandList;     // A dag containing the MI use operand list.
712*9880d681SAndroid Build Coastguard Worker    string AsmString = ""; // The .s format to print the instruction with.
713*9880d681SAndroid Build Coastguard Worker    list<dag> Pattern;     // Set to the DAG pattern for this instruction.
714*9880d681SAndroid Build Coastguard Worker    list<Register> Uses = [];
715*9880d681SAndroid Build Coastguard Worker    list<Register> Defs = [];
716*9880d681SAndroid Build Coastguard Worker    list<Predicate> Predicates = [];  // predicates turned into isel match code
717*9880d681SAndroid Build Coastguard Worker    ... remainder not shown for space ...
718*9880d681SAndroid Build Coastguard Worker  }
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard WorkerA ``SelectionDAG`` node (``SDNode``) should contain an object representing a
721*9880d681SAndroid Build Coastguard Workertarget-specific instruction that is defined in ``XXXInstrInfo.td``.  The
722*9880d681SAndroid Build Coastguard Workerinstruction objects should represent instructions from the architecture manual
723*9880d681SAndroid Build Coastguard Workerof the target machine (such as the SPARC Architecture Manual for the SPARC
724*9880d681SAndroid Build Coastguard Workertarget).
725*9880d681SAndroid Build Coastguard Worker
726*9880d681SAndroid Build Coastguard WorkerA single instruction from the architecture manual is often modeled as multiple
727*9880d681SAndroid Build Coastguard Workertarget instructions, depending upon its operands.  For example, a manual might
728*9880d681SAndroid Build Coastguard Workerdescribe an add instruction that takes a register or an immediate operand.  An
729*9880d681SAndroid Build Coastguard WorkerLLVM target could model this with two instructions named ``ADDri`` and
730*9880d681SAndroid Build Coastguard Worker``ADDrr``.
731*9880d681SAndroid Build Coastguard Worker
732*9880d681SAndroid Build Coastguard WorkerYou should define a class for each instruction category and define each opcode
733*9880d681SAndroid Build Coastguard Workeras a subclass of the category with appropriate parameters such as the fixed
734*9880d681SAndroid Build Coastguard Workerbinary encoding of opcodes and extended opcodes.  You should map the register
735*9880d681SAndroid Build Coastguard Workerbits to the bits of the instruction in which they are encoded (for the JIT).
736*9880d681SAndroid Build Coastguard WorkerAlso you should specify how the instruction should be printed when the
737*9880d681SAndroid Build Coastguard Workerautomatic assembly printer is used.
738*9880d681SAndroid Build Coastguard Worker
739*9880d681SAndroid Build Coastguard WorkerAs is described in the SPARC Architecture Manual, Version 8, there are three
740*9880d681SAndroid Build Coastguard Workermajor 32-bit formats for instructions.  Format 1 is only for the ``CALL``
741*9880d681SAndroid Build Coastguard Workerinstruction.  Format 2 is for branch on condition codes and ``SETHI`` (set high
742*9880d681SAndroid Build Coastguard Workerbits of a register) instructions.  Format 3 is for other instructions.
743*9880d681SAndroid Build Coastguard Worker
744*9880d681SAndroid Build Coastguard WorkerEach of these formats has corresponding classes in ``SparcInstrFormat.td``.
745*9880d681SAndroid Build Coastguard Worker``InstSP`` is a base class for other instruction classes.  Additional base
746*9880d681SAndroid Build Coastguard Workerclasses are specified for more precise formats: for example in
747*9880d681SAndroid Build Coastguard Worker``SparcInstrFormat.td``, ``F2_1`` is for ``SETHI``, and ``F2_2`` is for
748*9880d681SAndroid Build Coastguard Workerbranches.  There are three other base classes: ``F3_1`` for register/register
749*9880d681SAndroid Build Coastguard Workeroperations, ``F3_2`` for register/immediate operations, and ``F3_3`` for
750*9880d681SAndroid Build Coastguard Workerfloating-point operations.  ``SparcInstrInfo.td`` also adds the base class
751*9880d681SAndroid Build Coastguard Worker``Pseudo`` for synthetic SPARC instructions.
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker``SparcInstrInfo.td`` largely consists of operand and instruction definitions
754*9880d681SAndroid Build Coastguard Workerfor the SPARC target.  In ``SparcInstrInfo.td``, the following target
755*9880d681SAndroid Build Coastguard Workerdescription file entry, ``LDrr``, defines the Load Integer instruction for a
756*9880d681SAndroid Build Coastguard WorkerWord (the ``LD`` SPARC opcode) from a memory address to a register.  The first
757*9880d681SAndroid Build Coastguard Workerparameter, the value 3 (``11``\ :sub:`2`), is the operation value for this
758*9880d681SAndroid Build Coastguard Workercategory of operation.  The second parameter (``000000``\ :sub:`2`) is the
759*9880d681SAndroid Build Coastguard Workerspecific operation value for ``LD``/Load Word.  The third parameter is the
760*9880d681SAndroid Build Coastguard Workeroutput destination, which is a register operand and defined in the ``Register``
761*9880d681SAndroid Build Coastguard Workertarget description file (``IntRegs``).
762*9880d681SAndroid Build Coastguard Worker
763*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker  def LDrr : F3_1 <3, 0b000000, (outs IntRegs:$dst), (ins MEMrr:$addr),
766*9880d681SAndroid Build Coastguard Worker                   "ld [$addr], $dst",
767*9880d681SAndroid Build Coastguard Worker                   [(set i32:$dst, (load ADDRrr:$addr))]>;
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard WorkerThe fourth parameter is the input source, which uses the address operand
770*9880d681SAndroid Build Coastguard Worker``MEMrr`` that is defined earlier in ``SparcInstrInfo.td``:
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
773*9880d681SAndroid Build Coastguard Worker
774*9880d681SAndroid Build Coastguard Worker  def MEMrr : Operand<i32> {
775*9880d681SAndroid Build Coastguard Worker    let PrintMethod = "printMemOperand";
776*9880d681SAndroid Build Coastguard Worker    let MIOperandInfo = (ops IntRegs, IntRegs);
777*9880d681SAndroid Build Coastguard Worker  }
778*9880d681SAndroid Build Coastguard Worker
779*9880d681SAndroid Build Coastguard WorkerThe fifth parameter is a string that is used by the assembly printer and can be
780*9880d681SAndroid Build Coastguard Workerleft as an empty string until the assembly printer interface is implemented.
781*9880d681SAndroid Build Coastguard WorkerThe sixth and final parameter is the pattern used to match the instruction
782*9880d681SAndroid Build Coastguard Workerduring the SelectionDAG Select Phase described in :doc:`CodeGenerator`.
783*9880d681SAndroid Build Coastguard WorkerThis parameter is detailed in the next section, :ref:`instruction-selector`.
784*9880d681SAndroid Build Coastguard Worker
785*9880d681SAndroid Build Coastguard WorkerInstruction class definitions are not overloaded for different operand types,
786*9880d681SAndroid Build Coastguard Workerso separate versions of instructions are needed for register, memory, or
787*9880d681SAndroid Build Coastguard Workerimmediate value operands.  For example, to perform a Load Integer instruction
788*9880d681SAndroid Build Coastguard Workerfor a Word from an immediate operand to a register, the following instruction
789*9880d681SAndroid Build Coastguard Workerclass is defined:
790*9880d681SAndroid Build Coastguard Worker
791*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
792*9880d681SAndroid Build Coastguard Worker
793*9880d681SAndroid Build Coastguard Worker  def LDri : F3_2 <3, 0b000000, (outs IntRegs:$dst), (ins MEMri:$addr),
794*9880d681SAndroid Build Coastguard Worker                   "ld [$addr], $dst",
795*9880d681SAndroid Build Coastguard Worker                   [(set i32:$dst, (load ADDRri:$addr))]>;
796*9880d681SAndroid Build Coastguard Worker
797*9880d681SAndroid Build Coastguard WorkerWriting these definitions for so many similar instructions can involve a lot of
798*9880d681SAndroid Build Coastguard Workercut and paste.  In ``.td`` files, the ``multiclass`` directive enables the
799*9880d681SAndroid Build Coastguard Workercreation of templates to define several instruction classes at once (using the
800*9880d681SAndroid Build Coastguard Worker``defm`` directive).  For example in ``SparcInstrInfo.td``, the ``multiclass``
801*9880d681SAndroid Build Coastguard Workerpattern ``F3_12`` is defined to create 2 instruction classes each time
802*9880d681SAndroid Build Coastguard Worker``F3_12`` is invoked:
803*9880d681SAndroid Build Coastguard Worker
804*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker  multiclass F3_12 <string OpcStr, bits<6> Op3Val, SDNode OpNode> {
807*9880d681SAndroid Build Coastguard Worker    def rr  : F3_1 <2, Op3Val,
808*9880d681SAndroid Build Coastguard Worker                   (outs IntRegs:$dst), (ins IntRegs:$b, IntRegs:$c),
809*9880d681SAndroid Build Coastguard Worker                   !strconcat(OpcStr, " $b, $c, $dst"),
810*9880d681SAndroid Build Coastguard Worker                   [(set i32:$dst, (OpNode i32:$b, i32:$c))]>;
811*9880d681SAndroid Build Coastguard Worker    def ri  : F3_2 <2, Op3Val,
812*9880d681SAndroid Build Coastguard Worker                   (outs IntRegs:$dst), (ins IntRegs:$b, i32imm:$c),
813*9880d681SAndroid Build Coastguard Worker                   !strconcat(OpcStr, " $b, $c, $dst"),
814*9880d681SAndroid Build Coastguard Worker                   [(set i32:$dst, (OpNode i32:$b, simm13:$c))]>;
815*9880d681SAndroid Build Coastguard Worker  }
816*9880d681SAndroid Build Coastguard Worker
817*9880d681SAndroid Build Coastguard WorkerSo when the ``defm`` directive is used for the ``XOR`` and ``ADD``
818*9880d681SAndroid Build Coastguard Workerinstructions, as seen below, it creates four instruction objects: ``XORrr``,
819*9880d681SAndroid Build Coastguard Worker``XORri``, ``ADDrr``, and ``ADDri``.
820*9880d681SAndroid Build Coastguard Worker
821*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard Worker  defm XOR   : F3_12<"xor", 0b000011, xor>;
824*9880d681SAndroid Build Coastguard Worker  defm ADD   : F3_12<"add", 0b000000, add>;
825*9880d681SAndroid Build Coastguard Worker
826*9880d681SAndroid Build Coastguard Worker``SparcInstrInfo.td`` also includes definitions for condition codes that are
827*9880d681SAndroid Build Coastguard Workerreferenced by branch instructions.  The following definitions in
828*9880d681SAndroid Build Coastguard Worker``SparcInstrInfo.td`` indicate the bit location of the SPARC condition code.
829*9880d681SAndroid Build Coastguard WorkerFor example, the 10\ :sup:`th` bit represents the "greater than" condition for
830*9880d681SAndroid Build Coastguard Workerintegers, and the 22\ :sup:`nd` bit represents the "greater than" condition for
831*9880d681SAndroid Build Coastguard Workerfloats.
832*9880d681SAndroid Build Coastguard Worker
833*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
834*9880d681SAndroid Build Coastguard Worker
835*9880d681SAndroid Build Coastguard Worker  def ICC_NE  : ICC_VAL< 9>;  // Not Equal
836*9880d681SAndroid Build Coastguard Worker  def ICC_E   : ICC_VAL< 1>;  // Equal
837*9880d681SAndroid Build Coastguard Worker  def ICC_G   : ICC_VAL<10>;  // Greater
838*9880d681SAndroid Build Coastguard Worker  ...
839*9880d681SAndroid Build Coastguard Worker  def FCC_U   : FCC_VAL<23>;  // Unordered
840*9880d681SAndroid Build Coastguard Worker  def FCC_G   : FCC_VAL<22>;  // Greater
841*9880d681SAndroid Build Coastguard Worker  def FCC_UG  : FCC_VAL<21>;  // Unordered or Greater
842*9880d681SAndroid Build Coastguard Worker  ...
843*9880d681SAndroid Build Coastguard Worker
844*9880d681SAndroid Build Coastguard Worker(Note that ``Sparc.h`` also defines enums that correspond to the same SPARC
845*9880d681SAndroid Build Coastguard Workercondition codes.  Care must be taken to ensure the values in ``Sparc.h``
846*9880d681SAndroid Build Coastguard Workercorrespond to the values in ``SparcInstrInfo.td``.  I.e., ``SPCC::ICC_NE = 9``,
847*9880d681SAndroid Build Coastguard Worker``SPCC::FCC_U = 23`` and so on.)
848*9880d681SAndroid Build Coastguard Worker
849*9880d681SAndroid Build Coastguard WorkerInstruction Operand Mapping
850*9880d681SAndroid Build Coastguard Worker---------------------------
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard WorkerThe code generator backend maps instruction operands to fields in the
853*9880d681SAndroid Build Coastguard Workerinstruction.  Operands are assigned to unbound fields in the instruction in the
854*9880d681SAndroid Build Coastguard Workerorder they are defined.  Fields are bound when they are assigned a value.  For
855*9880d681SAndroid Build Coastguard Workerexample, the Sparc target defines the ``XNORrr`` instruction as a ``F3_1``
856*9880d681SAndroid Build Coastguard Workerformat instruction having three operands.
857*9880d681SAndroid Build Coastguard Worker
858*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
859*9880d681SAndroid Build Coastguard Worker
860*9880d681SAndroid Build Coastguard Worker  def XNORrr  : F3_1<2, 0b000111,
861*9880d681SAndroid Build Coastguard Worker                     (outs IntRegs:$dst), (ins IntRegs:$b, IntRegs:$c),
862*9880d681SAndroid Build Coastguard Worker                     "xnor $b, $c, $dst",
863*9880d681SAndroid Build Coastguard Worker                     [(set i32:$dst, (not (xor i32:$b, i32:$c)))]>;
864*9880d681SAndroid Build Coastguard Worker
865*9880d681SAndroid Build Coastguard WorkerThe instruction templates in ``SparcInstrFormats.td`` show the base class for
866*9880d681SAndroid Build Coastguard Worker``F3_1`` is ``InstSP``.
867*9880d681SAndroid Build Coastguard Worker
868*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
869*9880d681SAndroid Build Coastguard Worker
870*9880d681SAndroid Build Coastguard Worker  class InstSP<dag outs, dag ins, string asmstr, list<dag> pattern> : Instruction {
871*9880d681SAndroid Build Coastguard Worker    field bits<32> Inst;
872*9880d681SAndroid Build Coastguard Worker    let Namespace = "SP";
873*9880d681SAndroid Build Coastguard Worker    bits<2> op;
874*9880d681SAndroid Build Coastguard Worker    let Inst{31-30} = op;
875*9880d681SAndroid Build Coastguard Worker    dag OutOperandList = outs;
876*9880d681SAndroid Build Coastguard Worker    dag InOperandList = ins;
877*9880d681SAndroid Build Coastguard Worker    let AsmString   = asmstr;
878*9880d681SAndroid Build Coastguard Worker    let Pattern = pattern;
879*9880d681SAndroid Build Coastguard Worker  }
880*9880d681SAndroid Build Coastguard Worker
881*9880d681SAndroid Build Coastguard Worker``InstSP`` leaves the ``op`` field unbound.
882*9880d681SAndroid Build Coastguard Worker
883*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
884*9880d681SAndroid Build Coastguard Worker
885*9880d681SAndroid Build Coastguard Worker  class F3<dag outs, dag ins, string asmstr, list<dag> pattern>
886*9880d681SAndroid Build Coastguard Worker      : InstSP<outs, ins, asmstr, pattern> {
887*9880d681SAndroid Build Coastguard Worker    bits<5> rd;
888*9880d681SAndroid Build Coastguard Worker    bits<6> op3;
889*9880d681SAndroid Build Coastguard Worker    bits<5> rs1;
890*9880d681SAndroid Build Coastguard Worker    let op{1} = 1;   // Op = 2 or 3
891*9880d681SAndroid Build Coastguard Worker    let Inst{29-25} = rd;
892*9880d681SAndroid Build Coastguard Worker    let Inst{24-19} = op3;
893*9880d681SAndroid Build Coastguard Worker    let Inst{18-14} = rs1;
894*9880d681SAndroid Build Coastguard Worker  }
895*9880d681SAndroid Build Coastguard Worker
896*9880d681SAndroid Build Coastguard Worker``F3`` binds the ``op`` field and defines the ``rd``, ``op3``, and ``rs1``
897*9880d681SAndroid Build Coastguard Workerfields.  ``F3`` format instructions will bind the operands ``rd``, ``op3``, and
898*9880d681SAndroid Build Coastguard Worker``rs1`` fields.
899*9880d681SAndroid Build Coastguard Worker
900*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
901*9880d681SAndroid Build Coastguard Worker
902*9880d681SAndroid Build Coastguard Worker  class F3_1<bits<2> opVal, bits<6> op3val, dag outs, dag ins,
903*9880d681SAndroid Build Coastguard Worker             string asmstr, list<dag> pattern> : F3<outs, ins, asmstr, pattern> {
904*9880d681SAndroid Build Coastguard Worker    bits<8> asi = 0; // asi not currently used
905*9880d681SAndroid Build Coastguard Worker    bits<5> rs2;
906*9880d681SAndroid Build Coastguard Worker    let op         = opVal;
907*9880d681SAndroid Build Coastguard Worker    let op3        = op3val;
908*9880d681SAndroid Build Coastguard Worker    let Inst{13}   = 0;     // i field = 0
909*9880d681SAndroid Build Coastguard Worker    let Inst{12-5} = asi;   // address space identifier
910*9880d681SAndroid Build Coastguard Worker    let Inst{4-0}  = rs2;
911*9880d681SAndroid Build Coastguard Worker  }
912*9880d681SAndroid Build Coastguard Worker
913*9880d681SAndroid Build Coastguard Worker``F3_1`` binds the ``op3`` field and defines the ``rs2`` fields.  ``F3_1``
914*9880d681SAndroid Build Coastguard Workerformat instructions will bind the operands to the ``rd``, ``rs1``, and ``rs2``
915*9880d681SAndroid Build Coastguard Workerfields.  This results in the ``XNORrr`` instruction binding ``$dst``, ``$b``,
916*9880d681SAndroid Build Coastguard Workerand ``$c`` operands to the ``rd``, ``rs1``, and ``rs2`` fields respectively.
917*9880d681SAndroid Build Coastguard Worker
918*9880d681SAndroid Build Coastguard WorkerInstruction Operand Name Mapping
919*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
920*9880d681SAndroid Build Coastguard Worker
921*9880d681SAndroid Build Coastguard WorkerTableGen will also generate a function called getNamedOperandIdx() which
922*9880d681SAndroid Build Coastguard Workercan be used to look up an operand's index in a MachineInstr based on its
923*9880d681SAndroid Build Coastguard WorkerTableGen name.  Setting the UseNamedOperandTable bit in an instruction's
924*9880d681SAndroid Build Coastguard WorkerTableGen definition will add all of its operands to an enumeration in the
925*9880d681SAndroid Build Coastguard Workerllvm::XXX:OpName namespace and also add an entry for it into the OperandMap
926*9880d681SAndroid Build Coastguard Workertable, which can be queried using getNamedOperandIdx()
927*9880d681SAndroid Build Coastguard Worker
928*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
929*9880d681SAndroid Build Coastguard Worker
930*9880d681SAndroid Build Coastguard Worker  int DstIndex = SP::getNamedOperandIdx(SP::XNORrr, SP::OpName::dst); // => 0
931*9880d681SAndroid Build Coastguard Worker  int BIndex = SP::getNamedOperandIdx(SP::XNORrr, SP::OpName::b);     // => 1
932*9880d681SAndroid Build Coastguard Worker  int CIndex = SP::getNamedOperandIdx(SP::XNORrr, SP::OpName::c);     // => 2
933*9880d681SAndroid Build Coastguard Worker  int DIndex = SP::getNamedOperandIdx(SP::XNORrr, SP::OpName::d);     // => -1
934*9880d681SAndroid Build Coastguard Worker
935*9880d681SAndroid Build Coastguard Worker  ...
936*9880d681SAndroid Build Coastguard Worker
937*9880d681SAndroid Build Coastguard WorkerThe entries in the OpName enum are taken verbatim from the TableGen definitions,
938*9880d681SAndroid Build Coastguard Workerso operands with lowercase names will have lower case entries in the enum.
939*9880d681SAndroid Build Coastguard Worker
940*9880d681SAndroid Build Coastguard WorkerTo include the getNamedOperandIdx() function in your backend, you will need
941*9880d681SAndroid Build Coastguard Workerto define a few preprocessor macros in XXXInstrInfo.cpp and XXXInstrInfo.h.
942*9880d681SAndroid Build Coastguard WorkerFor example:
943*9880d681SAndroid Build Coastguard Worker
944*9880d681SAndroid Build Coastguard WorkerXXXInstrInfo.cpp:
945*9880d681SAndroid Build Coastguard Worker
946*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
947*9880d681SAndroid Build Coastguard Worker
948*9880d681SAndroid Build Coastguard Worker  #define GET_INSTRINFO_NAMED_OPS // For getNamedOperandIdx() function
949*9880d681SAndroid Build Coastguard Worker  #include "XXXGenInstrInfo.inc"
950*9880d681SAndroid Build Coastguard Worker
951*9880d681SAndroid Build Coastguard WorkerXXXInstrInfo.h:
952*9880d681SAndroid Build Coastguard Worker
953*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
954*9880d681SAndroid Build Coastguard Worker
955*9880d681SAndroid Build Coastguard Worker  #define GET_INSTRINFO_OPERAND_ENUM // For OpName enum
956*9880d681SAndroid Build Coastguard Worker  #include "XXXGenInstrInfo.inc"
957*9880d681SAndroid Build Coastguard Worker
958*9880d681SAndroid Build Coastguard Worker  namespace XXX {
959*9880d681SAndroid Build Coastguard Worker    int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex);
960*9880d681SAndroid Build Coastguard Worker  } // End namespace XXX
961*9880d681SAndroid Build Coastguard Worker
962*9880d681SAndroid Build Coastguard WorkerInstruction Operand Types
963*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^
964*9880d681SAndroid Build Coastguard Worker
965*9880d681SAndroid Build Coastguard WorkerTableGen will also generate an enumeration consisting of all named Operand
966*9880d681SAndroid Build Coastguard Workertypes defined in the backend, in the llvm::XXX::OpTypes namespace.
967*9880d681SAndroid Build Coastguard WorkerSome common immediate Operand types (for instance i8, i32, i64, f32, f64)
968*9880d681SAndroid Build Coastguard Workerare defined for all targets in ``include/llvm/Target/Target.td``, and are
969*9880d681SAndroid Build Coastguard Workeravailable in each Target's OpTypes enum.  Also, only named Operand types appear
970*9880d681SAndroid Build Coastguard Workerin the enumeration: anonymous types are ignored.
971*9880d681SAndroid Build Coastguard WorkerFor example, the X86 backend defines ``brtarget`` and ``brtarget8``, both
972*9880d681SAndroid Build Coastguard Workerinstances of the TableGen ``Operand`` class, which represent branch target
973*9880d681SAndroid Build Coastguard Workeroperands:
974*9880d681SAndroid Build Coastguard Worker
975*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
976*9880d681SAndroid Build Coastguard Worker
977*9880d681SAndroid Build Coastguard Worker  def brtarget : Operand<OtherVT>;
978*9880d681SAndroid Build Coastguard Worker  def brtarget8 : Operand<OtherVT>;
979*9880d681SAndroid Build Coastguard Worker
980*9880d681SAndroid Build Coastguard WorkerThis results in:
981*9880d681SAndroid Build Coastguard Worker
982*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
983*9880d681SAndroid Build Coastguard Worker
984*9880d681SAndroid Build Coastguard Worker  namespace X86 {
985*9880d681SAndroid Build Coastguard Worker  namespace OpTypes {
986*9880d681SAndroid Build Coastguard Worker  enum OperandType {
987*9880d681SAndroid Build Coastguard Worker    ...
988*9880d681SAndroid Build Coastguard Worker    brtarget,
989*9880d681SAndroid Build Coastguard Worker    brtarget8,
990*9880d681SAndroid Build Coastguard Worker    ...
991*9880d681SAndroid Build Coastguard Worker    i32imm,
992*9880d681SAndroid Build Coastguard Worker    i64imm,
993*9880d681SAndroid Build Coastguard Worker    ...
994*9880d681SAndroid Build Coastguard Worker    OPERAND_TYPE_LIST_END
995*9880d681SAndroid Build Coastguard Worker  } // End namespace OpTypes
996*9880d681SAndroid Build Coastguard Worker  } // End namespace X86
997*9880d681SAndroid Build Coastguard Worker
998*9880d681SAndroid Build Coastguard WorkerIn typical TableGen fashion, to use the enum, you will need to define a
999*9880d681SAndroid Build Coastguard Workerpreprocessor macro:
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1002*9880d681SAndroid Build Coastguard Worker
1003*9880d681SAndroid Build Coastguard Worker  #define GET_INSTRINFO_OPERAND_TYPES_ENUM // For OpTypes enum
1004*9880d681SAndroid Build Coastguard Worker  #include "XXXGenInstrInfo.inc"
1005*9880d681SAndroid Build Coastguard Worker
1006*9880d681SAndroid Build Coastguard Worker
1007*9880d681SAndroid Build Coastguard WorkerInstruction Scheduling
1008*9880d681SAndroid Build Coastguard Worker----------------------
1009*9880d681SAndroid Build Coastguard Worker
1010*9880d681SAndroid Build Coastguard WorkerInstruction itineraries can be queried using MCDesc::getSchedClass(). The
1011*9880d681SAndroid Build Coastguard Workervalue can be named by an enumemation in llvm::XXX::Sched namespace generated
1012*9880d681SAndroid Build Coastguard Workerby TableGen in XXXGenInstrInfo.inc. The name of the schedule classes are
1013*9880d681SAndroid Build Coastguard Workerthe same as provided in XXXSchedule.td plus a default NoItinerary class.
1014*9880d681SAndroid Build Coastguard Worker
1015*9880d681SAndroid Build Coastguard WorkerInstruction Relation Mapping
1016*9880d681SAndroid Build Coastguard Worker----------------------------
1017*9880d681SAndroid Build Coastguard Worker
1018*9880d681SAndroid Build Coastguard WorkerThis TableGen feature is used to relate instructions with each other.  It is
1019*9880d681SAndroid Build Coastguard Workerparticularly useful when you have multiple instruction formats and need to
1020*9880d681SAndroid Build Coastguard Workerswitch between them after instruction selection.  This entire feature is driven
1021*9880d681SAndroid Build Coastguard Workerby relation models which can be defined in ``XXXInstrInfo.td`` files
1022*9880d681SAndroid Build Coastguard Workeraccording to the target-specific instruction set.  Relation models are defined
1023*9880d681SAndroid Build Coastguard Workerusing ``InstrMapping`` class as a base.  TableGen parses all the models
1024*9880d681SAndroid Build Coastguard Workerand generates instruction relation maps using the specified information.
1025*9880d681SAndroid Build Coastguard WorkerRelation maps are emitted as tables in the ``XXXGenInstrInfo.inc`` file
1026*9880d681SAndroid Build Coastguard Workeralong with the functions to query them.  For the detailed information on how to
1027*9880d681SAndroid Build Coastguard Workeruse this feature, please refer to :doc:`HowToUseInstrMappings`.
1028*9880d681SAndroid Build Coastguard Worker
1029*9880d681SAndroid Build Coastguard WorkerImplement a subclass of ``TargetInstrInfo``
1030*9880d681SAndroid Build Coastguard Worker-------------------------------------------
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard WorkerThe final step is to hand code portions of ``XXXInstrInfo``, which implements
1033*9880d681SAndroid Build Coastguard Workerthe interface described in ``TargetInstrInfo.h`` (see :ref:`TargetInstrInfo`).
1034*9880d681SAndroid Build Coastguard WorkerThese functions return ``0`` or a Boolean or they assert, unless overridden.
1035*9880d681SAndroid Build Coastguard WorkerHere's a list of functions that are overridden for the SPARC implementation in
1036*9880d681SAndroid Build Coastguard Worker``SparcInstrInfo.cpp``:
1037*9880d681SAndroid Build Coastguard Worker
1038*9880d681SAndroid Build Coastguard Worker* ``isLoadFromStackSlot`` --- If the specified machine instruction is a direct
1039*9880d681SAndroid Build Coastguard Worker  load from a stack slot, return the register number of the destination and the
1040*9880d681SAndroid Build Coastguard Worker  ``FrameIndex`` of the stack slot.
1041*9880d681SAndroid Build Coastguard Worker
1042*9880d681SAndroid Build Coastguard Worker* ``isStoreToStackSlot`` --- If the specified machine instruction is a direct
1043*9880d681SAndroid Build Coastguard Worker  store to a stack slot, return the register number of the destination and the
1044*9880d681SAndroid Build Coastguard Worker  ``FrameIndex`` of the stack slot.
1045*9880d681SAndroid Build Coastguard Worker
1046*9880d681SAndroid Build Coastguard Worker* ``copyPhysReg`` --- Copy values between a pair of physical registers.
1047*9880d681SAndroid Build Coastguard Worker
1048*9880d681SAndroid Build Coastguard Worker* ``storeRegToStackSlot`` --- Store a register value to a stack slot.
1049*9880d681SAndroid Build Coastguard Worker
1050*9880d681SAndroid Build Coastguard Worker* ``loadRegFromStackSlot`` --- Load a register value from a stack slot.
1051*9880d681SAndroid Build Coastguard Worker
1052*9880d681SAndroid Build Coastguard Worker* ``storeRegToAddr`` --- Store a register value to memory.
1053*9880d681SAndroid Build Coastguard Worker
1054*9880d681SAndroid Build Coastguard Worker* ``loadRegFromAddr`` --- Load a register value from memory.
1055*9880d681SAndroid Build Coastguard Worker
1056*9880d681SAndroid Build Coastguard Worker* ``foldMemoryOperand`` --- Attempt to combine instructions of any load or
1057*9880d681SAndroid Build Coastguard Worker  store instruction for the specified operand(s).
1058*9880d681SAndroid Build Coastguard Worker
1059*9880d681SAndroid Build Coastguard WorkerBranch Folding and If Conversion
1060*9880d681SAndroid Build Coastguard Worker--------------------------------
1061*9880d681SAndroid Build Coastguard Worker
1062*9880d681SAndroid Build Coastguard WorkerPerformance can be improved by combining instructions or by eliminating
1063*9880d681SAndroid Build Coastguard Workerinstructions that are never reached.  The ``AnalyzeBranch`` method in
1064*9880d681SAndroid Build Coastguard Worker``XXXInstrInfo`` may be implemented to examine conditional instructions and
1065*9880d681SAndroid Build Coastguard Workerremove unnecessary instructions.  ``AnalyzeBranch`` looks at the end of a
1066*9880d681SAndroid Build Coastguard Workermachine basic block (MBB) for opportunities for improvement, such as branch
1067*9880d681SAndroid Build Coastguard Workerfolding and if conversion.  The ``BranchFolder`` and ``IfConverter`` machine
1068*9880d681SAndroid Build Coastguard Workerfunction passes (see the source files ``BranchFolding.cpp`` and
1069*9880d681SAndroid Build Coastguard Worker``IfConversion.cpp`` in the ``lib/CodeGen`` directory) call ``AnalyzeBranch``
1070*9880d681SAndroid Build Coastguard Workerto improve the control flow graph that represents the instructions.
1071*9880d681SAndroid Build Coastguard Worker
1072*9880d681SAndroid Build Coastguard WorkerSeveral implementations of ``AnalyzeBranch`` (for ARM, Alpha, and X86) can be
1073*9880d681SAndroid Build Coastguard Workerexamined as models for your own ``AnalyzeBranch`` implementation.  Since SPARC
1074*9880d681SAndroid Build Coastguard Workerdoes not implement a useful ``AnalyzeBranch``, the ARM target implementation is
1075*9880d681SAndroid Build Coastguard Workershown below.
1076*9880d681SAndroid Build Coastguard Worker
1077*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` returns a Boolean value and takes four parameters:
1078*9880d681SAndroid Build Coastguard Worker
1079*9880d681SAndroid Build Coastguard Worker* ``MachineBasicBlock &MBB`` --- The incoming block to be examined.
1080*9880d681SAndroid Build Coastguard Worker
1081*9880d681SAndroid Build Coastguard Worker* ``MachineBasicBlock *&TBB`` --- A destination block that is returned.  For a
1082*9880d681SAndroid Build Coastguard Worker  conditional branch that evaluates to true, ``TBB`` is the destination.
1083*9880d681SAndroid Build Coastguard Worker
1084*9880d681SAndroid Build Coastguard Worker* ``MachineBasicBlock *&FBB`` --- For a conditional branch that evaluates to
1085*9880d681SAndroid Build Coastguard Worker  false, ``FBB`` is returned as the destination.
1086*9880d681SAndroid Build Coastguard Worker
1087*9880d681SAndroid Build Coastguard Worker* ``std::vector<MachineOperand> &Cond`` --- List of operands to evaluate a
1088*9880d681SAndroid Build Coastguard Worker  condition for a conditional branch.
1089*9880d681SAndroid Build Coastguard Worker
1090*9880d681SAndroid Build Coastguard WorkerIn the simplest case, if a block ends without a branch, then it falls through
1091*9880d681SAndroid Build Coastguard Workerto the successor block.  No destination blocks are specified for either ``TBB``
1092*9880d681SAndroid Build Coastguard Workeror ``FBB``, so both parameters return ``NULL``.  The start of the
1093*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` (see code below for the ARM target) shows the function
1094*9880d681SAndroid Build Coastguard Workerparameters and the code for the simplest case.
1095*9880d681SAndroid Build Coastguard Worker
1096*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1097*9880d681SAndroid Build Coastguard Worker
1098*9880d681SAndroid Build Coastguard Worker  bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
1099*9880d681SAndroid Build Coastguard Worker                                   MachineBasicBlock *&TBB,
1100*9880d681SAndroid Build Coastguard Worker                                   MachineBasicBlock *&FBB,
1101*9880d681SAndroid Build Coastguard Worker                                   std::vector<MachineOperand> &Cond) const
1102*9880d681SAndroid Build Coastguard Worker  {
1103*9880d681SAndroid Build Coastguard Worker    MachineBasicBlock::iterator I = MBB.end();
1104*9880d681SAndroid Build Coastguard Worker    if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
1105*9880d681SAndroid Build Coastguard Worker      return false;
1106*9880d681SAndroid Build Coastguard Worker
1107*9880d681SAndroid Build Coastguard WorkerIf a block ends with a single unconditional branch instruction, then
1108*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` (shown below) should return the destination of that branch in
1109*9880d681SAndroid Build Coastguard Workerthe ``TBB`` parameter.
1110*9880d681SAndroid Build Coastguard Worker
1111*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1112*9880d681SAndroid Build Coastguard Worker
1113*9880d681SAndroid Build Coastguard Worker    if (LastOpc == ARM::B || LastOpc == ARM::tB) {
1114*9880d681SAndroid Build Coastguard Worker      TBB = LastInst->getOperand(0).getMBB();
1115*9880d681SAndroid Build Coastguard Worker      return false;
1116*9880d681SAndroid Build Coastguard Worker    }
1117*9880d681SAndroid Build Coastguard Worker
1118*9880d681SAndroid Build Coastguard WorkerIf a block ends with two unconditional branches, then the second branch is
1119*9880d681SAndroid Build Coastguard Workernever reached.  In that situation, as shown below, remove the last branch
1120*9880d681SAndroid Build Coastguard Workerinstruction and return the penultimate branch in the ``TBB`` parameter.
1121*9880d681SAndroid Build Coastguard Worker
1122*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1123*9880d681SAndroid Build Coastguard Worker
1124*9880d681SAndroid Build Coastguard Worker    if ((SecondLastOpc == ARM::B || SecondLastOpc == ARM::tB) &&
1125*9880d681SAndroid Build Coastguard Worker        (LastOpc == ARM::B || LastOpc == ARM::tB)) {
1126*9880d681SAndroid Build Coastguard Worker      TBB = SecondLastInst->getOperand(0).getMBB();
1127*9880d681SAndroid Build Coastguard Worker      I = LastInst;
1128*9880d681SAndroid Build Coastguard Worker      I->eraseFromParent();
1129*9880d681SAndroid Build Coastguard Worker      return false;
1130*9880d681SAndroid Build Coastguard Worker    }
1131*9880d681SAndroid Build Coastguard Worker
1132*9880d681SAndroid Build Coastguard WorkerA block may end with a single conditional branch instruction that falls through
1133*9880d681SAndroid Build Coastguard Workerto successor block if the condition evaluates to false.  In that case,
1134*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` (shown below) should return the destination of that
1135*9880d681SAndroid Build Coastguard Workerconditional branch in the ``TBB`` parameter and a list of operands in the
1136*9880d681SAndroid Build Coastguard Worker``Cond`` parameter to evaluate the condition.
1137*9880d681SAndroid Build Coastguard Worker
1138*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1139*9880d681SAndroid Build Coastguard Worker
1140*9880d681SAndroid Build Coastguard Worker    if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
1141*9880d681SAndroid Build Coastguard Worker      // Block ends with fall-through condbranch.
1142*9880d681SAndroid Build Coastguard Worker      TBB = LastInst->getOperand(0).getMBB();
1143*9880d681SAndroid Build Coastguard Worker      Cond.push_back(LastInst->getOperand(1));
1144*9880d681SAndroid Build Coastguard Worker      Cond.push_back(LastInst->getOperand(2));
1145*9880d681SAndroid Build Coastguard Worker      return false;
1146*9880d681SAndroid Build Coastguard Worker    }
1147*9880d681SAndroid Build Coastguard Worker
1148*9880d681SAndroid Build Coastguard WorkerIf a block ends with both a conditional branch and an ensuing unconditional
1149*9880d681SAndroid Build Coastguard Workerbranch, then ``AnalyzeBranch`` (shown below) should return the conditional
1150*9880d681SAndroid Build Coastguard Workerbranch destination (assuming it corresponds to a conditional evaluation of
1151*9880d681SAndroid Build Coastguard Worker"``true``") in the ``TBB`` parameter and the unconditional branch destination
1152*9880d681SAndroid Build Coastguard Workerin the ``FBB`` (corresponding to a conditional evaluation of "``false``").  A
1153*9880d681SAndroid Build Coastguard Workerlist of operands to evaluate the condition should be returned in the ``Cond``
1154*9880d681SAndroid Build Coastguard Workerparameter.
1155*9880d681SAndroid Build Coastguard Worker
1156*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1157*9880d681SAndroid Build Coastguard Worker
1158*9880d681SAndroid Build Coastguard Worker    unsigned SecondLastOpc = SecondLastInst->getOpcode();
1159*9880d681SAndroid Build Coastguard Worker
1160*9880d681SAndroid Build Coastguard Worker    if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
1161*9880d681SAndroid Build Coastguard Worker        (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
1162*9880d681SAndroid Build Coastguard Worker      TBB =  SecondLastInst->getOperand(0).getMBB();
1163*9880d681SAndroid Build Coastguard Worker      Cond.push_back(SecondLastInst->getOperand(1));
1164*9880d681SAndroid Build Coastguard Worker      Cond.push_back(SecondLastInst->getOperand(2));
1165*9880d681SAndroid Build Coastguard Worker      FBB = LastInst->getOperand(0).getMBB();
1166*9880d681SAndroid Build Coastguard Worker      return false;
1167*9880d681SAndroid Build Coastguard Worker    }
1168*9880d681SAndroid Build Coastguard Worker
1169*9880d681SAndroid Build Coastguard WorkerFor the last two cases (ending with a single conditional branch or ending with
1170*9880d681SAndroid Build Coastguard Workerone conditional and one unconditional branch), the operands returned in the
1171*9880d681SAndroid Build Coastguard Worker``Cond`` parameter can be passed to methods of other instructions to create new
1172*9880d681SAndroid Build Coastguard Workerbranches or perform other operations.  An implementation of ``AnalyzeBranch``
1173*9880d681SAndroid Build Coastguard Workerrequires the helper methods ``RemoveBranch`` and ``InsertBranch`` to manage
1174*9880d681SAndroid Build Coastguard Workersubsequent operations.
1175*9880d681SAndroid Build Coastguard Worker
1176*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` should return false indicating success in most circumstances.
1177*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` should only return true when the method is stumped about what
1178*9880d681SAndroid Build Coastguard Workerto do, for example, if a block has three terminating branches.
1179*9880d681SAndroid Build Coastguard Worker``AnalyzeBranch`` may return true if it encounters a terminator it cannot
1180*9880d681SAndroid Build Coastguard Workerhandle, such as an indirect branch.
1181*9880d681SAndroid Build Coastguard Worker
1182*9880d681SAndroid Build Coastguard Worker.. _instruction-selector:
1183*9880d681SAndroid Build Coastguard Worker
1184*9880d681SAndroid Build Coastguard WorkerInstruction Selector
1185*9880d681SAndroid Build Coastguard Worker====================
1186*9880d681SAndroid Build Coastguard Worker
1187*9880d681SAndroid Build Coastguard WorkerLLVM uses a ``SelectionDAG`` to represent LLVM IR instructions, and nodes of
1188*9880d681SAndroid Build Coastguard Workerthe ``SelectionDAG`` ideally represent native target instructions.  During code
1189*9880d681SAndroid Build Coastguard Workergeneration, instruction selection passes are performed to convert non-native
1190*9880d681SAndroid Build Coastguard WorkerDAG instructions into native target-specific instructions.  The pass described
1191*9880d681SAndroid Build Coastguard Workerin ``XXXISelDAGToDAG.cpp`` is used to match patterns and perform DAG-to-DAG
1192*9880d681SAndroid Build Coastguard Workerinstruction selection.  Optionally, a pass may be defined (in
1193*9880d681SAndroid Build Coastguard Worker``XXXBranchSelector.cpp``) to perform similar DAG-to-DAG operations for branch
1194*9880d681SAndroid Build Coastguard Workerinstructions.  Later, the code in ``XXXISelLowering.cpp`` replaces or removes
1195*9880d681SAndroid Build Coastguard Workeroperations and data types not supported natively (legalizes) in a
1196*9880d681SAndroid Build Coastguard Worker``SelectionDAG``.
1197*9880d681SAndroid Build Coastguard Worker
1198*9880d681SAndroid Build Coastguard WorkerTableGen generates code for instruction selection using the following target
1199*9880d681SAndroid Build Coastguard Workerdescription input files:
1200*9880d681SAndroid Build Coastguard Worker
1201*9880d681SAndroid Build Coastguard Worker* ``XXXInstrInfo.td`` --- Contains definitions of instructions in a
1202*9880d681SAndroid Build Coastguard Worker  target-specific instruction set, generates ``XXXGenDAGISel.inc``, which is
1203*9880d681SAndroid Build Coastguard Worker  included in ``XXXISelDAGToDAG.cpp``.
1204*9880d681SAndroid Build Coastguard Worker
1205*9880d681SAndroid Build Coastguard Worker* ``XXXCallingConv.td`` --- Contains the calling and return value conventions
1206*9880d681SAndroid Build Coastguard Worker  for the target architecture, and it generates ``XXXGenCallingConv.inc``,
1207*9880d681SAndroid Build Coastguard Worker  which is included in ``XXXISelLowering.cpp``.
1208*9880d681SAndroid Build Coastguard Worker
1209*9880d681SAndroid Build Coastguard WorkerThe implementation of an instruction selection pass must include a header that
1210*9880d681SAndroid Build Coastguard Workerdeclares the ``FunctionPass`` class or a subclass of ``FunctionPass``.  In
1211*9880d681SAndroid Build Coastguard Worker``XXXTargetMachine.cpp``, a Pass Manager (PM) should add each instruction
1212*9880d681SAndroid Build Coastguard Workerselection pass into the queue of passes to run.
1213*9880d681SAndroid Build Coastguard Worker
1214*9880d681SAndroid Build Coastguard WorkerThe LLVM static compiler (``llc``) is an excellent tool for visualizing the
1215*9880d681SAndroid Build Coastguard Workercontents of DAGs.  To display the ``SelectionDAG`` before or after specific
1216*9880d681SAndroid Build Coastguard Workerprocessing phases, use the command line options for ``llc``, described at
1217*9880d681SAndroid Build Coastguard Worker:ref:`SelectionDAG-Process`.
1218*9880d681SAndroid Build Coastguard Worker
1219*9880d681SAndroid Build Coastguard WorkerTo describe instruction selector behavior, you should add patterns for lowering
1220*9880d681SAndroid Build Coastguard WorkerLLVM code into a ``SelectionDAG`` as the last parameter of the instruction
1221*9880d681SAndroid Build Coastguard Workerdefinitions in ``XXXInstrInfo.td``.  For example, in ``SparcInstrInfo.td``,
1222*9880d681SAndroid Build Coastguard Workerthis entry defines a register store operation, and the last parameter describes
1223*9880d681SAndroid Build Coastguard Workera pattern with the store DAG operator.
1224*9880d681SAndroid Build Coastguard Worker
1225*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1226*9880d681SAndroid Build Coastguard Worker
1227*9880d681SAndroid Build Coastguard Worker  def STrr  : F3_1< 3, 0b000100, (outs), (ins MEMrr:$addr, IntRegs:$src),
1228*9880d681SAndroid Build Coastguard Worker                   "st $src, [$addr]", [(store i32:$src, ADDRrr:$addr)]>;
1229*9880d681SAndroid Build Coastguard Worker
1230*9880d681SAndroid Build Coastguard Worker``ADDRrr`` is a memory mode that is also defined in ``SparcInstrInfo.td``:
1231*9880d681SAndroid Build Coastguard Worker
1232*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1233*9880d681SAndroid Build Coastguard Worker
1234*9880d681SAndroid Build Coastguard Worker  def ADDRrr : ComplexPattern<i32, 2, "SelectADDRrr", [], []>;
1235*9880d681SAndroid Build Coastguard Worker
1236*9880d681SAndroid Build Coastguard WorkerThe definition of ``ADDRrr`` refers to ``SelectADDRrr``, which is a function
1237*9880d681SAndroid Build Coastguard Workerdefined in an implementation of the Instructor Selector (such as
1238*9880d681SAndroid Build Coastguard Worker``SparcISelDAGToDAG.cpp``).
1239*9880d681SAndroid Build Coastguard Worker
1240*9880d681SAndroid Build Coastguard WorkerIn ``lib/Target/TargetSelectionDAG.td``, the DAG operator for store is defined
1241*9880d681SAndroid Build Coastguard Workerbelow:
1242*9880d681SAndroid Build Coastguard Worker
1243*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1244*9880d681SAndroid Build Coastguard Worker
1245*9880d681SAndroid Build Coastguard Worker  def store : PatFrag<(ops node:$val, node:$ptr),
1246*9880d681SAndroid Build Coastguard Worker                      (st node:$val, node:$ptr), [{
1247*9880d681SAndroid Build Coastguard Worker    if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N))
1248*9880d681SAndroid Build Coastguard Worker      return !ST->isTruncatingStore() &&
1249*9880d681SAndroid Build Coastguard Worker             ST->getAddressingMode() == ISD::UNINDEXED;
1250*9880d681SAndroid Build Coastguard Worker    return false;
1251*9880d681SAndroid Build Coastguard Worker  }]>;
1252*9880d681SAndroid Build Coastguard Worker
1253*9880d681SAndroid Build Coastguard Worker``XXXInstrInfo.td`` also generates (in ``XXXGenDAGISel.inc``) the
1254*9880d681SAndroid Build Coastguard Worker``SelectCode`` method that is used to call the appropriate processing method
1255*9880d681SAndroid Build Coastguard Workerfor an instruction.  In this example, ``SelectCode`` calls ``Select_ISD_STORE``
1256*9880d681SAndroid Build Coastguard Workerfor the ``ISD::STORE`` opcode.
1257*9880d681SAndroid Build Coastguard Worker
1258*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1259*9880d681SAndroid Build Coastguard Worker
1260*9880d681SAndroid Build Coastguard Worker  SDNode *SelectCode(SDValue N) {
1261*9880d681SAndroid Build Coastguard Worker    ...
1262*9880d681SAndroid Build Coastguard Worker    MVT::ValueType NVT = N.getNode()->getValueType(0);
1263*9880d681SAndroid Build Coastguard Worker    switch (N.getOpcode()) {
1264*9880d681SAndroid Build Coastguard Worker    case ISD::STORE: {
1265*9880d681SAndroid Build Coastguard Worker      switch (NVT) {
1266*9880d681SAndroid Build Coastguard Worker      default:
1267*9880d681SAndroid Build Coastguard Worker        return Select_ISD_STORE(N);
1268*9880d681SAndroid Build Coastguard Worker        break;
1269*9880d681SAndroid Build Coastguard Worker      }
1270*9880d681SAndroid Build Coastguard Worker      break;
1271*9880d681SAndroid Build Coastguard Worker    }
1272*9880d681SAndroid Build Coastguard Worker    ...
1273*9880d681SAndroid Build Coastguard Worker
1274*9880d681SAndroid Build Coastguard WorkerThe pattern for ``STrr`` is matched, so elsewhere in ``XXXGenDAGISel.inc``,
1275*9880d681SAndroid Build Coastguard Workercode for ``STrr`` is created for ``Select_ISD_STORE``.  The ``Emit_22`` method
1276*9880d681SAndroid Build Coastguard Workeris also generated in ``XXXGenDAGISel.inc`` to complete the processing of this
1277*9880d681SAndroid Build Coastguard Workerinstruction.
1278*9880d681SAndroid Build Coastguard Worker
1279*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1280*9880d681SAndroid Build Coastguard Worker
1281*9880d681SAndroid Build Coastguard Worker  SDNode *Select_ISD_STORE(const SDValue &N) {
1282*9880d681SAndroid Build Coastguard Worker    SDValue Chain = N.getOperand(0);
1283*9880d681SAndroid Build Coastguard Worker    if (Predicate_store(N.getNode())) {
1284*9880d681SAndroid Build Coastguard Worker      SDValue N1 = N.getOperand(1);
1285*9880d681SAndroid Build Coastguard Worker      SDValue N2 = N.getOperand(2);
1286*9880d681SAndroid Build Coastguard Worker      SDValue CPTmp0;
1287*9880d681SAndroid Build Coastguard Worker      SDValue CPTmp1;
1288*9880d681SAndroid Build Coastguard Worker
1289*9880d681SAndroid Build Coastguard Worker      // Pattern: (st:void i32:i32:$src,
1290*9880d681SAndroid Build Coastguard Worker      //           ADDRrr:i32:$addr)<<P:Predicate_store>>
1291*9880d681SAndroid Build Coastguard Worker      // Emits: (STrr:void ADDRrr:i32:$addr, IntRegs:i32:$src)
1292*9880d681SAndroid Build Coastguard Worker      // Pattern complexity = 13  cost = 1  size = 0
1293*9880d681SAndroid Build Coastguard Worker      if (SelectADDRrr(N, N2, CPTmp0, CPTmp1) &&
1294*9880d681SAndroid Build Coastguard Worker          N1.getNode()->getValueType(0) == MVT::i32 &&
1295*9880d681SAndroid Build Coastguard Worker          N2.getNode()->getValueType(0) == MVT::i32) {
1296*9880d681SAndroid Build Coastguard Worker        return Emit_22(N, SP::STrr, CPTmp0, CPTmp1);
1297*9880d681SAndroid Build Coastguard Worker      }
1298*9880d681SAndroid Build Coastguard Worker  ...
1299*9880d681SAndroid Build Coastguard Worker
1300*9880d681SAndroid Build Coastguard WorkerThe SelectionDAG Legalize Phase
1301*9880d681SAndroid Build Coastguard Worker-------------------------------
1302*9880d681SAndroid Build Coastguard Worker
1303*9880d681SAndroid Build Coastguard WorkerThe Legalize phase converts a DAG to use types and operations that are natively
1304*9880d681SAndroid Build Coastguard Workersupported by the target.  For natively unsupported types and operations, you
1305*9880d681SAndroid Build Coastguard Workerneed to add code to the target-specific ``XXXTargetLowering`` implementation to
1306*9880d681SAndroid Build Coastguard Workerconvert unsupported types and operations to supported ones.
1307*9880d681SAndroid Build Coastguard Worker
1308*9880d681SAndroid Build Coastguard WorkerIn the constructor for the ``XXXTargetLowering`` class, first use the
1309*9880d681SAndroid Build Coastguard Worker``addRegisterClass`` method to specify which types are supported and which
1310*9880d681SAndroid Build Coastguard Workerregister classes are associated with them.  The code for the register classes
1311*9880d681SAndroid Build Coastguard Workerare generated by TableGen from ``XXXRegisterInfo.td`` and placed in
1312*9880d681SAndroid Build Coastguard Worker``XXXGenRegisterInfo.h.inc``.  For example, the implementation of the
1313*9880d681SAndroid Build Coastguard Workerconstructor for the SparcTargetLowering class (in ``SparcISelLowering.cpp``)
1314*9880d681SAndroid Build Coastguard Workerstarts with the following code:
1315*9880d681SAndroid Build Coastguard Worker
1316*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1317*9880d681SAndroid Build Coastguard Worker
1318*9880d681SAndroid Build Coastguard Worker  addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
1319*9880d681SAndroid Build Coastguard Worker  addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
1320*9880d681SAndroid Build Coastguard Worker  addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
1321*9880d681SAndroid Build Coastguard Worker
1322*9880d681SAndroid Build Coastguard WorkerYou should examine the node types in the ``ISD`` namespace
1323*9880d681SAndroid Build Coastguard Worker(``include/llvm/CodeGen/SelectionDAGNodes.h``) and determine which operations
1324*9880d681SAndroid Build Coastguard Workerthe target natively supports.  For operations that do **not** have native
1325*9880d681SAndroid Build Coastguard Workersupport, add a callback to the constructor for the ``XXXTargetLowering`` class,
1326*9880d681SAndroid Build Coastguard Workerso the instruction selection process knows what to do.  The ``TargetLowering``
1327*9880d681SAndroid Build Coastguard Workerclass callback methods (declared in ``llvm/Target/TargetLowering.h``) are:
1328*9880d681SAndroid Build Coastguard Worker
1329*9880d681SAndroid Build Coastguard Worker* ``setOperationAction`` --- General operation.
1330*9880d681SAndroid Build Coastguard Worker* ``setLoadExtAction`` --- Load with extension.
1331*9880d681SAndroid Build Coastguard Worker* ``setTruncStoreAction`` --- Truncating store.
1332*9880d681SAndroid Build Coastguard Worker* ``setIndexedLoadAction`` --- Indexed load.
1333*9880d681SAndroid Build Coastguard Worker* ``setIndexedStoreAction`` --- Indexed store.
1334*9880d681SAndroid Build Coastguard Worker* ``setConvertAction`` --- Type conversion.
1335*9880d681SAndroid Build Coastguard Worker* ``setCondCodeAction`` --- Support for a given condition code.
1336*9880d681SAndroid Build Coastguard Worker
1337*9880d681SAndroid Build Coastguard WorkerNote: on older releases, ``setLoadXAction`` is used instead of
1338*9880d681SAndroid Build Coastguard Worker``setLoadExtAction``.  Also, on older releases, ``setCondCodeAction`` may not
1339*9880d681SAndroid Build Coastguard Workerbe supported.  Examine your release to see what methods are specifically
1340*9880d681SAndroid Build Coastguard Workersupported.
1341*9880d681SAndroid Build Coastguard Worker
1342*9880d681SAndroid Build Coastguard WorkerThese callbacks are used to determine that an operation does or does not work
1343*9880d681SAndroid Build Coastguard Workerwith a specified type (or types).  And in all cases, the third parameter is a
1344*9880d681SAndroid Build Coastguard Worker``LegalAction`` type enum value: ``Promote``, ``Expand``, ``Custom``, or
1345*9880d681SAndroid Build Coastguard Worker``Legal``.  ``SparcISelLowering.cpp`` contains examples of all four
1346*9880d681SAndroid Build Coastguard Worker``LegalAction`` values.
1347*9880d681SAndroid Build Coastguard Worker
1348*9880d681SAndroid Build Coastguard WorkerPromote
1349*9880d681SAndroid Build Coastguard Worker^^^^^^^
1350*9880d681SAndroid Build Coastguard Worker
1351*9880d681SAndroid Build Coastguard WorkerFor an operation without native support for a given type, the specified type
1352*9880d681SAndroid Build Coastguard Workermay be promoted to a larger type that is supported.  For example, SPARC does
1353*9880d681SAndroid Build Coastguard Workernot support a sign-extending load for Boolean values (``i1`` type), so in
1354*9880d681SAndroid Build Coastguard Worker``SparcISelLowering.cpp`` the third parameter below, ``Promote``, changes
1355*9880d681SAndroid Build Coastguard Worker``i1`` type values to a large type before loading.
1356*9880d681SAndroid Build Coastguard Worker
1357*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1358*9880d681SAndroid Build Coastguard Worker
1359*9880d681SAndroid Build Coastguard Worker  setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
1360*9880d681SAndroid Build Coastguard Worker
1361*9880d681SAndroid Build Coastguard WorkerExpand
1362*9880d681SAndroid Build Coastguard Worker^^^^^^
1363*9880d681SAndroid Build Coastguard Worker
1364*9880d681SAndroid Build Coastguard WorkerFor a type without native support, a value may need to be broken down further,
1365*9880d681SAndroid Build Coastguard Workerrather than promoted.  For an operation without native support, a combination
1366*9880d681SAndroid Build Coastguard Workerof other operations may be used to similar effect.  In SPARC, the
1367*9880d681SAndroid Build Coastguard Workerfloating-point sine and cosine trig operations are supported by expansion to
1368*9880d681SAndroid Build Coastguard Workerother operations, as indicated by the third parameter, ``Expand``, to
1369*9880d681SAndroid Build Coastguard Worker``setOperationAction``:
1370*9880d681SAndroid Build Coastguard Worker
1371*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1372*9880d681SAndroid Build Coastguard Worker
1373*9880d681SAndroid Build Coastguard Worker  setOperationAction(ISD::FSIN, MVT::f32, Expand);
1374*9880d681SAndroid Build Coastguard Worker  setOperationAction(ISD::FCOS, MVT::f32, Expand);
1375*9880d681SAndroid Build Coastguard Worker
1376*9880d681SAndroid Build Coastguard WorkerCustom
1377*9880d681SAndroid Build Coastguard Worker^^^^^^
1378*9880d681SAndroid Build Coastguard Worker
1379*9880d681SAndroid Build Coastguard WorkerFor some operations, simple type promotion or operation expansion may be
1380*9880d681SAndroid Build Coastguard Workerinsufficient.  In some cases, a special intrinsic function must be implemented.
1381*9880d681SAndroid Build Coastguard Worker
1382*9880d681SAndroid Build Coastguard WorkerFor example, a constant value may require special treatment, or an operation
1383*9880d681SAndroid Build Coastguard Workermay require spilling and restoring registers in the stack and working with
1384*9880d681SAndroid Build Coastguard Workerregister allocators.
1385*9880d681SAndroid Build Coastguard Worker
1386*9880d681SAndroid Build Coastguard WorkerAs seen in ``SparcISelLowering.cpp`` code below, to perform a type conversion
1387*9880d681SAndroid Build Coastguard Workerfrom a floating point value to a signed integer, first the
1388*9880d681SAndroid Build Coastguard Worker``setOperationAction`` should be called with ``Custom`` as the third parameter:
1389*9880d681SAndroid Build Coastguard Worker
1390*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1391*9880d681SAndroid Build Coastguard Worker
1392*9880d681SAndroid Build Coastguard Worker  setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1393*9880d681SAndroid Build Coastguard Worker
1394*9880d681SAndroid Build Coastguard WorkerIn the ``LowerOperation`` method, for each ``Custom`` operation, a case
1395*9880d681SAndroid Build Coastguard Workerstatement should be added to indicate what function to call.  In the following
1396*9880d681SAndroid Build Coastguard Workercode, an ``FP_TO_SINT`` opcode will call the ``LowerFP_TO_SINT`` method:
1397*9880d681SAndroid Build Coastguard Worker
1398*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1399*9880d681SAndroid Build Coastguard Worker
1400*9880d681SAndroid Build Coastguard Worker  SDValue SparcTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
1401*9880d681SAndroid Build Coastguard Worker    switch (Op.getOpcode()) {
1402*9880d681SAndroid Build Coastguard Worker    case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1403*9880d681SAndroid Build Coastguard Worker    ...
1404*9880d681SAndroid Build Coastguard Worker    }
1405*9880d681SAndroid Build Coastguard Worker  }
1406*9880d681SAndroid Build Coastguard Worker
1407*9880d681SAndroid Build Coastguard WorkerFinally, the ``LowerFP_TO_SINT`` method is implemented, using an FP register to
1408*9880d681SAndroid Build Coastguard Workerconvert the floating-point value to an integer.
1409*9880d681SAndroid Build Coastguard Worker
1410*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1411*9880d681SAndroid Build Coastguard Worker
1412*9880d681SAndroid Build Coastguard Worker  static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
1413*9880d681SAndroid Build Coastguard Worker    assert(Op.getValueType() == MVT::i32);
1414*9880d681SAndroid Build Coastguard Worker    Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
1415*9880d681SAndroid Build Coastguard Worker    return DAG.getNode(ISD::BITCAST, MVT::i32, Op);
1416*9880d681SAndroid Build Coastguard Worker  }
1417*9880d681SAndroid Build Coastguard Worker
1418*9880d681SAndroid Build Coastguard WorkerLegal
1419*9880d681SAndroid Build Coastguard Worker^^^^^
1420*9880d681SAndroid Build Coastguard Worker
1421*9880d681SAndroid Build Coastguard WorkerThe ``Legal`` ``LegalizeAction`` enum value simply indicates that an operation
1422*9880d681SAndroid Build Coastguard Worker**is** natively supported.  ``Legal`` represents the default condition, so it
1423*9880d681SAndroid Build Coastguard Workeris rarely used.  In ``SparcISelLowering.cpp``, the action for ``CTPOP`` (an
1424*9880d681SAndroid Build Coastguard Workeroperation to count the bits set in an integer) is natively supported only for
1425*9880d681SAndroid Build Coastguard WorkerSPARC v9.  The following code enables the ``Expand`` conversion technique for
1426*9880d681SAndroid Build Coastguard Workernon-v9 SPARC implementations.
1427*9880d681SAndroid Build Coastguard Worker
1428*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1429*9880d681SAndroid Build Coastguard Worker
1430*9880d681SAndroid Build Coastguard Worker  setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1431*9880d681SAndroid Build Coastguard Worker  ...
1432*9880d681SAndroid Build Coastguard Worker  if (TM.getSubtarget<SparcSubtarget>().isV9())
1433*9880d681SAndroid Build Coastguard Worker    setOperationAction(ISD::CTPOP, MVT::i32, Legal);
1434*9880d681SAndroid Build Coastguard Worker
1435*9880d681SAndroid Build Coastguard WorkerCalling Conventions
1436*9880d681SAndroid Build Coastguard Worker-------------------
1437*9880d681SAndroid Build Coastguard Worker
1438*9880d681SAndroid Build Coastguard WorkerTo support target-specific calling conventions, ``XXXGenCallingConv.td`` uses
1439*9880d681SAndroid Build Coastguard Workerinterfaces (such as ``CCIfType`` and ``CCAssignToReg``) that are defined in
1440*9880d681SAndroid Build Coastguard Worker``lib/Target/TargetCallingConv.td``.  TableGen can take the target descriptor
1441*9880d681SAndroid Build Coastguard Workerfile ``XXXGenCallingConv.td`` and generate the header file
1442*9880d681SAndroid Build Coastguard Worker``XXXGenCallingConv.inc``, which is typically included in
1443*9880d681SAndroid Build Coastguard Worker``XXXISelLowering.cpp``.  You can use the interfaces in
1444*9880d681SAndroid Build Coastguard Worker``TargetCallingConv.td`` to specify:
1445*9880d681SAndroid Build Coastguard Worker
1446*9880d681SAndroid Build Coastguard Worker* The order of parameter allocation.
1447*9880d681SAndroid Build Coastguard Worker
1448*9880d681SAndroid Build Coastguard Worker* Where parameters and return values are placed (that is, on the stack or in
1449*9880d681SAndroid Build Coastguard Worker  registers).
1450*9880d681SAndroid Build Coastguard Worker
1451*9880d681SAndroid Build Coastguard Worker* Which registers may be used.
1452*9880d681SAndroid Build Coastguard Worker
1453*9880d681SAndroid Build Coastguard Worker* Whether the caller or callee unwinds the stack.
1454*9880d681SAndroid Build Coastguard Worker
1455*9880d681SAndroid Build Coastguard WorkerThe following example demonstrates the use of the ``CCIfType`` and
1456*9880d681SAndroid Build Coastguard Worker``CCAssignToReg`` interfaces.  If the ``CCIfType`` predicate is true (that is,
1457*9880d681SAndroid Build Coastguard Workerif the current argument is of type ``f32`` or ``f64``), then the action is
1458*9880d681SAndroid Build Coastguard Workerperformed.  In this case, the ``CCAssignToReg`` action assigns the argument
1459*9880d681SAndroid Build Coastguard Workervalue to the first available register: either ``R0`` or ``R1``.
1460*9880d681SAndroid Build Coastguard Worker
1461*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1462*9880d681SAndroid Build Coastguard Worker
1463*9880d681SAndroid Build Coastguard Worker  CCIfType<[f32,f64], CCAssignToReg<[R0, R1]>>
1464*9880d681SAndroid Build Coastguard Worker
1465*9880d681SAndroid Build Coastguard Worker``SparcCallingConv.td`` contains definitions for a target-specific return-value
1466*9880d681SAndroid Build Coastguard Workercalling convention (``RetCC_Sparc32``) and a basic 32-bit C calling convention
1467*9880d681SAndroid Build Coastguard Worker(``CC_Sparc32``).  The definition of ``RetCC_Sparc32`` (shown below) indicates
1468*9880d681SAndroid Build Coastguard Workerwhich registers are used for specified scalar return types.  A single-precision
1469*9880d681SAndroid Build Coastguard Workerfloat is returned to register ``F0``, and a double-precision float goes to
1470*9880d681SAndroid Build Coastguard Workerregister ``D0``.  A 32-bit integer is returned in register ``I0`` or ``I1``.
1471*9880d681SAndroid Build Coastguard Worker
1472*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1473*9880d681SAndroid Build Coastguard Worker
1474*9880d681SAndroid Build Coastguard Worker  def RetCC_Sparc32 : CallingConv<[
1475*9880d681SAndroid Build Coastguard Worker    CCIfType<[i32], CCAssignToReg<[I0, I1]>>,
1476*9880d681SAndroid Build Coastguard Worker    CCIfType<[f32], CCAssignToReg<[F0]>>,
1477*9880d681SAndroid Build Coastguard Worker    CCIfType<[f64], CCAssignToReg<[D0]>>
1478*9880d681SAndroid Build Coastguard Worker  ]>;
1479*9880d681SAndroid Build Coastguard Worker
1480*9880d681SAndroid Build Coastguard WorkerThe definition of ``CC_Sparc32`` in ``SparcCallingConv.td`` introduces
1481*9880d681SAndroid Build Coastguard Worker``CCAssignToStack``, which assigns the value to a stack slot with the specified
1482*9880d681SAndroid Build Coastguard Workersize and alignment.  In the example below, the first parameter, 4, indicates
1483*9880d681SAndroid Build Coastguard Workerthe size of the slot, and the second parameter, also 4, indicates the stack
1484*9880d681SAndroid Build Coastguard Workeralignment along 4-byte units.  (Special cases: if size is zero, then the ABI
1485*9880d681SAndroid Build Coastguard Workersize is used; if alignment is zero, then the ABI alignment is used.)
1486*9880d681SAndroid Build Coastguard Worker
1487*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1488*9880d681SAndroid Build Coastguard Worker
1489*9880d681SAndroid Build Coastguard Worker  def CC_Sparc32 : CallingConv<[
1490*9880d681SAndroid Build Coastguard Worker    // All arguments get passed in integer registers if there is space.
1491*9880d681SAndroid Build Coastguard Worker    CCIfType<[i32, f32, f64], CCAssignToReg<[I0, I1, I2, I3, I4, I5]>>,
1492*9880d681SAndroid Build Coastguard Worker    CCAssignToStack<4, 4>
1493*9880d681SAndroid Build Coastguard Worker  ]>;
1494*9880d681SAndroid Build Coastguard Worker
1495*9880d681SAndroid Build Coastguard Worker``CCDelegateTo`` is another commonly used interface, which tries to find a
1496*9880d681SAndroid Build Coastguard Workerspecified sub-calling convention, and, if a match is found, it is invoked.  In
1497*9880d681SAndroid Build Coastguard Workerthe following example (in ``X86CallingConv.td``), the definition of
1498*9880d681SAndroid Build Coastguard Worker``RetCC_X86_32_C`` ends with ``CCDelegateTo``.  After the current value is
1499*9880d681SAndroid Build Coastguard Workerassigned to the register ``ST0`` or ``ST1``, the ``RetCC_X86Common`` is
1500*9880d681SAndroid Build Coastguard Workerinvoked.
1501*9880d681SAndroid Build Coastguard Worker
1502*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1503*9880d681SAndroid Build Coastguard Worker
1504*9880d681SAndroid Build Coastguard Worker  def RetCC_X86_32_C : CallingConv<[
1505*9880d681SAndroid Build Coastguard Worker    CCIfType<[f32], CCAssignToReg<[ST0, ST1]>>,
1506*9880d681SAndroid Build Coastguard Worker    CCIfType<[f64], CCAssignToReg<[ST0, ST1]>>,
1507*9880d681SAndroid Build Coastguard Worker    CCDelegateTo<RetCC_X86Common>
1508*9880d681SAndroid Build Coastguard Worker  ]>;
1509*9880d681SAndroid Build Coastguard Worker
1510*9880d681SAndroid Build Coastguard Worker``CCIfCC`` is an interface that attempts to match the given name to the current
1511*9880d681SAndroid Build Coastguard Workercalling convention.  If the name identifies the current calling convention,
1512*9880d681SAndroid Build Coastguard Workerthen a specified action is invoked.  In the following example (in
1513*9880d681SAndroid Build Coastguard Worker``X86CallingConv.td``), if the ``Fast`` calling convention is in use, then
1514*9880d681SAndroid Build Coastguard Worker``RetCC_X86_32_Fast`` is invoked.  If the ``SSECall`` calling convention is in
1515*9880d681SAndroid Build Coastguard Workeruse, then ``RetCC_X86_32_SSE`` is invoked.
1516*9880d681SAndroid Build Coastguard Worker
1517*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1518*9880d681SAndroid Build Coastguard Worker
1519*9880d681SAndroid Build Coastguard Worker  def RetCC_X86_32 : CallingConv<[
1520*9880d681SAndroid Build Coastguard Worker    CCIfCC<"CallingConv::Fast", CCDelegateTo<RetCC_X86_32_Fast>>,
1521*9880d681SAndroid Build Coastguard Worker    CCIfCC<"CallingConv::X86_SSECall", CCDelegateTo<RetCC_X86_32_SSE>>,
1522*9880d681SAndroid Build Coastguard Worker    CCDelegateTo<RetCC_X86_32_C>
1523*9880d681SAndroid Build Coastguard Worker  ]>;
1524*9880d681SAndroid Build Coastguard Worker
1525*9880d681SAndroid Build Coastguard WorkerOther calling convention interfaces include:
1526*9880d681SAndroid Build Coastguard Worker
1527*9880d681SAndroid Build Coastguard Worker* ``CCIf <predicate, action>`` --- If the predicate matches, apply the action.
1528*9880d681SAndroid Build Coastguard Worker
1529*9880d681SAndroid Build Coastguard Worker* ``CCIfInReg <action>`` --- If the argument is marked with the "``inreg``"
1530*9880d681SAndroid Build Coastguard Worker  attribute, then apply the action.
1531*9880d681SAndroid Build Coastguard Worker
1532*9880d681SAndroid Build Coastguard Worker* ``CCIfNest <action>`` --- If the argument is marked with the "``nest``"
1533*9880d681SAndroid Build Coastguard Worker  attribute, then apply the action.
1534*9880d681SAndroid Build Coastguard Worker
1535*9880d681SAndroid Build Coastguard Worker* ``CCIfNotVarArg <action>`` --- If the current function does not take a
1536*9880d681SAndroid Build Coastguard Worker  variable number of arguments, apply the action.
1537*9880d681SAndroid Build Coastguard Worker
1538*9880d681SAndroid Build Coastguard Worker* ``CCAssignToRegWithShadow <registerList, shadowList>`` --- similar to
1539*9880d681SAndroid Build Coastguard Worker  ``CCAssignToReg``, but with a shadow list of registers.
1540*9880d681SAndroid Build Coastguard Worker
1541*9880d681SAndroid Build Coastguard Worker* ``CCPassByVal <size, align>`` --- Assign value to a stack slot with the
1542*9880d681SAndroid Build Coastguard Worker  minimum specified size and alignment.
1543*9880d681SAndroid Build Coastguard Worker
1544*9880d681SAndroid Build Coastguard Worker* ``CCPromoteToType <type>`` --- Promote the current value to the specified
1545*9880d681SAndroid Build Coastguard Worker  type.
1546*9880d681SAndroid Build Coastguard Worker
1547*9880d681SAndroid Build Coastguard Worker* ``CallingConv <[actions]>`` --- Define each calling convention that is
1548*9880d681SAndroid Build Coastguard Worker  supported.
1549*9880d681SAndroid Build Coastguard Worker
1550*9880d681SAndroid Build Coastguard WorkerAssembly Printer
1551*9880d681SAndroid Build Coastguard Worker================
1552*9880d681SAndroid Build Coastguard Worker
1553*9880d681SAndroid Build Coastguard WorkerDuring the code emission stage, the code generator may utilize an LLVM pass to
1554*9880d681SAndroid Build Coastguard Workerproduce assembly output.  To do this, you want to implement the code for a
1555*9880d681SAndroid Build Coastguard Workerprinter that converts LLVM IR to a GAS-format assembly language for your target
1556*9880d681SAndroid Build Coastguard Workermachine, using the following steps:
1557*9880d681SAndroid Build Coastguard Worker
1558*9880d681SAndroid Build Coastguard Worker* Define all the assembly strings for your target, adding them to the
1559*9880d681SAndroid Build Coastguard Worker  instructions defined in the ``XXXInstrInfo.td`` file.  (See
1560*9880d681SAndroid Build Coastguard Worker  :ref:`instruction-set`.)  TableGen will produce an output file
1561*9880d681SAndroid Build Coastguard Worker  (``XXXGenAsmWriter.inc``) with an implementation of the ``printInstruction``
1562*9880d681SAndroid Build Coastguard Worker  method for the ``XXXAsmPrinter`` class.
1563*9880d681SAndroid Build Coastguard Worker
1564*9880d681SAndroid Build Coastguard Worker* Write ``XXXTargetAsmInfo.h``, which contains the bare-bones declaration of
1565*9880d681SAndroid Build Coastguard Worker  the ``XXXTargetAsmInfo`` class (a subclass of ``TargetAsmInfo``).
1566*9880d681SAndroid Build Coastguard Worker
1567*9880d681SAndroid Build Coastguard Worker* Write ``XXXTargetAsmInfo.cpp``, which contains target-specific values for
1568*9880d681SAndroid Build Coastguard Worker  ``TargetAsmInfo`` properties and sometimes new implementations for methods.
1569*9880d681SAndroid Build Coastguard Worker
1570*9880d681SAndroid Build Coastguard Worker* Write ``XXXAsmPrinter.cpp``, which implements the ``AsmPrinter`` class that
1571*9880d681SAndroid Build Coastguard Worker  performs the LLVM-to-assembly conversion.
1572*9880d681SAndroid Build Coastguard Worker
1573*9880d681SAndroid Build Coastguard WorkerThe code in ``XXXTargetAsmInfo.h`` is usually a trivial declaration of the
1574*9880d681SAndroid Build Coastguard Worker``XXXTargetAsmInfo`` class for use in ``XXXTargetAsmInfo.cpp``.  Similarly,
1575*9880d681SAndroid Build Coastguard Worker``XXXTargetAsmInfo.cpp`` usually has a few declarations of ``XXXTargetAsmInfo``
1576*9880d681SAndroid Build Coastguard Workerreplacement values that override the default values in ``TargetAsmInfo.cpp``.
1577*9880d681SAndroid Build Coastguard WorkerFor example in ``SparcTargetAsmInfo.cpp``:
1578*9880d681SAndroid Build Coastguard Worker
1579*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1580*9880d681SAndroid Build Coastguard Worker
1581*9880d681SAndroid Build Coastguard Worker  SparcTargetAsmInfo::SparcTargetAsmInfo(const SparcTargetMachine &TM) {
1582*9880d681SAndroid Build Coastguard Worker    Data16bitsDirective = "\t.half\t";
1583*9880d681SAndroid Build Coastguard Worker    Data32bitsDirective = "\t.word\t";
1584*9880d681SAndroid Build Coastguard Worker    Data64bitsDirective = 0;  // .xword is only supported by V9.
1585*9880d681SAndroid Build Coastguard Worker    ZeroDirective = "\t.skip\t";
1586*9880d681SAndroid Build Coastguard Worker    CommentString = "!";
1587*9880d681SAndroid Build Coastguard Worker    ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
1588*9880d681SAndroid Build Coastguard Worker  }
1589*9880d681SAndroid Build Coastguard Worker
1590*9880d681SAndroid Build Coastguard WorkerThe X86 assembly printer implementation (``X86TargetAsmInfo``) is an example
1591*9880d681SAndroid Build Coastguard Workerwhere the target specific ``TargetAsmInfo`` class uses an overridden methods:
1592*9880d681SAndroid Build Coastguard Worker``ExpandInlineAsm``.
1593*9880d681SAndroid Build Coastguard Worker
1594*9880d681SAndroid Build Coastguard WorkerA target-specific implementation of ``AsmPrinter`` is written in
1595*9880d681SAndroid Build Coastguard Worker``XXXAsmPrinter.cpp``, which implements the ``AsmPrinter`` class that converts
1596*9880d681SAndroid Build Coastguard Workerthe LLVM to printable assembly.  The implementation must include the following
1597*9880d681SAndroid Build Coastguard Workerheaders that have declarations for the ``AsmPrinter`` and
1598*9880d681SAndroid Build Coastguard Worker``MachineFunctionPass`` classes.  The ``MachineFunctionPass`` is a subclass of
1599*9880d681SAndroid Build Coastguard Worker``FunctionPass``.
1600*9880d681SAndroid Build Coastguard Worker
1601*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1602*9880d681SAndroid Build Coastguard Worker
1603*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/AsmPrinter.h"
1604*9880d681SAndroid Build Coastguard Worker  #include "llvm/CodeGen/MachineFunctionPass.h"
1605*9880d681SAndroid Build Coastguard Worker
1606*9880d681SAndroid Build Coastguard WorkerAs a ``FunctionPass``, ``AsmPrinter`` first calls ``doInitialization`` to set
1607*9880d681SAndroid Build Coastguard Workerup the ``AsmPrinter``.  In ``SparcAsmPrinter``, a ``Mangler`` object is
1608*9880d681SAndroid Build Coastguard Workerinstantiated to process variable names.
1609*9880d681SAndroid Build Coastguard Worker
1610*9880d681SAndroid Build Coastguard WorkerIn ``XXXAsmPrinter.cpp``, the ``runOnMachineFunction`` method (declared in
1611*9880d681SAndroid Build Coastguard Worker``MachineFunctionPass``) must be implemented for ``XXXAsmPrinter``.  In
1612*9880d681SAndroid Build Coastguard Worker``MachineFunctionPass``, the ``runOnFunction`` method invokes
1613*9880d681SAndroid Build Coastguard Worker``runOnMachineFunction``.  Target-specific implementations of
1614*9880d681SAndroid Build Coastguard Worker``runOnMachineFunction`` differ, but generally do the following to process each
1615*9880d681SAndroid Build Coastguard Workermachine function:
1616*9880d681SAndroid Build Coastguard Worker
1617*9880d681SAndroid Build Coastguard Worker* Call ``SetupMachineFunction`` to perform initialization.
1618*9880d681SAndroid Build Coastguard Worker
1619*9880d681SAndroid Build Coastguard Worker* Call ``EmitConstantPool`` to print out (to the output stream) constants which
1620*9880d681SAndroid Build Coastguard Worker  have been spilled to memory.
1621*9880d681SAndroid Build Coastguard Worker
1622*9880d681SAndroid Build Coastguard Worker* Call ``EmitJumpTableInfo`` to print out jump tables used by the current
1623*9880d681SAndroid Build Coastguard Worker  function.
1624*9880d681SAndroid Build Coastguard Worker
1625*9880d681SAndroid Build Coastguard Worker* Print out the label for the current function.
1626*9880d681SAndroid Build Coastguard Worker
1627*9880d681SAndroid Build Coastguard Worker* Print out the code for the function, including basic block labels and the
1628*9880d681SAndroid Build Coastguard Worker  assembly for the instruction (using ``printInstruction``)
1629*9880d681SAndroid Build Coastguard Worker
1630*9880d681SAndroid Build Coastguard WorkerThe ``XXXAsmPrinter`` implementation must also include the code generated by
1631*9880d681SAndroid Build Coastguard WorkerTableGen that is output in the ``XXXGenAsmWriter.inc`` file.  The code in
1632*9880d681SAndroid Build Coastguard Worker``XXXGenAsmWriter.inc`` contains an implementation of the ``printInstruction``
1633*9880d681SAndroid Build Coastguard Workermethod that may call these methods:
1634*9880d681SAndroid Build Coastguard Worker
1635*9880d681SAndroid Build Coastguard Worker* ``printOperand``
1636*9880d681SAndroid Build Coastguard Worker* ``printMemOperand``
1637*9880d681SAndroid Build Coastguard Worker* ``printCCOperand`` (for conditional statements)
1638*9880d681SAndroid Build Coastguard Worker* ``printDataDirective``
1639*9880d681SAndroid Build Coastguard Worker* ``printDeclare``
1640*9880d681SAndroid Build Coastguard Worker* ``printImplicitDef``
1641*9880d681SAndroid Build Coastguard Worker* ``printInlineAsm``
1642*9880d681SAndroid Build Coastguard Worker
1643*9880d681SAndroid Build Coastguard WorkerThe implementations of ``printDeclare``, ``printImplicitDef``,
1644*9880d681SAndroid Build Coastguard Worker``printInlineAsm``, and ``printLabel`` in ``AsmPrinter.cpp`` are generally
1645*9880d681SAndroid Build Coastguard Workeradequate for printing assembly and do not need to be overridden.
1646*9880d681SAndroid Build Coastguard Worker
1647*9880d681SAndroid Build Coastguard WorkerThe ``printOperand`` method is implemented with a long ``switch``/``case``
1648*9880d681SAndroid Build Coastguard Workerstatement for the type of operand: register, immediate, basic block, external
1649*9880d681SAndroid Build Coastguard Workersymbol, global address, constant pool index, or jump table index.  For an
1650*9880d681SAndroid Build Coastguard Workerinstruction with a memory address operand, the ``printMemOperand`` method
1651*9880d681SAndroid Build Coastguard Workershould be implemented to generate the proper output.  Similarly,
1652*9880d681SAndroid Build Coastguard Worker``printCCOperand`` should be used to print a conditional operand.
1653*9880d681SAndroid Build Coastguard Worker
1654*9880d681SAndroid Build Coastguard Worker``doFinalization`` should be overridden in ``XXXAsmPrinter``, and it should be
1655*9880d681SAndroid Build Coastguard Workercalled to shut down the assembly printer.  During ``doFinalization``, global
1656*9880d681SAndroid Build Coastguard Workervariables and constants are printed to output.
1657*9880d681SAndroid Build Coastguard Worker
1658*9880d681SAndroid Build Coastguard WorkerSubtarget Support
1659*9880d681SAndroid Build Coastguard Worker=================
1660*9880d681SAndroid Build Coastguard Worker
1661*9880d681SAndroid Build Coastguard WorkerSubtarget support is used to inform the code generation process of instruction
1662*9880d681SAndroid Build Coastguard Workerset variations for a given chip set.  For example, the LLVM SPARC
1663*9880d681SAndroid Build Coastguard Workerimplementation provided covers three major versions of the SPARC microprocessor
1664*9880d681SAndroid Build Coastguard Workerarchitecture: Version 8 (V8, which is a 32-bit architecture), Version 9 (V9, a
1665*9880d681SAndroid Build Coastguard Worker64-bit architecture), and the UltraSPARC architecture.  V8 has 16
1666*9880d681SAndroid Build Coastguard Workerdouble-precision floating-point registers that are also usable as either 32
1667*9880d681SAndroid Build Coastguard Workersingle-precision or 8 quad-precision registers.  V8 is also purely big-endian.
1668*9880d681SAndroid Build Coastguard WorkerV9 has 32 double-precision floating-point registers that are also usable as 16
1669*9880d681SAndroid Build Coastguard Workerquad-precision registers, but cannot be used as single-precision registers.
1670*9880d681SAndroid Build Coastguard WorkerThe UltraSPARC architecture combines V9 with UltraSPARC Visual Instruction Set
1671*9880d681SAndroid Build Coastguard Workerextensions.
1672*9880d681SAndroid Build Coastguard Worker
1673*9880d681SAndroid Build Coastguard WorkerIf subtarget support is needed, you should implement a target-specific
1674*9880d681SAndroid Build Coastguard Worker``XXXSubtarget`` class for your architecture.  This class should process the
1675*9880d681SAndroid Build Coastguard Workercommand-line options ``-mcpu=`` and ``-mattr=``.
1676*9880d681SAndroid Build Coastguard Worker
1677*9880d681SAndroid Build Coastguard WorkerTableGen uses definitions in the ``Target.td`` and ``Sparc.td`` files to
1678*9880d681SAndroid Build Coastguard Workergenerate code in ``SparcGenSubtarget.inc``.  In ``Target.td``, shown below, the
1679*9880d681SAndroid Build Coastguard Worker``SubtargetFeature`` interface is defined.  The first 4 string parameters of
1680*9880d681SAndroid Build Coastguard Workerthe ``SubtargetFeature`` interface are a feature name, an attribute set by the
1681*9880d681SAndroid Build Coastguard Workerfeature, the value of the attribute, and a description of the feature.  (The
1682*9880d681SAndroid Build Coastguard Workerfifth parameter is a list of features whose presence is implied, and its
1683*9880d681SAndroid Build Coastguard Workerdefault value is an empty array.)
1684*9880d681SAndroid Build Coastguard Worker
1685*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1686*9880d681SAndroid Build Coastguard Worker
1687*9880d681SAndroid Build Coastguard Worker  class SubtargetFeature<string n, string a, string v, string d,
1688*9880d681SAndroid Build Coastguard Worker                         list<SubtargetFeature> i = []> {
1689*9880d681SAndroid Build Coastguard Worker    string Name = n;
1690*9880d681SAndroid Build Coastguard Worker    string Attribute = a;
1691*9880d681SAndroid Build Coastguard Worker    string Value = v;
1692*9880d681SAndroid Build Coastguard Worker    string Desc = d;
1693*9880d681SAndroid Build Coastguard Worker    list<SubtargetFeature> Implies = i;
1694*9880d681SAndroid Build Coastguard Worker  }
1695*9880d681SAndroid Build Coastguard Worker
1696*9880d681SAndroid Build Coastguard WorkerIn the ``Sparc.td`` file, the ``SubtargetFeature`` is used to define the
1697*9880d681SAndroid Build Coastguard Workerfollowing features.
1698*9880d681SAndroid Build Coastguard Worker
1699*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1700*9880d681SAndroid Build Coastguard Worker
1701*9880d681SAndroid Build Coastguard Worker  def FeatureV9 : SubtargetFeature<"v9", "IsV9", "true",
1702*9880d681SAndroid Build Coastguard Worker                       "Enable SPARC-V9 instructions">;
1703*9880d681SAndroid Build Coastguard Worker  def FeatureV8Deprecated : SubtargetFeature<"deprecated-v8",
1704*9880d681SAndroid Build Coastguard Worker                       "V8DeprecatedInsts", "true",
1705*9880d681SAndroid Build Coastguard Worker                       "Enable deprecated V8 instructions in V9 mode">;
1706*9880d681SAndroid Build Coastguard Worker  def FeatureVIS : SubtargetFeature<"vis", "IsVIS", "true",
1707*9880d681SAndroid Build Coastguard Worker                       "Enable UltraSPARC Visual Instruction Set extensions">;
1708*9880d681SAndroid Build Coastguard Worker
1709*9880d681SAndroid Build Coastguard WorkerElsewhere in ``Sparc.td``, the ``Proc`` class is defined and then is used to
1710*9880d681SAndroid Build Coastguard Workerdefine particular SPARC processor subtypes that may have the previously
1711*9880d681SAndroid Build Coastguard Workerdescribed features.
1712*9880d681SAndroid Build Coastguard Worker
1713*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
1714*9880d681SAndroid Build Coastguard Worker
1715*9880d681SAndroid Build Coastguard Worker  class Proc<string Name, list<SubtargetFeature> Features>
1716*9880d681SAndroid Build Coastguard Worker    : Processor<Name, NoItineraries, Features>;
1717*9880d681SAndroid Build Coastguard Worker
1718*9880d681SAndroid Build Coastguard Worker  def : Proc<"generic",         []>;
1719*9880d681SAndroid Build Coastguard Worker  def : Proc<"v8",              []>;
1720*9880d681SAndroid Build Coastguard Worker  def : Proc<"supersparc",      []>;
1721*9880d681SAndroid Build Coastguard Worker  def : Proc<"sparclite",       []>;
1722*9880d681SAndroid Build Coastguard Worker  def : Proc<"f934",            []>;
1723*9880d681SAndroid Build Coastguard Worker  def : Proc<"hypersparc",      []>;
1724*9880d681SAndroid Build Coastguard Worker  def : Proc<"sparclite86x",    []>;
1725*9880d681SAndroid Build Coastguard Worker  def : Proc<"sparclet",        []>;
1726*9880d681SAndroid Build Coastguard Worker  def : Proc<"tsc701",          []>;
1727*9880d681SAndroid Build Coastguard Worker  def : Proc<"v9",              [FeatureV9]>;
1728*9880d681SAndroid Build Coastguard Worker  def : Proc<"ultrasparc",      [FeatureV9, FeatureV8Deprecated]>;
1729*9880d681SAndroid Build Coastguard Worker  def : Proc<"ultrasparc3",     [FeatureV9, FeatureV8Deprecated]>;
1730*9880d681SAndroid Build Coastguard Worker  def : Proc<"ultrasparc3-vis", [FeatureV9, FeatureV8Deprecated, FeatureVIS]>;
1731*9880d681SAndroid Build Coastguard Worker
1732*9880d681SAndroid Build Coastguard WorkerFrom ``Target.td`` and ``Sparc.td`` files, the resulting
1733*9880d681SAndroid Build Coastguard Worker``SparcGenSubtarget.inc`` specifies enum values to identify the features,
1734*9880d681SAndroid Build Coastguard Workerarrays of constants to represent the CPU features and CPU subtypes, and the
1735*9880d681SAndroid Build Coastguard Worker``ParseSubtargetFeatures`` method that parses the features string that sets
1736*9880d681SAndroid Build Coastguard Workerspecified subtarget options.  The generated ``SparcGenSubtarget.inc`` file
1737*9880d681SAndroid Build Coastguard Workershould be included in the ``SparcSubtarget.cpp``.  The target-specific
1738*9880d681SAndroid Build Coastguard Workerimplementation of the ``XXXSubtarget`` method should follow this pseudocode:
1739*9880d681SAndroid Build Coastguard Worker
1740*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1741*9880d681SAndroid Build Coastguard Worker
1742*9880d681SAndroid Build Coastguard Worker  XXXSubtarget::XXXSubtarget(const Module &M, const std::string &FS) {
1743*9880d681SAndroid Build Coastguard Worker    // Set the default features
1744*9880d681SAndroid Build Coastguard Worker    // Determine default and user specified characteristics of the CPU
1745*9880d681SAndroid Build Coastguard Worker    // Call ParseSubtargetFeatures(FS, CPU) to parse the features string
1746*9880d681SAndroid Build Coastguard Worker    // Perform any additional operations
1747*9880d681SAndroid Build Coastguard Worker  }
1748*9880d681SAndroid Build Coastguard Worker
1749*9880d681SAndroid Build Coastguard WorkerJIT Support
1750*9880d681SAndroid Build Coastguard Worker===========
1751*9880d681SAndroid Build Coastguard Worker
1752*9880d681SAndroid Build Coastguard WorkerThe implementation of a target machine optionally includes a Just-In-Time (JIT)
1753*9880d681SAndroid Build Coastguard Workercode generator that emits machine code and auxiliary structures as binary
1754*9880d681SAndroid Build Coastguard Workeroutput that can be written directly to memory.  To do this, implement JIT code
1755*9880d681SAndroid Build Coastguard Workergeneration by performing the following steps:
1756*9880d681SAndroid Build Coastguard Worker
1757*9880d681SAndroid Build Coastguard Worker* Write an ``XXXCodeEmitter.cpp`` file that contains a machine function pass
1758*9880d681SAndroid Build Coastguard Worker  that transforms target-machine instructions into relocatable machine
1759*9880d681SAndroid Build Coastguard Worker  code.
1760*9880d681SAndroid Build Coastguard Worker
1761*9880d681SAndroid Build Coastguard Worker* Write an ``XXXJITInfo.cpp`` file that implements the JIT interfaces for
1762*9880d681SAndroid Build Coastguard Worker  target-specific code-generation activities, such as emitting machine code and
1763*9880d681SAndroid Build Coastguard Worker  stubs.
1764*9880d681SAndroid Build Coastguard Worker
1765*9880d681SAndroid Build Coastguard Worker* Modify ``XXXTargetMachine`` so that it provides a ``TargetJITInfo`` object
1766*9880d681SAndroid Build Coastguard Worker  through its ``getJITInfo`` method.
1767*9880d681SAndroid Build Coastguard Worker
1768*9880d681SAndroid Build Coastguard WorkerThere are several different approaches to writing the JIT support code.  For
1769*9880d681SAndroid Build Coastguard Workerinstance, TableGen and target descriptor files may be used for creating a JIT
1770*9880d681SAndroid Build Coastguard Workercode generator, but are not mandatory.  For the Alpha and PowerPC target
1771*9880d681SAndroid Build Coastguard Workermachines, TableGen is used to generate ``XXXGenCodeEmitter.inc``, which
1772*9880d681SAndroid Build Coastguard Workercontains the binary coding of machine instructions and the
1773*9880d681SAndroid Build Coastguard Worker``getBinaryCodeForInstr`` method to access those codes.  Other JIT
1774*9880d681SAndroid Build Coastguard Workerimplementations do not.
1775*9880d681SAndroid Build Coastguard Worker
1776*9880d681SAndroid Build Coastguard WorkerBoth ``XXXJITInfo.cpp`` and ``XXXCodeEmitter.cpp`` must include the
1777*9880d681SAndroid Build Coastguard Worker``llvm/CodeGen/MachineCodeEmitter.h`` header file that defines the
1778*9880d681SAndroid Build Coastguard Worker``MachineCodeEmitter`` class containing code for several callback functions
1779*9880d681SAndroid Build Coastguard Workerthat write data (in bytes, words, strings, etc.) to the output stream.
1780*9880d681SAndroid Build Coastguard Worker
1781*9880d681SAndroid Build Coastguard WorkerMachine Code Emitter
1782*9880d681SAndroid Build Coastguard Worker--------------------
1783*9880d681SAndroid Build Coastguard Worker
1784*9880d681SAndroid Build Coastguard WorkerIn ``XXXCodeEmitter.cpp``, a target-specific of the ``Emitter`` class is
1785*9880d681SAndroid Build Coastguard Workerimplemented as a function pass (subclass of ``MachineFunctionPass``).  The
1786*9880d681SAndroid Build Coastguard Workertarget-specific implementation of ``runOnMachineFunction`` (invoked by
1787*9880d681SAndroid Build Coastguard Worker``runOnFunction`` in ``MachineFunctionPass``) iterates through the
1788*9880d681SAndroid Build Coastguard Worker``MachineBasicBlock`` calls ``emitInstruction`` to process each instruction and
1789*9880d681SAndroid Build Coastguard Workeremit binary code.  ``emitInstruction`` is largely implemented with case
1790*9880d681SAndroid Build Coastguard Workerstatements on the instruction types defined in ``XXXInstrInfo.h``.  For
1791*9880d681SAndroid Build Coastguard Workerexample, in ``X86CodeEmitter.cpp``, the ``emitInstruction`` method is built
1792*9880d681SAndroid Build Coastguard Workeraround the following ``switch``/``case`` statements:
1793*9880d681SAndroid Build Coastguard Worker
1794*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1795*9880d681SAndroid Build Coastguard Worker
1796*9880d681SAndroid Build Coastguard Worker  switch (Desc->TSFlags & X86::FormMask) {
1797*9880d681SAndroid Build Coastguard Worker  case X86II::Pseudo:  // for not yet implemented instructions
1798*9880d681SAndroid Build Coastguard Worker     ...               // or pseudo-instructions
1799*9880d681SAndroid Build Coastguard Worker     break;
1800*9880d681SAndroid Build Coastguard Worker  case X86II::RawFrm:  // for instructions with a fixed opcode value
1801*9880d681SAndroid Build Coastguard Worker     ...
1802*9880d681SAndroid Build Coastguard Worker     break;
1803*9880d681SAndroid Build Coastguard Worker  case X86II::AddRegFrm: // for instructions that have one register operand
1804*9880d681SAndroid Build Coastguard Worker     ...                 // added to their opcode
1805*9880d681SAndroid Build Coastguard Worker     break;
1806*9880d681SAndroid Build Coastguard Worker  case X86II::MRMDestReg:// for instructions that use the Mod/RM byte
1807*9880d681SAndroid Build Coastguard Worker     ...                 // to specify a destination (register)
1808*9880d681SAndroid Build Coastguard Worker     break;
1809*9880d681SAndroid Build Coastguard Worker  case X86II::MRMDestMem:// for instructions that use the Mod/RM byte
1810*9880d681SAndroid Build Coastguard Worker     ...                 // to specify a destination (memory)
1811*9880d681SAndroid Build Coastguard Worker     break;
1812*9880d681SAndroid Build Coastguard Worker  case X86II::MRMSrcReg: // for instructions that use the Mod/RM byte
1813*9880d681SAndroid Build Coastguard Worker     ...                 // to specify a source (register)
1814*9880d681SAndroid Build Coastguard Worker     break;
1815*9880d681SAndroid Build Coastguard Worker  case X86II::MRMSrcMem: // for instructions that use the Mod/RM byte
1816*9880d681SAndroid Build Coastguard Worker     ...                 // to specify a source (memory)
1817*9880d681SAndroid Build Coastguard Worker     break;
1818*9880d681SAndroid Build Coastguard Worker  case X86II::MRM0r: case X86II::MRM1r:  // for instructions that operate on
1819*9880d681SAndroid Build Coastguard Worker  case X86II::MRM2r: case X86II::MRM3r:  // a REGISTER r/m operand and
1820*9880d681SAndroid Build Coastguard Worker  case X86II::MRM4r: case X86II::MRM5r:  // use the Mod/RM byte and a field
1821*9880d681SAndroid Build Coastguard Worker  case X86II::MRM6r: case X86II::MRM7r:  // to hold extended opcode data
1822*9880d681SAndroid Build Coastguard Worker     ...
1823*9880d681SAndroid Build Coastguard Worker     break;
1824*9880d681SAndroid Build Coastguard Worker  case X86II::MRM0m: case X86II::MRM1m:  // for instructions that operate on
1825*9880d681SAndroid Build Coastguard Worker  case X86II::MRM2m: case X86II::MRM3m:  // a MEMORY r/m operand and
1826*9880d681SAndroid Build Coastguard Worker  case X86II::MRM4m: case X86II::MRM5m:  // use the Mod/RM byte and a field
1827*9880d681SAndroid Build Coastguard Worker  case X86II::MRM6m: case X86II::MRM7m:  // to hold extended opcode data
1828*9880d681SAndroid Build Coastguard Worker     ...
1829*9880d681SAndroid Build Coastguard Worker     break;
1830*9880d681SAndroid Build Coastguard Worker  case X86II::MRMInitReg: // for instructions whose source and
1831*9880d681SAndroid Build Coastguard Worker     ...                  // destination are the same register
1832*9880d681SAndroid Build Coastguard Worker     break;
1833*9880d681SAndroid Build Coastguard Worker  }
1834*9880d681SAndroid Build Coastguard Worker
1835*9880d681SAndroid Build Coastguard WorkerThe implementations of these case statements often first emit the opcode and
1836*9880d681SAndroid Build Coastguard Workerthen get the operand(s).  Then depending upon the operand, helper methods may
1837*9880d681SAndroid Build Coastguard Workerbe called to process the operand(s).  For example, in ``X86CodeEmitter.cpp``,
1838*9880d681SAndroid Build Coastguard Workerfor the ``X86II::AddRegFrm`` case, the first data emitted (by ``emitByte``) is
1839*9880d681SAndroid Build Coastguard Workerthe opcode added to the register operand.  Then an object representing the
1840*9880d681SAndroid Build Coastguard Workermachine operand, ``MO1``, is extracted.  The helper methods such as
1841*9880d681SAndroid Build Coastguard Worker``isImmediate``, ``isGlobalAddress``, ``isExternalSymbol``,
1842*9880d681SAndroid Build Coastguard Worker``isConstantPoolIndex``, and ``isJumpTableIndex`` determine the operand type.
1843*9880d681SAndroid Build Coastguard Worker(``X86CodeEmitter.cpp`` also has private methods such as ``emitConstant``,
1844*9880d681SAndroid Build Coastguard Worker``emitGlobalAddress``, ``emitExternalSymbolAddress``, ``emitConstPoolAddress``,
1845*9880d681SAndroid Build Coastguard Workerand ``emitJumpTableAddress`` that emit the data into the output stream.)
1846*9880d681SAndroid Build Coastguard Worker
1847*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1848*9880d681SAndroid Build Coastguard Worker
1849*9880d681SAndroid Build Coastguard Worker  case X86II::AddRegFrm:
1850*9880d681SAndroid Build Coastguard Worker    MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
1851*9880d681SAndroid Build Coastguard Worker
1852*9880d681SAndroid Build Coastguard Worker    if (CurOp != NumOps) {
1853*9880d681SAndroid Build Coastguard Worker      const MachineOperand &MO1 = MI.getOperand(CurOp++);
1854*9880d681SAndroid Build Coastguard Worker      unsigned Size = X86InstrInfo::sizeOfImm(Desc);
1855*9880d681SAndroid Build Coastguard Worker      if (MO1.isImmediate())
1856*9880d681SAndroid Build Coastguard Worker        emitConstant(MO1.getImm(), Size);
1857*9880d681SAndroid Build Coastguard Worker      else {
1858*9880d681SAndroid Build Coastguard Worker        unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
1859*9880d681SAndroid Build Coastguard Worker          : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
1860*9880d681SAndroid Build Coastguard Worker        if (Opcode == X86::MOV64ri)
1861*9880d681SAndroid Build Coastguard Worker          rt = X86::reloc_absolute_dword;  // FIXME: add X86II flag?
1862*9880d681SAndroid Build Coastguard Worker        if (MO1.isGlobalAddress()) {
1863*9880d681SAndroid Build Coastguard Worker          bool NeedStub = isa<Function>(MO1.getGlobal());
1864*9880d681SAndroid Build Coastguard Worker          bool isLazy = gvNeedsLazyPtr(MO1.getGlobal());
1865*9880d681SAndroid Build Coastguard Worker          emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
1866*9880d681SAndroid Build Coastguard Worker                            NeedStub, isLazy);
1867*9880d681SAndroid Build Coastguard Worker        } else if (MO1.isExternalSymbol())
1868*9880d681SAndroid Build Coastguard Worker          emitExternalSymbolAddress(MO1.getSymbolName(), rt);
1869*9880d681SAndroid Build Coastguard Worker        else if (MO1.isConstantPoolIndex())
1870*9880d681SAndroid Build Coastguard Worker          emitConstPoolAddress(MO1.getIndex(), rt);
1871*9880d681SAndroid Build Coastguard Worker        else if (MO1.isJumpTableIndex())
1872*9880d681SAndroid Build Coastguard Worker          emitJumpTableAddress(MO1.getIndex(), rt);
1873*9880d681SAndroid Build Coastguard Worker      }
1874*9880d681SAndroid Build Coastguard Worker    }
1875*9880d681SAndroid Build Coastguard Worker    break;
1876*9880d681SAndroid Build Coastguard Worker
1877*9880d681SAndroid Build Coastguard WorkerIn the previous example, ``XXXCodeEmitter.cpp`` uses the variable ``rt``, which
1878*9880d681SAndroid Build Coastguard Workeris a ``RelocationType`` enum that may be used to relocate addresses (for
1879*9880d681SAndroid Build Coastguard Workerexample, a global address with a PIC base offset).  The ``RelocationType`` enum
1880*9880d681SAndroid Build Coastguard Workerfor that target is defined in the short target-specific ``XXXRelocations.h``
1881*9880d681SAndroid Build Coastguard Workerfile.  The ``RelocationType`` is used by the ``relocate`` method defined in
1882*9880d681SAndroid Build Coastguard Worker``XXXJITInfo.cpp`` to rewrite addresses for referenced global symbols.
1883*9880d681SAndroid Build Coastguard Worker
1884*9880d681SAndroid Build Coastguard WorkerFor example, ``X86Relocations.h`` specifies the following relocation types for
1885*9880d681SAndroid Build Coastguard Workerthe X86 addresses.  In all four cases, the relocated value is added to the
1886*9880d681SAndroid Build Coastguard Workervalue already in memory.  For ``reloc_pcrel_word`` and ``reloc_picrel_word``,
1887*9880d681SAndroid Build Coastguard Workerthere is an additional initial adjustment.
1888*9880d681SAndroid Build Coastguard Worker
1889*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1890*9880d681SAndroid Build Coastguard Worker
1891*9880d681SAndroid Build Coastguard Worker  enum RelocationType {
1892*9880d681SAndroid Build Coastguard Worker    reloc_pcrel_word = 0,    // add reloc value after adjusting for the PC loc
1893*9880d681SAndroid Build Coastguard Worker    reloc_picrel_word = 1,   // add reloc value after adjusting for the PIC base
1894*9880d681SAndroid Build Coastguard Worker    reloc_absolute_word = 2, // absolute relocation; no additional adjustment
1895*9880d681SAndroid Build Coastguard Worker    reloc_absolute_dword = 3 // absolute relocation; no additional adjustment
1896*9880d681SAndroid Build Coastguard Worker  };
1897*9880d681SAndroid Build Coastguard Worker
1898*9880d681SAndroid Build Coastguard WorkerTarget JIT Info
1899*9880d681SAndroid Build Coastguard Worker---------------
1900*9880d681SAndroid Build Coastguard Worker
1901*9880d681SAndroid Build Coastguard Worker``XXXJITInfo.cpp`` implements the JIT interfaces for target-specific
1902*9880d681SAndroid Build Coastguard Workercode-generation activities, such as emitting machine code and stubs.  At
1903*9880d681SAndroid Build Coastguard Workerminimum, a target-specific version of ``XXXJITInfo`` implements the following:
1904*9880d681SAndroid Build Coastguard Worker
1905*9880d681SAndroid Build Coastguard Worker* ``getLazyResolverFunction`` --- Initializes the JIT, gives the target a
1906*9880d681SAndroid Build Coastguard Worker  function that is used for compilation.
1907*9880d681SAndroid Build Coastguard Worker
1908*9880d681SAndroid Build Coastguard Worker* ``emitFunctionStub`` --- Returns a native function with a specified address
1909*9880d681SAndroid Build Coastguard Worker  for a callback function.
1910*9880d681SAndroid Build Coastguard Worker
1911*9880d681SAndroid Build Coastguard Worker* ``relocate`` --- Changes the addresses of referenced globals, based on
1912*9880d681SAndroid Build Coastguard Worker  relocation types.
1913*9880d681SAndroid Build Coastguard Worker
1914*9880d681SAndroid Build Coastguard Worker* Callback function that are wrappers to a function stub that is used when the
1915*9880d681SAndroid Build Coastguard Worker  real target is not initially known.
1916*9880d681SAndroid Build Coastguard Worker
1917*9880d681SAndroid Build Coastguard Worker``getLazyResolverFunction`` is generally trivial to implement.  It makes the
1918*9880d681SAndroid Build Coastguard Workerincoming parameter as the global ``JITCompilerFunction`` and returns the
1919*9880d681SAndroid Build Coastguard Workercallback function that will be used a function wrapper.  For the Alpha target
1920*9880d681SAndroid Build Coastguard Worker(in ``AlphaJITInfo.cpp``), the ``getLazyResolverFunction`` implementation is
1921*9880d681SAndroid Build Coastguard Workersimply:
1922*9880d681SAndroid Build Coastguard Worker
1923*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
1924*9880d681SAndroid Build Coastguard Worker
1925*9880d681SAndroid Build Coastguard Worker  TargetJITInfo::LazyResolverFn AlphaJITInfo::getLazyResolverFunction(
1926*9880d681SAndroid Build Coastguard Worker                                              JITCompilerFn F) {
1927*9880d681SAndroid Build Coastguard Worker    JITCompilerFunction = F;
1928*9880d681SAndroid Build Coastguard Worker    return AlphaCompilationCallback;
1929*9880d681SAndroid Build Coastguard Worker  }
1930*9880d681SAndroid Build Coastguard Worker
1931*9880d681SAndroid Build Coastguard WorkerFor the X86 target, the ``getLazyResolverFunction`` implementation is a little
1932*9880d681SAndroid Build Coastguard Workermore complicated, because it returns a different callback function for
1933*9880d681SAndroid Build Coastguard Workerprocessors with SSE instructions and XMM registers.
1934*9880d681SAndroid Build Coastguard Worker
1935*9880d681SAndroid Build Coastguard WorkerThe callback function initially saves and later restores the callee register
1936*9880d681SAndroid Build Coastguard Workervalues, incoming arguments, and frame and return address.  The callback
1937*9880d681SAndroid Build Coastguard Workerfunction needs low-level access to the registers or stack, so it is typically
1938*9880d681SAndroid Build Coastguard Workerimplemented with assembler.
1939*9880d681SAndroid Build Coastguard Worker
1940