blob: b3bc4814fdc0bf4631800cc60c708e6d0eea8d54 [file] [log] [blame]
Devang Patel93449f12006-08-14 18:03:40 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Reid Spencerb5fc9f52006-08-14 19:19:55 +00005 <title>LLVM Link Time Optimization: Design and Implementation</title>
Devang Patel93449f12006-08-14 18:03:40 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8
NAKAMURA Takumi05d02652011-04-18 23:59:50 +00009<h1>
Reid Spencerb5fc9f52006-08-14 19:19:55 +000010 LLVM Link Time Optimization: Design and Implementation
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000011</h1>
Devang Patel93449f12006-08-14 18:03:40 +000012
13<ul>
14 <li><a href="#desc">Description</a></li>
15 <li><a href="#design">Design Philosophy</a>
16 <ul>
17 <li><a href="#example1">Example of link time optimization</a></li>
18 <li><a href="#alternative_approaches">Alternative Approaches</a></li>
19 </ul></li>
Devang Patel2c1292f2006-08-14 18:39:35 +000020 <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a>
Devang Patel93449f12006-08-14 18:03:40 +000021 <ul>
Duncan Sands18d52f22010-09-29 20:09:55 +000022 <li><a href="#phase1">Phase 1 : Read LLVM Bitcode Files</a></li>
Devang Patel93449f12006-08-14 18:03:40 +000023 <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
Nick Kledzik84e5f772008-02-29 19:34:52 +000024 <li><a href="#phase3">Phase 3 : Optimize Bitcode Files</a></li>
Devang Patel93449f12006-08-14 18:03:40 +000025 <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
26 </ul></li>
Nick Kledzik84e5f772008-02-29 19:34:52 +000027 <li><a href="#lto">libLTO</a>
Devang Patel93449f12006-08-14 18:03:40 +000028 <ul>
Nick Kledzik84e5f772008-02-29 19:34:52 +000029 <li><a href="#lto_module_t">lto_module_t</a></li>
30 <li><a href="#lto_code_gen_t">lto_code_gen_t</a></li>
31 </ul>
Devang Patel93449f12006-08-14 18:03:40 +000032</ul>
33
34<div class="doc_author">
Nick Kledzik84e5f772008-02-29 19:34:52 +000035<p>Written by Devang Patel and Nick Kledzik</p>
Devang Patel93449f12006-08-14 18:03:40 +000036</div>
37
38<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000039<h2>
Devang Patel93449f12006-08-14 18:03:40 +000040<a name="desc">Description</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000041</h2>
Devang Patel93449f12006-08-14 18:03:40 +000042<!-- *********************************************************************** -->
43
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000044<div>
Devang Patel93449f12006-08-14 18:03:40 +000045<p>
Reid Spencerb5fc9f52006-08-14 19:19:55 +000046LLVM features powerful intermodular optimizations which can be used at link
Nick Kledzik84e5f772008-02-29 19:34:52 +000047time. Link Time Optimization (LTO) is another name for intermodular optimization
Reid Spencerb5fc9f52006-08-14 19:19:55 +000048when performed during the link stage. This document describes the interface
Nick Kledzik84e5f772008-02-29 19:34:52 +000049and design between the LTO optimizer and the linker.</p>
Devang Patel93449f12006-08-14 18:03:40 +000050</div>
51
52<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000053<h2>
Devang Patel93449f12006-08-14 18:03:40 +000054<a name="design">Design Philosophy</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000055</h2>
Devang Patel93449f12006-08-14 18:03:40 +000056<!-- *********************************************************************** -->
57
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000058<div>
Devang Patel93449f12006-08-14 18:03:40 +000059<p>
Reid Spencerb5fc9f52006-08-14 19:19:55 +000060The LLVM Link Time Optimizer provides complete transparency, while doing
61intermodular optimization, in the compiler tool chain. Its main goal is to let
62the developer take advantage of intermodular optimizations without making any
63significant changes to the developer's makefiles or build system. This is
64achieved through tight integration with the linker. In this model, the linker
Gabor Greif04367bf2007-07-06 22:07:22 +000065treates LLVM bitcode files like native object files and allows mixing and
Nick Kledzik84e5f772008-02-29 19:34:52 +000066matching among them. The linker uses <a href="#lto">libLTO</a>, a shared
67object, to handle LLVM bitcode files. This tight integration between
Reid Spencerb5fc9f52006-08-14 19:19:55 +000068the linker and LLVM optimizer helps to do optimizations that are not possible
69in other models. The linker input allows the optimizer to avoid relying on
70conservative escape analysis.
Devang Patel93449f12006-08-14 18:03:40 +000071</p>
72
73<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000074<h3>
Devang Patel93449f12006-08-14 18:03:40 +000075 <a name="example1">Example of link time optimization</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000076</h3>
Devang Patel93449f12006-08-14 18:03:40 +000077
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000078<div>
Duncan Sands6db928a2007-04-07 17:43:25 +000079 <p>The following example illustrates the advantages of LTO's integrated
80 approach and clean interface. This example requires a system linker which
81 supports LTO through the interface described in this document. Here,
Tanya Lattner5efa7e92007-08-24 23:23:23 +000082 llvm-gcc transparently invokes system linker. </p>
Reid Spencerb5fc9f52006-08-14 19:19:55 +000083 <ul>
Gabor Greif04367bf2007-07-06 22:07:22 +000084 <li> Input source file <tt>a.c</tt> is compiled into LLVM bitcode form.
Reid Spencerb5fc9f52006-08-14 19:19:55 +000085 <li> Input source file <tt>main.c</tt> is compiled into native object code.
86 </ul>
Misha Brukman64722e52008-12-16 03:07:49 +000087<pre class="doc_code">
Devang Patel2c1292f2006-08-14 18:39:35 +000088--- a.h ---
Reid Spencerb5fc9f52006-08-14 19:19:55 +000089extern int foo1(void);
90extern void foo2(void);
91extern void foo4(void);
92--- a.c ---
93#include "a.h"
94
95static signed int i = 0;
96
97void foo2(void) {
98 i = -1;
99}
100
101static int foo3() {
102foo4();
103return 10;
104}
105
106int foo1(void) {
107int data = 0;
108
109if (i &lt; 0) { data = foo3(); }
110
111data = data + 42;
112return data;
113}
114
115--- main.c ---
116#include &lt;stdio.h&gt;
117#include "a.h"
118
119void foo4(void) {
120 printf ("Hi\n");
121}
122
123int main() {
124 return foo1();
125}
126
127--- command lines ---
Tanya Lattner5efa7e92007-08-24 23:23:23 +0000128$ llvm-gcc --emit-llvm -c a.c -o a.o # &lt;-- a.o is LLVM bitcode file
129$ llvm-gcc -c main.c -o main.o # &lt;-- main.o is native object file
130$ llvm-gcc a.o main.o -o main # &lt;-- standard link command without any modifications
Misha Brukman64722e52008-12-16 03:07:49 +0000131</pre>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000132 <p>In this example, the linker recognizes that <tt>foo2()</tt> is an
Nick Kledzik84e5f772008-02-29 19:34:52 +0000133 externally visible symbol defined in LLVM bitcode file. The linker completes
134 its usual symbol resolution
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000135 pass and finds that <tt>foo2()</tt> is not used anywhere. This information
136 is used by the LLVM optimizer and it removes <tt>foo2()</tt>. As soon as
137 <tt>foo2()</tt> is removed, the optimizer recognizes that condition
138 <tt>i &lt; 0</tt> is always false, which means <tt>foo3()</tt> is never
139 used. Hence, the optimizer removes <tt>foo3()</tt>, also. And this in turn,
140 enables linker to remove <tt>foo4()</tt>. This example illustrates the
141 advantage of tight integration with the linker. Here, the optimizer can not
142 remove <tt>foo3()</tt> without the linker's input.
143 </p>
Devang Patel93449f12006-08-14 18:03:40 +0000144</div>
145
146<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000147<h3>
Devang Patel93449f12006-08-14 18:03:40 +0000148 <a name="alternative_approaches">Alternative Approaches</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000149</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000150
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000151<div>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000152 <dl>
153 <dt><b>Compiler driver invokes link time optimizer separately.</b></dt>
154 <dd>In this model the link time optimizer is not able to take advantage of
155 information collected during the linker's normal symbol resolution phase.
156 In the above example, the optimizer can not remove <tt>foo2()</tt> without
157 the linker's input because it is externally visible. This in turn prohibits
158 the optimizer from removing <tt>foo3()</tt>.</dd>
159 <dt><b>Use separate tool to collect symbol information from all object
160 files.</b></dt>
161 <dd>In this model, a new, separate, tool or library replicates the linker's
162 capability to collect information for link time optimization. Not only is
163 this code duplication difficult to justify, but it also has several other
164 disadvantages. For example, the linking semantics and the features
165 provided by the linker on various platform are not unique. This means,
166 this new tool needs to support all such features and platforms in one
167 super tool or a separate tool per platform is required. This increases
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000168 maintenance cost for link time optimizer significantly, which is not
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000169 necessary. This approach also requires staying synchronized with linker
170 developements on various platforms, which is not the main focus of the link
171 time optimizer. Finally, this approach increases end user's build time due
172 to the duplication of work done by this separate tool and the linker itself.
173 </dd>
174 </dl>
Devang Patel93449f12006-08-14 18:03:40 +0000175</div>
176
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000177</div>
178
Devang Patel93449f12006-08-14 18:03:40 +0000179<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000180<h2>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000181 <a name="multiphase">Multi-phase communication between libLTO and linker</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000182</h2>
Devang Patel93449f12006-08-14 18:03:40 +0000183
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000184<div>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000185 <p>The linker collects information about symbol defininitions and uses in
186 various link objects which is more accurate than any information collected
187 by other tools during typical build cycles. The linker collects this
188 information by looking at the definitions and uses of symbols in native .o
189 files and using symbol visibility information. The linker also uses
190 user-supplied information, such as a list of exported symbols. LLVM
191 optimizer collects control flow information, data flow information and knows
192 much more about program structure from the optimizer's point of view.
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000193 Our goal is to take advantage of tight integration between the linker and
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000194 the optimizer by sharing this information during various linking phases.
Devang Patel93449f12006-08-14 18:03:40 +0000195</p>
Devang Patel93449f12006-08-14 18:03:40 +0000196
197<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000198<h3>
Gabor Greif04367bf2007-07-06 22:07:22 +0000199 <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000200</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000201
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000202<div>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000203 <p>The linker first reads all object files in natural order and collects
Gabor Greif04367bf2007-07-06 22:07:22 +0000204 symbol information. This includes native object files as well as LLVM bitcode
Nick Kledzik84e5f772008-02-29 19:34:52 +0000205 files. To minimize the cost to the linker in the case that all .o files
206 are native object files, the linker only calls <tt>lto_module_create()</tt>
207 when a supplied object file is found to not be a native object file. If
208 <tt>lto_module_create()</tt> returns that the file is an LLVM bitcode file,
209 the linker
210 then iterates over the module using <tt>lto_module_get_symbol_name()</tt> and
211 <tt>lto_module_get_symbol_attribute()</tt> to get all symbols defined and
212 referenced.
213 This information is added to the linker's global symbol table.
214</p>
215 <p>The lto* functions are all implemented in a shared object libLTO. This
216 allows the LLVM LTO code to be updated independently of the linker tool.
217 On platforms that support it, the shared object is lazily loaded.
Devang Patel93449f12006-08-14 18:03:40 +0000218</p>
219</div>
220
221<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000222<h3>
Devang Patel93449f12006-08-14 18:03:40 +0000223 <a name="phase2">Phase 2 : Symbol Resolution</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000224</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000225
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000226<div>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000227 <p>In this stage, the linker resolves symbols using global symbol table.
228 It may report undefined symbol errors, read archive members, replace
229 weak symbols, etc. The linker is able to do this seamlessly even though it
230 does not know the exact content of input LLVM bitcode files. If dead code
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000231 stripping is enabled then the linker collects the list of live symbols.
232 </p>
Devang Patel93449f12006-08-14 18:03:40 +0000233</div>
234
235<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000236<h3>
Gabor Greif04367bf2007-07-06 22:07:22 +0000237 <a name="phase3">Phase 3 : Optimize Bitcode Files</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000238</h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000239<div>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000240 <p>After symbol resolution, the linker tells the LTO shared object which
241 symbols are needed by native object files. In the example above, the linker
242 reports that only <tt>foo1()</tt> is used by native object files using
243 <tt>lto_codegen_add_must_preserve_symbol()</tt>. Next the linker invokes
244 the LLVM optimizer and code generators using <tt>lto_codegen_compile()</tt>
245 which returns a native object file creating by merging the LLVM bitcode files
246 and applying various optimization passes.
Devang Patel93449f12006-08-14 18:03:40 +0000247</p>
248</div>
249
250<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000251<h3>
Devang Patel93449f12006-08-14 18:03:40 +0000252 <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000253</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000254
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000255<div>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000256 <p>In this phase, the linker reads optimized a native object file and
257 updates the internal global symbol table to reflect any changes. The linker
258 also collects information about any changes in use of external symbols by
Nick Lewycky1f048642010-04-24 22:01:40 +0000259 LLVM bitcode files. In the example above, the linker notes that
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000260 <tt>foo4()</tt> is not used any more. If dead code stripping is enabled then
261 the linker refreshes the live symbol information appropriately and performs
262 dead code stripping.</p>
263 <p>After this phase, the linker continues linking as if it never saw LLVM
Gabor Greif04367bf2007-07-06 22:07:22 +0000264 bitcode files.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000265</div>
266
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000267</div>
268
Devang Patel93449f12006-08-14 18:03:40 +0000269<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000270<h2>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000271<a name="lto">libLTO</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000272</h2>
Devang Patel93449f12006-08-14 18:03:40 +0000273
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000274<div>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000275 <p><tt>libLTO</tt> is a shared object that is part of the LLVM tools, and
276 is intended for use by a linker. <tt>libLTO</tt> provides an abstract C
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000277 interface to use the LLVM interprocedural optimizer without exposing details
278 of LLVM's internals. The intention is to keep the interface as stable as
Nick Kledzik84e5f772008-02-29 19:34:52 +0000279 possible even when the LLVM optimizer continues to evolve. It should even
280 be possible for a completely different compilation technology to provide
281 a different libLTO that works with their object files and the standard
282 linker tool.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000283
284<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000285<h3>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000286 <a name="lto_module_t">lto_module_t</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000287</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000288
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000289<div>
Misha Brukman64722e52008-12-16 03:07:49 +0000290
291<p>A non-native object file is handled via an <tt>lto_module_t</tt>.
292The following functions allow the linker to check if a file (on disk
293or in a memory buffer) is a file which libLTO can process:</p>
294
295<pre class="doc_code">
296lto_module_is_object_file(const char*)
297lto_module_is_object_file_for_target(const char*, const char*)
298lto_module_is_object_file_in_memory(const void*, size_t)
299lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*)
300</pre>
301
302<p>If the object file can be processed by libLTO, the linker creates a
303<tt>lto_module_t</tt> by using one of</p>
304
305<pre class="doc_code">
306lto_module_create(const char*)
307lto_module_create_from_memory(const void*, size_t)
308</pre>
309
310<p>and when done, the handle is released via</p>
311
312<pre class="doc_code">
313lto_module_dispose(lto_module_t)
314</pre>
315
316<p>The linker can introspect the non-native object file by getting the number of
317symbols and getting the name and attributes of each symbol via:</p>
318
319<pre class="doc_code">
320lto_module_get_num_symbols(lto_module_t)
321lto_module_get_symbol_name(lto_module_t, unsigned int)
322lto_module_get_symbol_attribute(lto_module_t, unsigned int)
323</pre>
324
325<p>The attributes of a symbol include the alignment, visibility, and kind.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000326</div>
327
328<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000329<h3>
Nick Kledzik84e5f772008-02-29 19:34:52 +0000330 <a name="lto_code_gen_t">lto_code_gen_t</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000331</h3>
Devang Patel93449f12006-08-14 18:03:40 +0000332
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000333<div>
Misha Brukman64722e52008-12-16 03:07:49 +0000334
335<p>Once the linker has loaded each non-native object files into an
336<tt>lto_module_t</tt>, it can request libLTO to process them all and
337generate a native object file. This is done in a couple of steps.
338First, a code generator is created with:</p>
339
340<pre class="doc_code">lto_codegen_create()</pre>
341
342<p>Then, each non-native object file is added to the code generator with:</p>
343
344<pre class="doc_code">
345lto_codegen_add_module(lto_code_gen_t, lto_module_t)
346</pre>
347
348<p>The linker then has the option of setting some codegen options. Whether or
349not to generate DWARF debug info is set with:</p>
350
351<pre class="doc_code">lto_codegen_set_debug_model(lto_code_gen_t)</pre>
352
353<p>Which kind of position independence is set with:</p>
354
355<pre class="doc_code">lto_codegen_set_pic_model(lto_code_gen_t) </pre>
356
357<p>And each symbol that is referenced by a native object file or otherwise must
358not be optimized away is set with:</p>
359
360<pre class="doc_code">
361lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*)
362</pre>
363
364<p>After all these settings are done, the linker requests that a native object
365file be created from the modules with the settings using:</p>
366
367<pre class="doc_code">lto_codegen_compile(lto_code_gen_t, size*)</pre>
368
369<p>which returns a pointer to a buffer containing the generated native
370object file. The linker then parses that and links it with the rest
371of the native object files.</p>
372
Devang Patel93449f12006-08-14 18:03:40 +0000373</div>
374
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000375</div>
376
Devang Patel93449f12006-08-14 18:03:40 +0000377<!-- *********************************************************************** -->
378
379<hr>
380<address>
381 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000382 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Devang Patel93449f12006-08-14 18:03:40 +0000383 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000384 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Devang Patel93449f12006-08-14 18:03:40 +0000385
Nick Kledzik84e5f772008-02-29 19:34:52 +0000386 Devang Patel and Nick Kledzik<br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000387 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Devang Patel93449f12006-08-14 18:03:40 +0000388 Last modified: $Date$
389</address>
390
391</body>
392</html>
Nick Kledzik55998ba2008-02-26 01:36:52 +0000393