re PR middle-end/30142 ([meta-bug] invalid gimple)
[official-gcc.git] / gcc / c-gimplify.c
blob5b309343361d4a8700ddd8dd185087fc27596433
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, 2005, 2007, 2008
6 Free Software Foundation, Inc.
7 Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
8 Re-written to support lowering of whole function trees, documentation
9 and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free
15 Software Foundation; either version 3, or (at your option) any later
16 version.
18 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING3. If not see
25 <http://www.gnu.org/licenses/>. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "varray.h"
33 #include "c-tree.h"
34 #include "c-common.h"
35 #include "gimple.h"
36 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43 #include "flags.h"
44 #include "rtl.h"
45 #include "toplev.h"
46 #include "tree-dump.h"
47 #include "c-pretty-print.h"
48 #include "cgraph.h"
51 /* The gimplification pass converts the language-dependent trees
52 (ld-trees) emitted by the parser into language-independent trees
53 (li-trees) that are the target of SSA analysis and transformations.
55 Language-independent trees are based on the SIMPLE intermediate
56 representation used in the McCAT compiler framework:
58 "Designing the McCAT Compiler Based on a Family of Structured
59 Intermediate Representations,"
60 L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
61 Proceedings of the 5th International Workshop on Languages and
62 Compilers for Parallel Computing, no. 757 in Lecture Notes in
63 Computer Science, New Haven, Connecticut, pp. 406-420,
64 Springer-Verlag, August 3-5, 1992.
66 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
68 Basically, we walk down gimplifying the nodes that we encounter. As we
69 walk back up, we check that they fit our constraints, and copy them
70 into temporaries if not. */
72 /* Gimplification of statement trees. */
74 /* Convert the tree representation of FNDECL from C frontend trees to
75 GENERIC. */
77 void
78 c_genericize (tree fndecl)
80 FILE *dump_orig;
81 int local_dump_flags;
82 struct cgraph_node *cgn;
84 /* Dump the C-specific tree IR. */
85 dump_orig = dump_begin (TDI_original, &local_dump_flags);
86 if (dump_orig)
88 fprintf (dump_orig, "\n;; Function %s",
89 lang_hooks.decl_printable_name (fndecl, 2));
90 fprintf (dump_orig, " (%s)\n",
91 (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
92 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
93 fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
94 fprintf (dump_orig, "\n");
96 if (local_dump_flags & TDF_RAW)
97 dump_node (DECL_SAVED_TREE (fndecl),
98 TDF_SLIM | local_dump_flags, dump_orig);
99 else
100 print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
101 fprintf (dump_orig, "\n");
103 dump_end (TDI_original, dump_orig);
106 /* Go ahead and gimplify for now. */
107 gimplify_function_tree (fndecl);
109 dump_function (TDI_generic, fndecl);
111 /* Genericize all nested functions now. We do things in this order so
112 that items like VLA sizes are expanded properly in the context of
113 the correct function. */
114 cgn = cgraph_node (fndecl);
115 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
116 c_genericize (cgn->decl);
119 static void
120 add_block_to_enclosing (tree block)
122 unsigned i;
123 tree enclosing;
124 gimple bind;
125 VEC(gimple, heap) *stack = gimple_bind_expr_stack ();
127 for (i = 0; VEC_iterate (gimple, stack, i, bind); i++)
128 if (gimple_bind_block (bind))
129 break;
131 enclosing = gimple_bind_block (bind);
132 BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
135 /* Genericize a scope by creating a new BIND_EXPR.
136 BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
137 In the latter case, we need to create a new BLOCK and add it to the
138 BLOCK_SUBBLOCKS of the enclosing block.
139 BODY is a chain of C _STMT nodes for the contents of the scope, to be
140 genericized. */
142 tree
143 c_build_bind_expr (tree block, tree body)
145 tree decls, bind;
147 if (block == NULL_TREE)
148 decls = NULL_TREE;
149 else if (TREE_CODE (block) == BLOCK)
150 decls = BLOCK_VARS (block);
151 else
153 decls = block;
154 if (DECL_ARTIFICIAL (decls))
155 block = NULL_TREE;
156 else
158 block = make_node (BLOCK);
159 BLOCK_VARS (block) = decls;
160 add_block_to_enclosing (block);
164 if (!body)
165 body = build_empty_stmt ();
166 if (decls || block)
168 bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
169 TREE_SIDE_EFFECTS (bind) = 1;
171 else
172 bind = body;
174 return bind;
177 /* Gimplification of expression trees. */
179 /* Gimplify a C99 compound literal expression. This just means adding
180 the DECL_EXPR before the current statement and using its anonymous
181 decl instead. */
183 static enum gimplify_status
184 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p)
186 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
187 tree decl = DECL_EXPR_DECL (decl_s);
188 /* Mark the decl as addressable if the compound literal
189 expression is addressable now, otherwise it is marked too late
190 after we gimplify the initialization expression. */
191 if (TREE_ADDRESSABLE (*expr_p))
192 TREE_ADDRESSABLE (decl) = 1;
194 /* Preliminarily mark non-addressed complex variables as eligible
195 for promotion to gimple registers. We'll transform their uses
196 as we find them. */
197 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
198 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
199 && !TREE_THIS_VOLATILE (decl)
200 && !needs_to_live_in_memory (decl))
201 DECL_GIMPLE_REG_P (decl) = 1;
203 /* This decl isn't mentioned in the enclosing block, so add it to the
204 list of temps. FIXME it seems a bit of a kludge to say that
205 anonymous artificial vars aren't pushed, but everything else is. */
206 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
207 gimple_add_tmp_var (decl);
209 gimplify_and_add (decl_s, pre_p);
210 *expr_p = decl;
211 return GS_OK;
214 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
215 return a new CONSTRUCTOR if something changed. */
217 static tree
218 optimize_compound_literals_in_ctor (tree orig_ctor)
220 tree ctor = orig_ctor;
221 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
222 unsigned int idx, num = VEC_length (constructor_elt, elts);
224 for (idx = 0; idx < num; idx++)
226 tree value = VEC_index (constructor_elt, elts, idx)->value;
227 tree newval = value;
228 if (TREE_CODE (value) == CONSTRUCTOR)
229 newval = optimize_compound_literals_in_ctor (value);
230 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
232 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (value);
233 tree decl = DECL_EXPR_DECL (decl_s);
234 tree init = DECL_INITIAL (decl);
236 if (!TREE_ADDRESSABLE (value)
237 && !TREE_ADDRESSABLE (decl)
238 && init)
239 newval = init;
241 if (newval == value)
242 continue;
244 if (ctor == orig_ctor)
246 ctor = copy_node (orig_ctor);
247 CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
248 elts = CONSTRUCTOR_ELTS (ctor);
250 VEC_index (constructor_elt, elts, idx)->value = newval;
252 return ctor;
255 /* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in
256 gimplify_expr. */
259 c_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
260 gimple_seq *post_p ATTRIBUTE_UNUSED)
262 enum tree_code code = TREE_CODE (*expr_p);
264 switch (code)
266 case DECL_EXPR:
267 /* This is handled mostly by gimplify.c, but we have to deal with
268 not warning about int x = x; as it is a GCC extension to turn off
269 this warning but only if warn_init_self is zero. */
270 if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
271 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
272 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
273 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p))
274 == DECL_EXPR_DECL (*expr_p))
275 && !warn_init_self)
276 TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
277 return GS_UNHANDLED;
279 case COMPOUND_LITERAL_EXPR:
280 return gimplify_compound_literal_expr (expr_p, pre_p);
282 case INIT_EXPR:
283 case MODIFY_EXPR:
284 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == COMPOUND_LITERAL_EXPR)
286 tree complit = TREE_OPERAND (*expr_p, 1);
287 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (complit);
288 tree decl = DECL_EXPR_DECL (decl_s);
289 tree init = DECL_INITIAL (decl);
291 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
292 into struct T x = { 0, 1, 2 } if the address of the
293 compound literal has never been taken. */
294 if (!TREE_ADDRESSABLE (complit)
295 && !TREE_ADDRESSABLE (decl)
296 && init)
298 *expr_p = copy_node (*expr_p);
299 TREE_OPERAND (*expr_p, 1) = init;
300 return GS_OK;
303 else if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR)
305 tree ctor
306 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
308 if (ctor != TREE_OPERAND (*expr_p, 1))
310 *expr_p = copy_node (*expr_p);
311 TREE_OPERAND (*expr_p, 1) = ctor;
312 return GS_OK;
315 return GS_UNHANDLED;
317 default:
318 return GS_UNHANDLED;