blob: d240636fc92363485043a1734ac8230b5d148856 [file] [log] [blame]
Chris Lattner565d7d52004-06-01 06:48:00 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Jim Laskey57825842006-12-15 10:40:48 +00005 <meta http-equiv="content-type" content="text/html; charset=utf-8">
Chris Lattner565d7d52004-06-01 06:48:00 +00006 <title>The LLVM Target-Independent Code Generator</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8</head>
9<body>
10
11<div class="doc_title">
12 The LLVM Target-Independent Code Generator
13</div>
14
15<ol>
16 <li><a href="#introduction">Introduction</a>
17 <ul>
18 <li><a href="#required">Required components in the code generator</a></li>
Chris Lattneracf3d622005-10-16 00:36:38 +000019 <li><a href="#high-level-design">The high-level design of the code
20 generator</a></li>
Chris Lattner565d7d52004-06-01 06:48:00 +000021 <li><a href="#tablegen">Using TableGen for target description</a></li>
22 </ul>
23 </li>
24 <li><a href="#targetdesc">Target description classes</a>
25 <ul>
26 <li><a href="#targetmachine">The <tt>TargetMachine</tt> class</a></li>
27 <li><a href="#targetdata">The <tt>TargetData</tt> class</a></li>
Chris Lattner54903b62005-01-28 17:22:53 +000028 <li><a href="#targetlowering">The <tt>TargetLowering</tt> class</a></li>
Dan Gohman3a4be0f2008-02-10 18:45:23 +000029 <li><a href="#targetregisterinfo">The <tt>TargetRegisterInfo</tt> class</a></li>
Chris Lattner565d7d52004-06-01 06:48:00 +000030 <li><a href="#targetinstrinfo">The <tt>TargetInstrInfo</tt> class</a></li>
31 <li><a href="#targetframeinfo">The <tt>TargetFrameInfo</tt> class</a></li>
Chris Lattnerc9afa282005-10-16 17:06:07 +000032 <li><a href="#targetsubtarget">The <tt>TargetSubtarget</tt> class</a></li>
Chris Lattner565d7d52004-06-01 06:48:00 +000033 <li><a href="#targetjitinfo">The <tt>TargetJITInfo</tt> class</a></li>
34 </ul>
35 </li>
36 <li><a href="#codegendesc">Machine code description classes</a>
Chris Lattnerb4e56642004-06-04 00:16:02 +000037 <ul>
Chris Lattner54903b62005-01-28 17:22:53 +000038 <li><a href="#machineinstr">The <tt>MachineInstr</tt> class</a></li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +000039 <li><a href="#machinebasicblock">The <tt>MachineBasicBlock</tt>
40 class</a></li>
41 <li><a href="#machinefunction">The <tt>MachineFunction</tt> class</a></li>
Chris Lattnerb4e56642004-06-04 00:16:02 +000042 </ul>
Chris Lattner565d7d52004-06-01 06:48:00 +000043 </li>
44 <li><a href="#codegenalgs">Target-independent code generation algorithms</a>
Chris Lattner54903b62005-01-28 17:22:53 +000045 <ul>
46 <li><a href="#instselect">Instruction Selection</a>
47 <ul>
48 <li><a href="#selectiondag_intro">Introduction to SelectionDAGs</a></li>
49 <li><a href="#selectiondag_process">SelectionDAG Code Generation
50 Process</a></li>
51 <li><a href="#selectiondag_build">Initial SelectionDAG
52 Construction</a></li>
53 <li><a href="#selectiondag_legalize">SelectionDAG Legalize Phase</a></li>
54 <li><a href="#selectiondag_optimize">SelectionDAG Optimization
Chris Lattneracf3d622005-10-16 00:36:38 +000055 Phase: the DAG Combiner</a></li>
Chris Lattner54903b62005-01-28 17:22:53 +000056 <li><a href="#selectiondag_select">SelectionDAG Select Phase</a></li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +000057 <li><a href="#selectiondag_sched">SelectionDAG Scheduling and Formation
Chris Lattneracf3d622005-10-16 00:36:38 +000058 Phase</a></li>
Chris Lattner54903b62005-01-28 17:22:53 +000059 <li><a href="#selectiondag_future">Future directions for the
60 SelectionDAG</a></li>
61 </ul></li>
Bill Wendlingd495bd02006-09-06 18:42:41 +000062 <li><a href="#liveintervals">Live Intervals</a>
Bill Wendlingbb902cf2006-09-04 23:35:52 +000063 <ul>
64 <li><a href="#livevariable_analysis">Live Variable Analysis</a></li>
Bill Wendlingd495bd02006-09-06 18:42:41 +000065 <li><a href="#liveintervals_analysis">Live Intervals Analysis</a></li>
Bill Wendlingbb902cf2006-09-04 23:35:52 +000066 </ul></li>
Bill Wendling00c5aec2006-09-01 21:46:00 +000067 <li><a href="#regalloc">Register Allocation</a>
68 <ul>
69 <li><a href="#regAlloc_represent">How registers are represented in
70 LLVM</a></li>
71 <li><a href="#regAlloc_howTo">Mapping virtual registers to physical
72 registers</a></li>
73 <li><a href="#regAlloc_twoAddr">Handling two address instructions</a></li>
74 <li><a href="#regAlloc_ssaDecon">The SSA deconstruction phase</a></li>
75 <li><a href="#regAlloc_fold">Instruction folding</a></li>
76 <li><a href="#regAlloc_builtIn">Built in register allocators</a></li>
77 </ul></li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +000078 <li><a href="#codeemit">Code Emission</a>
79 <ul>
80 <li><a href="#codeemit_asm">Generating Assembly Code</a></li>
81 <li><a href="#codeemit_bin">Generating Binary Machine Code</a></li>
82 </ul></li>
Chris Lattner54903b62005-01-28 17:22:53 +000083 </ul>
Chris Lattner565d7d52004-06-01 06:48:00 +000084 </li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +000085 <li><a href="#targetimpls">Target-specific Implementation Notes</a>
Chris Lattner565d7d52004-06-01 06:48:00 +000086 <ul>
Arnold Schwaighofer2c6b8882008-05-14 09:17:12 +000087 <li><a href="#tailcallopt">Tail call optimization</a></li>
Chris Lattner54903b62005-01-28 17:22:53 +000088 <li><a href="#x86">The X86 backend</a></li>
Jim Laskey57825842006-12-15 10:40:48 +000089 <li><a href="#ppc">The PowerPC backend</a>
Jim Laskeyef583342006-12-14 17:19:50 +000090 <ul>
91 <li><a href="#ppc_abi">LLVM PowerPC ABI</a></li>
92 <li><a href="#ppc_frame">Frame Layout</a></li>
93 <li><a href="#ppc_prolog">Prolog/Epilog</a></li>
94 <li><a href="#ppc_dynamic">Dynamic Allocation</a></li>
Jim Laskey57825842006-12-15 10:40:48 +000095 </ul></li>
96 </ul></li>
Chris Lattner565d7d52004-06-01 06:48:00 +000097
98</ol>
99
100<div class="doc_author">
Bill Wendling00c5aec2006-09-01 21:46:00 +0000101 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
Jim Laskey7248e712007-03-14 19:30:33 +0000102 <a href="mailto:isanbard@gmail.com">Bill Wendling</a>,
Bill Wendling00c5aec2006-09-01 21:46:00 +0000103 <a href="mailto:pronesto@gmail.com">Fernando Magno Quintao
Jim Laskey7248e712007-03-14 19:30:33 +0000104 Pereira</a> and
105 <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000106</div>
107
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000108<div class="doc_warning">
109 <p>Warning: This is a work in progress.</p>
110</div>
111
Chris Lattner565d7d52004-06-01 06:48:00 +0000112<!-- *********************************************************************** -->
113<div class="doc_section">
114 <a name="introduction">Introduction</a>
115</div>
116<!-- *********************************************************************** -->
117
118<div class="doc_text">
119
120<p>The LLVM target-independent code generator is a framework that provides a
121suite of reusable components for translating the LLVM internal representation to
Bill Wendling5c385de2006-08-28 02:26:32 +0000122the machine code for a specified target&mdash;either in assembly form (suitable
123for a static compiler) or in binary machine code format (usable for a JIT
124compiler). The LLVM target-independent code generator consists of five main
125components:</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000126
127<ol>
128<li><a href="#targetdesc">Abstract target description</a> interfaces which
Reid Spencer1253fb92004-06-05 14:39:24 +0000129capture important properties about various aspects of the machine, independently
Chris Lattner565d7d52004-06-01 06:48:00 +0000130of how they will be used. These interfaces are defined in
131<tt>include/llvm/Target/</tt>.</li>
132
133<li>Classes used to represent the <a href="#codegendesc">machine code</a> being
Reid Spencer1253fb92004-06-05 14:39:24 +0000134generated for a target. These classes are intended to be abstract enough to
Chris Lattner565d7d52004-06-01 06:48:00 +0000135represent the machine code for <i>any</i> target machine. These classes are
136defined in <tt>include/llvm/CodeGen/</tt>.</li>
137
138<li><a href="#codegenalgs">Target-independent algorithms</a> used to implement
139various phases of native code generation (register allocation, scheduling, stack
140frame representation, etc). This code lives in <tt>lib/CodeGen/</tt>.</li>
141
142<li><a href="#targetimpls">Implementations of the abstract target description
143interfaces</a> for particular targets. These machine descriptions make use of
144the components provided by LLVM, and can optionally provide custom
145target-specific passes, to build complete code generators for a specific target.
146Target descriptions live in <tt>lib/Target/</tt>.</li>
147
Chris Lattnerb4e56642004-06-04 00:16:02 +0000148<li><a href="#jit">The target-independent JIT components</a>. The LLVM JIT is
149completely target independent (it uses the <tt>TargetJITInfo</tt> structure to
150interface for target-specific issues. The code for the target-independent
151JIT lives in <tt>lib/ExecutionEngine/JIT</tt>.</li>
152
Chris Lattner565d7d52004-06-01 06:48:00 +0000153</ol>
154
155<p>
156Depending on which part of the code generator you are interested in working on,
157different pieces of this will be useful to you. In any case, you should be
158familiar with the <a href="#targetdesc">target description</a> and <a
159href="#codegendesc">machine code representation</a> classes. If you want to add
Reid Spencer1253fb92004-06-05 14:39:24 +0000160a backend for a new target, you will need to <a href="#targetimpls">implement the
161target description</a> classes for your new target and understand the <a
Chris Lattner565d7d52004-06-01 06:48:00 +0000162href="LangRef.html">LLVM code representation</a>. If you are interested in
163implementing a new <a href="#codegenalgs">code generation algorithm</a>, it
164should only depend on the target-description and machine code representation
165classes, ensuring that it is portable.
166</p>
167
168</div>
169
170<!-- ======================================================================= -->
171<div class="doc_subsection">
172 <a name="required">Required components in the code generator</a>
173</div>
174
175<div class="doc_text">
176
177<p>The two pieces of the LLVM code generator are the high-level interface to the
178code generator and the set of reusable components that can be used to build
179target-specific backends. The two most important interfaces (<a
180href="#targetmachine"><tt>TargetMachine</tt></a> and <a
Reid Spencer4da97842005-04-24 20:56:18 +0000181href="#targetdata"><tt>TargetData</tt></a>) are the only ones that are
Chris Lattner565d7d52004-06-01 06:48:00 +0000182required to be defined for a backend to fit into the LLVM system, but the others
183must be defined if the reusable code generator components are going to be
184used.</p>
185
186<p>This design has two important implications. The first is that LLVM can
187support completely non-traditional code generation targets. For example, the C
188backend does not require register allocation, instruction selection, or any of
189the other standard components provided by the system. As such, it only
190implements these two interfaces, and does its own thing. Another example of a
191code generator like this is a (purely hypothetical) backend that converts LLVM
192to the GCC RTL form and uses GCC to emit machine code for a target.</p>
193
Reid Spencer1253fb92004-06-05 14:39:24 +0000194<p>This design also implies that it is possible to design and
Chris Lattner565d7d52004-06-01 06:48:00 +0000195implement radically different code generators in the LLVM system that do not
196make use of any of the built-in components. Doing so is not recommended at all,
197but could be required for radically different targets that do not fit into the
Bill Wendling5c385de2006-08-28 02:26:32 +0000198LLVM machine description model: FPGAs for example.</p>
Chris Lattnere6cad6c2004-06-02 07:06:06 +0000199
Chris Lattner565d7d52004-06-01 06:48:00 +0000200</div>
201
202<!-- ======================================================================= -->
203<div class="doc_subsection">
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000204 <a name="high-level-design">The high-level design of the code generator</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000205</div>
206
207<div class="doc_text">
208
Chris Lattner54903b62005-01-28 17:22:53 +0000209<p>The LLVM target-independent code generator is designed to support efficient and
Chris Lattner565d7d52004-06-01 06:48:00 +0000210quality code generation for standard register-based microprocessors. Code
211generation in this model is divided into the following stages:</p>
212
213<ol>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000214<li><b><a href="#instselect">Instruction Selection</a></b> - This phase
215determines an efficient way to express the input LLVM code in the target
216instruction set.
Chris Lattner54903b62005-01-28 17:22:53 +0000217This stage produces the initial code for the program in the target instruction
218set, then makes use of virtual registers in SSA form and physical registers that
219represent any required register assignments due to target constraints or calling
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000220conventions. This step turns the LLVM code into a DAG of target
221instructions.</li>
222
223<li><b><a href="#selectiondag_sched">Scheduling and Formation</a></b> - This
224phase takes the DAG of target instructions produced by the instruction selection
225phase, determines an ordering of the instructions, then emits the instructions
Chris Lattner9fcf0002005-10-17 03:09:31 +0000226as <tt><a href="#machineinstr">MachineInstr</a></tt>s with that ordering. Note
227that we describe this in the <a href="#instselect">instruction selection
228section</a> because it operates on a <a
229href="#selectiondag_intro">SelectionDAG</a>.
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000230</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000231
Reid Spencer4da97842005-04-24 20:56:18 +0000232<li><b><a href="#ssamco">SSA-based Machine Code Optimizations</a></b> - This
233optional stage consists of a series of machine-code optimizations that
234operate on the SSA-form produced by the instruction selector. Optimizations
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000235like modulo-scheduling or peephole optimization work here.
Reid Spencer4da97842005-04-24 20:56:18 +0000236</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000237
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000238<li><b><a href="#regalloc">Register Allocation</a></b> - The
Reid Spencer4da97842005-04-24 20:56:18 +0000239target code is transformed from an infinite virtual register file in SSA form
240to the concrete register file used by the target. This phase introduces spill
241code and eliminates all virtual register references from the program.</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000242
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000243<li><b><a href="#proepicode">Prolog/Epilog Code Insertion</a></b> - Once the
Reid Spencer4da97842005-04-24 20:56:18 +0000244machine code has been generated for the function and the amount of stack space
245required is known (used for LLVM alloca's and spill slots), the prolog and
246epilog code for the function can be inserted and "abstract stack location
247references" can be eliminated. This stage is responsible for implementing
248optimizations like frame-pointer elimination and stack packing.</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000249
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000250<li><b><a href="#latemco">Late Machine Code Optimizations</a></b> - Optimizations
Reid Spencer4da97842005-04-24 20:56:18 +0000251that operate on "final" machine code can go here, such as spill code scheduling
252and peephole optimizations.</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000253
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000254<li><b><a href="#codeemit">Code Emission</a></b> - The final stage actually
Reid Spencer4da97842005-04-24 20:56:18 +0000255puts out the code for the current function, either in the target assembler
256format or in machine code.</li>
Chris Lattner565d7d52004-06-01 06:48:00 +0000257
258</ol>
259
Bill Wendling5c385de2006-08-28 02:26:32 +0000260<p>The code generator is based on the assumption that the instruction selector
261will use an optimal pattern matching selector to create high-quality sequences of
Reid Spencer1253fb92004-06-05 14:39:24 +0000262native instructions. Alternative code generator designs based on pattern
Bill Wendling5c385de2006-08-28 02:26:32 +0000263expansion and aggressive iterative peephole optimization are much slower. This
264design permits efficient compilation (important for JIT environments) and
Reid Spencer1253fb92004-06-05 14:39:24 +0000265aggressive optimization (used when generating code offline) by allowing
Chris Lattner54903b62005-01-28 17:22:53 +0000266components of varying levels of sophistication to be used for any step of
Reid Spencer1253fb92004-06-05 14:39:24 +0000267compilation.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000268
Bill Wendling5c385de2006-08-28 02:26:32 +0000269<p>In addition to these stages, target implementations can insert arbitrary
Chris Lattner565d7d52004-06-01 06:48:00 +0000270target-specific passes into the flow. For example, the X86 target uses a
271special pass to handle the 80x87 floating point stack architecture. Other
Bill Wendling5c385de2006-08-28 02:26:32 +0000272targets with unusual requirements can be supported with custom passes as
273needed.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000274
275</div>
276
277
278<!-- ======================================================================= -->
279<div class="doc_subsection">
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000280 <a name="tablegen">Using TableGen for target description</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000281</div>
282
283<div class="doc_text">
284
Chris Lattnerd9be5fa2004-06-01 18:35:00 +0000285<p>The target description classes require a detailed description of the target
Chris Lattner565d7d52004-06-01 06:48:00 +0000286architecture. These target descriptions often have a large amount of common
Reid Spencer4da97842005-04-24 20:56:18 +0000287information (e.g., an <tt>add</tt> instruction is almost identical to a
288<tt>sub</tt> instruction).
Chris Lattner565d7d52004-06-01 06:48:00 +0000289In order to allow the maximum amount of commonality to be factored out, the LLVM
290code generator uses the <a href="TableGenFundamentals.html">TableGen</a> tool to
Reid Spencer4da97842005-04-24 20:56:18 +0000291describe big chunks of the target machine, which allows the use of
292domain-specific and target-specific abstractions to reduce the amount of
Bill Wendling5c385de2006-08-28 02:26:32 +0000293repetition.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000294
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000295<p>As LLVM continues to be developed and refined, we plan to move more and more
Bill Wendling5c385de2006-08-28 02:26:32 +0000296of the target description to the <tt>.td</tt> form. Doing so gives us a
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000297number of advantages. The most important is that it makes it easier to port
Bill Wendling5c385de2006-08-28 02:26:32 +0000298LLVM because it reduces the amount of C++ code that has to be written, and the
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000299surface area of the code generator that needs to be understood before someone
Bill Wendling5c385de2006-08-28 02:26:32 +0000300can get something working. Second, it makes it easier to change things. In
301particular, if tables and other things are all emitted by <tt>tblgen</tt>, we
302only need a change in one place (<tt>tblgen</tt>) to update all of the targets
303to a new interface.</p>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000304
Chris Lattner565d7d52004-06-01 06:48:00 +0000305</div>
306
307<!-- *********************************************************************** -->
308<div class="doc_section">
309 <a name="targetdesc">Target description classes</a>
310</div>
311<!-- *********************************************************************** -->
312
313<div class="doc_text">
314
Bill Wendling5c385de2006-08-28 02:26:32 +0000315<p>The LLVM target description classes (located in the
Chris Lattner565d7d52004-06-01 06:48:00 +0000316<tt>include/llvm/Target</tt> directory) provide an abstract description of the
Bill Wendling5c385de2006-08-28 02:26:32 +0000317target machine independent of any particular client. These classes are
Reid Spencer4da97842005-04-24 20:56:18 +0000318designed to capture the <i>abstract</i> properties of the target (such as the
319instructions and registers it has), and do not incorporate any particular pieces
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000320of code generation algorithms.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000321
322<p>All of the target description classes (except the <tt><a
323href="#targetdata">TargetData</a></tt> class) are designed to be subclassed by
324the concrete target implementation, and have virtual methods implemented. To
Reid Spencer1253fb92004-06-05 14:39:24 +0000325get to these implementations, the <tt><a
Chris Lattner565d7d52004-06-01 06:48:00 +0000326href="#targetmachine">TargetMachine</a></tt> class provides accessors that
327should be implemented by the target.</p>
328
329</div>
330
331<!-- ======================================================================= -->
332<div class="doc_subsection">
333 <a name="targetmachine">The <tt>TargetMachine</tt> class</a>
334</div>
335
336<div class="doc_text">
337
338<p>The <tt>TargetMachine</tt> class provides virtual methods that are used to
339access the target-specific implementations of the various target description
Reid Spencer4da97842005-04-24 20:56:18 +0000340classes via the <tt>get*Info</tt> methods (<tt>getInstrInfo</tt>,
341<tt>getRegisterInfo</tt>, <tt>getFrameInfo</tt>, etc.). This class is
342designed to be specialized by
Chris Lattner565d7d52004-06-01 06:48:00 +0000343a concrete target implementation (e.g., <tt>X86TargetMachine</tt>) which
344implements the various virtual methods. The only required target description
345class is the <a href="#targetdata"><tt>TargetData</tt></a> class, but if the
346code generator components are to be used, the other interfaces should be
347implemented as well.</p>
348
349</div>
350
351
352<!-- ======================================================================= -->
353<div class="doc_subsection">
354 <a name="targetdata">The <tt>TargetData</tt> class</a>
355</div>
356
357<div class="doc_text">
358
359<p>The <tt>TargetData</tt> class is the only required target description class,
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000360and it is the only class that is not extensible (you cannot derived a new
361class from it). <tt>TargetData</tt> specifies information about how the target
Reid Spencer4da97842005-04-24 20:56:18 +0000362lays out memory for structures, the alignment requirements for various data
363types, the size of pointers in the target, and whether the target is
364little-endian or big-endian.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000365
366</div>
367
Chris Lattner54903b62005-01-28 17:22:53 +0000368<!-- ======================================================================= -->
369<div class="doc_subsection">
370 <a name="targetlowering">The <tt>TargetLowering</tt> class</a>
371</div>
372
373<div class="doc_text">
374
375<p>The <tt>TargetLowering</tt> class is used by SelectionDAG based instruction
376selectors primarily to describe how LLVM code should be lowered to SelectionDAG
Bill Wendling5c385de2006-08-28 02:26:32 +0000377operations. Among other things, this class indicates:</p>
378
379<ul>
380 <li>an initial register class to use for various <tt>ValueType</tt>s</li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000381 <li>which operations are natively supported by the target machine</li>
Bill Wendling5c385de2006-08-28 02:26:32 +0000382 <li>the return type of <tt>setcc</tt> operations</li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000383 <li>the type to use for shift amounts</li>
384 <li>various high-level characteristics, like whether it is profitable to turn
385 division by a constant into a multiplication sequence</li>
Jim Laskey57825842006-12-15 10:40:48 +0000386</ul>
Chris Lattner54903b62005-01-28 17:22:53 +0000387
388</div>
389
Chris Lattner565d7d52004-06-01 06:48:00 +0000390<!-- ======================================================================= -->
391<div class="doc_subsection">
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000392 <a name="targetregisterinfo">The <tt>TargetRegisterInfo</tt> class</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000393</div>
394
395<div class="doc_text">
396
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000397<p>The <tt>TargetRegisterInfo</tt> class is used to describe the register
398file of the target and any interactions between the registers.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000399
400<p>Registers in the code generator are represented in the code generator by
Bill Wendling5c385de2006-08-28 02:26:32 +0000401unsigned integers. Physical registers (those that actually exist in the target
Chris Lattner565d7d52004-06-01 06:48:00 +0000402description) are unique small numbers, and virtual registers are generally
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000403large. Note that register #0 is reserved as a flag value.</p>
Chris Lattner565d7d52004-06-01 06:48:00 +0000404
405<p>Each register in the processor description has an associated
Bill Wendling5c385de2006-08-28 02:26:32 +0000406<tt>TargetRegisterDesc</tt> entry, which provides a textual name for the
407register (used for assembly output and debugging dumps) and a set of aliases
408(used to indicate whether one register overlaps with another).
Chris Lattner565d7d52004-06-01 06:48:00 +0000409</p>
410
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000411<p>In addition to the per-register description, the <tt>TargetRegisterInfo</tt>
412class exposes a set of processor specific register classes (instances of the
Chris Lattner565d7d52004-06-01 06:48:00 +0000413<tt>TargetRegisterClass</tt> class). Each register class contains sets of
414registers that have the same properties (for example, they are all 32-bit
415integer registers). Each SSA virtual register created by the instruction
416selector has an associated register class. When the register allocator runs, it
417replaces virtual registers with a physical register in the set.</p>
418
419<p>
420The target-specific implementations of these classes is auto-generated from a <a
421href="TableGenFundamentals.html">TableGen</a> description of the register file.
422</p>
423
424</div>
425
426<!-- ======================================================================= -->
427<div class="doc_subsection">
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000428 <a name="targetinstrinfo">The <tt>TargetInstrInfo</tt> class</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000429</div>
430
Reid Spencere5dc84b2005-07-19 01:36:35 +0000431<div class="doc_text">
432 <p>The <tt>TargetInstrInfo</tt> class is used to describe the machine
433 instructions supported by the target. It is essentially an array of
434 <tt>TargetInstrDescriptor</tt> objects, each of which describes one
435 instruction the target supports. Descriptors define things like the mnemonic
Chris Lattnerfb634392005-07-19 03:37:48 +0000436 for the opcode, the number of operands, the list of implicit register uses
437 and defs, whether the instruction has certain target-independent properties
Bill Wendling5c385de2006-08-28 02:26:32 +0000438 (accesses memory, is commutable, etc), and holds any target-specific
439 flags.</p>
Reid Spencere5dc84b2005-07-19 01:36:35 +0000440</div>
441
Chris Lattner565d7d52004-06-01 06:48:00 +0000442<!-- ======================================================================= -->
443<div class="doc_subsection">
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000444 <a name="targetframeinfo">The <tt>TargetFrameInfo</tt> class</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000445</div>
446
Reid Spencere5dc84b2005-07-19 01:36:35 +0000447<div class="doc_text">
448 <p>The <tt>TargetFrameInfo</tt> class is used to provide information about the
449 stack frame layout of the target. It holds the direction of stack growth,
450 the known stack alignment on entry to each function, and the offset to the
Bill Wendling5c385de2006-08-28 02:26:32 +0000451 local area. The offset to the local area is the offset from the stack
Reid Spencere5dc84b2005-07-19 01:36:35 +0000452 pointer on function entry to the first location where function data (local
453 variables, spill locations) can be stored.</p>
Reid Spencere5dc84b2005-07-19 01:36:35 +0000454</div>
Chris Lattnerc9afa282005-10-16 17:06:07 +0000455
456<!-- ======================================================================= -->
457<div class="doc_subsection">
458 <a name="targetsubtarget">The <tt>TargetSubtarget</tt> class</a>
459</div>
460
461<div class="doc_text">
Jim Laskey87ce5d852005-10-17 12:19:10 +0000462 <p>The <tt>TargetSubtarget</tt> class is used to provide information about the
463 specific chip set being targeted. A sub-target informs code generation of
464 which instructions are supported, instruction latencies and instruction
465 execution itinerary; i.e., which processing units are used, in what order, and
Bill Wendling5c385de2006-08-28 02:26:32 +0000466 for how long.</p>
Chris Lattnerc9afa282005-10-16 17:06:07 +0000467</div>
468
469
Chris Lattner565d7d52004-06-01 06:48:00 +0000470<!-- ======================================================================= -->
471<div class="doc_subsection">
Chris Lattnerf249fdc2004-06-01 17:18:11 +0000472 <a name="targetjitinfo">The <tt>TargetJITInfo</tt> class</a>
Chris Lattner565d7d52004-06-01 06:48:00 +0000473</div>
474
Bill Wendling5c385de2006-08-28 02:26:32 +0000475<div class="doc_text">
476 <p>The <tt>TargetJITInfo</tt> class exposes an abstract interface used by the
477 Just-In-Time code generator to perform target-specific activities, such as
478 emitting stubs. If a <tt>TargetMachine</tt> supports JIT code generation, it
479 should provide one of these objects through the <tt>getJITInfo</tt>
480 method.</p>
481</div>
482
Chris Lattner565d7d52004-06-01 06:48:00 +0000483<!-- *********************************************************************** -->
484<div class="doc_section">
485 <a name="codegendesc">Machine code description classes</a>
486</div>
487<!-- *********************************************************************** -->
488
Chris Lattnerb4e56642004-06-04 00:16:02 +0000489<div class="doc_text">
Chris Lattner565d7d52004-06-01 06:48:00 +0000490
Bill Wendling5c385de2006-08-28 02:26:32 +0000491<p>At the high-level, LLVM code is translated to a machine specific
492representation formed out of
493<a href="#machinefunction"><tt>MachineFunction</tt></a>,
494<a href="#machinebasicblock"><tt>MachineBasicBlock</tt></a>, and <a
Chris Lattnerb4e56642004-06-04 00:16:02 +0000495href="#machineinstr"><tt>MachineInstr</tt></a> instances
Bill Wendling5c385de2006-08-28 02:26:32 +0000496(defined in <tt>include/llvm/CodeGen</tt>). This representation is completely
497target agnostic, representing instructions in their most abstract form: an
498opcode and a series of operands. This representation is designed to support
499both an SSA representation for machine code, as well as a register allocated,
500non-SSA form.</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000501
502</div>
503
504<!-- ======================================================================= -->
505<div class="doc_subsection">
506 <a name="machineinstr">The <tt>MachineInstr</tt> class</a>
507</div>
508
509<div class="doc_text">
510
511<p>Target machine instructions are represented as instances of the
512<tt>MachineInstr</tt> class. This class is an extremely abstract way of
Reid Spencer4da97842005-04-24 20:56:18 +0000513representing machine instructions. In particular, it only keeps track of
514an opcode number and a set of operands.</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000515
Bill Wendling5c385de2006-08-28 02:26:32 +0000516<p>The opcode number is a simple unsigned integer that only has meaning to a
Chris Lattnerb4e56642004-06-04 00:16:02 +0000517specific backend. All of the instructions for a target should be defined in
Reid Spencer4da97842005-04-24 20:56:18 +0000518the <tt>*InstrInfo.td</tt> file for the target. The opcode enum values
Chris Lattner54903b62005-01-28 17:22:53 +0000519are auto-generated from this description. The <tt>MachineInstr</tt> class does
520not have any information about how to interpret the instruction (i.e., what the
Bill Wendling5c385de2006-08-28 02:26:32 +0000521semantics of the instruction are); for that you must refer to the
Chris Lattnerb4e56642004-06-04 00:16:02 +0000522<tt><a href="#targetinstrinfo">TargetInstrInfo</a></tt> class.</p>
523
524<p>The operands of a machine instruction can be of several different types:
Bill Wendling5c385de2006-08-28 02:26:32 +0000525a register reference, a constant integer, a basic block reference, etc. In
526addition, a machine operand should be marked as a def or a use of the value
Chris Lattnerb4e56642004-06-04 00:16:02 +0000527(though only registers are allowed to be defs).</p>
528
529<p>By convention, the LLVM code generator orders instruction operands so that
530all register definitions come before the register uses, even on architectures
Chris Lattner54903b62005-01-28 17:22:53 +0000531that are normally printed in other orders. For example, the SPARC add
Chris Lattnerb4e56642004-06-04 00:16:02 +0000532instruction: "<tt>add %i1, %i2, %i3</tt>" adds the "%i1", and "%i2" registers
533and stores the result into the "%i3" register. In the LLVM code generator,
534the operands should be stored as "<tt>%i3, %i1, %i2</tt>": with the destination
535first.</p>
536
Reid Spencer4da97842005-04-24 20:56:18 +0000537<p>Keeping destination (definition) operands at the beginning of the operand
538list has several advantages. In particular, the debugging printer will print
539the instruction like this:</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000540
Bill Wendling5c385de2006-08-28 02:26:32 +0000541<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +0000542<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000543%r3 = add %i1, %i2
Chris Lattnerb4e56642004-06-04 00:16:02 +0000544</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000545</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000546
Bill Wendling5c385de2006-08-28 02:26:32 +0000547<p>Also if the first operand is a def, it is easier to <a
Chris Lattnerb4e56642004-06-04 00:16:02 +0000548href="#buildmi">create instructions</a> whose only def is the first
549operand.</p>
550
551</div>
552
553<!-- _______________________________________________________________________ -->
554<div class="doc_subsubsection">
555 <a name="buildmi">Using the <tt>MachineInstrBuilder.h</tt> functions</a>
556</div>
557
558<div class="doc_text">
559
560<p>Machine instructions are created by using the <tt>BuildMI</tt> functions,
561located in the <tt>include/llvm/CodeGen/MachineInstrBuilder.h</tt> file. The
562<tt>BuildMI</tt> functions make it easy to build arbitrary machine
Bill Wendling5c385de2006-08-28 02:26:32 +0000563instructions. Usage of the <tt>BuildMI</tt> functions look like this:</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000564
Bill Wendling5c385de2006-08-28 02:26:32 +0000565<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +0000566<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000567// Create a 'DestReg = mov 42' (rendered in X86 assembly as 'mov DestReg, 42')
568// instruction. The '1' specifies how many operands will be added.
569MachineInstr *MI = BuildMI(X86::MOV32ri, 1, DestReg).addImm(42);
Chris Lattnerb4e56642004-06-04 00:16:02 +0000570
Bill Wendling5c385de2006-08-28 02:26:32 +0000571// Create the same instr, but insert it at the end of a basic block.
572MachineBasicBlock &amp;MBB = ...
573BuildMI(MBB, X86::MOV32ri, 1, DestReg).addImm(42);
Chris Lattnerb4e56642004-06-04 00:16:02 +0000574
Bill Wendling5c385de2006-08-28 02:26:32 +0000575// Create the same instr, but insert it before a specified iterator point.
576MachineBasicBlock::iterator MBBI = ...
577BuildMI(MBB, MBBI, X86::MOV32ri, 1, DestReg).addImm(42);
Chris Lattnerb4e56642004-06-04 00:16:02 +0000578
Bill Wendling5c385de2006-08-28 02:26:32 +0000579// Create a 'cmp Reg, 0' instruction, no destination reg.
580MI = BuildMI(X86::CMP32ri, 2).addReg(Reg).addImm(0);
581// Create an 'sahf' instruction which takes no operands and stores nothing.
582MI = BuildMI(X86::SAHF, 0);
Chris Lattnerb4e56642004-06-04 00:16:02 +0000583
Bill Wendling5c385de2006-08-28 02:26:32 +0000584// Create a self looping branch instruction.
585BuildMI(MBB, X86::JNE, 1).addMBB(&amp;MBB);
Chris Lattnerb4e56642004-06-04 00:16:02 +0000586</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000587</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000588
Bill Wendling5c385de2006-08-28 02:26:32 +0000589<p>The key thing to remember with the <tt>BuildMI</tt> functions is that you
590have to specify the number of operands that the machine instruction will take.
591This allows for efficient memory allocation. You also need to specify if
592operands default to be uses of values, not definitions. If you need to add a
593definition operand (other than the optional destination register), you must
594explicitly mark it as such:</p>
595
596<div class="doc_code">
597<pre>
598MI.addReg(Reg, MachineOperand::Def);
599</pre>
600</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000601
602</div>
603
604<!-- _______________________________________________________________________ -->
605<div class="doc_subsubsection">
Reid Spencer4da97842005-04-24 20:56:18 +0000606 <a name="fixedregs">Fixed (preassigned) registers</a>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000607</div>
608
609<div class="doc_text">
610
611<p>One important issue that the code generator needs to be aware of is the
612presence of fixed registers. In particular, there are often places in the
613instruction stream where the register allocator <em>must</em> arrange for a
614particular value to be in a particular register. This can occur due to
Reid Spencer4da97842005-04-24 20:56:18 +0000615limitations of the instruction set (e.g., the X86 can only do a 32-bit divide
Chris Lattnerb4e56642004-06-04 00:16:02 +0000616with the <tt>EAX</tt>/<tt>EDX</tt> registers), or external factors like calling
617conventions. In any case, the instruction selector should emit code that
618copies a virtual register into or out of a physical register when needed.</p>
619
620<p>For example, consider this simple LLVM example:</p>
621
Bill Wendling5c385de2006-08-28 02:26:32 +0000622<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +0000623<pre>
Matthijs Kooijmana6bb22e2008-06-04 15:46:35 +0000624define i32 @test(i32 %X, i32 %Y) {
625 %Z = udiv i32 %X, %Y
626 ret i32 %Z
Bill Wendling5c385de2006-08-28 02:26:32 +0000627}
Chris Lattnerb4e56642004-06-04 00:16:02 +0000628</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000629</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000630
Bill Wendling5c385de2006-08-28 02:26:32 +0000631<p>The X86 instruction selector produces this machine code for the <tt>div</tt>
632and <tt>ret</tt> (use
Chris Lattnerb4e56642004-06-04 00:16:02 +0000633"<tt>llc X.bc -march=x86 -print-machineinstrs</tt>" to get this):</p>
634
Bill Wendling5c385de2006-08-28 02:26:32 +0000635<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +0000636<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000637;; Start of div
638%EAX = mov %reg1024 ;; Copy X (in reg1024) into EAX
639%reg1027 = sar %reg1024, 31
640%EDX = mov %reg1027 ;; Sign extend X into EDX
641idiv %reg1025 ;; Divide by Y (in reg1025)
642%reg1026 = mov %EAX ;; Read the result (Z) out of EAX
Chris Lattnerb4e56642004-06-04 00:16:02 +0000643
Bill Wendling5c385de2006-08-28 02:26:32 +0000644;; Start of ret
645%EAX = mov %reg1026 ;; 32-bit return value goes in EAX
646ret
Chris Lattnerb4e56642004-06-04 00:16:02 +0000647</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000648</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000649
Chris Lattner54903b62005-01-28 17:22:53 +0000650<p>By the end of code generation, the register allocator has coalesced
Bill Wendling5c385de2006-08-28 02:26:32 +0000651the registers and deleted the resultant identity moves producing the
Chris Lattnerb4e56642004-06-04 00:16:02 +0000652following code:</p>
653
Bill Wendling5c385de2006-08-28 02:26:32 +0000654<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +0000655<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000656;; X is in EAX, Y is in ECX
657mov %EAX, %EDX
658sar %EDX, 31
659idiv %ECX
660ret
Chris Lattnerb4e56642004-06-04 00:16:02 +0000661</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +0000662</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000663
664<p>This approach is extremely general (if it can handle the X86 architecture,
665it can handle anything!) and allows all of the target specific
666knowledge about the instruction stream to be isolated in the instruction
667selector. Note that physical registers should have a short lifetime for good
Bill Wendling5c385de2006-08-28 02:26:32 +0000668code generation, and all physical registers are assumed dead on entry to and
669exit from basic blocks (before register allocation). Thus, if you need a value
Chris Lattnerb4e56642004-06-04 00:16:02 +0000670to be live across basic block boundaries, it <em>must</em> live in a virtual
671register.</p>
672
673</div>
674
675<!-- _______________________________________________________________________ -->
676<div class="doc_subsubsection">
Bill Wendling5c385de2006-08-28 02:26:32 +0000677 <a name="ssa">Machine code in SSA form</a>
Chris Lattnerb4e56642004-06-04 00:16:02 +0000678</div>
679
680<div class="doc_text">
681
Reid Spencer4da97842005-04-24 20:56:18 +0000682<p><tt>MachineInstr</tt>'s are initially selected in SSA-form, and
Chris Lattnerb4e56642004-06-04 00:16:02 +0000683are maintained in SSA-form until register allocation happens. For the most
Bill Wendling5c385de2006-08-28 02:26:32 +0000684part, this is trivially simple since LLVM is already in SSA form; LLVM PHI nodes
Chris Lattnerb4e56642004-06-04 00:16:02 +0000685become machine code PHI nodes, and virtual registers are only allowed to have a
686single definition.</p>
687
Bill Wendling5c385de2006-08-28 02:26:32 +0000688<p>After register allocation, machine code is no longer in SSA-form because there
Chris Lattnerb4e56642004-06-04 00:16:02 +0000689are no virtual registers left in the code.</p>
690
691</div>
692
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000693<!-- ======================================================================= -->
694<div class="doc_subsection">
695 <a name="machinebasicblock">The <tt>MachineBasicBlock</tt> class</a>
696</div>
697
698<div class="doc_text">
699
700<p>The <tt>MachineBasicBlock</tt> class contains a list of machine instructions
Bill Wendling5c385de2006-08-28 02:26:32 +0000701(<tt><a href="#machineinstr">MachineInstr</a></tt> instances). It roughly
702corresponds to the LLVM code input to the instruction selector, but there can be
703a one-to-many mapping (i.e. one LLVM basic block can map to multiple machine
704basic blocks). The <tt>MachineBasicBlock</tt> class has a
705"<tt>getBasicBlock</tt>" method, which returns the LLVM basic block that it
706comes from.</p>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000707
708</div>
709
710<!-- ======================================================================= -->
711<div class="doc_subsection">
712 <a name="machinefunction">The <tt>MachineFunction</tt> class</a>
713</div>
714
715<div class="doc_text">
716
717<p>The <tt>MachineFunction</tt> class contains a list of machine basic blocks
Bill Wendling5c385de2006-08-28 02:26:32 +0000718(<tt><a href="#machinebasicblock">MachineBasicBlock</a></tt> instances). It
719corresponds one-to-one with the LLVM function input to the instruction selector.
720In addition to a list of basic blocks, the <tt>MachineFunction</tt> contains a
721a <tt>MachineConstantPool</tt>, a <tt>MachineFrameInfo</tt>, a
Chris Lattner21ec2b42007-12-31 04:16:08 +0000722<tt>MachineFunctionInfo</tt>, and a <tt>MachineRegisterInfo</tt>. See
Bill Wendling5c385de2006-08-28 02:26:32 +0000723<tt>include/llvm/CodeGen/MachineFunction.h</tt> for more information.</p>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000724
725</div>
726
Chris Lattnerb4e56642004-06-04 00:16:02 +0000727<!-- *********************************************************************** -->
728<div class="doc_section">
Chris Lattner54903b62005-01-28 17:22:53 +0000729 <a name="codegenalgs">Target-independent code generation algorithms</a>
730</div>
731<!-- *********************************************************************** -->
732
733<div class="doc_text">
734
735<p>This section documents the phases described in the <a
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000736href="#high-level-design">high-level design of the code generator</a>. It
Chris Lattner54903b62005-01-28 17:22:53 +0000737explains how they work and some of the rationale behind their design.</p>
738
739</div>
740
741<!-- ======================================================================= -->
742<div class="doc_subsection">
743 <a name="instselect">Instruction Selection</a>
744</div>
745
746<div class="doc_text">
747<p>
Reid Spencer4da97842005-04-24 20:56:18 +0000748Instruction Selection is the process of translating LLVM code presented to the
Chris Lattner54903b62005-01-28 17:22:53 +0000749code generator into target-specific machine instructions. There are several
Evan Cheng4c73bef2007-10-08 17:54:24 +0000750well-known ways to do this in the literature. LLVM uses a SelectionDAG based
751instruction selector.
Chris Lattner54903b62005-01-28 17:22:53 +0000752</p>
753
Chris Lattneracf3d622005-10-16 00:36:38 +0000754<p>Portions of the DAG instruction selector are generated from the target
Bill Wendling5c385de2006-08-28 02:26:32 +0000755description (<tt>*.td</tt>) files. Our goal is for the entire instruction
Dan Gohmand4551fd2007-12-13 20:43:47 +0000756selector to be generated from these <tt>.td</tt> files, though currently
757there are still things that require custom C++ code.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000758</div>
759
760<!-- _______________________________________________________________________ -->
761<div class="doc_subsubsection">
762 <a name="selectiondag_intro">Introduction to SelectionDAGs</a>
763</div>
764
765<div class="doc_text">
766
Bill Wendling5c385de2006-08-28 02:26:32 +0000767<p>The SelectionDAG provides an abstraction for code representation in a way
768that is amenable to instruction selection using automatic techniques
769(e.g. dynamic-programming based optimal pattern matching selectors). It is also
770well-suited to other phases of code generation; in particular,
Chris Lattner17acad62005-10-16 20:02:19 +0000771instruction scheduling (SelectionDAG's are very close to scheduling DAGs
772post-selection). Additionally, the SelectionDAG provides a host representation
773where a large variety of very-low-level (but target-independent)
Reid Spencer4da97842005-04-24 20:56:18 +0000774<a href="#selectiondag_optimize">optimizations</a> may be
Bill Wendling5c385de2006-08-28 02:26:32 +0000775performed; ones which require extensive information about the instructions
776efficiently supported by the target.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000777
Bill Wendling5c385de2006-08-28 02:26:32 +0000778<p>The SelectionDAG is a Directed-Acyclic-Graph whose nodes are instances of the
Reid Spencer4da97842005-04-24 20:56:18 +0000779<tt>SDNode</tt> class. The primary payload of the <tt>SDNode</tt> is its
Chris Lattner17acad62005-10-16 20:02:19 +0000780operation code (Opcode) that indicates what operation the node performs and
781the operands to the operation.
Reid Spencer4da97842005-04-24 20:56:18 +0000782The various operation node types are described at the top of the
Chris Lattner17acad62005-10-16 20:02:19 +0000783<tt>include/llvm/CodeGen/SelectionDAGNodes.h</tt> file.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000784
Reid Spencer4da97842005-04-24 20:56:18 +0000785<p>Although most operations define a single value, each node in the graph may
786define multiple values. For example, a combined div/rem operation will define
787both the dividend and the remainder. Many other situations require multiple
788values as well. Each node also has some number of operands, which are edges
789to the node defining the used value. Because nodes may define multiple values,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000790edges are represented by instances of the <tt>SDValue</tt> class, which is
Bill Wendling5c385de2006-08-28 02:26:32 +0000791a <tt>&lt;SDNode, unsigned&gt;</tt> pair, indicating the node and result
792value being used, respectively. Each value produced by an <tt>SDNode</tt> has
Duncan Sands13237ac2008-06-06 12:08:01 +0000793an associated <tt>MVT</tt> (Machine Value Type) indicating what the type of the
794value is.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000795
Bill Wendling5c385de2006-08-28 02:26:32 +0000796<p>SelectionDAGs contain two different kinds of values: those that represent
797data flow and those that represent control flow dependencies. Data values are
798simple edges with an integer or floating point value type. Control edges are
799represented as "chain" edges which are of type <tt>MVT::Other</tt>. These edges
800provide an ordering between nodes that have side effects (such as
801loads, stores, calls, returns, etc). All nodes that have side effects should
802take a token chain as input and produce a new one as output. By convention,
803token chain inputs are always operand #0, and chain results are always the last
Chris Lattner54903b62005-01-28 17:22:53 +0000804value produced by an operation.</p>
805
Bill Wendling5c385de2006-08-28 02:26:32 +0000806<p>A SelectionDAG has designated "Entry" and "Root" nodes. The Entry node is
807always a marker node with an Opcode of <tt>ISD::EntryToken</tt>. The Root node
808is the final side-effecting node in the token chain. For example, in a single
809basic block function it would be the return node.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000810
Bill Wendling5c385de2006-08-28 02:26:32 +0000811<p>One important concept for SelectionDAGs is the notion of a "legal" vs.
812"illegal" DAG. A legal DAG for a target is one that only uses supported
813operations and supported types. On a 32-bit PowerPC, for example, a DAG with
814a value of type i1, i8, i16, or i64 would be illegal, as would a DAG that uses a
815SREM or UREM operation. The
816<a href="#selectiondag_legalize">legalize</a> phase is responsible for turning
817an illegal DAG into a legal DAG.</p>
818
Chris Lattner54903b62005-01-28 17:22:53 +0000819</div>
820
821<!-- _______________________________________________________________________ -->
822<div class="doc_subsubsection">
Reid Spencer4da97842005-04-24 20:56:18 +0000823 <a name="selectiondag_process">SelectionDAG Instruction Selection Process</a>
Chris Lattner54903b62005-01-28 17:22:53 +0000824</div>
825
826<div class="doc_text">
827
Bill Wendling5c385de2006-08-28 02:26:32 +0000828<p>SelectionDAG-based instruction selection consists of the following steps:</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000829
830<ol>
Bill Wendling5c385de2006-08-28 02:26:32 +0000831<li><a href="#selectiondag_build">Build initial DAG</a> - This stage
832 performs a simple translation from the input LLVM code to an illegal
833 SelectionDAG.</li>
Chris Lattner54903b62005-01-28 17:22:53 +0000834<li><a href="#selectiondag_optimize">Optimize SelectionDAG</a> - This stage
Bill Wendling5c385de2006-08-28 02:26:32 +0000835 performs simple optimizations on the SelectionDAG to simplify it, and
836 recognize meta instructions (like rotates and <tt>div</tt>/<tt>rem</tt>
837 pairs) for targets that support these meta operations. This makes the
838 resultant code more efficient and the <a href="#selectiondag_select">select
839 instructions from DAG</a> phase (below) simpler.</li>
Chris Lattner54903b62005-01-28 17:22:53 +0000840<li><a href="#selectiondag_legalize">Legalize SelectionDAG</a> - This stage
Bill Wendling5c385de2006-08-28 02:26:32 +0000841 converts the illegal SelectionDAG to a legal SelectionDAG by eliminating
Chris Lattner54903b62005-01-28 17:22:53 +0000842 unsupported operations and data types.</li>
843<li><a href="#selectiondag_optimize">Optimize SelectionDAG (#2)</a> - This
Bill Wendling5c385de2006-08-28 02:26:32 +0000844 second run of the SelectionDAG optimizes the newly legalized DAG to
Chris Lattner54903b62005-01-28 17:22:53 +0000845 eliminate inefficiencies introduced by legalization.</li>
846<li><a href="#selectiondag_select">Select instructions from DAG</a> - Finally,
847 the target instruction selector matches the DAG operations to target
Chris Lattneracf3d622005-10-16 00:36:38 +0000848 instructions. This process translates the target-independent input DAG into
849 another DAG of target instructions.</li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +0000850<li><a href="#selectiondag_sched">SelectionDAG Scheduling and Formation</a>
Chris Lattneracf3d622005-10-16 00:36:38 +0000851 - The last phase assigns a linear order to the instructions in the
852 target-instruction DAG and emits them into the MachineFunction being
853 compiled. This step uses traditional prepass scheduling techniques.</li>
Chris Lattner54903b62005-01-28 17:22:53 +0000854</ol>
855
856<p>After all of these steps are complete, the SelectionDAG is destroyed and the
857rest of the code generation passes are run.</p>
858
Chris Lattner776c73f2005-10-17 01:40:33 +0000859<p>One great way to visualize what is going on here is to take advantage of a
Dan Gohmandd51d522008-09-10 22:23:41 +0000860few LLC command line options. The following options pop up a window displaying
861the SelectionDAG at specific times (if you only get errors printed to the console
862while using this, you probably
863<a href="ProgrammersManual.html#ViewGraph">need to configure your system</a> to
864add support for it).</p>
865
866<ul>
867<li><tt>-view-dag-combine1-dags</tt> displays the DAG after being built, before
868 the first optimization pass.</li>
869<li><tt>-view-legalize-dags</tt> displays the DAG before Legalization.</li>
870<li><tt>-view-dag-combine2-dags</tt> displays the DAG before the second
871 optimization pass.</li>
872<li><tt>-view-isel-dags</tt> displays the DAG before the Select phase.</li>
873<li><tt>-view-sched-dags</tt> displays the DAG before Scheduling.</li>
874</ul>
875
876<p>The <tt>-view-sunit-dags</tt> displays the Scheduler's dependency graph.
877This graph is based on the final SelectionDAG, with nodes that must be
878scheduled together bundled into a single scheduling-unit node, and with
879immediate operands and other nodes that aren't relevent for scheduling
880omitted.
Dan Gohmand4f21652007-10-15 21:07:59 +0000881</p>
Bill Wendling5c385de2006-08-28 02:26:32 +0000882
Chris Lattner54903b62005-01-28 17:22:53 +0000883</div>
884
885<!-- _______________________________________________________________________ -->
886<div class="doc_subsubsection">
887 <a name="selectiondag_build">Initial SelectionDAG Construction</a>
888</div>
889
890<div class="doc_text">
891
Bill Wendling6737f5d2006-08-28 03:04:05 +0000892<p>The initial SelectionDAG is na&iuml;vely peephole expanded from the LLVM
893input by the <tt>SelectionDAGLowering</tt> class in the
Bill Wendling5c385de2006-08-28 02:26:32 +0000894<tt>lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp</tt> file. The intent of this
895pass is to expose as much low-level, target-specific details to the SelectionDAG
896as possible. This pass is mostly hard-coded (e.g. an LLVM <tt>add</tt> turns
Dan Gohmand9ef0732008-10-03 00:07:11 +0000897into an <tt>SDNode add</tt> while a <tt>getelementptr</tt> is expanded into the
Bill Wendling5c385de2006-08-28 02:26:32 +0000898obvious arithmetic). This pass requires target-specific hooks to lower calls,
899returns, varargs, etc. For these features, the
900<tt><a href="#targetlowering">TargetLowering</a></tt> interface is used.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000901
902</div>
903
904<!-- _______________________________________________________________________ -->
905<div class="doc_subsubsection">
906 <a name="selectiondag_legalize">SelectionDAG Legalize Phase</a>
907</div>
908
909<div class="doc_text">
910
911<p>The Legalize phase is in charge of converting a DAG to only use the types and
912operations that are natively supported by the target. This involves two major
913tasks:</p>
914
915<ol>
916<li><p>Convert values of unsupported types to values of supported types.</p>
Chris Lattner17acad62005-10-16 20:02:19 +0000917 <p>There are two main ways of doing this: converting small types to
918 larger types ("promoting"), and breaking up large integer types
919 into smaller ones ("expanding"). For example, a target might require
920 that all f32 values are promoted to f64 and that all i1/i8/i16 values
921 are promoted to i32. The same target might require that all i64 values
922 be expanded into i32 values. These changes can insert sign and zero
Bill Wendling5c385de2006-08-28 02:26:32 +0000923 extensions as needed to make sure that the final code has the same
924 behavior as the input.</p>
Chris Lattner17acad62005-10-16 20:02:19 +0000925 <p>A target implementation tells the legalizer which types are supported
926 (and which register class to use for them) by calling the
Bill Wendling5c385de2006-08-28 02:26:32 +0000927 <tt>addRegisterClass</tt> method in its TargetLowering constructor.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000928</li>
929
Chris Lattner17acad62005-10-16 20:02:19 +0000930<li><p>Eliminate operations that are not supported by the target.</p>
931 <p>Targets often have weird constraints, such as not supporting every
Chris Lattner54903b62005-01-28 17:22:53 +0000932 operation on every supported datatype (e.g. X86 does not support byte
Chris Lattner17acad62005-10-16 20:02:19 +0000933 conditional moves and PowerPC does not support sign-extending loads from
Bill Wendling5c385de2006-08-28 02:26:32 +0000934 a 16-bit memory location). Legalize takes care of this by open-coding
Chris Lattner17acad62005-10-16 20:02:19 +0000935 another sequence of operations to emulate the operation ("expansion"), by
Bill Wendling5c385de2006-08-28 02:26:32 +0000936 promoting one type to a larger type that supports the operation
937 ("promotion"), or by using a target-specific hook to implement the
938 legalization ("custom").</p>
Chris Lattner17acad62005-10-16 20:02:19 +0000939 <p>A target implementation tells the legalizer which operations are not
940 supported (and which of the above three actions to take) by calling the
Bill Wendling5c385de2006-08-28 02:26:32 +0000941 <tt>setOperationAction</tt> method in its <tt>TargetLowering</tt>
942 constructor.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000943</li>
944</ol>
945
Bill Wendling5c385de2006-08-28 02:26:32 +0000946<p>Prior to the existance of the Legalize pass, we required that every target
947<a href="#selectiondag_optimize">selector</a> supported and handled every
Chris Lattner17acad62005-10-16 20:02:19 +0000948operator and type even if they are not natively supported. The introduction of
Bill Wendling5c385de2006-08-28 02:26:32 +0000949the Legalize phase allows all of the cannonicalization patterns to be shared
950across targets, and makes it very easy to optimize the cannonicalized code
951because it is still in the form of a DAG.</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000952
953</div>
954
955<!-- _______________________________________________________________________ -->
956<div class="doc_subsubsection">
Chris Lattneracf3d622005-10-16 00:36:38 +0000957 <a name="selectiondag_optimize">SelectionDAG Optimization Phase: the DAG
958 Combiner</a>
Chris Lattner54903b62005-01-28 17:22:53 +0000959</div>
960
961<div class="doc_text">
962
Bill Wendling5c385de2006-08-28 02:26:32 +0000963<p>The SelectionDAG optimization phase is run twice for code generation: once
Reid Spencer4da97842005-04-24 20:56:18 +0000964immediately after the DAG is built and once after legalization. The first run
965of the pass allows the initial code to be cleaned up (e.g. performing
966optimizations that depend on knowing that the operators have restricted type
967inputs). The second run of the pass cleans up the messy code generated by the
Chris Lattner17acad62005-10-16 20:02:19 +0000968Legalize pass, which allows Legalize to be very simple (it can focus on making
Bill Wendling5c385de2006-08-28 02:26:32 +0000969code legal instead of focusing on generating <em>good</em> and legal code).</p>
970
971<p>One important class of optimizations performed is optimizing inserted sign
972and zero extension instructions. We currently use ad-hoc techniques, but could
973move to more rigorous techniques in the future. Here are some good papers on
974the subject:</p>
Chris Lattner54903b62005-01-28 17:22:53 +0000975
976<p>
Bill Wendling5c385de2006-08-28 02:26:32 +0000977 "<a href="http://www.eecs.harvard.edu/~nr/pubs/widen-abstract.html">Widening
978 integer arithmetic</a>"<br>
979 Kevin Redwine and Norman Ramsey<br>
980 International Conference on Compiler Construction (CC) 2004
Chris Lattner54903b62005-01-28 17:22:53 +0000981</p>
982
983
984<p>
985 "<a href="http://portal.acm.org/citation.cfm?doid=512529.512552">Effective
986 sign extension elimination</a>"<br>
987 Motohiro Kawahito, Hideaki Komatsu, and Toshio Nakatani<br>
988 Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language Design
989 and Implementation.
990</p>
991
992</div>
993
994<!-- _______________________________________________________________________ -->
995<div class="doc_subsubsection">
996 <a name="selectiondag_select">SelectionDAG Select Phase</a>
997</div>
998
999<div class="doc_text">
1000
1001<p>The Select phase is the bulk of the target-specific code for instruction
Bill Wendling5c385de2006-08-28 02:26:32 +00001002selection. This phase takes a legal SelectionDAG as input, pattern matches the
1003instructions supported by the target to this DAG, and produces a new DAG of
1004target code. For example, consider the following LLVM fragment:</p>
Chris Lattner17acad62005-10-16 20:02:19 +00001005
Bill Wendling5c385de2006-08-28 02:26:32 +00001006<div class="doc_code">
Chris Lattner17acad62005-10-16 20:02:19 +00001007<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001008%t1 = add float %W, %X
1009%t2 = mul float %t1, %Y
1010%t3 = add float %t2, %Z
Chris Lattner17acad62005-10-16 20:02:19 +00001011</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001012</div>
Chris Lattner17acad62005-10-16 20:02:19 +00001013
Bill Wendling5c385de2006-08-28 02:26:32 +00001014<p>This LLVM code corresponds to a SelectionDAG that looks basically like
1015this:</p>
Chris Lattner17acad62005-10-16 20:02:19 +00001016
Bill Wendling5c385de2006-08-28 02:26:32 +00001017<div class="doc_code">
Chris Lattner17acad62005-10-16 20:02:19 +00001018<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001019(fadd:f32 (fmul:f32 (fadd:f32 W, X), Y), Z)
Chris Lattner17acad62005-10-16 20:02:19 +00001020</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001021</div>
Chris Lattner17acad62005-10-16 20:02:19 +00001022
Chris Lattner8113c672005-10-17 15:19:24 +00001023<p>If a target supports floating point multiply-and-add (FMA) operations, one
Chris Lattner17acad62005-10-16 20:02:19 +00001024of the adds can be merged with the multiply. On the PowerPC, for example, the
1025output of the instruction selector might look like this DAG:</p>
1026
Bill Wendling5c385de2006-08-28 02:26:32 +00001027<div class="doc_code">
Chris Lattner17acad62005-10-16 20:02:19 +00001028<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001029(FMADDS (FADDS W, X), Y, Z)
Chris Lattner17acad62005-10-16 20:02:19 +00001030</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001031</div>
Chris Lattner17acad62005-10-16 20:02:19 +00001032
Bill Wendling5c385de2006-08-28 02:26:32 +00001033<p>The <tt>FMADDS</tt> instruction is a ternary instruction that multiplies its
1034first two operands and adds the third (as single-precision floating-point
1035numbers). The <tt>FADDS</tt> instruction is a simple binary single-precision
1036add instruction. To perform this pattern match, the PowerPC backend includes
1037the following instruction definitions:</p>
Chris Lattner17acad62005-10-16 20:02:19 +00001038
Bill Wendling5c385de2006-08-28 02:26:32 +00001039<div class="doc_code">
Chris Lattner17acad62005-10-16 20:02:19 +00001040<pre>
1041def FMADDS : AForm_1&lt;59, 29,
1042 (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRC, F4RC:$FRB),
1043 "fmadds $FRT, $FRA, $FRC, $FRB",
1044 [<b>(set F4RC:$FRT, (fadd (fmul F4RC:$FRA, F4RC:$FRC),
1045 F4RC:$FRB))</b>]&gt;;
1046def FADDS : AForm_2&lt;59, 21,
1047 (ops F4RC:$FRT, F4RC:$FRA, F4RC:$FRB),
1048 "fadds $FRT, $FRA, $FRB",
1049 [<b>(set F4RC:$FRT, (fadd F4RC:$FRA, F4RC:$FRB))</b>]&gt;;
1050</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001051</div>
Chris Lattner17acad62005-10-16 20:02:19 +00001052
1053<p>The portion of the instruction definition in bold indicates the pattern used
1054to match the instruction. The DAG operators (like <tt>fmul</tt>/<tt>fadd</tt>)
1055are defined in the <tt>lib/Target/TargetSelectionDAG.td</tt> file.
1056"<tt>F4RC</tt>" is the register class of the input and result values.<p>
1057
1058<p>The TableGen DAG instruction selector generator reads the instruction
Bill Wendling5c385de2006-08-28 02:26:32 +00001059patterns in the <tt>.td</tt> file and automatically builds parts of the pattern
1060matching code for your target. It has the following strengths:</p>
Chris Lattner17acad62005-10-16 20:02:19 +00001061
1062<ul>
1063<li>At compiler-compiler time, it analyzes your instruction patterns and tells
Chris Lattner721f3ce2005-10-17 04:18:41 +00001064 you if your patterns make sense or not.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001065<li>It can handle arbitrary constraints on operands for the pattern match. In
Chris Lattner721f3ce2005-10-17 04:18:41 +00001066 particular, it is straight-forward to say things like "match any immediate
Chris Lattner17acad62005-10-16 20:02:19 +00001067 that is a 13-bit sign-extended value". For examples, see the
Bill Wendling5c385de2006-08-28 02:26:32 +00001068 <tt>immSExt16</tt> and related <tt>tblgen</tt> classes in the PowerPC
1069 backend.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001070<li>It knows several important identities for the patterns defined. For
1071 example, it knows that addition is commutative, so it allows the
1072 <tt>FMADDS</tt> pattern above to match "<tt>(fadd X, (fmul Y, Z))</tt>" as
1073 well as "<tt>(fadd (fmul X, Y), Z)</tt>", without the target author having
1074 to specially handle this case.</li>
Chris Lattner721f3ce2005-10-17 04:18:41 +00001075<li>It has a full-featured type-inferencing system. In particular, you should
Chris Lattner17acad62005-10-16 20:02:19 +00001076 rarely have to explicitly tell the system what type parts of your patterns
Bill Wendling5c385de2006-08-28 02:26:32 +00001077 are. In the <tt>FMADDS</tt> case above, we didn't have to tell
1078 <tt>tblgen</tt> that all of the nodes in the pattern are of type 'f32'. It
1079 was able to infer and propagate this knowledge from the fact that
1080 <tt>F4RC</tt> has type 'f32'.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001081<li>Targets can define their own (and rely on built-in) "pattern fragments".
1082 Pattern fragments are chunks of reusable patterns that get inlined into your
Bill Wendling5c385de2006-08-28 02:26:32 +00001083 patterns during compiler-compiler time. For example, the integer
1084 "<tt>(not x)</tt>" operation is actually defined as a pattern fragment that
1085 expands as "<tt>(xor x, -1)</tt>", since the SelectionDAG does not have a
1086 native '<tt>not</tt>' operation. Targets can define their own short-hand
1087 fragments as they see fit. See the definition of '<tt>not</tt>' and
1088 '<tt>ineg</tt>' for examples.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001089<li>In addition to instructions, targets can specify arbitrary patterns that
Bill Wendling5c385de2006-08-28 02:26:32 +00001090 map to one or more instructions using the 'Pat' class. For example,
Chris Lattner721f3ce2005-10-17 04:18:41 +00001091 the PowerPC has no way to load an arbitrary integer immediate into a
Chris Lattner17acad62005-10-16 20:02:19 +00001092 register in one instruction. To tell tblgen how to do this, it defines:
Bill Wendling5c385de2006-08-28 02:26:32 +00001093 <br>
1094 <br>
1095 <div class="doc_code">
Chris Lattner17acad62005-10-16 20:02:19 +00001096 <pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001097// Arbitrary immediate support. Implement in terms of LIS/ORI.
1098def : Pat&lt;(i32 imm:$imm),
1099 (ORI (LIS (HI16 imm:$imm)), (LO16 imm:$imm))&gt;;
Chris Lattner17acad62005-10-16 20:02:19 +00001100 </pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001101 </div>
1102 <br>
Chris Lattner17acad62005-10-16 20:02:19 +00001103 If none of the single-instruction patterns for loading an immediate into a
1104 register match, this will be used. This rule says "match an arbitrary i32
Bill Wendling5c385de2006-08-28 02:26:32 +00001105 immediate, turning it into an <tt>ORI</tt> ('or a 16-bit immediate') and an
1106 <tt>LIS</tt> ('load 16-bit immediate, where the immediate is shifted to the
1107 left 16 bits') instruction". To make this work, the
1108 <tt>LO16</tt>/<tt>HI16</tt> node transformations are used to manipulate the
1109 input immediate (in this case, take the high or low 16-bits of the
1110 immediate).</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001111<li>While the system does automate a lot, it still allows you to write custom
Bill Wendling5c385de2006-08-28 02:26:32 +00001112 C++ code to match special cases if there is something that is hard to
1113 express.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001114</ul>
1115
Bill Wendling5c385de2006-08-28 02:26:32 +00001116<p>While it has many strengths, the system currently has some limitations,
1117primarily because it is a work in progress and is not yet finished:</p>
Chris Lattner17acad62005-10-16 20:02:19 +00001118
1119<ul>
1120<li>Overall, there is no way to define or match SelectionDAG nodes that define
Bill Wendling5c385de2006-08-28 02:26:32 +00001121 multiple values (e.g. <tt>ADD_PARTS</tt>, <tt>LOAD</tt>, <tt>CALL</tt>,
1122 etc). This is the biggest reason that you currently still <em>have to</em>
1123 write custom C++ code for your instruction selector.</li>
1124<li>There is no great way to support matching complex addressing modes yet. In
1125 the future, we will extend pattern fragments to allow them to define
1126 multiple values (e.g. the four operands of the <a href="#x86_memory">X86
Dan Gohmand4551fd2007-12-13 20:43:47 +00001127 addressing mode</a>, which are currently matched with custom C++ code).
1128 In addition, we'll extend fragments so that a
Bill Wendling5c385de2006-08-28 02:26:32 +00001129 fragment can match multiple different patterns.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001130<li>We don't automatically infer flags like isStore/isLoad yet.</li>
1131<li>We don't automatically generate the set of supported registers and
Jim Laskey57825842006-12-15 10:40:48 +00001132 operations for the <a href="#selectiondag_legalize">Legalizer</a> yet.</li>
Chris Lattner17acad62005-10-16 20:02:19 +00001133<li>We don't have a way of tying in custom legalized nodes yet.</li>
Chris Lattner721f3ce2005-10-17 04:18:41 +00001134</ul>
Chris Lattner17acad62005-10-16 20:02:19 +00001135
1136<p>Despite these limitations, the instruction selector generator is still quite
1137useful for most of the binary and logical operations in typical instruction
1138sets. If you run into any problems or can't figure out how to do something,
1139please let Chris know!</p>
Chris Lattner54903b62005-01-28 17:22:53 +00001140
1141</div>
1142
1143<!-- _______________________________________________________________________ -->
1144<div class="doc_subsubsection">
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001145 <a name="selectiondag_sched">SelectionDAG Scheduling and Formation Phase</a>
Chris Lattneracf3d622005-10-16 00:36:38 +00001146</div>
1147
1148<div class="doc_text">
1149
1150<p>The scheduling phase takes the DAG of target instructions from the selection
1151phase and assigns an order. The scheduler can pick an order depending on
1152various constraints of the machines (i.e. order for minimal register pressure or
1153try to cover instruction latencies). Once an order is established, the DAG is
Bill Wendling5c385de2006-08-28 02:26:32 +00001154converted to a list of <tt><a href="#machineinstr">MachineInstr</a></tt>s and
1155the SelectionDAG is destroyed.</p>
Chris Lattneracf3d622005-10-16 00:36:38 +00001156
Jeff Cohendd24d7c2005-10-24 16:54:55 +00001157<p>Note that this phase is logically separate from the instruction selection
Chris Lattner9fcf0002005-10-17 03:09:31 +00001158phase, but is tied to it closely in the code because it operates on
1159SelectionDAGs.</p>
1160
Chris Lattneracf3d622005-10-16 00:36:38 +00001161</div>
1162
1163<!-- _______________________________________________________________________ -->
1164<div class="doc_subsubsection">
Chris Lattner54903b62005-01-28 17:22:53 +00001165 <a name="selectiondag_future">Future directions for the SelectionDAG</a>
1166</div>
1167
1168<div class="doc_text">
1169
1170<ol>
Chris Lattneracf3d622005-10-16 00:36:38 +00001171<li>Optional function-at-a-time selection.</li>
Bill Wendling5c385de2006-08-28 02:26:32 +00001172<li>Auto-generate entire selector from <tt>.td</tt> file.</li>
Chris Lattner54903b62005-01-28 17:22:53 +00001173</ol>
1174
1175</div>
Reid Spencer4da97842005-04-24 20:56:18 +00001176
1177<!-- ======================================================================= -->
1178<div class="doc_subsection">
1179 <a name="ssamco">SSA-based Machine Code Optimizations</a>
1180</div>
1181<div class="doc_text"><p>To Be Written</p></div>
Bill Wendling00c5aec2006-09-01 21:46:00 +00001182
Reid Spencer4da97842005-04-24 20:56:18 +00001183<!-- ======================================================================= -->
1184<div class="doc_subsection">
Bill Wendlingd495bd02006-09-06 18:42:41 +00001185 <a name="liveintervals">Live Intervals</a>
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001186</div>
1187
1188<div class="doc_text">
1189
Bill Wendlingd495bd02006-09-06 18:42:41 +00001190<p>Live Intervals are the ranges (intervals) where a variable is <i>live</i>.
1191They are used by some <a href="#regalloc">register allocator</a> passes to
Bill Wendlingd0c0bc52006-09-07 08:36:28 +00001192determine if two or more virtual registers which require the same physical
Bill Wendlingf5ce7ef2006-09-07 08:39:35 +00001193register are live at the same point in the program (i.e., they conflict). When
Bill Wendlingd0c0bc52006-09-07 08:36:28 +00001194this situation occurs, one virtual register must be <i>spilled</i>.</p>
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001195
1196</div>
1197
1198<!-- _______________________________________________________________________ -->
1199<div class="doc_subsubsection">
1200 <a name="livevariable_analysis">Live Variable Analysis</a>
1201</div>
1202
1203<div class="doc_text">
1204
Bill Wendlingd495bd02006-09-06 18:42:41 +00001205<p>The first step in determining the live intervals of variables is to
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001206calculate the set of registers that are immediately dead after the
Bill Wendlingd495bd02006-09-06 18:42:41 +00001207instruction (i.e., the instruction calculates the value, but it is
1208never used) and the set of registers that are used by the instruction,
1209but are never used after the instruction (i.e., they are killed). Live
Bill Wendlingd0c0bc52006-09-07 08:36:28 +00001210variable information is computed for each <i>virtual</i> register and
Bill Wendlingd495bd02006-09-06 18:42:41 +00001211<i>register allocatable</i> physical register in the function. This
1212is done in a very efficient manner because it uses SSA to sparsely
Bill Wendlingd0c0bc52006-09-07 08:36:28 +00001213compute lifetime information for virtual registers (which are in SSA
Bill Wendlingd495bd02006-09-06 18:42:41 +00001214form) and only has to track physical registers within a block. Before
1215register allocation, LLVM can assume that physical registers are only
1216live within a single basic block. This allows it to do a single,
1217local analysis to resolve physical register lifetimes within each
1218basic block. If a physical register is not register allocatable (e.g.,
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001219a stack pointer or condition codes), it is not tracked.</p>
1220
1221<p>Physical registers may be live in to or out of a function. Live in values
Bill Wendlingd495bd02006-09-06 18:42:41 +00001222are typically arguments in registers. Live out values are typically return
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001223values in registers. Live in values are marked as such, and are given a dummy
Bill Wendlingd0c0bc52006-09-07 08:36:28 +00001224"defining" instruction during live intervals analysis. If the last basic block
Bill Wendlingd495bd02006-09-06 18:42:41 +00001225of a function is a <tt>return</tt>, then it's marked as using all live out
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001226values in the function.</p>
1227
1228<p><tt>PHI</tt> nodes need to be handled specially, because the calculation
1229of the live variable information from a depth first traversal of the CFG of
Bill Wendlingd495bd02006-09-06 18:42:41 +00001230the function won't guarantee that a virtual register used by the <tt>PHI</tt>
1231node is defined before it's used. When a <tt>PHI</tt> node is encounted, only
1232the definition is handled, because the uses will be handled in other basic
1233blocks.</p>
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001234
1235<p>For each <tt>PHI</tt> node of the current basic block, we simulate an
1236assignment at the end of the current basic block and traverse the successor
1237basic blocks. If a successor basic block has a <tt>PHI</tt> node and one of
1238the <tt>PHI</tt> node's operands is coming from the current basic block,
1239then the variable is marked as <i>alive</i> within the current basic block
1240and all of its predecessor basic blocks, until the basic block with the
1241defining instruction is encountered.</p>
1242
1243</div>
1244
Bill Wendlingd495bd02006-09-06 18:42:41 +00001245<!-- _______________________________________________________________________ -->
1246<div class="doc_subsubsection">
1247 <a name="liveintervals_analysis">Live Intervals Analysis</a>
1248</div>
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001249
Bill Wendlingd495bd02006-09-06 18:42:41 +00001250<div class="doc_text">
Bill Wendling34ab0672006-10-11 06:30:10 +00001251
Bill Wendlingf21825f2006-10-11 18:00:22 +00001252<p>We now have the information available to perform the live intervals analysis
Bill Wendling34ab0672006-10-11 06:30:10 +00001253and build the live intervals themselves. We start off by numbering the basic
1254blocks and machine instructions. We then handle the "live-in" values. These
1255are in physical registers, so the physical register is assumed to be killed by
1256the end of the basic block. Live intervals for virtual registers are computed
Bill Wendlingf21825f2006-10-11 18:00:22 +00001257for some ordering of the machine instructions <tt>[1, N]</tt>. A live interval
1258is an interval <tt>[i, j)</tt>, where <tt>1 <= i <= j < N</tt>, for which a
Bill Wendling34ab0672006-10-11 06:30:10 +00001259variable is live.</p>
1260
Bill Wendlingf21825f2006-10-11 18:00:22 +00001261<p><i><b>More to come...</b></i></p>
1262
Bill Wendlingd495bd02006-09-06 18:42:41 +00001263</div>
Bill Wendlingbb902cf2006-09-04 23:35:52 +00001264
1265<!-- ======================================================================= -->
1266<div class="doc_subsection">
Reid Spencer4da97842005-04-24 20:56:18 +00001267 <a name="regalloc">Register Allocation</a>
1268</div>
Bill Wendling00c5aec2006-09-01 21:46:00 +00001269
1270<div class="doc_text">
1271
Bill Wendling34ab0672006-10-11 06:30:10 +00001272<p>The <i>Register Allocation problem</i> consists in mapping a program
1273<i>P<sub>v</sub></i>, that can use an unbounded number of virtual
1274registers, to a program <i>P<sub>p</sub></i> that contains a finite
1275(possibly small) number of physical registers. Each target architecture has
1276a different number of physical registers. If the number of physical
1277registers is not enough to accommodate all the virtual registers, some of
1278them will have to be mapped into memory. These virtuals are called
1279<i>spilled virtuals</i>.</p>
Bill Wendling00c5aec2006-09-01 21:46:00 +00001280
1281</div>
1282
1283<!-- _______________________________________________________________________ -->
1284
1285<div class="doc_subsubsection">
1286 <a name="regAlloc_represent">How registers are represented in LLVM</a>
1287</div>
1288
1289<div class="doc_text">
1290
1291<p>In LLVM, physical registers are denoted by integer numbers that
1292normally range from 1 to 1023. To see how this numbering is defined
1293for a particular architecture, you can read the
1294<tt>GenRegisterNames.inc</tt> file for that architecture. For
1295instance, by inspecting
1296<tt>lib/Target/X86/X86GenRegisterNames.inc</tt> we see that the 32-bit
1297register <tt>EAX</tt> is denoted by 15, and the MMX register
1298<tt>MM0</tt> is mapped to 48.</p>
1299
1300<p>Some architectures contain registers that share the same physical
1301location. A notable example is the X86 platform. For instance, in the
1302X86 architecture, the registers <tt>EAX</tt>, <tt>AX</tt> and
1303<tt>AL</tt> share the first eight bits. These physical registers are
1304marked as <i>aliased</i> in LLVM. Given a particular architecture, you
1305can check which registers are aliased by inspecting its
1306<tt>RegisterInfo.td</tt> file. Moreover, the method
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001307<tt>TargetRegisterInfo::getAliasSet(p_reg)</tt> returns an array containing
Bill Wendling00c5aec2006-09-01 21:46:00 +00001308all the physical registers aliased to the register <tt>p_reg</tt>.</p>
1309
1310<p>Physical registers, in LLVM, are grouped in <i>Register Classes</i>.
1311Elements in the same register class are functionally equivalent, and can
1312be interchangeably used. Each virtual register can only be mapped to
1313physical registers of a particular class. For instance, in the X86
1314architecture, some virtuals can only be allocated to 8 bit registers.
1315A register class is described by <tt>TargetRegisterClass</tt> objects.
1316To discover if a virtual register is compatible with a given physical,
1317this code can be used:
1318</p>
1319
1320<div class="doc_code">
1321<pre>
Jim Laskey57825842006-12-15 10:40:48 +00001322bool RegMapping_Fer::compatible_class(MachineFunction &amp;mf,
Bill Wendling00c5aec2006-09-01 21:46:00 +00001323 unsigned v_reg,
1324 unsigned p_reg) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001325 assert(TargetRegisterInfo::isPhysicalRegister(p_reg) &amp;&amp;
Bill Wendling00c5aec2006-09-01 21:46:00 +00001326 "Target register must be physical");
Chris Lattner21ec2b42007-12-31 04:16:08 +00001327 const TargetRegisterClass *trc = mf.getRegInfo().getRegClass(v_reg);
1328 return trc-&gt;contains(p_reg);
Bill Wendling00c5aec2006-09-01 21:46:00 +00001329}
1330</pre>
1331</div>
1332
1333<p>Sometimes, mostly for debugging purposes, it is useful to change
1334the number of physical registers available in the target
1335architecture. This must be done statically, inside the
1336<tt>TargetRegsterInfo.td</tt> file. Just <tt>grep</tt> for
1337<tt>RegisterClass</tt>, the last parameter of which is a list of
1338registers. Just commenting some out is one simple way to avoid them
1339being used. A more polite way is to explicitly exclude some registers
1340from the <i>allocation order</i>. See the definition of the
1341<tt>GR</tt> register class in
1342<tt>lib/Target/IA64/IA64RegisterInfo.td</tt> for an example of this
1343(e.g., <tt>numReservedRegs</tt> registers are hidden.)</p>
1344
1345<p>Virtual registers are also denoted by integer numbers. Contrary to
1346physical registers, different virtual registers never share the same
1347number. The smallest virtual register is normally assigned the number
13481024. This may change, so, in order to know which is the first virtual
1349register, you should access
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001350<tt>TargetRegisterInfo::FirstVirtualRegister</tt>. Any register whose
Bill Wendling00c5aec2006-09-01 21:46:00 +00001351number is greater than or equal to
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001352<tt>TargetRegisterInfo::FirstVirtualRegister</tt> is considered a virtual
Bill Wendling00c5aec2006-09-01 21:46:00 +00001353register. Whereas physical registers are statically defined in a
1354<tt>TargetRegisterInfo.td</tt> file and cannot be created by the
1355application developer, that is not the case with virtual registers.
1356In order to create new virtual registers, use the method
Chris Lattner21ec2b42007-12-31 04:16:08 +00001357<tt>MachineRegisterInfo::createVirtualRegister()</tt>. This method will return a
Bill Wendling00c5aec2006-09-01 21:46:00 +00001358virtual register with the highest code.
1359</p>
1360
1361<p>Before register allocation, the operands of an instruction are
1362mostly virtual registers, although physical registers may also be
1363used. In order to check if a given machine operand is a register, use
1364the boolean function <tt>MachineOperand::isRegister()</tt>. To obtain
1365the integer code of a register, use
1366<tt>MachineOperand::getReg()</tt>. An instruction may define or use a
1367register. For instance, <tt>ADD reg:1026 := reg:1025 reg:1024</tt>
1368defines the registers 1024, and uses registers 1025 and 1026. Given a
1369register operand, the method <tt>MachineOperand::isUse()</tt> informs
1370if that register is being used by the instruction. The method
1371<tt>MachineOperand::isDef()</tt> informs if that registers is being
1372defined.</p>
1373
Gabor Greifa54634a2007-07-06 22:07:22 +00001374<p>We will call physical registers present in the LLVM bitcode before
Bill Wendling00c5aec2006-09-01 21:46:00 +00001375register allocation <i>pre-colored registers</i>. Pre-colored
1376registers are used in many different situations, for instance, to pass
1377parameters of functions calls, and to store results of particular
1378instructions. There are two types of pre-colored registers: the ones
1379<i>implicitly</i> defined, and those <i>explicitly</i>
1380defined. Explicitly defined registers are normal operands, and can be
1381accessed with <tt>MachineInstr::getOperand(int)::getReg()</tt>. In
1382order to check which registers are implicitly defined by an
1383instruction, use the
1384<tt>TargetInstrInfo::get(opcode)::ImplicitDefs</tt>, where
1385<tt>opcode</tt> is the opcode of the target instruction. One important
1386difference between explicit and implicit physical registers is that
1387the latter are defined statically for each instruction, whereas the
1388former may vary depending on the program being compiled. For example,
1389an instruction that represents a function call will always implicitly
1390define or use the same set of physical registers. To read the
1391registers implicitly used by an instruction, use
1392<tt>TargetInstrInfo::get(opcode)::ImplicitUses</tt>. Pre-colored
1393registers impose constraints on any register allocation algorithm. The
1394register allocator must make sure that none of them is been
1395overwritten by the values of virtual registers while still alive.</p>
1396
1397</div>
1398
1399<!-- _______________________________________________________________________ -->
1400
1401<div class="doc_subsubsection">
1402 <a name="regAlloc_howTo">Mapping virtual registers to physical registers</a>
1403</div>
1404
1405<div class="doc_text">
1406
1407<p>There are two ways to map virtual registers to physical registers (or to
1408memory slots). The first way, that we will call <i>direct mapping</i>,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001409is based on the use of methods of the classes <tt>TargetRegisterInfo</tt>,
Bill Wendling00c5aec2006-09-01 21:46:00 +00001410and <tt>MachineOperand</tt>. The second way, that we will call
1411<i>indirect mapping</i>, relies on the <tt>VirtRegMap</tt> class in
1412order to insert loads and stores sending and getting values to and from
1413memory.</p>
1414
1415<p>The direct mapping provides more flexibility to the developer of
1416the register allocator; however, it is more error prone, and demands
1417more implementation work. Basically, the programmer will have to
1418specify where load and store instructions should be inserted in the
1419target function being compiled in order to get and store values in
1420memory. To assign a physical register to a virtual register present in
1421a given operand, use <tt>MachineOperand::setReg(p_reg)</tt>. To insert
1422a store instruction, use
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001423<tt>TargetRegisterInfo::storeRegToStackSlot(...)</tt>, and to insert a load
1424instruction, use <tt>TargetRegisterInfo::loadRegFromStackSlot</tt>.</p>
Bill Wendling00c5aec2006-09-01 21:46:00 +00001425
1426<p>The indirect mapping shields the application developer from the
1427complexities of inserting load and store instructions. In order to map
1428a virtual register to a physical one, use
1429<tt>VirtRegMap::assignVirt2Phys(vreg, preg)</tt>. In order to map a
1430certain virtual register to memory, use
1431<tt>VirtRegMap::assignVirt2StackSlot(vreg)</tt>. This method will
1432return the stack slot where <tt>vreg</tt>'s value will be located. If
1433it is necessary to map another virtual register to the same stack
1434slot, use <tt>VirtRegMap::assignVirt2StackSlot(vreg,
1435stack_location)</tt>. One important point to consider when using the
1436indirect mapping, is that even if a virtual register is mapped to
1437memory, it still needs to be mapped to a physical register. This
1438physical register is the location where the virtual register is
1439supposed to be found before being stored or after being reloaded.</p>
1440
1441<p>If the indirect strategy is used, after all the virtual registers
1442have been mapped to physical registers or stack slots, it is necessary
1443to use a spiller object to place load and store instructions in the
1444code. Every virtual that has been mapped to a stack slot will be
1445stored to memory after been defined and will be loaded before being
1446used. The implementation of the spiller tries to recycle load/store
1447instructions, avoiding unnecessary instructions. For an example of how
1448to invoke the spiller, see
1449<tt>RegAllocLinearScan::runOnMachineFunction</tt> in
1450<tt>lib/CodeGen/RegAllocLinearScan.cpp</tt>.</p>
1451
1452</div>
1453
1454<!-- _______________________________________________________________________ -->
1455<div class="doc_subsubsection">
1456 <a name="regAlloc_twoAddr">Handling two address instructions</a>
1457</div>
1458
1459<div class="doc_text">
1460
1461<p>With very rare exceptions (e.g., function calls), the LLVM machine
1462code instructions are three address instructions. That is, each
1463instruction is expected to define at most one register, and to use at
1464most two registers. However, some architectures use two address
1465instructions. In this case, the defined register is also one of the
1466used register. For instance, an instruction such as <tt>ADD %EAX,
1467%EBX</tt>, in X86 is actually equivalent to <tt>%EAX = %EAX +
1468%EBX</tt>.</p>
1469
1470<p>In order to produce correct code, LLVM must convert three address
1471instructions that represent two address instructions into true two
1472address instructions. LLVM provides the pass
1473<tt>TwoAddressInstructionPass</tt> for this specific purpose. It must
1474be run before register allocation takes place. After its execution,
1475the resulting code may no longer be in SSA form. This happens, for
1476instance, in situations where an instruction such as <tt>%a = ADD %b
1477%c</tt> is converted to two instructions such as:</p>
1478
1479<div class="doc_code">
1480<pre>
1481%a = MOVE %b
Dan Gohman01cd2d92008-06-13 17:55:57 +00001482%a = ADD %a %c
Bill Wendling00c5aec2006-09-01 21:46:00 +00001483</pre>
1484</div>
1485
1486<p>Notice that, internally, the second instruction is represented as
Dan Gohman01cd2d92008-06-13 17:55:57 +00001487<tt>ADD %a[def/use] %c</tt>. I.e., the register operand <tt>%a</tt> is
Bill Wendling00c5aec2006-09-01 21:46:00 +00001488both used and defined by the instruction.</p>
1489
1490</div>
1491
1492<!-- _______________________________________________________________________ -->
1493<div class="doc_subsubsection">
1494 <a name="regAlloc_ssaDecon">The SSA deconstruction phase</a>
1495</div>
1496
1497<div class="doc_text">
1498
1499<p>An important transformation that happens during register allocation is called
1500the <i>SSA Deconstruction Phase</i>. The SSA form simplifies many
1501analyses that are performed on the control flow graph of
1502programs. However, traditional instruction sets do not implement
1503PHI instructions. Thus, in order to generate executable code, compilers
1504must replace PHI instructions with other instructions that preserve their
1505semantics.</p>
1506
1507<p>There are many ways in which PHI instructions can safely be removed
1508from the target code. The most traditional PHI deconstruction
1509algorithm replaces PHI instructions with copy instructions. That is
1510the strategy adopted by LLVM. The SSA deconstruction algorithm is
1511implemented in n<tt>lib/CodeGen/>PHIElimination.cpp</tt>. In order to
1512invoke this pass, the identifier <tt>PHIEliminationID</tt> must be
1513marked as required in the code of the register allocator.</p>
1514
1515</div>
1516
1517<!-- _______________________________________________________________________ -->
1518<div class="doc_subsubsection">
1519 <a name="regAlloc_fold">Instruction folding</a>
1520</div>
1521
1522<div class="doc_text">
1523
1524<p><i>Instruction folding</i> is an optimization performed during
1525register allocation that removes unnecessary copy instructions. For
1526instance, a sequence of instructions such as:</p>
1527
1528<div class="doc_code">
1529<pre>
1530%EBX = LOAD %mem_address
1531%EAX = COPY %EBX
1532</pre>
1533</div>
1534
1535<p>can be safely substituted by the single instruction:
1536
1537<div class="doc_code">
1538<pre>
1539%EAX = LOAD %mem_address
1540</pre>
1541</div>
1542
1543<p>Instructions can be folded with the
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001544<tt>TargetRegisterInfo::foldMemoryOperand(...)</tt> method. Care must be
Bill Wendling00c5aec2006-09-01 21:46:00 +00001545taken when folding instructions; a folded instruction can be quite
1546different from the original instruction. See
1547<tt>LiveIntervals::addIntervalsForSpills</tt> in
1548<tt>lib/CodeGen/LiveIntervalAnalysis.cpp</tt> for an example of its use.</p>
1549
1550</div>
1551
1552<!-- _______________________________________________________________________ -->
1553
1554<div class="doc_subsubsection">
1555 <a name="regAlloc_builtIn">Built in register allocators</a>
1556</div>
1557
1558<div class="doc_text">
1559
1560<p>The LLVM infrastructure provides the application developer with
1561three different register allocators:</p>
1562
1563<ul>
1564 <li><i>Simple</i> - This is a very simple implementation that does
1565 not keep values in registers across instructions. This register
1566 allocator immediately spills every value right after it is
1567 computed, and reloads all used operands from memory to temporary
1568 registers before each instruction.</li>
1569 <li><i>Local</i> - This register allocator is an improvement on the
1570 <i>Simple</i> implementation. It allocates registers on a basic
1571 block level, attempting to keep values in registers and reusing
1572 registers as appropriate.</li>
1573 <li><i>Linear Scan</i> - <i>The default allocator</i>. This is the
1574 well-know linear scan register allocator. Whereas the
1575 <i>Simple</i> and <i>Local</i> algorithms use a direct mapping
1576 implementation technique, the <i>Linear Scan</i> implementation
1577 uses a spiller in order to place load and stores.</li>
1578</ul>
1579
1580<p>The type of register allocator used in <tt>llc</tt> can be chosen with the
1581command line option <tt>-regalloc=...</tt>:</p>
1582
1583<div class="doc_code">
1584<pre>
1585$ llc -f -regalloc=simple file.bc -o sp.s;
1586$ llc -f -regalloc=local file.bc -o lc.s;
1587$ llc -f -regalloc=linearscan file.bc -o ln.s;
1588</pre>
1589</div>
1590
1591</div>
1592
Reid Spencer4da97842005-04-24 20:56:18 +00001593<!-- ======================================================================= -->
1594<div class="doc_subsection">
1595 <a name="proepicode">Prolog/Epilog Code Insertion</a>
1596</div>
1597<div class="doc_text"><p>To Be Written</p></div>
1598<!-- ======================================================================= -->
1599<div class="doc_subsection">
1600 <a name="latemco">Late Machine Code Optimizations</a>
1601</div>
1602<div class="doc_text"><p>To Be Written</p></div>
1603<!-- ======================================================================= -->
1604<div class="doc_subsection">
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001605 <a name="codeemit">Code Emission</a>
Reid Spencer4da97842005-04-24 20:56:18 +00001606</div>
Bill Wendling5c385de2006-08-28 02:26:32 +00001607<div class="doc_text"><p>To Be Written</p></div>
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001608<!-- _______________________________________________________________________ -->
1609<div class="doc_subsubsection">
1610 <a name="codeemit_asm">Generating Assembly Code</a>
1611</div>
Bill Wendling5c385de2006-08-28 02:26:32 +00001612<div class="doc_text"><p>To Be Written</p></div>
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001613<!-- _______________________________________________________________________ -->
1614<div class="doc_subsubsection">
1615 <a name="codeemit_bin">Generating Binary Machine Code</a>
1616</div>
1617
1618<div class="doc_text">
Bill Wendling5c385de2006-08-28 02:26:32 +00001619 <p>For the JIT or <tt>.o</tt> file writer</p>
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001620</div>
1621
1622
Chris Lattner54903b62005-01-28 17:22:53 +00001623<!-- *********************************************************************** -->
1624<div class="doc_section">
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001625 <a name="targetimpls">Target-specific Implementation Notes</a>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001626</div>
1627<!-- *********************************************************************** -->
1628
1629<div class="doc_text">
1630
Reid Spencer4da97842005-04-24 20:56:18 +00001631<p>This section of the document explains features or design decisions that
Chris Lattnerb4e56642004-06-04 00:16:02 +00001632are specific to the code generator for a particular target.</p>
1633
1634</div>
1635
Arnold Schwaighofer2c6b8882008-05-14 09:17:12 +00001636<!-- ======================================================================= -->
1637<div class="doc_subsection">
1638 <a name="tailcallopt">Tail call optimization</a>
1639</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001640
Arnold Schwaighofer2c6b8882008-05-14 09:17:12 +00001641<div class="doc_text">
1642 <p>Tail call optimization, callee reusing the stack of the caller, is currently supported on x86/x86-64 and PowerPC. It is performed if:
1643 <ul>
1644 <li>Caller and callee have the calling convention <tt>fastcc</tt>.</li>
1645 <li>The call is a tail call - in tail position (ret immediately follows call and ret uses value of call or is void).</li>
1646 <li>Option <tt>-tailcallopt</tt> is enabled.</li>
1647 <li>Platform specific constraints are met.</li>
1648 </ul>
1649 </p>
1650
1651 <p>x86/x86-64 constraints:
1652 <ul>
1653 <li>No variable argument lists are used.</li>
1654 <li>On x86-64 when generating GOT/PIC code only module-local calls (visibility = hidden or protected) are supported.</li>
1655 </ul>
1656 </p>
1657 <p>PowerPC constraints:
1658 <ul>
1659 <li>No variable argument lists are used.</li>
1660 <li>No byval parameters are used.</li>
1661 <li>On ppc32/64 GOT/PIC only module-local calls (visibility = hidden or protected) are supported.</li>
1662 </ul>
1663 </p>
1664 <p>Example:</p>
1665 <p>Call as <tt>llc -tailcallopt test.ll</tt>.
1666 <div class="doc_code">
1667 <pre>
1668declare fastcc i32 @tailcallee(i32 inreg %a1, i32 inreg %a2, i32 %a3, i32 %a4)
1669
1670define fastcc i32 @tailcaller(i32 %in1, i32 %in2) {
1671 %l1 = add i32 %in1, %in2
1672 %tmp = tail call fastcc i32 @tailcallee(i32 %in1 inreg, i32 %in2 inreg, i32 %in1, i32 %l1)
1673 ret i32 %tmp
1674}</pre>
1675 </div>
1676 </p>
1677 <p>Implications of <tt>-tailcallopt</tt>:</p>
1678 <p>To support tail call optimization in situations where the callee has more arguments than the caller a 'callee pops arguments' convention is used. This currently causes each <tt>fastcc</tt> call that is not tail call optimized (because one or more of above constraints are not met) to be followed by a readjustment of the stack. So performance might be worse in such cases.</p>
1679 <p>On x86 and x86-64 one register is reserved for indirect tail calls (e.g via a function pointer). So there is one less register for integer argument passing. For x86 this means 2 registers (if <tt>inreg</tt> parameter attribute is used) and for x86-64 this means 5 register are used.</p>
1680</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001681<!-- ======================================================================= -->
1682<div class="doc_subsection">
1683 <a name="x86">The X86 backend</a>
1684</div>
1685
1686<div class="doc_text">
1687
Bill Wendling5c385de2006-08-28 02:26:32 +00001688<p>The X86 code generator lives in the <tt>lib/Target/X86</tt> directory. This
Dan Gohmand4551fd2007-12-13 20:43:47 +00001689code generator is capable of targeting a variety of x86-32 and x86-64
1690processors, and includes support for ISA extensions such as MMX and SSE.
1691</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001692
1693</div>
1694
1695<!-- _______________________________________________________________________ -->
1696<div class="doc_subsubsection">
Chris Lattnerde69bf92005-07-12 00:20:49 +00001697 <a name="x86_tt">X86 Target Triples Supported</a>
1698</div>
1699
1700<div class="doc_text">
Bill Wendling5c385de2006-08-28 02:26:32 +00001701
1702<p>The following are the known target triples that are supported by the X86
1703backend. This is not an exhaustive list, and it would be useful to add those
1704that people test.</p>
Chris Lattnerde69bf92005-07-12 00:20:49 +00001705
1706<ul>
1707<li><b>i686-pc-linux-gnu</b> - Linux</li>
1708<li><b>i386-unknown-freebsd5.3</b> - FreeBSD 5.3</li>
1709<li><b>i686-pc-cygwin</b> - Cygwin on Win32</li>
1710<li><b>i686-pc-mingw32</b> - MingW on Win32</li>
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001711<li><b>i386-pc-mingw32msvc</b> - MingW crosscompiler on Linux</li>
Chris Lattnerd6f1a332005-10-16 18:31:08 +00001712<li><b>i686-apple-darwin*</b> - Apple Darwin on X86</li>
Chris Lattnerde69bf92005-07-12 00:20:49 +00001713</ul>
1714
1715</div>
1716
1717<!-- _______________________________________________________________________ -->
1718<div class="doc_subsubsection">
Anton Korobeynikov6f7072c2006-09-17 20:25:45 +00001719 <a name="x86_cc">X86 Calling Conventions supported</a>
1720</div>
1721
1722
1723<div class="doc_text">
1724
1725<p>The folowing target-specific calling conventions are known to backend:</p>
1726
1727<ul>
1728<li><b>x86_StdCall</b> - stdcall calling convention seen on Microsoft Windows
1729platform (CC ID = 64).</li>
1730<li><b>x86_FastCall</b> - fastcall calling convention seen on Microsoft Windows
1731platform (CC ID = 65).</li>
1732</ul>
1733
1734</div>
1735
1736<!-- _______________________________________________________________________ -->
1737<div class="doc_subsubsection">
Chris Lattnerb4e56642004-06-04 00:16:02 +00001738 <a name="x86_memory">Representing X86 addressing modes in MachineInstrs</a>
1739</div>
1740
1741<div class="doc_text">
1742
Misha Brukman37036852005-02-17 22:22:24 +00001743<p>The x86 has a very flexible way of accessing memory. It is capable of
Chris Lattnerb4e56642004-06-04 00:16:02 +00001744forming memory addresses of the following expression directly in integer
1745instructions (which use ModR/M addressing):</p>
1746
Bill Wendling5c385de2006-08-28 02:26:32 +00001747<div class="doc_code">
Chris Lattnerb4e56642004-06-04 00:16:02 +00001748<pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001749Base + [1,2,4,8] * IndexReg + Disp32
Chris Lattnerb4e56642004-06-04 00:16:02 +00001750</pre>
Bill Wendling5c385de2006-08-28 02:26:32 +00001751</div>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001752
Misha Brukman37036852005-02-17 22:22:24 +00001753<p>In order to represent this, LLVM tracks no less than 4 operands for each
Bill Wendling5c385de2006-08-28 02:26:32 +00001754memory operand of this form. This means that the "load" form of '<tt>mov</tt>'
1755has the following <tt>MachineOperand</tt>s in this order:</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001756
1757<pre>
1758Index: 0 | 1 2 3 4
1759Meaning: DestReg, | BaseReg, Scale, IndexReg, Displacement
1760OperandTy: VirtReg, | VirtReg, UnsImm, VirtReg, SignExtImm
1761</pre>
1762
Reid Spencer4da97842005-04-24 20:56:18 +00001763<p>Stores, and all other instructions, treat the four memory operands in the
Bill Wendling5c385de2006-08-28 02:26:32 +00001764same way and in the same order.</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001765
1766</div>
1767
1768<!-- _______________________________________________________________________ -->
1769<div class="doc_subsubsection">
1770 <a name="x86_names">Instruction naming</a>
1771</div>
1772
1773<div class="doc_text">
1774
Bill Wendling5c385de2006-08-28 02:26:32 +00001775<p>An instruction name consists of the base name, a default operand size, and a
Reid Spencer4da97842005-04-24 20:56:18 +00001776a character per operand with an optional special size. For example:</p>
Chris Lattnerb4e56642004-06-04 00:16:02 +00001777
1778<p>
1779<tt>ADD8rr</tt> -&gt; add, 8-bit register, 8-bit register<br>
1780<tt>IMUL16rmi</tt> -&gt; imul, 16-bit register, 16-bit memory, 16-bit immediate<br>
1781<tt>IMUL16rmi8</tt> -&gt; imul, 16-bit register, 16-bit memory, 8-bit immediate<br>
1782<tt>MOVSX32rm16</tt> -&gt; movsx, 32-bit register, 16-bit memory
1783</p>
1784
1785</div>
Chris Lattner565d7d52004-06-01 06:48:00 +00001786
Jim Laskeyef583342006-12-14 17:19:50 +00001787<!-- ======================================================================= -->
1788<div class="doc_subsection">
1789 <a name="ppc">The PowerPC backend</a>
1790</div>
1791
1792<div class="doc_text">
1793<p>The PowerPC code generator lives in the lib/Target/PowerPC directory. The
1794code generation is retargetable to several variations or <i>subtargets</i> of
1795the PowerPC ISA; including ppc32, ppc64 and altivec.
1796</p>
1797</div>
1798
1799<!-- _______________________________________________________________________ -->
1800<div class="doc_subsubsection">
1801 <a name="ppc_abi">LLVM PowerPC ABI</a>
1802</div>
1803
1804<div class="doc_text">
1805<p>LLVM follows the AIX PowerPC ABI, with two deviations. LLVM uses a PC
1806relative (PIC) or static addressing for accessing global values, so no TOC (r2)
1807is used. Second, r31 is used as a frame pointer to allow dynamic growth of a
1808stack frame. LLVM takes advantage of having no TOC to provide space to save
1809the frame pointer in the PowerPC linkage area of the caller frame. Other
Jim Laskey57825842006-12-15 10:40:48 +00001810details of PowerPC ABI can be found at <a href=
1811"http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/32bitPowerPC.html"
1812>PowerPC ABI.</a> Note: This link describes the 32 bit ABI. The
181364 bit ABI is similar except space for GPRs are 8 bytes wide (not 4) and r13 is
1814reserved for system use.</p>
Jim Laskeyef583342006-12-14 17:19:50 +00001815</div>
1816
1817<!-- _______________________________________________________________________ -->
1818<div class="doc_subsubsection">
1819 <a name="ppc_frame">Frame Layout</a>
1820</div>
1821
1822<div class="doc_text">
1823<p>The size of a PowerPC frame is usually fixed for the duration of a
Jim Laskey57825842006-12-15 10:40:48 +00001824function&rsquo;s invocation. Since the frame is fixed size, all references into
Jim Laskeyef583342006-12-14 17:19:50 +00001825the frame can be accessed via fixed offsets from the stack pointer. The
1826exception to this is when dynamic alloca or variable sized arrays are present,
1827then a base pointer (r31) is used as a proxy for the stack pointer and stack
1828pointer is free to grow or shrink. A base pointer is also used if llvm-gcc is
1829not passed the -fomit-frame-pointer flag. The stack pointer is always aligned to
183016 bytes, so that space allocated for altivec vectors will be properly
1831aligned.</p>
1832<p>An invocation frame is layed out as follows (low memory at top);</p>
1833</div>
1834
1835<div class="doc_text">
1836<table class="layout">
1837 <tr>
1838 <td>Linkage<br><br></td>
1839 </tr>
1840 <tr>
1841 <td>Parameter area<br><br></td>
1842 </tr>
1843 <tr>
1844 <td>Dynamic area<br><br></td>
1845 </tr>
1846 <tr>
1847 <td>Locals area<br><br></td>
1848 </tr>
1849 <tr>
1850 <td>Saved registers area<br><br></td>
1851 </tr>
1852 <tr style="border-style: none hidden none hidden;">
1853 <td><br></td>
1854 </tr>
1855 <tr>
1856 <td>Previous Frame<br><br></td>
1857 </tr>
1858</table>
1859</div>
1860
1861<div class="doc_text">
1862<p>The <i>linkage</i> area is used by a callee to save special registers prior
1863to allocating its own frame. Only three entries are relevant to LLVM. The
1864first entry is the previous stack pointer (sp), aka link. This allows probing
1865tools like gdb or exception handlers to quickly scan the frames in the stack. A
1866function epilog can also use the link to pop the frame from the stack. The
1867third entry in the linkage area is used to save the return address from the lr
1868register. Finally, as mentioned above, the last entry is used to save the
1869previous frame pointer (r31.) The entries in the linkage area are the size of a
1870GPR, thus the linkage area is 24 bytes long in 32 bit mode and 48 bytes in 64
1871bit mode.</p>
1872</div>
1873
1874<div class="doc_text">
1875<p>32 bit linkage area</p>
1876<table class="layout">
1877 <tr>
1878 <td>0</td>
1879 <td>Saved SP (r1)</td>
1880 </tr>
1881 <tr>
1882 <td>4</td>
1883 <td>Saved CR</td>
1884 </tr>
1885 <tr>
1886 <td>8</td>
1887 <td>Saved LR</td>
1888 </tr>
1889 <tr>
1890 <td>12</td>
1891 <td>Reserved</td>
1892 </tr>
1893 <tr>
1894 <td>16</td>
1895 <td>Reserved</td>
1896 </tr>
1897 <tr>
1898 <td>20</td>
1899 <td>Saved FP (r31)</td>
1900 </tr>
1901</table>
1902</div>
1903
1904<div class="doc_text">
1905<p>64 bit linkage area</p>
1906<table class="layout">
1907 <tr>
1908 <td>0</td>
1909 <td>Saved SP (r1)</td>
1910 </tr>
1911 <tr>
1912 <td>8</td>
1913 <td>Saved CR</td>
1914 </tr>
1915 <tr>
1916 <td>16</td>
1917 <td>Saved LR</td>
1918 </tr>
1919 <tr>
1920 <td>24</td>
1921 <td>Reserved</td>
1922 </tr>
1923 <tr>
1924 <td>32</td>
1925 <td>Reserved</td>
1926 </tr>
1927 <tr>
1928 <td>40</td>
1929 <td>Saved FP (r31)</td>
1930 </tr>
1931</table>
1932</div>
1933
1934<div class="doc_text">
1935<p>The <i>parameter area</i> is used to store arguments being passed to a callee
1936function. Following the PowerPC ABI, the first few arguments are actually
1937passed in registers, with the space in the parameter area unused. However, if
1938there are not enough registers or the callee is a thunk or vararg function,
1939these register arguments can be spilled into the parameter area. Thus, the
1940parameter area must be large enough to store all the parameters for the largest
1941call sequence made by the caller. The size must also be mimimally large enough
1942to spill registers r3-r10. This allows callees blind to the call signature,
1943such as thunks and vararg functions, enough space to cache the argument
1944registers. Therefore, the parameter area is minimally 32 bytes (64 bytes in 64
1945bit mode.) Also note that since the parameter area is a fixed offset from the
1946top of the frame, that a callee can access its spilt arguments using fixed
1947offsets from the stack pointer (or base pointer.)</p>
1948</div>
1949
1950<div class="doc_text">
1951<p>Combining the information about the linkage, parameter areas and alignment. A
1952stack frame is minimally 64 bytes in 32 bit mode and 128 bytes in 64 bit
1953mode.</p>
1954</div>
1955
1956<div class="doc_text">
1957<p>The <i>dynamic area</i> starts out as size zero. If a function uses dynamic
1958alloca then space is added to the stack, the linkage and parameter areas are
1959shifted to top of stack, and the new space is available immediately below the
1960linkage and parameter areas. The cost of shifting the linkage and parameter
1961areas is minor since only the link value needs to be copied. The link value can
1962be easily fetched by adding the original frame size to the base pointer. Note
1963that allocations in the dynamic space need to observe 16 byte aligment.</p>
1964</div>
1965
1966<div class="doc_text">
1967<p>The <i>locals area</i> is where the llvm compiler reserves space for local
1968variables.</p>
1969</div>
1970
1971<div class="doc_text">
1972<p>The <i>saved registers area</i> is where the llvm compiler spills callee saved
1973registers on entry to the callee.</p>
1974</div>
1975
1976<!-- _______________________________________________________________________ -->
1977<div class="doc_subsubsection">
1978 <a name="ppc_prolog">Prolog/Epilog</a>
1979</div>
1980
1981<div class="doc_text">
1982<p>The llvm prolog and epilog are the same as described in the PowerPC ABI, with
1983the following exceptions. Callee saved registers are spilled after the frame is
1984created. This allows the llvm epilog/prolog support to be common with other
1985targets. The base pointer callee saved register r31 is saved in the TOC slot of
1986linkage area. This simplifies allocation of space for the base pointer and
1987makes it convenient to locate programatically and during debugging.</p>
1988</div>
1989
1990<!-- _______________________________________________________________________ -->
1991<div class="doc_subsubsection">
1992 <a name="ppc_dynamic">Dynamic Allocation</a>
1993</div>
1994
1995<div class="doc_text">
1996<p></p>
1997</div>
1998
Jim Laskey57825842006-12-15 10:40:48 +00001999<div class="doc_text">
2000<p><i>TODO - More to come.</i></p>
2001</div>
Jim Laskeyef583342006-12-14 17:19:50 +00002002
2003
Chris Lattner565d7d52004-06-01 06:48:00 +00002004<!-- *********************************************************************** -->
2005<hr>
2006<address>
2007 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2008 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
2009 <a href="http://validator.w3.org/check/referer"><img
2010 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
2011
2012 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencerca058542006-03-14 05:39:39 +00002013 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Chris Lattner565d7d52004-06-01 06:48:00 +00002014 Last modified: $Date$
2015</address>
2016
2017</body>
2018</html>