96bd877dd0eec4f4076dd66d267133d594effb38
[oota-llvm.git] / docs / GettingStarted.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3   <head>
4     <title>Getting Started with LLVM System</title>
5   </head>
6
7   <body bgcolor=white>
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>
11
12     <ul>
13       <li><a href="#quickstart">Getting started with LLVM</a>
14         <ol>
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>
18         </ol>
19       <li><a href="#layout">Program layout</a>
20         <ol>
21           <li><a href="#cvsdir">CVS directories</a>
22           <li><a href="#dd"><tt>Depend</tt>, <tt>Debug</tt>, &amp;
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>  
28         </ol>
29       <li><a href="#tutorial">An example using the LLVM tool chain</a>
30       <li><a href="#links">Links</a>
31     </ul>
32
33
34
35     <!--=====================================================================-->
36     <h2><a name="quickstart">Getting Started with LLVM</a></h2>
37     <!--=====================================================================-->
38
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>
44     
45
46     <!------------------------------------------------------------------------->
47     <h3><a name="tools">Checkout LLVM from CVS</a></h3>
48     <!------------------------------------------------------------------------->
49
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>
52
53     <p><tt>cvs -d /home/vadve/vadve/Research/DynOpt/CVSRepository checkout
54     llvm</tt></p>
55
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>
58     
59
60     <!------------------------------------------------------------------------->
61     <h3><a name="tools">Set up your environment</a></h3>
62     <!------------------------------------------------------------------------->
63     
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):
69
70     <pre>
71        # Make the C frontend easy to use...
72        alias llvmgcc /home/vadve/lattner/cvs/gcc_install/bin/gcc
73
74        # Make the LLVM tools easy to use...
75        setenv PATH ~/llvm/tools/Debug:${PATH}
76     </pre>
77
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
83     them.</p>
84
85
86
87     <!------------------------------------------------------------------------->
88     <h3><a name="compile">Compiling the Source Code</a></h3>
89     <!------------------------------------------------------------------------->
90
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>
99
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
107     <tt>/shared</tt>.<p>
108
109
110     <!--=====================================================================-->
111     <h2><a name="layout">Program Layout</a></h2>
112     <!--=====================================================================-->
113
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>
118
119
120     <!------------------------------------------------------------------------->
121     <h3><a name="cvsdir"><tt>CVS</tt> directories</a></h3>
122     <!------------------------------------------------------------------------->
123
124     Every directory checked out of CVS will contain a <tt>CVS</tt> directory,
125     for the most part these can just be ignored.
126
127
128     <!------------------------------------------------------------------------->
129     <h3><a name="ddr"><tt>Depend</tt>, <tt>Debug</tt>, &amp; <tt>Release</tt>
130     directories</a></h3>
131     <!------------------------------------------------------------------------->
132
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>
143
144
145     <!------------------------------------------------------------------------->
146     <h3><a name="include"><tt>llvm/include</tt></a></h3>
147     <!------------------------------------------------------------------------->
148
149     This directory contains public header files exported from the LLVM
150     library. The two main subdirectories of this directory are:<p>
151
152     <ol>
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...
157
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
161        library.
162     </ol>
163
164     <!------------------------------------------------------------------------->
165     <h3><a name="lib"><tt>llvm/lib</tt></a></h3>
166     <!------------------------------------------------------------------------->
167
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>
171
172      <dl compact>
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.
175
176       <dt><tt>llvm/lib/AsmParser/</tt><dd> This directory holds the source code
177       for the LLVM assembly language parser library.
178
179       <dt><tt>llvm/lib/ByteCode/</tt><dd> This directory holds code for reading
180       and write LLVM bytecode.
181
182       <dt><tt>llvm/lib/CWrite/</tt><dd> This directory implements the LLVM to C
183       converter.
184
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,
188       etc...
189
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
194       others...
195
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
199       description.<br>
200               
201       <dt><tt>llvm/lib/CodeGen/</tt><dd> This directory contains the major parts
202       of the code generator: Instruction Selector, Instruction Scheduling, and
203       Register Allocation.
204
205       <dt><tt>llvm/lib/Reoptimizer/</tt><dd> This directory holds code related
206       to the runtime reoptimizer framework that is currently under development.
207               
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>.
211     </dl>
212
213     <!------------------------------------------------------------------------->
214     <h3><a name="test"><tt>llvm/test</tt></a></h3>
215     <!------------------------------------------------------------------------->
216
217     <p>This directory contains regression tests and source code that is used to
218     test the LLVM infrastructure...</p>
219
220     <!------------------------------------------------------------------------->
221     <h3><a name="tools"><tt>llvm/tools</tt></a></h3>
222     <!------------------------------------------------------------------------->
223
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>
228
229     <dl compact>
230       <dt><tt><b>as</b></tt><dd>The assembler transforms the human readable
231       llvm assembly to llvm bytecode.<p>
232
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>
236
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>
242
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>
245
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>
252
253       <ol>
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"
264         assembler.<p>
265
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>
272       </ol>
273
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>
279
280  
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>
285
286     </dl>
287           
288     <!--=====================================================================-->
289     <h2><a name="tutorial">An example using the LLVM tool chain</h2>
290     <!--=====================================================================-->
291
292     <ol>
293     <li>First, create a simple C file, name it 'hello.c':
294        <pre>
295    #include &lt;stdio.h&gt;
296    int main() {
297      printf("hello world\n");
298      return 0;
299    }
300        </pre>
301
302     <li>Next, compile the C file into a LLVM bytecode file:<p>
303
304       <tt>% llvmgcc hello.c -o hello</tt><p>
305
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>
311
312     <li>Run the program. To make sure the program ran, execute one of the
313     following commands:<p>
314       
315       <tt>% ./hello</tt><p>
316  
317       or<p>
318
319       <tt>% lli hello.bc</tt><p>
320
321     <li>Use the <tt>dis</tt> utility to take a look at the LLVM assembly
322     code:<p>
323
324       <tt>% dis < hello.bc | less</tt><p>
325
326     <li>Compile the program to native Sparc assembly using the code
327     generator:<p>
328
329       <tt>% llc hello.bc -o hello.s</tt><p>
330
331     <li>Assemble the native sparc assemble file into a program:<p>
332
333       <tt>% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.sparc</tt><p>
334
335     <li>Execute the native sparc program:<p>
336
337       <tt>% ./hello.sparc</tt><p>
338
339     </ol>
340
341
342     <!--=====================================================================-->
343     <h2><a name="links">Links</a></h2>
344     <!--=====================================================================-->
345
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
350     out:</p>
351
352     <ul>
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>
355     </ul>
356
357     <hr>
358
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>
363
364             <!-- Created: Mon Jul  1 02:29:02 CDT 2002 -->
365             <!-- hhmts start -->
366 Last modified: Fri Aug  9 11:13:34 CDT 2002
367 <!-- hhmts end -->
368   </body>
369 </html>