1/* Tree lowering pass.  Lowers GIMPLE into unstructured form.
2
3   Copyright (C) 2003, 2005 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to the Free
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA.  */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "tree.h"
27#include "rtl.h"
28#include "varray.h"
29#include "tree-gimple.h"
30#include "tree-inline.h"
31#include "diagnostic.h"
32#include "langhooks.h"
33#include "langhooks-def.h"
34#include "tree-flow.h"
35#include "timevar.h"
36#include "except.h"
37#include "hashtab.h"
38#include "flags.h"
39#include "function.h"
40#include "expr.h"
41#include "toplev.h"
42#include "tree-pass.h"
43
44struct lower_data
45{
46  /* Block the current statement belongs to.  */
47  tree block;
48
49  /* A TREE_LIST of label and return statements to be moved to the end
50     of the function.  */
51  tree return_statements;
52
53  /* True if the function calls __builtin_setjmp.  */
54  bool calls_builtin_setjmp;
55};
56
57static void lower_stmt (tree_stmt_iterator *, struct lower_data *);
58static void lower_bind_expr (tree_stmt_iterator *, struct lower_data *);
59static void lower_cond_expr (tree_stmt_iterator *, struct lower_data *);
60static void lower_return_expr (tree_stmt_iterator *, struct lower_data *);
61static void lower_builtin_setjmp (tree_stmt_iterator *);
62
63/* Lower the body of current_function_decl.  */
64
65static unsigned int
66lower_function_body (void)
67{
68  struct lower_data data;
69  tree *body_p = &DECL_SAVED_TREE (current_function_decl);
70  tree bind = *body_p;
71  tree_stmt_iterator i;
72  tree t, x;
73
74  gcc_assert (TREE_CODE (bind) == BIND_EXPR);
75
76  memset (&data, 0, sizeof (data));
77  data.block = DECL_INITIAL (current_function_decl);
78  BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
79  BLOCK_CHAIN (data.block) = NULL_TREE;
80  TREE_ASM_WRITTEN (data.block) = 1;
81
82  *body_p = alloc_stmt_list ();
83  i = tsi_start (*body_p);
84  tsi_link_after (&i, bind, TSI_NEW_STMT);
85  lower_bind_expr (&i, &data);
86
87  i = tsi_last (*body_p);
88
89  /* If the function falls off the end, we need a null return statement.
90     If we've already got one in the return_statements list, we don't
91     need to do anything special.  Otherwise build one by hand.  */
92  if (block_may_fallthru (*body_p)
93      && (data.return_statements == NULL
94          || TREE_OPERAND (TREE_VALUE (data.return_statements), 0) != NULL))
95    {
96      x = build1 (RETURN_EXPR, void_type_node, NULL);
97      SET_EXPR_LOCATION (x, cfun->function_end_locus);
98      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
99    }
100
101  /* If we lowered any return statements, emit the representative
102     at the end of the function.  */
103  for (t = data.return_statements ; t ; t = TREE_CHAIN (t))
104    {
105      x = build1 (LABEL_EXPR, void_type_node, TREE_PURPOSE (t));
106      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
107
108      /* Remove the line number from the representative return statement.
109	 It now fills in for many such returns.  Failure to remove this
110	 will result in incorrect results for coverage analysis.  */
111      x = TREE_VALUE (t);
112#ifdef USE_MAPPED_LOCATION
113      SET_EXPR_LOCATION (x, UNKNOWN_LOCATION);
114#else
115      SET_EXPR_LOCUS (x, NULL);
116#endif
117      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
118    }
119
120  /* If the function calls __builtin_setjmp, we need to emit the computed
121     goto that will serve as the unique dispatcher for all the receivers.  */
122  if (data.calls_builtin_setjmp)
123    {
124      tree disp_label, disp_var, arg;
125
126      /* Build 'DISP_LABEL:' and insert.  */
127      disp_label = create_artificial_label ();
128      /* This mark will create forward edges from every call site.  */
129      DECL_NONLOCAL (disp_label) = 1;
130      current_function_has_nonlocal_label = 1;
131      x = build1 (LABEL_EXPR, void_type_node, disp_label);
132      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
133
134      /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);'
135	 and insert.  */
136      disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
137      t = build_addr (disp_label, current_function_decl);
138      arg = tree_cons (NULL, t, NULL);
139      t = implicit_built_in_decls[BUILT_IN_SETJMP_DISPATCHER];
140      t = build_function_call_expr (t,arg);
141      x = build2 (MODIFY_EXPR, void_type_node, disp_var, t);
142
143      /* Build 'goto DISP_VAR;' and insert.  */
144      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
145      x = build1 (GOTO_EXPR, void_type_node, disp_var);
146      tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
147    }
148
149  gcc_assert (data.block == DECL_INITIAL (current_function_decl));
150  BLOCK_SUBBLOCKS (data.block)
151    = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
152
153  clear_block_marks (data.block);
154  return 0;
155}
156
157struct tree_opt_pass pass_lower_cf =
158{
159  "lower",				/* name */
160  NULL,					/* gate */
161  lower_function_body,			/* execute */
162  NULL,					/* sub */
163  NULL,					/* next */
164  0,					/* static_pass_number */
165  0,					/* tv_id */
166  PROP_gimple_any,			/* properties_required */
167  PROP_gimple_lcf,			/* properties_provided */
168  0,					/* properties_destroyed */
169  0,					/* todo_flags_start */
170  TODO_dump_func,			/* todo_flags_finish */
171  0					/* letter */
172};
173
174
175/* Lower the EXPR.  Unlike gimplification the statements are not relowered
176   when they are changed -- if this has to be done, the lowering routine must
177   do it explicitly.  DATA is passed through the recursion.  */
178
179static void
180lower_stmt_body (tree expr, struct lower_data *data)
181{
182  tree_stmt_iterator tsi;
183
184  for (tsi = tsi_start (expr); !tsi_end_p (tsi); )
185    lower_stmt (&tsi, data);
186}
187
188
189/* Lower the OpenMP directive statement pointed by TSI.  DATA is
190   passed through the recursion.  */
191
192static void
193lower_omp_directive (tree_stmt_iterator *tsi, struct lower_data *data)
194{
195  tree stmt;
196
197  stmt = tsi_stmt (*tsi);
198
199  lower_stmt_body (OMP_BODY (stmt), data);
200  tsi_link_before (tsi, stmt, TSI_SAME_STMT);
201  tsi_link_before (tsi, OMP_BODY (stmt), TSI_SAME_STMT);
202  OMP_BODY (stmt) = NULL_TREE;
203  tsi_delink (tsi);
204}
205
206
207/* Lower statement TSI.  DATA is passed through the recursion.  */
208
209static void
210lower_stmt (tree_stmt_iterator *tsi, struct lower_data *data)
211{
212  tree stmt = tsi_stmt (*tsi);
213
214  if (EXPR_HAS_LOCATION (stmt) && data)
215    TREE_BLOCK (stmt) = data->block;
216
217  switch (TREE_CODE (stmt))
218    {
219    case BIND_EXPR:
220      lower_bind_expr (tsi, data);
221      return;
222    case COND_EXPR:
223      lower_cond_expr (tsi, data);
224      return;
225    case RETURN_EXPR:
226      lower_return_expr (tsi, data);
227      return;
228
229    case TRY_FINALLY_EXPR:
230    case TRY_CATCH_EXPR:
231      lower_stmt_body (TREE_OPERAND (stmt, 0), data);
232      lower_stmt_body (TREE_OPERAND (stmt, 1), data);
233      break;
234    case CATCH_EXPR:
235      lower_stmt_body (CATCH_BODY (stmt), data);
236      break;
237    case EH_FILTER_EXPR:
238      lower_stmt_body (EH_FILTER_FAILURE (stmt), data);
239      break;
240
241    case NOP_EXPR:
242    case ASM_EXPR:
243    case GOTO_EXPR:
244    case LABEL_EXPR:
245    case SWITCH_EXPR:
246    case OMP_FOR:
247    case OMP_SECTIONS:
248    case OMP_SECTION:
249    case OMP_SINGLE:
250    case OMP_MASTER:
251    case OMP_ORDERED:
252    case OMP_CRITICAL:
253    case OMP_RETURN:
254    case OMP_CONTINUE:
255      break;
256
257    case MODIFY_EXPR:
258      if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
259	stmt = TREE_OPERAND (stmt, 1);
260      else
261	break;
262      /* FALLTHRU */
263
264    case CALL_EXPR:
265      {
266	tree decl = get_callee_fndecl (stmt);
267	if (decl
268	    && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
269	    && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
270	  {
271	    data->calls_builtin_setjmp = true;
272	    lower_builtin_setjmp (tsi);
273	    return;
274	  }
275      }
276      break;
277
278    case OMP_PARALLEL:
279      lower_omp_directive (tsi, data);
280      return;
281
282    default:
283      gcc_unreachable ();
284    }
285
286  tsi_next (tsi);
287}
288
289/* Lower a bind_expr TSI.  DATA is passed through the recursion.  */
290
291static void
292lower_bind_expr (tree_stmt_iterator *tsi, struct lower_data *data)
293{
294  tree old_block = data->block;
295  tree stmt = tsi_stmt (*tsi);
296  tree new_block = BIND_EXPR_BLOCK (stmt);
297
298  if (new_block)
299    {
300      if (new_block == old_block)
301	{
302	  /* The outermost block of the original function may not be the
303	     outermost statement chain of the gimplified function.  So we
304	     may see the outermost block just inside the function.  */
305	  gcc_assert (new_block == DECL_INITIAL (current_function_decl));
306	  new_block = NULL;
307	}
308      else
309	{
310	  /* We do not expect to handle duplicate blocks.  */
311	  gcc_assert (!TREE_ASM_WRITTEN (new_block));
312	  TREE_ASM_WRITTEN (new_block) = 1;
313
314	  /* Block tree may get clobbered by inlining.  Normally this would
315	     be fixed in rest_of_decl_compilation using block notes, but
316	     since we are not going to emit them, it is up to us.  */
317	  BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
318	  BLOCK_SUBBLOCKS (old_block) = new_block;
319	  BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
320	  BLOCK_SUPERCONTEXT (new_block) = old_block;
321
322	  data->block = new_block;
323	}
324    }
325
326  record_vars (BIND_EXPR_VARS (stmt));
327  lower_stmt_body (BIND_EXPR_BODY (stmt), data);
328
329  if (new_block)
330    {
331      gcc_assert (data->block == new_block);
332
333      BLOCK_SUBBLOCKS (new_block)
334	= blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
335      data->block = old_block;
336    }
337
338  /* The BIND_EXPR no longer carries any useful information -- kill it.  */
339  tsi_link_before (tsi, BIND_EXPR_BODY (stmt), TSI_SAME_STMT);
340  tsi_delink (tsi);
341}
342
343/* Try to determine whether a TRY_CATCH expression can fall through.
344   This is a subroutine of block_may_fallthru.  */
345
346static bool
347try_catch_may_fallthru (tree stmt)
348{
349  tree_stmt_iterator i;
350
351  /* If the TRY block can fall through, the whole TRY_CATCH can
352     fall through.  */
353  if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
354    return true;
355
356  i = tsi_start (TREE_OPERAND (stmt, 1));
357  switch (TREE_CODE (tsi_stmt (i)))
358    {
359    case CATCH_EXPR:
360      /* We expect to see a sequence of CATCH_EXPR trees, each with a
361	 catch expression and a body.  The whole TRY_CATCH may fall
362	 through iff any of the catch bodies falls through.  */
363      for (; !tsi_end_p (i); tsi_next (&i))
364	{
365	  if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
366	    return true;
367	}
368      return false;
369
370    case EH_FILTER_EXPR:
371      /* The exception filter expression only matters if there is an
372	 exception.  If the exception does not match EH_FILTER_TYPES,
373	 we will execute EH_FILTER_FAILURE, and we will fall through
374	 if that falls through.  If the exception does match
375	 EH_FILTER_TYPES, the stack unwinder will continue up the
376	 stack, so we will not fall through.  We don't know whether we
377	 will throw an exception which matches EH_FILTER_TYPES or not,
378	 so we just ignore EH_FILTER_TYPES and assume that we might
379	 throw an exception which doesn't match.  */
380      return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
381
382    default:
383      /* This case represents statements to be executed when an
384	 exception occurs.  Those statements are implicitly followed
385	 by a RESX_EXPR to resume execution after the exception.  So
386	 in this case the TRY_CATCH never falls through.  */
387      return false;
388    }
389}
390
391/* Try to determine if we can fall out of the bottom of BLOCK.  This guess
392   need not be 100% accurate; simply be conservative and return true if we
393   don't know.  This is used only to avoid stupidly generating extra code.
394   If we're wrong, we'll just delete the extra code later.  */
395
396bool
397block_may_fallthru (tree block)
398{
399  tree stmt = expr_last (block);
400
401  switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
402    {
403    case GOTO_EXPR:
404    case RETURN_EXPR:
405    case RESX_EXPR:
406      /* Easy cases.  If the last statement of the block implies
407	 control transfer, then we can't fall through.  */
408      return false;
409
410    case SWITCH_EXPR:
411      /* If SWITCH_LABELS is set, this is lowered, and represents a
412	 branch to a selected label and hence can not fall through.
413	 Otherwise SWITCH_BODY is set, and the switch can fall
414	 through.  */
415      return SWITCH_LABELS (stmt) == NULL_TREE;
416
417    case COND_EXPR:
418      if (block_may_fallthru (COND_EXPR_THEN (stmt)))
419	return true;
420      return block_may_fallthru (COND_EXPR_ELSE (stmt));
421
422    case BIND_EXPR:
423      return block_may_fallthru (BIND_EXPR_BODY (stmt));
424
425    case TRY_CATCH_EXPR:
426      return try_catch_may_fallthru (stmt);
427
428    case TRY_FINALLY_EXPR:
429      /* The finally clause is always executed after the try clause,
430	 so if it does not fall through, then the try-finally will not
431	 fall through.  Otherwise, if the try clause does not fall
432	 through, then when the finally clause falls through it will
433	 resume execution wherever the try clause was going.  So the
434	 whole try-finally will only fall through if both the try
435	 clause and the finally clause fall through.  */
436      return (block_may_fallthru (TREE_OPERAND (stmt, 0))
437	      && block_may_fallthru (TREE_OPERAND (stmt, 1)));
438
439    case MODIFY_EXPR:
440      if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
441	stmt = TREE_OPERAND (stmt, 1);
442      else
443	return true;
444      /* FALLTHRU */
445
446    case CALL_EXPR:
447      /* Functions that do not return do not fall through.  */
448      return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
449
450    case CLEANUP_POINT_EXPR:
451      return block_may_fallthru (TREE_OPERAND (stmt, 0));
452
453    default:
454      return true;
455    }
456}
457
458/* Lower a cond_expr TSI.  DATA is passed through the recursion.  */
459
460static void
461lower_cond_expr (tree_stmt_iterator *tsi, struct lower_data *data)
462{
463  tree stmt = tsi_stmt (*tsi);
464  bool then_is_goto, else_is_goto;
465  tree then_branch, else_branch;
466  tree then_goto, else_goto;
467
468  then_branch = COND_EXPR_THEN (stmt);
469  else_branch = COND_EXPR_ELSE (stmt);
470
471  lower_stmt_body (then_branch, data);
472  lower_stmt_body (else_branch, data);
473
474  then_goto = expr_only (then_branch);
475  then_is_goto = then_goto && simple_goto_p (then_goto);
476
477  else_goto = expr_only (else_branch);
478  else_is_goto = else_goto && simple_goto_p (else_goto);
479
480  if (!then_is_goto || !else_is_goto)
481    {
482      tree then_label, else_label, end_label, t;
483
484      then_label = NULL_TREE;
485      else_label = NULL_TREE;
486      end_label = NULL_TREE;
487
488      /* Replace the cond_expr with explicit gotos.  */
489      if (!then_is_goto)
490	{
491	  t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
492	  if (TREE_SIDE_EFFECTS (then_branch))
493	    then_label = t;
494	  else
495	    end_label = t;
496	  then_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
497	}
498
499      if (!else_is_goto)
500	{
501	  t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
502	  if (TREE_SIDE_EFFECTS (else_branch))
503	    else_label = t;
504	  else
505	    {
506	      /* Both THEN and ELSE can be no-ops if one or both contained an
507	         empty BIND_EXPR that was associated with the toplevel block
508	         of an inlined function.  In that case remove_useless_stmts
509	         can't have cleaned things up for us; kill the whole
510	         conditional now.  */
511	      if (end_label)
512		{
513		  tsi_delink (tsi);
514		  return;
515		}
516	      else
517		end_label = t;
518	    }
519	  else_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
520	}
521
522      if (then_label)
523	{
524	  bool may_fallthru = block_may_fallthru (then_branch);
525
526	  tsi_link_after (tsi, then_label, TSI_CONTINUE_LINKING);
527	  tsi_link_after (tsi, then_branch, TSI_CONTINUE_LINKING);
528
529	  if (else_label && may_fallthru)
530	    {
531	      end_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
532	      t = build_and_jump (&LABEL_EXPR_LABEL (end_label));
533	      tsi_link_after (tsi, t, TSI_CONTINUE_LINKING);
534	    }
535	}
536
537      if (else_label)
538	{
539	  tsi_link_after (tsi, else_label, TSI_CONTINUE_LINKING);
540	  tsi_link_after (tsi, else_branch, TSI_CONTINUE_LINKING);
541	}
542
543      if (end_label)
544	tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
545    }
546
547  COND_EXPR_THEN (stmt) = then_goto;
548  COND_EXPR_ELSE (stmt) = else_goto;
549
550  tsi_next (tsi);
551}
552
553/* Lower a return_expr TSI.  DATA is passed through the recursion.  */
554
555static void
556lower_return_expr (tree_stmt_iterator *tsi, struct lower_data *data)
557{
558  tree stmt = tsi_stmt (*tsi);
559  tree value, t, label;
560
561  /* Extract the value being returned.  */
562  value = TREE_OPERAND (stmt, 0);
563  if (value && TREE_CODE (value) == MODIFY_EXPR)
564    value = TREE_OPERAND (value, 1);
565
566  /* Match this up with an existing return statement that's been created.  */
567  for (t = data->return_statements; t ; t = TREE_CHAIN (t))
568    {
569      tree tvalue = TREE_OPERAND (TREE_VALUE (t), 0);
570      if (tvalue && TREE_CODE (tvalue) == MODIFY_EXPR)
571	tvalue = TREE_OPERAND (tvalue, 1);
572
573      if (value == tvalue)
574	{
575	  label = TREE_PURPOSE (t);
576	  goto found;
577	}
578    }
579
580  /* Not found.  Create a new label and record the return statement.  */
581  label = create_artificial_label ();
582  data->return_statements = tree_cons (label, stmt, data->return_statements);
583
584  /* Generate a goto statement and remove the return statement.  */
585 found:
586  t = build1 (GOTO_EXPR, void_type_node, label);
587  SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
588  tsi_link_before (tsi, t, TSI_SAME_STMT);
589  tsi_delink (tsi);
590}
591
592/* Lower a __builtin_setjmp TSI.
593
594   __builtin_setjmp is passed a pointer to an array of five words (not
595   all will be used on all machines).  It operates similarly to the C
596   library function of the same name, but is more efficient.
597
598   It is lowered into 3 other builtins, namely __builtin_setjmp_setup,
599   __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with
600   __builtin_setjmp_dispatcher shared among all the instances; that's
601   why it is only emitted at the end by lower_function_body.
602
603   After full lowering, the body of the function should look like:
604
605    {
606      void * setjmpvar.0;
607      int D.1844;
608      int D.2844;
609
610      [...]
611
612      __builtin_setjmp_setup (&buf, &<D1847>);
613      D.1844 = 0;
614      goto <D1846>;
615      <D1847>:;
616      __builtin_setjmp_receiver (&<D1847>);
617      D.1844 = 1;
618      <D1846>:;
619      if (D.1844 == 0) goto <D1848>; else goto <D1849>;
620
621      [...]
622
623      __builtin_setjmp_setup (&buf, &<D2847>);
624      D.2844 = 0;
625      goto <D2846>;
626      <D2847>:;
627      __builtin_setjmp_receiver (&<D2847>);
628      D.2844 = 1;
629      <D2846>:;
630      if (D.2844 == 0) goto <D2848>; else goto <D2849>;
631
632      [...]
633
634      <D3850>:;
635      return;
636      <D3853>: [non-local];
637      setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>);
638      goto setjmpvar.0;
639    }
640
641   The dispatcher block will be both the unique destination of all the
642   abnormal call edges and the unique source of all the abnormal edges
643   to the receivers, thus keeping the complexity explosion localized.  */
644
645static void
646lower_builtin_setjmp (tree_stmt_iterator *tsi)
647{
648  tree stmt = tsi_stmt (*tsi);
649  tree cont_label = create_artificial_label ();
650  tree next_label = create_artificial_label ();
651  tree dest, t, arg;
652
653  /* NEXT_LABEL is the label __builtin_longjmp will jump to.  Its address is
654     passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver.  */
655  FORCED_LABEL (next_label) = 1;
656
657  if (TREE_CODE (stmt) == MODIFY_EXPR)
658    {
659      dest = TREE_OPERAND (stmt, 0);
660      stmt = TREE_OPERAND (stmt, 1);
661    }
662  else
663    dest = NULL_TREE;
664
665  /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert.  */
666  t = build_addr (next_label, current_function_decl);
667  arg = tree_cons (NULL, t, NULL);
668  t = TREE_VALUE (TREE_OPERAND (stmt, 1));
669  arg = tree_cons (NULL, t, arg);
670  t = implicit_built_in_decls[BUILT_IN_SETJMP_SETUP];
671  t = build_function_call_expr (t, arg);
672  SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
673  tsi_link_before (tsi, t, TSI_SAME_STMT);
674
675  /* Build 'DEST = 0' and insert.  */
676  if (dest)
677    {
678      t = build2 (MODIFY_EXPR, void_type_node, dest, integer_zero_node);
679      SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
680      tsi_link_before (tsi, t, TSI_SAME_STMT);
681    }
682
683  /* Build 'goto CONT_LABEL' and insert.  */
684  t = build1 (GOTO_EXPR, void_type_node, cont_label);
685  tsi_link_before (tsi, t, TSI_SAME_STMT);
686
687  /* Build 'NEXT_LABEL:' and insert.  */
688  t = build1 (LABEL_EXPR, void_type_node, next_label);
689  tsi_link_before (tsi, t, TSI_SAME_STMT);
690
691  /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert.  */
692  t = build_addr (next_label, current_function_decl);
693  arg = tree_cons (NULL, t, NULL);
694  t = implicit_built_in_decls[BUILT_IN_SETJMP_RECEIVER];
695  t = build_function_call_expr (t, arg);
696  SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
697  tsi_link_before (tsi, t, TSI_SAME_STMT);
698
699  /* Build 'DEST = 1' and insert.  */
700  if (dest)
701    {
702      t = build2 (MODIFY_EXPR, void_type_node, dest, integer_one_node);
703      SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
704      tsi_link_before (tsi, t, TSI_SAME_STMT);
705    }
706
707  /* Build 'CONT_LABEL:' and insert.  */
708  t = build1 (LABEL_EXPR, void_type_node, cont_label);
709  tsi_link_before (tsi, t, TSI_SAME_STMT);
710
711  /* Remove the call to __builtin_setjmp.  */
712  tsi_delink (tsi);
713}
714
715
716/* Record the variables in VARS into function FN.  */
717
718void
719record_vars_into (tree vars, tree fn)
720{
721  struct function *saved_cfun = cfun;
722
723  if (fn != current_function_decl)
724    cfun = DECL_STRUCT_FUNCTION (fn);
725
726  for (; vars; vars = TREE_CHAIN (vars))
727    {
728      tree var = vars;
729
730      /* BIND_EXPRs contains also function/type/constant declarations
731         we don't need to care about.  */
732      if (TREE_CODE (var) != VAR_DECL)
733	continue;
734
735      /* Nothing to do in this case.  */
736      if (DECL_EXTERNAL (var))
737	continue;
738
739      /* Record the variable.  */
740      cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
741					     cfun->unexpanded_var_list);
742    }
743
744  if (fn != current_function_decl)
745    cfun = saved_cfun;
746}
747
748
749/* Record the variables in VARS into current_function_decl.  */
750
751void
752record_vars (tree vars)
753{
754  record_vars_into (vars, current_function_decl);
755}
756
757
758/* Mark BLOCK used if it has a used variable in it, then recurse over its
759   subblocks.  */
760
761static void
762mark_blocks_with_used_vars (tree block)
763{
764  tree var;
765  tree subblock;
766
767  if (!TREE_USED (block))
768    {
769      for (var = BLOCK_VARS (block);
770	   var;
771	   var = TREE_CHAIN (var))
772	{
773	  if (TREE_USED (var))
774	    {
775	      TREE_USED (block) = true;
776	      break;
777	    }
778	}
779    }
780  for (subblock = BLOCK_SUBBLOCKS (block);
781       subblock;
782       subblock = BLOCK_CHAIN (subblock))
783    mark_blocks_with_used_vars (subblock);
784}
785
786/* Mark the used attribute on blocks correctly.  */
787
788static unsigned int
789mark_used_blocks (void)
790{
791  mark_blocks_with_used_vars (DECL_INITIAL (current_function_decl));
792  return 0;
793}
794
795
796struct tree_opt_pass pass_mark_used_blocks =
797{
798  "blocks",				/* name */
799  NULL,					/* gate */
800  mark_used_blocks,			/* execute */
801  NULL,					/* sub */
802  NULL,					/* next */
803  0,					/* static_pass_number */
804  0,					/* tv_id */
805  0,					/* properties_required */
806  0,					/* properties_provided */
807  0,					/* properties_destroyed */
808  0,					/* todo_flags_start */
809  TODO_dump_func,			/* todo_flags_finish */
810  0					/* letter */
811};
812