2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / gimple-low.c
blob7ae1330eafeb2e6ddf7bb5da0d9b3fb6a7273289
1 /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
3 Copyright (C) 2003-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "tree-nested.h"
37 #include "calls.h"
38 #include "predict.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "tree-iterator.h"
50 #include "tree-inline.h"
51 #include "flags.h"
52 #include "diagnostic-core.h"
53 #include "tree-pass.h"
54 #include "langhooks.h"
55 #include "gimple-low.h"
56 #include "tree-nested.h"
58 /* The differences between High GIMPLE and Low GIMPLE are the
59 following:
61 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
63 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
64 flow and exception regions are built as an on-the-side region
65 hierarchy (See tree-eh.c:lower_eh_constructs).
67 3- Multiple identical return statements are grouped into a single
68 return and gotos to the unique return site. */
70 /* Match a return statement with a label. During lowering, we identify
71 identical return statements and replace duplicates with a jump to
72 the corresponding label. */
73 struct return_statements_t
75 tree label;
76 greturn *stmt;
78 typedef struct return_statements_t return_statements_t;
81 struct lower_data
83 /* Block the current statement belongs to. */
84 tree block;
86 /* A vector of label and return statements to be moved to the end
87 of the function. */
88 vec<return_statements_t> return_statements;
90 /* True if the current statement cannot fall through. */
91 bool cannot_fallthru;
94 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
95 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
96 static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
97 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
98 static void lower_builtin_setjmp (gimple_stmt_iterator *);
99 static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
102 /* Lower the body of current_function_decl from High GIMPLE into Low
103 GIMPLE. */
105 static unsigned int
106 lower_function_body (void)
108 struct lower_data data;
109 gimple_seq body = gimple_body (current_function_decl);
110 gimple_seq lowered_body;
111 gimple_stmt_iterator i;
112 gimple bind;
113 gimple x;
115 /* The gimplifier should've left a body of exactly one statement,
116 namely a GIMPLE_BIND. */
117 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
118 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
120 memset (&data, 0, sizeof (data));
121 data.block = DECL_INITIAL (current_function_decl);
122 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
123 BLOCK_CHAIN (data.block) = NULL_TREE;
124 TREE_ASM_WRITTEN (data.block) = 1;
125 data.return_statements.create (8);
127 bind = gimple_seq_first_stmt (body);
128 lowered_body = NULL;
129 gimple_seq_add_stmt (&lowered_body, bind);
130 i = gsi_start (lowered_body);
131 lower_gimple_bind (&i, &data);
133 i = gsi_last (lowered_body);
135 /* If the function falls off the end, we need a null return statement.
136 If we've already got one in the return_statements vector, we don't
137 need to do anything special. Otherwise build one by hand. */
138 bool may_fallthru = gimple_seq_may_fallthru (lowered_body);
139 if (may_fallthru
140 && (data.return_statements.is_empty ()
141 || (gimple_return_retval (data.return_statements.last().stmt)
142 != NULL)))
144 x = gimple_build_return (NULL);
145 gimple_set_location (x, cfun->function_end_locus);
146 gimple_set_block (x, DECL_INITIAL (current_function_decl));
147 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
148 may_fallthru = false;
151 /* If we lowered any return statements, emit the representative
152 at the end of the function. */
153 while (!data.return_statements.is_empty ())
155 return_statements_t t = data.return_statements.pop ();
156 x = gimple_build_label (t.label);
157 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
158 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
159 if (may_fallthru)
161 /* Remove the line number from the representative return statement.
162 It now fills in for the fallthru too. Failure to remove this
163 will result in incorrect results for coverage analysis. */
164 gimple_set_location (t.stmt, UNKNOWN_LOCATION);
165 may_fallthru = false;
169 /* Once the old body has been lowered, replace it with the new
170 lowered sequence. */
171 gimple_set_body (current_function_decl, lowered_body);
173 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
174 BLOCK_SUBBLOCKS (data.block)
175 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
177 clear_block_marks (data.block);
178 data.return_statements.release ();
179 return 0;
182 namespace {
184 const pass_data pass_data_lower_cf =
186 GIMPLE_PASS, /* type */
187 "lower", /* name */
188 OPTGROUP_NONE, /* optinfo_flags */
189 TV_NONE, /* tv_id */
190 PROP_gimple_any, /* properties_required */
191 PROP_gimple_lcf, /* properties_provided */
192 0, /* properties_destroyed */
193 0, /* todo_flags_start */
194 0, /* todo_flags_finish */
197 class pass_lower_cf : public gimple_opt_pass
199 public:
200 pass_lower_cf (gcc::context *ctxt)
201 : gimple_opt_pass (pass_data_lower_cf, ctxt)
204 /* opt_pass methods: */
205 virtual unsigned int execute (function *) { return lower_function_body (); }
207 }; // class pass_lower_cf
209 } // anon namespace
211 gimple_opt_pass *
212 make_pass_lower_cf (gcc::context *ctxt)
214 return new pass_lower_cf (ctxt);
217 /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
218 when they are changed -- if this has to be done, the lowering routine must
219 do it explicitly. DATA is passed through the recursion. */
221 static void
222 lower_sequence (gimple_seq *seq, struct lower_data *data)
224 gimple_stmt_iterator gsi;
226 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
227 lower_stmt (&gsi, data);
231 /* Lower the OpenMP directive statement pointed by GSI. DATA is
232 passed through the recursion. */
234 static void
235 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
237 gimple stmt;
239 stmt = gsi_stmt (*gsi);
241 lower_sequence (gimple_omp_body_ptr (stmt), data);
242 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
243 gimple_omp_set_body (stmt, NULL);
244 gsi_next (gsi);
248 /* Lower statement GSI. DATA is passed through the recursion. We try to
249 track the fallthruness of statements and get rid of unreachable return
250 statements in order to prevent the EH lowering pass from adding useless
251 edges that can cause bogus warnings to be issued later; this guess need
252 not be 100% accurate, simply be conservative and reset cannot_fallthru
253 to false if we don't know. */
255 static void
256 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
258 gimple stmt = gsi_stmt (*gsi);
260 gimple_set_block (stmt, data->block);
262 switch (gimple_code (stmt))
264 case GIMPLE_BIND:
265 lower_gimple_bind (gsi, data);
266 /* Propagate fallthruness. */
267 return;
269 case GIMPLE_COND:
270 case GIMPLE_GOTO:
271 case GIMPLE_SWITCH:
272 data->cannot_fallthru = true;
273 gsi_next (gsi);
274 return;
276 case GIMPLE_RETURN:
277 if (data->cannot_fallthru)
279 gsi_remove (gsi, false);
280 /* Propagate fallthruness. */
282 else
284 lower_gimple_return (gsi, data);
285 data->cannot_fallthru = true;
287 return;
289 case GIMPLE_TRY:
290 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
291 lower_try_catch (gsi, data);
292 else
294 /* It must be a GIMPLE_TRY_FINALLY. */
295 bool cannot_fallthru;
296 lower_sequence (gimple_try_eval_ptr (stmt), data);
297 cannot_fallthru = data->cannot_fallthru;
299 /* The finally clause is always executed after the try clause,
300 so if it does not fall through, then the try-finally will not
301 fall through. Otherwise, if the try clause does not fall
302 through, then when the finally clause falls through it will
303 resume execution wherever the try clause was going. So the
304 whole try-finally will only fall through if both the try
305 clause and the finally clause fall through. */
306 data->cannot_fallthru = false;
307 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
308 data->cannot_fallthru |= cannot_fallthru;
309 gsi_next (gsi);
311 return;
313 case GIMPLE_EH_ELSE:
315 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
316 lower_sequence (gimple_eh_else_n_body_ptr (eh_else_stmt), data);
317 lower_sequence (gimple_eh_else_e_body_ptr (eh_else_stmt), data);
319 break;
321 case GIMPLE_NOP:
322 case GIMPLE_ASM:
323 case GIMPLE_ASSIGN:
324 case GIMPLE_PREDICT:
325 case GIMPLE_LABEL:
326 case GIMPLE_EH_MUST_NOT_THROW:
327 case GIMPLE_OMP_FOR:
328 case GIMPLE_OMP_SECTIONS:
329 case GIMPLE_OMP_SECTIONS_SWITCH:
330 case GIMPLE_OMP_SECTION:
331 case GIMPLE_OMP_SINGLE:
332 case GIMPLE_OMP_MASTER:
333 case GIMPLE_OMP_TASKGROUP:
334 case GIMPLE_OMP_ORDERED:
335 case GIMPLE_OMP_CRITICAL:
336 case GIMPLE_OMP_RETURN:
337 case GIMPLE_OMP_ATOMIC_LOAD:
338 case GIMPLE_OMP_ATOMIC_STORE:
339 case GIMPLE_OMP_CONTINUE:
340 break;
342 case GIMPLE_CALL:
344 tree decl = gimple_call_fndecl (stmt);
345 unsigned i;
347 for (i = 0; i < gimple_call_num_args (stmt); i++)
349 tree arg = gimple_call_arg (stmt, i);
350 if (EXPR_P (arg))
351 TREE_SET_BLOCK (arg, data->block);
354 if (decl
355 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
357 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
359 lower_builtin_setjmp (gsi);
360 data->cannot_fallthru = false;
361 return;
363 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
364 && flag_tree_bit_ccp)
366 lower_builtin_posix_memalign (gsi);
367 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 (
391 as_a <gtransaction *> (stmt)),
392 data);
393 break;
395 default:
396 gcc_unreachable ();
399 data->cannot_fallthru = false;
400 gsi_next (gsi);
403 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
405 static void
406 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
408 tree old_block = data->block;
409 gbind *stmt = as_a <gbind *> (gsi_stmt (*gsi));
410 tree new_block = gimple_bind_block (stmt);
412 if (new_block)
414 if (new_block == old_block)
416 /* The outermost block of the original function may not be the
417 outermost statement chain of the gimplified function. So we
418 may see the outermost block just inside the function. */
419 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
420 new_block = NULL;
422 else
424 /* We do not expect to handle duplicate blocks. */
425 gcc_assert (!TREE_ASM_WRITTEN (new_block));
426 TREE_ASM_WRITTEN (new_block) = 1;
428 /* Block tree may get clobbered by inlining. Normally this would
429 be fixed in rest_of_decl_compilation using block notes, but
430 since we are not going to emit them, it is up to us. */
431 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
432 BLOCK_SUBBLOCKS (old_block) = new_block;
433 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
434 BLOCK_SUPERCONTEXT (new_block) = old_block;
436 data->block = new_block;
440 record_vars (gimple_bind_vars (stmt));
441 lower_sequence (gimple_bind_body_ptr (stmt), data);
443 if (new_block)
445 gcc_assert (data->block == new_block);
447 BLOCK_SUBBLOCKS (new_block)
448 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
449 data->block = old_block;
452 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
453 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
454 gsi_remove (gsi, false);
457 /* Same as above, but for a GIMPLE_TRY_CATCH. */
459 static void
460 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
462 bool cannot_fallthru;
463 gimple stmt = gsi_stmt (*gsi);
464 gimple_stmt_iterator i;
466 /* We don't handle GIMPLE_TRY_FINALLY. */
467 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
469 lower_sequence (gimple_try_eval_ptr (stmt), data);
470 cannot_fallthru = data->cannot_fallthru;
472 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
473 switch (gimple_code (gsi_stmt (i)))
475 case GIMPLE_CATCH:
476 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
477 catch expression and a body. The whole try/catch may fall
478 through iff any of the catch bodies falls through. */
479 for (; !gsi_end_p (i); gsi_next (&i))
481 data->cannot_fallthru = false;
482 lower_sequence (gimple_catch_handler_ptr (
483 as_a <gcatch *> (gsi_stmt (i))),
484 data);
485 if (!data->cannot_fallthru)
486 cannot_fallthru = false;
488 break;
490 case GIMPLE_EH_FILTER:
491 /* The exception filter expression only matters if there is an
492 exception. If the exception does not match EH_FILTER_TYPES,
493 we will execute EH_FILTER_FAILURE, and we will fall through
494 if that falls through. If the exception does match
495 EH_FILTER_TYPES, the stack unwinder will continue up the
496 stack, so we will not fall through. We don't know whether we
497 will throw an exception which matches EH_FILTER_TYPES or not,
498 so we just ignore EH_FILTER_TYPES and assume that we might
499 throw an exception which doesn't match. */
500 data->cannot_fallthru = false;
501 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
502 if (!data->cannot_fallthru)
503 cannot_fallthru = false;
504 break;
506 default:
507 /* This case represents statements to be executed when an
508 exception occurs. Those statements are implicitly followed
509 by a GIMPLE_RESX to resume execution after the exception. So
510 in this case the try/catch never falls through. */
511 data->cannot_fallthru = false;
512 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
513 break;
516 data->cannot_fallthru = cannot_fallthru;
517 gsi_next (gsi);
521 /* Try to determine whether a TRY_CATCH expression can fall through.
522 This is a subroutine of gimple_stmt_may_fallthru. */
524 static bool
525 gimple_try_catch_may_fallthru (gtry *stmt)
527 gimple_stmt_iterator i;
529 /* We don't handle GIMPLE_TRY_FINALLY. */
530 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
532 /* If the TRY block can fall through, the whole TRY_CATCH can
533 fall through. */
534 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
535 return true;
537 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
538 switch (gimple_code (gsi_stmt (i)))
540 case GIMPLE_CATCH:
541 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
542 catch expression and a body. The whole try/catch may fall
543 through iff any of the catch bodies falls through. */
544 for (; !gsi_end_p (i); gsi_next (&i))
546 if (gimple_seq_may_fallthru (gimple_catch_handler (
547 as_a <gcatch *> (gsi_stmt (i)))))
548 return true;
550 return false;
552 case GIMPLE_EH_FILTER:
553 /* The exception filter expression only matters if there is an
554 exception. If the exception does not match EH_FILTER_TYPES,
555 we will execute EH_FILTER_FAILURE, and we will fall through
556 if that falls through. If the exception does match
557 EH_FILTER_TYPES, the stack unwinder will continue up the
558 stack, so we will not fall through. We don't know whether we
559 will throw an exception which matches EH_FILTER_TYPES or not,
560 so we just ignore EH_FILTER_TYPES and assume that we might
561 throw an exception which doesn't match. */
562 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
564 default:
565 /* This case represents statements to be executed when an
566 exception occurs. Those statements are implicitly followed
567 by a GIMPLE_RESX to resume execution after the exception. So
568 in this case the try/catch never falls through. */
569 return false;
574 /* Try to determine if we can continue executing the statement
575 immediately following STMT. This guess need not be 100% accurate;
576 simply be conservative and return true if we don't know. This is
577 used only to avoid stupidly generating extra code. If we're wrong,
578 we'll just delete the extra code later. */
580 bool
581 gimple_stmt_may_fallthru (gimple stmt)
583 if (!stmt)
584 return true;
586 switch (gimple_code (stmt))
588 case GIMPLE_GOTO:
589 case GIMPLE_RETURN:
590 case GIMPLE_RESX:
591 /* Easy cases. If the last statement of the seq implies
592 control transfer, then we can't fall through. */
593 return false;
595 case GIMPLE_SWITCH:
596 /* Switch has already been lowered and represents a branch
597 to a selected label and hence can't fall through. */
598 return false;
600 case GIMPLE_COND:
601 /* GIMPLE_COND's are already lowered into a two-way branch. They
602 can't fall through. */
603 return false;
605 case GIMPLE_BIND:
606 return gimple_seq_may_fallthru (
607 gimple_bind_body (as_a <gbind *> (stmt)));
609 case GIMPLE_TRY:
610 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
611 return gimple_try_catch_may_fallthru (as_a <gtry *> (stmt));
613 /* It must be a GIMPLE_TRY_FINALLY. */
615 /* The finally clause is always executed after the try clause,
616 so if it does not fall through, then the try-finally will not
617 fall through. Otherwise, if the try clause does not fall
618 through, then when the finally clause falls through it will
619 resume execution wherever the try clause was going. So the
620 whole try-finally will only fall through if both the try
621 clause and the finally clause fall through. */
622 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
623 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
625 case GIMPLE_EH_ELSE:
627 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
628 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (eh_else_stmt))
629 || gimple_seq_may_fallthru (gimple_eh_else_e_body (
630 eh_else_stmt)));
633 case GIMPLE_CALL:
634 /* Functions that do not return do not fall through. */
635 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
637 default:
638 return true;
643 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
645 bool
646 gimple_seq_may_fallthru (gimple_seq seq)
648 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
652 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
654 static void
655 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
657 greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
658 gimple t;
659 int i;
660 return_statements_t tmp_rs;
662 /* Match this up with an existing return statement that's been created. */
663 for (i = data->return_statements.length () - 1;
664 i >= 0; i--)
666 tmp_rs = data->return_statements[i];
668 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
670 /* Remove the line number from the representative return statement.
671 It now fills in for many such returns. Failure to remove this
672 will result in incorrect results for coverage analysis. */
673 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
675 goto found;
679 /* Not found. Create a new label and record the return statement. */
680 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
681 tmp_rs.stmt = stmt;
682 data->return_statements.safe_push (tmp_rs);
684 /* Generate a goto statement and remove the return statement. */
685 found:
686 /* When not optimizing, make sure user returns are preserved. */
687 if (!optimize && gimple_has_location (stmt))
688 DECL_ARTIFICIAL (tmp_rs.label) = 0;
689 t = gimple_build_goto (tmp_rs.label);
690 gimple_set_location (t, gimple_location (stmt));
691 gimple_set_block (t, gimple_block (stmt));
692 gsi_insert_before (gsi, t, GSI_SAME_STMT);
693 gsi_remove (gsi, false);
696 /* Lower a __builtin_setjmp GSI.
698 __builtin_setjmp is passed a pointer to an array of five words (not
699 all will be used on all machines). It operates similarly to the C
700 library function of the same name, but is more efficient.
702 It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
703 __builtin_setjmp_receiver.
705 After full lowering, the body of the function should look like:
708 int D.1844;
709 int D.2844;
711 [...]
713 __builtin_setjmp_setup (&buf, &<D1847>);
714 D.1844 = 0;
715 goto <D1846>;
716 <D1847>:;
717 __builtin_setjmp_receiver (&<D1847>);
718 D.1844 = 1;
719 <D1846>:;
720 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
722 [...]
724 __builtin_setjmp_setup (&buf, &<D2847>);
725 D.2844 = 0;
726 goto <D2846>;
727 <D2847>:;
728 __builtin_setjmp_receiver (&<D2847>);
729 D.2844 = 1;
730 <D2846>:;
731 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
733 [...]
735 <D3850>:;
736 return;
739 During cfg creation an extra per-function (or per-OpenMP region)
740 block with ABNORMAL_DISPATCHER internal call will be added, unique
741 destination of all the abnormal call edges and the unique source of
742 all the abnormal edges to the receivers, thus keeping the complexity
743 explosion localized. */
745 static void
746 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
748 gimple stmt = gsi_stmt (*gsi);
749 location_t loc = gimple_location (stmt);
750 tree cont_label = create_artificial_label (loc);
751 tree next_label = create_artificial_label (loc);
752 tree dest, t, arg;
753 gimple g;
755 /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
756 these builtins are modelled as non-local label jumps to the label
757 that is passed to these two builtins, so pretend we have a non-local
758 label during GIMPLE passes too. See PR60003. */
759 cfun->has_nonlocal_label = 1;
761 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
762 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
763 FORCED_LABEL (next_label) = 1;
765 dest = gimple_call_lhs (stmt);
767 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
768 arg = build_addr (next_label, current_function_decl);
769 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
770 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
771 gimple_set_location (g, loc);
772 gimple_set_block (g, gimple_block (stmt));
773 gsi_insert_before (gsi, g, GSI_SAME_STMT);
775 /* Build 'DEST = 0' and insert. */
776 if (dest)
778 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
779 gimple_set_location (g, loc);
780 gimple_set_block (g, gimple_block (stmt));
781 gsi_insert_before (gsi, g, GSI_SAME_STMT);
784 /* Build 'goto CONT_LABEL' and insert. */
785 g = gimple_build_goto (cont_label);
786 gsi_insert_before (gsi, g, GSI_SAME_STMT);
788 /* Build 'NEXT_LABEL:' and insert. */
789 g = gimple_build_label (next_label);
790 gsi_insert_before (gsi, g, GSI_SAME_STMT);
792 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
793 arg = build_addr (next_label, current_function_decl);
794 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
795 g = gimple_build_call (t, 1, arg);
796 gimple_set_location (g, loc);
797 gimple_set_block (g, gimple_block (stmt));
798 gsi_insert_before (gsi, g, GSI_SAME_STMT);
800 /* Build 'DEST = 1' and insert. */
801 if (dest)
803 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
804 integer_one_node));
805 gimple_set_location (g, loc);
806 gimple_set_block (g, gimple_block (stmt));
807 gsi_insert_before (gsi, g, GSI_SAME_STMT);
810 /* Build 'CONT_LABEL:' and insert. */
811 g = gimple_build_label (cont_label);
812 gsi_insert_before (gsi, g, GSI_SAME_STMT);
814 /* Remove the call to __builtin_setjmp. */
815 gsi_remove (gsi, false);
818 /* Lower calls to posix_memalign to
819 res = posix_memalign (ptr, align, size);
820 if (res == 0)
821 *ptr = __builtin_assume_aligned (*ptr, align);
822 or to
823 void *tem;
824 res = posix_memalign (&tem, align, size);
825 if (res == 0)
826 ptr = __builtin_assume_aligned (tem, align);
827 in case the first argument was &ptr. That way we can get at the
828 alignment of the heap pointer in CCP. */
830 static void
831 lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
833 gimple stmt, call = gsi_stmt (*gsi);
834 tree pptr = gimple_call_arg (call, 0);
835 tree align = gimple_call_arg (call, 1);
836 tree res = gimple_call_lhs (call);
837 tree ptr = create_tmp_reg (ptr_type_node);
838 if (TREE_CODE (pptr) == ADDR_EXPR)
840 tree tem = create_tmp_var (ptr_type_node);
841 TREE_ADDRESSABLE (tem) = 1;
842 gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
843 stmt = gimple_build_assign (ptr, tem);
845 else
846 stmt = gimple_build_assign (ptr,
847 fold_build2 (MEM_REF, ptr_type_node, pptr,
848 build_int_cst (ptr_type_node, 0)));
849 if (res == NULL_TREE)
851 res = create_tmp_reg (integer_type_node);
852 gimple_call_set_lhs (call, res);
854 tree align_label = create_artificial_label (UNKNOWN_LOCATION);
855 tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
856 gimple cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
857 align_label, noalign_label);
858 gsi_insert_after (gsi, cond, GSI_NEW_STMT);
859 gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
860 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
861 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
862 2, ptr, align);
863 gimple_call_set_lhs (stmt, ptr);
864 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
865 stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
866 build_int_cst (ptr_type_node, 0)),
867 ptr);
868 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
869 gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
873 /* Record the variables in VARS into function FN. */
875 void
876 record_vars_into (tree vars, tree fn)
878 for (; vars; vars = DECL_CHAIN (vars))
880 tree var = vars;
882 /* BIND_EXPRs contains also function/type/constant declarations
883 we don't need to care about. */
884 if (TREE_CODE (var) != VAR_DECL)
885 continue;
887 /* Nothing to do in this case. */
888 if (DECL_EXTERNAL (var))
889 continue;
891 /* Record the variable. */
892 add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
897 /* Record the variables in VARS into current_function_decl. */
899 void
900 record_vars (tree vars)
902 record_vars_into (vars, current_function_decl);