1 /* Tree lowering pass. This pass gimplifies the tree representation built
2 by the C-based front ends. The structure of gimplified, or
3 language-independent, trees is dictated by the grammar described in this
5 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
6 Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
7 Re-written to support lowering of whole function trees, documentation
8 and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 2, or (at your option) any later
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include "coretypes.h"
36 #include "tree-gimple.h"
37 #include "hard-reg-set.h"
38 #include "basic-block.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "diagnostic.h"
42 #include "langhooks.h"
43 #include "langhooks-def.h"
47 #include "tree-dump.h"
48 #include "c-pretty-print.h"
52 /* The gimplification pass converts the language-dependent trees
53 (ld-trees) emitted by the parser into language-independent trees
54 (li-trees) that are the target of SSA analysis and transformations.
56 Language-independent trees are based on the SIMPLE intermediate
57 representation used in the McCAT compiler framework:
59 "Designing the McCAT Compiler Based on a Family of Structured
60 Intermediate Representations,"
61 L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
62 Proceedings of the 5th International Workshop on Languages and
63 Compilers for Parallel Computing, no. 757 in Lecture Notes in
64 Computer Science, New Haven, Connecticut, pp. 406-420,
65 Springer-Verlag, August 3-5, 1992.
67 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
69 Basically, we walk down gimplifying the nodes that we encounter. As we
70 walk back up, we check that they fit our constraints, and copy them
71 into temporaries if not. */
73 /* Local declarations. */
75 static void gimplify_cleanup_stmts (tree);
77 enum bc_t { bc_break = 0, bc_continue = 1 };
79 static struct c_gimplify_ctx
81 /* For handling break and continue. */
82 tree current_bc_label;
91 ctxp = (struct c_gimplify_ctx *) xcalloc (1, sizeof (struct c_gimplify_ctx));
92 ctxp->bc_id[bc_continue] = get_identifier ("continue");
93 ctxp->bc_id[bc_break] = get_identifier ("break");
99 if (!ctxp || ctxp->current_bc_label)
105 /* Gimplification of statement trees. */
107 /* Convert the tree representation of FNDECL from C frontend trees to
111 c_genericize (tree fndecl)
114 int local_dump_flags;
115 struct cgraph_node *cgn;
117 /* Dump the C-specific tree IR. */
118 dump_file = dump_begin (TDI_original, &local_dump_flags);
121 fprintf (dump_file, "\n;; Function %s",
122 lang_hooks.decl_printable_name (fndecl, 2));
123 fprintf (dump_file, " (%s)\n",
124 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
125 fprintf (dump_file, ";; enabled by -%s\n", dump_flag_name (TDI_original));
126 fprintf (dump_file, "\n");
128 if (local_dump_flags & TDF_RAW)
129 dump_node (DECL_SAVED_TREE (fndecl),
130 TDF_SLIM | local_dump_flags, dump_file);
132 print_c_tree (dump_file, DECL_SAVED_TREE (fndecl));
133 fprintf (dump_file, "\n");
135 dump_end (TDI_original, dump_file);
138 /* Go ahead and gimplify for now. */
140 gimplify_cleanup_stmts (fndecl);
141 gimplify_function_tree (fndecl);
144 /* Dump the genericized tree IR. */
145 dump_function (TDI_generic, fndecl);
147 /* Genericize all nested functions now. We do things in this order so
148 that items like VLA sizes are expanded properly in the context of
149 the correct function. */
150 cgn = cgraph_node (fndecl);
151 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
152 c_genericize (cgn->decl);
155 /* Genericize a CLEANUP_STMT. This just turns into a TRY_FINALLY or
156 TRY_CATCH depending on whether it's EH-only. */
159 gimplify_cleanup_stmt (tree *stmt_p, int *walk_subtrees,
160 void *data ATTRIBUTE_UNUSED)
164 if (DECL_P (stmt) || TYPE_P (stmt))
166 else if (TREE_CODE (stmt) == CLEANUP_STMT)
167 *stmt_p = build (CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR,
168 void_type_node, CLEANUP_BODY (stmt), CLEANUP_EXPR (stmt));
174 gimplify_cleanup_stmts (tree fndecl)
176 walk_tree (&DECL_SAVED_TREE (fndecl), gimplify_cleanup_stmt, NULL, NULL);
180 add_block_to_enclosing (tree block)
184 for (enclosing = gimple_current_bind_expr ();
185 enclosing; enclosing = TREE_CHAIN (enclosing))
186 if (BIND_EXPR_BLOCK (enclosing))
189 enclosing = BIND_EXPR_BLOCK (enclosing);
190 BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
193 /* Genericize a scope by creating a new BIND_EXPR.
194 BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
195 In the latter case, we need to create a new BLOCK and add it to the
196 BLOCK_SUBBLOCKS of the enclosing block.
197 BODY is a chain of C _STMT nodes for the contents of the scope, to be
201 c_build_bind_expr (tree block, tree body)
205 if (block == NULL_TREE)
207 else if (TREE_CODE (block) == BLOCK)
208 decls = BLOCK_VARS (block);
212 if (DECL_ARTIFICIAL (decls))
216 block = make_node (BLOCK);
217 BLOCK_VARS (block) = decls;
218 add_block_to_enclosing (block);
223 body = build_empty_stmt ();
226 bind = build (BIND_EXPR, void_type_node, decls, body, block);
227 TREE_SIDE_EFFECTS (bind) = 1;
235 /* Gimplify an EXPR_STMT node.
237 STMT is the statement node.
239 PRE_P points to the list where side effects that must happen before
240 STMT should be stored.
242 POST_P points to the list where side effects that must happen after
243 STMT should be stored. */
245 static enum gimplify_status
246 gimplify_expr_stmt (tree *stmt_p)
248 tree stmt = EXPR_STMT_EXPR (*stmt_p);
250 if (stmt == error_mark_node)
253 /* Gimplification of a statement expression will nullify the
254 statement if all its side effects are moved to *PRE_P and *POST_P.
256 In this case we will not want to emit the gimplified statement.
257 However, we may still want to emit a warning, so we do that before
259 if (stmt && (extra_warnings || warn_unused_value))
261 if (!TREE_SIDE_EFFECTS (stmt))
263 if (!IS_EMPTY_STMT (stmt)
264 && !VOID_TYPE_P (TREE_TYPE (stmt))
265 && !TREE_NO_WARNING (stmt))
266 warning ("statement with no effect");
268 else if (warn_unused_value)
270 /* Kludge for 20020220-2.c. warn_if_unused_value shouldn't use
271 the stmt file location info. */
272 set_file_and_line_for_stmt (input_location);
273 warn_if_unused_value (stmt);
277 if (stmt == NULL_TREE)
278 stmt = build_empty_stmt ();
279 else if (stmts_are_full_exprs_p ())
280 stmt = build1 (CLEANUP_POINT_EXPR, void_type_node, stmt);
287 /* Begin a scope which can be exited by a break or continue statement. BC
290 Just creates a label and pushes it into the current context. */
293 begin_bc_block (enum bc_t bc)
295 tree label = create_artificial_label ();
296 DECL_NAME (label) = ctxp->bc_id[bc];
297 TREE_CHAIN (label) = ctxp->current_bc_label;
298 ctxp->current_bc_label = label;
302 /* Finish a scope which can be exited by a break or continue statement.
303 LABEL was returned from the most recent call to begin_bc_block. BODY is
304 an expression for the contents of the scope.
306 If we saw a break (or continue) in the scope, append a LABEL_EXPR to
307 body. Otherwise, just forget the label. */
310 finish_bc_block (tree label, tree body)
312 if (label != ctxp->current_bc_label)
315 if (TREE_USED (label))
319 /* Clear the name so flow can delete the label. */
320 DECL_NAME (label) = NULL_TREE;
321 t = build1 (LABEL_EXPR, void_type_node, label);
323 append_to_statement_list (body, &sl);
324 append_to_statement_list (t, &sl);
328 ctxp->current_bc_label = TREE_CHAIN (label);
329 TREE_CHAIN (label) = NULL_TREE;
333 /* Build a GOTO_EXPR to represent a break or continue statement. BC
337 build_bc_goto (enum bc_t bc)
340 tree target_name = ctxp->bc_id[bc];
342 /* Look for the appropriate type of label. */
343 for (label = ctxp->current_bc_label;
345 label = TREE_CHAIN (label))
346 if (DECL_NAME (label) == target_name)
349 if (label == NULL_TREE)
352 error ("break statement not within loop or switch");
354 error ("continue statement not within loop or switch");
359 /* Mark the label used for finish_bc_block. */
360 TREE_USED (label) = 1;
361 return build1 (GOTO_EXPR, void_type_node, label);
364 /* Build a generic representation of one of the C loop forms. COND is the
365 loop condition or NULL_TREE. BODY is the (possibly compound) statement
366 controlled by the loop. INCR is the increment expression of a for-loop,
367 or NULL_TREE. COND_IS_FIRST indicates whether the condition is
368 evaluated before the loop body as in while and for loops, or after the
369 loop body as in do-while loops. */
372 gimplify_c_loop (tree cond, tree body, tree incr, bool cond_is_first)
374 tree top, entry, exit, cont_block, break_block, stmt_list, t;
375 location_t stmt_locus;
377 stmt_locus = input_location;
379 /* Detect do { ... } while (0) and don't generate loop construct. */
380 if (!cond_is_first && cond && integer_zerop (cond))
384 /* If we use a LOOP_EXPR here, we have to feed the whole thing
385 back through the main gimplifier to lower it. Given that we
386 have to gimplify the loop body NOW so that we can resolve
387 break/continue stmts, seems easier to just expand to gotos. */
388 top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
391 break_block = begin_bc_block (bc_break);
395 /* If we have an exit condition, then we build an IF with gotos either
396 out of the loop, or to the top of it. If there's no exit condition,
397 then we just build a jump back to the top. */
398 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
401 t = build_bc_goto (bc_break);
402 exit = build (COND_EXPR, void_type_node, cond, exit, t);
404 gimplify_stmt (&exit);
410 cont_block = begin_bc_block (bc_continue);
412 gimplify_stmt (&body);
413 if (incr && stmts_are_full_exprs_p ())
414 incr = fold (build1 (CLEANUP_POINT_EXPR, void_type_node, incr));
415 gimplify_stmt (&incr);
417 body = finish_bc_block (cont_block, body);
421 if (cond_is_first && cond)
423 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
424 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
425 append_to_statement_list (t, &stmt_list);
430 append_to_statement_list (top, &stmt_list);
431 append_to_statement_list (body, &stmt_list);
432 append_to_statement_list (incr, &stmt_list);
433 append_to_statement_list (entry, &stmt_list);
434 append_to_statement_list (exit, &stmt_list);
436 annotate_all_with_locus (&stmt_list, stmt_locus);
438 return finish_bc_block (break_block, stmt_list);
441 /* Gimplify a FOR_STMT node. Move the stuff in the for-init-stmt into the
442 prequeue and hand off to gimplify_c_loop. */
444 static enum gimplify_status
445 gimplify_for_stmt (tree *stmt_p, tree *pre_p)
449 if (FOR_INIT_STMT (stmt))
451 gimplify_stmt (&FOR_INIT_STMT (stmt));
452 append_to_statement_list (FOR_INIT_STMT (stmt), pre_p);
454 *stmt_p = gimplify_c_loop (FOR_COND (stmt), FOR_BODY (stmt),
460 /* Gimplify a WHILE_STMT node. */
462 static enum gimplify_status
463 gimplify_while_stmt (tree *stmt_p)
466 *stmt_p = gimplify_c_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
471 /* Gimplify a DO_STMT node. */
473 static enum gimplify_status
474 gimplify_do_stmt (tree *stmt_p)
477 *stmt_p = gimplify_c_loop (DO_COND (stmt), DO_BODY (stmt),
482 /* Genericize an IF_STMT by turning it into a COND_EXPR. */
484 static enum gimplify_status
485 gimplify_if_stmt (tree *stmt_p)
487 tree stmt, then_, else_;
490 then_ = THEN_CLAUSE (stmt);
491 else_ = ELSE_CLAUSE (stmt);
494 then_ = build_empty_stmt ();
496 else_ = build_empty_stmt ();
498 stmt = build (COND_EXPR, void_type_node, IF_COND (stmt), then_, else_);
504 /* Genericize a SWITCH_STMT by turning it into a SWITCH_EXPR. */
506 static enum gimplify_status
507 gimplify_switch_stmt (tree *stmt_p)
510 tree break_block, body;
511 location_t stmt_locus = input_location;
513 break_block = begin_bc_block (bc_break);
515 body = SWITCH_BODY (stmt);
517 body = build_empty_stmt ();
519 *stmt_p = build (SWITCH_EXPR, SWITCH_TYPE (stmt), SWITCH_COND (stmt),
521 annotate_with_locus (*stmt_p, stmt_locus);
522 gimplify_stmt (stmt_p);
524 *stmt_p = finish_bc_block (break_block, *stmt_p);
528 /* Genericize a RETURN_STMT by turning it into a RETURN_EXPR. */
530 static enum gimplify_status
531 gimplify_return_stmt (tree *stmt_p)
533 tree expr = RETURN_STMT_EXPR (*stmt_p);
534 expr = build1 (RETURN_EXPR, void_type_node, expr);
535 if (stmts_are_full_exprs_p ())
536 expr = build1 (CLEANUP_POINT_EXPR, void_type_node, expr);
541 /* Gimplifies a DECL_STMT node *STMT_P by making any necessary allocation
542 and initialization explicit. */
544 static enum gimplify_status
545 gimplify_decl_stmt (tree *stmt_p)
548 tree decl = DECL_STMT_DECL (stmt);
549 tree pre = NULL_TREE;
550 tree post = NULL_TREE;
552 if (TREE_TYPE (decl) == error_mark_node)
558 if (TREE_CODE (decl) == TYPE_DECL)
560 tree type = TREE_TYPE (decl);
561 if (TYPE_SIZE_UNIT (type)
562 && !TREE_CONSTANT (TYPE_SIZE_UNIT (type)))
564 /* This is a variable-sized array type. Simplify its size. */
565 tree temp = TYPE_SIZE_UNIT (type);
566 gimplify_expr (&temp, &pre, &post, is_gimple_val, fb_rvalue);
570 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
572 tree init = DECL_INITIAL (decl);
574 if (!TREE_CONSTANT (DECL_SIZE (decl)))
576 tree pt_type = build_pointer_type (TREE_TYPE (decl));
579 /* This is a variable-sized decl. Simplify its size and mark it
580 for deferred expansion. Note that mudflap depends on the format
581 of the emitted code: see mx_register_decls(). */
583 size = get_initialized_tmp_var (DECL_SIZE_UNIT (decl), &pre, &post);
584 DECL_DEFER_OUTPUT (decl) = 1;
585 alloc = build_function_call_expr
586 (implicit_built_in_decls[BUILT_IN_STACK_ALLOC],
587 tree_cons (NULL_TREE,
588 build1 (ADDR_EXPR, pt_type, decl),
589 tree_cons (NULL_TREE, size, NULL_TREE)));
590 append_to_compound_expr (alloc, &pre);
593 if (init && init != error_mark_node)
595 if (!TREE_STATIC (decl))
597 /* Do not warn about int x = x; as it is a GCC extension
598 to turn off this warning but only if warn_init_self
600 if (init == decl && !warn_init_self)
601 TREE_NO_WARNING (decl) = 1;
603 DECL_INITIAL (decl) = NULL_TREE;
604 init = build (MODIFY_EXPR, void_type_node, decl, init);
605 if (stmts_are_full_exprs_p ())
606 init = build1 (CLEANUP_POINT_EXPR, void_type_node, init);
607 append_to_compound_expr (init, &pre);
611 /* We must still examine initializers for static variables
612 as they may contain a label address. */
613 walk_tree (&init, force_labels_r, NULL, NULL);
617 /* This decl isn't mentioned in the enclosing block, so add it to the
618 list of temps. FIXME it seems a bit of a kludge to say that
619 anonymous artificial vars aren't pushed, but everything else is. */
620 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
621 gimple_add_tmp_var (decl);
624 append_to_compound_expr (post, &pre);
629 /* Gimplification of expression trees. */
631 /* Gimplify a C99 compound literal expression. This just means adding the
632 DECL_STMT before the current EXPR_STMT and using its anonymous decl
635 static enum gimplify_status
636 gimplify_compound_literal_expr (tree *expr_p)
638 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
639 tree decl = DECL_STMT_DECL (decl_s);
641 /* This decl isn't mentioned in the enclosing block, so add it to the
642 list of temps. FIXME it seems a bit of a kludge to say that
643 anonymous artificial vars aren't pushed, but everything else is. */
644 if (DECL_NAME (decl) == NULL_TREE)
645 gimple_add_tmp_var (decl);
647 gimplify_decl_stmt (&decl_s);
648 *expr_p = decl_s ? decl_s : decl;
652 /* Do C-specific gimplification. Args are as for gimplify_expr. */
655 c_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
657 enum tree_code code = TREE_CODE (*expr_p);
661 case COMPOUND_LITERAL_EXPR:
662 return gimplify_compound_literal_expr (expr_p);
665 return gimplify_for_stmt (expr_p, pre_p);
668 return gimplify_while_stmt (expr_p);
671 return gimplify_do_stmt (expr_p);
674 return gimplify_if_stmt (expr_p);
677 return gimplify_switch_stmt (expr_p);
680 return gimplify_expr_stmt (expr_p);
683 return gimplify_return_stmt (expr_p);
686 return gimplify_decl_stmt (expr_p);
689 *expr_p = build_bc_goto (bc_continue);
693 *expr_p = build_bc_goto (bc_break);