xref: /aosp_15_r20/external/llvm/docs/FAQ.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker================================
2*9880d681SAndroid Build Coastguard WorkerFrequently Asked Questions (FAQ)
3*9880d681SAndroid Build Coastguard Worker================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard WorkerLicense
10*9880d681SAndroid Build Coastguard Worker=======
11*9880d681SAndroid Build Coastguard Worker
12*9880d681SAndroid Build Coastguard WorkerDoes the University of Illinois Open Source License really qualify as an "open source" license?
13*9880d681SAndroid Build Coastguard Worker-----------------------------------------------------------------------------------------------
14*9880d681SAndroid Build Coastguard WorkerYes, the license is `certified
15*9880d681SAndroid Build Coastguard Worker<http://www.opensource.org/licenses/UoI-NCSA.php>`_ by the Open Source
16*9880d681SAndroid Build Coastguard WorkerInitiative (OSI).
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard WorkerCan I modify LLVM source code and redistribute the modified source?
20*9880d681SAndroid Build Coastguard Worker-------------------------------------------------------------------
21*9880d681SAndroid Build Coastguard WorkerYes.  The modified source distribution must retain the copyright notice and
22*9880d681SAndroid Build Coastguard Workerfollow the three bulletted conditions listed in the `LLVM license
23*9880d681SAndroid Build Coastguard Worker<http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT>`_.
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard WorkerCan I modify the LLVM source code and redistribute binaries or other tools based on it, without redistributing the source?
27*9880d681SAndroid Build Coastguard Worker--------------------------------------------------------------------------------------------------------------------------
28*9880d681SAndroid Build Coastguard WorkerYes. This is why we distribute LLVM under a less restrictive license than GPL,
29*9880d681SAndroid Build Coastguard Workeras explained in the first question above.
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard WorkerSource Code
33*9880d681SAndroid Build Coastguard Worker===========
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard WorkerIn what language is LLVM written?
36*9880d681SAndroid Build Coastguard Worker---------------------------------
37*9880d681SAndroid Build Coastguard WorkerAll of the LLVM tools and libraries are written in C++ with extensive use of
38*9880d681SAndroid Build Coastguard Workerthe STL.
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard WorkerHow portable is the LLVM source code?
42*9880d681SAndroid Build Coastguard Worker-------------------------------------
43*9880d681SAndroid Build Coastguard WorkerThe LLVM source code should be portable to most modern Unix-like operating
44*9880d681SAndroid Build Coastguard Workersystems.  Most of the code is written in standard C++ with operating system
45*9880d681SAndroid Build Coastguard Workerservices abstracted to a support library.  The tools required to build and
46*9880d681SAndroid Build Coastguard Workertest LLVM have been ported to a plethora of platforms.
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard WorkerSome porting problems may exist in the following areas:
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker* The autoconf/makefile build system relies heavily on UNIX shell tools,
51*9880d681SAndroid Build Coastguard Worker  like the Bourne Shell and sed.  Porting to systems without these tools
52*9880d681SAndroid Build Coastguard Worker  (MacOS 9, Plan 9) will require more effort.
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard WorkerWhat API do I use to store a value to one of the virtual registers in LLVM IR's SSA representation?
55*9880d681SAndroid Build Coastguard Worker---------------------------------------------------------------------------------------------------
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard WorkerIn short: you can't. It's actually kind of a silly question once you grok
58*9880d681SAndroid Build Coastguard Workerwhat's going on. Basically, in code like:
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker    %result = add i32 %foo, %bar
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker, ``%result`` is just a name given to the ``Value`` of the ``add``
65*9880d681SAndroid Build Coastguard Workerinstruction. In other words, ``%result`` *is* the add instruction. The
66*9880d681SAndroid Build Coastguard Worker"assignment" doesn't explicitly "store" anything to any "virtual register";
67*9880d681SAndroid Build Coastguard Workerthe "``=``" is more like the mathematical sense of equality.
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard WorkerLonger explanation: In order to generate a textual representation of the
70*9880d681SAndroid Build Coastguard WorkerIR, some kind of name has to be given to each instruction so that other
71*9880d681SAndroid Build Coastguard Workerinstructions can textually reference it. However, the isomorphic in-memory
72*9880d681SAndroid Build Coastguard Workerrepresentation that you manipulate from C++ has no such restriction since
73*9880d681SAndroid Build Coastguard Workerinstructions can simply keep pointers to any other ``Value``'s that they
74*9880d681SAndroid Build Coastguard Workerreference. In fact, the names of dummy numbered temporaries like ``%1`` are
75*9880d681SAndroid Build Coastguard Workernot explicitly represented in the in-memory representation at all (see
76*9880d681SAndroid Build Coastguard Worker``Value::getName()``).
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard WorkerSource Languages
80*9880d681SAndroid Build Coastguard Worker================
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard WorkerWhat source languages are supported?
83*9880d681SAndroid Build Coastguard Worker------------------------------------
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard WorkerLLVM currently has full support for C and C++ source languages through
86*9880d681SAndroid Build Coastguard Worker`Clang <http://clang.llvm.org/>`_. Many other language frontends have
87*9880d681SAndroid Build Coastguard Workerbeen written using LLVM, and an incomplete list is available at
88*9880d681SAndroid Build Coastguard Worker`projects with LLVM <http://llvm.org/ProjectsWithLLVM/>`_.
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard WorkerI'd like to write a self-hosting LLVM compiler. How should I interface with the LLVM middle-end optimizers and back-end code generators?
92*9880d681SAndroid Build Coastguard Worker----------------------------------------------------------------------------------------------------------------------------------------
93*9880d681SAndroid Build Coastguard WorkerYour compiler front-end will communicate with LLVM by creating a module in the
94*9880d681SAndroid Build Coastguard WorkerLLVM intermediate representation (IR) format. Assuming you want to write your
95*9880d681SAndroid Build Coastguard Workerlanguage's compiler in the language itself (rather than C++), there are 3
96*9880d681SAndroid Build Coastguard Workermajor ways to tackle generating LLVM IR from a front-end:
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker1. **Call into the LLVM libraries code using your language's FFI (foreign
99*9880d681SAndroid Build Coastguard Worker   function interface).**
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker  * *for:* best tracks changes to the LLVM IR, .ll syntax, and .bc format
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker  * *for:* enables running LLVM optimization passes without a emit/parse
104*9880d681SAndroid Build Coastguard Worker    overhead
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker  * *for:* adapts well to a JIT context
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker  * *against:* lots of ugly glue code to write
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker2. **Emit LLVM assembly from your compiler's native language.**
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker  * *for:* very straightforward to get started
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker  * *against:* the .ll parser is slower than the bitcode reader when
115*9880d681SAndroid Build Coastguard Worker    interfacing to the middle end
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker  * *against:* it may be harder to track changes to the IR
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker3. **Emit LLVM bitcode from your compiler's native language.**
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker  * *for:* can use the more-efficient bitcode reader when interfacing to the
122*9880d681SAndroid Build Coastguard Worker    middle end
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker  * *against:* you'll have to re-engineer the LLVM IR object model and bitcode
125*9880d681SAndroid Build Coastguard Worker    writer in your language
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker  * *against:* it may be harder to track changes to the IR
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard WorkerIf you go with the first option, the C bindings in include/llvm-c should help
130*9880d681SAndroid Build Coastguard Workera lot, since most languages have strong support for interfacing with C. The
131*9880d681SAndroid Build Coastguard Workermost common hurdle with calling C from managed code is interfacing with the
132*9880d681SAndroid Build Coastguard Workergarbage collector. The C interface was designed to require very little memory
133*9880d681SAndroid Build Coastguard Workermanagement, and so is straightforward in this regard.
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard WorkerWhat support is there for a higher level source language constructs for building a compiler?
136*9880d681SAndroid Build Coastguard Worker--------------------------------------------------------------------------------------------
137*9880d681SAndroid Build Coastguard WorkerCurrently, there isn't much. LLVM supports an intermediate representation
138*9880d681SAndroid Build Coastguard Workerwhich is useful for code representation but will not support the high level
139*9880d681SAndroid Build Coastguard Worker(abstract syntax tree) representation needed by most compilers. There are no
140*9880d681SAndroid Build Coastguard Workerfacilities for lexical nor semantic analysis.
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard WorkerI don't understand the ``GetElementPtr`` instruction. Help!
144*9880d681SAndroid Build Coastguard Worker-----------------------------------------------------------
145*9880d681SAndroid Build Coastguard WorkerSee `The Often Misunderstood GEP Instruction <GetElementPtr.html>`_.
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard WorkerUsing the C and C++ Front Ends
149*9880d681SAndroid Build Coastguard Worker==============================
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard WorkerCan I compile C or C++ code to platform-independent LLVM bitcode?
152*9880d681SAndroid Build Coastguard Worker-----------------------------------------------------------------
153*9880d681SAndroid Build Coastguard WorkerNo. C and C++ are inherently platform-dependent languages. The most obvious
154*9880d681SAndroid Build Coastguard Workerexample of this is the preprocessor. A very common way that C code is made
155*9880d681SAndroid Build Coastguard Workerportable is by using the preprocessor to include platform-specific code. In
156*9880d681SAndroid Build Coastguard Workerpractice, information about other platforms is lost after preprocessing, so
157*9880d681SAndroid Build Coastguard Workerthe result is inherently dependent on the platform that the preprocessing was
158*9880d681SAndroid Build Coastguard Workertargeting.
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard WorkerAnother example is ``sizeof``. It's common for ``sizeof(long)`` to vary
161*9880d681SAndroid Build Coastguard Workerbetween platforms. In most C front-ends, ``sizeof`` is expanded to a
162*9880d681SAndroid Build Coastguard Workerconstant immediately, thus hard-wiring a platform-specific detail.
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard WorkerAlso, since many platforms define their ABIs in terms of C, and since LLVM is
165*9880d681SAndroid Build Coastguard Workerlower-level than C, front-ends currently must emit platform-specific IR in
166*9880d681SAndroid Build Coastguard Workerorder to have the result conform to the platform ABI.
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard WorkerQuestions about code generated by the demo page
170*9880d681SAndroid Build Coastguard Worker===============================================
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard WorkerWhat is this ``llvm.global_ctors`` and ``_GLOBAL__I_a...`` stuff that happens when I ``#include <iostream>``?
173*9880d681SAndroid Build Coastguard Worker-------------------------------------------------------------------------------------------------------------
174*9880d681SAndroid Build Coastguard WorkerIf you ``#include`` the ``<iostream>`` header into a C++ translation unit,
175*9880d681SAndroid Build Coastguard Workerthe file will probably use the ``std::cin``/``std::cout``/... global objects.
176*9880d681SAndroid Build Coastguard WorkerHowever, C++ does not guarantee an order of initialization between static
177*9880d681SAndroid Build Coastguard Workerobjects in different translation units, so if a static ctor/dtor in your .cpp
178*9880d681SAndroid Build Coastguard Workerfile used ``std::cout``, for example, the object would not necessarily be
179*9880d681SAndroid Build Coastguard Workerautomatically initialized before your use.
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard WorkerTo make ``std::cout`` and friends work correctly in these scenarios, the STL
182*9880d681SAndroid Build Coastguard Workerthat we use declares a static object that gets created in every translation
183*9880d681SAndroid Build Coastguard Workerunit that includes ``<iostream>``.  This object has a static constructor
184*9880d681SAndroid Build Coastguard Workerand destructor that initializes and destroys the global iostream objects
185*9880d681SAndroid Build Coastguard Workerbefore they could possibly be used in the file.  The code that you see in the
186*9880d681SAndroid Build Coastguard Worker``.ll`` file corresponds to the constructor and destructor registration code.
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard WorkerIf you would like to make it easier to *understand* the LLVM code generated
189*9880d681SAndroid Build Coastguard Workerby the compiler in the demo page, consider using ``printf()`` instead of
190*9880d681SAndroid Build Coastguard Worker``iostream``\s to print values.
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard WorkerWhere did all of my code go??
194*9880d681SAndroid Build Coastguard Worker-----------------------------
195*9880d681SAndroid Build Coastguard WorkerIf you are using the LLVM demo page, you may often wonder what happened to
196*9880d681SAndroid Build Coastguard Workerall of the code that you typed in.  Remember that the demo script is running
197*9880d681SAndroid Build Coastguard Workerthe code through the LLVM optimizers, so if your code doesn't actually do
198*9880d681SAndroid Build Coastguard Workeranything useful, it might all be deleted.
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard WorkerTo prevent this, make sure that the code is actually needed.  For example, if
201*9880d681SAndroid Build Coastguard Workeryou are computing some expression, return the value from the function instead
202*9880d681SAndroid Build Coastguard Workerof leaving it in a local variable.  If you really want to constrain the
203*9880d681SAndroid Build Coastguard Workeroptimizer, you can read from and assign to ``volatile`` global variables.
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard WorkerWhat is this "``undef``" thing that shows up in my code?
207*9880d681SAndroid Build Coastguard Worker--------------------------------------------------------
208*9880d681SAndroid Build Coastguard Worker``undef`` is the LLVM way of representing a value that is not defined.  You
209*9880d681SAndroid Build Coastguard Workercan get these if you do not initialize a variable before you use it.  For
210*9880d681SAndroid Build Coastguard Workerexample, the C function:
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker.. code-block:: c
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker   int X() { int i; return i; }
215*9880d681SAndroid Build Coastguard Worker
216*9880d681SAndroid Build Coastguard WorkerIs compiled to "``ret i32 undef``" because "``i``" never has a value specified
217*9880d681SAndroid Build Coastguard Workerfor it.
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard WorkerWhy does instcombine + simplifycfg turn a call to a function with a mismatched calling convention into "unreachable"? Why not make the verifier reject it?
221*9880d681SAndroid Build Coastguard Worker----------------------------------------------------------------------------------------------------------------------------------------------------------
222*9880d681SAndroid Build Coastguard WorkerThis is a common problem run into by authors of front-ends that are using
223*9880d681SAndroid Build Coastguard Workercustom calling conventions: you need to make sure to set the right calling
224*9880d681SAndroid Build Coastguard Workerconvention on both the function and on each call to the function.  For
225*9880d681SAndroid Build Coastguard Workerexample, this code:
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
230*9880d681SAndroid Build Coastguard Worker       ret void
231*9880d681SAndroid Build Coastguard Worker   }
232*9880d681SAndroid Build Coastguard Worker   define void @bar() {
233*9880d681SAndroid Build Coastguard Worker       call void @foo()
234*9880d681SAndroid Build Coastguard Worker       ret void
235*9880d681SAndroid Build Coastguard Worker   }
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard WorkerIs optimized to:
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
242*9880d681SAndroid Build Coastguard Worker       ret void
243*9880d681SAndroid Build Coastguard Worker   }
244*9880d681SAndroid Build Coastguard Worker   define void @bar() {
245*9880d681SAndroid Build Coastguard Worker       unreachable
246*9880d681SAndroid Build Coastguard Worker   }
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker... with "``opt -instcombine -simplifycfg``".  This often bites people because
249*9880d681SAndroid Build Coastguard Worker"all their code disappears".  Setting the calling convention on the caller and
250*9880d681SAndroid Build Coastguard Workercallee is required for indirect calls to work, so people often ask why not
251*9880d681SAndroid Build Coastguard Workermake the verifier reject this sort of thing.
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard WorkerThe answer is that this code has undefined behavior, but it is not illegal.
254*9880d681SAndroid Build Coastguard WorkerIf we made it illegal, then every transformation that could potentially create
255*9880d681SAndroid Build Coastguard Workerthis would have to ensure that it doesn't, and there is valid code that can
256*9880d681SAndroid Build Coastguard Workercreate this sort of construct (in dead code).  The sorts of things that can
257*9880d681SAndroid Build Coastguard Workercause this to happen are fairly contrived, but we still need to accept them.
258*9880d681SAndroid Build Coastguard WorkerHere's an example:
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
263*9880d681SAndroid Build Coastguard Worker       ret void
264*9880d681SAndroid Build Coastguard Worker   }
265*9880d681SAndroid Build Coastguard Worker   define internal void @bar(void()* %FP, i1 %cond) {
266*9880d681SAndroid Build Coastguard Worker       br i1 %cond, label %T, label %F
267*9880d681SAndroid Build Coastguard Worker   T:
268*9880d681SAndroid Build Coastguard Worker       call void %FP()
269*9880d681SAndroid Build Coastguard Worker       ret void
270*9880d681SAndroid Build Coastguard Worker   F:
271*9880d681SAndroid Build Coastguard Worker       call fastcc void %FP()
272*9880d681SAndroid Build Coastguard Worker       ret void
273*9880d681SAndroid Build Coastguard Worker   }
274*9880d681SAndroid Build Coastguard Worker   define void @test() {
275*9880d681SAndroid Build Coastguard Worker       %X = or i1 false, false
276*9880d681SAndroid Build Coastguard Worker       call void @bar(void()* @foo, i1 %X)
277*9880d681SAndroid Build Coastguard Worker       ret void
278*9880d681SAndroid Build Coastguard Worker   }
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard WorkerIn this example, "test" always passes ``@foo``/``false`` into ``bar``, which
281*9880d681SAndroid Build Coastguard Workerensures that it is dynamically called with the right calling conv (thus, the
282*9880d681SAndroid Build Coastguard Workercode is perfectly well defined).  If you run this through the inliner, you
283*9880d681SAndroid Build Coastguard Workerget this (the explicit "or" is there so that the inliner doesn't dead code
284*9880d681SAndroid Build Coastguard Workereliminate a bunch of stuff):
285*9880d681SAndroid Build Coastguard Worker
286*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
289*9880d681SAndroid Build Coastguard Worker       ret void
290*9880d681SAndroid Build Coastguard Worker   }
291*9880d681SAndroid Build Coastguard Worker   define void @test() {
292*9880d681SAndroid Build Coastguard Worker       %X = or i1 false, false
293*9880d681SAndroid Build Coastguard Worker       br i1 %X, label %T.i, label %F.i
294*9880d681SAndroid Build Coastguard Worker   T.i:
295*9880d681SAndroid Build Coastguard Worker       call void @foo()
296*9880d681SAndroid Build Coastguard Worker       br label %bar.exit
297*9880d681SAndroid Build Coastguard Worker   F.i:
298*9880d681SAndroid Build Coastguard Worker       call fastcc void @foo()
299*9880d681SAndroid Build Coastguard Worker       br label %bar.exit
300*9880d681SAndroid Build Coastguard Worker   bar.exit:
301*9880d681SAndroid Build Coastguard Worker       ret void
302*9880d681SAndroid Build Coastguard Worker   }
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard WorkerHere you can see that the inlining pass made an undefined call to ``@foo``
305*9880d681SAndroid Build Coastguard Workerwith the wrong calling convention.  We really don't want to make the inliner
306*9880d681SAndroid Build Coastguard Workerhave to know about this sort of thing, so it needs to be valid code.  In this
307*9880d681SAndroid Build Coastguard Workercase, dead code elimination can trivially remove the undefined code.  However,
308*9880d681SAndroid Build Coastguard Workerif ``%X`` was an input argument to ``@test``, the inliner would produce this:
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
313*9880d681SAndroid Build Coastguard Worker       ret void
314*9880d681SAndroid Build Coastguard Worker   }
315*9880d681SAndroid Build Coastguard Worker
316*9880d681SAndroid Build Coastguard Worker   define void @test(i1 %X) {
317*9880d681SAndroid Build Coastguard Worker       br i1 %X, label %T.i, label %F.i
318*9880d681SAndroid Build Coastguard Worker   T.i:
319*9880d681SAndroid Build Coastguard Worker       call void @foo()
320*9880d681SAndroid Build Coastguard Worker       br label %bar.exit
321*9880d681SAndroid Build Coastguard Worker   F.i:
322*9880d681SAndroid Build Coastguard Worker       call fastcc void @foo()
323*9880d681SAndroid Build Coastguard Worker       br label %bar.exit
324*9880d681SAndroid Build Coastguard Worker   bar.exit:
325*9880d681SAndroid Build Coastguard Worker       ret void
326*9880d681SAndroid Build Coastguard Worker   }
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard WorkerThe interesting thing about this is that ``%X`` *must* be false for the
329*9880d681SAndroid Build Coastguard Workercode to be well-defined, but no amount of dead code elimination will be able
330*9880d681SAndroid Build Coastguard Workerto delete the broken call as unreachable.  However, since
331*9880d681SAndroid Build Coastguard Worker``instcombine``/``simplifycfg`` turns the undefined call into unreachable, we
332*9880d681SAndroid Build Coastguard Workerend up with a branch on a condition that goes to unreachable: a branch to
333*9880d681SAndroid Build Coastguard Workerunreachable can never happen, so "``-inline -instcombine -simplifycfg``" is
334*9880d681SAndroid Build Coastguard Workerable to produce:
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard Worker   define fastcc void @foo() {
339*9880d681SAndroid Build Coastguard Worker      ret void
340*9880d681SAndroid Build Coastguard Worker   }
341*9880d681SAndroid Build Coastguard Worker   define void @test(i1 %X) {
342*9880d681SAndroid Build Coastguard Worker   F.i:
343*9880d681SAndroid Build Coastguard Worker      call fastcc void @foo()
344*9880d681SAndroid Build Coastguard Worker      ret void
345*9880d681SAndroid Build Coastguard Worker   }
346