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