blob: f3a61185938e0ae77be8c8e9d295b0d25a2e99f1 [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>
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>
48LLVM features powerful intermodular optimization which can be used at link time.
49Link Time Optimization is another name of intermodular optimization when it
50is done during link stage. This document describes the interface between LLVM
51intermodular 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>
63The LLVM Link Time Optimizer seeks complete transparency, while doing intermodular
64optimization, in compiler tool chain. Its main goal is to let developer take
65advantage of intermodular optimizer without making any significant changes to
66their makefiles or build system. This is achieved through tight integration with
67linker. In this model, linker treates LLVM bytecode files like native objects
68file and allows mixing and matching among them. The linker uses
69<a href="#lto">LLVMlto</a>, a dynamically loaded library, to handle LLVM bytecode
70files. This tight integration between the linker and LLVM optimizer helps to do
71optimizations that are not possible in other models. The linker input allows
72optimizer 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
83clean 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>
135In this example, the linker recognizes that <tt>foo2()</tt> is a externally visible
136symbol defined in LLVM byte code file. This information is collected using
137<a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>. Based on this
138information, 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
140and it removes <tt>foo2()</tt>. As soon as <tt>foo2()</tt> is removed, optimizer
141recognizes 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.
143And this in turn, enables linker to remove <tt>foo4()</tt>.
144This example illustrates advantage of tight integration with linker. Here,
145optimizer 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
158collected during normal linker's symbol resolution phase. In above example,
159optimizer can not remove <tt>foo2()</tt> without linker's input because it is
160externally 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
164capabilities to collect information for link time optimizer. Not only such code
165duplication is difficult to justify but it also has several other disadvantages.
166For example, the linking semantics and the features provided by linker on
167various platform are not unique. This means, this new tool needs to support all
168such features and platforms in one super tool or one new separate tool per
169platform is required. This increases maintance cost for link time optimizer
170significantly, which is not necessary. Plus, this approach requires staying
171synchronized with linker developements on various platforms, which is not the
172main focus of link time optimizer. Finally, this approach increases end user's build
173time 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>
184The linker collects information about symbol defininitions and uses in various
185link objects which is more accurate than any information collected by other tools
186during typical build cycle.
187The linker collects this information by looking at definitions and uses of
188symbols in native .o files and using symbol visibility information. The linker
189also uses user supplied information, such as list of exported symbol.
190LLVM optimizer collects control flow information, data flow information and
191knows much more about program structure from optimizer's point of view. Our
192goal is to take advantage of tight intergration between the linker and
193optimizer 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>
204The linker first reads all object files in natural order and collects symbol
205information. This includes native object files as well as LLVM byte code files.
206In this phase, the linker uses <a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>
207to collect symbol information from each LLVM bytecode files and updates its
208internal global symbol table accordingly. The intent of this interface is to
209avoid overhead in the non LLVM case, where all input object files are native
210object files, by putting this code in the error path of the linker. When the
211linker sees the first llvm .o file, it dlopen()s the dynamic library. This is
212to 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>
223In this stage, the linker resolves symbols using global symbol table information
224to report undefined symbol errors, read archive members, resolve weak
225symbols etc... The linker is able to do this seamlessly even though it does not
226know exact content of input LLVM bytecode files because it uses symbol information
227provided by <a href=#lreadllvmbytecodefile> readLLVMByteCodeFile() </a>.
228If 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>
238After symbol resolution, the linker updates symbol information supplied by LLVM
239bytecode files appropriately. For example, whether certain LLVM bytecode
240supplied symbols are used or not. In the example above, the linker reports
241that <tt>foo2()</tt> is not used anywhere in the program, including native .o
242files. This information is used by LLVM interprocedural optimizer. The
243linker uses <a href="#optimizemodules"> optimizeModules()</a> and requests
244optimized 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>
255In this phase, the linker reads optimized native object file and updates internal
256global symbol table to reflect any changes. Linker also collects information
257about any change in use of external symbols by LLVM bytecode files. In the examle
258above, the linker notes that <tt>foo4()</tt> is not used any more. If dead code
259striping is enabled then linker refreshes live symbol information appropriately
260and performs dead code stripping.
261<br>
262After this phase, the linker continues linking as if it never saw LLVM bytecode
263files.
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
275intended for use by a linker. <tt>LLVMlto</tt> provides an abstract C++ interface
276to use the LLVM interprocedural optimizer without exposing details of LLVM
277internals. The intention is to keep the interface as stable as possible even
278when 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
290and global variables, tdefined in LLVM bytecode files, to linker.
291This includes symbol visibility information. This information is used by linker
292to do symbol resolution. For example : function <tt>foo2()</tt> is defined inside
293a LLVM bytecode module and it is externally visible symbol.
294This helps linker connect use of <tt>foo2()</tt> in native object file with
295future definition of symbol <tt>foo2()</tt>. The linker will see actual definition
296of <tt>foo2()</tt> when it receives optimized native object file in <a href="#phase4">
297Symbol Resolution after optimization</a> phase. If the linker does not find any
298use of <tt>foo2()</tt>, it updates LLVMSymbol visibility information to notify
299LLVM intermodular optimizer that it is dead. The LLVM intermodular optimizer
300takes 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
312and collect LLVMSymbol nformation. This routine also
313supplies list of externally defined symbols that are used by LLVM bytecode
314files. Linker uses this symbol information to do symbol resolution. Internally,
315<a href="#lto">LLVMlto</a> maintains LLVM bytecode modules in memory. This
316function 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>
327The linker invokes <tt>optimizeModules</tt> to optimize already read LLVM
328bytecode files by applying LLVM intermodular optimization techniques. This
329function runs LLVM intermodular optimizer and generates native object code
330as .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>