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