1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>Getting Started with LLVM System</title>
8 <center><h1>Getting Started with the LLVM System<br><font size=3>By: <a
9 href="mailto:gshi1@uiuc.edu">Guochun Shi</a>,
10 <a href="mailto:sabre@nondot.org">Chris Lattner</a> and
11 <a href="http://www.cs.uiuc.edu/~vadve">Vikram Adve</a>
14 <!--=====================================================================-->
15 <h2><a name="Contents">Contents</a></h2>
16 <!--=====================================================================-->
19 <li><a href="#overview">Overview</a>
20 <li><a href="#starting">Getting started with LLVM</a>
22 <li><a href="#requirements">Requirements</a>
24 <li><a href="#hardware">Hardware</a>
25 <li><a href="#software">Software</a>
27 <li><a href="#quickstart">Getting started quickly (a summary)</a>
28 <li><a href="#checkout">Checkout LLVM from CVS</a>
29 <li><a href="#terminology">Terminology and Notation</tt></a>
30 <li><a href="#objfiles">The location for object files</tt></a>
31 <li><a href="#config">Local Configuration Options</tt></a>
32 <li><a href="#environment">Setting up your environment</a>
33 <li><a href="#compile">Compiling the source code</a>
35 <li><a href="#layout">Program layout</a>
37 <li><a href="#cvsdir">CVS directories</a>
38 <li><a href="#dd"><tt>Depend</tt>, <tt>Debug</tt>, &
39 <tt>Release</tt> directories</a></li>
40 <li><a href="#include"><tt>llvm/include</tt></a>
41 <li><a href="#lib"><tt>llvm/lib</tt></a>
42 <li><a href="#test"><tt>llvm/test</tt></a>
43 <li><a href="#tools"><tt>llvm/tools</tt></a>
45 <li><a href="#tutorial">An example using the LLVM tool chain</a>
46 <li><a href="#links">Links</a>
50 <!--=====================================================================-->
52 <h2><a name="overview"><b>Overview</b></a></h2>
54 <!--=====================================================================-->
56 <p>The <a href"starting">next section</a> of this guide is meant to get
57 you up and running with LLVM and to give you some basic information about
58 the LLVM environment. The <a href"#quickstart">first subsection</a> gives
59 a short summary for those who are already familiar with the system and
60 want to get started as quickly as possible.
62 <p>The later sections of this guide describe the <a
63 href"#layout">general layout</a> of the the LLVM source-tree, a <a
64 href="#tutorial">simple example</a> using the LLVM tool chain, and <a
65 href="#links">links</a> to find more information about LLVM or to get
68 <!--=====================================================================-->
70 <h2><a name="starting"><b>Getting Started</b></a></h2>
72 <!--=====================================================================-->
74 <!--=====================================================================-->
75 <h3><a name="requirements"><b>Requirements</b></a></h3>
76 <!--=====================================================================-->
78 <!--=====================================================================-->
79 <h4><a name="hardware"><b>Hardware</b></a></h4>
80 <!--=====================================================================-->
81 LLVM is known to work on the following platforms:
85 <li> Approximately 700 MB of Free Disk Space
87 <li>Source code: 30 MB
88 <li>Object code: 670 MB
93 <li> Approximately 1.03 GB of Free Disk Space
95 <li>Source code: 30 MB
96 <li>Object code: 1000 MB
101 LLVM may compile on other platforms. While the LLVM utilities should work,
102 they will only generate Sparc or x86 machine code.
104 <!--=====================================================================-->
105 <h4><a name="software"><b>Software</b></a></h4>
106 <!--=====================================================================-->
108 Compiling LLVM requires that you have several different software packages
114 The GNU Compiler Collection must be installed with C and C++ language
115 support. GCC 3.x is supported, although some effort has been made to
120 Note that we currently do not support any other C++ compiler.
125 The LLVM build system relies upon GNU Make extensions. Therefore, you
126 will need GNU Make (sometimes known as gmake) to build LLVM.
131 The LLVM source code is built using flex and bison. You will not be
132 able to configure and compile LLVM without them.
137 There are some additional tools that you may want to have when working with
142 <li> GNU Autoconf and GNU M4
144 If you want to make changes to the autoconf scripts which configure LLVM
145 for compilation, you will need GNU autoconf, and consequently, GNU M4.
146 LLVM was built with autoconf 2.53, so that release and any later
151 <!--=====================================================================-->
152 <h3><a name="quickstart"><b>Getting Started Quickly (A Summary)</b></a></h3>
153 <!--=====================================================================-->
155 Here's the short story for getting up and running quickly with LLVM:
157 <li>Find the path to the CVS repository containing LLVM (we'll call this <i>CVSROOTDIR</i>).
158 <li><tt>cd <i>where-you-want-llvm-to-live</i></tt>
159 <li><tt>cvs -d <i>CVSROOTDIR</i> checkout llvm</tt>
161 <li>Run <tt>configure</tt> to configure the Makefiles and header files.
162 Useful options include:
164 <li><tt>--with-objroot=<i>directory</i></tt>
166 Specifiy where object files should be placed during the build.
168 <li><tt>--with-llvmgccdir=<i>directory</i></tt>
170 Specifiy where the LLVM C frontend has been installed.
172 <li>Set your LLVM_LIB_SEARCH_PATH environment variable.
173 <li><tt>gmake -k |& tee gnumake.out
174 # this is csh or tcsh syntax</tt>
177 <p>See <a href="#environment">Setting up your environment</a> on tips to
178 simplify working with the LLVM front-end and compiled tools. See the
179 other sub-sections below for other useful details in working with LLVM,
180 or go straight to <a href="#layout">Program Layout</a> to learn about the
181 layout of the source code tree.
183 <!------------------------------------------------------------------------->
184 <h3><a name="terminology">Terminology and Notation</a></h3>
185 <!------------------------------------------------------------------------->
187 <p>Throughout this manual, the following names are used to denote paths
188 specific to the local system and working environment. <i>These are not
189 environment variables you need to set but just strings used in the rest
190 of this document below</i>. In any of the examples below, simply replace
191 each of these names with the appropriate pathname on your local system.
192 All these paths are absolute:</p>
196 <!------------------------------------------------------------------------->
197 <h3><a name="checkout">Checkout LLVM from CVS</a></h3>
198 <!------------------------------------------------------------------------->
200 <p>Before checking out the source code, you will need to know the path to
201 the CVS repository containing the LLVM source code (we'll call this
202 <i>CVSROOTDIR</i> below). Ask the person responsible for your local LLVM
203 installation to give you this path.
205 <p>To get a fresh copy of the entire source code, all you
206 need to do is check it out from CVS as follows:
208 <li><tt>cd <i>where-you-want-llvm-to-live</i></tt>
209 <li><tt>cvs -d <i>CVSROOTDIR</i> checkout llvm</tt></p>
212 <p>This will create an '<tt>llvm</tt>' directory in the current
213 directory and fully populate it with the LLVM source code, Makefiles,
214 test directories, and local copies of documentation files.</p>
216 <!------------------------------------------------------------------------->
217 <h3><a name="config">Local Configuration Options</a></h3>
218 <!------------------------------------------------------------------------->
220 <p>Once checked out from the CVS repository, options and pathnames specific
221 to an installation of LLVM can be set via the <tt>configure</tt> script.
222 This script sets variables in <tt>llvm/Makefile.config</tt> and
223 <tt>llvm/include/Config/config.h</tt>.
226 The following environment variables are used by <tt>configure</tt> to
227 configure Makefile.config:
231 <p><li><i>CXX</i> = Pathname of the C++ compiler to use.
232 <p><li><i>CC</i> = Pathname of the C compiler to use.
235 The following options can be used to set or enable LLVM specific options:
238 <p><li><i>--with-objroot=LLVM_OBJ_ROOT</i> =
239 Path to the directory where
240 object files, libraries, and executables should be placed.
241 (See the Section on <a href=#objfiles>
242 The location for LLVM object files</a>
243 for more information.)
244 <p><li><i>--with-llvmgccdir=LLVMGCCDIR</i> =
245 Path to the location of the LLVM front-end
246 binaries and associated libraries.
247 <p><li><i>--enable-optimized</i> =
248 Enables optimized compilation (debugging symbols are removed and GCC
249 optimization flags are enabled).
250 <p><li><i>--enable-jit</i> =
251 Compile the Just In Time (JIT) functionality. This is not available
255 In addition to running <tt>configure</tt>, you must set the
256 <tt>LLVM_LIB_SEARCH_PATH</tt> environment variable in your startup scripts.
257 This environment variable is used to locate "system" libraries like
258 "<tt>-lc</tt>" and "<tt>-lm</tt>" when linking. This variable should be set
259 to the absolute path for the bytecode-libs subdirectory of the C front-end
260 install. For example, one might set <tt>LLVM_LIB_SEARCH_PATH</tt> to
261 <tt>/home/vadve/lattner/local/x86/llvm-gcc/bytecode-libs</tt> for the X86
262 version of the C front-end on our research machines.<p>
264 <!------------------------------------------------------------------------->
265 <h3><a name="objfiles">The location for LLVM object files</a></h3>
266 <!------------------------------------------------------------------------->
268 <p>The LLVM make system sends most output files generated during the build
269 into the directory defined by the variable OBJ_ROOT in
270 <tt>llvm/Makefile.config</tt>. This can be either just your normal LLVM
271 source tree or some other directory writable by you. You may wish to put
272 object files on a different filesystem either to keep them from being backed
273 up or to speed up local builds.
275 <p>If you wish to place output files into a separate directory, use the
276 <tt>--with-objroot=<i>directory</i></tt> option of <tt>configure</tt> to
277 set the top level directory of where the object files will go. Otherwise,
278 leave this option unspecified, and <tt>configure</tt> will place files
279 within the LLVM source tree.
281 <!------------------------------------------------------------------------->
282 <h3><a name="environment">Setting up your environment</a></h3>
283 <!------------------------------------------------------------------------->
285 <i>NOTE: This step is optional but will set up your environment so you
286 can use the compiled LLVM tools with as little hassle as
289 <p>Add the following lines to your <tt>.cshrc</tt> (or the corresponding
290 lines to your <tt>.profile</tt> if you use a bourne shell derivative).
293 # Make the C front end easy to use...
294 alias llvmgcc <i>LLVMGCCDIR</i><tt>/bin/llvm-gcc</tt>
296 # Make the LLVM tools easy to use...
297 setenv PATH <i>OBJ_ROOT</i>/llvm/tools/Debug:${PATH}
299 The <tt>llvmgcc</tt> alias is useful because the C compiler is not
300 included in the CVS tree you just checked out.
302 <p>The other <a href="#tools">LLVM tools</a> are part of the LLVM
303 source base and built when compiling LLVM. They will be built into the
304 <tt><i>OBJ_ROOT</i>/tools/Debug</tt> directory.</p>
306 <!------------------------------------------------------------------------->
307 <h3><a name="compile">Compiling the source code</a></h3>
308 <!------------------------------------------------------------------------->
310 <p>Every directory in the LLVM source tree includes a <tt>Makefile</tt> to
311 build it and any subdirectories that it contains. These makefiles require
312 that you use GNU Make (sometimes called <tt>gmake</tt>) instead of
315 otherwise be used freely. To build the entire LLVM system, just enter the
316 top level <tt>llvm</tt> directory and type <tt>gmake</tt>. A few minutes
317 later you will hopefully have a freshly compiled toolchain waiting for you
318 in <tt><i>OBJ_ROOT</i>/llvm/tools/Debug</tt>. If you want to look at the
320 were compiled, look in <tt><i>OBJ_ROOT</i>/llvm/lib/Debug</tt>.</p>
322 If you get an error about a <tt>/localhome</tt> directory, follow the
323 instructions in the section about <a href="#environment">Setting Up Your
328 <!--=====================================================================-->
330 <h2><a name="layout"><b>Program Layout</b></a></h2>
332 <!--=====================================================================-->
334 <p>One useful source of infomation about the LLVM sourcebase is the LLVM <a
335 href="http://www.doxygen.org">doxygen</a> documentation, available at <tt><a
336 href="http://llvm.cs.uiuc.edu/doxygen/">http://llvm.cs.uiuc.edu/doxygen/</a></tt>. The
337 following is a brief introduction to code layout:</p>
340 <!------------------------------------------------------------------------->
341 <h3><a name="cvsdir"><tt>CVS</tt> directories</a></h3>
342 <!------------------------------------------------------------------------->
344 Every directory checked out of CVS will contain a <tt>CVS</tt> directory;
345 for the most part these can just be ignored.
348 <!------------------------------------------------------------------------->
349 <h3><a name="ddr"><tt>Depend</tt>, <tt>Debug</tt>, & <tt>Release</tt>
351 <!------------------------------------------------------------------------->
353 If you are building with the "<tt>OBJ_ROOT=.</tt>" option enabled in the
354 <tt>Makefile.config</tt> file (i.e. you did not specify
355 <tt>--with-objroot</tt> when you ran <tt>configure</tt>), most source
356 directories will contain two
357 directories, <tt>Depend</tt> and <tt>Debug</tt>. The <tt>Depend</tt>
358 directory contains automatically generated dependance files which are used
359 during compilation to make sure that source files get rebuilt if a header
360 file they use is modified. The <tt>Debug</tt> directory holds the object
361 files, library files, and executables that are used for building a debug
362 enabled build. The <tt>Release</tt> directory is created to hold the same
363 files when the <tt>ENABLE_OPTIMIZED=1</tt> flag is passed to <tt>gmake</tt>,
364 causing an optimized build to be performed.<p>
367 <!------------------------------------------------------------------------->
368 <h3><a name="include"><tt>llvm/include</tt></a></h3>
369 <!------------------------------------------------------------------------->
371 This directory contains public header files exported from the LLVM
372 library. The three main subdirectories of this directory are:<p>
375 <li><tt>llvm/include/llvm</tt> - This directory contains all of the LLVM
376 specific header files. This directory also has subdirectories for
377 different portions of LLVM: <tt>Analysis</tt>, <tt>CodeGen</tt>,
378 <tt>Reoptimizer</tt>, <tt>Target</tt>, <tt>Transforms</tt>, etc...
380 <li><tt>llvm/include/Support</tt> - This directory contains generic
381 support libraries that are independant of LLVM, but are used by LLVM.
382 For example, some C++ STL utilities and a Command Line option processing
385 <li><tt>llvm/include/Config</tt> - This directory contains header files
386 configured by the <tt>configure</tt> script. They wrap "standard" UNIX
387 and C header files. Source code can include these header files which
388 automatically take care of the conditional #includes that the configure
392 <!------------------------------------------------------------------------->
393 <h3><a name="lib"><tt>llvm/lib</tt></a></h3>
394 <!------------------------------------------------------------------------->
396 This directory contains most of the source files of the LLVM system. In
398 code exists in libraries, making it very easy to share code among the
399 different <a href="#tools">tools</a>.<p>
402 <dt><tt>llvm/lib/VMCore/</tt><dd> This directory holds the core LLVM
403 source files that implement core classes like Instruction and BasicBlock.
405 <dt><tt>llvm/lib/AsmParser/</tt><dd> This directory holds the source code
406 for the LLVM assembly language parser library.
408 <dt><tt>llvm/lib/ByteCode/</tt><dd> This directory holds code for reading
409 and write LLVM bytecode.
411 <dt><tt>llvm/lib/CWriter/</tt><dd> This directory implements the LLVM to C
414 <dt><tt>llvm/lib/Analysis/</tt><dd> This directory contains a variety of
415 different program analyses, such as Dominator Information, Call Graphs,
416 Induction Variables, Interval Identification, Natural Loop Identification,
419 <dt><tt>llvm/lib/Transforms/</tt><dd> This directory contains the source
420 code for the LLVM to LLVM program transformations, such as Aggressive Dead
421 Code Elimination, Sparse Conditional Constant Propagation, Inlining, Loop
422 Invarient Code Motion, Dead Global Elimination, Pool Allocation, and many
425 <dt><tt>llvm/lib/Target/</tt><dd> This directory contains files that
426 describe various target architectures for code generation. For example,
427 the llvm/lib/Target/Sparc directory holds the Sparc machine
430 <dt><tt>llvm/lib/CodeGen/</tt><dd> This directory contains the major parts
431 of the code generator: Instruction Selector, Instruction Scheduling, and
434 <dt><tt>llvm/lib/Reoptimizer/</tt><dd> This directory holds code related
435 to the runtime reoptimizer framework that is currently under development.
437 <dt><tt>llvm/lib/Support/</tt><dd> This directory contains the source code
438 that corresponds to the header files located in
439 <tt>llvm/include/Support/</tt>.
442 <!------------------------------------------------------------------------->
443 <h3><a name="test"><tt>llvm/test</tt></a></h3>
444 <!------------------------------------------------------------------------->
446 <p>This directory contains regression tests and source code that is used to
447 test the LLVM infrastructure...</p>
449 <!------------------------------------------------------------------------->
450 <h3><a name="tools"><tt>llvm/tools</tt></a></h3>
451 <!------------------------------------------------------------------------->
453 <p>The <b>tools</b> directory contains the executables built out of the
454 libraries above, which form the main part of the user interface. You can
455 always get help for a tool by typing <tt>tool_name --help</tt>. The
456 following is a brief introduction to the most important tools.</p>
459 <dt><tt><b>as</b></tt><dd>The assembler transforms the human readable
460 LLVM assembly to LLVM bytecode.<p>
462 <dt><tt><b>dis</b></tt><dd>The disassembler transforms the LLVM bytecode
463 to human readable LLVM assembly. Additionally it can convert LLVM
464 bytecode to C, which is enabled with the <tt>-c</tt> option.<p>
466 <dt><tt><b>lli</b></tt><dd> <tt>lli</tt> is the LLVM interpreter, which
467 can directly execute LLVM bytecode (although very slowly...). In addition
468 to a simple intepreter, <tt>lli</tt> is also has debugger and tracing
469 modes (entered by specifying <tt>-debug</tt> or <tt>-trace</tt> on the
470 command line, respectively).<p>
472 <dt><tt><b>llc</b></tt><dd> <tt>llc</tt> is the LLVM backend compiler,
473 which translates LLVM bytecode to a SPARC assembly file.<p>
475 <dt><tt><b>llvmgcc</b></tt><dd> <tt>llvmgcc</tt> is a GCC based C frontend
476 that has been retargeted to emit LLVM code as the machine code output. It
477 works just like any other GCC compiler, taking the typical <tt>-c, -S, -E,
478 -o</tt> options that are typically used. The source code for the
479 <tt>llvmgcc</tt> tool is currently not included in the LLVM cvs tree
480 because it is quite large and not very interesting.<p>
483 <dt><tt><b>gccas</b></tt><dd> This tool is invoked by the
484 <tt>llvmgcc</tt> frontend as the "assembler" part of the compiler. This
485 tool actually assembles LLVM assembly to LLVM bytecode,
486 performs a variety of optimizations,
487 and outputs LLVM bytecode. Thus when you invoke <tt>llvmgcc -c x.c -o
488 x.o</tt>, you are causing <tt>gccas</tt> to be run, which writes the
489 <tt>x.o</tt> file (which is an LLVM bytecode file that can be
490 disassembled or manipulated just like any other bytecode file). The
491 command line interface to <tt>gccas</tt> is designed to be as close as
492 possible to the <b>system</b> '<tt>as</tt>' utility so that the gcc
493 frontend itself did not have to be modified to interface to a "wierd"
496 <dt><tt><b>gccld</b></tt><dd> <tt>gccld</tt> links together several LLVM
497 bytecode files into one bytecode file and does some optimization. It is
498 the linker invoked by the gcc frontend when multiple .o files need to be
499 linked together. Like <tt>gccas</tt> the command line interface of
500 <tt>gccld</tt> is designed to match the system linker, to aid
501 interfacing with the GCC frontend.<p>
504 <dt><tt><b>opt</b></tt><dd> <tt>opt</tt> reads LLVM bytecode, applies a
505 series of LLVM to LLVM transformations (which are specified on the command
506 line), and then outputs the resultant bytecode. The '<tt>opt --help</tt>'
507 command is a good way to get a list of the program transformations
508 available in LLVM.<p>
511 <dt><tt><b>analyze</b></tt><dd> <tt>analyze</tt> is used to run a specific
512 analysis on an input LLVM bytecode file and print out the results. It is
513 primarily useful for debugging analyses, or familiarizing yourself with
514 what an analysis does.<p>
518 <!--=====================================================================-->
519 <h2><a name="tutorial">An example using the LLVM tool chain</h2>
520 <!--=====================================================================-->
523 <li>First, create a simple C file, name it 'hello.c':
525 #include <stdio.h>
527 printf("hello world\n");
532 <li>Next, compile the C file into a LLVM bytecode file:<p>
534 <tt>% llvmgcc hello.c -o hello</tt><p>
536 This will create two result files: <tt>hello</tt> and
537 <tt>hello.bc</tt>. The <tt>hello.bc</tt> is the LLVM bytecode that
538 corresponds the the compiled program and the library facilities that it
539 required. <tt>hello</tt> is a simple shell script that runs the bytecode
540 file with <tt>lli</tt>, making the result directly executable.<p>
542 <li>Run the program. To make sure the program ran, execute one of the
543 following commands:<p>
545 <tt>% ./hello</tt><p>
549 <tt>% lli hello.bc</tt><p>
551 <li>Use the <tt>dis</tt> utility to take a look at the LLVM assembly
554 <tt>% dis < hello.bc | less</tt><p>
556 <li>Compile the program to native Sparc assembly using the code
559 <tt>% llc hello.bc -o hello.s</tt><p>
561 <li>Assemble the native sparc assemble file into a program:<p>
563 <tt>% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.sparc</tt><p>
565 <li>Execute the native sparc program:<p>
567 <tt>% ./hello.sparc</tt><p>
572 <!--=====================================================================-->
573 <h2><a name="links">Links</a></h2>
574 <!--=====================================================================-->
576 <p>This document is just an <b>introduction</b> to how to use LLVM to do
577 some simple things... there are many more interesting and complicated things
578 that you can do that aren't documented here (but we'll gladly accept a patch
579 if you want to write something up!). For more information about LLVM, check
583 <li><a href="http://llvm.cs.uiuc.edu/">LLVM homepage</a></li>
584 <li><a href="http://llvm.cs.uiuc.edu/doxygen/">LLVM doxygen tree</a></li>
589 If you have any questions or run into any snags (or you have any
590 additions...), please send an email to
591 <a href="mailto:sabre@nondot.org">Chris Lattner</a>.</p>
593 <!-- Created: Mon Jul 1 02:29:02 CDT 2002 -->
595 Last modified: Tue Jun 3 22:06:43 CDT 2003