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