1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>Getting Started with LLVM System</title>
8 <h1>Getting Started with the LLVM System<br><font size=3>By: <a
9 href="mailto:gshi1@uiuc.edu">Guochun Shi</a> and <a
10 href="mailto:sabre@nondot.org">Chris Lattner</a></font></h1>
13 <li><a href="#quickstart">Getting started with LLVM</a>
15 <li><a href="#cvs">Checkout LLVM from CVS</a>
16 <li><a href="#environment">Set up your environment</a>
17 <li><a href="#compile">Compiling the Source Code</a>
19 <li><a href="#layout">Program layout</a>
21 <li><a href="#cvsdir">CVS directories</a>
22 <li><a href="#dd"><tt>Depend</tt>, <tt>Debug</tt>, &
23 <tt>Release</tt> directories</a></li>
24 <li><a href="#include"><tt>llvm/include</tt></a>
25 <li><a href="#lib"><tt>llvm/lib</tt></a>
26 <li><a href="#test"><tt>llvm/test</tt></a>
27 <li><a href="#tools"><tt>llvm/tools</tt></a>
29 <li><a href="#tutorial">An example using the LLVM tool chain</a>
30 <li><a href="#links">Links</a>
35 <!--=====================================================================-->
36 <h2><a name="quickstart">Getting Started with LLVM</a></h2>
37 <!--=====================================================================-->
39 <p>This guide is meant to get you up and running with LLVM as quickly as
40 possible. Once you get the basic system running you can choose an area to
41 dive into and learn more about. If you get stuck or something is missing
42 from this document, please email <a
43 href="mailto:sabre@nondot.org">Chris</a>.</p>
46 <!------------------------------------------------------------------------->
47 <h3><a name="tools">Checkout LLVM from CVS</a></h3>
48 <!------------------------------------------------------------------------->
50 <p>First step is to get the actual source code. To do this, all you need to
51 do is check it out from CVS. From your home directory, just enter:</p>
53 <p><tt>cvs -d /home/vadve/vadve/Research/DynOpt/CVSRepository checkout
56 <p>This will create an '<tt>llvm</tt>' directory in your home directory and
57 fully populate it with the source code for LLVM.</p>
60 <!------------------------------------------------------------------------->
61 <h3><a name="tools">Set up your environment</a></h3>
62 <!------------------------------------------------------------------------->
64 <p>Now that you have the source code available, you should set up your
65 environment to be able to use the LLVM tools (once compiled) with as little
66 hassle as possible. To do this, we recommend that you add the following
67 lines to your <tt>.cshrc</tt> (or the corresponding lines to your
68 <tt>.profile</tt> if you use a bourne shell derivative):
71 # Make the C frontend easy to use...
72 alias llvmgcc /home/vadve/lattner/cvs/gcc_install/bin/gcc
74 # Make the LLVM tools easy to use...
75 setenv PATH ~/llvm/tools/Debug:${PATH}
78 <p>The C compiler is not included in the CVS tree you just checked out, so
79 we just point to the cannonical location, and access it with the
80 <tt>llvmgcc</tt> command. The rest of the <a href="#tools">LLVM tools</a>
81 will be built into the <tt>llvm/tools/Debug</tt> directory inside of the
82 sourcebase. Adding them to your path will make it much easier to use
87 <!------------------------------------------------------------------------->
88 <h3><a name="compile">Compiling the Source Code</a></h3>
89 <!------------------------------------------------------------------------->
91 <p>Every directory in the LLVM source tree includes a Makefile to build it,
92 and any subdirectories that it contains. These makefiles require that you
93 use <tt>gmake</tt>, instead of <tt>make</tt> to build them, but can
94 otherwise be used freely. To build the entire LLVM system, just enter the
95 top level <tt>llvm</tt> directory and type <tt>gmake</tt>. A few minutes
96 later you will hopefully have a freshly compiled toolchain waiting for you
97 in <tt>llvm/tools/Debug</tt>. If you want to look at the libraries that
98 were compiled, look in <tt>llvm/lib/Debug</tt>.</p>
100 <p>By default, the LLVM build process sends all temporary (<tt>.o</tt>,
101 <tt>.so</tt>, <tt>.a</tt>) files into a <tt>/shared/[your login
102 name]/...</tt> directory, which is supposed to be on a disk local to the
103 current machine. If you get an error talking about a <tt>/shared</tt>
104 directory, and you find out that it doesn't exist, try enabling the
105 "<tt>BUILD_ROOT = .</tt> line in the top level <tt>Makefile.common</tt> that
106 will change LLVM to build into the current directory instead of
110 <!--=====================================================================-->
111 <h2><a name="layout">Program Layout</a></h2>
112 <!--=====================================================================-->
114 <p>One useful source of infomation about the LLVM sourcebase is the LLVM <a
115 href="http://www.doxygen.org">doxygen</a> documentation, available at <tt><a
116 href="http://llvm.cs.uiuc.edu/doxygen/">http://llvm.cs.uiuc.edu/doxygen/</a></tt>. The
117 following is a brief introduction to code layout:</p>
120 <!------------------------------------------------------------------------->
121 <h3><a name="cvsdir"><tt>CVS</tt> directories</a></h3>
122 <!------------------------------------------------------------------------->
124 Every directory checked out of CVS will contain a <tt>CVS</tt> directory,
125 for the most part these can just be ignored.
128 <!------------------------------------------------------------------------->
129 <h3><a name="ddr"><tt>Depend</tt>, <tt>Debug</tt>, & <tt>Release</tt>
131 <!------------------------------------------------------------------------->
133 If you are building with the "<tt>BUILD_ROOT=.</tt>" option enabled in the
134 <tt>Makefile.common</tt> file, most source directories will contain two
135 directories, <tt>Depend</tt> and <tt>Debug</tt>. The <tt>Depend</tt>
136 directory contains automatically generated dependance files which are used
137 during compilation to make sure that source files get rebuilt if a header
138 file they use is modified. The <tt>Debug</tt> directory holds the object
139 files, library files and executables that are used for building a debug
140 enabled build. The <tt>Release</tt> directory is created to hold the same
141 files when the <tt>ENABLE_OPTIMIZED=1</tt> flag is passed to <tt>gmake</tt>,
142 causing an optimized built to be performed.<p>
145 <!------------------------------------------------------------------------->
146 <h3><a name="include"><tt>llvm/include</tt></a></h3>
147 <!------------------------------------------------------------------------->
149 This directory contains public header files exported from the LLVM
150 library. The two main subdirectories of this directory are:<p>
153 <li><tt>llvm/include/llvm</tt> - This directory contains all of the LLVM
154 specific header files. This directory also has subdirectories for
155 different portions of llvm: <tt>Analysis</tt>, <tt>CodeGen</tt>,
156 <tt>Reoptimizer</tt>, <tt>Target</tt>, <tt>Transforms</tt>, etc...
158 <li><tt>llvm/include/Support</tt> - This directory contains generic
159 support libraries that are independant of LLVM, but are used by LLVM.
160 For example, some C++ STL utilities and a Command Line option processing
164 <!------------------------------------------------------------------------->
165 <h3><a name="lib"><tt>llvm/lib</tt></a></h3>
166 <!------------------------------------------------------------------------->
168 This directory contains most source files of LLVM system. In LLVM almost all
169 code exists in libraries, making it very easy to share code among the
170 different <a href="#tools">tools</a>.<p>
173 <dt><tt>llvm/lib/VMCore/</tt><dd> This directory holds the core LLVM
174 source files that implement core classes like Instruction and BasicBlock.
176 <dt><tt>llvm/lib/AsmParser/</tt><dd> This directory holds the source code
177 for the LLVM assembly language parser library.
179 <dt><tt>llvm/lib/ByteCode/</tt><dd> This directory holds code for reading
180 and write LLVM bytecode.
182 <dt><tt>llvm/lib/CWrite/</tt><dd> This directory implements the LLVM to C
185 <dt><tt>llvm/lib/Analysis/</tt><dd> This directory contains a variety of
186 different program analyses, such as Dominator Information, Call Graphs,
187 Induction Variables, Interval Identification, Natural Loop Identification,
190 <dt><tt>llvm/lib/Transforms/</tt><dd> This directory contains the source
191 code for the LLVM to LLVM program transformations, such as Aggressive Dead
192 Code Elimination, Sparse Conditional Constant Propogation, Inlining, Loop
193 Invarient Code Motion, Dead Global Elimination, Pool Allocation, and many
196 <dt><tt>llvm/lib/Target/</tt><dd> This directory contains files that
197 describe various target architectures for code generation. For example,
198 the llvm/lib/Target/Sparc directory holds the Sparc machine
201 <dt><tt>llvm/lib/CodeGen/</tt><dd> This directory contains the major parts
202 of the code generator: Instruction Selector, Instruction Scheduling, and
205 <dt><tt>llvm/lib/Reoptimizer/</tt><dd> This directory holds code related
206 to the runtime reoptimizer framework that is currently under development.
208 <dt><tt>llvm/lib/Support/</tt><dd> This directory contains the source code
209 that corresponds to the header files located in
210 <tt>llvm/include/Support/</tt>.
213 <!------------------------------------------------------------------------->
214 <h3><a name="test"><tt>llvm/test</tt></a></h3>
215 <!------------------------------------------------------------------------->
217 <p>This directory contains regression tests and source code that is used to
218 test the LLVM infrastructure...</p>
220 <!------------------------------------------------------------------------->
221 <h3><a name="tools"><tt>llvm/tools</tt></a></h3>
222 <!------------------------------------------------------------------------->
224 <p>The <b>tools</b> directory contains the executables built out of the
225 libraries above, which form the main part of the user interface. You can
226 always get help for a tool by typing <tt>tool_name --help</tt>. The
227 following is a brief introduction to the most important tools.</p>
230 <dt><tt><b>as</b></tt><dd>The assembler transforms the human readable
231 llvm assembly to llvm bytecode.<p>
233 <dt><tt><b>dis</b></tt><dd>The disassembler transforms the llvm bytecode
234 to human readable llvm assembly. Additionally it can convert LLVM
235 bytecode to C, which is enabled with the <tt>-c</tt> option.<p>
237 <dt><tt><b>lli</b></tt><dd> <tt>lli</tt> is the LLVM interpreter, which
238 can directly execute LLVM bytecode (although very slowly...). In addition
239 to a simple intepreter, <tt>lli</tt> is also has debugger and tracing
240 modes (entered by specifying <tt>-debug</tt> or <tt>-trace</tt> on the
241 command line, respectively).<p>
243 <dt><tt><b>llc</b></tt><dd> <tt>llc</tt> is the LLVM backend compiler,
244 which translates LLVM bytecode to a SPARC assembly file.<p>
246 <dt><tt><b>llvmgcc</b></tt><dd> <tt>llvmgcc</tt> is a GCC based C frontend
247 that has been retargeted to emit LLVM code as the machine code output. It
248 works just like any other GCC compiler, taking the typical <tt>-c, -S, -E,
249 -o</tt> options that are typically used. The source code for the
250 <tt>llvmgcc</tt> tool is currently not included in the LLVM cvs tree
251 because it is quite large and not very interesting.<p>
254 <dt><tt><b>gccas</b></tt><dd> This took is invoked by the
255 <tt>llvmgcc</tt> frontend as the "assembler" part of the compiler. This
256 tool actually assembles its input, performs a variety of optimizations,
257 and outputs LLVM bytecode. Thus when you invoke <tt>llvmgcc -c x.c -o
258 x.o</tt>, you are causing <tt>gccas</tt> to be run, which writes the
259 <tt>x.o</tt> file (which is an LLVM bytecode file that can be
260 disassembled or manipulated just like any other bytecode file). The
261 command line interface to <tt>gccas</tt> is designed to be as close as
262 possible to the <b>system</b> <tt>as</tt> utility so that the gcc
263 frontend itself did not have to be modified to interface to a "wierd"
266 <dt><tt><b>gccld</b></tt><dd> <tt>gccld</tt> links together several llvm
267 bytecode files into one bytecode file and does some optimization. It is
268 the linker invoked by the gcc frontend when multiple .o files need to be
269 linked together. Like <tt>gccas</tt> the command line interface of
270 <tt>gccld</tt> is designed to match the system linker, to aid
271 interfacing with the GCC frontend.<p>
274 <dt><tt><b>opt</b></tt><dd> <tt>opt</tt> reads llvm bytecode, applies a
275 series of LLVM to LLVM transformations (which are specified on the command
276 line), and then outputs the resultant bytecode. The '<tt>opt --help</tt>'
277 command is a good way to get a list of the program transformations
278 available in LLVM.<p>
281 <dt><tt><b>analyze</b></tt><dd> <tt>analyze</tt> is used to run a specific
282 analysis on an input LLVM bytecode file and print out the results. It is
283 primarily useful for debugging analyses, or familiarizing yourself with
284 what an analysis does.<p>
288 <!--=====================================================================-->
289 <h2><a name="tutorial">An example using the LLVM tool chain</h2>
290 <!--=====================================================================-->
293 <li>First, create a simple C file, name it 'hello.c':
295 #include <stdio.h>
297 printf("hello world\n");
302 <li>Next, compile the C file into a LLVM bytecode file:<p>
304 <tt>% llvmgcc hello.c -o hello</tt><p>
306 This will create two result files: <tt>hello</tt> and
307 <tt>hello.bc</tt>. The <tt>hello.bc</tt> is the LLVM bytecode that
308 corresponds the the compiled program and the library facilities that it
309 required. <tt>hello</tt> is a simple shell script that runs the bytecode
310 file with <tt>lli</tt>, making the result directly executable.<p>
312 <li>Run the program. To make sure the program ran, execute one of the
313 following commands:<p>
315 <tt>% ./hello</tt><p>
319 <tt>% lli hello.bc</tt><p>
321 <li>Use the <tt>dis</tt> utility to take a look at the LLVM assembly
324 <tt>% dis < hello.bc | less</tt><p>
326 <li>Compile the program to native Sparc assembly using the code
329 <tt>% llc hello.bc -o hello.s</tt><p>
331 <li>Assemble the native sparc assemble file into a program:<p>
333 <tt>% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.sparc</tt><p>
335 <li>Execute the native sparc program:<p>
337 <tt>% ./hello.sparc</tt><p>
342 <!--=====================================================================-->
343 <h2><a name="links">Links</a></h2>
344 <!--=====================================================================-->
346 <p>This document is just an <b>introduction</b> to how to use LLVM to do
347 some simple things... there are many more interesting and complicated things
348 that you can do that aren't documented here (but we'll gladly accept a patch
349 if you want to write something up!). For more information about LLVM, check
353 <li><a href="http://llvm.cs.uiuc.edu/">LLVM homepage</a></li>
354 <li><a href="http://tank.cs.uiuc.edu/doxygen/">LLVM doxygen tree</a></li>
359 If you have any questions or run into any snags (or you have any
360 additions...), please send an email to <a
361 href="mailto:sabre@nondot.org">Chris Lattner</a> or <a
362 href="mailto:gshi1@uiuc.edu">Guochun Shi</a>. <p>
364 <!-- Created: Mon Jul 1 02:29:02 CDT 2002 -->
366 Last modified: Fri Aug 9 11:13:34 CDT 2002