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. */
51 static void lower_stmt (tree_stmt_iterator
*, struct lower_data
*);
52 static void lower_bind_expr (tree_stmt_iterator
*, struct lower_data
*);
53 static void lower_cond_expr (tree_stmt_iterator
*, struct lower_data
*);
54 static bool expand_var_p (tree
);
56 /* Lowers the body of current_function_decl. */
59 lower_function_body (void)
61 struct lower_data data
;
62 tree
*body_p
= &DECL_SAVED_TREE (current_function_decl
);
66 if (TREE_CODE (bind
) != BIND_EXPR
)
69 data
.block
= DECL_INITIAL (current_function_decl
);
70 BLOCK_SUBBLOCKS (data
.block
) = NULL_TREE
;
71 BLOCK_CHAIN (data
.block
) = NULL_TREE
;
72 TREE_ASM_WRITTEN (data
.block
) = 1;
74 *body_p
= alloc_stmt_list ();
75 i
= tsi_start (*body_p
);
76 tsi_link_after (&i
, bind
, TSI_NEW_STMT
);
77 lower_bind_expr (&i
, &data
);
79 if (data
.block
!= DECL_INITIAL (current_function_decl
))
81 BLOCK_SUBBLOCKS (data
.block
)
82 = blocks_nreverse (BLOCK_SUBBLOCKS (data
.block
));
84 clear_block_marks (data
.block
);
86 /* Avoid producing notes for blocks. */
87 cfun
->dont_emit_block_notes
= 1;
88 reset_block_changes ();
91 struct tree_opt_pass pass_lower_cf
=
95 lower_function_body
, /* execute */
98 0, /* static_pass_number */
100 PROP_gimple_any
, /* properties_required */
101 PROP_gimple_lcf
, /* properties_provided */
102 PROP_gimple_any
, /* properties_destroyed */
103 0, /* todo_flags_start */
104 TODO_dump_func
/* todo_flags_finish */
108 /* Lowers the EXPR. Unlike gimplification the statements are not relowered
109 when they are changed -- if this has to be done, the lowering routine must
110 do it explicitly. DATA is passed through the recursion. */
113 lower_stmt_body (tree expr
, struct lower_data
*data
)
115 tree_stmt_iterator tsi
;
117 for (tsi
= tsi_start (expr
); !tsi_end_p (tsi
); )
118 lower_stmt (&tsi
, data
);
121 /* Lowers statement TSI. DATA is passed through the recursion. */
124 lower_stmt (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
126 tree stmt
= tsi_stmt (*tsi
);
128 if (EXPR_HAS_LOCATION (stmt
) && data
)
129 TREE_BLOCK (stmt
) = data
->block
;
131 switch (TREE_CODE (stmt
))
134 lower_bind_expr (tsi
, data
);
137 lower_cond_expr (tsi
, data
);
140 case TRY_FINALLY_EXPR
:
142 lower_stmt_body (TREE_OPERAND (stmt
, 0), data
);
143 lower_stmt_body (TREE_OPERAND (stmt
, 1), data
);
146 lower_stmt_body (CATCH_BODY (stmt
), data
);
149 lower_stmt_body (EH_FILTER_FAILURE (stmt
), data
);
164 print_node_brief (stderr
, "", stmt
, 0);
172 /* Lowers a bind_expr TSI. DATA is passed through the recursion. */
175 lower_bind_expr (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
177 tree old_block
= data
->block
;
178 tree stmt
= tsi_stmt (*tsi
);
179 tree new_block
= BIND_EXPR_BLOCK (stmt
);
183 if (new_block
== old_block
)
185 /* The outermost block of the original function may not be the
186 outermost statement chain of the gimplified function. So we
187 may see the outermost block just inside the function. */
188 if (new_block
!= DECL_INITIAL (current_function_decl
))
194 /* We do not expect to handle duplicate blocks. */
195 if (TREE_ASM_WRITTEN (new_block
))
197 TREE_ASM_WRITTEN (new_block
) = 1;
199 /* Block tree may get clobbered by inlining. Normally this would
200 be fixed in rest_of_decl_compilation using block notes, but
201 since we are not going to emit them, it is up to us. */
202 BLOCK_CHAIN (new_block
) = BLOCK_SUBBLOCKS (old_block
);
203 BLOCK_SUBBLOCKS (old_block
) = new_block
;
204 BLOCK_SUBBLOCKS (new_block
) = NULL_TREE
;
205 BLOCK_SUPERCONTEXT (new_block
) = old_block
;
207 data
->block
= new_block
;
211 record_vars (BIND_EXPR_VARS (stmt
));
212 lower_stmt_body (BIND_EXPR_BODY (stmt
), data
);
216 if (data
->block
!= new_block
)
219 BLOCK_SUBBLOCKS (new_block
)
220 = blocks_nreverse (BLOCK_SUBBLOCKS (new_block
));
221 data
->block
= old_block
;
224 /* The BIND_EXPR no longer carries any useful information -- kill it. */
225 tsi_link_before (tsi
, BIND_EXPR_BODY (stmt
), TSI_SAME_STMT
);
229 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
230 need not be 100% accurate; simply be conservative and return true if we
231 don't know. This is used only to avoid stupidly generating extra code.
232 If we're wrong, we'll just delete the extra code later. */
235 block_may_fallthru (tree block
)
237 tree stmt
= expr_last (block
);
239 switch (stmt
? TREE_CODE (stmt
) : ERROR_MARK
)
245 /* Easy cases. If the last statement of the block implies
246 control transfer, then we can't fall through. */
250 if (block_may_fallthru (COND_EXPR_THEN (stmt
)))
252 return block_may_fallthru (COND_EXPR_ELSE (stmt
));
255 return block_may_fallthru (BIND_EXPR_BODY (stmt
));
257 case TRY_FINALLY_EXPR
:
258 return block_may_fallthru (TREE_OPERAND (stmt
, 1));
261 if (TREE_CODE (TREE_OPERAND (stmt
, 1)) == CALL_EXPR
)
262 stmt
= TREE_OPERAND (stmt
, 1);
268 /* Functions that do not return do not fall through. */
269 return (call_expr_flags (stmt
) & ECF_NORETURN
) == 0;
276 /* Lowers a cond_expr TSI. DATA is passed through the recursion. */
279 lower_cond_expr (tree_stmt_iterator
*tsi
, struct lower_data
*data
)
281 tree stmt
= tsi_stmt (*tsi
);
282 bool then_is_goto
, else_is_goto
;
283 tree then_branch
, else_branch
;
284 tree then_goto
, else_goto
;
286 then_branch
= COND_EXPR_THEN (stmt
);
287 else_branch
= COND_EXPR_ELSE (stmt
);
289 lower_stmt_body (then_branch
, data
);
290 lower_stmt_body (else_branch
, data
);
292 then_goto
= expr_only (then_branch
);
293 then_is_goto
= then_goto
&& simple_goto_p (then_goto
);
295 else_goto
= expr_only (else_branch
);
296 else_is_goto
= else_goto
&& simple_goto_p (else_goto
);
298 if (!then_is_goto
|| !else_is_goto
)
300 tree then_label
, else_label
, end_label
, t
;
302 then_label
= NULL_TREE
;
303 else_label
= NULL_TREE
;
304 end_label
= NULL_TREE
;
306 /* Replace the cond_expr with explicit gotos. */
309 t
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
310 if (TREE_SIDE_EFFECTS (then_branch
))
314 then_goto
= build_and_jump (&LABEL_EXPR_LABEL (t
));
319 t
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
320 if (TREE_SIDE_EFFECTS (else_branch
))
324 /* Both THEN and ELSE can be no-ops if one or both contained an
325 empty BIND_EXPR that was associated with the toplevel block
326 of an inlined function. In that case remove_useless_stmts
327 can't have cleaned things up for us; kill the whole
337 else_goto
= build_and_jump (&LABEL_EXPR_LABEL (t
));
342 bool may_fallthru
= block_may_fallthru (then_branch
);
344 tsi_link_after (tsi
, then_label
, TSI_CONTINUE_LINKING
);
345 tsi_link_after (tsi
, then_branch
, TSI_CONTINUE_LINKING
);
347 if (else_label
&& may_fallthru
)
349 end_label
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
350 t
= build_and_jump (&LABEL_EXPR_LABEL (end_label
));
351 tsi_link_after (tsi
, t
, TSI_CONTINUE_LINKING
);
357 tsi_link_after (tsi
, else_label
, TSI_CONTINUE_LINKING
);
358 tsi_link_after (tsi
, else_branch
, TSI_CONTINUE_LINKING
);
362 tsi_link_after (tsi
, end_label
, TSI_CONTINUE_LINKING
);
365 COND_EXPR_THEN (stmt
) = then_goto
;
366 COND_EXPR_ELSE (stmt
) = else_goto
;
372 /* Record the variables in VARS. */
375 record_vars (tree vars
)
377 for (; vars
; vars
= TREE_CHAIN (vars
))
381 /* Nothing to do in this case. */
382 if (DECL_EXTERNAL (var
))
384 if (TREE_CODE (var
) == FUNCTION_DECL
)
387 /* Record the variable. */
388 cfun
->unexpanded_var_list
= tree_cons (NULL_TREE
, var
,
389 cfun
->unexpanded_var_list
);
393 /* Check whether to expand a variable VAR. */
396 expand_var_p (tree var
)
398 struct var_ann_d
*ann
;
400 if (TREE_CODE (var
) != VAR_DECL
)
405 /* Remove all unused, unaliased temporaries. Also remove unused, unaliased
406 local variables during highly optimizing compilations. */
409 && ! ann
->may_aliases
411 && ! ann
->has_hidden_use
412 && ! TREE_ADDRESSABLE (var
)
413 && ! TREE_THIS_VOLATILE (var
)
414 && (DECL_ARTIFICIAL (var
) || optimize
>= 2))
420 /* Throw away variables that are unused. */
423 remove_useless_vars (void)
427 for (cell
= &cfun
->unexpanded_var_list
; *cell
; )
429 var
= TREE_VALUE (*cell
);
431 if (!expand_var_p (var
))
433 *cell
= TREE_CHAIN (*cell
);
437 cell
= &TREE_CHAIN (*cell
);
441 /* Expand variables in the unexpanded_var_list. */
444 expand_used_vars (void)
448 cfun
->unexpanded_var_list
= nreverse (cfun
->unexpanded_var_list
);
450 for (cell
= cfun
->unexpanded_var_list
; cell
; cell
= TREE_CHAIN (cell
))
451 expand_var (TREE_VALUE (cell
));
453 cfun
->unexpanded_var_list
= NULL_TREE
;
456 struct tree_opt_pass pass_remove_useless_vars
=
460 remove_useless_vars
, /* execute */
463 0, /* static_pass_number */
465 0, /* properties_required */
466 0, /* properties_provided */
467 0, /* properties_destroyed */
468 0, /* todo_flags_start */
469 TODO_dump_func
/* todo_flags_finish */