2005-01-22 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / c-gimplify.c
blobdb6cd88fd9b566459572b90937e03b9315cd9e4c
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
4 file.
5 Copyright (C) 2002, 2003, 2004 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
15 version.
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
20 for more details.
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
25 02111-1307, USA. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "errors.h"
33 #include "varray.h"
34 #include "c-tree.h"
35 #include "c-common.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"
44 #include "flags.h"
45 #include "rtl.h"
46 #include "toplev.h"
47 #include "tree-dump.h"
48 #include "c-pretty-print.h"
49 #include "cgraph.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 enum bc_t { bc_break = 0, bc_continue = 1 };
77 static struct c_gimplify_ctx
79 /* For handling break and continue. */
80 tree current_bc_label;
81 tree bc_id[2];
82 } *ctxp;
84 static void
85 push_context (void)
87 gcc_assert (!ctxp);
88 ctxp = (struct c_gimplify_ctx *) xcalloc (1, sizeof (struct c_gimplify_ctx));
89 ctxp->bc_id[bc_continue] = get_identifier ("continue");
90 ctxp->bc_id[bc_break] = get_identifier ("break");
93 static void
94 pop_context (void)
96 gcc_assert (ctxp && !ctxp->current_bc_label);
97 free (ctxp);
98 ctxp = NULL;
101 /* Gimplification of statement trees. */
103 /* Convert the tree representation of FNDECL from C frontend trees to
104 GENERIC. */
106 void
107 c_genericize (tree fndecl)
109 FILE *dump_file;
110 int local_dump_flags;
111 struct cgraph_node *cgn;
113 /* Dump the C-specific tree IR. */
114 dump_file = dump_begin (TDI_original, &local_dump_flags);
115 if (dump_file)
117 fprintf (dump_file, "\n;; Function %s",
118 lang_hooks.decl_printable_name (fndecl, 2));
119 fprintf (dump_file, " (%s)\n",
120 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
121 fprintf (dump_file, ";; enabled by -%s\n", dump_flag_name (TDI_original));
122 fprintf (dump_file, "\n");
124 if (local_dump_flags & TDF_RAW)
125 dump_node (DECL_SAVED_TREE (fndecl),
126 TDF_SLIM | local_dump_flags, dump_file);
127 else
128 print_c_tree (dump_file, DECL_SAVED_TREE (fndecl));
129 fprintf (dump_file, "\n");
131 dump_end (TDI_original, dump_file);
134 /* Go ahead and gimplify for now. */
135 push_context ();
136 gimplify_function_tree (fndecl);
137 pop_context ();
139 /* Dump the genericized tree IR. */
140 dump_function (TDI_generic, fndecl);
142 /* Genericize all nested functions now. We do things in this order so
143 that items like VLA sizes are expanded properly in the context of
144 the correct function. */
145 cgn = cgraph_node (fndecl);
146 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
147 c_genericize (cgn->decl);
150 static void
151 add_block_to_enclosing (tree block)
153 tree enclosing;
155 for (enclosing = gimple_current_bind_expr ();
156 enclosing; enclosing = TREE_CHAIN (enclosing))
157 if (BIND_EXPR_BLOCK (enclosing))
158 break;
160 enclosing = BIND_EXPR_BLOCK (enclosing);
161 BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
164 /* Genericize a scope by creating a new BIND_EXPR.
165 BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
166 In the latter case, we need to create a new BLOCK and add it to the
167 BLOCK_SUBBLOCKS of the enclosing block.
168 BODY is a chain of C _STMT nodes for the contents of the scope, to be
169 genericized. */
171 tree
172 c_build_bind_expr (tree block, tree body)
174 tree decls, bind;
176 if (block == NULL_TREE)
177 decls = NULL_TREE;
178 else if (TREE_CODE (block) == BLOCK)
179 decls = BLOCK_VARS (block);
180 else
182 decls = block;
183 if (DECL_ARTIFICIAL (decls))
184 block = NULL_TREE;
185 else
187 block = make_node (BLOCK);
188 BLOCK_VARS (block) = decls;
189 add_block_to_enclosing (block);
193 if (!body)
194 body = build_empty_stmt ();
195 if (decls || block)
197 bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
198 TREE_SIDE_EFFECTS (bind) = 1;
200 else
201 bind = body;
203 return bind;
206 /* Gimplify an EXPR_STMT node.
208 STMT is the statement node.
210 PRE_P points to the list where side effects that must happen before
211 STMT should be stored.
213 POST_P points to the list where side effects that must happen after
214 STMT should be stored. */
216 static enum gimplify_status
217 gimplify_expr_stmt (tree *stmt_p)
219 tree stmt = EXPR_STMT_EXPR (*stmt_p);
221 if (stmt == error_mark_node)
222 stmt = NULL;
224 /* Gimplification of a statement expression will nullify the
225 statement if all its side effects are moved to *PRE_P and *POST_P.
227 In this case we will not want to emit the gimplified statement.
228 However, we may still want to emit a warning, so we do that before
229 gimplification. */
230 if (stmt && (extra_warnings || warn_unused_value))
232 if (!TREE_SIDE_EFFECTS (stmt))
234 if (!IS_EMPTY_STMT (stmt)
235 && !VOID_TYPE_P (TREE_TYPE (stmt))
236 && !TREE_NO_WARNING (stmt))
237 warning ("statement with no effect");
239 else if (warn_unused_value)
240 warn_if_unused_value (stmt, input_location);
243 if (stmt == NULL_TREE)
244 stmt = alloc_stmt_list ();
246 *stmt_p = stmt;
248 return GS_OK;
251 /* Begin a scope which can be exited by a break or continue statement. BC
252 indicates which.
254 Just creates a label and pushes it into the current context. */
256 static tree
257 begin_bc_block (enum bc_t bc)
259 tree label = create_artificial_label ();
260 DECL_NAME (label) = ctxp->bc_id[bc];
261 TREE_CHAIN (label) = ctxp->current_bc_label;
262 ctxp->current_bc_label = label;
263 return label;
266 /* Finish a scope which can be exited by a break or continue statement.
267 LABEL was returned from the most recent call to begin_bc_block. BODY is
268 an expression for the contents of the scope.
270 If we saw a break (or continue) in the scope, append a LABEL_EXPR to
271 body. Otherwise, just forget the label. */
273 static tree
274 finish_bc_block (tree label, tree body)
276 gcc_assert (label == ctxp->current_bc_label);
278 if (TREE_USED (label))
280 tree t, sl = NULL;
282 /* Clear the name so flow can delete the label. */
283 DECL_NAME (label) = NULL_TREE;
284 t = build1 (LABEL_EXPR, void_type_node, label);
286 append_to_statement_list (body, &sl);
287 append_to_statement_list (t, &sl);
288 body = sl;
291 ctxp->current_bc_label = TREE_CHAIN (label);
292 TREE_CHAIN (label) = NULL_TREE;
293 return body;
296 /* Build a GOTO_EXPR to represent a break or continue statement. BC
297 indicates which. */
299 static tree
300 build_bc_goto (enum bc_t bc)
302 tree label;
303 tree target_name = ctxp->bc_id[bc];
305 /* Look for the appropriate type of label. */
306 for (label = ctxp->current_bc_label;
307 label;
308 label = TREE_CHAIN (label))
309 if (DECL_NAME (label) == target_name)
310 break;
312 if (label == NULL_TREE)
314 if (bc == bc_break)
315 error ("break statement not within loop or switch");
316 else
317 error ("continue statement not within loop or switch");
319 return NULL_TREE;
322 /* Mark the label used for finish_bc_block. */
323 TREE_USED (label) = 1;
324 return build1 (GOTO_EXPR, void_type_node, label);
327 /* Build a generic representation of one of the C loop forms. COND is the
328 loop condition or NULL_TREE. BODY is the (possibly compound) statement
329 controlled by the loop. INCR is the increment expression of a for-loop,
330 or NULL_TREE. COND_IS_FIRST indicates whether the condition is
331 evaluated before the loop body as in while and for loops, or after the
332 loop body as in do-while loops. */
334 static tree
335 gimplify_c_loop (tree cond, tree body, tree incr, bool cond_is_first)
337 tree top, entry, exit, cont_block, break_block, stmt_list, t;
338 location_t stmt_locus;
340 stmt_locus = input_location;
341 stmt_list = NULL_TREE;
342 entry = NULL_TREE;
344 break_block = begin_bc_block (bc_break);
345 cont_block = begin_bc_block (bc_continue);
347 /* If condition is zero don't generate a loop construct. */
348 if (cond && integer_zerop (cond))
350 top = NULL_TREE;
351 exit = NULL_TREE;
352 if (cond_is_first)
354 t = build_bc_goto (bc_break);
355 append_to_statement_list (t, &stmt_list);
358 else
360 /* If we use a LOOP_EXPR here, we have to feed the whole thing
361 back through the main gimplifier to lower it. Given that we
362 have to gimplify the loop body NOW so that we can resolve
363 break/continue stmts, seems easier to just expand to gotos. */
364 top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
366 /* If we have an exit condition, then we build an IF with gotos either
367 out of the loop, or to the top of it. If there's no exit condition,
368 then we just build a jump back to the top. */
369 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
370 if (cond && !integer_nonzerop (cond))
372 t = build_bc_goto (bc_break);
373 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
374 exit = fold (exit);
375 gimplify_stmt (&exit);
377 if (cond_is_first)
379 if (incr)
381 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
382 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
384 else
385 t = build_bc_goto (bc_continue);
386 append_to_statement_list (t, &stmt_list);
391 gimplify_stmt (&body);
392 gimplify_stmt (&incr);
394 body = finish_bc_block (cont_block, body);
396 append_to_statement_list (top, &stmt_list);
397 append_to_statement_list (body, &stmt_list);
398 append_to_statement_list (incr, &stmt_list);
399 append_to_statement_list (entry, &stmt_list);
400 append_to_statement_list (exit, &stmt_list);
402 annotate_all_with_locus (&stmt_list, stmt_locus);
404 return finish_bc_block (break_block, stmt_list);
407 /* Gimplify a FOR_STMT node. Move the stuff in the for-init-stmt into the
408 prequeue and hand off to gimplify_c_loop. */
410 static enum gimplify_status
411 gimplify_for_stmt (tree *stmt_p, tree *pre_p)
413 tree stmt = *stmt_p;
415 if (FOR_INIT_STMT (stmt))
416 gimplify_and_add (FOR_INIT_STMT (stmt), pre_p);
418 *stmt_p = gimplify_c_loop (FOR_COND (stmt), FOR_BODY (stmt),
419 FOR_EXPR (stmt), 1);
421 return GS_ALL_DONE;
424 /* Gimplify a WHILE_STMT node. */
426 static enum gimplify_status
427 gimplify_while_stmt (tree *stmt_p)
429 tree stmt = *stmt_p;
430 *stmt_p = gimplify_c_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
431 NULL_TREE, 1);
432 return GS_ALL_DONE;
435 /* Gimplify a DO_STMT node. */
437 static enum gimplify_status
438 gimplify_do_stmt (tree *stmt_p)
440 tree stmt = *stmt_p;
441 *stmt_p = gimplify_c_loop (DO_COND (stmt), DO_BODY (stmt),
442 NULL_TREE, 0);
443 return GS_ALL_DONE;
446 /* Genericize a SWITCH_STMT by turning it into a SWITCH_EXPR. */
448 static enum gimplify_status
449 gimplify_switch_stmt (tree *stmt_p)
451 tree stmt = *stmt_p;
452 tree break_block, body;
453 location_t stmt_locus = input_location;
455 break_block = begin_bc_block (bc_break);
457 body = SWITCH_BODY (stmt);
458 if (!body)
459 body = build_empty_stmt ();
461 *stmt_p = build3 (SWITCH_EXPR, SWITCH_TYPE (stmt), SWITCH_COND (stmt),
462 body, NULL_TREE);
463 SET_EXPR_LOCATION (*stmt_p, stmt_locus);
464 gimplify_stmt (stmt_p);
466 *stmt_p = finish_bc_block (break_block, *stmt_p);
467 return GS_ALL_DONE;
470 /* Gimplification of expression trees. */
472 /* Gimplify a C99 compound literal expression. This just means adding the
473 DECL_EXPR before the current EXPR_STMT and using its anonymous decl
474 instead. */
476 static enum gimplify_status
477 gimplify_compound_literal_expr (tree *expr_p, tree *pre_p)
479 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
480 tree decl = DECL_EXPR_DECL (decl_s);
482 /* This decl isn't mentioned in the enclosing block, so add it to the
483 list of temps. FIXME it seems a bit of a kludge to say that
484 anonymous artificial vars aren't pushed, but everything else is. */
485 if (DECL_NAME (decl) == NULL_TREE)
486 gimple_add_tmp_var (decl);
488 gimplify_and_add (decl_s, pre_p);
489 *expr_p = decl;
490 return GS_OK;
493 /* Do C-specific gimplification. Args are as for gimplify_expr. */
496 c_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
498 enum tree_code code = TREE_CODE (*expr_p);
500 switch (code)
502 case DECL_EXPR:
503 /* This is handled mostly by gimplify.c, but we have to deal with
504 not warning about int x = x; as it is a GCC extension to turn off
505 this warning but only if warn_init_self is zero. */
506 if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
507 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
508 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
509 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p))
510 == DECL_EXPR_DECL (*expr_p))
511 && !warn_init_self)
512 TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
513 return GS_UNHANDLED;
515 case COMPOUND_LITERAL_EXPR:
516 return gimplify_compound_literal_expr (expr_p, pre_p);
518 case FOR_STMT:
519 return gimplify_for_stmt (expr_p, pre_p);
521 case WHILE_STMT:
522 return gimplify_while_stmt (expr_p);
524 case DO_STMT:
525 return gimplify_do_stmt (expr_p);
527 case SWITCH_STMT:
528 return gimplify_switch_stmt (expr_p);
530 case EXPR_STMT:
531 return gimplify_expr_stmt (expr_p);
533 case CONTINUE_STMT:
534 *expr_p = build_bc_goto (bc_continue);
535 return GS_ALL_DONE;
537 case BREAK_STMT:
538 *expr_p = build_bc_goto (bc_break);
539 return GS_ALL_DONE;
541 default:
542 return GS_UNHANDLED;