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