blob: 60192a99eebf2c8e2e41bd072c7cd06432657f95 [file] [log] [blame]
Misha Brukmana6538852003-11-06 21:55:44 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Bill Wendling290235f2009-04-07 18:40:56 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Misha Brukmana6538852003-11-06 21:55:44 +00006 <title>LLVM: Frequently Asked Questions</title>
Misha Brukman7ce62cc2004-06-01 18:51:03 +00007 <style type="text/css">
Daniel Dunbaradea4972012-04-19 20:20:34 +00008 @import url("_static/llvm.css");
Misha Brukmana6538852003-11-06 21:55:44 +00009 .question { font-weight: bold }
10 .answer { margin-left: 2em }
11 </style>
12</head>
13<body>
John Criswellc310f622003-10-13 16:13:06 +000014
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000015<h1>
Misha Brukmana6538852003-11-06 21:55:44 +000016 LLVM: Frequently Asked Questions
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000017</h1>
Misha Brukmana6538852003-11-06 21:55:44 +000018
19<ol>
20 <li><a href="#license">License</a>
21 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000022 <li>Does the University of Illinois Open Source License really qualify as an
23 "open source" license?</li>
24
25 <li>Can I modify LLVM source code and redistribute the modified source?</li>
26
27 <li>Can I modify LLVM source code and redistribute binaries or other tools
28 based on it, without redistributing the source?</li>
Misha Brukmana6538852003-11-06 21:55:44 +000029 </ol></li>
30
31 <li><a href="#source">Source code</a>
32 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000033 <li>In what language is LLVM written?</li>
34
35 <li>How portable is the LLVM source code?</li>
Misha Brukmana6538852003-11-06 21:55:44 +000036 </ol></li>
37
38 <li><a href="#build">Build Problems</a>
39 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000040 <li>When I run configure, it finds the wrong C compiler.</li>
41
42 <li>The <tt>configure</tt> script finds the right C compiler, but it uses
43 the LLVM linker from a previous build. What do I do?</li>
44
45 <li>When creating a dynamic library, I get a strange GLIBC error.</li>
46
47 <li>I've updated my source tree from Subversion, and now my build is trying
48 to use a file/directory that doesn't exist.</li>
49
50 <li>I've modified a Makefile in my source tree, but my build tree keeps
51 using the old version. What do I do?</li>
52
53 <li>I've upgraded to a new version of LLVM, and I get strange build
54 errors.</li>
55
56 <li>I've built LLVM and am testing it, but the tests freeze.</li>
57
58 <li>Why do test results differ when I perform different types of
59 builds?</li>
60
61 <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
62
63 <li>Compiling LLVM with GCC succeeds, but the resulting tools do not work,
64 what can be wrong?</li>
65
Bill Wendling290235f2009-04-07 18:40:56 +000066 <li>After Subversion update, rebuilding gives the error "No rule to make
67 target".</li>
68
Misha Brukmana6538852003-11-06 21:55:44 +000069 </ol></li>
John Criswell76c1e382003-11-18 16:08:49 +000070
Reid Spencer501bfee2006-04-26 14:52:19 +000071 <li><a href="#felangs">Source Languages</a>
72 <ol>
73 <li><a href="#langs">What source languages are supported?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +000074
Gordon Henriksene5079052008-02-22 21:55:51 +000075 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
Bill Wendling290235f2009-04-07 18:40:56 +000076 should I interface with the LLVM middle-end optimizers and back-end code
77 generators?</a></li>
78
Reid Spencer501bfee2006-04-26 14:52:19 +000079 <li><a href="#langhlsupp">What support is there for higher level source
Bill Wendling290235f2009-04-07 18:40:56 +000080 language constructs for building a compiler?</a></li>
81
Reid Spencere00906f2006-08-10 20:15:58 +000082 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
83 instruction. Help!</a></li>
Reid Spencer501bfee2006-04-26 14:52:19 +000084 </ol>
85
Michael J. Spencer75338092012-04-19 19:27:54 +000086 <li><a href="#cfe">Using the C and C++ Front Ends</a>
John Criswell76c1e382003-11-18 16:08:49 +000087 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000088 <li><a href="#platformindependent">Can I compile C or C++ code to
89 platform-independent LLVM bitcode?</a></li>
John Criswell76c1e382003-11-18 16:08:49 +000090 </ol>
91 </li>
Chris Lattnercc33d702003-11-19 05:53:12 +000092
Michael J. Spencer75338092012-04-19 19:27:54 +000093 <li><a href="#cfe_code">Questions about code generated by the demo page</a>
Chris Lattnercc33d702003-11-19 05:53:12 +000094 <ol>
Chris Lattner5a53c5d2005-02-25 20:30:21 +000095 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
Michael J. Spencer75338092012-04-19 19:27:54 +000096 <tt>_GLOBAL__I_a...</tt> stuff that happens when I
Chris Lattner5a53c5d2005-02-25 20:30:21 +000097 #include &lt;iostream&gt;?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +000098
Chris Lattner5a53c5d2005-02-25 20:30:21 +000099 <li><a href="#codedce">Where did all of my code go??</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +0000100
101 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in
102 my code?</a></li>
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000103
104 <li><a href="#callconvwrong">Why does instcombine + simplifycfg turn
105 a call to a function with a mismatched calling convention into "unreachable"?
106 Why not make the verifier reject it?</a></li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000107 </ol>
108 </li>
Misha Brukmana6538852003-11-06 21:55:44 +0000109</ol>
110
Chris Lattner7911ce22004-05-23 21:07:27 +0000111<div class="doc_author">
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000112 <p>Written by <a href="http://llvm.org/">The LLVM Team</a></p>
Chris Lattner7911ce22004-05-23 21:07:27 +0000113</div>
114
115
Misha Brukmana6538852003-11-06 21:55:44 +0000116<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000117<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000118 <a name="license">License</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000119</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000120<!-- *********************************************************************** -->
121
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000122<div>
123
Misha Brukmana6538852003-11-06 21:55:44 +0000124<div class="question">
Misha Brukmana6538852003-11-06 21:55:44 +0000125<p>Does the University of Illinois Open Source License really qualify as an
Bill Wendling290235f2009-04-07 18:40:56 +0000126 "open source" license?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000127</div>
128
129<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000130<p>Yes, the license
131 is <a href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by
132 the Open Source Initiative (OSI).</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000133</div>
134
135<div class="question">
136<p>Can I modify LLVM source code and redistribute the modified source?</p>
137</div>
138
139<div class="answer">
140<p>Yes. The modified source distribution must retain the copyright notice and
Bill Wendling290235f2009-04-07 18:40:56 +0000141 follow the three bulletted conditions listed in
142 the <a href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM
143 license</a>.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000144</div>
145
146<div class="question">
147<p>Can I modify LLVM source code and redistribute binaries or other tools based
Bill Wendling290235f2009-04-07 18:40:56 +0000148 on it, without redistributing the source?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000149</div>
150
151<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000152<p>Yes. This is why we distribute LLVM under a less restrictive license than
153 GPL, as explained in the first question above.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000154</div>
155
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000156</div>
157
Misha Brukmana6538852003-11-06 21:55:44 +0000158<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000159<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000160 <a name="source">Source Code</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000161</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000162<!-- *********************************************************************** -->
163
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000164<div>
165
Misha Brukmana6538852003-11-06 21:55:44 +0000166<div class="question">
167<p>In what language is LLVM written?</p>
168</div>
169
170<div class="answer">
171<p>All of the LLVM tools and libraries are written in C++ with extensive use of
Bill Wendling290235f2009-04-07 18:40:56 +0000172 the STL.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000173</div>
174
175<div class="question">
176<p>How portable is the LLVM source code?</p>
177</div>
178
179<div class="answer">
180<p>The LLVM source code should be portable to most modern UNIX-like operating
181systems. Most of the code is written in standard C++ with operating system
182services abstracted to a support library. The tools required to build and test
183LLVM have been ported to a plethora of platforms.</p>
184
185<p>Some porting problems may exist in the following areas:</p>
186
187<ul>
Michael J. Spencer75338092012-04-19 19:27:54 +0000188 <li>The autoconf/makefile build system relies heavily on UNIX shell tools,
189 like the Bourne Shell and sed. Porting to systems without these tools
190 (MacOS 9, Plan 9) Will require more effort.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000191</ul>
192
193</div>
194
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000195</div>
196
Misha Brukmana6538852003-11-06 21:55:44 +0000197<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000198<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000199 <a name="build">Build Problems</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000200</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000201<!-- *********************************************************************** -->
202
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000203<div>
204
Misha Brukmana6538852003-11-06 21:55:44 +0000205<div class="question">
206<p>When I run configure, it finds the wrong C compiler.</p>
207</div>
208
209<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000210<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
Bill Wendling290235f2009-04-07 18:40:56 +0000211 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt>
212 and <tt>CXX</tt> for the C and C++ compiler, respectively.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000213
214<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
Bill Wendling290235f2009-04-07 18:40:56 +0000215 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
216 explicitly.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000217
218</div>
219
220<div class="question">
Misha Brukmana6538852003-11-06 21:55:44 +0000221<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
Michael J. Spencer75338092012-04-19 19:27:54 +0000222 LLVM tools from a previous build. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000223</div>
224
225<div class="answer">
226<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
Bill Wendling290235f2009-04-07 18:40:56 +0000227 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
228 it:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000229
230<ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000231 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000232 program appears first in the <tt>PATH</tt>. This may work, but may not be
233 convenient when you want them <i>first</i> in your path for other
234 work.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000235
236 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Joel Jones93282d22012-04-20 16:08:56 +0000237 correct. In a Bourne compatible shell, the syntax would be:</p>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000238
Misha Brukmana54d4b22008-12-17 18:11:40 +0000239<pre class="doc_code">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000240% PATH=[the path without the bad program] ./configure ...
241</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000242
243 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000244 to do its work without having to adjust your <tt>PATH</tt>
245 permanently.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000246</ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000247</div>
248
249<div class="question">
250<p>When creating a dynamic library, I get a strange GLIBC error.</p>
251</div>
252
253<div class="answer">
254<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
Bill Wendling290235f2009-04-07 18:40:56 +0000255 GCC was compiled with the --disable-shared option. To work around this,
256 install your own version of GCC that has shared libraries enabled by
257 default.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000258</div>
259
260<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000261<p>I've updated my source tree from Subversion, and now my build is trying to
262 use a file/directory that doesn't exist.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000263</div>
264
265<div class="answer">
266<p>You need to re-run configure in your object directory. When new Makefiles
Bill Wendling290235f2009-04-07 18:40:56 +0000267 are added to the source tree, they have to be copied over to the object tree
268 in order to be used by the build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000269</div>
270
271<div class="question">
272<p>I've modified a Makefile in my source tree, but my build tree keeps using the
Bill Wendling290235f2009-04-07 18:40:56 +0000273 old version. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000274</div>
275
276<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000277<p>If the Makefile already exists in your object tree, you can just run the
278 following command in the top level directory of your object tree:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000279
Misha Brukmana54d4b22008-12-17 18:11:40 +0000280<pre class="doc_code">
281% ./config.status &lt;relative path to Makefile&gt;
282</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000283
284<p>If the Makefile is new, you will have to modify the configure script to copy
Bill Wendling290235f2009-04-07 18:40:56 +0000285 it over.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000286</div>
287
288<div class="question">
289<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
290</div>
291
292<div class="answer">
293
294<p>Sometimes, changes to the LLVM source code alters how the build system works.
Bill Wendling290235f2009-04-07 18:40:56 +0000295 Changes in libtool, autoconf, or header file dependencies are especially
296 prone to this sort of problem.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000297
298<p>The best thing to try is to remove the old files and re-build. In most
Bill Wendling290235f2009-04-07 18:40:56 +0000299 cases, this takes care of the problem. To do this, just type <tt>make
300 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000301</div>
302
303<div class="question">
304<p>I've built LLVM and am testing it, but the tests freeze.</p>
305</div>
306
307<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000308<p>This is most likely occurring because you built a profile or release
Bill Wendling290235f2009-04-07 18:40:56 +0000309 (optimized) build of LLVM and have not specified the same information on the
310 <tt>gmake</tt> command line.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000311
312<p>For example, if you built LLVM with the command:</p>
313
Misha Brukmana54d4b22008-12-17 18:11:40 +0000314<pre class="doc_code">
315% gmake ENABLE_PROFILING=1
316</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000317
318<p>...then you must run the tests with the following commands:</p>
319
Misha Brukmana54d4b22008-12-17 18:11:40 +0000320<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000321% cd llvm/test
322% gmake ENABLE_PROFILING=1
323</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000324</div>
325
326<div class="question">
327<p>Why do test results differ when I perform different types of builds?</p>
328</div>
329
330<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000331<p>The LLVM test suite is dependent upon several features of the LLVM tools and
Bill Wendling290235f2009-04-07 18:40:56 +0000332 libraries.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000333
334<p>First, the debugging assertions in code are not enabled in optimized or
Bill Wendling290235f2009-04-07 18:40:56 +0000335 profiling builds. Hence, tests that used to fail may pass.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000336
337<p>Second, some tests may rely upon debugging options or behavior that is only
Bill Wendling290235f2009-04-07 18:40:56 +0000338 available in the debug build. These tests will fail in an optimized or
339 profile build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000340</div>
341
Chris Lattner8a0b9242003-12-08 05:43:19 +0000342<div class="question">
Chris Lattner306acee2003-12-22 04:06:12 +0000343<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000344</div>
345
346<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000347<p>This is <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392">a bug in
348 GCC</a>, and affects projects other than LLVM. Try upgrading or downgrading
349 your GCC.</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000350</div>
351
John Criswelld1799612004-03-29 20:23:11 +0000352<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000353<p>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what
354 can be wrong?</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000355</div>
356
357<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000358<p>Several versions of GCC have shown a weakness in miscompiling the LLVM
359 codebase. Please consult your compiler version (<tt>gcc --version</tt>) to
360 find out whether it is <a href="GettingStarted.html#brokengcc">broken</a>.
361 If so, your only option is to upgrade GCC to a known good version.</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000362</div>
363
364<div class="question">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000365<p>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendling290235f2009-04-07 18:40:56 +0000366 target".</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000367</div>
368
369<div class="answer">
370<p>If the error is of the form:</p>
371
Misha Brukmana54d4b22008-12-17 18:11:40 +0000372<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000373gmake[2]: *** No rule to make target `/path/to/somefile', needed by
374`/path/to/another/file.d'.<br>
375Stop.
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000376</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000377
Bill Wendling290235f2009-04-07 18:40:56 +0000378<p>This may occur anytime files are moved within the Subversion repository or
379 removed entirely. In this case, the best solution is to erase all
380 <tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000381
Misha Brukmana54d4b22008-12-17 18:11:40 +0000382<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000383% cd $LLVM_OBJ_DIR
384% rm -f `find . -name \*\.d`
385% gmake
386</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000387
388<p>In other cases, it may be necessary to run <tt>make clean</tt> before
Bill Wendling290235f2009-04-07 18:40:56 +0000389 rebuilding.</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000390</div>
391
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000392</div>
393
Misha Brukmana6538852003-11-06 21:55:44 +0000394<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000395<h2>
396 <a name="felangs">Source Languages</a>
397</h2>
Reid Spencer501bfee2006-04-26 14:52:19 +0000398
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000399<div>
400
Bill Wendling290235f2009-04-07 18:40:56 +0000401<div class="question">
402<p><a name="langs">What source languages are supported?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000403</div>
Gordon Henriksen58366822008-02-22 20:58:29 +0000404
Gordon Henriksen58366822008-02-22 20:58:29 +0000405<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000406<p>LLVM currently has full support for C and C++ source languages. These are
Michael J. Spencer75338092012-04-19 19:27:54 +0000407 available through both <a href="http://clang.llvm.org/">Clang</a> and
408 <a href="http://dragonegg.llvm.org/">DragonEgg</a>.</p>
Bill Wendling290235f2009-04-07 18:40:56 +0000409
410<p>The PyPy developers are working on integrating LLVM into the PyPy backend so
411 that PyPy language can translate to LLVM.</p>
Gordon Henriksen58366822008-02-22 20:58:29 +0000412</div>
413
Bill Wendling290235f2009-04-07 18:40:56 +0000414<div class="question">
415<p><a name="langirgen">I'd like to write a self-hosting LLVM compiler. How
416 should I interface with the LLVM middle-end optimizers and back-end code
417 generators?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000418</div>
Chris Lattner33bef482006-08-15 00:43:35 +0000419
Chris Lattner33bef482006-08-15 00:43:35 +0000420<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000421<p>Your compiler front-end will communicate with LLVM by creating a module in
422 the LLVM intermediate representation (IR) format. Assuming you want to write
423 your language's compiler in the language itself (rather than C++), there are
424 3 major ways to tackle generating LLVM IR from a front-end:</p>
425
426<ul>
427 <li><strong>Call into the LLVM libraries code using your language's FFI
428 (foreign function interface).</strong>
429
430 <ul>
431 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax, and .bc
432 format</li>
433
434 <li><em>for:</em> enables running LLVM optimization passes without a
435 emit/parse overhead</li>
436
437 <li><em>for:</em> adapts well to a JIT context</li>
438
439 <li><em>against:</em> lots of ugly glue code to write</li>
440 </ul></li>
441
442 <li> <strong>Emit LLVM assembly from your compiler's native language.</strong>
443 <ul>
444 <li><em>for:</em> very straightforward to get started</li>
445
446 <li><em>against:</em> the .ll parser is slower than the bitcode reader
447 when interfacing to the middle end</li>
448
449 <li><em>against:</em> you'll have to re-engineer the LLVM IR object model
450 and asm writer in your language</li>
451
452 <li><em>against:</em> it may be harder to track changes to the IR</li>
453 </ul></li>
454
455 <li><strong>Emit LLVM bitcode from your compiler's native language.</strong>
456
457 <ul>
458 <li><em>for:</em> can use the more-efficient bitcode reader when
459 interfacing to the middle end</li>
460
461 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
462 model and bitcode writer in your language</li>
463
464 <li><em>against:</em> it may be harder to track changes to the IR</li>
465 </ul></li>
466</ul>
467
468<p>If you go with the first option, the C bindings in include/llvm-c should help
469 a lot, since most languages have strong support for interfacing with C. The
470 most common hurdle with calling C from managed code is interfacing with the
471 garbage collector. The C interface was designed to require very little memory
472 management, and so is straightforward in this regard.</p>
473</div>
474
475<div class="question">
476<p><a name="langhlsupp">What support is there for a higher level source language
477 constructs for building a compiler?</a></p>
478</div>
479
480<div class="answer">
481<p>Currently, there isn't much. LLVM supports an intermediate representation
482 which is useful for code representation but will not support the high level
483 (abstract syntax tree) representation needed by most compilers. There are no
Eric Christophereae5a132011-09-20 00:34:27 +0000484 facilities for lexical nor semantic analysis.</p>
Bill Wendling290235f2009-04-07 18:40:56 +0000485</div>
486
487<div class="question">
488<p><a name="getelementptr">I don't understand the GetElementPtr
489 instruction. Help!</a></p>
490</div>
491
492<div class="answer">
493<p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000494 Instruction</a>.</p>
Chris Lattner33bef482006-08-15 00:43:35 +0000495</div>
496
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000497</div>
498
Reid Spencer501bfee2006-04-26 14:52:19 +0000499<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000500<h2>
Michael J. Spencer75338092012-04-19 19:27:54 +0000501 <a name="cfe">Using the C and C++ Front Ends</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000502</h2>
John Criswell6ea30b02003-11-18 16:05:23 +0000503
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000504<div>
505
John Criswell6ea30b02003-11-18 16:05:23 +0000506<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000507<p><a name="platformindependent">Can I compile C or C++ code to
508 platform-independent LLVM bitcode?</a></p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000509</div>
510
511<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000512<p>No. C and C++ are inherently platform-dependent languages. The most obvious
513 example of this is the preprocessor. A very common way that C code is made
514 portable is by using the preprocessor to include platform-specific code. In
515 practice, information about other platforms is lost after preprocessing, so
516 the result is inherently dependent on the platform that the preprocessing was
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000517 targeting.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000518
Bill Wendling290235f2009-04-07 18:40:56 +0000519<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt> to
520 vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded to
Misha Brukman7e0fc8a2009-04-10 20:48:27 +0000521 a constant immediately, thus hard-wiring a platform-specific detail.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000522
Bill Wendling290235f2009-04-07 18:40:56 +0000523<p>Also, since many platforms define their ABIs in terms of C, and since LLVM is
524 lower-level than C, front-ends currently must emit platform-specific IR in
525 order to have the result conform to the platform ABI.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000526</div>
527
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000528</div>
529
Chris Lattnercc33d702003-11-19 05:53:12 +0000530<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000531<h2>
Michael J. Spencer75338092012-04-19 19:27:54 +0000532 <a name="cfe_code">Questions about code generated by the demo page</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000533</h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000534
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000535<div>
536
Misha Brukman237dc2a2004-12-03 23:58:18 +0000537<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000538<p><a name="iosinit">What is this <tt>llvm.global_ctors</tt> and
Michael J. Spencer75338092012-04-19 19:27:54 +0000539 <tt>_GLOBAL__I_a...</tt> stuff that happens when I <tt>#include
Bill Wendling290235f2009-04-07 18:40:56 +0000540 &lt;iostream&gt;</tt>?</a></p>
Misha Brukman237dc2a2004-12-03 23:58:18 +0000541</div>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000542
543<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000544<p>If you <tt>#include</tt> the <tt>&lt;iostream&gt;</tt> header into a C++
545 translation unit, the file will probably use
546 the <tt>std::cin</tt>/<tt>std::cout</tt>/... global objects. However, C++
547 does not guarantee an order of initialization between static objects in
548 different translation units, so if a static ctor/dtor in your .cpp file
549 used <tt>std::cout</tt>, for example, the object would not necessarily be
550 automatically initialized before your use.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000551
Misha Brukman237dc2a2004-12-03 23:58:18 +0000552<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Bill Wendling290235f2009-04-07 18:40:56 +0000553 STL that we use declares a static object that gets created in every
554 translation unit that includes <tt>&lt;iostream&gt;</tt>. This object has a
555 static constructor and destructor that initializes and destroys the global
556 iostream objects before they could possibly be used in the file. The code
557 that you see in the .ll file corresponds to the constructor and destructor
558 registration code.
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000559</p>
560
Misha Brukman237dc2a2004-12-03 23:58:18 +0000561<p>If you would like to make it easier to <b>understand</b> the LLVM code
Bill Wendling290235f2009-04-07 18:40:56 +0000562 generated by the compiler in the demo page, consider using <tt>printf()</tt>
563 instead of <tt>iostream</tt>s to print values.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000564</div>
565
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000566<!--=========================================================================-->
567
Bill Wendling290235f2009-04-07 18:40:56 +0000568<div class="question">
569<p><a name="codedce">Where did all of my code go??</a></p>
570</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000571
572<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000573<p>If you are using the LLVM demo page, you may often wonder what happened to
574 all of the code that you typed in. Remember that the demo script is running
575 the code through the LLVM optimizers, so if your code doesn't actually do
576 anything useful, it might all be deleted.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000577
Bill Wendling290235f2009-04-07 18:40:56 +0000578<p>To prevent this, make sure that the code is actually needed. For example, if
579 you are computing some expression, return the value from the function instead
580 of leaving it in a local variable. If you really want to constrain the
581 optimizer, you can read from and assign to <tt>volatile</tt> global
582 variables.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000583</div>
584
585<!--=========================================================================-->
586
Bill Wendling290235f2009-04-07 18:40:56 +0000587<div class="question">
588<p><a name="undef">What is this "<tt>undef</tt>" thing that shows up in my
Bill Wendlingb1a61bd2009-04-07 18:52:30 +0000589 code?</a></p>
Bill Wendling290235f2009-04-07 18:40:56 +0000590</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000591
592<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000593<p><a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of
594 representing a value that is not defined. You can get these if you do not
595 initialize a variable before you use it. For example, the C function:</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000596
Misha Brukmana54d4b22008-12-17 18:11:40 +0000597<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000598int X() { int i; return i; }
599</pre>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000600
Bill Wendling290235f2009-04-07 18:40:56 +0000601<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has a
602 value specified for it.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000603</div>
604
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000605<!--=========================================================================-->
606
607<div class="question">
608<p><a name="callconvwrong">Why does instcombine + simplifycfg turn
609 a call to a function with a mismatched calling convention into "unreachable"?
610 Why not make the verifier reject it?</a></p>
611</div>
612
613<div class="answer">
614<p>This is a common problem run into by authors of front-ends that are using
615custom calling conventions: you need to make sure to set the right calling
616convention on both the function and on each call to the function. For example,
617this code:</p>
618
619<pre class="doc_code">
620define fastcc void @foo() {
621 ret void
622}
623define void @bar() {
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000624 call void @foo()
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000625 ret void
626}
627</pre>
628
629<p>Is optimized to:</p>
630
631<pre class="doc_code">
632define fastcc void @foo() {
633 ret void
634}
635define void @bar() {
636 unreachable
637}
638</pre>
639
640<p>... with "opt -instcombine -simplifycfg". This often bites people because
641"all their code disappears". Setting the calling convention on the caller and
642callee is required for indirect calls to work, so people often ask why not make
643the verifier reject this sort of thing.</p>
644
645<p>The answer is that this code has undefined behavior, but it is not illegal.
646If we made it illegal, then every transformation that could potentially create
647this would have to ensure that it doesn't, and there is valid code that can
648create this sort of construct (in dead code). The sorts of things that can
649cause this to happen are fairly contrived, but we still need to accept them.
650Here's an example:</p>
651
652<pre class="doc_code">
653define fastcc void @foo() {
654 ret void
655}
656define internal void @bar(void()* %FP, i1 %cond) {
657 br i1 %cond, label %T, label %F
658T:
659 call void %FP()
660 ret void
661F:
662 call fastcc void %FP()
663 ret void
664}
665define void @test() {
666 %X = or i1 false, false
667 call void @bar(void()* @foo, i1 %X)
668 ret void
669}
670</pre>
671
672<p>In this example, "test" always passes @foo/false into bar, which ensures that
673 it is dynamically called with the right calling conv (thus, the code is
674 perfectly well defined). If you run this through the inliner, you get this
675 (the explicit "or" is there so that the inliner doesn't dead code eliminate
676 a bunch of stuff):
677</p>
678
679<pre class="doc_code">
680define fastcc void @foo() {
681 ret void
682}
683define void @test() {
684 %X = or i1 false, false
685 br i1 %X, label %T.i, label %F.i
686T.i:
687 call void @foo()
688 br label %bar.exit
689F.i:
690 call fastcc void @foo()
691 br label %bar.exit
692bar.exit:
693 ret void
694}
695</pre>
696
697<p>Here you can see that the inlining pass made an undefined call to @foo with
698 the wrong calling convention. We really don't want to make the inliner have
699 to know about this sort of thing, so it needs to be valid code. In this case,
700 dead code elimination can trivially remove the undefined code. However, if %X
701 was an input argument to @test, the inliner would produce this:
702</p>
703
704<pre class="doc_code">
705define fastcc void @foo() {
706 ret void
707}
708
709define void @test(i1 %X) {
710 br i1 %X, label %T.i, label %F.i
711T.i:
712 call void @foo()
713 br label %bar.exit
714F.i:
715 call fastcc void @foo()
716 br label %bar.exit
717bar.exit:
718 ret void
719}
720</pre>
721
722<p>The interesting thing about this is that %X <em>must</em> be false for the
723code to be well-defined, but no amount of dead code elimination will be able to
724delete the broken call as unreachable. However, since instcombine/simplifycfg
725turns the undefined call into unreachable, we end up with a branch on a
726condition that goes to unreachable: a branch to unreachable can never happen, so
727"-inline -instcombine -simplifycfg" is able to produce:</p>
728
729<pre class="doc_code">
730define fastcc void @foo() {
731 ret void
732}
733define void @test(i1 %X) {
734F.i:
735 call fastcc void @foo()
736 ret void
737}
738</pre>
739
740</div>
741
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000742</div>
743
John Criswell6ea30b02003-11-18 16:05:23 +0000744<!-- *********************************************************************** -->
John Criswellc310f622003-10-13 16:13:06 +0000745
746<hr>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000747<address>
748 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000749 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000750 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000751 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000752
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000753 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Misha Brukmana6538852003-11-06 21:55:44 +0000754 Last modified: $Date$
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000755</address>
John Criswellf08c5d82003-10-24 22:48:20 +0000756
John Criswellc310f622003-10-13 16:13:06 +0000757</body>
758</html>