2014-10-31 Olivier Hainque <hainque@adacore.com>
[official-gcc.git] / gcc / gimple-low.c
blob310ade86c1f383ae70b744bd626b9a8c8045feb5
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 "predict.h"
29 #include "vec.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "gimple-iterator.h"
43 #include "tree-iterator.h"
44 #include "tree-inline.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "tree-pass.h"
48 #include "langhooks.h"
49 #include "gimple-low.h"
50 #include "tree-nested.h"
52 /* The differences between High GIMPLE and Low GIMPLE are the
53 following:
55 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
57 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
58 flow and exception regions are built as an on-the-side region
59 hierarchy (See tree-eh.c:lower_eh_constructs).
61 3- Multiple identical return statements are grouped into a single
62 return and gotos to the unique return site. */
64 /* Match a return statement with a label. During lowering, we identify
65 identical return statements and replace duplicates with a jump to
66 the corresponding label. */
67 struct return_statements_t
69 tree label;
70 gimple stmt;
72 typedef struct return_statements_t return_statements_t;
75 struct lower_data
77 /* Block the current statement belongs to. */
78 tree block;
80 /* A vector of label and return statements to be moved to the end
81 of the function. */
82 vec<return_statements_t> return_statements;
84 /* True if the current statement cannot fall through. */
85 bool cannot_fallthru;
88 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
89 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
90 static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
91 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
92 static void lower_builtin_setjmp (gimple_stmt_iterator *);
93 static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
96 /* Lower the body of current_function_decl from High GIMPLE into Low
97 GIMPLE. */
99 static unsigned int
100 lower_function_body (void)
102 struct lower_data data;
103 gimple_seq body = gimple_body (current_function_decl);
104 gimple_seq lowered_body;
105 gimple_stmt_iterator i;
106 gimple bind;
107 gimple x;
109 /* The gimplifier should've left a body of exactly one statement,
110 namely a GIMPLE_BIND. */
111 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
112 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
114 memset (&data, 0, sizeof (data));
115 data.block = DECL_INITIAL (current_function_decl);
116 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
117 BLOCK_CHAIN (data.block) = NULL_TREE;
118 TREE_ASM_WRITTEN (data.block) = 1;
119 data.return_statements.create (8);
121 bind = gimple_seq_first_stmt (body);
122 lowered_body = NULL;
123 gimple_seq_add_stmt (&lowered_body, bind);
124 i = gsi_start (lowered_body);
125 lower_gimple_bind (&i, &data);
127 i = gsi_last (lowered_body);
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 && (data.return_statements.is_empty ()
134 || (gimple_return_retval (data.return_statements.last().stmt)
135 != NULL)))
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);
143 /* If we lowered any return statements, emit the representative
144 at the end of the function. */
145 while (!data.return_statements.is_empty ())
147 return_statements_t t = data.return_statements.pop ();
148 x = gimple_build_label (t.label);
149 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
150 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
153 /* Once the old body has been lowered, replace it with the new
154 lowered sequence. */
155 gimple_set_body (current_function_decl, lowered_body);
157 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
158 BLOCK_SUBBLOCKS (data.block)
159 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
161 clear_block_marks (data.block);
162 data.return_statements.release ();
163 return 0;
166 namespace {
168 const pass_data pass_data_lower_cf =
170 GIMPLE_PASS, /* type */
171 "lower", /* name */
172 OPTGROUP_NONE, /* optinfo_flags */
173 TV_NONE, /* tv_id */
174 PROP_gimple_any, /* properties_required */
175 PROP_gimple_lcf, /* properties_provided */
176 0, /* properties_destroyed */
177 0, /* todo_flags_start */
178 0, /* todo_flags_finish */
181 class pass_lower_cf : public gimple_opt_pass
183 public:
184 pass_lower_cf (gcc::context *ctxt)
185 : gimple_opt_pass (pass_data_lower_cf, ctxt)
188 /* opt_pass methods: */
189 virtual unsigned int execute (function *) { return lower_function_body (); }
191 }; // class pass_lower_cf
193 } // anon namespace
195 gimple_opt_pass *
196 make_pass_lower_cf (gcc::context *ctxt)
198 return new pass_lower_cf (ctxt);
201 /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
202 when they are changed -- if this has to be done, the lowering routine must
203 do it explicitly. DATA is passed through the recursion. */
205 static void
206 lower_sequence (gimple_seq *seq, struct lower_data *data)
208 gimple_stmt_iterator gsi;
210 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
211 lower_stmt (&gsi, data);
215 /* Lower the OpenMP directive statement pointed by GSI. DATA is
216 passed through the recursion. */
218 static void
219 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
221 gimple stmt;
223 stmt = gsi_stmt (*gsi);
225 lower_sequence (gimple_omp_body_ptr (stmt), data);
226 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
227 gimple_omp_set_body (stmt, NULL);
228 gsi_next (gsi);
232 /* Lower statement GSI. DATA is passed through the recursion. We try to
233 track the fallthruness of statements and get rid of unreachable return
234 statements in order to prevent the EH lowering pass from adding useless
235 edges that can cause bogus warnings to be issued later; this guess need
236 not be 100% accurate, simply be conservative and reset cannot_fallthru
237 to false if we don't know. */
239 static void
240 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
242 gimple stmt = gsi_stmt (*gsi);
244 gimple_set_block (stmt, data->block);
246 switch (gimple_code (stmt))
248 case GIMPLE_BIND:
249 lower_gimple_bind (gsi, data);
250 /* Propagate fallthruness. */
251 return;
253 case GIMPLE_COND:
254 case GIMPLE_GOTO:
255 case GIMPLE_SWITCH:
256 data->cannot_fallthru = true;
257 gsi_next (gsi);
258 return;
260 case GIMPLE_RETURN:
261 if (data->cannot_fallthru)
263 gsi_remove (gsi, false);
264 /* Propagate fallthruness. */
266 else
268 lower_gimple_return (gsi, data);
269 data->cannot_fallthru = true;
271 return;
273 case GIMPLE_TRY:
274 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
275 lower_try_catch (gsi, data);
276 else
278 /* It must be a GIMPLE_TRY_FINALLY. */
279 bool cannot_fallthru;
280 lower_sequence (gimple_try_eval_ptr (stmt), data);
281 cannot_fallthru = data->cannot_fallthru;
283 /* The finally clause is always executed after the try clause,
284 so if it does not fall through, then the try-finally will not
285 fall through. Otherwise, if the try clause does not fall
286 through, then when the finally clause falls through it will
287 resume execution wherever the try clause was going. So the
288 whole try-finally will only fall through if both the try
289 clause and the finally clause fall through. */
290 data->cannot_fallthru = false;
291 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
292 data->cannot_fallthru |= cannot_fallthru;
293 gsi_next (gsi);
295 return;
297 case GIMPLE_EH_ELSE:
298 lower_sequence (gimple_eh_else_n_body_ptr (stmt), data);
299 lower_sequence (gimple_eh_else_e_body_ptr (stmt), data);
300 break;
302 case GIMPLE_NOP:
303 case GIMPLE_ASM:
304 case GIMPLE_ASSIGN:
305 case GIMPLE_PREDICT:
306 case GIMPLE_LABEL:
307 case GIMPLE_EH_MUST_NOT_THROW:
308 case GIMPLE_OMP_FOR:
309 case GIMPLE_OMP_SECTIONS:
310 case GIMPLE_OMP_SECTIONS_SWITCH:
311 case GIMPLE_OMP_SECTION:
312 case GIMPLE_OMP_SINGLE:
313 case GIMPLE_OMP_MASTER:
314 case GIMPLE_OMP_TASKGROUP:
315 case GIMPLE_OMP_ORDERED:
316 case GIMPLE_OMP_CRITICAL:
317 case GIMPLE_OMP_RETURN:
318 case GIMPLE_OMP_ATOMIC_LOAD:
319 case GIMPLE_OMP_ATOMIC_STORE:
320 case GIMPLE_OMP_CONTINUE:
321 break;
323 case GIMPLE_CALL:
325 tree decl = gimple_call_fndecl (stmt);
326 unsigned i;
328 for (i = 0; i < gimple_call_num_args (stmt); i++)
330 tree arg = gimple_call_arg (stmt, i);
331 if (EXPR_P (arg))
332 TREE_SET_BLOCK (arg, data->block);
335 if (decl
336 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
338 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
340 lower_builtin_setjmp (gsi);
341 data->cannot_fallthru = false;
342 return;
344 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
345 && flag_tree_bit_ccp)
347 lower_builtin_posix_memalign (gsi);
348 return;
352 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
354 data->cannot_fallthru = true;
355 gsi_next (gsi);
356 return;
359 break;
361 case GIMPLE_OMP_PARALLEL:
362 case GIMPLE_OMP_TASK:
363 case GIMPLE_OMP_TARGET:
364 case GIMPLE_OMP_TEAMS:
365 data->cannot_fallthru = false;
366 lower_omp_directive (gsi, data);
367 data->cannot_fallthru = false;
368 return;
370 case GIMPLE_TRANSACTION:
371 lower_sequence (gimple_transaction_body_ptr (stmt), data);
372 break;
374 default:
375 gcc_unreachable ();
378 data->cannot_fallthru = false;
379 gsi_next (gsi);
382 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
384 static void
385 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
387 tree old_block = data->block;
388 gimple stmt = gsi_stmt (*gsi);
389 tree new_block = gimple_bind_block (stmt);
391 if (new_block)
393 if (new_block == old_block)
395 /* The outermost block of the original function may not be the
396 outermost statement chain of the gimplified function. So we
397 may see the outermost block just inside the function. */
398 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
399 new_block = NULL;
401 else
403 /* We do not expect to handle duplicate blocks. */
404 gcc_assert (!TREE_ASM_WRITTEN (new_block));
405 TREE_ASM_WRITTEN (new_block) = 1;
407 /* Block tree may get clobbered by inlining. Normally this would
408 be fixed in rest_of_decl_compilation using block notes, but
409 since we are not going to emit them, it is up to us. */
410 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
411 BLOCK_SUBBLOCKS (old_block) = new_block;
412 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
413 BLOCK_SUPERCONTEXT (new_block) = old_block;
415 data->block = new_block;
419 record_vars (gimple_bind_vars (stmt));
420 lower_sequence (gimple_bind_body_ptr (stmt), data);
422 if (new_block)
424 gcc_assert (data->block == new_block);
426 BLOCK_SUBBLOCKS (new_block)
427 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
428 data->block = old_block;
431 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
432 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
433 gsi_remove (gsi, false);
436 /* Same as above, but for a GIMPLE_TRY_CATCH. */
438 static void
439 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
441 bool cannot_fallthru;
442 gimple stmt = gsi_stmt (*gsi);
443 gimple_stmt_iterator i;
445 /* We don't handle GIMPLE_TRY_FINALLY. */
446 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
448 lower_sequence (gimple_try_eval_ptr (stmt), data);
449 cannot_fallthru = data->cannot_fallthru;
451 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
452 switch (gimple_code (gsi_stmt (i)))
454 case GIMPLE_CATCH:
455 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
456 catch expression and a body. The whole try/catch may fall
457 through iff any of the catch bodies falls through. */
458 for (; !gsi_end_p (i); gsi_next (&i))
460 data->cannot_fallthru = false;
461 lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data);
462 if (!data->cannot_fallthru)
463 cannot_fallthru = false;
465 break;
467 case GIMPLE_EH_FILTER:
468 /* The exception filter expression only matters if there is an
469 exception. If the exception does not match EH_FILTER_TYPES,
470 we will execute EH_FILTER_FAILURE, and we will fall through
471 if that falls through. If the exception does match
472 EH_FILTER_TYPES, the stack unwinder will continue up the
473 stack, so we will not fall through. We don't know whether we
474 will throw an exception which matches EH_FILTER_TYPES or not,
475 so we just ignore EH_FILTER_TYPES and assume that we might
476 throw an exception which doesn't match. */
477 data->cannot_fallthru = false;
478 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
479 if (!data->cannot_fallthru)
480 cannot_fallthru = false;
481 break;
483 default:
484 /* This case represents statements to be executed when an
485 exception occurs. Those statements are implicitly followed
486 by a GIMPLE_RESX to resume execution after the exception. So
487 in this case the try/catch never falls through. */
488 data->cannot_fallthru = false;
489 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
490 break;
493 data->cannot_fallthru = cannot_fallthru;
494 gsi_next (gsi);
498 /* Try to determine whether a TRY_CATCH expression can fall through.
499 This is a subroutine of gimple_stmt_may_fallthru. */
501 static bool
502 gimple_try_catch_may_fallthru (gimple stmt)
504 gimple_stmt_iterator i;
506 /* We don't handle GIMPLE_TRY_FINALLY. */
507 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
509 /* If the TRY block can fall through, the whole TRY_CATCH can
510 fall through. */
511 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
512 return true;
514 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
515 switch (gimple_code (gsi_stmt (i)))
517 case GIMPLE_CATCH:
518 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
519 catch expression and a body. The whole try/catch may fall
520 through iff any of the catch bodies falls through. */
521 for (; !gsi_end_p (i); gsi_next (&i))
523 if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
524 return true;
526 return false;
528 case GIMPLE_EH_FILTER:
529 /* The exception filter expression only matters if there is an
530 exception. If the exception does not match EH_FILTER_TYPES,
531 we will execute EH_FILTER_FAILURE, and we will fall through
532 if that falls through. If the exception does match
533 EH_FILTER_TYPES, the stack unwinder will continue up the
534 stack, so we will not fall through. We don't know whether we
535 will throw an exception which matches EH_FILTER_TYPES or not,
536 so we just ignore EH_FILTER_TYPES and assume that we might
537 throw an exception which doesn't match. */
538 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
540 default:
541 /* This case represents statements to be executed when an
542 exception occurs. Those statements are implicitly followed
543 by a GIMPLE_RESX to resume execution after the exception. So
544 in this case the try/catch never falls through. */
545 return false;
550 /* Try to determine if we can continue executing the statement
551 immediately following STMT. This guess need not be 100% accurate;
552 simply be conservative and return true if we don't know. This is
553 used only to avoid stupidly generating extra code. If we're wrong,
554 we'll just delete the extra code later. */
556 bool
557 gimple_stmt_may_fallthru (gimple stmt)
559 if (!stmt)
560 return true;
562 switch (gimple_code (stmt))
564 case GIMPLE_GOTO:
565 case GIMPLE_RETURN:
566 case GIMPLE_RESX:
567 /* Easy cases. If the last statement of the seq implies
568 control transfer, then we can't fall through. */
569 return false;
571 case GIMPLE_SWITCH:
572 /* Switch has already been lowered and represents a branch
573 to a selected label and hence can't fall through. */
574 return false;
576 case GIMPLE_COND:
577 /* GIMPLE_COND's are already lowered into a two-way branch. They
578 can't fall through. */
579 return false;
581 case GIMPLE_BIND:
582 return gimple_seq_may_fallthru (gimple_bind_body (stmt));
584 case GIMPLE_TRY:
585 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
586 return gimple_try_catch_may_fallthru (stmt);
588 /* It must be a GIMPLE_TRY_FINALLY. */
590 /* The finally clause is always executed after the try clause,
591 so if it does not fall through, then the try-finally will not
592 fall through. Otherwise, if the try clause does not fall
593 through, then when the finally clause falls through it will
594 resume execution wherever the try clause was going. So the
595 whole try-finally will only fall through if both the try
596 clause and the finally clause fall through. */
597 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
598 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
600 case GIMPLE_EH_ELSE:
601 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
602 || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
604 case GIMPLE_CALL:
605 /* Functions that do not return do not fall through. */
606 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
608 default:
609 return true;
614 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
616 bool
617 gimple_seq_may_fallthru (gimple_seq seq)
619 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
623 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
625 static void
626 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
628 gimple stmt = gsi_stmt (*gsi);
629 gimple t;
630 int i;
631 return_statements_t tmp_rs;
633 /* Match this up with an existing return statement that's been created. */
634 for (i = data->return_statements.length () - 1;
635 i >= 0; i--)
637 tmp_rs = data->return_statements[i];
639 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
641 /* Remove the line number from the representative return statement.
642 It now fills in for many such returns. Failure to remove this
643 will result in incorrect results for coverage analysis. */
644 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
646 goto found;
650 /* Not found. Create a new label and record the return statement. */
651 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
652 tmp_rs.stmt = stmt;
653 data->return_statements.safe_push (tmp_rs);
655 /* Generate a goto statement and remove the return statement. */
656 found:
657 /* When not optimizing, make sure user returns are preserved. */
658 if (!optimize && gimple_has_location (stmt))
659 DECL_ARTIFICIAL (tmp_rs.label) = 0;
660 t = gimple_build_goto (tmp_rs.label);
661 gimple_set_location (t, gimple_location (stmt));
662 gimple_set_block (t, gimple_block (stmt));
663 gsi_insert_before (gsi, t, GSI_SAME_STMT);
664 gsi_remove (gsi, false);
667 /* Lower a __builtin_setjmp GSI.
669 __builtin_setjmp is passed a pointer to an array of five words (not
670 all will be used on all machines). It operates similarly to the C
671 library function of the same name, but is more efficient.
673 It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
674 __builtin_setjmp_receiver.
676 After full lowering, the body of the function should look like:
679 int D.1844;
680 int D.2844;
682 [...]
684 __builtin_setjmp_setup (&buf, &<D1847>);
685 D.1844 = 0;
686 goto <D1846>;
687 <D1847>:;
688 __builtin_setjmp_receiver (&<D1847>);
689 D.1844 = 1;
690 <D1846>:;
691 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
693 [...]
695 __builtin_setjmp_setup (&buf, &<D2847>);
696 D.2844 = 0;
697 goto <D2846>;
698 <D2847>:;
699 __builtin_setjmp_receiver (&<D2847>);
700 D.2844 = 1;
701 <D2846>:;
702 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
704 [...]
706 <D3850>:;
707 return;
710 During cfg creation an extra per-function (or per-OpenMP region)
711 block with ABNORMAL_DISPATCHER internal call will be added, unique
712 destination of all the abnormal call edges and the unique source of
713 all the abnormal edges to the receivers, thus keeping the complexity
714 explosion localized. */
716 static void
717 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
719 gimple stmt = gsi_stmt (*gsi);
720 location_t loc = gimple_location (stmt);
721 tree cont_label = create_artificial_label (loc);
722 tree next_label = create_artificial_label (loc);
723 tree dest, t, arg;
724 gimple g;
726 /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
727 these builtins are modelled as non-local label jumps to the label
728 that is passed to these two builtins, so pretend we have a non-local
729 label during GIMPLE passes too. See PR60003. */
730 cfun->has_nonlocal_label = 1;
732 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
733 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
734 FORCED_LABEL (next_label) = 1;
736 dest = gimple_call_lhs (stmt);
738 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
739 arg = build_addr (next_label, current_function_decl);
740 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
741 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
742 gimple_set_location (g, loc);
743 gimple_set_block (g, gimple_block (stmt));
744 gsi_insert_before (gsi, g, GSI_SAME_STMT);
746 /* Build 'DEST = 0' and insert. */
747 if (dest)
749 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
750 gimple_set_location (g, loc);
751 gimple_set_block (g, gimple_block (stmt));
752 gsi_insert_before (gsi, g, GSI_SAME_STMT);
755 /* Build 'goto CONT_LABEL' and insert. */
756 g = gimple_build_goto (cont_label);
757 gsi_insert_before (gsi, g, GSI_SAME_STMT);
759 /* Build 'NEXT_LABEL:' and insert. */
760 g = gimple_build_label (next_label);
761 gsi_insert_before (gsi, g, GSI_SAME_STMT);
763 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
764 arg = build_addr (next_label, current_function_decl);
765 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
766 g = gimple_build_call (t, 1, arg);
767 gimple_set_location (g, loc);
768 gimple_set_block (g, gimple_block (stmt));
769 gsi_insert_before (gsi, g, GSI_SAME_STMT);
771 /* Build 'DEST = 1' and insert. */
772 if (dest)
774 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
775 integer_one_node));
776 gimple_set_location (g, loc);
777 gimple_set_block (g, gimple_block (stmt));
778 gsi_insert_before (gsi, g, GSI_SAME_STMT);
781 /* Build 'CONT_LABEL:' and insert. */
782 g = gimple_build_label (cont_label);
783 gsi_insert_before (gsi, g, GSI_SAME_STMT);
785 /* Remove the call to __builtin_setjmp. */
786 gsi_remove (gsi, false);
789 /* Lower calls to posix_memalign to
790 res = posix_memalign (ptr, align, size);
791 if (res == 0)
792 *ptr = __builtin_assume_aligned (*ptr, align);
793 or to
794 void *tem;
795 res = posix_memalign (&tem, align, size);
796 if (res == 0)
797 ptr = __builtin_assume_aligned (tem, align);
798 in case the first argument was &ptr. That way we can get at the
799 alignment of the heap pointer in CCP. */
801 static void
802 lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
804 gimple stmt, call = gsi_stmt (*gsi);
805 tree pptr = gimple_call_arg (call, 0);
806 tree align = gimple_call_arg (call, 1);
807 tree res = gimple_call_lhs (call);
808 tree ptr = create_tmp_reg (ptr_type_node, NULL);
809 if (TREE_CODE (pptr) == ADDR_EXPR)
811 tree tem = create_tmp_var (ptr_type_node, NULL);
812 TREE_ADDRESSABLE (tem) = 1;
813 gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
814 stmt = gimple_build_assign (ptr, tem);
816 else
817 stmt = gimple_build_assign (ptr,
818 fold_build2 (MEM_REF, ptr_type_node, pptr,
819 build_int_cst (ptr_type_node, 0)));
820 if (res == NULL_TREE)
822 res = create_tmp_reg (integer_type_node, NULL);
823 gimple_call_set_lhs (call, res);
825 tree align_label = create_artificial_label (UNKNOWN_LOCATION);
826 tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
827 gimple cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
828 align_label, noalign_label);
829 gsi_insert_after (gsi, cond, GSI_NEW_STMT);
830 gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
831 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
832 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
833 2, ptr, align);
834 gimple_call_set_lhs (stmt, ptr);
835 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
836 stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
837 build_int_cst (ptr_type_node, 0)),
838 ptr);
839 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
840 gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
844 /* Record the variables in VARS into function FN. */
846 void
847 record_vars_into (tree vars, tree fn)
849 for (; vars; vars = DECL_CHAIN (vars))
851 tree var = vars;
853 /* BIND_EXPRs contains also function/type/constant declarations
854 we don't need to care about. */
855 if (TREE_CODE (var) != VAR_DECL)
856 continue;
858 /* Nothing to do in this case. */
859 if (DECL_EXTERNAL (var))
860 continue;
862 /* Record the variable. */
863 add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
868 /* Record the variables in VARS into current_function_decl. */
870 void
871 record_vars (tree vars)
873 record_vars_into (vars, current_function_decl);