2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / gimple-low.c
blob71f8dfec3f807c438a11f4e38b9c1866bfba2dbe
1 /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
3 Copyright (C) 2003-2013 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tree-nested.h"
27 #include "calls.h"
28 #include "gimple.h"
29 #include "gimple-iterator.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "diagnostic-core.h"
35 #include "tree-pass.h"
36 #include "langhooks.h"
37 #include "gimple-low.h"
38 #include "tree-nested.h"
40 /* The differences between High GIMPLE and Low GIMPLE are the
41 following:
43 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
45 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
46 flow and exception regions are built as an on-the-side region
47 hierarchy (See tree-eh.c:lower_eh_constructs).
49 3- Multiple identical return statements are grouped into a single
50 return and gotos to the unique return site. */
52 /* Match a return statement with a label. During lowering, we identify
53 identical return statements and replace duplicates with a jump to
54 the corresponding label. */
55 struct return_statements_t
57 tree label;
58 gimple stmt;
60 typedef struct return_statements_t return_statements_t;
63 struct lower_data
65 /* Block the current statement belongs to. */
66 tree block;
68 /* A vector of label and return statements to be moved to the end
69 of the function. */
70 vec<return_statements_t> return_statements;
72 /* True if the current statement cannot fall through. */
73 bool cannot_fallthru;
75 /* True if the function calls __builtin_setjmp. */
76 bool calls_builtin_setjmp;
79 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
80 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
81 static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
82 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
83 static void lower_builtin_setjmp (gimple_stmt_iterator *);
86 /* Lower the body of current_function_decl from High GIMPLE into Low
87 GIMPLE. */
89 static unsigned int
90 lower_function_body (void)
92 struct lower_data data;
93 gimple_seq body = gimple_body (current_function_decl);
94 gimple_seq lowered_body;
95 gimple_stmt_iterator i;
96 gimple bind;
97 tree t;
98 gimple x;
100 /* The gimplifier should've left a body of exactly one statement,
101 namely a GIMPLE_BIND. */
102 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
103 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
105 memset (&data, 0, sizeof (data));
106 data.block = DECL_INITIAL (current_function_decl);
107 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
108 BLOCK_CHAIN (data.block) = NULL_TREE;
109 TREE_ASM_WRITTEN (data.block) = 1;
110 data.return_statements.create (8);
112 bind = gimple_seq_first_stmt (body);
113 lowered_body = NULL;
114 gimple_seq_add_stmt (&lowered_body, bind);
115 i = gsi_start (lowered_body);
116 lower_gimple_bind (&i, &data);
118 i = gsi_last (lowered_body);
120 /* If the function falls off the end, we need a null return statement.
121 If we've already got one in the return_statements vector, we don't
122 need to do anything special. Otherwise build one by hand. */
123 if (gimple_seq_may_fallthru (lowered_body)
124 && (data.return_statements.is_empty ()
125 || (gimple_return_retval (data.return_statements.last().stmt)
126 != NULL)))
128 x = gimple_build_return (NULL);
129 gimple_set_location (x, cfun->function_end_locus);
130 gimple_set_block (x, DECL_INITIAL (current_function_decl));
131 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
134 /* If we lowered any return statements, emit the representative
135 at the end of the function. */
136 while (!data.return_statements.is_empty ())
138 return_statements_t t = data.return_statements.pop ();
139 x = gimple_build_label (t.label);
140 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
141 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
144 /* If the function calls __builtin_setjmp, we need to emit the computed
145 goto that will serve as the unique dispatcher for all the receivers. */
146 if (data.calls_builtin_setjmp)
148 tree disp_label, disp_var, arg;
150 /* Build 'DISP_LABEL:' and insert. */
151 disp_label = create_artificial_label (cfun->function_end_locus);
152 /* This mark will create forward edges from every call site. */
153 DECL_NONLOCAL (disp_label) = 1;
154 cfun->has_nonlocal_label = 1;
155 x = gimple_build_label (disp_label);
156 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
158 /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);'
159 and insert. */
160 disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
161 arg = build_addr (disp_label, current_function_decl);
162 t = builtin_decl_implicit (BUILT_IN_SETJMP_DISPATCHER);
163 x = gimple_build_call (t, 1, arg);
164 gimple_call_set_lhs (x, disp_var);
166 /* Build 'goto DISP_VAR;' and insert. */
167 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
168 x = gimple_build_goto (disp_var);
169 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
172 /* Once the old body has been lowered, replace it with the new
173 lowered sequence. */
174 gimple_set_body (current_function_decl, lowered_body);
176 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
177 BLOCK_SUBBLOCKS (data.block)
178 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
180 clear_block_marks (data.block);
181 data.return_statements.release ();
182 return 0;
185 namespace {
187 const pass_data pass_data_lower_cf =
189 GIMPLE_PASS, /* type */
190 "lower", /* name */
191 OPTGROUP_NONE, /* optinfo_flags */
192 false, /* has_gate */
193 true, /* has_execute */
194 TV_NONE, /* tv_id */
195 PROP_gimple_any, /* properties_required */
196 PROP_gimple_lcf, /* properties_provided */
197 0, /* properties_destroyed */
198 0, /* todo_flags_start */
199 0, /* todo_flags_finish */
202 class pass_lower_cf : public gimple_opt_pass
204 public:
205 pass_lower_cf (gcc::context *ctxt)
206 : gimple_opt_pass (pass_data_lower_cf, ctxt)
209 /* opt_pass methods: */
210 unsigned int execute () { return lower_function_body (); }
212 }; // class pass_lower_cf
214 } // anon namespace
216 gimple_opt_pass *
217 make_pass_lower_cf (gcc::context *ctxt)
219 return new pass_lower_cf (ctxt);
222 /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
223 when they are changed -- if this has to be done, the lowering routine must
224 do it explicitly. DATA is passed through the recursion. */
226 static void
227 lower_sequence (gimple_seq *seq, struct lower_data *data)
229 gimple_stmt_iterator gsi;
231 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
232 lower_stmt (&gsi, data);
236 /* Lower the OpenMP directive statement pointed by GSI. DATA is
237 passed through the recursion. */
239 static void
240 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
242 gimple stmt;
244 stmt = gsi_stmt (*gsi);
246 lower_sequence (gimple_omp_body_ptr (stmt), data);
247 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
248 gimple_omp_set_body (stmt, NULL);
249 gsi_next (gsi);
253 /* Lower statement GSI. DATA is passed through the recursion. We try to
254 track the fallthruness of statements and get rid of unreachable return
255 statements in order to prevent the EH lowering pass from adding useless
256 edges that can cause bogus warnings to be issued later; this guess need
257 not be 100% accurate, simply be conservative and reset cannot_fallthru
258 to false if we don't know. */
260 static void
261 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
263 gimple stmt = gsi_stmt (*gsi);
265 gimple_set_block (stmt, data->block);
267 switch (gimple_code (stmt))
269 case GIMPLE_BIND:
270 lower_gimple_bind (gsi, data);
271 /* Propagate fallthruness. */
272 return;
274 case GIMPLE_COND:
275 case GIMPLE_GOTO:
276 case GIMPLE_SWITCH:
277 data->cannot_fallthru = true;
278 gsi_next (gsi);
279 return;
281 case GIMPLE_RETURN:
282 if (data->cannot_fallthru)
284 gsi_remove (gsi, false);
285 /* Propagate fallthruness. */
287 else
289 lower_gimple_return (gsi, data);
290 data->cannot_fallthru = true;
292 return;
294 case GIMPLE_TRY:
295 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
296 lower_try_catch (gsi, data);
297 else
299 /* It must be a GIMPLE_TRY_FINALLY. */
300 bool cannot_fallthru;
301 lower_sequence (gimple_try_eval_ptr (stmt), data);
302 cannot_fallthru = data->cannot_fallthru;
304 /* The finally clause is always executed after the try clause,
305 so if it does not fall through, then the try-finally will not
306 fall through. Otherwise, if the try clause does not fall
307 through, then when the finally clause falls through it will
308 resume execution wherever the try clause was going. So the
309 whole try-finally will only fall through if both the try
310 clause and the finally clause fall through. */
311 data->cannot_fallthru = false;
312 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
313 data->cannot_fallthru |= cannot_fallthru;
314 gsi_next (gsi);
316 return;
318 case GIMPLE_EH_ELSE:
319 lower_sequence (gimple_eh_else_n_body_ptr (stmt), data);
320 lower_sequence (gimple_eh_else_e_body_ptr (stmt), data);
321 break;
323 case GIMPLE_NOP:
324 case GIMPLE_ASM:
325 case GIMPLE_ASSIGN:
326 case GIMPLE_PREDICT:
327 case GIMPLE_LABEL:
328 case GIMPLE_EH_MUST_NOT_THROW:
329 case GIMPLE_OMP_FOR:
330 case GIMPLE_OMP_SECTIONS:
331 case GIMPLE_OMP_SECTIONS_SWITCH:
332 case GIMPLE_OMP_SECTION:
333 case GIMPLE_OMP_SINGLE:
334 case GIMPLE_OMP_MASTER:
335 case GIMPLE_OMP_TASKGROUP:
336 case GIMPLE_OMP_ORDERED:
337 case GIMPLE_OMP_CRITICAL:
338 case GIMPLE_OMP_RETURN:
339 case GIMPLE_OMP_ATOMIC_LOAD:
340 case GIMPLE_OMP_ATOMIC_STORE:
341 case GIMPLE_OMP_CONTINUE:
342 break;
344 case GIMPLE_CALL:
346 tree decl = gimple_call_fndecl (stmt);
347 unsigned i;
349 for (i = 0; i < gimple_call_num_args (stmt); i++)
351 tree arg = gimple_call_arg (stmt, i);
352 if (EXPR_P (arg))
353 TREE_SET_BLOCK (arg, data->block);
356 if (decl
357 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
358 && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
360 lower_builtin_setjmp (gsi);
361 data->cannot_fallthru = false;
362 data->calls_builtin_setjmp = true;
363 return;
366 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
368 data->cannot_fallthru = true;
369 gsi_next (gsi);
370 return;
373 break;
375 case GIMPLE_OMP_PARALLEL:
376 case GIMPLE_OMP_TASK:
377 case GIMPLE_OMP_TARGET:
378 case GIMPLE_OMP_TEAMS:
379 data->cannot_fallthru = false;
380 lower_omp_directive (gsi, data);
381 data->cannot_fallthru = false;
382 return;
384 case GIMPLE_TRANSACTION:
385 lower_sequence (gimple_transaction_body_ptr (stmt), data);
386 break;
388 default:
389 gcc_unreachable ();
392 data->cannot_fallthru = false;
393 gsi_next (gsi);
396 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
398 static void
399 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
401 tree old_block = data->block;
402 gimple stmt = gsi_stmt (*gsi);
403 tree new_block = gimple_bind_block (stmt);
405 if (new_block)
407 if (new_block == old_block)
409 /* The outermost block of the original function may not be the
410 outermost statement chain of the gimplified function. So we
411 may see the outermost block just inside the function. */
412 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
413 new_block = NULL;
415 else
417 /* We do not expect to handle duplicate blocks. */
418 gcc_assert (!TREE_ASM_WRITTEN (new_block));
419 TREE_ASM_WRITTEN (new_block) = 1;
421 /* Block tree may get clobbered by inlining. Normally this would
422 be fixed in rest_of_decl_compilation using block notes, but
423 since we are not going to emit them, it is up to us. */
424 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
425 BLOCK_SUBBLOCKS (old_block) = new_block;
426 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
427 BLOCK_SUPERCONTEXT (new_block) = old_block;
429 data->block = new_block;
433 record_vars (gimple_bind_vars (stmt));
434 lower_sequence (gimple_bind_body_ptr (stmt), data);
436 if (new_block)
438 gcc_assert (data->block == new_block);
440 BLOCK_SUBBLOCKS (new_block)
441 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
442 data->block = old_block;
445 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
446 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
447 gsi_remove (gsi, false);
450 /* Same as above, but for a GIMPLE_TRY_CATCH. */
452 static void
453 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
455 bool cannot_fallthru;
456 gimple stmt = gsi_stmt (*gsi);
457 gimple_stmt_iterator i;
459 /* We don't handle GIMPLE_TRY_FINALLY. */
460 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
462 lower_sequence (gimple_try_eval_ptr (stmt), data);
463 cannot_fallthru = data->cannot_fallthru;
465 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
466 switch (gimple_code (gsi_stmt (i)))
468 case GIMPLE_CATCH:
469 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
470 catch expression and a body. The whole try/catch may fall
471 through iff any of the catch bodies falls through. */
472 for (; !gsi_end_p (i); gsi_next (&i))
474 data->cannot_fallthru = false;
475 lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data);
476 if (!data->cannot_fallthru)
477 cannot_fallthru = false;
479 break;
481 case GIMPLE_EH_FILTER:
482 /* The exception filter expression only matters if there is an
483 exception. If the exception does not match EH_FILTER_TYPES,
484 we will execute EH_FILTER_FAILURE, and we will fall through
485 if that falls through. If the exception does match
486 EH_FILTER_TYPES, the stack unwinder will continue up the
487 stack, so we will not fall through. We don't know whether we
488 will throw an exception which matches EH_FILTER_TYPES or not,
489 so we just ignore EH_FILTER_TYPES and assume that we might
490 throw an exception which doesn't match. */
491 data->cannot_fallthru = false;
492 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
493 if (!data->cannot_fallthru)
494 cannot_fallthru = false;
495 break;
497 default:
498 /* This case represents statements to be executed when an
499 exception occurs. Those statements are implicitly followed
500 by a GIMPLE_RESX to resume execution after the exception. So
501 in this case the try/catch never falls through. */
502 data->cannot_fallthru = false;
503 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
504 break;
507 data->cannot_fallthru = cannot_fallthru;
508 gsi_next (gsi);
512 /* Try to determine whether a TRY_CATCH expression can fall through.
513 This is a subroutine of gimple_stmt_may_fallthru. */
515 static bool
516 gimple_try_catch_may_fallthru (gimple stmt)
518 gimple_stmt_iterator i;
520 /* We don't handle GIMPLE_TRY_FINALLY. */
521 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
523 /* If the TRY block can fall through, the whole TRY_CATCH can
524 fall through. */
525 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
526 return true;
528 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
529 switch (gimple_code (gsi_stmt (i)))
531 case GIMPLE_CATCH:
532 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
533 catch expression and a body. The whole try/catch may fall
534 through iff any of the catch bodies falls through. */
535 for (; !gsi_end_p (i); gsi_next (&i))
537 if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
538 return true;
540 return false;
542 case GIMPLE_EH_FILTER:
543 /* The exception filter expression only matters if there is an
544 exception. If the exception does not match EH_FILTER_TYPES,
545 we will execute EH_FILTER_FAILURE, and we will fall through
546 if that falls through. If the exception does match
547 EH_FILTER_TYPES, the stack unwinder will continue up the
548 stack, so we will not fall through. We don't know whether we
549 will throw an exception which matches EH_FILTER_TYPES or not,
550 so we just ignore EH_FILTER_TYPES and assume that we might
551 throw an exception which doesn't match. */
552 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
554 default:
555 /* This case represents statements to be executed when an
556 exception occurs. Those statements are implicitly followed
557 by a GIMPLE_RESX to resume execution after the exception. So
558 in this case the try/catch never falls through. */
559 return false;
564 /* Try to determine if we can continue executing the statement
565 immediately following STMT. This guess need not be 100% accurate;
566 simply be conservative and return true if we don't know. This is
567 used only to avoid stupidly generating extra code. If we're wrong,
568 we'll just delete the extra code later. */
570 bool
571 gimple_stmt_may_fallthru (gimple stmt)
573 if (!stmt)
574 return true;
576 switch (gimple_code (stmt))
578 case GIMPLE_GOTO:
579 case GIMPLE_RETURN:
580 case GIMPLE_RESX:
581 /* Easy cases. If the last statement of the seq implies
582 control transfer, then we can't fall through. */
583 return false;
585 case GIMPLE_SWITCH:
586 /* Switch has already been lowered and represents a branch
587 to a selected label and hence can't fall through. */
588 return false;
590 case GIMPLE_COND:
591 /* GIMPLE_COND's are already lowered into a two-way branch. They
592 can't fall through. */
593 return false;
595 case GIMPLE_BIND:
596 return gimple_seq_may_fallthru (gimple_bind_body (stmt));
598 case GIMPLE_TRY:
599 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
600 return gimple_try_catch_may_fallthru (stmt);
602 /* It must be a GIMPLE_TRY_FINALLY. */
604 /* The finally clause is always executed after the try clause,
605 so if it does not fall through, then the try-finally will not
606 fall through. Otherwise, if the try clause does not fall
607 through, then when the finally clause falls through it will
608 resume execution wherever the try clause was going. So the
609 whole try-finally will only fall through if both the try
610 clause and the finally clause fall through. */
611 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
612 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
614 case GIMPLE_EH_ELSE:
615 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
616 || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
618 case GIMPLE_CALL:
619 /* Functions that do not return do not fall through. */
620 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
622 default:
623 return true;
628 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
630 bool
631 gimple_seq_may_fallthru (gimple_seq seq)
633 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
637 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
639 static void
640 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
642 gimple stmt = gsi_stmt (*gsi);
643 gimple t;
644 int i;
645 return_statements_t tmp_rs;
647 /* Match this up with an existing return statement that's been created. */
648 for (i = data->return_statements.length () - 1;
649 i >= 0; i--)
651 tmp_rs = data->return_statements[i];
653 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
655 /* Remove the line number from the representative return statement.
656 It now fills in for many such returns. Failure to remove this
657 will result in incorrect results for coverage analysis. */
658 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
660 goto found;
664 /* Not found. Create a new label and record the return statement. */
665 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
666 tmp_rs.stmt = stmt;
667 data->return_statements.safe_push (tmp_rs);
669 /* Generate a goto statement and remove the return statement. */
670 found:
671 /* When not optimizing, make sure user returns are preserved. */
672 if (!optimize && gimple_has_location (stmt))
673 DECL_ARTIFICIAL (tmp_rs.label) = 0;
674 t = gimple_build_goto (tmp_rs.label);
675 gimple_set_location (t, gimple_location (stmt));
676 gimple_set_block (t, gimple_block (stmt));
677 gsi_insert_before (gsi, t, GSI_SAME_STMT);
678 gsi_remove (gsi, false);
681 /* Lower a __builtin_setjmp GSI.
683 __builtin_setjmp is passed a pointer to an array of five words (not
684 all will be used on all machines). It operates similarly to the C
685 library function of the same name, but is more efficient.
687 It is lowered into 3 other builtins, namely __builtin_setjmp_setup,
688 __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with
689 __builtin_setjmp_dispatcher shared among all the instances; that's
690 why it is only emitted at the end by lower_function_body.
692 After full lowering, the body of the function should look like:
695 void * setjmpvar.0;
696 int D.1844;
697 int D.2844;
699 [...]
701 __builtin_setjmp_setup (&buf, &<D1847>);
702 D.1844 = 0;
703 goto <D1846>;
704 <D1847>:;
705 __builtin_setjmp_receiver (&<D1847>);
706 D.1844 = 1;
707 <D1846>:;
708 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
710 [...]
712 __builtin_setjmp_setup (&buf, &<D2847>);
713 D.2844 = 0;
714 goto <D2846>;
715 <D2847>:;
716 __builtin_setjmp_receiver (&<D2847>);
717 D.2844 = 1;
718 <D2846>:;
719 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
721 [...]
723 <D3850>:;
724 return;
725 <D3853>: [non-local];
726 setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>);
727 goto setjmpvar.0;
730 The dispatcher block will be both the unique destination of all the
731 abnormal call edges and the unique source of all the abnormal edges
732 to the receivers, thus keeping the complexity explosion localized. */
734 static void
735 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
737 gimple stmt = gsi_stmt (*gsi);
738 location_t loc = gimple_location (stmt);
739 tree cont_label = create_artificial_label (loc);
740 tree next_label = create_artificial_label (loc);
741 tree dest, t, arg;
742 gimple g;
744 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
745 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
746 FORCED_LABEL (next_label) = 1;
748 dest = gimple_call_lhs (stmt);
750 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
751 arg = build_addr (next_label, current_function_decl);
752 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
753 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
754 gimple_set_location (g, loc);
755 gimple_set_block (g, gimple_block (stmt));
756 gsi_insert_before (gsi, g, GSI_SAME_STMT);
758 /* Build 'DEST = 0' and insert. */
759 if (dest)
761 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
762 gimple_set_location (g, loc);
763 gimple_set_block (g, gimple_block (stmt));
764 gsi_insert_before (gsi, g, GSI_SAME_STMT);
767 /* Build 'goto CONT_LABEL' and insert. */
768 g = gimple_build_goto (cont_label);
769 gsi_insert_before (gsi, g, GSI_SAME_STMT);
771 /* Build 'NEXT_LABEL:' and insert. */
772 g = gimple_build_label (next_label);
773 gsi_insert_before (gsi, g, GSI_SAME_STMT);
775 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
776 arg = build_addr (next_label, current_function_decl);
777 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
778 g = gimple_build_call (t, 1, arg);
779 gimple_set_location (g, loc);
780 gimple_set_block (g, gimple_block (stmt));
781 gsi_insert_before (gsi, g, GSI_SAME_STMT);
783 /* Build 'DEST = 1' and insert. */
784 if (dest)
786 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
787 integer_one_node));
788 gimple_set_location (g, loc);
789 gimple_set_block (g, gimple_block (stmt));
790 gsi_insert_before (gsi, g, GSI_SAME_STMT);
793 /* Build 'CONT_LABEL:' and insert. */
794 g = gimple_build_label (cont_label);
795 gsi_insert_before (gsi, g, GSI_SAME_STMT);
797 /* Remove the call to __builtin_setjmp. */
798 gsi_remove (gsi, false);
802 /* Record the variables in VARS into function FN. */
804 void
805 record_vars_into (tree vars, tree fn)
807 bool change_cfun = fn != current_function_decl;
809 if (change_cfun)
810 push_cfun (DECL_STRUCT_FUNCTION (fn));
812 for (; vars; vars = DECL_CHAIN (vars))
814 tree var = vars;
816 /* BIND_EXPRs contains also function/type/constant declarations
817 we don't need to care about. */
818 if (TREE_CODE (var) != VAR_DECL)
819 continue;
821 /* Nothing to do in this case. */
822 if (DECL_EXTERNAL (var))
823 continue;
825 /* Record the variable. */
826 add_local_decl (cfun, var);
829 if (change_cfun)
830 pop_cfun ();
834 /* Record the variables in VARS into current_function_decl. */
836 void
837 record_vars (tree vars)
839 record_vars_into (vars, current_function_decl);