PR c++/20142
[official-gcc.git] / gcc / gimple-low.c
blob4f814a5e6d7e0d3722e6563565152727d907fdba
1 /* Tree lowering pass. Lowers GIMPLE into unstructured form.
3 Copyright (C) 2003, 2005 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "varray.h"
30 #include "tree-gimple.h"
31 #include "tree-inline.h"
32 #include "diagnostic.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "tree-flow.h"
36 #include "timevar.h"
37 #include "except.h"
38 #include "hashtab.h"
39 #include "flags.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "toplev.h"
43 #include "tree-pass.h"
45 struct lower_data
47 /* Block the current statement belongs to. */
48 tree block;
50 /* A TREE_LIST of label and return statements to be moved to the end
51 of the function. */
52 tree return_statements;
55 static void lower_stmt (tree_stmt_iterator *, struct lower_data *);
56 static void lower_bind_expr (tree_stmt_iterator *, struct lower_data *);
57 static void lower_cond_expr (tree_stmt_iterator *, struct lower_data *);
58 static void lower_return_expr (tree_stmt_iterator *, struct lower_data *);
59 static bool expand_var_p (tree);
61 /* Lowers the body of current_function_decl. */
63 static void
64 lower_function_body (void)
66 struct lower_data data;
67 tree *body_p = &DECL_SAVED_TREE (current_function_decl);
68 tree bind = *body_p;
69 tree_stmt_iterator i;
70 tree t, x;
72 gcc_assert (TREE_CODE (bind) == BIND_EXPR);
74 data.block = DECL_INITIAL (current_function_decl);
75 BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
76 BLOCK_CHAIN (data.block) = NULL_TREE;
77 TREE_ASM_WRITTEN (data.block) = 1;
79 data.return_statements = NULL_TREE;
81 *body_p = alloc_stmt_list ();
82 i = tsi_start (*body_p);
83 tsi_link_after (&i, bind, TSI_NEW_STMT);
84 lower_bind_expr (&i, &data);
86 i = tsi_last (*body_p);
88 /* If the function falls off the end, we need a null return statement.
89 If we've already got one in the return_statements list, we don't
90 need to do anything special. Otherwise build one by hand. */
91 if (block_may_fallthru (*body_p)
92 && (data.return_statements == NULL
93 || TREE_OPERAND (TREE_VALUE (data.return_statements), 0) != NULL))
95 x = build (RETURN_EXPR, void_type_node, NULL);
96 SET_EXPR_LOCATION (x, cfun->function_end_locus);
97 tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
100 /* If we lowered any return statements, emit the representative
101 at the end of the function. */
102 for (t = data.return_statements ; t ; t = TREE_CHAIN (t))
104 x = build (LABEL_EXPR, void_type_node, TREE_PURPOSE (t));
105 tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
107 /* Remove the line number from the representative return statement.
108 It now fills in for many such returns. Failure to remove this
109 will result in incorrect results for coverage analysis. */
110 x = TREE_VALUE (t);
111 #ifdef USE_MAPPED_LOCATION
112 SET_EXPR_LOCATION (x, UNKNOWN_LOCATION);
113 #else
114 SET_EXPR_LOCUS (x, NULL);
115 #endif
116 tsi_link_after (&i, x, TSI_CONTINUE_LINKING);
119 gcc_assert (data.block == DECL_INITIAL (current_function_decl));
120 BLOCK_SUBBLOCKS (data.block)
121 = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
123 clear_block_marks (data.block);
126 struct tree_opt_pass pass_lower_cf =
128 "lower", /* name */
129 NULL, /* gate */
130 lower_function_body, /* execute */
131 NULL, /* sub */
132 NULL, /* next */
133 0, /* static_pass_number */
134 0, /* tv_id */
135 PROP_gimple_any, /* properties_required */
136 PROP_gimple_lcf, /* properties_provided */
137 PROP_gimple_any, /* properties_destroyed */
138 0, /* todo_flags_start */
139 TODO_dump_func, /* todo_flags_finish */
140 0 /* letter */
144 /* Lowers the EXPR. Unlike gimplification the statements are not relowered
145 when they are changed -- if this has to be done, the lowering routine must
146 do it explicitly. DATA is passed through the recursion. */
148 static void
149 lower_stmt_body (tree expr, struct lower_data *data)
151 tree_stmt_iterator tsi;
153 for (tsi = tsi_start (expr); !tsi_end_p (tsi); )
154 lower_stmt (&tsi, data);
157 /* Lowers statement TSI. DATA is passed through the recursion. */
159 static void
160 lower_stmt (tree_stmt_iterator *tsi, struct lower_data *data)
162 tree stmt = tsi_stmt (*tsi);
164 if (EXPR_HAS_LOCATION (stmt) && data)
165 TREE_BLOCK (stmt) = data->block;
167 switch (TREE_CODE (stmt))
169 case BIND_EXPR:
170 lower_bind_expr (tsi, data);
171 return;
172 case COND_EXPR:
173 lower_cond_expr (tsi, data);
174 return;
175 case RETURN_EXPR:
176 lower_return_expr (tsi, data);
177 return;
179 case TRY_FINALLY_EXPR:
180 case TRY_CATCH_EXPR:
181 lower_stmt_body (TREE_OPERAND (stmt, 0), data);
182 lower_stmt_body (TREE_OPERAND (stmt, 1), data);
183 break;
184 case CATCH_EXPR:
185 lower_stmt_body (CATCH_BODY (stmt), data);
186 break;
187 case EH_FILTER_EXPR:
188 lower_stmt_body (EH_FILTER_FAILURE (stmt), data);
189 break;
191 case NOP_EXPR:
192 case ASM_EXPR:
193 case MODIFY_EXPR:
194 case CALL_EXPR:
195 case GOTO_EXPR:
196 case LABEL_EXPR:
197 case SWITCH_EXPR:
198 break;
200 default:
201 #ifdef ENABLE_CHECKING
202 print_node_brief (stderr, "", stmt, 0);
203 internal_error ("unexpected node");
204 #endif
205 case COMPOUND_EXPR:
206 gcc_unreachable ();
209 tsi_next (tsi);
212 /* Lowers a bind_expr TSI. DATA is passed through the recursion. */
214 static void
215 lower_bind_expr (tree_stmt_iterator *tsi, struct lower_data *data)
217 tree old_block = data->block;
218 tree stmt = tsi_stmt (*tsi);
219 tree new_block = BIND_EXPR_BLOCK (stmt);
221 if (new_block)
223 if (new_block == old_block)
225 /* The outermost block of the original function may not be the
226 outermost statement chain of the gimplified function. So we
227 may see the outermost block just inside the function. */
228 gcc_assert (new_block == DECL_INITIAL (current_function_decl));
229 new_block = NULL;
231 else
233 /* We do not expect to handle duplicate blocks. */
234 gcc_assert (!TREE_ASM_WRITTEN (new_block));
235 TREE_ASM_WRITTEN (new_block) = 1;
237 /* Block tree may get clobbered by inlining. Normally this would
238 be fixed in rest_of_decl_compilation using block notes, but
239 since we are not going to emit them, it is up to us. */
240 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
241 BLOCK_SUBBLOCKS (old_block) = new_block;
242 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
243 BLOCK_SUPERCONTEXT (new_block) = old_block;
245 data->block = new_block;
249 record_vars (BIND_EXPR_VARS (stmt));
250 lower_stmt_body (BIND_EXPR_BODY (stmt), data);
252 if (new_block)
254 gcc_assert (data->block == new_block);
256 BLOCK_SUBBLOCKS (new_block)
257 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
258 data->block = old_block;
261 /* The BIND_EXPR no longer carries any useful information -- kill it. */
262 tsi_link_before (tsi, BIND_EXPR_BODY (stmt), TSI_SAME_STMT);
263 tsi_delink (tsi);
266 /* Try to determine whether a TRY_CATCH expression can fall through.
267 This is a subroutine of block_may_fallthru. */
269 static bool
270 try_catch_may_fallthru (tree stmt)
272 tree_stmt_iterator i;
274 /* If the TRY block can fall through, the whole TRY_CATCH can
275 fall through. */
276 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
277 return true;
279 i = tsi_start (TREE_OPERAND (stmt, 1));
280 switch (TREE_CODE (tsi_stmt (i)))
282 case CATCH_EXPR:
283 /* We expect to see a sequence of CATCH_EXPR trees, each with a
284 catch expression and a body. The whole TRY_CATCH may fall
285 through iff any of the catch bodies falls through. */
286 for (; !tsi_end_p (i); tsi_next (&i))
288 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
289 return true;
291 return false;
293 case EH_FILTER_EXPR:
294 /* The exception filter expression only matters if there is an
295 exception. If the exception does not match EH_FILTER_TYPES,
296 we will execute EH_FILTER_FAILURE, and we will fall through
297 if that falls through. If the exception does match
298 EH_FILTER_TYPES, the stack unwinder will continue up the
299 stack, so we will not fall through. We don't know whether we
300 will throw an exception which matches EH_FILTER_TYPES or not,
301 so we just ignore EH_FILTER_TYPES and assume that we might
302 throw an exception which doesn't match. */
303 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
305 default:
306 /* This case represents statements to be executed when an
307 exception occurs. Those statements are implicitly followed
308 by a RESX_EXPR to resume execution after the exception. So
309 in this case the TRY_CATCH never falls through. */
310 return false;
314 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
315 need not be 100% accurate; simply be conservative and return true if we
316 don't know. This is used only to avoid stupidly generating extra code.
317 If we're wrong, we'll just delete the extra code later. */
319 bool
320 block_may_fallthru (tree block)
322 tree stmt = expr_last (block);
324 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
326 case GOTO_EXPR:
327 case RETURN_EXPR:
328 case RESX_EXPR:
329 /* Easy cases. If the last statement of the block implies
330 control transfer, then we can't fall through. */
331 return false;
333 case SWITCH_EXPR:
334 /* If SWITCH_LABELS is set, this is lowered, and represents a
335 branch to a selected label and hence can not fall through.
336 Otherwise SWITCH_BODY is set, and the switch can fall
337 through. */
338 return SWITCH_LABELS (stmt) == NULL_TREE;
340 case COND_EXPR:
341 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
342 return true;
343 return block_may_fallthru (COND_EXPR_ELSE (stmt));
345 case BIND_EXPR:
346 return block_may_fallthru (BIND_EXPR_BODY (stmt));
348 case TRY_CATCH_EXPR:
349 return try_catch_may_fallthru (stmt);
351 case TRY_FINALLY_EXPR:
352 /* The finally clause is always executed after the try clause,
353 so if it does not fall through, then the try-finally will not
354 fall through. Otherwise, if the try clause does not fall
355 through, then when the finally clause falls through it will
356 resume execution wherever the try clause was going. So the
357 whole try-finally will only fall through if both the try
358 clause and the finally clause fall through. */
359 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
360 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
362 case MODIFY_EXPR:
363 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
364 stmt = TREE_OPERAND (stmt, 1);
365 else
366 return true;
367 /* FALLTHRU */
369 case CALL_EXPR:
370 /* Functions that do not return do not fall through. */
371 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
373 default:
374 return true;
378 /* Lowers a cond_expr TSI. DATA is passed through the recursion. */
380 static void
381 lower_cond_expr (tree_stmt_iterator *tsi, struct lower_data *data)
383 tree stmt = tsi_stmt (*tsi);
384 bool then_is_goto, else_is_goto;
385 tree then_branch, else_branch;
386 tree then_goto, else_goto;
388 then_branch = COND_EXPR_THEN (stmt);
389 else_branch = COND_EXPR_ELSE (stmt);
391 lower_stmt_body (then_branch, data);
392 lower_stmt_body (else_branch, data);
394 then_goto = expr_only (then_branch);
395 then_is_goto = then_goto && simple_goto_p (then_goto);
397 else_goto = expr_only (else_branch);
398 else_is_goto = else_goto && simple_goto_p (else_goto);
400 if (!then_is_goto || !else_is_goto)
402 tree then_label, else_label, end_label, t;
404 then_label = NULL_TREE;
405 else_label = NULL_TREE;
406 end_label = NULL_TREE;
408 /* Replace the cond_expr with explicit gotos. */
409 if (!then_is_goto)
411 t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
412 if (TREE_SIDE_EFFECTS (then_branch))
413 then_label = t;
414 else
415 end_label = t;
416 then_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
419 if (!else_is_goto)
421 t = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
422 if (TREE_SIDE_EFFECTS (else_branch))
423 else_label = t;
424 else
426 /* Both THEN and ELSE can be no-ops if one or both contained an
427 empty BIND_EXPR that was associated with the toplevel block
428 of an inlined function. In that case remove_useless_stmts
429 can't have cleaned things up for us; kill the whole
430 conditional now. */
431 if (end_label)
433 tsi_delink (tsi);
434 return;
436 else
437 end_label = t;
439 else_goto = build_and_jump (&LABEL_EXPR_LABEL (t));
442 if (then_label)
444 bool may_fallthru = block_may_fallthru (then_branch);
446 tsi_link_after (tsi, then_label, TSI_CONTINUE_LINKING);
447 tsi_link_after (tsi, then_branch, TSI_CONTINUE_LINKING);
449 if (else_label && may_fallthru)
451 end_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
452 t = build_and_jump (&LABEL_EXPR_LABEL (end_label));
453 tsi_link_after (tsi, t, TSI_CONTINUE_LINKING);
457 if (else_label)
459 tsi_link_after (tsi, else_label, TSI_CONTINUE_LINKING);
460 tsi_link_after (tsi, else_branch, TSI_CONTINUE_LINKING);
463 if (end_label)
464 tsi_link_after (tsi, end_label, TSI_CONTINUE_LINKING);
467 COND_EXPR_THEN (stmt) = then_goto;
468 COND_EXPR_ELSE (stmt) = else_goto;
470 tsi_next (tsi);
473 static void
474 lower_return_expr (tree_stmt_iterator *tsi, struct lower_data *data)
476 tree stmt = tsi_stmt (*tsi);
477 tree value, t, label;
479 /* Extract the value being returned. */
480 value = TREE_OPERAND (stmt, 0);
481 if (value && TREE_CODE (value) == MODIFY_EXPR)
482 value = TREE_OPERAND (value, 1);
484 /* Match this up with an existing return statement that's been created. */
485 for (t = data->return_statements; t ; t = TREE_CHAIN (t))
487 tree tvalue = TREE_OPERAND (TREE_VALUE (t), 0);
488 if (tvalue && TREE_CODE (tvalue) == MODIFY_EXPR)
489 tvalue = TREE_OPERAND (tvalue, 1);
491 if (value == tvalue)
493 label = TREE_PURPOSE (t);
494 goto found;
498 /* Not found. Create a new label and record the return statement. */
499 label = create_artificial_label ();
500 data->return_statements = tree_cons (label, stmt, data->return_statements);
502 /* Generate a goto statement and remove the return statement. */
503 found:
504 t = build (GOTO_EXPR, void_type_node, label);
505 SET_EXPR_LOCUS (t, EXPR_LOCUS (stmt));
506 tsi_link_before (tsi, t, TSI_SAME_STMT);
507 tsi_delink (tsi);
511 /* Record the variables in VARS. */
513 void
514 record_vars (tree vars)
516 for (; vars; vars = TREE_CHAIN (vars))
518 tree var = vars;
520 /* Nothing to do in this case. */
521 if (DECL_EXTERNAL (var))
522 continue;
523 if (TREE_CODE (var) == FUNCTION_DECL)
524 continue;
526 /* Record the variable. */
527 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
528 cfun->unexpanded_var_list);
532 /* Check whether to expand a variable VAR. */
534 static bool
535 expand_var_p (tree var)
537 struct var_ann_d *ann;
539 if (TREE_CODE (var) != VAR_DECL)
540 return true;
542 /* Leave statics and externals alone. */
543 if (TREE_STATIC (var) || DECL_EXTERNAL (var))
544 return true;
546 /* Remove all unused local variables. */
547 ann = var_ann (var);
548 if (!ann || !ann->used)
549 return false;
551 return true;
554 /* Throw away variables that are unused. */
556 static void
557 remove_useless_vars (void)
559 tree var, *cell;
560 FILE *df = NULL;
562 if (dump_file && (dump_flags & TDF_DETAILS))
564 df = dump_file;
565 fputs ("Discarding as unused:\n", df);
568 for (cell = &cfun->unexpanded_var_list; *cell; )
570 var = TREE_VALUE (*cell);
572 if (!expand_var_p (var))
574 if (df)
576 fputs (" ", df);
577 print_generic_expr (df, var, dump_flags);
578 fputc ('\n', df);
581 *cell = TREE_CHAIN (*cell);
582 continue;
585 cell = &TREE_CHAIN (*cell);
588 if (df)
589 fputc ('\n', df);
592 struct tree_opt_pass pass_remove_useless_vars =
594 "vars", /* name */
595 NULL, /* gate */
596 remove_useless_vars, /* execute */
597 NULL, /* sub */
598 NULL, /* next */
599 0, /* static_pass_number */
600 0, /* tv_id */
601 0, /* properties_required */
602 0, /* properties_provided */
603 0, /* properties_destroyed */
604 0, /* todo_flags_start */
605 TODO_dump_func, /* todo_flags_finish */
606 0 /* letter */
609 /* Mark BLOCK used if it has a used variable in it, then recurse over its
610 subblocks. */
612 static void
613 mark_blocks_with_used_vars (tree block)
615 tree var;
616 tree subblock;
618 if (!TREE_USED (block))
620 for (var = BLOCK_VARS (block);
621 var;
622 var = TREE_CHAIN (var))
624 if (TREE_USED (var))
626 TREE_USED (block) = true;
627 break;
631 for (subblock = BLOCK_SUBBLOCKS (block);
632 subblock;
633 subblock = BLOCK_CHAIN (subblock))
634 mark_blocks_with_used_vars (subblock);
637 /* Mark the used attribute on blocks correctly. */
639 static void
640 mark_used_blocks (void)
642 mark_blocks_with_used_vars (DECL_INITIAL (current_function_decl));
646 struct tree_opt_pass pass_mark_used_blocks =
648 "blocks", /* name */
649 NULL, /* gate */
650 mark_used_blocks, /* execute */
651 NULL, /* sub */
652 NULL, /* next */
653 0, /* static_pass_number */
654 0, /* tv_id */
655 0, /* properties_required */
656 0, /* properties_provided */
657 0, /* properties_destroyed */
658 0, /* todo_flags_start */
659 TODO_dump_func, /* todo_flags_finish */
660 0 /* letter */