blob: 6c30fec965b70e82144bc12bc758336dfa4e1453 [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
Gabor Greif04367bf2007-07-06 22:07:22 +000070treates LLVM bitcode files like native object files and allows mixing and
Reid Spencerb5fc9f52006-08-14 19:19:55 +000071matching among them. The linker uses <a href="#lto">LLVMlto</a>, a dynamically
Gabor Greif04367bf2007-07-06 22:07:22 +000072loaded library, to handle LLVM bitcode files. This tight integration between
Reid Spencerb5fc9f52006-08-14 19:19:55 +000073the 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">
Duncan Sands6db928a2007-04-07 17:43:25 +000085 <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,
Tanya Lattner5efa7e92007-08-24 23:23:23 +000088 llvm-gcc transparently invokes system linker. </p>
Reid Spencerb5fc9f52006-08-14 19:19:55 +000089 <ul>
Gabor Greif04367bf2007-07-06 22:07:22 +000090 <li> Input source file <tt>a.c</tt> is compiled into LLVM bitcode form.
Reid Spencerb5fc9f52006-08-14 19:19:55 +000091 <li> Input source file <tt>main.c</tt> is compiled into native object code.
92 </ul>
Devang Patel682f6752006-10-27 23:06:27 +000093<div class="doc_code"><pre>
Devang Patel2c1292f2006-08-14 18:39:35 +000094--- a.h ---
Reid Spencerb5fc9f52006-08-14 19:19:55 +000095extern int foo1(void);
96extern void foo2(void);
97extern void foo4(void);
98--- a.c ---
99#include "a.h"
100
101static signed int i = 0;
102
103void foo2(void) {
104 i = -1;
105}
106
107static int foo3() {
108foo4();
109return 10;
110}
111
112int foo1(void) {
113int data = 0;
114
115if (i &lt; 0) { data = foo3(); }
116
117data = data + 42;
118return data;
119}
120
121--- main.c ---
122#include &lt;stdio.h&gt;
123#include "a.h"
124
125void foo4(void) {
126 printf ("Hi\n");
127}
128
129int main() {
130 return foo1();
131}
132
133--- command lines ---
Tanya Lattner5efa7e92007-08-24 23:23:23 +0000134$ llvm-gcc --emit-llvm -c a.c -o a.o # &lt;-- a.o is LLVM bitcode file
135$ llvm-gcc -c main.c -o main.o # &lt;-- main.o is native object file
136$ llvm-gcc a.o main.o -o main # &lt;-- standard link command without any modifications
Devang Patel682f6752006-10-27 23:06:27 +0000137</pre></div>
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000138 <p>In this example, the linker recognizes that <tt>foo2()</tt> is an
Gabor Greif04367bf2007-07-06 22:07:22 +0000139 externally visible symbol defined in LLVM bitcode file. This information
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000140 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 &lt; 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.
150 </p>
Devang Patel93449f12006-08-14 18:03:40 +0000151</div>
152
153<!-- ======================================================================= -->
154<div class="doc_subsection">
155 <a name="alternative_approaches">Alternative Approaches</a>
156</div>
157
158<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000159 <dl>
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
167 files.</b></dt>
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.
180 </dd>
181 </dl>
Devang Patel93449f12006-08-14 18:03:40 +0000182</div>
183
184<!-- *********************************************************************** -->
185<div class="doc_section">
186 <a name="multiphase">Multi-phase communication between LLVM and linker</a>
187</div>
188
189<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000190 <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.
Devang Patel93449f12006-08-14 18:03:40 +0000200</p>
201</div>
202
203<!-- ======================================================================= -->
204<div class="doc_subsection">
Gabor Greif04367bf2007-07-06 22:07:22 +0000205 <a name="phase1">Phase 1 : Read LLVM Bitcode Files</a>
Devang Patel93449f12006-08-14 18:03:40 +0000206</div>
207
208<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000209 <p>The linker first reads all object files in natural order and collects
Gabor Greif04367bf2007-07-06 22:07:22 +0000210 symbol information. This includes native object files as well as LLVM bitcode
211 files. In this phase, the linker uses
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000212 <a href="#readllvmobjectfile"> readLLVMObjectFile() </a> to collect symbol
Gabor Greif04367bf2007-07-06 22:07:22 +0000213 information from each LLVM bitcode files and updates its internal global
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000214 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.
Devang Patel93449f12006-08-14 18:03:40 +0000219</p>
220</div>
221
222<!-- ======================================================================= -->
223<div class="doc_subsection">
224 <a name="phase2">Phase 2 : Symbol Resolution</a>
225</div>
226
227<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000228 <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
Gabor Greif04367bf2007-07-06 22:07:22 +0000231 does not know the exact content of input LLVM bitcode files because it uses
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000232 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.
235 </p>
Devang Patel93449f12006-08-14 18:03:40 +0000236</div>
237
238<!-- ======================================================================= -->
239<div class="doc_subsection">
Gabor Greif04367bf2007-07-06 22:07:22 +0000240 <a name="phase3">Phase 3 : Optimize Bitcode Files</a>
Devang Patel93449f12006-08-14 18:03:40 +0000241</div>
242<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000243 <p>After symbol resolution, the linker updates symbol information supplied
Gabor Greif04367bf2007-07-06 22:07:22 +0000244 by LLVM bitcode files appropriately. For example, whether certain LLVM
245 bitcode supplied symbols are used or not. In the example above, the linker
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000246 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
250 program.
Devang Patel93449f12006-08-14 18:03:40 +0000251</p>
252</div>
253
254<!-- ======================================================================= -->
255<div class="doc_subsection">
256 <a name="phase4">Phase 4 : Symbol Resolution after optimization</a>
257</div>
258
259<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000260 <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
Gabor Greif04367bf2007-07-06 22:07:22 +0000263 LLVM bitcode files. In the examle above, the linker notes that
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000264 <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
Gabor Greif04367bf2007-07-06 22:07:22 +0000268 bitcode files.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000269</div>
270
271<!-- *********************************************************************** -->
272<div class="doc_section">
273<a name="lto">LLVMlto</a>
274</div>
275
276<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000277 <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>
Devang Patel93449f12006-08-14 18:03:40 +0000282</div>
283
284<!-- ======================================================================= -->
285<div class="doc_subsection">
286 <a name="llvmsymbol">LLVMSymbol</a>
287</div>
288
289<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000290 <p>The <tt>LLVMSymbol</tt> class is used to describe the externally visible
Gabor Greif04367bf2007-07-06 22:07:22 +0000291 functions and global variables, defined in LLVM bitcode files, to the linker.
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000292 This includes symbol visibility information. This information is used by
293 the linker to do symbol resolution. For example: function <tt>foo2()</tt> is
Gabor Greif04367bf2007-07-06 22:07:22 +0000294 defined inside an LLVM bitcode module and it is an externally visible symbol.
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000295 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>
Devang Patel93449f12006-08-14 18:03:40 +0000304</div>
305
306<!-- ======================================================================= -->
307<div class="doc_subsection">
308 <a name="readllvmobjectfile">readLLVMObjectFile()</a>
309</div>
310
311<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000312 <p>The <tt>readLLVMObjectFile()</tt> function is used by the linker to read
Gabor Greif04367bf2007-07-06 22:07:22 +0000313 LLVM bitcode files and collect LLVMSymbol information. This routine also
314 supplies a list of externally defined symbols that are used by LLVM bitcode
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000315 files. The linker uses this symbol information to do symbol resolution.
Gabor Greif04367bf2007-07-06 22:07:22 +0000316 Internally, <a href="#lto">LLVMlto</a> maintains LLVM bitcode modules in
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000317 memory. This function also provides a list of external references used by
Gabor Greif04367bf2007-07-06 22:07:22 +0000318 bitcode files.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000319</div>
320
321<!-- ======================================================================= -->
322<div class="doc_subsection">
323 <a name="optimizemodules">optimizeModules()</a>
324</div>
325
326<div class="doc_text">
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000327 <p>The linker invokes <tt>optimizeModules</tt> to optimize already read
Gabor Greif04367bf2007-07-06 22:07:22 +0000328 LLVM bitcode files by applying LLVM intermodular optimization techniques.
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000329 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
331 linker.</p>
Devang Patel93449f12006-08-14 18:03:40 +0000332</div>
333
Devang Patelc1e6e132006-09-06 20:22:55 +0000334<!-- ======================================================================= -->
335<div class="doc_subsection">
336 <a name="gettargettriple">getTargetTriple()</a>
337</div>
338
339<div class="doc_text">
340 <p>The linker may use <tt>getTargetTriple()</tt> to query target architecture
Gabor Greif04367bf2007-07-06 22:07:22 +0000341 while validating LLVM bitcode file.</p>
Devang Patelc1e6e132006-09-06 20:22:55 +0000342</div>
343
Devang Patel9286cd32006-10-27 21:58:31 +0000344<!-- ======================================================================= -->
345<div class="doc_subsection">
Devang Patelca403272006-10-27 22:02:30 +0000346 <a name="removemodule">removeModule()</a>
347</div>
348
349<div class="doc_text">
Gabor Greif04367bf2007-07-06 22:07:22 +0000350 <p>Internally, <a href="#lto">LLVMlto</a> maintains LLVM bitcode modules in
Devang Patelca403272006-10-27 22:02:30 +0000351 memory. The linker may use <tt>removeModule()</tt> method to remove desired
352 modules from memory. </p>
353</div>
354
355<!-- ======================================================================= -->
356<div class="doc_subsection">
Devang Patel9286cd32006-10-27 21:58:31 +0000357 <a name="getalignment">getAlignment()</a>
358</div>
359
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>
363</div>
364
Devang Patel93449f12006-08-14 18:03:40 +0000365<!-- *********************************************************************** -->
366<div class="doc_section">
367 <a name="debug">Debugging Information</a>
368</div>
369<!-- *********************************************************************** -->
370
371<div class="doc_text">
372
Reid Spencerb5fc9f52006-08-14 19:19:55 +0000373<p><tt> ... To be completed ... </tt></p>
Devang Patel93449f12006-08-14 18:03:40 +0000374
375</div>
376
377<!-- *********************************************************************** -->
378
379<hr>
380<address>
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>
385
Devang Patel2c1292f2006-08-14 18:39:35 +0000386 Devang Patel<br>
Devang Patel93449f12006-08-14 18:03:40 +0000387 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
388 Last modified: $Date$
389</address>
390
391</body>
392</html>
Nick Kledzik55998ba2008-02-26 01:36:52 +0000393