PR target/58115
[official-gcc.git] / gcc / gimple-low.c
blob8d2e71103a4faef30d7cd05d9730fe6b21e03980
1 /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
3 Copyright (C) 2003-2014 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 "basic-block.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "tree-iterator.h"
36 #include "tree-inline.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "diagnostic-core.h"
40 #include "tree-pass.h"
41 #include "langhooks.h"
42 #include "gimple-low.h"
43 #include "tree-nested.h"
45 /* The differences between High GIMPLE and Low GIMPLE are the
46 following:
48 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
50 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
51 flow and exception regions are built as an on-the-side region
52 hierarchy (See tree-eh.c:lower_eh_constructs).
54 3- Multiple identical return statements are grouped into a single
55 return and gotos to the unique return site. */
57 /* Match a return statement with a label. During lowering, we identify
58 identical return statements and replace duplicates with a jump to
59 the corresponding label. */
60 struct return_statements_t
62 tree label;
63 gimple stmt;
65 typedef struct return_statements_t return_statements_t;
68 struct lower_data
70 /* Block the current statement belongs to. */
71 tree block;
73 /* A vector of label and return statements to be moved to the end
74 of the function. */
75 vec<return_statements_t> return_statements;
77 /* True if the current statement cannot fall through. */
78 bool cannot_fallthru;
80 /* True if the function calls __builtin_setjmp. */
81 bool calls_builtin_setjmp;
84 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
85 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
86 static void lower_try_catch (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 *);
91 /* Lower the body of current_function_decl from High GIMPLE into Low
92 GIMPLE. */
94 static unsigned int
95 lower_function_body (void)
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;
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);
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.create (8);
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);
123 i = gsi_last (lowered_body);
125 /* If the function falls off the end, we need a null return statement.
126 If we've already got one in the return_statements vector, we don't
127 need to do anything special. Otherwise build one by hand. */
128 if (gimple_seq_may_fallthru (lowered_body)
129 && (data.return_statements.is_empty ()
130 || (gimple_return_retval (data.return_statements.last().stmt)
131 != NULL)))
133 x = gimple_build_return (NULL);
134 gimple_set_location (x, cfun->function_end_locus);
135 gimple_set_block (x, DECL_INITIAL (current_function_decl));
136 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
139 /* If we lowered any return statements, emit the representative
140 at the end of the function. */
141 while (!data.return_statements.is_empty ())
143 return_statements_t t = data.return_statements.pop ();
144 x = gimple_build_label (t.label);
145 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
146 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
149 /* If the function calls __builtin_setjmp, we need to emit the computed
150 goto that will serve as the unique dispatcher for all the receivers. */
151 if (data.calls_builtin_setjmp)
153 tree disp_label, disp_var, arg;
155 /* Build 'DISP_LABEL:' and insert. */
156 disp_label = create_artificial_label (cfun->function_end_locus);
157 /* This mark will create forward edges from every call site. */
158 DECL_NONLOCAL (disp_label) = 1;
159 cfun->has_nonlocal_label = 1;
160 x = gimple_build_label (disp_label);
161 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
163 /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);'
164 and insert. */
165 disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
166 arg = build_addr (disp_label, current_function_decl);
167 t = builtin_decl_implicit (BUILT_IN_SETJMP_DISPATCHER);
168 x = gimple_build_call (t, 1, arg);
169 gimple_call_set_lhs (x, disp_var);
171 /* Build 'goto DISP_VAR;' and insert. */
172 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
173 x = gimple_build_goto (disp_var);
174 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
177 /* Once the old body has been lowered, replace it with the new
178 lowered sequence. */
179 gimple_set_body (current_function_decl, lowered_body);
181 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
182 BLOCK_SUBBLOCKS (data.block)
183 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
185 clear_block_marks (data.block);
186 data.return_statements.release ();
187 return 0;
190 namespace {
192 const pass_data pass_data_lower_cf =
194 GIMPLE_PASS, /* type */
195 "lower", /* name */
196 OPTGROUP_NONE, /* optinfo_flags */
197 false, /* has_gate */
198 true, /* has_execute */
199 TV_NONE, /* tv_id */
200 PROP_gimple_any, /* properties_required */
201 PROP_gimple_lcf, /* properties_provided */
202 0, /* properties_destroyed */
203 0, /* todo_flags_start */
204 0, /* todo_flags_finish */
207 class pass_lower_cf : public gimple_opt_pass
209 public:
210 pass_lower_cf (gcc::context *ctxt)
211 : gimple_opt_pass (pass_data_lower_cf, ctxt)
214 /* opt_pass methods: */
215 unsigned int execute () { return lower_function_body (); }
217 }; // class pass_lower_cf
219 } // anon namespace
221 gimple_opt_pass *
222 make_pass_lower_cf (gcc::context *ctxt)
224 return new pass_lower_cf (ctxt);
227 /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
228 when they are changed -- if this has to be done, the lowering routine must
229 do it explicitly. DATA is passed through the recursion. */
231 static void
232 lower_sequence (gimple_seq *seq, struct lower_data *data)
234 gimple_stmt_iterator gsi;
236 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
237 lower_stmt (&gsi, data);
241 /* Lower the OpenMP directive statement pointed by GSI. DATA is
242 passed through the recursion. */
244 static void
245 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
247 gimple stmt;
249 stmt = gsi_stmt (*gsi);
251 lower_sequence (gimple_omp_body_ptr (stmt), data);
252 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
253 gimple_omp_set_body (stmt, NULL);
254 gsi_next (gsi);
258 /* Lower statement GSI. DATA is passed through the recursion. We try to
259 track the fallthruness of statements and get rid of unreachable return
260 statements in order to prevent the EH lowering pass from adding useless
261 edges that can cause bogus warnings to be issued later; this guess need
262 not be 100% accurate, simply be conservative and reset cannot_fallthru
263 to false if we don't know. */
265 static void
266 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
268 gimple stmt = gsi_stmt (*gsi);
270 gimple_set_block (stmt, data->block);
272 switch (gimple_code (stmt))
274 case GIMPLE_BIND:
275 lower_gimple_bind (gsi, data);
276 /* Propagate fallthruness. */
277 return;
279 case GIMPLE_COND:
280 case GIMPLE_GOTO:
281 case GIMPLE_SWITCH:
282 data->cannot_fallthru = true;
283 gsi_next (gsi);
284 return;
286 case GIMPLE_RETURN:
287 if (data->cannot_fallthru)
289 gsi_remove (gsi, false);
290 /* Propagate fallthruness. */
292 else
294 lower_gimple_return (gsi, data);
295 data->cannot_fallthru = true;
297 return;
299 case GIMPLE_TRY:
300 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
301 lower_try_catch (gsi, data);
302 else
304 /* It must be a GIMPLE_TRY_FINALLY. */
305 bool cannot_fallthru;
306 lower_sequence (gimple_try_eval_ptr (stmt), data);
307 cannot_fallthru = data->cannot_fallthru;
309 /* The finally clause is always executed after the try clause,
310 so if it does not fall through, then the try-finally will not
311 fall through. Otherwise, if the try clause does not fall
312 through, then when the finally clause falls through it will
313 resume execution wherever the try clause was going. So the
314 whole try-finally will only fall through if both the try
315 clause and the finally clause fall through. */
316 data->cannot_fallthru = false;
317 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
318 data->cannot_fallthru |= cannot_fallthru;
319 gsi_next (gsi);
321 return;
323 case GIMPLE_EH_ELSE:
324 lower_sequence (gimple_eh_else_n_body_ptr (stmt), data);
325 lower_sequence (gimple_eh_else_e_body_ptr (stmt), data);
326 break;
328 case GIMPLE_NOP:
329 case GIMPLE_ASM:
330 case GIMPLE_ASSIGN:
331 case GIMPLE_PREDICT:
332 case GIMPLE_LABEL:
333 case GIMPLE_EH_MUST_NOT_THROW:
334 case GIMPLE_OMP_FOR:
335 case GIMPLE_OMP_SECTIONS:
336 case GIMPLE_OMP_SECTIONS_SWITCH:
337 case GIMPLE_OMP_SECTION:
338 case GIMPLE_OMP_SINGLE:
339 case GIMPLE_OMP_MASTER:
340 case GIMPLE_OMP_TASKGROUP:
341 case GIMPLE_OMP_ORDERED:
342 case GIMPLE_OMP_CRITICAL:
343 case GIMPLE_OMP_RETURN:
344 case GIMPLE_OMP_ATOMIC_LOAD:
345 case GIMPLE_OMP_ATOMIC_STORE:
346 case GIMPLE_OMP_CONTINUE:
347 break;
349 case GIMPLE_CALL:
351 tree decl = gimple_call_fndecl (stmt);
352 unsigned i;
354 for (i = 0; i < gimple_call_num_args (stmt); i++)
356 tree arg = gimple_call_arg (stmt, i);
357 if (EXPR_P (arg))
358 TREE_SET_BLOCK (arg, data->block);
361 if (decl
362 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
363 && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
365 lower_builtin_setjmp (gsi);
366 data->cannot_fallthru = false;
367 data->calls_builtin_setjmp = true;
368 return;
371 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
373 data->cannot_fallthru = true;
374 gsi_next (gsi);
375 return;
378 break;
380 case GIMPLE_OMP_PARALLEL:
381 case GIMPLE_OMP_TASK:
382 case GIMPLE_OMP_TARGET:
383 case GIMPLE_OMP_TEAMS:
384 data->cannot_fallthru = false;
385 lower_omp_directive (gsi, data);
386 data->cannot_fallthru = false;
387 return;
389 case GIMPLE_TRANSACTION:
390 lower_sequence (gimple_transaction_body_ptr (stmt), data);
391 break;
393 default:
394 gcc_unreachable ();
397 data->cannot_fallthru = false;
398 gsi_next (gsi);
401 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
403 static void
404 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
406 tree old_block = data->block;
407 gimple stmt = gsi_stmt (*gsi);
408 tree new_block = gimple_bind_block (stmt);
410 if (new_block)
412 if (new_block == old_block)
414 /* The outermost block of the original function may not be the
415 outermost statement chain of the gimplified function. So we
416 may see the outermost block just inside the function. */
417 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
418 new_block = NULL;
420 else
422 /* We do not expect to handle duplicate blocks. */
423 gcc_assert (!TREE_ASM_WRITTEN (new_block));
424 TREE_ASM_WRITTEN (new_block) = 1;
426 /* Block tree may get clobbered by inlining. Normally this would
427 be fixed in rest_of_decl_compilation using block notes, but
428 since we are not going to emit them, it is up to us. */
429 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
430 BLOCK_SUBBLOCKS (old_block) = new_block;
431 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
432 BLOCK_SUPERCONTEXT (new_block) = old_block;
434 data->block = new_block;
438 record_vars (gimple_bind_vars (stmt));
439 lower_sequence (gimple_bind_body_ptr (stmt), data);
441 if (new_block)
443 gcc_assert (data->block == new_block);
445 BLOCK_SUBBLOCKS (new_block)
446 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
447 data->block = old_block;
450 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
451 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
452 gsi_remove (gsi, false);
455 /* Same as above, but for a GIMPLE_TRY_CATCH. */
457 static void
458 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
460 bool cannot_fallthru;
461 gimple stmt = gsi_stmt (*gsi);
462 gimple_stmt_iterator i;
464 /* We don't handle GIMPLE_TRY_FINALLY. */
465 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
467 lower_sequence (gimple_try_eval_ptr (stmt), data);
468 cannot_fallthru = data->cannot_fallthru;
470 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
471 switch (gimple_code (gsi_stmt (i)))
473 case GIMPLE_CATCH:
474 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
475 catch expression and a body. The whole try/catch may fall
476 through iff any of the catch bodies falls through. */
477 for (; !gsi_end_p (i); gsi_next (&i))
479 data->cannot_fallthru = false;
480 lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data);
481 if (!data->cannot_fallthru)
482 cannot_fallthru = false;
484 break;
486 case GIMPLE_EH_FILTER:
487 /* The exception filter expression only matters if there is an
488 exception. If the exception does not match EH_FILTER_TYPES,
489 we will execute EH_FILTER_FAILURE, and we will fall through
490 if that falls through. If the exception does match
491 EH_FILTER_TYPES, the stack unwinder will continue up the
492 stack, so we will not fall through. We don't know whether we
493 will throw an exception which matches EH_FILTER_TYPES or not,
494 so we just ignore EH_FILTER_TYPES and assume that we might
495 throw an exception which doesn't match. */
496 data->cannot_fallthru = false;
497 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
498 if (!data->cannot_fallthru)
499 cannot_fallthru = false;
500 break;
502 default:
503 /* This case represents statements to be executed when an
504 exception occurs. Those statements are implicitly followed
505 by a GIMPLE_RESX to resume execution after the exception. So
506 in this case the try/catch never falls through. */
507 data->cannot_fallthru = false;
508 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
509 break;
512 data->cannot_fallthru = cannot_fallthru;
513 gsi_next (gsi);
517 /* Try to determine whether a TRY_CATCH expression can fall through.
518 This is a subroutine of gimple_stmt_may_fallthru. */
520 static bool
521 gimple_try_catch_may_fallthru (gimple stmt)
523 gimple_stmt_iterator i;
525 /* We don't handle GIMPLE_TRY_FINALLY. */
526 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
528 /* If the TRY block can fall through, the whole TRY_CATCH can
529 fall through. */
530 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
531 return true;
533 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
534 switch (gimple_code (gsi_stmt (i)))
536 case GIMPLE_CATCH:
537 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
538 catch expression and a body. The whole try/catch may fall
539 through iff any of the catch bodies falls through. */
540 for (; !gsi_end_p (i); gsi_next (&i))
542 if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
543 return true;
545 return false;
547 case GIMPLE_EH_FILTER:
548 /* The exception filter expression only matters if there is an
549 exception. If the exception does not match EH_FILTER_TYPES,
550 we will execute EH_FILTER_FAILURE, and we will fall through
551 if that falls through. If the exception does match
552 EH_FILTER_TYPES, the stack unwinder will continue up the
553 stack, so we will not fall through. We don't know whether we
554 will throw an exception which matches EH_FILTER_TYPES or not,
555 so we just ignore EH_FILTER_TYPES and assume that we might
556 throw an exception which doesn't match. */
557 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
559 default:
560 /* This case represents statements to be executed when an
561 exception occurs. Those statements are implicitly followed
562 by a GIMPLE_RESX to resume execution after the exception. So
563 in this case the try/catch never falls through. */
564 return false;
569 /* Try to determine if we can continue executing the statement
570 immediately following STMT. This guess need not be 100% accurate;
571 simply be conservative and return true if we don't know. This is
572 used only to avoid stupidly generating extra code. If we're wrong,
573 we'll just delete the extra code later. */
575 bool
576 gimple_stmt_may_fallthru (gimple stmt)
578 if (!stmt)
579 return true;
581 switch (gimple_code (stmt))
583 case GIMPLE_GOTO:
584 case GIMPLE_RETURN:
585 case GIMPLE_RESX:
586 /* Easy cases. If the last statement of the seq implies
587 control transfer, then we can't fall through. */
588 return false;
590 case GIMPLE_SWITCH:
591 /* Switch has already been lowered and represents a branch
592 to a selected label and hence can't fall through. */
593 return false;
595 case GIMPLE_COND:
596 /* GIMPLE_COND's are already lowered into a two-way branch. They
597 can't fall through. */
598 return false;
600 case GIMPLE_BIND:
601 return gimple_seq_may_fallthru (gimple_bind_body (stmt));
603 case GIMPLE_TRY:
604 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
605 return gimple_try_catch_may_fallthru (stmt);
607 /* It must be a GIMPLE_TRY_FINALLY. */
609 /* The finally clause is always executed after the try clause,
610 so if it does not fall through, then the try-finally will not
611 fall through. Otherwise, if the try clause does not fall
612 through, then when the finally clause falls through it will
613 resume execution wherever the try clause was going. So the
614 whole try-finally will only fall through if both the try
615 clause and the finally clause fall through. */
616 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
617 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
619 case GIMPLE_EH_ELSE:
620 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
621 || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
623 case GIMPLE_CALL:
624 /* Functions that do not return do not fall through. */
625 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
627 default:
628 return true;
633 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
635 bool
636 gimple_seq_may_fallthru (gimple_seq seq)
638 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
642 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
644 static void
645 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
647 gimple stmt = gsi_stmt (*gsi);
648 gimple t;
649 int i;
650 return_statements_t tmp_rs;
652 /* Match this up with an existing return statement that's been created. */
653 for (i = data->return_statements.length () - 1;
654 i >= 0; i--)
656 tmp_rs = data->return_statements[i];
658 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
660 /* Remove the line number from the representative return statement.
661 It now fills in for many such returns. Failure to remove this
662 will result in incorrect results for coverage analysis. */
663 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
665 goto found;
669 /* Not found. Create a new label and record the return statement. */
670 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
671 tmp_rs.stmt = stmt;
672 data->return_statements.safe_push (tmp_rs);
674 /* Generate a goto statement and remove the return statement. */
675 found:
676 /* When not optimizing, make sure user returns are preserved. */
677 if (!optimize && gimple_has_location (stmt))
678 DECL_ARTIFICIAL (tmp_rs.label) = 0;
679 t = gimple_build_goto (tmp_rs.label);
680 gimple_set_location (t, gimple_location (stmt));
681 gimple_set_block (t, gimple_block (stmt));
682 gsi_insert_before (gsi, t, GSI_SAME_STMT);
683 gsi_remove (gsi, false);
686 /* Lower a __builtin_setjmp GSI.
688 __builtin_setjmp is passed a pointer to an array of five words (not
689 all will be used on all machines). It operates similarly to the C
690 library function of the same name, but is more efficient.
692 It is lowered into 3 other builtins, namely __builtin_setjmp_setup,
693 __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with
694 __builtin_setjmp_dispatcher shared among all the instances; that's
695 why it is only emitted at the end by lower_function_body.
697 After full lowering, the body of the function should look like:
700 void * setjmpvar.0;
701 int D.1844;
702 int D.2844;
704 [...]
706 __builtin_setjmp_setup (&buf, &<D1847>);
707 D.1844 = 0;
708 goto <D1846>;
709 <D1847>:;
710 __builtin_setjmp_receiver (&<D1847>);
711 D.1844 = 1;
712 <D1846>:;
713 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
715 [...]
717 __builtin_setjmp_setup (&buf, &<D2847>);
718 D.2844 = 0;
719 goto <D2846>;
720 <D2847>:;
721 __builtin_setjmp_receiver (&<D2847>);
722 D.2844 = 1;
723 <D2846>:;
724 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
726 [...]
728 <D3850>:;
729 return;
730 <D3853>: [non-local];
731 setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>);
732 goto setjmpvar.0;
735 The dispatcher block will be both the unique destination of all the
736 abnormal call edges and the unique source of all the abnormal edges
737 to the receivers, thus keeping the complexity explosion localized. */
739 static void
740 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
742 gimple stmt = gsi_stmt (*gsi);
743 location_t loc = gimple_location (stmt);
744 tree cont_label = create_artificial_label (loc);
745 tree next_label = create_artificial_label (loc);
746 tree dest, t, arg;
747 gimple g;
749 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
750 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
751 FORCED_LABEL (next_label) = 1;
753 dest = gimple_call_lhs (stmt);
755 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
756 arg = build_addr (next_label, current_function_decl);
757 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
758 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
759 gimple_set_location (g, loc);
760 gimple_set_block (g, gimple_block (stmt));
761 gsi_insert_before (gsi, g, GSI_SAME_STMT);
763 /* Build 'DEST = 0' and insert. */
764 if (dest)
766 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
767 gimple_set_location (g, loc);
768 gimple_set_block (g, gimple_block (stmt));
769 gsi_insert_before (gsi, g, GSI_SAME_STMT);
772 /* Build 'goto CONT_LABEL' and insert. */
773 g = gimple_build_goto (cont_label);
774 gsi_insert_before (gsi, g, GSI_SAME_STMT);
776 /* Build 'NEXT_LABEL:' and insert. */
777 g = gimple_build_label (next_label);
778 gsi_insert_before (gsi, g, GSI_SAME_STMT);
780 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
781 arg = build_addr (next_label, current_function_decl);
782 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
783 g = gimple_build_call (t, 1, arg);
784 gimple_set_location (g, loc);
785 gimple_set_block (g, gimple_block (stmt));
786 gsi_insert_before (gsi, g, GSI_SAME_STMT);
788 /* Build 'DEST = 1' and insert. */
789 if (dest)
791 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
792 integer_one_node));
793 gimple_set_location (g, loc);
794 gimple_set_block (g, gimple_block (stmt));
795 gsi_insert_before (gsi, g, GSI_SAME_STMT);
798 /* Build 'CONT_LABEL:' and insert. */
799 g = gimple_build_label (cont_label);
800 gsi_insert_before (gsi, g, GSI_SAME_STMT);
802 /* Remove the call to __builtin_setjmp. */
803 gsi_remove (gsi, false);
807 /* Record the variables in VARS into function FN. */
809 void
810 record_vars_into (tree vars, tree fn)
812 bool change_cfun = fn != current_function_decl;
814 if (change_cfun)
815 push_cfun (DECL_STRUCT_FUNCTION (fn));
817 for (; vars; vars = DECL_CHAIN (vars))
819 tree var = vars;
821 /* BIND_EXPRs contains also function/type/constant declarations
822 we don't need to care about. */
823 if (TREE_CODE (var) != VAR_DECL)
824 continue;
826 /* Nothing to do in this case. */
827 if (DECL_EXTERNAL (var))
828 continue;
830 /* Record the variable. */
831 add_local_decl (cfun, var);
834 if (change_cfun)
835 pop_cfun ();
839 /* Record the variables in VARS into current_function_decl. */
841 void
842 record_vars (tree vars)
844 record_vars_into (vars, current_function_decl);