Add mention of Glasgow Haskell Compiler.
[oota-llvm.git] / docs / LinkTimeOptimization.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6  <title>LLVM Link Time Optimization: Design and Implementation</title>
7   <link rel="stylesheet" href="_static/llvm.css" type="text/css">
8 </head>
9
10 <h1>
11   LLVM Link Time Optimization: Design and Implementation
12 </h1>
13
14 <ul>
15   <li><a href="#desc">Description</a></li>
16   <li><a href="#design">Design Philosophy</a>
17   <ul>
18     <li><a href="#example1">Example of link time optimization</a></li>
19     <li><a href="#alternative_approaches">Alternative Approaches</a></li>
20   </ul></li>
21   <li><a href="#multiphase">Multi-phase communication between LLVM and linker</a>
22   <ul>
23     <li><a href="#phase1">Phase 1 : Read LLVM Bitcode Files</a></li>
24     <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
25     <li><a href="#phase3">Phase 3 : Optimize Bitcode Files</a></li>
26     <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
27   </ul></li>
28   <li><a href="#lto">libLTO</a>
29   <ul>
30     <li><a href="#lto_module_t">lto_module_t</a></li>
31     <li><a href="#lto_code_gen_t">lto_code_gen_t</a></li>
32   </ul>
33 </ul>
34
35 <div class="doc_author">
36 <p>Written by Devang Patel and Nick Kledzik</p>
37 </div>
38
39 <!-- *********************************************************************** -->
40 <h2>
41 <a name="desc">Description</a>
42 </h2>
43 <!-- *********************************************************************** -->
44
45 <div>
46 <p>
47 LLVM features powerful intermodular optimizations which can be used at link 
48 time.  Link Time Optimization (LTO) is another name for intermodular optimization 
49 when performed during the link stage. This document describes the interface 
50 and design between the LTO optimizer and the linker.</p>
51 </div>
52
53 <!-- *********************************************************************** -->
54 <h2>
55 <a name="design">Design Philosophy</a>
56 </h2>
57 <!-- *********************************************************************** -->
58
59 <div>
60 <p>
61 The LLVM Link Time Optimizer provides complete transparency, while doing 
62 intermodular optimization, in the compiler tool chain. Its main goal is to let 
63 the developer take advantage of intermodular optimizations without making any 
64 significant changes to the developer's makefiles or build system. This is 
65 achieved through tight integration with the linker. In this model, the linker 
66 treates LLVM bitcode files like native object files and allows mixing and 
67 matching among them. The linker uses <a href="#lto">libLTO</a>, a shared
68 object, to handle LLVM bitcode files. This tight integration between 
69 the linker and LLVM optimizer helps to do optimizations that are not possible 
70 in other models. The linker input allows the optimizer to avoid relying on 
71 conservative escape analysis.
72 </p>
73
74 <!-- ======================================================================= -->
75 <h3>
76   <a name="example1">Example of link time optimization</a>
77 </h3>
78
79 <div>
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   clang 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
94 --- a.c ---
95 #include "a.h"
96
97 static signed int i = 0;
98
99 void foo2(void) {
100   i = -1;
101 }
102
103 static int foo3() {
104   foo4();
105   return 10;
106 }
107
108 int foo1(void) {
109   int data = 0;
110
111   if (i &lt; 0) 
112     data = foo3();
113
114   data = data + 42;
115   return data;
116 }
117
118 --- main.c ---
119 #include &lt;stdio.h&gt;
120 #include "a.h"
121
122 void foo4(void) {
123   printf("Hi\n");
124 }
125
126 int main() {
127   return foo1();
128 }
129
130 --- command lines ---
131 $ clang -emit-llvm -c a.c -o a.o   # &lt;-- a.o is LLVM bitcode file
132 $ clang -c main.c -o main.o        # &lt;-- main.o is native object file
133 $ clang a.o main.o -o main         # &lt;-- standard link command without any modifications
134 </pre>
135
136 <ul>
137   <li>In this example, the linker recognizes that <tt>foo2()</tt> is an
138       externally visible symbol defined in LLVM bitcode file. The linker
139       completes its usual symbol resolution pass and finds that <tt>foo2()</tt>
140       is not used anywhere. This information is used by the LLVM optimizer and
141       it removes <tt>foo2()</tt>.</li>
142   <li>As soon as <tt>foo2()</tt> is removed, the optimizer recognizes that condition 
143       <tt>i &lt; 0</tt> is always false, which means <tt>foo3()</tt> is never 
144       used. Hence, the optimizer also removes <tt>foo3()</tt>.</li>
145   <li>And this in turn, enables linker to remove <tt>foo4()</tt>.</li>
146 </ul>
147
148 <p>This example illustrates the advantage of tight integration with the
149    linker. Here, the optimizer can not remove <tt>foo3()</tt> without the
150    linker's input.</p>
151
152 </div>
153
154 <!-- ======================================================================= -->
155 <h3>
156   <a name="alternative_approaches">Alternative Approaches</a>
157 </h3>
158
159 <div>
160   <dl>
161     <dt><b>Compiler driver invokes link time optimizer separately.</b></dt>
162     <dd>In this model the link time optimizer is not able to take advantage of 
163     information collected during the linker's normal symbol resolution phase. 
164     In the above example, the optimizer can not remove <tt>foo2()</tt> without 
165     the linker's input because it is externally visible. This in turn prohibits
166     the optimizer from removing <tt>foo3()</tt>.</dd>
167     <dt><b>Use separate tool to collect symbol information from all object
168     files.</b></dt>
169     <dd>In this model, a new, separate, tool or library replicates the linker's
170     capability to collect information for link time optimization. Not only is
171     this code duplication difficult to justify, but it also has several other 
172     disadvantages.  For example, the linking semantics and the features 
173     provided by the linker on various platform are not unique. This means, 
174     this new tool needs to support all such features and platforms in one 
175     super tool or a separate tool per platform is required. This increases 
176     maintenance cost for link time optimizer significantly, which is not 
177     necessary. This approach also requires staying synchronized with linker 
178     developements on various platforms, which is not the main focus of the link 
179     time optimizer. Finally, this approach increases end user's build time due 
180     to the duplication of work done by this separate tool and the linker itself.
181     </dd>
182   </dl>
183 </div>
184
185 </div>
186
187 <!-- *********************************************************************** -->
188 <h2>
189   <a name="multiphase">Multi-phase communication between libLTO and linker</a>
190 </h2>
191
192 <div>
193   <p>The linker collects information about symbol defininitions and uses in 
194   various link objects which is more accurate than any information collected 
195   by other tools during typical build cycles.  The linker collects this 
196   information by looking at the definitions and uses of symbols in native .o 
197   files and using symbol visibility information. The linker also uses 
198   user-supplied information, such as a list of exported symbols. LLVM 
199   optimizer collects control flow information, data flow information and knows 
200   much more about program structure from the optimizer's point of view. 
201   Our goal is to take advantage of tight integration between the linker and 
202   the optimizer by sharing this information during various linking phases.
203 </p>
204
205 <!-- ======================================================================= -->
206 <h3>
207   <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a>
208 </h3>
209
210 <div>
211   <p>The linker first reads all object files in natural order and collects 
212   symbol information. This includes native object files as well as LLVM bitcode 
213   files.  To minimize the cost to the linker in the case that all .o files
214   are native object files, the linker only calls <tt>lto_module_create()</tt> 
215   when a supplied object file is found to not be a native object file.  If
216   <tt>lto_module_create()</tt> returns that the file is an LLVM bitcode file, 
217   the linker
218   then iterates over the module using <tt>lto_module_get_symbol_name()</tt> and
219   <tt>lto_module_get_symbol_attribute()</tt> to get all symbols defined and 
220   referenced.
221   This information is added to the linker's global symbol table.
222 </p>
223   <p>The lto* functions are all implemented in a shared object libLTO.  This
224   allows the LLVM LTO code to be updated independently of the linker tool.
225   On platforms that support it, the shared object is lazily loaded. 
226 </p>
227 </div>
228
229 <!-- ======================================================================= -->
230 <h3>
231   <a name="phase2">Phase 2 : Symbol Resolution</a>
232 </h3>
233
234 <div>
235   <p>In this stage, the linker resolves symbols using global symbol table. 
236   It may report undefined symbol errors, read archive members, replace 
237   weak symbols, etc.  The linker is able to do this seamlessly even though it 
238   does not know the exact content of input LLVM bitcode files.  If dead code 
239   stripping is enabled then the linker collects the list of live symbols.
240   </p>
241 </div>
242
243 <!-- ======================================================================= -->
244 <h3>
245   <a name="phase3">Phase 3 : Optimize Bitcode Files</a>
246 </h3>
247 <div>
248   <p>After symbol resolution, the linker tells the LTO shared object which
249   symbols are needed by native object files.  In the example above, the linker 
250   reports that only <tt>foo1()</tt> is used by native object files using 
251   <tt>lto_codegen_add_must_preserve_symbol()</tt>.  Next the linker invokes
252   the LLVM optimizer and code generators using <tt>lto_codegen_compile()</tt>
253   which returns a native object file creating by merging the LLVM bitcode files 
254   and applying various optimization passes.  
255 </p>
256 </div>
257
258 <!-- ======================================================================= -->
259 <h3>
260   <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
261 </h3>
262
263 <div>
264   <p>In this phase, the linker reads optimized a native object file and 
265   updates the internal global symbol table to reflect any changes. The linker 
266   also collects information about any changes in use of external symbols by 
267   LLVM bitcode files. In the example above, the linker notes that 
268   <tt>foo4()</tt> is not used any more. If dead code stripping is enabled then 
269   the linker refreshes the live symbol information appropriately and performs 
270   dead code stripping.</p>
271   <p>After this phase, the linker continues linking as if it never saw LLVM 
272   bitcode files.</p>
273 </div>
274
275 </div>
276
277 <!-- *********************************************************************** -->
278 <h2>
279 <a name="lto">libLTO</a>
280 </h2>
281
282 <div>
283   <p><tt>libLTO</tt> is a shared object that is part of the LLVM tools, and 
284   is intended for use by a linker. <tt>libLTO</tt> provides an abstract C 
285   interface to use the LLVM interprocedural optimizer without exposing details 
286   of LLVM's internals. The intention is to keep the interface as stable as 
287   possible even when the LLVM optimizer continues to evolve. It should even
288   be possible for a completely different compilation technology to provide
289   a different libLTO that works with their object files and the standard
290   linker tool.</p>
291
292 <!-- ======================================================================= -->
293 <h3>
294   <a name="lto_module_t">lto_module_t</a>
295 </h3>
296
297 <div>
298
299 <p>A non-native object file is handled via an <tt>lto_module_t</tt>.  
300 The following functions allow the linker to check if a file (on disk
301 or in a memory buffer) is a file which libLTO can process:</p>
302
303 <pre class="doc_code">
304 lto_module_is_object_file(const char*)
305 lto_module_is_object_file_for_target(const char*, const char*)
306 lto_module_is_object_file_in_memory(const void*, size_t)
307 lto_module_is_object_file_in_memory_for_target(const void*, size_t, const char*)
308 </pre>
309
310 <p>If the object file can be processed by libLTO, the linker creates a
311 <tt>lto_module_t</tt> by using one of</p>
312
313 <pre class="doc_code">
314 lto_module_create(const char*)
315 lto_module_create_from_memory(const void*, size_t)
316 </pre>
317
318 <p>and when done, the handle is released via</p>
319
320 <pre class="doc_code">
321 lto_module_dispose(lto_module_t)
322 </pre>
323
324 <p>The linker can introspect the non-native object file by getting the number of
325 symbols and getting the name and attributes of each symbol via:</p>
326
327 <pre class="doc_code">
328 lto_module_get_num_symbols(lto_module_t)
329 lto_module_get_symbol_name(lto_module_t, unsigned int)
330 lto_module_get_symbol_attribute(lto_module_t, unsigned int)
331 </pre>
332
333 <p>The attributes of a symbol include the alignment, visibility, and kind.</p>
334 </div>
335
336 <!-- ======================================================================= -->
337 <h3>
338   <a name="lto_code_gen_t">lto_code_gen_t</a>
339 </h3>
340
341 <div>
342
343 <p>Once the linker has loaded each non-native object files into an
344 <tt>lto_module_t</tt>, it can request libLTO to process them all and
345 generate a native object file.  This is done in a couple of steps.
346 First, a code generator is created with:</p>
347
348 <pre class="doc_code">lto_codegen_create()</pre>
349
350 <p>Then, each non-native object file is added to the code generator with:</p>
351
352 <pre class="doc_code">
353 lto_codegen_add_module(lto_code_gen_t, lto_module_t)
354 </pre>
355
356 <p>The linker then has the option of setting some codegen options.  Whether or
357 not to generate DWARF debug info is set with:</p>
358   
359 <pre class="doc_code">lto_codegen_set_debug_model(lto_code_gen_t)</pre>
360
361 <p>Which kind of position independence is set with:</p>
362
363 <pre class="doc_code">lto_codegen_set_pic_model(lto_code_gen_t) </pre>
364   
365 <p>And each symbol that is referenced by a native object file or otherwise must
366 not be optimized away is set with:</p>
367
368 <pre class="doc_code">
369 lto_codegen_add_must_preserve_symbol(lto_code_gen_t, const char*)
370 </pre>
371
372 <p>After all these settings are done, the linker requests that a native object
373 file be created from the modules with the settings using:</p>
374
375 <pre class="doc_code">lto_codegen_compile(lto_code_gen_t, size*)</pre>
376
377 <p>which returns a pointer to a buffer containing the generated native
378 object file.  The linker then parses that and links it with the rest 
379 of the native object files.</p>
380
381 </div>
382
383 </div>
384
385 <!-- *********************************************************************** -->
386
387 <hr>
388 <address>
389   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
390   src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
391   <a href="http://validator.w3.org/check/referer"><img
392   src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
393
394   Devang Patel and Nick Kledzik<br>
395   <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
396   Last modified: $Date$
397 </address>
398
399 </body>
400 </html>
401