OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / gimple-low.c
1 /* GIMPLE lowering pass.  Converts High GIMPLE into Low GIMPLE.
2
3    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "varray.h"
28 #include "gimple.h"
29 #include "tree-iterator.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
44 /* The differences between High GIMPLE and Low GIMPLE are the
45    following:
46
47    1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
48
49    2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
50       flow and exception regions are built as an on-the-side region
51       hierarchy (See tree-eh.c:lower_eh_constructs).
52
53    3- Multiple identical return statements are grouped into a single
54       return and gotos to the unique return site.  */
55
56 /* Match a return statement with a label.  During lowering, we identify
57    identical return statements and replace duplicates with a jump to
58    the corresponding label.  */
59 struct return_statements_t
60 {
61   tree label;
62   gimple stmt;
63 };
64 typedef struct return_statements_t return_statements_t;
65
66 DEF_VEC_O(return_statements_t);
67 DEF_VEC_ALLOC_O(return_statements_t,heap);
68
69 struct lower_data
70 {
71   /* Block the current statement belongs to.  */
72   tree block;
73
74   /* A vector of label and return statements to be moved to the end
75      of the function.  */
76   VEC(return_statements_t,heap) *return_statements;
77
78   /* True if the function calls __builtin_setjmp.  */
79   bool calls_builtin_setjmp;
80 };
81
82 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
83 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
84 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
85 static void lower_builtin_setjmp (gimple_stmt_iterator *);
86
87
88 /* Lower the body of current_function_decl from High GIMPLE into Low
89    GIMPLE.  */
90
91 static unsigned int
92 lower_function_body (void)
93 {
94   struct lower_data data;
95   gimple_seq body = gimple_body (current_function_decl);
96   gimple_seq lowered_body;
97   gimple_stmt_iterator i;
98   gimple bind;
99   tree t;
100   gimple x;
101
102   /* The gimplifier should've left a body of exactly one statement,
103      namely a GIMPLE_BIND.  */
104   gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
105               && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
106
107   memset (&data, 0, sizeof (data));
108   data.block = DECL_INITIAL (current_function_decl);
109   BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
110   BLOCK_CHAIN (data.block) = NULL_TREE;
111   TREE_ASM_WRITTEN (data.block) = 1;
112   data.return_statements = VEC_alloc (return_statements_t, heap, 8);
113
114   bind = gimple_seq_first_stmt (body);
115   lowered_body = NULL;
116   gimple_seq_add_stmt (&lowered_body, bind);
117   i = gsi_start (lowered_body);
118   lower_gimple_bind (&i, &data);
119
120   /* Once the old body has been lowered, replace it with the new
121      lowered sequence.  */
122   gimple_set_body (current_function_decl, lowered_body);
123
124   i = gsi_last (lowered_body);
125
126   /* If the function falls off the end, we need a null return statement.
127      If we've already got one in the return_statements vector, we don't
128      need to do anything special.  Otherwise build one by hand.  */
129   if (gimple_seq_may_fallthru (lowered_body)
130       && (VEC_empty (return_statements_t, data.return_statements)
131           || gimple_return_retval (VEC_last (return_statements_t,
132                                    data.return_statements)->stmt) != NULL))
133     {
134       x = gimple_build_return (NULL);
135       gimple_set_location (x, cfun->function_end_locus);
136       gimple_set_block (x, DECL_INITIAL (current_function_decl));
137       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
138     }
139
140   /* If we lowered any return statements, emit the representative
141      at the end of the function.  */
142   while (!VEC_empty (return_statements_t, data.return_statements))
143     {
144       return_statements_t t;
145
146       /* Unfortunately, we can't use VEC_pop because it returns void for
147          objects.  */
148       t = *VEC_last (return_statements_t, data.return_statements);
149       VEC_truncate (return_statements_t,
150                     data.return_statements,
151                     VEC_length (return_statements_t,
152                                 data.return_statements) - 1);
153
154       x = gimple_build_label (t.label);
155       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
156
157       /* Remove the line number from the representative return statement.
158          It now fills in for many such returns.  Failure to remove this
159          will result in incorrect results for coverage analysis.  */
160       gimple_set_location (t.stmt, UNKNOWN_LOCATION);
161       gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
162     }
163
164   /* If the function calls __builtin_setjmp, we need to emit the computed
165      goto that will serve as the unique dispatcher for all the receivers.  */
166   if (data.calls_builtin_setjmp)
167     {
168       tree disp_label, disp_var, arg;
169
170       /* Build 'DISP_LABEL:' and insert.  */
171       disp_label = create_artificial_label ();
172       /* This mark will create forward edges from every call site.  */
173       DECL_NONLOCAL (disp_label) = 1;
174       cfun->has_nonlocal_label = 1;
175       x = gimple_build_label (disp_label);
176       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
177
178       /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);'
179          and insert.  */
180       disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
181       arg = build_addr (disp_label, current_function_decl);
182       t = implicit_built_in_decls[BUILT_IN_SETJMP_DISPATCHER];
183       x = gimple_build_call (t, 1, arg);
184       gimple_call_set_lhs (x, disp_var);
185
186       /* Build 'goto DISP_VAR;' and insert.  */
187       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
188       x = gimple_build_goto (disp_var);
189       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
190     }
191
192   gcc_assert (data.block == DECL_INITIAL (current_function_decl));
193   BLOCK_SUBBLOCKS (data.block)
194     = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
195
196   clear_block_marks (data.block);
197   VEC_free(return_statements_t, heap, data.return_statements);
198   return 0;
199 }
200
201 struct gimple_opt_pass pass_lower_cf = 
202 {
203  {
204   GIMPLE_PASS,
205   "lower",                              /* name */
206   NULL,                                 /* gate */
207   lower_function_body,                  /* execute */
208   NULL,                                 /* sub */
209   NULL,                                 /* next */
210   0,                                    /* static_pass_number */
211   0,                                    /* tv_id */
212   PROP_gimple_any,                      /* properties_required */
213   PROP_gimple_lcf,                      /* properties_provided */
214   0,                                    /* properties_destroyed */
215   0,                                    /* todo_flags_start */
216   TODO_dump_func                        /* todo_flags_finish */
217  }
218 };
219
220
221 /* Lower sequence SEQ.  Unlike gimplification the statements are not relowered
222    when they are changed -- if this has to be done, the lowering routine must
223    do it explicitly.  DATA is passed through the recursion.  */
224
225 static void
226 lower_sequence (gimple_seq seq, struct lower_data *data)
227 {
228   gimple_stmt_iterator gsi;
229
230   for (gsi = gsi_start (seq); !gsi_end_p (gsi); )
231     lower_stmt (&gsi, data);
232 }
233
234
235 /* Lower the OpenMP directive statement pointed by GSI.  DATA is
236    passed through the recursion.  */
237
238 static void
239 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
240 {
241   gimple stmt;
242   
243   stmt = gsi_stmt (*gsi);
244
245   lower_sequence (gimple_omp_body (stmt), data);
246   gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
247   gsi_insert_seq_before (gsi, gimple_omp_body (stmt), GSI_SAME_STMT);
248   gimple_omp_set_body (stmt, NULL);
249   gsi_remove (gsi, false);
250 }
251
252
253 /* Lower statement GSI.  DATA is passed through the recursion.  */
254
255 static void
256 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
257 {
258   gimple stmt = gsi_stmt (*gsi);
259
260   gimple_set_block (stmt, data->block);
261
262   switch (gimple_code (stmt))
263     {
264     case GIMPLE_BIND:
265       lower_gimple_bind (gsi, data);
266       return;
267
268     case GIMPLE_COND:
269       /* The gimplifier has already lowered this into gotos.  */
270       break;
271
272     case GIMPLE_RETURN:
273       lower_gimple_return (gsi, data);
274       return;
275
276     case GIMPLE_TRY:
277       lower_sequence (gimple_try_eval (stmt), data);
278       lower_sequence (gimple_try_cleanup (stmt), data);
279       break;
280
281     case GIMPLE_CATCH:
282       lower_sequence (gimple_catch_handler (stmt), data);
283       break;
284
285     case GIMPLE_EH_FILTER:
286       lower_sequence (gimple_eh_filter_failure (stmt), data);
287       break;
288
289     case GIMPLE_NOP:
290     case GIMPLE_ASM:
291     case GIMPLE_ASSIGN:
292     case GIMPLE_GOTO:
293     case GIMPLE_PREDICT:
294     case GIMPLE_LABEL:
295     case GIMPLE_SWITCH:
296     case GIMPLE_CHANGE_DYNAMIC_TYPE:
297     case GIMPLE_OMP_FOR:
298     case GIMPLE_OMP_SECTIONS:
299     case GIMPLE_OMP_SECTIONS_SWITCH:
300     case GIMPLE_OMP_SECTION:
301     case GIMPLE_OMP_SINGLE:
302     case GIMPLE_OMP_MASTER:
303     case GIMPLE_OMP_ORDERED:
304     case GIMPLE_OMP_CRITICAL:
305     case GIMPLE_OMP_RETURN:
306     case GIMPLE_OMP_ATOMIC_LOAD:
307     case GIMPLE_OMP_ATOMIC_STORE:
308     case GIMPLE_OMP_CONTINUE:
309       break;
310
311     case GIMPLE_CALL:
312       {
313         tree decl = gimple_call_fndecl (stmt);
314
315         if (decl
316             && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
317             && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
318           {
319             data->calls_builtin_setjmp = true;
320             lower_builtin_setjmp (gsi);
321             return;
322           }
323       }
324       break;
325
326     case GIMPLE_OMP_PARALLEL:
327     case GIMPLE_OMP_TASK:
328       lower_omp_directive (gsi, data);
329       return;
330
331     default:
332       gcc_unreachable ();
333     }
334
335   gsi_next (gsi);
336 }
337
338 /* Lower a bind_expr TSI.  DATA is passed through the recursion.  */
339
340 static void
341 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
342 {
343   tree old_block = data->block;
344   gimple stmt = gsi_stmt (*gsi);
345   tree new_block = gimple_bind_block (stmt);
346
347   if (new_block)
348     {
349       if (new_block == old_block)
350         {
351           /* The outermost block of the original function may not be the
352              outermost statement chain of the gimplified function.  So we
353              may see the outermost block just inside the function.  */
354           gcc_assert (new_block == DECL_INITIAL (current_function_decl));
355           new_block = NULL;
356         }
357       else
358         {
359           /* We do not expect to handle duplicate blocks.  */
360           gcc_assert (!TREE_ASM_WRITTEN (new_block));
361           TREE_ASM_WRITTEN (new_block) = 1;
362
363           /* Block tree may get clobbered by inlining.  Normally this would
364              be fixed in rest_of_decl_compilation using block notes, but
365              since we are not going to emit them, it is up to us.  */
366           BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
367           BLOCK_SUBBLOCKS (old_block) = new_block;
368           BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
369           BLOCK_SUPERCONTEXT (new_block) = old_block;
370
371           data->block = new_block;
372         }
373     }
374
375   record_vars (gimple_bind_vars (stmt));
376   lower_sequence (gimple_bind_body (stmt), data);
377
378   if (new_block)
379     {
380       gcc_assert (data->block == new_block);
381
382       BLOCK_SUBBLOCKS (new_block)
383         = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
384       data->block = old_block;
385     }
386
387   /* The GIMPLE_BIND no longer carries any useful information -- kill it.  */
388   gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
389   gsi_remove (gsi, false);
390 }
391
392 /* Try to determine whether a TRY_CATCH expression can fall through.
393    This is a subroutine of block_may_fallthru.  */
394
395 static bool
396 try_catch_may_fallthru (const_tree stmt)
397 {
398   tree_stmt_iterator i;
399
400   /* If the TRY block can fall through, the whole TRY_CATCH can
401      fall through.  */
402   if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
403     return true;
404
405   i = tsi_start (TREE_OPERAND (stmt, 1));
406   switch (TREE_CODE (tsi_stmt (i)))
407     {
408     case CATCH_EXPR:
409       /* We expect to see a sequence of CATCH_EXPR trees, each with a
410          catch expression and a body.  The whole TRY_CATCH may fall
411          through iff any of the catch bodies falls through.  */
412       for (; !tsi_end_p (i); tsi_next (&i))
413         {
414           if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
415             return true;
416         }
417       return false;
418
419     case EH_FILTER_EXPR:
420       /* The exception filter expression only matters if there is an
421          exception.  If the exception does not match EH_FILTER_TYPES,
422          we will execute EH_FILTER_FAILURE, and we will fall through
423          if that falls through.  If the exception does match
424          EH_FILTER_TYPES, the stack unwinder will continue up the
425          stack, so we will not fall through.  We don't know whether we
426          will throw an exception which matches EH_FILTER_TYPES or not,
427          so we just ignore EH_FILTER_TYPES and assume that we might
428          throw an exception which doesn't match.  */
429       return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
430
431     default:
432       /* This case represents statements to be executed when an
433          exception occurs.  Those statements are implicitly followed
434          by a RESX_EXPR to resume execution after the exception.  So
435          in this case the TRY_CATCH never falls through.  */
436       return false;
437     }
438 }
439
440
441 /* Same as above, but for a GIMPLE_TRY_CATCH.  */
442
443 static bool
444 gimple_try_catch_may_fallthru (gimple stmt)
445 {
446   gimple_stmt_iterator i;
447
448   /* We don't handle GIMPLE_TRY_FINALLY.  */
449   gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
450
451   /* If the TRY block can fall through, the whole TRY_CATCH can
452      fall through.  */
453   if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
454     return true;
455
456   i = gsi_start (gimple_try_cleanup (stmt));
457   switch (gimple_code (gsi_stmt (i)))
458     {
459     case GIMPLE_CATCH:
460       /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
461          catch expression and a body.  The whole try/catch may fall
462          through iff any of the catch bodies falls through.  */
463       for (; !gsi_end_p (i); gsi_next (&i))
464         {
465           if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
466             return true;
467         }
468       return false;
469
470     case GIMPLE_EH_FILTER:
471       /* The exception filter expression only matters if there is an
472          exception.  If the exception does not match EH_FILTER_TYPES,
473          we will execute EH_FILTER_FAILURE, and we will fall through
474          if that falls through.  If the exception does match
475          EH_FILTER_TYPES, the stack unwinder will continue up the
476          stack, so we will not fall through.  We don't know whether we
477          will throw an exception which matches EH_FILTER_TYPES or not,
478          so we just ignore EH_FILTER_TYPES and assume that we might
479          throw an exception which doesn't match.  */
480       return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
481
482     default:
483       /* This case represents statements to be executed when an
484          exception occurs.  Those statements are implicitly followed
485          by a GIMPLE_RESX to resume execution after the exception.  So
486          in this case the try/catch never falls through.  */
487       return false;
488     }
489 }
490
491
492 /* Try to determine if we can fall out of the bottom of BLOCK.  This guess
493    need not be 100% accurate; simply be conservative and return true if we
494    don't know.  This is used only to avoid stupidly generating extra code.
495    If we're wrong, we'll just delete the extra code later.  */
496
497 bool
498 block_may_fallthru (const_tree block)
499 {
500   /* This CONST_CAST is okay because expr_last returns its argument
501      unmodified and we assign it to a const_tree.  */
502   const_tree stmt = expr_last (CONST_CAST_TREE(block));
503
504   switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
505     {
506     case GOTO_EXPR:
507     case RETURN_EXPR:
508     case RESX_EXPR:
509       /* Easy cases.  If the last statement of the block implies 
510          control transfer, then we can't fall through.  */
511       return false;
512
513     case SWITCH_EXPR:
514       /* If SWITCH_LABELS is set, this is lowered, and represents a
515          branch to a selected label and hence can not fall through.
516          Otherwise SWITCH_BODY is set, and the switch can fall
517          through.  */
518       return SWITCH_LABELS (stmt) == NULL_TREE;
519
520     case COND_EXPR:
521       if (block_may_fallthru (COND_EXPR_THEN (stmt)))
522         return true;
523       return block_may_fallthru (COND_EXPR_ELSE (stmt));
524
525     case BIND_EXPR:
526       return block_may_fallthru (BIND_EXPR_BODY (stmt));
527
528     case TRY_CATCH_EXPR:
529       return try_catch_may_fallthru (stmt);
530
531     case TRY_FINALLY_EXPR:
532       /* The finally clause is always executed after the try clause,
533          so if it does not fall through, then the try-finally will not
534          fall through.  Otherwise, if the try clause does not fall
535          through, then when the finally clause falls through it will
536          resume execution wherever the try clause was going.  So the
537          whole try-finally will only fall through if both the try
538          clause and the finally clause fall through.  */
539       return (block_may_fallthru (TREE_OPERAND (stmt, 0))
540               && block_may_fallthru (TREE_OPERAND (stmt, 1)));
541
542     case MODIFY_EXPR:
543       if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
544         stmt = TREE_OPERAND (stmt, 1);
545       else
546         return true;
547       /* FALLTHRU */
548
549     case CALL_EXPR:
550       /* Functions that do not return do not fall through.  */
551       return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
552     
553     case CLEANUP_POINT_EXPR:
554       return block_may_fallthru (TREE_OPERAND (stmt, 0));
555
556     default:
557       return true;
558     }
559 }
560
561
562 /* Try to determine if we can continue executing the statement
563    immediately following STMT.  This guess need not be 100% accurate;
564    simply be conservative and return true if we don't know.  This is
565    used only to avoid stupidly generating extra code. If we're wrong,
566    we'll just delete the extra code later.  */
567
568 bool
569 gimple_stmt_may_fallthru (gimple stmt)
570 {
571   if (!stmt)
572     return true;
573
574   switch (gimple_code (stmt))
575     {
576     case GIMPLE_GOTO:
577     case GIMPLE_RETURN:
578     case GIMPLE_RESX:
579       /* Easy cases.  If the last statement of the seq implies 
580          control transfer, then we can't fall through.  */
581       return false;
582
583     case GIMPLE_SWITCH:
584       /* Switch has already been lowered and represents a
585          branch to a selected label and hence can not fall through.  */
586       return true;
587
588     case GIMPLE_COND:
589       /* GIMPLE_COND's are already lowered into a two-way branch.  They
590          can't fall through.  */
591       return false;
592
593     case GIMPLE_BIND:
594       return gimple_seq_may_fallthru (gimple_bind_body (stmt));
595
596     case GIMPLE_TRY:
597       if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
598         return gimple_try_catch_may_fallthru (stmt);
599
600       /* It must be a GIMPLE_TRY_FINALLY.  */
601
602       /* The finally clause is always executed after the try clause,
603          so if it does not fall through, then the try-finally will not
604          fall through.  Otherwise, if the try clause does not fall
605          through, then when the finally clause falls through it will
606          resume execution wherever the try clause was going.  So the
607          whole try-finally will only fall through if both the try
608          clause and the finally clause fall through.  */
609       return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
610               && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
611
612     case GIMPLE_ASSIGN:
613       return true;
614
615     case GIMPLE_CALL:
616       /* Functions that do not return do not fall through.  */
617       return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
618     
619     default:
620       return true;
621     }
622 }
623
624
625 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ.  */
626
627 bool
628 gimple_seq_may_fallthru (gimple_seq seq)
629 {
630   return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
631 }
632
633
634 /* Lower a GIMPLE_RETURN GSI.  DATA is passed through the recursion.  */
635
636 static void
637 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
638 {
639   gimple stmt = gsi_stmt (*gsi);
640   gimple t;
641   int i;
642   return_statements_t tmp_rs;
643
644   /* Match this up with an existing return statement that's been created.  */
645   for (i = VEC_length (return_statements_t, data->return_statements) - 1;
646        i >= 0; i--)
647     {
648       tmp_rs = *VEC_index (return_statements_t, data->return_statements, i);
649
650       if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
651         goto found;
652     }
653
654   /* Not found.  Create a new label and record the return statement.  */
655   tmp_rs.label = create_artificial_label ();
656   tmp_rs.stmt = stmt;
657   VEC_safe_push (return_statements_t, heap, data->return_statements, &tmp_rs);
658
659   /* Generate a goto statement and remove the return statement.  */
660  found:
661   t = gimple_build_goto (tmp_rs.label);
662   gimple_set_location (t, gimple_location (stmt));
663   gimple_set_block (t, gimple_block (stmt));
664   gsi_insert_before (gsi, t, GSI_SAME_STMT);
665   gsi_remove (gsi, false);
666 }
667
668 /* Lower a __builtin_setjmp TSI.
669
670    __builtin_setjmp is passed a pointer to an array of five words (not
671    all will be used on all machines).  It operates similarly to the C
672    library function of the same name, but is more efficient.
673
674    It is lowered into 3 other builtins, namely __builtin_setjmp_setup,
675    __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with
676    __builtin_setjmp_dispatcher shared among all the instances; that's
677    why it is only emitted at the end by lower_function_body.
678
679    After full lowering, the body of the function should look like:
680
681     {
682       void * setjmpvar.0;
683       int D.1844;
684       int D.2844;
685
686       [...]
687
688       __builtin_setjmp_setup (&buf, &<D1847>);
689       D.1844 = 0;
690       goto <D1846>;
691       <D1847>:;
692       __builtin_setjmp_receiver (&<D1847>);
693       D.1844 = 1;
694       <D1846>:;
695       if (D.1844 == 0) goto <D1848>; else goto <D1849>;
696
697       [...]
698
699       __builtin_setjmp_setup (&buf, &<D2847>);
700       D.2844 = 0;
701       goto <D2846>;
702       <D2847>:;
703       __builtin_setjmp_receiver (&<D2847>);
704       D.2844 = 1;
705       <D2846>:;
706       if (D.2844 == 0) goto <D2848>; else goto <D2849>;
707
708       [...]
709
710       <D3850>:;
711       return;
712       <D3853>: [non-local];
713       setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>);
714       goto setjmpvar.0;
715     }
716
717    The dispatcher block will be both the unique destination of all the
718    abnormal call edges and the unique source of all the abnormal edges
719    to the receivers, thus keeping the complexity explosion localized.  */
720
721 static void
722 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
723 {
724   gimple stmt = gsi_stmt (*gsi);
725   tree cont_label = create_artificial_label ();
726   tree next_label = create_artificial_label ();
727   tree dest, t, arg;
728   gimple g;
729
730   /* NEXT_LABEL is the label __builtin_longjmp will jump to.  Its address is
731      passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver.  */
732   FORCED_LABEL (next_label) = 1;
733
734   dest = gimple_call_lhs (stmt);
735
736   /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert.  */
737   arg = build_addr (next_label, current_function_decl);
738   t = implicit_built_in_decls[BUILT_IN_SETJMP_SETUP];
739   g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
740   gimple_set_location (g, gimple_location (stmt));
741   gimple_set_block (g, gimple_block (stmt));
742   gsi_insert_before (gsi, g, GSI_SAME_STMT);
743
744   /* Build 'DEST = 0' and insert.  */
745   if (dest)
746     {
747       g = gimple_build_assign (dest, fold_convert (TREE_TYPE (dest),
748                                                    integer_zero_node));
749       gimple_set_location (g, gimple_location (stmt));
750       gimple_set_block (g, gimple_block (stmt));
751       gsi_insert_before (gsi, g, GSI_SAME_STMT);
752     }
753
754   /* Build 'goto CONT_LABEL' and insert.  */
755   g = gimple_build_goto (cont_label);
756   gsi_insert_before (gsi, g, TSI_SAME_STMT);
757
758   /* Build 'NEXT_LABEL:' and insert.  */
759   g = gimple_build_label (next_label);
760   gsi_insert_before (gsi, g, GSI_SAME_STMT);
761
762   /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert.  */
763   arg = build_addr (next_label, current_function_decl);
764   t = implicit_built_in_decls[BUILT_IN_SETJMP_RECEIVER];
765   g = gimple_build_call (t, 1, arg);
766   gimple_set_location (g, gimple_location (stmt));
767   gimple_set_block (g, gimple_block (stmt));
768   gsi_insert_before (gsi, g, GSI_SAME_STMT);
769
770   /* Build 'DEST = 1' and insert.  */
771   if (dest)
772     {
773       g = gimple_build_assign (dest, fold_convert (TREE_TYPE (dest),
774                                                    integer_one_node));
775       gimple_set_location (g, gimple_location (stmt));
776       gimple_set_block (g, gimple_block (stmt));
777       gsi_insert_before (gsi, g, GSI_SAME_STMT);
778     }
779
780   /* Build 'CONT_LABEL:' and insert.  */
781   g = gimple_build_label (cont_label);
782   gsi_insert_before (gsi, g, GSI_SAME_STMT);
783
784   /* Remove the call to __builtin_setjmp.  */
785   gsi_remove (gsi, false);
786 }
787 \f
788
789 /* Record the variables in VARS into function FN.  */
790
791 void
792 record_vars_into (tree vars, tree fn)
793 {
794   if (fn != current_function_decl)
795     push_cfun (DECL_STRUCT_FUNCTION (fn));
796
797   for (; vars; vars = TREE_CHAIN (vars))
798     {
799       tree var = vars;
800
801       /* BIND_EXPRs contains also function/type/constant declarations
802          we don't need to care about.  */
803       if (TREE_CODE (var) != VAR_DECL)
804         continue;
805
806       /* Nothing to do in this case.  */
807       if (DECL_EXTERNAL (var))
808         continue;
809
810       /* Record the variable.  */
811       cfun->local_decls = tree_cons (NULL_TREE, var,
812                                              cfun->local_decls);
813     }
814
815   if (fn != current_function_decl)
816     pop_cfun ();
817 }
818
819
820 /* Record the variables in VARS into current_function_decl.  */
821
822 void
823 record_vars (tree vars)
824 {
825   record_vars_into (vars, current_function_decl);
826 }
827
828
829 /* Mark BLOCK used if it has a used variable in it, then recurse over its
830    subblocks.  */
831
832 static void
833 mark_blocks_with_used_vars (tree block)
834 {
835   tree var;
836   tree subblock;
837
838   if (!TREE_USED (block))
839     {
840       for (var = BLOCK_VARS (block);
841            var;
842            var = TREE_CHAIN (var))
843         {
844           if (TREE_USED (var))
845             {
846               TREE_USED (block) = true;
847               break;
848             }
849         }
850     }
851   for (subblock = BLOCK_SUBBLOCKS (block);
852        subblock;
853        subblock = BLOCK_CHAIN (subblock))
854     mark_blocks_with_used_vars (subblock);
855 }
856
857 /* Mark the used attribute on blocks correctly.  */
858   
859 static unsigned int
860 mark_used_blocks (void)
861 {  
862   mark_blocks_with_used_vars (DECL_INITIAL (current_function_decl));
863   return 0;
864 }
865
866
867 struct gimple_opt_pass pass_mark_used_blocks = 
868 {
869  {
870   GIMPLE_PASS,
871   "blocks",                             /* name */
872   NULL,                                 /* gate */
873   mark_used_blocks,                     /* execute */
874   NULL,                                 /* sub */
875   NULL,                                 /* next */
876   0,                                    /* static_pass_number */
877   0,                                    /* tv_id */
878   0,                                    /* properties_required */
879   0,                                    /* properties_provided */
880   0,                                    /* properties_destroyed */
881   0,                                    /* todo_flags_start */
882   TODO_dump_func                        /* todo_flags_finish */
883  }
884 };