Pass manager may require certain analysis. In such cases, initially
[oota-llvm.git] / docs / FAQ.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <title>LLVM: Frequently Asked Questions</title>
6   <style type="text/css">
7     @import url("llvm.css");
8     .question { font-weight: bold }
9     .answer   { margin-left: 2em  }
10   </style>
11 </head>
12 <body>
13
14 <div class="doc_title">
15   LLVM: Frequently Asked Questions
16 </div>
17
18 <ol>
19   <li><a href="#license">License</a>
20   <ol>
21   <li>Why are the LLVM source code and the front-end distributed under different
22   licenses?</li>
23   <li>Does the University of Illinois Open Source License really qualify as an
24   "open source" license?</li>
25   <li>Can I modify LLVM source code and redistribute the modified source?</li>
26   <li>Can I modify LLVM source code and redistribute binaries or other tools
27   based on it, without redistributing the source?</li>
28   </ol></li>
29
30   <li><a href="#source">Source code</a>
31   <ol>
32   <li>In what language is LLVM written?</li>
33   <li>How portable is the LLVM source code?</li>
34   </ol></li>
35
36   <li><a href="#build">Build Problems</a>
37   <ol>
38   <li>When I run configure, it finds the wrong C compiler.</li>
39   <li>I compile the code, and I get some error about <tt>/localhome</tt>.</li>
40   <li>The <tt>configure</tt> script finds the right C compiler, but it uses the
41   LLVM linker from a previous build.  What do I do?</li>
42   <li>When creating a dynamic library, I get a strange GLIBC error.</li>
43   <li>I've updated my source tree from CVS, and now my build is trying to use a
44   file/directory that doesn't exist.</li>
45   <li>I've modified a Makefile in my source tree, but my build tree keeps using
46   the old version.  What do I do?</li>
47   <li>I've upgraded to a new version of LLVM, and I get strange build
48   errors.</li>
49   <li>I've built LLVM and am testing it, but the tests freeze.</li>
50   <li>Why do test results differ when I perform different types of builds?</li>
51   <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
52   <li>When I use the test suite, all of the C Backend tests fail.  What is
53       wrong?</li>
54   <li>After CVS update, rebuilding gives the error "No rule to make
55   target".</li>
56   </ol></li>
57
58   <li><a href="#felangs">Source Languages</a>
59   <ol>
60     <li><a href="#langs">What source languages are supported?</a></li>
61     <li><a href="#langhlsupp">What support is there for higher level source
62       language constructs for building a compiler?</a></li>
63     <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
64       instruction. Help!</a></li>
65   </ol>
66
67   <li><a href="#cfe">Using the GCC Front End</a>
68   <ol>
69     <li>
70     When I compile software that uses a configure script, the configure script
71     thinks my system has all of the header files and libraries it is testing
72     for.  How do I get configure to work correctly?
73     </li>
74
75     <li>
76     When I compile code using the LLVM GCC front end, it complains that it
77     cannot find libcrtend.a.
78     </li>
79
80     <li>
81     How can I disable all optimizations when compiling code using the LLVM GCC front end?
82     </li>
83
84     <li><a href="#translatec++">Can I use LLVM to convert C++ code to C code?</a></li>
85
86   </ol>
87   </li>
88
89   <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
90   <ol>
91      <li><a href="#__main">What is this <tt>__main()</tt> call that gets inserted into
92          <tt>main()</tt>?</a></li>
93      <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
94           <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
95           #include &lt;iostream&gt;?</a></li>
96      <li><a href="#codedce">Where did all of my code go??</a></li>
97      <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in my code?</a></li>
98   </ol>
99   </li>
100 </ol>
101
102 <div class="doc_author">
103   <p>Written by <a href="http://llvm.org">The LLVM Team</a></p>
104 </div>
105
106
107 <!-- *********************************************************************** -->
108 <div class="doc_section">
109   <a name="license">License</a>
110 </div>
111 <!-- *********************************************************************** -->
112
113 <div class="question">
114 <p>Why are the LLVM source code and the front-end distributed under different
115 licenses?</p>
116 </div>
117         
118 <div class="answer">
119 <p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
120 Our aim is to distribute LLVM source code under a <em>much less restrictive</em>
121 license, in particular one that does not compel users who distribute tools based
122 on modifying the source to redistribute the modified source code as well.</p>
123 </div>
124
125 <div class="question">
126 <p>Does the University of Illinois Open Source License really qualify as an
127 "open source" license?</p>
128 </div>
129
130 <div class="answer">
131 <p>Yes, the license is <a
132 href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by the Open
133 Source Initiative (OSI).</p>
134 </div>
135
136 <div class="question">
137 <p>Can I modify LLVM source code and redistribute the modified source?</p>
138 </div>
139
140 <div class="answer">
141 <p>Yes.  The modified source distribution must retain the copyright notice and
142 follow the three bulletted conditions listed in the <a
143 href="http://llvm.org/releases/1.3/LICENSE.TXT">LLVM license</a>.</p>
144 </div>
145
146 <div class="question">
147 <p>Can I modify LLVM source code and redistribute binaries or other tools based
148 on it, without redistributing the source?</p>
149 </div>
150
151 <div class="answer">
152 <p>Yes, this is why we distribute LLVM under a less restrictive license than
153 GPL, as explained in the first question above.</p>
154 </div>
155
156 <!-- *********************************************************************** -->
157 <div class="doc_section">
158   <a name="source">Source Code</a>
159 </div>
160 <!-- *********************************************************************** -->
161
162 <div class="question">
163 <p>In what language is LLVM written?</p>
164 </div>
165
166 <div class="answer">
167 <p>All of the LLVM tools and libraries are written in C++ with extensive use of
168 the STL.</p>
169 </div>
170
171 <div class="question">
172 <p>How portable is the LLVM source code?</p>
173 </div>
174
175 <div class="answer">
176 <p>The LLVM source code should be portable to most modern UNIX-like operating
177 systems.  Most of the code is written in standard C++ with operating system
178 services abstracted to a support library.  The tools required to build and test
179 LLVM have been ported to a plethora of platforms.</p>
180
181 <p>Some porting problems may exist in the following areas:</p>
182
183 <ul>
184
185   <li>The GCC front end code is not as portable as the LLVM suite, so it may not
186   compile as well on unsupported platforms.</li>
187
188   <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
189   Shell and sed.  Porting to systems without these tools (MacOS 9, Plan 9) will
190   require more effort.</li>
191
192 </ul>
193
194 </div>
195
196 <!-- *********************************************************************** -->
197 <div class="doc_section">
198   <a name="build">Build Problems</a>
199 </div>
200 <!-- *********************************************************************** -->
201
202 <div class="question">
203 <p>When I run configure, it finds the wrong C compiler.</p>
204 </div>
205
206 <div class="answer">
207
208 <p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
209 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt> and <tt>CXX</tt>
210 for the C and C++ compiler, respectively.</p>
211
212 <p>If <tt>configure</tt> finds the wrong compiler, either adjust your
213 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
214 explicitly.</p>
215
216 </div>
217
218 <div class="question">
219 <p>I compile the code, and I get some error about <tt>/localhome</tt>.</p>
220 </div>
221
222 <div class="answer">
223
224 <p>There are several possible causes for this.  The first is that you didn't set
225 a pathname properly when using <tt>configure</tt>, and it defaulted to a
226 pathname that we use on our research machines.</p>
227
228 <p>Another possibility is that we hardcoded a path in our Makefiles.  If you see
229 this, please email the LLVM bug mailing list with the name of the offending
230 Makefile and a description of what is wrong with it.</p>
231
232 </div>
233
234 <div class="question">
235 <p>The <tt>configure</tt> script finds the right C compiler, but it uses the
236 LLVM linker from a previous build.  What do I do?</p>
237 </div>
238
239 <div class="answer">
240 <p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
241 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
242 it:</p>
243
244 <ol>
245                 
246   <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
247   program appears first in the <tt>PATH</tt>.  This may work, but may not be
248   convenient when you want them <i>first</i> in your path for other
249   work.</p></li>
250
251   <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
252   correct. In a Borne compatible shell, the syntax would be:</p>
253                 
254       <p><tt>PATH=[the path without the bad program] ./configure ...</tt></p>
255
256       <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
257       to do its work without having to adjust your <tt>PATH</tt>
258       permanently.</p></li>
259         
260 </ol>
261
262 </div>
263
264 <div class="question">
265 <p>When creating a dynamic library, I get a strange GLIBC error.</p>
266 </div>
267
268 <div class="answer">
269 <p>Under some operating systems (i.e. Linux), libtool does not work correctly if
270 GCC was compiled with the --disable-shared option.  To work around this, install
271 your own version of GCC that has shared libraries enabled by default.</p>
272 </div>
273
274 <div class="question">
275 <p>I've updated my source tree from CVS, and now my build is trying to use a
276 file/directory that doesn't exist.</p>
277 </div>
278
279 <div class="answer">
280 <p>You need to re-run configure in your object directory.  When new Makefiles
281 are added to the source tree, they have to be copied over to the object tree in
282 order to be used by the build.</p>
283 </div>
284
285 <div class="question">
286 <p>I've modified a Makefile in my source tree, but my build tree keeps using the
287 old version.  What do I do?</p>
288 </div>
289
290 <div class="answer">
291
292 <p>If the Makefile already exists in your object tree, you
293 can just run the following command in the top level directory of your object
294 tree:</p>
295
296 <p><tt>./config.status &lt;relative path to Makefile&gt;</tt><p>
297
298 <p>If the Makefile is new, you will have to modify the configure script to copy
299 it over.</p>
300
301 </div>
302
303 <div class="question">
304 <p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
305 </div>
306
307 <div class="answer">
308
309 <p>Sometimes, changes to the LLVM source code alters how the build system works.
310 Changes in libtool, autoconf, or header file dependencies are especially prone
311 to this sort of problem.</p>
312
313 <p>The best thing to try is to remove the old files and re-build.  In most
314 cases, this takes care of the problem.  To do this, just type <tt>make
315 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
316
317 </div>
318
319 <div class="question">
320 <p>I've built LLVM and am testing it, but the tests freeze.</p>
321 </div>
322
323 <div class="answer">
324
325 <p>This is most likely occurring because you built a profile or release
326 (optimized) build of LLVM and have not specified the same information on the
327 <tt>gmake</tt> command line.</p>
328
329 <p>For example, if you built LLVM with the command:</p>
330
331 <p><tt>gmake ENABLE_PROFILING=1</tt>
332
333 <p>...then you must run the tests with the following commands:</p>
334
335 <p><tt>cd llvm/test<br>gmake  ENABLE_PROFILING=1</tt></p>
336
337 </div>
338
339 <div class="question">
340 <p>Why do test results differ when I perform different types of builds?</p>
341 </div>
342
343 <div class="answer">
344
345 <p>The LLVM test suite is dependent upon several features of the LLVM tools and
346 libraries.</p>
347
348 <p>First, the debugging assertions in code are not enabled in optimized or
349 profiling builds.  Hence, tests that used to fail may pass.</p>
350         
351 <p>Second, some tests may rely upon debugging options or behavior that is only
352 available in the debug build.  These tests will fail in an optimized or profile
353 build.</p>
354
355 </div>
356
357 <div class="question">
358 <p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
359 </div>
360
361 <div class="answer">
362 <p>This is <a href="http://gcc.gnu.org/PR?13392">a bug in GCC</a>, and 
363    affects projects other than LLVM.  Try upgrading or downgrading your GCC.</p>
364 </div>
365
366 <div class="question">
367 <p>After CVS update, rebuilding gives the error "No rule to make target".</p>
368 </div>
369
370 <div class="answer">
371 <p>If the error is of the form:</p>
372
373 <div class="doc_code">
374 <tt>
375 gmake[2]: *** No rule to make target `/path/to/somefile', needed by
376 `/path/to/another/file.d'.<br>
377 Stop.
378 </tt>
379 </div>
380
381 <p>This may occur anytime files are moved within the CVS repository or removed
382 entirely.  In this case, the best solution is to erase all <tt>.d</tt> files,
383 which list dependencies for source files, and rebuild:</p>
384
385 <div class="doc_code">
386 <pre>
387 % cd $LLVM_OBJ_DIR
388 % rm -f `find . -name \*\.d` 
389 % gmake 
390 </pre>
391 </div>
392
393 <p>In other cases, it may be necessary to run <tt>make clean</tt> before
394 rebuilding.</p>
395 </div>
396
397 <!-- *********************************************************************** -->
398 <div class="doc_section"><a name="felangs">Source Languages</a></div>
399
400 <div class="question"><p>
401   <a name="langs">What source languages are supported?</a></p>
402 </div>
403 <div class="answer">
404   <p>LLVM currently has full support for C and C++ source languages. These are
405   available through a special version of GCC that LLVM calls the 
406   <a href="#cfe">C Front End</a></p>
407   <p>There is an incomplete version of a Java front end available in the
408   <tt>llvm-java</tt> CVS repository. There is no documentation on this yet so
409   you'll need to download the code, compile it, and try it.</p>
410   <p>In the <tt>examples/BFtoLLVM</tt> directory is a translator for the 
411   BrainF*** language (2002 Language Specification).</p>
412   <p>In the <tt>projects/Stacker</tt> directory is a compiler and runtime
413   library for the Stacker language, a "toy" language loosely based on Forth.</p>
414   <p>The PyPy developers are working on integrating LLVM into the PyPy backend
415   so that PyPy language can translate to LLVM.</p>
416 </div>
417 <div class="question"><a name="langhlsupp">
418   <p>What support is there for a higher level source language constructs for 
419   building a compiler?</a></p>
420 </div>
421 <div class="answer">
422   <p>Currently, there isn't much. LLVM supports an intermediate representation
423   which is useful for code representation but will not support the high level
424   (abstract syntax tree) representation needed by most compilers. There are no
425   facilities for lexical nor semantic analysis. There is, however, a <i>mostly
426     implemented</i> configuration-driven 
427   <a href="CompilerDriver.html">compiler driver</a> which simplifies the task
428   of running optimizations, linking, and executable generation.</p>
429 </div>
430
431 <div class="question"><a name="langhlsupp">
432   <p>I don't understand the GetElementPtr
433       instruction. Help!</a></p>
434 </div>
435 <div class="answer">
436   <p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
437    Instruction</a>.</li>
438 </div>
439
440 <!-- *********************************************************************** -->
441 <div class="doc_section">
442   <a name="cfe">Using the GCC Front End</a>
443 </div>
444
445 <div class="question">
446 <p>
447 When I compile software that uses a configure script, the configure script
448 thinks my system has all of the header files and libraries it is testing for.
449 How do I get configure to work correctly?
450 </p>
451 </div>
452
453 <div class="answer">
454 <p>
455 The configure script is getting things wrong because the LLVM linker allows
456 symbols to be undefined at link time (so that they can be resolved during JIT
457 or translation to the C back end).  That is why configure thinks your system
458 "has everything."
459 </p>
460 <p>
461 To work around this, perform the following steps:
462 </p>
463
464 <ol>
465   <li>
466   Make sure the CC and CXX environment variables contains the full path to the
467   LLVM GCC front end.
468   </li>
469
470   <li>
471   Make sure that the regular C compiler is first in your PATH.
472   </li>
473
474   <li>
475   Add the string "-Wl,-native" to your CFLAGS environment variable.
476   </li>
477 </ol>
478
479 <p>
480 This will allow the gccld linker to create a native code executable instead of
481 a shell script that runs the JIT.  Creating native code requires standard
482 linkage, which in turn will allow the configure script to find out if code is
483 not linking on your system because the feature isn't available on your system.
484 </p>
485 </div>
486
487 <div class="question">
488 <p>
489 When I compile code using the LLVM GCC front end, it complains that it cannot
490 find libcrtend.a.
491 </p>
492 </div>
493
494 <div class="answer">
495 <p>
496 The only way this can happen is if you haven't installed the runtime library. To
497 correct this, do:</p>
498 <pre>
499   % cd llvm/runtime
500   % make clean ; make install-bytecode
501 </pre>
502 </div>
503
504 <div class="question">
505 <p>
506 How can I disable all optimizations when compiling code using the LLVM GCC front end?
507 </p>
508 </div>
509
510 <div class="answer">
511 <p>
512 Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
513 optimizations done at the llvm level, leaving you with the truly horrible
514 code that you desire.
515 </p>
516 </div>
517
518
519 <div class="question">
520 <p>
521 <a name="translatec++">Can I use LLVM to convert C++ code to C code?</a>
522 </p>
523 </div>
524
525 <div class="answer">
526 <p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
527 Note that the generated C code will be very low level (all loops are lowered
528 to gotos, etc) and not very pretty (comments are stripped, original source
529 formatting is totally lost, variables are renamed, expressions are regrouped), 
530 so this may not be what you're looking for.  However, this is a good way to add
531 C++ support for a processor that does not otherwise have a C++ compiler.
532 </p>
533
534 <p>Use commands like this:</p>
535
536 <ol>
537 <li><p>Compile your program as normal with llvm-g++:</p></li>
538
539 <div class="doc_code">$ llvm-g++ x.cpp -o program</div>
540
541 <p>or:</p>
542
543 <div class="doc_code">
544  llvm-g++ a.cpp -c<br>
545  llvm-g++ b.cpp -c<br>
546  llvm-g++ a.o b.o -o program
547 </div>
548
549 <p>With llvm-gcc3, this will generate program and program.bc.  The .bc file is 
550 the LLVM version of the program all linked together.</p>
551
552 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
553 backend:</p></li>
554
555 <div class="doc_code">$ llc -march=c program.bc -o program.c</div>
556
557 <li><p>Finally, compile the c file:</p></li>
558
559 <div class="doc_code">$ cc x.c</div>
560
561 </ol>
562
563 <p>Note that, by default, the C backend does not support exception handling.
564 If you want/need it for a certain program, you can enable it by passing
565 "-enable-correct-eh-support" to the llc program.  The resultant code will
566 use setjmp/longjmp to implement exception support that is correct but
567 relatively slow.
568 </p>
569
570 <p>Also note: this specific sequence of commands won't work if you use a 
571 function defined in the C++ runtime library (or any other C++ library).  To 
572 access an external C++ library, you must manually 
573 compile libstdc++ to LLVM bytecode, statically link it into your program, then
574 use the commands above to convert the whole result into C code.  Alternatively,
575 you can compile the libraries and your application into two different chunks
576 of C code and link them.</p>
577
578 </div>
579
580
581
582 <!-- *********************************************************************** -->
583 <div class="doc_section">
584   <a name="cfe_code">Questions about code generated by the GCC front-end</a>
585 </div>
586
587 <div class="question"><p>
588 <a name="__main"></a>
589 What is this <tt>__main()</tt> call that gets inserted into <tt>main()</tt>?
590 </p></div>
591
592 <div class="answer">
593 <p>
594 The <tt>__main</tt> call is inserted by the C/C++ compiler in order to guarantee
595 that static constructors and destructors are called when the program starts up
596 and shuts down.  In C, you can create static constructors and destructors by
597 using GCC extensions, and in C++ you can do so by creating a global variable
598 whose class has a ctor or dtor.
599 </p>
600
601 <p>
602 The actual implementation of <tt>__main</tt> lives in the
603 <tt>llvm/runtime/GCCLibraries/crtend/</tt> directory in the source-base, and is
604 linked in automatically when you link the program.
605 </p>
606 </div>
607
608 <!--=========================================================================-->
609
610 <div class="question">
611 <a name="iosinit"></a>
612 <p> What is this <tt>llvm.global_ctors</tt> and
613 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I #include
614 &lt;iostream&gt;?</p>
615 </div>
616
617 <div class="answer">
618
619 <p>If you #include the &lt;iostream&gt; header into a C++ translation unit, the
620 file will probably use the <tt>std::cin</tt>/<tt>std::cout</tt>/... global
621 objects.  However, C++ does not guarantee an order of initialization between
622 static objects in different translation units, so if a static ctor/dtor in your
623 .cpp file used <tt>std::cout</tt>, for example, the object would not necessarily
624 be automatically initialized before your use.</p>
625
626 <p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
627 STL that we use declares a static object that gets created in every translation
628 unit that includes <tt>&lt;iostream&gt;</tt>.  This object has a static
629 constructor and destructor that initializes and destroys the global iostream
630 objects before they could possibly be used in the file.  The code that you see
631 in the .ll file corresponds to the constructor and destructor registration code.
632 </p>
633
634 <p>If you would like to make it easier to <b>understand</b> the LLVM code
635 generated by the compiler in the demo page, consider using <tt>printf()</tt>
636 instead of <tt>iostream</tt>s to print values.</p>
637
638 </div>
639
640 <!--=========================================================================-->
641
642 <div class="question"><p>
643 <a name="codedce"></a>
644 Where did all of my code go??
645 </p></div>
646
647 <div class="answer">
648 <p>
649 If you are using the LLVM demo page, you may often wonder what happened to all
650 of the code that you typed in.  Remember that the demo script is running the
651 code through the LLVM optimizers, so if your code doesn't actually do anything
652 useful, it might all be deleted.
653 </p>
654
655 <p>
656 To prevent this, make sure that the code is actually needed.  For example, if
657 you are computing some expression, return the value from the function instead of
658 leaving it in a local variable.  If you really want to constrain the optimizer,
659 you can read from and assign to <tt>volatile</tt> global variables.
660 </p>
661 </div>
662
663 <!--=========================================================================-->
664
665 <div class="question"><p>
666 <a name="undef"></a>
667 <p>What is this "<tt>undef</tt>" thing that shows up in my code?
668 </p></div>
669
670 <div class="answer">
671 <p>
672 <a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of representing
673 a value that is not defined.  You can get these if you do not initialize a 
674 variable before you use it.  For example, the C function:</p>
675
676 <div class="doc_code">
677   <tt>int X() { int i; return i; }</tt>
678 </div>
679
680 <p>Is compiled to "<tt>ret int undef</tt>" because "i" never has a value 
681 specified for it.
682 </p>
683 </div>
684
685 <!-- *********************************************************************** -->
686
687 <hr>
688 <address>
689   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
690   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
691   <a href="http://validator.w3.org/check/referer"><img
692   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
693
694   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
695   Last modified: $Date$
696 </address>
697
698 </body>
699 </html>