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