2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / gimple-low.c
blobbd858ce8cb3bf80c700f8b805d277e3897f82837
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 "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "tree-nested.h"
30 #include "calls.h"
31 #include "predict.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "gimple.h"
39 #include "gimple-iterator.h"
40 #include "tree-iterator.h"
41 #include "tree-inline.h"
42 #include "flags.h"
43 #include "diagnostic-core.h"
44 #include "tree-pass.h"
45 #include "langhooks.h"
46 #include "gimple-low.h"
47 #include "tree-nested.h"
49 /* The differences between High GIMPLE and Low GIMPLE are the
50 following:
52 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
54 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
55 flow and exception regions are built as an on-the-side region
56 hierarchy (See tree-eh.c:lower_eh_constructs).
58 3- Multiple identical return statements are grouped into a single
59 return and gotos to the unique return site. */
61 /* Match a return statement with a label. During lowering, we identify
62 identical return statements and replace duplicates with a jump to
63 the corresponding label. */
64 struct return_statements_t
66 tree label;
67 greturn *stmt;
69 typedef struct return_statements_t return_statements_t;
72 struct lower_data
74 /* Block the current statement belongs to. */
75 tree block;
77 /* A vector of label and return statements to be moved to the end
78 of the function. */
79 vec<return_statements_t> return_statements;
81 /* True if the current statement cannot fall through. */
82 bool cannot_fallthru;
85 static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
86 static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
87 static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
88 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
89 static void lower_builtin_setjmp (gimple_stmt_iterator *);
90 static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
93 /* Lower the body of current_function_decl from High GIMPLE into Low
94 GIMPLE. */
96 static unsigned int
97 lower_function_body (void)
99 struct lower_data data;
100 gimple_seq body = gimple_body (current_function_decl);
101 gimple_seq lowered_body;
102 gimple_stmt_iterator i;
103 gimple bind;
104 gimple x;
106 /* The gimplifier should've left a body of exactly one statement,
107 namely a GIMPLE_BIND. */
108 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
109 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
111 memset (&data, 0, sizeof (data));
112 data.block = DECL_INITIAL (current_function_decl);
113 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
114 BLOCK_CHAIN (data.block) = NULL_TREE;
115 TREE_ASM_WRITTEN (data.block) = 1;
116 data.return_statements.create (8);
118 bind = gimple_seq_first_stmt (body);
119 lowered_body = NULL;
120 gimple_seq_add_stmt (&lowered_body, bind);
121 i = gsi_start (lowered_body);
122 lower_gimple_bind (&i, &data);
124 i = gsi_last (lowered_body);
126 /* If the function falls off the end, we need a null return statement.
127 If we've already got one in the return_statements vector, we don't
128 need to do anything special. Otherwise build one by hand. */
129 bool may_fallthru = gimple_seq_may_fallthru (lowered_body);
130 if (may_fallthru
131 && (data.return_statements.is_empty ()
132 || (gimple_return_retval (data.return_statements.last().stmt)
133 != NULL)))
135 x = gimple_build_return (NULL);
136 gimple_set_location (x, cfun->function_end_locus);
137 gimple_set_block (x, DECL_INITIAL (current_function_decl));
138 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
139 may_fallthru = false;
142 /* If we lowered any return statements, emit the representative
143 at the end of the function. */
144 while (!data.return_statements.is_empty ())
146 return_statements_t t = data.return_statements.pop ();
147 x = gimple_build_label (t.label);
148 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
149 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
150 if (may_fallthru)
152 /* Remove the line number from the representative return statement.
153 It now fills in for the fallthru too. Failure to remove this
154 will result in incorrect results for coverage analysis. */
155 gimple_set_location (t.stmt, UNKNOWN_LOCATION);
156 may_fallthru = false;
160 /* Once the old body has been lowered, replace it with the new
161 lowered sequence. */
162 gimple_set_body (current_function_decl, lowered_body);
164 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
165 BLOCK_SUBBLOCKS (data.block)
166 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
168 clear_block_marks (data.block);
169 data.return_statements.release ();
170 return 0;
173 namespace {
175 const pass_data pass_data_lower_cf =
177 GIMPLE_PASS, /* type */
178 "lower", /* name */
179 OPTGROUP_NONE, /* optinfo_flags */
180 TV_NONE, /* tv_id */
181 PROP_gimple_any, /* properties_required */
182 PROP_gimple_lcf, /* properties_provided */
183 0, /* properties_destroyed */
184 0, /* todo_flags_start */
185 0, /* todo_flags_finish */
188 class pass_lower_cf : public gimple_opt_pass
190 public:
191 pass_lower_cf (gcc::context *ctxt)
192 : gimple_opt_pass (pass_data_lower_cf, ctxt)
195 /* opt_pass methods: */
196 virtual unsigned int execute (function *) { return lower_function_body (); }
198 }; // class pass_lower_cf
200 } // anon namespace
202 gimple_opt_pass *
203 make_pass_lower_cf (gcc::context *ctxt)
205 return new pass_lower_cf (ctxt);
208 /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
209 when they are changed -- if this has to be done, the lowering routine must
210 do it explicitly. DATA is passed through the recursion. */
212 static void
213 lower_sequence (gimple_seq *seq, struct lower_data *data)
215 gimple_stmt_iterator gsi;
217 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
218 lower_stmt (&gsi, data);
222 /* Lower the OpenMP directive statement pointed by GSI. DATA is
223 passed through the recursion. */
225 static void
226 lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
228 gimple stmt;
230 stmt = gsi_stmt (*gsi);
232 lower_sequence (gimple_omp_body_ptr (stmt), data);
233 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
234 gimple_omp_set_body (stmt, NULL);
235 gsi_next (gsi);
239 /* Lower statement GSI. DATA is passed through the recursion. We try to
240 track the fallthruness of statements and get rid of unreachable return
241 statements in order to prevent the EH lowering pass from adding useless
242 edges that can cause bogus warnings to be issued later; this guess need
243 not be 100% accurate, simply be conservative and reset cannot_fallthru
244 to false if we don't know. */
246 static void
247 lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
249 gimple stmt = gsi_stmt (*gsi);
251 gimple_set_block (stmt, data->block);
253 switch (gimple_code (stmt))
255 case GIMPLE_BIND:
256 lower_gimple_bind (gsi, data);
257 /* Propagate fallthruness. */
258 return;
260 case GIMPLE_COND:
261 case GIMPLE_GOTO:
262 case GIMPLE_SWITCH:
263 data->cannot_fallthru = true;
264 gsi_next (gsi);
265 return;
267 case GIMPLE_RETURN:
268 if (data->cannot_fallthru)
270 gsi_remove (gsi, false);
271 /* Propagate fallthruness. */
273 else
275 lower_gimple_return (gsi, data);
276 data->cannot_fallthru = true;
278 return;
280 case GIMPLE_TRY:
281 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
282 lower_try_catch (gsi, data);
283 else
285 /* It must be a GIMPLE_TRY_FINALLY. */
286 bool cannot_fallthru;
287 lower_sequence (gimple_try_eval_ptr (stmt), data);
288 cannot_fallthru = data->cannot_fallthru;
290 /* The finally clause is always executed after the try clause,
291 so if it does not fall through, then the try-finally will not
292 fall through. Otherwise, if the try clause does not fall
293 through, then when the finally clause falls through it will
294 resume execution wherever the try clause was going. So the
295 whole try-finally will only fall through if both the try
296 clause and the finally clause fall through. */
297 data->cannot_fallthru = false;
298 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
299 data->cannot_fallthru |= cannot_fallthru;
300 gsi_next (gsi);
302 return;
304 case GIMPLE_EH_ELSE:
306 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
307 lower_sequence (gimple_eh_else_n_body_ptr (eh_else_stmt), data);
308 lower_sequence (gimple_eh_else_e_body_ptr (eh_else_stmt), data);
310 break;
312 case GIMPLE_NOP:
313 case GIMPLE_ASM:
314 case GIMPLE_ASSIGN:
315 case GIMPLE_PREDICT:
316 case GIMPLE_LABEL:
317 case GIMPLE_EH_MUST_NOT_THROW:
318 case GIMPLE_OMP_FOR:
319 case GIMPLE_OMP_SECTIONS:
320 case GIMPLE_OMP_SECTIONS_SWITCH:
321 case GIMPLE_OMP_SECTION:
322 case GIMPLE_OMP_SINGLE:
323 case GIMPLE_OMP_MASTER:
324 case GIMPLE_OMP_TASKGROUP:
325 case GIMPLE_OMP_ORDERED:
326 case GIMPLE_OMP_CRITICAL:
327 case GIMPLE_OMP_RETURN:
328 case GIMPLE_OMP_ATOMIC_LOAD:
329 case GIMPLE_OMP_ATOMIC_STORE:
330 case GIMPLE_OMP_CONTINUE:
331 break;
333 case GIMPLE_CALL:
335 tree decl = gimple_call_fndecl (stmt);
336 unsigned i;
338 for (i = 0; i < gimple_call_num_args (stmt); i++)
340 tree arg = gimple_call_arg (stmt, i);
341 if (EXPR_P (arg))
342 TREE_SET_BLOCK (arg, data->block);
345 if (decl
346 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
348 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
350 lower_builtin_setjmp (gsi);
351 data->cannot_fallthru = false;
352 return;
354 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
355 && flag_tree_bit_ccp)
357 lower_builtin_posix_memalign (gsi);
358 return;
362 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
364 data->cannot_fallthru = true;
365 gsi_next (gsi);
366 return;
369 break;
371 case GIMPLE_OMP_PARALLEL:
372 case GIMPLE_OMP_TASK:
373 case GIMPLE_OMP_TARGET:
374 case GIMPLE_OMP_TEAMS:
375 data->cannot_fallthru = false;
376 lower_omp_directive (gsi, data);
377 data->cannot_fallthru = false;
378 return;
380 case GIMPLE_TRANSACTION:
381 lower_sequence (gimple_transaction_body_ptr (
382 as_a <gtransaction *> (stmt)),
383 data);
384 break;
386 default:
387 gcc_unreachable ();
390 data->cannot_fallthru = false;
391 gsi_next (gsi);
394 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
396 static void
397 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
399 tree old_block = data->block;
400 gbind *stmt = as_a <gbind *> (gsi_stmt (*gsi));
401 tree new_block = gimple_bind_block (stmt);
403 if (new_block)
405 if (new_block == old_block)
407 /* The outermost block of the original function may not be the
408 outermost statement chain of the gimplified function. So we
409 may see the outermost block just inside the function. */
410 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
411 new_block = NULL;
413 else
415 /* We do not expect to handle duplicate blocks. */
416 gcc_assert (!TREE_ASM_WRITTEN (new_block));
417 TREE_ASM_WRITTEN (new_block) = 1;
419 /* Block tree may get clobbered by inlining. Normally this would
420 be fixed in rest_of_decl_compilation using block notes, but
421 since we are not going to emit them, it is up to us. */
422 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
423 BLOCK_SUBBLOCKS (old_block) = new_block;
424 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
425 BLOCK_SUPERCONTEXT (new_block) = old_block;
427 data->block = new_block;
431 record_vars (gimple_bind_vars (stmt));
432 lower_sequence (gimple_bind_body_ptr (stmt), data);
434 if (new_block)
436 gcc_assert (data->block == new_block);
438 BLOCK_SUBBLOCKS (new_block)
439 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
440 data->block = old_block;
443 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
444 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
445 gsi_remove (gsi, false);
448 /* Same as above, but for a GIMPLE_TRY_CATCH. */
450 static void
451 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
453 bool cannot_fallthru;
454 gimple stmt = gsi_stmt (*gsi);
455 gimple_stmt_iterator i;
457 /* We don't handle GIMPLE_TRY_FINALLY. */
458 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
460 lower_sequence (gimple_try_eval_ptr (stmt), data);
461 cannot_fallthru = data->cannot_fallthru;
463 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
464 switch (gimple_code (gsi_stmt (i)))
466 case GIMPLE_CATCH:
467 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
468 catch expression and a body. The whole try/catch may fall
469 through iff any of the catch bodies falls through. */
470 for (; !gsi_end_p (i); gsi_next (&i))
472 data->cannot_fallthru = false;
473 lower_sequence (gimple_catch_handler_ptr (
474 as_a <gcatch *> (gsi_stmt (i))),
475 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 (gtry *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 (
538 as_a <gcatch *> (gsi_stmt (i)))))
539 return true;
541 return false;
543 case GIMPLE_EH_FILTER:
544 /* The exception filter expression only matters if there is an
545 exception. If the exception does not match EH_FILTER_TYPES,
546 we will execute EH_FILTER_FAILURE, and we will fall through
547 if that falls through. If the exception does match
548 EH_FILTER_TYPES, the stack unwinder will continue up the
549 stack, so we will not fall through. We don't know whether we
550 will throw an exception which matches EH_FILTER_TYPES or not,
551 so we just ignore EH_FILTER_TYPES and assume that we might
552 throw an exception which doesn't match. */
553 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
555 default:
556 /* This case represents statements to be executed when an
557 exception occurs. Those statements are implicitly followed
558 by a GIMPLE_RESX to resume execution after the exception. So
559 in this case the try/catch never falls through. */
560 return false;
565 /* Try to determine if we can continue executing the statement
566 immediately following STMT. This guess need not be 100% accurate;
567 simply be conservative and return true if we don't know. This is
568 used only to avoid stupidly generating extra code. If we're wrong,
569 we'll just delete the extra code later. */
571 bool
572 gimple_stmt_may_fallthru (gimple stmt)
574 if (!stmt)
575 return true;
577 switch (gimple_code (stmt))
579 case GIMPLE_GOTO:
580 case GIMPLE_RETURN:
581 case GIMPLE_RESX:
582 /* Easy cases. If the last statement of the seq implies
583 control transfer, then we can't fall through. */
584 return false;
586 case GIMPLE_SWITCH:
587 /* Switch has already been lowered and represents a branch
588 to a selected label and hence can't fall through. */
589 return false;
591 case GIMPLE_COND:
592 /* GIMPLE_COND's are already lowered into a two-way branch. They
593 can't fall through. */
594 return false;
596 case GIMPLE_BIND:
597 return gimple_seq_may_fallthru (
598 gimple_bind_body (as_a <gbind *> (stmt)));
600 case GIMPLE_TRY:
601 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
602 return gimple_try_catch_may_fallthru (as_a <gtry *> (stmt));
604 /* It must be a GIMPLE_TRY_FINALLY. */
606 /* The finally clause is always executed after the try clause,
607 so if it does not fall through, then the try-finally will not
608 fall through. Otherwise, if the try clause does not fall
609 through, then when the finally clause falls through it will
610 resume execution wherever the try clause was going. So the
611 whole try-finally will only fall through if both the try
612 clause and the finally clause fall through. */
613 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
614 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
616 case GIMPLE_EH_ELSE:
618 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
619 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (eh_else_stmt))
620 || gimple_seq_may_fallthru (gimple_eh_else_e_body (
621 eh_else_stmt)));
624 case GIMPLE_CALL:
625 /* Functions that do not return do not fall through. */
626 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
628 default:
629 return true;
634 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
636 bool
637 gimple_seq_may_fallthru (gimple_seq seq)
639 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
643 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
645 static void
646 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
648 greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
649 gimple t;
650 int i;
651 return_statements_t tmp_rs;
653 /* Match this up with an existing return statement that's been created. */
654 for (i = data->return_statements.length () - 1;
655 i >= 0; i--)
657 tmp_rs = data->return_statements[i];
659 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
661 /* Remove the line number from the representative return statement.
662 It now fills in for many such returns. Failure to remove this
663 will result in incorrect results for coverage analysis. */
664 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
666 goto found;
670 /* Not found. Create a new label and record the return statement. */
671 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
672 tmp_rs.stmt = stmt;
673 data->return_statements.safe_push (tmp_rs);
675 /* Generate a goto statement and remove the return statement. */
676 found:
677 /* When not optimizing, make sure user returns are preserved. */
678 if (!optimize && gimple_has_location (stmt))
679 DECL_ARTIFICIAL (tmp_rs.label) = 0;
680 t = gimple_build_goto (tmp_rs.label);
681 gimple_set_location (t, gimple_location (stmt));
682 gimple_set_block (t, gimple_block (stmt));
683 gsi_insert_before (gsi, t, GSI_SAME_STMT);
684 gsi_remove (gsi, false);
687 /* Lower a __builtin_setjmp GSI.
689 __builtin_setjmp is passed a pointer to an array of five words (not
690 all will be used on all machines). It operates similarly to the C
691 library function of the same name, but is more efficient.
693 It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
694 __builtin_setjmp_receiver.
696 After full lowering, the body of the function should look like:
699 int D.1844;
700 int D.2844;
702 [...]
704 __builtin_setjmp_setup (&buf, &<D1847>);
705 D.1844 = 0;
706 goto <D1846>;
707 <D1847>:;
708 __builtin_setjmp_receiver (&<D1847>);
709 D.1844 = 1;
710 <D1846>:;
711 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
713 [...]
715 __builtin_setjmp_setup (&buf, &<D2847>);
716 D.2844 = 0;
717 goto <D2846>;
718 <D2847>:;
719 __builtin_setjmp_receiver (&<D2847>);
720 D.2844 = 1;
721 <D2846>:;
722 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
724 [...]
726 <D3850>:;
727 return;
730 During cfg creation an extra per-function (or per-OpenMP region)
731 block with ABNORMAL_DISPATCHER internal call will be added, unique
732 destination of all the abnormal call edges and the unique source of
733 all the abnormal edges to the receivers, thus keeping the complexity
734 explosion localized. */
736 static void
737 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
739 gimple stmt = gsi_stmt (*gsi);
740 location_t loc = gimple_location (stmt);
741 tree cont_label = create_artificial_label (loc);
742 tree next_label = create_artificial_label (loc);
743 tree dest, t, arg;
744 gimple g;
746 /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
747 these builtins are modelled as non-local label jumps to the label
748 that is passed to these two builtins, so pretend we have a non-local
749 label during GIMPLE passes too. See PR60003. */
750 cfun->has_nonlocal_label = 1;
752 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
753 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
754 FORCED_LABEL (next_label) = 1;
756 dest = gimple_call_lhs (stmt);
758 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
759 arg = build_addr (next_label, current_function_decl);
760 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
761 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
762 gimple_set_location (g, loc);
763 gimple_set_block (g, gimple_block (stmt));
764 gsi_insert_before (gsi, g, GSI_SAME_STMT);
766 /* Build 'DEST = 0' and insert. */
767 if (dest)
769 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
770 gimple_set_location (g, loc);
771 gimple_set_block (g, gimple_block (stmt));
772 gsi_insert_before (gsi, g, GSI_SAME_STMT);
775 /* Build 'goto CONT_LABEL' and insert. */
776 g = gimple_build_goto (cont_label);
777 gsi_insert_before (gsi, g, GSI_SAME_STMT);
779 /* Build 'NEXT_LABEL:' and insert. */
780 g = gimple_build_label (next_label);
781 gsi_insert_before (gsi, g, GSI_SAME_STMT);
783 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
784 arg = build_addr (next_label, current_function_decl);
785 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
786 g = gimple_build_call (t, 1, arg);
787 gimple_set_location (g, loc);
788 gimple_set_block (g, gimple_block (stmt));
789 gsi_insert_before (gsi, g, GSI_SAME_STMT);
791 /* Build 'DEST = 1' and insert. */
792 if (dest)
794 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
795 integer_one_node));
796 gimple_set_location (g, loc);
797 gimple_set_block (g, gimple_block (stmt));
798 gsi_insert_before (gsi, g, GSI_SAME_STMT);
801 /* Build 'CONT_LABEL:' and insert. */
802 g = gimple_build_label (cont_label);
803 gsi_insert_before (gsi, g, GSI_SAME_STMT);
805 /* Remove the call to __builtin_setjmp. */
806 gsi_remove (gsi, false);
809 /* Lower calls to posix_memalign to
810 res = posix_memalign (ptr, align, size);
811 if (res == 0)
812 *ptr = __builtin_assume_aligned (*ptr, align);
813 or to
814 void *tem;
815 res = posix_memalign (&tem, align, size);
816 if (res == 0)
817 ptr = __builtin_assume_aligned (tem, align);
818 in case the first argument was &ptr. That way we can get at the
819 alignment of the heap pointer in CCP. */
821 static void
822 lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
824 gimple stmt, call = gsi_stmt (*gsi);
825 tree pptr = gimple_call_arg (call, 0);
826 tree align = gimple_call_arg (call, 1);
827 tree res = gimple_call_lhs (call);
828 tree ptr = create_tmp_reg (ptr_type_node);
829 if (TREE_CODE (pptr) == ADDR_EXPR)
831 tree tem = create_tmp_var (ptr_type_node);
832 TREE_ADDRESSABLE (tem) = 1;
833 gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
834 stmt = gimple_build_assign (ptr, tem);
836 else
837 stmt = gimple_build_assign (ptr,
838 fold_build2 (MEM_REF, ptr_type_node, pptr,
839 build_int_cst (ptr_type_node, 0)));
840 if (res == NULL_TREE)
842 res = create_tmp_reg (integer_type_node);
843 gimple_call_set_lhs (call, res);
845 tree align_label = create_artificial_label (UNKNOWN_LOCATION);
846 tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
847 gimple cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
848 align_label, noalign_label);
849 gsi_insert_after (gsi, cond, GSI_NEW_STMT);
850 gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
851 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
852 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
853 2, ptr, align);
854 gimple_call_set_lhs (stmt, ptr);
855 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
856 stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
857 build_int_cst (ptr_type_node, 0)),
858 ptr);
859 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
860 gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
864 /* Record the variables in VARS into function FN. */
866 void
867 record_vars_into (tree vars, tree fn)
869 for (; vars; vars = DECL_CHAIN (vars))
871 tree var = vars;
873 /* BIND_EXPRs contains also function/type/constant declarations
874 we don't need to care about. */
875 if (TREE_CODE (var) != VAR_DECL)
876 continue;
878 /* Nothing to do in this case. */
879 if (DECL_EXTERNAL (var))
880 continue;
882 /* Record the variable. */
883 add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
888 /* Record the variables in VARS into current_function_decl. */
890 void
891 record_vars (tree vars)
893 record_vars_into (vars, current_function_decl);