blob: 74e85347d1173b20166d74016679d03f020d1b10 [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">
Misha Brukmana6538852003-11-06 21:55:44 +00008 @import url("llvm.css");
9 .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>Why are the LLVM source code and the front-end distributed under
23 different licenses?</li>
24
25 <li>Does the University of Illinois Open Source License really qualify as an
26 "open source" license?</li>
27
28 <li>Can I modify LLVM source code and redistribute the modified source?</li>
29
30 <li>Can I modify LLVM source code and redistribute binaries or other tools
31 based on it, without redistributing the source?</li>
Misha Brukmana6538852003-11-06 21:55:44 +000032 </ol></li>
33
34 <li><a href="#source">Source code</a>
35 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000036 <li>In what language is LLVM written?</li>
37
38 <li>How portable is the LLVM source code?</li>
Misha Brukmana6538852003-11-06 21:55:44 +000039 </ol></li>
40
41 <li><a href="#build">Build Problems</a>
42 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000043 <li>When I run configure, it finds the wrong C compiler.</li>
44
45 <li>The <tt>configure</tt> script finds the right C compiler, but it uses
46 the LLVM linker from a previous build. What do I do?</li>
47
48 <li>When creating a dynamic library, I get a strange GLIBC error.</li>
49
50 <li>I've updated my source tree from Subversion, and now my build is trying
51 to use a file/directory that doesn't exist.</li>
52
53 <li>I've modified a Makefile in my source tree, but my build tree keeps
54 using the old version. What do I do?</li>
55
56 <li>I've upgraded to a new version of LLVM, and I get strange build
57 errors.</li>
58
59 <li>I've built LLVM and am testing it, but the tests freeze.</li>
60
61 <li>Why do test results differ when I perform different types of
62 builds?</li>
63
64 <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
65
66 <li>Compiling LLVM with GCC succeeds, but the resulting tools do not work,
67 what can be wrong?</li>
68
69 <li>When I use the test suite, all of the C Backend tests fail. What is
70 wrong?</li>
71
72 <li>After Subversion update, rebuilding gives the error "No rule to make
73 target".</li>
74
Bill Wendlingf1594022009-04-07 18:51:13 +000075 <li><a href="#srcdir-objdir">When I compile LLVM-GCC with srcdir == objdir,
76 it fails. Why?</a></li>
Misha Brukmana6538852003-11-06 21:55:44 +000077 </ol></li>
John Criswell76c1e382003-11-18 16:08:49 +000078
Reid Spencer501bfee2006-04-26 14:52:19 +000079 <li><a href="#felangs">Source Languages</a>
80 <ol>
81 <li><a href="#langs">What source languages are supported?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +000082
Gordon Henriksene5079052008-02-22 21:55:51 +000083 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
Bill Wendling290235f2009-04-07 18:40:56 +000084 should I interface with the LLVM middle-end optimizers and back-end code
85 generators?</a></li>
86
Reid Spencer501bfee2006-04-26 14:52:19 +000087 <li><a href="#langhlsupp">What support is there for higher level source
Bill Wendling290235f2009-04-07 18:40:56 +000088 language constructs for building a compiler?</a></li>
89
Reid Spencere00906f2006-08-10 20:15:58 +000090 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
91 instruction. Help!</a></li>
Reid Spencer501bfee2006-04-26 14:52:19 +000092 </ol>
93
Chris Lattnercc33d702003-11-19 05:53:12 +000094 <li><a href="#cfe">Using the GCC Front End</a>
John Criswell76c1e382003-11-18 16:08:49 +000095 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000096 <li>When I compile software that uses a configure script, the configure
97 script thinks my system has all of the header files and libraries it is
98 testing for. How do I get configure to work correctly?</li>
John Criswell76c1e382003-11-18 16:08:49 +000099
Bill Wendling290235f2009-04-07 18:40:56 +0000100 <li>When I compile code using the LLVM GCC front end, it complains that it
101 cannot find libcrtend.a?</li>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000102
Bill Wendling290235f2009-04-07 18:40:56 +0000103 <li>How can I disable all optimizations when compiling code using the LLVM
104 GCC front end?</li>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000105
Bill Wendling290235f2009-04-07 18:40:56 +0000106 <li><a href="#translatecxx">Can I use LLVM to convert C++ code to C
107 code?</a></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000108
Bill Wendling290235f2009-04-07 18:40:56 +0000109 <li><a href="#platformindependent">Can I compile C or C++ code to
110 platform-independent LLVM bitcode?</a></li>
John Criswell76c1e382003-11-18 16:08:49 +0000111 </ol>
112 </li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000113
114 <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
115 <ol>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000116 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000117 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000118 #include &lt;iostream&gt;?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +0000119
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000120 <li><a href="#codedce">Where did all of my code go??</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +0000121
122 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in
123 my code?</a></li>
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000124
125 <li><a href="#callconvwrong">Why does instcombine + simplifycfg turn
126 a call to a function with a mismatched calling convention into "unreachable"?
127 Why not make the verifier reject it?</a></li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000128 </ol>
129 </li>
Misha Brukmana6538852003-11-06 21:55:44 +0000130</ol>
131
Chris Lattner7911ce22004-05-23 21:07:27 +0000132<div class="doc_author">
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000133 <p>Written by <a href="http://llvm.org/">The LLVM Team</a></p>
Chris Lattner7911ce22004-05-23 21:07:27 +0000134</div>
135
136
Misha Brukmana6538852003-11-06 21:55:44 +0000137<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000138<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000139 <a name="license">License</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000140</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000141<!-- *********************************************************************** -->
142
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000143<div>
144
Misha Brukmana6538852003-11-06 21:55:44 +0000145<div class="question">
146<p>Why are the LLVM source code and the front-end distributed under different
Bill Wendling290235f2009-04-07 18:40:56 +0000147 licenses?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000148</div>
149
150<div class="answer">
151<p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
Bill Wendling290235f2009-04-07 18:40:56 +0000152 Our aim is to distribute LLVM source code under a <em>much less
153 restrictive</em> license, in particular one that does not compel users who
154 distribute tools based on modifying the source to redistribute the modified
155 source code as well.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000156</div>
157
158<div class="question">
159<p>Does the University of Illinois Open Source License really qualify as an
Bill Wendling290235f2009-04-07 18:40:56 +0000160 "open source" license?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000161</div>
162
163<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000164<p>Yes, the license
165 is <a href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by
166 the Open Source Initiative (OSI).</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000167</div>
168
169<div class="question">
170<p>Can I modify LLVM source code and redistribute the modified source?</p>
171</div>
172
173<div class="answer">
174<p>Yes. The modified source distribution must retain the copyright notice and
Bill Wendling290235f2009-04-07 18:40:56 +0000175 follow the three bulletted conditions listed in
176 the <a href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM
177 license</a>.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000178</div>
179
180<div class="question">
181<p>Can I modify LLVM source code and redistribute binaries or other tools based
Bill Wendling290235f2009-04-07 18:40:56 +0000182 on it, without redistributing the source?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000183</div>
184
185<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000186<p>Yes. This is why we distribute LLVM under a less restrictive license than
187 GPL, as explained in the first question above.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000188</div>
189
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000190</div>
191
Misha Brukmana6538852003-11-06 21:55:44 +0000192<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000193<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000194 <a name="source">Source Code</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000195</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000196<!-- *********************************************************************** -->
197
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000198<div>
199
Misha Brukmana6538852003-11-06 21:55:44 +0000200<div class="question">
201<p>In what language is LLVM written?</p>
202</div>
203
204<div class="answer">
205<p>All of the LLVM tools and libraries are written in C++ with extensive use of
Bill Wendling290235f2009-04-07 18:40:56 +0000206 the STL.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000207</div>
208
209<div class="question">
210<p>How portable is the LLVM source code?</p>
211</div>
212
213<div class="answer">
214<p>The LLVM source code should be portable to most modern UNIX-like operating
215systems. Most of the code is written in standard C++ with operating system
216services abstracted to a support library. The tools required to build and test
217LLVM have been ported to a plethora of platforms.</p>
218
219<p>Some porting problems may exist in the following areas:</p>
220
221<ul>
Misha Brukmana6538852003-11-06 21:55:44 +0000222 <li>The GCC front end code is not as portable as the LLVM suite, so it may not
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000223 compile as well on unsupported platforms.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000224
Misha Brukmana6538852003-11-06 21:55:44 +0000225 <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000226 Shell and sed. Porting to systems without these tools (MacOS 9, Plan 9)
227 will require more effort.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000228</ul>
229
230</div>
231
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000232</div>
233
Misha Brukmana6538852003-11-06 21:55:44 +0000234<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000235<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000236 <a name="build">Build Problems</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000237</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000238<!-- *********************************************************************** -->
239
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000240<div>
241
Misha Brukmana6538852003-11-06 21:55:44 +0000242<div class="question">
243<p>When I run configure, it finds the wrong C compiler.</p>
244</div>
245
246<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000247<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
Bill Wendling290235f2009-04-07 18:40:56 +0000248 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt>
249 and <tt>CXX</tt> for the C and C++ compiler, respectively.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000250
251<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
Bill Wendling290235f2009-04-07 18:40:56 +0000252 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
253 explicitly.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000254
255</div>
256
257<div class="question">
Misha Brukmana6538852003-11-06 21:55:44 +0000258<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
Bill Wendling290235f2009-04-07 18:40:56 +0000259 LLVM linker from a previous build. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000260</div>
261
262<div class="answer">
263<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
Bill Wendling290235f2009-04-07 18:40:56 +0000264 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
265 it:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000266
267<ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000268 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000269 program appears first in the <tt>PATH</tt>. This may work, but may not be
270 convenient when you want them <i>first</i> in your path for other
271 work.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000272
273 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000274 correct. In a Borne compatible shell, the syntax would be:</p>
275
Misha Brukmana54d4b22008-12-17 18:11:40 +0000276<pre class="doc_code">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000277% PATH=[the path without the bad program] ./configure ...
278</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000279
280 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000281 to do its work without having to adjust your <tt>PATH</tt>
282 permanently.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000283</ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000284</div>
285
286<div class="question">
287<p>When creating a dynamic library, I get a strange GLIBC error.</p>
288</div>
289
290<div class="answer">
291<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
Bill Wendling290235f2009-04-07 18:40:56 +0000292 GCC was compiled with the --disable-shared option. To work around this,
293 install your own version of GCC that has shared libraries enabled by
294 default.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000295</div>
296
297<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000298<p>I've updated my source tree from Subversion, and now my build is trying to
299 use a file/directory that doesn't exist.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000300</div>
301
302<div class="answer">
303<p>You need to re-run configure in your object directory. When new Makefiles
Bill Wendling290235f2009-04-07 18:40:56 +0000304 are added to the source tree, they have to be copied over to the object tree
305 in order to be used by the build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000306</div>
307
308<div class="question">
309<p>I've modified a Makefile in my source tree, but my build tree keeps using the
Bill Wendling290235f2009-04-07 18:40:56 +0000310 old version. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000311</div>
312
313<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000314<p>If the Makefile already exists in your object tree, you can just run the
315 following command in the top level directory of your object tree:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000316
Misha Brukmana54d4b22008-12-17 18:11:40 +0000317<pre class="doc_code">
318% ./config.status &lt;relative path to Makefile&gt;
319</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000320
321<p>If the Makefile is new, you will have to modify the configure script to copy
Bill Wendling290235f2009-04-07 18:40:56 +0000322 it over.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000323</div>
324
325<div class="question">
326<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
327</div>
328
329<div class="answer">
330
331<p>Sometimes, changes to the LLVM source code alters how the build system works.
Bill Wendling290235f2009-04-07 18:40:56 +0000332 Changes in libtool, autoconf, or header file dependencies are especially
333 prone to this sort of problem.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000334
335<p>The best thing to try is to remove the old files and re-build. In most
Bill Wendling290235f2009-04-07 18:40:56 +0000336 cases, this takes care of the problem. To do this, just type <tt>make
337 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000338</div>
339
340<div class="question">
341<p>I've built LLVM and am testing it, but the tests freeze.</p>
342</div>
343
344<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000345<p>This is most likely occurring because you built a profile or release
Bill Wendling290235f2009-04-07 18:40:56 +0000346 (optimized) build of LLVM and have not specified the same information on the
347 <tt>gmake</tt> command line.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000348
349<p>For example, if you built LLVM with the command:</p>
350
Misha Brukmana54d4b22008-12-17 18:11:40 +0000351<pre class="doc_code">
352% gmake ENABLE_PROFILING=1
353</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000354
355<p>...then you must run the tests with the following commands:</p>
356
Misha Brukmana54d4b22008-12-17 18:11:40 +0000357<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000358% cd llvm/test
359% gmake ENABLE_PROFILING=1
360</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000361</div>
362
363<div class="question">
364<p>Why do test results differ when I perform different types of builds?</p>
365</div>
366
367<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000368<p>The LLVM test suite is dependent upon several features of the LLVM tools and
Bill Wendling290235f2009-04-07 18:40:56 +0000369 libraries.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000370
371<p>First, the debugging assertions in code are not enabled in optimized or
Bill Wendling290235f2009-04-07 18:40:56 +0000372 profiling builds. Hence, tests that used to fail may pass.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000373
374<p>Second, some tests may rely upon debugging options or behavior that is only
Bill Wendling290235f2009-04-07 18:40:56 +0000375 available in the debug build. These tests will fail in an optimized or
376 profile build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000377</div>
378
Chris Lattner8a0b9242003-12-08 05:43:19 +0000379<div class="question">
Chris Lattner306acee2003-12-22 04:06:12 +0000380<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000381</div>
382
383<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000384<p>This is <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392">a bug in
385 GCC</a>, and affects projects other than LLVM. Try upgrading or downgrading
386 your GCC.</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000387</div>
388
John Criswelld1799612004-03-29 20:23:11 +0000389<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000390<p>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what
391 can be wrong?</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000392</div>
393
394<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000395<p>Several versions of GCC have shown a weakness in miscompiling the LLVM
396 codebase. Please consult your compiler version (<tt>gcc --version</tt>) to
397 find out whether it is <a href="GettingStarted.html#brokengcc">broken</a>.
398 If so, your only option is to upgrade GCC to a known good version.</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000399</div>
400
401<div class="question">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000402<p>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendling290235f2009-04-07 18:40:56 +0000403 target".</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000404</div>
405
406<div class="answer">
407<p>If the error is of the form:</p>
408
Misha Brukmana54d4b22008-12-17 18:11:40 +0000409<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000410gmake[2]: *** No rule to make target `/path/to/somefile', needed by
411`/path/to/another/file.d'.<br>
412Stop.
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000413</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000414
Bill Wendling290235f2009-04-07 18:40:56 +0000415<p>This may occur anytime files are moved within the Subversion repository or
416 removed entirely. In this case, the best solution is to erase all
417 <tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000418
Misha Brukmana54d4b22008-12-17 18:11:40 +0000419<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000420% cd $LLVM_OBJ_DIR
421% rm -f `find . -name \*\.d`
422% gmake
423</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000424
425<p>In other cases, it may be necessary to run <tt>make clean</tt> before
Bill Wendling290235f2009-04-07 18:40:56 +0000426 rebuilding.</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000427</div>
428
Bill Wendlingf1594022009-04-07 18:51:13 +0000429<div class="question">
Bill Wendlinga0bd8102009-04-07 18:54:06 +0000430<p><a name="srcdir-objdir">When I compile LLVM-GCC with srcdir == objdir, it
Bill Wendlingf1594022009-04-07 18:51:13 +0000431 fails. Why?</a></p>
432</div>
433
434<div class="answer">
435<p>The <tt>GNUmakefile</tt> in the top-level directory of LLVM-GCC is a special
436 <tt>Makefile</tt> used by Apple to invoke the <tt>build_gcc</tt> script after
Dan Gohmanabbf7cb2010-02-25 23:41:41 +0000437 setting up a special environment. This has the unfortunate side-effect that
Bill Wendlingf1594022009-04-07 18:51:13 +0000438 trying to build LLVM-GCC with srcdir == objdir in a "non-Apple way" invokes
439 the <tt>GNUmakefile</tt> instead of <tt>Makefile</tt>. Because the
440 environment isn't set up correctly to do this, the build fails.</p>
441
442<p>People not building LLVM-GCC the "Apple way" need to build LLVM-GCC with
443 srcdir != objdir, or simply remove the GNUmakefile entirely.</p>
444
445<p>We regret the inconvenience.</p>
446</div>
447
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000448</div>
449
Misha Brukmana6538852003-11-06 21:55:44 +0000450<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000451<h2>
452 <a name="felangs">Source Languages</a>
453</h2>
Reid Spencer501bfee2006-04-26 14:52:19 +0000454
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000455<div>
456
Bill Wendling290235f2009-04-07 18:40:56 +0000457<div class="question">
458<p><a name="langs">What source languages are supported?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000459</div>
Gordon Henriksen58366822008-02-22 20:58:29 +0000460
Gordon Henriksen58366822008-02-22 20:58:29 +0000461<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000462<p>LLVM currently has full support for C and C++ source languages. These are
463 available through a special version of GCC that LLVM calls the
464 <a href="#cfe">C Front End</a></p>
465
466<p>There is an incomplete version of a Java front end available in the
467 <tt>java</tt> module. There is no documentation on this yet so you'll need to
468 download the code, compile it, and try it.</p>
469
470<p>The PyPy developers are working on integrating LLVM into the PyPy backend so
471 that PyPy language can translate to LLVM.</p>
Gordon Henriksen58366822008-02-22 20:58:29 +0000472</div>
473
Bill Wendling290235f2009-04-07 18:40:56 +0000474<div class="question">
475<p><a name="langirgen">I'd like to write a self-hosting LLVM compiler. How
476 should I interface with the LLVM middle-end optimizers and back-end code
477 generators?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000478</div>
Chris Lattner33bef482006-08-15 00:43:35 +0000479
Chris Lattner33bef482006-08-15 00:43:35 +0000480<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000481<p>Your compiler front-end will communicate with LLVM by creating a module in
482 the LLVM intermediate representation (IR) format. Assuming you want to write
483 your language's compiler in the language itself (rather than C++), there are
484 3 major ways to tackle generating LLVM IR from a front-end:</p>
485
486<ul>
487 <li><strong>Call into the LLVM libraries code using your language's FFI
488 (foreign function interface).</strong>
489
490 <ul>
491 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax, and .bc
492 format</li>
493
494 <li><em>for:</em> enables running LLVM optimization passes without a
495 emit/parse overhead</li>
496
497 <li><em>for:</em> adapts well to a JIT context</li>
498
499 <li><em>against:</em> lots of ugly glue code to write</li>
500 </ul></li>
501
502 <li> <strong>Emit LLVM assembly from your compiler's native language.</strong>
503 <ul>
504 <li><em>for:</em> very straightforward to get started</li>
505
506 <li><em>against:</em> the .ll parser is slower than the bitcode reader
507 when interfacing to the middle end</li>
508
509 <li><em>against:</em> you'll have to re-engineer the LLVM IR object model
510 and asm writer in your language</li>
511
512 <li><em>against:</em> it may be harder to track changes to the IR</li>
513 </ul></li>
514
515 <li><strong>Emit LLVM bitcode from your compiler's native language.</strong>
516
517 <ul>
518 <li><em>for:</em> can use the more-efficient bitcode reader when
519 interfacing to the middle end</li>
520
521 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
522 model and bitcode writer in your language</li>
523
524 <li><em>against:</em> it may be harder to track changes to the IR</li>
525 </ul></li>
526</ul>
527
528<p>If you go with the first option, the C bindings in include/llvm-c should help
529 a lot, since most languages have strong support for interfacing with C. The
530 most common hurdle with calling C from managed code is interfacing with the
531 garbage collector. The C interface was designed to require very little memory
532 management, and so is straightforward in this regard.</p>
533</div>
534
535<div class="question">
536<p><a name="langhlsupp">What support is there for a higher level source language
537 constructs for building a compiler?</a></p>
538</div>
539
540<div class="answer">
541<p>Currently, there isn't much. LLVM supports an intermediate representation
542 which is useful for code representation but will not support the high level
543 (abstract syntax tree) representation needed by most compilers. There are no
Eric Christophereae5a132011-09-20 00:34:27 +0000544 facilities for lexical nor semantic analysis.</p>
Bill Wendling290235f2009-04-07 18:40:56 +0000545</div>
546
547<div class="question">
548<p><a name="getelementptr">I don't understand the GetElementPtr
549 instruction. Help!</a></p>
550</div>
551
552<div class="answer">
553<p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000554 Instruction</a>.</p>
Chris Lattner33bef482006-08-15 00:43:35 +0000555</div>
556
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000557</div>
558
Reid Spencer501bfee2006-04-26 14:52:19 +0000559<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000560<h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000561 <a name="cfe">Using the GCC Front End</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000562</h2>
John Criswell6ea30b02003-11-18 16:05:23 +0000563
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000564<div>
565
John Criswell6ea30b02003-11-18 16:05:23 +0000566<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000567<p>When I compile software that uses a configure script, the configure script
568 thinks my system has all of the header files and libraries it is testing for.
569 How do I get configure to work correctly?</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000570</div>
571
572<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000573<p>The configure script is getting things wrong because the LLVM linker allows
574 symbols to be undefined at link time (so that they can be resolved during JIT
575 or translation to the C back end). That is why configure thinks your system
576 "has everything."</p>
577
578<p>To work around this, perform the following steps:</p>
579
John Criswell6ea30b02003-11-18 16:05:23 +0000580<ol>
Bill Wendling290235f2009-04-07 18:40:56 +0000581 <li>Make sure the CC and CXX environment variables contains the full path to
582 the LLVM GCC front end.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000583
Reid Spencer434262a2007-02-09 15:59:08 +0000584 <li>Make sure that the regular C compiler is first in your PATH. </li>
John Criswell6ea30b02003-11-18 16:05:23 +0000585
Reid Spencer434262a2007-02-09 15:59:08 +0000586 <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000587</ol>
588
Bill Wendling290235f2009-04-07 18:40:56 +0000589<p>This will allow the <tt>llvm-ld</tt> linker to create a native code
590 executable instead of shell script that runs the JIT. Creating native code
591 requires standard linkage, which in turn will allow the configure script to
592 find out if code is not linking on your system because the feature isn't
593 available on your system.</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000594</div>
595
Chris Lattnercc33d702003-11-19 05:53:12 +0000596<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000597<p>When I compile code using the LLVM GCC front end, it complains that it cannot
598 find libcrtend.a.
John Criswell6ea30b02003-11-18 16:05:23 +0000599</p>
600</div>
601
Chris Lattnercc33d702003-11-19 05:53:12 +0000602<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000603<p>The only way this can happen is if you haven't installed the runtime
604 library. To correct this, do:</p>
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000605
Misha Brukmana54d4b22008-12-17 18:11:40 +0000606<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000607% cd llvm/runtime
608% make clean ; make install-bytecode
Reid Spencerf96eb572004-12-15 00:14:01 +0000609</pre>
John Criswell6ea30b02003-11-18 16:05:23 +0000610</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000611
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000612<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000613<p>How can I disable all optimizations when compiling code using the LLVM GCC
614 front end?</p>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000615</div>
616
617<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000618<p>Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
619 optimizations done at the llvm level, leaving you with the truly horrible
620 code that you desire.</p>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000621</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000622
Chris Lattneraf7fd202006-07-19 18:19:59 +0000623
624<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000625<p><a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a></p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000626</div>
627
628<div class="answer">
629<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
Bill Wendling290235f2009-04-07 18:40:56 +0000630 Note that the generated C code will be very low level (all loops are lowered
631 to gotos, etc) and not very pretty (comments are stripped, original source
632 formatting is totally lost, variables are renamed, expressions are
633 regrouped), so this may not be what you're looking for. Also, there are
634 several limitations noted below.<p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000635
636<p>Use commands like this:</p>
637
638<ol>
Chris Lattner860e0b42010-05-04 18:15:33 +0000639 <li><p>Compile your program with llvm-g++:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000640
Misha Brukmana54d4b22008-12-17 18:11:40 +0000641<pre class="doc_code">
Chris Lattner860e0b42010-05-04 18:15:33 +0000642% llvm-g++ -emit-llvm x.cpp -o program.bc -c
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000643</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000644
Bill Wendling290235f2009-04-07 18:40:56 +0000645 <p>or:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000646
Misha Brukmana54d4b22008-12-17 18:11:40 +0000647<pre class="doc_code">
Chris Lattner860e0b42010-05-04 18:15:33 +0000648% llvm-g++ a.cpp -c -emit-llvm
649% llvm-g++ b.cpp -c -emit-llvm
650% llvm-ld a.o b.o -o program
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000651</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000652
Chris Lattner860e0b42010-05-04 18:15:33 +0000653 <p>This will generate program and program.bc. The .bc
654 file is the LLVM version of the program all linked together.</p></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000655
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000656 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
657 backend:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000658
Misha Brukmana54d4b22008-12-17 18:11:40 +0000659<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000660% llc -march=c program.bc -o program.c
Misha Brukmana54d4b22008-12-17 18:11:40 +0000661</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000662
Bill Wendling290235f2009-04-07 18:40:56 +0000663 <li><p>Finally, compile the C file:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000664
Misha Brukmana54d4b22008-12-17 18:11:40 +0000665<pre class="doc_code">
Chris Lattnercd465012010-05-04 18:16:00 +0000666% cc x.c -lstdc++
Misha Brukmana54d4b22008-12-17 18:11:40 +0000667</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000668
669</ol>
670
Bill Wendling290235f2009-04-07 18:40:56 +0000671<p>Using LLVM does not eliminate the need for C++ library support. If you use
672 the llvm-g++ front-end, the generated code will depend on g++'s C++ support
673 libraries in the same way that code generated from g++ would. If you use
674 another C++ front-end, the generated code will depend on whatever library
675 that front-end would normally require.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000676
Bill Wendling290235f2009-04-07 18:40:56 +0000677<p>If you are working on a platform that does not provide any C++ libraries, you
678 may be able to manually compile libstdc++ to LLVM bitcode, statically link it
679 into your program, then use the commands above to convert the whole result
680 into C code. Alternatively, you might compile the libraries and your
681 application into two different chunks of C code and link them.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000682
Bill Wendling290235f2009-04-07 18:40:56 +0000683<p>Note that, by default, the C back end does not support exception handling.
684 If you want/need it for a certain program, you can enable it by passing
685 "-enable-correct-eh-support" to the llc program. The resultant code will use
686 setjmp/longjmp to implement exception support that is relatively slow, and
687 not C++-ABI-conforming on most platforms, but otherwise correct.</p>
Dan Gohmand5b455f2009-01-25 16:04:50 +0000688
Bill Wendling290235f2009-04-07 18:40:56 +0000689<p>Also, there are a number of other limitations of the C backend that cause it
690 to produce code that does not fully conform to the C++ ABI on most
691 platforms. Some of the C++ programs in LLVM's test suite are known to fail
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000692 when compiled with the C back end because of ABI incompatibilities with
Bill Wendling290235f2009-04-07 18:40:56 +0000693 standard C++ libraries.</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000694</div>
695
Dan Gohmancfbcd592009-02-10 17:26:53 +0000696<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000697<p><a name="platformindependent">Can I compile C or C++ code to
698 platform-independent LLVM bitcode?</a></p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000699</div>
700
701<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000702<p>No. C and C++ are inherently platform-dependent languages. The most obvious
703 example of this is the preprocessor. A very common way that C code is made
704 portable is by using the preprocessor to include platform-specific code. In
705 practice, information about other platforms is lost after preprocessing, so
706 the result is inherently dependent on the platform that the preprocessing was
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000707 targeting.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000708
Bill Wendling290235f2009-04-07 18:40:56 +0000709<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt> to
710 vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded to
Misha Brukman7e0fc8a2009-04-10 20:48:27 +0000711 a constant immediately, thus hard-wiring a platform-specific detail.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000712
Bill Wendling290235f2009-04-07 18:40:56 +0000713<p>Also, since many platforms define their ABIs in terms of C, and since LLVM is
714 lower-level than C, front-ends currently must emit platform-specific IR in
715 order to have the result conform to the platform ABI.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000716</div>
717
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000718</div>
719
Chris Lattnercc33d702003-11-19 05:53:12 +0000720<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000721<h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000722 <a name="cfe_code">Questions about code generated by the GCC front-end</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000723</h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000724
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000725<div>
726
Misha Brukman237dc2a2004-12-03 23:58:18 +0000727<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000728<p><a name="iosinit">What is this <tt>llvm.global_ctors</tt> and
729 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I <tt>#include
730 &lt;iostream&gt;</tt>?</a></p>
Misha Brukman237dc2a2004-12-03 23:58:18 +0000731</div>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000732
733<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000734<p>If you <tt>#include</tt> the <tt>&lt;iostream&gt;</tt> header into a C++
735 translation unit, the file will probably use
736 the <tt>std::cin</tt>/<tt>std::cout</tt>/... global objects. However, C++
737 does not guarantee an order of initialization between static objects in
738 different translation units, so if a static ctor/dtor in your .cpp file
739 used <tt>std::cout</tt>, for example, the object would not necessarily be
740 automatically initialized before your use.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000741
Misha Brukman237dc2a2004-12-03 23:58:18 +0000742<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Bill Wendling290235f2009-04-07 18:40:56 +0000743 STL that we use declares a static object that gets created in every
744 translation unit that includes <tt>&lt;iostream&gt;</tt>. This object has a
745 static constructor and destructor that initializes and destroys the global
746 iostream objects before they could possibly be used in the file. The code
747 that you see in the .ll file corresponds to the constructor and destructor
748 registration code.
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000749</p>
750
Misha Brukman237dc2a2004-12-03 23:58:18 +0000751<p>If you would like to make it easier to <b>understand</b> the LLVM code
Bill Wendling290235f2009-04-07 18:40:56 +0000752 generated by the compiler in the demo page, consider using <tt>printf()</tt>
753 instead of <tt>iostream</tt>s to print values.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000754</div>
755
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000756<!--=========================================================================-->
757
Bill Wendling290235f2009-04-07 18:40:56 +0000758<div class="question">
759<p><a name="codedce">Where did all of my code go??</a></p>
760</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000761
762<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000763<p>If you are using the LLVM demo page, you may often wonder what happened to
764 all of the code that you typed in. Remember that the demo script is running
765 the code through the LLVM optimizers, so if your code doesn't actually do
766 anything useful, it might all be deleted.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000767
Bill Wendling290235f2009-04-07 18:40:56 +0000768<p>To prevent this, make sure that the code is actually needed. For example, if
769 you are computing some expression, return the value from the function instead
770 of leaving it in a local variable. If you really want to constrain the
771 optimizer, you can read from and assign to <tt>volatile</tt> global
772 variables.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000773</div>
774
775<!--=========================================================================-->
776
Bill Wendling290235f2009-04-07 18:40:56 +0000777<div class="question">
778<p><a name="undef">What is this "<tt>undef</tt>" thing that shows up in my
Bill Wendlingb1a61bd2009-04-07 18:52:30 +0000779 code?</a></p>
Bill Wendling290235f2009-04-07 18:40:56 +0000780</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000781
782<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000783<p><a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of
784 representing a value that is not defined. You can get these if you do not
785 initialize a variable before you use it. For example, the C function:</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000786
Misha Brukmana54d4b22008-12-17 18:11:40 +0000787<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000788int X() { int i; return i; }
789</pre>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000790
Bill Wendling290235f2009-04-07 18:40:56 +0000791<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has a
792 value specified for it.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000793</div>
794
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000795<!--=========================================================================-->
796
797<div class="question">
798<p><a name="callconvwrong">Why does instcombine + simplifycfg turn
799 a call to a function with a mismatched calling convention into "unreachable"?
800 Why not make the verifier reject it?</a></p>
801</div>
802
803<div class="answer">
804<p>This is a common problem run into by authors of front-ends that are using
805custom calling conventions: you need to make sure to set the right calling
806convention on both the function and on each call to the function. For example,
807this code:</p>
808
809<pre class="doc_code">
810define fastcc void @foo() {
811 ret void
812}
813define void @bar() {
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000814 call void @foo()
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000815 ret void
816}
817</pre>
818
819<p>Is optimized to:</p>
820
821<pre class="doc_code">
822define fastcc void @foo() {
823 ret void
824}
825define void @bar() {
826 unreachable
827}
828</pre>
829
830<p>... with "opt -instcombine -simplifycfg". This often bites people because
831"all their code disappears". Setting the calling convention on the caller and
832callee is required for indirect calls to work, so people often ask why not make
833the verifier reject this sort of thing.</p>
834
835<p>The answer is that this code has undefined behavior, but it is not illegal.
836If we made it illegal, then every transformation that could potentially create
837this would have to ensure that it doesn't, and there is valid code that can
838create this sort of construct (in dead code). The sorts of things that can
839cause this to happen are fairly contrived, but we still need to accept them.
840Here's an example:</p>
841
842<pre class="doc_code">
843define fastcc void @foo() {
844 ret void
845}
846define internal void @bar(void()* %FP, i1 %cond) {
847 br i1 %cond, label %T, label %F
848T:
849 call void %FP()
850 ret void
851F:
852 call fastcc void %FP()
853 ret void
854}
855define void @test() {
856 %X = or i1 false, false
857 call void @bar(void()* @foo, i1 %X)
858 ret void
859}
860</pre>
861
862<p>In this example, "test" always passes @foo/false into bar, which ensures that
863 it is dynamically called with the right calling conv (thus, the code is
864 perfectly well defined). If you run this through the inliner, you get this
865 (the explicit "or" is there so that the inliner doesn't dead code eliminate
866 a bunch of stuff):
867</p>
868
869<pre class="doc_code">
870define fastcc void @foo() {
871 ret void
872}
873define void @test() {
874 %X = or i1 false, false
875 br i1 %X, label %T.i, label %F.i
876T.i:
877 call void @foo()
878 br label %bar.exit
879F.i:
880 call fastcc void @foo()
881 br label %bar.exit
882bar.exit:
883 ret void
884}
885</pre>
886
887<p>Here you can see that the inlining pass made an undefined call to @foo with
888 the wrong calling convention. We really don't want to make the inliner have
889 to know about this sort of thing, so it needs to be valid code. In this case,
890 dead code elimination can trivially remove the undefined code. However, if %X
891 was an input argument to @test, the inliner would produce this:
892</p>
893
894<pre class="doc_code">
895define fastcc void @foo() {
896 ret void
897}
898
899define void @test(i1 %X) {
900 br i1 %X, label %T.i, label %F.i
901T.i:
902 call void @foo()
903 br label %bar.exit
904F.i:
905 call fastcc void @foo()
906 br label %bar.exit
907bar.exit:
908 ret void
909}
910</pre>
911
912<p>The interesting thing about this is that %X <em>must</em> be false for the
913code to be well-defined, but no amount of dead code elimination will be able to
914delete the broken call as unreachable. However, since instcombine/simplifycfg
915turns the undefined call into unreachable, we end up with a branch on a
916condition that goes to unreachable: a branch to unreachable can never happen, so
917"-inline -instcombine -simplifycfg" is able to produce:</p>
918
919<pre class="doc_code">
920define fastcc void @foo() {
921 ret void
922}
923define void @test(i1 %X) {
924F.i:
925 call fastcc void @foo()
926 ret void
927}
928</pre>
929
930</div>
931
NAKAMURA Takumi9c55f592012-03-27 11:25:16 +0000932</div>
933
John Criswell6ea30b02003-11-18 16:05:23 +0000934<!-- *********************************************************************** -->
John Criswellc310f622003-10-13 16:13:06 +0000935
936<hr>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000937<address>
938 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000939 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000940 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000941 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000942
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000943 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Misha Brukmana6538852003-11-06 21:55:44 +0000944 Last modified: $Date$
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000945</address>
John Criswellf08c5d82003-10-24 22:48:20 +0000946
John Criswellc310f622003-10-13 16:13:06 +0000947</body>
948</html>