1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM Link Time Optimization: Design and Implementation
</title>
6 <link rel=
"stylesheet" href=
"llvm.css" type=
"text/css">
9 <div class=
"doc_title">
10 LLVM Link Time Optimization: Design and Implementation
14 <li><a href=
"#desc">Description
</a></li>
15 <li><a href=
"#design">Design Philosophy
</a>
17 <li><a href=
"#example1">Example of link time optimization
</a></li>
18 <li><a href=
"#alternative_approaches">Alternative Approaches
</a></li>
20 <li><a href=
"#multiphase">Multi-phase communication between LLVM and linker
</a>
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>
27 <li><a href=
"#lto">LLVMlto
</a>
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>
36 <li><a href=
"#debug">Debugging Information
</a></li>
39 <div class=
"doc_author">
40 <p>Written by Devang Patel
</p>
43 <!-- *********************************************************************** -->
44 <div class=
"doc_section">
45 <a name=
"desc">Description
</a>
47 <!-- *********************************************************************** -->
49 <div class=
"doc_text">
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>
57 <!-- *********************************************************************** -->
58 <div class=
"doc_section">
59 <a name=
"design">Design Philosophy
</a>
61 <!-- *********************************************************************** -->
63 <div class=
"doc_text">
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.
79 <!-- ======================================================================= -->
80 <div class=
"doc_subsection">
81 <a name=
"example1">Example of link time optimization
</a>
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>
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.
93 <div class=
"doc_code"><pre>
95 extern int foo1(void);
96 extern void foo2(void);
97 extern void foo4(void);
101 static signed int i =
0;
115 if (i
< 0) { data = foo3(); }
122 #include
<stdio.h
>
133 --- command lines ---
134 $ llvm-gcc --emit-llvm -c a.c -o a.o #
<-- a.o is LLVM bitcode file
135 $ llvm-gcc -c main.c -o main.o #
<-- main.o is native object file
136 $ llvm-gcc a.o main.o -o main #
<-- standard link command without any modifications
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
< 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.
153 <!-- ======================================================================= -->
154 <div class=
"doc_subsection">
155 <a name=
"alternative_approaches">Alternative Approaches
</a>
158 <div class=
"doc_text">
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
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.
184 <!-- *********************************************************************** -->
185 <div class=
"doc_section">
186 <a name=
"multiphase">Multi-phase communication between LLVM and linker
</a>
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.
203 <!-- ======================================================================= -->
204 <div class=
"doc_subsection">
205 <a name=
"phase1">Phase
1 : Read LLVM Bitcode Files
</a>
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.
222 <!-- ======================================================================= -->
223 <div class=
"doc_subsection">
224 <a name=
"phase2">Phase
2 : Symbol Resolution
</a>
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.
238 <!-- ======================================================================= -->
239 <div class=
"doc_subsection">
240 <a name=
"phase3">Phase
3 : Optimize Bitcode Files
</a>
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
254 <!-- ======================================================================= -->
255 <div class=
"doc_subsection">
256 <a name=
"phase4">Phase
4 : Symbol Resolution after optimization
</a>
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
271 <!-- *********************************************************************** -->
272 <div class=
"doc_section">
273 <a name=
"lto">LLVMlto
</a>
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>
284 <!-- ======================================================================= -->
285 <div class=
"doc_subsection">
286 <a name=
"llvmsymbol">LLVMSymbol
</a>
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>
306 <!-- ======================================================================= -->
307 <div class=
"doc_subsection">
308 <a name=
"readllvmobjectfile">readLLVMObjectFile()
</a>
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
321 <!-- ======================================================================= -->
322 <div class=
"doc_subsection">
323 <a name=
"optimizemodules">optimizeModules()
</a>
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
334 <!-- ======================================================================= -->
335 <div class=
"doc_subsection">
336 <a name=
"gettargettriple">getTargetTriple()
</a>
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>
344 <!-- ======================================================================= -->
345 <div class=
"doc_subsection">
346 <a name=
"removemodule">removeModule()
</a>
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>
355 <!-- ======================================================================= -->
356 <div class=
"doc_subsection">
357 <a name=
"getalignment">getAlignment()
</a>
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>
365 <!-- *********************************************************************** -->
366 <div class=
"doc_section">
367 <a name=
"debug">Debugging Information
</a>
369 <!-- *********************************************************************** -->
371 <div class=
"doc_text">
373 <p><tt> ... To be completed ...
</tt></p>
377 <!-- *********************************************************************** -->
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>
387 <a href=
"http://llvm.org">LLVM Compiler Infrastructure
</a><br>
388 Last modified: $Date$