Integrate GUPC into cc1.
[official-gcc.git] / gcc / c-family / c-gimplify.c
blob619fb7369dc45d92c53856ea4f8711ab4948fb7d
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-2013 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 3, 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 COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "c-common.h"
32 #include "basic-block.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "tree-inline.h"
40 #include "diagnostic-core.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43 #include "c-upc-low.h"
44 #include "flags.h"
45 #include "dumpfile.h"
46 #include "c-pretty-print.h"
47 #include "cgraph.h"
48 #include "cilk.h"
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.
54 Language-independent trees are based on the SIMPLE intermediate
55 representation used in the McCAT compiler framework:
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.
65 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
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. */
71 /* Gimplification of statement trees. */
73 /* Convert the tree representation of FNDECL from C frontend trees to
74 GENERIC. */
76 static void
77 c_common_genericize (tree fndecl)
79 FILE *dump_orig;
80 int local_dump_flags;
81 struct cgraph_node *cgn;
83 /* Dump the C-specific tree IR. */
84 dump_orig = dump_begin (TDI_original, &local_dump_flags);
85 if (dump_orig)
87 fprintf (dump_orig, "\n;; Function %s",
88 lang_hooks.decl_printable_name (fndecl, 2));
89 fprintf (dump_orig, " (%s)\n",
90 (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
91 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
92 fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
93 fprintf (dump_orig, "\n");
95 if (local_dump_flags & TDF_RAW)
96 dump_node (DECL_SAVED_TREE (fndecl),
97 TDF_SLIM | local_dump_flags, dump_orig);
98 else
99 print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
100 fprintf (dump_orig, "\n");
102 dump_end (TDI_original, dump_orig);
105 /* Dump all nested functions now. */
106 cgn = cgraph_get_create_node (fndecl);
107 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
108 c_genericize (cgn->decl);
111 void
112 c_genericize (tree fndecl)
114 if (flag_upc)
115 upc_genericize (fndecl);
116 c_common_genericize (fndecl);
119 static void
120 add_block_to_enclosing (tree block)
122 unsigned i;
123 tree enclosing;
124 gimple bind;
125 vec<gimple> stack = gimple_bind_expr_stack ();
127 FOR_EACH_VEC_ELT (stack, i, bind)
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 (location_t loc, 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 (loc);
166 if (decls || block)
168 bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
169 TREE_SIDE_EFFECTS (bind) = 1;
170 SET_EXPR_LOCATION (bind, loc);
172 else
173 bind = body;
175 return bind;
178 /* Gimplification of expression trees. */
180 /* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in
181 gimplify_expr. */
184 c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
185 gimple_seq *post_p ATTRIBUTE_UNUSED)
187 enum tree_code code = TREE_CODE (*expr_p);
189 switch (code)
191 case DECL_EXPR:
192 /* This is handled mostly by gimplify.c, but we have to deal with
193 not warning about int x = x; as it is a GCC extension to turn off
194 this warning but only if warn_init_self is zero. */
195 if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
196 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
197 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
198 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
199 && !warn_init_self)
200 TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
201 break;
203 case PREINCREMENT_EXPR:
204 case PREDECREMENT_EXPR:
205 case POSTINCREMENT_EXPR:
206 case POSTDECREMENT_EXPR:
208 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
209 if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
211 if (TYPE_OVERFLOW_UNDEFINED (type)
212 || ((flag_sanitize & SANITIZE_SI_OVERFLOW)
213 && !TYPE_OVERFLOW_WRAPS (type)))
214 type = unsigned_type_for (type);
215 return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
217 break;
220 case CILK_SPAWN_STMT:
221 gcc_assert
222 (fn_contains_cilk_spawn_p (cfun)
223 && cilk_detect_spawn_and_unwrap (expr_p));
225 /* If errors are seen, then just process it as a CALL_EXPR. */
226 if (!seen_error ())
227 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
229 case MODIFY_EXPR:
230 case INIT_EXPR:
231 case CALL_EXPR:
232 if (fn_contains_cilk_spawn_p (cfun)
233 && cilk_detect_spawn_and_unwrap (expr_p)
234 /* If an error is found, the spawn wrapper is removed and the
235 original expression (MODIFY/INIT/CALL_EXPR) is processes as
236 it is supposed to be. */
237 && !seen_error ())
238 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
240 default:;
243 return GS_UNHANDLED;