OpenACC deviceptr clause: Remove bogus assertion.
[official-gcc.git] / gcc / gimple-low.c
blob0f3343b6a7b9e8dceb33b267f046ffe00441e427
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_OACC_KERNELS:
362 case GIMPLE_OACC_PARALLEL:
363 case GIMPLE_OMP_PARALLEL:
364 case GIMPLE_OMP_TASK:
365 case GIMPLE_OMP_TARGET:
366 case GIMPLE_OMP_TEAMS:
367 data->cannot_fallthru = false;
368 lower_omp_directive (gsi, data);
369 data->cannot_fallthru = false;
370 return;
372 case GIMPLE_TRANSACTION:
373 lower_sequence (gimple_transaction_body_ptr (stmt), data);
374 break;
376 default:
377 gcc_unreachable ();
380 data->cannot_fallthru = false;
381 gsi_next (gsi);
384 /* Lower a bind_expr TSI. DATA is passed through the recursion. */
386 static void
387 lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
389 tree old_block = data->block;
390 gimple stmt = gsi_stmt (*gsi);
391 tree new_block = gimple_bind_block (stmt);
393 if (new_block)
395 if (new_block == old_block)
397 /* The outermost block of the original function may not be the
398 outermost statement chain of the gimplified function. So we
399 may see the outermost block just inside the function. */
400 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
401 new_block = NULL;
403 else
405 /* We do not expect to handle duplicate blocks. */
406 gcc_assert (!TREE_ASM_WRITTEN (new_block));
407 TREE_ASM_WRITTEN (new_block) = 1;
409 /* Block tree may get clobbered by inlining. Normally this would
410 be fixed in rest_of_decl_compilation using block notes, but
411 since we are not going to emit them, it is up to us. */
412 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
413 BLOCK_SUBBLOCKS (old_block) = new_block;
414 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
415 BLOCK_SUPERCONTEXT (new_block) = old_block;
417 data->block = new_block;
421 record_vars (gimple_bind_vars (stmt));
422 lower_sequence (gimple_bind_body_ptr (stmt), data);
424 if (new_block)
426 gcc_assert (data->block == new_block);
428 BLOCK_SUBBLOCKS (new_block)
429 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
430 data->block = old_block;
433 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
434 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
435 gsi_remove (gsi, false);
438 /* Same as above, but for a GIMPLE_TRY_CATCH. */
440 static void
441 lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
443 bool cannot_fallthru;
444 gimple stmt = gsi_stmt (*gsi);
445 gimple_stmt_iterator i;
447 /* We don't handle GIMPLE_TRY_FINALLY. */
448 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
450 lower_sequence (gimple_try_eval_ptr (stmt), data);
451 cannot_fallthru = data->cannot_fallthru;
453 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
454 switch (gimple_code (gsi_stmt (i)))
456 case GIMPLE_CATCH:
457 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
458 catch expression and a body. The whole try/catch may fall
459 through iff any of the catch bodies falls through. */
460 for (; !gsi_end_p (i); gsi_next (&i))
462 data->cannot_fallthru = false;
463 lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data);
464 if (!data->cannot_fallthru)
465 cannot_fallthru = false;
467 break;
469 case GIMPLE_EH_FILTER:
470 /* The exception filter expression only matters if there is an
471 exception. If the exception does not match EH_FILTER_TYPES,
472 we will execute EH_FILTER_FAILURE, and we will fall through
473 if that falls through. If the exception does match
474 EH_FILTER_TYPES, the stack unwinder will continue up the
475 stack, so we will not fall through. We don't know whether we
476 will throw an exception which matches EH_FILTER_TYPES or not,
477 so we just ignore EH_FILTER_TYPES and assume that we might
478 throw an exception which doesn't match. */
479 data->cannot_fallthru = false;
480 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
481 if (!data->cannot_fallthru)
482 cannot_fallthru = false;
483 break;
485 default:
486 /* This case represents statements to be executed when an
487 exception occurs. Those statements are implicitly followed
488 by a GIMPLE_RESX to resume execution after the exception. So
489 in this case the try/catch never falls through. */
490 data->cannot_fallthru = false;
491 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
492 break;
495 data->cannot_fallthru = cannot_fallthru;
496 gsi_next (gsi);
500 /* Try to determine whether a TRY_CATCH expression can fall through.
501 This is a subroutine of gimple_stmt_may_fallthru. */
503 static bool
504 gimple_try_catch_may_fallthru (gimple stmt)
506 gimple_stmt_iterator i;
508 /* We don't handle GIMPLE_TRY_FINALLY. */
509 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
511 /* If the TRY block can fall through, the whole TRY_CATCH can
512 fall through. */
513 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
514 return true;
516 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
517 switch (gimple_code (gsi_stmt (i)))
519 case GIMPLE_CATCH:
520 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
521 catch expression and a body. The whole try/catch may fall
522 through iff any of the catch bodies falls through. */
523 for (; !gsi_end_p (i); gsi_next (&i))
525 if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
526 return true;
528 return false;
530 case GIMPLE_EH_FILTER:
531 /* The exception filter expression only matters if there is an
532 exception. If the exception does not match EH_FILTER_TYPES,
533 we will execute EH_FILTER_FAILURE, and we will fall through
534 if that falls through. If the exception does match
535 EH_FILTER_TYPES, the stack unwinder will continue up the
536 stack, so we will not fall through. We don't know whether we
537 will throw an exception which matches EH_FILTER_TYPES or not,
538 so we just ignore EH_FILTER_TYPES and assume that we might
539 throw an exception which doesn't match. */
540 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
542 default:
543 /* This case represents statements to be executed when an
544 exception occurs. Those statements are implicitly followed
545 by a GIMPLE_RESX to resume execution after the exception. So
546 in this case the try/catch never falls through. */
547 return false;
552 /* Try to determine if we can continue executing the statement
553 immediately following STMT. This guess need not be 100% accurate;
554 simply be conservative and return true if we don't know. This is
555 used only to avoid stupidly generating extra code. If we're wrong,
556 we'll just delete the extra code later. */
558 bool
559 gimple_stmt_may_fallthru (gimple stmt)
561 if (!stmt)
562 return true;
564 switch (gimple_code (stmt))
566 case GIMPLE_GOTO:
567 case GIMPLE_RETURN:
568 case GIMPLE_RESX:
569 /* Easy cases. If the last statement of the seq implies
570 control transfer, then we can't fall through. */
571 return false;
573 case GIMPLE_SWITCH:
574 /* Switch has already been lowered and represents a branch
575 to a selected label and hence can't fall through. */
576 return false;
578 case GIMPLE_COND:
579 /* GIMPLE_COND's are already lowered into a two-way branch. They
580 can't fall through. */
581 return false;
583 case GIMPLE_BIND:
584 return gimple_seq_may_fallthru (gimple_bind_body (stmt));
586 case GIMPLE_TRY:
587 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
588 return gimple_try_catch_may_fallthru (stmt);
590 /* It must be a GIMPLE_TRY_FINALLY. */
592 /* The finally clause is always executed after the try clause,
593 so if it does not fall through, then the try-finally will not
594 fall through. Otherwise, if the try clause does not fall
595 through, then when the finally clause falls through it will
596 resume execution wherever the try clause was going. So the
597 whole try-finally will only fall through if both the try
598 clause and the finally clause fall through. */
599 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
600 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
602 case GIMPLE_EH_ELSE:
603 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
604 || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
606 case GIMPLE_CALL:
607 /* Functions that do not return do not fall through. */
608 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
610 default:
611 return true;
616 /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
618 bool
619 gimple_seq_may_fallthru (gimple_seq seq)
621 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
625 /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
627 static void
628 lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
630 gimple stmt = gsi_stmt (*gsi);
631 gimple t;
632 int i;
633 return_statements_t tmp_rs;
635 /* Match this up with an existing return statement that's been created. */
636 for (i = data->return_statements.length () - 1;
637 i >= 0; i--)
639 tmp_rs = data->return_statements[i];
641 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
643 /* Remove the line number from the representative return statement.
644 It now fills in for many such returns. Failure to remove this
645 will result in incorrect results for coverage analysis. */
646 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
648 goto found;
652 /* Not found. Create a new label and record the return statement. */
653 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
654 tmp_rs.stmt = stmt;
655 data->return_statements.safe_push (tmp_rs);
657 /* Generate a goto statement and remove the return statement. */
658 found:
659 /* When not optimizing, make sure user returns are preserved. */
660 if (!optimize && gimple_has_location (stmt))
661 DECL_ARTIFICIAL (tmp_rs.label) = 0;
662 t = gimple_build_goto (tmp_rs.label);
663 gimple_set_location (t, gimple_location (stmt));
664 gimple_set_block (t, gimple_block (stmt));
665 gsi_insert_before (gsi, t, GSI_SAME_STMT);
666 gsi_remove (gsi, false);
669 /* Lower a __builtin_setjmp GSI.
671 __builtin_setjmp is passed a pointer to an array of five words (not
672 all will be used on all machines). It operates similarly to the C
673 library function of the same name, but is more efficient.
675 It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
676 __builtin_setjmp_receiver.
678 After full lowering, the body of the function should look like:
681 int D.1844;
682 int D.2844;
684 [...]
686 __builtin_setjmp_setup (&buf, &<D1847>);
687 D.1844 = 0;
688 goto <D1846>;
689 <D1847>:;
690 __builtin_setjmp_receiver (&<D1847>);
691 D.1844 = 1;
692 <D1846>:;
693 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
695 [...]
697 __builtin_setjmp_setup (&buf, &<D2847>);
698 D.2844 = 0;
699 goto <D2846>;
700 <D2847>:;
701 __builtin_setjmp_receiver (&<D2847>);
702 D.2844 = 1;
703 <D2846>:;
704 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
706 [...]
708 <D3850>:;
709 return;
712 During cfg creation an extra per-function (or per-OpenMP region)
713 block with ABNORMAL_DISPATCHER internal call will be added, unique
714 destination of all the abnormal call edges and the unique source of
715 all the abnormal edges to the receivers, thus keeping the complexity
716 explosion localized. */
718 static void
719 lower_builtin_setjmp (gimple_stmt_iterator *gsi)
721 gimple stmt = gsi_stmt (*gsi);
722 location_t loc = gimple_location (stmt);
723 tree cont_label = create_artificial_label (loc);
724 tree next_label = create_artificial_label (loc);
725 tree dest, t, arg;
726 gimple g;
728 /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
729 these builtins are modelled as non-local label jumps to the label
730 that is passed to these two builtins, so pretend we have a non-local
731 label during GIMPLE passes too. See PR60003. */
732 cfun->has_nonlocal_label = 1;
734 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
735 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
736 FORCED_LABEL (next_label) = 1;
738 dest = gimple_call_lhs (stmt);
740 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
741 arg = build_addr (next_label, current_function_decl);
742 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
743 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
744 gimple_set_location (g, loc);
745 gimple_set_block (g, gimple_block (stmt));
746 gsi_insert_before (gsi, g, GSI_SAME_STMT);
748 /* Build 'DEST = 0' and insert. */
749 if (dest)
751 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
752 gimple_set_location (g, loc);
753 gimple_set_block (g, gimple_block (stmt));
754 gsi_insert_before (gsi, g, GSI_SAME_STMT);
757 /* Build 'goto CONT_LABEL' and insert. */
758 g = gimple_build_goto (cont_label);
759 gsi_insert_before (gsi, g, GSI_SAME_STMT);
761 /* Build 'NEXT_LABEL:' and insert. */
762 g = gimple_build_label (next_label);
763 gsi_insert_before (gsi, g, GSI_SAME_STMT);
765 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
766 arg = build_addr (next_label, current_function_decl);
767 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
768 g = gimple_build_call (t, 1, arg);
769 gimple_set_location (g, loc);
770 gimple_set_block (g, gimple_block (stmt));
771 gsi_insert_before (gsi, g, GSI_SAME_STMT);
773 /* Build 'DEST = 1' and insert. */
774 if (dest)
776 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
777 integer_one_node));
778 gimple_set_location (g, loc);
779 gimple_set_block (g, gimple_block (stmt));
780 gsi_insert_before (gsi, g, GSI_SAME_STMT);
783 /* Build 'CONT_LABEL:' and insert. */
784 g = gimple_build_label (cont_label);
785 gsi_insert_before (gsi, g, GSI_SAME_STMT);
787 /* Remove the call to __builtin_setjmp. */
788 gsi_remove (gsi, false);
791 /* Lower calls to posix_memalign to
792 res = posix_memalign (ptr, align, size);
793 if (res == 0)
794 *ptr = __builtin_assume_aligned (*ptr, align);
795 or to
796 void *tem;
797 res = posix_memalign (&tem, align, size);
798 if (res == 0)
799 ptr = __builtin_assume_aligned (tem, align);
800 in case the first argument was &ptr. That way we can get at the
801 alignment of the heap pointer in CCP. */
803 static void
804 lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
806 gimple stmt, call = gsi_stmt (*gsi);
807 tree pptr = gimple_call_arg (call, 0);
808 tree align = gimple_call_arg (call, 1);
809 tree res = gimple_call_lhs (call);
810 tree ptr = create_tmp_reg (ptr_type_node, NULL);
811 if (TREE_CODE (pptr) == ADDR_EXPR)
813 tree tem = create_tmp_var (ptr_type_node, NULL);
814 TREE_ADDRESSABLE (tem) = 1;
815 gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
816 stmt = gimple_build_assign (ptr, tem);
818 else
819 stmt = gimple_build_assign (ptr,
820 fold_build2 (MEM_REF, ptr_type_node, pptr,
821 build_int_cst (ptr_type_node, 0)));
822 if (res == NULL_TREE)
824 res = create_tmp_reg (integer_type_node, NULL);
825 gimple_call_set_lhs (call, res);
827 tree align_label = create_artificial_label (UNKNOWN_LOCATION);
828 tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
829 gimple cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
830 align_label, noalign_label);
831 gsi_insert_after (gsi, cond, GSI_NEW_STMT);
832 gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
833 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
834 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
835 2, ptr, align);
836 gimple_call_set_lhs (stmt, ptr);
837 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
838 stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
839 build_int_cst (ptr_type_node, 0)),
840 ptr);
841 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
842 gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
846 /* Record the variables in VARS into function FN. */
848 void
849 record_vars_into (tree vars, tree fn)
851 for (; vars; vars = DECL_CHAIN (vars))
853 tree var = vars;
855 /* BIND_EXPRs contains also function/type/constant declarations
856 we don't need to care about. */
857 if (TREE_CODE (var) != VAR_DECL)
858 continue;
860 /* Nothing to do in this case. */
861 if (DECL_EXTERNAL (var))
862 continue;
864 /* Record the variable. */
865 add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
870 /* Record the variables in VARS into current_function_decl. */
872 void
873 record_vars (tree vars)
875 record_vars_into (vars, current_function_decl);