xref: /dflybsd-src/contrib/gcc-4.7/gcc/tree-eh.c (revision 0a8dc9fc45f4d0b236341a473fac4a486375f60c)
1e4b17023SJohn Marino /* Exception handling semantics and decomposition for trees.
2e4b17023SJohn Marino    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3e4b17023SJohn Marino    Free Software Foundation, Inc.
4e4b17023SJohn Marino 
5e4b17023SJohn Marino This file is part of GCC.
6e4b17023SJohn Marino 
7e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10e4b17023SJohn Marino any later version.
11e4b17023SJohn Marino 
12e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15e4b17023SJohn Marino GNU General Public License for more details.
16e4b17023SJohn Marino 
17e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20e4b17023SJohn Marino 
21e4b17023SJohn Marino #include "config.h"
22e4b17023SJohn Marino #include "system.h"
23e4b17023SJohn Marino #include "coretypes.h"
24e4b17023SJohn Marino #include "tm.h"
25e4b17023SJohn Marino #include "tree.h"
26e4b17023SJohn Marino #include "flags.h"
27e4b17023SJohn Marino #include "function.h"
28e4b17023SJohn Marino #include "except.h"
29e4b17023SJohn Marino #include "pointer-set.h"
30e4b17023SJohn Marino #include "tree-flow.h"
31e4b17023SJohn Marino #include "tree-dump.h"
32e4b17023SJohn Marino #include "tree-inline.h"
33e4b17023SJohn Marino #include "tree-iterator.h"
34e4b17023SJohn Marino #include "tree-pass.h"
35e4b17023SJohn Marino #include "timevar.h"
36e4b17023SJohn Marino #include "langhooks.h"
37e4b17023SJohn Marino #include "ggc.h"
38e4b17023SJohn Marino #include "diagnostic-core.h"
39e4b17023SJohn Marino #include "gimple.h"
40e4b17023SJohn Marino #include "target.h"
41e4b17023SJohn Marino 
42e4b17023SJohn Marino /* In some instances a tree and a gimple need to be stored in a same table,
43e4b17023SJohn Marino    i.e. in hash tables. This is a structure to do this. */
44e4b17023SJohn Marino typedef union {tree *tp; tree t; gimple g;} treemple;
45e4b17023SJohn Marino 
46e4b17023SJohn Marino /* Nonzero if we are using EH to handle cleanups.  */
47e4b17023SJohn Marino static int using_eh_for_cleanups_p = 0;
48e4b17023SJohn Marino 
49e4b17023SJohn Marino void
using_eh_for_cleanups(void)50e4b17023SJohn Marino using_eh_for_cleanups (void)
51e4b17023SJohn Marino {
52e4b17023SJohn Marino   using_eh_for_cleanups_p = 1;
53e4b17023SJohn Marino }
54e4b17023SJohn Marino 
55e4b17023SJohn Marino /* Misc functions used in this file.  */
56e4b17023SJohn Marino 
57e4b17023SJohn Marino /* Remember and lookup EH landing pad data for arbitrary statements.
58e4b17023SJohn Marino    Really this means any statement that could_throw_p.  We could
59e4b17023SJohn Marino    stuff this information into the stmt_ann data structure, but:
60e4b17023SJohn Marino 
61e4b17023SJohn Marino    (1) We absolutely rely on this information being kept until
62e4b17023SJohn Marino    we get to rtl.  Once we're done with lowering here, if we lose
63e4b17023SJohn Marino    the information there's no way to recover it!
64e4b17023SJohn Marino 
65e4b17023SJohn Marino    (2) There are many more statements that *cannot* throw as
66e4b17023SJohn Marino    compared to those that can.  We should be saving some amount
67e4b17023SJohn Marino    of space by only allocating memory for those that can throw.  */
68e4b17023SJohn Marino 
69e4b17023SJohn Marino /* Add statement T in function IFUN to landing pad NUM.  */
70e4b17023SJohn Marino 
71e4b17023SJohn Marino void
add_stmt_to_eh_lp_fn(struct function * ifun,gimple t,int num)72e4b17023SJohn Marino add_stmt_to_eh_lp_fn (struct function *ifun, gimple t, int num)
73e4b17023SJohn Marino {
74e4b17023SJohn Marino   struct throw_stmt_node *n;
75e4b17023SJohn Marino   void **slot;
76e4b17023SJohn Marino 
77e4b17023SJohn Marino   gcc_assert (num != 0);
78e4b17023SJohn Marino 
79e4b17023SJohn Marino   n = ggc_alloc_throw_stmt_node ();
80e4b17023SJohn Marino   n->stmt = t;
81e4b17023SJohn Marino   n->lp_nr = num;
82e4b17023SJohn Marino 
83e4b17023SJohn Marino   if (!get_eh_throw_stmt_table (ifun))
84e4b17023SJohn Marino     set_eh_throw_stmt_table (ifun, htab_create_ggc (31, struct_ptr_hash,
85e4b17023SJohn Marino 						    struct_ptr_eq,
86e4b17023SJohn Marino 						    ggc_free));
87e4b17023SJohn Marino 
88e4b17023SJohn Marino   slot = htab_find_slot (get_eh_throw_stmt_table (ifun), n, INSERT);
89e4b17023SJohn Marino   gcc_assert (!*slot);
90e4b17023SJohn Marino   *slot = n;
91e4b17023SJohn Marino }
92e4b17023SJohn Marino 
93e4b17023SJohn Marino /* Add statement T in the current function (cfun) to EH landing pad NUM.  */
94e4b17023SJohn Marino 
95e4b17023SJohn Marino void
add_stmt_to_eh_lp(gimple t,int num)96e4b17023SJohn Marino add_stmt_to_eh_lp (gimple t, int num)
97e4b17023SJohn Marino {
98e4b17023SJohn Marino   add_stmt_to_eh_lp_fn (cfun, t, num);
99e4b17023SJohn Marino }
100e4b17023SJohn Marino 
101e4b17023SJohn Marino /* Add statement T to the single EH landing pad in REGION.  */
102e4b17023SJohn Marino 
103e4b17023SJohn Marino static void
record_stmt_eh_region(eh_region region,gimple t)104e4b17023SJohn Marino record_stmt_eh_region (eh_region region, gimple t)
105e4b17023SJohn Marino {
106e4b17023SJohn Marino   if (region == NULL)
107e4b17023SJohn Marino     return;
108e4b17023SJohn Marino   if (region->type == ERT_MUST_NOT_THROW)
109e4b17023SJohn Marino     add_stmt_to_eh_lp_fn (cfun, t, -region->index);
110e4b17023SJohn Marino   else
111e4b17023SJohn Marino     {
112e4b17023SJohn Marino       eh_landing_pad lp = region->landing_pads;
113e4b17023SJohn Marino       if (lp == NULL)
114e4b17023SJohn Marino 	lp = gen_eh_landing_pad (region);
115e4b17023SJohn Marino       else
116e4b17023SJohn Marino 	gcc_assert (lp->next_lp == NULL);
117e4b17023SJohn Marino       add_stmt_to_eh_lp_fn (cfun, t, lp->index);
118e4b17023SJohn Marino     }
119e4b17023SJohn Marino }
120e4b17023SJohn Marino 
121e4b17023SJohn Marino 
122e4b17023SJohn Marino /* Remove statement T in function IFUN from its EH landing pad.  */
123e4b17023SJohn Marino 
124e4b17023SJohn Marino bool
remove_stmt_from_eh_lp_fn(struct function * ifun,gimple t)125e4b17023SJohn Marino remove_stmt_from_eh_lp_fn (struct function *ifun, gimple t)
126e4b17023SJohn Marino {
127e4b17023SJohn Marino   struct throw_stmt_node dummy;
128e4b17023SJohn Marino   void **slot;
129e4b17023SJohn Marino 
130e4b17023SJohn Marino   if (!get_eh_throw_stmt_table (ifun))
131e4b17023SJohn Marino     return false;
132e4b17023SJohn Marino 
133e4b17023SJohn Marino   dummy.stmt = t;
134e4b17023SJohn Marino   slot = htab_find_slot (get_eh_throw_stmt_table (ifun), &dummy,
135e4b17023SJohn Marino                         NO_INSERT);
136e4b17023SJohn Marino   if (slot)
137e4b17023SJohn Marino     {
138e4b17023SJohn Marino       htab_clear_slot (get_eh_throw_stmt_table (ifun), slot);
139e4b17023SJohn Marino       return true;
140e4b17023SJohn Marino     }
141e4b17023SJohn Marino   else
142e4b17023SJohn Marino     return false;
143e4b17023SJohn Marino }
144e4b17023SJohn Marino 
145e4b17023SJohn Marino 
146e4b17023SJohn Marino /* Remove statement T in the current function (cfun) from its
147e4b17023SJohn Marino    EH landing pad.  */
148e4b17023SJohn Marino 
149e4b17023SJohn Marino bool
remove_stmt_from_eh_lp(gimple t)150e4b17023SJohn Marino remove_stmt_from_eh_lp (gimple t)
151e4b17023SJohn Marino {
152e4b17023SJohn Marino   return remove_stmt_from_eh_lp_fn (cfun, t);
153e4b17023SJohn Marino }
154e4b17023SJohn Marino 
155e4b17023SJohn Marino /* Determine if statement T is inside an EH region in function IFUN.
156e4b17023SJohn Marino    Positive numbers indicate a landing pad index; negative numbers
157e4b17023SJohn Marino    indicate a MUST_NOT_THROW region index; zero indicates that the
158e4b17023SJohn Marino    statement is not recorded in the region table.  */
159e4b17023SJohn Marino 
160e4b17023SJohn Marino int
lookup_stmt_eh_lp_fn(struct function * ifun,gimple t)161e4b17023SJohn Marino lookup_stmt_eh_lp_fn (struct function *ifun, gimple t)
162e4b17023SJohn Marino {
163e4b17023SJohn Marino   struct throw_stmt_node *p, n;
164e4b17023SJohn Marino 
165e4b17023SJohn Marino   if (ifun->eh->throw_stmt_table == NULL)
166e4b17023SJohn Marino     return 0;
167e4b17023SJohn Marino 
168e4b17023SJohn Marino   n.stmt = t;
169e4b17023SJohn Marino   p = (struct throw_stmt_node *) htab_find (ifun->eh->throw_stmt_table, &n);
170e4b17023SJohn Marino   return p ? p->lp_nr : 0;
171e4b17023SJohn Marino }
172e4b17023SJohn Marino 
173e4b17023SJohn Marino /* Likewise, but always use the current function.  */
174e4b17023SJohn Marino 
175e4b17023SJohn Marino int
lookup_stmt_eh_lp(gimple t)176e4b17023SJohn Marino lookup_stmt_eh_lp (gimple t)
177e4b17023SJohn Marino {
178e4b17023SJohn Marino   /* We can get called from initialized data when -fnon-call-exceptions
179e4b17023SJohn Marino      is on; prevent crash.  */
180e4b17023SJohn Marino   if (!cfun)
181e4b17023SJohn Marino     return 0;
182e4b17023SJohn Marino   return lookup_stmt_eh_lp_fn (cfun, t);
183e4b17023SJohn Marino }
184e4b17023SJohn Marino 
185e4b17023SJohn Marino /* First pass of EH node decomposition.  Build up a tree of GIMPLE_TRY_FINALLY
186e4b17023SJohn Marino    nodes and LABEL_DECL nodes.  We will use this during the second phase to
187e4b17023SJohn Marino    determine if a goto leaves the body of a TRY_FINALLY_EXPR node.  */
188e4b17023SJohn Marino 
189e4b17023SJohn Marino struct finally_tree_node
190e4b17023SJohn Marino {
191e4b17023SJohn Marino   /* When storing a GIMPLE_TRY, we have to record a gimple.  However
192e4b17023SJohn Marino      when deciding whether a GOTO to a certain LABEL_DECL (which is a
193e4b17023SJohn Marino      tree) leaves the TRY block, its necessary to record a tree in
194e4b17023SJohn Marino      this field.  Thus a treemple is used. */
195e4b17023SJohn Marino   treemple child;
196e4b17023SJohn Marino   gimple parent;
197e4b17023SJohn Marino };
198e4b17023SJohn Marino 
199e4b17023SJohn Marino /* Note that this table is *not* marked GTY.  It is short-lived.  */
200e4b17023SJohn Marino static htab_t finally_tree;
201e4b17023SJohn Marino 
202e4b17023SJohn Marino static void
record_in_finally_tree(treemple child,gimple parent)203e4b17023SJohn Marino record_in_finally_tree (treemple child, gimple parent)
204e4b17023SJohn Marino {
205e4b17023SJohn Marino   struct finally_tree_node *n;
206e4b17023SJohn Marino   void **slot;
207e4b17023SJohn Marino 
208e4b17023SJohn Marino   n = XNEW (struct finally_tree_node);
209e4b17023SJohn Marino   n->child = child;
210e4b17023SJohn Marino   n->parent = parent;
211e4b17023SJohn Marino 
212e4b17023SJohn Marino   slot = htab_find_slot (finally_tree, n, INSERT);
213e4b17023SJohn Marino   gcc_assert (!*slot);
214e4b17023SJohn Marino   *slot = n;
215e4b17023SJohn Marino }
216e4b17023SJohn Marino 
217e4b17023SJohn Marino static void
218e4b17023SJohn Marino collect_finally_tree (gimple stmt, gimple region);
219e4b17023SJohn Marino 
220e4b17023SJohn Marino /* Go through the gimple sequence.  Works with collect_finally_tree to
221e4b17023SJohn Marino    record all GIMPLE_LABEL and GIMPLE_TRY statements. */
222e4b17023SJohn Marino 
223e4b17023SJohn Marino static void
collect_finally_tree_1(gimple_seq seq,gimple region)224e4b17023SJohn Marino collect_finally_tree_1 (gimple_seq seq, gimple region)
225e4b17023SJohn Marino {
226e4b17023SJohn Marino   gimple_stmt_iterator gsi;
227e4b17023SJohn Marino 
228e4b17023SJohn Marino   for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
229e4b17023SJohn Marino     collect_finally_tree (gsi_stmt (gsi), region);
230e4b17023SJohn Marino }
231e4b17023SJohn Marino 
232e4b17023SJohn Marino static void
collect_finally_tree(gimple stmt,gimple region)233e4b17023SJohn Marino collect_finally_tree (gimple stmt, gimple region)
234e4b17023SJohn Marino {
235e4b17023SJohn Marino   treemple temp;
236e4b17023SJohn Marino 
237e4b17023SJohn Marino   switch (gimple_code (stmt))
238e4b17023SJohn Marino     {
239e4b17023SJohn Marino     case GIMPLE_LABEL:
240e4b17023SJohn Marino       temp.t = gimple_label_label (stmt);
241e4b17023SJohn Marino       record_in_finally_tree (temp, region);
242e4b17023SJohn Marino       break;
243e4b17023SJohn Marino 
244e4b17023SJohn Marino     case GIMPLE_TRY:
245e4b17023SJohn Marino       if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
246e4b17023SJohn Marino         {
247e4b17023SJohn Marino           temp.g = stmt;
248e4b17023SJohn Marino           record_in_finally_tree (temp, region);
249e4b17023SJohn Marino           collect_finally_tree_1 (gimple_try_eval (stmt), stmt);
250e4b17023SJohn Marino 	  collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
251e4b17023SJohn Marino         }
252e4b17023SJohn Marino       else if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
253e4b17023SJohn Marino         {
254e4b17023SJohn Marino           collect_finally_tree_1 (gimple_try_eval (stmt), region);
255e4b17023SJohn Marino           collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
256e4b17023SJohn Marino         }
257e4b17023SJohn Marino       break;
258e4b17023SJohn Marino 
259e4b17023SJohn Marino     case GIMPLE_CATCH:
260e4b17023SJohn Marino       collect_finally_tree_1 (gimple_catch_handler (stmt), region);
261e4b17023SJohn Marino       break;
262e4b17023SJohn Marino 
263e4b17023SJohn Marino     case GIMPLE_EH_FILTER:
264e4b17023SJohn Marino       collect_finally_tree_1 (gimple_eh_filter_failure (stmt), region);
265e4b17023SJohn Marino       break;
266e4b17023SJohn Marino 
267e4b17023SJohn Marino     case GIMPLE_EH_ELSE:
268e4b17023SJohn Marino       collect_finally_tree_1 (gimple_eh_else_n_body (stmt), region);
269e4b17023SJohn Marino       collect_finally_tree_1 (gimple_eh_else_e_body (stmt), region);
270e4b17023SJohn Marino       break;
271e4b17023SJohn Marino 
272e4b17023SJohn Marino     default:
273e4b17023SJohn Marino       /* A type, a decl, or some kind of statement that we're not
274e4b17023SJohn Marino 	 interested in.  Don't walk them.  */
275e4b17023SJohn Marino       break;
276e4b17023SJohn Marino     }
277e4b17023SJohn Marino }
278e4b17023SJohn Marino 
279e4b17023SJohn Marino 
280e4b17023SJohn Marino /* Use the finally tree to determine if a jump from START to TARGET
281e4b17023SJohn Marino    would leave the try_finally node that START lives in.  */
282e4b17023SJohn Marino 
283e4b17023SJohn Marino static bool
outside_finally_tree(treemple start,gimple target)284e4b17023SJohn Marino outside_finally_tree (treemple start, gimple target)
285e4b17023SJohn Marino {
286e4b17023SJohn Marino   struct finally_tree_node n, *p;
287e4b17023SJohn Marino 
288e4b17023SJohn Marino   do
289e4b17023SJohn Marino     {
290e4b17023SJohn Marino       n.child = start;
291e4b17023SJohn Marino       p = (struct finally_tree_node *) htab_find (finally_tree, &n);
292e4b17023SJohn Marino       if (!p)
293e4b17023SJohn Marino 	return true;
294e4b17023SJohn Marino       start.g = p->parent;
295e4b17023SJohn Marino     }
296e4b17023SJohn Marino   while (start.g != target);
297e4b17023SJohn Marino 
298e4b17023SJohn Marino   return false;
299e4b17023SJohn Marino }
300e4b17023SJohn Marino 
301e4b17023SJohn Marino /* Second pass of EH node decomposition.  Actually transform the GIMPLE_TRY
302e4b17023SJohn Marino    nodes into a set of gotos, magic labels, and eh regions.
303e4b17023SJohn Marino    The eh region creation is straight-forward, but frobbing all the gotos
304e4b17023SJohn Marino    and such into shape isn't.  */
305e4b17023SJohn Marino 
306e4b17023SJohn Marino /* The sequence into which we record all EH stuff.  This will be
307e4b17023SJohn Marino    placed at the end of the function when we're all done.  */
308e4b17023SJohn Marino static gimple_seq eh_seq;
309e4b17023SJohn Marino 
310e4b17023SJohn Marino /* Record whether an EH region contains something that can throw,
311e4b17023SJohn Marino    indexed by EH region number.  */
312e4b17023SJohn Marino static bitmap eh_region_may_contain_throw_map;
313e4b17023SJohn Marino 
314e4b17023SJohn Marino /* The GOTO_QUEUE is is an array of GIMPLE_GOTO and GIMPLE_RETURN
315e4b17023SJohn Marino    statements that are seen to escape this GIMPLE_TRY_FINALLY node.
316e4b17023SJohn Marino    The idea is to record a gimple statement for everything except for
317e4b17023SJohn Marino    the conditionals, which get their labels recorded. Since labels are
318e4b17023SJohn Marino    of type 'tree', we need this node to store both gimple and tree
319e4b17023SJohn Marino    objects.  REPL_STMT is the sequence used to replace the goto/return
320e4b17023SJohn Marino    statement.  CONT_STMT is used to store the statement that allows
321e4b17023SJohn Marino    the return/goto to jump to the original destination. */
322e4b17023SJohn Marino 
323e4b17023SJohn Marino struct goto_queue_node
324e4b17023SJohn Marino {
325e4b17023SJohn Marino   treemple stmt;
326e4b17023SJohn Marino   gimple_seq repl_stmt;
327e4b17023SJohn Marino   gimple cont_stmt;
328e4b17023SJohn Marino   int index;
329e4b17023SJohn Marino   /* This is used when index >= 0 to indicate that stmt is a label (as
330e4b17023SJohn Marino      opposed to a goto stmt).  */
331e4b17023SJohn Marino   int is_label;
332e4b17023SJohn Marino };
333e4b17023SJohn Marino 
334e4b17023SJohn Marino /* State of the world while lowering.  */
335e4b17023SJohn Marino 
336e4b17023SJohn Marino struct leh_state
337e4b17023SJohn Marino {
338e4b17023SJohn Marino   /* What's "current" while constructing the eh region tree.  These
339e4b17023SJohn Marino      correspond to variables of the same name in cfun->eh, which we
340e4b17023SJohn Marino      don't have easy access to.  */
341e4b17023SJohn Marino   eh_region cur_region;
342e4b17023SJohn Marino 
343e4b17023SJohn Marino   /* What's "current" for the purposes of __builtin_eh_pointer.  For
344e4b17023SJohn Marino      a CATCH, this is the associated TRY.  For an EH_FILTER, this is
345e4b17023SJohn Marino      the associated ALLOWED_EXCEPTIONS, etc.  */
346e4b17023SJohn Marino   eh_region ehp_region;
347e4b17023SJohn Marino 
348e4b17023SJohn Marino   /* Processing of TRY_FINALLY requires a bit more state.  This is
349e4b17023SJohn Marino      split out into a separate structure so that we don't have to
350e4b17023SJohn Marino      copy so much when processing other nodes.  */
351e4b17023SJohn Marino   struct leh_tf_state *tf;
352e4b17023SJohn Marino };
353e4b17023SJohn Marino 
354e4b17023SJohn Marino struct leh_tf_state
355e4b17023SJohn Marino {
356e4b17023SJohn Marino   /* Pointer to the GIMPLE_TRY_FINALLY node under discussion.  The
357e4b17023SJohn Marino      try_finally_expr is the original GIMPLE_TRY_FINALLY.  We need to retain
358e4b17023SJohn Marino      this so that outside_finally_tree can reliably reference the tree used
359e4b17023SJohn Marino      in the collect_finally_tree data structures.  */
360e4b17023SJohn Marino   gimple try_finally_expr;
361e4b17023SJohn Marino   gimple top_p;
362e4b17023SJohn Marino 
363e4b17023SJohn Marino   /* While lowering a top_p usually it is expanded into multiple statements,
364e4b17023SJohn Marino      thus we need the following field to store them. */
365e4b17023SJohn Marino   gimple_seq top_p_seq;
366e4b17023SJohn Marino 
367e4b17023SJohn Marino   /* The state outside this try_finally node.  */
368e4b17023SJohn Marino   struct leh_state *outer;
369e4b17023SJohn Marino 
370e4b17023SJohn Marino   /* The exception region created for it.  */
371e4b17023SJohn Marino   eh_region region;
372e4b17023SJohn Marino 
373e4b17023SJohn Marino   /* The goto queue.  */
374e4b17023SJohn Marino   struct goto_queue_node *goto_queue;
375e4b17023SJohn Marino   size_t goto_queue_size;
376e4b17023SJohn Marino   size_t goto_queue_active;
377e4b17023SJohn Marino 
378e4b17023SJohn Marino   /* Pointer map to help in searching goto_queue when it is large.  */
379e4b17023SJohn Marino   struct pointer_map_t *goto_queue_map;
380e4b17023SJohn Marino 
381e4b17023SJohn Marino   /* The set of unique labels seen as entries in the goto queue.  */
382e4b17023SJohn Marino   VEC(tree,heap) *dest_array;
383e4b17023SJohn Marino 
384e4b17023SJohn Marino   /* A label to be added at the end of the completed transformed
385e4b17023SJohn Marino      sequence.  It will be set if may_fallthru was true *at one time*,
386e4b17023SJohn Marino      though subsequent transformations may have cleared that flag.  */
387e4b17023SJohn Marino   tree fallthru_label;
388e4b17023SJohn Marino 
389e4b17023SJohn Marino   /* True if it is possible to fall out the bottom of the try block.
390e4b17023SJohn Marino      Cleared if the fallthru is converted to a goto.  */
391e4b17023SJohn Marino   bool may_fallthru;
392e4b17023SJohn Marino 
393e4b17023SJohn Marino   /* True if any entry in goto_queue is a GIMPLE_RETURN.  */
394e4b17023SJohn Marino   bool may_return;
395e4b17023SJohn Marino 
396e4b17023SJohn Marino   /* True if the finally block can receive an exception edge.
397e4b17023SJohn Marino      Cleared if the exception case is handled by code duplication.  */
398e4b17023SJohn Marino   bool may_throw;
399e4b17023SJohn Marino };
400e4b17023SJohn Marino 
401e4b17023SJohn Marino static gimple_seq lower_eh_must_not_throw (struct leh_state *, gimple);
402e4b17023SJohn Marino 
403e4b17023SJohn Marino /* Search for STMT in the goto queue.  Return the replacement,
404e4b17023SJohn Marino    or null if the statement isn't in the queue.  */
405e4b17023SJohn Marino 
406e4b17023SJohn Marino #define LARGE_GOTO_QUEUE 20
407e4b17023SJohn Marino 
408e4b17023SJohn Marino static void lower_eh_constructs_1 (struct leh_state *state, gimple_seq seq);
409e4b17023SJohn Marino 
410e4b17023SJohn Marino static gimple_seq
find_goto_replacement(struct leh_tf_state * tf,treemple stmt)411e4b17023SJohn Marino find_goto_replacement (struct leh_tf_state *tf, treemple stmt)
412e4b17023SJohn Marino {
413e4b17023SJohn Marino   unsigned int i;
414e4b17023SJohn Marino   void **slot;
415e4b17023SJohn Marino 
416e4b17023SJohn Marino   if (tf->goto_queue_active < LARGE_GOTO_QUEUE)
417e4b17023SJohn Marino     {
418e4b17023SJohn Marino       for (i = 0; i < tf->goto_queue_active; i++)
419e4b17023SJohn Marino 	if ( tf->goto_queue[i].stmt.g == stmt.g)
420e4b17023SJohn Marino 	  return tf->goto_queue[i].repl_stmt;
421e4b17023SJohn Marino       return NULL;
422e4b17023SJohn Marino     }
423e4b17023SJohn Marino 
424e4b17023SJohn Marino   /* If we have a large number of entries in the goto_queue, create a
425e4b17023SJohn Marino      pointer map and use that for searching.  */
426e4b17023SJohn Marino 
427e4b17023SJohn Marino   if (!tf->goto_queue_map)
428e4b17023SJohn Marino     {
429e4b17023SJohn Marino       tf->goto_queue_map = pointer_map_create ();
430e4b17023SJohn Marino       for (i = 0; i < tf->goto_queue_active; i++)
431e4b17023SJohn Marino 	{
432e4b17023SJohn Marino 	  slot = pointer_map_insert (tf->goto_queue_map,
433e4b17023SJohn Marino                                      tf->goto_queue[i].stmt.g);
434e4b17023SJohn Marino           gcc_assert (*slot == NULL);
435e4b17023SJohn Marino 	  *slot = &tf->goto_queue[i];
436e4b17023SJohn Marino 	}
437e4b17023SJohn Marino     }
438e4b17023SJohn Marino 
439e4b17023SJohn Marino   slot = pointer_map_contains (tf->goto_queue_map, stmt.g);
440e4b17023SJohn Marino   if (slot != NULL)
441e4b17023SJohn Marino     return (((struct goto_queue_node *) *slot)->repl_stmt);
442e4b17023SJohn Marino 
443e4b17023SJohn Marino   return NULL;
444e4b17023SJohn Marino }
445e4b17023SJohn Marino 
446e4b17023SJohn Marino /* A subroutine of replace_goto_queue_1.  Handles the sub-clauses of a
447e4b17023SJohn Marino    lowered GIMPLE_COND.  If, by chance, the replacement is a simple goto,
448e4b17023SJohn Marino    then we can just splat it in, otherwise we add the new stmts immediately
449e4b17023SJohn Marino    after the GIMPLE_COND and redirect.  */
450e4b17023SJohn Marino 
451e4b17023SJohn Marino static void
replace_goto_queue_cond_clause(tree * tp,struct leh_tf_state * tf,gimple_stmt_iterator * gsi)452e4b17023SJohn Marino replace_goto_queue_cond_clause (tree *tp, struct leh_tf_state *tf,
453e4b17023SJohn Marino 				gimple_stmt_iterator *gsi)
454e4b17023SJohn Marino {
455e4b17023SJohn Marino   tree label;
456e4b17023SJohn Marino   gimple_seq new_seq;
457e4b17023SJohn Marino   treemple temp;
458e4b17023SJohn Marino   location_t loc = gimple_location (gsi_stmt (*gsi));
459e4b17023SJohn Marino 
460e4b17023SJohn Marino   temp.tp = tp;
461e4b17023SJohn Marino   new_seq = find_goto_replacement (tf, temp);
462e4b17023SJohn Marino   if (!new_seq)
463e4b17023SJohn Marino     return;
464e4b17023SJohn Marino 
465e4b17023SJohn Marino   if (gimple_seq_singleton_p (new_seq)
466e4b17023SJohn Marino       && gimple_code (gimple_seq_first_stmt (new_seq)) == GIMPLE_GOTO)
467e4b17023SJohn Marino     {
468e4b17023SJohn Marino       *tp = gimple_goto_dest (gimple_seq_first_stmt (new_seq));
469e4b17023SJohn Marino       return;
470e4b17023SJohn Marino     }
471e4b17023SJohn Marino 
472e4b17023SJohn Marino   label = create_artificial_label (loc);
473e4b17023SJohn Marino   /* Set the new label for the GIMPLE_COND */
474e4b17023SJohn Marino   *tp = label;
475e4b17023SJohn Marino 
476e4b17023SJohn Marino   gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
477e4b17023SJohn Marino   gsi_insert_seq_after (gsi, gimple_seq_copy (new_seq), GSI_CONTINUE_LINKING);
478e4b17023SJohn Marino }
479e4b17023SJohn Marino 
480e4b17023SJohn Marino /* The real work of replace_goto_queue.  Returns with TSI updated to
481e4b17023SJohn Marino    point to the next statement.  */
482e4b17023SJohn Marino 
483e4b17023SJohn Marino static void replace_goto_queue_stmt_list (gimple_seq, struct leh_tf_state *);
484e4b17023SJohn Marino 
485e4b17023SJohn Marino static void
replace_goto_queue_1(gimple stmt,struct leh_tf_state * tf,gimple_stmt_iterator * gsi)486e4b17023SJohn Marino replace_goto_queue_1 (gimple stmt, struct leh_tf_state *tf,
487e4b17023SJohn Marino 		      gimple_stmt_iterator *gsi)
488e4b17023SJohn Marino {
489e4b17023SJohn Marino   gimple_seq seq;
490e4b17023SJohn Marino   treemple temp;
491e4b17023SJohn Marino   temp.g = NULL;
492e4b17023SJohn Marino 
493e4b17023SJohn Marino   switch (gimple_code (stmt))
494e4b17023SJohn Marino     {
495e4b17023SJohn Marino     case GIMPLE_GOTO:
496e4b17023SJohn Marino     case GIMPLE_RETURN:
497e4b17023SJohn Marino       temp.g = stmt;
498e4b17023SJohn Marino       seq = find_goto_replacement (tf, temp);
499e4b17023SJohn Marino       if (seq)
500e4b17023SJohn Marino 	{
501e4b17023SJohn Marino 	  gsi_insert_seq_before (gsi, gimple_seq_copy (seq), GSI_SAME_STMT);
502e4b17023SJohn Marino 	  gsi_remove (gsi, false);
503e4b17023SJohn Marino 	  return;
504e4b17023SJohn Marino 	}
505e4b17023SJohn Marino       break;
506e4b17023SJohn Marino 
507e4b17023SJohn Marino     case GIMPLE_COND:
508e4b17023SJohn Marino       replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 2), tf, gsi);
509e4b17023SJohn Marino       replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 3), tf, gsi);
510e4b17023SJohn Marino       break;
511e4b17023SJohn Marino 
512e4b17023SJohn Marino     case GIMPLE_TRY:
513e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_try_eval (stmt), tf);
514e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_try_cleanup (stmt), tf);
515e4b17023SJohn Marino       break;
516e4b17023SJohn Marino     case GIMPLE_CATCH:
517e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_catch_handler (stmt), tf);
518e4b17023SJohn Marino       break;
519e4b17023SJohn Marino     case GIMPLE_EH_FILTER:
520e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_eh_filter_failure (stmt), tf);
521e4b17023SJohn Marino       break;
522e4b17023SJohn Marino     case GIMPLE_EH_ELSE:
523e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_eh_else_n_body (stmt), tf);
524e4b17023SJohn Marino       replace_goto_queue_stmt_list (gimple_eh_else_e_body (stmt), tf);
525e4b17023SJohn Marino       break;
526e4b17023SJohn Marino 
527e4b17023SJohn Marino     default:
528e4b17023SJohn Marino       /* These won't have gotos in them.  */
529e4b17023SJohn Marino       break;
530e4b17023SJohn Marino     }
531e4b17023SJohn Marino 
532e4b17023SJohn Marino   gsi_next (gsi);
533e4b17023SJohn Marino }
534e4b17023SJohn Marino 
535e4b17023SJohn Marino /* A subroutine of replace_goto_queue.  Handles GIMPLE_SEQ.  */
536e4b17023SJohn Marino 
537e4b17023SJohn Marino static void
replace_goto_queue_stmt_list(gimple_seq seq,struct leh_tf_state * tf)538e4b17023SJohn Marino replace_goto_queue_stmt_list (gimple_seq seq, struct leh_tf_state *tf)
539e4b17023SJohn Marino {
540e4b17023SJohn Marino   gimple_stmt_iterator gsi = gsi_start (seq);
541e4b17023SJohn Marino 
542e4b17023SJohn Marino   while (!gsi_end_p (gsi))
543e4b17023SJohn Marino     replace_goto_queue_1 (gsi_stmt (gsi), tf, &gsi);
544e4b17023SJohn Marino }
545e4b17023SJohn Marino 
546e4b17023SJohn Marino /* Replace all goto queue members.  */
547e4b17023SJohn Marino 
548e4b17023SJohn Marino static void
replace_goto_queue(struct leh_tf_state * tf)549e4b17023SJohn Marino replace_goto_queue (struct leh_tf_state *tf)
550e4b17023SJohn Marino {
551e4b17023SJohn Marino   if (tf->goto_queue_active == 0)
552e4b17023SJohn Marino     return;
553e4b17023SJohn Marino   replace_goto_queue_stmt_list (tf->top_p_seq, tf);
554e4b17023SJohn Marino   replace_goto_queue_stmt_list (eh_seq, tf);
555e4b17023SJohn Marino }
556e4b17023SJohn Marino 
557e4b17023SJohn Marino /* Add a new record to the goto queue contained in TF. NEW_STMT is the
558e4b17023SJohn Marino    data to be added, IS_LABEL indicates whether NEW_STMT is a label or
559e4b17023SJohn Marino    a gimple return. */
560e4b17023SJohn Marino 
561e4b17023SJohn Marino static void
record_in_goto_queue(struct leh_tf_state * tf,treemple new_stmt,int index,bool is_label)562e4b17023SJohn Marino record_in_goto_queue (struct leh_tf_state *tf,
563e4b17023SJohn Marino                       treemple new_stmt,
564e4b17023SJohn Marino                       int index,
565e4b17023SJohn Marino                       bool is_label)
566e4b17023SJohn Marino {
567e4b17023SJohn Marino   size_t active, size;
568e4b17023SJohn Marino   struct goto_queue_node *q;
569e4b17023SJohn Marino 
570e4b17023SJohn Marino   gcc_assert (!tf->goto_queue_map);
571e4b17023SJohn Marino 
572e4b17023SJohn Marino   active = tf->goto_queue_active;
573e4b17023SJohn Marino   size = tf->goto_queue_size;
574e4b17023SJohn Marino   if (active >= size)
575e4b17023SJohn Marino     {
576e4b17023SJohn Marino       size = (size ? size * 2 : 32);
577e4b17023SJohn Marino       tf->goto_queue_size = size;
578e4b17023SJohn Marino       tf->goto_queue
579e4b17023SJohn Marino          = XRESIZEVEC (struct goto_queue_node, tf->goto_queue, size);
580e4b17023SJohn Marino     }
581e4b17023SJohn Marino 
582e4b17023SJohn Marino   q = &tf->goto_queue[active];
583e4b17023SJohn Marino   tf->goto_queue_active = active + 1;
584e4b17023SJohn Marino 
585e4b17023SJohn Marino   memset (q, 0, sizeof (*q));
586e4b17023SJohn Marino   q->stmt = new_stmt;
587e4b17023SJohn Marino   q->index = index;
588e4b17023SJohn Marino   q->is_label = is_label;
589e4b17023SJohn Marino }
590e4b17023SJohn Marino 
591e4b17023SJohn Marino /* Record the LABEL label in the goto queue contained in TF.
592e4b17023SJohn Marino    TF is not null.  */
593e4b17023SJohn Marino 
594e4b17023SJohn Marino static void
record_in_goto_queue_label(struct leh_tf_state * tf,treemple stmt,tree label)595e4b17023SJohn Marino record_in_goto_queue_label (struct leh_tf_state *tf, treemple stmt, tree label)
596e4b17023SJohn Marino {
597e4b17023SJohn Marino   int index;
598e4b17023SJohn Marino   treemple temp, new_stmt;
599e4b17023SJohn Marino 
600e4b17023SJohn Marino   if (!label)
601e4b17023SJohn Marino     return;
602e4b17023SJohn Marino 
603e4b17023SJohn Marino   /* Computed and non-local gotos do not get processed.  Given
604e4b17023SJohn Marino      their nature we can neither tell whether we've escaped the
605e4b17023SJohn Marino      finally block nor redirect them if we knew.  */
606e4b17023SJohn Marino   if (TREE_CODE (label) != LABEL_DECL)
607e4b17023SJohn Marino     return;
608e4b17023SJohn Marino 
609e4b17023SJohn Marino   /* No need to record gotos that don't leave the try block.  */
610e4b17023SJohn Marino   temp.t = label;
611e4b17023SJohn Marino   if (!outside_finally_tree (temp, tf->try_finally_expr))
612e4b17023SJohn Marino     return;
613e4b17023SJohn Marino 
614e4b17023SJohn Marino   if (! tf->dest_array)
615e4b17023SJohn Marino     {
616e4b17023SJohn Marino       tf->dest_array = VEC_alloc (tree, heap, 10);
617e4b17023SJohn Marino       VEC_quick_push (tree, tf->dest_array, label);
618e4b17023SJohn Marino       index = 0;
619e4b17023SJohn Marino     }
620e4b17023SJohn Marino   else
621e4b17023SJohn Marino     {
622e4b17023SJohn Marino       int n = VEC_length (tree, tf->dest_array);
623e4b17023SJohn Marino       for (index = 0; index < n; ++index)
624e4b17023SJohn Marino         if (VEC_index (tree, tf->dest_array, index) == label)
625e4b17023SJohn Marino           break;
626e4b17023SJohn Marino       if (index == n)
627e4b17023SJohn Marino         VEC_safe_push (tree, heap, tf->dest_array, label);
628e4b17023SJohn Marino     }
629e4b17023SJohn Marino 
630e4b17023SJohn Marino   /* In the case of a GOTO we want to record the destination label,
631e4b17023SJohn Marino      since with a GIMPLE_COND we have an easy access to the then/else
632e4b17023SJohn Marino      labels. */
633e4b17023SJohn Marino   new_stmt = stmt;
634e4b17023SJohn Marino   record_in_goto_queue (tf, new_stmt, index, true);
635e4b17023SJohn Marino }
636e4b17023SJohn Marino 
637e4b17023SJohn Marino /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
638e4b17023SJohn Marino    node, and if so record that fact in the goto queue associated with that
639e4b17023SJohn Marino    try_finally node.  */
640e4b17023SJohn Marino 
641e4b17023SJohn Marino static void
maybe_record_in_goto_queue(struct leh_state * state,gimple stmt)642e4b17023SJohn Marino maybe_record_in_goto_queue (struct leh_state *state, gimple stmt)
643e4b17023SJohn Marino {
644e4b17023SJohn Marino   struct leh_tf_state *tf = state->tf;
645e4b17023SJohn Marino   treemple new_stmt;
646e4b17023SJohn Marino 
647e4b17023SJohn Marino   if (!tf)
648e4b17023SJohn Marino     return;
649e4b17023SJohn Marino 
650e4b17023SJohn Marino   switch (gimple_code (stmt))
651e4b17023SJohn Marino     {
652e4b17023SJohn Marino     case GIMPLE_COND:
653e4b17023SJohn Marino       new_stmt.tp = gimple_op_ptr (stmt, 2);
654e4b17023SJohn Marino       record_in_goto_queue_label (tf, new_stmt, gimple_cond_true_label (stmt));
655e4b17023SJohn Marino       new_stmt.tp = gimple_op_ptr (stmt, 3);
656e4b17023SJohn Marino       record_in_goto_queue_label (tf, new_stmt, gimple_cond_false_label (stmt));
657e4b17023SJohn Marino       break;
658e4b17023SJohn Marino     case GIMPLE_GOTO:
659e4b17023SJohn Marino       new_stmt.g = stmt;
660e4b17023SJohn Marino       record_in_goto_queue_label (tf, new_stmt, gimple_goto_dest (stmt));
661e4b17023SJohn Marino       break;
662e4b17023SJohn Marino 
663e4b17023SJohn Marino     case GIMPLE_RETURN:
664e4b17023SJohn Marino       tf->may_return = true;
665e4b17023SJohn Marino       new_stmt.g = stmt;
666e4b17023SJohn Marino       record_in_goto_queue (tf, new_stmt, -1, false);
667e4b17023SJohn Marino       break;
668e4b17023SJohn Marino 
669e4b17023SJohn Marino     default:
670e4b17023SJohn Marino       gcc_unreachable ();
671e4b17023SJohn Marino     }
672e4b17023SJohn Marino }
673e4b17023SJohn Marino 
674e4b17023SJohn Marino 
675e4b17023SJohn Marino #ifdef ENABLE_CHECKING
676e4b17023SJohn Marino /* We do not process GIMPLE_SWITCHes for now.  As long as the original source
677e4b17023SJohn Marino    was in fact structured, and we've not yet done jump threading, then none
678e4b17023SJohn Marino    of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this.  */
679e4b17023SJohn Marino 
680e4b17023SJohn Marino static void
verify_norecord_switch_expr(struct leh_state * state,gimple switch_expr)681e4b17023SJohn Marino verify_norecord_switch_expr (struct leh_state *state, gimple switch_expr)
682e4b17023SJohn Marino {
683e4b17023SJohn Marino   struct leh_tf_state *tf = state->tf;
684e4b17023SJohn Marino   size_t i, n;
685e4b17023SJohn Marino 
686e4b17023SJohn Marino   if (!tf)
687e4b17023SJohn Marino     return;
688e4b17023SJohn Marino 
689e4b17023SJohn Marino   n = gimple_switch_num_labels (switch_expr);
690e4b17023SJohn Marino 
691e4b17023SJohn Marino   for (i = 0; i < n; ++i)
692e4b17023SJohn Marino     {
693e4b17023SJohn Marino       treemple temp;
694e4b17023SJohn Marino       tree lab = CASE_LABEL (gimple_switch_label (switch_expr, i));
695e4b17023SJohn Marino       temp.t = lab;
696e4b17023SJohn Marino       gcc_assert (!outside_finally_tree (temp, tf->try_finally_expr));
697e4b17023SJohn Marino     }
698e4b17023SJohn Marino }
699e4b17023SJohn Marino #else
700e4b17023SJohn Marino #define verify_norecord_switch_expr(state, switch_expr)
701e4b17023SJohn Marino #endif
702e4b17023SJohn Marino 
703e4b17023SJohn Marino /* Redirect a RETURN_EXPR pointed to by Q to FINLAB.  If MOD is
704e4b17023SJohn Marino    non-null, insert it before the new branch.  */
705e4b17023SJohn Marino 
706e4b17023SJohn Marino static void
do_return_redirection(struct goto_queue_node * q,tree finlab,gimple_seq mod)707e4b17023SJohn Marino do_return_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod)
708e4b17023SJohn Marino {
709e4b17023SJohn Marino   gimple x;
710e4b17023SJohn Marino 
711e4b17023SJohn Marino   /* In the case of a return, the queue node must be a gimple statement.  */
712e4b17023SJohn Marino   gcc_assert (!q->is_label);
713e4b17023SJohn Marino 
714e4b17023SJohn Marino   /* Note that the return value may have already been computed, e.g.,
715e4b17023SJohn Marino 
716e4b17023SJohn Marino 	int x;
717e4b17023SJohn Marino 	int foo (void)
718e4b17023SJohn Marino 	{
719e4b17023SJohn Marino 	  x = 0;
720e4b17023SJohn Marino 	  try {
721e4b17023SJohn Marino 	    return x;
722e4b17023SJohn Marino 	  } finally {
723e4b17023SJohn Marino 	    x++;
724e4b17023SJohn Marino 	  }
725e4b17023SJohn Marino 	}
726e4b17023SJohn Marino 
727e4b17023SJohn Marino      should return 0, not 1.  We don't have to do anything to make
728e4b17023SJohn Marino      this happens because the return value has been placed in the
729e4b17023SJohn Marino      RESULT_DECL already.  */
730e4b17023SJohn Marino 
731e4b17023SJohn Marino   q->cont_stmt = q->stmt.g;
732e4b17023SJohn Marino 
733e4b17023SJohn Marino   if (!q->repl_stmt)
734e4b17023SJohn Marino     q->repl_stmt = gimple_seq_alloc ();
735e4b17023SJohn Marino 
736e4b17023SJohn Marino   if (mod)
737e4b17023SJohn Marino     gimple_seq_add_seq (&q->repl_stmt, mod);
738e4b17023SJohn Marino 
739e4b17023SJohn Marino   x = gimple_build_goto (finlab);
740e4b17023SJohn Marino   gimple_seq_add_stmt (&q->repl_stmt, x);
741e4b17023SJohn Marino }
742e4b17023SJohn Marino 
743e4b17023SJohn Marino /* Similar, but easier, for GIMPLE_GOTO.  */
744e4b17023SJohn Marino 
745e4b17023SJohn Marino static void
do_goto_redirection(struct goto_queue_node * q,tree finlab,gimple_seq mod,struct leh_tf_state * tf)746e4b17023SJohn Marino do_goto_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod,
747e4b17023SJohn Marino 		     struct leh_tf_state *tf)
748e4b17023SJohn Marino {
749e4b17023SJohn Marino   gimple x;
750e4b17023SJohn Marino 
751e4b17023SJohn Marino   gcc_assert (q->is_label);
752e4b17023SJohn Marino   if (!q->repl_stmt)
753e4b17023SJohn Marino     q->repl_stmt = gimple_seq_alloc ();
754e4b17023SJohn Marino 
755e4b17023SJohn Marino   q->cont_stmt = gimple_build_goto (VEC_index (tree, tf->dest_array, q->index));
756e4b17023SJohn Marino 
757e4b17023SJohn Marino   if (mod)
758e4b17023SJohn Marino     gimple_seq_add_seq (&q->repl_stmt, mod);
759e4b17023SJohn Marino 
760e4b17023SJohn Marino   x = gimple_build_goto (finlab);
761e4b17023SJohn Marino   gimple_seq_add_stmt (&q->repl_stmt, x);
762e4b17023SJohn Marino }
763e4b17023SJohn Marino 
764e4b17023SJohn Marino /* Emit a standard landing pad sequence into SEQ for REGION.  */
765e4b17023SJohn Marino 
766e4b17023SJohn Marino static void
emit_post_landing_pad(gimple_seq * seq,eh_region region)767e4b17023SJohn Marino emit_post_landing_pad (gimple_seq *seq, eh_region region)
768e4b17023SJohn Marino {
769e4b17023SJohn Marino   eh_landing_pad lp = region->landing_pads;
770e4b17023SJohn Marino   gimple x;
771e4b17023SJohn Marino 
772e4b17023SJohn Marino   if (lp == NULL)
773e4b17023SJohn Marino     lp = gen_eh_landing_pad (region);
774e4b17023SJohn Marino 
775e4b17023SJohn Marino   lp->post_landing_pad = create_artificial_label (UNKNOWN_LOCATION);
776e4b17023SJohn Marino   EH_LANDING_PAD_NR (lp->post_landing_pad) = lp->index;
777e4b17023SJohn Marino 
778e4b17023SJohn Marino   x = gimple_build_label (lp->post_landing_pad);
779e4b17023SJohn Marino   gimple_seq_add_stmt (seq, x);
780e4b17023SJohn Marino }
781e4b17023SJohn Marino 
782e4b17023SJohn Marino /* Emit a RESX statement into SEQ for REGION.  */
783e4b17023SJohn Marino 
784e4b17023SJohn Marino static void
emit_resx(gimple_seq * seq,eh_region region)785e4b17023SJohn Marino emit_resx (gimple_seq *seq, eh_region region)
786e4b17023SJohn Marino {
787e4b17023SJohn Marino   gimple x = gimple_build_resx (region->index);
788e4b17023SJohn Marino   gimple_seq_add_stmt (seq, x);
789e4b17023SJohn Marino   if (region->outer)
790e4b17023SJohn Marino     record_stmt_eh_region (region->outer, x);
791e4b17023SJohn Marino }
792e4b17023SJohn Marino 
793e4b17023SJohn Marino /* Emit an EH_DISPATCH statement into SEQ for REGION.  */
794e4b17023SJohn Marino 
795e4b17023SJohn Marino static void
emit_eh_dispatch(gimple_seq * seq,eh_region region)796e4b17023SJohn Marino emit_eh_dispatch (gimple_seq *seq, eh_region region)
797e4b17023SJohn Marino {
798e4b17023SJohn Marino   gimple x = gimple_build_eh_dispatch (region->index);
799e4b17023SJohn Marino   gimple_seq_add_stmt (seq, x);
800e4b17023SJohn Marino }
801e4b17023SJohn Marino 
802e4b17023SJohn Marino /* Note that the current EH region may contain a throw, or a
803e4b17023SJohn Marino    call to a function which itself may contain a throw.  */
804e4b17023SJohn Marino 
805e4b17023SJohn Marino static void
note_eh_region_may_contain_throw(eh_region region)806e4b17023SJohn Marino note_eh_region_may_contain_throw (eh_region region)
807e4b17023SJohn Marino {
808e4b17023SJohn Marino   while (bitmap_set_bit (eh_region_may_contain_throw_map, region->index))
809e4b17023SJohn Marino     {
810e4b17023SJohn Marino       if (region->type == ERT_MUST_NOT_THROW)
811e4b17023SJohn Marino 	break;
812e4b17023SJohn Marino       region = region->outer;
813e4b17023SJohn Marino       if (region == NULL)
814e4b17023SJohn Marino 	break;
815e4b17023SJohn Marino     }
816e4b17023SJohn Marino }
817e4b17023SJohn Marino 
818e4b17023SJohn Marino /* Check if REGION has been marked as containing a throw.  If REGION is
819e4b17023SJohn Marino    NULL, this predicate is false.  */
820e4b17023SJohn Marino 
821e4b17023SJohn Marino static inline bool
eh_region_may_contain_throw(eh_region r)822e4b17023SJohn Marino eh_region_may_contain_throw (eh_region r)
823e4b17023SJohn Marino {
824e4b17023SJohn Marino   return r && bitmap_bit_p (eh_region_may_contain_throw_map, r->index);
825e4b17023SJohn Marino }
826e4b17023SJohn Marino 
827e4b17023SJohn Marino /* We want to transform
828e4b17023SJohn Marino 	try { body; } catch { stuff; }
829e4b17023SJohn Marino    to
830e4b17023SJohn Marino 	normal_seqence:
831e4b17023SJohn Marino 	  body;
832e4b17023SJohn Marino 	  over:
833e4b17023SJohn Marino 	eh_seqence:
834e4b17023SJohn Marino 	  landing_pad:
835e4b17023SJohn Marino 	  stuff;
836e4b17023SJohn Marino 	  goto over;
837e4b17023SJohn Marino 
838e4b17023SJohn Marino    TP is a GIMPLE_TRY node.  REGION is the region whose post_landing_pad
839e4b17023SJohn Marino    should be placed before the second operand, or NULL.  OVER is
840e4b17023SJohn Marino    an existing label that should be put at the exit, or NULL.  */
841e4b17023SJohn Marino 
842e4b17023SJohn Marino static gimple_seq
frob_into_branch_around(gimple tp,eh_region region,tree over)843e4b17023SJohn Marino frob_into_branch_around (gimple tp, eh_region region, tree over)
844e4b17023SJohn Marino {
845e4b17023SJohn Marino   gimple x;
846e4b17023SJohn Marino   gimple_seq cleanup, result;
847e4b17023SJohn Marino   location_t loc = gimple_location (tp);
848e4b17023SJohn Marino 
849e4b17023SJohn Marino   cleanup = gimple_try_cleanup (tp);
850e4b17023SJohn Marino   result = gimple_try_eval (tp);
851e4b17023SJohn Marino 
852e4b17023SJohn Marino   if (region)
853e4b17023SJohn Marino     emit_post_landing_pad (&eh_seq, region);
854e4b17023SJohn Marino 
855e4b17023SJohn Marino   if (gimple_seq_may_fallthru (cleanup))
856e4b17023SJohn Marino     {
857e4b17023SJohn Marino       if (!over)
858e4b17023SJohn Marino 	over = create_artificial_label (loc);
859e4b17023SJohn Marino       x = gimple_build_goto (over);
860e4b17023SJohn Marino       gimple_seq_add_stmt (&cleanup, x);
861e4b17023SJohn Marino     }
862e4b17023SJohn Marino   gimple_seq_add_seq (&eh_seq, cleanup);
863e4b17023SJohn Marino 
864e4b17023SJohn Marino   if (over)
865e4b17023SJohn Marino     {
866e4b17023SJohn Marino       x = gimple_build_label (over);
867e4b17023SJohn Marino       gimple_seq_add_stmt (&result, x);
868e4b17023SJohn Marino     }
869e4b17023SJohn Marino   return result;
870e4b17023SJohn Marino }
871e4b17023SJohn Marino 
872e4b17023SJohn Marino /* A subroutine of lower_try_finally.  Duplicate the tree rooted at T.
873e4b17023SJohn Marino    Make sure to record all new labels found.  */
874e4b17023SJohn Marino 
875e4b17023SJohn Marino static gimple_seq
lower_try_finally_dup_block(gimple_seq seq,struct leh_state * outer_state)876e4b17023SJohn Marino lower_try_finally_dup_block (gimple_seq seq, struct leh_state *outer_state)
877e4b17023SJohn Marino {
878e4b17023SJohn Marino   gimple region = NULL;
879e4b17023SJohn Marino   gimple_seq new_seq;
880e4b17023SJohn Marino 
881e4b17023SJohn Marino   new_seq = copy_gimple_seq_and_replace_locals (seq);
882e4b17023SJohn Marino 
883e4b17023SJohn Marino   if (outer_state->tf)
884e4b17023SJohn Marino     region = outer_state->tf->try_finally_expr;
885e4b17023SJohn Marino   collect_finally_tree_1 (new_seq, region);
886e4b17023SJohn Marino 
887e4b17023SJohn Marino   return new_seq;
888e4b17023SJohn Marino }
889e4b17023SJohn Marino 
890e4b17023SJohn Marino /* A subroutine of lower_try_finally.  Create a fallthru label for
891e4b17023SJohn Marino    the given try_finally state.  The only tricky bit here is that
892e4b17023SJohn Marino    we have to make sure to record the label in our outer context.  */
893e4b17023SJohn Marino 
894e4b17023SJohn Marino static tree
lower_try_finally_fallthru_label(struct leh_tf_state * tf)895e4b17023SJohn Marino lower_try_finally_fallthru_label (struct leh_tf_state *tf)
896e4b17023SJohn Marino {
897e4b17023SJohn Marino   tree label = tf->fallthru_label;
898e4b17023SJohn Marino   treemple temp;
899e4b17023SJohn Marino 
900e4b17023SJohn Marino   if (!label)
901e4b17023SJohn Marino     {
902e4b17023SJohn Marino       label = create_artificial_label (gimple_location (tf->try_finally_expr));
903e4b17023SJohn Marino       tf->fallthru_label = label;
904e4b17023SJohn Marino       if (tf->outer->tf)
905e4b17023SJohn Marino         {
906e4b17023SJohn Marino           temp.t = label;
907e4b17023SJohn Marino           record_in_finally_tree (temp, tf->outer->tf->try_finally_expr);
908e4b17023SJohn Marino         }
909e4b17023SJohn Marino     }
910e4b17023SJohn Marino   return label;
911e4b17023SJohn Marino }
912e4b17023SJohn Marino 
913e4b17023SJohn Marino /* A subroutine of lower_try_finally.  If FINALLY consits of a
914e4b17023SJohn Marino    GIMPLE_EH_ELSE node, return it.  */
915e4b17023SJohn Marino 
916e4b17023SJohn Marino static inline gimple
get_eh_else(gimple_seq finally)917e4b17023SJohn Marino get_eh_else (gimple_seq finally)
918e4b17023SJohn Marino {
919e4b17023SJohn Marino   gimple x = gimple_seq_first_stmt (finally);
920e4b17023SJohn Marino   if (gimple_code (x) == GIMPLE_EH_ELSE)
921e4b17023SJohn Marino     {
922e4b17023SJohn Marino       gcc_assert (gimple_seq_singleton_p (finally));
923e4b17023SJohn Marino       return x;
924e4b17023SJohn Marino     }
925e4b17023SJohn Marino   return NULL;
926e4b17023SJohn Marino }
927e4b17023SJohn Marino 
928e4b17023SJohn Marino /* A subroutine of lower_try_finally.  If the eh_protect_cleanup_actions
929e4b17023SJohn Marino    langhook returns non-null, then the language requires that the exception
930e4b17023SJohn Marino    path out of a try_finally be treated specially.  To wit: the code within
931e4b17023SJohn Marino    the finally block may not itself throw an exception.  We have two choices
932e4b17023SJohn Marino    here. First we can duplicate the finally block and wrap it in a
933e4b17023SJohn Marino    must_not_throw region.  Second, we can generate code like
934e4b17023SJohn Marino 
935e4b17023SJohn Marino 	try {
936e4b17023SJohn Marino 	  finally_block;
937e4b17023SJohn Marino 	} catch {
938e4b17023SJohn Marino 	  if (fintmp == eh_edge)
939e4b17023SJohn Marino 	    protect_cleanup_actions;
940e4b17023SJohn Marino 	}
941e4b17023SJohn Marino 
942e4b17023SJohn Marino    where "fintmp" is the temporary used in the switch statement generation
943e4b17023SJohn Marino    alternative considered below.  For the nonce, we always choose the first
944e4b17023SJohn Marino    option.
945e4b17023SJohn Marino 
946e4b17023SJohn Marino    THIS_STATE may be null if this is a try-cleanup, not a try-finally.  */
947e4b17023SJohn Marino 
948e4b17023SJohn Marino static void
honor_protect_cleanup_actions(struct leh_state * outer_state,struct leh_state * this_state,struct leh_tf_state * tf)949e4b17023SJohn Marino honor_protect_cleanup_actions (struct leh_state *outer_state,
950e4b17023SJohn Marino 			       struct leh_state *this_state,
951e4b17023SJohn Marino 			       struct leh_tf_state *tf)
952e4b17023SJohn Marino {
953e4b17023SJohn Marino   tree protect_cleanup_actions;
954e4b17023SJohn Marino   gimple_stmt_iterator gsi;
955e4b17023SJohn Marino   bool finally_may_fallthru;
956e4b17023SJohn Marino   gimple_seq finally;
957e4b17023SJohn Marino   gimple x, eh_else;
958e4b17023SJohn Marino 
959e4b17023SJohn Marino   /* First check for nothing to do.  */
960e4b17023SJohn Marino   if (lang_hooks.eh_protect_cleanup_actions == NULL)
961e4b17023SJohn Marino     return;
962e4b17023SJohn Marino   protect_cleanup_actions = lang_hooks.eh_protect_cleanup_actions ();
963e4b17023SJohn Marino   if (protect_cleanup_actions == NULL)
964e4b17023SJohn Marino     return;
965e4b17023SJohn Marino 
966e4b17023SJohn Marino   finally = gimple_try_cleanup (tf->top_p);
967e4b17023SJohn Marino   eh_else = get_eh_else (finally);
968e4b17023SJohn Marino 
969e4b17023SJohn Marino   /* Duplicate the FINALLY block.  Only need to do this for try-finally,
970e4b17023SJohn Marino      and not for cleanups.  If we've got an EH_ELSE, extract it now.  */
971e4b17023SJohn Marino   if (eh_else)
972e4b17023SJohn Marino     {
973e4b17023SJohn Marino       finally = gimple_eh_else_e_body (eh_else);
974e4b17023SJohn Marino       gimple_try_set_cleanup (tf->top_p, gimple_eh_else_n_body (eh_else));
975e4b17023SJohn Marino     }
976e4b17023SJohn Marino   else if (this_state)
977e4b17023SJohn Marino     finally = lower_try_finally_dup_block (finally, outer_state);
978e4b17023SJohn Marino   finally_may_fallthru = gimple_seq_may_fallthru (finally);
979e4b17023SJohn Marino 
980e4b17023SJohn Marino   /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
981e4b17023SJohn Marino      set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
982e4b17023SJohn Marino      to be in an enclosing scope, but needs to be implemented at this level
983e4b17023SJohn Marino      to avoid a nesting violation (see wrap_temporary_cleanups in
984e4b17023SJohn Marino      cp/decl.c).  Since it's logically at an outer level, we should call
985e4b17023SJohn Marino      terminate before we get to it, so strip it away before adding the
986e4b17023SJohn Marino      MUST_NOT_THROW filter.  */
987e4b17023SJohn Marino   gsi = gsi_start (finally);
988e4b17023SJohn Marino   x = gsi_stmt (gsi);
989e4b17023SJohn Marino   if (gimple_code (x) == GIMPLE_TRY
990e4b17023SJohn Marino       && gimple_try_kind (x) == GIMPLE_TRY_CATCH
991e4b17023SJohn Marino       && gimple_try_catch_is_cleanup (x))
992e4b17023SJohn Marino     {
993e4b17023SJohn Marino       gsi_insert_seq_before (&gsi, gimple_try_eval (x), GSI_SAME_STMT);
994e4b17023SJohn Marino       gsi_remove (&gsi, false);
995e4b17023SJohn Marino     }
996e4b17023SJohn Marino 
997e4b17023SJohn Marino   /* Wrap the block with protect_cleanup_actions as the action.  */
998e4b17023SJohn Marino   x = gimple_build_eh_must_not_throw (protect_cleanup_actions);
999e4b17023SJohn Marino   x = gimple_build_try (finally, gimple_seq_alloc_with_stmt (x),
1000e4b17023SJohn Marino 			GIMPLE_TRY_CATCH);
1001e4b17023SJohn Marino   finally = lower_eh_must_not_throw (outer_state, x);
1002e4b17023SJohn Marino 
1003e4b17023SJohn Marino   /* Drop all of this into the exception sequence.  */
1004e4b17023SJohn Marino   emit_post_landing_pad (&eh_seq, tf->region);
1005e4b17023SJohn Marino   gimple_seq_add_seq (&eh_seq, finally);
1006e4b17023SJohn Marino   if (finally_may_fallthru)
1007e4b17023SJohn Marino     emit_resx (&eh_seq, tf->region);
1008e4b17023SJohn Marino 
1009e4b17023SJohn Marino   /* Having now been handled, EH isn't to be considered with
1010e4b17023SJohn Marino      the rest of the outgoing edges.  */
1011e4b17023SJohn Marino   tf->may_throw = false;
1012e4b17023SJohn Marino }
1013e4b17023SJohn Marino 
1014e4b17023SJohn Marino /* A subroutine of lower_try_finally.  We have determined that there is
1015e4b17023SJohn Marino    no fallthru edge out of the finally block.  This means that there is
1016e4b17023SJohn Marino    no outgoing edge corresponding to any incoming edge.  Restructure the
1017e4b17023SJohn Marino    try_finally node for this special case.  */
1018e4b17023SJohn Marino 
1019e4b17023SJohn Marino static void
lower_try_finally_nofallthru(struct leh_state * state,struct leh_tf_state * tf)1020e4b17023SJohn Marino lower_try_finally_nofallthru (struct leh_state *state,
1021e4b17023SJohn Marino 			      struct leh_tf_state *tf)
1022e4b17023SJohn Marino {
1023e4b17023SJohn Marino   tree lab;
1024e4b17023SJohn Marino   gimple x, eh_else;
1025e4b17023SJohn Marino   gimple_seq finally;
1026e4b17023SJohn Marino   struct goto_queue_node *q, *qe;
1027e4b17023SJohn Marino 
1028e4b17023SJohn Marino   lab = create_artificial_label (gimple_location (tf->try_finally_expr));
1029e4b17023SJohn Marino 
1030e4b17023SJohn Marino   /* We expect that tf->top_p is a GIMPLE_TRY. */
1031e4b17023SJohn Marino   finally = gimple_try_cleanup (tf->top_p);
1032e4b17023SJohn Marino   tf->top_p_seq = gimple_try_eval (tf->top_p);
1033e4b17023SJohn Marino 
1034e4b17023SJohn Marino   x = gimple_build_label (lab);
1035e4b17023SJohn Marino   gimple_seq_add_stmt (&tf->top_p_seq, x);
1036e4b17023SJohn Marino 
1037e4b17023SJohn Marino   q = tf->goto_queue;
1038e4b17023SJohn Marino   qe = q + tf->goto_queue_active;
1039e4b17023SJohn Marino   for (; q < qe; ++q)
1040e4b17023SJohn Marino     if (q->index < 0)
1041e4b17023SJohn Marino       do_return_redirection (q, lab, NULL);
1042e4b17023SJohn Marino     else
1043e4b17023SJohn Marino       do_goto_redirection (q, lab, NULL, tf);
1044e4b17023SJohn Marino 
1045e4b17023SJohn Marino   replace_goto_queue (tf);
1046e4b17023SJohn Marino 
1047e4b17023SJohn Marino   /* Emit the finally block into the stream.  Lower EH_ELSE at this time.  */
1048e4b17023SJohn Marino   eh_else = get_eh_else (finally);
1049e4b17023SJohn Marino   if (eh_else)
1050e4b17023SJohn Marino     {
1051e4b17023SJohn Marino       finally = gimple_eh_else_n_body (eh_else);
1052e4b17023SJohn Marino       lower_eh_constructs_1 (state, finally);
1053e4b17023SJohn Marino       gimple_seq_add_seq (&tf->top_p_seq, finally);
1054e4b17023SJohn Marino 
1055e4b17023SJohn Marino       if (tf->may_throw)
1056e4b17023SJohn Marino 	{
1057e4b17023SJohn Marino 	  finally = gimple_eh_else_e_body (eh_else);
1058e4b17023SJohn Marino 	  lower_eh_constructs_1 (state, finally);
1059e4b17023SJohn Marino 
1060e4b17023SJohn Marino 	  emit_post_landing_pad (&eh_seq, tf->region);
1061e4b17023SJohn Marino 	  gimple_seq_add_seq (&eh_seq, finally);
1062e4b17023SJohn Marino 	}
1063e4b17023SJohn Marino     }
1064e4b17023SJohn Marino   else
1065e4b17023SJohn Marino     {
1066e4b17023SJohn Marino       lower_eh_constructs_1 (state, finally);
1067e4b17023SJohn Marino       gimple_seq_add_seq (&tf->top_p_seq, finally);
1068e4b17023SJohn Marino 
1069e4b17023SJohn Marino       if (tf->may_throw)
1070e4b17023SJohn Marino 	{
1071e4b17023SJohn Marino 	  emit_post_landing_pad (&eh_seq, tf->region);
1072e4b17023SJohn Marino 
1073e4b17023SJohn Marino 	  x = gimple_build_goto (lab);
1074e4b17023SJohn Marino 	  gimple_seq_add_stmt (&eh_seq, x);
1075e4b17023SJohn Marino 	}
1076e4b17023SJohn Marino     }
1077e4b17023SJohn Marino }
1078e4b17023SJohn Marino 
1079e4b17023SJohn Marino /* A subroutine of lower_try_finally.  We have determined that there is
1080e4b17023SJohn Marino    exactly one destination of the finally block.  Restructure the
1081e4b17023SJohn Marino    try_finally node for this special case.  */
1082e4b17023SJohn Marino 
1083e4b17023SJohn Marino static void
lower_try_finally_onedest(struct leh_state * state,struct leh_tf_state * tf)1084e4b17023SJohn Marino lower_try_finally_onedest (struct leh_state *state, struct leh_tf_state *tf)
1085e4b17023SJohn Marino {
1086e4b17023SJohn Marino   struct goto_queue_node *q, *qe;
1087e4b17023SJohn Marino   gimple x;
1088e4b17023SJohn Marino   gimple_seq finally;
1089e4b17023SJohn Marino   tree finally_label;
1090e4b17023SJohn Marino   location_t loc = gimple_location (tf->try_finally_expr);
1091e4b17023SJohn Marino 
1092e4b17023SJohn Marino   finally = gimple_try_cleanup (tf->top_p);
1093e4b17023SJohn Marino   tf->top_p_seq = gimple_try_eval (tf->top_p);
1094e4b17023SJohn Marino 
1095e4b17023SJohn Marino   /* Since there's only one destination, and the destination edge can only
1096e4b17023SJohn Marino      either be EH or non-EH, that implies that all of our incoming edges
1097e4b17023SJohn Marino      are of the same type.  Therefore we can lower EH_ELSE immediately.  */
1098e4b17023SJohn Marino   x = get_eh_else (finally);
1099e4b17023SJohn Marino   if (x)
1100e4b17023SJohn Marino     {
1101e4b17023SJohn Marino       if (tf->may_throw)
1102e4b17023SJohn Marino 	finally = gimple_eh_else_e_body (x);
1103e4b17023SJohn Marino       else
1104e4b17023SJohn Marino 	finally = gimple_eh_else_n_body (x);
1105e4b17023SJohn Marino     }
1106e4b17023SJohn Marino 
1107e4b17023SJohn Marino   lower_eh_constructs_1 (state, finally);
1108e4b17023SJohn Marino 
1109e4b17023SJohn Marino   if (tf->may_throw)
1110e4b17023SJohn Marino     {
1111e4b17023SJohn Marino       /* Only reachable via the exception edge.  Add the given label to
1112e4b17023SJohn Marino          the head of the FINALLY block.  Append a RESX at the end.  */
1113e4b17023SJohn Marino       emit_post_landing_pad (&eh_seq, tf->region);
1114e4b17023SJohn Marino       gimple_seq_add_seq (&eh_seq, finally);
1115e4b17023SJohn Marino       emit_resx (&eh_seq, tf->region);
1116e4b17023SJohn Marino       return;
1117e4b17023SJohn Marino     }
1118e4b17023SJohn Marino 
1119e4b17023SJohn Marino   if (tf->may_fallthru)
1120e4b17023SJohn Marino     {
1121e4b17023SJohn Marino       /* Only reachable via the fallthru edge.  Do nothing but let
1122e4b17023SJohn Marino 	 the two blocks run together; we'll fall out the bottom.  */
1123e4b17023SJohn Marino       gimple_seq_add_seq (&tf->top_p_seq, finally);
1124e4b17023SJohn Marino       return;
1125e4b17023SJohn Marino     }
1126e4b17023SJohn Marino 
1127e4b17023SJohn Marino   finally_label = create_artificial_label (loc);
1128e4b17023SJohn Marino   x = gimple_build_label (finally_label);
1129e4b17023SJohn Marino   gimple_seq_add_stmt (&tf->top_p_seq, x);
1130e4b17023SJohn Marino 
1131e4b17023SJohn Marino   gimple_seq_add_seq (&tf->top_p_seq, finally);
1132e4b17023SJohn Marino 
1133e4b17023SJohn Marino   q = tf->goto_queue;
1134e4b17023SJohn Marino   qe = q + tf->goto_queue_active;
1135e4b17023SJohn Marino 
1136e4b17023SJohn Marino   if (tf->may_return)
1137e4b17023SJohn Marino     {
1138e4b17023SJohn Marino       /* Reachable by return expressions only.  Redirect them.  */
1139e4b17023SJohn Marino       for (; q < qe; ++q)
1140e4b17023SJohn Marino 	do_return_redirection (q, finally_label, NULL);
1141e4b17023SJohn Marino       replace_goto_queue (tf);
1142e4b17023SJohn Marino     }
1143e4b17023SJohn Marino   else
1144e4b17023SJohn Marino     {
1145e4b17023SJohn Marino       /* Reachable by goto expressions only.  Redirect them.  */
1146e4b17023SJohn Marino       for (; q < qe; ++q)
1147e4b17023SJohn Marino 	do_goto_redirection (q, finally_label, NULL, tf);
1148e4b17023SJohn Marino       replace_goto_queue (tf);
1149e4b17023SJohn Marino 
1150e4b17023SJohn Marino       if (VEC_index (tree, tf->dest_array, 0) == tf->fallthru_label)
1151e4b17023SJohn Marino 	{
1152e4b17023SJohn Marino 	  /* Reachable by goto to fallthru label only.  Redirect it
1153e4b17023SJohn Marino 	     to the new label (already created, sadly), and do not
1154e4b17023SJohn Marino 	     emit the final branch out, or the fallthru label.  */
1155e4b17023SJohn Marino 	  tf->fallthru_label = NULL;
1156e4b17023SJohn Marino 	  return;
1157e4b17023SJohn Marino 	}
1158e4b17023SJohn Marino     }
1159e4b17023SJohn Marino 
1160e4b17023SJohn Marino   /* Place the original return/goto to the original destination
1161e4b17023SJohn Marino      immediately after the finally block. */
1162e4b17023SJohn Marino   x = tf->goto_queue[0].cont_stmt;
1163e4b17023SJohn Marino   gimple_seq_add_stmt (&tf->top_p_seq, x);
1164e4b17023SJohn Marino   maybe_record_in_goto_queue (state, x);
1165e4b17023SJohn Marino }
1166e4b17023SJohn Marino 
1167e4b17023SJohn Marino /* A subroutine of lower_try_finally.  There are multiple edges incoming
1168e4b17023SJohn Marino    and outgoing from the finally block.  Implement this by duplicating the
1169e4b17023SJohn Marino    finally block for every destination.  */
1170e4b17023SJohn Marino 
1171e4b17023SJohn Marino static void
lower_try_finally_copy(struct leh_state * state,struct leh_tf_state * tf)1172e4b17023SJohn Marino lower_try_finally_copy (struct leh_state *state, struct leh_tf_state *tf)
1173e4b17023SJohn Marino {
1174e4b17023SJohn Marino   gimple_seq finally;
1175e4b17023SJohn Marino   gimple_seq new_stmt;
1176e4b17023SJohn Marino   gimple_seq seq;
1177e4b17023SJohn Marino   gimple x, eh_else;
1178e4b17023SJohn Marino   tree tmp;
1179e4b17023SJohn Marino   location_t tf_loc = gimple_location (tf->try_finally_expr);
1180e4b17023SJohn Marino 
1181e4b17023SJohn Marino   finally = gimple_try_cleanup (tf->top_p);
1182e4b17023SJohn Marino 
1183e4b17023SJohn Marino   /* Notice EH_ELSE, and simplify some of the remaining code
1184e4b17023SJohn Marino      by considering FINALLY to be the normal return path only.  */
1185e4b17023SJohn Marino   eh_else = get_eh_else (finally);
1186e4b17023SJohn Marino   if (eh_else)
1187e4b17023SJohn Marino     finally = gimple_eh_else_n_body (eh_else);
1188e4b17023SJohn Marino 
1189e4b17023SJohn Marino   tf->top_p_seq = gimple_try_eval (tf->top_p);
1190e4b17023SJohn Marino   new_stmt = NULL;
1191e4b17023SJohn Marino 
1192e4b17023SJohn Marino   if (tf->may_fallthru)
1193e4b17023SJohn Marino     {
1194e4b17023SJohn Marino       seq = lower_try_finally_dup_block (finally, state);
1195e4b17023SJohn Marino       lower_eh_constructs_1 (state, seq);
1196e4b17023SJohn Marino       gimple_seq_add_seq (&new_stmt, seq);
1197e4b17023SJohn Marino 
1198e4b17023SJohn Marino       tmp = lower_try_finally_fallthru_label (tf);
1199e4b17023SJohn Marino       x = gimple_build_goto (tmp);
1200e4b17023SJohn Marino       gimple_seq_add_stmt (&new_stmt, x);
1201e4b17023SJohn Marino     }
1202e4b17023SJohn Marino 
1203e4b17023SJohn Marino   if (tf->may_throw)
1204e4b17023SJohn Marino     {
1205e4b17023SJohn Marino       /* We don't need to copy the EH path of EH_ELSE,
1206e4b17023SJohn Marino 	 since it is only emitted once.  */
1207e4b17023SJohn Marino       if (eh_else)
1208e4b17023SJohn Marino 	seq = gimple_eh_else_e_body (eh_else);
1209e4b17023SJohn Marino       else
1210e4b17023SJohn Marino 	seq = lower_try_finally_dup_block (finally, state);
1211e4b17023SJohn Marino       lower_eh_constructs_1 (state, seq);
1212e4b17023SJohn Marino 
1213e4b17023SJohn Marino       emit_post_landing_pad (&eh_seq, tf->region);
1214e4b17023SJohn Marino       gimple_seq_add_seq (&eh_seq, seq);
1215e4b17023SJohn Marino       emit_resx (&eh_seq, tf->region);
1216e4b17023SJohn Marino     }
1217e4b17023SJohn Marino 
1218e4b17023SJohn Marino   if (tf->goto_queue)
1219e4b17023SJohn Marino     {
1220e4b17023SJohn Marino       struct goto_queue_node *q, *qe;
1221e4b17023SJohn Marino       int return_index, index;
1222e4b17023SJohn Marino       struct labels_s
1223e4b17023SJohn Marino       {
1224e4b17023SJohn Marino 	struct goto_queue_node *q;
1225e4b17023SJohn Marino 	tree label;
1226e4b17023SJohn Marino       } *labels;
1227e4b17023SJohn Marino 
1228e4b17023SJohn Marino       return_index = VEC_length (tree, tf->dest_array);
1229e4b17023SJohn Marino       labels = XCNEWVEC (struct labels_s, return_index + 1);
1230e4b17023SJohn Marino 
1231e4b17023SJohn Marino       q = tf->goto_queue;
1232e4b17023SJohn Marino       qe = q + tf->goto_queue_active;
1233e4b17023SJohn Marino       for (; q < qe; q++)
1234e4b17023SJohn Marino 	{
1235e4b17023SJohn Marino 	  index = q->index < 0 ? return_index : q->index;
1236e4b17023SJohn Marino 
1237e4b17023SJohn Marino 	  if (!labels[index].q)
1238e4b17023SJohn Marino 	    labels[index].q = q;
1239e4b17023SJohn Marino 	}
1240e4b17023SJohn Marino 
1241e4b17023SJohn Marino       for (index = 0; index < return_index + 1; index++)
1242e4b17023SJohn Marino 	{
1243e4b17023SJohn Marino 	  tree lab;
1244e4b17023SJohn Marino 
1245e4b17023SJohn Marino 	  q = labels[index].q;
1246e4b17023SJohn Marino 	  if (! q)
1247e4b17023SJohn Marino 	    continue;
1248e4b17023SJohn Marino 
1249e4b17023SJohn Marino 	  lab = labels[index].label
1250e4b17023SJohn Marino 	    = create_artificial_label (tf_loc);
1251e4b17023SJohn Marino 
1252e4b17023SJohn Marino 	  if (index == return_index)
1253e4b17023SJohn Marino 	    do_return_redirection (q, lab, NULL);
1254e4b17023SJohn Marino 	  else
1255e4b17023SJohn Marino 	    do_goto_redirection (q, lab, NULL, tf);
1256e4b17023SJohn Marino 
1257e4b17023SJohn Marino 	  x = gimple_build_label (lab);
1258e4b17023SJohn Marino           gimple_seq_add_stmt (&new_stmt, x);
1259e4b17023SJohn Marino 
1260e4b17023SJohn Marino 	  seq = lower_try_finally_dup_block (finally, state);
1261e4b17023SJohn Marino 	  lower_eh_constructs_1 (state, seq);
1262e4b17023SJohn Marino           gimple_seq_add_seq (&new_stmt, seq);
1263e4b17023SJohn Marino 
1264e4b17023SJohn Marino           gimple_seq_add_stmt (&new_stmt, q->cont_stmt);
1265e4b17023SJohn Marino 	  maybe_record_in_goto_queue (state, q->cont_stmt);
1266e4b17023SJohn Marino 	}
1267e4b17023SJohn Marino 
1268e4b17023SJohn Marino       for (q = tf->goto_queue; q < qe; q++)
1269e4b17023SJohn Marino 	{
1270e4b17023SJohn Marino 	  tree lab;
1271e4b17023SJohn Marino 
1272e4b17023SJohn Marino 	  index = q->index < 0 ? return_index : q->index;
1273e4b17023SJohn Marino 
1274e4b17023SJohn Marino 	  if (labels[index].q == q)
1275e4b17023SJohn Marino 	    continue;
1276e4b17023SJohn Marino 
1277e4b17023SJohn Marino 	  lab = labels[index].label;
1278e4b17023SJohn Marino 
1279e4b17023SJohn Marino 	  if (index == return_index)
1280e4b17023SJohn Marino 	    do_return_redirection (q, lab, NULL);
1281e4b17023SJohn Marino 	  else
1282e4b17023SJohn Marino 	    do_goto_redirection (q, lab, NULL, tf);
1283e4b17023SJohn Marino 	}
1284e4b17023SJohn Marino 
1285e4b17023SJohn Marino       replace_goto_queue (tf);
1286e4b17023SJohn Marino       free (labels);
1287e4b17023SJohn Marino     }
1288e4b17023SJohn Marino 
1289e4b17023SJohn Marino   /* Need to link new stmts after running replace_goto_queue due
1290e4b17023SJohn Marino      to not wanting to process the same goto stmts twice.  */
1291e4b17023SJohn Marino   gimple_seq_add_seq (&tf->top_p_seq, new_stmt);
1292e4b17023SJohn Marino }
1293e4b17023SJohn Marino 
1294e4b17023SJohn Marino /* A subroutine of lower_try_finally.  There are multiple edges incoming
1295e4b17023SJohn Marino    and outgoing from the finally block.  Implement this by instrumenting
1296e4b17023SJohn Marino    each incoming edge and creating a switch statement at the end of the
1297e4b17023SJohn Marino    finally block that branches to the appropriate destination.  */
1298e4b17023SJohn Marino 
1299e4b17023SJohn Marino static void
lower_try_finally_switch(struct leh_state * state,struct leh_tf_state * tf)1300e4b17023SJohn Marino lower_try_finally_switch (struct leh_state *state, struct leh_tf_state *tf)
1301e4b17023SJohn Marino {
1302e4b17023SJohn Marino   struct goto_queue_node *q, *qe;
1303e4b17023SJohn Marino   tree finally_tmp, finally_label;
1304e4b17023SJohn Marino   int return_index, eh_index, fallthru_index;
1305e4b17023SJohn Marino   int nlabels, ndests, j, last_case_index;
1306e4b17023SJohn Marino   tree last_case;
1307e4b17023SJohn Marino   VEC (tree,heap) *case_label_vec;
1308e4b17023SJohn Marino   gimple_seq switch_body;
1309e4b17023SJohn Marino   gimple x, eh_else;
1310e4b17023SJohn Marino   tree tmp;
1311e4b17023SJohn Marino   gimple switch_stmt;
1312e4b17023SJohn Marino   gimple_seq finally;
1313e4b17023SJohn Marino   struct pointer_map_t *cont_map = NULL;
1314e4b17023SJohn Marino   /* The location of the TRY_FINALLY stmt.  */
1315e4b17023SJohn Marino   location_t tf_loc = gimple_location (tf->try_finally_expr);
1316e4b17023SJohn Marino   /* The location of the finally block.  */
1317e4b17023SJohn Marino   location_t finally_loc;
1318e4b17023SJohn Marino 
1319e4b17023SJohn Marino   switch_body = gimple_seq_alloc ();
1320e4b17023SJohn Marino   finally = gimple_try_cleanup (tf->top_p);
1321e4b17023SJohn Marino   eh_else = get_eh_else (finally);
1322e4b17023SJohn Marino 
1323e4b17023SJohn Marino   /* Mash the TRY block to the head of the chain.  */
1324e4b17023SJohn Marino   tf->top_p_seq = gimple_try_eval (tf->top_p);
1325e4b17023SJohn Marino 
1326e4b17023SJohn Marino   /* The location of the finally is either the last stmt in the finally
1327e4b17023SJohn Marino      block or the location of the TRY_FINALLY itself.  */
1328e4b17023SJohn Marino   x = gimple_seq_last_stmt (finally);
1329e4b17023SJohn Marino   finally_loc = x ? gimple_location (x) : tf_loc;
1330e4b17023SJohn Marino 
1331e4b17023SJohn Marino   /* Prepare for switch statement generation.  */
1332e4b17023SJohn Marino   nlabels = VEC_length (tree, tf->dest_array);
1333e4b17023SJohn Marino   return_index = nlabels;
1334e4b17023SJohn Marino   eh_index = return_index + tf->may_return;
1335e4b17023SJohn Marino   fallthru_index = eh_index + (tf->may_throw && !eh_else);
1336e4b17023SJohn Marino   ndests = fallthru_index + tf->may_fallthru;
1337e4b17023SJohn Marino 
1338e4b17023SJohn Marino   finally_tmp = create_tmp_var (integer_type_node, "finally_tmp");
1339e4b17023SJohn Marino   finally_label = create_artificial_label (finally_loc);
1340e4b17023SJohn Marino 
1341e4b17023SJohn Marino   /* We use VEC_quick_push on case_label_vec throughout this function,
1342e4b17023SJohn Marino      since we know the size in advance and allocate precisely as muce
1343e4b17023SJohn Marino      space as needed.  */
1344e4b17023SJohn Marino   case_label_vec = VEC_alloc (tree, heap, ndests);
1345e4b17023SJohn Marino   last_case = NULL;
1346e4b17023SJohn Marino   last_case_index = 0;
1347e4b17023SJohn Marino 
1348e4b17023SJohn Marino   /* Begin inserting code for getting to the finally block.  Things
1349e4b17023SJohn Marino      are done in this order to correspond to the sequence the code is
1350e4b17023SJohn Marino      layed out.  */
1351e4b17023SJohn Marino 
1352e4b17023SJohn Marino   if (tf->may_fallthru)
1353e4b17023SJohn Marino     {
1354e4b17023SJohn Marino       x = gimple_build_assign (finally_tmp,
1355e4b17023SJohn Marino 			       build_int_cst (integer_type_node,
1356e4b17023SJohn Marino 					      fallthru_index));
1357e4b17023SJohn Marino       gimple_seq_add_stmt (&tf->top_p_seq, x);
1358e4b17023SJohn Marino 
1359e4b17023SJohn Marino       tmp = build_int_cst (integer_type_node, fallthru_index);
1360e4b17023SJohn Marino       last_case = build_case_label (tmp, NULL,
1361e4b17023SJohn Marino 				    create_artificial_label (tf_loc));
1362e4b17023SJohn Marino       VEC_quick_push (tree, case_label_vec, last_case);
1363e4b17023SJohn Marino       last_case_index++;
1364e4b17023SJohn Marino 
1365e4b17023SJohn Marino       x = gimple_build_label (CASE_LABEL (last_case));
1366e4b17023SJohn Marino       gimple_seq_add_stmt (&switch_body, x);
1367e4b17023SJohn Marino 
1368e4b17023SJohn Marino       tmp = lower_try_finally_fallthru_label (tf);
1369e4b17023SJohn Marino       x = gimple_build_goto (tmp);
1370e4b17023SJohn Marino       gimple_seq_add_stmt (&switch_body, x);
1371e4b17023SJohn Marino     }
1372e4b17023SJohn Marino 
1373e4b17023SJohn Marino   /* For EH_ELSE, emit the exception path (plus resx) now, then
1374e4b17023SJohn Marino      subsequently we only need consider the normal path.  */
1375e4b17023SJohn Marino   if (eh_else)
1376e4b17023SJohn Marino     {
1377e4b17023SJohn Marino       if (tf->may_throw)
1378e4b17023SJohn Marino 	{
1379e4b17023SJohn Marino 	  finally = gimple_eh_else_e_body (eh_else);
1380e4b17023SJohn Marino 	  lower_eh_constructs_1 (state, finally);
1381e4b17023SJohn Marino 
1382e4b17023SJohn Marino 	  emit_post_landing_pad (&eh_seq, tf->region);
1383e4b17023SJohn Marino 	  gimple_seq_add_seq (&eh_seq, finally);
1384e4b17023SJohn Marino 	  emit_resx (&eh_seq, tf->region);
1385e4b17023SJohn Marino 	}
1386e4b17023SJohn Marino 
1387e4b17023SJohn Marino       finally = gimple_eh_else_n_body (eh_else);
1388e4b17023SJohn Marino     }
1389e4b17023SJohn Marino   else if (tf->may_throw)
1390e4b17023SJohn Marino     {
1391e4b17023SJohn Marino       emit_post_landing_pad (&eh_seq, tf->region);
1392e4b17023SJohn Marino 
1393e4b17023SJohn Marino       x = gimple_build_assign (finally_tmp,
1394e4b17023SJohn Marino 			       build_int_cst (integer_type_node, eh_index));
1395e4b17023SJohn Marino       gimple_seq_add_stmt (&eh_seq, x);
1396e4b17023SJohn Marino 
1397e4b17023SJohn Marino       x = gimple_build_goto (finally_label);
1398e4b17023SJohn Marino       gimple_seq_add_stmt (&eh_seq, x);
1399e4b17023SJohn Marino 
1400e4b17023SJohn Marino       tmp = build_int_cst (integer_type_node, eh_index);
1401e4b17023SJohn Marino       last_case = build_case_label (tmp, NULL,
1402e4b17023SJohn Marino 				    create_artificial_label (tf_loc));
1403e4b17023SJohn Marino       VEC_quick_push (tree, case_label_vec, last_case);
1404e4b17023SJohn Marino       last_case_index++;
1405e4b17023SJohn Marino 
1406e4b17023SJohn Marino       x = gimple_build_label (CASE_LABEL (last_case));
1407e4b17023SJohn Marino       gimple_seq_add_stmt (&eh_seq, x);
1408e4b17023SJohn Marino       emit_resx (&eh_seq, tf->region);
1409e4b17023SJohn Marino     }
1410e4b17023SJohn Marino 
1411e4b17023SJohn Marino   x = gimple_build_label (finally_label);
1412e4b17023SJohn Marino   gimple_seq_add_stmt (&tf->top_p_seq, x);
1413e4b17023SJohn Marino 
1414*95d28233SJohn Marino   lower_eh_constructs_1 (state, finally);
1415e4b17023SJohn Marino   gimple_seq_add_seq (&tf->top_p_seq, finally);
1416e4b17023SJohn Marino 
1417e4b17023SJohn Marino   /* Redirect each incoming goto edge.  */
1418e4b17023SJohn Marino   q = tf->goto_queue;
1419e4b17023SJohn Marino   qe = q + tf->goto_queue_active;
1420e4b17023SJohn Marino   j = last_case_index + tf->may_return;
1421e4b17023SJohn Marino   /* Prepare the assignments to finally_tmp that are executed upon the
1422e4b17023SJohn Marino      entrance through a particular edge. */
1423e4b17023SJohn Marino   for (; q < qe; ++q)
1424e4b17023SJohn Marino     {
1425e4b17023SJohn Marino       gimple_seq mod;
1426e4b17023SJohn Marino       int switch_id;
1427e4b17023SJohn Marino       unsigned int case_index;
1428e4b17023SJohn Marino 
1429e4b17023SJohn Marino       mod = gimple_seq_alloc ();
1430e4b17023SJohn Marino 
1431e4b17023SJohn Marino       if (q->index < 0)
1432e4b17023SJohn Marino 	{
1433e4b17023SJohn Marino 	  x = gimple_build_assign (finally_tmp,
1434e4b17023SJohn Marino 				   build_int_cst (integer_type_node,
1435e4b17023SJohn Marino 						  return_index));
1436e4b17023SJohn Marino 	  gimple_seq_add_stmt (&mod, x);
1437e4b17023SJohn Marino 	  do_return_redirection (q, finally_label, mod);
1438e4b17023SJohn Marino 	  switch_id = return_index;
1439e4b17023SJohn Marino 	}
1440e4b17023SJohn Marino       else
1441e4b17023SJohn Marino 	{
1442e4b17023SJohn Marino 	  x = gimple_build_assign (finally_tmp,
1443e4b17023SJohn Marino 				   build_int_cst (integer_type_node, q->index));
1444e4b17023SJohn Marino 	  gimple_seq_add_stmt (&mod, x);
1445e4b17023SJohn Marino 	  do_goto_redirection (q, finally_label, mod, tf);
1446e4b17023SJohn Marino 	  switch_id = q->index;
1447e4b17023SJohn Marino 	}
1448e4b17023SJohn Marino 
1449e4b17023SJohn Marino       case_index = j + q->index;
1450e4b17023SJohn Marino       if (VEC_length (tree, case_label_vec) <= case_index
1451e4b17023SJohn Marino           || !VEC_index (tree, case_label_vec, case_index))
1452e4b17023SJohn Marino         {
1453e4b17023SJohn Marino           tree case_lab;
1454e4b17023SJohn Marino           void **slot;
1455e4b17023SJohn Marino 	  tmp = build_int_cst (integer_type_node, switch_id);
1456e4b17023SJohn Marino           case_lab = build_case_label (tmp, NULL,
1457e4b17023SJohn Marino 				       create_artificial_label (tf_loc));
1458e4b17023SJohn Marino           /* We store the cont_stmt in the pointer map, so that we can recover
1459e4b17023SJohn Marino              it in the loop below.  */
1460e4b17023SJohn Marino           if (!cont_map)
1461e4b17023SJohn Marino             cont_map = pointer_map_create ();
1462e4b17023SJohn Marino           slot = pointer_map_insert (cont_map, case_lab);
1463e4b17023SJohn Marino           *slot = q->cont_stmt;
1464e4b17023SJohn Marino           VEC_quick_push (tree, case_label_vec, case_lab);
1465e4b17023SJohn Marino         }
1466e4b17023SJohn Marino     }
1467e4b17023SJohn Marino   for (j = last_case_index; j < last_case_index + nlabels; j++)
1468e4b17023SJohn Marino     {
1469e4b17023SJohn Marino       gimple cont_stmt;
1470e4b17023SJohn Marino       void **slot;
1471e4b17023SJohn Marino 
1472e4b17023SJohn Marino       last_case = VEC_index (tree, case_label_vec, j);
1473e4b17023SJohn Marino 
1474e4b17023SJohn Marino       gcc_assert (last_case);
1475e4b17023SJohn Marino       gcc_assert (cont_map);
1476e4b17023SJohn Marino 
1477e4b17023SJohn Marino       slot = pointer_map_contains (cont_map, last_case);
1478e4b17023SJohn Marino       gcc_assert (slot);
1479e4b17023SJohn Marino       cont_stmt = *(gimple *) slot;
1480e4b17023SJohn Marino 
1481e4b17023SJohn Marino       x = gimple_build_label (CASE_LABEL (last_case));
1482e4b17023SJohn Marino       gimple_seq_add_stmt (&switch_body, x);
1483e4b17023SJohn Marino       gimple_seq_add_stmt (&switch_body, cont_stmt);
1484e4b17023SJohn Marino       maybe_record_in_goto_queue (state, cont_stmt);
1485e4b17023SJohn Marino     }
1486e4b17023SJohn Marino   if (cont_map)
1487e4b17023SJohn Marino     pointer_map_destroy (cont_map);
1488e4b17023SJohn Marino 
1489e4b17023SJohn Marino   replace_goto_queue (tf);
1490e4b17023SJohn Marino 
1491e4b17023SJohn Marino   /* Make sure that the last case is the default label, as one is required.
1492e4b17023SJohn Marino      Then sort the labels, which is also required in GIMPLE.  */
1493e4b17023SJohn Marino   CASE_LOW (last_case) = NULL;
1494e4b17023SJohn Marino   sort_case_labels (case_label_vec);
1495e4b17023SJohn Marino 
1496e4b17023SJohn Marino   /* Build the switch statement, setting last_case to be the default
1497e4b17023SJohn Marino      label.  */
1498e4b17023SJohn Marino   switch_stmt = gimple_build_switch_vec (finally_tmp, last_case,
1499e4b17023SJohn Marino                                          case_label_vec);
1500e4b17023SJohn Marino   gimple_set_location (switch_stmt, finally_loc);
1501e4b17023SJohn Marino 
1502e4b17023SJohn Marino   /* Need to link SWITCH_STMT after running replace_goto_queue
1503e4b17023SJohn Marino      due to not wanting to process the same goto stmts twice.  */
1504e4b17023SJohn Marino   gimple_seq_add_stmt (&tf->top_p_seq, switch_stmt);
1505e4b17023SJohn Marino   gimple_seq_add_seq (&tf->top_p_seq, switch_body);
1506e4b17023SJohn Marino }
1507e4b17023SJohn Marino 
1508e4b17023SJohn Marino /* Decide whether or not we are going to duplicate the finally block.
1509e4b17023SJohn Marino    There are several considerations.
1510e4b17023SJohn Marino 
1511e4b17023SJohn Marino    First, if this is Java, then the finally block contains code
1512e4b17023SJohn Marino    written by the user.  It has line numbers associated with it,
1513e4b17023SJohn Marino    so duplicating the block means it's difficult to set a breakpoint.
1514e4b17023SJohn Marino    Since controlling code generation via -g is verboten, we simply
1515e4b17023SJohn Marino    never duplicate code without optimization.
1516e4b17023SJohn Marino 
1517e4b17023SJohn Marino    Second, we'd like to prevent egregious code growth.  One way to
1518e4b17023SJohn Marino    do this is to estimate the size of the finally block, multiply
1519e4b17023SJohn Marino    that by the number of copies we'd need to make, and compare against
1520e4b17023SJohn Marino    the estimate of the size of the switch machinery we'd have to add.  */
1521e4b17023SJohn Marino 
1522e4b17023SJohn Marino static bool
decide_copy_try_finally(int ndests,bool may_throw,gimple_seq finally)1523e4b17023SJohn Marino decide_copy_try_finally (int ndests, bool may_throw, gimple_seq finally)
1524e4b17023SJohn Marino {
1525e4b17023SJohn Marino   int f_estimate, sw_estimate;
1526e4b17023SJohn Marino   gimple eh_else;
1527e4b17023SJohn Marino 
1528e4b17023SJohn Marino   /* If there's an EH_ELSE involved, the exception path is separate
1529e4b17023SJohn Marino      and really doesn't come into play for this computation.  */
1530e4b17023SJohn Marino   eh_else = get_eh_else (finally);
1531e4b17023SJohn Marino   if (eh_else)
1532e4b17023SJohn Marino     {
1533e4b17023SJohn Marino       ndests -= may_throw;
1534e4b17023SJohn Marino       finally = gimple_eh_else_n_body (eh_else);
1535e4b17023SJohn Marino     }
1536e4b17023SJohn Marino 
1537e4b17023SJohn Marino   if (!optimize)
1538e4b17023SJohn Marino     {
1539e4b17023SJohn Marino       gimple_stmt_iterator gsi;
1540e4b17023SJohn Marino 
1541e4b17023SJohn Marino       if (ndests == 1)
1542e4b17023SJohn Marino         return true;
1543e4b17023SJohn Marino 
1544e4b17023SJohn Marino       for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1545e4b17023SJohn Marino 	{
1546e4b17023SJohn Marino 	  gimple stmt = gsi_stmt (gsi);
1547e4b17023SJohn Marino 	  if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
1548e4b17023SJohn Marino 	    return false;
1549e4b17023SJohn Marino 	}
1550e4b17023SJohn Marino       return true;
1551e4b17023SJohn Marino     }
1552e4b17023SJohn Marino 
1553e4b17023SJohn Marino   /* Finally estimate N times, plus N gotos.  */
1554e4b17023SJohn Marino   f_estimate = count_insns_seq (finally, &eni_size_weights);
1555e4b17023SJohn Marino   f_estimate = (f_estimate + 1) * ndests;
1556e4b17023SJohn Marino 
1557e4b17023SJohn Marino   /* Switch statement (cost 10), N variable assignments, N gotos.  */
1558e4b17023SJohn Marino   sw_estimate = 10 + 2 * ndests;
1559e4b17023SJohn Marino 
1560e4b17023SJohn Marino   /* Optimize for size clearly wants our best guess.  */
1561e4b17023SJohn Marino   if (optimize_function_for_size_p (cfun))
1562e4b17023SJohn Marino     return f_estimate < sw_estimate;
1563e4b17023SJohn Marino 
1564e4b17023SJohn Marino   /* ??? These numbers are completely made up so far.  */
1565e4b17023SJohn Marino   if (optimize > 1)
1566e4b17023SJohn Marino     return f_estimate < 100 || f_estimate < sw_estimate * 2;
1567e4b17023SJohn Marino   else
1568e4b17023SJohn Marino     return f_estimate < 40 || f_estimate * 2 < sw_estimate * 3;
1569e4b17023SJohn Marino }
1570e4b17023SJohn Marino 
1571e4b17023SJohn Marino /* REG is the enclosing region for a possible cleanup region, or the region
1572e4b17023SJohn Marino    itself.  Returns TRUE if such a region would be unreachable.
1573e4b17023SJohn Marino 
1574e4b17023SJohn Marino    Cleanup regions within a must-not-throw region aren't actually reachable
1575e4b17023SJohn Marino    even if there are throwing stmts within them, because the personality
1576e4b17023SJohn Marino    routine will call terminate before unwinding.  */
1577e4b17023SJohn Marino 
1578e4b17023SJohn Marino static bool
cleanup_is_dead_in(eh_region reg)1579e4b17023SJohn Marino cleanup_is_dead_in (eh_region reg)
1580e4b17023SJohn Marino {
1581e4b17023SJohn Marino   while (reg && reg->type == ERT_CLEANUP)
1582e4b17023SJohn Marino     reg = reg->outer;
1583e4b17023SJohn Marino   return (reg && reg->type == ERT_MUST_NOT_THROW);
1584e4b17023SJohn Marino }
1585e4b17023SJohn Marino 
1586e4b17023SJohn Marino /* A subroutine of lower_eh_constructs_1.  Lower a GIMPLE_TRY_FINALLY nodes
1587e4b17023SJohn Marino    to a sequence of labels and blocks, plus the exception region trees
1588e4b17023SJohn Marino    that record all the magic.  This is complicated by the need to
1589e4b17023SJohn Marino    arrange for the FINALLY block to be executed on all exits.  */
1590e4b17023SJohn Marino 
1591e4b17023SJohn Marino static gimple_seq
lower_try_finally(struct leh_state * state,gimple tp)1592e4b17023SJohn Marino lower_try_finally (struct leh_state *state, gimple tp)
1593e4b17023SJohn Marino {
1594e4b17023SJohn Marino   struct leh_tf_state this_tf;
1595e4b17023SJohn Marino   struct leh_state this_state;
1596e4b17023SJohn Marino   int ndests;
1597e4b17023SJohn Marino   gimple_seq old_eh_seq;
1598e4b17023SJohn Marino 
1599e4b17023SJohn Marino   /* Process the try block.  */
1600e4b17023SJohn Marino 
1601e4b17023SJohn Marino   memset (&this_tf, 0, sizeof (this_tf));
1602e4b17023SJohn Marino   this_tf.try_finally_expr = tp;
1603e4b17023SJohn Marino   this_tf.top_p = tp;
1604e4b17023SJohn Marino   this_tf.outer = state;
1605e4b17023SJohn Marino   if (using_eh_for_cleanups_p && !cleanup_is_dead_in (state->cur_region))
1606e4b17023SJohn Marino     {
1607e4b17023SJohn Marino       this_tf.region = gen_eh_region_cleanup (state->cur_region);
1608e4b17023SJohn Marino       this_state.cur_region = this_tf.region;
1609e4b17023SJohn Marino     }
1610e4b17023SJohn Marino   else
1611e4b17023SJohn Marino     {
1612e4b17023SJohn Marino       this_tf.region = NULL;
1613e4b17023SJohn Marino       this_state.cur_region = state->cur_region;
1614e4b17023SJohn Marino     }
1615e4b17023SJohn Marino 
1616e4b17023SJohn Marino   this_state.ehp_region = state->ehp_region;
1617e4b17023SJohn Marino   this_state.tf = &this_tf;
1618e4b17023SJohn Marino 
1619e4b17023SJohn Marino   old_eh_seq = eh_seq;
1620e4b17023SJohn Marino   eh_seq = NULL;
1621e4b17023SJohn Marino 
1622e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_try_eval(tp));
1623e4b17023SJohn Marino 
1624e4b17023SJohn Marino   /* Determine if the try block is escaped through the bottom.  */
1625e4b17023SJohn Marino   this_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
1626e4b17023SJohn Marino 
1627e4b17023SJohn Marino   /* Determine if any exceptions are possible within the try block.  */
1628e4b17023SJohn Marino   if (this_tf.region)
1629e4b17023SJohn Marino     this_tf.may_throw = eh_region_may_contain_throw (this_tf.region);
1630e4b17023SJohn Marino   if (this_tf.may_throw)
1631e4b17023SJohn Marino     honor_protect_cleanup_actions (state, &this_state, &this_tf);
1632e4b17023SJohn Marino 
1633e4b17023SJohn Marino   /* Determine how many edges (still) reach the finally block.  Or rather,
1634e4b17023SJohn Marino      how many destinations are reached by the finally block.  Use this to
1635e4b17023SJohn Marino      determine how we process the finally block itself.  */
1636e4b17023SJohn Marino 
1637e4b17023SJohn Marino   ndests = VEC_length (tree, this_tf.dest_array);
1638e4b17023SJohn Marino   ndests += this_tf.may_fallthru;
1639e4b17023SJohn Marino   ndests += this_tf.may_return;
1640e4b17023SJohn Marino   ndests += this_tf.may_throw;
1641e4b17023SJohn Marino 
1642e4b17023SJohn Marino   /* If the FINALLY block is not reachable, dike it out.  */
1643e4b17023SJohn Marino   if (ndests == 0)
1644e4b17023SJohn Marino     {
1645e4b17023SJohn Marino       gimple_seq_add_seq (&this_tf.top_p_seq, gimple_try_eval (tp));
1646e4b17023SJohn Marino       gimple_try_set_cleanup (tp, NULL);
1647e4b17023SJohn Marino     }
1648e4b17023SJohn Marino   /* If the finally block doesn't fall through, then any destination
1649e4b17023SJohn Marino      we might try to impose there isn't reached either.  There may be
1650e4b17023SJohn Marino      some minor amount of cleanup and redirection still needed.  */
1651e4b17023SJohn Marino   else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp)))
1652e4b17023SJohn Marino     lower_try_finally_nofallthru (state, &this_tf);
1653e4b17023SJohn Marino 
1654e4b17023SJohn Marino   /* We can easily special-case redirection to a single destination.  */
1655e4b17023SJohn Marino   else if (ndests == 1)
1656e4b17023SJohn Marino     lower_try_finally_onedest (state, &this_tf);
1657e4b17023SJohn Marino   else if (decide_copy_try_finally (ndests, this_tf.may_throw,
1658e4b17023SJohn Marino 				    gimple_try_cleanup (tp)))
1659e4b17023SJohn Marino     lower_try_finally_copy (state, &this_tf);
1660e4b17023SJohn Marino   else
1661e4b17023SJohn Marino     lower_try_finally_switch (state, &this_tf);
1662e4b17023SJohn Marino 
1663e4b17023SJohn Marino   /* If someone requested we add a label at the end of the transformed
1664e4b17023SJohn Marino      block, do so.  */
1665e4b17023SJohn Marino   if (this_tf.fallthru_label)
1666e4b17023SJohn Marino     {
1667e4b17023SJohn Marino       /* This must be reached only if ndests == 0. */
1668e4b17023SJohn Marino       gimple x = gimple_build_label (this_tf.fallthru_label);
1669e4b17023SJohn Marino       gimple_seq_add_stmt (&this_tf.top_p_seq, x);
1670e4b17023SJohn Marino     }
1671e4b17023SJohn Marino 
1672e4b17023SJohn Marino   VEC_free (tree, heap, this_tf.dest_array);
1673e4b17023SJohn Marino   free (this_tf.goto_queue);
1674e4b17023SJohn Marino   if (this_tf.goto_queue_map)
1675e4b17023SJohn Marino     pointer_map_destroy (this_tf.goto_queue_map);
1676e4b17023SJohn Marino 
1677e4b17023SJohn Marino   /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1678e4b17023SJohn Marino      If there was no old eh_seq, then the append is trivially already done.  */
1679e4b17023SJohn Marino   if (old_eh_seq)
1680e4b17023SJohn Marino     {
1681e4b17023SJohn Marino       if (eh_seq == NULL)
1682e4b17023SJohn Marino 	eh_seq = old_eh_seq;
1683e4b17023SJohn Marino       else
1684e4b17023SJohn Marino 	{
1685e4b17023SJohn Marino 	  gimple_seq new_eh_seq = eh_seq;
1686e4b17023SJohn Marino 	  eh_seq = old_eh_seq;
1687e4b17023SJohn Marino 	  gimple_seq_add_seq(&eh_seq, new_eh_seq);
1688e4b17023SJohn Marino 	}
1689e4b17023SJohn Marino     }
1690e4b17023SJohn Marino 
1691e4b17023SJohn Marino   return this_tf.top_p_seq;
1692e4b17023SJohn Marino }
1693e4b17023SJohn Marino 
1694e4b17023SJohn Marino /* A subroutine of lower_eh_constructs_1.  Lower a GIMPLE_TRY_CATCH with a
1695e4b17023SJohn Marino    list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1696e4b17023SJohn Marino    exception region trees that records all the magic.  */
1697e4b17023SJohn Marino 
1698e4b17023SJohn Marino static gimple_seq
lower_catch(struct leh_state * state,gimple tp)1699e4b17023SJohn Marino lower_catch (struct leh_state *state, gimple tp)
1700e4b17023SJohn Marino {
1701e4b17023SJohn Marino   eh_region try_region = NULL;
1702e4b17023SJohn Marino   struct leh_state this_state = *state;
1703e4b17023SJohn Marino   gimple_stmt_iterator gsi;
1704e4b17023SJohn Marino   tree out_label;
1705e4b17023SJohn Marino   gimple_seq new_seq;
1706e4b17023SJohn Marino   gimple x;
1707e4b17023SJohn Marino   location_t try_catch_loc = gimple_location (tp);
1708e4b17023SJohn Marino 
1709e4b17023SJohn Marino   if (flag_exceptions)
1710e4b17023SJohn Marino     {
1711e4b17023SJohn Marino       try_region = gen_eh_region_try (state->cur_region);
1712e4b17023SJohn Marino       this_state.cur_region = try_region;
1713e4b17023SJohn Marino     }
1714e4b17023SJohn Marino 
1715e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_try_eval (tp));
1716e4b17023SJohn Marino 
1717e4b17023SJohn Marino   if (!eh_region_may_contain_throw (try_region))
1718e4b17023SJohn Marino     return gimple_try_eval (tp);
1719e4b17023SJohn Marino 
1720e4b17023SJohn Marino   new_seq = NULL;
1721e4b17023SJohn Marino   emit_eh_dispatch (&new_seq, try_region);
1722e4b17023SJohn Marino   emit_resx (&new_seq, try_region);
1723e4b17023SJohn Marino 
1724e4b17023SJohn Marino   this_state.cur_region = state->cur_region;
1725e4b17023SJohn Marino   this_state.ehp_region = try_region;
1726e4b17023SJohn Marino 
1727e4b17023SJohn Marino   out_label = NULL;
1728e4b17023SJohn Marino   for (gsi = gsi_start (gimple_try_cleanup (tp));
1729e4b17023SJohn Marino        !gsi_end_p (gsi);
1730e4b17023SJohn Marino        gsi_next (&gsi))
1731e4b17023SJohn Marino     {
1732e4b17023SJohn Marino       eh_catch c;
1733e4b17023SJohn Marino       gimple gcatch;
1734e4b17023SJohn Marino       gimple_seq handler;
1735e4b17023SJohn Marino 
1736e4b17023SJohn Marino       gcatch = gsi_stmt (gsi);
1737e4b17023SJohn Marino       c = gen_eh_region_catch (try_region, gimple_catch_types (gcatch));
1738e4b17023SJohn Marino 
1739e4b17023SJohn Marino       handler = gimple_catch_handler (gcatch);
1740e4b17023SJohn Marino       lower_eh_constructs_1 (&this_state, handler);
1741e4b17023SJohn Marino 
1742e4b17023SJohn Marino       c->label = create_artificial_label (UNKNOWN_LOCATION);
1743e4b17023SJohn Marino       x = gimple_build_label (c->label);
1744e4b17023SJohn Marino       gimple_seq_add_stmt (&new_seq, x);
1745e4b17023SJohn Marino 
1746e4b17023SJohn Marino       gimple_seq_add_seq (&new_seq, handler);
1747e4b17023SJohn Marino 
1748e4b17023SJohn Marino       if (gimple_seq_may_fallthru (new_seq))
1749e4b17023SJohn Marino 	{
1750e4b17023SJohn Marino 	  if (!out_label)
1751e4b17023SJohn Marino 	    out_label = create_artificial_label (try_catch_loc);
1752e4b17023SJohn Marino 
1753e4b17023SJohn Marino 	  x = gimple_build_goto (out_label);
1754e4b17023SJohn Marino 	  gimple_seq_add_stmt (&new_seq, x);
1755e4b17023SJohn Marino 	}
1756e4b17023SJohn Marino       if (!c->type_list)
1757e4b17023SJohn Marino 	break;
1758e4b17023SJohn Marino     }
1759e4b17023SJohn Marino 
1760e4b17023SJohn Marino   gimple_try_set_cleanup (tp, new_seq);
1761e4b17023SJohn Marino 
1762e4b17023SJohn Marino   return frob_into_branch_around (tp, try_region, out_label);
1763e4b17023SJohn Marino }
1764e4b17023SJohn Marino 
1765e4b17023SJohn Marino /* A subroutine of lower_eh_constructs_1.  Lower a GIMPLE_TRY with a
1766e4b17023SJohn Marino    GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1767e4b17023SJohn Marino    region trees that record all the magic.  */
1768e4b17023SJohn Marino 
1769e4b17023SJohn Marino static gimple_seq
lower_eh_filter(struct leh_state * state,gimple tp)1770e4b17023SJohn Marino lower_eh_filter (struct leh_state *state, gimple tp)
1771e4b17023SJohn Marino {
1772e4b17023SJohn Marino   struct leh_state this_state = *state;
1773e4b17023SJohn Marino   eh_region this_region = NULL;
1774e4b17023SJohn Marino   gimple inner, x;
1775e4b17023SJohn Marino   gimple_seq new_seq;
1776e4b17023SJohn Marino 
1777e4b17023SJohn Marino   inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1778e4b17023SJohn Marino 
1779e4b17023SJohn Marino   if (flag_exceptions)
1780e4b17023SJohn Marino     {
1781e4b17023SJohn Marino       this_region = gen_eh_region_allowed (state->cur_region,
1782e4b17023SJohn Marino 				           gimple_eh_filter_types (inner));
1783e4b17023SJohn Marino       this_state.cur_region = this_region;
1784e4b17023SJohn Marino     }
1785e4b17023SJohn Marino 
1786e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_try_eval (tp));
1787e4b17023SJohn Marino 
1788e4b17023SJohn Marino   if (!eh_region_may_contain_throw (this_region))
1789e4b17023SJohn Marino     return gimple_try_eval (tp);
1790e4b17023SJohn Marino 
1791e4b17023SJohn Marino   new_seq = NULL;
1792e4b17023SJohn Marino   this_state.cur_region = state->cur_region;
1793e4b17023SJohn Marino   this_state.ehp_region = this_region;
1794e4b17023SJohn Marino 
1795e4b17023SJohn Marino   emit_eh_dispatch (&new_seq, this_region);
1796e4b17023SJohn Marino   emit_resx (&new_seq, this_region);
1797e4b17023SJohn Marino 
1798e4b17023SJohn Marino   this_region->u.allowed.label = create_artificial_label (UNKNOWN_LOCATION);
1799e4b17023SJohn Marino   x = gimple_build_label (this_region->u.allowed.label);
1800e4b17023SJohn Marino   gimple_seq_add_stmt (&new_seq, x);
1801e4b17023SJohn Marino 
1802e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_eh_filter_failure (inner));
1803e4b17023SJohn Marino   gimple_seq_add_seq (&new_seq, gimple_eh_filter_failure (inner));
1804e4b17023SJohn Marino 
1805e4b17023SJohn Marino   gimple_try_set_cleanup (tp, new_seq);
1806e4b17023SJohn Marino 
1807e4b17023SJohn Marino   return frob_into_branch_around (tp, this_region, NULL);
1808e4b17023SJohn Marino }
1809e4b17023SJohn Marino 
1810e4b17023SJohn Marino /* A subroutine of lower_eh_constructs_1.  Lower a GIMPLE_TRY with
1811e4b17023SJohn Marino    an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1812e4b17023SJohn Marino    plus the exception region trees that record all the magic.  */
1813e4b17023SJohn Marino 
1814e4b17023SJohn Marino static gimple_seq
lower_eh_must_not_throw(struct leh_state * state,gimple tp)1815e4b17023SJohn Marino lower_eh_must_not_throw (struct leh_state *state, gimple tp)
1816e4b17023SJohn Marino {
1817e4b17023SJohn Marino   struct leh_state this_state = *state;
1818e4b17023SJohn Marino 
1819e4b17023SJohn Marino   if (flag_exceptions)
1820e4b17023SJohn Marino     {
1821e4b17023SJohn Marino       gimple inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1822e4b17023SJohn Marino       eh_region this_region;
1823e4b17023SJohn Marino 
1824e4b17023SJohn Marino       this_region = gen_eh_region_must_not_throw (state->cur_region);
1825e4b17023SJohn Marino       this_region->u.must_not_throw.failure_decl
1826e4b17023SJohn Marino 	= gimple_eh_must_not_throw_fndecl (inner);
1827e4b17023SJohn Marino       this_region->u.must_not_throw.failure_loc = gimple_location (tp);
1828e4b17023SJohn Marino 
1829e4b17023SJohn Marino       /* In order to get mangling applied to this decl, we must mark it
1830e4b17023SJohn Marino 	 used now.  Otherwise, pass_ipa_free_lang_data won't think it
1831e4b17023SJohn Marino 	 needs to happen.  */
1832e4b17023SJohn Marino       TREE_USED (this_region->u.must_not_throw.failure_decl) = 1;
1833e4b17023SJohn Marino 
1834e4b17023SJohn Marino       this_state.cur_region = this_region;
1835e4b17023SJohn Marino     }
1836e4b17023SJohn Marino 
1837e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_try_eval (tp));
1838e4b17023SJohn Marino 
1839e4b17023SJohn Marino   return gimple_try_eval (tp);
1840e4b17023SJohn Marino }
1841e4b17023SJohn Marino 
1842e4b17023SJohn Marino /* Implement a cleanup expression.  This is similar to try-finally,
1843e4b17023SJohn Marino    except that we only execute the cleanup block for exception edges.  */
1844e4b17023SJohn Marino 
1845e4b17023SJohn Marino static gimple_seq
lower_cleanup(struct leh_state * state,gimple tp)1846e4b17023SJohn Marino lower_cleanup (struct leh_state *state, gimple tp)
1847e4b17023SJohn Marino {
1848e4b17023SJohn Marino   struct leh_state this_state = *state;
1849e4b17023SJohn Marino   eh_region this_region = NULL;
1850e4b17023SJohn Marino   struct leh_tf_state fake_tf;
1851e4b17023SJohn Marino   gimple_seq result;
1852e4b17023SJohn Marino   bool cleanup_dead = cleanup_is_dead_in (state->cur_region);
1853e4b17023SJohn Marino 
1854e4b17023SJohn Marino   if (flag_exceptions && !cleanup_dead)
1855e4b17023SJohn Marino     {
1856e4b17023SJohn Marino       this_region = gen_eh_region_cleanup (state->cur_region);
1857e4b17023SJohn Marino       this_state.cur_region = this_region;
1858e4b17023SJohn Marino     }
1859e4b17023SJohn Marino 
1860e4b17023SJohn Marino   lower_eh_constructs_1 (&this_state, gimple_try_eval (tp));
1861e4b17023SJohn Marino 
1862e4b17023SJohn Marino   if (cleanup_dead || !eh_region_may_contain_throw (this_region))
1863e4b17023SJohn Marino     return gimple_try_eval (tp);
1864e4b17023SJohn Marino 
1865e4b17023SJohn Marino   /* Build enough of a try-finally state so that we can reuse
1866e4b17023SJohn Marino      honor_protect_cleanup_actions.  */
1867e4b17023SJohn Marino   memset (&fake_tf, 0, sizeof (fake_tf));
1868e4b17023SJohn Marino   fake_tf.top_p = fake_tf.try_finally_expr = tp;
1869e4b17023SJohn Marino   fake_tf.outer = state;
1870e4b17023SJohn Marino   fake_tf.region = this_region;
1871e4b17023SJohn Marino   fake_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
1872e4b17023SJohn Marino   fake_tf.may_throw = true;
1873e4b17023SJohn Marino 
1874e4b17023SJohn Marino   honor_protect_cleanup_actions (state, NULL, &fake_tf);
1875e4b17023SJohn Marino 
1876e4b17023SJohn Marino   if (fake_tf.may_throw)
1877e4b17023SJohn Marino     {
1878e4b17023SJohn Marino       /* In this case honor_protect_cleanup_actions had nothing to do,
1879e4b17023SJohn Marino 	 and we should process this normally.  */
1880e4b17023SJohn Marino       lower_eh_constructs_1 (state, gimple_try_cleanup (tp));
1881e4b17023SJohn Marino       result = frob_into_branch_around (tp, this_region,
1882e4b17023SJohn Marino                                         fake_tf.fallthru_label);
1883e4b17023SJohn Marino     }
1884e4b17023SJohn Marino   else
1885e4b17023SJohn Marino     {
1886e4b17023SJohn Marino       /* In this case honor_protect_cleanup_actions did nearly all of
1887e4b17023SJohn Marino 	 the work.  All we have left is to append the fallthru_label.  */
1888e4b17023SJohn Marino 
1889e4b17023SJohn Marino       result = gimple_try_eval (tp);
1890e4b17023SJohn Marino       if (fake_tf.fallthru_label)
1891e4b17023SJohn Marino 	{
1892e4b17023SJohn Marino 	  gimple x = gimple_build_label (fake_tf.fallthru_label);
1893e4b17023SJohn Marino 	  gimple_seq_add_stmt (&result, x);
1894e4b17023SJohn Marino 	}
1895e4b17023SJohn Marino     }
1896e4b17023SJohn Marino   return result;
1897e4b17023SJohn Marino }
1898e4b17023SJohn Marino 
1899e4b17023SJohn Marino /* Main loop for lowering eh constructs. Also moves gsi to the next
1900e4b17023SJohn Marino    statement. */
1901e4b17023SJohn Marino 
1902e4b17023SJohn Marino static void
lower_eh_constructs_2(struct leh_state * state,gimple_stmt_iterator * gsi)1903e4b17023SJohn Marino lower_eh_constructs_2 (struct leh_state *state, gimple_stmt_iterator *gsi)
1904e4b17023SJohn Marino {
1905e4b17023SJohn Marino   gimple_seq replace;
1906e4b17023SJohn Marino   gimple x;
1907e4b17023SJohn Marino   gimple stmt = gsi_stmt (*gsi);
1908e4b17023SJohn Marino 
1909e4b17023SJohn Marino   switch (gimple_code (stmt))
1910e4b17023SJohn Marino     {
1911e4b17023SJohn Marino     case GIMPLE_CALL:
1912e4b17023SJohn Marino       {
1913e4b17023SJohn Marino 	tree fndecl = gimple_call_fndecl (stmt);
1914e4b17023SJohn Marino 	tree rhs, lhs;
1915e4b17023SJohn Marino 
1916e4b17023SJohn Marino 	if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1917e4b17023SJohn Marino 	  switch (DECL_FUNCTION_CODE (fndecl))
1918e4b17023SJohn Marino 	    {
1919e4b17023SJohn Marino 	    case BUILT_IN_EH_POINTER:
1920e4b17023SJohn Marino 	      /* The front end may have generated a call to
1921e4b17023SJohn Marino 		 __builtin_eh_pointer (0) within a catch region.  Replace
1922e4b17023SJohn Marino 		 this zero argument with the current catch region number.  */
1923e4b17023SJohn Marino 	      if (state->ehp_region)
1924e4b17023SJohn Marino 		{
1925e4b17023SJohn Marino 		  tree nr = build_int_cst (integer_type_node,
1926e4b17023SJohn Marino 					   state->ehp_region->index);
1927e4b17023SJohn Marino 		  gimple_call_set_arg (stmt, 0, nr);
1928e4b17023SJohn Marino 		}
1929e4b17023SJohn Marino 	      else
1930e4b17023SJohn Marino 		{
1931e4b17023SJohn Marino 		  /* The user has dome something silly.  Remove it.  */
1932e4b17023SJohn Marino 		  rhs = null_pointer_node;
1933e4b17023SJohn Marino 		  goto do_replace;
1934e4b17023SJohn Marino 		}
1935e4b17023SJohn Marino 	      break;
1936e4b17023SJohn Marino 
1937e4b17023SJohn Marino 	    case BUILT_IN_EH_FILTER:
1938e4b17023SJohn Marino 	      /* ??? This should never appear, but since it's a builtin it
1939e4b17023SJohn Marino 		 is accessible to abuse by users.  Just remove it and
1940e4b17023SJohn Marino 		 replace the use with the arbitrary value zero.  */
1941e4b17023SJohn Marino 	      rhs = build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
1942e4b17023SJohn Marino 	    do_replace:
1943e4b17023SJohn Marino 	      lhs = gimple_call_lhs (stmt);
1944e4b17023SJohn Marino 	      x = gimple_build_assign (lhs, rhs);
1945e4b17023SJohn Marino 	      gsi_insert_before (gsi, x, GSI_SAME_STMT);
1946e4b17023SJohn Marino 	      /* FALLTHRU */
1947e4b17023SJohn Marino 
1948e4b17023SJohn Marino 	    case BUILT_IN_EH_COPY_VALUES:
1949e4b17023SJohn Marino 	      /* Likewise this should not appear.  Remove it.  */
1950e4b17023SJohn Marino 	      gsi_remove (gsi, true);
1951e4b17023SJohn Marino 	      return;
1952e4b17023SJohn Marino 
1953e4b17023SJohn Marino 	    default:
1954e4b17023SJohn Marino 	      break;
1955e4b17023SJohn Marino 	    }
1956e4b17023SJohn Marino       }
1957e4b17023SJohn Marino       /* FALLTHRU */
1958e4b17023SJohn Marino 
1959e4b17023SJohn Marino     case GIMPLE_ASSIGN:
1960e4b17023SJohn Marino       /* If the stmt can throw use a new temporary for the assignment
1961e4b17023SJohn Marino          to a LHS.  This makes sure the old value of the LHS is
1962e4b17023SJohn Marino 	 available on the EH edge.  Only do so for statements that
1963e4b17023SJohn Marino 	 potentially fall thru (no noreturn calls e.g.), otherwise
1964e4b17023SJohn Marino 	 this new assignment might create fake fallthru regions.  */
1965e4b17023SJohn Marino       if (stmt_could_throw_p (stmt)
1966e4b17023SJohn Marino 	  && gimple_has_lhs (stmt)
1967e4b17023SJohn Marino 	  && gimple_stmt_may_fallthru (stmt)
1968e4b17023SJohn Marino 	  && !tree_could_throw_p (gimple_get_lhs (stmt))
1969e4b17023SJohn Marino 	  && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
1970e4b17023SJohn Marino 	{
1971e4b17023SJohn Marino 	  tree lhs = gimple_get_lhs (stmt);
1972e4b17023SJohn Marino 	  tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
1973e4b17023SJohn Marino 	  gimple s = gimple_build_assign (lhs, tmp);
1974e4b17023SJohn Marino 	  gimple_set_location (s, gimple_location (stmt));
1975e4b17023SJohn Marino 	  gimple_set_block (s, gimple_block (stmt));
1976e4b17023SJohn Marino 	  gimple_set_lhs (stmt, tmp);
1977e4b17023SJohn Marino 	  if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
1978e4b17023SJohn Marino 	      || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
1979e4b17023SJohn Marino 	    DECL_GIMPLE_REG_P (tmp) = 1;
1980e4b17023SJohn Marino 	  gsi_insert_after (gsi, s, GSI_SAME_STMT);
1981e4b17023SJohn Marino 	}
1982e4b17023SJohn Marino       /* Look for things that can throw exceptions, and record them.  */
1983e4b17023SJohn Marino       if (state->cur_region && stmt_could_throw_p (stmt))
1984e4b17023SJohn Marino 	{
1985e4b17023SJohn Marino 	  record_stmt_eh_region (state->cur_region, stmt);
1986e4b17023SJohn Marino 	  note_eh_region_may_contain_throw (state->cur_region);
1987e4b17023SJohn Marino 	}
1988e4b17023SJohn Marino       break;
1989e4b17023SJohn Marino 
1990e4b17023SJohn Marino     case GIMPLE_COND:
1991e4b17023SJohn Marino     case GIMPLE_GOTO:
1992e4b17023SJohn Marino     case GIMPLE_RETURN:
1993e4b17023SJohn Marino       maybe_record_in_goto_queue (state, stmt);
1994e4b17023SJohn Marino       break;
1995e4b17023SJohn Marino 
1996e4b17023SJohn Marino     case GIMPLE_SWITCH:
1997e4b17023SJohn Marino       verify_norecord_switch_expr (state, stmt);
1998e4b17023SJohn Marino       break;
1999e4b17023SJohn Marino 
2000e4b17023SJohn Marino     case GIMPLE_TRY:
2001e4b17023SJohn Marino       if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
2002e4b17023SJohn Marino 	replace = lower_try_finally (state, stmt);
2003e4b17023SJohn Marino       else
2004e4b17023SJohn Marino 	{
2005e4b17023SJohn Marino 	  x = gimple_seq_first_stmt (gimple_try_cleanup (stmt));
2006e4b17023SJohn Marino 	  if (!x)
2007e4b17023SJohn Marino 	    {
2008e4b17023SJohn Marino 	      replace = gimple_try_eval (stmt);
2009e4b17023SJohn Marino 	      lower_eh_constructs_1 (state, replace);
2010e4b17023SJohn Marino 	    }
2011e4b17023SJohn Marino 	  else
2012e4b17023SJohn Marino 	    switch (gimple_code (x))
2013e4b17023SJohn Marino 	      {
2014e4b17023SJohn Marino 		case GIMPLE_CATCH:
2015e4b17023SJohn Marino 		    replace = lower_catch (state, stmt);
2016e4b17023SJohn Marino 		    break;
2017e4b17023SJohn Marino 		case GIMPLE_EH_FILTER:
2018e4b17023SJohn Marino 		    replace = lower_eh_filter (state, stmt);
2019e4b17023SJohn Marino 		    break;
2020e4b17023SJohn Marino 		case GIMPLE_EH_MUST_NOT_THROW:
2021e4b17023SJohn Marino 		    replace = lower_eh_must_not_throw (state, stmt);
2022e4b17023SJohn Marino 		    break;
2023e4b17023SJohn Marino 		case GIMPLE_EH_ELSE:
2024e4b17023SJohn Marino 		    /* This code is only valid with GIMPLE_TRY_FINALLY.  */
2025e4b17023SJohn Marino 		    gcc_unreachable ();
2026e4b17023SJohn Marino 		default:
2027e4b17023SJohn Marino 		    replace = lower_cleanup (state, stmt);
2028e4b17023SJohn Marino 		    break;
2029e4b17023SJohn Marino 	      }
2030e4b17023SJohn Marino 	}
2031e4b17023SJohn Marino 
2032e4b17023SJohn Marino       /* Remove the old stmt and insert the transformed sequence
2033e4b17023SJohn Marino 	 instead. */
2034e4b17023SJohn Marino       gsi_insert_seq_before (gsi, replace, GSI_SAME_STMT);
2035e4b17023SJohn Marino       gsi_remove (gsi, true);
2036e4b17023SJohn Marino 
2037e4b17023SJohn Marino       /* Return since we don't want gsi_next () */
2038e4b17023SJohn Marino       return;
2039e4b17023SJohn Marino 
2040e4b17023SJohn Marino     case GIMPLE_EH_ELSE:
2041e4b17023SJohn Marino       /* We should be eliminating this in lower_try_finally et al.  */
2042e4b17023SJohn Marino       gcc_unreachable ();
2043e4b17023SJohn Marino 
2044e4b17023SJohn Marino     default:
2045e4b17023SJohn Marino       /* A type, a decl, or some kind of statement that we're not
2046e4b17023SJohn Marino 	 interested in.  Don't walk them.  */
2047e4b17023SJohn Marino       break;
2048e4b17023SJohn Marino     }
2049e4b17023SJohn Marino 
2050e4b17023SJohn Marino   gsi_next (gsi);
2051e4b17023SJohn Marino }
2052e4b17023SJohn Marino 
2053e4b17023SJohn Marino /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2054e4b17023SJohn Marino 
2055e4b17023SJohn Marino static void
lower_eh_constructs_1(struct leh_state * state,gimple_seq seq)2056e4b17023SJohn Marino lower_eh_constructs_1 (struct leh_state *state, gimple_seq seq)
2057e4b17023SJohn Marino {
2058e4b17023SJohn Marino   gimple_stmt_iterator gsi;
2059e4b17023SJohn Marino   for (gsi = gsi_start (seq); !gsi_end_p (gsi);)
2060e4b17023SJohn Marino     lower_eh_constructs_2 (state, &gsi);
2061e4b17023SJohn Marino }
2062e4b17023SJohn Marino 
2063e4b17023SJohn Marino static unsigned int
lower_eh_constructs(void)2064e4b17023SJohn Marino lower_eh_constructs (void)
2065e4b17023SJohn Marino {
2066e4b17023SJohn Marino   struct leh_state null_state;
2067e4b17023SJohn Marino   gimple_seq bodyp;
2068e4b17023SJohn Marino 
2069e4b17023SJohn Marino   bodyp = gimple_body (current_function_decl);
2070e4b17023SJohn Marino   if (bodyp == NULL)
2071e4b17023SJohn Marino     return 0;
2072e4b17023SJohn Marino 
2073e4b17023SJohn Marino   finally_tree = htab_create (31, struct_ptr_hash, struct_ptr_eq, free);
2074e4b17023SJohn Marino   eh_region_may_contain_throw_map = BITMAP_ALLOC (NULL);
2075e4b17023SJohn Marino   memset (&null_state, 0, sizeof (null_state));
2076e4b17023SJohn Marino 
2077e4b17023SJohn Marino   collect_finally_tree_1 (bodyp, NULL);
2078e4b17023SJohn Marino   lower_eh_constructs_1 (&null_state, bodyp);
2079e4b17023SJohn Marino 
2080e4b17023SJohn Marino   /* We assume there's a return statement, or something, at the end of
2081e4b17023SJohn Marino      the function, and thus ploping the EH sequence afterward won't
2082e4b17023SJohn Marino      change anything.  */
2083e4b17023SJohn Marino   gcc_assert (!gimple_seq_may_fallthru (bodyp));
2084e4b17023SJohn Marino   gimple_seq_add_seq (&bodyp, eh_seq);
2085e4b17023SJohn Marino 
2086e4b17023SJohn Marino   /* We assume that since BODYP already existed, adding EH_SEQ to it
2087e4b17023SJohn Marino      didn't change its value, and we don't have to re-set the function.  */
2088e4b17023SJohn Marino   gcc_assert (bodyp == gimple_body (current_function_decl));
2089e4b17023SJohn Marino 
2090e4b17023SJohn Marino   htab_delete (finally_tree);
2091e4b17023SJohn Marino   BITMAP_FREE (eh_region_may_contain_throw_map);
2092e4b17023SJohn Marino   eh_seq = NULL;
2093e4b17023SJohn Marino 
2094e4b17023SJohn Marino   /* If this function needs a language specific EH personality routine
2095e4b17023SJohn Marino      and the frontend didn't already set one do so now.  */
2096e4b17023SJohn Marino   if (function_needs_eh_personality (cfun) == eh_personality_lang
2097e4b17023SJohn Marino       && !DECL_FUNCTION_PERSONALITY (current_function_decl))
2098e4b17023SJohn Marino     DECL_FUNCTION_PERSONALITY (current_function_decl)
2099e4b17023SJohn Marino       = lang_hooks.eh_personality ();
2100e4b17023SJohn Marino 
2101e4b17023SJohn Marino   return 0;
2102e4b17023SJohn Marino }
2103e4b17023SJohn Marino 
2104e4b17023SJohn Marino struct gimple_opt_pass pass_lower_eh =
2105e4b17023SJohn Marino {
2106e4b17023SJohn Marino  {
2107e4b17023SJohn Marino   GIMPLE_PASS,
2108e4b17023SJohn Marino   "eh",					/* name */
2109e4b17023SJohn Marino   NULL,					/* gate */
2110e4b17023SJohn Marino   lower_eh_constructs,			/* execute */
2111e4b17023SJohn Marino   NULL,					/* sub */
2112e4b17023SJohn Marino   NULL,					/* next */
2113e4b17023SJohn Marino   0,					/* static_pass_number */
2114e4b17023SJohn Marino   TV_TREE_EH,				/* tv_id */
2115e4b17023SJohn Marino   PROP_gimple_lcf,			/* properties_required */
2116e4b17023SJohn Marino   PROP_gimple_leh,			/* properties_provided */
2117e4b17023SJohn Marino   0,					/* properties_destroyed */
2118e4b17023SJohn Marino   0,					/* todo_flags_start */
2119e4b17023SJohn Marino   0             			/* todo_flags_finish */
2120e4b17023SJohn Marino  }
2121e4b17023SJohn Marino };
2122e4b17023SJohn Marino 
2123e4b17023SJohn Marino /* Create the multiple edges from an EH_DISPATCH statement to all of
2124e4b17023SJohn Marino    the possible handlers for its EH region.  Return true if there's
2125e4b17023SJohn Marino    no fallthru edge; false if there is.  */
2126e4b17023SJohn Marino 
2127e4b17023SJohn Marino bool
make_eh_dispatch_edges(gimple stmt)2128e4b17023SJohn Marino make_eh_dispatch_edges (gimple stmt)
2129e4b17023SJohn Marino {
2130e4b17023SJohn Marino   eh_region r;
2131e4b17023SJohn Marino   eh_catch c;
2132e4b17023SJohn Marino   basic_block src, dst;
2133e4b17023SJohn Marino 
2134e4b17023SJohn Marino   r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2135e4b17023SJohn Marino   src = gimple_bb (stmt);
2136e4b17023SJohn Marino 
2137e4b17023SJohn Marino   switch (r->type)
2138e4b17023SJohn Marino     {
2139e4b17023SJohn Marino     case ERT_TRY:
2140e4b17023SJohn Marino       for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2141e4b17023SJohn Marino 	{
2142e4b17023SJohn Marino 	  dst = label_to_block (c->label);
2143e4b17023SJohn Marino 	  make_edge (src, dst, 0);
2144e4b17023SJohn Marino 
2145e4b17023SJohn Marino 	  /* A catch-all handler doesn't have a fallthru.  */
2146e4b17023SJohn Marino 	  if (c->type_list == NULL)
2147e4b17023SJohn Marino 	    return false;
2148e4b17023SJohn Marino 	}
2149e4b17023SJohn Marino       break;
2150e4b17023SJohn Marino 
2151e4b17023SJohn Marino     case ERT_ALLOWED_EXCEPTIONS:
2152e4b17023SJohn Marino       dst = label_to_block (r->u.allowed.label);
2153e4b17023SJohn Marino       make_edge (src, dst, 0);
2154e4b17023SJohn Marino       break;
2155e4b17023SJohn Marino 
2156e4b17023SJohn Marino     default:
2157e4b17023SJohn Marino       gcc_unreachable ();
2158e4b17023SJohn Marino     }
2159e4b17023SJohn Marino 
2160e4b17023SJohn Marino   return true;
2161e4b17023SJohn Marino }
2162e4b17023SJohn Marino 
2163e4b17023SJohn Marino /* Create the single EH edge from STMT to its nearest landing pad,
2164e4b17023SJohn Marino    if there is such a landing pad within the current function.  */
2165e4b17023SJohn Marino 
2166e4b17023SJohn Marino void
make_eh_edges(gimple stmt)2167e4b17023SJohn Marino make_eh_edges (gimple stmt)
2168e4b17023SJohn Marino {
2169e4b17023SJohn Marino   basic_block src, dst;
2170e4b17023SJohn Marino   eh_landing_pad lp;
2171e4b17023SJohn Marino   int lp_nr;
2172e4b17023SJohn Marino 
2173e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (stmt);
2174e4b17023SJohn Marino   if (lp_nr <= 0)
2175e4b17023SJohn Marino     return;
2176e4b17023SJohn Marino 
2177e4b17023SJohn Marino   lp = get_eh_landing_pad_from_number (lp_nr);
2178e4b17023SJohn Marino   gcc_assert (lp != NULL);
2179e4b17023SJohn Marino 
2180e4b17023SJohn Marino   src = gimple_bb (stmt);
2181e4b17023SJohn Marino   dst = label_to_block (lp->post_landing_pad);
2182e4b17023SJohn Marino   make_edge (src, dst, EDGE_EH);
2183e4b17023SJohn Marino }
2184e4b17023SJohn Marino 
2185e4b17023SJohn Marino /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2186e4b17023SJohn Marino    do not actually perform the final edge redirection.
2187e4b17023SJohn Marino 
2188e4b17023SJohn Marino    CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2189e4b17023SJohn Marino    we intend to change the destination EH region as well; this means
2190e4b17023SJohn Marino    EH_LANDING_PAD_NR must already be set on the destination block label.
2191e4b17023SJohn Marino    If false, we're being called from generic cfg manipulation code and we
2192e4b17023SJohn Marino    should preserve our place within the region tree.  */
2193e4b17023SJohn Marino 
2194e4b17023SJohn Marino static void
redirect_eh_edge_1(edge edge_in,basic_block new_bb,bool change_region)2195e4b17023SJohn Marino redirect_eh_edge_1 (edge edge_in, basic_block new_bb, bool change_region)
2196e4b17023SJohn Marino {
2197e4b17023SJohn Marino   eh_landing_pad old_lp, new_lp;
2198e4b17023SJohn Marino   basic_block old_bb;
2199e4b17023SJohn Marino   gimple throw_stmt;
2200e4b17023SJohn Marino   int old_lp_nr, new_lp_nr;
2201e4b17023SJohn Marino   tree old_label, new_label;
2202e4b17023SJohn Marino   edge_iterator ei;
2203e4b17023SJohn Marino   edge e;
2204e4b17023SJohn Marino 
2205e4b17023SJohn Marino   old_bb = edge_in->dest;
2206e4b17023SJohn Marino   old_label = gimple_block_label (old_bb);
2207e4b17023SJohn Marino   old_lp_nr = EH_LANDING_PAD_NR (old_label);
2208e4b17023SJohn Marino   gcc_assert (old_lp_nr > 0);
2209e4b17023SJohn Marino   old_lp = get_eh_landing_pad_from_number (old_lp_nr);
2210e4b17023SJohn Marino 
2211e4b17023SJohn Marino   throw_stmt = last_stmt (edge_in->src);
2212e4b17023SJohn Marino   gcc_assert (lookup_stmt_eh_lp (throw_stmt) == old_lp_nr);
2213e4b17023SJohn Marino 
2214e4b17023SJohn Marino   new_label = gimple_block_label (new_bb);
2215e4b17023SJohn Marino 
2216e4b17023SJohn Marino   /* Look for an existing region that might be using NEW_BB already.  */
2217e4b17023SJohn Marino   new_lp_nr = EH_LANDING_PAD_NR (new_label);
2218e4b17023SJohn Marino   if (new_lp_nr)
2219e4b17023SJohn Marino     {
2220e4b17023SJohn Marino       new_lp = get_eh_landing_pad_from_number (new_lp_nr);
2221e4b17023SJohn Marino       gcc_assert (new_lp);
2222e4b17023SJohn Marino 
2223e4b17023SJohn Marino       /* Unless CHANGE_REGION is true, the new and old landing pad
2224e4b17023SJohn Marino 	 had better be associated with the same EH region.  */
2225e4b17023SJohn Marino       gcc_assert (change_region || new_lp->region == old_lp->region);
2226e4b17023SJohn Marino     }
2227e4b17023SJohn Marino   else
2228e4b17023SJohn Marino     {
2229e4b17023SJohn Marino       new_lp = NULL;
2230e4b17023SJohn Marino       gcc_assert (!change_region);
2231e4b17023SJohn Marino     }
2232e4b17023SJohn Marino 
2233e4b17023SJohn Marino   /* Notice when we redirect the last EH edge away from OLD_BB.  */
2234e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, old_bb->preds)
2235e4b17023SJohn Marino     if (e != edge_in && (e->flags & EDGE_EH))
2236e4b17023SJohn Marino       break;
2237e4b17023SJohn Marino 
2238e4b17023SJohn Marino   if (new_lp)
2239e4b17023SJohn Marino     {
2240e4b17023SJohn Marino       /* NEW_LP already exists.  If there are still edges into OLD_LP,
2241e4b17023SJohn Marino 	 there's nothing to do with the EH tree.  If there are no more
2242e4b17023SJohn Marino 	 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2243e4b17023SJohn Marino 	 If CHANGE_REGION is true, then our caller is expecting to remove
2244e4b17023SJohn Marino 	 the landing pad.  */
2245e4b17023SJohn Marino       if (e == NULL && !change_region)
2246e4b17023SJohn Marino 	remove_eh_landing_pad (old_lp);
2247e4b17023SJohn Marino     }
2248e4b17023SJohn Marino   else
2249e4b17023SJohn Marino     {
2250e4b17023SJohn Marino       /* No correct landing pad exists.  If there are no more edges
2251e4b17023SJohn Marino 	 into OLD_LP, then we can simply re-use the existing landing pad.
2252e4b17023SJohn Marino 	 Otherwise, we have to create a new landing pad.  */
2253e4b17023SJohn Marino       if (e == NULL)
2254e4b17023SJohn Marino 	{
2255e4b17023SJohn Marino 	  EH_LANDING_PAD_NR (old_lp->post_landing_pad) = 0;
2256e4b17023SJohn Marino 	  new_lp = old_lp;
2257e4b17023SJohn Marino 	}
2258e4b17023SJohn Marino       else
2259e4b17023SJohn Marino 	new_lp = gen_eh_landing_pad (old_lp->region);
2260e4b17023SJohn Marino       new_lp->post_landing_pad = new_label;
2261e4b17023SJohn Marino       EH_LANDING_PAD_NR (new_label) = new_lp->index;
2262e4b17023SJohn Marino     }
2263e4b17023SJohn Marino 
2264e4b17023SJohn Marino   /* Maybe move the throwing statement to the new region.  */
2265e4b17023SJohn Marino   if (old_lp != new_lp)
2266e4b17023SJohn Marino     {
2267e4b17023SJohn Marino       remove_stmt_from_eh_lp (throw_stmt);
2268e4b17023SJohn Marino       add_stmt_to_eh_lp (throw_stmt, new_lp->index);
2269e4b17023SJohn Marino     }
2270e4b17023SJohn Marino }
2271e4b17023SJohn Marino 
2272e4b17023SJohn Marino /* Redirect EH edge E to NEW_BB.  */
2273e4b17023SJohn Marino 
2274e4b17023SJohn Marino edge
redirect_eh_edge(edge edge_in,basic_block new_bb)2275e4b17023SJohn Marino redirect_eh_edge (edge edge_in, basic_block new_bb)
2276e4b17023SJohn Marino {
2277e4b17023SJohn Marino   redirect_eh_edge_1 (edge_in, new_bb, false);
2278e4b17023SJohn Marino   return ssa_redirect_edge (edge_in, new_bb);
2279e4b17023SJohn Marino }
2280e4b17023SJohn Marino 
2281e4b17023SJohn Marino /* This is a subroutine of gimple_redirect_edge_and_branch.  Update the
2282e4b17023SJohn Marino    labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2283e4b17023SJohn Marino    The actual edge update will happen in the caller.  */
2284e4b17023SJohn Marino 
2285e4b17023SJohn Marino void
redirect_eh_dispatch_edge(gimple stmt,edge e,basic_block new_bb)2286e4b17023SJohn Marino redirect_eh_dispatch_edge (gimple stmt, edge e, basic_block new_bb)
2287e4b17023SJohn Marino {
2288e4b17023SJohn Marino   tree new_lab = gimple_block_label (new_bb);
2289e4b17023SJohn Marino   bool any_changed = false;
2290e4b17023SJohn Marino   basic_block old_bb;
2291e4b17023SJohn Marino   eh_region r;
2292e4b17023SJohn Marino   eh_catch c;
2293e4b17023SJohn Marino 
2294e4b17023SJohn Marino   r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2295e4b17023SJohn Marino   switch (r->type)
2296e4b17023SJohn Marino     {
2297e4b17023SJohn Marino     case ERT_TRY:
2298e4b17023SJohn Marino       for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2299e4b17023SJohn Marino 	{
2300e4b17023SJohn Marino 	  old_bb = label_to_block (c->label);
2301e4b17023SJohn Marino 	  if (old_bb == e->dest)
2302e4b17023SJohn Marino 	    {
2303e4b17023SJohn Marino 	      c->label = new_lab;
2304e4b17023SJohn Marino 	      any_changed = true;
2305e4b17023SJohn Marino 	    }
2306e4b17023SJohn Marino 	}
2307e4b17023SJohn Marino       break;
2308e4b17023SJohn Marino 
2309e4b17023SJohn Marino     case ERT_ALLOWED_EXCEPTIONS:
2310e4b17023SJohn Marino       old_bb = label_to_block (r->u.allowed.label);
2311e4b17023SJohn Marino       gcc_assert (old_bb == e->dest);
2312e4b17023SJohn Marino       r->u.allowed.label = new_lab;
2313e4b17023SJohn Marino       any_changed = true;
2314e4b17023SJohn Marino       break;
2315e4b17023SJohn Marino 
2316e4b17023SJohn Marino     default:
2317e4b17023SJohn Marino       gcc_unreachable ();
2318e4b17023SJohn Marino     }
2319e4b17023SJohn Marino 
2320e4b17023SJohn Marino   gcc_assert (any_changed);
2321e4b17023SJohn Marino }
2322e4b17023SJohn Marino 
2323e4b17023SJohn Marino /* Helper function for operation_could_trap_p and stmt_could_throw_p.  */
2324e4b17023SJohn Marino 
2325e4b17023SJohn Marino bool
operation_could_trap_helper_p(enum tree_code op,bool fp_operation,bool honor_trapv,bool honor_nans,bool honor_snans,tree divisor,bool * handled)2326e4b17023SJohn Marino operation_could_trap_helper_p (enum tree_code op,
2327e4b17023SJohn Marino 			       bool fp_operation,
2328e4b17023SJohn Marino 			       bool honor_trapv,
2329e4b17023SJohn Marino 			       bool honor_nans,
2330e4b17023SJohn Marino 			       bool honor_snans,
2331e4b17023SJohn Marino 			       tree divisor,
2332e4b17023SJohn Marino 			       bool *handled)
2333e4b17023SJohn Marino {
2334e4b17023SJohn Marino   *handled = true;
2335e4b17023SJohn Marino   switch (op)
2336e4b17023SJohn Marino     {
2337e4b17023SJohn Marino     case TRUNC_DIV_EXPR:
2338e4b17023SJohn Marino     case CEIL_DIV_EXPR:
2339e4b17023SJohn Marino     case FLOOR_DIV_EXPR:
2340e4b17023SJohn Marino     case ROUND_DIV_EXPR:
2341e4b17023SJohn Marino     case EXACT_DIV_EXPR:
2342e4b17023SJohn Marino     case CEIL_MOD_EXPR:
2343e4b17023SJohn Marino     case FLOOR_MOD_EXPR:
2344e4b17023SJohn Marino     case ROUND_MOD_EXPR:
2345e4b17023SJohn Marino     case TRUNC_MOD_EXPR:
2346e4b17023SJohn Marino     case RDIV_EXPR:
2347e4b17023SJohn Marino       if (honor_snans || honor_trapv)
2348e4b17023SJohn Marino 	return true;
2349e4b17023SJohn Marino       if (fp_operation)
2350e4b17023SJohn Marino 	return flag_trapping_math;
2351e4b17023SJohn Marino       if (!TREE_CONSTANT (divisor) || integer_zerop (divisor))
2352e4b17023SJohn Marino         return true;
2353e4b17023SJohn Marino       return false;
2354e4b17023SJohn Marino 
2355e4b17023SJohn Marino     case LT_EXPR:
2356e4b17023SJohn Marino     case LE_EXPR:
2357e4b17023SJohn Marino     case GT_EXPR:
2358e4b17023SJohn Marino     case GE_EXPR:
2359e4b17023SJohn Marino     case LTGT_EXPR:
2360e4b17023SJohn Marino       /* Some floating point comparisons may trap.  */
2361e4b17023SJohn Marino       return honor_nans;
2362e4b17023SJohn Marino 
2363e4b17023SJohn Marino     case EQ_EXPR:
2364e4b17023SJohn Marino     case NE_EXPR:
2365e4b17023SJohn Marino     case UNORDERED_EXPR:
2366e4b17023SJohn Marino     case ORDERED_EXPR:
2367e4b17023SJohn Marino     case UNLT_EXPR:
2368e4b17023SJohn Marino     case UNLE_EXPR:
2369e4b17023SJohn Marino     case UNGT_EXPR:
2370e4b17023SJohn Marino     case UNGE_EXPR:
2371e4b17023SJohn Marino     case UNEQ_EXPR:
2372e4b17023SJohn Marino       return honor_snans;
2373e4b17023SJohn Marino 
2374e4b17023SJohn Marino     case CONVERT_EXPR:
2375e4b17023SJohn Marino     case FIX_TRUNC_EXPR:
2376e4b17023SJohn Marino       /* Conversion of floating point might trap.  */
2377e4b17023SJohn Marino       return honor_nans;
2378e4b17023SJohn Marino 
2379e4b17023SJohn Marino     case NEGATE_EXPR:
2380e4b17023SJohn Marino     case ABS_EXPR:
2381e4b17023SJohn Marino     case CONJ_EXPR:
2382e4b17023SJohn Marino       /* These operations don't trap with floating point.  */
2383e4b17023SJohn Marino       if (honor_trapv)
2384e4b17023SJohn Marino 	return true;
2385e4b17023SJohn Marino       return false;
2386e4b17023SJohn Marino 
2387e4b17023SJohn Marino     case PLUS_EXPR:
2388e4b17023SJohn Marino     case MINUS_EXPR:
2389e4b17023SJohn Marino     case MULT_EXPR:
2390e4b17023SJohn Marino       /* Any floating arithmetic may trap.  */
2391e4b17023SJohn Marino       if (fp_operation && flag_trapping_math)
2392e4b17023SJohn Marino 	return true;
2393e4b17023SJohn Marino       if (honor_trapv)
2394e4b17023SJohn Marino 	return true;
2395e4b17023SJohn Marino       return false;
2396e4b17023SJohn Marino 
2397e4b17023SJohn Marino     case COMPLEX_EXPR:
2398e4b17023SJohn Marino     case CONSTRUCTOR:
2399e4b17023SJohn Marino       /* Constructing an object cannot trap.  */
2400e4b17023SJohn Marino       return false;
2401e4b17023SJohn Marino 
2402e4b17023SJohn Marino     default:
2403e4b17023SJohn Marino       /* Any floating arithmetic may trap.  */
2404e4b17023SJohn Marino       if (fp_operation && flag_trapping_math)
2405e4b17023SJohn Marino 	return true;
2406e4b17023SJohn Marino 
2407e4b17023SJohn Marino       *handled = false;
2408e4b17023SJohn Marino       return false;
2409e4b17023SJohn Marino     }
2410e4b17023SJohn Marino }
2411e4b17023SJohn Marino 
2412e4b17023SJohn Marino /* Return true if operation OP may trap.  FP_OPERATION is true if OP is applied
2413e4b17023SJohn Marino    on floating-point values.  HONOR_TRAPV is true if OP is applied on integer
2414e4b17023SJohn Marino    type operands that may trap.  If OP is a division operator, DIVISOR contains
2415e4b17023SJohn Marino    the value of the divisor.  */
2416e4b17023SJohn Marino 
2417e4b17023SJohn Marino bool
operation_could_trap_p(enum tree_code op,bool fp_operation,bool honor_trapv,tree divisor)2418e4b17023SJohn Marino operation_could_trap_p (enum tree_code op, bool fp_operation, bool honor_trapv,
2419e4b17023SJohn Marino 			tree divisor)
2420e4b17023SJohn Marino {
2421e4b17023SJohn Marino   bool honor_nans = (fp_operation && flag_trapping_math
2422e4b17023SJohn Marino 		     && !flag_finite_math_only);
2423e4b17023SJohn Marino   bool honor_snans = fp_operation && flag_signaling_nans != 0;
2424e4b17023SJohn Marino   bool handled;
2425e4b17023SJohn Marino 
2426e4b17023SJohn Marino   if (TREE_CODE_CLASS (op) != tcc_comparison
2427e4b17023SJohn Marino       && TREE_CODE_CLASS (op) != tcc_unary
2428e4b17023SJohn Marino       && TREE_CODE_CLASS (op) != tcc_binary)
2429e4b17023SJohn Marino     return false;
2430e4b17023SJohn Marino 
2431e4b17023SJohn Marino   return operation_could_trap_helper_p (op, fp_operation, honor_trapv,
2432e4b17023SJohn Marino 					honor_nans, honor_snans, divisor,
2433e4b17023SJohn Marino 					&handled);
2434e4b17023SJohn Marino }
2435e4b17023SJohn Marino 
2436e4b17023SJohn Marino /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2437e4b17023SJohn Marino    location or floating point arithmetic.  C.f. the rtl version, may_trap_p.
2438e4b17023SJohn Marino    This routine expects only GIMPLE lhs or rhs input.  */
2439e4b17023SJohn Marino 
2440e4b17023SJohn Marino bool
tree_could_trap_p(tree expr)2441e4b17023SJohn Marino tree_could_trap_p (tree expr)
2442e4b17023SJohn Marino {
2443e4b17023SJohn Marino   enum tree_code code;
2444e4b17023SJohn Marino   bool fp_operation = false;
2445e4b17023SJohn Marino   bool honor_trapv = false;
2446e4b17023SJohn Marino   tree t, base, div = NULL_TREE;
2447e4b17023SJohn Marino 
2448e4b17023SJohn Marino   if (!expr)
2449e4b17023SJohn Marino     return false;
2450e4b17023SJohn Marino 
2451e4b17023SJohn Marino   code = TREE_CODE (expr);
2452e4b17023SJohn Marino   t = TREE_TYPE (expr);
2453e4b17023SJohn Marino 
2454e4b17023SJohn Marino   if (t)
2455e4b17023SJohn Marino     {
2456e4b17023SJohn Marino       if (COMPARISON_CLASS_P (expr))
2457e4b17023SJohn Marino 	fp_operation = FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
2458e4b17023SJohn Marino       else
2459e4b17023SJohn Marino 	fp_operation = FLOAT_TYPE_P (t);
2460e4b17023SJohn Marino       honor_trapv = INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t);
2461e4b17023SJohn Marino     }
2462e4b17023SJohn Marino 
2463e4b17023SJohn Marino   if (TREE_CODE_CLASS (code) == tcc_binary)
2464e4b17023SJohn Marino     div = TREE_OPERAND (expr, 1);
2465e4b17023SJohn Marino   if (operation_could_trap_p (code, fp_operation, honor_trapv, div))
2466e4b17023SJohn Marino     return true;
2467e4b17023SJohn Marino 
2468e4b17023SJohn Marino  restart:
2469e4b17023SJohn Marino   switch (code)
2470e4b17023SJohn Marino     {
2471e4b17023SJohn Marino     case TARGET_MEM_REF:
2472e4b17023SJohn Marino       if (TREE_CODE (TMR_BASE (expr)) == ADDR_EXPR
2473e4b17023SJohn Marino 	  && !TMR_INDEX (expr) && !TMR_INDEX2 (expr))
2474e4b17023SJohn Marino 	return false;
2475e4b17023SJohn Marino       return !TREE_THIS_NOTRAP (expr);
2476e4b17023SJohn Marino 
2477e4b17023SJohn Marino     case COMPONENT_REF:
2478e4b17023SJohn Marino     case REALPART_EXPR:
2479e4b17023SJohn Marino     case IMAGPART_EXPR:
2480e4b17023SJohn Marino     case BIT_FIELD_REF:
2481e4b17023SJohn Marino     case VIEW_CONVERT_EXPR:
2482e4b17023SJohn Marino     case WITH_SIZE_EXPR:
2483e4b17023SJohn Marino       expr = TREE_OPERAND (expr, 0);
2484e4b17023SJohn Marino       code = TREE_CODE (expr);
2485e4b17023SJohn Marino       goto restart;
2486e4b17023SJohn Marino 
2487e4b17023SJohn Marino     case ARRAY_RANGE_REF:
2488e4b17023SJohn Marino       base = TREE_OPERAND (expr, 0);
2489e4b17023SJohn Marino       if (tree_could_trap_p (base))
2490e4b17023SJohn Marino 	return true;
2491e4b17023SJohn Marino       if (TREE_THIS_NOTRAP (expr))
2492e4b17023SJohn Marino 	return false;
2493e4b17023SJohn Marino       return !range_in_array_bounds_p (expr);
2494e4b17023SJohn Marino 
2495e4b17023SJohn Marino     case ARRAY_REF:
2496e4b17023SJohn Marino       base = TREE_OPERAND (expr, 0);
2497e4b17023SJohn Marino       if (tree_could_trap_p (base))
2498e4b17023SJohn Marino 	return true;
2499e4b17023SJohn Marino       if (TREE_THIS_NOTRAP (expr))
2500e4b17023SJohn Marino 	return false;
2501e4b17023SJohn Marino       return !in_array_bounds_p (expr);
2502e4b17023SJohn Marino 
2503e4b17023SJohn Marino     case MEM_REF:
2504e4b17023SJohn Marino       if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
2505e4b17023SJohn Marino 	return false;
2506e4b17023SJohn Marino       /* Fallthru.  */
2507e4b17023SJohn Marino     case INDIRECT_REF:
2508e4b17023SJohn Marino       return !TREE_THIS_NOTRAP (expr);
2509e4b17023SJohn Marino 
2510e4b17023SJohn Marino     case ASM_EXPR:
2511e4b17023SJohn Marino       return TREE_THIS_VOLATILE (expr);
2512e4b17023SJohn Marino 
2513e4b17023SJohn Marino     case CALL_EXPR:
2514e4b17023SJohn Marino       t = get_callee_fndecl (expr);
2515e4b17023SJohn Marino       /* Assume that calls to weak functions may trap.  */
2516e4b17023SJohn Marino       if (!t || !DECL_P (t))
2517e4b17023SJohn Marino 	return true;
2518e4b17023SJohn Marino       if (DECL_WEAK (t))
2519e4b17023SJohn Marino 	return tree_could_trap_p (t);
2520e4b17023SJohn Marino       return false;
2521e4b17023SJohn Marino 
2522e4b17023SJohn Marino     case FUNCTION_DECL:
2523e4b17023SJohn Marino       /* Assume that accesses to weak functions may trap, unless we know
2524e4b17023SJohn Marino 	 they are certainly defined in current TU or in some other
2525e4b17023SJohn Marino 	 LTO partition.  */
2526e4b17023SJohn Marino       if (DECL_WEAK (expr))
2527e4b17023SJohn Marino 	{
2528e4b17023SJohn Marino 	  struct cgraph_node *node;
2529e4b17023SJohn Marino 	  if (!DECL_EXTERNAL (expr))
2530e4b17023SJohn Marino 	    return false;
2531e4b17023SJohn Marino 	  node = cgraph_function_node (cgraph_get_node (expr), NULL);
2532e4b17023SJohn Marino 	  if (node && node->in_other_partition)
2533e4b17023SJohn Marino 	    return false;
2534e4b17023SJohn Marino 	  return true;
2535e4b17023SJohn Marino 	}
2536e4b17023SJohn Marino       return false;
2537e4b17023SJohn Marino 
2538e4b17023SJohn Marino     case VAR_DECL:
2539e4b17023SJohn Marino       /* Assume that accesses to weak vars may trap, unless we know
2540e4b17023SJohn Marino 	 they are certainly defined in current TU or in some other
2541e4b17023SJohn Marino 	 LTO partition.  */
2542e4b17023SJohn Marino       if (DECL_WEAK (expr))
2543e4b17023SJohn Marino 	{
2544e4b17023SJohn Marino 	  struct varpool_node *node;
2545e4b17023SJohn Marino 	  if (!DECL_EXTERNAL (expr))
2546e4b17023SJohn Marino 	    return false;
2547e4b17023SJohn Marino 	  node = varpool_variable_node (varpool_get_node (expr), NULL);
2548e4b17023SJohn Marino 	  if (node && node->in_other_partition)
2549e4b17023SJohn Marino 	    return false;
2550e4b17023SJohn Marino 	  return true;
2551e4b17023SJohn Marino 	}
2552e4b17023SJohn Marino       return false;
2553e4b17023SJohn Marino 
2554e4b17023SJohn Marino     default:
2555e4b17023SJohn Marino       return false;
2556e4b17023SJohn Marino     }
2557e4b17023SJohn Marino }
2558e4b17023SJohn Marino 
2559e4b17023SJohn Marino 
2560e4b17023SJohn Marino /* Helper for stmt_could_throw_p.  Return true if STMT (assumed to be a
2561e4b17023SJohn Marino    an assignment or a conditional) may throw.  */
2562e4b17023SJohn Marino 
2563e4b17023SJohn Marino static bool
stmt_could_throw_1_p(gimple stmt)2564e4b17023SJohn Marino stmt_could_throw_1_p (gimple stmt)
2565e4b17023SJohn Marino {
2566e4b17023SJohn Marino   enum tree_code code = gimple_expr_code (stmt);
2567e4b17023SJohn Marino   bool honor_nans = false;
2568e4b17023SJohn Marino   bool honor_snans = false;
2569e4b17023SJohn Marino   bool fp_operation = false;
2570e4b17023SJohn Marino   bool honor_trapv = false;
2571e4b17023SJohn Marino   tree t;
2572e4b17023SJohn Marino   size_t i;
2573e4b17023SJohn Marino   bool handled, ret;
2574e4b17023SJohn Marino 
2575e4b17023SJohn Marino   if (TREE_CODE_CLASS (code) == tcc_comparison
2576e4b17023SJohn Marino       || TREE_CODE_CLASS (code) == tcc_unary
2577e4b17023SJohn Marino       || TREE_CODE_CLASS (code) == tcc_binary)
2578e4b17023SJohn Marino     {
2579e4b17023SJohn Marino       if (is_gimple_assign (stmt)
2580e4b17023SJohn Marino 	  && TREE_CODE_CLASS (code) == tcc_comparison)
2581e4b17023SJohn Marino 	t = TREE_TYPE (gimple_assign_rhs1 (stmt));
2582e4b17023SJohn Marino       else if (gimple_code (stmt) == GIMPLE_COND)
2583e4b17023SJohn Marino 	t = TREE_TYPE (gimple_cond_lhs (stmt));
2584e4b17023SJohn Marino       else
2585e4b17023SJohn Marino 	t = gimple_expr_type (stmt);
2586e4b17023SJohn Marino       fp_operation = FLOAT_TYPE_P (t);
2587e4b17023SJohn Marino       if (fp_operation)
2588e4b17023SJohn Marino 	{
2589e4b17023SJohn Marino 	  honor_nans = flag_trapping_math && !flag_finite_math_only;
2590e4b17023SJohn Marino 	  honor_snans = flag_signaling_nans != 0;
2591e4b17023SJohn Marino 	}
2592e4b17023SJohn Marino       else if (INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t))
2593e4b17023SJohn Marino 	honor_trapv = true;
2594e4b17023SJohn Marino     }
2595e4b17023SJohn Marino 
2596e4b17023SJohn Marino   /* Check if the main expression may trap.  */
2597e4b17023SJohn Marino   t = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) : NULL;
2598e4b17023SJohn Marino   ret = operation_could_trap_helper_p (code, fp_operation, honor_trapv,
2599e4b17023SJohn Marino 				       honor_nans, honor_snans, t,
2600e4b17023SJohn Marino 				       &handled);
2601e4b17023SJohn Marino   if (handled)
2602e4b17023SJohn Marino     return ret;
2603e4b17023SJohn Marino 
2604e4b17023SJohn Marino   /* If the expression does not trap, see if any of the individual operands may
2605e4b17023SJohn Marino      trap.  */
2606e4b17023SJohn Marino   for (i = 0; i < gimple_num_ops (stmt); i++)
2607e4b17023SJohn Marino     if (tree_could_trap_p (gimple_op (stmt, i)))
2608e4b17023SJohn Marino       return true;
2609e4b17023SJohn Marino 
2610e4b17023SJohn Marino   return false;
2611e4b17023SJohn Marino }
2612e4b17023SJohn Marino 
2613e4b17023SJohn Marino 
2614e4b17023SJohn Marino /* Return true if statement STMT could throw an exception.  */
2615e4b17023SJohn Marino 
2616e4b17023SJohn Marino bool
stmt_could_throw_p(gimple stmt)2617e4b17023SJohn Marino stmt_could_throw_p (gimple stmt)
2618e4b17023SJohn Marino {
2619e4b17023SJohn Marino   if (!flag_exceptions)
2620e4b17023SJohn Marino     return false;
2621e4b17023SJohn Marino 
2622e4b17023SJohn Marino   /* The only statements that can throw an exception are assignments,
2623e4b17023SJohn Marino      conditionals, calls, resx, and asms.  */
2624e4b17023SJohn Marino   switch (gimple_code (stmt))
2625e4b17023SJohn Marino     {
2626e4b17023SJohn Marino     case GIMPLE_RESX:
2627e4b17023SJohn Marino       return true;
2628e4b17023SJohn Marino 
2629e4b17023SJohn Marino     case GIMPLE_CALL:
2630e4b17023SJohn Marino       return !gimple_call_nothrow_p (stmt);
2631e4b17023SJohn Marino 
2632e4b17023SJohn Marino     case GIMPLE_ASSIGN:
2633e4b17023SJohn Marino     case GIMPLE_COND:
2634e4b17023SJohn Marino       if (!cfun->can_throw_non_call_exceptions)
2635e4b17023SJohn Marino         return false;
2636e4b17023SJohn Marino       return stmt_could_throw_1_p (stmt);
2637e4b17023SJohn Marino 
2638e4b17023SJohn Marino     case GIMPLE_ASM:
2639e4b17023SJohn Marino       if (!cfun->can_throw_non_call_exceptions)
2640e4b17023SJohn Marino         return false;
2641e4b17023SJohn Marino       return gimple_asm_volatile_p (stmt);
2642e4b17023SJohn Marino 
2643e4b17023SJohn Marino     default:
2644e4b17023SJohn Marino       return false;
2645e4b17023SJohn Marino     }
2646e4b17023SJohn Marino }
2647e4b17023SJohn Marino 
2648e4b17023SJohn Marino 
2649e4b17023SJohn Marino /* Return true if expression T could throw an exception.  */
2650e4b17023SJohn Marino 
2651e4b17023SJohn Marino bool
tree_could_throw_p(tree t)2652e4b17023SJohn Marino tree_could_throw_p (tree t)
2653e4b17023SJohn Marino {
2654e4b17023SJohn Marino   if (!flag_exceptions)
2655e4b17023SJohn Marino     return false;
2656e4b17023SJohn Marino   if (TREE_CODE (t) == MODIFY_EXPR)
2657e4b17023SJohn Marino     {
2658e4b17023SJohn Marino       if (cfun->can_throw_non_call_exceptions
2659e4b17023SJohn Marino           && tree_could_trap_p (TREE_OPERAND (t, 0)))
2660e4b17023SJohn Marino         return true;
2661e4b17023SJohn Marino       t = TREE_OPERAND (t, 1);
2662e4b17023SJohn Marino     }
2663e4b17023SJohn Marino 
2664e4b17023SJohn Marino   if (TREE_CODE (t) == WITH_SIZE_EXPR)
2665e4b17023SJohn Marino     t = TREE_OPERAND (t, 0);
2666e4b17023SJohn Marino   if (TREE_CODE (t) == CALL_EXPR)
2667e4b17023SJohn Marino     return (call_expr_flags (t) & ECF_NOTHROW) == 0;
2668e4b17023SJohn Marino   if (cfun->can_throw_non_call_exceptions)
2669e4b17023SJohn Marino     return tree_could_trap_p (t);
2670e4b17023SJohn Marino   return false;
2671e4b17023SJohn Marino }
2672e4b17023SJohn Marino 
2673e4b17023SJohn Marino /* Return true if STMT can throw an exception that is not caught within
2674e4b17023SJohn Marino    the current function (CFUN).  */
2675e4b17023SJohn Marino 
2676e4b17023SJohn Marino bool
stmt_can_throw_external(gimple stmt)2677e4b17023SJohn Marino stmt_can_throw_external (gimple stmt)
2678e4b17023SJohn Marino {
2679e4b17023SJohn Marino   int lp_nr;
2680e4b17023SJohn Marino 
2681e4b17023SJohn Marino   if (!stmt_could_throw_p (stmt))
2682e4b17023SJohn Marino     return false;
2683e4b17023SJohn Marino 
2684e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (stmt);
2685e4b17023SJohn Marino   return lp_nr == 0;
2686e4b17023SJohn Marino }
2687e4b17023SJohn Marino 
2688e4b17023SJohn Marino /* Return true if STMT can throw an exception that is caught within
2689e4b17023SJohn Marino    the current function (CFUN).  */
2690e4b17023SJohn Marino 
2691e4b17023SJohn Marino bool
stmt_can_throw_internal(gimple stmt)2692e4b17023SJohn Marino stmt_can_throw_internal (gimple stmt)
2693e4b17023SJohn Marino {
2694e4b17023SJohn Marino   int lp_nr;
2695e4b17023SJohn Marino 
2696e4b17023SJohn Marino   if (!stmt_could_throw_p (stmt))
2697e4b17023SJohn Marino     return false;
2698e4b17023SJohn Marino 
2699e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (stmt);
2700e4b17023SJohn Marino   return lp_nr > 0;
2701e4b17023SJohn Marino }
2702e4b17023SJohn Marino 
2703e4b17023SJohn Marino /* Given a statement STMT in IFUN, if STMT can no longer throw, then
2704e4b17023SJohn Marino    remove any entry it might have from the EH table.  Return true if
2705e4b17023SJohn Marino    any change was made.  */
2706e4b17023SJohn Marino 
2707e4b17023SJohn Marino bool
maybe_clean_eh_stmt_fn(struct function * ifun,gimple stmt)2708e4b17023SJohn Marino maybe_clean_eh_stmt_fn (struct function *ifun, gimple stmt)
2709e4b17023SJohn Marino {
2710e4b17023SJohn Marino   if (stmt_could_throw_p (stmt))
2711e4b17023SJohn Marino     return false;
2712e4b17023SJohn Marino   return remove_stmt_from_eh_lp_fn (ifun, stmt);
2713e4b17023SJohn Marino }
2714e4b17023SJohn Marino 
2715e4b17023SJohn Marino /* Likewise, but always use the current function.  */
2716e4b17023SJohn Marino 
2717e4b17023SJohn Marino bool
maybe_clean_eh_stmt(gimple stmt)2718e4b17023SJohn Marino maybe_clean_eh_stmt (gimple stmt)
2719e4b17023SJohn Marino {
2720e4b17023SJohn Marino   return maybe_clean_eh_stmt_fn (cfun, stmt);
2721e4b17023SJohn Marino }
2722e4b17023SJohn Marino 
2723e4b17023SJohn Marino /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2724e4b17023SJohn Marino    OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2725e4b17023SJohn Marino    in the table if it should be in there.  Return TRUE if a replacement was
2726e4b17023SJohn Marino    done that my require an EH edge purge.  */
2727e4b17023SJohn Marino 
2728e4b17023SJohn Marino bool
maybe_clean_or_replace_eh_stmt(gimple old_stmt,gimple new_stmt)2729e4b17023SJohn Marino maybe_clean_or_replace_eh_stmt (gimple old_stmt, gimple new_stmt)
2730e4b17023SJohn Marino {
2731e4b17023SJohn Marino   int lp_nr = lookup_stmt_eh_lp (old_stmt);
2732e4b17023SJohn Marino 
2733e4b17023SJohn Marino   if (lp_nr != 0)
2734e4b17023SJohn Marino     {
2735e4b17023SJohn Marino       bool new_stmt_could_throw = stmt_could_throw_p (new_stmt);
2736e4b17023SJohn Marino 
2737e4b17023SJohn Marino       if (new_stmt == old_stmt && new_stmt_could_throw)
2738e4b17023SJohn Marino 	return false;
2739e4b17023SJohn Marino 
2740e4b17023SJohn Marino       remove_stmt_from_eh_lp (old_stmt);
2741e4b17023SJohn Marino       if (new_stmt_could_throw)
2742e4b17023SJohn Marino 	{
2743e4b17023SJohn Marino 	  add_stmt_to_eh_lp (new_stmt, lp_nr);
2744e4b17023SJohn Marino 	  return false;
2745e4b17023SJohn Marino 	}
2746e4b17023SJohn Marino       else
2747e4b17023SJohn Marino 	return true;
2748e4b17023SJohn Marino     }
2749e4b17023SJohn Marino 
2750e4b17023SJohn Marino   return false;
2751e4b17023SJohn Marino }
2752e4b17023SJohn Marino 
2753e4b17023SJohn Marino /* Given a statement OLD_STMT in OLD_FUN and a duplicate statment NEW_STMT
2754e4b17023SJohn Marino    in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT.  The MAP
2755e4b17023SJohn Marino    operand is the return value of duplicate_eh_regions.  */
2756e4b17023SJohn Marino 
2757e4b17023SJohn Marino bool
maybe_duplicate_eh_stmt_fn(struct function * new_fun,gimple new_stmt,struct function * old_fun,gimple old_stmt,struct pointer_map_t * map,int default_lp_nr)2758e4b17023SJohn Marino maybe_duplicate_eh_stmt_fn (struct function *new_fun, gimple new_stmt,
2759e4b17023SJohn Marino 			    struct function *old_fun, gimple old_stmt,
2760e4b17023SJohn Marino 			    struct pointer_map_t *map, int default_lp_nr)
2761e4b17023SJohn Marino {
2762e4b17023SJohn Marino   int old_lp_nr, new_lp_nr;
2763e4b17023SJohn Marino   void **slot;
2764e4b17023SJohn Marino 
2765e4b17023SJohn Marino   if (!stmt_could_throw_p (new_stmt))
2766e4b17023SJohn Marino     return false;
2767e4b17023SJohn Marino 
2768e4b17023SJohn Marino   old_lp_nr = lookup_stmt_eh_lp_fn (old_fun, old_stmt);
2769e4b17023SJohn Marino   if (old_lp_nr == 0)
2770e4b17023SJohn Marino     {
2771e4b17023SJohn Marino       if (default_lp_nr == 0)
2772e4b17023SJohn Marino 	return false;
2773e4b17023SJohn Marino       new_lp_nr = default_lp_nr;
2774e4b17023SJohn Marino     }
2775e4b17023SJohn Marino   else if (old_lp_nr > 0)
2776e4b17023SJohn Marino     {
2777e4b17023SJohn Marino       eh_landing_pad old_lp, new_lp;
2778e4b17023SJohn Marino 
2779e4b17023SJohn Marino       old_lp = VEC_index (eh_landing_pad, old_fun->eh->lp_array, old_lp_nr);
2780e4b17023SJohn Marino       slot = pointer_map_contains (map, old_lp);
2781e4b17023SJohn Marino       new_lp = (eh_landing_pad) *slot;
2782e4b17023SJohn Marino       new_lp_nr = new_lp->index;
2783e4b17023SJohn Marino     }
2784e4b17023SJohn Marino   else
2785e4b17023SJohn Marino     {
2786e4b17023SJohn Marino       eh_region old_r, new_r;
2787e4b17023SJohn Marino 
2788e4b17023SJohn Marino       old_r = VEC_index (eh_region, old_fun->eh->region_array, -old_lp_nr);
2789e4b17023SJohn Marino       slot = pointer_map_contains (map, old_r);
2790e4b17023SJohn Marino       new_r = (eh_region) *slot;
2791e4b17023SJohn Marino       new_lp_nr = -new_r->index;
2792e4b17023SJohn Marino     }
2793e4b17023SJohn Marino 
2794e4b17023SJohn Marino   add_stmt_to_eh_lp_fn (new_fun, new_stmt, new_lp_nr);
2795e4b17023SJohn Marino   return true;
2796e4b17023SJohn Marino }
2797e4b17023SJohn Marino 
2798e4b17023SJohn Marino /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
2799e4b17023SJohn Marino    and thus no remapping is required.  */
2800e4b17023SJohn Marino 
2801e4b17023SJohn Marino bool
maybe_duplicate_eh_stmt(gimple new_stmt,gimple old_stmt)2802e4b17023SJohn Marino maybe_duplicate_eh_stmt (gimple new_stmt, gimple old_stmt)
2803e4b17023SJohn Marino {
2804e4b17023SJohn Marino   int lp_nr;
2805e4b17023SJohn Marino 
2806e4b17023SJohn Marino   if (!stmt_could_throw_p (new_stmt))
2807e4b17023SJohn Marino     return false;
2808e4b17023SJohn Marino 
2809e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (old_stmt);
2810e4b17023SJohn Marino   if (lp_nr == 0)
2811e4b17023SJohn Marino     return false;
2812e4b17023SJohn Marino 
2813e4b17023SJohn Marino   add_stmt_to_eh_lp (new_stmt, lp_nr);
2814e4b17023SJohn Marino   return true;
2815e4b17023SJohn Marino }
2816e4b17023SJohn Marino 
2817e4b17023SJohn Marino /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
2818e4b17023SJohn Marino    GIMPLE_TRY) that are similar enough to be considered the same.  Currently
2819e4b17023SJohn Marino    this only handles handlers consisting of a single call, as that's the
2820e4b17023SJohn Marino    important case for C++: a destructor call for a particular object showing
2821e4b17023SJohn Marino    up in multiple handlers.  */
2822e4b17023SJohn Marino 
2823e4b17023SJohn Marino static bool
same_handler_p(gimple_seq oneh,gimple_seq twoh)2824e4b17023SJohn Marino same_handler_p (gimple_seq oneh, gimple_seq twoh)
2825e4b17023SJohn Marino {
2826e4b17023SJohn Marino   gimple_stmt_iterator gsi;
2827e4b17023SJohn Marino   gimple ones, twos;
2828e4b17023SJohn Marino   unsigned int ai;
2829e4b17023SJohn Marino 
2830e4b17023SJohn Marino   gsi = gsi_start (oneh);
2831e4b17023SJohn Marino   if (!gsi_one_before_end_p (gsi))
2832e4b17023SJohn Marino     return false;
2833e4b17023SJohn Marino   ones = gsi_stmt (gsi);
2834e4b17023SJohn Marino 
2835e4b17023SJohn Marino   gsi = gsi_start (twoh);
2836e4b17023SJohn Marino   if (!gsi_one_before_end_p (gsi))
2837e4b17023SJohn Marino     return false;
2838e4b17023SJohn Marino   twos = gsi_stmt (gsi);
2839e4b17023SJohn Marino 
2840e4b17023SJohn Marino   if (!is_gimple_call (ones)
2841e4b17023SJohn Marino       || !is_gimple_call (twos)
2842e4b17023SJohn Marino       || gimple_call_lhs (ones)
2843e4b17023SJohn Marino       || gimple_call_lhs (twos)
2844e4b17023SJohn Marino       || gimple_call_chain (ones)
2845e4b17023SJohn Marino       || gimple_call_chain (twos)
2846e4b17023SJohn Marino       || !gimple_call_same_target_p (ones, twos)
2847e4b17023SJohn Marino       || gimple_call_num_args (ones) != gimple_call_num_args (twos))
2848e4b17023SJohn Marino     return false;
2849e4b17023SJohn Marino 
2850e4b17023SJohn Marino   for (ai = 0; ai < gimple_call_num_args (ones); ++ai)
2851e4b17023SJohn Marino     if (!operand_equal_p (gimple_call_arg (ones, ai),
2852e4b17023SJohn Marino                           gimple_call_arg (twos, ai), 0))
2853e4b17023SJohn Marino       return false;
2854e4b17023SJohn Marino 
2855e4b17023SJohn Marino   return true;
2856e4b17023SJohn Marino }
2857e4b17023SJohn Marino 
2858e4b17023SJohn Marino /* Optimize
2859e4b17023SJohn Marino     try { A() } finally { try { ~B() } catch { ~A() } }
2860e4b17023SJohn Marino     try { ... } finally { ~A() }
2861e4b17023SJohn Marino    into
2862e4b17023SJohn Marino     try { A() } catch { ~B() }
2863e4b17023SJohn Marino     try { ~B() ... } finally { ~A() }
2864e4b17023SJohn Marino 
2865e4b17023SJohn Marino    This occurs frequently in C++, where A is a local variable and B is a
2866e4b17023SJohn Marino    temporary used in the initializer for A.  */
2867e4b17023SJohn Marino 
2868e4b17023SJohn Marino static void
optimize_double_finally(gimple one,gimple two)2869e4b17023SJohn Marino optimize_double_finally (gimple one, gimple two)
2870e4b17023SJohn Marino {
2871e4b17023SJohn Marino   gimple oneh;
2872e4b17023SJohn Marino   gimple_stmt_iterator gsi;
2873e4b17023SJohn Marino 
2874e4b17023SJohn Marino   gsi = gsi_start (gimple_try_cleanup (one));
2875e4b17023SJohn Marino   if (!gsi_one_before_end_p (gsi))
2876e4b17023SJohn Marino     return;
2877e4b17023SJohn Marino 
2878e4b17023SJohn Marino   oneh = gsi_stmt (gsi);
2879e4b17023SJohn Marino   if (gimple_code (oneh) != GIMPLE_TRY
2880e4b17023SJohn Marino       || gimple_try_kind (oneh) != GIMPLE_TRY_CATCH)
2881e4b17023SJohn Marino     return;
2882e4b17023SJohn Marino 
2883e4b17023SJohn Marino   if (same_handler_p (gimple_try_cleanup (oneh), gimple_try_cleanup (two)))
2884e4b17023SJohn Marino     {
2885e4b17023SJohn Marino       gimple_seq seq = gimple_try_eval (oneh);
2886e4b17023SJohn Marino 
2887e4b17023SJohn Marino       gimple_try_set_cleanup (one, seq);
2888e4b17023SJohn Marino       gimple_try_set_kind (one, GIMPLE_TRY_CATCH);
2889e4b17023SJohn Marino       seq = copy_gimple_seq_and_replace_locals (seq);
2890e4b17023SJohn Marino       gimple_seq_add_seq (&seq, gimple_try_eval (two));
2891e4b17023SJohn Marino       gimple_try_set_eval (two, seq);
2892e4b17023SJohn Marino     }
2893e4b17023SJohn Marino }
2894e4b17023SJohn Marino 
2895e4b17023SJohn Marino /* Perform EH refactoring optimizations that are simpler to do when code
2896e4b17023SJohn Marino    flow has been lowered but EH structures haven't.  */
2897e4b17023SJohn Marino 
2898e4b17023SJohn Marino static void
refactor_eh_r(gimple_seq seq)2899e4b17023SJohn Marino refactor_eh_r (gimple_seq seq)
2900e4b17023SJohn Marino {
2901e4b17023SJohn Marino   gimple_stmt_iterator gsi;
2902e4b17023SJohn Marino   gimple one, two;
2903e4b17023SJohn Marino 
2904e4b17023SJohn Marino   one = NULL;
2905e4b17023SJohn Marino   two = NULL;
2906e4b17023SJohn Marino   gsi = gsi_start (seq);
2907e4b17023SJohn Marino   while (1)
2908e4b17023SJohn Marino     {
2909e4b17023SJohn Marino       one = two;
2910e4b17023SJohn Marino       if (gsi_end_p (gsi))
2911e4b17023SJohn Marino 	two = NULL;
2912e4b17023SJohn Marino       else
2913e4b17023SJohn Marino 	two = gsi_stmt (gsi);
2914e4b17023SJohn Marino       if (one
2915e4b17023SJohn Marino 	  && two
2916e4b17023SJohn Marino 	  && gimple_code (one) == GIMPLE_TRY
2917e4b17023SJohn Marino 	  && gimple_code (two) == GIMPLE_TRY
2918e4b17023SJohn Marino 	  && gimple_try_kind (one) == GIMPLE_TRY_FINALLY
2919e4b17023SJohn Marino 	  && gimple_try_kind (two) == GIMPLE_TRY_FINALLY)
2920e4b17023SJohn Marino 	optimize_double_finally (one, two);
2921e4b17023SJohn Marino       if (one)
2922e4b17023SJohn Marino 	switch (gimple_code (one))
2923e4b17023SJohn Marino 	  {
2924e4b17023SJohn Marino 	  case GIMPLE_TRY:
2925e4b17023SJohn Marino 	    refactor_eh_r (gimple_try_eval (one));
2926e4b17023SJohn Marino 	    refactor_eh_r (gimple_try_cleanup (one));
2927e4b17023SJohn Marino 	    break;
2928e4b17023SJohn Marino 	  case GIMPLE_CATCH:
2929e4b17023SJohn Marino 	    refactor_eh_r (gimple_catch_handler (one));
2930e4b17023SJohn Marino 	    break;
2931e4b17023SJohn Marino 	  case GIMPLE_EH_FILTER:
2932e4b17023SJohn Marino 	    refactor_eh_r (gimple_eh_filter_failure (one));
2933e4b17023SJohn Marino 	    break;
2934e4b17023SJohn Marino 	  case GIMPLE_EH_ELSE:
2935e4b17023SJohn Marino 	    refactor_eh_r (gimple_eh_else_n_body (one));
2936e4b17023SJohn Marino 	    refactor_eh_r (gimple_eh_else_e_body (one));
2937e4b17023SJohn Marino 	    break;
2938e4b17023SJohn Marino 	  default:
2939e4b17023SJohn Marino 	    break;
2940e4b17023SJohn Marino 	  }
2941e4b17023SJohn Marino       if (two)
2942e4b17023SJohn Marino 	gsi_next (&gsi);
2943e4b17023SJohn Marino       else
2944e4b17023SJohn Marino 	break;
2945e4b17023SJohn Marino     }
2946e4b17023SJohn Marino }
2947e4b17023SJohn Marino 
2948e4b17023SJohn Marino static unsigned
refactor_eh(void)2949e4b17023SJohn Marino refactor_eh (void)
2950e4b17023SJohn Marino {
2951e4b17023SJohn Marino   refactor_eh_r (gimple_body (current_function_decl));
2952e4b17023SJohn Marino   return 0;
2953e4b17023SJohn Marino }
2954e4b17023SJohn Marino 
2955e4b17023SJohn Marino static bool
gate_refactor_eh(void)2956e4b17023SJohn Marino gate_refactor_eh (void)
2957e4b17023SJohn Marino {
2958e4b17023SJohn Marino   return flag_exceptions != 0;
2959e4b17023SJohn Marino }
2960e4b17023SJohn Marino 
2961e4b17023SJohn Marino struct gimple_opt_pass pass_refactor_eh =
2962e4b17023SJohn Marino {
2963e4b17023SJohn Marino  {
2964e4b17023SJohn Marino   GIMPLE_PASS,
2965e4b17023SJohn Marino   "ehopt",				/* name */
2966e4b17023SJohn Marino   gate_refactor_eh,			/* gate */
2967e4b17023SJohn Marino   refactor_eh,				/* execute */
2968e4b17023SJohn Marino   NULL,					/* sub */
2969e4b17023SJohn Marino   NULL,					/* next */
2970e4b17023SJohn Marino   0,					/* static_pass_number */
2971e4b17023SJohn Marino   TV_TREE_EH,				/* tv_id */
2972e4b17023SJohn Marino   PROP_gimple_lcf,			/* properties_required */
2973e4b17023SJohn Marino   0,					/* properties_provided */
2974e4b17023SJohn Marino   0,					/* properties_destroyed */
2975e4b17023SJohn Marino   0,					/* todo_flags_start */
2976e4b17023SJohn Marino   0             			/* todo_flags_finish */
2977e4b17023SJohn Marino  }
2978e4b17023SJohn Marino };
2979e4b17023SJohn Marino 
2980e4b17023SJohn Marino /* At the end of gimple optimization, we can lower RESX.  */
2981e4b17023SJohn Marino 
2982e4b17023SJohn Marino static bool
lower_resx(basic_block bb,gimple stmt,struct pointer_map_t * mnt_map)2983e4b17023SJohn Marino lower_resx (basic_block bb, gimple stmt, struct pointer_map_t *mnt_map)
2984e4b17023SJohn Marino {
2985e4b17023SJohn Marino   int lp_nr;
2986e4b17023SJohn Marino   eh_region src_r, dst_r;
2987e4b17023SJohn Marino   gimple_stmt_iterator gsi;
2988e4b17023SJohn Marino   gimple x;
2989e4b17023SJohn Marino   tree fn, src_nr;
2990e4b17023SJohn Marino   bool ret = false;
2991e4b17023SJohn Marino 
2992e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (stmt);
2993e4b17023SJohn Marino   if (lp_nr != 0)
2994e4b17023SJohn Marino     dst_r = get_eh_region_from_lp_number (lp_nr);
2995e4b17023SJohn Marino   else
2996e4b17023SJohn Marino     dst_r = NULL;
2997e4b17023SJohn Marino 
2998e4b17023SJohn Marino   src_r = get_eh_region_from_number (gimple_resx_region (stmt));
2999e4b17023SJohn Marino   gsi = gsi_last_bb (bb);
3000e4b17023SJohn Marino 
3001e4b17023SJohn Marino   if (src_r == NULL)
3002e4b17023SJohn Marino     {
3003e4b17023SJohn Marino       /* We can wind up with no source region when pass_cleanup_eh shows
3004e4b17023SJohn Marino 	 that there are no entries into an eh region and deletes it, but
3005e4b17023SJohn Marino 	 then the block that contains the resx isn't removed.  This can
3006e4b17023SJohn Marino 	 happen without optimization when the switch statement created by
3007e4b17023SJohn Marino 	 lower_try_finally_switch isn't simplified to remove the eh case.
3008e4b17023SJohn Marino 
3009e4b17023SJohn Marino 	 Resolve this by expanding the resx node to an abort.  */
3010e4b17023SJohn Marino 
3011e4b17023SJohn Marino       fn = builtin_decl_implicit (BUILT_IN_TRAP);
3012e4b17023SJohn Marino       x = gimple_build_call (fn, 0);
3013e4b17023SJohn Marino       gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3014e4b17023SJohn Marino 
3015e4b17023SJohn Marino       while (EDGE_COUNT (bb->succs) > 0)
3016e4b17023SJohn Marino 	remove_edge (EDGE_SUCC (bb, 0));
3017e4b17023SJohn Marino     }
3018e4b17023SJohn Marino   else if (dst_r)
3019e4b17023SJohn Marino     {
3020e4b17023SJohn Marino       /* When we have a destination region, we resolve this by copying
3021e4b17023SJohn Marino 	 the excptr and filter values into place, and changing the edge
3022e4b17023SJohn Marino 	 to immediately after the landing pad.  */
3023e4b17023SJohn Marino       edge e;
3024e4b17023SJohn Marino 
3025e4b17023SJohn Marino       if (lp_nr < 0)
3026e4b17023SJohn Marino 	{
3027e4b17023SJohn Marino 	  basic_block new_bb;
3028e4b17023SJohn Marino 	  void **slot;
3029e4b17023SJohn Marino 	  tree lab;
3030e4b17023SJohn Marino 
3031e4b17023SJohn Marino 	  /* We are resuming into a MUST_NOT_CALL region.  Expand a call to
3032e4b17023SJohn Marino 	     the failure decl into a new block, if needed.  */
3033e4b17023SJohn Marino 	  gcc_assert (dst_r->type == ERT_MUST_NOT_THROW);
3034e4b17023SJohn Marino 
3035e4b17023SJohn Marino 	  slot = pointer_map_contains (mnt_map, dst_r);
3036e4b17023SJohn Marino 	  if (slot == NULL)
3037e4b17023SJohn Marino 	    {
3038e4b17023SJohn Marino 	      gimple_stmt_iterator gsi2;
3039e4b17023SJohn Marino 
3040e4b17023SJohn Marino 	      new_bb = create_empty_bb (bb);
3041e4b17023SJohn Marino 	      lab = gimple_block_label (new_bb);
3042e4b17023SJohn Marino 	      gsi2 = gsi_start_bb (new_bb);
3043e4b17023SJohn Marino 
3044e4b17023SJohn Marino 	      fn = dst_r->u.must_not_throw.failure_decl;
3045e4b17023SJohn Marino 	      x = gimple_build_call (fn, 0);
3046e4b17023SJohn Marino 	      gimple_set_location (x, dst_r->u.must_not_throw.failure_loc);
3047e4b17023SJohn Marino 	      gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
3048e4b17023SJohn Marino 
3049e4b17023SJohn Marino 	      slot = pointer_map_insert (mnt_map, dst_r);
3050e4b17023SJohn Marino 	      *slot = lab;
3051e4b17023SJohn Marino 	    }
3052e4b17023SJohn Marino 	  else
3053e4b17023SJohn Marino 	    {
3054e4b17023SJohn Marino 	      lab = (tree) *slot;
3055e4b17023SJohn Marino 	      new_bb = label_to_block (lab);
3056e4b17023SJohn Marino 	    }
3057e4b17023SJohn Marino 
3058e4b17023SJohn Marino 	  gcc_assert (EDGE_COUNT (bb->succs) == 0);
3059e4b17023SJohn Marino 	  e = make_edge (bb, new_bb, EDGE_FALLTHRU);
3060e4b17023SJohn Marino 	  e->count = bb->count;
3061e4b17023SJohn Marino 	  e->probability = REG_BR_PROB_BASE;
3062e4b17023SJohn Marino 	}
3063e4b17023SJohn Marino       else
3064e4b17023SJohn Marino 	{
3065e4b17023SJohn Marino 	  edge_iterator ei;
3066e4b17023SJohn Marino 	  tree dst_nr = build_int_cst (integer_type_node, dst_r->index);
3067e4b17023SJohn Marino 
3068e4b17023SJohn Marino 	  fn = builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES);
3069e4b17023SJohn Marino 	  src_nr = build_int_cst (integer_type_node, src_r->index);
3070e4b17023SJohn Marino 	  x = gimple_build_call (fn, 2, dst_nr, src_nr);
3071e4b17023SJohn Marino 	  gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3072e4b17023SJohn Marino 
3073e4b17023SJohn Marino 	  /* Update the flags for the outgoing edge.  */
3074e4b17023SJohn Marino 	  e = single_succ_edge (bb);
3075e4b17023SJohn Marino 	  gcc_assert (e->flags & EDGE_EH);
3076e4b17023SJohn Marino 	  e->flags = (e->flags & ~EDGE_EH) | EDGE_FALLTHRU;
3077e4b17023SJohn Marino 
3078e4b17023SJohn Marino 	  /* If there are no more EH users of the landing pad, delete it.  */
3079e4b17023SJohn Marino 	  FOR_EACH_EDGE (e, ei, e->dest->preds)
3080e4b17023SJohn Marino 	    if (e->flags & EDGE_EH)
3081e4b17023SJohn Marino 	      break;
3082e4b17023SJohn Marino 	  if (e == NULL)
3083e4b17023SJohn Marino 	    {
3084e4b17023SJohn Marino 	      eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
3085e4b17023SJohn Marino 	      remove_eh_landing_pad (lp);
3086e4b17023SJohn Marino 	    }
3087e4b17023SJohn Marino 	}
3088e4b17023SJohn Marino 
3089e4b17023SJohn Marino       ret = true;
3090e4b17023SJohn Marino     }
3091e4b17023SJohn Marino   else
3092e4b17023SJohn Marino     {
3093e4b17023SJohn Marino       tree var;
3094e4b17023SJohn Marino 
3095e4b17023SJohn Marino       /* When we don't have a destination region, this exception escapes
3096e4b17023SJohn Marino 	 up the call chain.  We resolve this by generating a call to the
3097e4b17023SJohn Marino 	 _Unwind_Resume library function.  */
3098e4b17023SJohn Marino 
3099e4b17023SJohn Marino       /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3100e4b17023SJohn Marino 	 with no arguments for C++ and Java.  Check for that.  */
3101e4b17023SJohn Marino       if (src_r->use_cxa_end_cleanup)
3102e4b17023SJohn Marino 	{
3103e4b17023SJohn Marino 	  fn = builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP);
3104e4b17023SJohn Marino 	  x = gimple_build_call (fn, 0);
3105e4b17023SJohn Marino 	  gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3106e4b17023SJohn Marino 	}
3107e4b17023SJohn Marino       else
3108e4b17023SJohn Marino 	{
3109e4b17023SJohn Marino 	  fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
3110e4b17023SJohn Marino 	  src_nr = build_int_cst (integer_type_node, src_r->index);
3111e4b17023SJohn Marino 	  x = gimple_build_call (fn, 1, src_nr);
3112e4b17023SJohn Marino 	  var = create_tmp_var (ptr_type_node, NULL);
3113e4b17023SJohn Marino 	  var = make_ssa_name (var, x);
3114e4b17023SJohn Marino 	  gimple_call_set_lhs (x, var);
3115e4b17023SJohn Marino 	  gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3116e4b17023SJohn Marino 
3117e4b17023SJohn Marino 	  fn = builtin_decl_implicit (BUILT_IN_UNWIND_RESUME);
3118e4b17023SJohn Marino 	  x = gimple_build_call (fn, 1, var);
3119e4b17023SJohn Marino 	  gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3120e4b17023SJohn Marino 	}
3121e4b17023SJohn Marino 
3122e4b17023SJohn Marino       gcc_assert (EDGE_COUNT (bb->succs) == 0);
3123e4b17023SJohn Marino     }
3124e4b17023SJohn Marino 
3125e4b17023SJohn Marino   gsi_remove (&gsi, true);
3126e4b17023SJohn Marino 
3127e4b17023SJohn Marino   return ret;
3128e4b17023SJohn Marino }
3129e4b17023SJohn Marino 
3130e4b17023SJohn Marino static unsigned
execute_lower_resx(void)3131e4b17023SJohn Marino execute_lower_resx (void)
3132e4b17023SJohn Marino {
3133e4b17023SJohn Marino   basic_block bb;
3134e4b17023SJohn Marino   struct pointer_map_t *mnt_map;
3135e4b17023SJohn Marino   bool dominance_invalidated = false;
3136e4b17023SJohn Marino   bool any_rewritten = false;
3137e4b17023SJohn Marino 
3138e4b17023SJohn Marino   mnt_map = pointer_map_create ();
3139e4b17023SJohn Marino 
3140e4b17023SJohn Marino   FOR_EACH_BB (bb)
3141e4b17023SJohn Marino     {
3142e4b17023SJohn Marino       gimple last = last_stmt (bb);
3143e4b17023SJohn Marino       if (last && is_gimple_resx (last))
3144e4b17023SJohn Marino 	{
3145e4b17023SJohn Marino 	  dominance_invalidated |= lower_resx (bb, last, mnt_map);
3146e4b17023SJohn Marino 	  any_rewritten = true;
3147e4b17023SJohn Marino 	}
3148e4b17023SJohn Marino     }
3149e4b17023SJohn Marino 
3150e4b17023SJohn Marino   pointer_map_destroy (mnt_map);
3151e4b17023SJohn Marino 
3152e4b17023SJohn Marino   if (dominance_invalidated)
3153e4b17023SJohn Marino     {
3154e4b17023SJohn Marino       free_dominance_info (CDI_DOMINATORS);
3155e4b17023SJohn Marino       free_dominance_info (CDI_POST_DOMINATORS);
3156e4b17023SJohn Marino     }
3157e4b17023SJohn Marino 
3158e4b17023SJohn Marino   return any_rewritten ? TODO_update_ssa_only_virtuals : 0;
3159e4b17023SJohn Marino }
3160e4b17023SJohn Marino 
3161e4b17023SJohn Marino static bool
gate_lower_resx(void)3162e4b17023SJohn Marino gate_lower_resx (void)
3163e4b17023SJohn Marino {
3164e4b17023SJohn Marino   return flag_exceptions != 0;
3165e4b17023SJohn Marino }
3166e4b17023SJohn Marino 
3167e4b17023SJohn Marino struct gimple_opt_pass pass_lower_resx =
3168e4b17023SJohn Marino {
3169e4b17023SJohn Marino  {
3170e4b17023SJohn Marino   GIMPLE_PASS,
3171e4b17023SJohn Marino   "resx",				/* name */
3172e4b17023SJohn Marino   gate_lower_resx,			/* gate */
3173e4b17023SJohn Marino   execute_lower_resx,			/* execute */
3174e4b17023SJohn Marino   NULL,					/* sub */
3175e4b17023SJohn Marino   NULL,					/* next */
3176e4b17023SJohn Marino   0,					/* static_pass_number */
3177e4b17023SJohn Marino   TV_TREE_EH,				/* tv_id */
3178e4b17023SJohn Marino   PROP_gimple_lcf,			/* properties_required */
3179e4b17023SJohn Marino   0,					/* properties_provided */
3180e4b17023SJohn Marino   0,					/* properties_destroyed */
3181e4b17023SJohn Marino   0,					/* todo_flags_start */
3182e4b17023SJohn Marino   TODO_verify_flow	                /* todo_flags_finish */
3183e4b17023SJohn Marino  }
3184e4b17023SJohn Marino };
3185e4b17023SJohn Marino 
3186e4b17023SJohn Marino /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3187e4b17023SJohn Marino    external throw.  */
3188e4b17023SJohn Marino 
3189e4b17023SJohn Marino static void
optimize_clobbers(basic_block bb)3190e4b17023SJohn Marino optimize_clobbers (basic_block bb)
3191e4b17023SJohn Marino {
3192e4b17023SJohn Marino   gimple_stmt_iterator gsi = gsi_last_bb (bb);
3193e4b17023SJohn Marino   for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3194e4b17023SJohn Marino     {
3195e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
3196e4b17023SJohn Marino       if (is_gimple_debug (stmt))
3197e4b17023SJohn Marino 	continue;
3198e4b17023SJohn Marino       if (!gimple_clobber_p (stmt)
3199e4b17023SJohn Marino 	  || TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
3200e4b17023SJohn Marino 	return;
3201e4b17023SJohn Marino       unlink_stmt_vdef (stmt);
3202e4b17023SJohn Marino       gsi_remove (&gsi, true);
3203e4b17023SJohn Marino       release_defs (stmt);
3204e4b17023SJohn Marino     }
3205e4b17023SJohn Marino }
3206e4b17023SJohn Marino 
3207e4b17023SJohn Marino /* Try to sink var = {v} {CLOBBER} stmts followed just by
3208e4b17023SJohn Marino    internal throw to successor BB.  */
3209e4b17023SJohn Marino 
3210e4b17023SJohn Marino static int
sink_clobbers(basic_block bb)3211e4b17023SJohn Marino sink_clobbers (basic_block bb)
3212e4b17023SJohn Marino {
3213e4b17023SJohn Marino   edge e;
3214e4b17023SJohn Marino   edge_iterator ei;
3215e4b17023SJohn Marino   gimple_stmt_iterator gsi, dgsi;
3216e4b17023SJohn Marino   basic_block succbb;
3217e4b17023SJohn Marino   bool any_clobbers = false;
3218e4b17023SJohn Marino 
3219e4b17023SJohn Marino   /* Only optimize if BB has a single EH successor and
3220e4b17023SJohn Marino      all predecessor edges are EH too.  */
3221e4b17023SJohn Marino   if (!single_succ_p (bb)
3222e4b17023SJohn Marino       || (single_succ_edge (bb)->flags & EDGE_EH) == 0)
3223e4b17023SJohn Marino     return 0;
3224e4b17023SJohn Marino 
3225e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->preds)
3226e4b17023SJohn Marino     {
3227e4b17023SJohn Marino       if ((e->flags & EDGE_EH) == 0)
3228e4b17023SJohn Marino 	return 0;
3229e4b17023SJohn Marino     }
3230e4b17023SJohn Marino 
3231e4b17023SJohn Marino   /* And BB contains only CLOBBER stmts before the final
3232e4b17023SJohn Marino      RESX.  */
3233e4b17023SJohn Marino   gsi = gsi_last_bb (bb);
3234e4b17023SJohn Marino   for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3235e4b17023SJohn Marino     {
3236e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
3237e4b17023SJohn Marino       if (is_gimple_debug (stmt))
3238e4b17023SJohn Marino 	continue;
3239e4b17023SJohn Marino       if (gimple_code (stmt) == GIMPLE_LABEL)
3240e4b17023SJohn Marino 	break;
3241e4b17023SJohn Marino       if (!gimple_clobber_p (stmt)
3242e4b17023SJohn Marino 	  || TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
3243e4b17023SJohn Marino 	return 0;
3244e4b17023SJohn Marino       any_clobbers = true;
3245e4b17023SJohn Marino     }
3246e4b17023SJohn Marino   if (!any_clobbers)
3247e4b17023SJohn Marino     return 0;
3248e4b17023SJohn Marino 
3249e4b17023SJohn Marino   succbb = single_succ (bb);
3250e4b17023SJohn Marino   dgsi = gsi_after_labels (succbb);
3251e4b17023SJohn Marino   gsi = gsi_last_bb (bb);
3252e4b17023SJohn Marino   for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3253e4b17023SJohn Marino     {
3254e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
3255e4b17023SJohn Marino       tree vdef;
3256e4b17023SJohn Marino       if (is_gimple_debug (stmt))
3257e4b17023SJohn Marino 	continue;
3258e4b17023SJohn Marino       if (gimple_code (stmt) == GIMPLE_LABEL)
3259e4b17023SJohn Marino 	break;
3260e4b17023SJohn Marino       unlink_stmt_vdef (stmt);
3261e4b17023SJohn Marino       gsi_remove (&gsi, false);
3262e4b17023SJohn Marino       vdef = gimple_vdef (stmt);
3263e4b17023SJohn Marino       if (vdef && TREE_CODE (vdef) == SSA_NAME)
3264e4b17023SJohn Marino 	{
3265e4b17023SJohn Marino 	  vdef = SSA_NAME_VAR (vdef);
3266e4b17023SJohn Marino 	  mark_sym_for_renaming (vdef);
3267e4b17023SJohn Marino 	  gimple_set_vdef (stmt, vdef);
3268e4b17023SJohn Marino 	  gimple_set_vuse (stmt, vdef);
3269e4b17023SJohn Marino 	}
3270e4b17023SJohn Marino       release_defs (stmt);
3271e4b17023SJohn Marino       gsi_insert_before (&dgsi, stmt, GSI_SAME_STMT);
3272e4b17023SJohn Marino     }
3273e4b17023SJohn Marino 
3274e4b17023SJohn Marino   return TODO_update_ssa_only_virtuals;
3275e4b17023SJohn Marino }
3276e4b17023SJohn Marino 
3277e4b17023SJohn Marino /* At the end of inlining, we can lower EH_DISPATCH.  Return true when
3278e4b17023SJohn Marino    we have found some duplicate labels and removed some edges.  */
3279e4b17023SJohn Marino 
3280e4b17023SJohn Marino static bool
lower_eh_dispatch(basic_block src,gimple stmt)3281e4b17023SJohn Marino lower_eh_dispatch (basic_block src, gimple stmt)
3282e4b17023SJohn Marino {
3283e4b17023SJohn Marino   gimple_stmt_iterator gsi;
3284e4b17023SJohn Marino   int region_nr;
3285e4b17023SJohn Marino   eh_region r;
3286e4b17023SJohn Marino   tree filter, fn;
3287e4b17023SJohn Marino   gimple x;
3288e4b17023SJohn Marino   bool redirected = false;
3289e4b17023SJohn Marino 
3290e4b17023SJohn Marino   region_nr = gimple_eh_dispatch_region (stmt);
3291e4b17023SJohn Marino   r = get_eh_region_from_number (region_nr);
3292e4b17023SJohn Marino 
3293e4b17023SJohn Marino   gsi = gsi_last_bb (src);
3294e4b17023SJohn Marino 
3295e4b17023SJohn Marino   switch (r->type)
3296e4b17023SJohn Marino     {
3297e4b17023SJohn Marino     case ERT_TRY:
3298e4b17023SJohn Marino       {
3299e4b17023SJohn Marino 	VEC (tree, heap) *labels = NULL;
3300e4b17023SJohn Marino 	tree default_label = NULL;
3301e4b17023SJohn Marino 	eh_catch c;
3302e4b17023SJohn Marino 	edge_iterator ei;
3303e4b17023SJohn Marino 	edge e;
3304e4b17023SJohn Marino 	struct pointer_set_t *seen_values = pointer_set_create ();
3305e4b17023SJohn Marino 
3306e4b17023SJohn Marino 	/* Collect the labels for a switch.  Zero the post_landing_pad
3307e4b17023SJohn Marino 	   field becase we'll no longer have anything keeping these labels
3308e4b17023SJohn Marino 	   in existance and the optimizer will be free to merge these
3309e4b17023SJohn Marino 	   blocks at will.  */
3310e4b17023SJohn Marino 	for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
3311e4b17023SJohn Marino 	  {
3312e4b17023SJohn Marino 	    tree tp_node, flt_node, lab = c->label;
3313e4b17023SJohn Marino 	    bool have_label = false;
3314e4b17023SJohn Marino 
3315e4b17023SJohn Marino 	    c->label = NULL;
3316e4b17023SJohn Marino 	    tp_node = c->type_list;
3317e4b17023SJohn Marino 	    flt_node = c->filter_list;
3318e4b17023SJohn Marino 
3319e4b17023SJohn Marino 	    if (tp_node == NULL)
3320e4b17023SJohn Marino 	      {
3321e4b17023SJohn Marino 	        default_label = lab;
3322e4b17023SJohn Marino 		break;
3323e4b17023SJohn Marino 	      }
3324e4b17023SJohn Marino 	    do
3325e4b17023SJohn Marino 	      {
3326e4b17023SJohn Marino 		/* Filter out duplicate labels that arise when this handler
3327e4b17023SJohn Marino 		   is shadowed by an earlier one.  When no labels are
3328e4b17023SJohn Marino 		   attached to the handler anymore, we remove
3329e4b17023SJohn Marino 		   the corresponding edge and then we delete unreachable
3330e4b17023SJohn Marino 		   blocks at the end of this pass.  */
3331e4b17023SJohn Marino 		if (! pointer_set_contains (seen_values, TREE_VALUE (flt_node)))
3332e4b17023SJohn Marino 		  {
3333e4b17023SJohn Marino 		    tree t = build_case_label (TREE_VALUE (flt_node),
3334e4b17023SJohn Marino 					       NULL, lab);
3335e4b17023SJohn Marino 		    VEC_safe_push (tree, heap, labels, t);
3336e4b17023SJohn Marino 		    pointer_set_insert (seen_values, TREE_VALUE (flt_node));
3337e4b17023SJohn Marino 		    have_label = true;
3338e4b17023SJohn Marino 		  }
3339e4b17023SJohn Marino 
3340e4b17023SJohn Marino 		tp_node = TREE_CHAIN (tp_node);
3341e4b17023SJohn Marino 		flt_node = TREE_CHAIN (flt_node);
3342e4b17023SJohn Marino 	      }
3343e4b17023SJohn Marino 	    while (tp_node);
3344e4b17023SJohn Marino 	    if (! have_label)
3345e4b17023SJohn Marino 	      {
3346e4b17023SJohn Marino 	        remove_edge (find_edge (src, label_to_block (lab)));
3347e4b17023SJohn Marino 	        redirected = true;
3348e4b17023SJohn Marino 	      }
3349e4b17023SJohn Marino 	  }
3350e4b17023SJohn Marino 
3351e4b17023SJohn Marino 	/* Clean up the edge flags.  */
3352e4b17023SJohn Marino 	FOR_EACH_EDGE (e, ei, src->succs)
3353e4b17023SJohn Marino 	  {
3354e4b17023SJohn Marino 	    if (e->flags & EDGE_FALLTHRU)
3355e4b17023SJohn Marino 	      {
3356e4b17023SJohn Marino 		/* If there was no catch-all, use the fallthru edge.  */
3357e4b17023SJohn Marino 		if (default_label == NULL)
3358e4b17023SJohn Marino 		  default_label = gimple_block_label (e->dest);
3359e4b17023SJohn Marino 		e->flags &= ~EDGE_FALLTHRU;
3360e4b17023SJohn Marino 	      }
3361e4b17023SJohn Marino 	  }
3362e4b17023SJohn Marino 	gcc_assert (default_label != NULL);
3363e4b17023SJohn Marino 
3364e4b17023SJohn Marino 	/* Don't generate a switch if there's only a default case.
3365e4b17023SJohn Marino 	   This is common in the form of try { A; } catch (...) { B; }.  */
3366e4b17023SJohn Marino 	if (labels == NULL)
3367e4b17023SJohn Marino 	  {
3368e4b17023SJohn Marino 	    e = single_succ_edge (src);
3369e4b17023SJohn Marino 	    e->flags |= EDGE_FALLTHRU;
3370e4b17023SJohn Marino 	  }
3371e4b17023SJohn Marino 	else
3372e4b17023SJohn Marino 	  {
3373e4b17023SJohn Marino 	    fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3374e4b17023SJohn Marino 	    x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3375e4b17023SJohn Marino 							 region_nr));
3376e4b17023SJohn Marino 	    filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)), NULL);
3377e4b17023SJohn Marino 	    filter = make_ssa_name (filter, x);
3378e4b17023SJohn Marino 	    gimple_call_set_lhs (x, filter);
3379e4b17023SJohn Marino 	    gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3380e4b17023SJohn Marino 
3381e4b17023SJohn Marino 	    /* Turn the default label into a default case.  */
3382e4b17023SJohn Marino 	    default_label = build_case_label (NULL, NULL, default_label);
3383e4b17023SJohn Marino 	    sort_case_labels (labels);
3384e4b17023SJohn Marino 
3385e4b17023SJohn Marino 	    x = gimple_build_switch_vec (filter, default_label, labels);
3386e4b17023SJohn Marino 	    gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3387e4b17023SJohn Marino 
3388e4b17023SJohn Marino 	    VEC_free (tree, heap, labels);
3389e4b17023SJohn Marino 	  }
3390e4b17023SJohn Marino 	pointer_set_destroy (seen_values);
3391e4b17023SJohn Marino       }
3392e4b17023SJohn Marino       break;
3393e4b17023SJohn Marino 
3394e4b17023SJohn Marino     case ERT_ALLOWED_EXCEPTIONS:
3395e4b17023SJohn Marino       {
3396e4b17023SJohn Marino 	edge b_e = BRANCH_EDGE (src);
3397e4b17023SJohn Marino 	edge f_e = FALLTHRU_EDGE (src);
3398e4b17023SJohn Marino 
3399e4b17023SJohn Marino 	fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3400e4b17023SJohn Marino 	x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3401e4b17023SJohn Marino 						     region_nr));
3402e4b17023SJohn Marino 	filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)), NULL);
3403e4b17023SJohn Marino 	filter = make_ssa_name (filter, x);
3404e4b17023SJohn Marino 	gimple_call_set_lhs (x, filter);
3405e4b17023SJohn Marino 	gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3406e4b17023SJohn Marino 
3407e4b17023SJohn Marino 	r->u.allowed.label = NULL;
3408e4b17023SJohn Marino 	x = gimple_build_cond (EQ_EXPR, filter,
3409e4b17023SJohn Marino 			       build_int_cst (TREE_TYPE (filter),
3410e4b17023SJohn Marino 					      r->u.allowed.filter),
3411e4b17023SJohn Marino 			       NULL_TREE, NULL_TREE);
3412e4b17023SJohn Marino 	gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3413e4b17023SJohn Marino 
3414e4b17023SJohn Marino 	b_e->flags = b_e->flags | EDGE_TRUE_VALUE;
3415e4b17023SJohn Marino         f_e->flags = (f_e->flags & ~EDGE_FALLTHRU) | EDGE_FALSE_VALUE;
3416e4b17023SJohn Marino       }
3417e4b17023SJohn Marino       break;
3418e4b17023SJohn Marino 
3419e4b17023SJohn Marino     default:
3420e4b17023SJohn Marino       gcc_unreachable ();
3421e4b17023SJohn Marino     }
3422e4b17023SJohn Marino 
3423e4b17023SJohn Marino   /* Replace the EH_DISPATCH with the SWITCH or COND generated above.  */
3424e4b17023SJohn Marino   gsi_remove (&gsi, true);
3425e4b17023SJohn Marino   return redirected;
3426e4b17023SJohn Marino }
3427e4b17023SJohn Marino 
3428e4b17023SJohn Marino static unsigned
execute_lower_eh_dispatch(void)3429e4b17023SJohn Marino execute_lower_eh_dispatch (void)
3430e4b17023SJohn Marino {
3431e4b17023SJohn Marino   basic_block bb;
3432e4b17023SJohn Marino   int flags = 0;
3433e4b17023SJohn Marino   bool redirected = false;
3434e4b17023SJohn Marino 
3435e4b17023SJohn Marino   assign_filter_values ();
3436e4b17023SJohn Marino 
3437e4b17023SJohn Marino   FOR_EACH_BB (bb)
3438e4b17023SJohn Marino     {
3439e4b17023SJohn Marino       gimple last = last_stmt (bb);
3440e4b17023SJohn Marino       if (last == NULL)
3441e4b17023SJohn Marino 	continue;
3442e4b17023SJohn Marino       if (gimple_code (last) == GIMPLE_EH_DISPATCH)
3443e4b17023SJohn Marino 	{
3444e4b17023SJohn Marino 	  redirected |= lower_eh_dispatch (bb, last);
3445e4b17023SJohn Marino 	  flags |= TODO_update_ssa_only_virtuals;
3446e4b17023SJohn Marino 	}
3447e4b17023SJohn Marino       else if (gimple_code (last) == GIMPLE_RESX)
3448e4b17023SJohn Marino 	{
3449e4b17023SJohn Marino 	  if (stmt_can_throw_external (last))
3450e4b17023SJohn Marino 	    optimize_clobbers (bb);
3451e4b17023SJohn Marino 	  else
3452e4b17023SJohn Marino 	    flags |= sink_clobbers (bb);
3453e4b17023SJohn Marino 	}
3454e4b17023SJohn Marino     }
3455e4b17023SJohn Marino 
3456e4b17023SJohn Marino   if (redirected)
3457e4b17023SJohn Marino     delete_unreachable_blocks ();
3458e4b17023SJohn Marino   return flags;
3459e4b17023SJohn Marino }
3460e4b17023SJohn Marino 
3461e4b17023SJohn Marino static bool
gate_lower_eh_dispatch(void)3462e4b17023SJohn Marino gate_lower_eh_dispatch (void)
3463e4b17023SJohn Marino {
3464e4b17023SJohn Marino   return cfun->eh->region_tree != NULL;
3465e4b17023SJohn Marino }
3466e4b17023SJohn Marino 
3467e4b17023SJohn Marino struct gimple_opt_pass pass_lower_eh_dispatch =
3468e4b17023SJohn Marino {
3469e4b17023SJohn Marino  {
3470e4b17023SJohn Marino   GIMPLE_PASS,
3471e4b17023SJohn Marino   "ehdisp",				/* name */
3472e4b17023SJohn Marino   gate_lower_eh_dispatch,		/* gate */
3473e4b17023SJohn Marino   execute_lower_eh_dispatch,		/* execute */
3474e4b17023SJohn Marino   NULL,					/* sub */
3475e4b17023SJohn Marino   NULL,					/* next */
3476e4b17023SJohn Marino   0,					/* static_pass_number */
3477e4b17023SJohn Marino   TV_TREE_EH,				/* tv_id */
3478e4b17023SJohn Marino   PROP_gimple_lcf,			/* properties_required */
3479e4b17023SJohn Marino   0,					/* properties_provided */
3480e4b17023SJohn Marino   0,					/* properties_destroyed */
3481e4b17023SJohn Marino   0,					/* todo_flags_start */
3482e4b17023SJohn Marino   TODO_verify_flow	                /* todo_flags_finish */
3483e4b17023SJohn Marino  }
3484e4b17023SJohn Marino };
3485e4b17023SJohn Marino 
3486e4b17023SJohn Marino /* Walk statements, see what regions are really referenced and remove
3487e4b17023SJohn Marino    those that are unused.  */
3488e4b17023SJohn Marino 
3489e4b17023SJohn Marino static void
remove_unreachable_handlers(void)3490e4b17023SJohn Marino remove_unreachable_handlers (void)
3491e4b17023SJohn Marino {
3492e4b17023SJohn Marino   sbitmap r_reachable, lp_reachable;
3493e4b17023SJohn Marino   eh_region region;
3494e4b17023SJohn Marino   eh_landing_pad lp;
3495e4b17023SJohn Marino   basic_block bb;
3496e4b17023SJohn Marino   int lp_nr, r_nr;
3497e4b17023SJohn Marino 
3498e4b17023SJohn Marino   r_reachable = sbitmap_alloc (VEC_length (eh_region, cfun->eh->region_array));
3499e4b17023SJohn Marino   lp_reachable
3500e4b17023SJohn Marino     = sbitmap_alloc (VEC_length (eh_landing_pad, cfun->eh->lp_array));
3501e4b17023SJohn Marino   sbitmap_zero (r_reachable);
3502e4b17023SJohn Marino   sbitmap_zero (lp_reachable);
3503e4b17023SJohn Marino 
3504e4b17023SJohn Marino   FOR_EACH_BB (bb)
3505e4b17023SJohn Marino     {
3506e4b17023SJohn Marino       gimple_stmt_iterator gsi;
3507e4b17023SJohn Marino 
3508e4b17023SJohn Marino       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3509e4b17023SJohn Marino 	{
3510e4b17023SJohn Marino 	  gimple stmt = gsi_stmt (gsi);
3511e4b17023SJohn Marino 	  lp_nr = lookup_stmt_eh_lp (stmt);
3512e4b17023SJohn Marino 
3513e4b17023SJohn Marino 	  /* Negative LP numbers are MUST_NOT_THROW regions which
3514e4b17023SJohn Marino 	     are not considered BB enders.  */
3515e4b17023SJohn Marino 	  if (lp_nr < 0)
3516e4b17023SJohn Marino 	    SET_BIT (r_reachable, -lp_nr);
3517e4b17023SJohn Marino 
3518e4b17023SJohn Marino 	  /* Positive LP numbers are real landing pads, are are BB enders.  */
3519e4b17023SJohn Marino 	  else if (lp_nr > 0)
3520e4b17023SJohn Marino 	    {
3521e4b17023SJohn Marino 	      gcc_assert (gsi_one_before_end_p (gsi));
3522e4b17023SJohn Marino 	      region = get_eh_region_from_lp_number (lp_nr);
3523e4b17023SJohn Marino 	      SET_BIT (r_reachable, region->index);
3524e4b17023SJohn Marino 	      SET_BIT (lp_reachable, lp_nr);
3525e4b17023SJohn Marino 	    }
3526e4b17023SJohn Marino 
3527e4b17023SJohn Marino 	  /* Avoid removing regions referenced from RESX/EH_DISPATCH.  */
3528e4b17023SJohn Marino 	  switch (gimple_code (stmt))
3529e4b17023SJohn Marino 	    {
3530e4b17023SJohn Marino 	    case GIMPLE_RESX:
3531e4b17023SJohn Marino 	      SET_BIT (r_reachable, gimple_resx_region (stmt));
3532e4b17023SJohn Marino 	      break;
3533e4b17023SJohn Marino 	    case GIMPLE_EH_DISPATCH:
3534e4b17023SJohn Marino 	      SET_BIT (r_reachable, gimple_eh_dispatch_region (stmt));
3535e4b17023SJohn Marino 	      break;
3536e4b17023SJohn Marino 	    default:
3537e4b17023SJohn Marino 	      break;
3538e4b17023SJohn Marino 	    }
3539e4b17023SJohn Marino 	}
3540e4b17023SJohn Marino     }
3541e4b17023SJohn Marino 
3542e4b17023SJohn Marino   if (dump_file)
3543e4b17023SJohn Marino     {
3544e4b17023SJohn Marino       fprintf (dump_file, "Before removal of unreachable regions:\n");
3545e4b17023SJohn Marino       dump_eh_tree (dump_file, cfun);
3546e4b17023SJohn Marino       fprintf (dump_file, "Reachable regions: ");
3547e4b17023SJohn Marino       dump_sbitmap_file (dump_file, r_reachable);
3548e4b17023SJohn Marino       fprintf (dump_file, "Reachable landing pads: ");
3549e4b17023SJohn Marino       dump_sbitmap_file (dump_file, lp_reachable);
3550e4b17023SJohn Marino     }
3551e4b17023SJohn Marino 
3552e4b17023SJohn Marino   for (r_nr = 1;
3553e4b17023SJohn Marino        VEC_iterate (eh_region, cfun->eh->region_array, r_nr, region); ++r_nr)
3554e4b17023SJohn Marino     if (region && !TEST_BIT (r_reachable, r_nr))
3555e4b17023SJohn Marino       {
3556e4b17023SJohn Marino 	if (dump_file)
3557e4b17023SJohn Marino 	  fprintf (dump_file, "Removing unreachable region %d\n", r_nr);
3558e4b17023SJohn Marino 	remove_eh_handler (region);
3559e4b17023SJohn Marino       }
3560e4b17023SJohn Marino 
3561e4b17023SJohn Marino   for (lp_nr = 1;
3562e4b17023SJohn Marino        VEC_iterate (eh_landing_pad, cfun->eh->lp_array, lp_nr, lp); ++lp_nr)
3563e4b17023SJohn Marino     if (lp && !TEST_BIT (lp_reachable, lp_nr))
3564e4b17023SJohn Marino       {
3565e4b17023SJohn Marino 	if (dump_file)
3566e4b17023SJohn Marino 	  fprintf (dump_file, "Removing unreachable landing pad %d\n", lp_nr);
3567e4b17023SJohn Marino 	remove_eh_landing_pad (lp);
3568e4b17023SJohn Marino       }
3569e4b17023SJohn Marino 
3570e4b17023SJohn Marino   if (dump_file)
3571e4b17023SJohn Marino     {
3572e4b17023SJohn Marino       fprintf (dump_file, "\n\nAfter removal of unreachable regions:\n");
3573e4b17023SJohn Marino       dump_eh_tree (dump_file, cfun);
3574e4b17023SJohn Marino       fprintf (dump_file, "\n\n");
3575e4b17023SJohn Marino     }
3576e4b17023SJohn Marino 
3577e4b17023SJohn Marino   sbitmap_free (r_reachable);
3578e4b17023SJohn Marino   sbitmap_free (lp_reachable);
3579e4b17023SJohn Marino 
3580e4b17023SJohn Marino #ifdef ENABLE_CHECKING
3581e4b17023SJohn Marino   verify_eh_tree (cfun);
3582e4b17023SJohn Marino #endif
3583e4b17023SJohn Marino }
3584e4b17023SJohn Marino 
3585e4b17023SJohn Marino /* Remove unreachable handlers if any landing pads have been removed after
3586e4b17023SJohn Marino    last ehcleanup pass (due to gimple_purge_dead_eh_edges).  */
3587e4b17023SJohn Marino 
3588e4b17023SJohn Marino void
maybe_remove_unreachable_handlers(void)3589e4b17023SJohn Marino maybe_remove_unreachable_handlers (void)
3590e4b17023SJohn Marino {
3591e4b17023SJohn Marino   eh_landing_pad lp;
3592e4b17023SJohn Marino   int i;
3593e4b17023SJohn Marino 
3594e4b17023SJohn Marino   if (cfun->eh == NULL)
3595e4b17023SJohn Marino     return;
3596e4b17023SJohn Marino 
3597e4b17023SJohn Marino   for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
3598e4b17023SJohn Marino     if (lp && lp->post_landing_pad)
3599e4b17023SJohn Marino       {
3600e4b17023SJohn Marino 	if (label_to_block (lp->post_landing_pad) == NULL)
3601e4b17023SJohn Marino 	  {
3602e4b17023SJohn Marino 	    remove_unreachable_handlers ();
3603e4b17023SJohn Marino 	    return;
3604e4b17023SJohn Marino 	  }
3605e4b17023SJohn Marino       }
3606e4b17023SJohn Marino }
3607e4b17023SJohn Marino 
3608e4b17023SJohn Marino /* Remove regions that do not have landing pads.  This assumes
3609e4b17023SJohn Marino    that remove_unreachable_handlers has already been run, and
3610e4b17023SJohn Marino    that we've just manipulated the landing pads since then.  */
3611e4b17023SJohn Marino 
3612e4b17023SJohn Marino static void
remove_unreachable_handlers_no_lp(void)3613e4b17023SJohn Marino remove_unreachable_handlers_no_lp (void)
3614e4b17023SJohn Marino {
3615e4b17023SJohn Marino   eh_region r;
3616e4b17023SJohn Marino   int i;
3617e4b17023SJohn Marino   sbitmap r_reachable;
3618e4b17023SJohn Marino   basic_block bb;
3619e4b17023SJohn Marino 
3620e4b17023SJohn Marino   r_reachable = sbitmap_alloc (VEC_length (eh_region, cfun->eh->region_array));
3621e4b17023SJohn Marino   sbitmap_zero (r_reachable);
3622e4b17023SJohn Marino 
3623e4b17023SJohn Marino   FOR_EACH_BB (bb)
3624e4b17023SJohn Marino     {
3625e4b17023SJohn Marino       gimple stmt = last_stmt (bb);
3626e4b17023SJohn Marino       if (stmt)
3627e4b17023SJohn Marino 	/* Avoid removing regions referenced from RESX/EH_DISPATCH.  */
3628e4b17023SJohn Marino 	switch (gimple_code (stmt))
3629e4b17023SJohn Marino 	  {
3630e4b17023SJohn Marino 	  case GIMPLE_RESX:
3631e4b17023SJohn Marino 	    SET_BIT (r_reachable, gimple_resx_region (stmt));
3632e4b17023SJohn Marino 	    break;
3633e4b17023SJohn Marino 	  case GIMPLE_EH_DISPATCH:
3634e4b17023SJohn Marino 	    SET_BIT (r_reachable, gimple_eh_dispatch_region (stmt));
3635e4b17023SJohn Marino 	    break;
3636e4b17023SJohn Marino 	  default:
3637e4b17023SJohn Marino 	    break;
3638e4b17023SJohn Marino 	  }
3639e4b17023SJohn Marino     }
3640e4b17023SJohn Marino 
3641e4b17023SJohn Marino   for (i = 1; VEC_iterate (eh_region, cfun->eh->region_array, i, r); ++i)
3642e4b17023SJohn Marino     if (r && r->landing_pads == NULL && r->type != ERT_MUST_NOT_THROW
3643e4b17023SJohn Marino 	&& !TEST_BIT (r_reachable, i))
3644e4b17023SJohn Marino       {
3645e4b17023SJohn Marino 	if (dump_file)
3646e4b17023SJohn Marino 	  fprintf (dump_file, "Removing unreachable region %d\n", i);
3647e4b17023SJohn Marino 	remove_eh_handler (r);
3648e4b17023SJohn Marino       }
3649e4b17023SJohn Marino 
3650e4b17023SJohn Marino   sbitmap_free (r_reachable);
3651e4b17023SJohn Marino }
3652e4b17023SJohn Marino 
3653e4b17023SJohn Marino /* Undo critical edge splitting on an EH landing pad.  Earlier, we
3654e4b17023SJohn Marino    optimisticaly split all sorts of edges, including EH edges.  The
3655e4b17023SJohn Marino    optimization passes in between may not have needed them; if not,
3656e4b17023SJohn Marino    we should undo the split.
3657e4b17023SJohn Marino 
3658e4b17023SJohn Marino    Recognize this case by having one EH edge incoming to the BB and
3659e4b17023SJohn Marino    one normal edge outgoing; BB should be empty apart from the
3660e4b17023SJohn Marino    post_landing_pad label.
3661e4b17023SJohn Marino 
3662e4b17023SJohn Marino    Note that this is slightly different from the empty handler case
3663e4b17023SJohn Marino    handled by cleanup_empty_eh, in that the actual handler may yet
3664e4b17023SJohn Marino    have actual code but the landing pad has been separated from the
3665e4b17023SJohn Marino    handler.  As such, cleanup_empty_eh relies on this transformation
3666e4b17023SJohn Marino    having been done first.  */
3667e4b17023SJohn Marino 
3668e4b17023SJohn Marino static bool
unsplit_eh(eh_landing_pad lp)3669e4b17023SJohn Marino unsplit_eh (eh_landing_pad lp)
3670e4b17023SJohn Marino {
3671e4b17023SJohn Marino   basic_block bb = label_to_block (lp->post_landing_pad);
3672e4b17023SJohn Marino   gimple_stmt_iterator gsi;
3673e4b17023SJohn Marino   edge e_in, e_out;
3674e4b17023SJohn Marino 
3675e4b17023SJohn Marino   /* Quickly check the edge counts on BB for singularity.  */
3676e4b17023SJohn Marino   if (EDGE_COUNT (bb->preds) != 1 || EDGE_COUNT (bb->succs) != 1)
3677e4b17023SJohn Marino     return false;
3678e4b17023SJohn Marino   e_in = EDGE_PRED (bb, 0);
3679e4b17023SJohn Marino   e_out = EDGE_SUCC (bb, 0);
3680e4b17023SJohn Marino 
3681e4b17023SJohn Marino   /* Input edge must be EH and output edge must be normal.  */
3682e4b17023SJohn Marino   if ((e_in->flags & EDGE_EH) == 0 || (e_out->flags & EDGE_EH) != 0)
3683e4b17023SJohn Marino     return false;
3684e4b17023SJohn Marino 
3685e4b17023SJohn Marino   /* The block must be empty except for the labels and debug insns.  */
3686e4b17023SJohn Marino   gsi = gsi_after_labels (bb);
3687e4b17023SJohn Marino   if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
3688e4b17023SJohn Marino     gsi_next_nondebug (&gsi);
3689e4b17023SJohn Marino   if (!gsi_end_p (gsi))
3690e4b17023SJohn Marino     return false;
3691e4b17023SJohn Marino 
3692e4b17023SJohn Marino   /* The destination block must not already have a landing pad
3693e4b17023SJohn Marino      for a different region.  */
3694e4b17023SJohn Marino   for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3695e4b17023SJohn Marino     {
3696e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
3697e4b17023SJohn Marino       tree lab;
3698e4b17023SJohn Marino       int lp_nr;
3699e4b17023SJohn Marino 
3700e4b17023SJohn Marino       if (gimple_code (stmt) != GIMPLE_LABEL)
3701e4b17023SJohn Marino 	break;
3702e4b17023SJohn Marino       lab = gimple_label_label (stmt);
3703e4b17023SJohn Marino       lp_nr = EH_LANDING_PAD_NR (lab);
3704e4b17023SJohn Marino       if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
3705e4b17023SJohn Marino 	return false;
3706e4b17023SJohn Marino     }
3707e4b17023SJohn Marino 
3708e4b17023SJohn Marino   /* The new destination block must not already be a destination of
3709e4b17023SJohn Marino      the source block, lest we merge fallthru and eh edges and get
3710e4b17023SJohn Marino      all sorts of confused.  */
3711e4b17023SJohn Marino   if (find_edge (e_in->src, e_out->dest))
3712e4b17023SJohn Marino     return false;
3713e4b17023SJohn Marino 
3714e4b17023SJohn Marino   /* ??? We can get degenerate phis due to cfg cleanups.  I would have
3715e4b17023SJohn Marino      thought this should have been cleaned up by a phicprop pass, but
3716e4b17023SJohn Marino      that doesn't appear to handle virtuals.  Propagate by hand.  */
3717e4b17023SJohn Marino   if (!gimple_seq_empty_p (phi_nodes (bb)))
3718e4b17023SJohn Marino     {
3719e4b17023SJohn Marino       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
3720e4b17023SJohn Marino 	{
3721e4b17023SJohn Marino 	  gimple use_stmt, phi = gsi_stmt (gsi);
3722e4b17023SJohn Marino 	  tree lhs = gimple_phi_result (phi);
3723e4b17023SJohn Marino 	  tree rhs = gimple_phi_arg_def (phi, 0);
3724e4b17023SJohn Marino 	  use_operand_p use_p;
3725e4b17023SJohn Marino 	  imm_use_iterator iter;
3726e4b17023SJohn Marino 
3727e4b17023SJohn Marino 	  FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3728e4b17023SJohn Marino 	    {
3729e4b17023SJohn Marino 	      FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3730e4b17023SJohn Marino 		SET_USE (use_p, rhs);
3731e4b17023SJohn Marino 	    }
3732e4b17023SJohn Marino 
3733e4b17023SJohn Marino 	  if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3734e4b17023SJohn Marino 	    SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
3735e4b17023SJohn Marino 
3736e4b17023SJohn Marino 	  remove_phi_node (&gsi, true);
3737e4b17023SJohn Marino 	}
3738e4b17023SJohn Marino     }
3739e4b17023SJohn Marino 
3740e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
3741e4b17023SJohn Marino     fprintf (dump_file, "Unsplit EH landing pad %d to block %i.\n",
3742e4b17023SJohn Marino 	     lp->index, e_out->dest->index);
3743e4b17023SJohn Marino 
3744e4b17023SJohn Marino   /* Redirect the edge.  Since redirect_eh_edge_1 expects to be moving
3745e4b17023SJohn Marino      a successor edge, humor it.  But do the real CFG change with the
3746e4b17023SJohn Marino      predecessor of E_OUT in order to preserve the ordering of arguments
3747e4b17023SJohn Marino      to the PHI nodes in E_OUT->DEST.  */
3748e4b17023SJohn Marino   redirect_eh_edge_1 (e_in, e_out->dest, false);
3749e4b17023SJohn Marino   redirect_edge_pred (e_out, e_in->src);
3750e4b17023SJohn Marino   e_out->flags = e_in->flags;
3751e4b17023SJohn Marino   e_out->probability = e_in->probability;
3752e4b17023SJohn Marino   e_out->count = e_in->count;
3753e4b17023SJohn Marino   remove_edge (e_in);
3754e4b17023SJohn Marino 
3755e4b17023SJohn Marino   return true;
3756e4b17023SJohn Marino }
3757e4b17023SJohn Marino 
3758e4b17023SJohn Marino /* Examine each landing pad block and see if it matches unsplit_eh.  */
3759e4b17023SJohn Marino 
3760e4b17023SJohn Marino static bool
unsplit_all_eh(void)3761e4b17023SJohn Marino unsplit_all_eh (void)
3762e4b17023SJohn Marino {
3763e4b17023SJohn Marino   bool changed = false;
3764e4b17023SJohn Marino   eh_landing_pad lp;
3765e4b17023SJohn Marino   int i;
3766e4b17023SJohn Marino 
3767e4b17023SJohn Marino   for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
3768e4b17023SJohn Marino     if (lp)
3769e4b17023SJohn Marino       changed |= unsplit_eh (lp);
3770e4b17023SJohn Marino 
3771e4b17023SJohn Marino   return changed;
3772e4b17023SJohn Marino }
3773e4b17023SJohn Marino 
3774e4b17023SJohn Marino /* A subroutine of cleanup_empty_eh.  Redirect all EH edges incoming
3775e4b17023SJohn Marino    to OLD_BB to NEW_BB; return true on success, false on failure.
3776e4b17023SJohn Marino 
3777e4b17023SJohn Marino    OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
3778e4b17023SJohn Marino    PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
3779e4b17023SJohn Marino    Virtual PHIs may be deleted and marked for renaming.  */
3780e4b17023SJohn Marino 
3781e4b17023SJohn Marino static bool
cleanup_empty_eh_merge_phis(basic_block new_bb,basic_block old_bb,edge old_bb_out,bool change_region)3782e4b17023SJohn Marino cleanup_empty_eh_merge_phis (basic_block new_bb, basic_block old_bb,
3783e4b17023SJohn Marino 			     edge old_bb_out, bool change_region)
3784e4b17023SJohn Marino {
3785e4b17023SJohn Marino   gimple_stmt_iterator ngsi, ogsi;
3786e4b17023SJohn Marino   edge_iterator ei;
3787e4b17023SJohn Marino   edge e;
3788e4b17023SJohn Marino   bitmap rename_virts;
3789e4b17023SJohn Marino   bitmap ophi_handled;
3790e4b17023SJohn Marino 
3791e4b17023SJohn Marino   /* The destination block must not be a regular successor for any
3792e4b17023SJohn Marino      of the preds of the landing pad.  Thus, avoid turning
3793e4b17023SJohn Marino         <..>
3794e4b17023SJohn Marino 	 |  \ EH
3795e4b17023SJohn Marino 	 |  <..>
3796e4b17023SJohn Marino 	 |  /
3797e4b17023SJohn Marino 	<..>
3798e4b17023SJohn Marino      into
3799e4b17023SJohn Marino         <..>
3800e4b17023SJohn Marino 	|  | EH
3801e4b17023SJohn Marino 	<..>
3802e4b17023SJohn Marino      which CFG verification would choke on.  See PR45172 and PR51089.  */
3803e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, old_bb->preds)
3804e4b17023SJohn Marino     if (find_edge (e->src, new_bb))
3805e4b17023SJohn Marino       return false;
3806e4b17023SJohn Marino 
3807e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, old_bb->preds)
3808e4b17023SJohn Marino     redirect_edge_var_map_clear (e);
3809e4b17023SJohn Marino 
3810e4b17023SJohn Marino   ophi_handled = BITMAP_ALLOC (NULL);
3811e4b17023SJohn Marino   rename_virts = BITMAP_ALLOC (NULL);
3812e4b17023SJohn Marino 
3813e4b17023SJohn Marino   /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
3814e4b17023SJohn Marino      for the edges we're going to move.  */
3815e4b17023SJohn Marino   for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); gsi_next (&ngsi))
3816e4b17023SJohn Marino     {
3817e4b17023SJohn Marino       gimple ophi, nphi = gsi_stmt (ngsi);
3818e4b17023SJohn Marino       tree nresult, nop;
3819e4b17023SJohn Marino 
3820e4b17023SJohn Marino       nresult = gimple_phi_result (nphi);
3821e4b17023SJohn Marino       nop = gimple_phi_arg_def (nphi, old_bb_out->dest_idx);
3822e4b17023SJohn Marino 
3823e4b17023SJohn Marino       /* Find the corresponding PHI in OLD_BB so we can forward-propagate
3824e4b17023SJohn Marino 	 the source ssa_name.  */
3825e4b17023SJohn Marino       ophi = NULL;
3826e4b17023SJohn Marino       for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
3827e4b17023SJohn Marino 	{
3828e4b17023SJohn Marino 	  ophi = gsi_stmt (ogsi);
3829e4b17023SJohn Marino 	  if (gimple_phi_result (ophi) == nop)
3830e4b17023SJohn Marino 	    break;
3831e4b17023SJohn Marino 	  ophi = NULL;
3832e4b17023SJohn Marino 	}
3833e4b17023SJohn Marino 
3834e4b17023SJohn Marino       /* If we did find the corresponding PHI, copy those inputs.  */
3835e4b17023SJohn Marino       if (ophi)
3836e4b17023SJohn Marino 	{
3837e4b17023SJohn Marino 	  /* If NOP is used somewhere else beyond phis in new_bb, give up.  */
3838e4b17023SJohn Marino 	  if (!has_single_use (nop))
3839e4b17023SJohn Marino 	    {
3840e4b17023SJohn Marino 	      imm_use_iterator imm_iter;
3841e4b17023SJohn Marino 	      use_operand_p use_p;
3842e4b17023SJohn Marino 
3843e4b17023SJohn Marino 	      FOR_EACH_IMM_USE_FAST (use_p, imm_iter, nop)
3844e4b17023SJohn Marino 		{
3845e4b17023SJohn Marino 		  if (!gimple_debug_bind_p (USE_STMT (use_p))
3846e4b17023SJohn Marino 		      && (gimple_code (USE_STMT (use_p)) != GIMPLE_PHI
3847e4b17023SJohn Marino 			  || gimple_bb (USE_STMT (use_p)) != new_bb))
3848e4b17023SJohn Marino 		    goto fail;
3849e4b17023SJohn Marino 		}
3850e4b17023SJohn Marino 	    }
3851e4b17023SJohn Marino 	  bitmap_set_bit (ophi_handled, SSA_NAME_VERSION (nop));
3852e4b17023SJohn Marino 	  FOR_EACH_EDGE (e, ei, old_bb->preds)
3853e4b17023SJohn Marino 	    {
3854e4b17023SJohn Marino 	      location_t oloc;
3855e4b17023SJohn Marino 	      tree oop;
3856e4b17023SJohn Marino 
3857e4b17023SJohn Marino 	      if ((e->flags & EDGE_EH) == 0)
3858e4b17023SJohn Marino 		continue;
3859e4b17023SJohn Marino 	      oop = gimple_phi_arg_def (ophi, e->dest_idx);
3860e4b17023SJohn Marino 	      oloc = gimple_phi_arg_location (ophi, e->dest_idx);
3861e4b17023SJohn Marino 	      redirect_edge_var_map_add (e, nresult, oop, oloc);
3862e4b17023SJohn Marino 	    }
3863e4b17023SJohn Marino 	}
3864e4b17023SJohn Marino       /* If we didn't find the PHI, but it's a VOP, remember to rename
3865e4b17023SJohn Marino 	 it later, assuming all other tests succeed.  */
3866e4b17023SJohn Marino       else if (!is_gimple_reg (nresult))
3867e4b17023SJohn Marino 	bitmap_set_bit (rename_virts, SSA_NAME_VERSION (nresult));
3868e4b17023SJohn Marino       /* If we didn't find the PHI, and it's a real variable, we know
3869e4b17023SJohn Marino 	 from the fact that OLD_BB is tree_empty_eh_handler_p that the
3870e4b17023SJohn Marino 	 variable is unchanged from input to the block and we can simply
3871e4b17023SJohn Marino 	 re-use the input to NEW_BB from the OLD_BB_OUT edge.  */
3872e4b17023SJohn Marino       else
3873e4b17023SJohn Marino 	{
3874e4b17023SJohn Marino 	  location_t nloc
3875e4b17023SJohn Marino 	    = gimple_phi_arg_location (nphi, old_bb_out->dest_idx);
3876e4b17023SJohn Marino 	  FOR_EACH_EDGE (e, ei, old_bb->preds)
3877e4b17023SJohn Marino 	    redirect_edge_var_map_add (e, nresult, nop, nloc);
3878e4b17023SJohn Marino 	}
3879e4b17023SJohn Marino     }
3880e4b17023SJohn Marino 
3881e4b17023SJohn Marino   /* Second, verify that all PHIs from OLD_BB have been handled.  If not,
3882e4b17023SJohn Marino      we don't know what values from the other edges into NEW_BB to use.  */
3883e4b17023SJohn Marino   for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
3884e4b17023SJohn Marino     {
3885e4b17023SJohn Marino       gimple ophi = gsi_stmt (ogsi);
3886e4b17023SJohn Marino       tree oresult = gimple_phi_result (ophi);
3887e4b17023SJohn Marino       if (!bitmap_bit_p (ophi_handled, SSA_NAME_VERSION (oresult)))
3888e4b17023SJohn Marino 	goto fail;
3889e4b17023SJohn Marino     }
3890e4b17023SJohn Marino 
3891e4b17023SJohn Marino   /* At this point we know that the merge will succeed.  Remove the PHI
3892e4b17023SJohn Marino      nodes for the virtuals that we want to rename.  */
3893e4b17023SJohn Marino   if (!bitmap_empty_p (rename_virts))
3894e4b17023SJohn Marino     {
3895e4b17023SJohn Marino       for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); )
3896e4b17023SJohn Marino 	{
3897e4b17023SJohn Marino 	  gimple nphi = gsi_stmt (ngsi);
3898e4b17023SJohn Marino 	  tree nresult = gimple_phi_result (nphi);
3899e4b17023SJohn Marino 	  if (bitmap_bit_p (rename_virts, SSA_NAME_VERSION (nresult)))
3900e4b17023SJohn Marino 	    {
3901e4b17023SJohn Marino 	      mark_virtual_phi_result_for_renaming (nphi);
3902e4b17023SJohn Marino 	      remove_phi_node (&ngsi, true);
3903e4b17023SJohn Marino 	    }
3904e4b17023SJohn Marino 	  else
3905e4b17023SJohn Marino 	    gsi_next (&ngsi);
3906e4b17023SJohn Marino 	}
3907e4b17023SJohn Marino     }
3908e4b17023SJohn Marino 
3909e4b17023SJohn Marino   /* Finally, move the edges and update the PHIs.  */
3910e4b17023SJohn Marino   for (ei = ei_start (old_bb->preds); (e = ei_safe_edge (ei)); )
3911e4b17023SJohn Marino     if (e->flags & EDGE_EH)
3912e4b17023SJohn Marino       {
3913e4b17023SJohn Marino 	redirect_eh_edge_1 (e, new_bb, change_region);
3914e4b17023SJohn Marino 	redirect_edge_succ (e, new_bb);
3915e4b17023SJohn Marino 	flush_pending_stmts (e);
3916e4b17023SJohn Marino       }
3917e4b17023SJohn Marino     else
3918e4b17023SJohn Marino       ei_next (&ei);
3919e4b17023SJohn Marino 
3920e4b17023SJohn Marino   BITMAP_FREE (ophi_handled);
3921e4b17023SJohn Marino   BITMAP_FREE (rename_virts);
3922e4b17023SJohn Marino   return true;
3923e4b17023SJohn Marino 
3924e4b17023SJohn Marino  fail:
3925e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, old_bb->preds)
3926e4b17023SJohn Marino     redirect_edge_var_map_clear (e);
3927e4b17023SJohn Marino   BITMAP_FREE (ophi_handled);
3928e4b17023SJohn Marino   BITMAP_FREE (rename_virts);
3929e4b17023SJohn Marino   return false;
3930e4b17023SJohn Marino }
3931e4b17023SJohn Marino 
3932e4b17023SJohn Marino /* A subroutine of cleanup_empty_eh.  Move a landing pad LP from its
3933e4b17023SJohn Marino    old region to NEW_REGION at BB.  */
3934e4b17023SJohn Marino 
3935e4b17023SJohn Marino static void
cleanup_empty_eh_move_lp(basic_block bb,edge e_out,eh_landing_pad lp,eh_region new_region)3936e4b17023SJohn Marino cleanup_empty_eh_move_lp (basic_block bb, edge e_out,
3937e4b17023SJohn Marino 			  eh_landing_pad lp, eh_region new_region)
3938e4b17023SJohn Marino {
3939e4b17023SJohn Marino   gimple_stmt_iterator gsi;
3940e4b17023SJohn Marino   eh_landing_pad *pp;
3941e4b17023SJohn Marino 
3942e4b17023SJohn Marino   for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
3943e4b17023SJohn Marino     continue;
3944e4b17023SJohn Marino   *pp = lp->next_lp;
3945e4b17023SJohn Marino 
3946e4b17023SJohn Marino   lp->region = new_region;
3947e4b17023SJohn Marino   lp->next_lp = new_region->landing_pads;
3948e4b17023SJohn Marino   new_region->landing_pads = lp;
3949e4b17023SJohn Marino 
3950e4b17023SJohn Marino   /* Delete the RESX that was matched within the empty handler block.  */
3951e4b17023SJohn Marino   gsi = gsi_last_bb (bb);
3952e4b17023SJohn Marino   mark_virtual_ops_for_renaming (gsi_stmt (gsi));
3953e4b17023SJohn Marino   gsi_remove (&gsi, true);
3954e4b17023SJohn Marino 
3955e4b17023SJohn Marino   /* Clean up E_OUT for the fallthru.  */
3956e4b17023SJohn Marino   e_out->flags = (e_out->flags & ~EDGE_EH) | EDGE_FALLTHRU;
3957e4b17023SJohn Marino   e_out->probability = REG_BR_PROB_BASE;
3958e4b17023SJohn Marino }
3959e4b17023SJohn Marino 
3960e4b17023SJohn Marino /* A subroutine of cleanup_empty_eh.  Handle more complex cases of
3961e4b17023SJohn Marino    unsplitting than unsplit_eh was prepared to handle, e.g. when
3962e4b17023SJohn Marino    multiple incoming edges and phis are involved.  */
3963e4b17023SJohn Marino 
3964e4b17023SJohn Marino static bool
cleanup_empty_eh_unsplit(basic_block bb,edge e_out,eh_landing_pad lp)3965e4b17023SJohn Marino cleanup_empty_eh_unsplit (basic_block bb, edge e_out, eh_landing_pad lp)
3966e4b17023SJohn Marino {
3967e4b17023SJohn Marino   gimple_stmt_iterator gsi;
3968e4b17023SJohn Marino   tree lab;
3969e4b17023SJohn Marino 
3970e4b17023SJohn Marino   /* We really ought not have totally lost everything following
3971e4b17023SJohn Marino      a landing pad label.  Given that BB is empty, there had better
3972e4b17023SJohn Marino      be a successor.  */
3973e4b17023SJohn Marino   gcc_assert (e_out != NULL);
3974e4b17023SJohn Marino 
3975e4b17023SJohn Marino   /* The destination block must not already have a landing pad
3976e4b17023SJohn Marino      for a different region.  */
3977e4b17023SJohn Marino   lab = NULL;
3978e4b17023SJohn Marino   for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3979e4b17023SJohn Marino     {
3980e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
3981e4b17023SJohn Marino       int lp_nr;
3982e4b17023SJohn Marino 
3983e4b17023SJohn Marino       if (gimple_code (stmt) != GIMPLE_LABEL)
3984e4b17023SJohn Marino 	break;
3985e4b17023SJohn Marino       lab = gimple_label_label (stmt);
3986e4b17023SJohn Marino       lp_nr = EH_LANDING_PAD_NR (lab);
3987e4b17023SJohn Marino       if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
3988e4b17023SJohn Marino 	return false;
3989e4b17023SJohn Marino     }
3990e4b17023SJohn Marino 
3991e4b17023SJohn Marino   /* Attempt to move the PHIs into the successor block.  */
3992e4b17023SJohn Marino   if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, false))
3993e4b17023SJohn Marino     {
3994e4b17023SJohn Marino       if (dump_file && (dump_flags & TDF_DETAILS))
3995e4b17023SJohn Marino 	fprintf (dump_file,
3996e4b17023SJohn Marino 		 "Unsplit EH landing pad %d to block %i "
3997e4b17023SJohn Marino 		 "(via cleanup_empty_eh).\n",
3998e4b17023SJohn Marino 		 lp->index, e_out->dest->index);
3999e4b17023SJohn Marino       return true;
4000e4b17023SJohn Marino     }
4001e4b17023SJohn Marino 
4002e4b17023SJohn Marino   return false;
4003e4b17023SJohn Marino }
4004e4b17023SJohn Marino 
4005e4b17023SJohn Marino /* Return true if edge E_FIRST is part of an empty infinite loop
4006e4b17023SJohn Marino    or leads to such a loop through a series of single successor
4007e4b17023SJohn Marino    empty bbs.  */
4008e4b17023SJohn Marino 
4009e4b17023SJohn Marino static bool
infinite_empty_loop_p(edge e_first)4010e4b17023SJohn Marino infinite_empty_loop_p (edge e_first)
4011e4b17023SJohn Marino {
4012e4b17023SJohn Marino   bool inf_loop = false;
4013e4b17023SJohn Marino   edge e;
4014e4b17023SJohn Marino 
4015e4b17023SJohn Marino   if (e_first->dest == e_first->src)
4016e4b17023SJohn Marino     return true;
4017e4b17023SJohn Marino 
4018e4b17023SJohn Marino   e_first->src->aux = (void *) 1;
4019e4b17023SJohn Marino   for (e = e_first; single_succ_p (e->dest); e = single_succ_edge (e->dest))
4020e4b17023SJohn Marino     {
4021e4b17023SJohn Marino       gimple_stmt_iterator gsi;
4022e4b17023SJohn Marino       if (e->dest->aux)
4023e4b17023SJohn Marino 	{
4024e4b17023SJohn Marino 	  inf_loop = true;
4025e4b17023SJohn Marino 	  break;
4026e4b17023SJohn Marino 	}
4027e4b17023SJohn Marino       e->dest->aux = (void *) 1;
4028e4b17023SJohn Marino       gsi = gsi_after_labels (e->dest);
4029e4b17023SJohn Marino       if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4030e4b17023SJohn Marino 	gsi_next_nondebug (&gsi);
4031e4b17023SJohn Marino       if (!gsi_end_p (gsi))
4032e4b17023SJohn Marino 	break;
4033e4b17023SJohn Marino     }
4034e4b17023SJohn Marino   e_first->src->aux = NULL;
4035e4b17023SJohn Marino   for (e = e_first; e->dest->aux; e = single_succ_edge (e->dest))
4036e4b17023SJohn Marino     e->dest->aux = NULL;
4037e4b17023SJohn Marino 
4038e4b17023SJohn Marino   return inf_loop;
4039e4b17023SJohn Marino }
4040e4b17023SJohn Marino 
4041e4b17023SJohn Marino /* Examine the block associated with LP to determine if it's an empty
4042e4b17023SJohn Marino    handler for its EH region.  If so, attempt to redirect EH edges to
4043e4b17023SJohn Marino    an outer region.  Return true the CFG was updated in any way.  This
4044e4b17023SJohn Marino    is similar to jump forwarding, just across EH edges.  */
4045e4b17023SJohn Marino 
4046e4b17023SJohn Marino static bool
cleanup_empty_eh(eh_landing_pad lp)4047e4b17023SJohn Marino cleanup_empty_eh (eh_landing_pad lp)
4048e4b17023SJohn Marino {
4049e4b17023SJohn Marino   basic_block bb = label_to_block (lp->post_landing_pad);
4050e4b17023SJohn Marino   gimple_stmt_iterator gsi;
4051e4b17023SJohn Marino   gimple resx;
4052e4b17023SJohn Marino   eh_region new_region;
4053e4b17023SJohn Marino   edge_iterator ei;
4054e4b17023SJohn Marino   edge e, e_out;
4055e4b17023SJohn Marino   bool has_non_eh_pred;
4056e4b17023SJohn Marino   bool ret = false;
4057e4b17023SJohn Marino   int new_lp_nr;
4058e4b17023SJohn Marino 
4059e4b17023SJohn Marino   /* There can be zero or one edges out of BB.  This is the quickest test.  */
4060e4b17023SJohn Marino   switch (EDGE_COUNT (bb->succs))
4061e4b17023SJohn Marino     {
4062e4b17023SJohn Marino     case 0:
4063e4b17023SJohn Marino       e_out = NULL;
4064e4b17023SJohn Marino       break;
4065e4b17023SJohn Marino     case 1:
4066e4b17023SJohn Marino       e_out = EDGE_SUCC (bb, 0);
4067e4b17023SJohn Marino       break;
4068e4b17023SJohn Marino     default:
4069e4b17023SJohn Marino       return false;
4070e4b17023SJohn Marino     }
4071e4b17023SJohn Marino 
4072e4b17023SJohn Marino   resx = last_stmt (bb);
4073e4b17023SJohn Marino   if (resx && is_gimple_resx (resx))
4074e4b17023SJohn Marino     {
4075e4b17023SJohn Marino       if (stmt_can_throw_external (resx))
4076e4b17023SJohn Marino 	optimize_clobbers (bb);
4077e4b17023SJohn Marino       else if (sink_clobbers (bb))
4078e4b17023SJohn Marino 	ret = true;
4079e4b17023SJohn Marino     }
4080e4b17023SJohn Marino 
4081e4b17023SJohn Marino   gsi = gsi_after_labels (bb);
4082e4b17023SJohn Marino 
4083e4b17023SJohn Marino   /* Make sure to skip debug statements.  */
4084e4b17023SJohn Marino   if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4085e4b17023SJohn Marino     gsi_next_nondebug (&gsi);
4086e4b17023SJohn Marino 
4087e4b17023SJohn Marino   /* If the block is totally empty, look for more unsplitting cases.  */
4088e4b17023SJohn Marino   if (gsi_end_p (gsi))
4089e4b17023SJohn Marino     {
4090e4b17023SJohn Marino       /* For the degenerate case of an infinite loop bail out.  */
4091e4b17023SJohn Marino       if (infinite_empty_loop_p (e_out))
4092e4b17023SJohn Marino 	return ret;
4093e4b17023SJohn Marino 
4094e4b17023SJohn Marino       return ret | cleanup_empty_eh_unsplit (bb, e_out, lp);
4095e4b17023SJohn Marino     }
4096e4b17023SJohn Marino 
4097e4b17023SJohn Marino   /* The block should consist only of a single RESX statement, modulo a
4098e4b17023SJohn Marino      preceding call to __builtin_stack_restore if there is no outgoing
4099e4b17023SJohn Marino      edge, since the call can be eliminated in this case.  */
4100e4b17023SJohn Marino   resx = gsi_stmt (gsi);
4101e4b17023SJohn Marino   if (!e_out && gimple_call_builtin_p (resx, BUILT_IN_STACK_RESTORE))
4102e4b17023SJohn Marino     {
4103e4b17023SJohn Marino       gsi_next (&gsi);
4104e4b17023SJohn Marino       resx = gsi_stmt (gsi);
4105e4b17023SJohn Marino     }
4106e4b17023SJohn Marino   if (!is_gimple_resx (resx))
4107e4b17023SJohn Marino     return ret;
4108e4b17023SJohn Marino   gcc_assert (gsi_one_before_end_p (gsi));
4109e4b17023SJohn Marino 
4110e4b17023SJohn Marino   /* Determine if there are non-EH edges, or resx edges into the handler.  */
4111e4b17023SJohn Marino   has_non_eh_pred = false;
4112e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->preds)
4113e4b17023SJohn Marino     if (!(e->flags & EDGE_EH))
4114e4b17023SJohn Marino       has_non_eh_pred = true;
4115e4b17023SJohn Marino 
4116e4b17023SJohn Marino   /* Find the handler that's outer of the empty handler by looking at
4117e4b17023SJohn Marino      where the RESX instruction was vectored.  */
4118e4b17023SJohn Marino   new_lp_nr = lookup_stmt_eh_lp (resx);
4119e4b17023SJohn Marino   new_region = get_eh_region_from_lp_number (new_lp_nr);
4120e4b17023SJohn Marino 
4121e4b17023SJohn Marino   /* If there's no destination region within the current function,
4122e4b17023SJohn Marino      redirection is trivial via removing the throwing statements from
4123e4b17023SJohn Marino      the EH region, removing the EH edges, and allowing the block
4124e4b17023SJohn Marino      to go unreachable.  */
4125e4b17023SJohn Marino   if (new_region == NULL)
4126e4b17023SJohn Marino     {
4127e4b17023SJohn Marino       gcc_assert (e_out == NULL);
4128e4b17023SJohn Marino       for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4129e4b17023SJohn Marino 	if (e->flags & EDGE_EH)
4130e4b17023SJohn Marino 	  {
4131e4b17023SJohn Marino 	    gimple stmt = last_stmt (e->src);
4132e4b17023SJohn Marino 	    remove_stmt_from_eh_lp (stmt);
4133e4b17023SJohn Marino 	    remove_edge (e);
4134e4b17023SJohn Marino 	  }
4135e4b17023SJohn Marino 	else
4136e4b17023SJohn Marino 	  ei_next (&ei);
4137e4b17023SJohn Marino       goto succeed;
4138e4b17023SJohn Marino     }
4139e4b17023SJohn Marino 
4140e4b17023SJohn Marino   /* If the destination region is a MUST_NOT_THROW, allow the runtime
4141e4b17023SJohn Marino      to handle the abort and allow the blocks to go unreachable.  */
4142e4b17023SJohn Marino   if (new_region->type == ERT_MUST_NOT_THROW)
4143e4b17023SJohn Marino     {
4144e4b17023SJohn Marino       for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4145e4b17023SJohn Marino 	if (e->flags & EDGE_EH)
4146e4b17023SJohn Marino 	  {
4147e4b17023SJohn Marino 	    gimple stmt = last_stmt (e->src);
4148e4b17023SJohn Marino 	    remove_stmt_from_eh_lp (stmt);
4149e4b17023SJohn Marino 	    add_stmt_to_eh_lp (stmt, new_lp_nr);
4150e4b17023SJohn Marino 	    remove_edge (e);
4151e4b17023SJohn Marino 	  }
4152e4b17023SJohn Marino 	else
4153e4b17023SJohn Marino 	  ei_next (&ei);
4154e4b17023SJohn Marino       goto succeed;
4155e4b17023SJohn Marino     }
4156e4b17023SJohn Marino 
4157e4b17023SJohn Marino   /* Try to redirect the EH edges and merge the PHIs into the destination
4158e4b17023SJohn Marino      landing pad block.  If the merge succeeds, we'll already have redirected
4159e4b17023SJohn Marino      all the EH edges.  The handler itself will go unreachable if there were
4160e4b17023SJohn Marino      no normal edges.  */
4161e4b17023SJohn Marino   if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, true))
4162e4b17023SJohn Marino     goto succeed;
4163e4b17023SJohn Marino 
4164e4b17023SJohn Marino   /* Finally, if all input edges are EH edges, then we can (potentially)
4165e4b17023SJohn Marino      reduce the number of transfers from the runtime by moving the landing
4166e4b17023SJohn Marino      pad from the original region to the new region.  This is a win when
4167e4b17023SJohn Marino      we remove the last CLEANUP region along a particular exception
4168e4b17023SJohn Marino      propagation path.  Since nothing changes except for the region with
4169e4b17023SJohn Marino      which the landing pad is associated, the PHI nodes do not need to be
4170e4b17023SJohn Marino      adjusted at all.  */
4171e4b17023SJohn Marino   if (!has_non_eh_pred)
4172e4b17023SJohn Marino     {
4173e4b17023SJohn Marino       cleanup_empty_eh_move_lp (bb, e_out, lp, new_region);
4174e4b17023SJohn Marino       if (dump_file && (dump_flags & TDF_DETAILS))
4175e4b17023SJohn Marino 	fprintf (dump_file, "Empty EH handler %i moved to EH region %i.\n",
4176e4b17023SJohn Marino 		 lp->index, new_region->index);
4177e4b17023SJohn Marino 
4178e4b17023SJohn Marino       /* ??? The CFG didn't change, but we may have rendered the
4179e4b17023SJohn Marino 	 old EH region unreachable.  Trigger a cleanup there.  */
4180e4b17023SJohn Marino       return true;
4181e4b17023SJohn Marino     }
4182e4b17023SJohn Marino 
4183e4b17023SJohn Marino   return ret;
4184e4b17023SJohn Marino 
4185e4b17023SJohn Marino  succeed:
4186e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
4187e4b17023SJohn Marino     fprintf (dump_file, "Empty EH handler %i removed.\n", lp->index);
4188e4b17023SJohn Marino   remove_eh_landing_pad (lp);
4189e4b17023SJohn Marino   return true;
4190e4b17023SJohn Marino }
4191e4b17023SJohn Marino 
4192e4b17023SJohn Marino /* Do a post-order traversal of the EH region tree.  Examine each
4193e4b17023SJohn Marino    post_landing_pad block and see if we can eliminate it as empty.  */
4194e4b17023SJohn Marino 
4195e4b17023SJohn Marino static bool
cleanup_all_empty_eh(void)4196e4b17023SJohn Marino cleanup_all_empty_eh (void)
4197e4b17023SJohn Marino {
4198e4b17023SJohn Marino   bool changed = false;
4199e4b17023SJohn Marino   eh_landing_pad lp;
4200e4b17023SJohn Marino   int i;
4201e4b17023SJohn Marino 
4202e4b17023SJohn Marino   for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
4203e4b17023SJohn Marino     if (lp)
4204e4b17023SJohn Marino       changed |= cleanup_empty_eh (lp);
4205e4b17023SJohn Marino 
4206e4b17023SJohn Marino   return changed;
4207e4b17023SJohn Marino }
4208e4b17023SJohn Marino 
4209e4b17023SJohn Marino /* Perform cleanups and lowering of exception handling
4210e4b17023SJohn Marino     1) cleanups regions with handlers doing nothing are optimized out
4211e4b17023SJohn Marino     2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4212e4b17023SJohn Marino     3) Info about regions that are containing instructions, and regions
4213e4b17023SJohn Marino        reachable via local EH edges is collected
4214e4b17023SJohn Marino     4) Eh tree is pruned for regions no longer neccesary.
4215e4b17023SJohn Marino 
4216e4b17023SJohn Marino    TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4217e4b17023SJohn Marino 	 Unify those that have the same failure decl and locus.
4218e4b17023SJohn Marino */
4219e4b17023SJohn Marino 
4220e4b17023SJohn Marino static unsigned int
execute_cleanup_eh_1(void)4221e4b17023SJohn Marino execute_cleanup_eh_1 (void)
4222e4b17023SJohn Marino {
4223e4b17023SJohn Marino   /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4224e4b17023SJohn Marino      looking up unreachable landing pads.  */
4225e4b17023SJohn Marino   remove_unreachable_handlers ();
4226e4b17023SJohn Marino 
4227e4b17023SJohn Marino   /* Watch out for the region tree vanishing due to all unreachable.  */
4228e4b17023SJohn Marino   if (cfun->eh->region_tree && optimize)
4229e4b17023SJohn Marino     {
4230e4b17023SJohn Marino       bool changed = false;
4231e4b17023SJohn Marino 
4232e4b17023SJohn Marino       changed |= unsplit_all_eh ();
4233e4b17023SJohn Marino       changed |= cleanup_all_empty_eh ();
4234e4b17023SJohn Marino 
4235e4b17023SJohn Marino       if (changed)
4236e4b17023SJohn Marino 	{
4237e4b17023SJohn Marino 	  free_dominance_info (CDI_DOMINATORS);
4238e4b17023SJohn Marino 	  free_dominance_info (CDI_POST_DOMINATORS);
4239e4b17023SJohn Marino 
4240e4b17023SJohn Marino           /* We delayed all basic block deletion, as we may have performed
4241e4b17023SJohn Marino 	     cleanups on EH edges while non-EH edges were still present.  */
4242e4b17023SJohn Marino 	  delete_unreachable_blocks ();
4243e4b17023SJohn Marino 
4244e4b17023SJohn Marino 	  /* We manipulated the landing pads.  Remove any region that no
4245e4b17023SJohn Marino 	     longer has a landing pad.  */
4246e4b17023SJohn Marino 	  remove_unreachable_handlers_no_lp ();
4247e4b17023SJohn Marino 
4248e4b17023SJohn Marino 	  return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
4249e4b17023SJohn Marino 	}
4250e4b17023SJohn Marino     }
4251e4b17023SJohn Marino 
4252e4b17023SJohn Marino   return 0;
4253e4b17023SJohn Marino }
4254e4b17023SJohn Marino 
4255e4b17023SJohn Marino static unsigned int
execute_cleanup_eh(void)4256e4b17023SJohn Marino execute_cleanup_eh (void)
4257e4b17023SJohn Marino {
4258e4b17023SJohn Marino   int ret = execute_cleanup_eh_1 ();
4259e4b17023SJohn Marino 
4260e4b17023SJohn Marino   /* If the function no longer needs an EH personality routine
4261e4b17023SJohn Marino      clear it.  This exposes cross-language inlining opportunities
4262e4b17023SJohn Marino      and avoids references to a never defined personality routine.  */
4263e4b17023SJohn Marino   if (DECL_FUNCTION_PERSONALITY (current_function_decl)
4264e4b17023SJohn Marino       && function_needs_eh_personality (cfun) != eh_personality_lang)
4265e4b17023SJohn Marino     DECL_FUNCTION_PERSONALITY (current_function_decl) = NULL_TREE;
4266e4b17023SJohn Marino 
4267e4b17023SJohn Marino   return ret;
4268e4b17023SJohn Marino }
4269e4b17023SJohn Marino 
4270e4b17023SJohn Marino static bool
gate_cleanup_eh(void)4271e4b17023SJohn Marino gate_cleanup_eh (void)
4272e4b17023SJohn Marino {
4273e4b17023SJohn Marino   return cfun->eh != NULL && cfun->eh->region_tree != NULL;
4274e4b17023SJohn Marino }
4275e4b17023SJohn Marino 
4276e4b17023SJohn Marino struct gimple_opt_pass pass_cleanup_eh = {
4277e4b17023SJohn Marino   {
4278e4b17023SJohn Marino    GIMPLE_PASS,
4279e4b17023SJohn Marino    "ehcleanup",			/* name */
4280e4b17023SJohn Marino    gate_cleanup_eh,		/* gate */
4281e4b17023SJohn Marino    execute_cleanup_eh,		/* execute */
4282e4b17023SJohn Marino    NULL,			/* sub */
4283e4b17023SJohn Marino    NULL,			/* next */
4284e4b17023SJohn Marino    0,				/* static_pass_number */
4285e4b17023SJohn Marino    TV_TREE_EH,			/* tv_id */
4286e4b17023SJohn Marino    PROP_gimple_lcf,		/* properties_required */
4287e4b17023SJohn Marino    0,				/* properties_provided */
4288e4b17023SJohn Marino    0,				/* properties_destroyed */
4289e4b17023SJohn Marino    0,				/* todo_flags_start */
4290e4b17023SJohn Marino    0             		/* todo_flags_finish */
4291e4b17023SJohn Marino    }
4292e4b17023SJohn Marino };
4293e4b17023SJohn Marino 
4294e4b17023SJohn Marino /* Verify that BB containing STMT as the last statement, has precisely the
4295e4b17023SJohn Marino    edge that make_eh_edges would create.  */
4296e4b17023SJohn Marino 
4297e4b17023SJohn Marino DEBUG_FUNCTION bool
verify_eh_edges(gimple stmt)4298e4b17023SJohn Marino verify_eh_edges (gimple stmt)
4299e4b17023SJohn Marino {
4300e4b17023SJohn Marino   basic_block bb = gimple_bb (stmt);
4301e4b17023SJohn Marino   eh_landing_pad lp = NULL;
4302e4b17023SJohn Marino   int lp_nr;
4303e4b17023SJohn Marino   edge_iterator ei;
4304e4b17023SJohn Marino   edge e, eh_edge;
4305e4b17023SJohn Marino 
4306e4b17023SJohn Marino   lp_nr = lookup_stmt_eh_lp (stmt);
4307e4b17023SJohn Marino   if (lp_nr > 0)
4308e4b17023SJohn Marino     lp = get_eh_landing_pad_from_number (lp_nr);
4309e4b17023SJohn Marino 
4310e4b17023SJohn Marino   eh_edge = NULL;
4311e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->succs)
4312e4b17023SJohn Marino     {
4313e4b17023SJohn Marino       if (e->flags & EDGE_EH)
4314e4b17023SJohn Marino 	{
4315e4b17023SJohn Marino 	  if (eh_edge)
4316e4b17023SJohn Marino 	    {
4317e4b17023SJohn Marino 	      error ("BB %i has multiple EH edges", bb->index);
4318e4b17023SJohn Marino 	      return true;
4319e4b17023SJohn Marino 	    }
4320e4b17023SJohn Marino 	  else
4321e4b17023SJohn Marino 	    eh_edge = e;
4322e4b17023SJohn Marino 	}
4323e4b17023SJohn Marino     }
4324e4b17023SJohn Marino 
4325e4b17023SJohn Marino   if (lp == NULL)
4326e4b17023SJohn Marino     {
4327e4b17023SJohn Marino       if (eh_edge)
4328e4b17023SJohn Marino 	{
4329e4b17023SJohn Marino 	  error ("BB %i can not throw but has an EH edge", bb->index);
4330e4b17023SJohn Marino 	  return true;
4331e4b17023SJohn Marino 	}
4332e4b17023SJohn Marino       return false;
4333e4b17023SJohn Marino     }
4334e4b17023SJohn Marino 
4335e4b17023SJohn Marino   if (!stmt_could_throw_p (stmt))
4336e4b17023SJohn Marino     {
4337e4b17023SJohn Marino       error ("BB %i last statement has incorrectly set lp", bb->index);
4338e4b17023SJohn Marino       return true;
4339e4b17023SJohn Marino     }
4340e4b17023SJohn Marino 
4341e4b17023SJohn Marino   if (eh_edge == NULL)
4342e4b17023SJohn Marino     {
4343e4b17023SJohn Marino       error ("BB %i is missing an EH edge", bb->index);
4344e4b17023SJohn Marino       return true;
4345e4b17023SJohn Marino     }
4346e4b17023SJohn Marino 
4347e4b17023SJohn Marino   if (eh_edge->dest != label_to_block (lp->post_landing_pad))
4348e4b17023SJohn Marino     {
4349e4b17023SJohn Marino       error ("Incorrect EH edge %i->%i", bb->index, eh_edge->dest->index);
4350e4b17023SJohn Marino       return true;
4351e4b17023SJohn Marino     }
4352e4b17023SJohn Marino 
4353e4b17023SJohn Marino   return false;
4354e4b17023SJohn Marino }
4355e4b17023SJohn Marino 
4356e4b17023SJohn Marino /* Similarly, but handle GIMPLE_EH_DISPATCH specifically.  */
4357e4b17023SJohn Marino 
4358e4b17023SJohn Marino DEBUG_FUNCTION bool
verify_eh_dispatch_edge(gimple stmt)4359e4b17023SJohn Marino verify_eh_dispatch_edge (gimple stmt)
4360e4b17023SJohn Marino {
4361e4b17023SJohn Marino   eh_region r;
4362e4b17023SJohn Marino   eh_catch c;
4363e4b17023SJohn Marino   basic_block src, dst;
4364e4b17023SJohn Marino   bool want_fallthru = true;
4365e4b17023SJohn Marino   edge_iterator ei;
4366e4b17023SJohn Marino   edge e, fall_edge;
4367e4b17023SJohn Marino 
4368e4b17023SJohn Marino   r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
4369e4b17023SJohn Marino   src = gimple_bb (stmt);
4370e4b17023SJohn Marino 
4371e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, src->succs)
4372e4b17023SJohn Marino     gcc_assert (e->aux == NULL);
4373e4b17023SJohn Marino 
4374e4b17023SJohn Marino   switch (r->type)
4375e4b17023SJohn Marino     {
4376e4b17023SJohn Marino     case ERT_TRY:
4377e4b17023SJohn Marino       for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
4378e4b17023SJohn Marino 	{
4379e4b17023SJohn Marino 	  dst = label_to_block (c->label);
4380e4b17023SJohn Marino 	  e = find_edge (src, dst);
4381e4b17023SJohn Marino 	  if (e == NULL)
4382e4b17023SJohn Marino 	    {
4383e4b17023SJohn Marino 	      error ("BB %i is missing an edge", src->index);
4384e4b17023SJohn Marino 	      return true;
4385e4b17023SJohn Marino 	    }
4386e4b17023SJohn Marino 	  e->aux = (void *)e;
4387e4b17023SJohn Marino 
4388e4b17023SJohn Marino 	  /* A catch-all handler doesn't have a fallthru.  */
4389e4b17023SJohn Marino 	  if (c->type_list == NULL)
4390e4b17023SJohn Marino 	    {
4391e4b17023SJohn Marino 	      want_fallthru = false;
4392e4b17023SJohn Marino 	      break;
4393e4b17023SJohn Marino 	    }
4394e4b17023SJohn Marino 	}
4395e4b17023SJohn Marino       break;
4396e4b17023SJohn Marino 
4397e4b17023SJohn Marino     case ERT_ALLOWED_EXCEPTIONS:
4398e4b17023SJohn Marino       dst = label_to_block (r->u.allowed.label);
4399e4b17023SJohn Marino       e = find_edge (src, dst);
4400e4b17023SJohn Marino       if (e == NULL)
4401e4b17023SJohn Marino 	{
4402e4b17023SJohn Marino 	  error ("BB %i is missing an edge", src->index);
4403e4b17023SJohn Marino 	  return true;
4404e4b17023SJohn Marino 	}
4405e4b17023SJohn Marino       e->aux = (void *)e;
4406e4b17023SJohn Marino       break;
4407e4b17023SJohn Marino 
4408e4b17023SJohn Marino     default:
4409e4b17023SJohn Marino       gcc_unreachable ();
4410e4b17023SJohn Marino     }
4411e4b17023SJohn Marino 
4412e4b17023SJohn Marino   fall_edge = NULL;
4413e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, src->succs)
4414e4b17023SJohn Marino     {
4415e4b17023SJohn Marino       if (e->flags & EDGE_FALLTHRU)
4416e4b17023SJohn Marino 	{
4417e4b17023SJohn Marino 	  if (fall_edge != NULL)
4418e4b17023SJohn Marino 	    {
4419e4b17023SJohn Marino 	      error ("BB %i too many fallthru edges", src->index);
4420e4b17023SJohn Marino 	      return true;
4421e4b17023SJohn Marino 	    }
4422e4b17023SJohn Marino 	  fall_edge = e;
4423e4b17023SJohn Marino 	}
4424e4b17023SJohn Marino       else if (e->aux)
4425e4b17023SJohn Marino 	e->aux = NULL;
4426e4b17023SJohn Marino       else
4427e4b17023SJohn Marino 	{
4428e4b17023SJohn Marino 	  error ("BB %i has incorrect edge", src->index);
4429e4b17023SJohn Marino 	  return true;
4430e4b17023SJohn Marino 	}
4431e4b17023SJohn Marino     }
4432e4b17023SJohn Marino   if ((fall_edge != NULL) ^ want_fallthru)
4433e4b17023SJohn Marino     {
4434e4b17023SJohn Marino       error ("BB %i has incorrect fallthru edge", src->index);
4435e4b17023SJohn Marino       return true;
4436e4b17023SJohn Marino     }
4437e4b17023SJohn Marino 
4438e4b17023SJohn Marino   return false;
4439e4b17023SJohn Marino }
4440