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