Teach mergefunc that intptr_t is the same width as a pointer. We still can't
[llvm.git] / docs / LinkTimeOptimization.html
blobdbe8f389ce80fd1a1165b3b9f0c306aae3ae94fd
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5 <title>LLVM Link Time Optimization: Design and Implementation</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7 </head>
9 <div class="doc_title">
10 LLVM Link Time Optimization: Design and Implementation
11 </div>
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>
20 <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a>
21 <ul>
22 <li><a href="#phase1">Phase 1 : Read LLVM Bitcode Files</a></li>
23 <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
24 <li><a href="#phase3">Phase 3 : Optimize Bitcode Files</a></li>
25 <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
26 </ul></li>
27 <li><a href="#lto">libLTO</a>
28 <ul>
29 <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>
32 </ul>
34 <div class="doc_author">
35 <p>Written by Devang Patel and Nick Kledzik</p>
36 </div>
38 <!-- *********************************************************************** -->
39 <div class="doc_section">
40 <a name="desc">Description</a>
41 </div>
42 <!-- *********************************************************************** -->
44 <div class="doc_text">
45 <p>
46 LLVM features powerful intermodular optimizations which can be used at link
47 time. Link Time Optimization (LTO) is another name for intermodular optimization
48 when performed during the link stage. This document describes the interface
49 and design between the LTO optimizer and the linker.</p>
50 </div>
52 <!-- *********************************************************************** -->
53 <div class="doc_section">
54 <a name="design">Design Philosophy</a>
55 </div>
56 <!-- *********************************************************************** -->
58 <div class="doc_text">
59 <p>
60 The LLVM Link Time Optimizer provides complete transparency, while doing
61 intermodular optimization, in the compiler tool chain. Its main goal is to let
62 the developer take advantage of intermodular optimizations without making any
63 significant changes to the developer's makefiles or build system. This is
64 achieved through tight integration with the linker. In this model, the linker
65 treates LLVM bitcode files like native object files and allows mixing and
66 matching among them. The linker uses <a href="#lto">libLTO</a>, a shared
67 object, to handle LLVM bitcode files. This tight integration between
68 the linker and LLVM optimizer helps to do optimizations that are not possible
69 in other models. The linker input allows the optimizer to avoid relying on
70 conservative escape analysis.
71 </p>
72 </div>
74 <!-- ======================================================================= -->
75 <div class="doc_subsection">
76 <a name="example1">Example of link time optimization</a>
77 </div>
79 <div class="doc_text">
80 <p>The following example illustrates the advantages of LTO's integrated
81 approach and clean interface. This example requires a system linker which
82 supports LTO through the interface described in this document. Here,
83 llvm-gcc transparently invokes system linker. </p>
84 <ul>
85 <li> Input source file <tt>a.c</tt> is compiled into LLVM bitcode form.
86 <li> Input source file <tt>main.c</tt> is compiled into native object code.
87 </ul>
88 <pre class="doc_code">
89 --- a.h ---
90 extern int foo1(void);
91 extern void foo2(void);
92 extern void foo4(void);
93 --- a.c ---
94 #include "a.h"
96 static signed int i = 0;
98 void foo2(void) {
99 i = -1;
102 static int foo3() {
103 foo4();
104 return 10;
107 int foo1(void) {
108 int data = 0;
110 if (i &lt; 0) { data = foo3(); }
112 data = data + 42;
113 return data;
116 --- main.c ---
117 #include &lt;stdio.h&gt;
118 #include "a.h"
120 void foo4(void) {
121 printf ("Hi\n");
124 int main() {
125 return foo1();
128 --- command lines ---
129 $ llvm-gcc --emit-llvm -c a.c -o a.o # &lt;-- a.o is LLVM bitcode file
130 $ llvm-gcc -c main.c -o main.o # &lt;-- main.o is native object file
131 $ llvm-gcc a.o main.o -o main # &lt;-- standard link command without any modifications
132 </pre>
133 <p>In this example, the linker recognizes that <tt>foo2()</tt> is an
134 externally visible symbol defined in LLVM bitcode file. The linker completes
135 its usual symbol resolution
136 pass and finds that <tt>foo2()</tt> is not used anywhere. This information
137 is used by the LLVM optimizer and it removes <tt>foo2()</tt>. As soon as
138 <tt>foo2()</tt> is removed, the optimizer recognizes that condition
139 <tt>i &lt; 0</tt> is always false, which means <tt>foo3()</tt> is never
140 used. Hence, the optimizer removes <tt>foo3()</tt>, also. And this in turn,
141 enables linker to remove <tt>foo4()</tt>. This example illustrates the
142 advantage of tight integration with the linker. Here, the optimizer can not
143 remove <tt>foo3()</tt> without the linker's input.
144 </p>
145 </div>
147 <!-- ======================================================================= -->
148 <div class="doc_subsection">
149 <a name="alternative_approaches">Alternative Approaches</a>
150 </div>
152 <div class="doc_text">
153 <dl>
154 <dt><b>Compiler driver invokes link time optimizer separately.</b></dt>
155 <dd>In this model the link time optimizer is not able to take advantage of
156 information collected during the linker's normal symbol resolution phase.
157 In the above example, the optimizer can not remove <tt>foo2()</tt> without
158 the linker's input because it is externally visible. This in turn prohibits
159 the optimizer from removing <tt>foo3()</tt>.</dd>
160 <dt><b>Use separate tool to collect symbol information from all object
161 files.</b></dt>
162 <dd>In this model, a new, separate, tool or library replicates the linker's
163 capability to collect information for link time optimization. Not only is
164 this code duplication difficult to justify, but it also has several other
165 disadvantages. For example, the linking semantics and the features
166 provided by the linker on various platform are not unique. This means,
167 this new tool needs to support all such features and platforms in one
168 super tool or a separate tool per platform is required. This increases
169 maintenance cost for link time optimizer significantly, which is not
170 necessary. This approach also requires staying synchronized with linker
171 developements on various platforms, which is not the main focus of the link
172 time optimizer. Finally, this approach increases end user's build time due
173 to the duplication of work done by this separate tool and the linker itself.
174 </dd>
175 </dl>
176 </div>
178 <!-- *********************************************************************** -->
179 <div class="doc_section">
180 <a name="multiphase">Multi-phase communication between libLTO and linker</a>
181 </div>
183 <div class="doc_text">
184 <p>The linker collects information about symbol defininitions and uses in
185 various link objects which is more accurate than any information collected
186 by other tools during typical build cycles. The linker collects this
187 information by looking at the definitions and uses of symbols in native .o
188 files and using symbol visibility information. The linker also uses
189 user-supplied information, such as a list of exported symbols. LLVM
190 optimizer collects control flow information, data flow information and knows
191 much more about program structure from the optimizer's point of view.
192 Our goal is to take advantage of tight integration between the linker and
193 the optimizer by sharing this information during various linking phases.
194 </p>
195 </div>
197 <!-- ======================================================================= -->
198 <div class="doc_subsection">
199 <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a>
200 </div>
202 <div class="doc_text">
203 <p>The linker first reads all object files in natural order and collects
204 symbol information. This includes native object files as well as LLVM bitcode
205 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.
218 </p>
219 </div>
221 <!-- ======================================================================= -->
222 <div class="doc_subsection">
223 <a name="phase2">Phase 2 : Symbol Resolution</a>
224 </div>
226 <div class="doc_text">
227 <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
231 stripping is enabled then the linker collects the list of live symbols.
232 </p>
233 </div>
235 <!-- ======================================================================= -->
236 <div class="doc_subsection">
237 <a name="phase3">Phase 3 : Optimize Bitcode Files</a>
238 </div>
239 <div class="doc_text">
240 <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.
247 </p>
248 </div>
250 <!-- ======================================================================= -->
251 <div class="doc_subsection">
252 <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
253 </div>
255 <div class="doc_text">
256 <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
259 LLVM bitcode files. In the example above, the linker notes that
260 <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
264 bitcode files.</p>
265 </div>
267 <!-- *********************************************************************** -->
268 <div class="doc_section">
269 <a name="lto">libLTO</a>
270 </div>
272 <div class="doc_text">
273 <p><tt>libLTO</tt> is a shared object that is part of the LLVM tools, and
274 is intended for use by a linker. <tt>libLTO</tt> provides an abstract C
275 interface to use the LLVM interprocedural optimizer without exposing details
276 of LLVM's internals. The intention is to keep the interface as stable as
277 possible even when the LLVM optimizer continues to evolve. It should even
278 be possible for a completely different compilation technology to provide
279 a different libLTO that works with their object files and the standard
280 linker tool.</p>
281 </div>
283 <!-- ======================================================================= -->
284 <div class="doc_subsection">
285 <a name="lto_module_t">lto_module_t</a>
286 </div>
288 <div class="doc_text">
290 <p>A non-native object file is handled via an <tt>lto_module_t</tt>.
291 The following functions allow the linker to check if a file (on disk
292 or in a memory buffer) is a file which libLTO can process:</p>
294 <pre class="doc_code">
295 lto_module_is_object_file(const char*)
296 lto_module_is_object_file_for_target(const char*, const char*)
297 lto_module_is_object_file_in_memory(const void*, size_t)
298 lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*)
299 </pre>
301 <p>If the object file can be processed by libLTO, the linker creates a
302 <tt>lto_module_t</tt> by using one of</p>
304 <pre class="doc_code">
305 lto_module_create(const char*)
306 lto_module_create_from_memory(const void*, size_t)
307 </pre>
309 <p>and when done, the handle is released via</p>
311 <pre class="doc_code">
312 lto_module_dispose(lto_module_t)
313 </pre>
315 <p>The linker can introspect the non-native object file by getting the number of
316 symbols and getting the name and attributes of each symbol via:</p>
318 <pre class="doc_code">
319 lto_module_get_num_symbols(lto_module_t)
320 lto_module_get_symbol_name(lto_module_t, unsigned int)
321 lto_module_get_symbol_attribute(lto_module_t, unsigned int)
322 </pre>
324 <p>The attributes of a symbol include the alignment, visibility, and kind.</p>
325 </div>
327 <!-- ======================================================================= -->
328 <div class="doc_subsection">
329 <a name="lto_code_gen_t">lto_code_gen_t</a>
330 </div>
332 <div class="doc_text">
334 <p>Once the linker has loaded each non-native object files into an
335 <tt>lto_module_t</tt>, it can request libLTO to process them all and
336 generate a native object file. This is done in a couple of steps.
337 First, a code generator is created with:</p>
339 <pre class="doc_code">lto_codegen_create()</pre>
341 <p>Then, each non-native object file is added to the code generator with:</p>
343 <pre class="doc_code">
344 lto_codegen_add_module(lto_code_gen_t, lto_module_t)
345 </pre>
347 <p>The linker then has the option of setting some codegen options. Whether or
348 not to generate DWARF debug info is set with:</p>
350 <pre class="doc_code">lto_codegen_set_debug_model(lto_code_gen_t)</pre>
352 <p>Which kind of position independence is set with:</p>
354 <pre class="doc_code">lto_codegen_set_pic_model(lto_code_gen_t) </pre>
356 <p>And each symbol that is referenced by a native object file or otherwise must
357 not be optimized away is set with:</p>
359 <pre class="doc_code">
360 lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*)
361 </pre>
363 <p>After all these settings are done, the linker requests that a native object
364 file be created from the modules with the settings using:</p>
366 <pre class="doc_code">lto_codegen_compile(lto_code_gen_t, size*)</pre>
368 <p>which returns a pointer to a buffer containing the generated native
369 object file. The linker then parses that and links it with the rest
370 of the native object files.</p>
372 </div>
374 <!-- *********************************************************************** -->
376 <hr>
377 <address>
378 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
379 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
380 <a href="http://validator.w3.org/check/referer"><img
381 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
383 Devang Patel and Nick Kledzik<br>
384 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
385 Last modified: $Date$
386 </address>
388 </body>
389 </html>