2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c-family / c-gimplify.c
blobb646a3bccaac288cd927676007505be190d1a4a2
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-2015 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 "alias.h"
31 #include "symtab.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "c-common.h"
35 #include "predict.h"
36 #include "hard-reg-set.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "gimple.h"
43 #include "gimplify.h"
44 #include "tree-inline.h"
45 #include "diagnostic-core.h"
46 #include "langhooks.h"
47 #include "langhooks-def.h"
48 #include "flags.h"
49 #include "dumpfile.h"
50 #include "c-pretty-print.h"
51 #include "plugin-api.h"
52 #include "ipa-ref.h"
53 #include "cgraph.h"
54 #include "cilk.h"
55 #include "c-ubsan.h"
57 /* The gimplification pass converts the language-dependent trees
58 (ld-trees) emitted by the parser into language-independent trees
59 (li-trees) that are the target of SSA analysis and transformations.
61 Language-independent trees are based on the SIMPLE intermediate
62 representation used in the McCAT compiler framework:
64 "Designing the McCAT Compiler Based on a Family of Structured
65 Intermediate Representations,"
66 L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
67 Proceedings of the 5th International Workshop on Languages and
68 Compilers for Parallel Computing, no. 757 in Lecture Notes in
69 Computer Science, New Haven, Connecticut, pp. 406-420,
70 Springer-Verlag, August 3-5, 1992.
72 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
74 Basically, we walk down gimplifying the nodes that we encounter. As we
75 walk back up, we check that they fit our constraints, and copy them
76 into temporaries if not. */
78 /* Callback for c_genericize. */
80 static tree
81 ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data)
83 hash_set<tree> *pset = (hash_set<tree> *) data;
85 /* Since walk_tree doesn't call the callback function on the decls
86 in BIND_EXPR_VARS, we have to walk them manually. */
87 if (TREE_CODE (*tp) == BIND_EXPR)
89 for (tree decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
91 if (TREE_STATIC (decl))
93 *walk_subtrees = 0;
94 continue;
96 walk_tree (&DECL_INITIAL (decl), ubsan_walk_array_refs_r, pset,
97 pset);
98 walk_tree (&DECL_SIZE (decl), ubsan_walk_array_refs_r, pset, pset);
99 walk_tree (&DECL_SIZE_UNIT (decl), ubsan_walk_array_refs_r, pset,
100 pset);
103 else if (TREE_CODE (*tp) == ADDR_EXPR
104 && TREE_CODE (TREE_OPERAND (*tp, 0)) == ARRAY_REF)
106 ubsan_maybe_instrument_array_ref (&TREE_OPERAND (*tp, 0), true);
107 /* Make sure ubsan_maybe_instrument_array_ref is not called again
108 on the ARRAY_REF, the above call might not instrument anything
109 as the index might be constant or masked, so ensure it is not
110 walked again and walk its subtrees manually. */
111 tree aref = TREE_OPERAND (*tp, 0);
112 pset->add (aref);
113 *walk_subtrees = 0;
114 walk_tree (&TREE_OPERAND (aref, 0), ubsan_walk_array_refs_r, pset, pset);
115 walk_tree (&TREE_OPERAND (aref, 1), ubsan_walk_array_refs_r, pset, pset);
116 walk_tree (&TREE_OPERAND (aref, 2), ubsan_walk_array_refs_r, pset, pset);
117 walk_tree (&TREE_OPERAND (aref, 3), ubsan_walk_array_refs_r, pset, pset);
119 else if (TREE_CODE (*tp) == ARRAY_REF)
120 ubsan_maybe_instrument_array_ref (tp, false);
121 return NULL_TREE;
124 /* Gimplification of statement trees. */
126 /* Convert the tree representation of FNDECL from C frontend trees to
127 GENERIC. */
129 void
130 c_genericize (tree fndecl)
132 FILE *dump_orig;
133 int local_dump_flags;
134 struct cgraph_node *cgn;
136 if (flag_sanitize & SANITIZE_BOUNDS)
138 hash_set<tree> pset;
139 walk_tree (&DECL_SAVED_TREE (fndecl), ubsan_walk_array_refs_r, &pset,
140 &pset);
143 /* Dump the C-specific tree IR. */
144 dump_orig = get_dump_info (TDI_original, &local_dump_flags);
145 if (dump_orig)
147 fprintf (dump_orig, "\n;; Function %s",
148 lang_hooks.decl_printable_name (fndecl, 2));
149 fprintf (dump_orig, " (%s)\n",
150 (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
151 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
152 fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
153 fprintf (dump_orig, "\n");
155 if (local_dump_flags & TDF_RAW)
156 dump_node (DECL_SAVED_TREE (fndecl),
157 TDF_SLIM | local_dump_flags, dump_orig);
158 else
159 print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
160 fprintf (dump_orig, "\n");
163 /* Dump all nested functions now. */
164 cgn = cgraph_node::get_create (fndecl);
165 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
166 c_genericize (cgn->decl);
169 static void
170 add_block_to_enclosing (tree block)
172 unsigned i;
173 tree enclosing;
174 gbind *bind;
175 vec<gbind *> stack = gimple_bind_expr_stack ();
177 FOR_EACH_VEC_ELT (stack, i, bind)
178 if (gimple_bind_block (bind))
179 break;
181 enclosing = gimple_bind_block (bind);
182 BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
185 /* Genericize a scope by creating a new BIND_EXPR.
186 BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
187 In the latter case, we need to create a new BLOCK and add it to the
188 BLOCK_SUBBLOCKS of the enclosing block.
189 BODY is a chain of C _STMT nodes for the contents of the scope, to be
190 genericized. */
192 tree
193 c_build_bind_expr (location_t loc, tree block, tree body)
195 tree decls, bind;
197 if (block == NULL_TREE)
198 decls = NULL_TREE;
199 else if (TREE_CODE (block) == BLOCK)
200 decls = BLOCK_VARS (block);
201 else
203 decls = block;
204 if (DECL_ARTIFICIAL (decls))
205 block = NULL_TREE;
206 else
208 block = make_node (BLOCK);
209 BLOCK_VARS (block) = decls;
210 add_block_to_enclosing (block);
214 if (!body)
215 body = build_empty_stmt (loc);
216 if (decls || block)
218 bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
219 TREE_SIDE_EFFECTS (bind) = 1;
220 SET_EXPR_LOCATION (bind, loc);
222 else
223 bind = body;
225 return bind;
228 /* Gimplification of expression trees. */
230 /* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in
231 gimplify_expr. */
234 c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
235 gimple_seq *post_p ATTRIBUTE_UNUSED)
237 enum tree_code code = TREE_CODE (*expr_p);
239 switch (code)
241 case LSHIFT_EXPR:
242 case RSHIFT_EXPR:
244 /* We used to convert the right operand of a shift-expression
245 to an integer_type_node in the FEs. But it is unnecessary
246 and not desirable for diagnostics and sanitizers. We keep
247 this here to not pessimize the code, but we convert to an
248 unsigned type, because negative shift counts are undefined
249 anyway.
250 We should get rid of this conversion when we have a proper
251 type demotion/promotion pass. */
252 tree *op1_p = &TREE_OPERAND (*expr_p, 1);
253 if (TREE_CODE (TREE_TYPE (*op1_p)) != VECTOR_TYPE
254 && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
255 unsigned_type_node)
256 && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
257 integer_type_node))
258 *op1_p = convert (unsigned_type_node, *op1_p);
259 break;
262 case DECL_EXPR:
263 /* This is handled mostly by gimplify.c, but we have to deal with
264 not warning about int x = x; as it is a GCC extension to turn off
265 this warning but only if warn_init_self is zero. */
266 if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
267 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
268 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
269 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
270 && !warn_init_self)
271 TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
272 break;
274 case PREINCREMENT_EXPR:
275 case PREDECREMENT_EXPR:
276 case POSTINCREMENT_EXPR:
277 case POSTDECREMENT_EXPR:
279 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
280 if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
282 if (!TYPE_OVERFLOW_WRAPS (type))
283 type = unsigned_type_for (type);
284 return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
286 break;
289 case CILK_SPAWN_STMT:
290 gcc_assert
291 (fn_contains_cilk_spawn_p (cfun)
292 && cilk_detect_spawn_and_unwrap (expr_p));
294 /* If errors are seen, then just process it as a CALL_EXPR. */
295 if (!seen_error ())
296 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
298 case MODIFY_EXPR:
299 case INIT_EXPR:
300 case CALL_EXPR:
301 if (fn_contains_cilk_spawn_p (cfun)
302 && cilk_detect_spawn_and_unwrap (expr_p)
303 /* If an error is found, the spawn wrapper is removed and the
304 original expression (MODIFY/INIT/CALL_EXPR) is processes as
305 it is supposed to be. */
306 && !seen_error ())
307 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
309 default:;
312 return GS_UNHANDLED;