+
+ <p>You can produce bitcode files from <tt>clang</tt> using
+ <tt>-emit-llvm</tt> or <tt>-flto</tt>, or the <tt>-O4</tt> flag which is
+ synonymous with <tt>-O3 -flto</tt>.</p>
+
+ <p><tt>Clang</tt> has a <tt>-use-gold-plugin</tt> option which looks for the
+ gold plugin in the same directories as it looks for <tt>cc1</tt> and passes
+ the <tt>-plugin</tt> option to <tt>ld</tt>. It will not look for an alternate
+ linker, which is why you need gold to be the installed system linker in your
+ path.</p>
+
+ <p>If you want <tt>ar</tt> and <tt>nm</tt> to work seamlessly as well, install
+ <tt>LLVMgold.so</tt> to <tt>/usr/lib/bfd-plugins</tt>. If you built your
+ own gold, be sure to install the <tt>ar</tt> and <tt>nm-new</tt> you built to
+ <tt>/usr/bin</tt>.<p>
+
+<!-- ======================================================================= -->
+<h3>
+ <a name="example1">Example of link time optimization</a>
+</h3>
+
+<div>
+ <p>The following example shows a worked example of the gold plugin mixing
+ LLVM bitcode and native code.
+<pre class="doc_code">
+--- a.c ---
+#include <stdio.h>
+
+extern void foo1(void);
+extern void foo4(void);
+
+void foo2(void) {
+ printf("Foo2\n");
+}
+
+void foo3(void) {
+ foo4();
+}
+
+int main(void) {
+ foo1();
+}
+
+--- b.c ---
+#include <stdio.h>
+
+extern void foo2(void);
+
+void foo1(void) {
+ foo2();
+}
+
+void foo4(void) {
+ printf("Foo4");
+}
+
+--- command lines ---
+$ clang -flto a.c -c -o a.o # <-- a.o is LLVM bitcode file
+$ ar q a.a a.o # <-- a.a is an archive with LLVM bitcode
+$ clang b.c -c -o b.o # <-- b.o is native object file
+$ clang -use-gold-plugin a.a b.o -o main # <-- link with LLVMgold plugin
+</pre>
+
+ <p>Gold informs the plugin that foo3 is never referenced outside the IR,
+ leading LLVM to delete that function. However, unlike in the
+ <a href="LinkTimeOptimization.html#example1">libLTO
+ example</a> gold does not currently eliminate foo4.</p>
+</div>
+
+</div>
+
+<!--=========================================================================-->
+<h2>
+ <a name="lto_autotools">
+ Quickstart for using LTO with autotooled projects
+ </a>
+</h2>
+<!--=========================================================================-->
+<div>
+ <p>Once your system <tt>ld</tt>, <tt>ar</tt>, and <tt>nm</tt> all support LLVM
+ bitcode, everything is in place for an easy to use LTO build of autotooled
+ projects:</p>
+
+ <ul>
+ <li>Follow the instructions <a href="#build">on how to build LLVMgold.so</a>.</li>
+ <li>Install the newly built binutils to <tt>$PREFIX</tt></li>
+ <li>Copy <tt>Release/lib/LLVMgold.so</tt> to
+ <tt>$PREFIX/libexec/gcc/x86_64-unknown-linux-gnu/4.2.1/</tt> and
+ <tt>$PREFIX/lib/bfd-plugins/</tt></li>
+ <li>Set environment variables (<tt>$PREFIX</tt> is where you installed clang and
+ binutils):
+<pre class="doc_code">
+export CC="$PREFIX/bin/clang -use-gold-plugin"
+export CXX="$PREFIX/bin/clang++ -use-gold-plugin"
+export AR="$PREFIX/bin/ar"
+export NM="$PREFIX/bin/nm"
+export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a
+export CFLAGS="-O4"
+</pre>
+ </li>
+ <li>Or you can just set your path:
+<pre class="doc_code">
+export PATH="$PREFIX/bin:$PATH"
+export CC="clang -use-gold-plugin"
+export CXX="clang++ -use-gold-plugin"
+export RANLIB=/bin/true
+export CFLAGS="-O4"
+</pre></li>
+ <li>Configure & build the project as usual:
+<pre class="doc_code">
+% ./configure && make && make check
+</pre></li>
+ </ul>
+
+ <p>The environment variable settings may work for non-autotooled projects
+ too, but you may need to set the <tt>LD</tt> environment variable as
+ well.</p>