]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-gimplify.c
genattrtab.c (write_header): Include hash-set.h...
[thirdparty/gcc.git] / gcc / c-family / 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.
5624e564 5 Copyright (C) 2002-2015 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"
40e23961
MC
30#include "hash-set.h"
31#include "machmode.h"
32#include "vec.h"
33#include "double-int.h"
34#include "input.h"
35#include "alias.h"
36#include "symtab.h"
37#include "wide-int.h"
38#include "inchash.h"
6de9cd9a 39#include "tree.h"
40e23961 40#include "fold-const.h"
6de9cd9a 41#include "c-common.h"
60393bbc
AM
42#include "predict.h"
43#include "vec.h"
44#include "hashtab.h"
45#include "hash-set.h"
46#include "machmode.h"
47#include "hard-reg-set.h"
48#include "input.h"
49#include "function.h"
2fb9a547
AM
50#include "basic-block.h"
51#include "tree-ssa-alias.h"
52#include "internal-fn.h"
53#include "gimple-expr.h"
54#include "is-a.h"
18f429e2 55#include "gimple.h"
45b0be94 56#include "gimplify.h"
6de9cd9a 57#include "tree-inline.h"
1da2ed5f 58#include "diagnostic-core.h"
6de9cd9a
DN
59#include "langhooks.h"
60#include "langhooks-def.h"
61#include "flags.h"
7ee2468b 62#include "dumpfile.h"
6de9cd9a 63#include "c-pretty-print.h"
c582198b
AM
64#include "hash-map.h"
65#include "plugin-api.h"
66#include "ipa-ref.h"
6de9cd9a 67#include "cgraph.h"
12893402 68#include "cilk.h"
0e37a2f3 69#include "c-ubsan.h"
6de9cd9a
DN
70
71/* The gimplification pass converts the language-dependent trees
72 (ld-trees) emitted by the parser into language-independent trees
73 (li-trees) that are the target of SSA analysis and transformations.
74
75 Language-independent trees are based on the SIMPLE intermediate
76 representation used in the McCAT compiler framework:
77
78 "Designing the McCAT Compiler Based on a Family of Structured
79 Intermediate Representations,"
80 L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
81 Proceedings of the 5th International Workshop on Languages and
82 Compilers for Parallel Computing, no. 757 in Lecture Notes in
83 Computer Science, New Haven, Connecticut, pp. 406-420,
84 Springer-Verlag, August 3-5, 1992.
85
86 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
87
88 Basically, we walk down gimplifying the nodes that we encounter. As we
89 walk back up, we check that they fit our constraints, and copy them
90 into temporaries if not. */
91
0e37a2f3
MP
92/* Callback for c_genericize. */
93
94static tree
95ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data)
96{
6e2830c3 97 hash_set<tree> *pset = (hash_set<tree> *) data;
0e37a2f3
MP
98
99 /* Since walk_tree doesn't call the callback function on the decls
100 in BIND_EXPR_VARS, we have to walk them manually. */
101 if (TREE_CODE (*tp) == BIND_EXPR)
102 {
103 for (tree decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
104 {
105 if (TREE_STATIC (decl))
106 {
107 *walk_subtrees = 0;
108 continue;
109 }
110 walk_tree (&DECL_INITIAL (decl), ubsan_walk_array_refs_r, pset,
111 pset);
112 walk_tree (&DECL_SIZE (decl), ubsan_walk_array_refs_r, pset, pset);
113 walk_tree (&DECL_SIZE_UNIT (decl), ubsan_walk_array_refs_r, pset,
114 pset);
115 }
116 }
117 else if (TREE_CODE (*tp) == ADDR_EXPR
118 && TREE_CODE (TREE_OPERAND (*tp, 0)) == ARRAY_REF)
570a11fe
JJ
119 {
120 ubsan_maybe_instrument_array_ref (&TREE_OPERAND (*tp, 0), true);
121 /* Make sure ubsan_maybe_instrument_array_ref is not called again
122 on the ARRAY_REF, the above call might not instrument anything
123 as the index might be constant or masked, so ensure it is not
124 walked again and walk its subtrees manually. */
125 tree aref = TREE_OPERAND (*tp, 0);
126 pset->add (aref);
127 *walk_subtrees = 0;
128 walk_tree (&TREE_OPERAND (aref, 0), ubsan_walk_array_refs_r, pset, pset);
129 walk_tree (&TREE_OPERAND (aref, 1), ubsan_walk_array_refs_r, pset, pset);
130 walk_tree (&TREE_OPERAND (aref, 2), ubsan_walk_array_refs_r, pset, pset);
131 walk_tree (&TREE_OPERAND (aref, 3), ubsan_walk_array_refs_r, pset, pset);
132 }
0e37a2f3
MP
133 else if (TREE_CODE (*tp) == ARRAY_REF)
134 ubsan_maybe_instrument_array_ref (tp, false);
135 return NULL_TREE;
136}
137
6de9cd9a
DN
138/* Gimplification of statement trees. */
139
140/* Convert the tree representation of FNDECL from C frontend trees to
141 GENERIC. */
142
143void
144c_genericize (tree fndecl)
145{
10d22567 146 FILE *dump_orig;
6de9cd9a
DN
147 int local_dump_flags;
148 struct cgraph_node *cgn;
149
0e37a2f3
MP
150 if (flag_sanitize & SANITIZE_BOUNDS)
151 {
6e2830c3
TS
152 hash_set<tree> pset;
153 walk_tree (&DECL_SAVED_TREE (fndecl), ubsan_walk_array_refs_r, &pset,
154 &pset);
0e37a2f3
MP
155 }
156
6de9cd9a 157 /* Dump the C-specific tree IR. */
f8a36c78 158 dump_orig = get_dump_info (TDI_original, &local_dump_flags);
10d22567 159 if (dump_orig)
6de9cd9a 160 {
10d22567 161 fprintf (dump_orig, "\n;; Function %s",
673fda6b 162 lang_hooks.decl_printable_name (fndecl, 2));
10d22567 163 fprintf (dump_orig, " (%s)\n",
fd9fffd1
AO
164 (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
165 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
10d22567
ZD
166 fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
167 fprintf (dump_orig, "\n");
6de9cd9a
DN
168
169 if (local_dump_flags & TDF_RAW)
170 dump_node (DECL_SAVED_TREE (fndecl),
10d22567 171 TDF_SLIM | local_dump_flags, dump_orig);
6de9cd9a 172 else
10d22567
ZD
173 print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
174 fprintf (dump_orig, "\n");
6de9cd9a
DN
175 }
176
a406865a 177 /* Dump all nested functions now. */
d52f5295 178 cgn = cgraph_node::get_create (fndecl);
6de9cd9a 179 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
67348ccc 180 c_genericize (cgn->decl);
6de9cd9a
DN
181}
182
6de9cd9a
DN
183static void
184add_block_to_enclosing (tree block)
185{
726a989a 186 unsigned i;
6de9cd9a 187 tree enclosing;
538dd0b7
DM
188 gbind *bind;
189 vec<gbind *> stack = gimple_bind_expr_stack ();
6de9cd9a 190
9771b263 191 FOR_EACH_VEC_ELT (stack, i, bind)
726a989a 192 if (gimple_bind_block (bind))
6de9cd9a
DN
193 break;
194
726a989a 195 enclosing = gimple_bind_block (bind);
6de9cd9a
DN
196 BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
197}
198
199/* Genericize a scope by creating a new BIND_EXPR.
200 BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
201 In the latter case, we need to create a new BLOCK and add it to the
202 BLOCK_SUBBLOCKS of the enclosing block.
203 BODY is a chain of C _STMT nodes for the contents of the scope, to be
204 genericized. */
205
325c3691 206tree
c2255bc4 207c_build_bind_expr (location_t loc, tree block, tree body)
6de9cd9a
DN
208{
209 tree decls, bind;
210
211 if (block == NULL_TREE)
212 decls = NULL_TREE;
213 else if (TREE_CODE (block) == BLOCK)
214 decls = BLOCK_VARS (block);
215 else
216 {
217 decls = block;
218 if (DECL_ARTIFICIAL (decls))
219 block = NULL_TREE;
220 else
221 {
222 block = make_node (BLOCK);
223 BLOCK_VARS (block) = decls;
224 add_block_to_enclosing (block);
225 }
226 }
227
228 if (!body)
c2255bc4 229 body = build_empty_stmt (loc);
325c3691 230 if (decls || block)
6de9cd9a 231 {
53fb4de3 232 bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
325c3691 233 TREE_SIDE_EFFECTS (bind) = 1;
c2255bc4 234 SET_EXPR_LOCATION (bind, loc);
6de9cd9a
DN
235 }
236 else
325c3691 237 bind = body;
6de9cd9a 238
325c3691 239 return bind;
6de9cd9a
DN
240}
241
6de9cd9a
DN
242/* Gimplification of expression trees. */
243
726a989a
RB
244/* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in
245 gimplify_expr. */
6de9cd9a
DN
246
247int
2ec5deb5 248c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
726a989a 249 gimple_seq *post_p ATTRIBUTE_UNUSED)
6de9cd9a
DN
250{
251 enum tree_code code = TREE_CODE (*expr_p);
252
cc3c4f62
RB
253 switch (code)
254 {
541e35a6
MP
255 case LSHIFT_EXPR:
256 case RSHIFT_EXPR:
257 {
258 /* We used to convert the right operand of a shift-expression
259 to an integer_type_node in the FEs. But it is unnecessary
260 and not desirable for diagnostics and sanitizers. We keep
261 this here to not pessimize the code, but we convert to an
262 unsigned type, because negative shift counts are undefined
263 anyway.
264 We should get rid of this conversion when we have a proper
265 type demotion/promotion pass. */
266 tree *op1_p = &TREE_OPERAND (*expr_p, 1);
267 if (TREE_CODE (TREE_TYPE (*op1_p)) != VECTOR_TYPE
7fb66c15
MP
268 && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
269 unsigned_type_node)
270 && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
271 integer_type_node))
541e35a6
MP
272 *op1_p = convert (unsigned_type_node, *op1_p);
273 break;
274 }
275
cc3c4f62
RB
276 case DECL_EXPR:
277 /* This is handled mostly by gimplify.c, but we have to deal with
278 not warning about int x = x; as it is a GCC extension to turn off
279 this warning but only if warn_init_self is zero. */
280 if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
281 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
282 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
283 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
284 && !warn_init_self)
285 TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
286 break;
287
288 case PREINCREMENT_EXPR:
289 case PREDECREMENT_EXPR:
290 case POSTINCREMENT_EXPR:
291 case POSTDECREMENT_EXPR:
292 {
293 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
294 if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
295 {
63dfbb95 296 if (!TYPE_OVERFLOW_WRAPS (type))
cc3c4f62
RB
297 type = unsigned_type_for (type);
298 return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
299 }
300 break;
301 }
0e37a2f3 302
12893402 303 case CILK_SPAWN_STMT:
0e37a2f3
MP
304 gcc_assert
305 (fn_contains_cilk_spawn_p (cfun)
12893402 306 && cilk_detect_spawn_and_unwrap (expr_p));
0e37a2f3 307
12893402
BI
308 /* If errors are seen, then just process it as a CALL_EXPR. */
309 if (!seen_error ())
310 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
0e37a2f3 311
12893402
BI
312 case MODIFY_EXPR:
313 case INIT_EXPR:
314 case CALL_EXPR:
315 if (fn_contains_cilk_spawn_p (cfun)
316 && cilk_detect_spawn_and_unwrap (expr_p)
317 /* If an error is found, the spawn wrapper is removed and the
318 original expression (MODIFY/INIT/CALL_EXPR) is processes as
319 it is supposed to be. */
320 && !seen_error ())
321 return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
cc3c4f62
RB
322
323 default:;
324 }
2ec5deb5
PB
325
326 return GS_UNHANDLED;
6de9cd9a 327}