1 /* Tree lowering pass. Lowers GIMPLE into unstructured form.
3 Copyright (C) 2003 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
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
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
24 #include "coretypes.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"
43 #include "tree-pass.h"
47 /* Block the current statement belongs to. */
50 /* A TREE_LIST of label and return statements to be moved to the end
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. */
64 lower_function_body (void)
66 struct lower_data data
;
67 tree
*body_p
= &DECL_SAVED_TREE (current_function_decl
);
72 if (TREE_CODE (bind
) != BIND_EXPR
)
75 data
.block
= DECL_INITIAL (current_function_decl
);
76 BLOCK_SUBBLOCKS (data
.block
) = NULL_TREE
;
77 BLOCK_CHAIN (data
.block
) = NULL_TREE
;
78 TREE_ASM_WRITTEN (data
.block
) = 1;
80 data
.return_statements
= NULL_TREE
;
82 *body_p
= alloc_stmt_list ();
83 i
= tsi_start (*body_p
);
84 tsi_link_after (&i
, bind
, TSI_NEW_STMT
);
85 lower_bind_expr (&i
, &data
);
87 i
= tsi_last (*body_p
);
89 /* If the function falls off the end, we need a null return statement.
90 If we've already got one in the return_statements list, we don't
91 need to do anything special. Otherwise build one by hand. */
92 if (block_may_fallthru (*body_p
)
93 && (data
.return_statements
== NULL
94 || TREE_OPERAND (TREE_VALUE (data
.return_statements
), 0) != NULL
))
96 x
= build (RETURN_EXPR
, void_type_node
, NULL
);
97 SET_EXPR_LOCATION (x
, cfun
->function_end_locus
);
98 tsi_link_after (&i
, x
, TSI_CONTINUE_LINKING
);
101 /* If we lowered any return statements, emit the representative
102 at the end of the function. */
103 for (t
= data
.return_statements
; t
; t
= TREE_CHAIN (t
))
105 x
= build (LABEL_EXPR
, void_type_node
, TREE_PURPOSE (t
));
106 tsi_link_after (&i
, x
, TSI_CONTINUE_LINKING
);
108 /* Remove the line number from the representative return statement.
109 It now fills in for many such returns. Failure to remove this
110 will result in incorrect results for coverage analysis. */
112 #ifdef USE_MAPPED_LOCATION
113 SET_EXPR_LOCATION (x
, UNKNOWN_LOCATION
);
115 SET_EXPR_LOCUS (x
, NULL
);
117 tsi_link_after (&i
, x
, TSI_CONTINUE_LINKING
);
120 if (data
.block
!= DECL_INITIAL (current_function_decl
))
122 BLOCK_SUBBLOCKS (data
.block
)
123 = blocks_nreverse (BLOCK_SUBBLOCKS (data
.block
));
125 clear_block_marks (data
.block
);
128 struct tree_opt_pass pass_lower_cf
=
132 lower_function_body
, /* execute */
135 0, /* static_pass_number */
137 PROP_gimple_any
, /* properties_required */
138 PROP_gimple_lcf
, /* properties_provided */
139 PROP_gimple_any
, /* properties_destroyed */
140 0, /* todo_flags_start */
141 TODO_dump_func
/* todo_flags_finish */
145 /* Lowers the EXPR. Unlike gimplification the statements are not relowered
146 when they are changed -- if this has to be done, the lowering routine must
147 do it explicitly. DATA is passed through the recursion. */
150 lower_stmt_body (tree expr
, struct lower_data
*data
)
152 tree_stmt_iterator tsi
;
154 for (tsi
= tsi_start (expr
); !tsi_end_p (tsi
); )
155 lower_stmt (&tsi
, data
);
158 /* Lowers statement TSI. DATA is passed through the recursion. */
161 lower_stmt (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
163 tree stmt
= tsi_stmt (*tsi
);
165 if (EXPR_HAS_LOCATION (stmt
) && data
)
166 TREE_BLOCK (stmt
) = data
->block
;
168 switch (TREE_CODE (stmt
))
171 lower_bind_expr (tsi
, data
);
174 lower_cond_expr (tsi
, data
);
177 lower_return_expr (tsi
, data
);
180 case TRY_FINALLY_EXPR
:
182 lower_stmt_body (TREE_OPERAND (stmt
, 0), data
);
183 lower_stmt_body (TREE_OPERAND (stmt
, 1), data
);
186 lower_stmt_body (CATCH_BODY (stmt
), data
);
189 lower_stmt_body (EH_FILTER_FAILURE (stmt
), data
);
202 print_node_brief (stderr
, "", stmt
, 0);
210 /* Lowers a bind_expr TSI. DATA is passed through the recursion. */
213 lower_bind_expr (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
215 tree old_block
= data
->block
;
216 tree stmt
= tsi_stmt (*tsi
);
217 tree new_block
= BIND_EXPR_BLOCK (stmt
);
221 if (new_block
== old_block
)
223 /* The outermost block of the original function may not be the
224 outermost statement chain of the gimplified function. So we
225 may see the outermost block just inside the function. */
226 if (new_block
!= DECL_INITIAL (current_function_decl
))
232 /* We do not expect to handle duplicate blocks. */
233 if (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
);
254 if (data
->block
!= new_block
)
257 BLOCK_SUBBLOCKS (new_block
)
258 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block
));
259 data
->block
= old_block
;
262 /* The BIND_EXPR no longer carries any useful information -- kill it. */
263 tsi_link_before (tsi
, BIND_EXPR_BODY (stmt
), TSI_SAME_STMT
);
267 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
268 need not be 100% accurate; simply be conservative and return true if we
269 don't know. This is used only to avoid stupidly generating extra code.
270 If we're wrong, we'll just delete the extra code later. */
273 block_may_fallthru (tree block
)
275 tree stmt
= expr_last (block
);
277 switch (stmt
? TREE_CODE (stmt
) : ERROR_MARK
)
283 /* Easy cases. If the last statement of the block implies
284 control transfer, then we can't fall through. */
288 if (block_may_fallthru (COND_EXPR_THEN (stmt
)))
290 return block_may_fallthru (COND_EXPR_ELSE (stmt
));
293 return block_may_fallthru (BIND_EXPR_BODY (stmt
));
295 case TRY_FINALLY_EXPR
:
296 return block_may_fallthru (TREE_OPERAND (stmt
, 1));
299 if (TREE_CODE (TREE_OPERAND (stmt
, 1)) == CALL_EXPR
)
300 stmt
= TREE_OPERAND (stmt
, 1);
306 /* Functions that do not return do not fall through. */
307 return (call_expr_flags (stmt
) & ECF_NORETURN
) == 0;
314 /* Lowers a cond_expr TSI. DATA is passed through the recursion. */
317 lower_cond_expr (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
319 tree stmt
= tsi_stmt (*tsi
);
320 bool then_is_goto
, else_is_goto
;
321 tree then_branch
, else_branch
;
322 tree then_goto
, else_goto
;
324 then_branch
= COND_EXPR_THEN (stmt
);
325 else_branch
= COND_EXPR_ELSE (stmt
);
327 lower_stmt_body (then_branch
, data
);
328 lower_stmt_body (else_branch
, data
);
330 then_goto
= expr_only (then_branch
);
331 then_is_goto
= then_goto
&& simple_goto_p (then_goto
);
333 else_goto
= expr_only (else_branch
);
334 else_is_goto
= else_goto
&& simple_goto_p (else_goto
);
336 if (!then_is_goto
|| !else_is_goto
)
338 tree then_label
, else_label
, end_label
, t
;
340 then_label
= NULL_TREE
;
341 else_label
= NULL_TREE
;
342 end_label
= NULL_TREE
;
344 /* Replace the cond_expr with explicit gotos. */
347 t
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
348 if (TREE_SIDE_EFFECTS (then_branch
))
352 then_goto
= build_and_jump (&LABEL_EXPR_LABEL (t
));
357 t
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
358 if (TREE_SIDE_EFFECTS (else_branch
))
362 /* Both THEN and ELSE can be no-ops if one or both contained an
363 empty BIND_EXPR that was associated with the toplevel block
364 of an inlined function. In that case remove_useless_stmts
365 can't have cleaned things up for us; kill the whole
375 else_goto
= build_and_jump (&LABEL_EXPR_LABEL (t
));
380 bool may_fallthru
= block_may_fallthru (then_branch
);
382 tsi_link_after (tsi
, then_label
, TSI_CONTINUE_LINKING
);
383 tsi_link_after (tsi
, then_branch
, TSI_CONTINUE_LINKING
);
385 if (else_label
&& may_fallthru
)
387 end_label
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
388 t
= build_and_jump (&LABEL_EXPR_LABEL (end_label
));
389 tsi_link_after (tsi
, t
, TSI_CONTINUE_LINKING
);
395 tsi_link_after (tsi
, else_label
, TSI_CONTINUE_LINKING
);
396 tsi_link_after (tsi
, else_branch
, TSI_CONTINUE_LINKING
);
400 tsi_link_after (tsi
, end_label
, TSI_CONTINUE_LINKING
);
403 COND_EXPR_THEN (stmt
) = then_goto
;
404 COND_EXPR_ELSE (stmt
) = else_goto
;
410 lower_return_expr (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
412 tree stmt
= tsi_stmt (*tsi
);
413 tree value
, t
, label
;
415 /* Extract the value being returned. */
416 value
= TREE_OPERAND (stmt
, 0);
417 if (value
&& TREE_CODE (value
) == MODIFY_EXPR
)
418 value
= TREE_OPERAND (value
, 1);
420 /* Match this up with an existing return statement that's been created. */
421 for (t
= data
->return_statements
; t
; t
= TREE_CHAIN (t
))
423 tree tvalue
= TREE_OPERAND (TREE_VALUE (t
), 0);
424 if (tvalue
&& TREE_CODE (tvalue
) == MODIFY_EXPR
)
425 tvalue
= TREE_OPERAND (tvalue
, 1);
429 label
= TREE_PURPOSE (t
);
434 /* Not found. Create a new label and record the return statement. */
435 label
= create_artificial_label ();
436 data
->return_statements
= tree_cons (label
, stmt
, data
->return_statements
);
438 /* Generate a goto statement and remove the return statement. */
440 t
= build (GOTO_EXPR
, void_type_node
, label
);
441 SET_EXPR_LOCUS (t
, EXPR_LOCUS (stmt
));
442 tsi_link_before (tsi
, t
, TSI_SAME_STMT
);
447 /* Record the variables in VARS. */
450 record_vars (tree vars
)
452 for (; vars
; vars
= TREE_CHAIN (vars
))
456 /* Nothing to do in this case. */
457 if (DECL_EXTERNAL (var
))
459 if (TREE_CODE (var
) == FUNCTION_DECL
)
462 /* Record the variable. */
463 cfun
->unexpanded_var_list
= tree_cons (NULL_TREE
, var
,
464 cfun
->unexpanded_var_list
);
468 /* Check whether to expand a variable VAR. */
471 expand_var_p (tree var
)
473 struct var_ann_d
*ann
;
475 if (TREE_CODE (var
) != VAR_DECL
)
478 /* Leave statics and externals alone. */
479 if (TREE_STATIC (var
) || DECL_EXTERNAL (var
))
482 /* Remove all unused local variables. */
484 if (!ann
|| !ann
->used
)
490 /* Throw away variables that are unused. */
493 remove_useless_vars (void)
498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
501 fputs ("Discarding as unused:\n", df
);
504 for (cell
= &cfun
->unexpanded_var_list
; *cell
; )
506 var
= TREE_VALUE (*cell
);
508 if (!expand_var_p (var
))
513 print_generic_expr (df
, var
, dump_flags
);
517 *cell
= TREE_CHAIN (*cell
);
521 cell
= &TREE_CHAIN (*cell
);
528 struct tree_opt_pass pass_remove_useless_vars
=
532 remove_useless_vars
, /* execute */
535 0, /* static_pass_number */
537 0, /* properties_required */
538 0, /* properties_provided */
539 0, /* properties_destroyed */
540 0, /* todo_flags_start */
541 TODO_dump_func
/* todo_flags_finish */