blob: 9e3ea7f2a8d1f0dd043b86ad96e3054d6aaa03d0 [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
75 <li><a href="#llvmc">The <tt>llvmc</tt> program gives me errors/doesn't
76 work.</a></li>
Bill Wendlingf1594022009-04-07 18:51:13 +000077
78 <li><a href="#srcdir-objdir">When I compile LLVM-GCC with srcdir == objdir,
79 it fails. Why?</a></li>
Misha Brukmana6538852003-11-06 21:55:44 +000080 </ol></li>
John Criswell76c1e382003-11-18 16:08:49 +000081
Reid Spencer501bfee2006-04-26 14:52:19 +000082 <li><a href="#felangs">Source Languages</a>
83 <ol>
84 <li><a href="#langs">What source languages are supported?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +000085
Gordon Henriksene5079052008-02-22 21:55:51 +000086 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
Bill Wendling290235f2009-04-07 18:40:56 +000087 should I interface with the LLVM middle-end optimizers and back-end code
88 generators?</a></li>
89
Reid Spencer501bfee2006-04-26 14:52:19 +000090 <li><a href="#langhlsupp">What support is there for higher level source
Bill Wendling290235f2009-04-07 18:40:56 +000091 language constructs for building a compiler?</a></li>
92
Reid Spencere00906f2006-08-10 20:15:58 +000093 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
94 instruction. Help!</a></li>
Reid Spencer501bfee2006-04-26 14:52:19 +000095 </ol>
96
Chris Lattnercc33d702003-11-19 05:53:12 +000097 <li><a href="#cfe">Using the GCC Front End</a>
John Criswell76c1e382003-11-18 16:08:49 +000098 <ol>
Bill Wendling290235f2009-04-07 18:40:56 +000099 <li>When I compile software that uses a configure script, the configure
100 script thinks my system has all of the header files and libraries it is
101 testing for. How do I get configure to work correctly?</li>
John Criswell76c1e382003-11-18 16:08:49 +0000102
Bill Wendling290235f2009-04-07 18:40:56 +0000103 <li>When I compile code using the LLVM GCC front end, it complains that it
104 cannot find libcrtend.a?</li>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000105
Bill Wendling290235f2009-04-07 18:40:56 +0000106 <li>How can I disable all optimizations when compiling code using the LLVM
107 GCC front end?</li>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000108
Bill Wendling290235f2009-04-07 18:40:56 +0000109 <li><a href="#translatecxx">Can I use LLVM to convert C++ code to C
110 code?</a></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000111
Bill Wendling290235f2009-04-07 18:40:56 +0000112 <li><a href="#platformindependent">Can I compile C or C++ code to
113 platform-independent LLVM bitcode?</a></li>
John Criswell76c1e382003-11-18 16:08:49 +0000114 </ol>
115 </li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000116
117 <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
118 <ol>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000119 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000120 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000121 #include &lt;iostream&gt;?</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +0000122
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000123 <li><a href="#codedce">Where did all of my code go??</a></li>
Bill Wendling290235f2009-04-07 18:40:56 +0000124
125 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in
126 my code?</a></li>
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000127
128 <li><a href="#callconvwrong">Why does instcombine + simplifycfg turn
129 a call to a function with a mismatched calling convention into "unreachable"?
130 Why not make the verifier reject it?</a></li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000131 </ol>
132 </li>
Misha Brukmana6538852003-11-06 21:55:44 +0000133</ol>
134
Chris Lattner7911ce22004-05-23 21:07:27 +0000135<div class="doc_author">
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000136 <p>Written by <a href="http://llvm.org/">The LLVM Team</a></p>
Chris Lattner7911ce22004-05-23 21:07:27 +0000137</div>
138
139
Misha Brukmana6538852003-11-06 21:55:44 +0000140<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000141<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000142 <a name="license">License</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000143</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000144<!-- *********************************************************************** -->
145
146<div class="question">
147<p>Why are the LLVM source code and the front-end distributed under different
Bill Wendling290235f2009-04-07 18:40:56 +0000148 licenses?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000149</div>
150
151<div class="answer">
152<p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
Bill Wendling290235f2009-04-07 18:40:56 +0000153 Our aim is to distribute LLVM source code under a <em>much less
154 restrictive</em> license, in particular one that does not compel users who
155 distribute tools based on modifying the source to redistribute the modified
156 source code as well.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000157</div>
158
159<div class="question">
160<p>Does the University of Illinois Open Source License really qualify as an
Bill Wendling290235f2009-04-07 18:40:56 +0000161 "open source" license?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000162</div>
163
164<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000165<p>Yes, the license
166 is <a href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by
167 the Open Source Initiative (OSI).</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000168</div>
169
170<div class="question">
171<p>Can I modify LLVM source code and redistribute the modified source?</p>
172</div>
173
174<div class="answer">
175<p>Yes. The modified source distribution must retain the copyright notice and
Bill Wendling290235f2009-04-07 18:40:56 +0000176 follow the three bulletted conditions listed in
177 the <a href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM
178 license</a>.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000179</div>
180
181<div class="question">
182<p>Can I modify LLVM source code and redistribute binaries or other tools based
Bill Wendling290235f2009-04-07 18:40:56 +0000183 on it, without redistributing the source?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000184</div>
185
186<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000187<p>Yes. This is why we distribute LLVM under a less restrictive license than
188 GPL, as explained in the first question above.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000189</div>
190
191<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000192<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000193 <a name="source">Source Code</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000194</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000195<!-- *********************************************************************** -->
196
197<div class="question">
198<p>In what language is LLVM written?</p>
199</div>
200
201<div class="answer">
202<p>All of the LLVM tools and libraries are written in C++ with extensive use of
Bill Wendling290235f2009-04-07 18:40:56 +0000203 the STL.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000204</div>
205
206<div class="question">
207<p>How portable is the LLVM source code?</p>
208</div>
209
210<div class="answer">
211<p>The LLVM source code should be portable to most modern UNIX-like operating
212systems. Most of the code is written in standard C++ with operating system
213services abstracted to a support library. The tools required to build and test
214LLVM have been ported to a plethora of platforms.</p>
215
216<p>Some porting problems may exist in the following areas:</p>
217
218<ul>
Misha Brukmana6538852003-11-06 21:55:44 +0000219 <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 +0000220 compile as well on unsupported platforms.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000221
Misha Brukmana6538852003-11-06 21:55:44 +0000222 <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000223 Shell and sed. Porting to systems without these tools (MacOS 9, Plan 9)
224 will require more effort.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000225</ul>
226
227</div>
228
229<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000230<h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000231 <a name="build">Build Problems</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000232</h2>
Misha Brukmana6538852003-11-06 21:55:44 +0000233<!-- *********************************************************************** -->
234
235<div class="question">
236<p>When I run configure, it finds the wrong C compiler.</p>
237</div>
238
239<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000240<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
Bill Wendling290235f2009-04-07 18:40:56 +0000241 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt>
242 and <tt>CXX</tt> for the C and C++ compiler, respectively.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000243
244<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
Bill Wendling290235f2009-04-07 18:40:56 +0000245 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
246 explicitly.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000247
248</div>
249
250<div class="question">
Misha Brukmana6538852003-11-06 21:55:44 +0000251<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
Bill Wendling290235f2009-04-07 18:40:56 +0000252 LLVM linker from a previous build. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000253</div>
254
255<div class="answer">
256<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
Bill Wendling290235f2009-04-07 18:40:56 +0000257 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
258 it:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000259
260<ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000261 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000262 program appears first in the <tt>PATH</tt>. This may work, but may not be
263 convenient when you want them <i>first</i> in your path for other
264 work.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000265
266 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000267 correct. In a Borne compatible shell, the syntax would be:</p>
268
Misha Brukmana54d4b22008-12-17 18:11:40 +0000269<pre class="doc_code">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000270% PATH=[the path without the bad program] ./configure ...
271</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000272
273 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000274 to do its work without having to adjust your <tt>PATH</tt>
275 permanently.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000276</ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000277</div>
278
279<div class="question">
280<p>When creating a dynamic library, I get a strange GLIBC error.</p>
281</div>
282
283<div class="answer">
284<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
Bill Wendling290235f2009-04-07 18:40:56 +0000285 GCC was compiled with the --disable-shared option. To work around this,
286 install your own version of GCC that has shared libraries enabled by
287 default.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000288</div>
289
290<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000291<p>I've updated my source tree from Subversion, and now my build is trying to
292 use a file/directory that doesn't exist.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000293</div>
294
295<div class="answer">
296<p>You need to re-run configure in your object directory. When new Makefiles
Bill Wendling290235f2009-04-07 18:40:56 +0000297 are added to the source tree, they have to be copied over to the object tree
298 in order to be used by the build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000299</div>
300
301<div class="question">
302<p>I've modified a Makefile in my source tree, but my build tree keeps using the
Bill Wendling290235f2009-04-07 18:40:56 +0000303 old version. What do I do?</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000304</div>
305
306<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000307<p>If the Makefile already exists in your object tree, you can just run the
308 following command in the top level directory of your object tree:</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000309
Misha Brukmana54d4b22008-12-17 18:11:40 +0000310<pre class="doc_code">
311% ./config.status &lt;relative path to Makefile&gt;
312</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000313
314<p>If the Makefile is new, you will have to modify the configure script to copy
Bill Wendling290235f2009-04-07 18:40:56 +0000315 it over.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000316</div>
317
318<div class="question">
319<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
320</div>
321
322<div class="answer">
323
324<p>Sometimes, changes to the LLVM source code alters how the build system works.
Bill Wendling290235f2009-04-07 18:40:56 +0000325 Changes in libtool, autoconf, or header file dependencies are especially
326 prone to this sort of problem.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000327
328<p>The best thing to try is to remove the old files and re-build. In most
Bill Wendling290235f2009-04-07 18:40:56 +0000329 cases, this takes care of the problem. To do this, just type <tt>make
330 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000331</div>
332
333<div class="question">
334<p>I've built LLVM and am testing it, but the tests freeze.</p>
335</div>
336
337<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000338<p>This is most likely occurring because you built a profile or release
Bill Wendling290235f2009-04-07 18:40:56 +0000339 (optimized) build of LLVM and have not specified the same information on the
340 <tt>gmake</tt> command line.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000341
342<p>For example, if you built LLVM with the command:</p>
343
Misha Brukmana54d4b22008-12-17 18:11:40 +0000344<pre class="doc_code">
345% gmake ENABLE_PROFILING=1
346</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000347
348<p>...then you must run the tests with the following commands:</p>
349
Misha Brukmana54d4b22008-12-17 18:11:40 +0000350<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000351% cd llvm/test
352% gmake ENABLE_PROFILING=1
353</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000354</div>
355
356<div class="question">
357<p>Why do test results differ when I perform different types of builds?</p>
358</div>
359
360<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000361<p>The LLVM test suite is dependent upon several features of the LLVM tools and
Bill Wendling290235f2009-04-07 18:40:56 +0000362 libraries.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000363
364<p>First, the debugging assertions in code are not enabled in optimized or
Bill Wendling290235f2009-04-07 18:40:56 +0000365 profiling builds. Hence, tests that used to fail may pass.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000366
367<p>Second, some tests may rely upon debugging options or behavior that is only
Bill Wendling290235f2009-04-07 18:40:56 +0000368 available in the debug build. These tests will fail in an optimized or
369 profile build.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000370</div>
371
Chris Lattner8a0b9242003-12-08 05:43:19 +0000372<div class="question">
Chris Lattner306acee2003-12-22 04:06:12 +0000373<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000374</div>
375
376<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000377<p>This is <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392">a bug in
378 GCC</a>, and affects projects other than LLVM. Try upgrading or downgrading
379 your GCC.</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000380</div>
381
John Criswelld1799612004-03-29 20:23:11 +0000382<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000383<p>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what
384 can be wrong?</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000385</div>
386
387<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000388<p>Several versions of GCC have shown a weakness in miscompiling the LLVM
389 codebase. Please consult your compiler version (<tt>gcc --version</tt>) to
390 find out whether it is <a href="GettingStarted.html#brokengcc">broken</a>.
391 If so, your only option is to upgrade GCC to a known good version.</p>
Gabor Greif54820ce2009-03-02 19:08:05 +0000392</div>
393
394<div class="question">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000395<p>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendling290235f2009-04-07 18:40:56 +0000396 target".</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000397</div>
398
399<div class="answer">
400<p>If the error is of the form:</p>
401
Misha Brukmana54d4b22008-12-17 18:11:40 +0000402<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000403gmake[2]: *** No rule to make target `/path/to/somefile', needed by
404`/path/to/another/file.d'.<br>
405Stop.
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000406</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000407
Bill Wendling290235f2009-04-07 18:40:56 +0000408<p>This may occur anytime files are moved within the Subversion repository or
409 removed entirely. In this case, the best solution is to erase all
410 <tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000411
Misha Brukmana54d4b22008-12-17 18:11:40 +0000412<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000413% cd $LLVM_OBJ_DIR
414% rm -f `find . -name \*\.d`
415% gmake
416</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000417
418<p>In other cases, it may be necessary to run <tt>make clean</tt> before
Bill Wendling290235f2009-04-07 18:40:56 +0000419 rebuilding.</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000420</div>
421
Bill Wendlingf1594022009-04-07 18:51:13 +0000422<div class="question">
423<p><a name="llvmc">The <tt>llvmc</tt> program gives me errors/doesn't
424 work.</a></p>
Bill Wendling174d5782007-05-29 09:35:34 +0000425</div>
426
427<div class="answer">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000428<p><tt>llvmc</tt> is experimental and isn't really supported. We suggest
Bill Wendling290235f2009-04-07 18:40:56 +0000429 using <tt>llvm-gcc</tt> instead.</p>
Bill Wendling174d5782007-05-29 09:35:34 +0000430</div>
431
Bill Wendlingf1594022009-04-07 18:51:13 +0000432<div class="question">
Bill Wendlinga0bd8102009-04-07 18:54:06 +0000433<p><a name="srcdir-objdir">When I compile LLVM-GCC with srcdir == objdir, it
Bill Wendlingf1594022009-04-07 18:51:13 +0000434 fails. Why?</a></p>
435</div>
436
437<div class="answer">
438<p>The <tt>GNUmakefile</tt> in the top-level directory of LLVM-GCC is a special
439 <tt>Makefile</tt> used by Apple to invoke the <tt>build_gcc</tt> script after
Dan Gohmanabbf7cb2010-02-25 23:41:41 +0000440 setting up a special environment. This has the unfortunate side-effect that
Bill Wendlingf1594022009-04-07 18:51:13 +0000441 trying to build LLVM-GCC with srcdir == objdir in a "non-Apple way" invokes
442 the <tt>GNUmakefile</tt> instead of <tt>Makefile</tt>. Because the
443 environment isn't set up correctly to do this, the build fails.</p>
444
445<p>People not building LLVM-GCC the "Apple way" need to build LLVM-GCC with
446 srcdir != objdir, or simply remove the GNUmakefile entirely.</p>
447
448<p>We regret the inconvenience.</p>
449</div>
450
Misha Brukmana6538852003-11-06 21:55:44 +0000451<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000452<h2>
453 <a name="felangs">Source Languages</a>
454</h2>
Reid Spencer501bfee2006-04-26 14:52:19 +0000455
Bill Wendling290235f2009-04-07 18:40:56 +0000456<div class="question">
457<p><a name="langs">What source languages are supported?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000458</div>
Gordon Henriksen58366822008-02-22 20:58:29 +0000459
Gordon Henriksen58366822008-02-22 20:58:29 +0000460<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000461<p>LLVM currently has full support for C and C++ source languages. These are
462 available through a special version of GCC that LLVM calls the
463 <a href="#cfe">C Front End</a></p>
464
465<p>There is an incomplete version of a Java front end available in the
466 <tt>java</tt> module. There is no documentation on this yet so you'll need to
467 download the code, compile it, and try it.</p>
468
469<p>The PyPy developers are working on integrating LLVM into the PyPy backend so
470 that PyPy language can translate to LLVM.</p>
Gordon Henriksen58366822008-02-22 20:58:29 +0000471</div>
472
Bill Wendling290235f2009-04-07 18:40:56 +0000473<div class="question">
474<p><a name="langirgen">I'd like to write a self-hosting LLVM compiler. How
475 should I interface with the LLVM middle-end optimizers and back-end code
476 generators?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000477</div>
Chris Lattner33bef482006-08-15 00:43:35 +0000478
Chris Lattner33bef482006-08-15 00:43:35 +0000479<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000480<p>Your compiler front-end will communicate with LLVM by creating a module in
481 the LLVM intermediate representation (IR) format. Assuming you want to write
482 your language's compiler in the language itself (rather than C++), there are
483 3 major ways to tackle generating LLVM IR from a front-end:</p>
484
485<ul>
486 <li><strong>Call into the LLVM libraries code using your language's FFI
487 (foreign function interface).</strong>
488
489 <ul>
490 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax, and .bc
491 format</li>
492
493 <li><em>for:</em> enables running LLVM optimization passes without a
494 emit/parse overhead</li>
495
496 <li><em>for:</em> adapts well to a JIT context</li>
497
498 <li><em>against:</em> lots of ugly glue code to write</li>
499 </ul></li>
500
501 <li> <strong>Emit LLVM assembly from your compiler's native language.</strong>
502 <ul>
503 <li><em>for:</em> very straightforward to get started</li>
504
505 <li><em>against:</em> the .ll parser is slower than the bitcode reader
506 when interfacing to the middle end</li>
507
508 <li><em>against:</em> you'll have to re-engineer the LLVM IR object model
509 and asm writer in your language</li>
510
511 <li><em>against:</em> it may be harder to track changes to the IR</li>
512 </ul></li>
513
514 <li><strong>Emit LLVM bitcode from your compiler's native language.</strong>
515
516 <ul>
517 <li><em>for:</em> can use the more-efficient bitcode reader when
518 interfacing to the middle end</li>
519
520 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
521 model and bitcode writer in your language</li>
522
523 <li><em>against:</em> it may be harder to track changes to the IR</li>
524 </ul></li>
525</ul>
526
527<p>If you go with the first option, the C bindings in include/llvm-c should help
528 a lot, since most languages have strong support for interfacing with C. The
529 most common hurdle with calling C from managed code is interfacing with the
530 garbage collector. The C interface was designed to require very little memory
531 management, and so is straightforward in this regard.</p>
532</div>
533
534<div class="question">
535<p><a name="langhlsupp">What support is there for a higher level source language
536 constructs for building a compiler?</a></p>
537</div>
538
539<div class="answer">
540<p>Currently, there isn't much. LLVM supports an intermediate representation
541 which is useful for code representation but will not support the high level
542 (abstract syntax tree) representation needed by most compilers. There are no
Eric Christophereae5a132011-09-20 00:34:27 +0000543 facilities for lexical nor semantic analysis.</p>
Bill Wendling290235f2009-04-07 18:40:56 +0000544</div>
545
546<div class="question">
547<p><a name="getelementptr">I don't understand the GetElementPtr
548 instruction. Help!</a></p>
549</div>
550
551<div class="answer">
552<p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000553 Instruction</a>.</p>
Chris Lattner33bef482006-08-15 00:43:35 +0000554</div>
555
Reid Spencer501bfee2006-04-26 14:52:19 +0000556<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000557<h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000558 <a name="cfe">Using the GCC Front End</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000559</h2>
John Criswell6ea30b02003-11-18 16:05:23 +0000560
561<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000562<p>When I compile software that uses a configure script, the configure script
563 thinks my system has all of the header files and libraries it is testing for.
564 How do I get configure to work correctly?</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000565</div>
566
567<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000568<p>The configure script is getting things wrong because the LLVM linker allows
569 symbols to be undefined at link time (so that they can be resolved during JIT
570 or translation to the C back end). That is why configure thinks your system
571 "has everything."</p>
572
573<p>To work around this, perform the following steps:</p>
574
John Criswell6ea30b02003-11-18 16:05:23 +0000575<ol>
Bill Wendling290235f2009-04-07 18:40:56 +0000576 <li>Make sure the CC and CXX environment variables contains the full path to
577 the LLVM GCC front end.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000578
Reid Spencer434262a2007-02-09 15:59:08 +0000579 <li>Make sure that the regular C compiler is first in your PATH. </li>
John Criswell6ea30b02003-11-18 16:05:23 +0000580
Reid Spencer434262a2007-02-09 15:59:08 +0000581 <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000582</ol>
583
Bill Wendling290235f2009-04-07 18:40:56 +0000584<p>This will allow the <tt>llvm-ld</tt> linker to create a native code
585 executable instead of shell script that runs the JIT. Creating native code
586 requires standard linkage, which in turn will allow the configure script to
587 find out if code is not linking on your system because the feature isn't
588 available on your system.</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000589</div>
590
Chris Lattnercc33d702003-11-19 05:53:12 +0000591<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000592<p>When I compile code using the LLVM GCC front end, it complains that it cannot
593 find libcrtend.a.
John Criswell6ea30b02003-11-18 16:05:23 +0000594</p>
595</div>
596
Chris Lattnercc33d702003-11-19 05:53:12 +0000597<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000598<p>The only way this can happen is if you haven't installed the runtime
599 library. To correct this, do:</p>
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000600
Misha Brukmana54d4b22008-12-17 18:11:40 +0000601<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000602% cd llvm/runtime
603% make clean ; make install-bytecode
Reid Spencerf96eb572004-12-15 00:14:01 +0000604</pre>
John Criswell6ea30b02003-11-18 16:05:23 +0000605</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000606
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000607<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000608<p>How can I disable all optimizations when compiling code using the LLVM GCC
609 front end?</p>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000610</div>
611
612<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000613<p>Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
614 optimizations done at the llvm level, leaving you with the truly horrible
615 code that you desire.</p>
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000616</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000617
Chris Lattneraf7fd202006-07-19 18:19:59 +0000618
619<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000620<p><a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a></p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000621</div>
622
623<div class="answer">
624<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
Bill Wendling290235f2009-04-07 18:40:56 +0000625 Note that the generated C code will be very low level (all loops are lowered
626 to gotos, etc) and not very pretty (comments are stripped, original source
627 formatting is totally lost, variables are renamed, expressions are
628 regrouped), so this may not be what you're looking for. Also, there are
629 several limitations noted below.<p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000630
631<p>Use commands like this:</p>
632
633<ol>
Chris Lattner860e0b42010-05-04 18:15:33 +0000634 <li><p>Compile your program with llvm-g++:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000635
Misha Brukmana54d4b22008-12-17 18:11:40 +0000636<pre class="doc_code">
Chris Lattner860e0b42010-05-04 18:15:33 +0000637% llvm-g++ -emit-llvm x.cpp -o program.bc -c
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000638</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000639
Bill Wendling290235f2009-04-07 18:40:56 +0000640 <p>or:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000641
Misha Brukmana54d4b22008-12-17 18:11:40 +0000642<pre class="doc_code">
Chris Lattner860e0b42010-05-04 18:15:33 +0000643% llvm-g++ a.cpp -c -emit-llvm
644% llvm-g++ b.cpp -c -emit-llvm
645% llvm-ld a.o b.o -o program
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000646</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000647
Chris Lattner860e0b42010-05-04 18:15:33 +0000648 <p>This will generate program and program.bc. The .bc
649 file is the LLVM version of the program all linked together.</p></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000650
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000651 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
652 backend:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000653
Misha Brukmana54d4b22008-12-17 18:11:40 +0000654<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000655% llc -march=c program.bc -o program.c
Misha Brukmana54d4b22008-12-17 18:11:40 +0000656</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000657
Bill Wendling290235f2009-04-07 18:40:56 +0000658 <li><p>Finally, compile the C file:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000659
Misha Brukmana54d4b22008-12-17 18:11:40 +0000660<pre class="doc_code">
Chris Lattnercd465012010-05-04 18:16:00 +0000661% cc x.c -lstdc++
Misha Brukmana54d4b22008-12-17 18:11:40 +0000662</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000663
664</ol>
665
Bill Wendling290235f2009-04-07 18:40:56 +0000666<p>Using LLVM does not eliminate the need for C++ library support. If you use
667 the llvm-g++ front-end, the generated code will depend on g++'s C++ support
668 libraries in the same way that code generated from g++ would. If you use
669 another C++ front-end, the generated code will depend on whatever library
670 that front-end would normally require.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000671
Bill Wendling290235f2009-04-07 18:40:56 +0000672<p>If you are working on a platform that does not provide any C++ libraries, you
673 may be able to manually compile libstdc++ to LLVM bitcode, statically link it
674 into your program, then use the commands above to convert the whole result
675 into C code. Alternatively, you might compile the libraries and your
676 application into two different chunks of C code and link them.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000677
Bill Wendling290235f2009-04-07 18:40:56 +0000678<p>Note that, by default, the C back end does not support exception handling.
679 If you want/need it for a certain program, you can enable it by passing
680 "-enable-correct-eh-support" to the llc program. The resultant code will use
681 setjmp/longjmp to implement exception support that is relatively slow, and
682 not C++-ABI-conforming on most platforms, but otherwise correct.</p>
Dan Gohmand5b455f2009-01-25 16:04:50 +0000683
Bill Wendling290235f2009-04-07 18:40:56 +0000684<p>Also, there are a number of other limitations of the C backend that cause it
685 to produce code that does not fully conform to the C++ ABI on most
686 platforms. Some of the C++ programs in LLVM's test suite are known to fail
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000687 when compiled with the C back end because of ABI incompatibilities with
Bill Wendling290235f2009-04-07 18:40:56 +0000688 standard C++ libraries.</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000689</div>
690
Dan Gohmancfbcd592009-02-10 17:26:53 +0000691<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000692<p><a name="platformindependent">Can I compile C or C++ code to
693 platform-independent LLVM bitcode?</a></p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000694</div>
695
696<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000697<p>No. C and C++ are inherently platform-dependent languages. The most obvious
698 example of this is the preprocessor. A very common way that C code is made
699 portable is by using the preprocessor to include platform-specific code. In
700 practice, information about other platforms is lost after preprocessing, so
701 the result is inherently dependent on the platform that the preprocessing was
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000702 targeting.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000703
Bill Wendling290235f2009-04-07 18:40:56 +0000704<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt> to
705 vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded to
Misha Brukman7e0fc8a2009-04-10 20:48:27 +0000706 a constant immediately, thus hard-wiring a platform-specific detail.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000707
Bill Wendling290235f2009-04-07 18:40:56 +0000708<p>Also, since many platforms define their ABIs in terms of C, and since LLVM is
709 lower-level than C, front-ends currently must emit platform-specific IR in
710 order to have the result conform to the platform ABI.</p>
Dan Gohmancfbcd592009-02-10 17:26:53 +0000711</div>
712
Chris Lattnercc33d702003-11-19 05:53:12 +0000713<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000714<h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000715 <a name="cfe_code">Questions about code generated by the GCC front-end</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000716</h2>
Chris Lattnercc33d702003-11-19 05:53:12 +0000717
Misha Brukman237dc2a2004-12-03 23:58:18 +0000718<div class="question">
Bill Wendling290235f2009-04-07 18:40:56 +0000719<p><a name="iosinit">What is this <tt>llvm.global_ctors</tt> and
720 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I <tt>#include
721 &lt;iostream&gt;</tt>?</a></p>
Misha Brukman237dc2a2004-12-03 23:58:18 +0000722</div>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000723
724<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000725<p>If you <tt>#include</tt> the <tt>&lt;iostream&gt;</tt> header into a C++
726 translation unit, the file will probably use
727 the <tt>std::cin</tt>/<tt>std::cout</tt>/... global objects. However, C++
728 does not guarantee an order of initialization between static objects in
729 different translation units, so if a static ctor/dtor in your .cpp file
730 used <tt>std::cout</tt>, for example, the object would not necessarily be
731 automatically initialized before your use.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000732
Misha Brukman237dc2a2004-12-03 23:58:18 +0000733<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Bill Wendling290235f2009-04-07 18:40:56 +0000734 STL that we use declares a static object that gets created in every
735 translation unit that includes <tt>&lt;iostream&gt;</tt>. This object has a
736 static constructor and destructor that initializes and destroys the global
737 iostream objects before they could possibly be used in the file. The code
738 that you see in the .ll file corresponds to the constructor and destructor
739 registration code.
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000740</p>
741
Misha Brukman237dc2a2004-12-03 23:58:18 +0000742<p>If you would like to make it easier to <b>understand</b> the LLVM code
Bill Wendling290235f2009-04-07 18:40:56 +0000743 generated by the compiler in the demo page, consider using <tt>printf()</tt>
744 instead of <tt>iostream</tt>s to print values.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000745</div>
746
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000747<!--=========================================================================-->
748
Bill Wendling290235f2009-04-07 18:40:56 +0000749<div class="question">
750<p><a name="codedce">Where did all of my code go??</a></p>
751</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000752
753<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000754<p>If you are using the LLVM demo page, you may often wonder what happened to
755 all of the code that you typed in. Remember that the demo script is running
756 the code through the LLVM optimizers, so if your code doesn't actually do
757 anything useful, it might all be deleted.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000758
Bill Wendling290235f2009-04-07 18:40:56 +0000759<p>To prevent this, make sure that the code is actually needed. For example, if
760 you are computing some expression, return the value from the function instead
761 of leaving it in a local variable. If you really want to constrain the
762 optimizer, you can read from and assign to <tt>volatile</tt> global
763 variables.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000764</div>
765
766<!--=========================================================================-->
767
Bill Wendling290235f2009-04-07 18:40:56 +0000768<div class="question">
769<p><a name="undef">What is this "<tt>undef</tt>" thing that shows up in my
Bill Wendlingb1a61bd2009-04-07 18:52:30 +0000770 code?</a></p>
Bill Wendling290235f2009-04-07 18:40:56 +0000771</div>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000772
773<div class="answer">
Bill Wendling290235f2009-04-07 18:40:56 +0000774<p><a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of
775 representing a value that is not defined. You can get these if you do not
776 initialize a variable before you use it. For example, the C function:</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000777
Misha Brukmana54d4b22008-12-17 18:11:40 +0000778<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000779int X() { int i; return i; }
780</pre>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000781
Bill Wendling290235f2009-04-07 18:40:56 +0000782<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has a
783 value specified for it.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000784</div>
785
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000786<!--=========================================================================-->
787
788<div class="question">
789<p><a name="callconvwrong">Why does instcombine + simplifycfg turn
790 a call to a function with a mismatched calling convention into "unreachable"?
791 Why not make the verifier reject it?</a></p>
792</div>
793
794<div class="answer">
795<p>This is a common problem run into by authors of front-ends that are using
796custom calling conventions: you need to make sure to set the right calling
797convention on both the function and on each call to the function. For example,
798this code:</p>
799
800<pre class="doc_code">
801define fastcc void @foo() {
802 ret void
803}
804define void @bar() {
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000805 call void @foo()
Chris Lattner2c6f9f72009-06-30 17:10:19 +0000806 ret void
807}
808</pre>
809
810<p>Is optimized to:</p>
811
812<pre class="doc_code">
813define fastcc void @foo() {
814 ret void
815}
816define void @bar() {
817 unreachable
818}
819</pre>
820
821<p>... with "opt -instcombine -simplifycfg". This often bites people because
822"all their code disappears". Setting the calling convention on the caller and
823callee is required for indirect calls to work, so people often ask why not make
824the verifier reject this sort of thing.</p>
825
826<p>The answer is that this code has undefined behavior, but it is not illegal.
827If we made it illegal, then every transformation that could potentially create
828this would have to ensure that it doesn't, and there is valid code that can
829create this sort of construct (in dead code). The sorts of things that can
830cause this to happen are fairly contrived, but we still need to accept them.
831Here's an example:</p>
832
833<pre class="doc_code">
834define fastcc void @foo() {
835 ret void
836}
837define internal void @bar(void()* %FP, i1 %cond) {
838 br i1 %cond, label %T, label %F
839T:
840 call void %FP()
841 ret void
842F:
843 call fastcc void %FP()
844 ret void
845}
846define void @test() {
847 %X = or i1 false, false
848 call void @bar(void()* @foo, i1 %X)
849 ret void
850}
851</pre>
852
853<p>In this example, "test" always passes @foo/false into bar, which ensures that
854 it is dynamically called with the right calling conv (thus, the code is
855 perfectly well defined). If you run this through the inliner, you get this
856 (the explicit "or" is there so that the inliner doesn't dead code eliminate
857 a bunch of stuff):
858</p>
859
860<pre class="doc_code">
861define fastcc void @foo() {
862 ret void
863}
864define void @test() {
865 %X = or i1 false, false
866 br i1 %X, label %T.i, label %F.i
867T.i:
868 call void @foo()
869 br label %bar.exit
870F.i:
871 call fastcc void @foo()
872 br label %bar.exit
873bar.exit:
874 ret void
875}
876</pre>
877
878<p>Here you can see that the inlining pass made an undefined call to @foo with
879 the wrong calling convention. We really don't want to make the inliner have
880 to know about this sort of thing, so it needs to be valid code. In this case,
881 dead code elimination can trivially remove the undefined code. However, if %X
882 was an input argument to @test, the inliner would produce this:
883</p>
884
885<pre class="doc_code">
886define fastcc void @foo() {
887 ret void
888}
889
890define void @test(i1 %X) {
891 br i1 %X, label %T.i, label %F.i
892T.i:
893 call void @foo()
894 br label %bar.exit
895F.i:
896 call fastcc void @foo()
897 br label %bar.exit
898bar.exit:
899 ret void
900}
901</pre>
902
903<p>The interesting thing about this is that %X <em>must</em> be false for the
904code to be well-defined, but no amount of dead code elimination will be able to
905delete the broken call as unreachable. However, since instcombine/simplifycfg
906turns the undefined call into unreachable, we end up with a branch on a
907condition that goes to unreachable: a branch to unreachable can never happen, so
908"-inline -instcombine -simplifycfg" is able to produce:</p>
909
910<pre class="doc_code">
911define fastcc void @foo() {
912 ret void
913}
914define void @test(i1 %X) {
915F.i:
916 call fastcc void @foo()
917 ret void
918}
919</pre>
920
921</div>
922
John Criswell6ea30b02003-11-18 16:05:23 +0000923<!-- *********************************************************************** -->
John Criswellc310f622003-10-13 16:13:06 +0000924
925<hr>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000926<address>
927 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000928 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000929 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000930 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000931
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000932 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Misha Brukmana6538852003-11-06 21:55:44 +0000933 Last modified: $Date$
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000934</address>
John Criswellf08c5d82003-10-24 22:48:20 +0000935
John Criswellc310f622003-10-13 16:13:06 +0000936</body>
937</html>