xref: /aosp_15_r20/external/llvm/docs/ExceptionHandling.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker==========================
2*9880d681SAndroid Build Coastguard WorkerException Handling in LLVM
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 WorkerIntroduction
9*9880d681SAndroid Build Coastguard Worker============
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerThis document is the central repository for all information pertaining to
12*9880d681SAndroid Build Coastguard Workerexception handling in LLVM.  It describes the format that LLVM exception
13*9880d681SAndroid Build Coastguard Workerhandling information takes, which is useful for those interested in creating
14*9880d681SAndroid Build Coastguard Workerfront-ends or dealing directly with the information.  Further, this document
15*9880d681SAndroid Build Coastguard Workerprovides specific examples of what exception handling information is used for in
16*9880d681SAndroid Build Coastguard WorkerC and C++.
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard WorkerItanium ABI Zero-cost Exception Handling
19*9880d681SAndroid Build Coastguard Worker----------------------------------------
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard WorkerException handling for most programming languages is designed to recover from
22*9880d681SAndroid Build Coastguard Workerconditions that rarely occur during general use of an application.  To that end,
23*9880d681SAndroid Build Coastguard Workerexception handling should not interfere with the main flow of an application's
24*9880d681SAndroid Build Coastguard Workeralgorithm by performing checkpointing tasks, such as saving the current pc or
25*9880d681SAndroid Build Coastguard Workerregister state.
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard WorkerThe Itanium ABI Exception Handling Specification defines a methodology for
28*9880d681SAndroid Build Coastguard Workerproviding outlying data in the form of exception tables without inlining
29*9880d681SAndroid Build Coastguard Workerspeculative exception handling code in the flow of an application's main
30*9880d681SAndroid Build Coastguard Workeralgorithm.  Thus, the specification is said to add "zero-cost" to the normal
31*9880d681SAndroid Build Coastguard Workerexecution of an application.
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard WorkerA more complete description of the Itanium ABI exception handling runtime
34*9880d681SAndroid Build Coastguard Workersupport of can be found at `Itanium C++ ABI: Exception Handling
35*9880d681SAndroid Build Coastguard Worker<http://mentorembedded.github.com/cxx-abi/abi-eh.html>`_. A description of the
36*9880d681SAndroid Build Coastguard Workerexception frame format can be found at `Exception Frames
37*9880d681SAndroid Build Coastguard Worker<http://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html>`_,
38*9880d681SAndroid Build Coastguard Workerwith details of the DWARF 4 specification at `DWARF 4 Standard
39*9880d681SAndroid Build Coastguard Worker<http://dwarfstd.org/Dwarf4Std.php>`_.  A description for the C++ exception
40*9880d681SAndroid Build Coastguard Workertable formats can be found at `Exception Handling Tables
41*9880d681SAndroid Build Coastguard Worker<http://mentorembedded.github.com/cxx-abi/exceptions.pdf>`_.
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard WorkerSetjmp/Longjmp Exception Handling
44*9880d681SAndroid Build Coastguard Worker---------------------------------
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard WorkerSetjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
47*9880d681SAndroid Build Coastguard Worker`llvm.eh.sjlj.setjmp`_ and `llvm.eh.sjlj.longjmp`_ to handle control flow for
48*9880d681SAndroid Build Coastguard Workerexception handling.
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard WorkerFor each function which does exception processing --- be it ``try``/``catch``
51*9880d681SAndroid Build Coastguard Workerblocks or cleanups --- that function registers itself on a global frame
52*9880d681SAndroid Build Coastguard Workerlist. When exceptions are unwinding, the runtime uses this list to identify
53*9880d681SAndroid Build Coastguard Workerwhich functions need processing.
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard WorkerLanding pad selection is encoded in the call site entry of the function
56*9880d681SAndroid Build Coastguard Workercontext. The runtime returns to the function via `llvm.eh.sjlj.longjmp`_, where
57*9880d681SAndroid Build Coastguard Workera switch table transfers control to the appropriate landing pad based on the
58*9880d681SAndroid Build Coastguard Workerindex stored in the function context.
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard WorkerIn contrast to DWARF exception handling, which encodes exception regions and
61*9880d681SAndroid Build Coastguard Workerframe information in out-of-line tables, SJLJ exception handling builds and
62*9880d681SAndroid Build Coastguard Workerremoves the unwind frame context at runtime. This results in faster exception
63*9880d681SAndroid Build Coastguard Workerhandling at the expense of slower execution when no exceptions are thrown. As
64*9880d681SAndroid Build Coastguard Workerexceptions are, by their nature, intended for uncommon code paths, DWARF
65*9880d681SAndroid Build Coastguard Workerexception handling is generally preferred to SJLJ.
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard WorkerWindows Runtime Exception Handling
68*9880d681SAndroid Build Coastguard Worker-----------------------------------
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard WorkerLLVM supports handling exceptions produced by the Windows runtime, but it
71*9880d681SAndroid Build Coastguard Workerrequires a very different intermediate representation. It is not based on the
72*9880d681SAndroid Build Coastguard Worker":ref:`landingpad <i_landingpad>`" instruction like the other two models, and is
73*9880d681SAndroid Build Coastguard Workerdescribed later in this document under :ref:`wineh`.
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard WorkerOverview
76*9880d681SAndroid Build Coastguard Worker--------
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard WorkerWhen an exception is thrown in LLVM code, the runtime does its best to find a
79*9880d681SAndroid Build Coastguard Workerhandler suited to processing the circumstance.
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard WorkerThe runtime first attempts to find an *exception frame* corresponding to the
82*9880d681SAndroid Build Coastguard Workerfunction where the exception was thrown.  If the programming language supports
83*9880d681SAndroid Build Coastguard Workerexception handling (e.g. C++), the exception frame contains a reference to an
84*9880d681SAndroid Build Coastguard Workerexception table describing how to process the exception.  If the language does
85*9880d681SAndroid Build Coastguard Workernot support exception handling (e.g. C), or if the exception needs to be
86*9880d681SAndroid Build Coastguard Workerforwarded to a prior activation, the exception frame contains information about
87*9880d681SAndroid Build Coastguard Workerhow to unwind the current activation and restore the state of the prior
88*9880d681SAndroid Build Coastguard Workeractivation.  This process is repeated until the exception is handled. If the
89*9880d681SAndroid Build Coastguard Workerexception is not handled and no activations remain, then the application is
90*9880d681SAndroid Build Coastguard Workerterminated with an appropriate error message.
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard WorkerBecause different programming languages have different behaviors when handling
93*9880d681SAndroid Build Coastguard Workerexceptions, the exception handling ABI provides a mechanism for
94*9880d681SAndroid Build Coastguard Workersupplying *personalities*. An exception handling personality is defined by
95*9880d681SAndroid Build Coastguard Workerway of a *personality function* (e.g. ``__gxx_personality_v0`` in C++),
96*9880d681SAndroid Build Coastguard Workerwhich receives the context of the exception, an *exception structure*
97*9880d681SAndroid Build Coastguard Workercontaining the exception object type and value, and a reference to the exception
98*9880d681SAndroid Build Coastguard Workertable for the current function.  The personality function for the current
99*9880d681SAndroid Build Coastguard Workercompile unit is specified in a *common exception frame*.
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard WorkerThe organization of an exception table is language dependent. For C++, an
102*9880d681SAndroid Build Coastguard Workerexception table is organized as a series of code ranges defining what to do if
103*9880d681SAndroid Build Coastguard Workeran exception occurs in that range. Typically, the information associated with a
104*9880d681SAndroid Build Coastguard Workerrange defines which types of exception objects (using C++ *type info*) that are
105*9880d681SAndroid Build Coastguard Workerhandled in that range, and an associated action that should take place. Actions
106*9880d681SAndroid Build Coastguard Workertypically pass control to a *landing pad*.
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard WorkerA landing pad corresponds roughly to the code found in the ``catch`` portion of
109*9880d681SAndroid Build Coastguard Workera ``try``/``catch`` sequence. When execution resumes at a landing pad, it
110*9880d681SAndroid Build Coastguard Workerreceives an *exception structure* and a *selector value* corresponding to the
111*9880d681SAndroid Build Coastguard Worker*type* of exception thrown. The selector is then used to determine which *catch*
112*9880d681SAndroid Build Coastguard Workershould actually process the exception.
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard WorkerLLVM Code Generation
115*9880d681SAndroid Build Coastguard Worker====================
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard WorkerFrom a C++ developer's perspective, exceptions are defined in terms of the
118*9880d681SAndroid Build Coastguard Worker``throw`` and ``try``/``catch`` statements. In this section we will describe the
119*9880d681SAndroid Build Coastguard Workerimplementation of LLVM exception handling in terms of C++ examples.
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard WorkerThrow
122*9880d681SAndroid Build Coastguard Worker-----
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard WorkerLanguages that support exception handling typically provide a ``throw``
125*9880d681SAndroid Build Coastguard Workeroperation to initiate the exception process. Internally, a ``throw`` operation
126*9880d681SAndroid Build Coastguard Workerbreaks down into two steps.
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker#. A request is made to allocate exception space for an exception structure.
129*9880d681SAndroid Build Coastguard Worker   This structure needs to survive beyond the current activation. This structure
130*9880d681SAndroid Build Coastguard Worker   will contain the type and value of the object being thrown.
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker#. A call is made to the runtime to raise the exception, passing the exception
133*9880d681SAndroid Build Coastguard Worker   structure as an argument.
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard WorkerIn C++, the allocation of the exception structure is done by the
136*9880d681SAndroid Build Coastguard Worker``__cxa_allocate_exception`` runtime function. The exception raising is handled
137*9880d681SAndroid Build Coastguard Workerby ``__cxa_throw``. The type of the exception is represented using a C++ RTTI
138*9880d681SAndroid Build Coastguard Workerstructure.
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard WorkerTry/Catch
141*9880d681SAndroid Build Coastguard Worker---------
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard WorkerA call within the scope of a *try* statement can potentially raise an
144*9880d681SAndroid Build Coastguard Workerexception. In those circumstances, the LLVM C++ front-end replaces the call with
145*9880d681SAndroid Build Coastguard Workeran ``invoke`` instruction. Unlike a call, the ``invoke`` has two potential
146*9880d681SAndroid Build Coastguard Workercontinuation points:
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker#. where to continue when the call succeeds as per normal, and
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker#. where to continue if the call raises an exception, either by a throw or the
151*9880d681SAndroid Build Coastguard Worker   unwinding of a throw
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard WorkerThe term used to define the place where an ``invoke`` continues after an
154*9880d681SAndroid Build Coastguard Workerexception is called a *landing pad*. LLVM landing pads are conceptually
155*9880d681SAndroid Build Coastguard Workeralternative function entry points where an exception structure reference and a
156*9880d681SAndroid Build Coastguard Workertype info index are passed in as arguments. The landing pad saves the exception
157*9880d681SAndroid Build Coastguard Workerstructure reference and then proceeds to select the catch block that corresponds
158*9880d681SAndroid Build Coastguard Workerto the type info of the exception object.
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard WorkerThe LLVM :ref:`i_landingpad` is used to convey information about the landing
161*9880d681SAndroid Build Coastguard Workerpad to the back end. For C++, the ``landingpad`` instruction returns a pointer
162*9880d681SAndroid Build Coastguard Workerand integer pair corresponding to the pointer to the *exception structure* and
163*9880d681SAndroid Build Coastguard Workerthe *selector value* respectively.
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard WorkerThe ``landingpad`` instruction looks for a reference to the personality
166*9880d681SAndroid Build Coastguard Workerfunction to be used for this ``try``/``catch`` sequence in the parent
167*9880d681SAndroid Build Coastguard Workerfunction's attribute list. The instruction contains a list of *cleanup*,
168*9880d681SAndroid Build Coastguard Worker*catch*, and *filter* clauses. The exception is tested against the clauses
169*9880d681SAndroid Build Coastguard Workersequentially from first to last. The clauses have the following meanings:
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard Worker-  ``catch <type> @ExcType``
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker   - This clause means that the landingpad block should be entered if the
174*9880d681SAndroid Build Coastguard Worker     exception being thrown is of type ``@ExcType`` or a subtype of
175*9880d681SAndroid Build Coastguard Worker     ``@ExcType``. For C++, ``@ExcType`` is a pointer to the ``std::type_info``
176*9880d681SAndroid Build Coastguard Worker     object (an RTTI object) representing the C++ exception type.
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker   - If ``@ExcType`` is ``null``, any exception matches, so the landingpad
179*9880d681SAndroid Build Coastguard Worker     should always be entered. This is used for C++ catch-all blocks ("``catch
180*9880d681SAndroid Build Coastguard Worker     (...)``").
181*9880d681SAndroid Build Coastguard Worker
182*9880d681SAndroid Build Coastguard Worker   - When this clause is matched, the selector value will be equal to the value
183*9880d681SAndroid Build Coastguard Worker     returned by "``@llvm.eh.typeid.for(i8* @ExcType)``". This will always be a
184*9880d681SAndroid Build Coastguard Worker     positive value.
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker-  ``filter <type> [<type> @ExcType1, ..., <type> @ExcTypeN]``
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker   - This clause means that the landingpad should be entered if the exception
189*9880d681SAndroid Build Coastguard Worker     being thrown does *not* match any of the types in the list (which, for C++,
190*9880d681SAndroid Build Coastguard Worker     are again specified as ``std::type_info`` pointers).
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker   - C++ front-ends use this to implement C++ exception specifications, such as
193*9880d681SAndroid Build Coastguard Worker     "``void foo() throw (ExcType1, ..., ExcTypeN) { ... }``".
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker   - When this clause is matched, the selector value will be negative.
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker   - The array argument to ``filter`` may be empty; for example, "``[0 x i8**]
198*9880d681SAndroid Build Coastguard Worker     undef``". This means that the landingpad should always be entered. (Note
199*9880d681SAndroid Build Coastguard Worker     that such a ``filter`` would not be equivalent to "``catch i8* null``",
200*9880d681SAndroid Build Coastguard Worker     because ``filter`` and ``catch`` produce negative and positive selector
201*9880d681SAndroid Build Coastguard Worker     values respectively.)
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker-  ``cleanup``
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker   - This clause means that the landingpad should always be entered.
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker   - C++ front-ends use this for calling objects' destructors.
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker   - When this clause is matched, the selector value will be zero.
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Worker   - The runtime may treat "``cleanup``" differently from "``catch <type>
212*9880d681SAndroid Build Coastguard Worker     null``".
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker     In C++, if an unhandled exception occurs, the language runtime will call
215*9880d681SAndroid Build Coastguard Worker     ``std::terminate()``, but it is implementation-defined whether the runtime
216*9880d681SAndroid Build Coastguard Worker     unwinds the stack and calls object destructors first. For example, the GNU
217*9880d681SAndroid Build Coastguard Worker     C++ unwinder does not call object destructors when an unhandled exception
218*9880d681SAndroid Build Coastguard Worker     occurs. The reason for this is to improve debuggability: it ensures that
219*9880d681SAndroid Build Coastguard Worker     ``std::terminate()`` is called from the context of the ``throw``, so that
220*9880d681SAndroid Build Coastguard Worker     this context is not lost by unwinding the stack. A runtime will typically
221*9880d681SAndroid Build Coastguard Worker     implement this by searching for a matching non-``cleanup`` clause, and
222*9880d681SAndroid Build Coastguard Worker     aborting if it does not find one, before entering any landingpad blocks.
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard WorkerOnce the landing pad has the type info selector, the code branches to the code
225*9880d681SAndroid Build Coastguard Workerfor the first catch. The catch then checks the value of the type info selector
226*9880d681SAndroid Build Coastguard Workeragainst the index of type info for that catch.  Since the type info index is not
227*9880d681SAndroid Build Coastguard Workerknown until all the type infos have been gathered in the backend, the catch code
228*9880d681SAndroid Build Coastguard Workermust call the `llvm.eh.typeid.for`_ intrinsic to determine the index for a given
229*9880d681SAndroid Build Coastguard Workertype info. If the catch fails to match the selector then control is passed on to
230*9880d681SAndroid Build Coastguard Workerthe next catch.
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard WorkerFinally, the entry and exit of catch code is bracketed with calls to
233*9880d681SAndroid Build Coastguard Worker``__cxa_begin_catch`` and ``__cxa_end_catch``.
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker* ``__cxa_begin_catch`` takes an exception structure reference as an argument
236*9880d681SAndroid Build Coastguard Worker  and returns the value of the exception object.
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker* ``__cxa_end_catch`` takes no arguments. This function:
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker  #. Locates the most recently caught exception and decrements its handler
241*9880d681SAndroid Build Coastguard Worker     count,
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker  #. Removes the exception from the *caught* stack if the handler count goes to
244*9880d681SAndroid Build Coastguard Worker     zero, and
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker  #. Destroys the exception if the handler count goes to zero and the exception
247*9880d681SAndroid Build Coastguard Worker     was not re-thrown by throw.
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker  .. note::
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker    a rethrow from within the catch may replace this call with a
252*9880d681SAndroid Build Coastguard Worker    ``__cxa_rethrow``.
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard WorkerCleanups
255*9880d681SAndroid Build Coastguard Worker--------
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard WorkerA cleanup is extra code which needs to be run as part of unwinding a scope.  C++
258*9880d681SAndroid Build Coastguard Workerdestructors are a typical example, but other languages and language extensions
259*9880d681SAndroid Build Coastguard Workerprovide a variety of different kinds of cleanups. In general, a landing pad may
260*9880d681SAndroid Build Coastguard Workerneed to run arbitrary amounts of cleanup code before actually entering a catch
261*9880d681SAndroid Build Coastguard Workerblock. To indicate the presence of cleanups, a :ref:`i_landingpad` should have
262*9880d681SAndroid Build Coastguard Workera *cleanup* clause.  Otherwise, the unwinder will not stop at the landing pad if
263*9880d681SAndroid Build Coastguard Workerthere are no catches or filters that require it to.
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker.. note::
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker  Do not allow a new exception to propagate out of the execution of a
268*9880d681SAndroid Build Coastguard Worker  cleanup. This can corrupt the internal state of the unwinder.  Different
269*9880d681SAndroid Build Coastguard Worker  languages describe different high-level semantics for these situations: for
270*9880d681SAndroid Build Coastguard Worker  example, C++ requires that the process be terminated, whereas Ada cancels both
271*9880d681SAndroid Build Coastguard Worker  exceptions and throws a third.
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard WorkerWhen all cleanups are finished, if the exception is not handled by the current
274*9880d681SAndroid Build Coastguard Workerfunction, resume unwinding by calling the :ref:`resume instruction <i_resume>`,
275*9880d681SAndroid Build Coastguard Workerpassing in the result of the ``landingpad`` instruction for the original
276*9880d681SAndroid Build Coastguard Workerlanding pad.
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard WorkerThrow Filters
279*9880d681SAndroid Build Coastguard Worker-------------
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard WorkerC++ allows the specification of which exception types may be thrown from a
282*9880d681SAndroid Build Coastguard Workerfunction. To represent this, a top level landing pad may exist to filter out
283*9880d681SAndroid Build Coastguard Workerinvalid types. To express this in LLVM code the :ref:`i_landingpad` will have a
284*9880d681SAndroid Build Coastguard Workerfilter clause. The clause consists of an array of type infos.
285*9880d681SAndroid Build Coastguard Worker``landingpad`` will return a negative value
286*9880d681SAndroid Build Coastguard Workerif the exception does not match any of the type infos. If no match is found then
287*9880d681SAndroid Build Coastguard Workera call to ``__cxa_call_unexpected`` should be made, otherwise
288*9880d681SAndroid Build Coastguard Worker``_Unwind_Resume``.  Each of these functions requires a reference to the
289*9880d681SAndroid Build Coastguard Workerexception structure.  Note that the most general form of a ``landingpad``
290*9880d681SAndroid Build Coastguard Workerinstruction can have any number of catch, cleanup, and filter clauses (though
291*9880d681SAndroid Build Coastguard Workerhaving more than one cleanup is pointless). The LLVM C++ front-end can generate
292*9880d681SAndroid Build Coastguard Workersuch ``landingpad`` instructions due to inlining creating nested exception
293*9880d681SAndroid Build Coastguard Workerhandling scopes.
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker.. _undefined:
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard WorkerRestrictions
298*9880d681SAndroid Build Coastguard Worker------------
299*9880d681SAndroid Build Coastguard Worker
300*9880d681SAndroid Build Coastguard WorkerThe unwinder delegates the decision of whether to stop in a call frame to that
301*9880d681SAndroid Build Coastguard Workercall frame's language-specific personality function. Not all unwinders guarantee
302*9880d681SAndroid Build Coastguard Workerthat they will stop to perform cleanups. For example, the GNU C++ unwinder
303*9880d681SAndroid Build Coastguard Workerdoesn't do so unless the exception is actually caught somewhere further up the
304*9880d681SAndroid Build Coastguard Workerstack.
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard WorkerIn order for inlining to behave correctly, landing pads must be prepared to
307*9880d681SAndroid Build Coastguard Workerhandle selector results that they did not originally advertise. Suppose that a
308*9880d681SAndroid Build Coastguard Workerfunction catches exceptions of type ``A``, and it's inlined into a function that
309*9880d681SAndroid Build Coastguard Workercatches exceptions of type ``B``. The inliner will update the ``landingpad``
310*9880d681SAndroid Build Coastguard Workerinstruction for the inlined landing pad to include the fact that ``B`` is also
311*9880d681SAndroid Build Coastguard Workercaught. If that landing pad assumes that it will only be entered to catch an
312*9880d681SAndroid Build Coastguard Worker``A``, it's in for a rude awakening.  Consequently, landing pads must test for
313*9880d681SAndroid Build Coastguard Workerthe selector results they understand and then resume exception propagation with
314*9880d681SAndroid Build Coastguard Workerthe `resume instruction <LangRef.html#i_resume>`_ if none of the conditions
315*9880d681SAndroid Build Coastguard Workermatch.
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard WorkerException Handling Intrinsics
318*9880d681SAndroid Build Coastguard Worker=============================
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard WorkerIn addition to the ``landingpad`` and ``resume`` instructions, LLVM uses several
321*9880d681SAndroid Build Coastguard Workerintrinsic functions (name prefixed with ``llvm.eh``) to provide exception
322*9880d681SAndroid Build Coastguard Workerhandling information at various points in generated code.
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.typeid.for:
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker``llvm.eh.typeid.for``
327*9880d681SAndroid Build Coastguard Worker----------------------
328*9880d681SAndroid Build Coastguard Worker
329*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker  i32 @llvm.eh.typeid.for(i8* %type_info)
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard WorkerThis intrinsic returns the type info index in the exception table of the current
335*9880d681SAndroid Build Coastguard Workerfunction.  This value can be used to compare against the result of
336*9880d681SAndroid Build Coastguard Worker``landingpad`` instruction.  The single argument is a reference to a type info.
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard WorkerUses of this intrinsic are generated by the C++ front-end.
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.begincatch:
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard Worker``llvm.eh.begincatch``
343*9880d681SAndroid Build Coastguard Worker----------------------
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker  void @llvm.eh.begincatch(i8* %ehptr, i8* %ehobj)
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard WorkerThis intrinsic marks the beginning of catch handling code within the blocks
351*9880d681SAndroid Build Coastguard Workerfollowing a ``landingpad`` instruction.  The exact behavior of this function
352*9880d681SAndroid Build Coastguard Workerdepends on the compilation target and the personality function associated
353*9880d681SAndroid Build Coastguard Workerwith the ``landingpad`` instruction.
354*9880d681SAndroid Build Coastguard Worker
355*9880d681SAndroid Build Coastguard WorkerThe first argument to this intrinsic is a pointer that was previously extracted
356*9880d681SAndroid Build Coastguard Workerfrom the aggregate return value of the ``landingpad`` instruction.  The second
357*9880d681SAndroid Build Coastguard Workerargument to the intrinsic is a pointer to stack space where the exception object
358*9880d681SAndroid Build Coastguard Workershould be stored. The runtime handles the details of copying the exception
359*9880d681SAndroid Build Coastguard Workerobject into the slot. If the second parameter is null, no copy occurs.
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard WorkerUses of this intrinsic are generated by the C++ front-end.  Many targets will
362*9880d681SAndroid Build Coastguard Workeruse implementation-specific functions (such as ``__cxa_begin_catch``) instead
363*9880d681SAndroid Build Coastguard Workerof this intrinsic.  The intrinsic is provided for targets that require a more
364*9880d681SAndroid Build Coastguard Workerabstract interface.
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard WorkerWhen used in the native Windows C++ exception handling implementation, this
367*9880d681SAndroid Build Coastguard Workerintrinsic serves as a placeholder to delimit code before a catch handler is
368*9880d681SAndroid Build Coastguard Workeroutlined.  When the handler is is outlined, this intrinsic will be replaced
369*9880d681SAndroid Build Coastguard Workerby instructions that retrieve the exception object pointer from the frame
370*9880d681SAndroid Build Coastguard Workerallocation block.
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.endcatch:
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard Worker``llvm.eh.endcatch``
376*9880d681SAndroid Build Coastguard Worker----------------------
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard Worker  void @llvm.eh.endcatch()
381*9880d681SAndroid Build Coastguard Worker
382*9880d681SAndroid Build Coastguard Worker
383*9880d681SAndroid Build Coastguard WorkerThis intrinsic marks the end of catch handling code within the current block,
384*9880d681SAndroid Build Coastguard Workerwhich will be a successor of a block which called ``llvm.eh.begincatch''.
385*9880d681SAndroid Build Coastguard WorkerThe exact behavior of this function depends on the compilation target and the
386*9880d681SAndroid Build Coastguard Workerpersonality function associated with the corresponding ``landingpad``
387*9880d681SAndroid Build Coastguard Workerinstruction.
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard WorkerThere may be more than one call to ``llvm.eh.endcatch`` for any given call to
390*9880d681SAndroid Build Coastguard Worker``llvm.eh.begincatch`` with each ``llvm.eh.endcatch`` call corresponding to the
391*9880d681SAndroid Build Coastguard Workerend of a different control path.  All control paths following a call to
392*9880d681SAndroid Build Coastguard Worker``llvm.eh.begincatch`` must reach a call to ``llvm.eh.endcatch``.
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard WorkerUses of this intrinsic are generated by the C++ front-end.  Many targets will
395*9880d681SAndroid Build Coastguard Workeruse implementation-specific functions (such as ``__cxa_begin_catch``) instead
396*9880d681SAndroid Build Coastguard Workerof this intrinsic.  The intrinsic is provided for targets that require a more
397*9880d681SAndroid Build Coastguard Workerabstract interface.
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard WorkerWhen used in the native Windows C++ exception handling implementation, this
400*9880d681SAndroid Build Coastguard Workerintrinsic serves as a placeholder to delimit code before a catch handler is
401*9880d681SAndroid Build Coastguard Workeroutlined.  After the handler is outlined, this intrinsic is simply removed.
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker
404*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.exceptionpointer:
405*9880d681SAndroid Build Coastguard Worker
406*9880d681SAndroid Build Coastguard Worker``llvm.eh.exceptionpointer``
407*9880d681SAndroid Build Coastguard Worker----------------------------
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker  i8 addrspace(N)* @llvm.eh.padparam.pNi8(token %catchpad)
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker
414*9880d681SAndroid Build Coastguard WorkerThis intrinsic retrieves a pointer to the exception caught by the given
415*9880d681SAndroid Build Coastguard Worker``catchpad``.
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard WorkerSJLJ Intrinsics
419*9880d681SAndroid Build Coastguard Worker---------------
420*9880d681SAndroid Build Coastguard Worker
421*9880d681SAndroid Build Coastguard WorkerThe ``llvm.eh.sjlj`` intrinsics are used internally within LLVM's
422*9880d681SAndroid Build Coastguard Workerbackend.  Uses of them are generated by the backend's
423*9880d681SAndroid Build Coastguard Worker``SjLjEHPrepare`` pass.
424*9880d681SAndroid Build Coastguard Worker
425*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.sjlj.setjmp:
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard Worker``llvm.eh.sjlj.setjmp``
428*9880d681SAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker  i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard WorkerFor SJLJ based exception handling, this intrinsic forces register saving for the
435*9880d681SAndroid Build Coastguard Workercurrent function and stores the address of the following instruction for use as
436*9880d681SAndroid Build Coastguard Workera destination address by `llvm.eh.sjlj.longjmp`_. The buffer format and the
437*9880d681SAndroid Build Coastguard Workeroverall functioning of this intrinsic is compatible with the GCC
438*9880d681SAndroid Build Coastguard Worker``__builtin_setjmp`` implementation allowing code built with the clang and GCC
439*9880d681SAndroid Build Coastguard Workerto interoperate.
440*9880d681SAndroid Build Coastguard Worker
441*9880d681SAndroid Build Coastguard WorkerThe single parameter is a pointer to a five word buffer in which the calling
442*9880d681SAndroid Build Coastguard Workercontext is saved. The front end places the frame pointer in the first word, and
443*9880d681SAndroid Build Coastguard Workerthe target implementation of this intrinsic should place the destination address
444*9880d681SAndroid Build Coastguard Workerfor a `llvm.eh.sjlj.longjmp`_ in the second word. The following three words are
445*9880d681SAndroid Build Coastguard Workeravailable for use in a target-specific manner.
446*9880d681SAndroid Build Coastguard Worker
447*9880d681SAndroid Build Coastguard Worker.. _llvm.eh.sjlj.longjmp:
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker``llvm.eh.sjlj.longjmp``
450*9880d681SAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
453*9880d681SAndroid Build Coastguard Worker
454*9880d681SAndroid Build Coastguard Worker  void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard WorkerFor SJLJ based exception handling, the ``llvm.eh.sjlj.longjmp`` intrinsic is
457*9880d681SAndroid Build Coastguard Workerused to implement ``__builtin_longjmp()``. The single parameter is a pointer to
458*9880d681SAndroid Build Coastguard Workera buffer populated by `llvm.eh.sjlj.setjmp`_. The frame pointer and stack
459*9880d681SAndroid Build Coastguard Workerpointer are restored from the buffer, then control is transferred to the
460*9880d681SAndroid Build Coastguard Workerdestination address.
461*9880d681SAndroid Build Coastguard Worker
462*9880d681SAndroid Build Coastguard Worker``llvm.eh.sjlj.lsda``
463*9880d681SAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~
464*9880d681SAndroid Build Coastguard Worker
465*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker  i8* @llvm.eh.sjlj.lsda()
468*9880d681SAndroid Build Coastguard Worker
469*9880d681SAndroid Build Coastguard WorkerFor SJLJ based exception handling, the ``llvm.eh.sjlj.lsda`` intrinsic returns
470*9880d681SAndroid Build Coastguard Workerthe address of the Language Specific Data Area (LSDA) for the current
471*9880d681SAndroid Build Coastguard Workerfunction. The SJLJ front-end code stores this address in the exception handling
472*9880d681SAndroid Build Coastguard Workerfunction context for use by the runtime.
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker``llvm.eh.sjlj.callsite``
475*9880d681SAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard Worker  void @llvm.eh.sjlj.callsite(i32 %call_site_num)
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard WorkerFor SJLJ based exception handling, the ``llvm.eh.sjlj.callsite`` intrinsic
482*9880d681SAndroid Build Coastguard Workeridentifies the callsite value associated with the following ``invoke``
483*9880d681SAndroid Build Coastguard Workerinstruction. This is used to ensure that landing pad entries in the LSDA are
484*9880d681SAndroid Build Coastguard Workergenerated in matching order.
485*9880d681SAndroid Build Coastguard Worker
486*9880d681SAndroid Build Coastguard WorkerAsm Table Formats
487*9880d681SAndroid Build Coastguard Worker=================
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard WorkerThere are two tables that are used by the exception handling runtime to
490*9880d681SAndroid Build Coastguard Workerdetermine which actions should be taken when an exception is thrown.
491*9880d681SAndroid Build Coastguard Worker
492*9880d681SAndroid Build Coastguard WorkerException Handling Frame
493*9880d681SAndroid Build Coastguard Worker------------------------
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard WorkerAn exception handling frame ``eh_frame`` is very similar to the unwind frame
496*9880d681SAndroid Build Coastguard Workerused by DWARF debug info. The frame contains all the information necessary to
497*9880d681SAndroid Build Coastguard Workertear down the current frame and restore the state of the prior frame. There is
498*9880d681SAndroid Build Coastguard Workeran exception handling frame for each function in a compile unit, plus a common
499*9880d681SAndroid Build Coastguard Workerexception handling frame that defines information common to all functions in the
500*9880d681SAndroid Build Coastguard Workerunit.
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard WorkerThe format of this call frame information (CFI) is often platform-dependent,
503*9880d681SAndroid Build Coastguard Workerhowever. ARM, for example, defines their own format. Apple has their own compact
504*9880d681SAndroid Build Coastguard Workerunwind info format.  On Windows, another format is used for all architectures
505*9880d681SAndroid Build Coastguard Workersince 32-bit x86.  LLVM will emit whatever information is required by the
506*9880d681SAndroid Build Coastguard Workertarget.
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard WorkerException Tables
509*9880d681SAndroid Build Coastguard Worker----------------
510*9880d681SAndroid Build Coastguard Worker
511*9880d681SAndroid Build Coastguard WorkerAn exception table contains information about what actions to take when an
512*9880d681SAndroid Build Coastguard Workerexception is thrown in a particular part of a function's code. This is typically
513*9880d681SAndroid Build Coastguard Workerreferred to as the language-specific data area (LSDA). The format of the LSDA
514*9880d681SAndroid Build Coastguard Workertable is specific to the personality function, but the majority of personalities
515*9880d681SAndroid Build Coastguard Workerout there use a variation of the tables consumed by ``__gxx_personality_v0``.
516*9880d681SAndroid Build Coastguard WorkerThere is one exception table per function, except leaf functions and functions
517*9880d681SAndroid Build Coastguard Workerthat have calls only to non-throwing functions. They do not need an exception
518*9880d681SAndroid Build Coastguard Workertable.
519*9880d681SAndroid Build Coastguard Worker
520*9880d681SAndroid Build Coastguard Worker.. _wineh:
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard WorkerException Handling using the Windows Runtime
523*9880d681SAndroid Build Coastguard Worker=================================================
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard WorkerBackground on Windows exceptions
526*9880d681SAndroid Build Coastguard Worker---------------------------------
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard WorkerInteracting with exceptions on Windows is significantly more complicated than
529*9880d681SAndroid Build Coastguard Workeron Itanium C++ ABI platforms. The fundamental difference between the two models
530*9880d681SAndroid Build Coastguard Workeris that Itanium EH is designed around the idea of "successive unwinding," while
531*9880d681SAndroid Build Coastguard WorkerWindows EH is not.
532*9880d681SAndroid Build Coastguard Worker
533*9880d681SAndroid Build Coastguard WorkerUnder Itanium, throwing an exception typically involes allocating thread local
534*9880d681SAndroid Build Coastguard Workermemory to hold the exception, and calling into the EH runtime. The runtime
535*9880d681SAndroid Build Coastguard Workeridentifies frames with appropriate exception handling actions, and successively
536*9880d681SAndroid Build Coastguard Workerresets the register context of the current thread to the most recently active
537*9880d681SAndroid Build Coastguard Workerframe with actions to run. In LLVM, execution resumes at a ``landingpad``
538*9880d681SAndroid Build Coastguard Workerinstruction, which produces register values provided by the runtime. If a
539*9880d681SAndroid Build Coastguard Workerfunction is only cleaning up allocated resources, the function is responsible
540*9880d681SAndroid Build Coastguard Workerfor calling ``_Unwind_Resume`` to transition to the next most recently active
541*9880d681SAndroid Build Coastguard Workerframe after it is finished cleaning up. Eventually, the frame responsible for
542*9880d681SAndroid Build Coastguard Workerhandling the exception calls ``__cxa_end_catch`` to destroy the exception,
543*9880d681SAndroid Build Coastguard Workerrelease its memory, and resume normal control flow.
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard WorkerThe Windows EH model does not use these successive register context resets.
546*9880d681SAndroid Build Coastguard WorkerInstead, the active exception is typically described by a frame on the stack.
547*9880d681SAndroid Build Coastguard WorkerIn the case of C++ exceptions, the exception object is allocated in stack memory
548*9880d681SAndroid Build Coastguard Workerand its address is passed to ``__CxxThrowException``. General purpose structured
549*9880d681SAndroid Build Coastguard Workerexceptions (SEH) are more analogous to Linux signals, and they are dispatched by
550*9880d681SAndroid Build Coastguard Workeruserspace DLLs provided with Windows. Each frame on the stack has an assigned EH
551*9880d681SAndroid Build Coastguard Workerpersonality routine, which decides what actions to take to handle the exception.
552*9880d681SAndroid Build Coastguard WorkerThere are a few major personalities for C and C++ code: the C++ personality
553*9880d681SAndroid Build Coastguard Worker(``__CxxFrameHandler3``) and the SEH personalities (``_except_handler3``,
554*9880d681SAndroid Build Coastguard Worker``_except_handler4``, and ``__C_specific_handler``). All of them implement
555*9880d681SAndroid Build Coastguard Workercleanups by calling back into a "funclet" contained in the parent function.
556*9880d681SAndroid Build Coastguard Worker
557*9880d681SAndroid Build Coastguard WorkerFunclets, in this context, are regions of the parent function that can be called
558*9880d681SAndroid Build Coastguard Workeras though they were a function pointer with a very special calling convention.
559*9880d681SAndroid Build Coastguard WorkerThe frame pointer of the parent frame is passed into the funclet either using
560*9880d681SAndroid Build Coastguard Workerthe standard EBP register or as the first parameter register, depending on the
561*9880d681SAndroid Build Coastguard Workerarchitecture. The funclet implements the EH action by accessing local variables
562*9880d681SAndroid Build Coastguard Workerin memory through the frame pointer, and returning some appropriate value,
563*9880d681SAndroid Build Coastguard Workercontinuing the EH process.  No variables live in to or out of the funclet can be
564*9880d681SAndroid Build Coastguard Workerallocated in registers.
565*9880d681SAndroid Build Coastguard Worker
566*9880d681SAndroid Build Coastguard WorkerThe C++ personality also uses funclets to contain the code for catch blocks
567*9880d681SAndroid Build Coastguard Worker(i.e. all user code between the braces in ``catch (Type obj) { ... }``). The
568*9880d681SAndroid Build Coastguard Workerruntime must use funclets for catch bodies because the C++ exception object is
569*9880d681SAndroid Build Coastguard Workerallocated in a child stack frame of the function handling the exception. If the
570*9880d681SAndroid Build Coastguard Workerruntime rewound the stack back to frame of the catch, the memory holding the
571*9880d681SAndroid Build Coastguard Workerexception would be overwritten quickly by subsequent function calls.  The use of
572*9880d681SAndroid Build Coastguard Workerfunclets also allows ``__CxxFrameHandler3`` to implement rethrow without
573*9880d681SAndroid Build Coastguard Workerresorting to TLS. Instead, the runtime throws a special exception, and then uses
574*9880d681SAndroid Build Coastguard WorkerSEH (``__try / __except``) to resume execution with new information in the child
575*9880d681SAndroid Build Coastguard Workerframe.
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard WorkerIn other words, the successive unwinding approach is incompatible with Visual
578*9880d681SAndroid Build Coastguard WorkerC++ exceptions and general purpose Windows exception handling. Because the C++
579*9880d681SAndroid Build Coastguard Workerexception object lives in stack memory, LLVM cannot provide a custom personality
580*9880d681SAndroid Build Coastguard Workerfunction that uses landingpads.  Similarly, SEH does not provide any mechanism
581*9880d681SAndroid Build Coastguard Workerto rethrow an exception or continue unwinding.  Therefore, LLVM must use the IR
582*9880d681SAndroid Build Coastguard Workerconstructs described later in this document to implement compatible exception
583*9880d681SAndroid Build Coastguard Workerhandling.
584*9880d681SAndroid Build Coastguard Worker
585*9880d681SAndroid Build Coastguard WorkerSEH filter expressions
586*9880d681SAndroid Build Coastguard Worker-----------------------
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard WorkerThe SEH personality functions also use funclets to implement filter expressions,
589*9880d681SAndroid Build Coastguard Workerwhich allow executing arbitrary user code to decide which exceptions to catch.
590*9880d681SAndroid Build Coastguard WorkerFilter expressions should not be confused with the ``filter`` clause of the LLVM
591*9880d681SAndroid Build Coastguard Worker``landingpad`` instruction.  Typically filter expressions are used to determine
592*9880d681SAndroid Build Coastguard Workerif the exception came from a particular DLL or code region, or if code faulted
593*9880d681SAndroid Build Coastguard Workerwhile accessing a particular memory address range. LLVM does not currently have
594*9880d681SAndroid Build Coastguard WorkerIR to represent filter expressions because it is difficult to represent their
595*9880d681SAndroid Build Coastguard Workercontrol dependencies.  Filter expressions run during the first phase of EH,
596*9880d681SAndroid Build Coastguard Workerbefore cleanups run, making it very difficult to build a faithful control flow
597*9880d681SAndroid Build Coastguard Workergraph.  For now, the new EH instructions cannot represent SEH filter
598*9880d681SAndroid Build Coastguard Workerexpressions, and frontends must outline them ahead of time. Local variables of
599*9880d681SAndroid Build Coastguard Workerthe parent function can be escaped and accessed using the ``llvm.localescape``
600*9880d681SAndroid Build Coastguard Workerand ``llvm.localrecover`` intrinsics.
601*9880d681SAndroid Build Coastguard Worker
602*9880d681SAndroid Build Coastguard WorkerNew exception handling instructions
603*9880d681SAndroid Build Coastguard Worker------------------------------------
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard WorkerThe primary design goal of the new EH instructions is to support funclet
606*9880d681SAndroid Build Coastguard Workergeneration while preserving information about the CFG so that SSA formation
607*9880d681SAndroid Build Coastguard Workerstill works.  As a secondary goal, they are designed to be generic across MSVC
608*9880d681SAndroid Build Coastguard Workerand Itanium C++ exceptions. They make very few assumptions about the data
609*9880d681SAndroid Build Coastguard Workerrequired by the personality, so long as it uses the familiar core EH actions:
610*9880d681SAndroid Build Coastguard Workercatch, cleanup, and terminate.  However, the new instructions are hard to modify
611*9880d681SAndroid Build Coastguard Workerwithout knowing details of the EH personality. While they can be used to
612*9880d681SAndroid Build Coastguard Workerrepresent Itanium EH, the landingpad model is strictly better for optimization
613*9880d681SAndroid Build Coastguard Workerpurposes.
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard WorkerThe following new instructions are considered "exception handling pads", in that
616*9880d681SAndroid Build Coastguard Workerthey must be the first non-phi instruction of a basic block that may be the
617*9880d681SAndroid Build Coastguard Workerunwind destination of an EH flow edge:
618*9880d681SAndroid Build Coastguard Worker``catchswitch``, ``catchpad``, and ``cleanuppad``.
619*9880d681SAndroid Build Coastguard WorkerAs with landingpads, when entering a try scope, if the
620*9880d681SAndroid Build Coastguard Workerfrontend encounters a call site that may throw an exception, it should emit an
621*9880d681SAndroid Build Coastguard Workerinvoke that unwinds to a ``catchswitch`` block. Similarly, inside the scope of a
622*9880d681SAndroid Build Coastguard WorkerC++ object with a destructor, invokes should unwind to a ``cleanuppad``.
623*9880d681SAndroid Build Coastguard Worker
624*9880d681SAndroid Build Coastguard WorkerNew instructions are also used to mark the points where control is transferred
625*9880d681SAndroid Build Coastguard Workerout of a catch/cleanup handler (which will correspond to exits from the
626*9880d681SAndroid Build Coastguard Workergenerated funclet).  A catch handler which reaches its end by normal execution
627*9880d681SAndroid Build Coastguard Workerexecutes a ``catchret`` instruction, which is a terminator indicating where in
628*9880d681SAndroid Build Coastguard Workerthe function control is returned to.  A cleanup handler which reaches its end
629*9880d681SAndroid Build Coastguard Workerby normal execution executes a ``cleanupret`` instruction, which is a terminator
630*9880d681SAndroid Build Coastguard Workerindicating where the active exception will unwind to next.
631*9880d681SAndroid Build Coastguard Worker
632*9880d681SAndroid Build Coastguard WorkerEach of these new EH pad instructions has a way to identify which action should
633*9880d681SAndroid Build Coastguard Workerbe considered after this action. The ``catchswitch`` instruction is a terminator
634*9880d681SAndroid Build Coastguard Workerand has an unwind destination operand analogous to the unwind destination of an
635*9880d681SAndroid Build Coastguard Workerinvoke.  The ``cleanuppad`` instruction is not
636*9880d681SAndroid Build Coastguard Workera terminator, so the unwind destination is stored on the ``cleanupret``
637*9880d681SAndroid Build Coastguard Workerinstruction instead. Successfully executing a catch handler should resume
638*9880d681SAndroid Build Coastguard Workernormal control flow, so neither ``catchpad`` nor ``catchret`` instructions can
639*9880d681SAndroid Build Coastguard Workerunwind. All of these "unwind edges" may refer to a basic block that contains an
640*9880d681SAndroid Build Coastguard WorkerEH pad instruction, or they may unwind to the caller.  Unwinding to the caller
641*9880d681SAndroid Build Coastguard Workerhas roughly the same semantics as the ``resume`` instruction in the landingpad
642*9880d681SAndroid Build Coastguard Workermodel. When inlining through an invoke, instructions that unwind to the caller
643*9880d681SAndroid Build Coastguard Workerare hooked up to unwind to the unwind destination of the call site.
644*9880d681SAndroid Build Coastguard Worker
645*9880d681SAndroid Build Coastguard WorkerPutting things together, here is a hypothetical lowering of some C++ that uses
646*9880d681SAndroid Build Coastguard Workerall of the new IR instructions:
647*9880d681SAndroid Build Coastguard Worker
648*9880d681SAndroid Build Coastguard Worker.. code-block:: c
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker  struct Cleanup {
651*9880d681SAndroid Build Coastguard Worker    Cleanup();
652*9880d681SAndroid Build Coastguard Worker    ~Cleanup();
653*9880d681SAndroid Build Coastguard Worker    int m;
654*9880d681SAndroid Build Coastguard Worker  };
655*9880d681SAndroid Build Coastguard Worker  void may_throw();
656*9880d681SAndroid Build Coastguard Worker  int f() noexcept {
657*9880d681SAndroid Build Coastguard Worker    try {
658*9880d681SAndroid Build Coastguard Worker      Cleanup obj;
659*9880d681SAndroid Build Coastguard Worker      may_throw();
660*9880d681SAndroid Build Coastguard Worker    } catch (int e) {
661*9880d681SAndroid Build Coastguard Worker      may_throw();
662*9880d681SAndroid Build Coastguard Worker      return e;
663*9880d681SAndroid Build Coastguard Worker    }
664*9880d681SAndroid Build Coastguard Worker    return 0;
665*9880d681SAndroid Build Coastguard Worker  }
666*9880d681SAndroid Build Coastguard Worker
667*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker  define i32 @f() nounwind personality i32 (...)* @__CxxFrameHandler3 {
670*9880d681SAndroid Build Coastguard Worker  entry:
671*9880d681SAndroid Build Coastguard Worker    %obj = alloca %struct.Cleanup, align 4
672*9880d681SAndroid Build Coastguard Worker    %e = alloca i32, align 4
673*9880d681SAndroid Build Coastguard Worker    %call = invoke %struct.Cleanup* @"\01??0Cleanup@@QEAA@XZ"(%struct.Cleanup* nonnull %obj)
674*9880d681SAndroid Build Coastguard Worker            to label %invoke.cont unwind label %lpad.catch
675*9880d681SAndroid Build Coastguard Worker
676*9880d681SAndroid Build Coastguard Worker  invoke.cont:                                      ; preds = %entry
677*9880d681SAndroid Build Coastguard Worker    invoke void @"\01?may_throw@@YAXXZ"()
678*9880d681SAndroid Build Coastguard Worker            to label %invoke.cont.2 unwind label %lpad.cleanup
679*9880d681SAndroid Build Coastguard Worker
680*9880d681SAndroid Build Coastguard Worker  invoke.cont.2:                                    ; preds = %invoke.cont
681*9880d681SAndroid Build Coastguard Worker    call void @"\01??_DCleanup@@QEAA@XZ"(%struct.Cleanup* nonnull %obj) nounwind
682*9880d681SAndroid Build Coastguard Worker    br label %return
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker  return:                                           ; preds = %invoke.cont.3, %invoke.cont.2
685*9880d681SAndroid Build Coastguard Worker    %retval.0 = phi i32 [ 0, %invoke.cont.2 ], [ %3, %invoke.cont.3 ]
686*9880d681SAndroid Build Coastguard Worker    ret i32 %retval.0
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker  lpad.cleanup:                                     ; preds = %invoke.cont.2
689*9880d681SAndroid Build Coastguard Worker    %0 = cleanuppad within none []
690*9880d681SAndroid Build Coastguard Worker    call void @"\01??1Cleanup@@QEAA@XZ"(%struct.Cleanup* nonnull %obj) nounwind
691*9880d681SAndroid Build Coastguard Worker    cleanupret %0 unwind label %lpad.catch
692*9880d681SAndroid Build Coastguard Worker
693*9880d681SAndroid Build Coastguard Worker  lpad.catch:                                       ; preds = %lpad.cleanup, %entry
694*9880d681SAndroid Build Coastguard Worker    %1 = catchswitch within none [label %catch.body] unwind label %lpad.terminate
695*9880d681SAndroid Build Coastguard Worker
696*9880d681SAndroid Build Coastguard Worker  catch.body:                                       ; preds = %lpad.catch
697*9880d681SAndroid Build Coastguard Worker    %catch = catchpad within %1 [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i32* %e]
698*9880d681SAndroid Build Coastguard Worker    invoke void @"\01?may_throw@@YAXXZ"()
699*9880d681SAndroid Build Coastguard Worker            to label %invoke.cont.3 unwind label %lpad.terminate
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker  invoke.cont.3:                                    ; preds = %catch.body
702*9880d681SAndroid Build Coastguard Worker    %3 = load i32, i32* %e, align 4
703*9880d681SAndroid Build Coastguard Worker    catchret from %catch to label %return
704*9880d681SAndroid Build Coastguard Worker
705*9880d681SAndroid Build Coastguard Worker  lpad.terminate:                                   ; preds = %catch.body, %lpad.catch
706*9880d681SAndroid Build Coastguard Worker    cleanuppad within none []
707*9880d681SAndroid Build Coastguard Worker    call void @"\01?terminate@@YAXXZ"
708*9880d681SAndroid Build Coastguard Worker    unreachable
709*9880d681SAndroid Build Coastguard Worker  }
710*9880d681SAndroid Build Coastguard Worker
711*9880d681SAndroid Build Coastguard WorkerFunclet parent tokens
712*9880d681SAndroid Build Coastguard Worker-----------------------
713*9880d681SAndroid Build Coastguard Worker
714*9880d681SAndroid Build Coastguard WorkerIn order to produce tables for EH personalities that use funclets, it is
715*9880d681SAndroid Build Coastguard Workernecessary to recover the nesting that was present in the source. This funclet
716*9880d681SAndroid Build Coastguard Workerparent relationship is encoded in the IR using tokens produced by the new "pad"
717*9880d681SAndroid Build Coastguard Workerinstructions. The token operand of a "pad" or "ret" instruction indicates which
718*9880d681SAndroid Build Coastguard Workerfunclet it is in, or "none" if it is not nested within another funclet.
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard WorkerThe ``catchpad`` and ``cleanuppad`` instructions establish new funclets, and
721*9880d681SAndroid Build Coastguard Workertheir tokens are consumed by other "pad" instructions to establish membership.
722*9880d681SAndroid Build Coastguard WorkerThe ``catchswitch`` instruction does not create a funclet, but it produces a
723*9880d681SAndroid Build Coastguard Workertoken that is always consumed by its immediate successor ``catchpad``
724*9880d681SAndroid Build Coastguard Workerinstructions. This ensures that every catch handler modelled by a ``catchpad``
725*9880d681SAndroid Build Coastguard Workerbelongs to exactly one ``catchswitch``, which models the dispatch point after a
726*9880d681SAndroid Build Coastguard WorkerC++ try.
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard WorkerHere is an example of what this nesting looks like using some hypothetical
729*9880d681SAndroid Build Coastguard WorkerC++ code:
730*9880d681SAndroid Build Coastguard Worker
731*9880d681SAndroid Build Coastguard Worker.. code-block:: c
732*9880d681SAndroid Build Coastguard Worker
733*9880d681SAndroid Build Coastguard Worker  void f() {
734*9880d681SAndroid Build Coastguard Worker    try {
735*9880d681SAndroid Build Coastguard Worker      throw;
736*9880d681SAndroid Build Coastguard Worker    } catch (...) {
737*9880d681SAndroid Build Coastguard Worker      try {
738*9880d681SAndroid Build Coastguard Worker        throw;
739*9880d681SAndroid Build Coastguard Worker      } catch (...) {
740*9880d681SAndroid Build Coastguard Worker      }
741*9880d681SAndroid Build Coastguard Worker    }
742*9880d681SAndroid Build Coastguard Worker  }
743*9880d681SAndroid Build Coastguard Worker
744*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker  define void @f() #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
747*9880d681SAndroid Build Coastguard Worker  entry:
748*9880d681SAndroid Build Coastguard Worker    invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #1
749*9880d681SAndroid Build Coastguard Worker            to label %unreachable unwind label %catch.dispatch
750*9880d681SAndroid Build Coastguard Worker
751*9880d681SAndroid Build Coastguard Worker  catch.dispatch:                                   ; preds = %entry
752*9880d681SAndroid Build Coastguard Worker    %0 = catchswitch within none [label %catch] unwind to caller
753*9880d681SAndroid Build Coastguard Worker
754*9880d681SAndroid Build Coastguard Worker  catch:                                            ; preds = %catch.dispatch
755*9880d681SAndroid Build Coastguard Worker    %1 = catchpad within %0 [i8* null, i32 64, i8* null]
756*9880d681SAndroid Build Coastguard Worker    invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #1
757*9880d681SAndroid Build Coastguard Worker            to label %unreachable unwind label %catch.dispatch2
758*9880d681SAndroid Build Coastguard Worker
759*9880d681SAndroid Build Coastguard Worker  catch.dispatch2:                                  ; preds = %catch
760*9880d681SAndroid Build Coastguard Worker    %2 = catchswitch within %1 [label %catch3] unwind to caller
761*9880d681SAndroid Build Coastguard Worker
762*9880d681SAndroid Build Coastguard Worker  catch3:                                           ; preds = %catch.dispatch2
763*9880d681SAndroid Build Coastguard Worker    %3 = catchpad within %2 [i8* null, i32 64, i8* null]
764*9880d681SAndroid Build Coastguard Worker    catchret from %3 to label %try.cont
765*9880d681SAndroid Build Coastguard Worker
766*9880d681SAndroid Build Coastguard Worker  try.cont:                                         ; preds = %catch3
767*9880d681SAndroid Build Coastguard Worker    catchret from %1 to label %try.cont6
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker  try.cont6:                                        ; preds = %try.cont
770*9880d681SAndroid Build Coastguard Worker    ret void
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker  unreachable:                                      ; preds = %catch, %entry
773*9880d681SAndroid Build Coastguard Worker    unreachable
774*9880d681SAndroid Build Coastguard Worker  }
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard WorkerThe "inner" ``catchswitch`` consumes ``%1`` which is produced by the outer
777*9880d681SAndroid Build Coastguard Workercatchswitch.
778*9880d681SAndroid Build Coastguard Worker
779*9880d681SAndroid Build Coastguard Worker.. _wineh-constraints:
780*9880d681SAndroid Build Coastguard Worker
781*9880d681SAndroid Build Coastguard WorkerFunclet transitions
782*9880d681SAndroid Build Coastguard Worker-----------------------
783*9880d681SAndroid Build Coastguard Worker
784*9880d681SAndroid Build Coastguard WorkerThe EH tables for personalities that use funclets make implicit use of the
785*9880d681SAndroid Build Coastguard Workerfunclet nesting relationship to encode unwind destinations, and so are
786*9880d681SAndroid Build Coastguard Workerconstrained in the set of funclet transitions they can represent.  The related
787*9880d681SAndroid Build Coastguard WorkerLLVM IR instructions accordingly have constraints that ensure encodability of
788*9880d681SAndroid Build Coastguard Workerthe EH edges in the flow graph.
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard WorkerA ``catchswitch``, ``catchpad``, or ``cleanuppad`` is said to be "entered"
791*9880d681SAndroid Build Coastguard Workerwhen it executes.  It may subsequently be "exited" by any of the following
792*9880d681SAndroid Build Coastguard Workermeans:
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker* A ``catchswitch`` is immediately exited when none of its constituent
795*9880d681SAndroid Build Coastguard Worker  ``catchpad``\ s are appropriate for the in-flight exception and it unwinds
796*9880d681SAndroid Build Coastguard Worker  to its unwind destination or the caller.
797*9880d681SAndroid Build Coastguard Worker* A ``catchpad`` and its parent ``catchswitch`` are both exited when a
798*9880d681SAndroid Build Coastguard Worker  ``catchret`` from the ``catchpad`` is executed.
799*9880d681SAndroid Build Coastguard Worker* A ``cleanuppad`` is exited when a ``cleanupret`` from it is executed.
800*9880d681SAndroid Build Coastguard Worker* Any of these pads is exited when control unwinds to the function's caller,
801*9880d681SAndroid Build Coastguard Worker  either by a ``call`` which unwinds all the way to the function's caller,
802*9880d681SAndroid Build Coastguard Worker  a nested ``catchswitch`` marked "``unwinds to caller``", or a nested
803*9880d681SAndroid Build Coastguard Worker  ``cleanuppad``\ 's ``cleanupret`` marked "``unwinds to caller"``.
804*9880d681SAndroid Build Coastguard Worker* Any of these pads is exited when an unwind edge (from an ``invoke``,
805*9880d681SAndroid Build Coastguard Worker  nested ``catchswitch``, or nested ``cleanuppad``\ 's ``cleanupret``)
806*9880d681SAndroid Build Coastguard Worker  unwinds to a destination pad that is not a descendant of the given pad.
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard WorkerNote that the ``ret`` instruction is *not* a valid way to exit a funclet pad;
809*9880d681SAndroid Build Coastguard Workerit is undefined behavior to execute a ``ret`` when a pad has been entered but
810*9880d681SAndroid Build Coastguard Workernot exited.
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard WorkerA single unwind edge may exit any number of pads (with the restrictions that
813*9880d681SAndroid Build Coastguard Workerthe edge from a ``catchswitch`` must exit at least itself, and the edge from
814*9880d681SAndroid Build Coastguard Workera ``cleanupret`` must exit at least its ``cleanuppad``), and then must enter
815*9880d681SAndroid Build Coastguard Workerexactly one pad, which must be distinct from all the exited pads.  The parent
816*9880d681SAndroid Build Coastguard Workerof the pad that an unwind edge enters must be the most-recently-entered
817*9880d681SAndroid Build Coastguard Workernot-yet-exited pad (after exiting from any pads that the unwind edge exits),
818*9880d681SAndroid Build Coastguard Workeror "none" if there is no such pad.  This ensures that the stack of executing
819*9880d681SAndroid Build Coastguard Workerfunclets at run-time always corresponds to some path in the funclet pad tree
820*9880d681SAndroid Build Coastguard Workerthat the parent tokens encode.
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard WorkerAll unwind edges which exit any given funclet pad (including ``cleanupret``
823*9880d681SAndroid Build Coastguard Workeredges exiting their ``cleanuppad`` and ``catchswitch`` edges exiting their
824*9880d681SAndroid Build Coastguard Worker``catchswitch``) must share the same unwind destination.  Similarly, any
825*9880d681SAndroid Build Coastguard Workerfunclet pad which may be exited by unwind to caller must not be exited by
826*9880d681SAndroid Build Coastguard Workerany exception edges which unwind anywhere other than the caller.  This
827*9880d681SAndroid Build Coastguard Workerensures that each funclet as a whole has only one unwind destination, which
828*9880d681SAndroid Build Coastguard WorkerEH tables for funclet personalities may require.  Note that any unwind edge
829*9880d681SAndroid Build Coastguard Workerwhich exits a ``catchpad`` also exits its parent ``catchswitch``, so this
830*9880d681SAndroid Build Coastguard Workerimplies that for any given ``catchswitch``, its unwind destination must also
831*9880d681SAndroid Build Coastguard Workerbe the unwind destination of any unwind edge that exits any of its constituent
832*9880d681SAndroid Build Coastguard Worker``catchpad``\s.  Because ``catchswitch`` has no ``nounwind`` variant, and
833*9880d681SAndroid Build Coastguard Workerbecause IR producers are not *required* to annotate calls which will not
834*9880d681SAndroid Build Coastguard Workerunwind as ``nounwind``, it is legal to nest a ``call`` or an "``unwind to
835*9880d681SAndroid Build Coastguard Workercaller``\ " ``catchswitch`` within a funclet pad that has an unwind
836*9880d681SAndroid Build Coastguard Workerdestination other than caller; it is undefined behavior for such a ``call``
837*9880d681SAndroid Build Coastguard Workeror ``catchswitch`` to unwind.
838*9880d681SAndroid Build Coastguard Worker
839*9880d681SAndroid Build Coastguard WorkerFinally, the funclet pads' unwind destinations cannot form a cycle.  This
840*9880d681SAndroid Build Coastguard Workerensures that EH lowering can construct "try regions" with a tree-like
841*9880d681SAndroid Build Coastguard Workerstructure, which funclet-based personalities may require.
842