]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-low.c
* gimple.h: Remove all includes.
[thirdparty/gcc.git] / gcc / gimple-low.c
CommitLineData
75a70cf9 1/* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
4ee9c684 2
711789cc 3 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4ee9c684 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
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
4ee9c684 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
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4ee9c684 20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
9ed99284 26#include "tree-nested.h"
27#include "calls.h"
bc61cadb 28#include "basic-block.h"
29#include "tree-ssa-alias.h"
30#include "internal-fn.h"
31#include "gimple-expr.h"
32#include "is-a.h"
75a70cf9 33#include "gimple.h"
dcf1a1ec 34#include "gimple-iterator.h"
75a70cf9 35#include "tree-iterator.h"
4ee9c684 36#include "tree-inline.h"
4ee9c684 37#include "flags.h"
38#include "function.h"
0b205f4c 39#include "diagnostic-core.h"
4ee9c684 40#include "tree-pass.h"
6cb25bec 41#include "langhooks.h"
424a4a92 42#include "gimple-low.h"
e797f49f 43#include "tree-nested.h"
4ee9c684 44
75a70cf9 45/* The differences between High GIMPLE and Low GIMPLE are the
46 following:
47
48 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
49
50 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
51 flow and exception regions are built as an on-the-side region
52 hierarchy (See tree-eh.c:lower_eh_constructs).
53
54 3- Multiple identical return statements are grouped into a single
55 return and gotos to the unique return site. */
56
57/* Match a return statement with a label. During lowering, we identify
58 identical return statements and replace duplicates with a jump to
59 the corresponding label. */
60struct return_statements_t
61{
62 tree label;
63 gimple stmt;
64};
65typedef struct return_statements_t return_statements_t;
66
75a70cf9 67
4ee9c684 68struct lower_data
69{
70 /* Block the current statement belongs to. */
71 tree block;
22e30d4e 72
75a70cf9 73 /* A vector of label and return statements to be moved to the end
6c6a0f2f 74 of the function. */
f1f41a6c 75 vec<return_statements_t> return_statements;
2c8a1497 76
a2159096 77 /* True if the current statement cannot fall through. */
78 bool cannot_fallthru;
79
2c8a1497 80 /* True if the function calls __builtin_setjmp. */
81 bool calls_builtin_setjmp;
4ee9c684 82};
83
75a70cf9 84static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
85static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
929384bb 86static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
75a70cf9 87static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
88static void lower_builtin_setjmp (gimple_stmt_iterator *);
4ee9c684 89
75a70cf9 90
91/* Lower the body of current_function_decl from High GIMPLE into Low
92 GIMPLE. */
4ee9c684 93
2a1990e9 94static unsigned int
4ee9c684 95lower_function_body (void)
96{
97 struct lower_data data;
75a70cf9 98 gimple_seq body = gimple_body (current_function_decl);
99 gimple_seq lowered_body;
100 gimple_stmt_iterator i;
101 gimple bind;
102 tree t;
103 gimple x;
104
105 /* The gimplifier should've left a body of exactly one statement,
106 namely a GIMPLE_BIND. */
107 gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
108 && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
4ee9c684 109
1e8e9920 110 memset (&data, 0, sizeof (data));
4ee9c684 111 data.block = DECL_INITIAL (current_function_decl);
112 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
113 BLOCK_CHAIN (data.block) = NULL_TREE;
114 TREE_ASM_WRITTEN (data.block) = 1;
f1f41a6c 115 data.return_statements.create (8);
75a70cf9 116
117 bind = gimple_seq_first_stmt (body);
118 lowered_body = NULL;
119 gimple_seq_add_stmt (&lowered_body, bind);
120 i = gsi_start (lowered_body);
121 lower_gimple_bind (&i, &data);
4ee9c684 122
75a70cf9 123 i = gsi_last (lowered_body);
751ddc2b 124
125 /* If the function falls off the end, we need a null return statement.
75a70cf9 126 If we've already got one in the return_statements vector, we don't
751ddc2b 127 need to do anything special. Otherwise build one by hand. */
75a70cf9 128 if (gimple_seq_may_fallthru (lowered_body)
f1f41a6c 129 && (data.return_statements.is_empty ()
9af5ce0c 130 || (gimple_return_retval (data.return_statements.last().stmt)
131 != NULL)))
751ddc2b 132 {
75a70cf9 133 x = gimple_build_return (NULL);
134 gimple_set_location (x, cfun->function_end_locus);
32dedf8f 135 gimple_set_block (x, DECL_INITIAL (current_function_decl));
75a70cf9 136 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
751ddc2b 137 }
138
139 /* If we lowered any return statements, emit the representative
140 at the end of the function. */
f1f41a6c 141 while (!data.return_statements.is_empty ())
22e30d4e 142 {
f1f41a6c 143 return_statements_t t = data.return_statements.pop ();
75a70cf9 144 x = gimple_build_label (t.label);
145 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
75a70cf9 146 gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
22e30d4e 147 }
148
2c8a1497 149 /* If the function calls __builtin_setjmp, we need to emit the computed
150 goto that will serve as the unique dispatcher for all the receivers. */
151 if (data.calls_builtin_setjmp)
152 {
153 tree disp_label, disp_var, arg;
154
155 /* Build 'DISP_LABEL:' and insert. */
e60a6f7b 156 disp_label = create_artificial_label (cfun->function_end_locus);
2c8a1497 157 /* This mark will create forward edges from every call site. */
158 DECL_NONLOCAL (disp_label) = 1;
18d50ae6 159 cfun->has_nonlocal_label = 1;
75a70cf9 160 x = gimple_build_label (disp_label);
161 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
2c8a1497 162
163 /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);'
164 and insert. */
165 disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
c2f47e15 166 arg = build_addr (disp_label, current_function_decl);
b9a16870 167 t = builtin_decl_implicit (BUILT_IN_SETJMP_DISPATCHER);
75a70cf9 168 x = gimple_build_call (t, 1, arg);
169 gimple_call_set_lhs (x, disp_var);
2c8a1497 170
171 /* Build 'goto DISP_VAR;' and insert. */
75a70cf9 172 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
173 x = gimple_build_goto (disp_var);
174 gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
2c8a1497 175 }
176
e3a19533 177 /* Once the old body has been lowered, replace it with the new
178 lowered sequence. */
179 gimple_set_body (current_function_decl, lowered_body);
180
0d59b19d 181 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
4ee9c684 182 BLOCK_SUBBLOCKS (data.block)
183 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
184
185 clear_block_marks (data.block);
f1f41a6c 186 data.return_statements.release ();
2a1990e9 187 return 0;
4ee9c684 188}
189
cbe8bda8 190namespace {
191
192const pass_data pass_data_lower_cf =
4ee9c684 193{
cbe8bda8 194 GIMPLE_PASS, /* type */
195 "lower", /* name */
196 OPTGROUP_NONE, /* optinfo_flags */
197 false, /* has_gate */
198 true, /* has_execute */
199 TV_NONE, /* tv_id */
200 PROP_gimple_any, /* properties_required */
201 PROP_gimple_lcf, /* properties_provided */
202 0, /* properties_destroyed */
203 0, /* todo_flags_start */
204 0, /* todo_flags_finish */
4ee9c684 205};
206
cbe8bda8 207class pass_lower_cf : public gimple_opt_pass
208{
209public:
9af5ce0c 210 pass_lower_cf (gcc::context *ctxt)
211 : gimple_opt_pass (pass_data_lower_cf, ctxt)
cbe8bda8 212 {}
213
214 /* opt_pass methods: */
215 unsigned int execute () { return lower_function_body (); }
216
217}; // class pass_lower_cf
218
219} // anon namespace
220
221gimple_opt_pass *
222make_pass_lower_cf (gcc::context *ctxt)
223{
224 return new pass_lower_cf (ctxt);
225}
226
75a70cf9 227/* Lower sequence SEQ. Unlike gimplification the statements are not relowered
4ee9c684 228 when they are changed -- if this has to be done, the lowering routine must
229 do it explicitly. DATA is passed through the recursion. */
230
c939e803 231static void
e3a19533 232lower_sequence (gimple_seq *seq, struct lower_data *data)
4ee9c684 233{
75a70cf9 234 gimple_stmt_iterator gsi;
4ee9c684 235
e3a19533 236 for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
75a70cf9 237 lower_stmt (&gsi, data);
4ee9c684 238}
239
773c5ba7 240
75a70cf9 241/* Lower the OpenMP directive statement pointed by GSI. DATA is
773c5ba7 242 passed through the recursion. */
243
244static void
75a70cf9 245lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
773c5ba7 246{
75a70cf9 247 gimple stmt;
48e1416a 248
75a70cf9 249 stmt = gsi_stmt (*gsi);
773c5ba7 250
e3a19533 251 lower_sequence (gimple_omp_body_ptr (stmt), data);
252 gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
75a70cf9 253 gimple_omp_set_body (stmt, NULL);
e3a19533 254 gsi_next (gsi);
773c5ba7 255}
256
257
a2159096 258/* Lower statement GSI. DATA is passed through the recursion. We try to
259 track the fallthruness of statements and get rid of unreachable return
260 statements in order to prevent the EH lowering pass from adding useless
261 edges that can cause bogus warnings to be issued later; this guess need
262 not be 100% accurate, simply be conservative and reset cannot_fallthru
263 to false if we don't know. */
4ee9c684 264
265static void
75a70cf9 266lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
4ee9c684 267{
75a70cf9 268 gimple stmt = gsi_stmt (*gsi);
4ee9c684 269
75a70cf9 270 gimple_set_block (stmt, data->block);
4ee9c684 271
75a70cf9 272 switch (gimple_code (stmt))
4ee9c684 273 {
75a70cf9 274 case GIMPLE_BIND:
275 lower_gimple_bind (gsi, data);
a2159096 276 /* Propagate fallthruness. */
22e30d4e 277 return;
4ee9c684 278
75a70cf9 279 case GIMPLE_COND:
a2159096 280 case GIMPLE_GOTO:
281 case GIMPLE_SWITCH:
282 data->cannot_fallthru = true;
283 gsi_next (gsi);
284 return;
75a70cf9 285
286 case GIMPLE_RETURN:
a2159096 287 if (data->cannot_fallthru)
288 {
289 gsi_remove (gsi, false);
290 /* Propagate fallthruness. */
291 }
292 else
293 {
294 lower_gimple_return (gsi, data);
295 data->cannot_fallthru = true;
296 }
75a70cf9 297 return;
298
299 case GIMPLE_TRY:
929384bb 300 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
301 lower_try_catch (gsi, data);
302 else
303 {
304 /* It must be a GIMPLE_TRY_FINALLY. */
305 bool cannot_fallthru;
306 lower_sequence (gimple_try_eval_ptr (stmt), data);
307 cannot_fallthru = data->cannot_fallthru;
308
309 /* The finally clause is always executed after the try clause,
310 so if it does not fall through, then the try-finally will not
311 fall through. Otherwise, if the try clause does not fall
312 through, then when the finally clause falls through it will
313 resume execution wherever the try clause was going. So the
314 whole try-finally will only fall through if both the try
315 clause and the finally clause fall through. */
316 data->cannot_fallthru = false;
317 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
318 data->cannot_fallthru |= cannot_fallthru;
319 gsi_next (gsi);
320 }
321 return;
61e47ac8 322
4c0315d0 323 case GIMPLE_EH_ELSE:
e3a19533 324 lower_sequence (gimple_eh_else_n_body_ptr (stmt), data);
325 lower_sequence (gimple_eh_else_e_body_ptr (stmt), data);
4c0315d0 326 break;
327
75a70cf9 328 case GIMPLE_NOP:
329 case GIMPLE_ASM:
330 case GIMPLE_ASSIGN:
75a70cf9 331 case GIMPLE_PREDICT:
332 case GIMPLE_LABEL:
e38def9c 333 case GIMPLE_EH_MUST_NOT_THROW:
75a70cf9 334 case GIMPLE_OMP_FOR:
335 case GIMPLE_OMP_SECTIONS:
336 case GIMPLE_OMP_SECTIONS_SWITCH:
337 case GIMPLE_OMP_SECTION:
338 case GIMPLE_OMP_SINGLE:
339 case GIMPLE_OMP_MASTER:
bc7bff74 340 case GIMPLE_OMP_TASKGROUP:
75a70cf9 341 case GIMPLE_OMP_ORDERED:
342 case GIMPLE_OMP_CRITICAL:
343 case GIMPLE_OMP_RETURN:
344 case GIMPLE_OMP_ATOMIC_LOAD:
345 case GIMPLE_OMP_ATOMIC_STORE:
346 case GIMPLE_OMP_CONTINUE:
347 break;
2c8a1497 348
75a70cf9 349 case GIMPLE_CALL:
2c8a1497 350 {
75a70cf9 351 tree decl = gimple_call_fndecl (stmt);
3c8c0942 352 unsigned i;
353
354 for (i = 0; i < gimple_call_num_args (stmt); i++)
355 {
356 tree arg = gimple_call_arg (stmt, i);
357 if (EXPR_P (arg))
358 TREE_SET_BLOCK (arg, data->block);
359 }
75a70cf9 360
2c8a1497 361 if (decl
362 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
363 && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
364 {
75a70cf9 365 lower_builtin_setjmp (gsi);
a2159096 366 data->cannot_fallthru = false;
367 data->calls_builtin_setjmp = true;
2c8a1497 368 return;
369 }
264ee46d 370
264ee46d 371 if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
372 {
a2159096 373 data->cannot_fallthru = true;
264ee46d 374 gsi_next (gsi);
264ee46d 375 return;
376 }
2c8a1497 377 }
378 break;
379
75a70cf9 380 case GIMPLE_OMP_PARALLEL:
381 case GIMPLE_OMP_TASK:
bc7bff74 382 case GIMPLE_OMP_TARGET:
383 case GIMPLE_OMP_TEAMS:
a2159096 384 data->cannot_fallthru = false;
75a70cf9 385 lower_omp_directive (gsi, data);
a2159096 386 data->cannot_fallthru = false;
773c5ba7 387 return;
388
4c0315d0 389 case GIMPLE_TRANSACTION:
e3a19533 390 lower_sequence (gimple_transaction_body_ptr (stmt), data);
4c0315d0 391 break;
392
4ee9c684 393 default:
0d59b19d 394 gcc_unreachable ();
4ee9c684 395 }
396
a2159096 397 data->cannot_fallthru = false;
75a70cf9 398 gsi_next (gsi);
4ee9c684 399}
400
2c8a1497 401/* Lower a bind_expr TSI. DATA is passed through the recursion. */
4ee9c684 402
403static void
75a70cf9 404lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
4ee9c684 405{
406 tree old_block = data->block;
75a70cf9 407 gimple stmt = gsi_stmt (*gsi);
408 tree new_block = gimple_bind_block (stmt);
4ee9c684 409
410 if (new_block)
411 {
412 if (new_block == old_block)
413 {
414 /* The outermost block of the original function may not be the
415 outermost statement chain of the gimplified function. So we
416 may see the outermost block just inside the function. */
0d59b19d 417 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
4ee9c684 418 new_block = NULL;
419 }
420 else
421 {
422 /* We do not expect to handle duplicate blocks. */
0d59b19d 423 gcc_assert (!TREE_ASM_WRITTEN (new_block));
4ee9c684 424 TREE_ASM_WRITTEN (new_block) = 1;
425
426 /* Block tree may get clobbered by inlining. Normally this would
427 be fixed in rest_of_decl_compilation using block notes, but
428 since we are not going to emit them, it is up to us. */
429 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
430 BLOCK_SUBBLOCKS (old_block) = new_block;
431 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
432 BLOCK_SUPERCONTEXT (new_block) = old_block;
433
434 data->block = new_block;
435 }
436 }
437
75a70cf9 438 record_vars (gimple_bind_vars (stmt));
e3a19533 439 lower_sequence (gimple_bind_body_ptr (stmt), data);
4ee9c684 440
441 if (new_block)
442 {
0d59b19d 443 gcc_assert (data->block == new_block);
4ee9c684 444
445 BLOCK_SUBBLOCKS (new_block)
446 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
447 data->block = old_block;
448 }
449
75a70cf9 450 /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
451 gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
452 gsi_remove (gsi, false);
4ee9c684 453}
454
929384bb 455/* Same as above, but for a GIMPLE_TRY_CATCH. */
456
457static void
458lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
459{
460 bool cannot_fallthru;
461 gimple stmt = gsi_stmt (*gsi);
462 gimple_stmt_iterator i;
463
464 /* We don't handle GIMPLE_TRY_FINALLY. */
465 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
466
467 lower_sequence (gimple_try_eval_ptr (stmt), data);
468 cannot_fallthru = data->cannot_fallthru;
469
470 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
471 switch (gimple_code (gsi_stmt (i)))
472 {
473 case GIMPLE_CATCH:
474 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
475 catch expression and a body. The whole try/catch may fall
476 through iff any of the catch bodies falls through. */
477 for (; !gsi_end_p (i); gsi_next (&i))
478 {
479 data->cannot_fallthru = false;
480 lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data);
481 if (!data->cannot_fallthru)
482 cannot_fallthru = false;
483 }
484 break;
485
486 case GIMPLE_EH_FILTER:
487 /* The exception filter expression only matters if there is an
488 exception. If the exception does not match EH_FILTER_TYPES,
489 we will execute EH_FILTER_FAILURE, and we will fall through
490 if that falls through. If the exception does match
491 EH_FILTER_TYPES, the stack unwinder will continue up the
492 stack, so we will not fall through. We don't know whether we
493 will throw an exception which matches EH_FILTER_TYPES or not,
494 so we just ignore EH_FILTER_TYPES and assume that we might
495 throw an exception which doesn't match. */
496 data->cannot_fallthru = false;
497 lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
498 if (!data->cannot_fallthru)
499 cannot_fallthru = false;
500 break;
501
502 default:
503 /* This case represents statements to be executed when an
504 exception occurs. Those statements are implicitly followed
505 by a GIMPLE_RESX to resume execution after the exception. So
506 in this case the try/catch never falls through. */
507 data->cannot_fallthru = false;
508 lower_sequence (gimple_try_cleanup_ptr (stmt), data);
509 break;
510 }
511
512 data->cannot_fallthru = cannot_fallthru;
513 gsi_next (gsi);
514}
515
93f29170 516
0d9585ca 517/* Try to determine whether a TRY_CATCH expression can fall through.
518 This is a subroutine of gimple_stmt_may_fallthru. */
75a70cf9 519
520static bool
521gimple_try_catch_may_fallthru (gimple stmt)
522{
523 gimple_stmt_iterator i;
524
525 /* We don't handle GIMPLE_TRY_FINALLY. */
526 gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
527
528 /* If the TRY block can fall through, the whole TRY_CATCH can
529 fall through. */
530 if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
531 return true;
532
e3a19533 533 i = gsi_start (*gimple_try_cleanup_ptr (stmt));
75a70cf9 534 switch (gimple_code (gsi_stmt (i)))
535 {
536 case GIMPLE_CATCH:
537 /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
538 catch expression and a body. The whole try/catch may fall
539 through iff any of the catch bodies falls through. */
540 for (; !gsi_end_p (i); gsi_next (&i))
541 {
542 if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i))))
543 return true;
544 }
545 return false;
546
547 case GIMPLE_EH_FILTER:
548 /* The exception filter expression only matters if there is an
549 exception. If the exception does not match EH_FILTER_TYPES,
550 we will execute EH_FILTER_FAILURE, and we will fall through
551 if that falls through. If the exception does match
552 EH_FILTER_TYPES, the stack unwinder will continue up the
553 stack, so we will not fall through. We don't know whether we
554 will throw an exception which matches EH_FILTER_TYPES or not,
555 so we just ignore EH_FILTER_TYPES and assume that we might
556 throw an exception which doesn't match. */
557 return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
558
559 default:
560 /* This case represents statements to be executed when an
561 exception occurs. Those statements are implicitly followed
562 by a GIMPLE_RESX to resume execution after the exception. So
563 in this case the try/catch never falls through. */
564 return false;
565 }
566}
567
568
75a70cf9 569/* Try to determine if we can continue executing the statement
570 immediately following STMT. This guess need not be 100% accurate;
571 simply be conservative and return true if we don't know. This is
572 used only to avoid stupidly generating extra code. If we're wrong,
573 we'll just delete the extra code later. */
574
575bool
576gimple_stmt_may_fallthru (gimple stmt)
4ee9c684 577{
75a70cf9 578 if (!stmt)
579 return true;
4ee9c684 580
75a70cf9 581 switch (gimple_code (stmt))
582 {
583 case GIMPLE_GOTO:
584 case GIMPLE_RETURN:
585 case GIMPLE_RESX:
48e1416a 586 /* Easy cases. If the last statement of the seq implies
75a70cf9 587 control transfer, then we can't fall through. */
588 return false;
4ee9c684 589
75a70cf9 590 case GIMPLE_SWITCH:
a2159096 591 /* Switch has already been lowered and represents a branch
592 to a selected label and hence can't fall through. */
593 return false;
4ee9c684 594
75a70cf9 595 case GIMPLE_COND:
596 /* GIMPLE_COND's are already lowered into a two-way branch. They
597 can't fall through. */
598 return false;
4ee9c684 599
75a70cf9 600 case GIMPLE_BIND:
601 return gimple_seq_may_fallthru (gimple_bind_body (stmt));
4ee9c684 602
75a70cf9 603 case GIMPLE_TRY:
604 if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
605 return gimple_try_catch_may_fallthru (stmt);
4ee9c684 606
75a70cf9 607 /* It must be a GIMPLE_TRY_FINALLY. */
4ee9c684 608
75a70cf9 609 /* The finally clause is always executed after the try clause,
610 so if it does not fall through, then the try-finally will not
611 fall through. Otherwise, if the try clause does not fall
612 through, then when the finally clause falls through it will
613 resume execution wherever the try clause was going. So the
614 whole try-finally will only fall through if both the try
615 clause and the finally clause fall through. */
616 return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
617 && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
618
4c0315d0 619 case GIMPLE_EH_ELSE:
620 return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
621 || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
622
75a70cf9 623 case GIMPLE_CALL:
624 /* Functions that do not return do not fall through. */
625 return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
a2159096 626
75a70cf9 627 default:
628 return true;
4ee9c684 629 }
75a70cf9 630}
631
4ee9c684 632
75a70cf9 633/* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
4ee9c684 634
75a70cf9 635bool
636gimple_seq_may_fallthru (gimple_seq seq)
637{
638 return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq));
4ee9c684 639}
22e30d4e 640
75a70cf9 641
642/* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
2c8a1497 643
22e30d4e 644static void
75a70cf9 645lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
22e30d4e 646{
75a70cf9 647 gimple stmt = gsi_stmt (*gsi);
648 gimple t;
649 int i;
650 return_statements_t tmp_rs;
22e30d4e 651
6c6a0f2f 652 /* Match this up with an existing return statement that's been created. */
f1f41a6c 653 for (i = data->return_statements.length () - 1;
75a70cf9 654 i >= 0; i--)
22e30d4e 655 {
f1f41a6c 656 tmp_rs = data->return_statements[i];
6c6a0f2f 657
75a70cf9 658 if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
417a43d7 659 {
660 /* Remove the line number from the representative return statement.
661 It now fills in for many such returns. Failure to remove this
662 will result in incorrect results for coverage analysis. */
663 gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
664
665 goto found;
666 }
22e30d4e 667 }
668
6c6a0f2f 669 /* Not found. Create a new label and record the return statement. */
e60a6f7b 670 tmp_rs.label = create_artificial_label (cfun->function_end_locus);
75a70cf9 671 tmp_rs.stmt = stmt;
f1f41a6c 672 data->return_statements.safe_push (tmp_rs);
6c6a0f2f 673
674 /* Generate a goto statement and remove the return statement. */
675 found:
28f9c1a1 676 /* When not optimizing, make sure user returns are preserved. */
677 if (!optimize && gimple_has_location (stmt))
678 DECL_ARTIFICIAL (tmp_rs.label) = 0;
75a70cf9 679 t = gimple_build_goto (tmp_rs.label);
680 gimple_set_location (t, gimple_location (stmt));
32dedf8f 681 gimple_set_block (t, gimple_block (stmt));
75a70cf9 682 gsi_insert_before (gsi, t, GSI_SAME_STMT);
683 gsi_remove (gsi, false);
2c8a1497 684}
685
a2159096 686/* Lower a __builtin_setjmp GSI.
2c8a1497 687
688 __builtin_setjmp is passed a pointer to an array of five words (not
689 all will be used on all machines). It operates similarly to the C
690 library function of the same name, but is more efficient.
691
692 It is lowered into 3 other builtins, namely __builtin_setjmp_setup,
693 __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with
694 __builtin_setjmp_dispatcher shared among all the instances; that's
695 why it is only emitted at the end by lower_function_body.
696
697 After full lowering, the body of the function should look like:
698
699 {
700 void * setjmpvar.0;
701 int D.1844;
702 int D.2844;
703
704 [...]
705
706 __builtin_setjmp_setup (&buf, &<D1847>);
707 D.1844 = 0;
708 goto <D1846>;
709 <D1847>:;
710 __builtin_setjmp_receiver (&<D1847>);
711 D.1844 = 1;
712 <D1846>:;
713 if (D.1844 == 0) goto <D1848>; else goto <D1849>;
714
715 [...]
716
717 __builtin_setjmp_setup (&buf, &<D2847>);
718 D.2844 = 0;
719 goto <D2846>;
720 <D2847>:;
721 __builtin_setjmp_receiver (&<D2847>);
722 D.2844 = 1;
723 <D2846>:;
724 if (D.2844 == 0) goto <D2848>; else goto <D2849>;
725
726 [...]
727
728 <D3850>:;
729 return;
730 <D3853>: [non-local];
731 setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>);
732 goto setjmpvar.0;
733 }
734
735 The dispatcher block will be both the unique destination of all the
736 abnormal call edges and the unique source of all the abnormal edges
737 to the receivers, thus keeping the complexity explosion localized. */
738
739static void
75a70cf9 740lower_builtin_setjmp (gimple_stmt_iterator *gsi)
2c8a1497 741{
75a70cf9 742 gimple stmt = gsi_stmt (*gsi);
e60a6f7b 743 location_t loc = gimple_location (stmt);
744 tree cont_label = create_artificial_label (loc);
745 tree next_label = create_artificial_label (loc);
2c8a1497 746 tree dest, t, arg;
75a70cf9 747 gimple g;
2c8a1497 748
749 /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
750 passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
751 FORCED_LABEL (next_label) = 1;
752
75a70cf9 753 dest = gimple_call_lhs (stmt);
2c8a1497 754
755 /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
c2f47e15 756 arg = build_addr (next_label, current_function_decl);
b9a16870 757 t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
75a70cf9 758 g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
389dd41b 759 gimple_set_location (g, loc);
32dedf8f 760 gimple_set_block (g, gimple_block (stmt));
75a70cf9 761 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 762
763 /* Build 'DEST = 0' and insert. */
764 if (dest)
765 {
385f3f36 766 g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
389dd41b 767 gimple_set_location (g, loc);
32dedf8f 768 gimple_set_block (g, gimple_block (stmt));
75a70cf9 769 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 770 }
771
772 /* Build 'goto CONT_LABEL' and insert. */
75a70cf9 773 g = gimple_build_goto (cont_label);
b9c74b4d 774 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 775
776 /* Build 'NEXT_LABEL:' and insert. */
75a70cf9 777 g = gimple_build_label (next_label);
778 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 779
780 /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
c2f47e15 781 arg = build_addr (next_label, current_function_decl);
b9a16870 782 t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
75a70cf9 783 g = gimple_build_call (t, 1, arg);
389dd41b 784 gimple_set_location (g, loc);
32dedf8f 785 gimple_set_block (g, gimple_block (stmt));
75a70cf9 786 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 787
788 /* Build 'DEST = 1' and insert. */
789 if (dest)
790 {
389dd41b 791 g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
792 integer_one_node));
793 gimple_set_location (g, loc);
32dedf8f 794 gimple_set_block (g, gimple_block (stmt));
75a70cf9 795 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 796 }
797
798 /* Build 'CONT_LABEL:' and insert. */
75a70cf9 799 g = gimple_build_label (cont_label);
800 gsi_insert_before (gsi, g, GSI_SAME_STMT);
2c8a1497 801
802 /* Remove the call to __builtin_setjmp. */
75a70cf9 803 gsi_remove (gsi, false);
22e30d4e 804}
4ee9c684 805\f
806
773c5ba7 807/* Record the variables in VARS into function FN. */
4ee9c684 808
809void
773c5ba7 810record_vars_into (tree vars, tree fn)
4ee9c684 811{
9078126c 812 bool change_cfun = fn != current_function_decl;
813
814 if (change_cfun)
87d4aa85 815 push_cfun (DECL_STRUCT_FUNCTION (fn));
773c5ba7 816
1767a056 817 for (; vars; vars = DECL_CHAIN (vars))
4ee9c684 818 {
819 tree var = vars;
820
b3d24a23 821 /* BIND_EXPRs contains also function/type/constant declarations
822 we don't need to care about. */
823 if (TREE_CODE (var) != VAR_DECL)
824 continue;
773c5ba7 825
4ee9c684 826 /* Nothing to do in this case. */
827 if (DECL_EXTERNAL (var))
828 continue;
4ee9c684 829
830 /* Record the variable. */
2ab2ce89 831 add_local_decl (cfun, var);
4ee9c684 832 }
773c5ba7 833
9078126c 834 if (change_cfun)
87d4aa85 835 pop_cfun ();
773c5ba7 836}
837
838
839/* Record the variables in VARS into current_function_decl. */
840
841void
842record_vars (tree vars)
843{
844 record_vars_into (vars, current_function_decl);
4ee9c684 845}