LCOV - code coverage report
Current view: top level - gcc - except.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 71.4 % 1422 1015
Test Date: 2026-10-17 16:30:31 Functions: 85.1 % 94 80
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Implements exception handling.
       2              :    Copyright (C) 1989-2026 Free Software Foundation, Inc.
       3              :    Contributed by Mike Stump <mrs@cygnus.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : 
      22              : /* An exception is an event that can be "thrown" from within a
      23              :    function.  This event can then be "caught" by the callers of
      24              :    the function.
      25              : 
      26              :    The representation of exceptions changes several times during
      27              :    the compilation process:
      28              : 
      29              :    In the beginning, in the front end, we have the GENERIC trees
      30              :    TRY_CATCH_EXPR, TRY_FINALLY_EXPR, EH_ELSE_EXPR, WITH_CLEANUP_EXPR,
      31              :    CLEANUP_POINT_EXPR, CATCH_EXPR, and EH_FILTER_EXPR.
      32              : 
      33              :    During initial gimplification (gimplify.cc) these are lowered to the
      34              :    GIMPLE_TRY, GIMPLE_CATCH, GIMPLE_EH_ELSE, and GIMPLE_EH_FILTER
      35              :    nodes.  The WITH_CLEANUP_EXPR and CLEANUP_POINT_EXPR nodes are
      36              :    converted into GIMPLE_TRY_FINALLY nodes; the others are a more
      37              :    direct 1-1 conversion.
      38              : 
      39              :    During pass_lower_eh (tree-eh.cc) we record the nested structure
      40              :    of the TRY nodes in EH_REGION nodes in CFUN->EH->REGION_TREE.
      41              :    We expand the eh_protect_cleanup_actions langhook into MUST_NOT_THROW
      42              :    regions at this time.  We can then flatten the statements within
      43              :    the TRY nodes to straight-line code.  Statements that had been within
      44              :    TRY nodes that can throw are recorded within CFUN->EH->THROW_STMT_TABLE,
      45              :    so that we may remember what action is supposed to be taken if
      46              :    a given statement does throw.  During this lowering process,
      47              :    we create an EH_LANDING_PAD node for each EH_REGION that has
      48              :    some code within the function that needs to be executed if a
      49              :    throw does happen.  We also create RESX statements that are
      50              :    used to transfer control from an inner EH_REGION to an outer
      51              :    EH_REGION.  We also create EH_DISPATCH statements as placeholders
      52              :    for a runtime type comparison that should be made in order to
      53              :    select the action to perform among different CATCH and EH_FILTER
      54              :    regions.
      55              : 
      56              :    During pass_lower_eh_dispatch (tree-eh.cc), which is run after
      57              :    all inlining is complete, we are able to run assign_filter_values,
      58              :    which allows us to map the set of types manipulated by all of the
      59              :    CATCH and EH_FILTER regions to a set of integers.  This set of integers
      60              :    will be how the exception runtime communicates with the code generated
      61              :    within the function.  We then expand the GIMPLE_EH_DISPATCH statements
      62              :    to a switch or conditional branches that use the argument provided by
      63              :    the runtime (__builtin_eh_filter) and the set of integers we computed
      64              :    in assign_filter_values.
      65              : 
      66              :    During pass_lower_resx (tree-eh.cc), which is run near the end
      67              :    of optimization, we expand RESX statements.  If the eh region
      68              :    that is outer to the RESX statement is a MUST_NOT_THROW, then
      69              :    the RESX expands to some form of abort statement.  If the eh
      70              :    region that is outer to the RESX statement is within the current
      71              :    function, then the RESX expands to a bookkeeping call
      72              :    (__builtin_eh_copy_values) and a goto.  Otherwise, the next
      73              :    handler for the exception must be within a function somewhere
      74              :    up the call chain, so we call back into the exception runtime
      75              :    (__builtin_unwind_resume).
      76              : 
      77              :    During pass_expand (cfgexpand.cc), we generate REG_EH_REGION notes
      78              :    that create an rtl to eh_region mapping that corresponds to the
      79              :    gimple to eh_region mapping that had been recorded in the
      80              :    THROW_STMT_TABLE.
      81              : 
      82              :    Then, via finish_eh_generation, we generate the real landing pads
      83              :    to which the runtime will actually transfer control.  These new
      84              :    landing pads perform whatever bookkeeping is needed by the target
      85              :    backend in order to resume execution within the current function.
      86              :    Each of these new landing pads falls through into the post_landing_pad
      87              :    label which had been used within the CFG up to this point.  All
      88              :    exception edges within the CFG are redirected to the new landing pads.
      89              :    If the target uses setjmp to implement exceptions, the various extra
      90              :    calls into the runtime to register and unregister the current stack
      91              :    frame are emitted at this time.
      92              : 
      93              :    During pass_convert_to_eh_region_ranges (except.cc), we transform
      94              :    the REG_EH_REGION notes attached to individual insns into
      95              :    non-overlapping ranges of insns bounded by NOTE_INSN_EH_REGION_BEG
      96              :    and NOTE_INSN_EH_REGION_END.  Each insn within such ranges has the
      97              :    same associated action within the exception region tree, meaning
      98              :    that (1) the exception is caught by the same landing pad within the
      99              :    current function, (2) the exception is blocked by the runtime with
     100              :    a MUST_NOT_THROW region, or (3) the exception is not handled at all
     101              :    within the current function.
     102              : 
     103              :    Finally, during assembly generation, we call
     104              :    output_function_exception_table (except.cc) to emit the tables with
     105              :    which the exception runtime can determine if a given stack frame
     106              :    handles a given exception, and if so what filter value to provide
     107              :    to the function when the non-local control transfer is effected.
     108              :    If the target uses dwarf2 unwinding to implement exceptions, then
     109              :    output_call_frame_info (dwarf2out.cc) emits the required unwind data.  */
     110              : 
     111              : 
     112              : #include "config.h"
     113              : #include "system.h"
     114              : #include "coretypes.h"
     115              : #include "backend.h"
     116              : #include "target.h"
     117              : #include "rtl.h"
     118              : #include "tree.h"
     119              : #include "cfghooks.h"
     120              : #include "tree-pass.h"
     121              : #include "memmodel.h"
     122              : #include "tm_p.h"
     123              : #include "stringpool.h"
     124              : #include "expmed.h"
     125              : #include "optabs.h"
     126              : #include "emit-rtl.h"
     127              : #include "cgraph.h"
     128              : #include "diagnostic.h"
     129              : #include "fold-const.h"
     130              : #include "stor-layout.h"
     131              : #include "explow.h"
     132              : #include "stmt.h"
     133              : #include "expr.h"
     134              : #include "calls.h"
     135              : #include "libfuncs.h"
     136              : #include "except.h"
     137              : #include "output.h"
     138              : #include "dwarf2asm.h"
     139              : #include "dwarf2.h"
     140              : #include "common/common-target.h"
     141              : #include "langhooks.h"
     142              : #include "cfgrtl.h"
     143              : #include "tree-pretty-print.h"
     144              : #include "cfgloop.h"
     145              : #include "builtins.h"
     146              : #include "tree-hash-traits.h"
     147              : #include "flags.h"
     148              : 
     149              : static GTY(()) int call_site_base;
     150              : 
     151              : static GTY(()) hash_map<tree_hash, tree> *type_to_runtime_map;
     152              : 
     153              : static GTY(()) tree setjmp_fn;
     154              : 
     155              : /* Describe the SjLj_Function_Context structure.  */
     156              : static GTY(()) tree sjlj_fc_type_node;
     157              : static int sjlj_fc_call_site_ofs;
     158              : static int sjlj_fc_data_ofs;
     159              : static int sjlj_fc_personality_ofs;
     160              : static int sjlj_fc_lsda_ofs;
     161              : static int sjlj_fc_jbuf_ofs;
     162              : 
     163              : 
     164              : struct GTY(()) call_site_record_d
     165              : {
     166              :   rtx landing_pad;
     167              :   int action;
     168              : };
     169              : 
     170              : /* In the following structure and associated functions,
     171              :    we represent entries in the action table as 1-based indices.
     172              :    Special cases are:
     173              : 
     174              :          0:     null action record, non-null landing pad; implies cleanups
     175              :         -1:     null action record, null landing pad; implies no action
     176              :         -2:     no call-site entry; implies must_not_throw
     177              :         -3:     we have yet to process outer regions
     178              : 
     179              :    Further, no special cases apply to the "next" field of the record.
     180              :    For next, 0 means end of list.  */
     181              : 
     182              : struct action_record
     183              : {
     184              :   int offset;
     185              :   int filter;
     186              :   int next;
     187              : };
     188              : 
     189              : /* Hashtable helpers.  */
     190              : 
     191              : struct action_record_hasher : free_ptr_hash <action_record>
     192              : {
     193              :   static inline hashval_t hash (const action_record *);
     194              :   static inline bool equal (const action_record *, const action_record *);
     195              : };
     196              : 
     197              : inline hashval_t
     198       745389 : action_record_hasher::hash (const action_record *entry)
     199              : {
     200       745389 :   return entry->next * 1009 + entry->filter;
     201              : }
     202              : 
     203              : inline bool
     204       391631 : action_record_hasher::equal (const action_record *entry,
     205              :                              const action_record *data)
     206              : {
     207       391631 :   return entry->filter == data->filter && entry->next == data->next;
     208              : }
     209              : 
     210              : typedef hash_table<action_record_hasher> action_hash_type;
     211              : 
     212              : static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
     213              :                                            eh_landing_pad *);
     214              : 
     215              : static void dw2_build_landing_pads (void);
     216              : 
     217              : static int collect_one_action_chain (action_hash_type *, eh_region);
     218              : static int add_call_site (rtx, int, int);
     219              : 
     220              : static void push_uleb128 (vec<uchar, va_gc> **, unsigned int);
     221              : static void push_sleb128 (vec<uchar, va_gc> **, int);
     222              : static int dw2_size_of_call_site_table (int);
     223              : static int sjlj_size_of_call_site_table (void);
     224              : static void dw2_output_call_site_table (int, int);
     225              : static void sjlj_output_call_site_table (void);
     226              : 
     227              : 
     228              : void
     229       306207 : init_eh (void)
     230              : {
     231       306207 :   if (! flag_exceptions)
     232              :     return;
     233              : 
     234       144192 :   type_to_runtime_map = hash_map<tree_hash, tree>::create_ggc (31);
     235              : 
     236              :   /* Create the SjLj_Function_Context structure.  This should match
     237              :      the definition in unwind-sjlj.c.  */
     238       144192 :   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
     239              :     {
     240            0 :       tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
     241              : 
     242            0 :       sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
     243              : 
     244            0 :       f_prev = build_decl (BUILTINS_LOCATION,
     245              :                            FIELD_DECL, get_identifier ("__prev"),
     246              :                            build_pointer_type (sjlj_fc_type_node));
     247            0 :       DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
     248              : 
     249            0 :       f_cs = build_decl (BUILTINS_LOCATION,
     250              :                          FIELD_DECL, get_identifier ("__call_site"),
     251              :                          integer_type_node);
     252            0 :       DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
     253              : 
     254            0 :       tmp = build_index_type (size_int (4 - 1));
     255            0 :       tmp = build_array_type (lang_hooks.types.type_for_mode
     256            0 :                                 (targetm.unwind_word_mode (), 1),
     257              :                               tmp);
     258            0 :       f_data = build_decl (BUILTINS_LOCATION,
     259              :                            FIELD_DECL, get_identifier ("__data"), tmp);
     260            0 :       DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
     261              : 
     262            0 :       f_per = build_decl (BUILTINS_LOCATION,
     263              :                           FIELD_DECL, get_identifier ("__personality"),
     264              :                           ptr_type_node);
     265            0 :       DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
     266              : 
     267            0 :       f_lsda = build_decl (BUILTINS_LOCATION,
     268              :                            FIELD_DECL, get_identifier ("__lsda"),
     269              :                            ptr_type_node);
     270            0 :       DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
     271              : 
     272              : #ifdef DONT_USE_BUILTIN_SETJMP
     273              : #ifdef JMP_BUF_SIZE
     274              :       tmp = size_int (JMP_BUF_SIZE - 1);
     275              : #else
     276              :       /* Should be large enough for most systems, if it is not,
     277              :          JMP_BUF_SIZE should be defined with the proper value.  It will
     278              :          also tend to be larger than necessary for most systems, a more
     279              :          optimal port will define JMP_BUF_SIZE.  */
     280              :       tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
     281              : #endif
     282              : #else
     283              :       /* Compute a minimally sized jump buffer.  We need room to store at
     284              :          least 3 pointers - stack pointer, frame pointer and return address.
     285              :          Plus for some targets we need room for an extra pointer - in the
     286              :          case of MIPS this is the global pointer.  This makes a total of four
     287              :          pointers, but to be safe we actually allocate room for 5.
     288              : 
     289              :          If pointers are smaller than words then we allocate enough room for
     290              :          5 words, just in case the backend needs this much room.  For more
     291              :          discussion on this issue see:
     292              :          http://gcc.gnu.org/ml/gcc-patches/2014-05/msg00313.html.  */
     293            0 :       if (POINTER_SIZE > BITS_PER_WORD)
     294            0 :         tmp = size_int (5 - 1);
     295              :       else
     296            0 :         tmp = size_int ((5 * BITS_PER_WORD / POINTER_SIZE) - 1);
     297              : #endif
     298              : 
     299            0 :       tmp = build_index_type (tmp);
     300            0 :       tmp = build_array_type (ptr_type_node, tmp);
     301            0 :       f_jbuf = build_decl (BUILTINS_LOCATION,
     302              :                            FIELD_DECL, get_identifier ("__jbuf"), tmp);
     303              : #ifdef DONT_USE_BUILTIN_SETJMP
     304              :       /* We don't know what the alignment requirements of the
     305              :          runtime's jmp_buf has.  Overestimate.  */
     306              :       SET_DECL_ALIGN (f_jbuf, BIGGEST_ALIGNMENT);
     307              :       DECL_USER_ALIGN (f_jbuf) = 1;
     308              : #endif
     309            0 :       DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
     310              : 
     311            0 :       TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
     312            0 :       TREE_CHAIN (f_prev) = f_cs;
     313            0 :       TREE_CHAIN (f_cs) = f_data;
     314            0 :       TREE_CHAIN (f_data) = f_per;
     315            0 :       TREE_CHAIN (f_per) = f_lsda;
     316            0 :       TREE_CHAIN (f_lsda) = f_jbuf;
     317              : 
     318            0 :       layout_type (sjlj_fc_type_node);
     319              : 
     320              :       /* Cache the interesting field offsets so that we have
     321              :          easy access from rtl.  */
     322            0 :       sjlj_fc_call_site_ofs
     323            0 :         = (tree_to_uhwi (DECL_FIELD_OFFSET (f_cs))
     324            0 :            + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_cs)) / BITS_PER_UNIT);
     325            0 :       sjlj_fc_data_ofs
     326            0 :         = (tree_to_uhwi (DECL_FIELD_OFFSET (f_data))
     327            0 :            + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_data)) / BITS_PER_UNIT);
     328            0 :       sjlj_fc_personality_ofs
     329            0 :         = (tree_to_uhwi (DECL_FIELD_OFFSET (f_per))
     330            0 :            + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_per)) / BITS_PER_UNIT);
     331            0 :       sjlj_fc_lsda_ofs
     332            0 :         = (tree_to_uhwi (DECL_FIELD_OFFSET (f_lsda))
     333            0 :            + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_lsda)) / BITS_PER_UNIT);
     334            0 :       sjlj_fc_jbuf_ofs
     335            0 :         = (tree_to_uhwi (DECL_FIELD_OFFSET (f_jbuf))
     336            0 :            + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_jbuf)) / BITS_PER_UNIT);
     337              : 
     338              : #ifdef DONT_USE_BUILTIN_SETJMP
     339              :       tmp = build_function_type_list (integer_type_node, TREE_TYPE (f_jbuf),
     340              :                                       NULL);
     341              :       setjmp_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
     342              :                               get_identifier ("setjmp"), tmp);
     343              :       TREE_PUBLIC (setjmp_fn) = 1;
     344              :       DECL_EXTERNAL (setjmp_fn) = 1;
     345              :       DECL_ASSEMBLER_NAME (setjmp_fn);
     346              : #endif
     347              :     }
     348              : }
     349              : 
     350              : void
     351    208861897 : init_eh_for_function (void)
     352              : {
     353    208861897 :   cfun->eh = ggc_cleared_alloc<eh_status> ();
     354              : 
     355              :   /* Make sure zero'th entries are used.  */
     356    208861897 :   vec_safe_push (cfun->eh->region_array, (eh_region)0);
     357    208861897 :   vec_safe_push (cfun->eh->lp_array, (eh_landing_pad)0);
     358    208861897 : }
     359              : 
     360              : /* Routines to generate the exception tree somewhat directly.
     361              :    These are used from tree-eh.cc when processing exception related
     362              :    nodes during tree optimization.  */
     363              : 
     364              : static eh_region
     365      3046971 : gen_eh_region (enum eh_region_type type, eh_region outer)
     366              : {
     367      3046971 :   eh_region new_eh;
     368              : 
     369              :   /* Insert a new blank region as a leaf in the tree.  */
     370      3046971 :   new_eh = ggc_cleared_alloc<eh_region_d> ();
     371      3046971 :   new_eh->type = type;
     372      3046971 :   new_eh->outer = outer;
     373      3046971 :   if (outer)
     374              :     {
     375      1358001 :       new_eh->next_peer = outer->inner;
     376      1358001 :       outer->inner = new_eh;
     377              :     }
     378              :   else
     379              :     {
     380      1688970 :       new_eh->next_peer = cfun->eh->region_tree;
     381      1688970 :       cfun->eh->region_tree = new_eh;
     382              :     }
     383              : 
     384      3046971 :   new_eh->index = vec_safe_length (cfun->eh->region_array);
     385      3046971 :   vec_safe_push (cfun->eh->region_array, new_eh);
     386              : 
     387              :   /* Copy the language's notion of whether to use __cxa_end_cleanup.  */
     388      3046971 :   if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
     389            0 :     new_eh->use_cxa_end_cleanup = true;
     390              : 
     391      3046971 :   return new_eh;
     392              : }
     393              : 
     394              : eh_region
     395      1341346 : gen_eh_region_cleanup (eh_region outer)
     396              : {
     397      1341346 :   return gen_eh_region (ERT_CLEANUP, outer);
     398              : }
     399              : 
     400              : eh_region
     401        47911 : gen_eh_region_try (eh_region outer)
     402              : {
     403        47911 :   return gen_eh_region (ERT_TRY, outer);
     404              : }
     405              : 
     406              : eh_catch
     407        57449 : gen_eh_region_catch (eh_region t, tree type_or_list)
     408              : {
     409        57449 :   eh_catch c, l;
     410        57449 :   tree type_list, type_node;
     411              : 
     412        57449 :   gcc_assert (t->type == ERT_TRY);
     413              : 
     414              :   /* Ensure to always end up with a type list to normalize further
     415              :      processing, then register each type against the runtime types map.  */
     416        57449 :   type_list = type_or_list;
     417        57449 :   if (type_or_list)
     418              :     {
     419         8502 :       if (TREE_CODE (type_or_list) != TREE_LIST)
     420         6990 :         type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
     421              : 
     422         8502 :       type_node = type_list;
     423        17004 :       for (; type_node; type_node = TREE_CHAIN (type_node))
     424         8502 :         add_type_for_runtime (TREE_VALUE (type_node));
     425              :     }
     426              : 
     427        57449 :   c = ggc_cleared_alloc<eh_catch_d> ();
     428        57449 :   c->type_list = type_list;
     429        57449 :   l = t->u.eh_try.last_catch;
     430        57449 :   c->prev_catch = l;
     431        57449 :   if (l)
     432         3572 :     l->next_catch = c;
     433              :   else
     434        53877 :     t->u.eh_try.first_catch = c;
     435        57449 :   t->u.eh_try.last_catch = c;
     436              : 
     437        57449 :   return c;
     438              : }
     439              : 
     440              : eh_region
     441         5891 : gen_eh_region_allowed (eh_region outer, tree allowed)
     442              : {
     443         5891 :   eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
     444         5891 :   region->u.allowed.type_list = allowed;
     445              : 
     446         6058 :   for (; allowed ; allowed = TREE_CHAIN (allowed))
     447          167 :     add_type_for_runtime (TREE_VALUE (allowed));
     448              : 
     449         5891 :   return region;
     450              : }
     451              : 
     452              : eh_region
     453      1387895 : gen_eh_region_must_not_throw (eh_region outer)
     454              : {
     455      1387895 :   return gen_eh_region (ERT_MUST_NOT_THROW, outer);
     456              : }
     457              : 
     458              : eh_landing_pad
     459      1917330 : gen_eh_landing_pad (eh_region region)
     460              : {
     461      1917330 :   eh_landing_pad lp = ggc_cleared_alloc<eh_landing_pad_d> ();
     462              : 
     463      1917330 :   lp->next_lp = region->landing_pads;
     464      1917330 :   lp->region = region;
     465      1917330 :   lp->index = vec_safe_length (cfun->eh->lp_array);
     466      1917330 :   region->landing_pads = lp;
     467              : 
     468      1917330 :   vec_safe_push (cfun->eh->lp_array, lp);
     469              : 
     470      1917330 :   return lp;
     471              : }
     472              : 
     473              : eh_region
     474      2368529 : get_eh_region_from_number_fn (struct function *ifun, int i)
     475              : {
     476      2368529 :   return (*ifun->eh->region_array)[i];
     477              : }
     478              : 
     479              : eh_region
     480      2264165 : get_eh_region_from_number (int i)
     481              : {
     482      2264165 :   return get_eh_region_from_number_fn (cfun, i);
     483              : }
     484              : 
     485              : eh_landing_pad
     486    299209594 : get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
     487              : {
     488    299209594 :   return (*ifun->eh->lp_array)[i];
     489              : }
     490              : 
     491              : eh_landing_pad
     492    299209594 : get_eh_landing_pad_from_number (int i)
     493              : {
     494    299209594 :   return get_eh_landing_pad_from_number_fn (cfun, i);
     495              : }
     496              : 
     497              : eh_region
     498     12199441 : get_eh_region_from_lp_number_fn (struct function *ifun, int i)
     499              : {
     500     12199441 :   if (i < 0)
     501        64424 :     return (*ifun->eh->region_array)[-i];
     502     12135017 :   else if (i == 0)
     503              :     return NULL;
     504              :   else
     505              :     {
     506      5216254 :       eh_landing_pad lp;
     507      5216254 :       lp = (*ifun->eh->lp_array)[i];
     508      5216254 :       return lp->region;
     509              :     }
     510              : }
     511              : 
     512              : eh_region
     513      5056031 : get_eh_region_from_lp_number (int i)
     514              : {
     515      5056031 :   return get_eh_region_from_lp_number_fn (cfun, i);
     516              : }
     517              : 
     518              : /* Returns true if the current function has exception handling regions.  */
     519              : 
     520              : bool
     521          186 : current_function_has_exception_handlers (void)
     522              : {
     523          186 :   return cfun->eh->region_tree != NULL;
     524              : }
     525              : 
     526              : /* A subroutine of duplicate_eh_regions.  Copy the eh_region tree at OLD.
     527              :    Root it at OUTER, and apply LP_OFFSET to the lp numbers.  */
     528              : 
     529              : struct duplicate_eh_regions_data
     530              : {
     531              :   duplicate_eh_regions_map label_map;
     532              :   void *label_map_data;
     533              :   hash_map<void *, void *> *eh_map;
     534              : };
     535              : 
     536              : static void
     537       263928 : duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
     538              :                         eh_region old_r, eh_region outer)
     539              : {
     540       263928 :   eh_landing_pad old_lp, new_lp;
     541       263928 :   eh_region new_r;
     542              : 
     543       263928 :   new_r = gen_eh_region (old_r->type, outer);
     544       263928 :   bool existed = data->eh_map->put (old_r, new_r);
     545       263928 :   gcc_assert (!existed);
     546              : 
     547       263928 :   switch (old_r->type)
     548              :     {
     549              :     case ERT_CLEANUP:
     550              :       break;
     551              : 
     552        10899 :     case ERT_TRY:
     553        10899 :       {
     554        10899 :         eh_catch oc, nc;
     555        22300 :         for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
     556              :           {
     557              :             /* We should be doing all our region duplication before and
     558              :                during inlining, which is before filter lists are created.  */
     559        11401 :             gcc_assert (oc->filter_list == NULL);
     560        11401 :             nc = gen_eh_region_catch (new_r, oc->type_list);
     561        11401 :             nc->label = data->label_map (oc->label, data->label_map_data);
     562              :           }
     563              :       }
     564              :       break;
     565              : 
     566         1182 :     case ERT_ALLOWED_EXCEPTIONS:
     567         1182 :       new_r->u.allowed.type_list = old_r->u.allowed.type_list;
     568         1182 :       if (old_r->u.allowed.label)
     569          163 :         new_r->u.allowed.label
     570          163 :             = data->label_map (old_r->u.allowed.label, data->label_map_data);
     571              :       else
     572         1019 :         new_r->u.allowed.label = NULL_TREE;
     573              :       break;
     574              : 
     575       166955 :     case ERT_MUST_NOT_THROW:
     576       333910 :       new_r->u.must_not_throw.failure_loc =
     577       166955 :         LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
     578       166955 :       new_r->u.must_not_throw.failure_decl =
     579       166955 :         old_r->u.must_not_throw.failure_decl;
     580       166955 :       break;
     581              :     }
     582              : 
     583       361552 :   for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
     584              :     {
     585              :       /* Don't bother copying unused landing pads.  */
     586        97624 :       if (old_lp->post_landing_pad == NULL)
     587         2468 :         continue;
     588              : 
     589        95156 :       new_lp = gen_eh_landing_pad (new_r);
     590        95156 :       bool existed = data->eh_map->put (old_lp, new_lp);
     591        95156 :       gcc_assert (!existed);
     592              : 
     593        95156 :       new_lp->post_landing_pad
     594        95156 :         = data->label_map (old_lp->post_landing_pad, data->label_map_data);
     595        95156 :       EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
     596              :     }
     597              : 
     598              :   /* Make sure to preserve the original use of __cxa_end_cleanup.  */
     599       263928 :   new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
     600              : 
     601       289568 :   for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
     602        25640 :     duplicate_eh_regions_1 (data, old_r, new_r);
     603       263928 : }
     604              : 
     605              : /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
     606              :    the current function and root the tree below OUTER_REGION.
     607              :    The special case of COPY_REGION of NULL means all regions.
     608              :    Remap labels using MAP/MAP_DATA callback.  Return a pointer map
     609              :    that allows the caller to remap uses of both EH regions and
     610              :    EH landing pads.  */
     611              : 
     612              : hash_map<void *, void *> *
     613      5057300 : duplicate_eh_regions (struct function *ifun,
     614              :                       eh_region copy_region, int outer_lp,
     615              :                       duplicate_eh_regions_map map, void *map_data)
     616              : {
     617      5057300 :   struct duplicate_eh_regions_data data;
     618      5057300 :   eh_region outer_region;
     619              : 
     620      5057300 :   if (flag_checking)
     621      5057288 :     verify_eh_tree (ifun);
     622              : 
     623      5057300 :   data.label_map = map;
     624      5057300 :   data.label_map_data = map_data;
     625      5057300 :   data.eh_map = new hash_map<void *, void *>;
     626              : 
     627      5057300 :   outer_region = get_eh_region_from_lp_number_fn (cfun, outer_lp);
     628              : 
     629              :   /* Copy all the regions in the subtree.  */
     630      5057300 :   if (copy_region)
     631         2045 :     duplicate_eh_regions_1 (&data, copy_region, outer_region);
     632              :   else
     633              :     {
     634      5055255 :       eh_region r;
     635      5291498 :       for (r = ifun->eh->region_tree; r ; r = r->next_peer)
     636       236243 :         duplicate_eh_regions_1 (&data, r, outer_region);
     637              :     }
     638              : 
     639      5057300 :   if (flag_checking)
     640      5057288 :     verify_eh_tree (cfun);
     641              : 
     642      5057300 :   return data.eh_map;
     643              : }
     644              : 
     645              : /* Return the region that is outer to both REGION_A and REGION_B in IFUN.  */
     646              : 
     647              : eh_region
     648          249 : eh_region_outermost (struct function *ifun, eh_region region_a,
     649              :                      eh_region region_b)
     650              : {
     651          249 :   gcc_assert (ifun->eh->region_array);
     652          249 :   gcc_assert (ifun->eh->region_tree);
     653              : 
     654          249 :   auto_sbitmap b_outer (ifun->eh->region_array->length ());
     655          249 :   bitmap_clear (b_outer);
     656              : 
     657          600 :   do
     658              :     {
     659          600 :       bitmap_set_bit (b_outer, region_b->index);
     660          600 :       region_b = region_b->outer;
     661              :     }
     662          600 :   while (region_b);
     663              : 
     664          489 :   do
     665              :     {
     666          489 :       if (bitmap_bit_p (b_outer, region_a->index))
     667              :         break;
     668          241 :       region_a = region_a->outer;
     669              :     }
     670          241 :   while (region_a);
     671              : 
     672          249 :   return region_a;
     673          249 : }
     674              : 
     675              : void
     676         8671 : add_type_for_runtime (tree type)
     677              : {
     678              :   /* If TYPE is NOP_EXPR, it means that it already is a runtime type.  */
     679         8671 :   if (TREE_CODE (type) == NOP_EXPR)
     680           11 :     return;
     681              : 
     682         8660 :   bool existed = false;
     683         8660 :   tree *slot = &type_to_runtime_map->get_or_insert (type, &existed);
     684         8660 :   if (!existed)
     685         3203 :     *slot = lang_hooks.eh_runtime_type (type);
     686              : }
     687              : 
     688              : tree
     689        34808 : lookup_type_for_runtime (tree type)
     690              : {
     691              :   /* If TYPE is NOP_EXPR, it means that it already is a runtime type.  */
     692        34808 :   if (TREE_CODE (type) == NOP_EXPR)
     693              :     return type;
     694              : 
     695              :   /* We should have always inserted the data earlier.  */
     696        34808 :   return *type_to_runtime_map->get (type);
     697              : }
     698              : 
     699              : 
     700              : /* Represent an entry in @TTypes for either catch actions
     701              :    or exception filter actions.  */
     702              : struct ttypes_filter {
     703              :   tree t;
     704              :   int filter;
     705              : };
     706              : 
     707              : /* Helper for ttypes_filter hashing.  */
     708              : 
     709              : struct ttypes_filter_hasher : free_ptr_hash <ttypes_filter>
     710              : {
     711              :   typedef tree_node *compare_type;
     712              :   static inline hashval_t hash (const ttypes_filter *);
     713              :   static inline bool equal (const ttypes_filter *, const tree_node *);
     714              : };
     715              : 
     716              : /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
     717              :    (a tree) for a @TTypes type node we are thinking about adding.  */
     718              : 
     719              : inline bool
     720        23422 : ttypes_filter_hasher::equal (const ttypes_filter *entry, const tree_node *data)
     721              : {
     722        23422 :   return entry->t == data;
     723              : }
     724              : 
     725              : inline hashval_t
     726        26874 : ttypes_filter_hasher::hash (const ttypes_filter *entry)
     727              : {
     728        26874 :   return TREE_HASH (entry->t);
     729              : }
     730              : 
     731              : typedef hash_table<ttypes_filter_hasher> ttypes_hash_type;
     732              : 
     733              : 
     734              : /* Helper for ehspec hashing.  */
     735              : 
     736              : struct ehspec_hasher : free_ptr_hash <ttypes_filter>
     737              : {
     738              :   static inline hashval_t hash (const ttypes_filter *);
     739              :   static inline bool equal (const ttypes_filter *, const ttypes_filter *);
     740              : };
     741              : 
     742              : /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
     743              :    exception specification list we are thinking about adding.  */
     744              : /* ??? Currently we use the type lists in the order given.  Someone
     745              :    should put these in some canonical order.  */
     746              : 
     747              : inline bool
     748          536 : ehspec_hasher::equal (const ttypes_filter *entry, const ttypes_filter *data)
     749              : {
     750          536 :   return type_list_equal (entry->t, data->t);
     751              : }
     752              : 
     753              : /* Hash function for exception specification lists.  */
     754              : 
     755              : inline hashval_t
     756         1845 : ehspec_hasher::hash (const ttypes_filter *entry)
     757              : {
     758         1845 :   hashval_t h = 0;
     759         1845 :   tree list;
     760              : 
     761         1985 :   for (list = entry->t; list ; list = TREE_CHAIN (list))
     762          140 :     h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
     763         1845 :   return h;
     764              : }
     765              : 
     766              : typedef hash_table<ehspec_hasher> ehspec_hash_type;
     767              : 
     768              : 
     769              : /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
     770              :    to speed up the search.  Return the filter value to be used.  */
     771              : 
     772              : static int
     773        47187 : add_ttypes_entry (ttypes_hash_type *ttypes_hash, tree type)
     774              : {
     775        47187 :   struct ttypes_filter **slot, *n;
     776              : 
     777        47187 :   slot = ttypes_hash->find_slot_with_hash (type, (hashval_t) TREE_HASH (type),
     778              :                                           INSERT);
     779              : 
     780        47187 :   if ((n = *slot) == NULL)
     781              :     {
     782              :       /* Filter value is a 1 based table index.  */
     783              : 
     784        24047 :       n = XNEW (struct ttypes_filter);
     785        24047 :       n->t = type;
     786        24047 :       n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
     787        24047 :       *slot = n;
     788              : 
     789        24047 :       vec_safe_push (cfun->eh->ttype_data, type);
     790              :     }
     791              : 
     792        47187 :   return n->filter;
     793              : }
     794              : 
     795              : /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
     796              :    to speed up the search.  Return the filter value to be used.  */
     797              : 
     798              : static int
     799         1321 : add_ehspec_entry (ehspec_hash_type *ehspec_hash, ttypes_hash_type *ttypes_hash,
     800              :                   tree list)
     801              : {
     802         1321 :   struct ttypes_filter **slot, *n;
     803         1321 :   struct ttypes_filter dummy;
     804              : 
     805         1321 :   dummy.t = list;
     806         1321 :   slot = ehspec_hash->find_slot (&dummy, INSERT);
     807              : 
     808         1321 :   if ((n = *slot) == NULL)
     809              :     {
     810          785 :       int len;
     811              : 
     812          785 :       if (targetm.arm_eabi_unwinder)
     813            0 :         len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
     814              :       else
     815         1570 :         len = vec_safe_length (cfun->eh->ehspec_data.other);
     816              : 
     817              :       /* Filter value is a -1 based byte index into a uleb128 buffer.  */
     818              : 
     819          785 :       n = XNEW (struct ttypes_filter);
     820          785 :       n->t = list;
     821          785 :       n->filter = -(len + 1);
     822          785 :       *slot = n;
     823              : 
     824              :       /* Generate a 0 terminated list of filter values.  */
     825          913 :       for (; list ; list = TREE_CHAIN (list))
     826              :         {
     827          128 :           if (targetm.arm_eabi_unwinder)
     828            0 :             vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
     829              :           else
     830              :             {
     831              :               /* Look up each type in the list and encode its filter
     832              :                  value as a uleb128.  */
     833          256 :               push_uleb128 (&cfun->eh->ehspec_data.other,
     834          128 :                             add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
     835              :             }
     836              :         }
     837          785 :       if (targetm.arm_eabi_unwinder)
     838            0 :         vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
     839              :       else
     840          785 :         vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
     841              :     }
     842              : 
     843         1321 :   return n->filter;
     844              : }
     845              : 
     846              : /* Generate the action filter values to be used for CATCH and
     847              :    ALLOWED_EXCEPTIONS regions.  When using dwarf2 exception regions,
     848              :    we use lots of landing pads, and so every type or list can share
     849              :    the same filter value, which saves table space.  */
     850              : 
     851              : void
     852       149027 : assign_filter_values (void)
     853              : {
     854       149027 :   int i;
     855       149027 :   eh_region r;
     856       149027 :   eh_catch c;
     857              : 
     858       149027 :   vec_alloc (cfun->eh->ttype_data, 16);
     859       149027 :   if (targetm.arm_eabi_unwinder)
     860            0 :     vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
     861              :   else
     862       149027 :     vec_alloc (cfun->eh->ehspec_data.other, 64);
     863              : 
     864       149027 :   ehspec_hash_type ehspec (31);
     865       149027 :   ttypes_hash_type ttypes (31);
     866              : 
     867      1490688 :   for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
     868              :     {
     869      1192634 :       if (r == NULL)
     870       705428 :         continue;
     871              : 
     872       487206 :       switch (r->type)
     873              :         {
     874        44991 :         case ERT_TRY:
     875        92050 :           for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
     876              :             {
     877              :               /* Whatever type_list is (NULL or true list), we build a list
     878              :                  of filters for the region.  */
     879        47059 :               c->filter_list = NULL_TREE;
     880              : 
     881        47059 :               if (c->type_list != NULL)
     882              :                 {
     883              :                   /* Get a filter value for each of the types caught and store
     884              :                      them in the region's dedicated list.  */
     885              :                   tree tp_node = c->type_list;
     886              : 
     887        13986 :                   for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
     888              :                     {
     889         6993 :                       int flt
     890         6993 :                         = add_ttypes_entry (&ttypes, TREE_VALUE (tp_node));
     891         6993 :                       tree flt_node = build_int_cst (integer_type_node, flt);
     892              : 
     893         6993 :                       c->filter_list
     894         6993 :                         = tree_cons (NULL_TREE, flt_node, c->filter_list);
     895              :                     }
     896              :                 }
     897              :               else
     898              :                 {
     899              :                   /* Get a filter value for the NULL list also since it
     900              :                      will need an action record anyway.  */
     901        40066 :                   int flt = add_ttypes_entry (&ttypes, NULL);
     902        40066 :                   tree flt_node = build_int_cst (integer_type_node, flt);
     903              : 
     904        40066 :                   c->filter_list
     905        40066 :                     = tree_cons (NULL_TREE, flt_node, NULL);
     906              :                 }
     907              :             }
     908              :           break;
     909              : 
     910         1321 :         case ERT_ALLOWED_EXCEPTIONS:
     911         1321 :           r->u.allowed.filter
     912         1321 :             = add_ehspec_entry (&ehspec, &ttypes, r->u.allowed.type_list);
     913         1321 :           break;
     914              : 
     915              :         default:
     916              :           break;
     917              :         }
     918              :     }
     919       149027 : }
     920              : 
     921              : /* Emit SEQ into basic block just before INSN (that is assumed to be
     922              :    first instruction of some existing BB and return the newly
     923              :    produced block.  */
     924              : static basic_block
     925       229125 : emit_to_new_bb_before (rtx_insn *seq, rtx_insn *insn)
     926              : {
     927       229125 :   rtx_insn *next, *last;
     928       229125 :   basic_block bb;
     929       229125 :   edge e;
     930       229125 :   edge_iterator ei;
     931              : 
     932              :   /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
     933              :      call), we don't want it to go into newly created landing pad or other EH
     934              :      construct.  */
     935      1156311 :   for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
     936       927186 :     if (e->flags & EDGE_FALLTHRU)
     937        60688 :       force_nonfallthru (e);
     938              :     else
     939       866498 :       ei_next (&ei);
     940              : 
     941              :   /* Make sure to put the location of INSN or a subsequent instruction on SEQ
     942              :      to avoid inheriting the location of the previous instruction.  */
     943              :   next = insn;
     944      1106254 :   while (next && !NONDEBUG_INSN_P (next))
     945       877129 :     next = NEXT_INSN (next);
     946       229125 :   if (next)
     947       229117 :     last = emit_insn_before_setloc (seq, insn, INSN_LOCATION (next));
     948              :   else
     949            8 :     last = emit_insn_before (seq, insn);
     950       229125 :   if (BARRIER_P (last))
     951            0 :     last = PREV_INSN (last);
     952       229125 :   bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
     953       229125 :   update_bb_for_insn (bb);
     954       229125 :   bb->flags |= BB_SUPERBLOCK;
     955       229125 :   return bb;
     956              : }
     957              : 
     958              : /* A subroutine of dw2_build_landing_pads, also used for edge splitting
     959              :    at the rtl level.  Emit the code required by the target at a landing
     960              :    pad for the given region.  */
     961              : 
     962              : static void
     963       229125 : expand_dw2_landing_pad_for_region (eh_region region)
     964              : {
     965       229125 :   if (targetm.have_exception_receiver ())
     966            0 :     emit_insn (targetm.gen_exception_receiver ());
     967       229125 :   else if (targetm.have_nonlocal_goto_receiver ())
     968            0 :     emit_insn (targetm.gen_nonlocal_goto_receiver ());
     969              :   else
     970              :     { /* Nothing */ }
     971              : 
     972       229125 :   if (region->exc_ptr_reg)
     973              :     {
     974       201641 :       rtx exc_ptr_reg;
     975       201641 :       if (EH_RETURN_DATA_REGNO (0) != INVALID_REGNUM)
     976       201641 :         exc_ptr_reg = gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0));
     977              :       else
     978              :         /* The target must be doing something special.  Submit a dummy.  */
     979              :         exc_ptr_reg = constm1_rtx;
     980       201641 :       emit_move_insn (region->exc_ptr_reg, exc_ptr_reg);
     981              :     }
     982       229125 :   if (region->filter_reg)
     983              :     {
     984       127409 :       rtx filter_reg;
     985       127409 :       if (EH_RETURN_DATA_REGNO (1) != INVALID_REGNUM)
     986       127409 :         filter_reg = gen_rtx_REG (targetm.eh_return_filter_mode (),
     987              :                                   EH_RETURN_DATA_REGNO (1));
     988              :       else
     989              :         /* The target must be doing something special.  Submit a dummy.  */
     990              :         filter_reg = constm1_rtx;
     991       127409 :       emit_move_insn (region->filter_reg, filter_reg);
     992              :     }
     993       229125 : }
     994              : 
     995              : /* Expand the extra code needed at landing pads for dwarf2 unwinding.  */
     996              : 
     997              : static void
     998        66410 : dw2_build_landing_pads (void)
     999              : {
    1000        66410 :   int i;
    1001        66410 :   eh_landing_pad lp;
    1002        66410 :   int e_flags = EDGE_FALLTHRU;
    1003              : 
    1004              :   /* If we're going to partition blocks, we need to be able to add
    1005              :      new landing pads later, which means that we need to hold on to
    1006              :      the post-landing-pad block.  Prevent it from being merged away.
    1007              :      We'll remove this bit after partitioning.  */
    1008        66410 :   if (flag_reorder_blocks_and_partition)
    1009        45373 :     e_flags |= EDGE_PRESERVE;
    1010              : 
    1011      1379408 :   for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
    1012              :     {
    1013      1312998 :       basic_block bb;
    1014      1312998 :       rtx_insn *seq;
    1015              : 
    1016      1312998 :       if (lp == NULL || lp->post_landing_pad == NULL)
    1017      1083873 :         continue;
    1018              : 
    1019       229125 :       start_sequence ();
    1020              : 
    1021       229125 :       lp->landing_pad = gen_label_rtx ();
    1022       229125 :       emit_label (lp->landing_pad);
    1023       229125 :       LABEL_PRESERVE_P (lp->landing_pad) = 1;
    1024              : 
    1025       229125 :       expand_dw2_landing_pad_for_region (lp->region);
    1026              : 
    1027       229125 :       seq = end_sequence ();
    1028              : 
    1029       229125 :       bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
    1030       229125 :       bb->count = bb->next_bb->count;
    1031       229125 :       make_single_succ_edge (bb, bb->next_bb, e_flags);
    1032       229125 :       if (current_loops)
    1033              :         {
    1034       229125 :           class loop *loop = bb->next_bb->loop_father;
    1035              :           /* If we created a pre-header block, add the new block to the
    1036              :              outer loop, otherwise to the loop itself.  */
    1037       229125 :           if (bb->next_bb == loop->header)
    1038         8330 :             add_bb_to_loop (bb, loop_outer (loop));
    1039              :           else
    1040       220795 :             add_bb_to_loop (bb, loop);
    1041              :         }
    1042              :     }
    1043        66410 : }
    1044              : 
    1045              : 
    1046              : static vec<int> sjlj_lp_call_site_index;
    1047              : 
    1048              : /* Process all active landing pads.  Assign each one a compact dispatch
    1049              :    index, and a call-site index.  */
    1050              : 
    1051              : static int
    1052            0 : sjlj_assign_call_site_values (void)
    1053              : {
    1054            0 :   action_hash_type ar_hash (31);
    1055            0 :   int i, disp_index;
    1056            0 :   eh_landing_pad lp;
    1057              : 
    1058            0 :   vec_alloc (crtl->eh.action_record_data, 64);
    1059              : 
    1060            0 :   disp_index = 0;
    1061            0 :   call_site_base = 1;
    1062            0 :   for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
    1063            0 :     if (lp && lp->post_landing_pad)
    1064              :       {
    1065            0 :         int action, call_site;
    1066              : 
    1067              :         /* First: build the action table.  */
    1068            0 :         action = collect_one_action_chain (&ar_hash, lp->region);
    1069              : 
    1070              :         /* Next: assign call-site values.  If dwarf2 terms, this would be
    1071              :            the region number assigned by convert_to_eh_region_ranges, but
    1072              :            handles no-action and must-not-throw differently.  */
    1073              :         /* Map must-not-throw to otherwise unused call-site index 0.  */
    1074            0 :         if (action == -2)
    1075              :           call_site = 0;
    1076              :         /* Map no-action to otherwise unused call-site index -1.  */
    1077            0 :         else if (action == -1)
    1078              :           call_site = -1;
    1079              :         /* Otherwise, look it up in the table.  */
    1080              :         else
    1081            0 :           call_site = add_call_site (GEN_INT (disp_index), action, 0);
    1082            0 :         sjlj_lp_call_site_index[i] = call_site;
    1083              : 
    1084            0 :         disp_index++;
    1085              :       }
    1086              : 
    1087            0 :   return disp_index;
    1088            0 : }
    1089              : 
    1090              : /* Emit code to record the current call-site index before every
    1091              :    insn that can throw.  */
    1092              : 
    1093              : static void
    1094            0 : sjlj_mark_call_sites (void)
    1095              : {
    1096            0 :   int last_call_site = -2;
    1097            0 :   rtx_insn *insn;
    1098            0 :   rtx mem;
    1099              : 
    1100            0 :   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
    1101              :     {
    1102            0 :       eh_landing_pad lp;
    1103            0 :       eh_region r;
    1104            0 :       bool nothrow;
    1105            0 :       int this_call_site;
    1106            0 :       rtx_insn *before, *p;
    1107              : 
    1108              :       /* Reset value tracking at extended basic block boundaries.  */
    1109            0 :       if (LABEL_P (insn))
    1110            0 :         last_call_site = -2;
    1111              : 
    1112              :       /* If the function allocates dynamic stack space, the context must
    1113              :          be updated after every allocation/deallocation accordingly.  */
    1114            0 :       if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_UPDATE_SJLJ_CONTEXT)
    1115              :         {
    1116            0 :           rtx buf_addr;
    1117              : 
    1118            0 :           start_sequence ();
    1119            0 :           buf_addr = plus_constant (Pmode, XEXP (crtl->eh.sjlj_fc, 0),
    1120            0 :                                     sjlj_fc_jbuf_ofs);
    1121            0 :           expand_builtin_update_setjmp_buf (buf_addr);
    1122            0 :           p = end_sequence ();
    1123            0 :           emit_insn_before (p, insn);
    1124              :         }
    1125              : 
    1126            0 :       if (! INSN_P (insn))
    1127            0 :         continue;
    1128              : 
    1129            0 :       nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
    1130            0 :       if (nothrow)
    1131            0 :         continue;
    1132            0 :       if (lp)
    1133            0 :         this_call_site = sjlj_lp_call_site_index[lp->index];
    1134            0 :       else if (r == NULL)
    1135              :         {
    1136              :           /* Calls (and trapping insns) without notes are outside any
    1137              :              exception handling region in this function.  Mark them as
    1138              :              no action.  */
    1139              :           this_call_site = -1;
    1140              :         }
    1141              :       else
    1142              :         {
    1143            0 :           gcc_assert (r->type == ERT_MUST_NOT_THROW);
    1144              :           this_call_site = 0;
    1145              :         }
    1146              : 
    1147            0 :       if (this_call_site != -1)
    1148            0 :         crtl->uses_eh_lsda = 1;
    1149              : 
    1150            0 :       if (this_call_site == last_call_site)
    1151            0 :         continue;
    1152              : 
    1153              :       /* Don't separate a call from it's argument loads.  */
    1154            0 :       before = insn;
    1155            0 :       if (CALL_P (insn))
    1156            0 :         before = find_first_parameter_load (insn, NULL);
    1157              : 
    1158            0 :       start_sequence ();
    1159            0 :       mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
    1160              :                             sjlj_fc_call_site_ofs);
    1161            0 :       emit_move_insn (mem, gen_int_mode (this_call_site, GET_MODE (mem)));
    1162            0 :       p = end_sequence ();
    1163              : 
    1164            0 :       emit_insn_before (p, before);
    1165            0 :       last_call_site = this_call_site;
    1166              :     }
    1167            0 : }
    1168              : 
    1169              : /* Construct the SjLj_Function_Context.  */
    1170              : 
    1171              : static void
    1172            0 : sjlj_emit_function_enter (rtx_code_label *dispatch_label)
    1173              : {
    1174            0 :   rtx_insn *fn_begin, *seq;
    1175            0 :   rtx fc, mem;
    1176            0 :   bool fn_begin_outside_block;
    1177            0 :   rtx personality = get_personality_function (current_function_decl);
    1178              : 
    1179            0 :   fc = crtl->eh.sjlj_fc;
    1180              : 
    1181            0 :   start_sequence ();
    1182              : 
    1183              :   /* We're storing this libcall's address into memory instead of
    1184              :      calling it directly.  Thus, we must call assemble_external_libcall
    1185              :      here, as we cannot depend on emit_library_call to do it for us.  */
    1186            0 :   assemble_external_libcall (personality);
    1187            0 :   mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
    1188            0 :   emit_move_insn (mem, personality);
    1189              : 
    1190            0 :   mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
    1191            0 :   if (crtl->uses_eh_lsda)
    1192              :     {
    1193            0 :       char buf[20];
    1194            0 :       rtx sym;
    1195              : 
    1196            0 :       ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
    1197            0 :       sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
    1198            0 :       SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
    1199            0 :       emit_move_insn (mem, sym);
    1200              :     }
    1201              :   else
    1202            0 :     emit_move_insn (mem, const0_rtx);
    1203              : 
    1204            0 :   if (dispatch_label)
    1205              :     {
    1206            0 :       rtx addr = plus_constant (Pmode, XEXP (fc, 0), sjlj_fc_jbuf_ofs);
    1207              : 
    1208              : #ifdef DONT_USE_BUILTIN_SETJMP
    1209              :       addr = copy_addr_to_reg (addr);
    1210              :       addr = convert_memory_address (ptr_mode, addr);
    1211              :       tree addr_tree = make_tree (ptr_type_node, addr);
    1212              : 
    1213              :       tree call_expr = build_call_expr (setjmp_fn, 1, addr_tree);
    1214              :       rtx x = expand_call (call_expr, NULL_RTX, false);
    1215              : 
    1216              :       emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
    1217              :                                TYPE_MODE (integer_type_node), 0,
    1218              :                                dispatch_label,
    1219              :                                profile_probability::unlikely ());
    1220              : #else
    1221            0 :       expand_builtin_setjmp_setup (addr, dispatch_label);
    1222              : #endif
    1223              :     }
    1224              : 
    1225            0 :   emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
    1226            0 :                      XEXP (fc, 0), Pmode);
    1227              : 
    1228            0 :   seq = end_sequence ();
    1229              : 
    1230              :   /* ??? Instead of doing this at the beginning of the function,
    1231              :      do this in a block that is at loop level 0 and dominates all
    1232              :      can_throw_internal instructions.  */
    1233              : 
    1234            0 :   fn_begin_outside_block = true;
    1235            0 :   for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
    1236            0 :     if (NOTE_P (fn_begin))
    1237              :       {
    1238            0 :         if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
    1239              :           break;
    1240            0 :         else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
    1241            0 :           fn_begin_outside_block = false;
    1242              :       }
    1243              :     /* assign_params can indirectly call emit_block_move_via_loop, e.g.
    1244              :        for g++.dg/torture/pr85627.C for 16-bit targets.  */
    1245            0 :     else if (JUMP_P (fn_begin))
    1246            0 :       fn_begin_outside_block = true;
    1247              : 
    1248              : #ifdef DONT_USE_BUILTIN_SETJMP
    1249              :   if (dispatch_label)
    1250              :     {
    1251              :       /* The sequence contains a branch in the middle so we need to force
    1252              :          the creation of a new basic block by means of BB_SUPERBLOCK.  */
    1253              :       if (fn_begin_outside_block)
    1254              :         {
    1255              :           basic_block bb
    1256              :             = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1257              :           if (JUMP_P (BB_END (bb)))
    1258              :             emit_insn_before (seq, BB_END (bb));
    1259              :           else
    1260              :             emit_insn_after (seq, BB_END (bb));
    1261              :         }
    1262              :       else
    1263              :         emit_insn_after (seq, fn_begin);
    1264              : 
    1265              :       single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flags |= BB_SUPERBLOCK;
    1266              :       return;
    1267              :     }
    1268              : #endif
    1269              : 
    1270            0 :   if (fn_begin_outside_block)
    1271            0 :     insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1272              :   else
    1273            0 :     emit_insn_after (seq, fn_begin);
    1274            0 : }
    1275              : 
    1276              : /* Call back from expand_function_end to know where we should put
    1277              :    the call to unwind_sjlj_unregister_libfunc if needed.  */
    1278              : 
    1279              : void
    1280            0 : sjlj_emit_function_exit_after (rtx_insn *after)
    1281              : {
    1282            0 :   crtl->eh.sjlj_exit_after = after;
    1283            0 : }
    1284              : 
    1285              : static void
    1286            0 : sjlj_emit_function_exit (void)
    1287              : {
    1288            0 :   rtx_insn *seq, *insn;
    1289              : 
    1290            0 :   start_sequence ();
    1291              : 
    1292            0 :   emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
    1293            0 :                      XEXP (crtl->eh.sjlj_fc, 0), Pmode);
    1294              : 
    1295            0 :   seq = end_sequence ();
    1296              : 
    1297              :   /* ??? Really this can be done in any block at loop level 0 that
    1298              :      post-dominates all can_throw_internal instructions.  This is
    1299              :      the last possible moment.  */
    1300              : 
    1301            0 :   insn = crtl->eh.sjlj_exit_after;
    1302            0 :   if (LABEL_P (insn))
    1303            0 :     insn = NEXT_INSN (insn);
    1304              : 
    1305            0 :   emit_insn_after (seq, insn);
    1306            0 : }
    1307              : 
    1308              : static void
    1309            0 : sjlj_emit_dispatch_table (rtx_code_label *dispatch_label, int num_dispatch)
    1310              : {
    1311            0 :   scalar_int_mode unwind_word_mode = targetm.unwind_word_mode ();
    1312            0 :   scalar_int_mode filter_mode = targetm.eh_return_filter_mode ();
    1313            0 :   eh_landing_pad lp;
    1314            0 :   rtx mem, fc, exc_ptr_reg, filter_reg;
    1315            0 :   rtx_insn *seq;
    1316            0 :   basic_block bb;
    1317            0 :   eh_region r;
    1318            0 :   int i, disp_index;
    1319            0 :   vec<tree> dispatch_labels = vNULL;
    1320              : 
    1321            0 :   fc = crtl->eh.sjlj_fc;
    1322              : 
    1323            0 :   start_sequence ();
    1324              : 
    1325            0 :   emit_label (dispatch_label);
    1326              : 
    1327              : #ifndef DONT_USE_BUILTIN_SETJMP
    1328            0 :   expand_builtin_setjmp_receiver (dispatch_label);
    1329              : 
    1330              :   /* The caller of expand_builtin_setjmp_receiver is responsible for
    1331              :      making sure that the label doesn't vanish.  The only other caller
    1332              :      is the expander for __builtin_setjmp_receiver, which places this
    1333              :      label on the nonlocal_goto_label list.  Since we're modeling these
    1334              :      CFG edges more exactly, we can use the forced_labels list instead.  */
    1335            0 :   LABEL_PRESERVE_P (dispatch_label) = 1;
    1336            0 :   vec_safe_push<rtx_insn *> (forced_labels, dispatch_label);
    1337              : #endif
    1338              : 
    1339              :   /* Load up exc_ptr and filter values from the function context.  */
    1340            0 :   mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
    1341            0 :   if (unwind_word_mode != ptr_mode)
    1342              :     {
    1343              : #ifdef POINTERS_EXTEND_UNSIGNED
    1344            0 :       mem = convert_memory_address (ptr_mode, mem);
    1345              : #else
    1346              :       mem = convert_to_mode (ptr_mode, mem, 0);
    1347              : #endif
    1348              :     }
    1349            0 :   exc_ptr_reg = force_reg (ptr_mode, mem);
    1350              : 
    1351            0 :   mem = adjust_address (fc, unwind_word_mode,
    1352              :                         sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
    1353            0 :   if (unwind_word_mode != filter_mode)
    1354            0 :     mem = convert_to_mode (filter_mode, mem, 0);
    1355            0 :   filter_reg = force_reg (filter_mode, mem);
    1356              : 
    1357              :   /* Jump to one of the directly reachable regions.  */
    1358              : 
    1359            0 :   disp_index = 0;
    1360            0 :   rtx_code_label *first_reachable_label = NULL;
    1361              : 
    1362              :   /* If there's exactly one call site in the function, don't bother
    1363              :      generating a switch statement.  */
    1364            0 :   if (num_dispatch > 1)
    1365            0 :     dispatch_labels.create (num_dispatch);
    1366              : 
    1367            0 :   for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
    1368            0 :     if (lp && lp->post_landing_pad)
    1369              :       {
    1370            0 :         rtx_insn *seq2;
    1371            0 :         rtx_code_label *label;
    1372              : 
    1373            0 :         start_sequence ();
    1374              : 
    1375            0 :         lp->landing_pad = dispatch_label;
    1376              : 
    1377            0 :         if (num_dispatch > 1)
    1378              :           {
    1379            0 :             tree t_label, case_elt, t;
    1380              : 
    1381            0 :             t_label = create_artificial_label (UNKNOWN_LOCATION);
    1382            0 :             t = build_int_cst (integer_type_node, disp_index);
    1383            0 :             case_elt = build_case_label (t, NULL, t_label);
    1384            0 :             dispatch_labels.quick_push (case_elt);
    1385            0 :             label = jump_target_rtx (t_label);
    1386              :           }
    1387              :         else
    1388            0 :           label = gen_label_rtx ();
    1389              : 
    1390            0 :         if (disp_index == 0)
    1391            0 :           first_reachable_label = label;
    1392            0 :         emit_label (label);
    1393              : 
    1394            0 :         r = lp->region;
    1395            0 :         if (r->exc_ptr_reg)
    1396            0 :           emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
    1397            0 :         if (r->filter_reg)
    1398            0 :           emit_move_insn (r->filter_reg, filter_reg);
    1399              : 
    1400            0 :         seq2 = end_sequence ();
    1401              : 
    1402            0 :         rtx_insn *before = label_rtx (lp->post_landing_pad);
    1403            0 :         bb = emit_to_new_bb_before (seq2, before);
    1404            0 :         make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
    1405            0 :         if (current_loops)
    1406              :           {
    1407            0 :             class loop *loop = bb->next_bb->loop_father;
    1408              :             /* If we created a pre-header block, add the new block to the
    1409              :                outer loop, otherwise to the loop itself.  */
    1410            0 :             if (bb->next_bb == loop->header)
    1411            0 :               add_bb_to_loop (bb, loop_outer (loop));
    1412              :             else
    1413            0 :               add_bb_to_loop (bb, loop);
    1414              :             /* ???  For multiple dispatches we will end up with edges
    1415              :                from the loop tree root into this loop, making it a
    1416              :                multiple-entry loop.  Discard all affected loops.  */
    1417            0 :             if (num_dispatch > 1)
    1418              :               {
    1419            0 :                 for (loop = bb->loop_father;
    1420            0 :                      loop_outer (loop); loop = loop_outer (loop))
    1421            0 :                   mark_loop_for_removal (loop);
    1422              :               }
    1423              :           }
    1424              : 
    1425            0 :         disp_index++;
    1426              :       }
    1427            0 :   gcc_assert (disp_index == num_dispatch);
    1428              : 
    1429            0 :   if (num_dispatch > 1)
    1430              :     {
    1431            0 :       rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
    1432              :                                  sjlj_fc_call_site_ofs);
    1433            0 :       expand_sjlj_dispatch_table (disp, dispatch_labels);
    1434              :     }
    1435              : 
    1436            0 :   seq = end_sequence ();
    1437              : 
    1438            0 :   bb = emit_to_new_bb_before (seq, first_reachable_label);
    1439            0 :   if (num_dispatch == 1)
    1440              :     {
    1441            0 :       make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
    1442            0 :       if (current_loops)
    1443              :         {
    1444            0 :           class loop *loop = bb->next_bb->loop_father;
    1445              :           /* If we created a pre-header block, add the new block to the
    1446              :              outer loop, otherwise to the loop itself.  */
    1447            0 :           if (bb->next_bb == loop->header)
    1448            0 :             add_bb_to_loop (bb, loop_outer (loop));
    1449              :           else
    1450            0 :             add_bb_to_loop (bb, loop);
    1451              :         }
    1452              :     }
    1453              :   else
    1454              :     {
    1455              :       /* We are not wiring up edges here, but as the dispatcher call
    1456              :          is at function begin simply associate the block with the
    1457              :          outermost (non-)loop.  */
    1458            0 :       if (current_loops)
    1459            0 :         add_bb_to_loop (bb, current_loops->tree_root);
    1460              :     }
    1461            0 : }
    1462              : 
    1463              : static void
    1464            0 : sjlj_build_landing_pads (void)
    1465              : {
    1466            0 :   int num_dispatch;
    1467              : 
    1468            0 :   num_dispatch = vec_safe_length (cfun->eh->lp_array);
    1469            0 :   if (num_dispatch == 0)
    1470              :     return;
    1471            0 :   sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch, true);
    1472              : 
    1473            0 :   num_dispatch = sjlj_assign_call_site_values ();
    1474            0 :   if (num_dispatch > 0)
    1475              :     {
    1476            0 :       rtx_code_label *dispatch_label = gen_label_rtx ();
    1477            0 :       int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
    1478              :                                         TYPE_MODE (sjlj_fc_type_node),
    1479              :                                         TYPE_ALIGN (sjlj_fc_type_node));
    1480            0 :       crtl->eh.sjlj_fc
    1481            0 :         = assign_stack_local_1 (TYPE_MODE (sjlj_fc_type_node),
    1482            0 :                                 int_size_in_bytes (sjlj_fc_type_node),
    1483              :                                 align, ASLK_RECORD_PAD | ASLK_REDUCE_ALIGN);
    1484              : 
    1485            0 :       sjlj_mark_call_sites ();
    1486            0 :       sjlj_emit_function_enter (dispatch_label);
    1487            0 :       sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
    1488            0 :       sjlj_emit_function_exit ();
    1489              :     }
    1490              : 
    1491              :   /* If we do not have any landing pads, we may still need to register a
    1492              :      personality routine and (empty) LSDA to handle must-not-throw regions.  */
    1493            0 :   else if (function_needs_eh_personality (cfun) != eh_personality_none)
    1494              :     {
    1495            0 :       int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
    1496              :                                         TYPE_MODE (sjlj_fc_type_node),
    1497              :                                         TYPE_ALIGN (sjlj_fc_type_node));
    1498            0 :       crtl->eh.sjlj_fc
    1499            0 :         = assign_stack_local_1 (TYPE_MODE (sjlj_fc_type_node),
    1500            0 :                                 int_size_in_bytes (sjlj_fc_type_node),
    1501              :                                 align, ASLK_RECORD_PAD | ASLK_REDUCE_ALIGN);
    1502              : 
    1503            0 :       sjlj_mark_call_sites ();
    1504            0 :       sjlj_emit_function_enter (NULL);
    1505            0 :       sjlj_emit_function_exit ();
    1506              :     }
    1507              : 
    1508            0 :   sjlj_lp_call_site_index.release ();
    1509              : }
    1510              : 
    1511              : /* Update the sjlj function context.  This function should be called
    1512              :    whenever we allocate or deallocate dynamic stack space.  */
    1513              : 
    1514              : void
    1515            0 : update_sjlj_context (void)
    1516              : {
    1517            0 :   if (!flag_exceptions)
    1518              :     return;
    1519              : 
    1520            0 :   emit_note (NOTE_INSN_UPDATE_SJLJ_CONTEXT);
    1521              : }
    1522              : 
    1523              : /* After initial rtl generation, call back to finish generating
    1524              :    exception support code.  */
    1525              : 
    1526              : void
    1527        66410 : finish_eh_generation (void)
    1528              : {
    1529        66410 :   basic_block bb;
    1530              : 
    1531              :   /* Construct the landing pads.  */
    1532        66410 :   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
    1533            0 :     sjlj_build_landing_pads ();
    1534              :   else
    1535        66410 :     dw2_build_landing_pads ();
    1536              : 
    1537        66410 :   break_superblocks ();
    1538              : 
    1539              :   /* Redirect all EH edges from the post_landing_pad to the landing pad.  */
    1540      3914827 :   FOR_EACH_BB_FN (bb, cfun)
    1541              :     {
    1542      3848417 :       eh_landing_pad lp;
    1543      3848417 :       edge_iterator ei;
    1544      3848417 :       edge e;
    1545              : 
    1546      3848417 :       lp = get_eh_landing_pad_from_rtx (BB_END (bb));
    1547              : 
    1548      7979527 :       FOR_EACH_EDGE (e, ei, bb->succs)
    1549      4897706 :         if (e->flags & EDGE_EH)
    1550              :           break;
    1551              : 
    1552              :       /* We should not have generated any new throwing insns during this
    1553              :          pass, and we should not have lost any EH edges, so we only need
    1554              :          to handle two cases here:
    1555              :          (1) reachable handler and an existing edge to post-landing-pad,
    1556              :          (2) no reachable handler and no edge.  */
    1557      3848417 :       gcc_assert ((lp != NULL) == (e != NULL));
    1558      3848417 :       if (lp != NULL)
    1559              :         {
    1560       766596 :           gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
    1561              : 
    1562       766596 :           redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
    1563       766596 :           e->flags |= (CALL_P (BB_END (bb))
    1564       766596 :                        ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
    1565              :                        : EDGE_ABNORMAL);
    1566              :         }
    1567              :     }
    1568              : 
    1569        66410 :   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
    1570              :       /* Kludge for Alpha (see alpha_gp_save_rtx).  */
    1571        66410 :       || single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->insns.r)
    1572            0 :     commit_edge_insertions ();
    1573        66410 : }
    1574              : 
    1575              : /* This section handles removing dead code for flow.  */
    1576              : 
    1577              : void
    1578      1442920 : remove_eh_landing_pad (eh_landing_pad lp)
    1579              : {
    1580      1442920 :   eh_landing_pad *pp;
    1581              : 
    1582     30961547 :   for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
    1583     29518627 :     continue;
    1584      1442920 :   *pp = lp->next_lp;
    1585              : 
    1586      1442920 :   if (lp->post_landing_pad)
    1587      1435657 :     EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
    1588      1442920 :   (*cfun->eh->lp_array)[lp->index] = NULL;
    1589     29518627 : }
    1590              : 
    1591              : /* Splice the EH region at PP from the region tree.  */
    1592              : 
    1593              : static void
    1594      2622521 : remove_eh_handler_splicer (eh_region *pp)
    1595              : {
    1596      2622521 :   eh_region region = *pp;
    1597      2622521 :   eh_landing_pad lp;
    1598              : 
    1599      2782622 :   for (lp = region->landing_pads; lp ; lp = lp->next_lp)
    1600              :     {
    1601       160101 :       if (lp->post_landing_pad)
    1602       160054 :         EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
    1603       160101 :       (*cfun->eh->lp_array)[lp->index] = NULL;
    1604              :     }
    1605              : 
    1606      2622521 :   if (region->inner)
    1607              :     {
    1608       136015 :       eh_region p, outer;
    1609       136015 :       outer = region->outer;
    1610              : 
    1611       136015 :       *pp = p = region->inner;
    1612       161062 :       do
    1613              :         {
    1614       161062 :           p->outer = outer;
    1615       161062 :           pp = &p->next_peer;
    1616       161062 :           p = *pp;
    1617              :         }
    1618       161062 :       while (p);
    1619              :     }
    1620      2622521 :   *pp = region->next_peer;
    1621              : 
    1622      2622521 :   (*cfun->eh->region_array)[region->index] = NULL;
    1623      2622521 : }
    1624              : 
    1625              : /* Splice a single EH region REGION from the region tree.
    1626              : 
    1627              :    To unlink REGION, we need to find the pointer to it with a relatively
    1628              :    expensive search in REGION's outer region.  If you are going to
    1629              :    remove a number of handlers, using remove_unreachable_eh_regions may
    1630              :    be a better option.  */
    1631              : 
    1632              : void
    1633            0 : remove_eh_handler (eh_region region)
    1634              : {
    1635            0 :   eh_region *pp, *pp_start, p, outer;
    1636              : 
    1637            0 :   outer = region->outer;
    1638            0 :   if (outer)
    1639            0 :     pp_start = &outer->inner;
    1640              :   else
    1641            0 :     pp_start = &cfun->eh->region_tree;
    1642            0 :   for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
    1643            0 :     continue;
    1644              : 
    1645            0 :   remove_eh_handler_splicer (pp);
    1646            0 : }
    1647              : 
    1648              : /* Worker for remove_unreachable_eh_regions.
    1649              :    PP is a pointer to the region to start a region tree depth-first
    1650              :    search from.  R_REACHABLE is the set of regions that have to be
    1651              :    preserved.  */
    1652              : 
    1653              : static void
    1654      5854796 : remove_unreachable_eh_regions_worker (eh_region *pp, sbitmap r_reachable)
    1655              : {
    1656     10220825 :   while (*pp)
    1657              :     {
    1658      4366029 :       eh_region region = *pp;
    1659      4366029 :       remove_unreachable_eh_regions_worker (&region->inner, r_reachable);
    1660      4366029 :       if (!bitmap_bit_p (r_reachable, region->index))
    1661      2622521 :         remove_eh_handler_splicer (pp);
    1662              :       else
    1663      1743508 :         pp = &region->next_peer;
    1664              :     }
    1665      5854796 : }
    1666              : 
    1667              : /* Splice all EH regions *not* marked in R_REACHABLE from the region tree.
    1668              :    Do this by traversing the EH tree top-down and splice out regions that
    1669              :    are not marked.  By removing regions from the leaves, we avoid costly
    1670              :    searches in the region tree.  */
    1671              : 
    1672              : void
    1673      1488767 : remove_unreachable_eh_regions (sbitmap r_reachable)
    1674              : {
    1675      1488767 :   remove_unreachable_eh_regions_worker (&cfun->eh->region_tree, r_reachable);
    1676      1488767 : }
    1677              : 
    1678              : /* Invokes CALLBACK for every exception handler landing pad label.
    1679              :    Only used by reload hackery; should not be used by new code.  */
    1680              : 
    1681              : void
    1682            0 : for_each_eh_label (void (*callback) (rtx))
    1683              : {
    1684            0 :   eh_landing_pad lp;
    1685            0 :   int i;
    1686              : 
    1687            0 :   for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
    1688              :     {
    1689            0 :       if (lp)
    1690              :         {
    1691            0 :           rtx_code_label *lab = lp->landing_pad;
    1692            0 :           if (lab && LABEL_P (lab))
    1693            0 :             (*callback) (lab);
    1694              :         }
    1695              :     }
    1696            0 : }
    1697              : 
    1698              : /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
    1699              :    call insn.
    1700              : 
    1701              :    At the gimple level, we use LP_NR
    1702              :        > 0 : The statement transfers to landing pad LP_NR
    1703              :        = 0 : The statement is outside any EH region
    1704              :        < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
    1705              : 
    1706              :    At the rtl level, we use LP_NR
    1707              :        > 0 : The insn transfers to landing pad LP_NR
    1708              :        = 0 : The insn cannot throw
    1709              :        < 0 : The insn is within MUST_NOT_THROW region -LP_NR
    1710              :        = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
    1711              :        missing note: The insn is outside any EH region.
    1712              : 
    1713              :   ??? This difference probably ought to be avoided.  We could stand
    1714              :   to record nothrow for arbitrary gimple statements, and so avoid
    1715              :   some moderately complex lookups in stmt_could_throw_p.  Perhaps
    1716              :   NOTHROW should be mapped on both sides to INT_MIN.  Perhaps the
    1717              :   no-nonlocal-goto property should be recorded elsewhere as a bit
    1718              :   on the call_insn directly.  Perhaps we should make more use of
    1719              :   attaching the trees to call_insns (reachable via symbol_ref in
    1720              :   direct call cases) and just pull the data out of the trees.  */
    1721              : 
    1722              : void
    1723      7049349 : make_reg_eh_region_note (rtx_insn *insn, int ecf_flags, int lp_nr)
    1724              : {
    1725      7049349 :   rtx value;
    1726      7049349 :   if (ecf_flags & ECF_NOTHROW)
    1727      2465478 :     value = const0_rtx;
    1728      4583871 :   else if (lp_nr != 0)
    1729       843594 :     value = GEN_INT (lp_nr);
    1730              :   else
    1731              :     return;
    1732      3309072 :   add_reg_note (insn, REG_EH_REGION, value);
    1733              : }
    1734              : 
    1735              : /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
    1736              :    nor perform a non-local goto.  Replace the region note if it
    1737              :    already exists.  */
    1738              : 
    1739              : void
    1740       507115 : make_reg_eh_region_note_nothrow_nononlocal (rtx_insn *insn)
    1741              : {
    1742       507115 :   rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    1743       507115 :   rtx intmin = GEN_INT (INT_MIN);
    1744              : 
    1745       507115 :   if (note != 0)
    1746       242407 :     XEXP (note, 0) = intmin;
    1747              :   else
    1748       264708 :     add_reg_note (insn, REG_EH_REGION, intmin);
    1749       507115 : }
    1750              : 
    1751              : /* Return true if INSN could throw, assuming no REG_EH_REGION note
    1752              :    to the contrary.  */
    1753              : 
    1754              : bool
    1755   3182787647 : insn_could_throw_p (const_rtx insn)
    1756              : {
    1757   3182787647 :   if (!flag_exceptions)
    1758              :     return false;
    1759   2638102609 :   if (CALL_P (insn))
    1760              :     return true;
    1761   2261206235 :   if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
    1762   1911679923 :     return may_trap_p (PATTERN (insn));
    1763              :   return false;
    1764              : }
    1765              : 
    1766              : /* Copy an REG_EH_REGION note to each insn that might throw beginning
    1767              :    at FIRST and ending at LAST.  NOTE_OR_INSN is either the source insn
    1768              :    to look for a note, or the note itself.  */
    1769              : 
    1770              : void
    1771      2412528 : copy_reg_eh_region_note_forward (rtx note_or_insn, rtx_insn *first, rtx last)
    1772              : {
    1773      2412528 :   rtx_insn *insn;
    1774      2412528 :   rtx note = note_or_insn;
    1775              : 
    1776      2412528 :   if (INSN_P (note_or_insn))
    1777              :     {
    1778      2329095 :       note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
    1779      2329095 :       if (note == NULL)
    1780              :         return;
    1781              :     }
    1782        83433 :   else if (is_a <rtx_insn *> (note_or_insn))
    1783              :     return;
    1784        41785 :   note = XEXP (note, 0);
    1785              : 
    1786        91217 :   for (insn = first; insn != last ; insn = NEXT_INSN (insn))
    1787        49432 :     if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
    1788        49432 :         && insn_could_throw_p (insn))
    1789        12947 :       add_reg_note (insn, REG_EH_REGION, note);
    1790              : }
    1791              : 
    1792              : /* Likewise, but iterate backward.  */
    1793              : 
    1794              : void
    1795         2826 : copy_reg_eh_region_note_backward (rtx note_or_insn, rtx_insn *last, rtx first)
    1796              : {
    1797         2826 :   rtx_insn *insn;
    1798         2826 :   rtx note = note_or_insn;
    1799              : 
    1800         2826 :   if (INSN_P (note_or_insn))
    1801              :     {
    1802            0 :       note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
    1803            0 :       if (note == NULL)
    1804              :         return;
    1805              :     }
    1806         2826 :   else if (is_a <rtx_insn *> (note_or_insn))
    1807              :     return;
    1808         2826 :   note = XEXP (note, 0);
    1809              : 
    1810         8313 :   for (insn = last; insn != first; insn = PREV_INSN (insn))
    1811         5487 :     if (insn_could_throw_p (insn))
    1812         5257 :       add_reg_note (insn, REG_EH_REGION, note);
    1813              : }
    1814              : 
    1815              : 
    1816              : /* Extract all EH information from INSN.  Return true if the insn
    1817              :    was marked NOTHROW.  */
    1818              : 
    1819              : static bool
    1820   3435964214 : get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
    1821              :                                eh_landing_pad *plp)
    1822              : {
    1823   3435964214 :   rtx note;
    1824   3435964214 :   int lp_nr;
    1825              : 
    1826   3435964214 :   *pr = nullptr;
    1827   3435964214 :   *plp = nullptr;
    1828   3435964214 :   if (! INSN_P (insn))
    1829              :     return false;
    1830              : 
    1831   3435411074 :   rtx_sequence *seq = dyn_cast<rtx_sequence *> (PATTERN (insn));
    1832   3435411074 :   if (NONJUMP_INSN_P (insn) && seq)
    1833            0 :     insn = seq->insn (0);
    1834              : 
    1835   3435411074 :   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    1836   3435411074 :   if (!note)
    1837   3179033710 :     return !insn_could_throw_p (insn);
    1838              : 
    1839    256377364 :   lp_nr = INTVAL (XEXP (note, 0));
    1840    256377364 :   if (lp_nr == 0 || lp_nr == INT_MIN)
    1841              :     return true;
    1842              : 
    1843     47098562 :   if (lp_nr < 0)
    1844      4829433 :     *pr = (*cfun->eh->region_array)[-lp_nr];
    1845              :   else
    1846              :     {
    1847     42269129 :       *plp = (*cfun->eh->lp_array)[lp_nr];
    1848     42269129 :       *pr = (*plp)->region;
    1849              :     }
    1850              : 
    1851              :   return false;
    1852              : }
    1853              : 
    1854              : /* Return the landing pad to which INSN may go, or NULL if it does not
    1855              :    have a reachable landing pad within this function.  */
    1856              : 
    1857              : eh_landing_pad
    1858   2198114118 : get_eh_landing_pad_from_rtx (const_rtx insn)
    1859              : {
    1860   2198114118 :   eh_landing_pad lp;
    1861   2198114118 :   eh_region r;
    1862              : 
    1863   2198114118 :   get_eh_region_and_lp_from_rtx (insn, &r, &lp);
    1864   2198114118 :   return lp;
    1865              : }
    1866              : 
    1867              : /* Return the region to which INSN may go, or NULL if it does not
    1868              :    have a reachable region within this function.  */
    1869              : 
    1870              : eh_region
    1871      3973247 : get_eh_region_from_rtx (const_rtx insn)
    1872              : {
    1873      3973247 :   eh_landing_pad lp;
    1874      3973247 :   eh_region r;
    1875              : 
    1876      3973247 :   get_eh_region_and_lp_from_rtx (insn, &r, &lp);
    1877      3973247 :   return r;
    1878              : }
    1879              : 
    1880              : /* Return true if INSN throws and is caught by something in this function.  */
    1881              : 
    1882              : bool
    1883   2159132705 : can_throw_internal (const_rtx insn)
    1884              : {
    1885   2159132705 :   return get_eh_landing_pad_from_rtx (insn) != NULL;
    1886              : }
    1887              : 
    1888              : /* Return true if INSN throws and escapes from the current function.  */
    1889              : 
    1890              : bool
    1891     40803746 : can_throw_external (const_rtx insn)
    1892              : {
    1893     40803746 :   eh_landing_pad lp;
    1894     40803746 :   eh_region r;
    1895     40803746 :   bool nothrow;
    1896              : 
    1897     40803746 :   if (! INSN_P (insn))
    1898              :     return false;
    1899              : 
    1900     12626624 :   rtx_sequence *seq = dyn_cast<rtx_sequence *> (PATTERN (insn));
    1901     12626624 :   if (NONJUMP_INSN_P (insn) && seq)
    1902              :     {
    1903            0 :       int i, n = seq->len ();
    1904              : 
    1905            0 :       for (i = 0; i < n; i++)
    1906            0 :         if (can_throw_external (seq->element (i)))
    1907              :           return true;
    1908              : 
    1909              :       return false;
    1910              :     }
    1911              : 
    1912     12626624 :   nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
    1913              : 
    1914              :   /* If we can't throw, we obviously can't throw external.  */
    1915     12626624 :   if (nothrow)
    1916              :     return false;
    1917              : 
    1918              :   /* If we have an internal landing pad, then we're not external.  */
    1919       652333 :   if (lp != NULL)
    1920              :     return false;
    1921              : 
    1922              :   /* If we're not within an EH region, then we are external.  */
    1923       489470 :   if (r == NULL)
    1924              :     return true;
    1925              : 
    1926              :   /* The only thing that ought to be left is MUST_NOT_THROW regions,
    1927              :      which don't always have landing pads.  */
    1928        17632 :   gcc_assert (r->type == ERT_MUST_NOT_THROW);
    1929              :   return false;
    1930              : }
    1931              : 
    1932              : /* Return true if INSN cannot throw at all.  */
    1933              : 
    1934              : bool
    1935   1207586790 : insn_nothrow_p (const_rtx insn)
    1936              : {
    1937   1207586790 :   eh_landing_pad lp;
    1938   1207586790 :   eh_region r;
    1939              : 
    1940   1207586790 :   if (! INSN_P (insn))
    1941              :     return true;
    1942              : 
    1943   1207586790 :   rtx_sequence *seq = dyn_cast<rtx_sequence *> (PATTERN (insn));
    1944   1207586790 :   if (NONJUMP_INSN_P (insn) && seq)
    1945              :     {
    1946            0 :       int i, n = seq->len ();
    1947              : 
    1948            0 :       for (i = 0; i < n; i++)
    1949            0 :         if (!insn_nothrow_p (seq->element (i)))
    1950              :           return false;
    1951              : 
    1952              :       return true;
    1953              :     }
    1954              : 
    1955   1207586790 :   return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
    1956              : }
    1957              : 
    1958              : /* Return true if INSN can perform a non-local goto.  */
    1959              : /* ??? This test is here in this file because it (ab)uses REG_EH_REGION.  */
    1960              : 
    1961              : bool
    1962    418258731 : can_nonlocal_goto (const rtx_insn *insn)
    1963              : {
    1964    418258731 :   if (nonlocal_goto_handler_labels && CALL_P (insn))
    1965              :     {
    1966        89241 :       rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    1967        89241 :       if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
    1968        87693 :         return true;
    1969              :     }
    1970              :   return false;
    1971              : }
    1972              : 
    1973              : /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls.  */
    1974              : 
    1975              : static unsigned int
    1976      1515922 : set_nothrow_function_flags (void)
    1977              : {
    1978      1515922 :   rtx_insn *insn;
    1979              : 
    1980      1515922 :   crtl->nothrow = 1;
    1981              : 
    1982              :   /* Assume crtl->all_throwers_are_sibcalls until we encounter
    1983              :      something that can throw an exception.  We specifically exempt
    1984              :      CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
    1985              :      and can't throw.  Most CALL_INSNs are not SIBLING_CALL_P, so this
    1986              :      is optimistic.  */
    1987              : 
    1988      1515922 :   crtl->all_throwers_are_sibcalls = 1;
    1989              : 
    1990              :   /* If we don't know that this implementation of the function will
    1991              :      actually be used, then we must not set TREE_NOTHROW, since
    1992              :      callers must not assume that this function does not throw.  */
    1993      1515922 :   if (TREE_NOTHROW (current_function_decl))
    1994              :     return 0;
    1995              : 
    1996       730085 :   if (! flag_exceptions)
    1997              :     return 0;
    1998              : 
    1999     40434756 :   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
    2000     40372134 :     if (can_throw_external (insn))
    2001              :       {
    2002       469830 :         crtl->nothrow = 0;
    2003              : 
    2004       469830 :         if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
    2005              :           {
    2006       449012 :             crtl->all_throwers_are_sibcalls = 0;
    2007       449012 :             return 0;
    2008              :           }
    2009              :       }
    2010              : 
    2011        62622 :   if (crtl->nothrow
    2012        62622 :       && (cgraph_node::get (current_function_decl)->get_availability ()
    2013              :           >= AVAIL_AVAILABLE))
    2014              :     {
    2015        40120 :       struct cgraph_node *node = cgraph_node::get (current_function_decl);
    2016        40120 :       struct cgraph_edge *e;
    2017        52828 :       for (e = node->callers; e; e = e->next_caller)
    2018        12708 :         e->can_throw_external = false;
    2019        40120 :       node->set_nothrow_flag (true);
    2020              : 
    2021        40120 :       if (dump_file)
    2022            5 :         fprintf (dump_file, "Marking function nothrow: %s\n\n",
    2023              :                  current_function_name ());
    2024              :     }
    2025              :   return 0;
    2026              : }
    2027              : 
    2028              : namespace {
    2029              : 
    2030              : const pass_data pass_data_set_nothrow_function_flags =
    2031              : {
    2032              :   RTL_PASS, /* type */
    2033              :   "nothrow", /* name */
    2034              :   OPTGROUP_NONE, /* optinfo_flags */
    2035              :   TV_NONE, /* tv_id */
    2036              :   0, /* properties_required */
    2037              :   0, /* properties_provided */
    2038              :   0, /* properties_destroyed */
    2039              :   0, /* todo_flags_start */
    2040              :   0, /* todo_flags_finish */
    2041              : };
    2042              : 
    2043              : class pass_set_nothrow_function_flags : public rtl_opt_pass
    2044              : {
    2045              : public:
    2046       294536 :   pass_set_nothrow_function_flags (gcc::context *ctxt)
    2047       589072 :     : rtl_opt_pass (pass_data_set_nothrow_function_flags, ctxt)
    2048              :   {}
    2049              : 
    2050              :   /* opt_pass methods: */
    2051      1515922 :   unsigned int execute (function *) final override
    2052              :     {
    2053      1515922 :       return set_nothrow_function_flags ();
    2054              :     }
    2055              : 
    2056              : }; // class pass_set_nothrow_function_flags
    2057              : 
    2058              : } // anon namespace
    2059              : 
    2060              : rtl_opt_pass *
    2061       294536 : make_pass_set_nothrow_function_flags (gcc::context *ctxt)
    2062              : {
    2063       294536 :   return new pass_set_nothrow_function_flags (ctxt);
    2064              : }
    2065              : 
    2066              : 
    2067              : /* Various hooks for unwind library.  */
    2068              : 
    2069              : /* Expand the EH support builtin functions:
    2070              :    __builtin_eh_pointer and __builtin_eh_filter.  */
    2071              : 
    2072              : static eh_region
    2073       327391 : expand_builtin_eh_common (tree region_nr_t)
    2074              : {
    2075       327391 :   HOST_WIDE_INT region_nr;
    2076       327391 :   eh_region region;
    2077              : 
    2078       327391 :   gcc_assert (tree_fits_shwi_p (region_nr_t));
    2079       327391 :   region_nr = tree_to_shwi (region_nr_t);
    2080              : 
    2081       327391 :   region = (*cfun->eh->region_array)[region_nr];
    2082              : 
    2083              :   /* ??? We shouldn't have been able to delete a eh region without
    2084              :      deleting all the code that depended on it.  */
    2085       327391 :   gcc_assert (region != NULL);
    2086              : 
    2087       327391 :   return region;
    2088              : }
    2089              : 
    2090              : /* Expand to the exc_ptr value from the given eh region.  */
    2091              : 
    2092              : rtx
    2093       106012 : expand_builtin_eh_pointer (tree exp)
    2094              : {
    2095       106012 :   eh_region region
    2096       106012 :     = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
    2097       106012 :   if (region->exc_ptr_reg == NULL)
    2098        70119 :     region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
    2099       106012 :   return region->exc_ptr_reg;
    2100              : }
    2101              : 
    2102              : /* Expand to the filter value from the given eh region.  */
    2103              : 
    2104              : rtx
    2105         6187 : expand_builtin_eh_filter (tree exp)
    2106              : {
    2107         6187 :   eh_region region
    2108         6187 :     = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
    2109         6187 :   if (region->filter_reg == NULL)
    2110         4602 :     region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
    2111         6187 :   return region->filter_reg;
    2112              : }
    2113              : 
    2114              : /* Copy the exc_ptr and filter values from one landing pad's registers
    2115              :    to another.  This is used to inline the resx statement.  */
    2116              : 
    2117              : rtx
    2118       107596 : expand_builtin_eh_copy_values (tree exp)
    2119              : {
    2120       107596 :   eh_region dst
    2121       107596 :     = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
    2122       107596 :   eh_region src
    2123       107596 :     = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
    2124       107596 :   scalar_int_mode fmode = targetm.eh_return_filter_mode ();
    2125              : 
    2126       107596 :   if (dst->exc_ptr_reg == NULL)
    2127        78677 :     dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
    2128       107596 :   if (src->exc_ptr_reg == NULL)
    2129        58541 :     src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
    2130              : 
    2131       107596 :   if (dst->filter_reg == NULL)
    2132        78679 :     dst->filter_reg = gen_reg_rtx (fmode);
    2133       107596 :   if (src->filter_reg == NULL)
    2134        57825 :     src->filter_reg = gen_reg_rtx (fmode);
    2135              : 
    2136       107596 :   emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
    2137       107596 :   emit_move_insn (dst->filter_reg, src->filter_reg);
    2138              : 
    2139       107596 :   return const0_rtx;
    2140              : }
    2141              : 
    2142              : /* Do any necessary initialization to access arbitrary stack frames.
    2143              :    On the SPARC, this means flushing the register windows.  */
    2144              : 
    2145              : void
    2146           34 : expand_builtin_unwind_init (void)
    2147              : {
    2148              :   /* Set this so all the registers get saved in our frame; we need to be
    2149              :      able to copy the saved values for any registers from frames we unwind.  */
    2150           34 :   crtl->saves_all_registers = 1;
    2151              : 
    2152           34 :   SETUP_FRAME_ADDRESSES ();
    2153           34 : }
    2154              : 
    2155              : /* Map a non-negative number to an eh return data register number; expands
    2156              :    to -1 if no return data register is associated with the input number.
    2157              :    At least the inputs 0 and 1 must be mapped; the target may provide more.  */
    2158              : 
    2159              : rtx
    2160           21 : expand_builtin_eh_return_data_regno (tree exp)
    2161              : {
    2162           21 :   tree which = CALL_EXPR_ARG (exp, 0);
    2163           21 :   unsigned HOST_WIDE_INT iwhich;
    2164              : 
    2165           21 :   if (TREE_CODE (which) != INTEGER_CST)
    2166              :     {
    2167            0 :       error ("argument of %<__builtin_eh_return_regno%> must be constant");
    2168            0 :       return constm1_rtx;
    2169              :     }
    2170              : 
    2171           21 :   if (!tree_fits_uhwi_p (which))
    2172            1 :     return constm1_rtx;
    2173              : 
    2174           20 :   iwhich = tree_to_uhwi (which);
    2175           20 :   iwhich = EH_RETURN_DATA_REGNO (iwhich);
    2176           20 :   if (iwhich == INVALID_REGNUM)
    2177            0 :     return constm1_rtx;
    2178              : 
    2179              : #ifdef DWARF_FRAME_REGNUM
    2180           20 :   iwhich = DWARF_FRAME_REGNUM (iwhich);
    2181              : #else
    2182              :   iwhich = DEBUGGER_REGNO (iwhich);
    2183              : #endif
    2184              : 
    2185           20 :   return GEN_INT (iwhich);
    2186              : }
    2187              : 
    2188              : /* Given a value extracted from the return address register or stack slot,
    2189              :    return the actual address encoded in that value.  */
    2190              : 
    2191              : rtx
    2192         2352 : expand_builtin_extract_return_addr (tree addr_tree)
    2193              : {
    2194         2668 :   rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
    2195              : 
    2196         2352 :   if (GET_MODE (addr) != Pmode
    2197         2352 :       && GET_MODE (addr) != VOIDmode)
    2198              :     {
    2199              : #ifdef POINTERS_EXTEND_UNSIGNED
    2200            0 :       addr = convert_memory_address (Pmode, addr);
    2201              : #else
    2202              :       addr = convert_to_mode (Pmode, addr, 0);
    2203              : #endif
    2204              :     }
    2205              : 
    2206              :   /* First mask out any unwanted bits.  */
    2207         2352 :   rtx mask = MASK_RETURN_ADDR;
    2208         2352 :   if (mask)
    2209              :     expand_and (Pmode, addr, mask, addr);
    2210              : 
    2211              :   /* Then adjust to find the real return address.  */
    2212         2352 :   if (RETURN_ADDR_OFFSET)
    2213              :     addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
    2214              : 
    2215         2352 :   return addr;
    2216              : }
    2217              : 
    2218              : /* Given an actual address in addr_tree, do any necessary encoding
    2219              :    and return the value to be stored in the return address register or
    2220              :    stack slot so the epilogue will return to that address.  */
    2221              : 
    2222              : rtx
    2223           19 : expand_builtin_frob_return_addr (tree addr_tree)
    2224              : {
    2225           19 :   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
    2226              : 
    2227           27 :   addr = convert_memory_address (Pmode, addr);
    2228              : 
    2229           19 :   if (RETURN_ADDR_OFFSET)
    2230              :     {
    2231              :       addr = force_reg (Pmode, addr);
    2232              :       addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
    2233              :     }
    2234              : 
    2235           19 :   return addr;
    2236              : }
    2237              : 
    2238              : /* Set up the epilogue with the magic bits we'll need to return to the
    2239              :    exception handler.  */
    2240              : 
    2241              : void
    2242           29 : expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
    2243              :                           tree handler_tree)
    2244              : {
    2245           29 :   rtx tmp;
    2246              : 
    2247              : #ifdef EH_RETURN_STACKADJ_RTX
    2248           29 :   tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
    2249              :                      VOIDmode, EXPAND_NORMAL);
    2250           37 :   tmp = convert_memory_address (Pmode, tmp);
    2251           29 :   if (!crtl->eh.ehr_stackadj)
    2252           29 :     crtl->eh.ehr_stackadj = copy_addr_to_reg (tmp);
    2253            0 :   else if (tmp != crtl->eh.ehr_stackadj)
    2254            0 :     emit_move_insn (crtl->eh.ehr_stackadj, tmp);
    2255              : #endif
    2256              : 
    2257           29 :   tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
    2258              :                      VOIDmode, EXPAND_NORMAL);
    2259           37 :   tmp = convert_memory_address (Pmode, tmp);
    2260           29 :   if (!crtl->eh.ehr_handler)
    2261           29 :     crtl->eh.ehr_handler = copy_addr_to_reg (tmp);
    2262            0 :   else if (tmp != crtl->eh.ehr_handler)
    2263            0 :     emit_move_insn (crtl->eh.ehr_handler, tmp);
    2264              : 
    2265           29 :   if (!crtl->eh.ehr_label)
    2266           29 :     crtl->eh.ehr_label = gen_label_rtx ();
    2267           29 :   emit_jump (crtl->eh.ehr_label);
    2268           29 : }
    2269              : 
    2270              : /* Expand __builtin_eh_return.  This exit path from the function loads up
    2271              :    the eh return data registers, adjusts the stack, and branches to a
    2272              :    given PC other than the normal return address.  */
    2273              : 
    2274              : void
    2275      1516700 : expand_eh_return (void)
    2276              : {
    2277      1516700 :   rtx_code_label *around_label;
    2278              : 
    2279      1516700 :   if (! crtl->eh.ehr_label)
    2280              :     return;
    2281              : 
    2282           29 :   crtl->calls_eh_return = 1;
    2283              : 
    2284              : #ifdef EH_RETURN_STACKADJ_RTX
    2285           37 :   emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
    2286              : #endif
    2287              : 
    2288              : #ifdef EH_RETURN_TAKEN_RTX
    2289              :   emit_move_insn (EH_RETURN_TAKEN_RTX, const0_rtx);
    2290              : #endif
    2291              : 
    2292           29 :   around_label = gen_label_rtx ();
    2293           29 :   emit_jump (around_label);
    2294              : 
    2295           29 :   emit_label (crtl->eh.ehr_label);
    2296           29 :   clobber_return_register ();
    2297              : 
    2298              : #ifdef EH_RETURN_STACKADJ_RTX
    2299           37 :   emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
    2300              : #endif
    2301              : 
    2302              : #ifdef EH_RETURN_TAKEN_RTX
    2303              :   emit_move_insn (EH_RETURN_TAKEN_RTX, const1_rtx);
    2304              : #endif
    2305              : 
    2306           29 :   if (targetm.have_eh_return ())
    2307           29 :     emit_insn (targetm.gen_eh_return (crtl->eh.ehr_handler));
    2308              :   else
    2309              :     {
    2310            0 :       if (rtx handler = EH_RETURN_HANDLER_RTX)
    2311              :         emit_move_insn (handler, crtl->eh.ehr_handler);
    2312              :       else
    2313            0 :         error ("%<__builtin_eh_return%> not supported on this target");
    2314              :     }
    2315              : 
    2316              : #ifdef EH_RETURN_TAKEN_RTX
    2317              :   rtx_code_label *eh_done_label = gen_label_rtx ();
    2318              :   emit_jump (eh_done_label);
    2319              : #endif
    2320              : 
    2321           29 :   emit_label (around_label);
    2322              : 
    2323              : #ifdef EH_RETURN_TAKEN_RTX
    2324              :   for (rtx tmp : { EH_RETURN_STACKADJ_RTX, EH_RETURN_HANDLER_RTX })
    2325              :     if (tmp && REG_P (tmp))
    2326              :       emit_clobber (tmp);
    2327              :   emit_label (eh_done_label);
    2328              : #endif
    2329              : }
    2330              : 
    2331              : /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
    2332              :    POINTERS_EXTEND_UNSIGNED and return it.  */
    2333              : 
    2334              : rtx
    2335            2 : expand_builtin_extend_pointer (tree addr_tree)
    2336              : {
    2337            2 :   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
    2338            2 :   int extend;
    2339              : 
    2340              : #ifdef POINTERS_EXTEND_UNSIGNED
    2341            2 :   extend = POINTERS_EXTEND_UNSIGNED;
    2342              : #else
    2343              :   /* The previous EH code did an unsigned extend by default, so we do this also
    2344              :      for consistency.  */
    2345              :   extend = 1;
    2346              : #endif
    2347              : 
    2348            2 :   return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
    2349              : }
    2350              : 
    2351              : static int
    2352       379554 : add_action_record (action_hash_type *ar_hash, int filter, int next)
    2353              : {
    2354       379554 :   struct action_record **slot, *new_ar, tmp;
    2355              : 
    2356       379554 :   tmp.filter = filter;
    2357       379554 :   tmp.next = next;
    2358       379554 :   slot = ar_hash->find_slot (&tmp, INSERT);
    2359              : 
    2360       379554 :   if ((new_ar = *slot) == NULL)
    2361              :     {
    2362        27570 :       new_ar = XNEW (struct action_record);
    2363        27570 :       new_ar->offset = crtl->eh.action_record_data->length () + 1;
    2364        27570 :       new_ar->filter = filter;
    2365        27570 :       new_ar->next = next;
    2366        27570 :       *slot = new_ar;
    2367              : 
    2368              :       /* The filter value goes in untouched.  The link to the next
    2369              :          record is a "self-relative" byte offset, or zero to indicate
    2370              :          that there is no next record.  So convert the absolute 1 based
    2371              :          indices we've been carrying around into a displacement.  */
    2372              : 
    2373        27570 :       push_sleb128 (&crtl->eh.action_record_data, filter);
    2374        27570 :       if (next)
    2375         5644 :         next -= crtl->eh.action_record_data->length () + 1;
    2376        27570 :       push_sleb128 (&crtl->eh.action_record_data, next);
    2377              :     }
    2378              : 
    2379       379554 :   return new_ar->offset;
    2380              : }
    2381              : 
    2382              : static int
    2383     25638705 : collect_one_action_chain (action_hash_type *ar_hash, eh_region region)
    2384              : {
    2385     25638705 :   int next;
    2386              : 
    2387              :   /* If we've reached the top of the region chain, then we have
    2388              :      no actions, and require no landing pad.  */
    2389     25638705 :   if (region == NULL)
    2390              :     return -1;
    2391              : 
    2392     25277441 :   switch (region->type)
    2393              :     {
    2394     24867361 :     case ERT_CLEANUP:
    2395     24867361 :       {
    2396     24867361 :         eh_region r;
    2397              :         /* A cleanup adds a zero filter to the beginning of the chain, but
    2398              :            there are special cases to look out for.  If there are *only*
    2399              :            cleanups along a path, then it compresses to a zero action.
    2400              :            Further, if there are multiple cleanups along a path, we only
    2401              :            need to represent one of them, as that is enough to trigger
    2402              :            entry to the landing pad at runtime.  */
    2403     24867361 :         next = collect_one_action_chain (ar_hash, region->outer);
    2404     24867361 :         if (next <= 0)
    2405              :           return 0;
    2406        76654 :         for (r = region->outer; r ; r = r->outer)
    2407        63359 :           if (r->type == ERT_CLEANUP)
    2408              :             return next;
    2409        13295 :         return add_action_record (ar_hash, 0, next);
    2410              :       }
    2411              : 
    2412       352675 :     case ERT_TRY:
    2413       352675 :       {
    2414       352675 :         eh_catch c;
    2415              : 
    2416              :         /* Process the associated catch regions in reverse order.
    2417              :            If there's a catch-all handler, then we don't need to
    2418              :            search outer regions.  Use a magic -3 value to record
    2419              :            that we haven't done the outer search.  */
    2420       352675 :         next = -3;
    2421       713113 :         for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
    2422              :           {
    2423       360438 :             if (c->type_list == NULL)
    2424              :               {
    2425              :                 /* Retrieve the filter from the head of the filter list
    2426              :                    where we have stored it (see assign_filter_values).  */
    2427       337617 :                 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
    2428       337617 :                 next = add_action_record (ar_hash, filter, 0);
    2429              :               }
    2430              :             else
    2431              :               {
    2432              :                 /* Once the outer search is done, trigger an action record for
    2433              :                    each filter we have.  */
    2434        22821 :                 tree flt_node;
    2435              : 
    2436        22821 :                 if (next == -3)
    2437              :                   {
    2438        15058 :                     next = collect_one_action_chain (ar_hash, region->outer);
    2439              : 
    2440              :                     /* If there is no next action, terminate the chain.  */
    2441        15058 :                     if (next == -1)
    2442              :                       next = 0;
    2443              :                     /* If all outer actions are cleanups or must_not_throw,
    2444              :                        we'll have no action record for it, since we had wanted
    2445              :                        to encode these states in the call-site record directly.
    2446              :                        Add a cleanup action to the chain to catch these.  */
    2447        10175 :                     else if (next <= 0)
    2448         5294 :                       next = add_action_record (ar_hash, 0, 0);
    2449              :                   }
    2450              : 
    2451        22821 :                 flt_node = c->filter_list;
    2452        45642 :                 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
    2453              :                   {
    2454        22821 :                     int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
    2455        22821 :                     next = add_action_record (ar_hash, filter, next);
    2456              :                   }
    2457              :               }
    2458              :           }
    2459              :         return next;
    2460              :       }
    2461              : 
    2462          495 :     case ERT_ALLOWED_EXCEPTIONS:
    2463              :       /* An exception specification adds its filter to the
    2464              :          beginning of the chain.  */
    2465          495 :       next = collect_one_action_chain (ar_hash, region->outer);
    2466              : 
    2467              :       /* If there is no next action, terminate the chain.  */
    2468          495 :       if (next == -1)
    2469              :         next = 0;
    2470              :       /* If all outer actions are cleanups or must_not_throw,
    2471              :          we'll have no action record for it, since we had wanted
    2472              :          to encode these states in the call-site record directly.
    2473              :          Add a cleanup action to the chain to catch these.  */
    2474           41 :       else if (next <= 0)
    2475           32 :         next = add_action_record (ar_hash, 0, 0);
    2476              : 
    2477          495 :       return add_action_record (ar_hash, region->u.allowed.filter, next);
    2478              : 
    2479              :     case ERT_MUST_NOT_THROW:
    2480              :       /* A must-not-throw region with no inner handlers or cleanups
    2481              :          requires no call-site entry.  Note that this differs from
    2482              :          the no handler or cleanup case in that we do require an lsda
    2483              :          to be generated.  Return a magic -2 value to record this.  */
    2484              :       return -2;
    2485              :     }
    2486              : 
    2487            0 :   gcc_unreachable ();
    2488              : }
    2489              : 
    2490              : static int
    2491       443213 : add_call_site (rtx landing_pad, int action, int section)
    2492              : {
    2493       443213 :   call_site_record record;
    2494              : 
    2495       443213 :   record = ggc_alloc<call_site_record_d> ();
    2496       443213 :   record->landing_pad = landing_pad;
    2497       443213 :   record->action = action;
    2498              : 
    2499       443213 :   vec_safe_push (crtl->eh.call_site_record_v[section], record);
    2500              : 
    2501       443213 :   return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
    2502              : }
    2503              : 
    2504              : static rtx_note *
    2505       443213 : emit_note_eh_region_end (rtx_insn *insn)
    2506              : {
    2507            0 :   return emit_note_after (NOTE_INSN_EH_REGION_END, insn);
    2508              : }
    2509              : 
    2510              : /* Add NOP after NOTE_INSN_SWITCH_TEXT_SECTIONS when the cold section starts
    2511              :    with landing pad.
    2512              :    With landing pad being at offset 0 from the start label of the section
    2513              :    we would miss EH delivery because 0 is special and means no landing pad.  */
    2514              : 
    2515              : static bool
    2516        66387 : maybe_add_nop_after_section_switch (void)
    2517              : {
    2518        66387 :   if (!crtl->uses_eh_lsda
    2519        66321 :       || !crtl->eh.call_site_record_v[1])
    2520              :     return false;
    2521        14830 :   int n = vec_safe_length (crtl->eh.call_site_record_v[1]);
    2522        14830 :   hash_set<rtx_insn *> visited;
    2523              : 
    2524        72716 :   for (int i = 0; i < n; ++i)
    2525              :     {
    2526        43056 :       struct call_site_record_d *cs
    2527        43056 :          = (*crtl->eh.call_site_record_v[1])[i];
    2528        43056 :       if (cs->landing_pad)
    2529              :         {
    2530        23352 :           rtx_insn *insn = as_a <rtx_insn *> (cs->landing_pad);
    2531       393086 :           while (true)
    2532              :             {
    2533              :               /* Landing pads have LABEL_PRESERVE_P flag set.  This check make
    2534              :                  sure that we do not walk past landing pad visited earlier
    2535              :                  which would result in possible quadratic behaviour.  */
    2536        23352 :               if (LABEL_P (insn) && LABEL_PRESERVE_P (insn)
    2537       231571 :                   && visited.add (insn))
    2538              :                 break;
    2539              : 
    2540              :               /* Conservatively assume that ASM insn may be empty.  We have
    2541              :                  now way to tell what they contain.  */
    2542       205327 :               if (active_insn_p (insn)
    2543        20101 :                   && GET_CODE (PATTERN (insn)) != ASM_INPUT
    2544       225428 :                   && GET_CODE (PATTERN (insn)) != ASM_OPERANDS)
    2545              :                 break;
    2546              : 
    2547              :               /* If we reached the start of hot section, then NOP will be
    2548              :                  needed.  */
    2549       185226 :               if (GET_CODE (insn) == NOTE
    2550       144665 :                   && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
    2551              :                 {
    2552          359 :                   emit_insn_after (gen_nop (), insn);
    2553          359 :                   break;
    2554              :                 }
    2555              : 
    2556              :               /* We visit only labels from cold section.  We should never hit
    2557              :                  beginning of the insn stream here.  */
    2558       184867 :               insn = PREV_INSN (insn);
    2559       184867 :             }
    2560              :         }
    2561              :     }
    2562        14830 :   return false;
    2563        14830 : }
    2564              : 
    2565              : /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
    2566              :    The new note numbers will not refer to region numbers, but
    2567              :    instead to call site entries.  */
    2568              : 
    2569              : static unsigned int
    2570        66387 : convert_to_eh_region_ranges (void)
    2571              : {
    2572        66387 :   rtx insn;
    2573        66387 :   rtx_insn *iter;
    2574        66387 :   rtx_note *note;
    2575        66387 :   action_hash_type ar_hash (31);
    2576        66387 :   int last_action = -3;
    2577        66387 :   rtx_insn *last_action_insn = NULL;
    2578        66387 :   rtx last_landing_pad = NULL_RTX;
    2579        66387 :   rtx_insn *first_no_action_insn = NULL;
    2580        66387 :   int call_site = 0;
    2581        66387 :   int cur_sec = 0;
    2582        66387 :   rtx_insn *section_switch_note = NULL;
    2583        66387 :   rtx_insn *first_no_action_insn_before_switch = NULL;
    2584        66387 :   rtx_insn *last_no_action_insn_before_switch = NULL;
    2585        66387 :   int saved_call_site_base = call_site_base;
    2586              : 
    2587        66387 :   vec_alloc (crtl->eh.action_record_data, 64);
    2588              : 
    2589     64002156 :   for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
    2590     63935769 :     if (INSN_P (iter))
    2591              :       {
    2592     13663435 :         eh_landing_pad lp;
    2593     13663435 :         eh_region region;
    2594     13663435 :         bool nothrow;
    2595     13663435 :         int this_action;
    2596     13663435 :         rtx_code_label *this_landing_pad;
    2597              : 
    2598     13663435 :         insn = iter;
    2599     13663435 :         rtx_sequence *seq = dyn_cast<rtx_sequence *> (PATTERN (insn));
    2600     13663435 :         if (NONJUMP_INSN_P (insn) && seq)
    2601            0 :           insn = seq->element (0);
    2602              : 
    2603     13663435 :         nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
    2604     13663435 :         if (nothrow)
    2605     12638085 :           continue;
    2606      1025350 :         if (region)
    2607       755791 :           this_action = collect_one_action_chain (&ar_hash, region);
    2608              :         else
    2609              :           this_action = -1;
    2610              : 
    2611              :         /* Existence of catch handlers, or must-not-throw regions
    2612              :            implies that an lsda is needed (even if empty).  */
    2613       755791 :         if (this_action != -1)
    2614       755791 :           crtl->uses_eh_lsda = 1;
    2615              : 
    2616              :         /* Delay creation of region notes for no-action regions
    2617              :            until we're sure that an lsda will be required.  */
    2618       269559 :         else if (last_action == -3)
    2619              :           {
    2620        29710 :             first_no_action_insn = iter;
    2621        29710 :             last_action = -1;
    2622              :           }
    2623              : 
    2624      1025350 :         if (this_action >= 0)
    2625       700930 :           this_landing_pad = lp->landing_pad;
    2626              :         else
    2627              :           this_landing_pad = NULL;
    2628              : 
    2629              :         /* Differing actions or landing pads implies a change in call-site
    2630              :            info, which implies some EH_REGION note should be emitted.  */
    2631      1025350 :         if (last_action != this_action
    2632      1025350 :             || last_landing_pad != this_landing_pad)
    2633              :           {
    2634              :             /* If there is a queued no-action region in the other section
    2635              :                with hot/cold partitioning, emit it now.  */
    2636       446068 :             if (first_no_action_insn_before_switch)
    2637              :               {
    2638          634 :                 gcc_assert (this_action != -1
    2639              :                             && last_action == (first_no_action_insn
    2640              :                                                ? -1 : -3));
    2641          386 :                 call_site = add_call_site (NULL_RTX, 0, 0);
    2642          386 :                 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
    2643              :                                          first_no_action_insn_before_switch);
    2644          386 :                 NOTE_EH_HANDLER (note) = call_site;
    2645          386 :                 note
    2646          386 :                   = emit_note_eh_region_end (last_no_action_insn_before_switch);
    2647          386 :                 NOTE_EH_HANDLER (note) = call_site;
    2648          386 :                 gcc_assert (last_action != -3
    2649              :                             || (last_action_insn
    2650              :                                 == last_no_action_insn_before_switch));
    2651          386 :                 first_no_action_insn_before_switch = NULL;
    2652          386 :                 last_no_action_insn_before_switch = NULL;
    2653          386 :                 call_site_base++;
    2654              :               }
    2655              :             /* If we'd not seen a previous action (-3) or the previous
    2656              :                action was must-not-throw (-2), then we do not need an
    2657              :                end note.  */
    2658       446068 :             if (last_action >= -1)
    2659              :               {
    2660              :                 /* If we delayed the creation of the begin, do it now.  */
    2661       386737 :                 if (first_no_action_insn)
    2662              :                   {
    2663        29284 :                     call_site = add_call_site (NULL_RTX, 0, cur_sec);
    2664        29284 :                     note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
    2665              :                                              first_no_action_insn);
    2666        29284 :                     NOTE_EH_HANDLER (note) = call_site;
    2667        29284 :                     first_no_action_insn = NULL;
    2668              :                   }
    2669              : 
    2670       386737 :                 note = emit_note_eh_region_end (last_action_insn);
    2671       386737 :                 NOTE_EH_HANDLER (note) = call_site;
    2672              :               }
    2673              : 
    2674              :             /* If the new action is must-not-throw, then no region notes
    2675              :                are created.  */
    2676       446068 :             if (this_action >= -1)
    2677              :               {
    2678       413543 :                 call_site = add_call_site (this_landing_pad,
    2679              :                                            this_action < 0 ? 0 : this_action,
    2680              :                                            cur_sec);
    2681       413543 :                 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
    2682       413543 :                 NOTE_EH_HANDLER (note) = call_site;
    2683              :               }
    2684              : 
    2685              :             last_action = this_action;
    2686              :             last_landing_pad = this_landing_pad;
    2687              :           }
    2688      1025350 :         last_action_insn = iter;
    2689              :       }
    2690     50272334 :     else if (NOTE_P (iter)
    2691     47733003 :              && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
    2692              :       {
    2693        14842 :         gcc_assert (section_switch_note == NULL_RTX);
    2694        14842 :         gcc_assert (flag_reorder_blocks_and_partition);
    2695        14842 :         section_switch_note = iter;
    2696        14842 :         if (first_no_action_insn)
    2697              :           {
    2698          386 :             first_no_action_insn_before_switch = first_no_action_insn;
    2699          386 :             last_no_action_insn_before_switch = last_action_insn;
    2700          386 :             first_no_action_insn = NULL;
    2701          386 :             gcc_assert (last_action == -1);
    2702              :             last_action = -3;
    2703              :           }
    2704              :         /* Force closing of current EH region before section switch and
    2705              :            opening a new one afterwards.  */
    2706        14456 :         else if (last_action != -3)
    2707        13684 :           last_landing_pad = pc_rtx;
    2708        14842 :         if (crtl->eh.call_site_record_v[cur_sec])
    2709        13573 :           call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
    2710        14842 :         cur_sec++;
    2711        14842 :         gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
    2712        14842 :         vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
    2713              :       }
    2714              : 
    2715        66387 :   if (last_action >= -1 && ! first_no_action_insn)
    2716              :     {
    2717        56090 :       note = emit_note_eh_region_end (last_action_insn);
    2718        56090 :       NOTE_EH_HANDLER (note) = call_site;
    2719              :     }
    2720              : 
    2721        66387 :   call_site_base = saved_call_site_base;
    2722              : 
    2723       132774 :   return 0;
    2724        66387 : }
    2725              : 
    2726              : namespace {
    2727              : 
    2728              : const pass_data pass_data_convert_to_eh_region_ranges =
    2729              : {
    2730              :   RTL_PASS, /* type */
    2731              :   "eh_ranges", /* name */
    2732              :   OPTGROUP_NONE, /* optinfo_flags */
    2733              :   TV_NONE, /* tv_id */
    2734              :   0, /* properties_required */
    2735              :   0, /* properties_provided */
    2736              :   0, /* properties_destroyed */
    2737              :   0, /* todo_flags_start */
    2738              :   0, /* todo_flags_finish */
    2739              : };
    2740              : 
    2741              : class pass_convert_to_eh_region_ranges : public rtl_opt_pass
    2742              : {
    2743              : public:
    2744       294536 :   pass_convert_to_eh_region_ranges (gcc::context *ctxt)
    2745       589072 :     : rtl_opt_pass (pass_data_convert_to_eh_region_ranges, ctxt)
    2746              :   {}
    2747              : 
    2748              :   /* opt_pass methods: */
    2749              :   bool gate (function *) final override;
    2750        66387 :   unsigned int execute (function *) final override
    2751              :     {
    2752        66387 :       int ret = convert_to_eh_region_ranges ();
    2753        66387 :       maybe_add_nop_after_section_switch ();
    2754        66387 :       return ret;
    2755              :     }
    2756              : 
    2757              : }; // class pass_convert_to_eh_region_ranges
    2758              : 
    2759              : bool
    2760      1515929 : pass_convert_to_eh_region_ranges::gate (function *)
    2761              : {
    2762              :   /* Nothing to do for SJLJ exceptions or if no regions created.  */
    2763      1515929 :   if (cfun->eh->region_tree == NULL)
    2764              :     return false;
    2765        66387 :   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
    2766            0 :     return false;
    2767              :   return true;
    2768              : }
    2769              : 
    2770              : } // anon namespace
    2771              : 
    2772              : rtl_opt_pass *
    2773       294536 : make_pass_convert_to_eh_region_ranges (gcc::context *ctxt)
    2774              : {
    2775       294536 :   return new pass_convert_to_eh_region_ranges (ctxt);
    2776              : }
    2777              : 
    2778              : static void
    2779          128 : push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
    2780              : {
    2781          128 :   do
    2782              :     {
    2783          128 :       unsigned char byte = value & 0x7f;
    2784          128 :       value >>= 7;
    2785          128 :       if (value)
    2786            0 :         byte |= 0x80;
    2787          128 :       vec_safe_push (*data_area, byte);
    2788              :     }
    2789          128 :   while (value);
    2790          128 : }
    2791              : 
    2792              : static void
    2793        55140 : push_sleb128 (vec<uchar, va_gc> **data_area, int value)
    2794              : {
    2795        55140 :   unsigned char byte;
    2796        55140 :   int more;
    2797              : 
    2798        55140 :   do
    2799              :     {
    2800        55140 :       byte = value & 0x7f;
    2801        55140 :       value >>= 7;
    2802        55140 :       more = ! ((value == 0 && (byte & 0x40) == 0)
    2803         5880 :                 || (value == -1 && (byte & 0x40) != 0));
    2804              :       if (more)
    2805            0 :         byte |= 0x80;
    2806        55140 :       vec_safe_push (*data_area, byte);
    2807              :     }
    2808        55140 :   while (more);
    2809        55140 : }
    2810              : 
    2811              : 
    2812              : static int
    2813            0 : dw2_size_of_call_site_table (int section)
    2814              : {
    2815            0 :   int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
    2816            0 :   int size = n * (4 + 4 + 4);
    2817            0 :   int i;
    2818              : 
    2819            0 :   for (i = 0; i < n; ++i)
    2820              :     {
    2821            0 :       struct call_site_record_d *cs =
    2822            0 :         (*crtl->eh.call_site_record_v[section])[i];
    2823            0 :       size += size_of_uleb128 (cs->action);
    2824              :     }
    2825              : 
    2826            0 :   return size;
    2827              : }
    2828              : 
    2829              : static int
    2830            0 : sjlj_size_of_call_site_table (void)
    2831              : {
    2832            0 :   int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
    2833            0 :   int size = 0;
    2834            0 :   int i;
    2835              : 
    2836            0 :   for (i = 0; i < n; ++i)
    2837              :     {
    2838            0 :       struct call_site_record_d *cs =
    2839            0 :         (*crtl->eh.call_site_record_v[0])[i];
    2840            0 :       size += size_of_uleb128 (INTVAL (cs->landing_pad));
    2841            0 :       size += size_of_uleb128 (cs->action);
    2842              :     }
    2843              : 
    2844            0 :   return size;
    2845              : }
    2846              : 
    2847              : static void
    2848        81151 : dw2_output_call_site_table (int cs_format, int section)
    2849              : {
    2850        81151 :   int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
    2851        81151 :   int i;
    2852        81151 :   const char *begin;
    2853              : 
    2854        81151 :   if (section == 0)
    2855        66321 :     begin = current_function_func_begin_label;
    2856        14830 :   else if (first_function_block_is_cold)
    2857            0 :     begin = crtl->subsections.hot_section_label;
    2858              :   else
    2859        14830 :     begin = crtl->subsections.cold_section_label;
    2860              : 
    2861       524364 :   for (i = 0; i < n; ++i)
    2862              :     {
    2863       443213 :       struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
    2864       443213 :       char reg_start_lab[32];
    2865       443213 :       char reg_end_lab[32];
    2866       443213 :       char landing_pad_lab[32];
    2867              : 
    2868       443213 :       ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
    2869       443213 :       ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
    2870              : 
    2871       443213 :       if (cs->landing_pad)
    2872       294570 :         ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
    2873              :                                      CODE_LABEL_NUMBER (cs->landing_pad));
    2874              : 
    2875              :       /* ??? Perhaps use insn length scaling if the assembler supports
    2876              :          generic arithmetic.  */
    2877              :       /* ??? Perhaps use attr_length to choose data1 or data2 instead of
    2878              :          data4 if the function is small enough.  */
    2879       443213 :       if (cs_format == DW_EH_PE_uleb128)
    2880              :         {
    2881       443213 :           dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
    2882              :                                         "region %d start", i);
    2883       443213 :           dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
    2884              :                                         "length");
    2885       443213 :           if (cs->landing_pad)
    2886       294570 :             dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
    2887              :                                           "landing pad");
    2888              :           else
    2889       148643 :             dw2_asm_output_data_uleb128 (0, "landing pad");
    2890              :         }
    2891              :       else
    2892              :         {
    2893            0 :           dw2_asm_output_delta (4, reg_start_lab, begin,
    2894              :                                 "region %d start", i);
    2895            0 :           dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
    2896            0 :           if (cs->landing_pad)
    2897            0 :             dw2_asm_output_delta (4, landing_pad_lab, begin,
    2898              :                                   "landing pad");
    2899              :           else
    2900            0 :             dw2_asm_output_data (4, 0, "landing pad");
    2901              :         }
    2902       443213 :       dw2_asm_output_data_uleb128 (cs->action, "action");
    2903              :     }
    2904              : 
    2905        81151 :   call_site_base += n;
    2906        81151 : }
    2907              : 
    2908              : static void
    2909            0 : sjlj_output_call_site_table (void)
    2910              : {
    2911            0 :   int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
    2912            0 :   int i;
    2913              : 
    2914            0 :   for (i = 0; i < n; ++i)
    2915              :     {
    2916            0 :       struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
    2917              : 
    2918            0 :       dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
    2919              :                                    "region %d landing pad", i);
    2920            0 :       dw2_asm_output_data_uleb128 (cs->action, "action");
    2921              :     }
    2922              : 
    2923            0 :   call_site_base += n;
    2924            0 : }
    2925              : 
    2926              : /* Switch to the section that should be used for exception tables.  */
    2927              : 
    2928              : static void
    2929        81151 : switch_to_exception_section (const char * ARG_UNUSED (fnname))
    2930              : {
    2931        81151 :   section *s;
    2932              : 
    2933        81151 :   if (exception_section
    2934              :   /* Don't use the cached section for comdat if it will be different. */
    2935              : #ifdef HAVE_LD_EH_GC_SECTIONS
    2936        96806 :       && !(targetm_common.have_named_sections
    2937        15655 :            && DECL_COMDAT_GROUP (current_function_decl)
    2938              :            && HAVE_COMDAT_GROUP)
    2939              : #endif
    2940              :      )
    2941        14424 :     s = exception_section;
    2942              :   else
    2943              :     {
    2944        66727 :       int flags;
    2945              : 
    2946        66727 :       if (EH_TABLES_CAN_BE_READ_ONLY)
    2947              :         {
    2948        66727 :           int tt_format =
    2949        66727 :             ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
    2950       133454 :           flags = ((! flag_pic
    2951         8109 :                     || ((tt_format & 0x70) != DW_EH_PE_absptr
    2952         8109 :                         && (tt_format & 0x70) != DW_EH_PE_aligned))
    2953        66727 :                    ? 0 : SECTION_WRITE);
    2954              :         }
    2955              :       else
    2956              :         flags = SECTION_WRITE;
    2957              : 
    2958              :       /* Compute the section and cache it into exception_section,
    2959              :          unless it depends on the function name.  */
    2960        66727 :       if (targetm_common.have_named_sections)
    2961              :         {
    2962              : #ifdef HAVE_LD_EH_GC_SECTIONS
    2963        66727 :           if (flag_function_sections
    2964        66727 :               || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
    2965              :             {
    2966        57011 :               char *section_name = XNEWVEC (char, strlen (fnname) + 32);
    2967              :               /* The EH table must match the code section, so only mark
    2968              :                  it linkonce if we have COMDAT groups to tie them together.  */
    2969        57011 :               if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
    2970        31726 :                 flags |= SECTION_LINKONCE;
    2971        57011 :               sprintf (section_name, ".gcc_except_table.%s", fnname);
    2972        57011 :               s = get_section (section_name, flags, current_function_decl);
    2973        57011 :               free (section_name);
    2974              :             }
    2975              :           else
    2976              : #endif
    2977         9716 :             exception_section
    2978         9716 :               = s = get_section (".gcc_except_table", flags, NULL);
    2979              :         }
    2980              :       else
    2981            0 :         exception_section
    2982            0 :           = s = flags == SECTION_WRITE ? data_section : readonly_data_section;
    2983              :     }
    2984              : 
    2985        81151 :   switch_to_section (s);
    2986        81151 : }
    2987              : 
    2988              : /* Output a reference from an exception table to the type_info object TYPE.
    2989              :    TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
    2990              :    the value.  */
    2991              : 
    2992              : static void
    2993        28170 : output_ttype (tree type, int tt_format, int tt_format_size)
    2994              : {
    2995        28170 :   rtx value;
    2996        28170 :   bool is_public = true;
    2997              : 
    2998        28170 :   if (type == NULL_TREE)
    2999        21770 :     value = const0_rtx;
    3000              :   else
    3001              :     {
    3002              :       /* FIXME lto.  pass_ipa_free_lang_data changes all types to
    3003              :          runtime types so TYPE should already be a runtime type
    3004              :          reference.  When pass_ipa_free_lang data is made a default
    3005              :          pass, we can then remove the call to lookup_type_for_runtime
    3006              :          below.  */
    3007         6400 :       if (TYPE_P (type))
    3008         6100 :         type = lookup_type_for_runtime (type);
    3009              : 
    3010         6400 :       value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
    3011              : 
    3012              :       /* Let cgraph know that the rtti decl is used.  Not all of the
    3013              :          paths below go through assemble_integer, which would take
    3014              :          care of this for us.  */
    3015         6400 :       STRIP_NOPS (type);
    3016         6400 :       if (TREE_CODE (type) == ADDR_EXPR)
    3017              :         {
    3018         6400 :           type = TREE_OPERAND (type, 0);
    3019         6400 :           if (VAR_P (type))
    3020         6400 :             is_public = TREE_PUBLIC (type);
    3021              :         }
    3022              :       else
    3023            0 :         gcc_assert (TREE_CODE (type) == INTEGER_CST);
    3024              :     }
    3025              : 
    3026              :   /* Allow the target to override the type table entry format.  */
    3027        28170 :   if (targetm.asm_out.ttype (value))
    3028              :     return;
    3029              : 
    3030        28170 :   if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
    3031         3061 :     assemble_integer (value, tt_format_size,
    3032         3061 :                       tt_format_size * BITS_PER_UNIT, 1);
    3033              :   else
    3034        25109 :     dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
    3035              : }
    3036              : 
    3037              : /* Output an exception table for the current function according to SECTION.
    3038              : 
    3039              :    If the function has been partitioned into hot and cold parts, value 0 for
    3040              :    SECTION refers to the table associated with the hot part while value 1
    3041              :    refers to the table associated with the cold part.  If the function has
    3042              :    not been partitioned, value 0 refers to the single exception table.  */
    3043              : 
    3044              : static void
    3045        81151 : output_one_function_exception_table (int section)
    3046              : {
    3047        81151 :   int tt_format, cs_format, lp_format, i;
    3048        81151 :   char ttype_label[32];
    3049        81151 :   char cs_after_size_label[32];
    3050        81151 :   char cs_end_label[32];
    3051        81151 :   int call_site_len;
    3052        81151 :   int have_tt_data;
    3053        81151 :   int tt_format_size = 0;
    3054              : 
    3055        81151 :   have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
    3056        80860 :                   || (targetm.arm_eabi_unwinder
    3057        55942 :                       ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
    3058       111593 :                       : vec_safe_length (cfun->eh->ehspec_data.other)));
    3059              : 
    3060              :   /* Indicate the format of the @TType entries.  */
    3061              :   if (! have_tt_data)
    3062              :     tt_format = DW_EH_PE_omit;
    3063              :   else
    3064              :     {
    3065        25452 :       tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
    3066        25452 :       if (HAVE_AS_LEB128)
    3067        47300 :         ASM_GENERATE_INTERNAL_LABEL (ttype_label,
    3068              :                                      section ? "LLSDATTC" : "LLSDATT",
    3069              :                                      current_function_funcdef_no);
    3070              : 
    3071        25452 :       tt_format_size = size_of_encoded_value (tt_format);
    3072              : 
    3073        25452 :       assemble_align (tt_format_size * BITS_PER_UNIT);
    3074              :     }
    3075              : 
    3076        81151 :   targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
    3077        81151 :                                   current_function_funcdef_no);
    3078              : 
    3079              :   /* The LSDA header.  */
    3080              : 
    3081              :   /* Indicate the format of the landing pad start pointer.  An omitted
    3082              :      field implies @LPStart == @Start.  */
    3083              :   /* Currently we always put @LPStart == @Start.  This field would
    3084              :      be most useful in moving the landing pads completely out of
    3085              :      line to another section, but it could also be used to minimize
    3086              :      the size of uleb128 landing pad offsets.  */
    3087        81151 :   lp_format = DW_EH_PE_omit;
    3088        81151 :   dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
    3089              :                        eh_data_format_name (lp_format));
    3090              : 
    3091              :   /* @LPStart pointer would go here.  */
    3092              : 
    3093        81151 :   dw2_asm_output_data (1, tt_format, "@TType format (%s)",
    3094              :                        eh_data_format_name (tt_format));
    3095              : 
    3096        81151 :   if (!HAVE_AS_LEB128)
    3097              :     {
    3098              :       if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
    3099              :         call_site_len = sjlj_size_of_call_site_table ();
    3100              :       else
    3101              :         call_site_len = dw2_size_of_call_site_table (section);
    3102              :     }
    3103              : 
    3104              :   /* A pc-relative 4-byte displacement to the @TType data.  */
    3105        81151 :   if (have_tt_data)
    3106              :     {
    3107        25452 :       if (HAVE_AS_LEB128)
    3108              :         {
    3109        25452 :           char ttype_after_disp_label[32];
    3110        47300 :           ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
    3111              :                                        section ? "LLSDATTDC" : "LLSDATTD",
    3112              :                                        current_function_funcdef_no);
    3113        25452 :           dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
    3114              :                                         "@TType base offset");
    3115        25452 :           ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
    3116              :         }
    3117              :       else
    3118              :         {
    3119              :           /* Ug.  Alignment queers things.  */
    3120              :           unsigned int before_disp, after_disp, last_disp, disp;
    3121              : 
    3122              :           before_disp = 1 + 1;
    3123              :           after_disp = (1 + size_of_uleb128 (call_site_len)
    3124              :                         + call_site_len
    3125              :                         + vec_safe_length (crtl->eh.action_record_data)
    3126              :                         + (vec_safe_length (cfun->eh->ttype_data)
    3127              :                            * tt_format_size));
    3128              : 
    3129              :           disp = after_disp;
    3130              :           do
    3131              :             {
    3132              :               unsigned int disp_size, pad;
    3133              : 
    3134              :               last_disp = disp;
    3135              :               disp_size = size_of_uleb128 (disp);
    3136              :               pad = before_disp + disp_size + after_disp;
    3137              :               if (pad % tt_format_size)
    3138              :                 pad = tt_format_size - (pad % tt_format_size);
    3139              :               else
    3140              :                 pad = 0;
    3141              :               disp = after_disp + pad;
    3142              :             }
    3143              :           while (disp != last_disp);
    3144              : 
    3145              :           dw2_asm_output_data_uleb128 (disp, "@TType base offset");
    3146              :         }
    3147              :         }
    3148              : 
    3149              :   /* Indicate the format of the call-site offsets.  */
    3150        81151 :   if (HAVE_AS_LEB128)
    3151        81151 :     cs_format = DW_EH_PE_uleb128;
    3152              :   else
    3153              :     cs_format = DW_EH_PE_udata4;
    3154              : 
    3155        81151 :   dw2_asm_output_data (1, cs_format, "call-site format (%s)",
    3156              :                        eh_data_format_name (cs_format));
    3157              : 
    3158        81151 :   if (HAVE_AS_LEB128)
    3159              :     {
    3160       147472 :       ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
    3161              :                                    section ? "LLSDACSBC" : "LLSDACSB",
    3162              :                                    current_function_funcdef_no);
    3163       147472 :       ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
    3164              :                                    section ? "LLSDACSEC" : "LLSDACSE",
    3165              :                                    current_function_funcdef_no);
    3166        81151 :       dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
    3167              :                                     "Call-site table length");
    3168        81151 :       ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
    3169        81151 :       if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
    3170            0 :         sjlj_output_call_site_table ();
    3171              :       else
    3172        81151 :         dw2_output_call_site_table (cs_format, section);
    3173        81151 :       ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
    3174              :     }
    3175              :   else
    3176              :     {
    3177              :       dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
    3178              :       if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
    3179              :         sjlj_output_call_site_table ();
    3180              :       else
    3181              :         dw2_output_call_site_table (cs_format, section);
    3182              :     }
    3183              : 
    3184              :   /* ??? Decode and interpret the data for flag_debug_asm.  */
    3185        81151 :   {
    3186        81151 :     uchar uc;
    3187       227934 :     FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
    3188       105942 :       dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
    3189              :   }
    3190              : 
    3191        81151 :   if (have_tt_data)
    3192        25452 :     assemble_align (tt_format_size * BITS_PER_UNIT);
    3193              : 
    3194        81151 :   i = vec_safe_length (cfun->eh->ttype_data);
    3195       109321 :   while (i-- > 0)
    3196              :     {
    3197        28170 :       tree type = (*cfun->eh->ttype_data)[i];
    3198        28170 :       output_ttype (type, tt_format, tt_format_size);
    3199              :     }
    3200              : 
    3201        81151 :   if (HAVE_AS_LEB128 && have_tt_data)
    3202        25452 :     ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
    3203              : 
    3204              :   /* ??? Decode and interpret the data for flag_debug_asm.  */
    3205        81151 :   if (targetm.arm_eabi_unwinder)
    3206              :     {
    3207              :       tree type;
    3208            0 :       for (i = 0;
    3209            0 :            vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
    3210            0 :         output_ttype (type, tt_format, tt_format_size);
    3211              :     }
    3212              :   else
    3213              :     {
    3214              :       uchar uc;
    3215          518 :       for (i = 0;
    3216        81669 :            vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
    3217          648 :         dw2_asm_output_data (1, uc,
    3218              :                              i ? NULL : "Exception specification table");
    3219              :     }
    3220        81151 : }
    3221              : 
    3222              : /* Output an exception table for the current function according to SECTION,
    3223              :    switching back and forth from the function section appropriately.
    3224              : 
    3225              :    If the function has been partitioned into hot and cold parts, value 0 for
    3226              :    SECTION refers to the table associated with the hot part while value 1
    3227              :    refers to the table associated with the cold part.  If the function has
    3228              :    not been partitioned, value 0 refers to the single exception table.  */
    3229              : 
    3230              : void
    3231      1582464 : output_function_exception_table (int section)
    3232              : {
    3233              :   /* Not all functions need anything.  */
    3234      1582464 :   if (!crtl->uses_eh_lsda
    3235      1582464 :       || targetm_common.except_unwind_info (&global_options) == UI_NONE)
    3236              :     return;
    3237              : 
    3238              :   /* No need to emit any boilerplate stuff for the cold part.  */
    3239        81151 :   if (section == 1 && !crtl->eh.call_site_record_v[1])
    3240              :     return;
    3241              : 
    3242        81151 :   const char *fnname = get_fnname_from_decl (current_function_decl);
    3243        81151 :   rtx personality = get_personality_function (current_function_decl);
    3244              : 
    3245        81151 :   if (personality)
    3246              :     {
    3247        81151 :       assemble_external_libcall (personality);
    3248              : 
    3249        81151 :       if (targetm.asm_out.emit_except_personality)
    3250            0 :         targetm.asm_out.emit_except_personality (personality);
    3251              :     }
    3252              : 
    3253        81151 :   switch_to_exception_section (fnname);
    3254              : 
    3255              :   /* If the target wants a label to begin the table, emit it here.  */
    3256        81151 :   targetm.asm_out.emit_except_table_label (asm_out_file);
    3257              : 
    3258              :   /* Do the real work.  */
    3259        81151 :   output_one_function_exception_table (section);
    3260              : 
    3261        81151 :   switch_to_section (current_function_section ());
    3262              : }
    3263              : 
    3264              : void
    3265      1948032 : set_eh_throw_stmt_table (function *fun, hash_map<gimple *, int> *table)
    3266              : {
    3267      1948032 :   fun->eh->throw_stmt_table = table;
    3268      1948032 : }
    3269              : 
    3270              : hash_map<gimple *, int> *
    3271    737081406 : get_eh_throw_stmt_table (struct function *fun)
    3272              : {
    3273    737081406 :   return fun->eh->throw_stmt_table;
    3274              : }
    3275              : 
    3276              : /* Determine if the function needs an EH personality function.  */
    3277              : 
    3278              : enum eh_personality_kind
    3279      5626343 : function_needs_eh_personality (struct function *fn)
    3280              : {
    3281      5626343 :   enum eh_personality_kind kind = eh_personality_none;
    3282      5626343 :   eh_region i;
    3283              : 
    3284      6608770 :   FOR_ALL_EH_REGION_FN (i, fn)
    3285              :     {
    3286      1991473 :       switch (i->type)
    3287              :         {
    3288       982427 :         case ERT_CLEANUP:
    3289              :           /* Can do with any personality including the generic C one.  */
    3290       982427 :           kind = eh_personality_any;
    3291       982427 :           break;
    3292              : 
    3293              :         case ERT_TRY:
    3294              :         case ERT_ALLOWED_EXCEPTIONS:
    3295              :           /* Always needs a EH personality function.  The generic C
    3296              :              personality doesn't handle these even for empty type lists.  */
    3297              :           return eh_personality_lang;
    3298              : 
    3299              :         case ERT_MUST_NOT_THROW:
    3300              :           /* Always needs a EH personality function.  The language may specify
    3301              :              what abort routine that must be used, e.g. std::terminate.  */
    3302              :           return eh_personality_lang;
    3303              :         }
    3304              :     }
    3305              : 
    3306              :   return kind;
    3307              : }
    3308              : 
    3309              : /* Dump EH information to OUT.  */
    3310              : 
    3311              : void
    3312          674 : dump_eh_tree (FILE * out, struct function *fun)
    3313              : {
    3314          674 :   eh_region i;
    3315          674 :   int depth = 0;
    3316          674 :   static const char *const type_name[] = {
    3317              :     "cleanup", "try", "allowed_exceptions", "must_not_throw"
    3318              :   };
    3319              : 
    3320          674 :   i = fun->eh->region_tree;
    3321          674 :   if (!i)
    3322              :     return;
    3323              : 
    3324           55 :   fprintf (out, "Eh tree:\n");
    3325          108 :   while (1)
    3326              :     {
    3327          108 :       fprintf (out, "  %*s %i %s", depth * 2, "",
    3328          108 :                i->index, type_name[(int) i->type]);
    3329              : 
    3330          108 :       if (i->landing_pads)
    3331              :         {
    3332           43 :           eh_landing_pad lp;
    3333              : 
    3334           43 :           fprintf (out, " land:");
    3335           43 :           if (current_ir_type () == IR_GIMPLE)
    3336              :             {
    3337           86 :               for (lp = i->landing_pads; lp ; lp = lp->next_lp)
    3338              :                 {
    3339           43 :                   fprintf (out, "{%i,", lp->index);
    3340           43 :                   print_generic_expr (out, lp->post_landing_pad);
    3341           43 :                   fputc ('}', out);
    3342           43 :                   if (lp->next_lp)
    3343            0 :                     fputc (',', out);
    3344              :                 }
    3345              :             }
    3346              :           else
    3347              :             {
    3348            0 :               for (lp = i->landing_pads; lp ; lp = lp->next_lp)
    3349              :                 {
    3350            0 :                   fprintf (out, "{%i,", lp->index);
    3351            0 :                   if (lp->landing_pad)
    3352            0 :                     fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
    3353            0 :                              NOTE_P (lp->landing_pad) ? "(del)" : "");
    3354              :                   else
    3355            0 :                     fprintf (out, "(nil),");
    3356            0 :                   if (lp->post_landing_pad)
    3357              :                     {
    3358            0 :                       rtx_insn *lab = label_rtx (lp->post_landing_pad);
    3359            0 :                       fprintf (out, "%i%s}", INSN_UID (lab),
    3360            0 :                                NOTE_P (lab) ? "(del)" : "");
    3361              :                     }
    3362              :                   else
    3363            0 :                     fprintf (out, "(nil)}");
    3364            0 :                   if (lp->next_lp)
    3365            0 :                     fputc (',', out);
    3366              :                 }
    3367              :             }
    3368              :         }
    3369              : 
    3370          108 :       switch (i->type)
    3371              :         {
    3372              :         case ERT_CLEANUP:
    3373              :         case ERT_MUST_NOT_THROW:
    3374              :           break;
    3375              : 
    3376           14 :         case ERT_TRY:
    3377           14 :           {
    3378           14 :             eh_catch c;
    3379           14 :             fprintf (out, " catch:");
    3380           28 :             for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
    3381              :               {
    3382           14 :                 fputc ('{', out);
    3383           14 :                 if (c->label)
    3384              :                   {
    3385            0 :                     fprintf (out, "lab:");
    3386            0 :                     print_generic_expr (out, c->label);
    3387            0 :                     fputc (';', out);
    3388              :                   }
    3389           14 :                 print_generic_expr (out, c->type_list);
    3390           14 :                 fputc ('}', out);
    3391           14 :                 if (c->next_catch)
    3392            0 :                   fputc (',', out);
    3393              :               }
    3394              :           }
    3395              :           break;
    3396              : 
    3397            0 :         case ERT_ALLOWED_EXCEPTIONS:
    3398            0 :           fprintf (out, " filter :%i types:", i->u.allowed.filter);
    3399            0 :           print_generic_expr (out, i->u.allowed.type_list);
    3400            0 :           break;
    3401              :         }
    3402          108 :       fputc ('\n', out);
    3403              : 
    3404              :       /* If there are sub-regions, process them.  */
    3405          108 :       if (i->inner)
    3406           30 :         i = i->inner, depth++;
    3407              :       /* If there are peers, process them.  */
    3408           78 :       else if (i->next_peer)
    3409              :         i = i->next_peer;
    3410              :       /* Otherwise, step back up the tree to the next peer.  */
    3411              :       else
    3412              :         {
    3413           85 :           do
    3414              :             {
    3415           85 :               i = i->outer;
    3416           85 :               depth--;
    3417           85 :               if (i == NULL)
    3418              :                 return;
    3419              :             }
    3420           30 :           while (i->next_peer == NULL);
    3421              :           i = i->next_peer;
    3422              :         }
    3423              :     }
    3424              : }
    3425              : 
    3426              : /* Dump the EH tree for FN on stderr.  */
    3427              : 
    3428              : DEBUG_FUNCTION void
    3429            0 : debug_eh_tree (struct function *fn)
    3430              : {
    3431            0 :   dump_eh_tree (stderr, fn);
    3432            0 : }
    3433              : 
    3434              : /* Verify invariants on EH datastructures.  */
    3435              : 
    3436              : DEBUG_FUNCTION void
    3437     11303521 : verify_eh_tree (struct function *fun)
    3438              : {
    3439     11303521 :   eh_region r, outer;
    3440     11303521 :   int nvisited_lp, nvisited_r;
    3441     11303521 :   int count_lp, count_r, depth, i;
    3442     11303521 :   eh_landing_pad lp;
    3443     11303521 :   bool err = false;
    3444              : 
    3445     11303521 :   if (!fun->eh->region_tree)
    3446     11303521 :     return;
    3447              : 
    3448              :   count_r = 0;
    3449    143559587 :   for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
    3450    139805253 :     if (r)
    3451              :       {
    3452     86915013 :         if (r->index == i)
    3453     86915013 :           count_r++;
    3454              :         else
    3455              :           {
    3456            0 :             error ("%<region_array%> is corrupted for region %i", r->index);
    3457            0 :             err = true;
    3458              :           }
    3459              :       }
    3460              : 
    3461              :   count_lp = 0;
    3462     66773656 :   for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
    3463     63019322 :     if (lp)
    3464              :       {
    3465     45474865 :         if (lp->index == i)
    3466     45474865 :           count_lp++;
    3467              :         else
    3468              :           {
    3469            0 :             error ("%<lp_array%> is corrupted for lp %i", lp->index);
    3470            0 :             err = true;
    3471              :           }
    3472              :       }
    3473              : 
    3474      3754334 :   depth = nvisited_lp = nvisited_r = 0;
    3475      3754334 :   outer = NULL;
    3476      3754334 :   r = fun->eh->region_tree;
    3477     86915013 :   while (1)
    3478              :     {
    3479     86915013 :       if ((*fun->eh->region_array)[r->index] != r)
    3480              :         {
    3481            0 :           error ("%<region_array%> is corrupted for region %i", r->index);
    3482            0 :           err = true;
    3483              :         }
    3484     86915013 :       if (r->outer != outer)
    3485              :         {
    3486            0 :           error ("outer block of region %i is wrong", r->index);
    3487            0 :           err = true;
    3488              :         }
    3489     86915013 :       if (depth < 0)
    3490              :         {
    3491            0 :           error ("negative nesting depth of region %i", r->index);
    3492            0 :           err = true;
    3493              :         }
    3494     86915013 :       nvisited_r++;
    3495              : 
    3496    132389878 :       for (lp = r->landing_pads; lp ; lp = lp->next_lp)
    3497              :         {
    3498     45474865 :           if ((*fun->eh->lp_array)[lp->index] != lp)
    3499              :             {
    3500            0 :               error ("%<lp_array%> is corrupted for lp %i", lp->index);
    3501            0 :               err = true;
    3502              :             }
    3503     45474865 :           if (lp->region != r)
    3504              :             {
    3505            0 :               error ("region of lp %i is wrong", lp->index);
    3506            0 :               err = true;
    3507              :             }
    3508     45474865 :           nvisited_lp++;
    3509              :         }
    3510              : 
    3511     86915013 :       if (r->inner)
    3512     28792693 :         outer = r, r = r->inner, depth++;
    3513     58122320 :       else if (r->next_peer)
    3514              :         r = r->next_peer;
    3515              :       else
    3516              :         {
    3517     32547027 :           do
    3518              :             {
    3519     32547027 :               r = r->outer;
    3520     32547027 :               if (r == NULL)
    3521      3754334 :                 goto region_done;
    3522     28792693 :               depth--;
    3523     28792693 :               outer = r->outer;
    3524              :             }
    3525     28792693 :           while (r->next_peer == NULL);
    3526              :           r = r->next_peer;
    3527              :         }
    3528              :     }
    3529      3754334 :  region_done:
    3530      3754334 :   if (depth != 0)
    3531              :     {
    3532            0 :       error ("tree list ends on depth %i", depth);
    3533            0 :       err = true;
    3534              :     }
    3535      3754334 :   if (count_r != nvisited_r)
    3536              :     {
    3537            0 :       error ("%<region_array%> does not match %<region_tree%>");
    3538            0 :       err = true;
    3539              :     }
    3540      3754334 :   if (count_lp != nvisited_lp)
    3541              :     {
    3542            0 :       error ("%<lp_array%> does not match %<region_tree%>");
    3543            0 :       err = true;
    3544              :     }
    3545              : 
    3546      3754334 :   if (err)
    3547              :     {
    3548            0 :       dump_eh_tree (stderr, fun);
    3549            0 :       internal_error ("%qs failed", __func__);
    3550              :     }
    3551              : }
    3552              : 
    3553              : #include "gt-except.h"
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.