Fix srcdir <> objdir builds with ocaml 2.10. Downrev versions don't care whether
[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  <title>LLVM Link Time Optimization: Design and Implementation</title>
6   <link rel="stylesheet" href="llvm.css" type="text/css">
7 </head>
8
9 <div class="doc_title">
10   LLVM Link Time Optimization: Design and Implementation
11 </div>
12
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 Bytecode Files</a></li>
23     <li><a href="#phase2">Phase 2 : Symbol Resolution</a></li>
24     <li><a href="#phase3">Phase 3 : Optimize Bytecode Files</a></li>
25     <li><a href="#phase4">Phase 4 : Symbol Resolution after optimization</a></li>
26   </ul></li>
27   <li><a href="#lto">LLVMlto</a>
28   <ul>
29     <li><a href="#llvmsymbol">LLVMSymbol</a></li>
30     <li><a href="#readllvmobjectfile">readLLVMObjectFile()</a></li>
31     <li><a href="#optimizemodules">optimizeModules()</a></li>
32     <li><a href="#gettargettriple">getTargetTriple()</a></li>
33     <li><a href="#removemodule">removeModule()</a></li>
34     <li><a href="#getalignment">getAlignment()</a></li>
35   </ul></li>
36   <li><a href="#debug">Debugging Information</a></li>
37 </ul>
38
39 <div class="doc_author">
40 <p>Written by Devang Patel</p>
41 </div>
42
43 <!-- *********************************************************************** -->
44 <div class="doc_section">
45 <a name="desc">Description</a>
46 </div>
47 <!-- *********************************************************************** -->
48
49 <div class="doc_text">
50 <p>
51 LLVM features powerful intermodular optimizations which can be used at link 
52 time.  Link Time Optimization is another name for intermodular optimization 
53 when performed during the link stage. This document describes the interface 
54 and design between the LLVM intermodular optimizer and the linker.</p>
55 </div>
56
57 <!-- *********************************************************************** -->
58 <div class="doc_section">
59 <a name="design">Design Philosophy</a>
60 </div>
61 <!-- *********************************************************************** -->
62
63 <div class="doc_text">
64 <p>
65 The LLVM Link Time Optimizer provides complete transparency, while doing 
66 intermodular optimization, in the compiler tool chain. Its main goal is to let 
67 the developer take advantage of intermodular optimizations without making any 
68 significant changes to the developer's makefiles or build system. This is 
69 achieved through tight integration with the linker. In this model, the linker 
70 treates LLVM bitcode files like native object files and allows mixing and 
71 matching among them. The linker uses <a href="#lto">LLVMlto</a>, a dynamically 
72 loaded library, to handle LLVM bitcode files. This tight integration between 
73 the linker and LLVM optimizer helps to do optimizations that are not possible 
74 in other models. The linker input allows the optimizer to avoid relying on 
75 conservative escape analysis.
76 </p>
77 </div>
78
79 <!-- ======================================================================= -->
80 <div class="doc_subsection">
81   <a name="example1">Example of link time optimization</a>
82 </div>
83
84 <div class="doc_text">
85   <p>The following example illustrates the advantages of LTO's integrated
86   approach and clean interface. This example requires a system linker which
87   supports LTO through the interface described in this document.  Here,
88   llvm-gcc transparently invokes system linker. </p>
89   <ul>
90     <li> Input source file <tt>a.c</tt> is compiled into LLVM bitcode form.
91     <li> Input source file <tt>main.c</tt> is compiled into native object code.
92   </ul>
93 <div class="doc_code"><pre>
94 --- a.h ---
95 extern int foo1(void);
96 extern void foo2(void);
97 extern void foo4(void);
98 --- a.c ---
99 #include "a.h"
100
101 static signed int i = 0;
102
103 void foo2(void) {
104  i = -1;
105 }
106
107 static int foo3() {
108 foo4();
109 return 10;
110 }
111
112 int foo1(void) {
113 int data = 0;
114
115 if (i &lt; 0) { data = foo3(); }
116
117 data = data + 42;
118 return data;
119 }
120
121 --- main.c ---
122 #include &lt;stdio.h&gt;
123 #include "a.h"
124
125 void foo4(void) {
126  printf ("Hi\n");
127 }
128
129 int main() {
130  return foo1();
131 }
132
133 --- command lines ---
134 $ llvm-gcc --emit-llvm -c a.c -o a.o  # &lt;-- a.o is LLVM bitcode file
135 $ llvm-gcc -c main.c -o main.o # &lt;-- main.o is native object file
136 $ llvm-gcc a.o main.o -o main # &lt;-- standard link command without any modifications
137 </pre></div>
138   <p>In this example, the linker recognizes that <tt>foo2()</tt> is an 
139   externally visible symbol defined in LLVM bitcode file. This information 
140   is collected using <a href="#readllvmobjectfile"> readLLVMObjectFile()</a>. 
141   Based on this information, the linker completes its usual symbol resolution 
142   pass and finds that <tt>foo2()</tt> is not used anywhere. This information 
143   is used by the LLVM optimizer and it removes <tt>foo2()</tt>. As soon as 
144   <tt>foo2()</tt> is removed, the optimizer recognizes that condition 
145   <tt>i &lt; 0</tt> is always false, which means <tt>foo3()</tt> is never 
146   used. Hence, the optimizer removes <tt>foo3()</tt>, also.  And this in turn, 
147   enables linker to remove <tt>foo4()</tt>.  This example illustrates the 
148   advantage of tight integration with the linker. Here, the optimizer can not 
149   remove <tt>foo3()</tt> without the linker's input.
150   </p>
151 </div>
152
153 <!-- ======================================================================= -->
154 <div class="doc_subsection">
155   <a name="alternative_approaches">Alternative Approaches</a>
156 </div>
157
158 <div class="doc_text">
159   <dl>
160     <dt><b>Compiler driver invokes link time optimizer separately.</b></dt>
161     <dd>In this model the link time optimizer is not able to take advantage of 
162     information collected during the linker's normal symbol resolution phase. 
163     In the above example, the optimizer can not remove <tt>foo2()</tt> without 
164     the linker's input because it is externally visible. This in turn prohibits
165     the optimizer from removing <tt>foo3()</tt>.</dd>
166     <dt><b>Use separate tool to collect symbol information from all object
167     files.</b></dt>
168     <dd>In this model, a new, separate, tool or library replicates the linker's
169     capability to collect information for link time optimization. Not only is
170     this code duplication difficult to justify, but it also has several other 
171     disadvantages.  For example, the linking semantics and the features 
172     provided by the linker on various platform are not unique. This means, 
173     this new tool needs to support all such features and platforms in one 
174     super tool or a separate tool per platform is required. This increases 
175     maintance cost for link time optimizer significantly, which is not 
176     necessary. This approach also requires staying synchronized with linker 
177     developements on various platforms, which is not the main focus of the link 
178     time optimizer. Finally, this approach increases end user's build time due 
179     to the duplication of work done by this separate tool and the linker itself.
180     </dd>
181   </dl>
182 </div>
183
184 <!-- *********************************************************************** -->
185 <div class="doc_section">
186   <a name="multiphase">Multi-phase communication between LLVM and linker</a>
187 </div>
188
189 <div class="doc_text">
190   <p>The linker collects information about symbol defininitions and uses in 
191   various link objects which is more accurate than any information collected 
192   by other tools during typical build cycles.  The linker collects this 
193   information by looking at the definitions and uses of symbols in native .o 
194   files and using symbol visibility information. The linker also uses 
195   user-supplied information, such as a list of exported symbols. LLVM 
196   optimizer collects control flow information, data flow information and knows 
197   much more about program structure from the optimizer's point of view. 
198   Our goal is to take advantage of tight intergration between the linker and 
199   the optimizer by sharing this information during various linking phases.
200 </p>
201 </div>
202
203 <!-- ======================================================================= -->
204 <div class="doc_subsection">
205   <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a>
206 </div>
207
208 <div class="doc_text">
209   <p>The linker first reads all object files in natural order and collects 
210   symbol information. This includes native object files as well as LLVM bitcode 
211   files.  In this phase, the linker uses 
212   <a href="#readllvmobjectfile"> readLLVMObjectFile() </a>  to collect symbol
213   information from each LLVM bitcode files and updates its internal global 
214   symbol table accordingly. The intent of this interface is to avoid overhead 
215   in the non LLVM case, where all input object files are native object files, 
216   by putting this code in the error path of the linker. When the linker sees 
217   the first llvm .o file, it <tt>dlopen()</tt>s the dynamic library. This is
218   to allow changes to the LLVM LTO code without relinking the linker.
219 </p>
220 </div>
221
222 <!-- ======================================================================= -->
223 <div class="doc_subsection">
224   <a name="phase2">Phase 2 : Symbol Resolution</a>
225 </div>
226
227 <div class="doc_text">
228   <p>In this stage, the linker resolves symbols using global symbol table 
229   information to report undefined symbol errors, read archive members, resolve 
230   weak symbols, etc. The linker is able to do this seamlessly even though it 
231   does not know the exact content of input LLVM bitcode files because it uses 
232   symbol information provided by 
233   <a href="#readllvmobjectfile">readLLVMObjectFile()</a>.  If dead code 
234   stripping is enabled then the linker collects the list of live symbols.
235   </p>
236 </div>
237
238 <!-- ======================================================================= -->
239 <div class="doc_subsection">
240   <a name="phase3">Phase 3 : Optimize Bitcode Files</a>
241 </div>
242 <div class="doc_text">
243   <p>After symbol resolution, the linker updates symbol information supplied 
244   by LLVM bitcode files appropriately. For example, whether certain LLVM 
245   bitcode supplied symbols are used or not. In the example above, the linker 
246   reports that <tt>foo2()</tt> is not used anywhere in the program, including 
247   native <tt>.o</tt> files. This information is used by the LLVM interprocedural
248   optimizer. The linker uses <a href="#optimizemodules">optimizeModules()</a> 
249   and requests an optimized native object file of the LLVM portion of the 
250   program. 
251 </p>
252 </div>
253
254 <!-- ======================================================================= -->
255 <div class="doc_subsection">
256   <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
257 </div>
258
259 <div class="doc_text">
260   <p>In this phase, the linker reads optimized a native object file and 
261   updates the internal global symbol table to reflect any changes. The linker 
262   also collects information about any changes in use of external symbols by 
263   LLVM bitcode files. In the examle above, the linker notes that 
264   <tt>foo4()</tt> is not used any more. If dead code stripping is enabled then 
265   the linker refreshes the live symbol information appropriately and performs 
266   dead code stripping.</p>
267   <p>After this phase, the linker continues linking as if it never saw LLVM 
268   bitcode files.</p>
269 </div>
270
271 <!-- *********************************************************************** -->
272 <div class="doc_section">
273 <a name="lto">LLVMlto</a>
274 </div>
275
276 <div class="doc_text">
277   <p><tt>LLVMlto</tt> is a dynamic library that is part of the LLVM tools, and 
278   is intended for use by a linker. <tt>LLVMlto</tt> provides an abstract C++ 
279   interface to use the LLVM interprocedural optimizer without exposing details 
280   of LLVM's internals. The intention is to keep the interface as stable as 
281   possible even when the LLVM optimizer continues to evolve.</p>
282 </div>
283
284 <!-- ======================================================================= -->
285 <div class="doc_subsection">
286   <a name="llvmsymbol">LLVMSymbol</a>
287 </div>
288
289 <div class="doc_text">
290   <p>The <tt>LLVMSymbol</tt> class is used to describe the externally visible 
291   functions and global variables, defined in LLVM bitcode files, to the linker.
292   This includes symbol visibility information. This information is used by 
293   the linker to do symbol resolution. For example: function <tt>foo2()</tt> is 
294   defined inside an LLVM bitcode module and it is an externally visible symbol.
295   This helps the linker connect the use of <tt>foo2()</tt> in native object 
296   files with a future definition of the symbol <tt>foo2()</tt>. The linker 
297   will see the actual definition of <tt>foo2()</tt> when it receives the 
298   optimized native object file in 
299   <a href="#phase4">Symbol Resolution after optimization</a> phase. If the 
300   linker does not find any uses of <tt>foo2()</tt>, it updates LLVMSymbol 
301   visibility information to notify LLVM intermodular optimizer that it is dead.
302   The LLVM intermodular optimizer takes advantage of such information to 
303   generate better code.</p>
304 </div>
305
306 <!-- ======================================================================= -->
307 <div class="doc_subsection">
308   <a name="readllvmobjectfile">readLLVMObjectFile()</a>
309 </div>
310
311 <div class="doc_text">
312   <p>The <tt>readLLVMObjectFile()</tt> function is used by the linker to read 
313   LLVM bitcode files and collect LLVMSymbol information. This routine also
314   supplies a list of externally defined symbols that are used by LLVM bitcode
315   files. The linker uses this symbol information to do symbol resolution. 
316   Internally, <a href="#lto">LLVMlto</a> maintains LLVM bitcode modules in 
317   memory. This function also provides a list of external references used by 
318   bitcode files.</p>
319 </div>
320
321 <!-- ======================================================================= -->
322 <div class="doc_subsection">
323   <a name="optimizemodules">optimizeModules()</a>
324 </div>
325
326 <div class="doc_text">
327   <p>The linker invokes <tt>optimizeModules</tt> to optimize already read 
328   LLVM bitcode files by applying LLVM intermodular optimization techniques. 
329   This function runs the LLVM intermodular optimizer and generates native 
330   object code as <tt>.o</tt> files at the name and location provided by the 
331   linker.</p>
332 </div>
333
334 <!-- ======================================================================= -->
335 <div class="doc_subsection">
336   <a name="gettargettriple">getTargetTriple()</a>
337 </div>
338
339 <div class="doc_text">
340   <p>The linker may use <tt>getTargetTriple()</tt> to query target architecture
341   while validating LLVM bitcode file.</p>
342 </div>
343
344 <!-- ======================================================================= -->
345 <div class="doc_subsection">
346   <a name="removemodule">removeModule()</a>
347 </div>
348
349 <div class="doc_text">
350   <p>Internally, <a href="#lto">LLVMlto</a> maintains LLVM bitcode modules in 
351   memory. The linker may use <tt>removeModule()</tt> method to remove desired
352   modules from memory. </p>
353 </div>
354
355 <!-- ======================================================================= -->
356 <div class="doc_subsection">
357   <a name="getalignment">getAlignment()</a>
358 </div>
359
360 <div class="doc_text">
361   <p>The linker may use <a href="#llvmsymbol">LLVMSymbol</a> method 
362   <tt>getAlignment()</tt> to query symbol alignment information.</p>
363 </div>
364
365 <!-- *********************************************************************** -->
366 <div class="doc_section">
367   <a name="debug">Debugging Information</a>
368 </div>
369 <!-- *********************************************************************** -->
370
371 <div class="doc_text">
372
373 <p><tt> ... To be completed ... </tt></p>
374
375 </div>
376
377 <!-- *********************************************************************** -->
378
379 <hr>
380 <address>
381   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
382   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
383   <a href="http://validator.w3.org/check/referer"><img
384   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
385
386   Devang Patel<br>
387   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
388   Last modified: $Date$
389 </address>
390
391 </body>
392 </html>