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-profiling</i> =
251 Enables profiling compilation (compiler flags needed to add profiling
253 <p><li><i>--enable-verbose</i> =
254 Enables verbose messages during the compile.
255 <p><li><i>--enable-jit</i> =
256 Compile the Just In Time (JIT) functionality. This is not available
260 In addition to running <tt>configure</tt>, you must set the
261 <tt>LLVM_LIB_SEARCH_PATH</tt> environment variable in your startup scripts.
262 This environment variable is used to locate "system" libraries like
263 "<tt>-lc</tt>" and "<tt>-lm</tt>" when linking. This variable should be set
264 to the absolute path for the bytecode-libs subdirectory of the C front-end
265 install. For example, one might set <tt>LLVM_LIB_SEARCH_PATH</tt> to
266 <tt>/home/vadve/lattner/local/x86/llvm-gcc/bytecode-libs</tt> for the X86
267 version of the C front-end on our research machines.<p>
269 <!------------------------------------------------------------------------->
270 <h3><a name="objfiles">The location for LLVM object files</a></h3>
271 <!------------------------------------------------------------------------->
273 <p>The LLVM make system sends most output files generated during the build
274 into the directory defined by the variable OBJ_ROOT in
275 <tt>llvm/Makefile.config</tt>. This can be either just your normal LLVM
276 source tree or some other directory writable by you. You may wish to put
277 object files on a different filesystem either to keep them from being backed
278 up or to speed up local builds.
280 <p>If you wish to place output files into a separate directory, use the
281 <tt>--with-objroot=<i>directory</i></tt> option of <tt>configure</tt> to
282 set the top level directory of where the object files will go. Otherwise,
283 leave this option unspecified, and <tt>configure</tt> will place files
284 within the LLVM source tree.
286 <!------------------------------------------------------------------------->
287 <h3><a name="environment">Setting up your environment</a></h3>
288 <!------------------------------------------------------------------------->
290 <i>NOTE: This step is optional but will set up your environment so you
291 can use the compiled LLVM tools with as little hassle as
294 <p>Add the following lines to your <tt>.cshrc</tt> (or the corresponding
295 lines to your <tt>.profile</tt> if you use a bourne shell derivative).
298 # Make the C front end easy to use...
299 alias llvmgcc <i>LLVMGCCDIR</i><tt>/bin/llvm-gcc</tt>
301 # Make the LLVM tools easy to use...
302 setenv PATH <i>OBJ_ROOT</i>/llvm/tools/Debug:${PATH}
304 The <tt>llvmgcc</tt> alias is useful because the C compiler is not
305 included in the CVS tree you just checked out.
307 <p>The other <a href="#tools">LLVM tools</a> are part of the LLVM
308 source base and built when compiling LLVM. They will be built into the
309 <tt><i>OBJ_ROOT</i>/tools/Debug</tt> directory.</p>
311 <!------------------------------------------------------------------------->
312 <h3><a name="compile">Compiling the source code</a></h3>
313 <!------------------------------------------------------------------------->
315 <p>Every directory in the LLVM source tree includes a <tt>Makefile</tt> to
316 build it and any subdirectories that it contains. These makefiles require
317 that you use GNU Make (sometimes called <tt>gmake</tt>) instead of
320 otherwise be used freely. To build the entire LLVM system, just enter the
321 top level <tt>llvm</tt> directory and type <tt>gmake</tt>. A few minutes
322 later you will hopefully have a freshly compiled toolchain waiting for you
323 in <tt><i>OBJ_ROOT</i>/llvm/tools/Debug</tt>. If you want to look at the
325 were compiled, look in <tt><i>OBJ_ROOT</i>/llvm/lib/Debug</tt>.</p>
327 If you get an error about a <tt>/localhome</tt> directory, follow the
328 instructions in the section about <a href="#environment">Setting Up Your
333 <!--=====================================================================-->
335 <h2><a name="layout"><b>Program Layout</b></a></h2>
337 <!--=====================================================================-->
339 <p>One useful source of infomation about the LLVM sourcebase is the LLVM <a
340 href="http://www.doxygen.org">doxygen</a> documentation, available at <tt><a
341 href="http://llvm.cs.uiuc.edu/doxygen/">http://llvm.cs.uiuc.edu/doxygen/</a></tt>. The
342 following is a brief introduction to code layout:</p>
345 <!------------------------------------------------------------------------->
346 <h3><a name="cvsdir"><tt>CVS</tt> directories</a></h3>
347 <!------------------------------------------------------------------------->
349 Every directory checked out of CVS will contain a <tt>CVS</tt> directory;
350 for the most part these can just be ignored.
353 <!------------------------------------------------------------------------->
354 <h3><a name="ddr"><tt>Depend</tt>, <tt>Debug</tt>, & <tt>Release</tt>
356 <!------------------------------------------------------------------------->
358 If you are building with the "<tt>OBJ_ROOT=.</tt>" option enabled in the
359 <tt>Makefile.config</tt> file (i.e. you did not specify
360 <tt>--with-objroot</tt> when you ran <tt>configure</tt>), most source
361 directories will contain two
362 directories, <tt>Depend</tt> and <tt>Debug</tt>. The <tt>Depend</tt>
363 directory contains automatically generated dependance files which are used
364 during compilation to make sure that source files get rebuilt if a header
365 file they use is modified. The <tt>Debug</tt> directory holds the object
366 files, library files, and executables that are used for building a debug
367 enabled build. The <tt>Release</tt> directory is created to hold the same
368 files when the <tt>ENABLE_OPTIMIZED=1</tt> flag is passed to <tt>gmake</tt>,
369 causing an optimized build to be performed.<p>
372 <!------------------------------------------------------------------------->
373 <h3><a name="include"><tt>llvm/include</tt></a></h3>
374 <!------------------------------------------------------------------------->
376 This directory contains public header files exported from the LLVM
377 library. The three main subdirectories of this directory are:<p>
380 <li><tt>llvm/include/llvm</tt> - This directory contains all of the LLVM
381 specific header files. This directory also has subdirectories for
382 different portions of LLVM: <tt>Analysis</tt>, <tt>CodeGen</tt>,
383 <tt>Reoptimizer</tt>, <tt>Target</tt>, <tt>Transforms</tt>, etc...
385 <li><tt>llvm/include/Support</tt> - This directory contains generic
386 support libraries that are independant of LLVM, but are used by LLVM.
387 For example, some C++ STL utilities and a Command Line option processing
390 <li><tt>llvm/include/Config</tt> - This directory contains header files
391 configured by the <tt>configure</tt> script. They wrap "standard" UNIX
392 and C header files. Source code can include these header files which
393 automatically take care of the conditional #includes that the configure
397 <!------------------------------------------------------------------------->
398 <h3><a name="lib"><tt>llvm/lib</tt></a></h3>
399 <!------------------------------------------------------------------------->
401 This directory contains most of the source files of the LLVM system. In
403 code exists in libraries, making it very easy to share code among the
404 different <a href="#tools">tools</a>.<p>
407 <dt><tt>llvm/lib/VMCore/</tt><dd> This directory holds the core LLVM
408 source files that implement core classes like Instruction and BasicBlock.
410 <dt><tt>llvm/lib/AsmParser/</tt><dd> This directory holds the source code
411 for the LLVM assembly language parser library.
413 <dt><tt>llvm/lib/ByteCode/</tt><dd> This directory holds code for reading
414 and write LLVM bytecode.
416 <dt><tt>llvm/lib/CWriter/</tt><dd> This directory implements the LLVM to C
419 <dt><tt>llvm/lib/Analysis/</tt><dd> This directory contains a variety of
420 different program analyses, such as Dominator Information, Call Graphs,
421 Induction Variables, Interval Identification, Natural Loop Identification,
424 <dt><tt>llvm/lib/Transforms/</tt><dd> This directory contains the source
425 code for the LLVM to LLVM program transformations, such as Aggressive Dead
426 Code Elimination, Sparse Conditional Constant Propagation, Inlining, Loop
427 Invarient Code Motion, Dead Global Elimination, Pool Allocation, and many
430 <dt><tt>llvm/lib/Target/</tt><dd> This directory contains files that
431 describe various target architectures for code generation. For example,
432 the llvm/lib/Target/Sparc directory holds the Sparc machine
435 <dt><tt>llvm/lib/CodeGen/</tt><dd> This directory contains the major parts
436 of the code generator: Instruction Selector, Instruction Scheduling, and
439 <dt><tt>llvm/lib/Reoptimizer/</tt><dd> This directory holds code related
440 to the runtime reoptimizer framework that is currently under development.
442 <dt><tt>llvm/lib/Support/</tt><dd> This directory contains the source code
443 that corresponds to the header files located in
444 <tt>llvm/include/Support/</tt>.
447 <!------------------------------------------------------------------------->
448 <h3><a name="test"><tt>llvm/test</tt></a></h3>
449 <!------------------------------------------------------------------------->
451 <p>This directory contains regression tests and source code that is used to
452 test the LLVM infrastructure...</p>
454 <!------------------------------------------------------------------------->
455 <h3><a name="tools"><tt>llvm/tools</tt></a></h3>
456 <!------------------------------------------------------------------------->
458 <p>The <b>tools</b> directory contains the executables built out of the
459 libraries above, which form the main part of the user interface. You can
460 always get help for a tool by typing <tt>tool_name --help</tt>. The
461 following is a brief introduction to the most important tools.</p>
464 <dt><tt><b>as</b></tt><dd>The assembler transforms the human readable
465 LLVM assembly to LLVM bytecode.<p>
467 <dt><tt><b>dis</b></tt><dd>The disassembler transforms the LLVM bytecode
468 to human readable LLVM assembly. Additionally it can convert LLVM
469 bytecode to C, which is enabled with the <tt>-c</tt> option.<p>
471 <dt><tt><b>lli</b></tt><dd> <tt>lli</tt> is the LLVM interpreter, which
472 can directly execute LLVM bytecode (although very slowly...). In addition
473 to a simple intepreter, <tt>lli</tt> is also has debugger and tracing
474 modes (entered by specifying <tt>-debug</tt> or <tt>-trace</tt> on the
475 command line, respectively).<p>
477 <dt><tt><b>llc</b></tt><dd> <tt>llc</tt> is the LLVM backend compiler,
478 which translates LLVM bytecode to a SPARC assembly file.<p>
480 <dt><tt><b>llvmgcc</b></tt><dd> <tt>llvmgcc</tt> is a GCC based C frontend
481 that has been retargeted to emit LLVM code as the machine code output. It
482 works just like any other GCC compiler, taking the typical <tt>-c, -S, -E,
483 -o</tt> options that are typically used. The source code for the
484 <tt>llvmgcc</tt> tool is currently not included in the LLVM cvs tree
485 because it is quite large and not very interesting.<p>
488 <dt><tt><b>gccas</b></tt><dd> This tool is invoked by the
489 <tt>llvmgcc</tt> frontend as the "assembler" part of the compiler. This
490 tool actually assembles LLVM assembly to LLVM bytecode,
491 performs a variety of optimizations,
492 and outputs LLVM bytecode. Thus when you invoke <tt>llvmgcc -c x.c -o
493 x.o</tt>, you are causing <tt>gccas</tt> to be run, which writes the
494 <tt>x.o</tt> file (which is an LLVM bytecode file that can be
495 disassembled or manipulated just like any other bytecode file). The
496 command line interface to <tt>gccas</tt> is designed to be as close as
497 possible to the <b>system</b> '<tt>as</tt>' utility so that the gcc
498 frontend itself did not have to be modified to interface to a "wierd"
501 <dt><tt><b>gccld</b></tt><dd> <tt>gccld</tt> links together several LLVM
502 bytecode files into one bytecode file and does some optimization. It is
503 the linker invoked by the gcc frontend when multiple .o files need to be
504 linked together. Like <tt>gccas</tt> the command line interface of
505 <tt>gccld</tt> is designed to match the system linker, to aid
506 interfacing with the GCC frontend.<p>
509 <dt><tt><b>opt</b></tt><dd> <tt>opt</tt> reads LLVM bytecode, applies a
510 series of LLVM to LLVM transformations (which are specified on the command
511 line), and then outputs the resultant bytecode. The '<tt>opt --help</tt>'
512 command is a good way to get a list of the program transformations
513 available in LLVM.<p>
516 <dt><tt><b>analyze</b></tt><dd> <tt>analyze</tt> is used to run a specific
517 analysis on an input LLVM bytecode file and print out the results. It is
518 primarily useful for debugging analyses, or familiarizing yourself with
519 what an analysis does.<p>
523 <!--=====================================================================-->
524 <h2><a name="tutorial">An example using the LLVM tool chain</h2>
525 <!--=====================================================================-->
528 <li>First, create a simple C file, name it 'hello.c':
530 #include <stdio.h>
532 printf("hello world\n");
537 <li>Next, compile the C file into a LLVM bytecode file:<p>
539 <tt>% llvmgcc hello.c -o hello</tt><p>
541 This will create two result files: <tt>hello</tt> and
542 <tt>hello.bc</tt>. The <tt>hello.bc</tt> is the LLVM bytecode that
543 corresponds the the compiled program and the library facilities that it
544 required. <tt>hello</tt> is a simple shell script that runs the bytecode
545 file with <tt>lli</tt>, making the result directly executable.<p>
547 <li>Run the program. To make sure the program ran, execute one of the
548 following commands:<p>
550 <tt>% ./hello</tt><p>
554 <tt>% lli hello.bc</tt><p>
556 <li>Use the <tt>dis</tt> utility to take a look at the LLVM assembly
559 <tt>% dis < hello.bc | less</tt><p>
561 <li>Compile the program to native Sparc assembly using the code
564 <tt>% llc hello.bc -o hello.s</tt><p>
566 <li>Assemble the native sparc assemble file into a program:<p>
568 <tt>% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.sparc</tt><p>
570 <li>Execute the native sparc program:<p>
572 <tt>% ./hello.sparc</tt><p>
577 <!--=====================================================================-->
578 <h2><a name="links">Links</a></h2>
579 <!--=====================================================================-->
581 <p>This document is just an <b>introduction</b> to how to use LLVM to do
582 some simple things... there are many more interesting and complicated things
583 that you can do that aren't documented here (but we'll gladly accept a patch
584 if you want to write something up!). For more information about LLVM, check
588 <li><a href="http://llvm.cs.uiuc.edu/">LLVM homepage</a></li>
589 <li><a href="http://llvm.cs.uiuc.edu/doxygen/">LLVM doxygen tree</a></li>
594 If you have any questions or run into any snags (or you have any
595 additions...), please send an email to
596 <a href="mailto:sabre@nondot.org">Chris Lattner</a>.</p>
598 <!-- Created: Mon Jul 1 02:29:02 CDT 2002 -->
600 Last modified: Tue Jun 3 22:06:43 CDT 2003