]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-low.c
[6/6] Preprocessor forced macro location
[thirdparty/gcc.git] / gcc / gimple-low.c
CommitLineData
726a989a 1/* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
6de9cd9a 2
85ec4feb 3 Copyright (C) 2003-2018 Free Software Foundation, Inc.
6de9cd9a
DN
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
6de9cd9a
DN
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
c7131fb2 24#include "backend.h"
6de9cd9a 25#include "tree.h"
c7131fb2 26#include "gimple.h"
957060b5 27#include "tree-pass.h"
40e23961 28#include "fold-const.h"
d8a2d370
DN
29#include "tree-nested.h"
30#include "calls.h"
5be5c238 31#include "gimple-iterator.h"
4484a35a 32#include "gimple-low.h"
e59a1c22
ML
33#include "predict.h"
34#include "gimple-predict.h"
6de9cd9a 35
726a989a
RB
36/* The differences between High GIMPLE and Low GIMPLE are the
37 following:
38
39 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
40
41 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
42 flow and exception regions are built as an on-the-side region
43 hierarchy (See tree-eh.c:lower_eh_constructs).
44
45 3- Multiple identical return statements are grouped into a single
46 return and gotos to the unique return site. */
47
48/* Match a return statement with a label. During lowering, we identify
49 identical return statements and replace duplicates with a jump to
50 the corresponding label. */
51struct return_statements_t
52{
53 tree label;
538dd0b7 54 greturn *stmt;
726a989a
RB
55};
56typedef struct return_statements_t return_statements_t;
57
726a989a 58
6de9cd9a
DN
59struct lower_data
60{
61 /* Block the current statement belongs to. */
62 tree block;
f5a76aea 63
726a989a 64 /* A vector of label and return statements to be moved to the end
71877985 65 of the function. */
9771b263 66 vec<return_statements_t> return_statements;
4f6c2131 67
a141816c
EB
68 /* True if the current statement cannot fall through. */
69 bool cannot_fallthru;
6de9cd9a
DN
70};
71
726a989a
RB
72static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
73static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
f778c049 74static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
726a989a
RB
75static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
76static void lower_builtin_setjmp (gimple_stmt_iterator *);
831806cb 77static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
6de9cd9a 78
726a989a
RB
79
80/* Lower the body of current_function_decl from High GIMPLE into Low
81 GIMPLE. */
6de9cd9a 82
c2924966 83static unsigned int
6de9cd9a
DN
84lower_function_body (void)
85{
86 struct lower_data data;
726a989a
RB
87 gimple_seq body = gimple_body (current_function_decl);
88 gimple_seq lowered_body;
89 gimple_stmt_iterator i;
355fe088
TS
90 gimple *bind;
91 gimple *x;
726a989a
RB
92
93 /* The gimplifier should've left a body of exactly one statement,
94 namely a GIMPLE_BIND. */
95 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
96 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
6de9cd9a 97
953ff289 98 memset (&data, 0, sizeof (data));
6de9cd9a
DN
99 data.block = DECL_INITIAL (current_function_decl);
100 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
101 BLOCK_CHAIN (data.block) = NULL_TREE;
102 TREE_ASM_WRITTEN (data.block) = 1;
9771b263 103 data.return_statements.create (8);
726a989a
RB
104
105 bind = gimple_seq_first_stmt (body);
106 lowered_body = NULL;
107 gimple_seq_add_stmt (&lowered_body, bind);
108 i = gsi_start (lowered_body);
109 lower_gimple_bind (&i, &data);
6de9cd9a 110
726a989a 111 i = gsi_last (lowered_body);
ff98621c 112
96a95ac1
AO
113 /* If we had begin stmt markers from e.g. PCH, but this compilation
114 doesn't want them, lower_stmt will have cleaned them up; we can
115 now clear the flag that indicates we had them. */
116 if (!MAY_HAVE_DEBUG_MARKER_STMTS && cfun->debug_nonbind_markers)
117 {
118 /* This counter needs not be exact, but before lowering it will
119 most certainly be. */
120 gcc_assert (cfun->debug_marker_count == 0);
121 cfun->debug_nonbind_markers = false;
122 }
123
ff98621c 124 /* If the function falls off the end, we need a null return statement.
726a989a 125 If we've already got one in the return_statements vector, we don't
ff98621c 126 need to do anything special. Otherwise build one by hand. */
67b69814
EB
127 bool may_fallthru = gimple_seq_may_fallthru (lowered_body);
128 if (may_fallthru
9771b263 129 && (data.return_statements.is_empty ()
c3284718
RS
130 || (gimple_return_retval (data.return_statements.last().stmt)
131 != NULL)))
ff98621c 132 {
726a989a
RB
133 x = gimple_build_return (NULL);
134 gimple_set_location (x, cfun->function_end_locus);
cc2a64dd 135 gimple_set_block (x, DECL_INITIAL (current_function_decl));
726a989a 136 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
67b69814 137 may_fallthru = false;
ff98621c
RH
138 }
139
140 /* If we lowered any return statements, emit the representative
141 at the end of the function. */
9771b263 142 while (!data.return_statements.is_empty ())
f5a76aea 143 {
9771b263 144 return_statements_t t = data.return_statements.pop ();
726a989a
RB
145 x = gimple_build_label (t.label);
146 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
726a989a 147 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
67b69814
EB
148 if (may_fallthru)
149 {
150 /* Remove the line number from the representative return statement.
151 It now fills in for the fallthru too. Failure to remove this
152 will result in incorrect results for coverage analysis. */
153 gimple_set_location (t.stmt, UNKNOWN_LOCATION);
154 may_fallthru = false;
155 }
f5a76aea
RH
156 }
157
355a7673
MM
158 /* Once the old body has been lowered, replace it with the new
159 lowered sequence. */
160 gimple_set_body (current_function_decl, lowered_body);
161
282899df 162 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
6de9cd9a
DN
163 BLOCK_SUBBLOCKS (data.block)
164 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
165
166 clear_block_marks (data.block);
9771b263 167 data.return_statements.release ();
c2924966 168 return 0;
6de9cd9a
DN
169}
170
27a4cd48
DM
171namespace {
172
173const pass_data pass_data_lower_cf =
6de9cd9a 174{
27a4cd48
DM
175 GIMPLE_PASS, /* type */
176 "lower", /* name */
177 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
178 TV_NONE, /* tv_id */
179 PROP_gimple_any, /* properties_required */
180 PROP_gimple_lcf, /* properties_provided */
181 0, /* properties_destroyed */
182 0, /* todo_flags_start */
183 0, /* todo_flags_finish */
6de9cd9a
DN
184};
185
27a4cd48
DM
186class pass_lower_cf : public gimple_opt_pass
187{
188public:
c3284718
RS
189 pass_lower_cf (gcc::context *ctxt)
190 : gimple_opt_pass (pass_data_lower_cf, ctxt)
27a4cd48
DM
191 {}
192
193 /* opt_pass methods: */
be55bfe6 194 virtual unsigned int execute (function *) { return lower_function_body (); }
27a4cd48
DM
195
196}; // class pass_lower_cf
197
198} // anon namespace
199
200gimple_opt_pass *
201make_pass_lower_cf (gcc::context *ctxt)
202{
203 return new pass_lower_cf (ctxt);
204}
205
726a989a 206/* Lower sequence SEQ. Unlike gimplification the statements are not relowered
6de9cd9a
DN
207 when they are changed -- if this has to be done, the lowering routine must
208 do it explicitly. DATA is passed through the recursion. */
209
1ebf7687 210static void
355a7673 211lower_sequence (gimple_seq *seq, struct lower_data *data)
6de9cd9a 212{
726a989a 213 gimple_stmt_iterator gsi;
6de9cd9a 214
355a7673 215 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
726a989a 216 lower_stmt (&gsi, data);
6de9cd9a
DN
217}
218
50674e96 219
726a989a 220/* Lower the OpenMP directive statement pointed by GSI. DATA is
50674e96
DN
221 passed through the recursion. */
222
223static void
726a989a 224lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
50674e96 225{
355fe088 226 gimple *stmt;
b8698a0f 227
726a989a 228 stmt = gsi_stmt (*gsi);
50674e96 229
355a7673
MM
230 lower_sequence (gimple_omp_body_ptr (stmt), data);
231 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
726a989a 232 gimple_omp_set_body (stmt, NULL);
355a7673 233 gsi_next (gsi);
50674e96
DN
234}
235
236
a141816c
EB
237/* Lower statement GSI. DATA is passed through the recursion. We try to
238 track the fallthruness of statements and get rid of unreachable return
239 statements in order to prevent the EH lowering pass from adding useless
240 edges that can cause bogus warnings to be issued later; this guess need
241 not be 100% accurate, simply be conservative and reset cannot_fallthru
242 to false if we don't know. */
6de9cd9a
DN
243
244static void
726a989a 245lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
6de9cd9a 246{
355fe088 247 gimple *stmt = gsi_stmt (*gsi);
6de9cd9a 248
726a989a 249 gimple_set_block (stmt, data->block);
6de9cd9a 250
726a989a 251 switch (gimple_code (stmt))
6de9cd9a 252 {
726a989a
RB
253 case GIMPLE_BIND:
254 lower_gimple_bind (gsi, data);
a141816c 255 /* Propagate fallthruness. */
f5a76aea 256 return;
6de9cd9a 257
726a989a 258 case GIMPLE_COND:
a141816c
EB
259 case GIMPLE_GOTO:
260 case GIMPLE_SWITCH:
261 data->cannot_fallthru = true;
262 gsi_next (gsi);
263 return;
726a989a
RB
264
265 case GIMPLE_RETURN:
a141816c
EB
266 if (data->cannot_fallthru)
267 {
268 gsi_remove (gsi, false);
269 /* Propagate fallthruness. */
270 }
271 else
272 {
273 lower_gimple_return (gsi, data);
274 data->cannot_fallthru = true;
275 }
726a989a
RB
276 return;
277
278 case GIMPLE_TRY:
f778c049
EB
279 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
280 lower_try_catch (gsi, data);
281 else
282 {
283 /* It must be a GIMPLE_TRY_FINALLY. */
284 bool cannot_fallthru;
285 lower_sequence (gimple_try_eval_ptr (stmt), data);
286 cannot_fallthru = data->cannot_fallthru;
287
288 /* The finally clause is always executed after the try clause,
289 so if it does not fall through, then the try-finally will not
290 fall through. Otherwise, if the try clause does not fall
291 through, then when the finally clause falls through it will
292 resume execution wherever the try clause was going. So the
293 whole try-finally will only fall through if both the try
294 clause and the finally clause fall through. */
295 data->cannot_fallthru = false;
296 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
297 data->cannot_fallthru |= cannot_fallthru;
298 gsi_next (gsi);
299 }
300 return;
777f7f9a 301
0a35513e 302 case GIMPLE_EH_ELSE:
538dd0b7
DM
303 {
304 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
305 lower_sequence (gimple_eh_else_n_body_ptr (eh_else_stmt), data);
306 lower_sequence (gimple_eh_else_e_body_ptr (eh_else_stmt), data);
307 }
0a35513e
AH
308 break;
309
96a95ac1
AO
310 case GIMPLE_DEBUG:
311 gcc_checking_assert (cfun->debug_nonbind_markers);
312 /* We can't possibly have debug bind stmts before lowering, we
313 first emit them when entering SSA. */
314 gcc_checking_assert (gimple_debug_nonbind_marker_p (stmt));
315 /* Propagate fallthruness. */
316 /* If the function (e.g. from PCH) had debug stmts, but they're
317 disabled for this compilation, remove them. */
318 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
319 gsi_remove (gsi, true);
320 else
321 gsi_next (gsi);
322 return;
323
726a989a
RB
324 case GIMPLE_NOP:
325 case GIMPLE_ASM:
326 case GIMPLE_ASSIGN:
726a989a
RB
327 case GIMPLE_PREDICT:
328 case GIMPLE_LABEL:
1d65f45c 329 case GIMPLE_EH_MUST_NOT_THROW:
726a989a
RB
330 case GIMPLE_OMP_FOR:
331 case GIMPLE_OMP_SECTIONS:
332 case GIMPLE_OMP_SECTIONS_SWITCH:
333 case GIMPLE_OMP_SECTION:
334 case GIMPLE_OMP_SINGLE:
335 case GIMPLE_OMP_MASTER:
acf0174b 336 case GIMPLE_OMP_TASKGROUP:
726a989a
RB
337 case GIMPLE_OMP_ORDERED:
338 case GIMPLE_OMP_CRITICAL:
339 case GIMPLE_OMP_RETURN:
340 case GIMPLE_OMP_ATOMIC_LOAD:
341 case GIMPLE_OMP_ATOMIC_STORE:
342 case GIMPLE_OMP_CONTINUE:
343 break;
4f6c2131 344
726a989a 345 case GIMPLE_CALL:
4f6c2131 346 {
726a989a 347 tree decl = gimple_call_fndecl (stmt);
f16dd822
DC
348 unsigned i;
349
350 for (i = 0; i < gimple_call_num_args (stmt); i++)
351 {
352 tree arg = gimple_call_arg (stmt, i);
353 if (EXPR_P (arg))
354 TREE_SET_BLOCK (arg, data->block);
355 }
726a989a 356
4f6c2131 357 if (decl
3d78e008 358 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
4f6c2131 359 {
903c723b 360 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
831806cb
RB
361 {
362 lower_builtin_setjmp (gsi);
363 data->cannot_fallthru = false;
364 return;
903c723b
TC
365 }
366 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
367 && flag_tree_bit_ccp
368 && gimple_builtin_call_types_compatible_p (stmt, decl))
369 {
370 lower_builtin_posix_memalign (gsi);
831806cb
RB
371 return;
372 }
4f6c2131 373 }
79ddec02 374
79ddec02
EB
375 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
376 {
a141816c 377 data->cannot_fallthru = true;
79ddec02 378 gsi_next (gsi);
79ddec02
EB
379 return;
380 }
4f6c2131
EB
381 }
382 break;
383
726a989a
RB
384 case GIMPLE_OMP_PARALLEL:
385 case GIMPLE_OMP_TASK:
acf0174b
JJ
386 case GIMPLE_OMP_TARGET:
387 case GIMPLE_OMP_TEAMS:
b2b40051 388 case GIMPLE_OMP_GRID_BODY:
a141816c 389 data->cannot_fallthru = false;
726a989a 390 lower_omp_directive (gsi, data);
a141816c 391 data->cannot_fallthru = false;
50674e96
DN
392 return;
393
0a35513e 394 case GIMPLE_TRANSACTION:
538dd0b7
DM
395 lower_sequence (gimple_transaction_body_ptr (
396 as_a <gtransaction *> (stmt)),
397 data);
0a35513e
AH
398 break;
399
6de9cd9a 400 default:
282899df 401 gcc_unreachable ();
6de9cd9a
DN
402 }
403
a141816c 404 data->cannot_fallthru = false;
726a989a 405 gsi_next (gsi);
6de9cd9a
DN
406}
407
4f6c2131 408/* Lower a bind_expr TSI. DATA is passed through the recursion. */
6de9cd9a
DN
409
410static void
726a989a 411lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
6de9cd9a
DN
412{
413 tree old_block = data->block;
538dd0b7 414 gbind *stmt = as_a <gbind *> (gsi_stmt (*gsi));
726a989a 415 tree new_block = gimple_bind_block (stmt);
6de9cd9a
DN
416
417 if (new_block)
418 {
419 if (new_block == old_block)
420 {
421 /* The outermost block of the original function may not be the
422 outermost statement chain of the gimplified function. So we
423 may see the outermost block just inside the function. */
282899df 424 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
6de9cd9a
DN
425 new_block = NULL;
426 }
427 else
428 {
429 /* We do not expect to handle duplicate blocks. */
282899df 430 gcc_assert (!TREE_ASM_WRITTEN (new_block));
6de9cd9a
DN
431 TREE_ASM_WRITTEN (new_block) = 1;
432
433 /* Block tree may get clobbered by inlining. Normally this would
434 be fixed in rest_of_decl_compilation using block notes, but
435 since we are not going to emit them, it is up to us. */
436 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
437 BLOCK_SUBBLOCKS (old_block) = new_block;
438 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
439 BLOCK_SUPERCONTEXT (new_block) = old_block;
440
441 data->block = new_block;
442 }
443 }
444
726a989a 445 record_vars (gimple_bind_vars (stmt));
014b59e6
RB
446
447 /* Scrap DECL_CHAIN up to BLOCK_VARS to ease GC after we no longer
448 need gimple_bind_vars. */
449 tree next;
b13ff1f5
RB
450 /* BLOCK_VARS and gimple_bind_vars share a common sub-chain. Find
451 it by marking all BLOCK_VARS. */
014b59e6 452 if (gimple_bind_block (stmt))
b13ff1f5
RB
453 for (tree t = BLOCK_VARS (gimple_bind_block (stmt)); t; t = DECL_CHAIN (t))
454 TREE_VISITED (t) = 1;
455 for (tree var = gimple_bind_vars (stmt);
456 var && ! TREE_VISITED (var); var = next)
014b59e6 457 {
014b59e6
RB
458 next = DECL_CHAIN (var);
459 DECL_CHAIN (var) = NULL_TREE;
460 }
b13ff1f5
RB
461 /* Unmark BLOCK_VARS. */
462 if (gimple_bind_block (stmt))
463 for (tree t = BLOCK_VARS (gimple_bind_block (stmt)); t; t = DECL_CHAIN (t))
464 TREE_VISITED (t) = 0;
014b59e6 465
355a7673 466 lower_sequence (gimple_bind_body_ptr (stmt), data);
6de9cd9a
DN
467
468 if (new_block)
469 {
282899df 470 gcc_assert (data->block == new_block);
6de9cd9a
DN
471
472 BLOCK_SUBBLOCKS (new_block)
473 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
474 data->block = old_block;
475 }
476
726a989a
RB
477 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
478 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
479 gsi_remove (gsi, false);
6de9cd9a
DN
480}
481
f778c049
EB
482/* Same as above, but for a GIMPLE_TRY_CATCH. */
483
484static void
485lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
486{
487 bool cannot_fallthru;
355fe088 488 gimple *stmt = gsi_stmt (*gsi);
f778c049
EB
489 gimple_stmt_iterator i;
490
491 /* We don't handle GIMPLE_TRY_FINALLY. */
492 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
493
494 lower_sequence (gimple_try_eval_ptr (stmt), data);
495 cannot_fallthru = data->cannot_fallthru;
496
497 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
498 switch (gimple_code (gsi_stmt (i)))
499 {
500 case GIMPLE_CATCH:
501 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
502 catch expression and a body. The whole try/catch may fall
503 through iff any of the catch bodies falls through. */
504 for (; !gsi_end_p (i); gsi_next (&i))
505 {
506 data->cannot_fallthru = false;
538dd0b7
DM
507 lower_sequence (gimple_catch_handler_ptr (
508 as_a <gcatch *> (gsi_stmt (i))),
509 data);
f778c049
EB
510 if (!data->cannot_fallthru)
511 cannot_fallthru = false;
512 }
513 break;
514
515 case GIMPLE_EH_FILTER:
516 /* The exception filter expression only matters if there is an
517 exception. If the exception does not match EH_FILTER_TYPES,
518 we will execute EH_FILTER_FAILURE, and we will fall through
519 if that falls through. If the exception does match
520 EH_FILTER_TYPES, the stack unwinder will continue up the
521 stack, so we will not fall through. We don't know whether we
522 will throw an exception which matches EH_FILTER_TYPES or not,
523 so we just ignore EH_FILTER_TYPES and assume that we might
524 throw an exception which doesn't match. */
525 data->cannot_fallthru = false;
526 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
527 if (!data->cannot_fallthru)
528 cannot_fallthru = false;
529 break;
530
96a95ac1
AO
531 case GIMPLE_DEBUG:
532 gcc_checking_assert (gimple_debug_begin_stmt_p (stmt));
533 break;
534
f778c049
EB
535 default:
536 /* This case represents statements to be executed when an
537 exception occurs. Those statements are implicitly followed
538 by a GIMPLE_RESX to resume execution after the exception. So
539 in this case the try/catch never falls through. */
540 data->cannot_fallthru = false;
541 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
542 break;
543 }
544
545 data->cannot_fallthru = cannot_fallthru;
546 gsi_next (gsi);
547}
548
6737ba67 549
cf2d1b38
AM
550/* Try to determine whether a TRY_CATCH expression can fall through.
551 This is a subroutine of gimple_stmt_may_fallthru. */
726a989a
RB
552
553static bool
538dd0b7 554gimple_try_catch_may_fallthru (gtry *stmt)
726a989a
RB
555{
556 gimple_stmt_iterator i;
557
558 /* We don't handle GIMPLE_TRY_FINALLY. */
559 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
560
561 /* If the TRY block can fall through, the whole TRY_CATCH can
562 fall through. */
563 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
564 return true;
565
355a7673 566 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
726a989a
RB
567 switch (gimple_code (gsi_stmt (i)))
568 {
569 case GIMPLE_CATCH:
570 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
571 catch expression and a body. The whole try/catch may fall
572 through iff any of the catch bodies falls through. */
573 for (; !gsi_end_p (i); gsi_next (&i))
574 {
538dd0b7
DM
575 if (gimple_seq_may_fallthru (gimple_catch_handler (
576 as_a <gcatch *> (gsi_stmt (i)))))
726a989a
RB
577 return true;
578 }
579 return false;
580
581 case GIMPLE_EH_FILTER:
582 /* The exception filter expression only matters if there is an
583 exception. If the exception does not match EH_FILTER_TYPES,
584 we will execute EH_FILTER_FAILURE, and we will fall through
585 if that falls through. If the exception does match
586 EH_FILTER_TYPES, the stack unwinder will continue up the
587 stack, so we will not fall through. We don't know whether we
588 will throw an exception which matches EH_FILTER_TYPES or not,
589 so we just ignore EH_FILTER_TYPES and assume that we might
590 throw an exception which doesn't match. */
591 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
592
593 default:
594 /* This case represents statements to be executed when an
595 exception occurs. Those statements are implicitly followed
596 by a GIMPLE_RESX to resume execution after the exception. So
597 in this case the try/catch never falls through. */
598 return false;
599 }
600}
601
602
726a989a
RB
603/* Try to determine if we can continue executing the statement
604 immediately following STMT. This guess need not be 100% accurate;
605 simply be conservative and return true if we don't know. This is
606 used only to avoid stupidly generating extra code. If we're wrong,
607 we'll just delete the extra code later. */
608
609bool
355fe088 610gimple_stmt_may_fallthru (gimple *stmt)
6de9cd9a 611{
726a989a
RB
612 if (!stmt)
613 return true;
6de9cd9a 614
726a989a
RB
615 switch (gimple_code (stmt))
616 {
617 case GIMPLE_GOTO:
618 case GIMPLE_RETURN:
619 case GIMPLE_RESX:
b8698a0f 620 /* Easy cases. If the last statement of the seq implies
726a989a
RB
621 control transfer, then we can't fall through. */
622 return false;
6de9cd9a 623
726a989a 624 case GIMPLE_SWITCH:
a141816c
EB
625 /* Switch has already been lowered and represents a branch
626 to a selected label and hence can't fall through. */
627 return false;
6de9cd9a 628
726a989a
RB
629 case GIMPLE_COND:
630 /* GIMPLE_COND's are already lowered into a two-way branch. They
631 can't fall through. */
632 return false;
6de9cd9a 633
726a989a 634 case GIMPLE_BIND:
538dd0b7
DM
635 return gimple_seq_may_fallthru (
636 gimple_bind_body (as_a <gbind *> (stmt)));
6de9cd9a 637
726a989a
RB
638 case GIMPLE_TRY:
639 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
538dd0b7 640 return gimple_try_catch_may_fallthru (as_a <gtry *> (stmt));
6de9cd9a 641
726a989a 642 /* It must be a GIMPLE_TRY_FINALLY. */
6de9cd9a 643
726a989a
RB
644 /* The finally clause is always executed after the try clause,
645 so if it does not fall through, then the try-finally will not
646 fall through. Otherwise, if the try clause does not fall
647 through, then when the finally clause falls through it will
648 resume execution wherever the try clause was going. So the
649 whole try-finally will only fall through if both the try
650 clause and the finally clause fall through. */
651 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
652 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
653
0a35513e 654 case GIMPLE_EH_ELSE:
538dd0b7
DM
655 {
656 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
657 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (eh_else_stmt))
658 || gimple_seq_may_fallthru (gimple_eh_else_e_body (
659 eh_else_stmt)));
660 }
0a35513e 661
726a989a
RB
662 case GIMPLE_CALL:
663 /* Functions that do not return do not fall through. */
865f7046 664 return !gimple_call_noreturn_p (stmt);
a141816c 665
726a989a
RB
666 default:
667 return true;
6de9cd9a 668 }
726a989a
RB
669}
670
6de9cd9a 671
726a989a 672/* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
6de9cd9a 673
726a989a
RB
674bool
675gimple_seq_may_fallthru (gimple_seq seq)
676{
65f4b875 677 return gimple_stmt_may_fallthru (gimple_seq_last_nondebug_stmt (seq));
6de9cd9a 678}
f5a76aea 679
726a989a
RB
680
681/* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
4f6c2131 682
f5a76aea 683static void
726a989a 684lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
f5a76aea 685{
538dd0b7 686 greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
355fe088 687 gimple *t;
726a989a
RB
688 int i;
689 return_statements_t tmp_rs;
f5a76aea 690
71877985 691 /* Match this up with an existing return statement that's been created. */
9771b263 692 for (i = data->return_statements.length () - 1;
726a989a 693 i >= 0; i--)
f5a76aea 694 {
9771b263 695 tmp_rs = data->return_statements[i];
71877985 696
726a989a 697 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
0efb9d64
AK
698 {
699 /* Remove the line number from the representative return statement.
700 It now fills in for many such returns. Failure to remove this
701 will result in incorrect results for coverage analysis. */
702 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
703
704 goto found;
705 }
f5a76aea
RH
706 }
707
71877985 708 /* Not found. Create a new label and record the return statement. */
c2255bc4 709 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
726a989a 710 tmp_rs.stmt = stmt;
9771b263 711 data->return_statements.safe_push (tmp_rs);
71877985
RH
712
713 /* Generate a goto statement and remove the return statement. */
714 found:
516426da
EB
715 /* When not optimizing, make sure user returns are preserved. */
716 if (!optimize && gimple_has_location (stmt))
717 DECL_ARTIFICIAL (tmp_rs.label) = 0;
726a989a
RB
718 t = gimple_build_goto (tmp_rs.label);
719 gimple_set_location (t, gimple_location (stmt));
cc2a64dd 720 gimple_set_block (t, gimple_block (stmt));
726a989a
RB
721 gsi_insert_before (gsi, t, GSI_SAME_STMT);
722 gsi_remove (gsi, false);
4f6c2131
EB
723}
724
a141816c 725/* Lower a __builtin_setjmp GSI.
4f6c2131
EB
726
727 __builtin_setjmp is passed a pointer to an array of five words (not
728 all will be used on all machines). It operates similarly to the C
729 library function of the same name, but is more efficient.
730
09b22f48
JJ
731 It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
732 __builtin_setjmp_receiver.
4f6c2131
EB
733
734 After full lowering, the body of the function should look like:
735
736 {
4f6c2131
EB
737 int D.1844;
738 int D.2844;
739
740 [...]
741
742 __builtin_setjmp_setup (&buf, &<D1847>);
743 D.1844 = 0;
744 goto <D1846>;
745 <D1847>:;
746 __builtin_setjmp_receiver (&<D1847>);
747 D.1844 = 1;
748 <D1846>:;
749 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
750
751 [...]
752
753 __builtin_setjmp_setup (&buf, &<D2847>);
754 D.2844 = 0;
755 goto <D2846>;
756 <D2847>:;
757 __builtin_setjmp_receiver (&<D2847>);
758 D.2844 = 1;
759 <D2846>:;
760 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
761
762 [...]
763
764 <D3850>:;
765 return;
4f6c2131
EB
766 }
767
09b22f48
JJ
768 During cfg creation an extra per-function (or per-OpenMP region)
769 block with ABNORMAL_DISPATCHER internal call will be added, unique
770 destination of all the abnormal call edges and the unique source of
771 all the abnormal edges to the receivers, thus keeping the complexity
772 explosion localized. */
4f6c2131
EB
773
774static void
726a989a 775lower_builtin_setjmp (gimple_stmt_iterator *gsi)
4f6c2131 776{
355fe088 777 gimple *stmt = gsi_stmt (*gsi);
c2255bc4
AH
778 location_t loc = gimple_location (stmt);
779 tree cont_label = create_artificial_label (loc);
780 tree next_label = create_artificial_label (loc);
4f6c2131 781 tree dest, t, arg;
355fe088 782 gimple *g;
4f6c2131 783
021293cb
JJ
784 /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
785 these builtins are modelled as non-local label jumps to the label
786 that is passed to these two builtins, so pretend we have a non-local
787 label during GIMPLE passes too. See PR60003. */
d764963b 788 cfun->has_nonlocal_label = 1;
021293cb 789
4f6c2131
EB
790 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
791 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
792 FORCED_LABEL (next_label) = 1;
793
381cdae4
RB
794 tree orig_dest = dest = gimple_call_lhs (stmt);
795 if (orig_dest && TREE_CODE (orig_dest) == SSA_NAME)
796 dest = create_tmp_reg (TREE_TYPE (orig_dest));
4f6c2131
EB
797
798 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
aa00059c 799 arg = build_addr (next_label);
e79983f4 800 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
726a989a 801 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
db3927fb 802 gimple_set_location (g, loc);
cc2a64dd 803 gimple_set_block (g, gimple_block (stmt));
726a989a 804 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
805
806 /* Build 'DEST = 0' and insert. */
807 if (dest)
808 {
e8160c9a 809 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
db3927fb 810 gimple_set_location (g, loc);
cc2a64dd 811 gimple_set_block (g, gimple_block (stmt));
726a989a 812 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
813 }
814
815 /* Build 'goto CONT_LABEL' and insert. */
726a989a 816 g = gimple_build_goto (cont_label);
bbbbb16a 817 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
818
819 /* Build 'NEXT_LABEL:' and insert. */
726a989a
RB
820 g = gimple_build_label (next_label);
821 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
822
823 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
aa00059c 824 arg = build_addr (next_label);
e79983f4 825 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
726a989a 826 g = gimple_build_call (t, 1, arg);
db3927fb 827 gimple_set_location (g, loc);
cc2a64dd 828 gimple_set_block (g, gimple_block (stmt));
726a989a 829 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
830
831 /* Build 'DEST = 1' and insert. */
832 if (dest)
833 {
db3927fb
AH
834 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
835 integer_one_node));
836 gimple_set_location (g, loc);
cc2a64dd 837 gimple_set_block (g, gimple_block (stmt));
726a989a 838 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131
EB
839 }
840
841 /* Build 'CONT_LABEL:' and insert. */
726a989a
RB
842 g = gimple_build_label (cont_label);
843 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4f6c2131 844
381cdae4
RB
845 /* Build orig_dest = dest if necessary. */
846 if (dest != orig_dest)
847 {
848 g = gimple_build_assign (orig_dest, dest);
849 gsi_insert_before (gsi, g, GSI_SAME_STMT);
850 }
851
4f6c2131 852 /* Remove the call to __builtin_setjmp. */
726a989a 853 gsi_remove (gsi, false);
f5a76aea 854}
831806cb
RB
855
856/* Lower calls to posix_memalign to
c4c8514e
RB
857 res = posix_memalign (ptr, align, size);
858 if (res == 0)
859 *ptr = __builtin_assume_aligned (*ptr, align);
831806cb
RB
860 or to
861 void *tem;
c4c8514e
RB
862 res = posix_memalign (&tem, align, size);
863 if (res == 0)
864 ptr = __builtin_assume_aligned (tem, align);
831806cb
RB
865 in case the first argument was &ptr. That way we can get at the
866 alignment of the heap pointer in CCP. */
867
868static void
869lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
870{
355fe088 871 gimple *stmt, *call = gsi_stmt (*gsi);
c4c8514e
RB
872 tree pptr = gimple_call_arg (call, 0);
873 tree align = gimple_call_arg (call, 1);
874 tree res = gimple_call_lhs (call);
b731b390 875 tree ptr = create_tmp_reg (ptr_type_node);
831806cb
RB
876 if (TREE_CODE (pptr) == ADDR_EXPR)
877 {
b731b390 878 tree tem = create_tmp_var (ptr_type_node);
831806cb 879 TREE_ADDRESSABLE (tem) = 1;
c4c8514e 880 gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
831806cb
RB
881 stmt = gimple_build_assign (ptr, tem);
882 }
883 else
884 stmt = gimple_build_assign (ptr,
885 fold_build2 (MEM_REF, ptr_type_node, pptr,
886 build_int_cst (ptr_type_node, 0)));
c4c8514e
RB
887 if (res == NULL_TREE)
888 {
b731b390 889 res = create_tmp_reg (integer_type_node);
c4c8514e
RB
890 gimple_call_set_lhs (call, res);
891 }
892 tree align_label = create_artificial_label (UNKNOWN_LOCATION);
893 tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
355fe088 894 gimple *cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
c4c8514e
RB
895 align_label, noalign_label);
896 gsi_insert_after (gsi, cond, GSI_NEW_STMT);
897 gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
831806cb
RB
898 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
899 stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
900 2, ptr, align);
901 gimple_call_set_lhs (stmt, ptr);
902 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
903 stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
904 build_int_cst (ptr_type_node, 0)),
905 ptr);
906 gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
c4c8514e 907 gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
831806cb 908}
6de9cd9a
DN
909\f
910
50674e96 911/* Record the variables in VARS into function FN. */
6de9cd9a
DN
912
913void
50674e96 914record_vars_into (tree vars, tree fn)
6de9cd9a 915{
910ad8de 916 for (; vars; vars = DECL_CHAIN (vars))
6de9cd9a
DN
917 {
918 tree var = vars;
919
acb8f212
JH
920 /* BIND_EXPRs contains also function/type/constant declarations
921 we don't need to care about. */
8813a647 922 if (!VAR_P (var))
acb8f212 923 continue;
50674e96 924
6de9cd9a
DN
925 /* Nothing to do in this case. */
926 if (DECL_EXTERNAL (var))
927 continue;
6de9cd9a
DN
928
929 /* Record the variable. */
45b62594 930 add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
6de9cd9a 931 }
50674e96
DN
932}
933
934
935/* Record the variables in VARS into current_function_decl. */
936
937void
938record_vars (tree vars)
939{
940 record_vars_into (vars, current_function_decl);
6de9cd9a 941}