Branch data Line data Source code
1 : : /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 : : tree representation into the GIMPLE form.
3 : : Copyright (C) 2002-2025 Free Software Foundation, Inc.
4 : : Major work done by Sebastian Pop <s.pop@laposte.net>,
5 : : Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
6 : :
7 : : This file is part of GCC.
8 : :
9 : : GCC is free software; you can redistribute it and/or modify it under
10 : : the terms of the GNU General Public License as published by the Free
11 : : Software Foundation; either version 3, or (at your option) any later
12 : : version.
13 : :
14 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : : for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GCC; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "backend.h"
27 : : #include "target.h"
28 : : #include "rtl.h"
29 : : #include "tree.h"
30 : : #include "memmodel.h"
31 : : #include "tm_p.h"
32 : : #include "gimple.h"
33 : : #include "gimple-predict.h"
34 : : #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 : : #include "ssa.h"
36 : : #include "cgraph.h"
37 : : #include "tree-pretty-print.h"
38 : : #include "diagnostic-core.h"
39 : : #include "diagnostic.h" /* For errorcount. */
40 : : #include "alias.h"
41 : : #include "fold-const.h"
42 : : #include "calls.h"
43 : : #include "varasm.h"
44 : : #include "stmt.h"
45 : : #include "expr.h"
46 : : #include "gimple-iterator.h"
47 : : #include "gimple-fold.h"
48 : : #include "tree-eh.h"
49 : : #include "gimplify.h"
50 : : #include "stor-layout.h"
51 : : #include "print-tree.h"
52 : : #include "tree-iterator.h"
53 : : #include "tree-inline.h"
54 : : #include "langhooks.h"
55 : : #include "tree-cfg.h"
56 : : #include "tree-ssa.h"
57 : : #include "tree-hash-traits.h"
58 : : #include "omp-general.h"
59 : : #include "omp-low.h"
60 : : #include "gimple-low.h"
61 : : #include "gomp-constants.h"
62 : : #include "splay-tree.h"
63 : : #include "gimple-walk.h"
64 : : #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
65 : : #include "builtins.h"
66 : : #include "stringpool.h"
67 : : #include "attribs.h"
68 : : #include "asan.h"
69 : : #include "dbgcnt.h"
70 : : #include "omp-offload.h"
71 : : #include "context.h"
72 : : #include "tree-nested.h"
73 : : #include "gcc-urlifier.h"
74 : : #include "insn-config.h"
75 : : #include "recog.h"
76 : : #include "output.h"
77 : : #include "gimplify_reg_info.h"
78 : :
79 : : /* Identifier for a basic condition, mapping it to other basic conditions of
80 : : its Boolean expression. Basic conditions given the same uid (in the same
81 : : function) are parts of the same ANDIF/ORIF expression. Used for condition
82 : : coverage. */
83 : : static unsigned nextconduid = 1;
84 : :
85 : : /* Annotated gconds so that basic conditions in the same expression map to
86 : : the same uid. This is used for condition coverage. */
87 : : static hash_map <tree, unsigned> *cond_uids;
88 : :
89 : : /* Get a fresh identifier for a new condition expression. This is used for
90 : : condition coverage. */
91 : : static unsigned
92 : 5734169 : next_cond_uid ()
93 : : {
94 : 5734169 : return nextconduid++;
95 : : }
96 : :
97 : : /* Reset the condition uid to the value it should have when compiling a new
98 : : function. 0 is already the default/untouched value, so start at non-zero.
99 : : A valid and set id should always be > 0. This is used for condition
100 : : coverage. */
101 : : static void
102 : 2923403 : reset_cond_uid ()
103 : : {
104 : 2923403 : nextconduid = 1;
105 : 0 : }
106 : :
107 : : /* Associate the condition STMT with the discriminator UID. STMTs that are
108 : : broken down with ANDIF/ORIF from the same Boolean expression should be given
109 : : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
110 : : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
111 : : for condition coverage. */
112 : : static void
113 : 1220195 : tree_associate_condition_with_expr (tree stmt, unsigned uid)
114 : : {
115 : 1220195 : if (!condition_coverage_flag)
116 : : return;
117 : :
118 : 527 : if (!cond_uids)
119 : 65 : cond_uids = new hash_map <tree, unsigned> ();
120 : :
121 : 527 : cond_uids->put (stmt, uid);
122 : : }
123 : :
124 : : /* Hash set of poisoned variables in a bind expr. */
125 : : static hash_set<tree> *asan_poisoned_variables = NULL;
126 : :
127 : : /* Hash set of already-resolved calls to OpenMP "declare variant"
128 : : functions. A call can resolve to the original function and
129 : : we don't want to repeat the resolution multiple times. */
130 : : static hash_set<tree> *omp_resolved_variant_calls = NULL;
131 : :
132 : : enum gimplify_omp_var_data
133 : : {
134 : : GOVD_SEEN = 0x000001,
135 : : GOVD_EXPLICIT = 0x000002,
136 : : GOVD_SHARED = 0x000004,
137 : : GOVD_PRIVATE = 0x000008,
138 : : GOVD_FIRSTPRIVATE = 0x000010,
139 : : GOVD_LASTPRIVATE = 0x000020,
140 : : GOVD_REDUCTION = 0x000040,
141 : : GOVD_LOCAL = 0x00080,
142 : : GOVD_MAP = 0x000100,
143 : : GOVD_DEBUG_PRIVATE = 0x000200,
144 : : GOVD_PRIVATE_OUTER_REF = 0x000400,
145 : : GOVD_LINEAR = 0x000800,
146 : : GOVD_ALIGNED = 0x001000,
147 : :
148 : : /* Flag for GOVD_MAP: don't copy back. */
149 : : GOVD_MAP_TO_ONLY = 0x002000,
150 : :
151 : : /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
152 : : GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
153 : :
154 : : GOVD_MAP_0LEN_ARRAY = 0x008000,
155 : :
156 : : /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
157 : : GOVD_MAP_ALWAYS_TO = 0x010000,
158 : :
159 : : /* Flag for shared vars that are or might be stored to in the region. */
160 : : GOVD_WRITTEN = 0x020000,
161 : :
162 : : /* Flag for GOVD_MAP, if it is a forced mapping. */
163 : : GOVD_MAP_FORCE = 0x040000,
164 : :
165 : : /* Flag for GOVD_MAP: must be present already. */
166 : : GOVD_MAP_FORCE_PRESENT = 0x080000,
167 : :
168 : : /* Flag for GOVD_MAP: only allocate. */
169 : : GOVD_MAP_ALLOC_ONLY = 0x100000,
170 : :
171 : : /* Flag for GOVD_MAP: only copy back. */
172 : : GOVD_MAP_FROM_ONLY = 0x200000,
173 : :
174 : : GOVD_NONTEMPORAL = 0x400000,
175 : :
176 : : /* Flag for GOVD_LASTPRIVATE: conditional modifier. */
177 : : GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
178 : :
179 : : GOVD_CONDTEMP = 0x1000000,
180 : :
181 : : /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause. */
182 : : GOVD_REDUCTION_INSCAN = 0x2000000,
183 : :
184 : : /* Flag for GOVD_FIRSTPRIVATE: OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT. */
185 : : GOVD_FIRSTPRIVATE_IMPLICIT = 0x4000000,
186 : :
187 : : GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
188 : : | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
189 : : | GOVD_LOCAL)
190 : : };
191 : :
192 : :
193 : : enum omp_region_type
194 : : {
195 : : ORT_WORKSHARE = 0x00,
196 : : ORT_TASKGROUP = 0x01,
197 : : ORT_DISPATCH = 0x02,
198 : : ORT_SIMD = 0x04,
199 : :
200 : : ORT_PARALLEL = 0x08,
201 : : ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
202 : :
203 : : ORT_TASK = 0x10,
204 : : ORT_UNTIED_TASK = ORT_TASK | 1,
205 : : ORT_TASKLOOP = ORT_TASK | 2,
206 : : ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
207 : :
208 : : ORT_TEAMS = 0x20,
209 : : ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
210 : : ORT_HOST_TEAMS = ORT_TEAMS | 2,
211 : : ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
212 : :
213 : : /* Data region. */
214 : : ORT_TARGET_DATA = 0x40,
215 : :
216 : : /* Data region with offloading. */
217 : : ORT_TARGET = 0x80,
218 : : ORT_COMBINED_TARGET = ORT_TARGET | 1,
219 : : ORT_IMPLICIT_TARGET = ORT_TARGET | 2,
220 : :
221 : : /* OpenACC variants. */
222 : : ORT_ACC = 0x100, /* A generic OpenACC region. */
223 : : ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
224 : : ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
225 : : ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
226 : : ORT_ACC_SERIAL = ORT_ACC | ORT_TARGET | 4, /* Serial construct. */
227 : : ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
228 : :
229 : : /* Dummy OpenMP region, used to disable expansion of
230 : : DECL_VALUE_EXPRs in taskloop pre body. */
231 : : ORT_NONE = 0x200
232 : : };
233 : :
234 : : /* Gimplify hashtable helper. */
235 : :
236 : : struct gimplify_hasher : free_ptr_hash <elt_t>
237 : : {
238 : : static inline hashval_t hash (const elt_t *);
239 : : static inline bool equal (const elt_t *, const elt_t *);
240 : : };
241 : :
242 : : struct gimplify_ctx
243 : : {
244 : : struct gimplify_ctx *prev_context;
245 : :
246 : : vec<gbind *> bind_expr_stack;
247 : : tree temps;
248 : : gimple_seq conditional_cleanups;
249 : : tree exit_label;
250 : : tree return_temp;
251 : :
252 : : vec<tree> case_labels;
253 : : hash_set<tree> *live_switch_vars;
254 : : /* The formal temporary table. Should this be persistent? */
255 : : hash_table<gimplify_hasher> *temp_htab;
256 : :
257 : : int conditions;
258 : : unsigned into_ssa : 1;
259 : : unsigned allow_rhs_cond_expr : 1;
260 : : unsigned in_cleanup_point_expr : 1;
261 : : unsigned keep_stack : 1;
262 : : unsigned save_stack : 1;
263 : : unsigned in_switch_expr : 1;
264 : : unsigned in_handler_expr : 1;
265 : : };
266 : :
267 : : enum gimplify_defaultmap_kind
268 : : {
269 : : GDMK_SCALAR,
270 : : GDMK_SCALAR_TARGET, /* w/ Fortran's target attr, implicit mapping, only. */
271 : : GDMK_AGGREGATE,
272 : : GDMK_ALLOCATABLE,
273 : : GDMK_POINTER
274 : : };
275 : :
276 : : struct gimplify_omp_ctx
277 : : {
278 : : struct gimplify_omp_ctx *outer_context;
279 : : splay_tree variables;
280 : : hash_map<omp_name_type<tree>, tree> *implicit_mappers;
281 : : hash_set<tree> *privatized_types;
282 : : tree clauses;
283 : : /* Iteration variables in an OMP_FOR. */
284 : : vec<tree> loop_iter_var;
285 : : location_t location;
286 : : enum omp_clause_default_kind default_kind;
287 : : enum omp_region_type region_type;
288 : : enum tree_code code;
289 : : bool combined_loop;
290 : : bool distribute;
291 : : bool target_firstprivatize_array_bases;
292 : : bool add_safelen1;
293 : : bool order_concurrent;
294 : : bool has_depend;
295 : : bool in_for_exprs;
296 : : bool in_call_args;
297 : : int defaultmap[5];
298 : : };
299 : :
300 : : static struct gimplify_ctx *gimplify_ctxp;
301 : : static struct gimplify_omp_ctx *gimplify_omp_ctxp;
302 : : static bool in_omp_construct;
303 : :
304 : : /* Forward declaration. */
305 : : static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
306 : : static hash_map<tree, tree> *oacc_declare_returns;
307 : : static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
308 : : bool (*) (tree), fallback_t, bool);
309 : : static void prepare_gimple_addressable (tree *, gimple_seq *);
310 : :
311 : : /* Shorter alias name for the above function for use in gimplify.cc
312 : : only. */
313 : :
314 : : static inline void
315 : 91823686 : gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
316 : : {
317 : 91823686 : gimple_seq_add_stmt_without_update (seq_p, gs);
318 : 6612927 : }
319 : :
320 : : /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
321 : : NULL, a new sequence is allocated. This function is
322 : : similar to gimple_seq_add_seq, but does not scan the operands.
323 : : During gimplification, we need to manipulate statement sequences
324 : : before the def/use vectors have been constructed. */
325 : :
326 : : static void
327 : 9471715 : gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
328 : : {
329 : 9471715 : gimple_stmt_iterator si;
330 : :
331 : 9471715 : if (src == NULL)
332 : 3993087 : return;
333 : :
334 : 5478628 : si = gsi_last (*dst_p);
335 : 5478628 : gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
336 : : }
337 : :
338 : :
339 : : /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
340 : : and popping gimplify contexts. */
341 : :
342 : : static struct gimplify_ctx *ctx_pool = NULL;
343 : :
344 : : /* Return a gimplify context struct from the pool. */
345 : :
346 : : static inline struct gimplify_ctx *
347 : 8956112 : ctx_alloc (void)
348 : : {
349 : 8956112 : struct gimplify_ctx * c = ctx_pool;
350 : :
351 : 8956112 : if (c)
352 : 8693796 : ctx_pool = c->prev_context;
353 : : else
354 : 262316 : c = XNEW (struct gimplify_ctx);
355 : :
356 : 8956112 : memset (c, '\0', sizeof (*c));
357 : 8956112 : return c;
358 : : }
359 : :
360 : : /* Put gimplify context C back into the pool. */
361 : :
362 : : static inline void
363 : 8956108 : ctx_free (struct gimplify_ctx *c)
364 : : {
365 : 8956108 : c->prev_context = ctx_pool;
366 : 8956108 : ctx_pool = c;
367 : : }
368 : :
369 : : /* Free allocated ctx stack memory. */
370 : :
371 : : void
372 : 230654 : free_gimplify_stack (void)
373 : : {
374 : 230654 : struct gimplify_ctx *c;
375 : :
376 : 462153 : while ((c = ctx_pool))
377 : : {
378 : 231499 : ctx_pool = c->prev_context;
379 : 231499 : free (c);
380 : : }
381 : 230654 : }
382 : :
383 : :
384 : : /* Set up a context for the gimplifier. */
385 : :
386 : : void
387 : 8956112 : push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
388 : : {
389 : 8956112 : struct gimplify_ctx *c = ctx_alloc ();
390 : :
391 : 8956112 : c->prev_context = gimplify_ctxp;
392 : 8956112 : gimplify_ctxp = c;
393 : 8956112 : gimplify_ctxp->into_ssa = in_ssa;
394 : 8956112 : gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
395 : 8956112 : }
396 : :
397 : : /* Tear down a context for the gimplifier. If BODY is non-null, then
398 : : put the temporaries into the outer BIND_EXPR. Otherwise, put them
399 : : in the local_decls.
400 : :
401 : : BODY is not a sequence, but the first tuple in a sequence. */
402 : :
403 : : void
404 : 8956108 : pop_gimplify_context (gimple *body)
405 : : {
406 : 8956108 : struct gimplify_ctx *c = gimplify_ctxp;
407 : :
408 : 8956108 : gcc_assert (c
409 : : && (!c->bind_expr_stack.exists ()
410 : : || c->bind_expr_stack.is_empty ()));
411 : 8956108 : c->bind_expr_stack.release ();
412 : 8956108 : gimplify_ctxp = c->prev_context;
413 : :
414 : 8956108 : if (body)
415 : 3047155 : declare_vars (c->temps, body, false);
416 : : else
417 : 5908953 : record_vars (c->temps);
418 : :
419 : 8956108 : delete c->temp_htab;
420 : 8956108 : c->temp_htab = NULL;
421 : 8956108 : ctx_free (c);
422 : 8956108 : }
423 : :
424 : : /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
425 : :
426 : : static void
427 : 5959795 : gimple_push_bind_expr (gbind *bind_stmt)
428 : : {
429 : 5959795 : gimplify_ctxp->bind_expr_stack.reserve (8);
430 : 5959795 : gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
431 : 5959795 : }
432 : :
433 : : /* Pop the first element off the stack of bindings. */
434 : :
435 : : static void
436 : 5959795 : gimple_pop_bind_expr (void)
437 : : {
438 : 0 : gimplify_ctxp->bind_expr_stack.pop ();
439 : 1261 : }
440 : :
441 : : /* Return the first element of the stack of bindings. */
442 : :
443 : : gbind *
444 : 0 : gimple_current_bind_expr (void)
445 : : {
446 : 0 : return gimplify_ctxp->bind_expr_stack.last ();
447 : : }
448 : :
449 : : /* Return the stack of bindings created during gimplification. */
450 : :
451 : : vec<gbind *>
452 : 331 : gimple_bind_expr_stack (void)
453 : : {
454 : 331 : return gimplify_ctxp->bind_expr_stack;
455 : : }
456 : :
457 : : /* Return true iff there is a COND_EXPR between us and the innermost
458 : : CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
459 : :
460 : : static bool
461 : 2844840 : gimple_conditional_context (void)
462 : : {
463 : 2844840 : return gimplify_ctxp->conditions > 0;
464 : : }
465 : :
466 : : /* Note that we've entered a COND_EXPR. */
467 : :
468 : : static void
469 : 5734604 : gimple_push_condition (void)
470 : : {
471 : : #ifdef ENABLE_GIMPLE_CHECKING
472 : 5734604 : if (gimplify_ctxp->conditions == 0)
473 : 3766860 : gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
474 : : #endif
475 : 5734604 : ++(gimplify_ctxp->conditions);
476 : 5734604 : }
477 : :
478 : : /* Note that we've left a COND_EXPR. If we're back at unconditional scope
479 : : now, add any conditional cleanups we've seen to the prequeue. */
480 : :
481 : : static void
482 : 5734604 : gimple_pop_condition (gimple_seq *pre_p)
483 : : {
484 : 5734604 : int conds = --(gimplify_ctxp->conditions);
485 : :
486 : 5734604 : gcc_assert (conds >= 0);
487 : 5734604 : if (conds == 0)
488 : : {
489 : 3766860 : gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
490 : 3766860 : gimplify_ctxp->conditional_cleanups = NULL;
491 : : }
492 : 5734604 : }
493 : :
494 : : /* A stable comparison routine for use with splay trees and DECLs. */
495 : :
496 : : static int
497 : 17904139 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
498 : : {
499 : 17904139 : tree a = (tree) xa;
500 : 17904139 : tree b = (tree) xb;
501 : :
502 : 17904139 : return DECL_UID (a) - DECL_UID (b);
503 : : }
504 : :
505 : : /* Create a new omp construct that deals with variable remapping. */
506 : :
507 : : static struct gimplify_omp_ctx *
508 : 139258 : new_omp_context (enum omp_region_type region_type)
509 : : {
510 : 139258 : struct gimplify_omp_ctx *c;
511 : :
512 : 139258 : c = XCNEW (struct gimplify_omp_ctx);
513 : 139258 : c->outer_context = gimplify_omp_ctxp;
514 : 139258 : c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
515 : 139258 : c->implicit_mappers = new hash_map<omp_name_type<tree>, tree>;
516 : 139258 : c->privatized_types = new hash_set<tree>;
517 : 139258 : c->location = input_location;
518 : 139258 : c->region_type = region_type;
519 : 139258 : if ((region_type & ORT_TASK) == 0)
520 : 133058 : c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
521 : : else
522 : 6200 : c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
523 : 139258 : c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
524 : 139258 : c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
525 : 139258 : c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
526 : 139258 : c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
527 : 139258 : c->defaultmap[GDMK_POINTER] = GOVD_MAP;
528 : :
529 : 139258 : return c;
530 : : }
531 : :
532 : : /* Destroy an omp construct that deals with variable remapping. */
533 : :
534 : : static void
535 : 138699 : delete_omp_context (struct gimplify_omp_ctx *c)
536 : : {
537 : 138699 : splay_tree_delete (c->variables);
538 : 277398 : delete c->privatized_types;
539 : 277398 : delete c->implicit_mappers;
540 : 138699 : c->loop_iter_var.release ();
541 : 138699 : XDELETE (c);
542 : 138699 : }
543 : :
544 : : static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
545 : : static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
546 : :
547 : : /* Both gimplify the statement T and append it to *SEQ_P. This function
548 : : behaves exactly as gimplify_stmt, but you don't have to pass T as a
549 : : reference. */
550 : :
551 : : void
552 : 37724809 : gimplify_and_add (tree t, gimple_seq *seq_p)
553 : : {
554 : 37724809 : gimplify_stmt (&t, seq_p);
555 : 37724809 : }
556 : :
557 : : /* Gimplify statement T into sequence *SEQ_P, and return the first
558 : : tuple in the sequence of generated tuples for this statement.
559 : : Return NULL if gimplifying T produced no tuples. */
560 : :
561 : : static gimple *
562 : 104657 : gimplify_and_return_first (tree t, gimple_seq *seq_p)
563 : : {
564 : 104657 : gimple_stmt_iterator last = gsi_last (*seq_p);
565 : :
566 : 104657 : gimplify_and_add (t, seq_p);
567 : :
568 : 104657 : if (!gsi_end_p (last))
569 : : {
570 : 4871 : gsi_next (&last);
571 : 4871 : return gsi_stmt (last);
572 : : }
573 : : else
574 : 99786 : return gimple_seq_first_stmt (*seq_p);
575 : : }
576 : :
577 : : /* Returns true iff T is a valid RHS for an assignment to an un-renamed
578 : : LHS, or for a call argument. */
579 : :
580 : : static bool
581 : 235493 : is_gimple_mem_rhs (tree t)
582 : : {
583 : : /* If we're dealing with a renamable type, either source or dest must be
584 : : a renamed variable. */
585 : 235493 : if (is_gimple_reg_type (TREE_TYPE (t)))
586 : 232141 : return is_gimple_val (t);
587 : : else
588 : 3352 : return is_gimple_val (t) || is_gimple_lvalue (t);
589 : : }
590 : :
591 : : /* Return true if T is a CALL_EXPR or an expression that can be
592 : : assigned to a temporary. Note that this predicate should only be
593 : : used during gimplification. See the rationale for this in
594 : : gimplify_modify_expr. */
595 : :
596 : : static bool
597 : 96630249 : is_gimple_reg_rhs_or_call (tree t)
598 : : {
599 : 70898820 : return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
600 : 96630249 : || TREE_CODE (t) == CALL_EXPR);
601 : : }
602 : :
603 : : /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
604 : : this predicate should only be used during gimplification. See the
605 : : rationale for this in gimplify_modify_expr. */
606 : :
607 : : static bool
608 : 14184064 : is_gimple_mem_rhs_or_call (tree t)
609 : : {
610 : : /* If we're dealing with a renamable type, either source or dest must be
611 : : a renamed variable. */
612 : 14184064 : if (is_gimple_reg_type (TREE_TYPE (t)))
613 : 10834477 : return is_gimple_val (t);
614 : : else
615 : 3349587 : return (is_gimple_val (t)
616 : 1637503 : || is_gimple_lvalue (t)
617 : 1108168 : || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
618 : 4457275 : || TREE_CODE (t) == CALL_EXPR);
619 : : }
620 : :
621 : : /* Create a temporary with a name derived from VAL. Subroutine of
622 : : lookup_tmp_var; nobody else should call this function. */
623 : :
624 : : static inline tree
625 : 2124444 : create_tmp_from_val (tree val)
626 : : {
627 : : /* Drop all qualifiers and address-space information from the value type. */
628 : 2124444 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
629 : 2124444 : tree var = create_tmp_var (type, get_name (val));
630 : 2124444 : return var;
631 : : }
632 : :
633 : : /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
634 : : an existing expression temporary. If NOT_GIMPLE_REG, mark it as such. */
635 : :
636 : : static tree
637 : 2353646 : lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
638 : : {
639 : 2353646 : tree ret;
640 : :
641 : : /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P. */
642 : 2353646 : gcc_assert (!is_formal || !not_gimple_reg);
643 : :
644 : : /* If not optimizing, never really reuse a temporary. local-alloc
645 : : won't allocate any variable that is used in more than one basic
646 : : block, which means it will go into memory, causing much extra
647 : : work in reload and final and poorer code generation, outweighing
648 : : the extra memory allocation here. */
649 : 2353646 : if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
650 : : {
651 : 1081863 : ret = create_tmp_from_val (val);
652 : 1081863 : DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
653 : : }
654 : : else
655 : : {
656 : 1271783 : elt_t elt, *elt_p;
657 : 1271783 : elt_t **slot;
658 : :
659 : 1271783 : elt.val = val;
660 : 1271783 : if (!gimplify_ctxp->temp_htab)
661 : 390534 : gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
662 : 1271783 : slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
663 : 1271783 : if (*slot == NULL)
664 : : {
665 : 1042581 : elt_p = XNEW (elt_t);
666 : 1042581 : elt_p->val = val;
667 : 1042581 : elt_p->temp = ret = create_tmp_from_val (val);
668 : 1042581 : *slot = elt_p;
669 : : }
670 : : else
671 : : {
672 : 229202 : elt_p = *slot;
673 : 229202 : ret = elt_p->temp;
674 : : }
675 : : }
676 : :
677 : 2353646 : return ret;
678 : : }
679 : :
680 : : /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
681 : :
682 : : static tree
683 : 26262614 : internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
684 : : bool is_formal, bool allow_ssa, bool not_gimple_reg)
685 : : {
686 : 26262614 : tree t, mod;
687 : :
688 : : /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
689 : : can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
690 : 26262614 : gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
691 : : fb_rvalue);
692 : :
693 : 26262614 : if (allow_ssa
694 : 26005329 : && gimplify_ctxp->into_ssa
695 : 50849497 : && is_gimple_reg_type (TREE_TYPE (val)))
696 : : {
697 : 23908969 : t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
698 : 23908969 : if (! gimple_in_ssa_p (cfun))
699 : : {
700 : 20576146 : const char *name = get_name (val);
701 : 20576146 : if (name)
702 : 6762678 : SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
703 : : }
704 : : }
705 : : else
706 : 2353645 : t = lookup_tmp_var (val, is_formal, not_gimple_reg);
707 : :
708 : 26262614 : mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
709 : :
710 : 26262614 : SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
711 : :
712 : : /* gimplify_modify_expr might want to reduce this further. */
713 : 26262614 : gimplify_and_add (mod, pre_p);
714 : 26262614 : ggc_free (mod);
715 : :
716 : : /* If we failed to gimplify VAL then we can end up with the temporary
717 : : SSA name not having a definition. In this case return a decl. */
718 : 26262614 : if (TREE_CODE (t) == SSA_NAME && ! SSA_NAME_DEF_STMT (t))
719 : 1 : return lookup_tmp_var (val, is_formal, not_gimple_reg);
720 : :
721 : : return t;
722 : : }
723 : :
724 : : /* Return a formal temporary variable initialized with VAL. PRE_P is as
725 : : in gimplify_expr. Only use this function if:
726 : :
727 : : 1) The value of the unfactored expression represented by VAL will not
728 : : change between the initialization and use of the temporary, and
729 : : 2) The temporary will not be otherwise modified.
730 : :
731 : : For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
732 : : and #2 means it is inappropriate for && temps.
733 : :
734 : : For other cases, use get_initialized_tmp_var instead. */
735 : :
736 : : tree
737 : 25770008 : get_formal_tmp_var (tree val, gimple_seq *pre_p)
738 : : {
739 : 25770008 : return internal_get_tmp_var (val, pre_p, NULL, true, true, false);
740 : : }
741 : :
742 : : /* Return a temporary variable initialized with VAL. PRE_P and POST_P
743 : : are as in gimplify_expr. */
744 : :
745 : : tree
746 : 480541 : get_initialized_tmp_var (tree val, gimple_seq *pre_p,
747 : : gimple_seq *post_p /* = NULL */,
748 : : bool allow_ssa /* = true */)
749 : : {
750 : 480541 : return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa, false);
751 : : }
752 : :
753 : : /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
754 : : generate debug info for them; otherwise don't. */
755 : :
756 : : void
757 : 3054794 : declare_vars (tree vars, gimple *gs, bool debug_info)
758 : : {
759 : 3054794 : tree last = vars;
760 : 3054794 : if (last)
761 : : {
762 : 1904065 : tree temps, block;
763 : :
764 : 1904065 : gbind *scope = as_a <gbind *> (gs);
765 : :
766 : 1904065 : temps = nreverse (last);
767 : :
768 : 1904065 : block = gimple_bind_block (scope);
769 : 1904065 : gcc_assert (!block || TREE_CODE (block) == BLOCK);
770 : 1904065 : if (!block || !debug_info)
771 : : {
772 : 1900529 : DECL_CHAIN (last) = gimple_bind_vars (scope);
773 : 1900529 : gimple_bind_set_vars (scope, temps);
774 : : }
775 : : else
776 : : {
777 : : /* We need to attach the nodes both to the BIND_EXPR and to its
778 : : associated BLOCK for debugging purposes. The key point here
779 : : is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
780 : : is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
781 : 3536 : if (BLOCK_VARS (block))
782 : 3343 : BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
783 : : else
784 : : {
785 : 193 : gimple_bind_set_vars (scope,
786 : : chainon (gimple_bind_vars (scope), temps));
787 : 193 : BLOCK_VARS (block) = temps;
788 : : }
789 : : }
790 : : }
791 : 3054794 : }
792 : :
793 : : /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
794 : : for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
795 : : no such upper bound can be obtained. */
796 : :
797 : : static void
798 : 0 : force_constant_size (tree var)
799 : : {
800 : : /* The only attempt we make is by querying the maximum size of objects
801 : : of the variable's type. */
802 : :
803 : 0 : HOST_WIDE_INT max_size;
804 : :
805 : 0 : gcc_assert (VAR_P (var));
806 : :
807 : 0 : max_size = max_int_size_in_bytes (TREE_TYPE (var));
808 : :
809 : 0 : gcc_assert (max_size >= 0);
810 : :
811 : 0 : DECL_SIZE_UNIT (var)
812 : 0 : = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
813 : 0 : DECL_SIZE (var)
814 : 0 : = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
815 : 0 : }
816 : :
817 : : /* Push the temporary variable TMP into the current binding. */
818 : :
819 : : void
820 : 32752 : gimple_add_tmp_var_fn (struct function *fn, tree tmp)
821 : : {
822 : 32752 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
823 : :
824 : : /* Later processing assumes that the object size is constant, which might
825 : : not be true at this point. Force the use of a constant upper bound in
826 : : this case. */
827 : 32752 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
828 : 0 : force_constant_size (tmp);
829 : :
830 : 32752 : DECL_CONTEXT (tmp) = fn->decl;
831 : 32752 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
832 : :
833 : 32752 : record_vars_into (tmp, fn->decl);
834 : 32752 : }
835 : :
836 : : /* Push the temporary variable TMP into the current binding. */
837 : :
838 : : void
839 : 16287453 : gimple_add_tmp_var (tree tmp)
840 : : {
841 : 16287453 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
842 : :
843 : : /* Later processing assumes that the object size is constant, which might
844 : : not be true at this point. Force the use of a constant upper bound in
845 : : this case. */
846 : 16287453 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
847 : 0 : force_constant_size (tmp);
848 : :
849 : 16287453 : DECL_CONTEXT (tmp) = current_function_decl;
850 : 16287453 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
851 : :
852 : 16287453 : if (gimplify_ctxp)
853 : : {
854 : 5596487 : DECL_CHAIN (tmp) = gimplify_ctxp->temps;
855 : 5596487 : gimplify_ctxp->temps = tmp;
856 : :
857 : : /* Mark temporaries local within the nearest enclosing parallel. */
858 : 5596487 : if (gimplify_omp_ctxp)
859 : : {
860 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
861 : 546186 : int flag = GOVD_LOCAL | GOVD_SEEN;
862 : : while (ctx
863 : 546186 : && (ctx->region_type == ORT_WORKSHARE
864 : : || ctx->region_type == ORT_TASKGROUP
865 : 398706 : || ctx->region_type == ORT_SIMD
866 : 348550 : || ctx->region_type == ORT_ACC))
867 : : {
868 : 204651 : if (ctx->region_type == ORT_SIMD
869 : 50156 : && TREE_ADDRESSABLE (tmp)
870 : 96 : && !TREE_STATIC (tmp))
871 : : {
872 : 96 : if (TREE_CODE (DECL_SIZE_UNIT (tmp)) != INTEGER_CST)
873 : 0 : ctx->add_safelen1 = true;
874 : 96 : else if (ctx->in_for_exprs)
875 : : flag = GOVD_PRIVATE;
876 : : else
877 : : flag = GOVD_PRIVATE | GOVD_SEEN;
878 : : break;
879 : : }
880 : 204555 : ctx = ctx->outer_context;
881 : : }
882 : 341535 : if (ctx)
883 : 314546 : omp_add_variable (ctx, tmp, flag);
884 : : }
885 : : }
886 : 10690966 : else if (cfun)
887 : 10690966 : record_vars (tmp);
888 : : else
889 : : {
890 : 0 : gimple_seq body_seq;
891 : :
892 : : /* This case is for nested functions. We need to expose the locals
893 : : they create. */
894 : 0 : body_seq = gimple_body (current_function_decl);
895 : 0 : declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
896 : : }
897 : 16287453 : }
898 : :
899 : :
900 : :
901 : : /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
902 : : nodes that are referenced more than once in GENERIC functions. This is
903 : : necessary because gimplification (translation into GIMPLE) is performed
904 : : by modifying tree nodes in-place, so gimplication of a shared node in a
905 : : first context could generate an invalid GIMPLE form in a second context.
906 : :
907 : : This is achieved with a simple mark/copy/unmark algorithm that walks the
908 : : GENERIC representation top-down, marks nodes with TREE_VISITED the first
909 : : time it encounters them, duplicates them if they already have TREE_VISITED
910 : : set, and finally removes the TREE_VISITED marks it has set.
911 : :
912 : : The algorithm works only at the function level, i.e. it generates a GENERIC
913 : : representation of a function with no nodes shared within the function when
914 : : passed a GENERIC function (except for nodes that are allowed to be shared).
915 : :
916 : : At the global level, it is also necessary to unshare tree nodes that are
917 : : referenced in more than one function, for the same aforementioned reason.
918 : : This requires some cooperation from the front-end. There are 2 strategies:
919 : :
920 : : 1. Manual unsharing. The front-end needs to call unshare_expr on every
921 : : expression that might end up being shared across functions.
922 : :
923 : : 2. Deep unsharing. This is an extension of regular unsharing. Instead
924 : : of calling unshare_expr on expressions that might be shared across
925 : : functions, the front-end pre-marks them with TREE_VISITED. This will
926 : : ensure that they are unshared on the first reference within functions
927 : : when the regular unsharing algorithm runs. The counterpart is that
928 : : this algorithm must look deeper than for manual unsharing, which is
929 : : specified by LANG_HOOKS_DEEP_UNSHARING.
930 : :
931 : : If there are only few specific cases of node sharing across functions, it is
932 : : probably easier for a front-end to unshare the expressions manually. On the
933 : : contrary, if the expressions generated at the global level are as widespread
934 : : as expressions generated within functions, deep unsharing is very likely the
935 : : way to go. */
936 : :
937 : : /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
938 : : These nodes model computations that must be done once. If we were to
939 : : unshare something like SAVE_EXPR(i++), the gimplification process would
940 : : create wrong code. However, if DATA is non-null, it must hold a pointer
941 : : set that is used to unshare the subtrees of these nodes. */
942 : :
943 : : static tree
944 : 2220124539 : mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
945 : : {
946 : 2220124539 : tree t = *tp;
947 : 2220124539 : enum tree_code code = TREE_CODE (t);
948 : :
949 : : /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
950 : : copy their subtrees if we can make sure to do it only once. */
951 : 2220124539 : if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
952 : : {
953 : 8497477 : if (data && !((hash_set<tree> *)data)->add (t))
954 : : ;
955 : : else
956 : 8497477 : *walk_subtrees = 0;
957 : : }
958 : :
959 : : /* Stop at types, decls, constants like copy_tree_r. */
960 : 2211627062 : else if (TREE_CODE_CLASS (code) == tcc_type
961 : : || TREE_CODE_CLASS (code) == tcc_declaration
962 : 2211627062 : || TREE_CODE_CLASS (code) == tcc_constant)
963 : 1341325120 : *walk_subtrees = 0;
964 : :
965 : : /* Cope with the statement expression extension. */
966 : 870301942 : else if (code == STATEMENT_LIST)
967 : : ;
968 : :
969 : : /* Leave the bulk of the work to copy_tree_r itself. */
970 : : else
971 : 870252308 : copy_tree_r (tp, walk_subtrees, NULL);
972 : :
973 : 2220124539 : return NULL_TREE;
974 : : }
975 : :
976 : : /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
977 : : If *TP has been visited already, then *TP is deeply copied by calling
978 : : mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
979 : :
980 : : static tree
981 : 279068076 : copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
982 : : {
983 : 279068076 : tree t = *tp;
984 : 279068076 : enum tree_code code = TREE_CODE (t);
985 : :
986 : : /* Skip types, decls, and constants. But we do want to look at their
987 : : types and the bounds of types. Mark them as visited so we properly
988 : : unmark their subtrees on the unmark pass. If we've already seen them,
989 : : don't look down further. */
990 : 279068076 : if (TREE_CODE_CLASS (code) == tcc_type
991 : : || TREE_CODE_CLASS (code) == tcc_declaration
992 : 279068076 : || TREE_CODE_CLASS (code) == tcc_constant)
993 : : {
994 : 131890499 : if (TREE_VISITED (t))
995 : 80799991 : *walk_subtrees = 0;
996 : : else
997 : 51090508 : TREE_VISITED (t) = 1;
998 : : }
999 : :
1000 : : /* If this node has been visited already, unshare it and don't look
1001 : : any deeper. */
1002 : 147177577 : else if (TREE_VISITED (t))
1003 : : {
1004 : 1640584 : walk_tree (tp, mostly_copy_tree_r, data, NULL);
1005 : 1640584 : *walk_subtrees = 0;
1006 : : }
1007 : :
1008 : : /* Otherwise, mark the node as visited and keep looking. */
1009 : : else
1010 : 145536993 : TREE_VISITED (t) = 1;
1011 : :
1012 : 279068076 : return NULL_TREE;
1013 : : }
1014 : :
1015 : : /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
1016 : : copy_if_shared_r callback unmodified. */
1017 : :
1018 : : void
1019 : 8846073 : copy_if_shared (tree *tp, void *data)
1020 : : {
1021 : 8846073 : walk_tree (tp, copy_if_shared_r, data, NULL);
1022 : 8846073 : }
1023 : :
1024 : : /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
1025 : : any nested functions. */
1026 : :
1027 : : static void
1028 : 2948691 : unshare_body (tree fndecl)
1029 : : {
1030 : 2948691 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
1031 : : /* If the language requires deep unsharing, we need a pointer set to make
1032 : : sure we don't repeatedly unshare subtrees of unshareable nodes. */
1033 : 2948691 : hash_set<tree> *visited
1034 : 2948691 : = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
1035 : :
1036 : 2948691 : copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
1037 : 2948691 : copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
1038 : 2948691 : copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1039 : :
1040 : 2951987 : delete visited;
1041 : :
1042 : 2948691 : if (cgn)
1043 : 5940240 : for (cgn = first_nested_function (cgn); cgn;
1044 : 24770 : cgn = next_nested_function (cgn))
1045 : 24770 : unshare_body (cgn->decl);
1046 : 2948691 : }
1047 : :
1048 : : /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1049 : : Subtrees are walked until the first unvisited node is encountered. */
1050 : :
1051 : : static tree
1052 : 279067650 : unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1053 : : {
1054 : 279067650 : tree t = *tp;
1055 : :
1056 : : /* If this node has been visited, unmark it and keep looking. */
1057 : 279067650 : if (TREE_VISITED (t))
1058 : 196631855 : TREE_VISITED (t) = 0;
1059 : :
1060 : : /* Otherwise, don't look any deeper. */
1061 : : else
1062 : 82435795 : *walk_subtrees = 0;
1063 : :
1064 : 279067650 : return NULL_TREE;
1065 : : }
1066 : :
1067 : : /* Unmark the visited trees rooted at *TP. */
1068 : :
1069 : : static inline void
1070 : 8846073 : unmark_visited (tree *tp)
1071 : : {
1072 : 8846073 : walk_tree (tp, unmark_visited_r, NULL, NULL);
1073 : 8846073 : }
1074 : :
1075 : : /* Likewise, but mark all trees as not visited. */
1076 : :
1077 : : static void
1078 : 2948691 : unvisit_body (tree fndecl)
1079 : : {
1080 : 2948691 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
1081 : :
1082 : 2948691 : unmark_visited (&DECL_SAVED_TREE (fndecl));
1083 : 2948691 : unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1084 : 2948691 : unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1085 : :
1086 : 2948691 : if (cgn)
1087 : 2970120 : for (cgn = first_nested_function (cgn);
1088 : 2970120 : cgn; cgn = next_nested_function (cgn))
1089 : 24770 : unvisit_body (cgn->decl);
1090 : 2948691 : }
1091 : :
1092 : : /* Unconditionally make an unshared copy of EXPR. This is used when using
1093 : : stored expressions which span multiple functions, such as BINFO_VTABLE,
1094 : : as the normal unsharing process can't tell that they're shared. */
1095 : :
1096 : : tree
1097 : 1211136301 : unshare_expr (tree expr)
1098 : : {
1099 : 1211136301 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1100 : 1211136301 : return expr;
1101 : : }
1102 : :
1103 : : /* Worker for unshare_expr_without_location. */
1104 : :
1105 : : static tree
1106 : 8412664 : prune_expr_location (tree *tp, int *walk_subtrees, void *)
1107 : : {
1108 : 8412664 : if (EXPR_P (*tp))
1109 : 3942040 : SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1110 : : else
1111 : 4470624 : *walk_subtrees = 0;
1112 : 8412664 : return NULL_TREE;
1113 : : }
1114 : :
1115 : : /* Similar to unshare_expr but also prune all expression locations
1116 : : from EXPR. */
1117 : :
1118 : : tree
1119 : 20850596 : unshare_expr_without_location (tree expr)
1120 : : {
1121 : 20850596 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1122 : 20850596 : if (EXPR_P (expr))
1123 : 2718991 : walk_tree (&expr, prune_expr_location, NULL, NULL);
1124 : 20850596 : return expr;
1125 : : }
1126 : :
1127 : : /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
1128 : : one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
1129 : : comprising at least one DEBUG_BEGIN_STMT followed by exactly one
1130 : : EXPR is the location of the EXPR. */
1131 : :
1132 : : static location_t
1133 : 1177212 : rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1134 : : {
1135 : 1177212 : if (!expr)
1136 : : return or_else;
1137 : :
1138 : 1177212 : if (EXPR_HAS_LOCATION (expr))
1139 : 822627 : return EXPR_LOCATION (expr);
1140 : :
1141 : 354585 : if (TREE_CODE (expr) != STATEMENT_LIST)
1142 : : return or_else;
1143 : :
1144 : 0 : tree_stmt_iterator i = tsi_start (expr);
1145 : :
1146 : 0 : bool found = false;
1147 : 0 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1148 : : {
1149 : 0 : found = true;
1150 : 0 : tsi_next (&i);
1151 : : }
1152 : :
1153 : 354585 : if (!found || !tsi_one_before_end_p (i))
1154 : : return or_else;
1155 : :
1156 : 0 : return rexpr_location (tsi_stmt (i), or_else);
1157 : : }
1158 : :
1159 : : /* Return TRUE iff EXPR (maybe recursively) has a location; see
1160 : : rexpr_location for the potential recursion. */
1161 : :
1162 : : static inline bool
1163 : 510575 : rexpr_has_location (tree expr)
1164 : : {
1165 : 303466 : return rexpr_location (expr) != UNKNOWN_LOCATION;
1166 : : }
1167 : :
1168 : :
1169 : : /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1170 : : contain statements and have a value. Assign its value to a temporary
1171 : : and give it void_type_node. Return the temporary, or NULL_TREE if
1172 : : WRAPPER was already void. */
1173 : :
1174 : : tree
1175 : 20696834 : voidify_wrapper_expr (tree wrapper, tree temp)
1176 : : {
1177 : 20696834 : tree type = TREE_TYPE (wrapper);
1178 : 20696834 : if (type && !VOID_TYPE_P (type))
1179 : : {
1180 : : tree *p;
1181 : :
1182 : : /* Set p to point to the body of the wrapper. Loop until we find
1183 : : something that isn't a wrapper. */
1184 : 764630 : for (p = &wrapper; p && *p; )
1185 : : {
1186 : 764630 : switch (TREE_CODE (*p))
1187 : : {
1188 : 3077 : case BIND_EXPR:
1189 : 3077 : TREE_SIDE_EFFECTS (*p) = 1;
1190 : 3077 : TREE_TYPE (*p) = void_type_node;
1191 : : /* For a BIND_EXPR, the body is operand 1. */
1192 : 3077 : p = &BIND_EXPR_BODY (*p);
1193 : 3077 : break;
1194 : :
1195 : 355290 : case CLEANUP_POINT_EXPR:
1196 : 355290 : case TRY_FINALLY_EXPR:
1197 : 355290 : case TRY_CATCH_EXPR:
1198 : 355290 : TREE_SIDE_EFFECTS (*p) = 1;
1199 : 355290 : TREE_TYPE (*p) = void_type_node;
1200 : 355290 : p = &TREE_OPERAND (*p, 0);
1201 : 355290 : break;
1202 : :
1203 : 19049 : case STATEMENT_LIST:
1204 : 19049 : {
1205 : 19049 : tree_stmt_iterator i = tsi_last (*p);
1206 : 19049 : TREE_SIDE_EFFECTS (*p) = 1;
1207 : 19049 : TREE_TYPE (*p) = void_type_node;
1208 : 19049 : p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1209 : : }
1210 : 19049 : break;
1211 : :
1212 : : case COMPOUND_EXPR:
1213 : : /* Advance to the last statement. Set all container types to
1214 : : void. */
1215 : 49246 : for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1216 : : {
1217 : 24626 : TREE_SIDE_EFFECTS (*p) = 1;
1218 : 24626 : TREE_TYPE (*p) = void_type_node;
1219 : : }
1220 : : break;
1221 : :
1222 : 76 : case TRANSACTION_EXPR:
1223 : 76 : TREE_SIDE_EFFECTS (*p) = 1;
1224 : 76 : TREE_TYPE (*p) = void_type_node;
1225 : 76 : p = &TRANSACTION_EXPR_BODY (*p);
1226 : 76 : break;
1227 : :
1228 : 362518 : default:
1229 : : /* Assume that any tree upon which voidify_wrapper_expr is
1230 : : directly called is a wrapper, and that its body is op0. */
1231 : 362518 : if (p == &wrapper)
1232 : : {
1233 : 33 : TREE_SIDE_EFFECTS (*p) = 1;
1234 : 33 : TREE_TYPE (*p) = void_type_node;
1235 : 33 : p = &TREE_OPERAND (*p, 0);
1236 : 33 : break;
1237 : : }
1238 : 362485 : goto out;
1239 : : }
1240 : : }
1241 : :
1242 : 0 : out:
1243 : 362485 : if (p == NULL || IS_EMPTY_STMT (*p))
1244 : : temp = NULL_TREE;
1245 : 362485 : else if (temp)
1246 : : {
1247 : : /* The wrapper is on the RHS of an assignment that we're pushing
1248 : : down. */
1249 : 1681 : gcc_assert (TREE_CODE (temp) == INIT_EXPR
1250 : : || TREE_CODE (temp) == MODIFY_EXPR);
1251 : 1681 : TREE_OPERAND (temp, 1) = *p;
1252 : 1681 : *p = temp;
1253 : : }
1254 : : else
1255 : : {
1256 : 360804 : temp = create_tmp_var (type, "retval");
1257 : 360804 : *p = build2 (INIT_EXPR, type, temp, *p);
1258 : : }
1259 : :
1260 : 362485 : return temp;
1261 : : }
1262 : :
1263 : : return NULL_TREE;
1264 : : }
1265 : :
1266 : : /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1267 : : a temporary through which they communicate. */
1268 : :
1269 : : static void
1270 : 7783 : build_stack_save_restore (gcall **save, gcall **restore)
1271 : : {
1272 : 7783 : tree tmp_var;
1273 : :
1274 : 15566 : *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1275 : 7783 : tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1276 : 7783 : gimple_call_set_lhs (*save, tmp_var);
1277 : :
1278 : 7783 : *restore
1279 : 7783 : = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1280 : : 1, tmp_var);
1281 : 7783 : }
1282 : :
1283 : : /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1284 : :
1285 : : static tree
1286 : 428 : build_asan_poison_call_expr (tree decl)
1287 : : {
1288 : : /* Do not poison variables that have size equal to zero. */
1289 : 428 : tree unit_size = DECL_SIZE_UNIT (decl);
1290 : 428 : if (zerop (unit_size))
1291 : : return NULL_TREE;
1292 : :
1293 : 428 : tree base = build_fold_addr_expr (decl);
1294 : :
1295 : 428 : return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1296 : : void_type_node, 3,
1297 : : build_int_cst (integer_type_node,
1298 : : ASAN_MARK_POISON),
1299 : : base, unit_size);
1300 : : }
1301 : :
1302 : : /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1303 : : on POISON flag, shadow memory of a DECL variable. The call will be
1304 : : put on location identified by IT iterator, where BEFORE flag drives
1305 : : position where the stmt will be put. */
1306 : :
1307 : : static void
1308 : 4721 : asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1309 : : bool before)
1310 : : {
1311 : 4721 : tree unit_size = DECL_SIZE_UNIT (decl);
1312 : 4721 : tree base = build_fold_addr_expr (decl);
1313 : :
1314 : : /* Do not poison variables that have size equal to zero. */
1315 : 4721 : if (zerop (unit_size))
1316 : 4721 : return;
1317 : :
1318 : : /* It's necessary to have all stack variables aligned to ASAN granularity
1319 : : bytes. */
1320 : 4707 : gcc_assert (!hwasan_sanitize_p () || hwasan_sanitize_stack_p ());
1321 : 4707 : unsigned shadow_granularity
1322 : 4707 : = hwasan_sanitize_p () ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY;
1323 : 4707 : if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
1324 : 4505 : SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
1325 : :
1326 : 4707 : HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1327 : :
1328 : 4707 : gimple *g
1329 : 4707 : = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1330 : 4707 : build_int_cst (integer_type_node, flags),
1331 : : base, unit_size);
1332 : :
1333 : 4707 : if (before)
1334 : 2464 : gsi_insert_before (it, g, GSI_NEW_STMT);
1335 : : else
1336 : 2243 : gsi_insert_after (it, g, GSI_NEW_STMT);
1337 : : }
1338 : :
1339 : : /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1340 : : either poisons or unpoisons a DECL. Created statement is appended
1341 : : to SEQ_P gimple sequence. */
1342 : :
1343 : : static void
1344 : 4293 : asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1345 : : {
1346 : 4293 : gimple_stmt_iterator it = gsi_last (*seq_p);
1347 : 4293 : bool before = false;
1348 : :
1349 : 4293 : if (gsi_end_p (it))
1350 : 2256 : before = true;
1351 : :
1352 : 4293 : asan_poison_variable (decl, poison, &it, before);
1353 : 4293 : }
1354 : :
1355 : : /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1356 : :
1357 : : static int
1358 : 135 : sort_by_decl_uid (const void *a, const void *b)
1359 : : {
1360 : 135 : const tree *t1 = (const tree *)a;
1361 : 135 : const tree *t2 = (const tree *)b;
1362 : :
1363 : 135 : int uid1 = DECL_UID (*t1);
1364 : 135 : int uid2 = DECL_UID (*t2);
1365 : :
1366 : 135 : if (uid1 < uid2)
1367 : : return -1;
1368 : 57 : else if (uid1 > uid2)
1369 : : return 1;
1370 : : else
1371 : 0 : return 0;
1372 : : }
1373 : :
1374 : : /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1375 : : depending on POISON flag. Created statement is appended
1376 : : to SEQ_P gimple sequence. */
1377 : :
1378 : : static void
1379 : 1062522 : asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1380 : : {
1381 : 1062522 : unsigned c = variables->elements ();
1382 : 1062522 : if (c == 0)
1383 : 1062346 : return;
1384 : :
1385 : 176 : auto_vec<tree> sorted_variables (c);
1386 : :
1387 : 176 : for (hash_set<tree>::iterator it = variables->begin ();
1388 : 570 : it != variables->end (); ++it)
1389 : 197 : sorted_variables.safe_push (*it);
1390 : :
1391 : 176 : sorted_variables.qsort (sort_by_decl_uid);
1392 : :
1393 : : unsigned i;
1394 : : tree var;
1395 : 549 : FOR_EACH_VEC_ELT (sorted_variables, i, var)
1396 : : {
1397 : 197 : asan_poison_variable (var, poison, seq_p);
1398 : :
1399 : : /* Add use_after_scope_memory attribute for the variable in order
1400 : : to prevent re-written into SSA. */
1401 : 197 : if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1402 : 197 : DECL_ATTRIBUTES (var)))
1403 : 90 : DECL_ATTRIBUTES (var)
1404 : 180 : = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1405 : : integer_one_node,
1406 : 90 : DECL_ATTRIBUTES (var));
1407 : : }
1408 : 176 : }
1409 : :
1410 : : /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1411 : :
1412 : : static enum gimplify_status
1413 : 5958534 : gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1414 : : {
1415 : 5958534 : tree bind_expr = *expr_p;
1416 : 5958534 : bool old_keep_stack = gimplify_ctxp->keep_stack;
1417 : 5958534 : bool old_save_stack = gimplify_ctxp->save_stack;
1418 : 5958534 : tree t;
1419 : 5958534 : gbind *bind_stmt;
1420 : 5958534 : gimple_seq body, cleanup;
1421 : 5958534 : gcall *stack_save;
1422 : 5958534 : location_t start_locus = 0, end_locus = 0;
1423 : 5958534 : tree ret_clauses = NULL;
1424 : :
1425 : 5958534 : tree temp = voidify_wrapper_expr (bind_expr, NULL);
1426 : :
1427 : : /* Mark variables seen in this bind expr. */
1428 : 12569108 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1429 : : {
1430 : 6610574 : if (VAR_P (t))
1431 : : {
1432 : 5974564 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1433 : 5974564 : tree attr;
1434 : :
1435 : 5974564 : if (flag_openmp
1436 : 233984 : && !is_global_var (t)
1437 : 226096 : && !TREE_STATIC (t)
1438 : 226096 : && DECL_CONTEXT (t) == current_function_decl
1439 : 226096 : && TREE_USED (t)
1440 : 6199122 : && (attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1441 : : != NULL_TREE)
1442 : : {
1443 : 74 : gcc_assert (!DECL_HAS_VALUE_EXPR_P (t));
1444 : 74 : tree alloc = TREE_PURPOSE (TREE_VALUE (attr));
1445 : 74 : tree align = TREE_VALUE (TREE_VALUE (attr));
1446 : : /* Allocate directives that appear in a target region must specify
1447 : : an allocator clause unless a requires directive with the
1448 : : dynamic_allocators clause is present in the same compilation
1449 : : unit. */
1450 : 74 : bool missing_dyn_alloc = false;
1451 : 74 : if (alloc == NULL_TREE
1452 : 48 : && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS)
1453 : : == 0))
1454 : : {
1455 : : /* This comes too early for omp_discover_declare_target...,
1456 : : but should at least catch the most common cases. */
1457 : 42 : missing_dyn_alloc
1458 : 42 : = cgraph_node::get (current_function_decl)->offloadable;
1459 : 42 : for (struct gimplify_omp_ctx *ctx2 = ctx;
1460 : 48 : ctx2 && !missing_dyn_alloc; ctx2 = ctx2->outer_context)
1461 : 6 : if (ctx2->code == OMP_TARGET)
1462 : 2 : missing_dyn_alloc = true;
1463 : : }
1464 : 42 : if (missing_dyn_alloc)
1465 : 4 : error_at (DECL_SOURCE_LOCATION (t),
1466 : : "%<allocate%> directive for %qD inside a target "
1467 : : "region must specify an %<allocator%> clause", t);
1468 : : /* Skip for omp_default_mem_alloc (= 1),
1469 : : unless align is present. For C/C++, there should be always a
1470 : : statement list following if TREE_USED, except for, e.g., using
1471 : : this decl in a static_assert; in that case, only a single
1472 : : DECL_EXPR remains, which can be skipped here. */
1473 : 70 : else if (!errorcount
1474 : 55 : && (align != NULL_TREE
1475 : 55 : || alloc == NULL_TREE
1476 : 10 : || !integer_onep (alloc))
1477 : 123 : && (lang_GNU_Fortran ()
1478 : 25 : || (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1479 : : != DECL_EXPR)))
1480 : : {
1481 : : /* Fortran might already use a pointer type internally;
1482 : : use that pointer except for type(C_ptr) and type(C_funptr);
1483 : : note that normal proc pointers are rejected. */
1484 : 53 : tree type = TREE_TYPE (t);
1485 : 53 : tree tmp, v;
1486 : 53 : if (lang_GNU_Fortran ()
1487 : 28 : && POINTER_TYPE_P (type)
1488 : 8 : && TREE_TYPE (type) != void_type_node
1489 : 59 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
1490 : : {
1491 : 6 : type = TREE_TYPE (type);
1492 : 6 : v = t;
1493 : : }
1494 : : else
1495 : : {
1496 : 47 : tmp = build_pointer_type (type);
1497 : 47 : v = create_tmp_var (tmp, get_name (t));
1498 : 47 : DECL_IGNORED_P (v) = 0;
1499 : 47 : DECL_ATTRIBUTES (v)
1500 : 47 : = tree_cons (get_identifier ("omp allocate var"),
1501 : : build_tree_list (NULL_TREE, t),
1502 : : remove_attribute ("omp allocate",
1503 : 47 : DECL_ATTRIBUTES (t)));
1504 : 47 : tmp = build_fold_indirect_ref (v);
1505 : 47 : TREE_THIS_NOTRAP (tmp) = 1;
1506 : 47 : SET_DECL_VALUE_EXPR (t, tmp);
1507 : 47 : DECL_HAS_VALUE_EXPR_P (t) = 1;
1508 : : }
1509 : 53 : tree sz = TYPE_SIZE_UNIT (type);
1510 : : /* The size to use in Fortran might not match TYPE_SIZE_UNIT;
1511 : : hence, for some decls, a size variable is saved in the
1512 : : attributes; use it, if available. */
1513 : 53 : if (TREE_CHAIN (TREE_VALUE (attr))
1514 : 28 : && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))
1515 : 59 : && TREE_PURPOSE (
1516 : : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))))
1517 : : {
1518 : 6 : sz = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1519 : 6 : sz = TREE_PURPOSE (sz);
1520 : : }
1521 : 53 : if (alloc == NULL_TREE)
1522 : 36 : alloc = build_zero_cst (ptr_type_node);
1523 : 53 : if (align == NULL_TREE)
1524 : 42 : align = build_int_cst (size_type_node, DECL_ALIGN_UNIT (t));
1525 : : else
1526 : 11 : align = build_int_cst (size_type_node,
1527 : 11 : MAX (tree_to_uhwi (align),
1528 : : DECL_ALIGN_UNIT (t)));
1529 : 53 : location_t loc = DECL_SOURCE_LOCATION (t);
1530 : 53 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
1531 : 53 : tmp = build_call_expr_loc (loc, tmp, 3, align, sz, alloc);
1532 : 53 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1533 : 53 : fold_convert (TREE_TYPE (v), tmp));
1534 : 53 : gcc_assert (BIND_EXPR_BODY (bind_expr) != NULL_TREE);
1535 : : /* Ensure that either TREE_CHAIN (TREE_VALUE (attr) is set
1536 : : and GOMP_FREE added here or that DECL_HAS_VALUE_EXPR_P (t)
1537 : : is set, using in a condition much further below. */
1538 : 59 : gcc_assert (DECL_HAS_VALUE_EXPR_P (t)
1539 : : || TREE_CHAIN (TREE_VALUE (attr)));
1540 : 53 : if (TREE_CHAIN (TREE_VALUE (attr)))
1541 : : {
1542 : : /* Fortran is special as it does not have properly nest
1543 : : declarations in blocks. And as there is no
1544 : : initializer, there is also no expression to look for.
1545 : : Hence, the FE makes the statement list of the
1546 : : try-finally block available. We can put the GOMP_alloc
1547 : : at the top, unless an allocator or size expression
1548 : : requires to put it afterward; note that the size is
1549 : : always later in generated code; for strings, no
1550 : : size expr but still an expr might be available.
1551 : : As LTO does not handle a statement list, 'sl' has
1552 : : to be removed; done so by removing the attribute. */
1553 : 28 : DECL_ATTRIBUTES (t)
1554 : 28 : = remove_attribute ("omp allocate",
1555 : 28 : DECL_ATTRIBUTES (t));
1556 : 28 : tree sl = TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)));
1557 : 28 : tree_stmt_iterator e = tsi_start (sl);
1558 : 28 : tree needle = NULL_TREE;
1559 : 28 : if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1560 : : {
1561 : 6 : needle = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1562 : 6 : needle = (TREE_VALUE (needle) ? TREE_VALUE (needle)
1563 : : : sz);
1564 : : }
1565 : 22 : else if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1566 : : needle = sz;
1567 : 22 : else if (DECL_P (alloc) && DECL_ARTIFICIAL (alloc))
1568 : : needle = alloc;
1569 : :
1570 : 10 : if (needle != NULL_TREE)
1571 : : {
1572 : 49 : while (!tsi_end_p (e))
1573 : : {
1574 : 49 : if (*e == needle
1575 : 49 : || (TREE_CODE (*e) == MODIFY_EXPR
1576 : 49 : && TREE_OPERAND (*e, 0) == needle))
1577 : : break;
1578 : 39 : ++e;
1579 : : }
1580 : 10 : gcc_assert (!tsi_end_p (e));
1581 : : }
1582 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1583 : :
1584 : : /* As the cleanup is in BIND_EXPR_BODY, GOMP_free is added
1585 : : here; for C/C++ it will be added in the 'cleanup'
1586 : : section after gimplification. But Fortran already has
1587 : : a try-finally block. */
1588 : 28 : sl = TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)));
1589 : 28 : e = tsi_last (sl);
1590 : 28 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1591 : 28 : tmp = build_call_expr_loc (EXPR_LOCATION (*e), tmp, 2, v,
1592 : : build_zero_cst (ptr_type_node));
1593 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1594 : 28 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1595 : 28 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1596 : 28 : fold_convert (TREE_TYPE (v), tmp));
1597 : 28 : ++e;
1598 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1599 : : }
1600 : : else
1601 : : {
1602 : 25 : gcc_assert (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1603 : : == STATEMENT_LIST);
1604 : 25 : tree_stmt_iterator e;
1605 : 25 : e = tsi_start (BIND_EXPR_BODY (bind_expr));
1606 : 113 : while (!tsi_end_p (e))
1607 : : {
1608 : 88 : if ((TREE_CODE (*e) == DECL_EXPR
1609 : 58 : && TREE_OPERAND (*e, 0) == t)
1610 : 121 : || (TREE_CODE (*e) == CLEANUP_POINT_EXPR
1611 : 0 : && (TREE_CODE (TREE_OPERAND (*e, 0))
1612 : : == DECL_EXPR)
1613 : 0 : && (TREE_OPERAND (TREE_OPERAND (*e, 0), 0)
1614 : : == t)))
1615 : : break;
1616 : 63 : ++e;
1617 : : }
1618 : 25 : gcc_assert (!tsi_end_p (e));
1619 : 25 : tsi_link_before (&e, tmp, TSI_SAME_STMT);
1620 : : }
1621 : : }
1622 : : }
1623 : :
1624 : : /* Mark variable as local. */
1625 : 5974564 : if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1626 : : {
1627 : 95610 : if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1628 : 95610 : || splay_tree_lookup (ctx->variables,
1629 : : (splay_tree_key) t) == NULL)
1630 : : {
1631 : 95610 : int flag = GOVD_LOCAL;
1632 : 95610 : if (ctx->region_type == ORT_SIMD
1633 : 4374 : && TREE_ADDRESSABLE (t)
1634 : 81 : && !TREE_STATIC (t))
1635 : : {
1636 : 69 : if (TREE_CODE (DECL_SIZE_UNIT (t)) != INTEGER_CST)
1637 : 2 : ctx->add_safelen1 = true;
1638 : : else
1639 : : flag = GOVD_PRIVATE;
1640 : : }
1641 : 95610 : omp_add_variable (ctx, t, flag | GOVD_SEEN);
1642 : : }
1643 : : /* Static locals inside of target construct or offloaded
1644 : : routines need to be "omp declare target". */
1645 : 95610 : if (TREE_STATIC (t))
1646 : 1278 : for (; ctx; ctx = ctx->outer_context)
1647 : 1123 : if ((ctx->region_type & ORT_TARGET) != 0)
1648 : : {
1649 : 910 : if (!lookup_attribute ("omp declare target",
1650 : 910 : DECL_ATTRIBUTES (t)))
1651 : : {
1652 : 898 : tree id = get_identifier ("omp declare target");
1653 : 898 : DECL_ATTRIBUTES (t)
1654 : 898 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
1655 : 898 : varpool_node *node = varpool_node::get (t);
1656 : 898 : if (node)
1657 : : {
1658 : 33 : node->offloadable = 1;
1659 : 33 : if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
1660 : : {
1661 : : g->have_offload = true;
1662 : : if (!in_lto_p)
1663 : : vec_safe_push (offload_vars, t);
1664 : : }
1665 : : }
1666 : : }
1667 : : break;
1668 : : }
1669 : : }
1670 : :
1671 : 5974564 : DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1672 : :
1673 : 5974564 : if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1674 : 1081 : cfun->has_local_explicit_reg_vars = true;
1675 : : }
1676 : : }
1677 : :
1678 : 11917068 : bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1679 : 5958534 : BIND_EXPR_BLOCK (bind_expr));
1680 : 5958534 : gimple_push_bind_expr (bind_stmt);
1681 : :
1682 : 5958534 : gimplify_ctxp->keep_stack = false;
1683 : 5958534 : gimplify_ctxp->save_stack = false;
1684 : :
1685 : : /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1686 : 5958534 : body = NULL;
1687 : 5958534 : gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1688 : 5958534 : gimple_bind_set_body (bind_stmt, body);
1689 : :
1690 : : /* Source location wise, the cleanup code (stack_restore and clobbers)
1691 : : belongs to the end of the block, so propagate what we have. The
1692 : : stack_save operation belongs to the beginning of block, which we can
1693 : : infer from the bind_expr directly if the block has no explicit
1694 : : assignment. */
1695 : 5958534 : if (BIND_EXPR_BLOCK (bind_expr))
1696 : : {
1697 : 5828195 : end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1698 : 5828195 : start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1699 : : }
1700 : 5828195 : if (start_locus == 0)
1701 : 5958534 : start_locus = EXPR_LOCATION (bind_expr);
1702 : :
1703 : 5958534 : cleanup = NULL;
1704 : 5958534 : stack_save = NULL;
1705 : :
1706 : : /* Add clobbers for all variables that go out of scope. */
1707 : 12569108 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1708 : : {
1709 : 6610574 : if (VAR_P (t)
1710 : 5974564 : && !is_global_var (t)
1711 : 12372836 : && DECL_CONTEXT (t) == current_function_decl)
1712 : : {
1713 : 5762262 : if (flag_openmp
1714 : 226089 : && DECL_HAS_VALUE_EXPR_P (t)
1715 : 1019 : && TREE_USED (t)
1716 : 5763265 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1717 : : {
1718 : : /* For Fortran, TREE_CHAIN (TREE_VALUE (attr)) is set, which
1719 : : causes that the GOMP_free call is already added above;
1720 : : and "omp allocate" is removed from DECL_ATTRIBUTES. */
1721 : 25 : tree v = TREE_OPERAND (DECL_VALUE_EXPR (t), 0);
1722 : 25 : tree tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1723 : 25 : tmp = build_call_expr_loc (end_locus, tmp, 2, v,
1724 : : build_zero_cst (ptr_type_node));
1725 : 25 : gimplify_and_add (tmp, &cleanup);
1726 : 25 : gimple *clobber_stmt;
1727 : 25 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1728 : 25 : clobber_stmt = gimple_build_assign (v, tmp);
1729 : 25 : gimple_set_location (clobber_stmt, end_locus);
1730 : 25 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1731 : : }
1732 : 5762262 : if (!DECL_HARD_REGISTER (t)
1733 : 5761181 : && !TREE_THIS_VOLATILE (t)
1734 : 5722246 : && !DECL_HAS_VALUE_EXPR_P (t)
1735 : : /* Only care for variables that have to be in memory. Others
1736 : : will be rewritten into SSA names, hence moved to the
1737 : : top-level. */
1738 : 5619615 : && !is_gimple_reg (t)
1739 : 6982920 : && flag_stack_reuse != SR_NONE)
1740 : : {
1741 : 1217584 : tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
1742 : 1217584 : gimple *clobber_stmt;
1743 : 1217584 : clobber_stmt = gimple_build_assign (t, clobber);
1744 : 1217584 : gimple_set_location (clobber_stmt, end_locus);
1745 : 1217584 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1746 : : }
1747 : :
1748 : 5762262 : if (flag_openacc && oacc_declare_returns != NULL)
1749 : : {
1750 : 207 : tree key = t;
1751 : 207 : if (DECL_HAS_VALUE_EXPR_P (key))
1752 : : {
1753 : 8 : key = DECL_VALUE_EXPR (key);
1754 : 8 : if (INDIRECT_REF_P (key))
1755 : 8 : key = TREE_OPERAND (key, 0);
1756 : : }
1757 : 207 : tree *c = oacc_declare_returns->get (key);
1758 : 207 : if (c != NULL)
1759 : : {
1760 : 116 : if (ret_clauses)
1761 : 64 : OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1762 : :
1763 : 116 : ret_clauses = unshare_expr (*c);
1764 : :
1765 : 116 : oacc_declare_returns->remove (key);
1766 : :
1767 : 116 : if (oacc_declare_returns->is_empty ())
1768 : : {
1769 : 40 : delete oacc_declare_returns;
1770 : 40 : oacc_declare_returns = NULL;
1771 : : }
1772 : : }
1773 : : }
1774 : : }
1775 : :
1776 : 6610574 : if (asan_poisoned_variables != NULL
1777 : 6610574 : && asan_poisoned_variables->contains (t))
1778 : : {
1779 : 2048 : asan_poisoned_variables->remove (t);
1780 : 2048 : asan_poison_variable (t, true, &cleanup);
1781 : : }
1782 : :
1783 : 6610574 : if (gimplify_ctxp->live_switch_vars != NULL
1784 : 6610574 : && gimplify_ctxp->live_switch_vars->contains (t))
1785 : 55 : gimplify_ctxp->live_switch_vars->remove (t);
1786 : : }
1787 : :
1788 : : /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1789 : : the stack space allocated to the VLAs. */
1790 : 5958534 : if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1791 : : {
1792 : 7783 : gcall *stack_restore;
1793 : :
1794 : : /* Save stack on entry and restore it on exit. Add a try_finally
1795 : : block to achieve this. */
1796 : 7783 : build_stack_save_restore (&stack_save, &stack_restore);
1797 : :
1798 : 7783 : gimple_set_location (stack_save, start_locus);
1799 : 7783 : gimple_set_location (stack_restore, end_locus);
1800 : :
1801 : 7783 : gimplify_seq_add_stmt (&cleanup, stack_restore);
1802 : : }
1803 : :
1804 : 5958534 : if (ret_clauses)
1805 : : {
1806 : 52 : gomp_target *stmt;
1807 : 52 : gimple_stmt_iterator si = gsi_start (cleanup);
1808 : :
1809 : 52 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1810 : : ret_clauses);
1811 : 52 : gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1812 : : }
1813 : :
1814 : 5958534 : if (cleanup)
1815 : : {
1816 : 746156 : gtry *gs;
1817 : 746156 : gimple_seq new_body;
1818 : :
1819 : 746156 : new_body = NULL;
1820 : 746156 : gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1821 : : GIMPLE_TRY_FINALLY);
1822 : :
1823 : 746156 : if (stack_save)
1824 : 7783 : gimplify_seq_add_stmt (&new_body, stack_save);
1825 : 746156 : gimplify_seq_add_stmt (&new_body, gs);
1826 : 746156 : gimple_bind_set_body (bind_stmt, new_body);
1827 : : }
1828 : :
1829 : : /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1830 : 5958534 : if (!gimplify_ctxp->keep_stack)
1831 : 5942911 : gimplify_ctxp->keep_stack = old_keep_stack;
1832 : 5958534 : gimplify_ctxp->save_stack = old_save_stack;
1833 : :
1834 : 5958534 : gimple_pop_bind_expr ();
1835 : :
1836 : 5958534 : gimplify_seq_add_stmt (pre_p, bind_stmt);
1837 : :
1838 : 5958534 : if (temp)
1839 : : {
1840 : 1709 : *expr_p = temp;
1841 : 1709 : return GS_OK;
1842 : : }
1843 : :
1844 : 5956825 : *expr_p = NULL_TREE;
1845 : 5956825 : return GS_ALL_DONE;
1846 : : }
1847 : :
1848 : : /* Maybe add early return predict statement to PRE_P sequence. */
1849 : :
1850 : : static void
1851 : 2238800 : maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1852 : : {
1853 : : /* If we are not in a conditional context, add PREDICT statement. */
1854 : 2238800 : if (gimple_conditional_context ())
1855 : : {
1856 : 445364 : gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1857 : : NOT_TAKEN);
1858 : 445364 : gimplify_seq_add_stmt (pre_p, predict);
1859 : : }
1860 : 2238800 : }
1861 : :
1862 : : /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1863 : : GIMPLE value, it is assigned to a new temporary and the statement is
1864 : : re-written to return the temporary.
1865 : :
1866 : : PRE_P points to the sequence where side effects that must happen before
1867 : : STMT should be stored. */
1868 : :
1869 : : static enum gimplify_status
1870 : 2238806 : gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1871 : : {
1872 : 2238806 : greturn *ret;
1873 : 2238806 : tree ret_expr = TREE_OPERAND (stmt, 0);
1874 : 2238806 : tree result_decl, result;
1875 : :
1876 : 2238806 : if (ret_expr == error_mark_node)
1877 : : return GS_ERROR;
1878 : :
1879 : 2238800 : if (!ret_expr
1880 : 2147947 : || TREE_CODE (ret_expr) == RESULT_DECL)
1881 : : {
1882 : 106089 : maybe_add_early_return_predict_stmt (pre_p);
1883 : 106089 : greturn *ret = gimple_build_return (ret_expr);
1884 : 106089 : copy_warning (ret, stmt);
1885 : 106089 : gimplify_seq_add_stmt (pre_p, ret);
1886 : 106089 : return GS_ALL_DONE;
1887 : : }
1888 : :
1889 : 2132711 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1890 : : result_decl = NULL_TREE;
1891 : 2132390 : else if (TREE_CODE (ret_expr) == COMPOUND_EXPR)
1892 : : {
1893 : : /* Used in C++ for handling EH cleanup of the return value if a local
1894 : : cleanup throws. Assume the front-end knows what it's doing. */
1895 : 5068 : result_decl = DECL_RESULT (current_function_decl);
1896 : : /* But crash if we end up trying to modify ret_expr below. */
1897 : 5068 : ret_expr = NULL_TREE;
1898 : : }
1899 : : else
1900 : : {
1901 : 2127322 : result_decl = TREE_OPERAND (ret_expr, 0);
1902 : :
1903 : : /* See through a return by reference. */
1904 : 2127322 : if (INDIRECT_REF_P (result_decl))
1905 : 44301 : result_decl = TREE_OPERAND (result_decl, 0);
1906 : :
1907 : 2127322 : gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1908 : : || TREE_CODE (ret_expr) == INIT_EXPR)
1909 : : && TREE_CODE (result_decl) == RESULT_DECL);
1910 : : }
1911 : :
1912 : : /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1913 : : Recall that aggregate_value_p is FALSE for any aggregate type that is
1914 : : returned in registers. If we're returning values in registers, then
1915 : : we don't want to extend the lifetime of the RESULT_DECL, particularly
1916 : : across another call. In addition, for those aggregates for which
1917 : : hard_function_value generates a PARALLEL, we'll die during normal
1918 : : expansion of structure assignments; there's special code in expand_return
1919 : : to handle this case that does not exist in expand_expr. */
1920 : 5068 : if (!result_decl)
1921 : : result = NULL_TREE;
1922 : 2132390 : else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1923 : : {
1924 : 182599 : if (!poly_int_tree_p (DECL_SIZE (result_decl)))
1925 : : {
1926 : 43 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1927 : 0 : gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1928 : : /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1929 : : should be effectively allocated by the caller, i.e. all calls to
1930 : : this function must be subject to the Return Slot Optimization. */
1931 : 43 : gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1932 : 43 : gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1933 : : }
1934 : : result = result_decl;
1935 : : }
1936 : 1949791 : else if (gimplify_ctxp->return_temp)
1937 : : result = gimplify_ctxp->return_temp;
1938 : : else
1939 : : {
1940 : 1456112 : result = create_tmp_reg (TREE_TYPE (result_decl));
1941 : :
1942 : : /* ??? With complex control flow (usually involving abnormal edges),
1943 : : we can wind up warning about an uninitialized value for this. Due
1944 : : to how this variable is constructed and initialized, this is never
1945 : : true. Give up and never warn. */
1946 : 1456112 : suppress_warning (result, OPT_Wuninitialized);
1947 : :
1948 : 1456112 : gimplify_ctxp->return_temp = result;
1949 : : }
1950 : :
1951 : : /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1952 : : Then gimplify the whole thing. */
1953 : 2132711 : if (result != result_decl)
1954 : 1949791 : TREE_OPERAND (ret_expr, 0) = result;
1955 : :
1956 : 2132711 : gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1957 : :
1958 : 2132711 : maybe_add_early_return_predict_stmt (pre_p);
1959 : 2132711 : ret = gimple_build_return (result);
1960 : 2132711 : copy_warning (ret, stmt);
1961 : 2132711 : gimplify_seq_add_stmt (pre_p, ret);
1962 : :
1963 : 2132711 : return GS_ALL_DONE;
1964 : : }
1965 : :
1966 : : /* Gimplify a variable-length array DECL. */
1967 : :
1968 : : static void
1969 : 8861 : gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1970 : : {
1971 : : /* This is a variable-sized decl. Simplify its size and mark it
1972 : : for deferred expansion. */
1973 : 8861 : tree t, addr, ptr_type;
1974 : :
1975 : 8861 : gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1976 : 8861 : gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1977 : :
1978 : : /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1979 : 8861 : if (DECL_HAS_VALUE_EXPR_P (decl))
1980 : : return;
1981 : :
1982 : : /* All occurrences of this decl in final gimplified code will be
1983 : : replaced by indirection. Setting DECL_VALUE_EXPR does two
1984 : : things: First, it lets the rest of the gimplifier know what
1985 : : replacement to use. Second, it lets the debug info know
1986 : : where to find the value. */
1987 : 8856 : ptr_type = build_pointer_type (TREE_TYPE (decl));
1988 : 8856 : addr = create_tmp_var (ptr_type, get_name (decl));
1989 : 8856 : DECL_IGNORED_P (addr) = 0;
1990 : 8856 : t = build_fold_indirect_ref (addr);
1991 : 8856 : TREE_THIS_NOTRAP (t) = 1;
1992 : 8856 : SET_DECL_VALUE_EXPR (decl, t);
1993 : 8856 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1994 : :
1995 : 8856 : t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1996 : 8856 : max_int_size_in_bytes (TREE_TYPE (decl)));
1997 : : /* The call has been built for a variable-sized object. */
1998 : 8856 : CALL_ALLOCA_FOR_VAR_P (t) = 1;
1999 : 8856 : t = fold_convert (ptr_type, t);
2000 : 8856 : t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
2001 : :
2002 : 8856 : gimplify_and_add (t, seq_p);
2003 : :
2004 : : /* Record the dynamic allocation associated with DECL if requested. */
2005 : 8856 : if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
2006 : 0 : record_dynamic_alloc (decl);
2007 : : }
2008 : :
2009 : : /* A helper function to be called via walk_tree. Mark all labels under *TP
2010 : : as being forced. To be called for DECL_INITIAL of static variables. */
2011 : :
2012 : : static tree
2013 : 859565 : force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2014 : : {
2015 : 859565 : if (TYPE_P (*tp))
2016 : 0 : *walk_subtrees = 0;
2017 : 859565 : if (TREE_CODE (*tp) == LABEL_DECL)
2018 : : {
2019 : 921 : FORCED_LABEL (*tp) = 1;
2020 : 921 : cfun->has_forced_label_in_static = 1;
2021 : : }
2022 : :
2023 : 859565 : return NULL_TREE;
2024 : : }
2025 : :
2026 : : /* Generate an initialization to automatic variable DECL based on INIT_TYPE.
2027 : : Build a call to internal const function DEFERRED_INIT:
2028 : : 1st argument: SIZE of the DECL;
2029 : : 2nd argument: INIT_TYPE;
2030 : : 3rd argument: NAME of the DECL;
2031 : :
2032 : : as LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL). */
2033 : :
2034 : : static void
2035 : 590 : gimple_add_init_for_auto_var (tree decl,
2036 : : enum auto_init_type init_type,
2037 : : gimple_seq *seq_p)
2038 : : {
2039 : 590 : gcc_assert (auto_var_p (decl));
2040 : 590 : gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
2041 : :
2042 : 590 : const location_t loc = DECL_SOURCE_LOCATION (decl);
2043 : 590 : tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
2044 : 590 : tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
2045 : 590 : tree decl_name;
2046 : :
2047 : 590 : if (DECL_NAME (decl))
2048 : 587 : decl_name = build_string_literal (DECL_NAME (decl));
2049 : : else
2050 : : {
2051 : 3 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
2052 : 3 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
2053 : 3 : decl_name = build_string_literal (decl_name_anonymous);
2054 : : }
2055 : :
2056 : 590 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
2057 : 590 : TREE_TYPE (decl), 3,
2058 : : decl_size, init_type_node,
2059 : : decl_name);
2060 : :
2061 : 590 : gimplify_assign (decl, call, seq_p);
2062 : 590 : }
2063 : :
2064 : : /* Generate padding initialization for automatic variable DECL.
2065 : : C guarantees that brace-init with fewer initializers than members
2066 : : aggregate will initialize the rest of the aggregate as-if it were
2067 : : static initialization. In turn static initialization guarantees
2068 : : that padding is initialized to zero. So, we always initialize paddings
2069 : : to zeroes regardless INIT_TYPE.
2070 : : To do the padding initialization, we insert a call to
2071 : : __builtin_clear_padding (&decl, 0, for_auto_init = true).
2072 : : Note, we add an additional dummy argument for __builtin_clear_padding,
2073 : : 'for_auto_init' to distinguish whether this call is for automatic
2074 : : variable initialization or not.
2075 : : */
2076 : : static void
2077 : 109 : gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
2078 : : gimple_seq *seq_p)
2079 : : {
2080 : 109 : tree addr_of_decl = NULL_TREE;
2081 : 109 : tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
2082 : :
2083 : 109 : if (is_vla)
2084 : : {
2085 : : /* The temporary address variable for this vla should be
2086 : : created in gimplify_vla_decl. */
2087 : 0 : gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
2088 : 0 : gcc_assert (INDIRECT_REF_P (DECL_VALUE_EXPR (decl)));
2089 : 0 : addr_of_decl = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
2090 : : }
2091 : : else
2092 : : {
2093 : 109 : mark_addressable (decl);
2094 : 109 : addr_of_decl = build_fold_addr_expr (decl);
2095 : : }
2096 : :
2097 : 109 : gimple *call = gimple_build_call (fn, 2, addr_of_decl,
2098 : 109 : build_one_cst (TREE_TYPE (addr_of_decl)));
2099 : 109 : gimplify_seq_add_stmt (seq_p, call);
2100 : 109 : }
2101 : :
2102 : : /* Return true if the DECL need to be automaticly initialized by the
2103 : : compiler. */
2104 : : static bool
2105 : 3372104 : is_var_need_auto_init (tree decl)
2106 : : {
2107 : 3372104 : if (auto_var_p (decl)
2108 : 3276775 : && (TREE_CODE (decl) != VAR_DECL
2109 : 3274903 : || !DECL_HARD_REGISTER (decl))
2110 : 3276073 : && (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2111 : 643 : && (!lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl)))
2112 : 635 : && !OPAQUE_TYPE_P (TREE_TYPE (decl))
2113 : 3372739 : && !is_empty_type (TREE_TYPE (decl)))
2114 : : return true;
2115 : : return false;
2116 : : }
2117 : :
2118 : : /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
2119 : : and initialization explicit. */
2120 : :
2121 : : static enum gimplify_status
2122 : 6443146 : gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
2123 : : {
2124 : 6443146 : tree stmt = *stmt_p;
2125 : 6443146 : tree decl = DECL_EXPR_DECL (stmt);
2126 : :
2127 : 6443146 : *stmt_p = NULL_TREE;
2128 : :
2129 : 6443146 : if (TREE_TYPE (decl) == error_mark_node)
2130 : : return GS_ERROR;
2131 : :
2132 : 6443079 : if ((TREE_CODE (decl) == TYPE_DECL
2133 : 6400079 : || VAR_P (decl))
2134 : 12840189 : && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
2135 : : {
2136 : 693625 : gimplify_type_sizes (TREE_TYPE (decl), seq_p);
2137 : 693625 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
2138 : 22478 : gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
2139 : : }
2140 : :
2141 : : /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
2142 : : in case its size expressions contain problematic nodes like CALL_EXPR. */
2143 : 6443079 : if (TREE_CODE (decl) == TYPE_DECL
2144 : 43000 : && DECL_ORIGINAL_TYPE (decl)
2145 : 6448394 : && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
2146 : : {
2147 : 0 : gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
2148 : 0 : if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
2149 : 0 : gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
2150 : : }
2151 : :
2152 : 6443079 : if (VAR_P (decl) && !DECL_EXTERNAL (decl))
2153 : : {
2154 : 6397110 : tree init = DECL_INITIAL (decl);
2155 : 6397110 : bool is_vla = false;
2156 : : /* Check whether a decl has FE created VALUE_EXPR here BEFORE
2157 : : gimplify_vla_decl creates VALUE_EXPR for a vla decl.
2158 : : If the decl has VALUE_EXPR that was created by FE (usually
2159 : : C++FE), it's a proxy varaible, and FE already initialized
2160 : : the VALUE_EXPR of it, we should not initialize it anymore. */
2161 : 6397110 : bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
2162 : :
2163 : 6397110 : poly_uint64 size;
2164 : 6397110 : if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
2165 : 6397110 : || (!TREE_STATIC (decl)
2166 : 6272033 : && flag_stack_check == GENERIC_STACK_CHECK
2167 : 228 : && maybe_gt (size,
2168 : : (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
2169 : : {
2170 : 8738 : gimplify_vla_decl (decl, seq_p);
2171 : 8738 : is_vla = true;
2172 : : }
2173 : :
2174 : 6397110 : if (asan_poisoned_variables
2175 : 4814 : && !is_vla
2176 : 4628 : && TREE_ADDRESSABLE (decl)
2177 : 2301 : && !TREE_STATIC (decl)
2178 : 2091 : && !DECL_HAS_VALUE_EXPR_P (decl)
2179 : 2056 : && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
2180 : 2056 : && dbg_cnt (asan_use_after_scope)
2181 : 2056 : && !gimplify_omp_ctxp
2182 : : /* GNAT introduces temporaries to hold return values of calls in
2183 : : initializers of variables defined in other units, so the
2184 : : declaration of the variable is discarded completely. We do not
2185 : : want to issue poison calls for such dropped variables. */
2186 : 6399158 : && (DECL_SEEN_IN_BIND_EXPR_P (decl)
2187 : 0 : || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
2188 : : {
2189 : 2048 : asan_poisoned_variables->add (decl);
2190 : 2048 : asan_poison_variable (decl, false, seq_p);
2191 : 2048 : if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
2192 : 55 : gimplify_ctxp->live_switch_vars->add (decl);
2193 : : }
2194 : :
2195 : : /* Some front ends do not explicitly declare all anonymous
2196 : : artificial variables. We compensate here by declaring the
2197 : : variables, though it would be better if the front ends would
2198 : : explicitly declare them. */
2199 : 6397110 : if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
2200 : 6397110 : && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
2201 : 18147 : gimple_add_tmp_var (decl);
2202 : :
2203 : 6397110 : if (init && init != error_mark_node)
2204 : : {
2205 : 3681064 : if (!TREE_STATIC (decl))
2206 : : {
2207 : 3651026 : DECL_INITIAL (decl) = NULL_TREE;
2208 : 3651026 : init = build2 (INIT_EXPR, void_type_node, decl, init);
2209 : 3651026 : gimplify_and_add (init, seq_p);
2210 : 3651026 : ggc_free (init);
2211 : : /* Clear TREE_READONLY if we really have an initialization. */
2212 : 3651026 : if (!DECL_INITIAL (decl)
2213 : 3651026 : && !omp_privatize_by_reference (decl))
2214 : 3632607 : TREE_READONLY (decl) = 0;
2215 : : }
2216 : : else
2217 : : /* We must still examine initializers for static variables
2218 : : as they may contain a label address. */
2219 : 30038 : walk_tree (&init, force_labels_r, NULL, NULL);
2220 : : }
2221 : : /* When there is no explicit initializer, if the user requested,
2222 : : We should insert an artifical initializer for this automatic
2223 : : variable. */
2224 : 2716046 : else if (is_var_need_auto_init (decl)
2225 : 2716046 : && !decl_had_value_expr_p)
2226 : : {
2227 : 590 : gimple_add_init_for_auto_var (decl,
2228 : : flag_auto_var_init,
2229 : : seq_p);
2230 : : /* The expanding of a call to the above .DEFERRED_INIT will apply
2231 : : block initialization to the whole space covered by this variable.
2232 : : As a result, all the paddings will be initialized to zeroes
2233 : : for zero initialization and 0xFE byte-repeatable patterns for
2234 : : pattern initialization.
2235 : : In order to make the paddings as zeroes for pattern init, We
2236 : : should add a call to __builtin_clear_padding to clear the
2237 : : paddings to zero in compatiple with CLANG.
2238 : : We cannot insert this call if the variable is a gimple register
2239 : : since __builtin_clear_padding will take the address of the
2240 : : variable. As a result, if a long double/_Complex long double
2241 : : variable will spilled into stack later, its padding is 0XFE. */
2242 : 590 : if (flag_auto_var_init == AUTO_INIT_PATTERN
2243 : 180 : && !is_gimple_reg (decl)
2244 : 684 : && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
2245 : 71 : gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
2246 : : }
2247 : : }
2248 : :
2249 : : return GS_ALL_DONE;
2250 : : }
2251 : :
2252 : : /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
2253 : : and replacing the LOOP_EXPR with goto, but if the loop contains an
2254 : : EXIT_EXPR, we need to append a label for it to jump to. */
2255 : :
2256 : : static enum gimplify_status
2257 : 194582 : gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
2258 : : {
2259 : 194582 : tree saved_label = gimplify_ctxp->exit_label;
2260 : 194582 : tree start_label = create_artificial_label (UNKNOWN_LOCATION);
2261 : :
2262 : 194582 : gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
2263 : :
2264 : 194582 : gimplify_ctxp->exit_label = NULL_TREE;
2265 : :
2266 : 194582 : gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
2267 : :
2268 : 194582 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
2269 : :
2270 : 194582 : if (gimplify_ctxp->exit_label)
2271 : 6190 : gimplify_seq_add_stmt (pre_p,
2272 : 3095 : gimple_build_label (gimplify_ctxp->exit_label));
2273 : :
2274 : 194582 : gimplify_ctxp->exit_label = saved_label;
2275 : :
2276 : 194582 : *expr_p = NULL;
2277 : 194582 : return GS_ALL_DONE;
2278 : : }
2279 : :
2280 : : /* Gimplify a statement list onto a sequence. These may be created either
2281 : : by an enlightened front-end, or by shortcut_cond_expr. */
2282 : :
2283 : : static enum gimplify_status
2284 : 8679403 : gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
2285 : : {
2286 : 8679403 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
2287 : :
2288 : 8679403 : tree_stmt_iterator i = tsi_start (*expr_p);
2289 : :
2290 : 55163822 : while (!tsi_end_p (i))
2291 : : {
2292 : 37805016 : gimplify_stmt (tsi_stmt_ptr (i), pre_p);
2293 : 37805016 : tsi_delink (&i);
2294 : : }
2295 : :
2296 : 8679403 : if (temp)
2297 : : {
2298 : 15972 : *expr_p = temp;
2299 : 15972 : return GS_OK;
2300 : : }
2301 : :
2302 : : return GS_ALL_DONE;
2303 : : }
2304 : :
2305 : :
2306 : : /* Emit warning for the unreachable statment STMT if needed.
2307 : : Return the gimple itself when the warning is emitted, otherwise
2308 : : return NULL. */
2309 : : static gimple *
2310 : 188 : emit_warn_switch_unreachable (gimple *stmt)
2311 : : {
2312 : 188 : if (gimple_code (stmt) == GIMPLE_GOTO
2313 : 45 : && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
2314 : 233 : && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
2315 : : /* Don't warn for compiler-generated gotos. These occur
2316 : : in Duff's devices, for example. */
2317 : : return NULL;
2318 : 143 : else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2319 : 143 : && ((gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2320 : 6 : || (gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
2321 : 2 : && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
2322 : 4 : || (is_gimple_assign (stmt)
2323 : 4 : && gimple_assign_single_p (stmt)
2324 : 4 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2325 : 4 : && gimple_call_internal_p (
2326 : 4 : SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)),
2327 : : IFN_DEFERRED_INIT))))
2328 : : /* Don't warn for compiler-generated initializations for
2329 : : -ftrivial-auto-var-init.
2330 : : There are 3 cases:
2331 : : case 1: a call to .DEFERRED_INIT;
2332 : : case 2: a call to __builtin_clear_padding with the 2nd argument is
2333 : : present and non-zero;
2334 : : case 3: a gimple assign store right after the call to .DEFERRED_INIT
2335 : : that has the LHS of .DEFERRED_INIT as the RHS as following:
2336 : : _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
2337 : : i1 = _1. */
2338 : 12 : return NULL;
2339 : : else
2340 : 131 : warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
2341 : : "statement will never be executed");
2342 : 131 : return stmt;
2343 : : }
2344 : :
2345 : : /* Callback for walk_gimple_seq. */
2346 : :
2347 : : static tree
2348 : 52194 : warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
2349 : : bool *handled_ops_p,
2350 : : struct walk_stmt_info *wi)
2351 : : {
2352 : 52194 : gimple *stmt = gsi_stmt (*gsi_p);
2353 : 52194 : bool unreachable_issued = wi->info != NULL;
2354 : :
2355 : 52194 : *handled_ops_p = true;
2356 : 52194 : switch (gimple_code (stmt))
2357 : : {
2358 : 133 : case GIMPLE_TRY:
2359 : : /* A compiler-generated cleanup or a user-written try block.
2360 : : If it's empty, don't dive into it--that would result in
2361 : : worse location info. */
2362 : 133 : if (gimple_try_eval (stmt) == NULL)
2363 : : {
2364 : 3 : if (warn_switch_unreachable && !unreachable_issued)
2365 : 3 : wi->info = emit_warn_switch_unreachable (stmt);
2366 : :
2367 : : /* Stop when auto var init warning is not on. */
2368 : 3 : if (!warn_trivial_auto_var_init)
2369 : 3 : return integer_zero_node;
2370 : : }
2371 : : /* Fall through. */
2372 : 3170 : case GIMPLE_BIND:
2373 : 3170 : case GIMPLE_CATCH:
2374 : 3170 : case GIMPLE_EH_FILTER:
2375 : 3170 : case GIMPLE_TRANSACTION:
2376 : : /* Walk the sub-statements. */
2377 : 3170 : *handled_ops_p = false;
2378 : 3170 : break;
2379 : :
2380 : : case GIMPLE_DEBUG:
2381 : : /* Ignore these. We may generate them before declarations that
2382 : : are never executed. If there's something to warn about,
2383 : : there will be non-debug stmts too, and we'll catch those. */
2384 : : break;
2385 : :
2386 : 47896 : case GIMPLE_LABEL:
2387 : : /* Stop till the first Label. */
2388 : 47896 : return integer_zero_node;
2389 : 46 : case GIMPLE_CALL:
2390 : 46 : if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2391 : : {
2392 : 8 : *handled_ops_p = false;
2393 : 8 : break;
2394 : : }
2395 : 38 : if (warn_trivial_auto_var_init
2396 : 14 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2397 : 52 : && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2398 : : {
2399 : : /* Get the variable name from the 3rd argument of call. */
2400 : 12 : tree var_name = gimple_call_arg (stmt, 2);
2401 : 12 : var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
2402 : 12 : const char *var_name_str = TREE_STRING_POINTER (var_name);
2403 : :
2404 : 12 : warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
2405 : : "%qs cannot be initialized with "
2406 : : "%<-ftrivial-auto-var_init%>",
2407 : : var_name_str);
2408 : 12 : break;
2409 : : }
2410 : :
2411 : : /* Fall through. */
2412 : 185 : default:
2413 : : /* check the first "real" statement (not a decl/lexical scope/...), issue
2414 : : warning if needed. */
2415 : 185 : if (warn_switch_unreachable && !unreachable_issued)
2416 : 185 : wi->info = emit_warn_switch_unreachable (stmt);
2417 : : /* Stop when auto var init warning is not on. */
2418 : 185 : if (!warn_trivial_auto_var_init)
2419 : 179 : return integer_zero_node;
2420 : : break;
2421 : : }
2422 : : return NULL_TREE;
2423 : : }
2424 : :
2425 : :
2426 : : /* Possibly warn about unreachable statements between switch's controlling
2427 : : expression and the first case. Also warn about -ftrivial-auto-var-init
2428 : : cannot initialize the auto variable under such situation.
2429 : : SEQ is the body of a switch expression. */
2430 : :
2431 : : static void
2432 : 51843 : maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
2433 : : {
2434 : 60 : if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
2435 : : /* This warning doesn't play well with Fortran when optimizations
2436 : : are on. */
2437 : 51783 : || lang_GNU_Fortran ()
2438 : 100079 : || seq == NULL)
2439 : 3762 : return;
2440 : :
2441 : 48081 : struct walk_stmt_info wi;
2442 : :
2443 : 48081 : memset (&wi, 0, sizeof (wi));
2444 : 48081 : walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
2445 : : }
2446 : :
2447 : :
2448 : : /* A label entry that pairs label and a location. */
2449 : : struct label_entry
2450 : : {
2451 : : tree label;
2452 : : location_t loc;
2453 : : };
2454 : :
2455 : : /* Find LABEL in vector of label entries VEC. */
2456 : :
2457 : : static struct label_entry *
2458 : 11755 : find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
2459 : : {
2460 : 11755 : unsigned int i;
2461 : 11755 : struct label_entry *l;
2462 : :
2463 : 22516 : FOR_EACH_VEC_ELT (*vec, i, l)
2464 : 19243 : if (l->label == label)
2465 : : return l;
2466 : : return NULL;
2467 : : }
2468 : :
2469 : : /* Return true if LABEL, a LABEL_DECL, represents a case label
2470 : : in a vector of labels CASES. */
2471 : :
2472 : : static bool
2473 : 15196 : case_label_p (const vec<tree> *cases, tree label)
2474 : : {
2475 : 15196 : unsigned int i;
2476 : 15196 : tree l;
2477 : :
2478 : 155886 : FOR_EACH_VEC_ELT (*cases, i, l)
2479 : 155560 : if (CASE_LABEL (l) == label)
2480 : : return true;
2481 : : return false;
2482 : : }
2483 : :
2484 : : /* Find the last nondebug statement in a scope STMT. */
2485 : :
2486 : : static gimple *
2487 : 2024 : last_stmt_in_scope (gimple *stmt)
2488 : : {
2489 : 3802 : if (!stmt)
2490 : : return NULL;
2491 : :
2492 : 3791 : switch (gimple_code (stmt))
2493 : : {
2494 : 1324 : case GIMPLE_BIND:
2495 : 1324 : {
2496 : 1324 : gbind *bind = as_a <gbind *> (stmt);
2497 : 1324 : stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
2498 : 1324 : return last_stmt_in_scope (stmt);
2499 : : }
2500 : :
2501 : 617 : case GIMPLE_TRY:
2502 : 617 : {
2503 : 617 : gtry *try_stmt = as_a <gtry *> (stmt);
2504 : 617 : stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
2505 : 617 : gimple *last_eval = last_stmt_in_scope (stmt);
2506 : 617 : if (gimple_stmt_may_fallthru (last_eval)
2507 : 473 : && (last_eval == NULL
2508 : 470 : || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
2509 : 1083 : && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2510 : : {
2511 : 454 : stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
2512 : 454 : return last_stmt_in_scope (stmt);
2513 : : }
2514 : : else
2515 : : return last_eval;
2516 : : }
2517 : :
2518 : 0 : case GIMPLE_DEBUG:
2519 : 0 : gcc_unreachable ();
2520 : :
2521 : : default:
2522 : : return stmt;
2523 : : }
2524 : : }
2525 : :
2526 : : /* Collect labels that may fall through into LABELS and return the statement
2527 : : preceding another case label, or a user-defined label. Store a location
2528 : : useful to give warnings at *PREVLOC (usually the location of the returned
2529 : : statement or of its surrounding scope). */
2530 : :
2531 : : static gimple *
2532 : 19579 : collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
2533 : : auto_vec <struct label_entry> *labels,
2534 : : location_t *prevloc)
2535 : : {
2536 : 19579 : gimple *prev = NULL;
2537 : :
2538 : 19579 : *prevloc = UNKNOWN_LOCATION;
2539 : 96038 : do
2540 : : {
2541 : 96038 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
2542 : : {
2543 : : /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
2544 : : which starts on a GIMPLE_SWITCH and ends with a break label.
2545 : : Handle that as a single statement that can fall through. */
2546 : 1385 : gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
2547 : 1385 : gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
2548 : 1385 : gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
2549 : 1377 : if (last
2550 : 1377 : && gimple_code (first) == GIMPLE_SWITCH
2551 : 1479 : && gimple_code (last) == GIMPLE_LABEL)
2552 : : {
2553 : 102 : tree label = gimple_label_label (as_a <glabel *> (last));
2554 : 102 : if (SWITCH_BREAK_LABEL_P (label))
2555 : : {
2556 : 102 : prev = bind;
2557 : 102 : gsi_next (gsi_p);
2558 : 102 : continue;
2559 : : }
2560 : : }
2561 : : }
2562 : 95936 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
2563 : 95936 : || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2564 : : {
2565 : : /* Nested scope. Only look at the last statement of
2566 : : the innermost scope. */
2567 : 1407 : location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2568 : 1407 : gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2569 : 1407 : if (last)
2570 : : {
2571 : 1399 : prev = last;
2572 : : /* It might be a label without a location. Use the
2573 : : location of the scope then. */
2574 : 1399 : if (!gimple_has_location (prev))
2575 : 616 : *prevloc = bind_loc;
2576 : : }
2577 : 1407 : gsi_next (gsi_p);
2578 : 1407 : continue;
2579 : 1407 : }
2580 : :
2581 : : /* Ifs are tricky. */
2582 : 94529 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2583 : : {
2584 : 6949 : gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2585 : 6949 : tree false_lab = gimple_cond_false_label (cond_stmt);
2586 : 6949 : location_t if_loc = gimple_location (cond_stmt);
2587 : :
2588 : : /* If we have e.g.
2589 : : if (i > 1) goto <D.2259>; else goto D;
2590 : : we can't do much with the else-branch. */
2591 : 6949 : if (!DECL_ARTIFICIAL (false_lab))
2592 : : break;
2593 : :
2594 : : /* Go on until the false label, then one step back. */
2595 : 53080 : for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2596 : : {
2597 : 53080 : gimple *stmt = gsi_stmt (*gsi_p);
2598 : 53080 : if (gimple_code (stmt) == GIMPLE_LABEL
2599 : 53080 : && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2600 : : break;
2601 : : }
2602 : :
2603 : : /* Not found? Oops. */
2604 : 6949 : if (gsi_end_p (*gsi_p))
2605 : : break;
2606 : :
2607 : : /* A dead label can't fall through. */
2608 : 6949 : if (!UNUSED_LABEL_P (false_lab))
2609 : : {
2610 : 6902 : struct label_entry l = { false_lab, if_loc };
2611 : 6902 : labels->safe_push (l);
2612 : : }
2613 : :
2614 : : /* Go to the last statement of the then branch. */
2615 : 6949 : gsi_prev (gsi_p);
2616 : :
2617 : : /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2618 : : <D.1759>:
2619 : : <stmt>;
2620 : : goto <D.1761>;
2621 : : <D.1760>:
2622 : : */
2623 : 6949 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2624 : 6949 : && !gimple_has_location (gsi_stmt (*gsi_p)))
2625 : : {
2626 : : /* Look at the statement before, it might be
2627 : : attribute fallthrough, in which case don't warn. */
2628 : 1439 : gsi_prev (gsi_p);
2629 : 1439 : bool fallthru_before_dest
2630 : 1439 : = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2631 : 1439 : gsi_next (gsi_p);
2632 : 1439 : tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2633 : 1439 : if (!fallthru_before_dest)
2634 : : {
2635 : 1382 : struct label_entry l = { goto_dest, if_loc };
2636 : 1382 : labels->safe_push (l);
2637 : : }
2638 : : }
2639 : : /* This case is about
2640 : : if (1 != 0) goto <D.2022>; else goto <D.2023>;
2641 : : <D.2022>:
2642 : : n = n + 1; // #1
2643 : : <D.2023>: // #2
2644 : : <D.1988>: // #3
2645 : : where #2 is UNUSED_LABEL_P and we want to warn about #1 falling
2646 : : through to #3. So set PREV to #1. */
2647 : 5510 : else if (UNUSED_LABEL_P (false_lab))
2648 : 47 : prev = gsi_stmt (*gsi_p);
2649 : :
2650 : : /* And move back. */
2651 : 6949 : gsi_next (gsi_p);
2652 : : }
2653 : :
2654 : : /* Remember the last statement. Skip labels that are of no interest
2655 : : to us. */
2656 : 94529 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2657 : : {
2658 : 11537 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2659 : 11537 : if (find_label_entry (labels, label))
2660 : 94529 : prev = gsi_stmt (*gsi_p);
2661 : : }
2662 : 82992 : else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2663 : : ;
2664 : 82992 : else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
2665 : : ;
2666 : 81824 : else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2667 : 94529 : prev = gsi_stmt (*gsi_p);
2668 : 94529 : gsi_next (gsi_p);
2669 : : }
2670 : 96038 : while (!gsi_end_p (*gsi_p)
2671 : : /* Stop if we find a case or a user-defined label. */
2672 : 192076 : && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2673 : 20296 : || !gimple_has_location (gsi_stmt (*gsi_p))));
2674 : :
2675 : 19579 : if (prev && gimple_has_location (prev))
2676 : 19126 : *prevloc = gimple_location (prev);
2677 : 19579 : return prev;
2678 : : }
2679 : :
2680 : : /* Return true if the switch fallthough warning should occur. LABEL is
2681 : : the label statement that we're falling through to. */
2682 : :
2683 : : static bool
2684 : 15701 : should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2685 : : {
2686 : 15701 : gimple_stmt_iterator gsi = *gsi_p;
2687 : :
2688 : : /* Don't warn if the label is marked with a "falls through" comment. */
2689 : 15701 : if (FALLTHROUGH_LABEL_P (label))
2690 : : return false;
2691 : :
2692 : : /* Don't warn for non-case labels followed by a statement:
2693 : : case 0:
2694 : : foo ();
2695 : : label:
2696 : : bar ();
2697 : : as these are likely intentional. */
2698 : 14941 : if (!case_label_p (&gimplify_ctxp->case_labels, label))
2699 : : {
2700 : : tree l;
2701 : 326 : while (!gsi_end_p (gsi)
2702 : 318 : && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2703 : 255 : && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2704 : 581 : && !case_label_p (&gimplify_ctxp->case_labels, l))
2705 : 169 : gsi_next_nondebug (&gsi);
2706 : 157 : if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2707 : : return false;
2708 : : }
2709 : :
2710 : : /* Don't warn for terminated branches, i.e. when the subsequent case labels
2711 : : immediately breaks. */
2712 : 14870 : gsi = *gsi_p;
2713 : :
2714 : : /* Skip all immediately following labels. */
2715 : 33187 : while (!gsi_end_p (gsi)
2716 : 33187 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2717 : 15007 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2718 : 18317 : gsi_next_nondebug (&gsi);
2719 : :
2720 : : /* { ... something; default:; } */
2721 : 14870 : if (gsi_end_p (gsi)
2722 : : /* { ... something; default: break; } or
2723 : : { ... something; default: goto L; } */
2724 : 14806 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2725 : : /* { ... something; default: return; } */
2726 : 28593 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2727 : : return false;
2728 : :
2729 : : return true;
2730 : : }
2731 : :
2732 : : /* Callback for walk_gimple_seq. */
2733 : :
2734 : : static tree
2735 : 20433 : warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2736 : : struct walk_stmt_info *)
2737 : : {
2738 : 20433 : gimple *stmt = gsi_stmt (*gsi_p);
2739 : :
2740 : 20433 : *handled_ops_p = true;
2741 : 20433 : switch (gimple_code (stmt))
2742 : : {
2743 : 222 : case GIMPLE_TRY:
2744 : 222 : case GIMPLE_BIND:
2745 : 222 : case GIMPLE_CATCH:
2746 : 222 : case GIMPLE_EH_FILTER:
2747 : 222 : case GIMPLE_TRANSACTION:
2748 : : /* Walk the sub-statements. */
2749 : 222 : *handled_ops_p = false;
2750 : 222 : break;
2751 : :
2752 : : /* Find a sequence of form:
2753 : :
2754 : : GIMPLE_LABEL
2755 : : [...]
2756 : : <may fallthru stmt>
2757 : : GIMPLE_LABEL
2758 : :
2759 : : and possibly warn. */
2760 : : case GIMPLE_LABEL:
2761 : : {
2762 : : /* Found a label. Skip all immediately following labels. */
2763 : 46077 : while (!gsi_end_p (*gsi_p)
2764 : 46077 : && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2765 : 26311 : gsi_next_nondebug (gsi_p);
2766 : :
2767 : : /* There might be no more statements. */
2768 : 19766 : if (gsi_end_p (*gsi_p))
2769 : 4058 : return integer_zero_node;
2770 : :
2771 : : /* Vector of labels that fall through. */
2772 : 19579 : auto_vec <struct label_entry> labels;
2773 : 19579 : location_t prevloc;
2774 : 19579 : gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2775 : :
2776 : : /* There might be no more statements. */
2777 : 19579 : if (gsi_end_p (*gsi_p))
2778 : 3871 : return integer_zero_node;
2779 : :
2780 : 15708 : gimple *next = gsi_stmt (*gsi_p);
2781 : 15708 : tree label;
2782 : : /* If what follows is a label, then we may have a fallthrough. */
2783 : 15708 : if (gimple_code (next) == GIMPLE_LABEL
2784 : 15708 : && gimple_has_location (next)
2785 : 15708 : && (label = gimple_label_label (as_a <glabel *> (next)))
2786 : 31416 : && prev != NULL)
2787 : : {
2788 : 15701 : struct label_entry *l;
2789 : 15701 : bool warned_p = false;
2790 : 15701 : auto_diagnostic_group d;
2791 : 15701 : if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2792 : : /* Quiet. */;
2793 : 13696 : else if (gimple_code (prev) == GIMPLE_LABEL
2794 : 218 : && (label = gimple_label_label (as_a <glabel *> (prev)))
2795 : 13914 : && (l = find_label_entry (&labels, label)))
2796 : 198 : warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2797 : : "this statement may fall through");
2798 : 13498 : else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2799 : : /* Try to be clever and don't warn when the statement
2800 : : can't actually fall through. */
2801 : 13101 : && gimple_stmt_may_fallthru (prev)
2802 : 13791 : && prevloc != UNKNOWN_LOCATION)
2803 : 293 : warned_p = warning_at (prevloc,
2804 : 293 : OPT_Wimplicit_fallthrough_,
2805 : : "this statement may fall through");
2806 : 491 : if (warned_p)
2807 : 491 : inform (gimple_location (next), "here");
2808 : :
2809 : : /* Mark this label as processed so as to prevent multiple
2810 : : warnings in nested switches. */
2811 : 15701 : FALLTHROUGH_LABEL_P (label) = true;
2812 : :
2813 : : /* So that next warn_implicit_fallthrough_r will start looking for
2814 : : a new sequence starting with this label. */
2815 : 15701 : gsi_prev (gsi_p);
2816 : 15701 : }
2817 : 3871 : }
2818 : 15708 : break;
2819 : : default:
2820 : : break;
2821 : : }
2822 : : return NULL_TREE;
2823 : : }
2824 : :
2825 : : /* Warn when a switch case falls through. */
2826 : :
2827 : : static void
2828 : 51843 : maybe_warn_implicit_fallthrough (gimple_seq seq)
2829 : : {
2830 : 51843 : if (!warn_implicit_fallthrough)
2831 : 47881 : return;
2832 : :
2833 : : /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2834 : 3962 : if (!(lang_GNU_C ()
2835 : 1059 : || lang_GNU_CXX ()
2836 : 0 : || lang_GNU_OBJC ()))
2837 : : return;
2838 : :
2839 : 3962 : struct walk_stmt_info wi;
2840 : 3962 : memset (&wi, 0, sizeof (wi));
2841 : 3962 : walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2842 : : }
2843 : :
2844 : : /* Callback for walk_gimple_seq. */
2845 : :
2846 : : static tree
2847 : 3362727 : expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2848 : : struct walk_stmt_info *wi)
2849 : : {
2850 : 3362727 : gimple *stmt = gsi_stmt (*gsi_p);
2851 : :
2852 : 3362727 : *handled_ops_p = true;
2853 : 3362727 : switch (gimple_code (stmt))
2854 : : {
2855 : 155241 : case GIMPLE_TRY:
2856 : 155241 : case GIMPLE_BIND:
2857 : 155241 : case GIMPLE_CATCH:
2858 : 155241 : case GIMPLE_EH_FILTER:
2859 : 155241 : case GIMPLE_TRANSACTION:
2860 : : /* Walk the sub-statements. */
2861 : 155241 : *handled_ops_p = false;
2862 : 155241 : break;
2863 : 269977 : case GIMPLE_CALL:
2864 : 269977 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2865 : 269977 : if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2866 : : {
2867 : 5403 : location_t loc = gimple_location (stmt);
2868 : 5403 : gsi_remove (gsi_p, true);
2869 : 5403 : wi->removed_stmt = true;
2870 : :
2871 : : /* nothrow flag is added by genericize_c_loop to mark fallthrough
2872 : : statement at the end of some loop's body. Those should be
2873 : : always diagnosed, either because they indeed don't precede
2874 : : a case label or default label, or because the next statement
2875 : : is not within the same iteration statement. */
2876 : 5403 : if ((stmt->subcode & GF_CALL_NOTHROW) != 0)
2877 : : {
2878 : 12 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2879 : : "a case label or default label");
2880 : 12 : break;
2881 : : }
2882 : :
2883 : 5391 : if (gsi_end_p (*gsi_p))
2884 : : {
2885 : 32 : static_cast<location_t *>(wi->info)[0] = BUILTINS_LOCATION;
2886 : 32 : static_cast<location_t *>(wi->info)[1] = loc;
2887 : 32 : break;
2888 : : }
2889 : :
2890 : 5359 : bool found = false;
2891 : :
2892 : 5359 : gimple_stmt_iterator gsi2 = *gsi_p;
2893 : 5359 : stmt = gsi_stmt (gsi2);
2894 : 5359 : if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2895 : : {
2896 : : /* Go on until the artificial label. */
2897 : 57 : tree goto_dest = gimple_goto_dest (stmt);
2898 : 414 : for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2899 : : {
2900 : 357 : if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2901 : 357 : && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2902 : : == goto_dest)
2903 : : break;
2904 : : }
2905 : :
2906 : : /* Not found? Stop. */
2907 : 57 : if (gsi_end_p (gsi2))
2908 : : break;
2909 : :
2910 : : /* Look one past it. */
2911 : 57 : gsi_next (&gsi2);
2912 : : }
2913 : :
2914 : : /* We're looking for a case label or default label here. */
2915 : 5457 : while (!gsi_end_p (gsi2))
2916 : : {
2917 : 5457 : stmt = gsi_stmt (gsi2);
2918 : 5457 : if (gimple_code (stmt) == GIMPLE_LABEL)
2919 : : {
2920 : 5414 : tree label = gimple_label_label (as_a <glabel *> (stmt));
2921 : 5414 : if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2922 : : {
2923 : : found = true;
2924 : : break;
2925 : : }
2926 : : }
2927 : 43 : else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2928 : : ;
2929 : 43 : else if (!is_gimple_debug (stmt))
2930 : : /* Anything else is not expected. */
2931 : : break;
2932 : 98 : gsi_next (&gsi2);
2933 : : }
2934 : 5359 : if (!found)
2935 : 23 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2936 : : "a case label or default label");
2937 : : }
2938 : : break;
2939 : 2937509 : default:
2940 : 2937509 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2941 : 2937509 : break;
2942 : : }
2943 : 3362727 : return NULL_TREE;
2944 : : }
2945 : :
2946 : : /* Expand all FALLTHROUGH () calls in SEQ. */
2947 : :
2948 : : static void
2949 : 50519 : expand_FALLTHROUGH (gimple_seq *seq_p)
2950 : : {
2951 : 50519 : auto_urlify_attributes sentinel;
2952 : :
2953 : 50519 : struct walk_stmt_info wi;
2954 : 50519 : location_t loc[2];
2955 : 50519 : memset (&wi, 0, sizeof (wi));
2956 : 50519 : loc[0] = UNKNOWN_LOCATION;
2957 : 50519 : loc[1] = UNKNOWN_LOCATION;
2958 : 50519 : wi.info = (void *) &loc[0];
2959 : 50519 : walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2960 : 50519 : if (loc[0] != UNKNOWN_LOCATION)
2961 : : /* We've found [[fallthrough]]; at the end of a switch, which the C++
2962 : : standard says is ill-formed; see [dcl.attr.fallthrough]. */
2963 : 8 : pedwarn (loc[1], 0, "attribute %<fallthrough%> not preceding "
2964 : : "a case label or default label");
2965 : 50519 : }
2966 : :
2967 : :
2968 : : /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2969 : : branch to. */
2970 : :
2971 : : static enum gimplify_status
2972 : 51843 : gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2973 : : {
2974 : 51843 : tree switch_expr = *expr_p;
2975 : 51843 : gimple_seq switch_body_seq = NULL;
2976 : 51843 : enum gimplify_status ret;
2977 : 51843 : tree index_type = TREE_TYPE (switch_expr);
2978 : 51843 : if (index_type == NULL_TREE)
2979 : 11293 : index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2980 : :
2981 : 51843 : ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2982 : : fb_rvalue);
2983 : 51843 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
2984 : : return ret;
2985 : :
2986 : 51843 : if (SWITCH_BODY (switch_expr))
2987 : : {
2988 : 51843 : vec<tree> labels;
2989 : 51843 : vec<tree> saved_labels;
2990 : 51843 : hash_set<tree> *saved_live_switch_vars = NULL;
2991 : 51843 : tree default_case = NULL_TREE;
2992 : 51843 : gswitch *switch_stmt;
2993 : :
2994 : : /* Save old labels, get new ones from body, then restore the old
2995 : : labels. Save all the things from the switch body to append after. */
2996 : 51843 : saved_labels = gimplify_ctxp->case_labels;
2997 : 51843 : gimplify_ctxp->case_labels.create (8);
2998 : :
2999 : : /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
3000 : 51843 : saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
3001 : 51843 : tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
3002 : 51843 : if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
3003 : 51575 : gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
3004 : : else
3005 : 268 : gimplify_ctxp->live_switch_vars = NULL;
3006 : :
3007 : 51843 : bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
3008 : 51843 : gimplify_ctxp->in_switch_expr = true;
3009 : :
3010 : 51843 : gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
3011 : :
3012 : 51843 : gimplify_ctxp->in_switch_expr = old_in_switch_expr;
3013 : 51843 : maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
3014 : 51843 : maybe_warn_implicit_fallthrough (switch_body_seq);
3015 : : /* Only do this for the outermost GIMPLE_SWITCH. */
3016 : 51843 : if (!gimplify_ctxp->in_switch_expr)
3017 : 50519 : expand_FALLTHROUGH (&switch_body_seq);
3018 : :
3019 : 51843 : labels = gimplify_ctxp->case_labels;
3020 : 51843 : gimplify_ctxp->case_labels = saved_labels;
3021 : :
3022 : 51843 : if (gimplify_ctxp->live_switch_vars)
3023 : : {
3024 : 51575 : gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
3025 : 51575 : delete gimplify_ctxp->live_switch_vars;
3026 : : }
3027 : 51843 : gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
3028 : :
3029 : 51843 : preprocess_case_label_vec_for_gimple (labels, index_type,
3030 : : &default_case);
3031 : :
3032 : 51843 : bool add_bind = false;
3033 : 51843 : if (!default_case)
3034 : : {
3035 : 14619 : glabel *new_default;
3036 : :
3037 : 14619 : default_case
3038 : 14619 : = build_case_label (NULL_TREE, NULL_TREE,
3039 : : create_artificial_label (UNKNOWN_LOCATION));
3040 : 14619 : if (old_in_switch_expr)
3041 : : {
3042 : 765 : SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
3043 : 765 : add_bind = true;
3044 : : }
3045 : 14619 : new_default = gimple_build_label (CASE_LABEL (default_case));
3046 : 14619 : gimplify_seq_add_stmt (&switch_body_seq, new_default);
3047 : : }
3048 : 37224 : else if (old_in_switch_expr)
3049 : : {
3050 : 559 : gimple *last = gimple_seq_last_stmt (switch_body_seq);
3051 : 559 : if (last && gimple_code (last) == GIMPLE_LABEL)
3052 : : {
3053 : 302 : tree label = gimple_label_label (as_a <glabel *> (last));
3054 : 302 : if (SWITCH_BREAK_LABEL_P (label))
3055 : 51843 : add_bind = true;
3056 : : }
3057 : : }
3058 : :
3059 : 51843 : switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
3060 : : default_case, labels);
3061 : 51843 : gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
3062 : : /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
3063 : : ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
3064 : : wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
3065 : : so that we can easily find the start and end of the switch
3066 : : statement. */
3067 : 51843 : if (add_bind)
3068 : : {
3069 : 1066 : gimple_seq bind_body = NULL;
3070 : 1066 : gimplify_seq_add_stmt (&bind_body, switch_stmt);
3071 : 1066 : gimple_seq_add_seq (&bind_body, switch_body_seq);
3072 : 1066 : gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
3073 : 1066 : gimple_set_location (bind, EXPR_LOCATION (switch_expr));
3074 : 1066 : gimplify_seq_add_stmt (pre_p, bind);
3075 : : }
3076 : : else
3077 : : {
3078 : 50777 : gimplify_seq_add_stmt (pre_p, switch_stmt);
3079 : 50777 : gimplify_seq_add_seq (pre_p, switch_body_seq);
3080 : : }
3081 : 51843 : labels.release ();
3082 : : }
3083 : : else
3084 : 0 : gcc_unreachable ();
3085 : :
3086 : 51843 : return GS_ALL_DONE;
3087 : : }
3088 : :
3089 : : /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
3090 : :
3091 : : static enum gimplify_status
3092 : 2510213 : gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
3093 : : {
3094 : 2510213 : gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3095 : : == current_function_decl);
3096 : :
3097 : 2510213 : tree label = LABEL_EXPR_LABEL (*expr_p);
3098 : 2510213 : glabel *label_stmt = gimple_build_label (label);
3099 : 2510213 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3100 : 2510213 : gimplify_seq_add_stmt (pre_p, label_stmt);
3101 : :
3102 : 2510213 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3103 : 21 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3104 : : NOT_TAKEN));
3105 : 2510192 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3106 : 9 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3107 : : TAKEN));
3108 : :
3109 : 2510213 : return GS_ALL_DONE;
3110 : : }
3111 : :
3112 : : /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
3113 : :
3114 : : static enum gimplify_status
3115 : 1061903 : gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
3116 : : {
3117 : 1061903 : struct gimplify_ctx *ctxp;
3118 : 1061903 : glabel *label_stmt;
3119 : :
3120 : : /* Invalid programs can play Duff's Device type games with, for example,
3121 : : #pragma omp parallel. At least in the C front end, we don't
3122 : : detect such invalid branches until after gimplification, in the
3123 : : diagnose_omp_blocks pass. */
3124 : 1061913 : for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
3125 : 1061913 : if (ctxp->case_labels.exists ())
3126 : : break;
3127 : :
3128 : 1061903 : tree label = CASE_LABEL (*expr_p);
3129 : 1061903 : label_stmt = gimple_build_label (label);
3130 : 1061903 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3131 : 1061903 : ctxp->case_labels.safe_push (*expr_p);
3132 : 1061903 : gimplify_seq_add_stmt (pre_p, label_stmt);
3133 : :
3134 : 1061903 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3135 : 16 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3136 : : NOT_TAKEN));
3137 : 1061887 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3138 : 20 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3139 : : TAKEN));
3140 : :
3141 : 1061903 : return GS_ALL_DONE;
3142 : : }
3143 : :
3144 : : /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
3145 : : if necessary. */
3146 : :
3147 : : tree
3148 : 1797641 : build_and_jump (tree *label_p)
3149 : : {
3150 : 1797641 : if (label_p == NULL)
3151 : : /* If there's nowhere to jump, just fall through. */
3152 : : return NULL_TREE;
3153 : :
3154 : 1218053 : if (*label_p == NULL_TREE)
3155 : : {
3156 : 698706 : tree label = create_artificial_label (UNKNOWN_LOCATION);
3157 : 698706 : *label_p = label;
3158 : : }
3159 : :
3160 : 1218053 : return build1 (GOTO_EXPR, void_type_node, *label_p);
3161 : : }
3162 : :
3163 : : /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
3164 : : This also involves building a label to jump to and communicating it to
3165 : : gimplify_loop_expr through gimplify_ctxp->exit_label. */
3166 : :
3167 : : static enum gimplify_status
3168 : 3095 : gimplify_exit_expr (tree *expr_p)
3169 : : {
3170 : 3095 : tree cond = TREE_OPERAND (*expr_p, 0);
3171 : 3095 : tree expr;
3172 : :
3173 : 3095 : expr = build_and_jump (&gimplify_ctxp->exit_label);
3174 : 3095 : expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
3175 : 3095 : *expr_p = expr;
3176 : :
3177 : 3095 : return GS_OK;
3178 : : }
3179 : :
3180 : : /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
3181 : : different from its canonical type, wrap the whole thing inside a
3182 : : NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
3183 : : type.
3184 : :
3185 : : The canonical type of a COMPONENT_REF is the type of the field being
3186 : : referenced--unless the field is a bit-field which can be read directly
3187 : : in a smaller mode, in which case the canonical type is the
3188 : : sign-appropriate type corresponding to that mode. */
3189 : :
3190 : : static void
3191 : 18351721 : canonicalize_component_ref (tree *expr_p)
3192 : : {
3193 : 18351721 : tree expr = *expr_p;
3194 : 18351721 : tree type;
3195 : :
3196 : 18351721 : gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
3197 : :
3198 : 18351721 : if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
3199 : 7362439 : type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
3200 : : else
3201 : 10989282 : type = TREE_TYPE (TREE_OPERAND (expr, 1));
3202 : :
3203 : : /* One could argue that all the stuff below is not necessary for
3204 : : the non-bitfield case and declare it a FE error if type
3205 : : adjustment would be needed. */
3206 : 18351721 : if (TREE_TYPE (expr) != type)
3207 : : {
3208 : : #ifdef ENABLE_TYPES_CHECKING
3209 : 863779 : tree old_type = TREE_TYPE (expr);
3210 : : #endif
3211 : 863779 : int type_quals;
3212 : :
3213 : : /* We need to preserve qualifiers and propagate them from
3214 : : operand 0. */
3215 : 863779 : type_quals = TYPE_QUALS (type)
3216 : 863779 : | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
3217 : 863779 : if (TYPE_QUALS (type) != type_quals)
3218 : 863542 : type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
3219 : :
3220 : : /* Set the type of the COMPONENT_REF to the underlying type. */
3221 : 863779 : TREE_TYPE (expr) = type;
3222 : :
3223 : : #ifdef ENABLE_TYPES_CHECKING
3224 : : /* It is now a FE error, if the conversion from the canonical
3225 : : type to the original expression type is not useless. */
3226 : 863779 : gcc_assert (useless_type_conversion_p (old_type, type));
3227 : : #endif
3228 : : }
3229 : 18351721 : }
3230 : :
3231 : : /* If a NOP conversion is changing a pointer to array of foo to a pointer
3232 : : to foo, embed that change in the ADDR_EXPR by converting
3233 : : T array[U];
3234 : : (T *)&array
3235 : : ==>
3236 : : &array[L]
3237 : : where L is the lower bound. For simplicity, only do this for constant
3238 : : lower bound.
3239 : : The constraint is that the type of &array[L] is trivially convertible
3240 : : to T *. */
3241 : :
3242 : : static void
3243 : 517961 : canonicalize_addr_expr (tree *expr_p)
3244 : : {
3245 : 517961 : tree expr = *expr_p;
3246 : 517961 : tree addr_expr = TREE_OPERAND (expr, 0);
3247 : 517961 : tree datype, ddatype, pddatype;
3248 : :
3249 : : /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
3250 : 1035576 : if (!POINTER_TYPE_P (TREE_TYPE (expr))
3251 : 517961 : || TREE_CODE (addr_expr) != ADDR_EXPR)
3252 : : return;
3253 : :
3254 : : /* The addr_expr type should be a pointer to an array. */
3255 : 346 : datype = TREE_TYPE (TREE_TYPE (addr_expr));
3256 : 346 : if (TREE_CODE (datype) != ARRAY_TYPE)
3257 : : return;
3258 : :
3259 : : /* The pointer to element type shall be trivially convertible to
3260 : : the expression pointer type. */
3261 : 28 : ddatype = TREE_TYPE (datype);
3262 : 28 : pddatype = build_pointer_type (ddatype);
3263 : 28 : if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
3264 : : pddatype))
3265 : : return;
3266 : :
3267 : : /* The lower bound and element sizes must be constant. */
3268 : 0 : if (!TYPE_SIZE_UNIT (ddatype)
3269 : 0 : || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
3270 : 0 : || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
3271 : 0 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
3272 : : return;
3273 : :
3274 : : /* All checks succeeded. Build a new node to merge the cast. */
3275 : 0 : *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
3276 : 0 : TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
3277 : : NULL_TREE, NULL_TREE);
3278 : 0 : *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
3279 : :
3280 : : /* We can have stripped a required restrict qualifier above. */
3281 : 0 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
3282 : 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
3283 : : }
3284 : :
3285 : : /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
3286 : : underneath as appropriate. */
3287 : :
3288 : : static enum gimplify_status
3289 : 12565206 : gimplify_conversion (tree *expr_p)
3290 : : {
3291 : 12565206 : location_t loc = EXPR_LOCATION (*expr_p);
3292 : 12565206 : gcc_assert (CONVERT_EXPR_P (*expr_p));
3293 : :
3294 : : /* Then strip away all but the outermost conversion. */
3295 : 12565206 : STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
3296 : :
3297 : : /* And remove the outermost conversion if it's useless. */
3298 : 12565206 : if (tree_ssa_useless_type_conversion (*expr_p))
3299 : 0 : *expr_p = TREE_OPERAND (*expr_p, 0);
3300 : :
3301 : : /* If we still have a conversion at the toplevel,
3302 : : then canonicalize some constructs. */
3303 : 12565206 : if (CONVERT_EXPR_P (*expr_p))
3304 : : {
3305 : 12565206 : tree sub = TREE_OPERAND (*expr_p, 0);
3306 : :
3307 : : /* If a NOP conversion is changing the type of a COMPONENT_REF
3308 : : expression, then canonicalize its type now in order to expose more
3309 : : redundant conversions. */
3310 : 12565206 : if (TREE_CODE (sub) == COMPONENT_REF)
3311 : 267170 : canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
3312 : :
3313 : : /* If a NOP conversion is changing a pointer to array of foo
3314 : : to a pointer to foo, embed that change in the ADDR_EXPR. */
3315 : 12298036 : else if (TREE_CODE (sub) == ADDR_EXPR)
3316 : 517961 : canonicalize_addr_expr (expr_p);
3317 : : }
3318 : :
3319 : : /* If we have a conversion to a non-register type force the
3320 : : use of a VIEW_CONVERT_EXPR instead. */
3321 : 12565206 : if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
3322 : 90 : *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
3323 : 90 : TREE_OPERAND (*expr_p, 0));
3324 : :
3325 : : /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
3326 : 12565206 : if (TREE_CODE (*expr_p) == CONVERT_EXPR)
3327 : 225706 : TREE_SET_CODE (*expr_p, NOP_EXPR);
3328 : :
3329 : 12565206 : return GS_OK;
3330 : : }
3331 : :
3332 : : /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
3333 : : DECL_VALUE_EXPR, and it's worth re-examining things. */
3334 : :
3335 : : static enum gimplify_status
3336 : 125837402 : gimplify_var_or_parm_decl (tree *expr_p)
3337 : : {
3338 : 125837402 : tree decl = *expr_p;
3339 : :
3340 : : /* ??? If this is a local variable, and it has not been seen in any
3341 : : outer BIND_EXPR, then it's probably the result of a duplicate
3342 : : declaration, for which we've already issued an error. It would
3343 : : be really nice if the front end wouldn't leak these at all.
3344 : : Currently the only known culprit is C++ destructors, as seen
3345 : : in g++.old-deja/g++.jason/binding.C.
3346 : : Another possible culpit are size expressions for variably modified
3347 : : types which are lost in the FE or not gimplified correctly. */
3348 : 125837402 : if (VAR_P (decl)
3349 : 97406240 : && !DECL_SEEN_IN_BIND_EXPR_P (decl)
3350 : 16985860 : && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
3351 : 125892633 : && decl_function_context (decl) == current_function_decl)
3352 : : {
3353 : 66 : gcc_assert (seen_error ());
3354 : : return GS_ERROR;
3355 : : }
3356 : :
3357 : : /* When within an OMP context, notice uses of variables. */
3358 : 125837336 : if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
3359 : : return GS_ALL_DONE;
3360 : :
3361 : : /* If the decl is an alias for another expression, substitute it now. */
3362 : 125831561 : if (DECL_HAS_VALUE_EXPR_P (decl))
3363 : : {
3364 : 436514 : *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
3365 : 436514 : return GS_OK;
3366 : : }
3367 : :
3368 : : return GS_ALL_DONE;
3369 : : }
3370 : :
3371 : : /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
3372 : :
3373 : : static void
3374 : 109822279 : recalculate_side_effects (tree t)
3375 : : {
3376 : 109822279 : enum tree_code code = TREE_CODE (t);
3377 : 109822279 : int len = TREE_OPERAND_LENGTH (t);
3378 : 109822279 : int i;
3379 : :
3380 : 109822279 : switch (TREE_CODE_CLASS (code))
3381 : : {
3382 : 661804 : case tcc_expression:
3383 : 661804 : switch (code)
3384 : : {
3385 : : case INIT_EXPR:
3386 : : case MODIFY_EXPR:
3387 : : case VA_ARG_EXPR:
3388 : : case PREDECREMENT_EXPR:
3389 : : case PREINCREMENT_EXPR:
3390 : : case POSTDECREMENT_EXPR:
3391 : : case POSTINCREMENT_EXPR:
3392 : : /* All of these have side-effects, no matter what their
3393 : : operands are. */
3394 : : return;
3395 : :
3396 : : default:
3397 : : break;
3398 : : }
3399 : : /* Fall through. */
3400 : :
3401 : 109822277 : case tcc_comparison: /* a comparison expression */
3402 : 109822277 : case tcc_unary: /* a unary arithmetic expression */
3403 : 109822277 : case tcc_binary: /* a binary arithmetic expression */
3404 : 109822277 : case tcc_reference: /* a reference */
3405 : 109822277 : case tcc_vl_exp: /* a function call */
3406 : 109822277 : TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3407 : 356998407 : for (i = 0; i < len; ++i)
3408 : : {
3409 : 247176130 : tree op = TREE_OPERAND (t, i);
3410 : 247176130 : if (op && TREE_SIDE_EFFECTS (op))
3411 : 1288599 : TREE_SIDE_EFFECTS (t) = 1;
3412 : : }
3413 : : break;
3414 : :
3415 : : case tcc_constant:
3416 : : /* No side-effects. */
3417 : : return;
3418 : :
3419 : 2 : default:
3420 : 2 : if (code == SSA_NAME)
3421 : : /* No side-effects. */
3422 : : return;
3423 : 0 : gcc_unreachable ();
3424 : : }
3425 : : }
3426 : :
3427 : : /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
3428 : : node *EXPR_P.
3429 : :
3430 : : compound_lval
3431 : : : min_lval '[' val ']'
3432 : : | min_lval '.' ID
3433 : : | compound_lval '[' val ']'
3434 : : | compound_lval '.' ID
3435 : :
3436 : : This is not part of the original SIMPLE definition, which separates
3437 : : array and member references, but it seems reasonable to handle them
3438 : : together. Also, this way we don't run into problems with union
3439 : : aliasing; gcc requires that for accesses through a union to alias, the
3440 : : union reference must be explicit, which was not always the case when we
3441 : : were splitting up array and member refs.
3442 : :
3443 : : PRE_P points to the sequence where side effects that must happen before
3444 : : *EXPR_P should be stored.
3445 : :
3446 : : POST_P points to the sequence where side effects that must happen after
3447 : : *EXPR_P should be stored. */
3448 : :
3449 : : static enum gimplify_status
3450 : 29566230 : gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3451 : : fallback_t fallback)
3452 : : {
3453 : 29566230 : tree *p;
3454 : 29566230 : enum gimplify_status ret = GS_ALL_DONE, tret;
3455 : 29566230 : int i;
3456 : 29566230 : location_t loc = EXPR_LOCATION (*expr_p);
3457 : 29566230 : tree expr = *expr_p;
3458 : :
3459 : : /* Create a stack of the subexpressions so later we can walk them in
3460 : : order from inner to outer. */
3461 : 29566230 : auto_vec<tree, 10> expr_stack;
3462 : :
3463 : : /* We can handle anything that get_inner_reference can deal with. */
3464 : 71181614 : for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
3465 : : {
3466 : 41615384 : restart:
3467 : : /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
3468 : 71240889 : if (TREE_CODE (*p) == INDIRECT_REF)
3469 : 3957741 : *p = fold_indirect_ref_loc (loc, *p);
3470 : :
3471 : 71240889 : if (handled_component_p (*p))
3472 : : ;
3473 : : /* Expand DECL_VALUE_EXPR now. In some cases that may expose
3474 : : additional COMPONENT_REFs. */
3475 : 13192699 : else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
3476 : 31047778 : && gimplify_var_or_parm_decl (p) == GS_OK)
3477 : 59275 : goto restart;
3478 : : else
3479 : : break;
3480 : :
3481 : 41615384 : expr_stack.safe_push (*p);
3482 : : }
3483 : :
3484 : 29566230 : gcc_assert (expr_stack.length ());
3485 : :
3486 : : /* Now EXPR_STACK is a stack of pointers to all the refs we've
3487 : : walked through and P points to the innermost expression.
3488 : :
3489 : : Java requires that we elaborated nodes in source order. That
3490 : : means we must gimplify the inner expression followed by each of
3491 : : the indices, in order. But we can't gimplify the inner
3492 : : expression until we deal with any variable bounds, sizes, or
3493 : : positions in order to deal with PLACEHOLDER_EXPRs.
3494 : :
3495 : : The base expression may contain a statement expression that
3496 : : has declarations used in size expressions, so has to be
3497 : : gimplified before gimplifying the size expressions.
3498 : :
3499 : : So we do this in three steps. First we deal with variable
3500 : : bounds, sizes, and positions, then we gimplify the base and
3501 : : ensure it is memory if needed, then we deal with the annotations
3502 : : for any variables in the components and any indices, from left
3503 : : to right. */
3504 : :
3505 : 29566230 : bool need_non_reg = false;
3506 : 71181611 : for (i = expr_stack.length () - 1; i >= 0; i--)
3507 : : {
3508 : 41615384 : tree t = expr_stack[i];
3509 : :
3510 : 41615384 : if (error_operand_p (TREE_OPERAND (t, 0)))
3511 : : return GS_ERROR;
3512 : :
3513 : 41615381 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3514 : : {
3515 : : /* Deal with the low bound and element type size and put them into
3516 : : the ARRAY_REF. If these values are set, they have already been
3517 : : gimplified. */
3518 : 8597535 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3519 : : {
3520 : 8546450 : tree low = unshare_expr (array_ref_low_bound (t));
3521 : 8546450 : if (!is_gimple_min_invariant (low))
3522 : : {
3523 : 0 : TREE_OPERAND (t, 2) = low;
3524 : : }
3525 : : }
3526 : :
3527 : 8597535 : if (TREE_OPERAND (t, 3) == NULL_TREE)
3528 : : {
3529 : 8580460 : tree elmt_size = array_ref_element_size (t);
3530 : 8580460 : if (!is_gimple_min_invariant (elmt_size))
3531 : : {
3532 : 8428 : elmt_size = unshare_expr (elmt_size);
3533 : 8428 : tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
3534 : 8428 : tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
3535 : :
3536 : : /* Divide the element size by the alignment of the element
3537 : : type (above). */
3538 : 8428 : elmt_size = size_binop_loc (loc, EXACT_DIV_EXPR,
3539 : : elmt_size, factor);
3540 : :
3541 : 8428 : TREE_OPERAND (t, 3) = elmt_size;
3542 : : }
3543 : : }
3544 : : need_non_reg = true;
3545 : : }
3546 : 33017846 : else if (TREE_CODE (t) == COMPONENT_REF)
3547 : : {
3548 : : /* Set the field offset into T and gimplify it. */
3549 : 31735914 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3550 : : {
3551 : 31735535 : tree offset = component_ref_field_offset (t);
3552 : 31735535 : if (!is_gimple_min_invariant (offset))
3553 : : {
3554 : 287 : offset = unshare_expr (offset);
3555 : 287 : tree field = TREE_OPERAND (t, 1);
3556 : 287 : tree factor
3557 : 287 : = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
3558 : :
3559 : : /* Divide the offset by its alignment. */
3560 : 287 : offset = size_binop_loc (loc, EXACT_DIV_EXPR,
3561 : : offset, factor);
3562 : :
3563 : 287 : TREE_OPERAND (t, 2) = offset;
3564 : : }
3565 : : }
3566 : : need_non_reg = true;
3567 : : }
3568 : 1281932 : else if (!is_gimple_reg_type (TREE_TYPE (t)))
3569 : : /* When the result of an operation, in particular a VIEW_CONVERT_EXPR
3570 : : is a non-register type then require the base object to be a
3571 : : non-register as well. */
3572 : 40608410 : need_non_reg = true;
3573 : : }
3574 : :
3575 : : /* Step 2 is to gimplify the base expression. Make sure lvalue is set
3576 : : so as to match the min_lval predicate. Failure to do so may result
3577 : : in the creation of large aggregate temporaries. */
3578 : 29566227 : tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3579 : : fallback | fb_lvalue);
3580 : 29566227 : ret = MIN (ret, tret);
3581 : 29566227 : if (ret == GS_ERROR)
3582 : : return GS_ERROR;
3583 : :
3584 : : /* Step 2a: if we have component references we do not support on
3585 : : registers then make sure the base isn't a register. Of course
3586 : : we can only do so if an rvalue is OK. */
3587 : 29566227 : if (need_non_reg && (fallback & fb_rvalue))
3588 : 22084204 : prepare_gimple_addressable (p, pre_p);
3589 : :
3590 : :
3591 : : /* Step 3: gimplify size expressions and the indices and operands of
3592 : : ARRAY_REF. During this loop we also remove any useless conversions.
3593 : : If we operate on a register also make sure to properly gimplify
3594 : : to individual operations. */
3595 : :
3596 : 29566227 : bool reg_operations = is_gimple_reg (*p);
3597 : 71181608 : for (; expr_stack.length () > 0; )
3598 : : {
3599 : 41615381 : tree t = expr_stack.pop ();
3600 : :
3601 : 41615381 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3602 : : {
3603 : 8597535 : gcc_assert (!reg_operations);
3604 : :
3605 : : /* Gimplify the low bound and element type size. */
3606 : 8597535 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3607 : : is_gimple_reg, fb_rvalue);
3608 : 8597535 : ret = MIN (ret, tret);
3609 : :
3610 : 8597535 : tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
3611 : : is_gimple_reg, fb_rvalue);
3612 : 8597535 : ret = MIN (ret, tret);
3613 : :
3614 : : /* Gimplify the dimension. */
3615 : 8597535 : tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3616 : : is_gimple_val, fb_rvalue);
3617 : 8597535 : ret = MIN (ret, tret);
3618 : : }
3619 : 33017846 : else if (TREE_CODE (t) == COMPONENT_REF)
3620 : : {
3621 : 31735914 : gcc_assert (!reg_operations);
3622 : :
3623 : 31735914 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3624 : : is_gimple_reg, fb_rvalue);
3625 : 31735914 : ret = MIN (ret, tret);
3626 : : }
3627 : 1281932 : else if (reg_operations)
3628 : : {
3629 : 750618 : tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
3630 : : is_gimple_val, fb_rvalue);
3631 : 1281932 : ret = MIN (ret, tret);
3632 : : }
3633 : :
3634 : 41615381 : STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
3635 : :
3636 : : /* The innermost expression P may have originally had
3637 : : TREE_SIDE_EFFECTS set which would have caused all the outer
3638 : : expressions in *EXPR_P leading to P to also have had
3639 : : TREE_SIDE_EFFECTS set. */
3640 : 41615381 : recalculate_side_effects (t);
3641 : : }
3642 : :
3643 : : /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3644 : 29566227 : if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3645 : : {
3646 : 18084551 : canonicalize_component_ref (expr_p);
3647 : : }
3648 : :
3649 : 29566227 : expr_stack.release ();
3650 : :
3651 : 29566227 : gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3652 : :
3653 : : return ret;
3654 : 29566230 : }
3655 : :
3656 : : /* Gimplify the self modifying expression pointed to by EXPR_P
3657 : : (++, --, +=, -=).
3658 : :
3659 : : PRE_P points to the list where side effects that must happen before
3660 : : *EXPR_P should be stored.
3661 : :
3662 : : POST_P points to the list where side effects that must happen after
3663 : : *EXPR_P should be stored.
3664 : :
3665 : : WANT_VALUE is nonzero iff we want to use the value of this expression
3666 : : in another expression.
3667 : :
3668 : : ARITH_TYPE is the type the computation should be performed in. */
3669 : :
3670 : : enum gimplify_status
3671 : 1139746 : gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3672 : : bool want_value, tree arith_type)
3673 : : {
3674 : 1139746 : enum tree_code code;
3675 : 1139746 : tree lhs, lvalue, rhs, t1;
3676 : 1139746 : gimple_seq post = NULL, *orig_post_p = post_p;
3677 : 1139746 : bool postfix;
3678 : 1139746 : enum tree_code arith_code;
3679 : 1139746 : enum gimplify_status ret;
3680 : 1139746 : location_t loc = EXPR_LOCATION (*expr_p);
3681 : :
3682 : 1139746 : code = TREE_CODE (*expr_p);
3683 : :
3684 : 1139746 : gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3685 : : || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3686 : :
3687 : : /* Prefix or postfix? */
3688 : 1139746 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3689 : : /* Faster to treat as prefix if result is not used. */
3690 : : postfix = want_value;
3691 : : else
3692 : : postfix = false;
3693 : :
3694 : : /* For postfix, make sure the inner expression's post side effects
3695 : : are executed after side effects from this expression. */
3696 : 397202 : if (postfix)
3697 : 1139746 : post_p = &post;
3698 : :
3699 : : /* Add or subtract? */
3700 : 1139746 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3701 : : arith_code = PLUS_EXPR;
3702 : : else
3703 : 49689 : arith_code = MINUS_EXPR;
3704 : :
3705 : : /* Gimplify the LHS into a GIMPLE lvalue. */
3706 : 1139746 : lvalue = TREE_OPERAND (*expr_p, 0);
3707 : 1139746 : ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3708 : 1139746 : if (ret == GS_ERROR)
3709 : : return ret;
3710 : :
3711 : : /* Extract the operands to the arithmetic operation. */
3712 : 1139744 : lhs = lvalue;
3713 : 1139744 : rhs = TREE_OPERAND (*expr_p, 1);
3714 : :
3715 : : /* For postfix operator, we evaluate the LHS to an rvalue and then use
3716 : : that as the result value and in the postqueue operation. */
3717 : 1139744 : if (postfix)
3718 : : {
3719 : 190624 : ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3720 : 190624 : if (ret == GS_ERROR)
3721 : : return ret;
3722 : :
3723 : 190624 : lhs = get_initialized_tmp_var (lhs, pre_p);
3724 : : }
3725 : :
3726 : : /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3727 : 1139744 : if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3728 : : {
3729 : 243557 : rhs = convert_to_ptrofftype_loc (loc, rhs);
3730 : 243557 : if (arith_code == MINUS_EXPR)
3731 : 10825 : rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3732 : 243557 : t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3733 : : }
3734 : : else
3735 : 896187 : t1 = fold_convert (TREE_TYPE (*expr_p),
3736 : : fold_build2 (arith_code, arith_type,
3737 : : fold_convert (arith_type, lhs),
3738 : : fold_convert (arith_type, rhs)));
3739 : :
3740 : 1139744 : if (postfix)
3741 : : {
3742 : 190624 : gimplify_assign (lvalue, t1, pre_p);
3743 : 190624 : gimplify_seq_add_seq (orig_post_p, post);
3744 : 190624 : *expr_p = lhs;
3745 : 190624 : return GS_ALL_DONE;
3746 : : }
3747 : : else
3748 : : {
3749 : 949120 : *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3750 : 949120 : return GS_OK;
3751 : : }
3752 : : }
3753 : :
3754 : : /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3755 : :
3756 : : static void
3757 : 80005312 : maybe_with_size_expr (tree *expr_p)
3758 : : {
3759 : 80005312 : tree expr = *expr_p;
3760 : 80005312 : tree type = TREE_TYPE (expr);
3761 : 80005312 : tree size;
3762 : :
3763 : : /* If we've already wrapped this or the type is error_mark_node, we can't do
3764 : : anything. */
3765 : 80005312 : if (TREE_CODE (expr) == WITH_SIZE_EXPR
3766 : 80005187 : || type == error_mark_node)
3767 : : return;
3768 : :
3769 : : /* If the size isn't known or is a constant, we have nothing to do. */
3770 : 80004904 : size = TYPE_SIZE_UNIT (type);
3771 : 80004904 : if (!size || poly_int_tree_p (size))
3772 : : return;
3773 : :
3774 : : /* Otherwise, make a WITH_SIZE_EXPR. */
3775 : 1495 : size = unshare_expr (size);
3776 : 1495 : size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3777 : 1495 : *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3778 : : }
3779 : :
3780 : : /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3781 : : Store any side-effects in PRE_P. CALL_LOCATION is the location of
3782 : : the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3783 : : gimplified to an SSA name. */
3784 : :
3785 : : enum gimplify_status
3786 : 33091664 : gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3787 : : bool allow_ssa)
3788 : : {
3789 : 33091664 : bool (*test) (tree);
3790 : 33091664 : fallback_t fb;
3791 : :
3792 : : /* In general, we allow lvalues for function arguments to avoid
3793 : : extra overhead of copying large aggregates out of even larger
3794 : : aggregates into temporaries only to copy the temporaries to
3795 : : the argument list. Make optimizers happy by pulling out to
3796 : : temporaries those types that fit in registers. */
3797 : 33091664 : if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3798 : : test = is_gimple_val, fb = fb_rvalue;
3799 : : else
3800 : : {
3801 : 1582153 : test = is_gimple_lvalue, fb = fb_either;
3802 : : /* Also strip a TARGET_EXPR that would force an extra copy. */
3803 : 1582153 : if (TREE_CODE (*arg_p) == TARGET_EXPR)
3804 : : {
3805 : 340292 : tree init = TARGET_EXPR_INITIAL (*arg_p);
3806 : 340292 : if (init
3807 : 340289 : && !VOID_TYPE_P (TREE_TYPE (init))
3808 : : /* Currently, due to c++/116015, it is not desirable to
3809 : : strip a TARGET_EXPR whose initializer is a {}. The
3810 : : problem is that if we do elide it, we also have to
3811 : : replace all the occurrences of the slot temporary in the
3812 : : initializer with the temporary created for the argument.
3813 : : But we do not have that temporary yet so the replacement
3814 : : would be quite awkward and it might be needed to resort
3815 : : back to a PLACEHOLDER_EXPR. Note that stripping the
3816 : : TARGET_EXPR wouldn't help anyway, as gimplify_expr would
3817 : : just allocate a temporary to store the CONSTRUCTOR into.
3818 : : (FIXME PR116375.)
3819 : :
3820 : : See convert_for_arg_passing for the C++ code that marks
3821 : : the TARGET_EXPR as eliding or not. */
3822 : 639239 : && TREE_CODE (init) != CONSTRUCTOR)
3823 : 269246 : *arg_p = init;
3824 : : }
3825 : : }
3826 : :
3827 : : /* If this is a variable sized type, we must remember the size. */
3828 : 33091664 : maybe_with_size_expr (arg_p);
3829 : :
3830 : : /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3831 : : /* Make sure arguments have the same location as the function call
3832 : : itself. */
3833 : 33091664 : protected_set_expr_location (*arg_p, call_location);
3834 : :
3835 : : /* There is a sequence point before a function call. Side effects in
3836 : : the argument list must occur before the actual call. So, when
3837 : : gimplifying arguments, force gimplify_expr to use an internal
3838 : : post queue which is then appended to the end of PRE_P. */
3839 : 33091664 : return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3840 : : }
3841 : :
3842 : : /* Don't fold inside offloading or taskreg regions: it can break code by
3843 : : adding decl references that weren't in the source. We'll do it during
3844 : : omplower pass instead. */
3845 : :
3846 : : static bool
3847 : 58360820 : maybe_fold_stmt (gimple_stmt_iterator *gsi)
3848 : : {
3849 : 58360820 : struct gimplify_omp_ctx *ctx;
3850 : 58886837 : for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3851 : 1176849 : if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3852 : : return false;
3853 : 530894 : else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3854 : : return false;
3855 : : /* Delay folding of builtins until the IL is in consistent state
3856 : : so the diagnostic machinery can do a better job. */
3857 : 57709988 : if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3858 : : return false;
3859 : 55361851 : return fold_stmt (gsi);
3860 : : }
3861 : :
3862 : : static tree
3863 : : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
3864 : : tree construct_context);
3865 : :
3866 : :
3867 : : /* Helper function for calls to omp_dynamic_cond: find the current
3868 : : enclosing block in the gimplification context. */
3869 : : static tree
3870 : 331 : find_supercontext (void)
3871 : : {
3872 : 331 : vec<gbind *>stack = gimple_bind_expr_stack ();
3873 : 633 : for (int i = stack.length () - 1; i >= 0; i++)
3874 : : {
3875 : 302 : gbind *b = stack[i];
3876 : 302 : if (b->block)
3877 : : return b->block;
3878 : : }
3879 : : return NULL_TREE;
3880 : : }
3881 : :
3882 : : /* OpenMP: Handle the append_args and adjust_args clauses of
3883 : : declare_variant for EXPR, which is a CALL_EXPR whose CALL_EXPR_FN
3884 : : is the variant, within a dispatch construct with clauses DISPATCH_CLAUSES.
3885 : : WANT_VALUE and POINTERIZE are as for expand_variant_call_expr.
3886 : :
3887 : : 'append_args' causes interop objects are added after the last regular
3888 : : (nonhidden, nonvariadic) arguments of the variant function.
3889 : : 'adjust_args' with need_device_{addr,ptr} converts the pointer target of
3890 : : a pointer from a host to a device address. This uses either the default
3891 : : device or the passed device number, which then sets the default device
3892 : : address. */
3893 : : static tree
3894 : 471 : modify_call_for_omp_dispatch (tree expr, tree dispatch_clauses,
3895 : : bool want_value, bool pointerize)
3896 : : {
3897 : 471 : location_t loc = EXPR_LOCATION (expr);
3898 : 471 : tree fndecl = get_callee_fndecl (expr);
3899 : :
3900 : : /* Skip processing if we don't get the expected call form. */
3901 : 471 : if (!fndecl)
3902 : : return expr;
3903 : :
3904 : 471 : tree init_code = NULL_TREE;
3905 : 471 : tree cleanup = NULL_TREE;
3906 : 471 : tree clobbers = NULL_TREE;
3907 : 471 : int nargs = call_expr_nargs (expr);
3908 : 471 : tree dispatch_device_num = NULL_TREE;
3909 : 471 : tree dispatch_interop = NULL_TREE;
3910 : 471 : tree dispatch_append_args = NULL_TREE;
3911 : 471 : int nfirst_args = 0;
3912 : 471 : tree dispatch_adjust_args_list
3913 : 471 : = lookup_attribute ("omp declare variant variant args",
3914 : 471 : DECL_ATTRIBUTES (fndecl));
3915 : :
3916 : 471 : if (dispatch_adjust_args_list)
3917 : : {
3918 : 389 : dispatch_adjust_args_list = TREE_VALUE (dispatch_adjust_args_list);
3919 : 389 : dispatch_append_args = TREE_CHAIN (dispatch_adjust_args_list);
3920 : 389 : if (TREE_PURPOSE (dispatch_adjust_args_list) == NULL_TREE
3921 : 389 : && TREE_VALUE (dispatch_adjust_args_list) == NULL_TREE)
3922 : : dispatch_adjust_args_list = NULL_TREE;
3923 : : }
3924 : 389 : if (dispatch_append_args)
3925 : : {
3926 : 245 : nfirst_args = tree_to_shwi (TREE_PURPOSE (dispatch_append_args));
3927 : 245 : dispatch_append_args = TREE_VALUE (dispatch_append_args);
3928 : : }
3929 : 471 : dispatch_device_num = omp_find_clause (dispatch_clauses, OMP_CLAUSE_DEVICE);
3930 : 471 : if (dispatch_device_num)
3931 : 287 : dispatch_device_num = OMP_CLAUSE_DEVICE_ID (dispatch_device_num);
3932 : 471 : dispatch_interop = omp_find_clause (dispatch_clauses, OMP_CLAUSE_INTEROP);
3933 : 471 : int nappend = 0, ninterop = 0;
3934 : 880 : for (tree t = dispatch_append_args; t; t = TREE_CHAIN (t))
3935 : 409 : nappend++;
3936 : :
3937 : : /* FIXME: error checking should be taken out of this function and
3938 : : handled before any attempt at filtering or resolution happens.
3939 : : Otherwise whether or not diagnostics appear is determined by
3940 : : GCC internals, how good the front ends are at constant-folding,
3941 : : the split between early/late resolution, etc instead of the code
3942 : : as written by the user. */
3943 : 471 : if (dispatch_interop)
3944 : 861 : for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
3945 : 605 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3946 : 409 : ninterop++;
3947 : 471 : if (dispatch_interop && !dispatch_device_num)
3948 : : {
3949 : 7 : gcc_checking_assert (ninterop > 1);
3950 : 7 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3951 : : "the %<device%> clause must be present if the %<interop%> "
3952 : : "clause has more than one list item");
3953 : : }
3954 : 471 : if (nappend < ninterop)
3955 : : {
3956 : 48 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3957 : : "number of list items in %<interop%> clause (%d) "
3958 : : "exceeds the number of %<append_args%> items (%d) for "
3959 : : "%<declare variant%> candidate %qD", ninterop, nappend, fndecl);
3960 : 88 : inform (dispatch_append_args
3961 : 8 : ? EXPR_LOCATION (TREE_PURPOSE (dispatch_append_args))
3962 : 40 : : DECL_SOURCE_LOCATION (fndecl),
3963 : : "%<declare variant%> candidate %qD declared here", fndecl);
3964 : 48 : ninterop = nappend;
3965 : : }
3966 : 471 : if (dispatch_append_args)
3967 : : {
3968 : 245 : tree *buffer = XALLOCAVEC (tree, nargs + nappend);
3969 : 245 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3970 : : /* Copy the first arguments; insert then the interop objects,
3971 : : and then copy the rest (nargs - nfirst_args) args. */
3972 : 245 : int i;
3973 : 679 : for (i = 0; i < nfirst_args; i++)
3974 : : {
3975 : 434 : arg = TREE_CHAIN (arg);
3976 : 434 : buffer[i] = CALL_EXPR_ARG (expr, i);
3977 : : }
3978 : : int j = ninterop;
3979 : 583 : for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
3980 : 338 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3981 : 338 : buffer[i + --j] = OMP_CLAUSE_DECL (t);
3982 : 245 : gcc_checking_assert (j == 0);
3983 : :
3984 : : /* Do we need to create additional interop objects? */
3985 : 245 : if (ninterop < nappend)
3986 : : {
3987 : 56 : if (dispatch_device_num == NULL_TREE)
3988 : : /* Not remapping device number. */
3989 : 22 : dispatch_device_num = build_int_cst (integer_type_node,
3990 : : GOMP_DEVICE_DEFAULT_OMP_61);
3991 : 56 : int nnew = nappend - ninterop;
3992 : 56 : tree nobjs = build_int_cst (integer_type_node, nnew);
3993 : 56 : tree a, t;
3994 : :
3995 : : /* Skip to the append_args clause for the first constructed
3996 : : interop argument. */
3997 : 56 : tree apparg = dispatch_append_args;
3998 : 83 : for (j = 0; j < ninterop; j++)
3999 : 27 : apparg = TREE_CHAIN (apparg);
4000 : :
4001 : : /* omp_interop_t *objs[n]; */
4002 : 56 : tree objtype = build_pointer_type (pointer_sized_int_node);
4003 : 56 : t = build_array_type_nelts (objtype, nnew);
4004 : 56 : tree objs = create_tmp_var (t, "interopobjs");
4005 : :
4006 : : /* int target_tgtsync[n]; */
4007 : 56 : t = build_array_type_nelts (integer_type_node, nnew);
4008 : 56 : tree target_tgtsync = create_tmp_var (t, "tgt_tgtsync");
4009 : :
4010 : : /* Scan first to determine if we need a prefer_type array. */
4011 : 56 : tree prefer_type = NULL_TREE;
4012 : 56 : tree prefer_type_type = NULL_TREE;
4013 : 141 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4014 : 64 : if (TREE_VALUE (a) != NULL_TREE)
4015 : : {
4016 : : /* const char *prefer_type[n]; */
4017 : 35 : t = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4018 : 35 : prefer_type_type = build_pointer_type (t);
4019 : 35 : t = build_array_type_nelts (prefer_type_type, nnew);
4020 : 35 : prefer_type = create_tmp_var (t, "pref_type");
4021 : 35 : break;
4022 : : }
4023 : :
4024 : : /* Initialize the arrays, generating temp vars and clobbers for
4025 : : the interobject objects. (The constructed array holding the
4026 : : pointers to these objects shouldn't need clobbering as there's
4027 : : no reason for GOMP_interop to modify its contents.) */
4028 : 127 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4029 : : {
4030 : : /* The allocated temporaries for the interop objects
4031 : : have type omp_interop_t, which is an integer type that
4032 : : can encode a pointer. */
4033 : 71 : tree objvar = create_tmp_var (pointer_sized_int_node, "interop");
4034 : 71 : buffer[i + j] = objvar;
4035 : 71 : TREE_ADDRESSABLE (objvar) = 1;
4036 : : /* Generate a clobber for the temporary for when we're done
4037 : : with it. */
4038 : 71 : tree c = build_clobber (pointer_sized_int_node,
4039 : : CLOBBER_OBJECT_END);
4040 : 71 : c = build2 (MODIFY_EXPR, pointer_sized_int_node, objvar, c);
4041 : 71 : if (clobbers)
4042 : 15 : clobbers = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4043 : : c, clobbers);
4044 : : else
4045 : : clobbers = c;
4046 : :
4047 : : /* objs[offset] = &objvar; */
4048 : 71 : tree offset = build_int_cst (integer_type_node, j - ninterop);
4049 : 71 : tree init = build4 (ARRAY_REF, objtype, objs, offset,
4050 : : NULL_TREE, NULL_TREE);
4051 : 71 : init = build2 (MODIFY_EXPR, objtype, init,
4052 : : build_fold_addr_expr (objvar));
4053 : 71 : if (init_code)
4054 : 15 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4055 : : init_code, init);
4056 : : else
4057 : : init_code = init;
4058 : :
4059 : : /* target_tgtsync[offset] = tgt;
4060 : : (Don't blame me, I didn't design the encoding of this
4061 : : info into the dispatch interop clause data structure,
4062 : : but the runtime wants a bit mask.) */
4063 : 71 : tree tree_tgt = TREE_OPERAND (TREE_PURPOSE (a), 0);
4064 : 71 : int tgt = 0;
4065 : 71 : if (TREE_PURPOSE (tree_tgt) == boolean_true_node)
4066 : 42 : tgt |= GOMP_INTEROP_TARGET;
4067 : 71 : if (TREE_VALUE (tree_tgt) == boolean_true_node)
4068 : 36 : tgt |= GOMP_INTEROP_TARGETSYNC;
4069 : 71 : init = build4 (ARRAY_REF, integer_type_node,
4070 : : target_tgtsync, offset, NULL_TREE, NULL_TREE);
4071 : 71 : init = build2 (MODIFY_EXPR, integer_type_node, init,
4072 : 71 : build_int_cst (integer_type_node, tgt));
4073 : 71 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4074 : : init_code, init);
4075 : :
4076 : 71 : if (prefer_type)
4077 : : {
4078 : 45 : tree pref = TREE_VALUE (a);
4079 : 45 : if (pref == NULL_TREE)
4080 : 6 : pref = null_pointer_node;
4081 : : else
4082 : 39 : pref = build_fold_addr_expr (pref);
4083 : 45 : init = build4 (ARRAY_REF, prefer_type_type, prefer_type,
4084 : : offset, NULL_TREE, NULL_TREE);
4085 : 45 : init = build2 (MODIFY_EXPR, prefer_type_type, init,
4086 : : pref);
4087 : 45 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4088 : : init_code, init);
4089 : : }
4090 : : }
4091 : :
4092 : 56 : objs = build_fold_addr_expr (objs);
4093 : 56 : target_tgtsync = build_fold_addr_expr (target_tgtsync);
4094 : 56 : prefer_type = prefer_type ? build_fold_addr_expr (prefer_type)
4095 : : : null_pointer_node;
4096 : 56 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
4097 : 56 : tree create
4098 : 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4099 : : nobjs, objs, target_tgtsync, prefer_type,
4100 : : integer_zero_node, null_pointer_node,
4101 : : integer_zero_node, null_pointer_node,
4102 : : integer_zero_node, null_pointer_node);
4103 : 56 : if (init_code)
4104 : 56 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (create),
4105 : : init_code, create);
4106 : : else
4107 : : init_code = create;
4108 : :
4109 : 56 : cleanup
4110 : 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4111 : : integer_zero_node, null_pointer_node,
4112 : : null_pointer_node, null_pointer_node,
4113 : : integer_zero_node, null_pointer_node,
4114 : : nobjs, objs,
4115 : : integer_zero_node, null_pointer_node);
4116 : 56 : if (clobbers)
4117 : 56 : cleanup = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4118 : : cleanup, clobbers);
4119 : : }
4120 : :
4121 : 654 : for (j = 0; j < nappend; j++)
4122 : : {
4123 : : /* Fortran permits by-reference or by-value for the dummy arg
4124 : : and by-value, by-reference, ptr by-reference as actual
4125 : : argument. Handle this. */
4126 : 409 : tree obj = buffer[i + j]; // interop object
4127 : 409 : tree a2 = TREE_VALUE (arg); // parameter type
4128 : 766 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4129 : 436 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (obj))))
4130 : : {
4131 : 32 : tree t = TREE_TYPE (TREE_TYPE (obj));
4132 : 32 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (t)));
4133 : 32 : obj = fold_build1 (INDIRECT_REF, t, obj);
4134 : : }
4135 : 750 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4136 : 420 : && INTEGRAL_TYPE_P (a2))
4137 : : {
4138 : 43 : tree t = TREE_TYPE (TREE_TYPE (obj));
4139 : 43 : gcc_checking_assert (INTEGRAL_TYPE_P (t));
4140 : 43 : obj = fold_build1 (INDIRECT_REF, t, obj);
4141 : : }
4142 : 497 : else if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
4143 : 461 : && POINTER_TYPE_P (a2))
4144 : : {
4145 : 18 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4146 : 18 : obj = build_fold_addr_expr (obj);
4147 : : }
4148 : 348 : else if (!INTEGRAL_TYPE_P (a2)
4149 : 348 : || !INTEGRAL_TYPE_P (TREE_TYPE (obj)))
4150 : : {
4151 : 36 : tree t = TREE_TYPE (obj);
4152 : 36 : gcc_checking_assert (POINTER_TYPE_P (t)
4153 : : && POINTER_TYPE_P (a2)
4154 : : && INTEGRAL_TYPE_P (TREE_TYPE (t))
4155 : : && INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4156 : : }
4157 : 409 : buffer[i + j] = obj;
4158 : 409 : arg = TREE_CHAIN (arg);
4159 : : }
4160 : 245 : i += nappend;
4161 : 534 : for (j = nfirst_args; j < nargs; j++)
4162 : 289 : buffer[i++] = CALL_EXPR_ARG (expr, j);
4163 : 245 : nargs += nappend;
4164 : 245 : tree call = expr;
4165 : 490 : expr = build_call_array_loc (EXPR_LOCATION (expr), TREE_TYPE (call),
4166 : 245 : CALL_EXPR_FN (call), nargs, buffer);
4167 : :
4168 : : /* Copy all CALL_EXPR flags. */
4169 : 245 : CALL_EXPR_STATIC_CHAIN (expr) = CALL_EXPR_STATIC_CHAIN (call);
4170 : 245 : CALL_EXPR_TAILCALL (expr) = CALL_EXPR_TAILCALL (call);
4171 : 245 : CALL_EXPR_RETURN_SLOT_OPT (expr)
4172 : 245 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4173 : 245 : CALL_FROM_THUNK_P (expr) = CALL_FROM_THUNK_P (call);
4174 : 245 : SET_EXPR_LOCATION (expr, EXPR_LOCATION (call));
4175 : 245 : CALL_EXPR_VA_ARG_PACK (expr) = CALL_EXPR_VA_ARG_PACK (call);
4176 : : }
4177 : :
4178 : : /* Nothing to do for adjust_args? */
4179 : 471 : if (!dispatch_adjust_args_list || !TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
4180 : 260 : goto add_cleanup;
4181 : :
4182 : : /* Handle adjust_args. */
4183 : 966 : for (int i = 0; i < nargs; i++)
4184 : : {
4185 : 755 : tree *arg_p = &CALL_EXPR_ARG (expr, i);
4186 : :
4187 : : /* Nothing to do if arg is constant null pointer. */
4188 : 755 : if (integer_zerop (*arg_p))
4189 : 6 : continue;
4190 : :
4191 : : bool need_device_ptr = false;
4192 : : bool need_device_addr = false;
4193 : 2247 : for (int need_addr = 0; need_addr <= 1; need_addr++)
4194 : 2301 : for (tree arg = (need_addr
4195 : 1498 : ? TREE_VALUE (dispatch_adjust_args_list)
4196 : 1498 : : TREE_PURPOSE (dispatch_adjust_args_list));
4197 : 2301 : arg != NULL; arg = TREE_CHAIN (arg))
4198 : : {
4199 : 1152 : if (TREE_VALUE (arg)
4200 : 1152 : && TREE_CODE (TREE_VALUE (arg)) == INTEGER_CST
4201 : 2304 : && wi::eq_p (i, wi::to_wide (TREE_VALUE (arg))))
4202 : : {
4203 : 349 : if (need_addr)
4204 : : need_device_addr = true;
4205 : : else
4206 : 349 : need_device_ptr = true;
4207 : : break;
4208 : : }
4209 : : }
4210 : :
4211 : 749 : if (need_device_ptr || need_device_addr)
4212 : : {
4213 : 740 : bool is_device_ptr = false;
4214 : 740 : bool has_device_addr = false;
4215 : :
4216 : 740 : for (tree c = dispatch_clauses; c; c = TREE_CHAIN (c))
4217 : : {
4218 : 454 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
4219 : 454 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
4220 : : {
4221 : 164 : tree decl1 = DECL_NAME (OMP_CLAUSE_DECL (c));
4222 : 164 : tree decl2 = tree_strip_nop_conversions (*arg_p);
4223 : 164 : if (TREE_CODE (decl2) == ADDR_EXPR)
4224 : 19 : decl2 = TREE_OPERAND (decl2, 0);
4225 : 164 : if (VAR_P (decl2) || TREE_CODE (decl2) == PARM_DECL)
4226 : : {
4227 : 140 : decl2 = DECL_NAME (decl2);
4228 : 140 : if (decl1 == decl2
4229 : 203 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
4230 : : {
4231 : 58 : if (need_device_addr)
4232 : 0 : warning_at (OMP_CLAUSE_LOCATION (c),
4233 : 0 : OPT_Wopenmp,
4234 : : "%<is_device_ptr%> for %qD does"
4235 : : " not imply %<has_device_addr%> "
4236 : : "required for %<need_device_addr%>",
4237 : 0 : OMP_CLAUSE_DECL (c));
4238 : : is_device_ptr = true;
4239 : : break;
4240 : : }
4241 : 82 : else if (decl1 == decl2)
4242 : : {
4243 : 5 : if (need_device_ptr)
4244 : 10 : warning_at (OMP_CLAUSE_LOCATION (c),
4245 : 5 : OPT_Wopenmp,
4246 : : "%<has_device_addr%> for %qD does"
4247 : : " not imply %<is_device_ptr%> "
4248 : : "required for %<need_device_ptr%>",
4249 : 5 : OMP_CLAUSE_DECL (c));
4250 : : has_device_addr = true;
4251 : : break;
4252 : : }
4253 : : }
4254 : : }
4255 : : }
4256 : :
4257 : 349 : if ((need_device_ptr && !is_device_ptr)
4258 : 58 : || (need_device_addr && !has_device_addr))
4259 : : {
4260 : 291 : if (dispatch_device_num == NULL_TREE)
4261 : : {
4262 : : // device_num = omp_get_default_device ()
4263 : 105 : tree fn
4264 : 105 : = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
4265 : 105 : tree call = build_call_expr (fn, 0);
4266 : 105 : dispatch_device_num = create_tmp_var_raw (TREE_TYPE (call));
4267 : 105 : tree init
4268 : 105 : = build4 (TARGET_EXPR, TREE_TYPE (call),
4269 : : dispatch_device_num, call, NULL_TREE, NULL_TREE);
4270 : 105 : if (init_code)
4271 : 0 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4272 : : init_code, init);
4273 : : else
4274 : : init_code = init;
4275 : : }
4276 : :
4277 : : // We want to emit the following statement:
4278 : : // mapped_arg = omp_get_mapped_ptr (arg,
4279 : : // device_num)
4280 : : // but arg has to be the actual pointer, not a
4281 : : // reference or a conversion expression.
4282 : 291 : tree actual_ptr
4283 : 291 : = ((TREE_CODE (*arg_p) == ADDR_EXPR)
4284 : 291 : ? TREE_OPERAND (*arg_p, 0)
4285 : 38 : : *arg_p);
4286 : 291 : if (TREE_CODE (actual_ptr) == NOP_EXPR
4287 : 291 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (actual_ptr, 0)))
4288 : : == REFERENCE_TYPE))
4289 : : {
4290 : 6 : actual_ptr = TREE_OPERAND (actual_ptr, 0);
4291 : 6 : actual_ptr = build1 (INDIRECT_REF,
4292 : 6 : TREE_TYPE (actual_ptr),
4293 : : actual_ptr);
4294 : : }
4295 : 291 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_MAPPED_PTR);
4296 : 291 : tree mapped_arg = build_call_expr_loc (loc, fn, 2, actual_ptr,
4297 : : dispatch_device_num);
4298 : :
4299 : 291 : if (TREE_CODE (*arg_p) == ADDR_EXPR
4300 : 291 : || (TREE_CODE (TREE_TYPE (actual_ptr)) == REFERENCE_TYPE))
4301 : 47 : mapped_arg = build_fold_addr_expr (mapped_arg);
4302 : 244 : else if (TREE_CODE (*arg_p) == NOP_EXPR)
4303 : 35 : mapped_arg = build1 (NOP_EXPR, TREE_TYPE (*arg_p),
4304 : : mapped_arg);
4305 : 291 : *arg_p = mapped_arg;
4306 : : }
4307 : : }
4308 : : }
4309 : :
4310 : 471 : add_cleanup:
4311 : 471 : if (cleanup)
4312 : : {
4313 : 56 : tree result = NULL_TREE;
4314 : 56 : if (want_value && pointerize)
4315 : : {
4316 : 0 : tree tmp = create_tmp_var (build_pointer_type (TREE_TYPE (expr)),
4317 : : "cleanuptmp");
4318 : 0 : result = build_simple_mem_ref (tmp);
4319 : 0 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp,
4320 : : build_fold_addr_expr (expr));
4321 : : }
4322 : 56 : else if (want_value)
4323 : : {
4324 : 18 : tree tmp = create_tmp_var (TREE_TYPE (expr), "cleanuptmp");
4325 : 18 : result = tmp;
4326 : 18 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp, expr);
4327 : : }
4328 : 56 : if (init_code)
4329 : 56 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4330 : 56 : expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, cleanup);
4331 : :
4332 : 56 : if (result)
4333 : 18 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (result), expr, result);
4334 : : }
4335 : 415 : else if (init_code)
4336 : 105 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4337 : :
4338 : : return expr;
4339 : : }
4340 : :
4341 : : /* Helper function for gimplify_call_expr: handle "declare variant"
4342 : : resolution and expansion of the CALL_EXPR EXPR. WANT_VALUE is true
4343 : : if the result value of the call is needed; POINTERIZE is true if it
4344 : : also needs to be pointerized. If OMP_DISPATCH_P is true, apply
4345 : : associated transformations using DISPATCH_CLAUSES.
4346 : : This function may return either the original call or some other
4347 : : expression such as a conditional to select one of multiple calls.
4348 : :
4349 : : FIXME: this function is written to be independent of gimplifier internals
4350 : : so that it could be moved to omp-general.cc and invoked from the
4351 : : front ends instead, per PR115076. */
4352 : :
4353 : : static tree
4354 : 1086 : expand_variant_call_expr (tree expr, bool want_value, bool pointerize,
4355 : : bool omp_dispatch_p, tree dispatch_clauses)
4356 : : {
4357 : : /* If we've already processed this call, stop now. This can happen
4358 : : if the variant call resolves to the original function, or to
4359 : : a dynamic conditional that includes the default call to the original
4360 : : function. */
4361 : 1086 : gcc_assert (omp_resolved_variant_calls != NULL);
4362 : 1086 : if (omp_resolved_variant_calls->contains (expr))
4363 : 96 : return expr;
4364 : :
4365 : 990 : tree fndecl = get_callee_fndecl (expr);
4366 : 990 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
4367 : 990 : location_t loc = EXPR_LOCATION (expr);
4368 : 990 : tree construct_context = omp_get_construct_context ();
4369 : 990 : vec<struct omp_variant> all_candidates
4370 : 990 : = omp_declare_variant_candidates (fndecl, construct_context);
4371 : 990 : gcc_assert (!all_candidates.is_empty ());
4372 : 990 : vec<struct omp_variant> candidates
4373 : 990 : = omp_get_dynamic_candidates (all_candidates, construct_context);
4374 : :
4375 : : /* If the variant call could be resolved now, build a nest of COND_EXPRs
4376 : : if there are dynamic candidates, and/or a new CALL_EXPR for each
4377 : : candidate call. */
4378 : 990 : if (!candidates.is_empty ())
4379 : : {
4380 : 978 : int n = candidates.length ();
4381 : 978 : tree tail = NULL_TREE;
4382 : :
4383 : 1988 : for (int i = n - 1; i >= 0; i--)
4384 : : {
4385 : 1010 : if (tail)
4386 : 32 : gcc_assert (candidates[i].dynamic_selector);
4387 : : else
4388 : 978 : gcc_assert (!candidates[i].dynamic_selector);
4389 : 1010 : if (candidates[i].alternative == fndecl)
4390 : : {
4391 : : /* We should only get the original function back as the
4392 : : default. */
4393 : 197 : gcc_assert (!tail);
4394 : 197 : omp_resolved_variant_calls->add (expr);
4395 : 197 : tail = expr;
4396 : : }
4397 : : else
4398 : : {
4399 : : /* For the final static selector, we can re-use the old
4400 : : CALL_EXPR and just replace the function, unless it may
4401 : : need dispatch argument modification. Otherwise,
4402 : : make a copy of it. */
4403 : 813 : tree thiscall = (tail || omp_dispatch_p
4404 : 813 : ? unshare_expr (expr) : expr);
4405 : 813 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
4406 : 813 : candidates[i].alternative);
4407 : 813 : if (omp_dispatch_p)
4408 : 471 : thiscall = modify_call_for_omp_dispatch (thiscall,
4409 : : dispatch_clauses,
4410 : : want_value,
4411 : : pointerize);
4412 : 813 : if (!tail)
4413 : : tail = thiscall;
4414 : : else
4415 : 64 : tail = build3 (COND_EXPR, TREE_TYPE (expr),
4416 : 32 : omp_dynamic_cond (candidates[i].selector,
4417 : : find_supercontext ()),
4418 : : thiscall, tail);
4419 : : }
4420 : : }
4421 : : return tail;
4422 : : }
4423 : :
4424 : : /* If we couldn't resolve the variant call now, expand it into a loop using
4425 : : a switch and OMP_NEXT_VARIANT for dispatch. The ompdevlow pass will
4426 : : handle OMP_NEXT_VARIANT expansion. */
4427 : : else
4428 : : {
4429 : : /* If we need a usable return value, we need a temporary
4430 : : and an assignment in each alternative. This logic was borrowed
4431 : : from gimplify_cond_expr. */
4432 : 12 : tree type = TREE_TYPE (expr);
4433 : 12 : tree tmp = NULL_TREE, result = NULL_TREE;
4434 : :
4435 : 12 : if (want_value)
4436 : : {
4437 : 12 : if (pointerize)
4438 : : {
4439 : 0 : type = build_pointer_type (type);
4440 : 0 : tmp = create_tmp_var (type, "iftmp");
4441 : 0 : result = build_simple_mem_ref_loc (loc, tmp);
4442 : : }
4443 : : else
4444 : : {
4445 : 12 : tmp = create_tmp_var (type, "iftmp");
4446 : 12 : result = tmp;
4447 : : }
4448 : : }
4449 : :
4450 : : /* Preprocess the all_candidates array so that the alternative field of
4451 : : each element holds the actual function call expression and possible
4452 : : assignment, instead of just the decl for the variant function. */
4453 : 60 : for (unsigned int i = 0; i < all_candidates.length (); i++)
4454 : : {
4455 : 48 : tree decl = all_candidates[i].alternative;
4456 : 48 : tree thiscall;
4457 : :
4458 : : /* We need to turn the decl from the candidate into a function
4459 : : call and possible assignment, and stuff that in
4460 : : the directive seq of the gomp_variant. */
4461 : 48 : if (decl == fndecl)
4462 : : {
4463 : 12 : thiscall = expr;
4464 : 12 : omp_resolved_variant_calls->add (expr);
4465 : : }
4466 : : else
4467 : : {
4468 : 36 : thiscall = unshare_expr (expr);
4469 : 36 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype, decl);
4470 : 36 : if (omp_dispatch_p)
4471 : 0 : thiscall = modify_call_for_omp_dispatch (thiscall,
4472 : : dispatch_clauses,
4473 : : want_value,
4474 : : pointerize);
4475 : : }
4476 : 48 : if (pointerize)
4477 : 0 : thiscall = build_fold_addr_expr_loc (loc, thiscall);
4478 : 48 : if (want_value)
4479 : 48 : thiscall = build2 (INIT_EXPR, type, tmp, thiscall);
4480 : 48 : all_candidates[i].alternative = thiscall;
4481 : : }
4482 : :
4483 : 12 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
4484 : 12 : tree expansion = expand_late_variant_directive (all_candidates,
4485 : : construct_context);
4486 : 12 : if (result)
4487 : 12 : expansion = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4488 : : expansion, result);
4489 : 12 : return expansion;
4490 : : }
4491 : : }
4492 : :
4493 : : /* Wrapper around expand_variant_call_expr to interface with gimplifier
4494 : : state. EXPR and OMP_DISPATCH_P are as for expand_variant_call_expr,
4495 : : FALLBACK is used to compute the WANT_VALUE and POINTERIZE arguments. */
4496 : : static tree
4497 : 1086 : gimplify_variant_call_expr (tree expr, fallback_t fallback,
4498 : : bool omp_dispatch_p)
4499 : : {
4500 : 1086 : tree type = TREE_TYPE (expr);
4501 : 1086 : bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
4502 : 475 : bool pointerize = false;
4503 : : /* If the result value must be an lvalue or the result type must
4504 : : live in memory, then we have to pointerize it if we need a temporary. */
4505 : 475 : if (want_value
4506 : 475 : && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
4507 : 475 : || TREE_ADDRESSABLE (type)))
4508 : 0 : pointerize = true;
4509 : :
4510 : 1598 : return expand_variant_call_expr (expr, want_value, pointerize,
4511 : : omp_dispatch_p,
4512 : : (omp_dispatch_p
4513 : 512 : ? gimplify_omp_ctxp->clauses
4514 : 1086 : : NULL_TREE));
4515 : : }
4516 : :
4517 : :
4518 : : /* Helper function for gimplify_call_expr, called via walk_tree.
4519 : : Find used user labels. */
4520 : :
4521 : : static tree
4522 : 789 : find_used_user_labels (tree *tp, int *, void *)
4523 : : {
4524 : 789 : if (TREE_CODE (*tp) == LABEL_EXPR
4525 : 15 : && !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (*tp))
4526 : 15 : && DECL_NAME (LABEL_EXPR_LABEL (*tp))
4527 : 804 : && TREE_USED (LABEL_EXPR_LABEL (*tp)))
4528 : 15 : return *tp;
4529 : : return NULL_TREE;
4530 : : }
4531 : :
4532 : :
4533 : : /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
4534 : : WANT_VALUE is true if the result of the call is desired. */
4535 : :
4536 : : static enum gimplify_status
4537 : 16686568 : gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4538 : : {
4539 : 16686568 : bool want_value = (fallback != fb_none);
4540 : 16686568 : tree fndecl, parms, p, fnptrtype;
4541 : 16686568 : enum gimplify_status ret;
4542 : 16686568 : int i, nargs;
4543 : 16686568 : gcall *call;
4544 : 16686568 : bool builtin_va_start_p = false, omp_dispatch_p = false;
4545 : 16686568 : location_t loc = EXPR_LOCATION (*expr_p);
4546 : :
4547 : 16686568 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4548 : :
4549 : : /* For reliable diagnostics during inlining, it is necessary that
4550 : : every call_expr be annotated with file and line. */
4551 : 16686568 : if (! EXPR_HAS_LOCATION (*expr_p))
4552 : 560172 : SET_EXPR_LOCATION (*expr_p, input_location);
4553 : :
4554 : : /* Gimplify internal functions created in the FEs. */
4555 : 16686568 : if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
4556 : : {
4557 : 416299 : enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
4558 : 416299 : if (ifn == IFN_GOMP_DISPATCH)
4559 : : {
4560 : 878 : gcc_assert (flag_openmp
4561 : : && gimplify_omp_ctxp
4562 : : && gimplify_omp_ctxp->code == OMP_DISPATCH);
4563 : 878 : *expr_p = CALL_EXPR_ARG (*expr_p, 0);
4564 : 878 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4565 : 878 : if (! EXPR_HAS_LOCATION (*expr_p))
4566 : 94 : SET_EXPR_LOCATION (*expr_p, input_location);
4567 : : omp_dispatch_p = true;
4568 : : }
4569 : : else
4570 : : {
4571 : 415421 : if (want_value)
4572 : : return GS_ALL_DONE;
4573 : :
4574 : 21894 : nargs = call_expr_nargs (*expr_p);
4575 : 21894 : auto_vec<tree> vargs (nargs);
4576 : :
4577 : 21894 : if (ifn == IFN_ASSUME)
4578 : : {
4579 : 511 : if (simple_condition_p (CALL_EXPR_ARG (*expr_p, 0)))
4580 : : {
4581 : : /* If the [[assume (cond)]]; condition is simple
4582 : : enough and can be evaluated unconditionally
4583 : : without side-effects, expand it as
4584 : : if (!cond) __builtin_unreachable (); */
4585 : 293 : tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
4586 : 293 : *expr_p
4587 : 586 : = build3 (COND_EXPR, void_type_node,
4588 : 293 : CALL_EXPR_ARG (*expr_p, 0), void_node,
4589 : 293 : build_call_expr_loc (EXPR_LOCATION (*expr_p),
4590 : : fndecl, 0));
4591 : 293 : return GS_OK;
4592 : : }
4593 : : /* If not optimizing, ignore the assumptions unless there
4594 : : are used user labels in it. */
4595 : 218 : if ((!optimize
4596 : 98 : && !walk_tree_without_duplicates (&CALL_EXPR_ARG (*expr_p,
4597 : : 0),
4598 : : find_used_user_labels,
4599 : : NULL))
4600 : 233 : || seen_error ())
4601 : : {
4602 : 94 : *expr_p = NULL_TREE;
4603 : 94 : return GS_ALL_DONE;
4604 : : }
4605 : : /* Temporarily, until gimple lowering, transform
4606 : : .ASSUME (cond);
4607 : : into:
4608 : : [[assume (guard)]]
4609 : : {
4610 : : guard = cond;
4611 : : }
4612 : : such that gimple lowering can outline the condition into
4613 : : a separate function easily. */
4614 : 124 : tree guard = create_tmp_var (boolean_type_node);
4615 : 124 : *expr_p = build2 (MODIFY_EXPR, void_type_node, guard,
4616 : 124 : gimple_boolify (CALL_EXPR_ARG (*expr_p, 0)));
4617 : 124 : *expr_p = build3 (BIND_EXPR, void_type_node, NULL, *expr_p, NULL);
4618 : 124 : push_gimplify_context ();
4619 : 124 : gimple_seq body = NULL;
4620 : 124 : gimple *g = gimplify_and_return_first (*expr_p, &body);
4621 : 124 : pop_gimplify_context (g);
4622 : 124 : g = gimple_build_assume (guard, body);
4623 : 124 : gimple_set_location (g, loc);
4624 : 124 : gimplify_seq_add_stmt (pre_p, g);
4625 : 124 : *expr_p = NULL_TREE;
4626 : 124 : return GS_ALL_DONE;
4627 : : }
4628 : :
4629 : 46058 : for (i = 0; i < nargs; i++)
4630 : : {
4631 : 24675 : gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
4632 : 24675 : EXPR_LOCATION (*expr_p));
4633 : 24675 : vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
4634 : : }
4635 : :
4636 : 21383 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
4637 : 21383 : gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
4638 : 21383 : gimplify_seq_add_stmt (pre_p, call);
4639 : 21383 : return GS_ALL_DONE;
4640 : 21894 : }
4641 : : }
4642 : :
4643 : : /* This may be a call to a builtin function.
4644 : :
4645 : : Builtin function calls may be transformed into different
4646 : : (and more efficient) builtin function calls under certain
4647 : : circumstances. Unfortunately, gimplification can muck things
4648 : : up enough that the builtin expanders are not aware that certain
4649 : : transformations are still valid.
4650 : :
4651 : : So we attempt transformation/gimplification of the call before
4652 : : we gimplify the CALL_EXPR. At this time we do not manage to
4653 : : transform all calls in the same manner as the expanders do, but
4654 : : we do transform most of them. */
4655 : 16271147 : fndecl = get_callee_fndecl (*expr_p);
4656 : 16271147 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4657 : 3911925 : switch (DECL_FUNCTION_CODE (fndecl))
4658 : : {
4659 : 81278 : CASE_BUILT_IN_ALLOCA:
4660 : : /* If the call has been built for a variable-sized object, then we
4661 : : want to restore the stack level when the enclosing BIND_EXPR is
4662 : : exited to reclaim the allocated space; otherwise, we precisely
4663 : : need to do the opposite and preserve the latest stack level. */
4664 : 81278 : if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
4665 : 8856 : gimplify_ctxp->save_stack = true;
4666 : : else
4667 : 72422 : gimplify_ctxp->keep_stack = true;
4668 : : break;
4669 : :
4670 : 20681 : case BUILT_IN_VA_START:
4671 : 20681 : {
4672 : 20681 : builtin_va_start_p = true;
4673 : 20681 : if (call_expr_nargs (*expr_p) < 2)
4674 : : {
4675 : 1 : error ("too few arguments to function %<va_start%>");
4676 : 1 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4677 : 1 : return GS_OK;
4678 : : }
4679 : :
4680 : 20680 : if (fold_builtin_next_arg (*expr_p, true))
4681 : : {
4682 : 8 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4683 : 8 : return GS_OK;
4684 : : }
4685 : : break;
4686 : : }
4687 : :
4688 : 30 : case BUILT_IN_EH_RETURN:
4689 : 30 : cfun->calls_eh_return = true;
4690 : 30 : break;
4691 : :
4692 : 1014 : case BUILT_IN_CLEAR_PADDING:
4693 : 1014 : if (call_expr_nargs (*expr_p) == 1)
4694 : : {
4695 : : /* Remember the original type of the argument in an internal
4696 : : dummy second argument, as in GIMPLE pointer conversions are
4697 : : useless. Also mark this call as not for automatic
4698 : : initialization in the internal dummy third argument. */
4699 : 507 : p = CALL_EXPR_ARG (*expr_p, 0);
4700 : 507 : *expr_p
4701 : 507 : = build_call_expr_loc (EXPR_LOCATION (*expr_p), fndecl, 2, p,
4702 : 507 : build_zero_cst (TREE_TYPE (p)));
4703 : 507 : return GS_OK;
4704 : : }
4705 : : break;
4706 : :
4707 : : default:
4708 : : ;
4709 : : }
4710 : 16270631 : if (fndecl && fndecl_built_in_p (fndecl))
4711 : : {
4712 : 4144395 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4713 : 4144395 : if (new_tree && new_tree != *expr_p)
4714 : : {
4715 : : /* There was a transformation of this call which computes the
4716 : : same value, but in a more efficient way. Return and try
4717 : : again. */
4718 : 717 : *expr_p = new_tree;
4719 : 717 : return GS_OK;
4720 : : }
4721 : : }
4722 : :
4723 : : /* Remember the original function pointer type. */
4724 : 16269914 : fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
4725 : :
4726 : : /* Handle "declare variant" resolution and arglist processing. */
4727 : 16269914 : if (flag_openmp
4728 : 439856 : && fndecl
4729 : 439154 : && cfun
4730 : 439154 : && (cfun->curr_properties & PROP_gimple_any) == 0
4731 : 242267 : && !omp_has_novariants ()
4732 : 16512075 : && lookup_attribute ("omp declare variant base",
4733 : 242161 : DECL_ATTRIBUTES (fndecl)))
4734 : : {
4735 : 1086 : tree orig = *expr_p;
4736 : 1086 : *expr_p = gimplify_variant_call_expr (*expr_p, fallback,
4737 : : omp_dispatch_p);
4738 : :
4739 : : /* This may resolve to the same call, or the call expr with just
4740 : : the function replaced, in which case we should just continue to
4741 : : gimplify it normally. Otherwise, if we get something else back,
4742 : : stop here and re-gimplify the whole replacement expr. */
4743 : 1086 : if (*expr_p != orig)
4744 : : return GS_OK;
4745 : : }
4746 : :
4747 : : /* There is a sequence point before the call, so any side effects in
4748 : : the calling expression must occur before the actual call. Force
4749 : : gimplify_expr to use an internal post queue. */
4750 : 16269431 : ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
4751 : : is_gimple_call_addr, fb_rvalue);
4752 : :
4753 : 16269431 : if (ret == GS_ERROR)
4754 : : return GS_ERROR;
4755 : :
4756 : 16269429 : nargs = call_expr_nargs (*expr_p);
4757 : :
4758 : : /* Get argument types for verification. */
4759 : 16269429 : fndecl = get_callee_fndecl (*expr_p);
4760 : 16269429 : parms = NULL_TREE;
4761 : 16269429 : if (fndecl)
4762 : 16049354 : parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4763 : : else
4764 : 220075 : parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
4765 : :
4766 : 32318783 : if (fndecl && DECL_ARGUMENTS (fndecl))
4767 : : p = DECL_ARGUMENTS (fndecl);
4768 : 7228800 : else if (parms)
4769 : : p = parms;
4770 : : else
4771 : : p = NULL_TREE;
4772 : 43107760 : for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
4773 : : ;
4774 : :
4775 : : /* If the last argument is __builtin_va_arg_pack () and it is not
4776 : : passed as a named argument, decrease the number of CALL_EXPR
4777 : : arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
4778 : 16269429 : if (!p
4779 : 16269429 : && i < nargs
4780 : 16269429 : && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
4781 : : {
4782 : 6794 : tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
4783 : 6794 : tree last_arg_fndecl = get_callee_fndecl (last_arg);
4784 : :
4785 : 6794 : if (last_arg_fndecl
4786 : 6794 : && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
4787 : : {
4788 : 130 : tree call = *expr_p;
4789 : :
4790 : 130 : --nargs;
4791 : 260 : *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
4792 : 130 : CALL_EXPR_FN (call),
4793 : 130 : nargs, CALL_EXPR_ARGP (call));
4794 : :
4795 : : /* Copy all CALL_EXPR flags, location and block, except
4796 : : CALL_EXPR_VA_ARG_PACK flag. */
4797 : 130 : CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
4798 : 130 : CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
4799 : 130 : CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
4800 : 130 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4801 : 130 : CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
4802 : 130 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
4803 : :
4804 : : /* Set CALL_EXPR_VA_ARG_PACK. */
4805 : 130 : CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
4806 : : }
4807 : : }
4808 : :
4809 : : /* If the call returns twice then after building the CFG the call
4810 : : argument computations will no longer dominate the call because
4811 : : we add an abnormal incoming edge to the call. So do not use SSA
4812 : : vars there. */
4813 : 16269429 : bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
4814 : :
4815 : :
4816 : : /* Gimplify the function arguments. */
4817 : 16269429 : if (nargs > 0)
4818 : : {
4819 : 14098464 : for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
4820 : 42378802 : PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
4821 : 28280338 : PUSH_ARGS_REVERSED ? i-- : i++)
4822 : : {
4823 : 28280338 : enum gimplify_status t;
4824 : :
4825 : : /* Avoid gimplifying the second argument to va_start, which needs to
4826 : : be the plain PARM_DECL. */
4827 : 28280338 : if ((i != 1) || !builtin_va_start_p)
4828 : : {
4829 : 28259666 : tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
4830 : :
4831 : 28259666 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4832 : 6685 : gimplify_omp_ctxp->in_call_args = true;
4833 : 28259666 : t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
4834 : 28259666 : !returns_twice);
4835 : 28259666 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4836 : 6685 : gimplify_omp_ctxp->in_call_args = false;
4837 : :
4838 : 28259666 : if (t == GS_ERROR)
4839 : 28280338 : ret = GS_ERROR;
4840 : : }
4841 : : }
4842 : : }
4843 : :
4844 : : /* Gimplify the static chain. */
4845 : 16269429 : if (CALL_EXPR_STATIC_CHAIN (*expr_p))
4846 : : {
4847 : 28852 : if (fndecl && !DECL_STATIC_CHAIN (fndecl))
4848 : 3 : CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
4849 : : else
4850 : : {
4851 : 28173 : enum gimplify_status t;
4852 : 28173 : t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
4853 : 28173 : EXPR_LOCATION (*expr_p), ! returns_twice);
4854 : 28173 : if (t == GS_ERROR)
4855 : 16269429 : ret = GS_ERROR;
4856 : : }
4857 : : }
4858 : :
4859 : : /* Verify the function result. */
4860 : 16269429 : if (want_value && fndecl
4861 : 16269429 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
4862 : : {
4863 : 0 : error_at (loc, "using result of function returning %<void%>");
4864 : 0 : ret = GS_ERROR;
4865 : : }
4866 : :
4867 : : /* Try this again in case gimplification exposed something. */
4868 : 16269429 : if (ret != GS_ERROR)
4869 : : {
4870 : 16269146 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4871 : :
4872 : 16269146 : if (new_tree && new_tree != *expr_p)
4873 : : {
4874 : : /* There was a transformation of this call which computes the
4875 : : same value, but in a more efficient way. Return and try
4876 : : again. */
4877 : 11512 : *expr_p = new_tree;
4878 : 11512 : return GS_OK;
4879 : : }
4880 : : }
4881 : : else
4882 : : {
4883 : 283 : *expr_p = error_mark_node;
4884 : 283 : return GS_ERROR;
4885 : : }
4886 : :
4887 : : /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
4888 : : decl. This allows us to eliminate redundant or useless
4889 : : calls to "const" functions. */
4890 : 16257634 : if (TREE_CODE (*expr_p) == CALL_EXPR)
4891 : : {
4892 : 16257634 : int flags = call_expr_flags (*expr_p);
4893 : 16257634 : if (flags & (ECF_CONST | ECF_PURE)
4894 : : /* An infinite loop is considered a side effect. */
4895 : 2440228 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
4896 : 2035420 : TREE_SIDE_EFFECTS (*expr_p) = 0;
4897 : : }
4898 : :
4899 : : /* If the value is not needed by the caller, emit a new GIMPLE_CALL
4900 : : and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
4901 : : form and delegate the creation of a GIMPLE_CALL to
4902 : : gimplify_modify_expr. This is always possible because when
4903 : : WANT_VALUE is true, the caller wants the result of this call into
4904 : : a temporary, which means that we will emit an INIT_EXPR in
4905 : : internal_get_tmp_var which will then be handled by
4906 : : gimplify_modify_expr. */
4907 : 16257634 : if (!want_value)
4908 : : {
4909 : : /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
4910 : : have to do is replicate it as a GIMPLE_CALL tuple. */
4911 : 6211593 : gimple_stmt_iterator gsi;
4912 : 6211593 : call = gimple_build_call_from_tree (*expr_p, fnptrtype);
4913 : 6211593 : notice_special_calls (call);
4914 : 6211593 : gimplify_seq_add_stmt (pre_p, call);
4915 : 6211593 : gsi = gsi_last (*pre_p);
4916 : 6211593 : maybe_fold_stmt (&gsi);
4917 : 6211593 : *expr_p = NULL_TREE;
4918 : : }
4919 : : else
4920 : : /* Remember the original function type. */
4921 : 10046041 : CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
4922 : 10046041 : CALL_EXPR_FN (*expr_p));
4923 : :
4924 : : return ret;
4925 : : }
4926 : :
4927 : : /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
4928 : : rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
4929 : :
4930 : : TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
4931 : : condition is true or false, respectively. If null, we should generate
4932 : : our own to skip over the evaluation of this specific expression.
4933 : :
4934 : : LOCUS is the source location of the COND_EXPR.
4935 : :
4936 : : The condition_uid is a discriminator tag for condition coverage used to map
4937 : : conditions to its corresponding full Boolean function.
4938 : :
4939 : : This function is the tree equivalent of do_jump.
4940 : :
4941 : : shortcut_cond_r should only be called by shortcut_cond_expr. */
4942 : :
4943 : : static tree
4944 : 1325145 : shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
4945 : : location_t locus, unsigned condition_uid)
4946 : : {
4947 : 1325145 : tree local_label = NULL_TREE;
4948 : 1325145 : tree t, expr = NULL;
4949 : :
4950 : : /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
4951 : : retain the shortcut semantics. Just insert the gotos here;
4952 : : shortcut_cond_expr will append the real blocks later. */
4953 : 1325145 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
4954 : : {
4955 : 125513 : location_t new_locus;
4956 : :
4957 : : /* Turn if (a && b) into
4958 : :
4959 : : if (a); else goto no;
4960 : : if (b) goto yes; else goto no;
4961 : : (no:) */
4962 : :
4963 : 125513 : if (false_label_p == NULL)
4964 : 4001 : false_label_p = &local_label;
4965 : :
4966 : : /* Keep the original source location on the first 'if'. */
4967 : 125513 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
4968 : : condition_uid);
4969 : 125513 : append_to_statement_list (t, &expr);
4970 : :
4971 : : /* Set the source location of the && on the second 'if'. */
4972 : 125513 : new_locus = rexpr_location (pred, locus);
4973 : 125513 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4974 : : new_locus, condition_uid);
4975 : 125513 : append_to_statement_list (t, &expr);
4976 : : }
4977 : 1199632 : else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
4978 : : {
4979 : 381643 : location_t new_locus;
4980 : :
4981 : : /* Turn if (a || b) into
4982 : :
4983 : : if (a) goto yes;
4984 : : if (b) goto yes; else goto no;
4985 : : (yes:) */
4986 : :
4987 : 381643 : if (true_label_p == NULL)
4988 : 228909 : true_label_p = &local_label;
4989 : :
4990 : : /* Keep the original source location on the first 'if'. */
4991 : 381643 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
4992 : : condition_uid);
4993 : 381643 : append_to_statement_list (t, &expr);
4994 : :
4995 : : /* Set the source location of the || on the second 'if'. */
4996 : 381643 : new_locus = rexpr_location (pred, locus);
4997 : 381643 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4998 : : new_locus, condition_uid);
4999 : 381643 : append_to_statement_list (t, &expr);
5000 : : }
5001 : 817989 : else if (TREE_CODE (pred) == COND_EXPR
5002 : 848 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
5003 : 818837 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
5004 : : {
5005 : 842 : location_t new_locus;
5006 : :
5007 : : /* As long as we're messing with gotos, turn if (a ? b : c) into
5008 : : if (a)
5009 : : if (b) goto yes; else goto no;
5010 : : else
5011 : : if (c) goto yes; else goto no;
5012 : :
5013 : : Don't do this if one of the arms has void type, which can happen
5014 : : in C++ when the arm is throw. */
5015 : :
5016 : : /* Keep the original source location on the first 'if'. Set the source
5017 : : location of the ? on the second 'if'. */
5018 : 842 : new_locus = rexpr_location (pred, locus);
5019 : 1684 : expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
5020 : 842 : shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
5021 : : false_label_p, locus, condition_uid),
5022 : 842 : shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
5023 : : false_label_p, new_locus,
5024 : : condition_uid));
5025 : 842 : tree_associate_condition_with_expr (expr, condition_uid);
5026 : : }
5027 : : else
5028 : : {
5029 : 817147 : expr = build3 (COND_EXPR, void_type_node, pred,
5030 : : build_and_jump (true_label_p),
5031 : : build_and_jump (false_label_p));
5032 : 817147 : SET_EXPR_LOCATION (expr, locus);
5033 : 817147 : tree_associate_condition_with_expr (expr, condition_uid);
5034 : : }
5035 : :
5036 : 1325145 : if (local_label)
5037 : : {
5038 : 232910 : t = build1 (LABEL_EXPR, void_type_node, local_label);
5039 : 232910 : append_to_statement_list (t, &expr);
5040 : : }
5041 : :
5042 : 1325145 : return expr;
5043 : : }
5044 : :
5045 : : /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
5046 : : any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
5047 : : statement, if it is the last one. Otherwise, return NULL. */
5048 : :
5049 : : static tree
5050 : 11383644 : find_goto (tree expr)
5051 : : {
5052 : 11694585 : if (!expr)
5053 : : return NULL_TREE;
5054 : :
5055 : 9759711 : if (TREE_CODE (expr) == GOTO_EXPR)
5056 : : return expr;
5057 : :
5058 : 7784008 : if (TREE_CODE (expr) != STATEMENT_LIST)
5059 : : return NULL_TREE;
5060 : :
5061 : 990261 : tree_stmt_iterator i = tsi_start (expr);
5062 : :
5063 : 1331695 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
5064 : 341434 : tsi_next (&i);
5065 : :
5066 : 10302218 : if (!tsi_one_before_end_p (i))
5067 : : return NULL_TREE;
5068 : :
5069 : 310941 : return find_goto (tsi_stmt (i));
5070 : : }
5071 : :
5072 : : /* Same as find_goto, except that it returns NULL if the destination
5073 : : is not a LABEL_DECL. */
5074 : :
5075 : : static inline tree
5076 : 11383644 : find_goto_label (tree expr)
5077 : : {
5078 : 11383644 : tree dest = find_goto (expr);
5079 : 13359347 : if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
5080 : 1975592 : return dest;
5081 : : return NULL_TREE;
5082 : : }
5083 : :
5084 : :
5085 : : /* Given a multi-term condition (ANDIF, ORIF), walk the predicate PRED and tag
5086 : : every basic condition with CONDITION_UID. Two basic conditions share the
5087 : : CONDITION_UID discriminator when they belong to the same predicate, which is
5088 : : used by the condition coverage. Doing this as an explicit step makes for a
5089 : : simpler implementation than weaving it into the splitting code as the
5090 : : splitting code eventually calls the entry point gimplfiy_expr which makes
5091 : : bookkeeping complicated. */
5092 : : static void
5093 : 568886 : tag_shortcut_cond (tree pred, unsigned condition_uid)
5094 : : {
5095 : 607051 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
5096 : 607051 : || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5097 : : {
5098 : 559090 : tree fst = TREE_OPERAND (pred, 0);
5099 : 559090 : tree lst = TREE_OPERAND (pred, 1);
5100 : :
5101 : 559090 : if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
5102 : 559090 : || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
5103 : 167362 : tag_shortcut_cond (fst, condition_uid);
5104 : 391728 : else if (TREE_CODE (fst) == COND_EXPR)
5105 : 244 : tree_associate_condition_with_expr (fst, condition_uid);
5106 : :
5107 : 559090 : if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
5108 : 559090 : || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
5109 : : tag_shortcut_cond (lst, condition_uid);
5110 : 520925 : else if (TREE_CODE (lst) == COND_EXPR)
5111 : 438 : tree_associate_condition_with_expr (lst, condition_uid);
5112 : : }
5113 : 568886 : }
5114 : :
5115 : : /* Given a conditional expression EXPR with short-circuit boolean
5116 : : predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
5117 : : predicate apart into the equivalent sequence of conditionals. CONDITION_UID
5118 : : is a the tag/discriminator for this EXPR - all basic conditions in the
5119 : : expression will be given the same CONDITION_UID. */
5120 : : static tree
5121 : 401524 : shortcut_cond_expr (tree expr, unsigned condition_uid)
5122 : : {
5123 : 401524 : tree pred = TREE_OPERAND (expr, 0);
5124 : 401524 : tree then_ = TREE_OPERAND (expr, 1);
5125 : 401524 : tree else_ = TREE_OPERAND (expr, 2);
5126 : 401524 : tree true_label, false_label, end_label, t;
5127 : 401524 : tree *true_label_p;
5128 : 401524 : tree *false_label_p;
5129 : 401524 : bool emit_end, emit_false, jump_over_else;
5130 : 401524 : bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
5131 : 401524 : bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
5132 : :
5133 : 401524 : tag_shortcut_cond (pred, condition_uid);
5134 : :
5135 : : /* First do simple transformations. */
5136 : 401524 : if (!else_se)
5137 : : {
5138 : : /* If there is no 'else', turn
5139 : : if (a && b) then c
5140 : : into
5141 : : if (a) if (b) then c. */
5142 : 285139 : while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
5143 : : {
5144 : : /* Keep the original source location on the first 'if'. */
5145 : 48781 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5146 : 48781 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5147 : : /* Set the source location of the && on the second 'if'. */
5148 : 48781 : if (rexpr_has_location (pred))
5149 : 47761 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5150 : 48781 : then_ = shortcut_cond_expr (expr, condition_uid);
5151 : 48781 : then_se = then_ && TREE_SIDE_EFFECTS (then_);
5152 : 48781 : pred = TREE_OPERAND (pred, 0);
5153 : 48781 : expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
5154 : 48781 : SET_EXPR_LOCATION (expr, locus);
5155 : : }
5156 : : }
5157 : :
5158 : 401524 : if (!then_se)
5159 : : {
5160 : : /* If there is no 'then', turn
5161 : : if (a || b); else d
5162 : : into
5163 : : if (a); else if (b); else d. */
5164 : 26350 : while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5165 : : {
5166 : : /* Keep the original source location on the first 'if'. */
5167 : 812 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5168 : 812 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5169 : : /* Set the source location of the || on the second 'if'. */
5170 : 812 : if (rexpr_has_location (pred))
5171 : 811 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5172 : 812 : else_ = shortcut_cond_expr (expr, condition_uid);
5173 : 812 : else_se = else_ && TREE_SIDE_EFFECTS (else_);
5174 : 812 : pred = TREE_OPERAND (pred, 0);
5175 : 812 : expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
5176 : 812 : SET_EXPR_LOCATION (expr, locus);
5177 : : }
5178 : : }
5179 : :
5180 : : /* The expr tree should also have the expression id set. */
5181 : 401524 : tree_associate_condition_with_expr (expr, condition_uid);
5182 : :
5183 : : /* If we're done, great. */
5184 : 401524 : if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
5185 : 401524 : && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
5186 : 92375 : return expr;
5187 : :
5188 : : /* Otherwise we need to mess with gotos. Change
5189 : : if (a) c; else d;
5190 : : to
5191 : : if (a); else goto no;
5192 : : c; goto end;
5193 : : no: d; end:
5194 : : and recursively gimplify the condition. */
5195 : :
5196 : 309149 : true_label = false_label = end_label = NULL_TREE;
5197 : :
5198 : : /* If our arms just jump somewhere, hijack those labels so we don't
5199 : : generate jumps to jumps. */
5200 : :
5201 : 309149 : if (tree then_goto = find_goto_label (then_))
5202 : : {
5203 : 4578 : true_label = GOTO_DESTINATION (then_goto);
5204 : 4578 : then_ = NULL;
5205 : 4578 : then_se = false;
5206 : : }
5207 : :
5208 : 309149 : if (tree else_goto = find_goto_label (else_))
5209 : : {
5210 : 4172 : false_label = GOTO_DESTINATION (else_goto);
5211 : 4172 : else_ = NULL;
5212 : 4172 : else_se = false;
5213 : : }
5214 : :
5215 : : /* If we aren't hijacking a label for the 'then' branch, it falls through. */
5216 : 309149 : if (true_label)
5217 : : true_label_p = &true_label;
5218 : : else
5219 : 304571 : true_label_p = NULL;
5220 : :
5221 : : /* The 'else' branch also needs a label if it contains interesting code. */
5222 : 309149 : if (false_label || else_se)
5223 : : false_label_p = &false_label;
5224 : : else
5225 : : false_label_p = NULL;
5226 : :
5227 : : /* If there was nothing else in our arms, just forward the label(s). */
5228 : 149156 : if (!then_se && !else_se)
5229 : 8484 : return shortcut_cond_r (pred, true_label_p, false_label_p,
5230 : 12726 : EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
5231 : :
5232 : : /* If our last subexpression already has a terminal label, reuse it. */
5233 : 304907 : if (else_se)
5234 : 159993 : t = expr_last (else_);
5235 : 144914 : else if (then_se)
5236 : 144914 : t = expr_last (then_);
5237 : : else
5238 : : t = NULL;
5239 : 304907 : if (t && TREE_CODE (t) == LABEL_EXPR)
5240 : 949 : end_label = LABEL_EXPR_LABEL (t);
5241 : :
5242 : : /* If we don't care about jumping to the 'else' branch, jump to the end
5243 : : if the condition is false. */
5244 : 304907 : if (!false_label_p)
5245 : 144671 : false_label_p = &end_label;
5246 : :
5247 : : /* We only want to emit these labels if we aren't hijacking them. */
5248 : 304907 : emit_end = (end_label == NULL_TREE);
5249 : 304907 : emit_false = (false_label == NULL_TREE);
5250 : :
5251 : : /* We only emit the jump over the else clause if we have to--if the
5252 : : then clause may fall through. Otherwise we can wind up with a
5253 : : useless jump and a useless label at the end of gimplified code,
5254 : : which will cause us to think that this conditional as a whole
5255 : : falls through even if it doesn't. If we then inline a function
5256 : : which ends with such a condition, that can cause us to issue an
5257 : : inappropriate warning about control reaching the end of a
5258 : : non-void function. */
5259 : 304907 : jump_over_else = block_may_fallthru (then_);
5260 : :
5261 : 609191 : pred = shortcut_cond_r (pred, true_label_p, false_label_p,
5262 : 609191 : EXPR_LOC_OR_LOC (expr, input_location),
5263 : : condition_uid);
5264 : :
5265 : 304907 : expr = NULL;
5266 : 304907 : append_to_statement_list (pred, &expr);
5267 : :
5268 : 304907 : append_to_statement_list (then_, &expr);
5269 : 304907 : if (else_se)
5270 : : {
5271 : 159993 : if (jump_over_else)
5272 : : {
5273 : 157516 : tree last = expr_last (expr);
5274 : 157516 : t = build_and_jump (&end_label);
5275 : 157516 : if (rexpr_has_location (last))
5276 : 38089 : SET_EXPR_LOCATION (t, rexpr_location (last));
5277 : 157516 : append_to_statement_list (t, &expr);
5278 : : }
5279 : 159993 : if (emit_false)
5280 : : {
5281 : 159993 : t = build1 (LABEL_EXPR, void_type_node, false_label);
5282 : 159993 : append_to_statement_list (t, &expr);
5283 : : }
5284 : 159993 : append_to_statement_list (else_, &expr);
5285 : : }
5286 : 304907 : if (emit_end && end_label)
5287 : : {
5288 : 301243 : t = build1 (LABEL_EXPR, void_type_node, end_label);
5289 : 301243 : append_to_statement_list (t, &expr);
5290 : : }
5291 : :
5292 : 304907 : return expr;
5293 : : }
5294 : :
5295 : : /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
5296 : :
5297 : : tree
5298 : 16321375 : gimple_boolify (tree expr)
5299 : : {
5300 : 16321375 : tree type = TREE_TYPE (expr);
5301 : 16321375 : location_t loc = EXPR_LOCATION (expr);
5302 : :
5303 : 16321375 : if (TREE_CODE (expr) == NE_EXPR
5304 : 5636708 : && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
5305 : 17247179 : && integer_zerop (TREE_OPERAND (expr, 1)))
5306 : : {
5307 : 475970 : tree call = TREE_OPERAND (expr, 0);
5308 : 475970 : tree fn = get_callee_fndecl (call);
5309 : :
5310 : : /* For __builtin_expect ((long) (x), y) recurse into x as well
5311 : : if x is truth_value_p. */
5312 : 475970 : if (fn
5313 : 475397 : && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
5314 : 603811 : && call_expr_nargs (call) == 2)
5315 : : {
5316 : 127841 : tree arg = CALL_EXPR_ARG (call, 0);
5317 : 127841 : if (arg)
5318 : : {
5319 : 127841 : if (TREE_CODE (arg) == NOP_EXPR
5320 : 127841 : && TREE_TYPE (arg) == TREE_TYPE (call))
5321 : 61877 : arg = TREE_OPERAND (arg, 0);
5322 : 127841 : if (truth_value_p (TREE_CODE (arg)))
5323 : : {
5324 : 105650 : arg = gimple_boolify (arg);
5325 : 105650 : CALL_EXPR_ARG (call, 0)
5326 : 211300 : = fold_convert_loc (loc, TREE_TYPE (call), arg);
5327 : : }
5328 : : }
5329 : : }
5330 : : }
5331 : :
5332 : 16321375 : switch (TREE_CODE (expr))
5333 : : {
5334 : 1229870 : case TRUTH_AND_EXPR:
5335 : 1229870 : case TRUTH_OR_EXPR:
5336 : 1229870 : case TRUTH_XOR_EXPR:
5337 : 1229870 : case TRUTH_ANDIF_EXPR:
5338 : 1229870 : case TRUTH_ORIF_EXPR:
5339 : : /* Also boolify the arguments of truth exprs. */
5340 : 1229870 : TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
5341 : : /* FALLTHRU */
5342 : :
5343 : 1639966 : case TRUTH_NOT_EXPR:
5344 : 1639966 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5345 : :
5346 : : /* These expressions always produce boolean results. */
5347 : 1639966 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5348 : 304108 : TREE_TYPE (expr) = boolean_type_node;
5349 : : return expr;
5350 : :
5351 : 4937 : case ANNOTATE_EXPR:
5352 : 4937 : switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
5353 : : {
5354 : 4937 : case annot_expr_ivdep_kind:
5355 : 4937 : case annot_expr_unroll_kind:
5356 : 4937 : case annot_expr_no_vector_kind:
5357 : 4937 : case annot_expr_vector_kind:
5358 : 4937 : case annot_expr_parallel_kind:
5359 : 4937 : case annot_expr_maybe_infinite_kind:
5360 : 4937 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5361 : 4937 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5362 : 3025 : TREE_TYPE (expr) = boolean_type_node;
5363 : : return expr;
5364 : 0 : default:
5365 : 0 : gcc_unreachable ();
5366 : : }
5367 : :
5368 : 14676472 : default:
5369 : 14676472 : if (COMPARISON_CLASS_P (expr))
5370 : : {
5371 : : /* These expressions always produce boolean results. */
5372 : 12859222 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5373 : 1552749 : TREE_TYPE (expr) = boolean_type_node;
5374 : 12859222 : return expr;
5375 : : }
5376 : : /* Other expressions that get here must have boolean values, but
5377 : : might need to be converted to the appropriate mode. */
5378 : 1817250 : if (TREE_CODE (type) == BOOLEAN_TYPE)
5379 : : return expr;
5380 : 198293 : return fold_convert_loc (loc, boolean_type_node, expr);
5381 : : }
5382 : : }
5383 : :
5384 : : /* Given a conditional expression *EXPR_P without side effects, gimplify
5385 : : its operands. New statements are inserted to PRE_P. */
5386 : :
5387 : : static enum gimplify_status
5388 : 46764 : gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
5389 : : {
5390 : 46764 : tree expr = *expr_p, cond;
5391 : 46764 : enum gimplify_status ret, tret;
5392 : 46764 : enum tree_code code;
5393 : :
5394 : 46764 : cond = gimple_boolify (COND_EXPR_COND (expr));
5395 : :
5396 : : /* We need to handle && and || specially, as their gimplification
5397 : : creates pure cond_expr, thus leading to an infinite cycle otherwise. */
5398 : 46764 : code = TREE_CODE (cond);
5399 : 46764 : if (code == TRUTH_ANDIF_EXPR)
5400 : 32 : TREE_SET_CODE (cond, TRUTH_AND_EXPR);
5401 : 46732 : else if (code == TRUTH_ORIF_EXPR)
5402 : 4585 : TREE_SET_CODE (cond, TRUTH_OR_EXPR);
5403 : 46764 : ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
5404 : 46764 : COND_EXPR_COND (*expr_p) = cond;
5405 : :
5406 : 46764 : tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
5407 : : is_gimple_val, fb_rvalue);
5408 : 46764 : ret = MIN (ret, tret);
5409 : 46764 : tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
5410 : : is_gimple_val, fb_rvalue);
5411 : :
5412 : 46764 : return MIN (ret, tret);
5413 : : }
5414 : :
5415 : : /* Return true if evaluating EXPR could trap.
5416 : : EXPR is GENERIC, while tree_could_trap_p can be called
5417 : : only on GIMPLE. */
5418 : :
5419 : : bool
5420 : 13059335 : generic_expr_could_trap_p (tree expr)
5421 : : {
5422 : 13059335 : unsigned i, n;
5423 : :
5424 : 13059335 : if (!expr || is_gimple_val (expr))
5425 : 5365052 : return false;
5426 : :
5427 : 7694283 : if (!EXPR_P (expr) || tree_could_trap_p (expr))
5428 : 2385324 : return true;
5429 : :
5430 : 5308959 : n = TREE_OPERAND_LENGTH (expr);
5431 : 11937712 : for (i = 0; i < n; i++)
5432 : 8887009 : if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
5433 : : return true;
5434 : :
5435 : : return false;
5436 : : }
5437 : :
5438 : : /* Associate the condition STMT with the discriminator UID. STMTs that are
5439 : : broken down with ANDIF/ORIF from the same Boolean expression should be given
5440 : : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
5441 : : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
5442 : : for condition coverage. */
5443 : : static void
5444 : 5382673 : gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
5445 : : unsigned uid)
5446 : : {
5447 : 5382673 : if (!condition_coverage_flag)
5448 : : return;
5449 : :
5450 : 636 : if (!fn->cond_uids)
5451 : 135 : fn->cond_uids = new hash_map <gcond*, unsigned> ();
5452 : :
5453 : 636 : fn->cond_uids->put (stmt, uid);
5454 : : }
5455 : :
5456 : : /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
5457 : : into
5458 : :
5459 : : if (p) if (p)
5460 : : t1 = a; a;
5461 : : else or else
5462 : : t1 = b; b;
5463 : : t1;
5464 : :
5465 : : The second form is used when *EXPR_P is of type void.
5466 : :
5467 : : PRE_P points to the list where side effects that must happen before
5468 : : *EXPR_P should be stored. */
5469 : :
5470 : : static enum gimplify_status
5471 : 6151014 : gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
5472 : : {
5473 : 6151014 : tree expr = *expr_p;
5474 : 6151014 : tree type = TREE_TYPE (expr);
5475 : 6151014 : location_t loc = EXPR_LOCATION (expr);
5476 : 6151014 : tree tmp, arm1, arm2;
5477 : 6151014 : enum gimplify_status ret;
5478 : 6151014 : tree label_true, label_false, label_cont;
5479 : 6151014 : bool have_then_clause_p, have_else_clause_p;
5480 : 6151014 : gcond *cond_stmt;
5481 : 6151014 : enum tree_code pred_code;
5482 : 6151014 : gimple_seq seq = NULL;
5483 : :
5484 : : /* If this COND_EXPR has a value, copy the values into a temporary within
5485 : : the arms. */
5486 : 6151014 : if (!VOID_TYPE_P (type))
5487 : : {
5488 : 415646 : tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
5489 : 415646 : tree result;
5490 : :
5491 : : /* If either an rvalue is ok or we do not require an lvalue, create the
5492 : : temporary. But we cannot do that if the type is addressable. */
5493 : 415646 : if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
5494 : 415639 : && !TREE_ADDRESSABLE (type))
5495 : : {
5496 : 415639 : if (gimplify_ctxp->allow_rhs_cond_expr
5497 : : /* If either branch has side effects or could trap, it can't be
5498 : : evaluated unconditionally. */
5499 : 46764 : && !TREE_SIDE_EFFECTS (then_)
5500 : 46764 : && !generic_expr_could_trap_p (then_)
5501 : 46764 : && !TREE_SIDE_EFFECTS (else_)
5502 : 462403 : && !generic_expr_could_trap_p (else_))
5503 : 46764 : return gimplify_pure_cond_expr (expr_p, pre_p);
5504 : :
5505 : 368875 : tmp = create_tmp_var (type, "iftmp");
5506 : 368875 : result = tmp;
5507 : : }
5508 : :
5509 : : /* Otherwise, only create and copy references to the values. */
5510 : : else
5511 : : {
5512 : 7 : type = build_pointer_type (type);
5513 : :
5514 : 7 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5515 : 7 : then_ = build_fold_addr_expr_loc (loc, then_);
5516 : :
5517 : 7 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5518 : 7 : else_ = build_fold_addr_expr_loc (loc, else_);
5519 : :
5520 : 7 : expr
5521 : 7 : = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
5522 : :
5523 : 7 : tmp = create_tmp_var (type, "iftmp");
5524 : 7 : result = build_simple_mem_ref_loc (loc, tmp);
5525 : : }
5526 : :
5527 : : /* Build the new then clause, `tmp = then_;'. But don't build the
5528 : : assignment if the value is void; in C++ it can be if it's a throw. */
5529 : 368882 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5530 : 287322 : TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
5531 : :
5532 : : /* Similarly, build the new else clause, `tmp = else_;'. */
5533 : 368882 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5534 : 358263 : TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
5535 : :
5536 : 368882 : TREE_TYPE (expr) = void_type_node;
5537 : 368882 : recalculate_side_effects (expr);
5538 : :
5539 : : /* Move the COND_EXPR to the prequeue. */
5540 : 368882 : gimplify_stmt (&expr, pre_p);
5541 : :
5542 : 368882 : *expr_p = result;
5543 : 368882 : return GS_ALL_DONE;
5544 : : }
5545 : :
5546 : : /* Remove any COMPOUND_EXPR so the following cases will be caught. */
5547 : 5749762 : STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
5548 : 5735368 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
5549 : 80332 : gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
5550 : :
5551 : : /* Make sure the condition has BOOLEAN_TYPE. */
5552 : 5735368 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5553 : :
5554 : : /* Break apart && and || conditions. */
5555 : 5735368 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
5556 : 5735368 : || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
5557 : : {
5558 : 351931 : expr = shortcut_cond_expr (expr, next_cond_uid ());
5559 : :
5560 : 351931 : if (expr != *expr_p)
5561 : : {
5562 : 351931 : *expr_p = expr;
5563 : :
5564 : : /* We can't rely on gimplify_expr to re-gimplify the expanded
5565 : : form properly, as cleanups might cause the target labels to be
5566 : : wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
5567 : : set up a conditional context. */
5568 : 351931 : gimple_push_condition ();
5569 : 351931 : gimplify_stmt (expr_p, &seq);
5570 : 351931 : gimple_pop_condition (pre_p);
5571 : 351931 : gimple_seq_add_seq (pre_p, seq);
5572 : :
5573 : 351931 : return GS_ALL_DONE;
5574 : : }
5575 : : }
5576 : :
5577 : : /* Now do the normal gimplification. */
5578 : :
5579 : : /* Gimplify condition. */
5580 : 5383437 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
5581 : : is_gimple_condexpr_for_cond, fb_rvalue);
5582 : 5383437 : if (ret == GS_ERROR)
5583 : : return GS_ERROR;
5584 : 5382673 : gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
5585 : :
5586 : 5382673 : gimple_push_condition ();
5587 : :
5588 : 5382673 : have_then_clause_p = have_else_clause_p = false;
5589 : 5382673 : label_true = find_goto_label (TREE_OPERAND (expr, 1));
5590 : 5382673 : if (label_true
5591 : 1250492 : && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
5592 : : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5593 : : have different locations, otherwise we end up with incorrect
5594 : : location information on the branches. */
5595 : 6633165 : && (optimize
5596 : 230482 : || !EXPR_HAS_LOCATION (expr)
5597 : 203791 : || !rexpr_has_location (label_true)
5598 : 71836 : || EXPR_LOCATION (expr) == rexpr_location (label_true)))
5599 : : {
5600 : 1249347 : have_then_clause_p = true;
5601 : 1249347 : label_true = GOTO_DESTINATION (label_true);
5602 : : }
5603 : : else
5604 : 4133326 : label_true = create_artificial_label (UNKNOWN_LOCATION);
5605 : 5382673 : label_false = find_goto_label (TREE_OPERAND (expr, 2));
5606 : 5382673 : if (label_false
5607 : 716350 : && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
5608 : : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5609 : : have different locations, otherwise we end up with incorrect
5610 : : location information on the branches. */
5611 : 6099023 : && (optimize
5612 : 100524 : || !EXPR_HAS_LOCATION (expr)
5613 : 99675 : || !rexpr_has_location (label_false)
5614 : 142 : || EXPR_LOCATION (expr) == rexpr_location (label_false)))
5615 : : {
5616 : 716220 : have_else_clause_p = true;
5617 : 716220 : label_false = GOTO_DESTINATION (label_false);
5618 : : }
5619 : : else
5620 : 4666453 : label_false = create_artificial_label (UNKNOWN_LOCATION);
5621 : :
5622 : 5382673 : unsigned cond_uid = 0;
5623 : 5382673 : if (cond_uids)
5624 : 455 : if (unsigned *v = cond_uids->get (expr))
5625 : 435 : cond_uid = *v;
5626 : 435 : if (cond_uid == 0)
5627 : 5382238 : cond_uid = next_cond_uid ();
5628 : :
5629 : 5382673 : gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
5630 : : &arm2);
5631 : 5382673 : cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
5632 : : label_false);
5633 : 5382673 : gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
5634 : 5382673 : gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
5635 : 5382673 : copy_warning (cond_stmt, COND_EXPR_COND (expr));
5636 : 5382673 : gimplify_seq_add_stmt (&seq, cond_stmt);
5637 : 5382673 : gimple_stmt_iterator gsi = gsi_last (seq);
5638 : 5382673 : maybe_fold_stmt (&gsi);
5639 : :
5640 : 5382673 : label_cont = NULL_TREE;
5641 : 5382673 : if (!have_then_clause_p)
5642 : : {
5643 : : /* For if (...) {} else { code; } put label_true after
5644 : : the else block. */
5645 : 4133326 : if (TREE_OPERAND (expr, 1) == NULL_TREE
5646 : 203273 : && !have_else_clause_p
5647 : 4134111 : && TREE_OPERAND (expr, 2) != NULL_TREE)
5648 : : {
5649 : : /* For if (0) {} else { code; } tell -Wimplicit-fallthrough
5650 : : handling that label_cont == label_true can be only reached
5651 : : through fallthrough from { code; }. */
5652 : 785 : if (integer_zerop (COND_EXPR_COND (expr)))
5653 : 0 : UNUSED_LABEL_P (label_true) = 1;
5654 : : label_cont = label_true;
5655 : : }
5656 : : else
5657 : : {
5658 : 4132541 : bool then_side_effects
5659 : 4132541 : = (TREE_OPERAND (expr, 1)
5660 : 4132541 : && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
5661 : 4132541 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
5662 : 4132541 : have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
5663 : : /* For if (...) { code; } else {} or
5664 : : if (...) { code; } else goto label; or
5665 : : if (...) { code; return; } else { ... }
5666 : : label_cont isn't needed. */
5667 : 4132541 : if (!have_else_clause_p
5668 : 3928898 : && TREE_OPERAND (expr, 2) != NULL_TREE
5669 : 6858791 : && gimple_seq_may_fallthru (seq))
5670 : : {
5671 : 1909225 : gimple *g;
5672 : 1909225 : label_cont = create_artificial_label (UNKNOWN_LOCATION);
5673 : :
5674 : : /* For if (0) { non-side-effect-code } else { code }
5675 : : tell -Wimplicit-fallthrough handling that label_cont can
5676 : : be only reached through fallthrough from { code }. */
5677 : 1909225 : if (integer_zerop (COND_EXPR_COND (expr)))
5678 : : {
5679 : 66550 : UNUSED_LABEL_P (label_true) = 1;
5680 : 66550 : if (!then_side_effects)
5681 : 11453 : UNUSED_LABEL_P (label_cont) = 1;
5682 : : }
5683 : :
5684 : 1909225 : g = gimple_build_goto (label_cont);
5685 : :
5686 : : /* GIMPLE_COND's are very low level; they have embedded
5687 : : gotos. This particular embedded goto should not be marked
5688 : : with the location of the original COND_EXPR, as it would
5689 : : correspond to the COND_EXPR's condition, not the ELSE or the
5690 : : THEN arms. To avoid marking it with the wrong location, flag
5691 : : it as "no location". */
5692 : 1909225 : gimple_set_do_not_emit_location (g);
5693 : :
5694 : 1909225 : gimplify_seq_add_stmt (&seq, g);
5695 : : }
5696 : : }
5697 : : }
5698 : 5382673 : if (!have_else_clause_p)
5699 : : {
5700 : : /* For if (1) { code } or if (1) { code } else { non-side-effect-code }
5701 : : tell -Wimplicit-fallthrough handling that label_false can be only
5702 : : reached through fallthrough from { code }. */
5703 : 4666453 : if (integer_nonzerop (COND_EXPR_COND (expr))
5704 : 4666453 : && (TREE_OPERAND (expr, 2) == NULL_TREE
5705 : 56297 : || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
5706 : 6902 : UNUSED_LABEL_P (label_false) = 1;
5707 : 4666453 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
5708 : 4666453 : have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
5709 : : }
5710 : 5382673 : if (label_cont)
5711 : 1910010 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
5712 : :
5713 : 5382673 : gimple_pop_condition (pre_p);
5714 : 5382673 : gimple_seq_add_seq (pre_p, seq);
5715 : :
5716 : 5382673 : if (ret == GS_ERROR)
5717 : : ; /* Do nothing. */
5718 : 5382673 : else if (have_then_clause_p || have_else_clause_p)
5719 : : ret = GS_ALL_DONE;
5720 : : else
5721 : : {
5722 : : /* Both arms are empty; replace the COND_EXPR with its predicate. */
5723 : 2695 : expr = TREE_OPERAND (expr, 0);
5724 : 2695 : gimplify_stmt (&expr, pre_p);
5725 : : }
5726 : :
5727 : 5382673 : *expr_p = NULL;
5728 : 5382673 : return ret;
5729 : : }
5730 : :
5731 : : /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
5732 : : to be marked addressable.
5733 : :
5734 : : We cannot rely on such an expression being directly markable if a temporary
5735 : : has been created by the gimplification. In this case, we create another
5736 : : temporary and initialize it with a copy, which will become a store after we
5737 : : mark it addressable. This can happen if the front-end passed us something
5738 : : that it could not mark addressable yet, like a Fortran pass-by-reference
5739 : : parameter (int) floatvar. */
5740 : :
5741 : : static void
5742 : 55033189 : prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
5743 : : {
5744 : 60360473 : while (handled_component_p (*expr_p))
5745 : 5327284 : expr_p = &TREE_OPERAND (*expr_p, 0);
5746 : :
5747 : : /* Do not allow an SSA name as the temporary. */
5748 : 55033189 : if (is_gimple_reg (*expr_p))
5749 : 12065 : *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
5750 : 55033189 : }
5751 : :
5752 : : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5753 : : a call to __builtin_memcpy. */
5754 : :
5755 : : static enum gimplify_status
5756 : 1074 : gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
5757 : : gimple_seq *seq_p)
5758 : : {
5759 : 1074 : tree t, to, to_ptr, from, from_ptr;
5760 : 1074 : gcall *gs;
5761 : 1074 : location_t loc = EXPR_LOCATION (*expr_p);
5762 : :
5763 : 1074 : to = TREE_OPERAND (*expr_p, 0);
5764 : 1074 : from = TREE_OPERAND (*expr_p, 1);
5765 : 1074 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to)))
5766 : : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))));
5767 : :
5768 : : /* Mark the RHS addressable. Beware that it may not be possible to do so
5769 : : directly if a temporary has been created by the gimplification. */
5770 : 1074 : prepare_gimple_addressable (&from, seq_p);
5771 : :
5772 : 1074 : mark_addressable (from);
5773 : 1074 : from_ptr = build_fold_addr_expr_loc (loc, from);
5774 : 1074 : gimplify_arg (&from_ptr, seq_p, loc);
5775 : :
5776 : 1074 : mark_addressable (to);
5777 : 1074 : to_ptr = build_fold_addr_expr_loc (loc, to);
5778 : 1074 : gimplify_arg (&to_ptr, seq_p, loc);
5779 : :
5780 : 1074 : t = builtin_decl_implicit (BUILT_IN_MEMCPY);
5781 : :
5782 : 1074 : gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
5783 : 1074 : gimple_call_set_alloca_for_var (gs, true);
5784 : :
5785 : 1074 : if (want_value)
5786 : : {
5787 : : /* tmp = memcpy() */
5788 : 8 : t = create_tmp_var (TREE_TYPE (to_ptr));
5789 : 8 : gimple_call_set_lhs (gs, t);
5790 : 8 : gimplify_seq_add_stmt (seq_p, gs);
5791 : :
5792 : 8 : *expr_p = build_simple_mem_ref (t);
5793 : 8 : return GS_ALL_DONE;
5794 : : }
5795 : :
5796 : 1066 : gimplify_seq_add_stmt (seq_p, gs);
5797 : 1066 : *expr_p = NULL;
5798 : 1066 : return GS_ALL_DONE;
5799 : : }
5800 : :
5801 : : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5802 : : a call to __builtin_memset. In this case we know that the RHS is
5803 : : a CONSTRUCTOR with an empty element list. */
5804 : :
5805 : : static enum gimplify_status
5806 : 67 : gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
5807 : : gimple_seq *seq_p)
5808 : : {
5809 : 67 : tree t, from, to, to_ptr;
5810 : 67 : gcall *gs;
5811 : 67 : location_t loc = EXPR_LOCATION (*expr_p);
5812 : :
5813 : : /* Assert our assumptions, to abort instead of producing wrong code
5814 : : silently if they are not met. Beware that the RHS CONSTRUCTOR might
5815 : : not be immediately exposed. */
5816 : 67 : from = TREE_OPERAND (*expr_p, 1);
5817 : 67 : if (TREE_CODE (from) == WITH_SIZE_EXPR)
5818 : 67 : from = TREE_OPERAND (from, 0);
5819 : :
5820 : 67 : gcc_assert (TREE_CODE (from) == CONSTRUCTOR
5821 : : && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
5822 : :
5823 : : /* Now proceed. */
5824 : 67 : to = TREE_OPERAND (*expr_p, 0);
5825 : 67 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to))));
5826 : :
5827 : 67 : to_ptr = build_fold_addr_expr_loc (loc, to);
5828 : 67 : gimplify_arg (&to_ptr, seq_p, loc);
5829 : 67 : t = builtin_decl_implicit (BUILT_IN_MEMSET);
5830 : :
5831 : 67 : gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
5832 : :
5833 : 67 : if (want_value)
5834 : : {
5835 : : /* tmp = memset() */
5836 : 0 : t = create_tmp_var (TREE_TYPE (to_ptr));
5837 : 0 : gimple_call_set_lhs (gs, t);
5838 : 0 : gimplify_seq_add_stmt (seq_p, gs);
5839 : :
5840 : 0 : *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
5841 : 0 : return GS_ALL_DONE;
5842 : : }
5843 : :
5844 : 67 : gimplify_seq_add_stmt (seq_p, gs);
5845 : 67 : *expr_p = NULL;
5846 : 67 : return GS_ALL_DONE;
5847 : : }
5848 : :
5849 : : /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
5850 : : determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
5851 : : assignment. Return non-null if we detect a potential overlap. */
5852 : :
5853 : : struct gimplify_init_ctor_preeval_data
5854 : : {
5855 : : /* The base decl of the lhs object. May be NULL, in which case we
5856 : : have to assume the lhs is indirect. */
5857 : : tree lhs_base_decl;
5858 : :
5859 : : /* The alias set of the lhs object. */
5860 : : alias_set_type lhs_alias_set;
5861 : : };
5862 : :
5863 : : static tree
5864 : 115152 : gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
5865 : : {
5866 : 115152 : struct gimplify_init_ctor_preeval_data *data
5867 : : = (struct gimplify_init_ctor_preeval_data *) xdata;
5868 : 115152 : tree t = *tp;
5869 : :
5870 : : /* If we find the base object, obviously we have overlap. */
5871 : 115152 : if (data->lhs_base_decl == t)
5872 : : return t;
5873 : :
5874 : : /* If the constructor component is indirect, determine if we have a
5875 : : potential overlap with the lhs. The only bits of information we
5876 : : have to go on at this point are addressability and alias sets. */
5877 : 115151 : if ((INDIRECT_REF_P (t)
5878 : 115151 : || TREE_CODE (t) == MEM_REF)
5879 : 643 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5880 : 115663 : && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
5881 : : return t;
5882 : :
5883 : : /* If the constructor component is a call, determine if it can hide a
5884 : : potential overlap with the lhs through an INDIRECT_REF like above.
5885 : : ??? Ugh - this is completely broken. In fact this whole analysis
5886 : : doesn't look conservative. */
5887 : 114639 : if (TREE_CODE (t) == CALL_EXPR)
5888 : : {
5889 : 0 : tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
5890 : :
5891 : 0 : for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
5892 : 0 : if (POINTER_TYPE_P (TREE_VALUE (type))
5893 : 0 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5894 : 0 : && alias_sets_conflict_p (data->lhs_alias_set,
5895 : : get_alias_set
5896 : 0 : (TREE_TYPE (TREE_VALUE (type)))))
5897 : : return t;
5898 : : }
5899 : :
5900 : 114639 : if (IS_TYPE_OR_DECL_P (t))
5901 : 10964 : *walk_subtrees = 0;
5902 : : return NULL;
5903 : : }
5904 : :
5905 : : /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
5906 : : force values that overlap with the lhs (as described by *DATA)
5907 : : into temporaries. */
5908 : :
5909 : : static void
5910 : 408298 : gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5911 : : struct gimplify_init_ctor_preeval_data *data)
5912 : : {
5913 : 408298 : enum gimplify_status one;
5914 : :
5915 : : /* If the value is constant, then there's nothing to pre-evaluate. */
5916 : 408298 : if (TREE_CONSTANT (*expr_p))
5917 : : {
5918 : : /* Ensure it does not have side effects, it might contain a reference to
5919 : : the object we're initializing. */
5920 : 192440 : gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
5921 : : return;
5922 : : }
5923 : :
5924 : : /* If the type has non-trivial constructors, we can't pre-evaluate. */
5925 : 215858 : if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
5926 : : return;
5927 : :
5928 : : /* Recurse for nested constructors. */
5929 : 215858 : if (TREE_CODE (*expr_p) == CONSTRUCTOR)
5930 : : {
5931 : 73072 : unsigned HOST_WIDE_INT ix;
5932 : 73072 : constructor_elt *ce;
5933 : 73072 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
5934 : :
5935 : 269012 : FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
5936 : 195940 : gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
5937 : :
5938 : : return;
5939 : : }
5940 : :
5941 : : /* If this is a variable sized type, we must remember the size. */
5942 : 142786 : maybe_with_size_expr (expr_p);
5943 : :
5944 : : /* Gimplify the constructor element to something appropriate for the rhs
5945 : : of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
5946 : : the gimplifier will consider this a store to memory. Doing this
5947 : : gimplification now means that we won't have to deal with complicated
5948 : : language-specific trees, nor trees like SAVE_EXPR that can induce
5949 : : exponential search behavior. */
5950 : 142786 : one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
5951 : 142786 : if (one == GS_ERROR)
5952 : : {
5953 : 0 : *expr_p = NULL;
5954 : 0 : return;
5955 : : }
5956 : :
5957 : : /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
5958 : : with the lhs, since "a = { .x=a }" doesn't make sense. This will
5959 : : always be true for all scalars, since is_gimple_mem_rhs insists on a
5960 : : temporary variable for them. */
5961 : 142786 : if (DECL_P (*expr_p))
5962 : : return;
5963 : :
5964 : : /* If this is of variable size, we have no choice but to assume it doesn't
5965 : : overlap since we can't make a temporary for it. */
5966 : 103608 : if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
5967 : : return;
5968 : :
5969 : : /* Otherwise, we must search for overlap ... */
5970 : 103608 : if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
5971 : : return;
5972 : :
5973 : : /* ... and if found, force the value into a temporary. */
5974 : 513 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
5975 : : }
5976 : :
5977 : : /* A subroutine of gimplify_init_ctor_eval. Create a loop for
5978 : : a RANGE_EXPR in a CONSTRUCTOR for an array.
5979 : :
5980 : : var = lower;
5981 : : loop_entry:
5982 : : object[var] = value;
5983 : : if (var == upper)
5984 : : goto loop_exit;
5985 : : var = var + 1;
5986 : : goto loop_entry;
5987 : : loop_exit:
5988 : :
5989 : : We increment var _after_ the loop exit check because we might otherwise
5990 : : fail if upper == TYPE_MAX_VALUE (type for upper).
5991 : :
5992 : : Note that we never have to deal with SAVE_EXPRs here, because this has
5993 : : already been taken care of for us, in gimplify_init_ctor_preeval(). */
5994 : :
5995 : : static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
5996 : : gimple_seq *, bool);
5997 : :
5998 : : static void
5999 : 429 : gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
6000 : : tree value, tree array_elt_type,
6001 : : gimple_seq *pre_p, bool cleared)
6002 : : {
6003 : 429 : tree loop_entry_label, loop_exit_label, fall_thru_label;
6004 : 429 : tree var, var_type, cref, tmp;
6005 : :
6006 : 429 : loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
6007 : 429 : loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
6008 : 429 : fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
6009 : :
6010 : : /* Create and initialize the index variable. */
6011 : 429 : var_type = TREE_TYPE (upper);
6012 : 429 : var = create_tmp_var (var_type);
6013 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
6014 : :
6015 : : /* Add the loop entry label. */
6016 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
6017 : :
6018 : : /* Build the reference. */
6019 : 429 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6020 : : var, NULL_TREE, NULL_TREE);
6021 : :
6022 : : /* If we are a constructor, just call gimplify_init_ctor_eval to do
6023 : : the store. Otherwise just assign value to the reference. */
6024 : :
6025 : 429 : if (TREE_CODE (value) == CONSTRUCTOR)
6026 : : /* NB we might have to call ourself recursively through
6027 : : gimplify_init_ctor_eval if the value is a constructor. */
6028 : 111 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6029 : : pre_p, cleared);
6030 : : else
6031 : : {
6032 : 318 : if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
6033 : : != GS_ERROR)
6034 : 318 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
6035 : : }
6036 : :
6037 : : /* We exit the loop when the index var is equal to the upper bound. */
6038 : 858 : gimplify_seq_add_stmt (pre_p,
6039 : 429 : gimple_build_cond (EQ_EXPR, var, upper,
6040 : : loop_exit_label, fall_thru_label));
6041 : :
6042 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
6043 : :
6044 : : /* Otherwise, increment the index var... */
6045 : 429 : tmp = build2 (PLUS_EXPR, var_type, var,
6046 : : fold_convert (var_type, integer_one_node));
6047 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
6048 : :
6049 : : /* ...and jump back to the loop entry. */
6050 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
6051 : :
6052 : : /* Add the loop exit label. */
6053 : 429 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
6054 : 429 : }
6055 : :
6056 : : /* A subroutine of gimplify_init_constructor. Generate individual
6057 : : MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
6058 : : assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
6059 : : CONSTRUCTOR. CLEARED is true if the entire LHS object has been
6060 : : zeroed first. */
6061 : :
6062 : : static void
6063 : 1236237 : gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
6064 : : gimple_seq *pre_p, bool cleared)
6065 : : {
6066 : 1236237 : tree array_elt_type = NULL;
6067 : 1236237 : unsigned HOST_WIDE_INT ix;
6068 : 1236237 : tree purpose, value;
6069 : :
6070 : 1236237 : if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
6071 : 136714 : array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
6072 : :
6073 : 4580705 : FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
6074 : : {
6075 : 3344468 : tree cref;
6076 : :
6077 : : /* NULL values are created above for gimplification errors. */
6078 : 3344468 : if (value == NULL)
6079 : 364959 : continue;
6080 : :
6081 : 3344468 : if (cleared && initializer_zerop (value))
6082 : 364368 : continue;
6083 : :
6084 : : /* ??? Here's to hoping the front end fills in all of the indices,
6085 : : so we don't have to figure out what's missing ourselves. */
6086 : 2980100 : gcc_assert (purpose);
6087 : :
6088 : : /* Skip zero-sized fields, unless value has side-effects. This can
6089 : : happen with calls to functions returning a empty type, which
6090 : : we shouldn't discard. As a number of downstream passes don't
6091 : : expect sets of empty type fields, we rely on the gimplification of
6092 : : the MODIFY_EXPR we make below to drop the assignment statement. */
6093 : 2980100 : if (!TREE_SIDE_EFFECTS (value)
6094 : 2685787 : && TREE_CODE (purpose) == FIELD_DECL
6095 : 5266841 : && is_empty_type (TREE_TYPE (purpose)))
6096 : 162 : continue;
6097 : :
6098 : : /* If we have a RANGE_EXPR, we have to build a loop to assign the
6099 : : whole range. */
6100 : 2979938 : if (TREE_CODE (purpose) == RANGE_EXPR)
6101 : : {
6102 : 439 : tree lower = TREE_OPERAND (purpose, 0);
6103 : 439 : tree upper = TREE_OPERAND (purpose, 1);
6104 : :
6105 : : /* If the lower bound is equal to upper, just treat it as if
6106 : : upper was the index. */
6107 : 439 : if (simple_cst_equal (lower, upper))
6108 : : purpose = upper;
6109 : : else
6110 : : {
6111 : 429 : gimplify_init_ctor_eval_range (object, lower, upper, value,
6112 : : array_elt_type, pre_p, cleared);
6113 : 429 : continue;
6114 : : }
6115 : : }
6116 : :
6117 : 2979509 : if (array_elt_type)
6118 : : {
6119 : : /* Do not use bitsizetype for ARRAY_REF indices. */
6120 : 490491 : if (TYPE_DOMAIN (TREE_TYPE (object)))
6121 : 490491 : purpose
6122 : 490491 : = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
6123 : : purpose);
6124 : 490491 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6125 : : purpose, NULL_TREE, NULL_TREE);
6126 : : }
6127 : : else
6128 : : {
6129 : 2489018 : gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
6130 : 2489018 : cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
6131 : : unshare_expr (object), purpose, NULL_TREE);
6132 : : }
6133 : :
6134 : 2979509 : if (TREE_CODE (value) == CONSTRUCTOR
6135 : 2979509 : && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
6136 : 296080 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6137 : : pre_p, cleared);
6138 : 2683429 : else if (TREE_CODE (value) == RAW_DATA_CST)
6139 : : {
6140 : 31 : if (RAW_DATA_LENGTH (value) <= 32)
6141 : : {
6142 : 13 : for (unsigned int i = 0; i < (unsigned) RAW_DATA_LENGTH (value);
6143 : : ++i)
6144 : 12 : if (!cleared || RAW_DATA_POINTER (value)[i])
6145 : : {
6146 : 12 : if (i)
6147 : : {
6148 : 11 : tree p
6149 : 11 : = fold_build2 (PLUS_EXPR, TREE_TYPE (purpose),
6150 : : purpose,
6151 : : build_int_cst (TREE_TYPE (purpose),
6152 : : i));
6153 : 11 : cref = build4 (ARRAY_REF, array_elt_type,
6154 : : unshare_expr (object), p, NULL_TREE,
6155 : : NULL_TREE);
6156 : : }
6157 : 12 : tree init
6158 : 12 : = build2 (INIT_EXPR, TREE_TYPE (cref), cref,
6159 : 12 : build_int_cst (TREE_TYPE (value),
6160 : 12 : RAW_DATA_UCHAR_ELT (value, i)));
6161 : 12 : gimplify_and_add (init, pre_p);
6162 : 12 : ggc_free (init);
6163 : : }
6164 : : }
6165 : : else
6166 : : {
6167 : 30 : tree rtype = build_array_type_nelts (TREE_TYPE (value),
6168 : 30 : RAW_DATA_LENGTH (value));
6169 : 30 : tree rctor = build_constructor_single (rtype, bitsize_zero_node,
6170 : : value);
6171 : 30 : tree addr = build_fold_addr_expr (cref);
6172 : 30 : cref = build2 (MEM_REF, rtype, addr,
6173 : : build_int_cst (ptr_type_node, 0));
6174 : 30 : rctor = tree_output_constant_def (rctor);
6175 : 30 : if (!useless_type_conversion_p (rtype, TREE_TYPE (rctor)))
6176 : 4 : rctor = build1 (VIEW_CONVERT_EXPR, rtype, rctor);
6177 : 30 : if (gimplify_expr (&cref, pre_p, NULL, is_gimple_lvalue,
6178 : : fb_lvalue) != GS_ERROR)
6179 : 60 : gimplify_seq_add_stmt (pre_p,
6180 : 30 : gimple_build_assign (cref, rctor));
6181 : : }
6182 : : }
6183 : : else
6184 : : {
6185 : 2683398 : tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
6186 : 2683398 : gimplify_and_add (init, pre_p);
6187 : 2683398 : ggc_free (init);
6188 : : }
6189 : : }
6190 : 1236237 : }
6191 : :
6192 : : /* Return the appropriate RHS predicate for this LHS. */
6193 : :
6194 : : gimple_predicate
6195 : 47973892 : rhs_predicate_for (tree lhs)
6196 : : {
6197 : 47973892 : if (is_gimple_reg (lhs))
6198 : : return is_gimple_reg_rhs_or_call;
6199 : : else
6200 : 11314952 : return is_gimple_mem_rhs_or_call;
6201 : : }
6202 : :
6203 : : /* Return the initial guess for an appropriate RHS predicate for this LHS,
6204 : : before the LHS has been gimplified. */
6205 : :
6206 : : static gimple_predicate
6207 : 46770795 : initial_rhs_predicate_for (tree lhs)
6208 : : {
6209 : 46770795 : if (is_gimple_reg_type (TREE_TYPE (lhs)))
6210 : : return is_gimple_reg_rhs_or_call;
6211 : : else
6212 : 2312157 : return is_gimple_mem_rhs_or_call;
6213 : : }
6214 : :
6215 : : /* Gimplify a C99 compound literal expression. This just means adding
6216 : : the DECL_EXPR before the current statement and using its anonymous
6217 : : decl instead. */
6218 : :
6219 : : static enum gimplify_status
6220 : 36549 : gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
6221 : : bool (*gimple_test_f) (tree),
6222 : : fallback_t fallback)
6223 : : {
6224 : 36549 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
6225 : 36549 : tree decl = DECL_EXPR_DECL (decl_s);
6226 : 36549 : tree init = DECL_INITIAL (decl);
6227 : : /* Mark the decl as addressable if the compound literal
6228 : : expression is addressable now, otherwise it is marked too late
6229 : : after we gimplify the initialization expression. */
6230 : 36549 : if (TREE_ADDRESSABLE (*expr_p))
6231 : 495 : TREE_ADDRESSABLE (decl) = 1;
6232 : : /* Otherwise, if we don't need an lvalue and have a literal directly
6233 : : substitute it. Check if it matches the gimple predicate, as
6234 : : otherwise we'd generate a new temporary, and we can as well just
6235 : : use the decl we already have. */
6236 : 36054 : else if (!TREE_ADDRESSABLE (decl)
6237 : 36054 : && !TREE_THIS_VOLATILE (decl)
6238 : 36049 : && init
6239 : 36042 : && (fallback & fb_lvalue) == 0
6240 : 71978 : && gimple_test_f (init))
6241 : : {
6242 : 34862 : *expr_p = init;
6243 : 34862 : return GS_OK;
6244 : : }
6245 : :
6246 : : /* If the decl is not addressable, then it is being used in some
6247 : : expression or on the right hand side of a statement, and it can
6248 : : be put into a readonly data section. */
6249 : 1687 : if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
6250 : 1067 : TREE_READONLY (decl) = 1;
6251 : :
6252 : : /* This decl isn't mentioned in the enclosing block, so add it to the
6253 : : list of temps. FIXME it seems a bit of a kludge to say that
6254 : : anonymous artificial vars aren't pushed, but everything else is. */
6255 : 1687 : if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
6256 : 2 : gimple_add_tmp_var (decl);
6257 : :
6258 : 1687 : gimplify_and_add (decl_s, pre_p);
6259 : 1687 : *expr_p = decl;
6260 : 1687 : return GS_OK;
6261 : : }
6262 : :
6263 : : /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
6264 : : return a new CONSTRUCTOR if something changed. */
6265 : :
6266 : : static tree
6267 : 1828978 : optimize_compound_literals_in_ctor (tree orig_ctor)
6268 : : {
6269 : 1828978 : tree ctor = orig_ctor;
6270 : 1828978 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6271 : 1828978 : unsigned int idx, num = vec_safe_length (elts);
6272 : :
6273 : 6097387 : for (idx = 0; idx < num; idx++)
6274 : : {
6275 : 4268409 : tree value = (*elts)[idx].value;
6276 : 4268409 : tree newval = value;
6277 : 4268409 : if (TREE_CODE (value) == CONSTRUCTOR)
6278 : 531262 : newval = optimize_compound_literals_in_ctor (value);
6279 : 3737147 : else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6280 : : {
6281 : 79 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
6282 : 79 : tree decl = DECL_EXPR_DECL (decl_s);
6283 : 79 : tree init = DECL_INITIAL (decl);
6284 : :
6285 : 79 : if (!TREE_ADDRESSABLE (value)
6286 : 79 : && !TREE_ADDRESSABLE (decl)
6287 : 79 : && init
6288 : 79 : && TREE_CODE (init) == CONSTRUCTOR)
6289 : 48 : newval = optimize_compound_literals_in_ctor (init);
6290 : : }
6291 : 4268409 : if (newval == value)
6292 : 4268353 : continue;
6293 : :
6294 : 56 : if (ctor == orig_ctor)
6295 : : {
6296 : 45 : ctor = copy_node (orig_ctor);
6297 : 90 : CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
6298 : 45 : elts = CONSTRUCTOR_ELTS (ctor);
6299 : : }
6300 : 56 : (*elts)[idx].value = newval;
6301 : : }
6302 : 1828978 : return ctor;
6303 : : }
6304 : :
6305 : : /* A subroutine of gimplify_modify_expr. Break out elements of a
6306 : : CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
6307 : :
6308 : : Note that we still need to clear any elements that don't have explicit
6309 : : initializers, so if not all elements are initialized we keep the
6310 : : original MODIFY_EXPR, we just remove all of the constructor elements.
6311 : :
6312 : : If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
6313 : : GS_ERROR if we would have to create a temporary when gimplifying
6314 : : this constructor. Otherwise, return GS_OK.
6315 : :
6316 : : If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
6317 : :
6318 : : static enum gimplify_status
6319 : 1297668 : gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6320 : : bool want_value, bool notify_temp_creation)
6321 : : {
6322 : 1297668 : tree object, ctor, type;
6323 : 1297668 : enum gimplify_status ret;
6324 : 1297668 : vec<constructor_elt, va_gc> *elts;
6325 : 1297668 : bool cleared = false;
6326 : 1297668 : bool is_empty_ctor = false;
6327 : 1297668 : bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
6328 : :
6329 : 1297668 : gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
6330 : :
6331 : 1297668 : if (!notify_temp_creation)
6332 : : {
6333 : 1289578 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6334 : : is_gimple_lvalue, fb_lvalue);
6335 : 1289578 : if (ret == GS_ERROR)
6336 : : return ret;
6337 : : }
6338 : :
6339 : 1297668 : object = TREE_OPERAND (*expr_p, 0);
6340 : 1297668 : ctor = TREE_OPERAND (*expr_p, 1)
6341 : 1297668 : = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
6342 : 1297668 : type = TREE_TYPE (ctor);
6343 : 1297668 : elts = CONSTRUCTOR_ELTS (ctor);
6344 : 1297668 : ret = GS_ALL_DONE;
6345 : :
6346 : 1297668 : switch (TREE_CODE (type))
6347 : : {
6348 : 1291124 : case RECORD_TYPE:
6349 : 1291124 : case UNION_TYPE:
6350 : 1291124 : case QUAL_UNION_TYPE:
6351 : 1291124 : case ARRAY_TYPE:
6352 : 1291124 : {
6353 : : /* Use readonly data for initializers of this or smaller size
6354 : : regardless of the num_nonzero_elements / num_unique_nonzero_elements
6355 : : ratio. */
6356 : 1291124 : const HOST_WIDE_INT min_unique_size = 64;
6357 : : /* If num_nonzero_elements / num_unique_nonzero_elements ratio
6358 : : is smaller than this, use readonly data. */
6359 : 1291124 : const int unique_nonzero_ratio = 8;
6360 : : /* True if a single access of the object must be ensured. This is the
6361 : : case if the target is volatile, the type is non-addressable and more
6362 : : than one field need to be assigned. */
6363 : 1291124 : const bool ensure_single_access
6364 : 1291124 : = TREE_THIS_VOLATILE (object)
6365 : 222 : && !TREE_ADDRESSABLE (type)
6366 : 1291312 : && vec_safe_length (elts) > 1;
6367 : 1291124 : struct gimplify_init_ctor_preeval_data preeval_data;
6368 : 1291124 : HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
6369 : 1291124 : HOST_WIDE_INT num_unique_nonzero_elements;
6370 : 1291124 : int complete_p;
6371 : 1291124 : bool valid_const_initializer;
6372 : :
6373 : : /* Aggregate types must lower constructors to initialization of
6374 : : individual elements. The exception is that a CONSTRUCTOR node
6375 : : with no elements indicates zero-initialization of the whole. */
6376 : 1291124 : if (vec_safe_is_empty (elts))
6377 : : {
6378 : 324166 : if (notify_temp_creation)
6379 : 9621 : return GS_OK;
6380 : :
6381 : : /* The var will be initialized and so appear on lhs of
6382 : : assignment, it can't be TREE_READONLY anymore. */
6383 : 324118 : if (VAR_P (object))
6384 : 183674 : TREE_READONLY (object) = 0;
6385 : :
6386 : : is_empty_ctor = true;
6387 : 325987 : break;
6388 : : }
6389 : :
6390 : : /* Fetch information about the constructor to direct later processing.
6391 : : We might want to make static versions of it in various cases, and
6392 : : can only do so if it known to be a valid constant initializer. */
6393 : 966958 : valid_const_initializer
6394 : 966958 : = categorize_ctor_elements (ctor, &num_nonzero_elements,
6395 : : &num_unique_nonzero_elements,
6396 : : &num_ctor_elements, &complete_p);
6397 : :
6398 : : /* If a const aggregate variable is being initialized, then it
6399 : : should never be a lose to promote the variable to be static. */
6400 : 966958 : if (valid_const_initializer
6401 : 590470 : && num_nonzero_elements > 1
6402 : 439614 : && TREE_READONLY (object)
6403 : 4772 : && VAR_P (object)
6404 : 4386 : && !DECL_REGISTER (object)
6405 : 4379 : && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
6406 : 4176 : || DECL_MERGEABLE (object))
6407 : : /* For ctors that have many repeated nonzero elements
6408 : : represented through RANGE_EXPRs, prefer initializing
6409 : : those through runtime loops over copies of large amounts
6410 : : of data from readonly data section. */
6411 : 966958 : && (num_unique_nonzero_elements
6412 : 1869 : > num_nonzero_elements / unique_nonzero_ratio
6413 : 0 : || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
6414 : : <= (unsigned HOST_WIDE_INT) min_unique_size)))
6415 : : {
6416 : 1869 : if (notify_temp_creation)
6417 : : return GS_ERROR;
6418 : :
6419 : 1869 : DECL_INITIAL (object) = ctor;
6420 : 1869 : TREE_STATIC (object) = 1;
6421 : 1869 : if (!DECL_NAME (object) || DECL_NAMELESS (object))
6422 : 1687 : DECL_NAME (object) = create_tmp_var_name ("C");
6423 : 1869 : walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
6424 : :
6425 : : /* ??? C++ doesn't automatically append a .<number> to the
6426 : : assembler name, and even when it does, it looks at FE private
6427 : : data structures to figure out what that number should be,
6428 : : which are not set for this variable. I suppose this is
6429 : : important for local statics for inline functions, which aren't
6430 : : "local" in the object file sense. So in order to get a unique
6431 : : TU-local symbol, we must invoke the lhd version now. */
6432 : 1869 : lhd_set_decl_assembler_name (object);
6433 : :
6434 : 1869 : *expr_p = NULL_TREE;
6435 : 1869 : break;
6436 : : }
6437 : :
6438 : : /* The var will be initialized and so appear on lhs of
6439 : : assignment, it can't be TREE_READONLY anymore. */
6440 : 965089 : if (VAR_P (object) && !notify_temp_creation)
6441 : 765021 : TREE_READONLY (object) = 0;
6442 : :
6443 : : /* If there are "lots" of initialized elements, even discounting
6444 : : those that are not address constants (and thus *must* be
6445 : : computed at runtime), then partition the constructor into
6446 : : constant and non-constant parts. Block copy the constant
6447 : : parts in, then generate code for the non-constant parts. */
6448 : : /* TODO. There's code in cp/typeck.cc to do this. */
6449 : :
6450 : 965089 : if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
6451 : : /* store_constructor will ignore the clearing of variable-sized
6452 : : objects. Initializers for such objects must explicitly set
6453 : : every field that needs to be set. */
6454 : : cleared = false;
6455 : 965072 : else if (!complete_p)
6456 : : /* If the constructor isn't complete, clear the whole object
6457 : : beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
6458 : :
6459 : : ??? This ought not to be needed. For any element not present
6460 : : in the initializer, we should simply set them to zero. Except
6461 : : we'd need to *find* the elements that are not present, and that
6462 : : requires trickery to avoid quadratic compile-time behavior in
6463 : : large cases or excessive memory use in small cases. */
6464 : 178127 : cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
6465 : 786945 : else if (num_ctor_elements - num_nonzero_elements
6466 : 786945 : > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
6467 : 786945 : && num_nonzero_elements < num_ctor_elements / 4)
6468 : : /* If there are "lots" of zeros, it's more efficient to clear
6469 : : the memory and then set the nonzero elements. */
6470 : : cleared = true;
6471 : 786100 : else if (ensure_single_access && num_nonzero_elements == 0)
6472 : : /* If a single access to the target must be ensured and all elements
6473 : : are zero, then it's optimal to clear whatever their number. */
6474 : : cleared = true;
6475 : : /* If the object is small enough to go in registers, and it's
6476 : : not required to be constructed in memory, clear it first.
6477 : : That will avoid wasting cycles preserving any padding bits
6478 : : that might be there, and if there aren't any, the compiler
6479 : : is smart enough to optimize the clearing out. */
6480 : 786095 : else if (complete_p <= 0
6481 : 14897 : && !TREE_ADDRESSABLE (ctor)
6482 : 14883 : && !TREE_THIS_VOLATILE (object)
6483 : 14880 : && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
6484 : 793247 : && optimize)
6485 : : cleared = true;
6486 : : else
6487 : : cleared = false;
6488 : :
6489 : : /* If there are "lots" of initialized elements, and all of them
6490 : : are valid address constants, then the entire initializer can
6491 : : be dropped to memory, and then memcpy'd out. Don't do this
6492 : : for sparse arrays, though, as it's more efficient to follow
6493 : : the standard CONSTRUCTOR behavior of memset followed by
6494 : : individual element initialization. Also don't do this for small
6495 : : all-zero initializers (which aren't big enough to merit
6496 : : clearing), and don't try to make bitwise copies of
6497 : : TREE_ADDRESSABLE types. */
6498 : 965089 : if (valid_const_initializer
6499 : 588601 : && complete_p
6500 : 425099 : && !(cleared || num_nonzero_elements == 0)
6501 : 331916 : && !TREE_ADDRESSABLE (type))
6502 : : {
6503 : 330779 : HOST_WIDE_INT size = int_size_in_bytes (type);
6504 : 330779 : unsigned int align;
6505 : :
6506 : : /* ??? We can still get unbounded array types, at least
6507 : : from the C++ front end. This seems wrong, but attempt
6508 : : to work around it for now. */
6509 : 330779 : if (size < 0)
6510 : : {
6511 : 8 : size = int_size_in_bytes (TREE_TYPE (object));
6512 : 8 : if (size >= 0)
6513 : 0 : TREE_TYPE (ctor) = type = TREE_TYPE (object);
6514 : : }
6515 : :
6516 : : /* Find the maximum alignment we can assume for the object. */
6517 : : /* ??? Make use of DECL_OFFSET_ALIGN. */
6518 : 330779 : if (DECL_P (object))
6519 : 317277 : align = DECL_ALIGN (object);
6520 : : else
6521 : 13502 : align = TYPE_ALIGN (type);
6522 : :
6523 : : /* Do a block move either if the size is so small as to make
6524 : : each individual move a sub-unit move on average, or if it
6525 : : is so large as to make individual moves inefficient. */
6526 : 330779 : if (size > 0
6527 : 330771 : && num_nonzero_elements > 1
6528 : : /* For ctors that have many repeated nonzero elements
6529 : : represented through RANGE_EXPRs, prefer initializing
6530 : : those through runtime loops over copies of large amounts
6531 : : of data from readonly data section. */
6532 : 299684 : && (num_unique_nonzero_elements
6533 : 299684 : > num_nonzero_elements / unique_nonzero_ratio
6534 : 67 : || size <= min_unique_size)
6535 : 630396 : && (size < num_nonzero_elements
6536 : 299511 : || !can_move_by_pieces (size, align)))
6537 : : {
6538 : 2760 : if (notify_temp_creation)
6539 : : return GS_ERROR;
6540 : :
6541 : 1380 : walk_tree (&ctor, force_labels_r, NULL, NULL);
6542 : 1380 : ctor = tree_output_constant_def (ctor);
6543 : 1380 : if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
6544 : 0 : ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
6545 : 1380 : TREE_OPERAND (*expr_p, 1) = ctor;
6546 : :
6547 : : /* This is no longer an assignment of a CONSTRUCTOR, but
6548 : : we still may have processing to do on the LHS. So
6549 : : pretend we didn't do anything here to let that happen. */
6550 : 1380 : return GS_UNHANDLED;
6551 : : }
6552 : : }
6553 : :
6554 : : /* If a single access to the target must be ensured and there are
6555 : : nonzero elements or the zero elements are not assigned en masse,
6556 : : initialize the target from a temporary. */
6557 : 962329 : if (ensure_single_access && (num_nonzero_elements > 0 || !cleared))
6558 : : {
6559 : 157 : if (notify_temp_creation)
6560 : : return GS_ERROR;
6561 : :
6562 : 151 : tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
6563 : 151 : TREE_OPERAND (*expr_p, 0) = temp;
6564 : 151 : *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
6565 : : *expr_p,
6566 : : build2 (MODIFY_EXPR, void_type_node,
6567 : : object, temp));
6568 : 151 : return GS_OK;
6569 : : }
6570 : :
6571 : 962172 : if (notify_temp_creation)
6572 : : return GS_OK;
6573 : :
6574 : : /* If there are nonzero elements and if needed, pre-evaluate to capture
6575 : : elements overlapping with the lhs into temporaries. We must do this
6576 : : before clearing to fetch the values before they are zeroed-out. */
6577 : 955516 : if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
6578 : : {
6579 : 212358 : preeval_data.lhs_base_decl = get_base_address (object);
6580 : 212358 : if (!DECL_P (preeval_data.lhs_base_decl))
6581 : 40666 : preeval_data.lhs_base_decl = NULL;
6582 : 212358 : preeval_data.lhs_alias_set = get_alias_set (object);
6583 : :
6584 : 212358 : gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
6585 : : pre_p, post_p, &preeval_data);
6586 : : }
6587 : :
6588 : 955516 : bool ctor_has_side_effects_p
6589 : 955516 : = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
6590 : :
6591 : 955516 : if (cleared)
6592 : : {
6593 : : /* Zap the CONSTRUCTOR element list, which simplifies this case.
6594 : : Note that we still have to gimplify, in order to handle the
6595 : : case of variable sized types. Avoid shared tree structures. */
6596 : 185021 : CONSTRUCTOR_ELTS (ctor) = NULL;
6597 : 185021 : TREE_SIDE_EFFECTS (ctor) = 0;
6598 : 185021 : object = unshare_expr (object);
6599 : 185021 : gimplify_stmt (expr_p, pre_p);
6600 : : }
6601 : :
6602 : : /* If we have not block cleared the object, or if there are nonzero
6603 : : elements in the constructor, or if the constructor has side effects,
6604 : : add assignments to the individual scalar fields of the object. */
6605 : 185021 : if (!cleared
6606 : 185021 : || num_nonzero_elements > 0
6607 : 15841 : || ctor_has_side_effects_p)
6608 : 940046 : gimplify_init_ctor_eval (object, elts, pre_p, cleared);
6609 : :
6610 : 955516 : *expr_p = NULL_TREE;
6611 : : }
6612 : 955516 : break;
6613 : :
6614 : 0 : case COMPLEX_TYPE:
6615 : 0 : {
6616 : 0 : tree r, i;
6617 : :
6618 : 0 : if (notify_temp_creation)
6619 : : return GS_OK;
6620 : :
6621 : : /* Extract the real and imaginary parts out of the ctor. */
6622 : 0 : gcc_assert (elts->length () == 2);
6623 : 0 : r = (*elts)[0].value;
6624 : 0 : i = (*elts)[1].value;
6625 : 0 : if (r == NULL || i == NULL)
6626 : : {
6627 : 0 : tree zero = build_zero_cst (TREE_TYPE (type));
6628 : 0 : if (r == NULL)
6629 : 0 : r = zero;
6630 : 0 : if (i == NULL)
6631 : 0 : i = zero;
6632 : : }
6633 : :
6634 : : /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
6635 : : represent creation of a complex value. */
6636 : 0 : if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
6637 : : {
6638 : 0 : ctor = build_complex (type, r, i);
6639 : 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6640 : : }
6641 : : else
6642 : : {
6643 : 0 : ctor = build2 (COMPLEX_EXPR, type, r, i);
6644 : 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6645 : 0 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
6646 : : pre_p,
6647 : : post_p,
6648 : 0 : rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
6649 : : fb_rvalue);
6650 : : }
6651 : : }
6652 : : break;
6653 : :
6654 : 6544 : case VECTOR_TYPE:
6655 : 6544 : {
6656 : 6544 : unsigned HOST_WIDE_INT ix;
6657 : 6544 : constructor_elt *ce;
6658 : :
6659 : 6544 : if (notify_temp_creation)
6660 : 1297668 : return GS_OK;
6661 : :
6662 : : /* Vector types use CONSTRUCTOR all the way through gimple
6663 : : compilation as a general initializer. */
6664 : 60973 : FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
6665 : : {
6666 : 54429 : enum gimplify_status tret;
6667 : 54429 : tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
6668 : : fb_rvalue);
6669 : 54429 : if (tret == GS_ERROR)
6670 : : ret = GS_ERROR;
6671 : 54428 : else if (TREE_STATIC (ctor)
6672 : 54460 : && !initializer_constant_valid_p (ce->value,
6673 : 32 : TREE_TYPE (ce->value)))
6674 : 32 : TREE_STATIC (ctor) = 0;
6675 : : }
6676 : 6544 : recompute_constructor_flags (ctor);
6677 : :
6678 : : /* Go ahead and simplify constant constructors to VECTOR_CST. */
6679 : 6544 : if (TREE_CONSTANT (ctor))
6680 : : {
6681 : 697 : bool constant_p = true;
6682 : : tree value;
6683 : :
6684 : : /* Even when ctor is constant, it might contain non-*_CST
6685 : : elements, such as addresses or trapping values like
6686 : : 1.0/0.0 - 1.0/0.0. Such expressions don't belong
6687 : : in VECTOR_CST nodes. */
6688 : 697 : FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
6689 : 478 : if (!CONSTANT_CLASS_P (value))
6690 : : {
6691 : : constant_p = false;
6692 : : break;
6693 : : }
6694 : :
6695 : 219 : if (constant_p)
6696 : : {
6697 : 219 : TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
6698 : 219 : break;
6699 : : }
6700 : : }
6701 : :
6702 : 6325 : if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
6703 : 915 : TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
6704 : : }
6705 : : break;
6706 : :
6707 : 0 : default:
6708 : : /* So how did we get a CONSTRUCTOR for a scalar type? */
6709 : 0 : gcc_unreachable ();
6710 : : }
6711 : :
6712 : 1288047 : if (ret == GS_ERROR)
6713 : : return GS_ERROR;
6714 : : /* If we have gimplified both sides of the initializer but have
6715 : : not emitted an assignment, do so now. */
6716 : 1288046 : if (*expr_p
6717 : : /* If the type is an empty type, we don't need to emit the
6718 : : assignment. */
6719 : 1288046 : && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
6720 : : {
6721 : 326855 : tree lhs = TREE_OPERAND (*expr_p, 0);
6722 : 326855 : tree rhs = TREE_OPERAND (*expr_p, 1);
6723 : 326855 : if (want_value && object == lhs)
6724 : 12 : lhs = unshare_expr (lhs);
6725 : 326855 : gassign *init = gimple_build_assign (lhs, rhs);
6726 : 326855 : gimplify_seq_add_stmt (pre_p, init);
6727 : : }
6728 : 1288046 : if (want_value)
6729 : : {
6730 : 19 : *expr_p = object;
6731 : 19 : ret = GS_OK;
6732 : : }
6733 : : else
6734 : : {
6735 : 1288027 : *expr_p = NULL;
6736 : 1288027 : ret = GS_ALL_DONE;
6737 : : }
6738 : :
6739 : : /* If the user requests to initialize automatic variables, we
6740 : : should initialize paddings inside the variable. Add a call to
6741 : : __builtin_clear_pading (&object, 0, for_auto_init = true) to
6742 : : initialize paddings of object always to zero regardless of
6743 : : INIT_TYPE. Note, we will not insert this call if the aggregate
6744 : : variable has be completely cleared already or it's initialized
6745 : : with an empty constructor. We cannot insert this call if the
6746 : : variable is a gimple register since __builtin_clear_padding will take
6747 : : the address of the variable. As a result, if a long double/_Complex long
6748 : : double variable will be spilled into stack later, its padding cannot
6749 : : be cleared with __builtin_clear_padding. We should clear its padding
6750 : : when it is spilled into memory. */
6751 : 1288046 : if (is_init_expr
6752 : 917519 : && !is_gimple_reg (object)
6753 : 913540 : && clear_padding_type_may_have_padding_p (type)
6754 : 874583 : && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
6755 : 218525 : || !AGGREGATE_TYPE_P (type))
6756 : 1944104 : && is_var_need_auto_init (object))
6757 : 38 : gimple_add_padding_init_for_auto_var (object, false, pre_p);
6758 : :
6759 : : return ret;
6760 : : }
6761 : :
6762 : : /* Given a pointer value OP0, return a simplified version of an
6763 : : indirection through OP0, or NULL_TREE if no simplification is
6764 : : possible. This may only be applied to a rhs of an expression.
6765 : : Note that the resulting type may be different from the type pointed
6766 : : to in the sense that it is still compatible from the langhooks
6767 : : point of view. */
6768 : :
6769 : : static tree
6770 : 528315 : gimple_fold_indirect_ref_rhs (tree t)
6771 : : {
6772 : 0 : return gimple_fold_indirect_ref (t);
6773 : : }
6774 : :
6775 : : /* Subroutine of gimplify_modify_expr to do simplifications of
6776 : : MODIFY_EXPRs based on the code of the RHS. We loop for as long as
6777 : : something changes. */
6778 : :
6779 : : static enum gimplify_status
6780 : 95568000 : gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
6781 : : gimple_seq *pre_p, gimple_seq *post_p,
6782 : : bool want_value)
6783 : : {
6784 : 95568000 : enum gimplify_status ret = GS_UNHANDLED;
6785 : 96229731 : bool changed;
6786 : :
6787 : 94789119 : do
6788 : : {
6789 : 96229731 : changed = false;
6790 : 96229731 : switch (TREE_CODE (*from_p))
6791 : : {
6792 : 11815775 : case VAR_DECL:
6793 : : /* If we're assigning from a read-only variable initialized with
6794 : : a constructor and not volatile, do the direct assignment from
6795 : : the constructor, but only if the target is not volatile either
6796 : : since this latter assignment might end up being done on a per
6797 : : field basis. However, if the target is volatile and the type
6798 : : is aggregate and non-addressable, gimplify_init_constructor
6799 : : knows that it needs to ensure a single access to the target
6800 : : and it will return GS_OK only in this case. */
6801 : 11815775 : if (TREE_READONLY (*from_p)
6802 : 101980 : && DECL_INITIAL (*from_p)
6803 : 65652 : && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
6804 : 8090 : && !TREE_THIS_VOLATILE (*from_p)
6805 : 11823865 : && (!TREE_THIS_VOLATILE (*to_p)
6806 : 6 : || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
6807 : 6 : && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
6808 : : {
6809 : 8090 : tree old_from = *from_p;
6810 : 8090 : enum gimplify_status subret;
6811 : :
6812 : : /* Move the constructor into the RHS. */
6813 : 8090 : *from_p = unshare_expr (DECL_INITIAL (*from_p));
6814 : :
6815 : : /* Let's see if gimplify_init_constructor will need to put
6816 : : it in memory. */
6817 : 8090 : subret = gimplify_init_constructor (expr_p, NULL, NULL,
6818 : : false, true);
6819 : 8090 : if (subret == GS_ERROR)
6820 : : {
6821 : : /* If so, revert the change. */
6822 : 1386 : *from_p = old_from;
6823 : : }
6824 : : else
6825 : : {
6826 : : ret = GS_OK;
6827 : : changed = true;
6828 : : }
6829 : : }
6830 : : break;
6831 : 533425 : case INDIRECT_REF:
6832 : 533425 : if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
6833 : : /* If we have code like
6834 : :
6835 : : *(const A*)(A*)&x
6836 : :
6837 : : where the type of "x" is a (possibly cv-qualified variant
6838 : : of "A"), treat the entire expression as identical to "x".
6839 : : This kind of code arises in C++ when an object is bound
6840 : : to a const reference, and if "x" is a TARGET_EXPR we want
6841 : : to take advantage of the optimization below. But not if
6842 : : the type is TREE_ADDRESSABLE; then C++17 says that the
6843 : : TARGET_EXPR needs to be a temporary. */
6844 : 1056630 : if (tree t
6845 : 528315 : = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
6846 : : {
6847 : 136970 : bool volatile_p = TREE_THIS_VOLATILE (*from_p);
6848 : 136970 : if (TREE_THIS_VOLATILE (t) != volatile_p)
6849 : : {
6850 : 5 : if (DECL_P (t))
6851 : 0 : t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
6852 : : build_fold_addr_expr (t));
6853 : 5 : if (REFERENCE_CLASS_P (t))
6854 : 5 : TREE_THIS_VOLATILE (t) = volatile_p;
6855 : : }
6856 : 136970 : *from_p = t;
6857 : 136970 : ret = GS_OK;
6858 : 136970 : changed = true;
6859 : : }
6860 : : break;
6861 : :
6862 : 215896 : case TARGET_EXPR:
6863 : 215896 : {
6864 : : /* If we are initializing something from a TARGET_EXPR, strip the
6865 : : TARGET_EXPR and initialize it directly, if possible. This can't
6866 : : be done if the initializer is void, since that implies that the
6867 : : temporary is set in some non-trivial way.
6868 : :
6869 : : ??? What about code that pulls out the temp and uses it
6870 : : elsewhere? I think that such code never uses the TARGET_EXPR as
6871 : : an initializer. If I'm wrong, we'll die because the temp won't
6872 : : have any RTL. In that case, I guess we'll need to replace
6873 : : references somehow. */
6874 : 215896 : tree init = TARGET_EXPR_INITIAL (*from_p);
6875 : :
6876 : 215896 : if (init
6877 : 215180 : && (TREE_CODE (*expr_p) != MODIFY_EXPR
6878 : 67141 : || !TARGET_EXPR_NO_ELIDE (*from_p))
6879 : 430786 : && !VOID_TYPE_P (TREE_TYPE (init)))
6880 : : {
6881 : 189912 : *from_p = init;
6882 : 189912 : ret = GS_OK;
6883 : 189912 : changed = true;
6884 : : }
6885 : : }
6886 : : break;
6887 : :
6888 : 327936 : case COMPOUND_EXPR:
6889 : : /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
6890 : : caught. */
6891 : 327936 : gimplify_compound_expr (from_p, pre_p, true);
6892 : 327936 : ret = GS_OK;
6893 : 327936 : changed = true;
6894 : 327936 : break;
6895 : :
6896 : 1311140 : case CONSTRUCTOR:
6897 : : /* If we already made some changes, let the front end have a
6898 : : crack at this before we break it down. */
6899 : 1311140 : if (ret != GS_UNHANDLED)
6900 : : break;
6901 : :
6902 : : /* If we're initializing from a CONSTRUCTOR, break this into
6903 : : individual MODIFY_EXPRs. */
6904 : 1289578 : ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
6905 : : false);
6906 : 1289578 : return ret;
6907 : :
6908 : 227182 : case COND_EXPR:
6909 : : /* If we're assigning to a non-register type, push the assignment
6910 : : down into the branches. This is mandatory for ADDRESSABLE types,
6911 : : since we cannot generate temporaries for such, but it saves a
6912 : : copy in other cases as well.
6913 : : Also avoid an extra temporary and copy when assigning to
6914 : : a register. */
6915 : 227182 : if (!is_gimple_reg_type (TREE_TYPE (*from_p))
6916 : 227182 : || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
6917 : : {
6918 : : /* This code should mirror the code in gimplify_cond_expr. */
6919 : 145195 : enum tree_code code = TREE_CODE (*expr_p);
6920 : 145195 : tree cond = *from_p;
6921 : 145195 : tree result = *to_p;
6922 : :
6923 : 145195 : ret = gimplify_expr (&result, pre_p, post_p,
6924 : : is_gimple_lvalue, fb_lvalue);
6925 : 145195 : if (ret != GS_ERROR)
6926 : 145195 : ret = GS_OK;
6927 : :
6928 : : /* If we are going to write RESULT more than once, clear
6929 : : TREE_READONLY flag, otherwise we might incorrectly promote
6930 : : the variable to static const and initialize it at compile
6931 : : time in one of the branches. */
6932 : 145195 : if (VAR_P (result)
6933 : 144147 : && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
6934 : 280269 : && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6935 : 134979 : TREE_READONLY (result) = 0;
6936 : 145195 : if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
6937 : 135986 : TREE_OPERAND (cond, 1)
6938 : 271972 : = build2 (code, void_type_node, result,
6939 : 135986 : TREE_OPERAND (cond, 1));
6940 : 145195 : if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6941 : 145100 : TREE_OPERAND (cond, 2)
6942 : 290200 : = build2 (code, void_type_node, unshare_expr (result),
6943 : 145100 : TREE_OPERAND (cond, 2));
6944 : :
6945 : 145195 : TREE_TYPE (cond) = void_type_node;
6946 : 145195 : recalculate_side_effects (cond);
6947 : :
6948 : 145195 : if (want_value)
6949 : : {
6950 : 66 : gimplify_and_add (cond, pre_p);
6951 : 66 : *expr_p = unshare_expr (result);
6952 : : }
6953 : : else
6954 : 145129 : *expr_p = cond;
6955 : 145195 : return ret;
6956 : : }
6957 : : break;
6958 : :
6959 : 9625001 : case CALL_EXPR:
6960 : : /* For calls that return in memory, give *to_p as the CALL_EXPR's
6961 : : return slot so that we don't generate a temporary. */
6962 : 9625001 : if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
6963 : 9625001 : && aggregate_value_p (*from_p, *from_p))
6964 : : {
6965 : 250873 : bool use_target;
6966 : :
6967 : 250873 : if (!(rhs_predicate_for (*to_p))(*from_p))
6968 : : /* If we need a temporary, *to_p isn't accurate. */
6969 : : use_target = false;
6970 : : /* It's OK to use the return slot directly unless it's an NRV. */
6971 : 250130 : else if (TREE_CODE (*to_p) == RESULT_DECL
6972 : 5037 : && DECL_NAME (*to_p) == NULL_TREE
6973 : 255031 : && needs_to_live_in_memory (*to_p))
6974 : : use_target = true;
6975 : 245229 : else if (is_gimple_reg_type (TREE_TYPE (*to_p))
6976 : 245229 : || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
6977 : : /* Don't force regs into memory. */
6978 : : use_target = false;
6979 : 231177 : else if (TREE_CODE (*expr_p) == INIT_EXPR)
6980 : : /* It's OK to use the target directly if it's being
6981 : : initialized. */
6982 : : use_target = true;
6983 : 13695 : else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
6984 : : != INTEGER_CST)
6985 : : /* Always use the target and thus RSO for variable-sized types.
6986 : : GIMPLE cannot deal with a variable-sized assignment
6987 : : embedded in a call statement. */
6988 : : use_target = true;
6989 : 13667 : else if (TREE_CODE (*to_p) != SSA_NAME
6990 : 13667 : && (!is_gimple_variable (*to_p)
6991 : 10653 : || needs_to_live_in_memory (*to_p)))
6992 : : /* Don't use the original target if it's already addressable;
6993 : : if its address escapes, and the called function uses the
6994 : : NRV optimization, a conforming program could see *to_p
6995 : : change before the called function returns; see c++/19317.
6996 : : When optimizing, the return_slot pass marks more functions
6997 : : as safe after we have escape info. */
6998 : : use_target = false;
6999 : : else
7000 : : use_target = true;
7001 : :
7002 : : if (use_target)
7003 : : {
7004 : 224474 : CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
7005 : 224474 : mark_addressable (*to_p);
7006 : : }
7007 : : }
7008 : : break;
7009 : :
7010 : 1374 : case WITH_SIZE_EXPR:
7011 : : /* Likewise for calls that return an aggregate of non-constant size,
7012 : : since we would not be able to generate a temporary at all. */
7013 : 1374 : if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
7014 : : {
7015 : 196 : *from_p = TREE_OPERAND (*from_p, 0);
7016 : : /* We don't change ret in this case because the
7017 : : WITH_SIZE_EXPR might have been added in
7018 : : gimplify_modify_expr, so returning GS_OK would lead to an
7019 : : infinite loop. */
7020 : 196 : changed = true;
7021 : : }
7022 : : break;
7023 : :
7024 : : /* If we're initializing from a container, push the initialization
7025 : : inside it. */
7026 : 1681 : case CLEANUP_POINT_EXPR:
7027 : 1681 : case BIND_EXPR:
7028 : 1681 : case STATEMENT_LIST:
7029 : 1681 : {
7030 : 1681 : tree wrap = *from_p;
7031 : 1681 : tree t;
7032 : :
7033 : 1681 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
7034 : : fb_lvalue);
7035 : 1681 : if (ret != GS_ERROR)
7036 : 1681 : ret = GS_OK;
7037 : :
7038 : 1681 : t = voidify_wrapper_expr (wrap, *expr_p);
7039 : 1681 : gcc_assert (t == *expr_p);
7040 : :
7041 : 1681 : if (want_value)
7042 : : {
7043 : 6 : gimplify_and_add (wrap, pre_p);
7044 : 6 : *expr_p = unshare_expr (*to_p);
7045 : : }
7046 : : else
7047 : 1675 : *expr_p = wrap;
7048 : : return GS_OK;
7049 : : }
7050 : :
7051 : 9209491 : case NOP_EXPR:
7052 : : /* Pull out compound literal expressions from a NOP_EXPR.
7053 : : Those are created in the C FE to drop qualifiers during
7054 : : lvalue conversion. */
7055 : 9209491 : if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
7056 : 9209491 : && tree_ssa_useless_type_conversion (*from_p))
7057 : : {
7058 : 13 : *from_p = TREE_OPERAND (*from_p, 0);
7059 : 13 : ret = GS_OK;
7060 : 13 : changed = true;
7061 : : }
7062 : : break;
7063 : :
7064 : 4158 : case COMPOUND_LITERAL_EXPR:
7065 : 4158 : {
7066 : 4158 : tree complit = TREE_OPERAND (*expr_p, 1);
7067 : 4158 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
7068 : 4158 : tree decl = DECL_EXPR_DECL (decl_s);
7069 : 4158 : tree init = DECL_INITIAL (decl);
7070 : :
7071 : : /* struct T x = (struct T) { 0, 1, 2 } can be optimized
7072 : : into struct T x = { 0, 1, 2 } if the address of the
7073 : : compound literal has never been taken. */
7074 : 4158 : if (!TREE_ADDRESSABLE (complit)
7075 : 4158 : && !TREE_ADDRESSABLE (decl)
7076 : 4158 : && init)
7077 : : {
7078 : 4158 : *expr_p = copy_node (*expr_p);
7079 : 4158 : TREE_OPERAND (*expr_p, 1) = init;
7080 : 4158 : return GS_OK;
7081 : : }
7082 : : }
7083 : :
7084 : : default:
7085 : : break;
7086 : : }
7087 : : }
7088 : : while (changed);
7089 : :
7090 : : return ret;
7091 : : }
7092 : :
7093 : :
7094 : : /* Return true if T looks like a valid GIMPLE statement. */
7095 : :
7096 : : static bool
7097 : 19299687 : is_gimple_stmt (tree t)
7098 : : {
7099 : 19299687 : const enum tree_code code = TREE_CODE (t);
7100 : :
7101 : 19299687 : switch (code)
7102 : : {
7103 : 1690277 : case NOP_EXPR:
7104 : : /* The only valid NOP_EXPR is the empty statement. */
7105 : 1690277 : return IS_EMPTY_STMT (t);
7106 : :
7107 : 0 : case BIND_EXPR:
7108 : 0 : case COND_EXPR:
7109 : : /* These are only valid if they're void. */
7110 : 0 : return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
7111 : :
7112 : : case SWITCH_EXPR:
7113 : : case GOTO_EXPR:
7114 : : case RETURN_EXPR:
7115 : : case LABEL_EXPR:
7116 : : case CASE_LABEL_EXPR:
7117 : : case TRY_CATCH_EXPR:
7118 : : case TRY_FINALLY_EXPR:
7119 : : case EH_FILTER_EXPR:
7120 : : case CATCH_EXPR:
7121 : : case ASM_EXPR:
7122 : : case STATEMENT_LIST:
7123 : : case OACC_PARALLEL:
7124 : : case OACC_KERNELS:
7125 : : case OACC_SERIAL:
7126 : : case OACC_DATA:
7127 : : case OACC_HOST_DATA:
7128 : : case OACC_DECLARE:
7129 : : case OACC_UPDATE:
7130 : : case OACC_ENTER_DATA:
7131 : : case OACC_EXIT_DATA:
7132 : : case OACC_CACHE:
7133 : : case OMP_PARALLEL:
7134 : : case OMP_FOR:
7135 : : case OMP_SIMD:
7136 : : case OMP_DISTRIBUTE:
7137 : : case OMP_LOOP:
7138 : : case OMP_TILE:
7139 : : case OMP_UNROLL:
7140 : : case OACC_LOOP:
7141 : : case OMP_SCAN:
7142 : : case OMP_SCOPE:
7143 : : case OMP_DISPATCH:
7144 : : case OMP_SECTIONS:
7145 : : case OMP_SECTION:
7146 : : case OMP_STRUCTURED_BLOCK:
7147 : : case OMP_SINGLE:
7148 : : case OMP_MASTER:
7149 : : case OMP_MASKED:
7150 : : case OMP_TASKGROUP:
7151 : : case OMP_ORDERED:
7152 : : case OMP_CRITICAL:
7153 : : case OMP_METADIRECTIVE:
7154 : : case OMP_TASK:
7155 : : case OMP_TARGET:
7156 : : case OMP_TARGET_DATA:
7157 : : case OMP_TARGET_UPDATE:
7158 : : case OMP_TARGET_ENTER_DATA:
7159 : : case OMP_TARGET_EXIT_DATA:
7160 : : case OMP_TASKLOOP:
7161 : : case OMP_TEAMS:
7162 : : /* These are always void. */
7163 : : return true;
7164 : :
7165 : : case CALL_EXPR:
7166 : : case MODIFY_EXPR:
7167 : : case PREDICT_EXPR:
7168 : : /* These are valid regardless of their type. */
7169 : : return true;
7170 : :
7171 : : default:
7172 : : return false;
7173 : : }
7174 : : }
7175 : :
7176 : :
7177 : : /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
7178 : : a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a gimple register.
7179 : :
7180 : : IMPORTANT NOTE: This promotion is performed by introducing a load of the
7181 : : other, unmodified part of the complex object just before the total store.
7182 : : As a consequence, if the object is still uninitialized, an undefined value
7183 : : will be loaded into a register, which may result in a spurious exception
7184 : : if the register is floating-point and the value happens to be a signaling
7185 : : NaN for example. Then the fully-fledged complex operations lowering pass
7186 : : followed by a DCE pass are necessary in order to fix things up. */
7187 : :
7188 : : static enum gimplify_status
7189 : 1960 : gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
7190 : : bool want_value)
7191 : : {
7192 : 1960 : enum tree_code code, ocode;
7193 : 1960 : tree lhs, rhs, new_rhs, other, realpart, imagpart;
7194 : :
7195 : 1960 : lhs = TREE_OPERAND (*expr_p, 0);
7196 : 1960 : rhs = TREE_OPERAND (*expr_p, 1);
7197 : 1960 : code = TREE_CODE (lhs);
7198 : 1960 : lhs = TREE_OPERAND (lhs, 0);
7199 : :
7200 : 1960 : ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
7201 : 1960 : other = build1 (ocode, TREE_TYPE (rhs), lhs);
7202 : 1960 : suppress_warning (other);
7203 : 1960 : other = get_formal_tmp_var (other, pre_p);
7204 : :
7205 : 1960 : realpart = code == REALPART_EXPR ? rhs : other;
7206 : 966 : imagpart = code == REALPART_EXPR ? other : rhs;
7207 : :
7208 : 1960 : if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
7209 : 0 : new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
7210 : : else
7211 : 1960 : new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
7212 : :
7213 : 1960 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
7214 : 1960 : *expr_p = (want_value) ? rhs : NULL_TREE;
7215 : :
7216 : 1960 : return GS_ALL_DONE;
7217 : : }
7218 : :
7219 : : /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
7220 : :
7221 : : modify_expr
7222 : : : varname '=' rhs
7223 : : | '*' ID '=' rhs
7224 : :
7225 : : PRE_P points to the list where side effects that must happen before
7226 : : *EXPR_P should be stored.
7227 : :
7228 : : POST_P points to the list where side effects that must happen after
7229 : : *EXPR_P should be stored.
7230 : :
7231 : : WANT_VALUE is nonzero iff we want to use the value of this expression
7232 : : in another expression. */
7233 : :
7234 : : static enum gimplify_status
7235 : 49562338 : gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7236 : : bool want_value)
7237 : : {
7238 : 49562338 : tree *from_p = &TREE_OPERAND (*expr_p, 1);
7239 : 49562338 : tree *to_p = &TREE_OPERAND (*expr_p, 0);
7240 : 49562338 : enum gimplify_status ret = GS_UNHANDLED;
7241 : 49562338 : gimple *assign;
7242 : 49562338 : location_t loc = EXPR_LOCATION (*expr_p);
7243 : 49562338 : gimple_stmt_iterator gsi;
7244 : :
7245 : 49562338 : if (error_operand_p (*from_p) || error_operand_p (*to_p))
7246 : : return GS_ERROR;
7247 : :
7248 : 49562306 : gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
7249 : : || TREE_CODE (*expr_p) == INIT_EXPR);
7250 : :
7251 : : /* Trying to simplify a clobber using normal logic doesn't work,
7252 : : so handle it here. */
7253 : 49562306 : if (TREE_CLOBBER_P (*from_p))
7254 : : {
7255 : 764683 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7256 : 764683 : if (ret == GS_ERROR)
7257 : : return ret;
7258 : 764683 : gcc_assert (!want_value);
7259 : 764683 : if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
7260 : : {
7261 : 233 : tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
7262 : : pre_p, post_p);
7263 : 233 : *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
7264 : : }
7265 : 764683 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
7266 : 764683 : *expr_p = NULL;
7267 : 764683 : return GS_ALL_DONE;
7268 : : }
7269 : :
7270 : : /* Convert initialization from an empty variable-size CONSTRUCTOR to
7271 : : memset. */
7272 : 48797623 : if (TREE_TYPE (*from_p) != error_mark_node
7273 : 48797623 : && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
7274 : 48797623 : && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
7275 : 1410 : && TREE_CODE (*from_p) == CONSTRUCTOR
7276 : 48797707 : && CONSTRUCTOR_NELTS (*from_p) == 0)
7277 : : {
7278 : 67 : maybe_with_size_expr (from_p);
7279 : 67 : gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR);
7280 : 67 : return gimplify_modify_expr_to_memset (expr_p,
7281 : 67 : TREE_OPERAND (*from_p, 1),
7282 : 67 : want_value, pre_p);
7283 : : }
7284 : :
7285 : : /* Insert pointer conversions required by the middle-end that are not
7286 : : required by the frontend. This fixes middle-end type checking for
7287 : : for example gcc.dg/redecl-6.c. */
7288 : 48797556 : if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
7289 : : {
7290 : 13229759 : STRIP_USELESS_TYPE_CONVERSION (*from_p);
7291 : 13229759 : if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
7292 : 501 : *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
7293 : : }
7294 : :
7295 : : /* See if any simplifications can be done based on what the RHS is. */
7296 : 48797556 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7297 : : want_value);
7298 : 48797556 : if (ret != GS_UNHANDLED)
7299 : : return ret;
7300 : :
7301 : : /* For empty types only gimplify the left hand side and right hand
7302 : : side as statements and throw away the assignment. Do this after
7303 : : gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
7304 : : types properly. */
7305 : 46772847 : if (is_empty_type (TREE_TYPE (*from_p))
7306 : 13769 : && !want_value
7307 : : /* Don't do this for calls that return addressable types, expand_call
7308 : : relies on those having a lhs. */
7309 : 46786609 : && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
7310 : 11710 : && TREE_CODE (*from_p) == CALL_EXPR))
7311 : : {
7312 : 2052 : suppress_warning (*from_p, OPT_Wunused_result);
7313 : 2052 : gimplify_stmt (from_p, pre_p);
7314 : 2052 : gimplify_stmt (to_p, pre_p);
7315 : 2052 : *expr_p = NULL_TREE;
7316 : 2052 : return GS_ALL_DONE;
7317 : : }
7318 : :
7319 : : /* If the value being copied is of variable width, compute the length
7320 : : of the copy into a WITH_SIZE_EXPR. Note that we need to do this
7321 : : before gimplifying any of the operands so that we can resolve any
7322 : : PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
7323 : : the size of the expression to be copied, not of the destination, so
7324 : : that is what we must do here. */
7325 : 46770795 : maybe_with_size_expr (from_p);
7326 : :
7327 : : /* As a special case, we have to temporarily allow for assignments
7328 : : with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
7329 : : a toplevel statement, when gimplifying the GENERIC expression
7330 : : MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
7331 : : GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
7332 : :
7333 : : Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
7334 : : prevent gimplify_expr from trying to create a new temporary for
7335 : : foo's LHS, we tell it that it should only gimplify until it
7336 : : reaches the CALL_EXPR. On return from gimplify_expr, the newly
7337 : : created GIMPLE_CALL <foo> will be the last statement in *PRE_P
7338 : : and all we need to do here is set 'a' to be its LHS. */
7339 : :
7340 : : /* Gimplify the RHS first for C++17 and bug 71104. */
7341 : 46770795 : gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
7342 : 46770795 : ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
7343 : 46770795 : if (ret == GS_ERROR)
7344 : : return ret;
7345 : :
7346 : : /* Then gimplify the LHS. */
7347 : : /* If we gimplified the RHS to a CALL_EXPR and that call may return
7348 : : twice we have to make sure to gimplify into non-SSA as otherwise
7349 : : the abnormal edge added later will make those defs not dominate
7350 : : their uses.
7351 : : ??? Technically this applies only to the registers used in the
7352 : : resulting non-register *TO_P. */
7353 : 46770510 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
7354 : 46770510 : if (saved_into_ssa
7355 : 44466091 : && TREE_CODE (*from_p) == CALL_EXPR
7356 : 51654300 : && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
7357 : 1481 : gimplify_ctxp->into_ssa = false;
7358 : 46770510 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7359 : 46770510 : gimplify_ctxp->into_ssa = saved_into_ssa;
7360 : 46770510 : if (ret == GS_ERROR)
7361 : : return ret;
7362 : :
7363 : : /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
7364 : : guess for the predicate was wrong. */
7365 : 46770444 : gimple_predicate final_pred = rhs_predicate_for (*to_p);
7366 : 46770444 : if (final_pred != initial_pred)
7367 : : {
7368 : 7975179 : ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
7369 : 7975179 : if (ret == GS_ERROR)
7370 : : return ret;
7371 : : }
7372 : :
7373 : : /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
7374 : : size as argument to the call. */
7375 : 46770444 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7376 : : {
7377 : 1271 : tree call = TREE_OPERAND (*from_p, 0);
7378 : 1271 : tree vlasize = TREE_OPERAND (*from_p, 1);
7379 : :
7380 : 1271 : if (TREE_CODE (call) == CALL_EXPR
7381 : 1271 : && CALL_EXPR_IFN (call) == IFN_VA_ARG)
7382 : : {
7383 : 95 : int nargs = call_expr_nargs (call);
7384 : 95 : tree type = TREE_TYPE (call);
7385 : 95 : tree ap = CALL_EXPR_ARG (call, 0);
7386 : 95 : tree tag = CALL_EXPR_ARG (call, 1);
7387 : 95 : tree aptag = CALL_EXPR_ARG (call, 2);
7388 : 95 : tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
7389 : : IFN_VA_ARG, type,
7390 : : nargs + 1, ap, tag,
7391 : : aptag, vlasize);
7392 : 95 : TREE_OPERAND (*from_p, 0) = newcall;
7393 : : }
7394 : : }
7395 : :
7396 : : /* Now see if the above changed *from_p to something we handle specially. */
7397 : 46770444 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7398 : : want_value);
7399 : 46770444 : if (ret != GS_UNHANDLED)
7400 : : return ret;
7401 : :
7402 : : /* If we've got a variable sized assignment between two lvalues (i.e. does
7403 : : not involve a call), then we can make things a bit more straightforward
7404 : : by converting the assignment to memcpy or memset. */
7405 : 46769588 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7406 : : {
7407 : 1075 : tree from = TREE_OPERAND (*from_p, 0);
7408 : 1075 : tree size = TREE_OPERAND (*from_p, 1);
7409 : :
7410 : 1075 : if (TREE_CODE (from) == CONSTRUCTOR)
7411 : 0 : return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
7412 : 1075 : else if (is_gimple_addressable (from)
7413 : 1075 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (*to_p)))
7414 : 2149 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))))
7415 : : {
7416 : 1074 : *from_p = from;
7417 : 1074 : return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
7418 : 1074 : pre_p);
7419 : : }
7420 : : }
7421 : :
7422 : : /* Transform partial stores to non-addressable complex variables into
7423 : : total stores. This allows us to use real instead of virtual operands
7424 : : for these variables, which improves optimization. */
7425 : 46768514 : if ((TREE_CODE (*to_p) == REALPART_EXPR
7426 : 46768514 : || TREE_CODE (*to_p) == IMAGPART_EXPR)
7427 : 46768514 : && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
7428 : 1960 : return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
7429 : :
7430 : : /* Try to alleviate the effects of the gimplification creating artificial
7431 : : temporaries (see for example is_gimple_reg_rhs) on the debug info, but
7432 : : make sure not to create DECL_DEBUG_EXPR links across functions. */
7433 : 46766554 : if (!gimplify_ctxp->into_ssa
7434 : 2303520 : && VAR_P (*from_p)
7435 : 456365 : && DECL_IGNORED_P (*from_p)
7436 : 334146 : && DECL_P (*to_p)
7437 : 125093 : && !DECL_IGNORED_P (*to_p)
7438 : 29324 : && decl_function_context (*to_p) == current_function_decl
7439 : 46790414 : && decl_function_context (*from_p) == current_function_decl)
7440 : : {
7441 : 23855 : if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
7442 : 18482 : DECL_NAME (*from_p)
7443 : 36964 : = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
7444 : 23855 : DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
7445 : 23855 : SET_DECL_DEBUG_EXPR (*from_p, *to_p);
7446 : : }
7447 : :
7448 : 46766554 : if (want_value && TREE_THIS_VOLATILE (*to_p))
7449 : 773 : *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
7450 : :
7451 : 46766554 : if (TREE_CODE (*from_p) == CALL_EXPR)
7452 : : {
7453 : : /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
7454 : : instead of a GIMPLE_ASSIGN. */
7455 : 4590297 : gcall *call_stmt;
7456 : 4590297 : if (CALL_EXPR_FN (*from_p) == NULL_TREE)
7457 : : {
7458 : : /* Gimplify internal functions created in the FEs. */
7459 : 176825 : int nargs = call_expr_nargs (*from_p), i;
7460 : 176825 : enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
7461 : 176825 : auto_vec<tree> vargs (nargs);
7462 : :
7463 : 903678 : for (i = 0; i < nargs; i++)
7464 : : {
7465 : 550028 : gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
7466 : 550028 : EXPR_LOCATION (*from_p));
7467 : 550028 : vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
7468 : : }
7469 : 176825 : call_stmt = gimple_build_call_internal_vec (ifn, vargs);
7470 : 176825 : gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
7471 : 353650 : gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
7472 : 176825 : }
7473 : : else
7474 : : {
7475 : 4413472 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
7476 : 4413472 : CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
7477 : 4413472 : STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
7478 : 4413472 : tree fndecl = get_callee_fndecl (*from_p);
7479 : 4413472 : if (fndecl
7480 : 4299006 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
7481 : 4540203 : && call_expr_nargs (*from_p) == 3)
7482 : 63789 : call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
7483 : 63789 : CALL_EXPR_ARG (*from_p, 0),
7484 : 63789 : CALL_EXPR_ARG (*from_p, 1),
7485 : 63789 : CALL_EXPR_ARG (*from_p, 2));
7486 : : else
7487 : : {
7488 : 4349683 : call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
7489 : : }
7490 : : }
7491 : 4590297 : notice_special_calls (call_stmt);
7492 : 4590297 : if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
7493 : 4590179 : gimple_call_set_lhs (call_stmt, *to_p);
7494 : 118 : else if (TREE_CODE (*to_p) == SSA_NAME)
7495 : : /* The above is somewhat premature, avoid ICEing later for a
7496 : : SSA name w/o a definition. We may have uses in the GIMPLE IL.
7497 : : ??? This doesn't make it a default-def. */
7498 : 28 : SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
7499 : :
7500 : : assign = call_stmt;
7501 : : }
7502 : : else
7503 : : {
7504 : 42176257 : assign = gimple_build_assign (*to_p, *from_p);
7505 : 42176257 : gimple_set_location (assign, EXPR_LOCATION (*expr_p));
7506 : 42176257 : if (COMPARISON_CLASS_P (*from_p))
7507 : 1213870 : copy_warning (assign, *from_p);
7508 : : }
7509 : :
7510 : 46766554 : if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
7511 : : {
7512 : : /* We should have got an SSA name from the start. */
7513 : 34644895 : gcc_assert (TREE_CODE (*to_p) == SSA_NAME
7514 : : || ! gimple_in_ssa_p (cfun));
7515 : : }
7516 : :
7517 : 46766554 : gimplify_seq_add_stmt (pre_p, assign);
7518 : 46766554 : gsi = gsi_last (*pre_p);
7519 : 46766554 : maybe_fold_stmt (&gsi);
7520 : :
7521 : 46766554 : if (want_value)
7522 : : {
7523 : 638515 : *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
7524 : 638515 : return GS_OK;
7525 : : }
7526 : : else
7527 : 46128039 : *expr_p = NULL;
7528 : :
7529 : 46128039 : return GS_ALL_DONE;
7530 : : }
7531 : :
7532 : : /* Gimplify a comparison between two variable-sized objects. Do this
7533 : : with a call to BUILT_IN_MEMCMP. */
7534 : :
7535 : : static enum gimplify_status
7536 : 0 : gimplify_variable_sized_compare (tree *expr_p)
7537 : : {
7538 : 0 : location_t loc = EXPR_LOCATION (*expr_p);
7539 : 0 : tree op0 = TREE_OPERAND (*expr_p, 0);
7540 : 0 : tree op1 = TREE_OPERAND (*expr_p, 1);
7541 : 0 : tree t, arg, dest, src, expr;
7542 : :
7543 : 0 : arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
7544 : 0 : arg = unshare_expr (arg);
7545 : 0 : arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
7546 : 0 : src = build_fold_addr_expr_loc (loc, op1);
7547 : 0 : dest = build_fold_addr_expr_loc (loc, op0);
7548 : 0 : t = builtin_decl_implicit (BUILT_IN_MEMCMP);
7549 : 0 : t = build_call_expr_loc (loc, t, 3, dest, src, arg);
7550 : :
7551 : 0 : expr
7552 : 0 : = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
7553 : 0 : SET_EXPR_LOCATION (expr, loc);
7554 : 0 : *expr_p = expr;
7555 : :
7556 : 0 : return GS_OK;
7557 : : }
7558 : :
7559 : : /* Gimplify a comparison between two aggregate objects of integral scalar
7560 : : mode as a comparison between the bitwise equivalent scalar values. */
7561 : :
7562 : : static enum gimplify_status
7563 : 16 : gimplify_scalar_mode_aggregate_compare (tree *expr_p)
7564 : : {
7565 : 16 : const location_t loc = EXPR_LOCATION (*expr_p);
7566 : 16 : const enum tree_code code = TREE_CODE (*expr_p);
7567 : 16 : tree op0 = TREE_OPERAND (*expr_p, 0);
7568 : 16 : tree op1 = TREE_OPERAND (*expr_p, 1);
7569 : 16 : tree type = TREE_TYPE (op0);
7570 : 16 : tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
7571 : :
7572 : 16 : op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
7573 : 16 : op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
7574 : :
7575 : : /* We need to perform ordering comparisons in memory order like memcmp and,
7576 : : therefore, may need to byte-swap operands for little-endian targets. */
7577 : 16 : if (code != EQ_EXPR && code != NE_EXPR)
7578 : : {
7579 : 0 : gcc_assert (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN);
7580 : 0 : gcc_assert (TREE_CODE (scalar_type) == INTEGER_TYPE);
7581 : 0 : tree fndecl;
7582 : :
7583 : 0 : if (BYTES_BIG_ENDIAN)
7584 : : fndecl = NULL_TREE;
7585 : : else
7586 : 0 : switch (int_size_in_bytes (scalar_type))
7587 : : {
7588 : : case 1:
7589 : : fndecl = NULL_TREE;
7590 : : break;
7591 : 0 : case 2:
7592 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP16);
7593 : 0 : break;
7594 : 0 : case 4:
7595 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP32);
7596 : 0 : break;
7597 : 0 : case 8:
7598 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP64);
7599 : 0 : break;
7600 : 0 : case 16:
7601 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP128);
7602 : 0 : break;
7603 : 0 : default:
7604 : 0 : gcc_unreachable ();
7605 : : }
7606 : :
7607 : 0 : if (fndecl)
7608 : : {
7609 : 0 : op0 = build_call_expr_loc (loc, fndecl, 1, op0);
7610 : 0 : op1 = build_call_expr_loc (loc, fndecl, 1, op1);
7611 : : }
7612 : : }
7613 : :
7614 : 16 : *expr_p = fold_build2_loc (loc, code, TREE_TYPE (*expr_p), op0, op1);
7615 : :
7616 : 16 : return GS_OK;
7617 : : }
7618 : :
7619 : : /* Gimplify an expression sequence. This function gimplifies each
7620 : : expression and rewrites the original expression with the last
7621 : : expression of the sequence in GIMPLE form.
7622 : :
7623 : : PRE_P points to the list where the side effects for all the
7624 : : expressions in the sequence will be emitted.
7625 : :
7626 : : WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
7627 : :
7628 : : static enum gimplify_status
7629 : 1093307 : gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
7630 : : {
7631 : 1093307 : tree t = *expr_p;
7632 : :
7633 : 1160938 : do
7634 : : {
7635 : 1160938 : tree *sub_p = &TREE_OPERAND (t, 0);
7636 : :
7637 : 1160938 : if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
7638 : 202411 : gimplify_compound_expr (sub_p, pre_p, false);
7639 : : else
7640 : 958527 : gimplify_stmt (sub_p, pre_p);
7641 : :
7642 : 1160938 : t = TREE_OPERAND (t, 1);
7643 : : }
7644 : 1160938 : while (TREE_CODE (t) == COMPOUND_EXPR);
7645 : :
7646 : 1093307 : *expr_p = t;
7647 : 1093307 : if (want_value)
7648 : : return GS_OK;
7649 : : else
7650 : : {
7651 : 595836 : gimplify_stmt (expr_p, pre_p);
7652 : 595836 : return GS_ALL_DONE;
7653 : : }
7654 : : }
7655 : :
7656 : : /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
7657 : : gimplify. After gimplification, EXPR_P will point to a new temporary
7658 : : that holds the original value of the SAVE_EXPR node.
7659 : :
7660 : : PRE_P points to the list where side effects that must happen before
7661 : : *EXPR_P should be stored. */
7662 : :
7663 : : static enum gimplify_status
7664 : 424660 : gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7665 : : {
7666 : 424660 : enum gimplify_status ret = GS_ALL_DONE;
7667 : 424660 : tree val;
7668 : :
7669 : 424660 : gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
7670 : 424660 : val = TREE_OPERAND (*expr_p, 0);
7671 : :
7672 : 424660 : if (val && TREE_TYPE (val) == error_mark_node)
7673 : : return GS_ERROR;
7674 : :
7675 : : /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
7676 : 424658 : if (!SAVE_EXPR_RESOLVED_P (*expr_p))
7677 : : {
7678 : : /* The operand may be a void-valued expression. It is
7679 : : being executed only for its side-effects. */
7680 : 161148 : if (TREE_TYPE (val) == void_type_node)
7681 : : {
7682 : 0 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7683 : : is_gimple_stmt, fb_none);
7684 : 0 : val = NULL;
7685 : : }
7686 : : else
7687 : : /* The temporary may not be an SSA name as later abnormal and EH
7688 : : control flow may invalidate use/def domination. When in SSA
7689 : : form then assume there are no such issues and SAVE_EXPRs only
7690 : : appear via GENERIC foldings. */
7691 : 322296 : val = get_initialized_tmp_var (val, pre_p, post_p,
7692 : 322296 : gimple_in_ssa_p (cfun));
7693 : :
7694 : 161148 : TREE_OPERAND (*expr_p, 0) = val;
7695 : 161148 : SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
7696 : : }
7697 : :
7698 : 424658 : *expr_p = val;
7699 : :
7700 : 424658 : return ret;
7701 : : }
7702 : :
7703 : : /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
7704 : :
7705 : : unary_expr
7706 : : : ...
7707 : : | '&' varname
7708 : : ...
7709 : :
7710 : : PRE_P points to the list where side effects that must happen before
7711 : : *EXPR_P should be stored.
7712 : :
7713 : : POST_P points to the list where side effects that must happen after
7714 : : *EXPR_P should be stored. */
7715 : :
7716 : : static enum gimplify_status
7717 : 33028312 : gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7718 : : {
7719 : 33028312 : tree expr = *expr_p;
7720 : 33028312 : tree op0 = TREE_OPERAND (expr, 0);
7721 : 33028312 : enum gimplify_status ret;
7722 : 33028312 : location_t loc = EXPR_LOCATION (*expr_p);
7723 : :
7724 : 33028312 : switch (TREE_CODE (op0))
7725 : : {
7726 : 96594 : case INDIRECT_REF:
7727 : 96594 : do_indirect_ref:
7728 : : /* Check if we are dealing with an expression of the form '&*ptr'.
7729 : : While the front end folds away '&*ptr' into 'ptr', these
7730 : : expressions may be generated internally by the compiler (e.g.,
7731 : : builtins like __builtin_va_end). */
7732 : : /* Caution: the silent array decomposition semantics we allow for
7733 : : ADDR_EXPR means we can't always discard the pair. */
7734 : : /* Gimplification of the ADDR_EXPR operand may drop
7735 : : cv-qualification conversions, so make sure we add them if
7736 : : needed. */
7737 : 96594 : {
7738 : 96594 : tree op00 = TREE_OPERAND (op0, 0);
7739 : 96594 : tree t_expr = TREE_TYPE (expr);
7740 : 96594 : tree t_op00 = TREE_TYPE (op00);
7741 : :
7742 : 96594 : if (!useless_type_conversion_p (t_expr, t_op00))
7743 : 0 : op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
7744 : 96594 : *expr_p = op00;
7745 : 96594 : ret = GS_OK;
7746 : : }
7747 : 96594 : break;
7748 : :
7749 : 0 : case VIEW_CONVERT_EXPR:
7750 : : /* Take the address of our operand and then convert it to the type of
7751 : : this ADDR_EXPR.
7752 : :
7753 : : ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
7754 : : all clear. The impact of this transformation is even less clear. */
7755 : :
7756 : : /* If the operand is a useless conversion, look through it. Doing so
7757 : : guarantees that the ADDR_EXPR and its operand will remain of the
7758 : : same type. */
7759 : 0 : if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
7760 : 0 : op0 = TREE_OPERAND (op0, 0);
7761 : :
7762 : 0 : *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
7763 : : build_fold_addr_expr_loc (loc,
7764 : 0 : TREE_OPERAND (op0, 0)));
7765 : 0 : ret = GS_OK;
7766 : 0 : break;
7767 : :
7768 : 63016 : case MEM_REF:
7769 : 63016 : if (integer_zerop (TREE_OPERAND (op0, 1)))
7770 : 36025 : goto do_indirect_ref;
7771 : :
7772 : : /* fall through */
7773 : :
7774 : 32947896 : default:
7775 : : /* If we see a call to a declared builtin or see its address
7776 : : being taken (we can unify those cases here) then we can mark
7777 : : the builtin for implicit generation by GCC. */
7778 : 32947896 : if (TREE_CODE (op0) == FUNCTION_DECL
7779 : 16380244 : && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
7780 : 36859358 : && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
7781 : 1286047 : set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
7782 : :
7783 : : /* We use fb_either here because the C frontend sometimes takes
7784 : : the address of a call that returns a struct; see
7785 : : gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
7786 : : the implied temporary explicit. */
7787 : :
7788 : : /* Make the operand addressable. */
7789 : 32947896 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
7790 : : is_gimple_addressable, fb_either);
7791 : 32947896 : if (ret == GS_ERROR)
7792 : : break;
7793 : :
7794 : : /* Then mark it. Beware that it may not be possible to do so directly
7795 : : if a temporary has been created by the gimplification. */
7796 : 32947894 : prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
7797 : :
7798 : 32947894 : op0 = TREE_OPERAND (expr, 0);
7799 : :
7800 : : /* For various reasons, the gimplification of the expression
7801 : : may have made a new INDIRECT_REF. */
7802 : 32947894 : if (INDIRECT_REF_P (op0)
7803 : 32947894 : || (TREE_CODE (op0) == MEM_REF
7804 : 43169 : && integer_zerop (TREE_OPERAND (op0, 1))))
7805 : 16178 : goto do_indirect_ref;
7806 : :
7807 : 32931716 : mark_addressable (TREE_OPERAND (expr, 0));
7808 : :
7809 : : /* The FEs may end up building ADDR_EXPRs early on a decl with
7810 : : an incomplete type. Re-build ADDR_EXPRs in canonical form
7811 : : here. */
7812 : 32931716 : if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
7813 : 144979 : *expr_p = build_fold_addr_expr (op0);
7814 : :
7815 : : /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
7816 : 32931716 : if (TREE_CODE (*expr_p) == ADDR_EXPR)
7817 : 32931715 : recompute_tree_invariant_for_addr_expr (*expr_p);
7818 : :
7819 : : /* If we re-built the ADDR_EXPR add a conversion to the original type
7820 : : if required. */
7821 : 32931716 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
7822 : 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
7823 : :
7824 : : break;
7825 : : }
7826 : :
7827 : 33028312 : return ret;
7828 : : }
7829 : :
7830 : : /* Return the number of times character C occurs in string S. */
7831 : :
7832 : : static int
7833 : 88801 : num_occurrences (int c, const char *s)
7834 : : {
7835 : 88801 : int n = 0;
7836 : 241712 : while (*s)
7837 : 152911 : n += (*s++ == c);
7838 : 88801 : return n;
7839 : : }
7840 : :
7841 : : /* A subroutine of gimplify_asm_expr. Check that all operands have
7842 : : the same number of alternatives. Return -1 if this is violated. Otherwise
7843 : : return the number of alternatives. */
7844 : :
7845 : : static int
7846 : 190746 : num_alternatives (const_tree link)
7847 : : {
7848 : 190746 : if (link == nullptr)
7849 : : return 0;
7850 : :
7851 : 45782 : const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7852 : 45782 : int num = num_occurrences (',', constraint);
7853 : :
7854 : 45782 : if (num + 1 > MAX_RECOG_ALTERNATIVES)
7855 : : return -1;
7856 : :
7857 : 88801 : for (link = TREE_CHAIN (link); link; link = TREE_CHAIN (link))
7858 : : {
7859 : 43019 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7860 : 86038 : if (num_occurrences (',', constraint) != num)
7861 : : return -1;
7862 : : }
7863 : 45782 : return num + 1;
7864 : : }
7865 : :
7866 : : /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
7867 : : value; output operands should be a gimple lvalue. */
7868 : :
7869 : : static enum gimplify_status
7870 : 95373 : gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7871 : : {
7872 : 95373 : tree expr;
7873 : 95373 : int noutputs;
7874 : 95373 : const char **oconstraints;
7875 : 95373 : int i;
7876 : 95373 : tree link;
7877 : 95373 : const char *constraint;
7878 : 95373 : bool allows_mem, allows_reg, is_inout;
7879 : 95373 : enum gimplify_status ret, tret;
7880 : 95373 : gasm *stmt;
7881 : 95373 : vec<tree, va_gc> *inputs;
7882 : 95373 : vec<tree, va_gc> *outputs;
7883 : 95373 : vec<tree, va_gc> *clobbers;
7884 : 95373 : vec<tree, va_gc> *labels;
7885 : 95373 : tree link_next;
7886 : :
7887 : 95373 : expr = *expr_p;
7888 : 95373 : noutputs = list_length (ASM_OUTPUTS (expr));
7889 : 95373 : oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
7890 : :
7891 : 95373 : inputs = NULL;
7892 : 95373 : outputs = NULL;
7893 : 95373 : clobbers = NULL;
7894 : 95373 : labels = NULL;
7895 : :
7896 : 95373 : int num_alternatives_out = num_alternatives (ASM_OUTPUTS (expr));
7897 : 95373 : int num_alternatives_in = num_alternatives (ASM_INPUTS (expr));
7898 : 95373 : if (num_alternatives_out == -1 || num_alternatives_in == -1
7899 : 95373 : || (num_alternatives_out > 0 && num_alternatives_in > 0
7900 : 16235 : && num_alternatives_out != num_alternatives_in))
7901 : : {
7902 : 0 : error ("operand constraints for %<asm%> differ "
7903 : : "in number of alternatives");
7904 : 0 : return GS_ERROR;
7905 : : }
7906 : 95373 : int num_alternatives = MAX (num_alternatives_out, num_alternatives_in);
7907 : :
7908 : 95373 : gimplify_reg_info reg_info (num_alternatives, noutputs);
7909 : :
7910 : 95373 : link_next = NULL_TREE;
7911 : 189850 : for (link = ASM_CLOBBERS (expr); link; link = link_next)
7912 : : {
7913 : : /* The clobber entry could also be an error marker. */
7914 : 94477 : if (TREE_CODE (TREE_VALUE (link)) == STRING_CST)
7915 : : {
7916 : 94454 : const char *regname= TREE_STRING_POINTER (TREE_VALUE (link));
7917 : 94454 : int regno = decode_reg_name (regname);
7918 : 94454 : if (regno >= 0)
7919 : 41248 : reg_info.set_clobbered (regno);
7920 : : }
7921 : 94477 : link_next = TREE_CHAIN (link);
7922 : 94477 : TREE_CHAIN (link) = NULL_TREE;
7923 : 94477 : vec_safe_push (clobbers, link);
7924 : : }
7925 : :
7926 : 95373 : ret = GS_ALL_DONE;
7927 : 95373 : link_next = NULL_TREE;
7928 : 153224 : for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
7929 : : {
7930 : 57863 : bool ok;
7931 : 57863 : size_t constraint_len;
7932 : :
7933 : 57863 : link_next = TREE_CHAIN (link);
7934 : :
7935 : 115726 : oconstraints[i]
7936 : 57863 : = constraint
7937 : 57863 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7938 : 57863 : constraint_len = strlen (constraint);
7939 : 57863 : if (constraint_len == 0)
7940 : 6 : continue;
7941 : :
7942 : 57857 : reg_info.operand = TREE_VALUE (link);
7943 : 57857 : ok = parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
7944 : : &allows_reg, &is_inout, ®_info);
7945 : 57857 : if (!ok)
7946 : : {
7947 : 10 : ret = GS_ERROR;
7948 : 10 : is_inout = false;
7949 : : }
7950 : :
7951 : : /* If we can't make copies, we can only accept memory.
7952 : : Similarly for VLAs. */
7953 : 57857 : tree outtype = TREE_TYPE (TREE_VALUE (link));
7954 : 57857 : if (outtype != error_mark_node
7955 : 57857 : && (TREE_ADDRESSABLE (outtype)
7956 : 57827 : || !COMPLETE_TYPE_P (outtype)
7957 : 57802 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype))))
7958 : : {
7959 : 246 : if (allows_mem)
7960 : 234 : allows_reg = 0;
7961 : : else
7962 : : {
7963 : 12 : error ("impossible constraint in %<asm%>");
7964 : 12 : error ("non-memory output %d must stay in memory", i);
7965 : 12 : return GS_ERROR;
7966 : : }
7967 : : }
7968 : :
7969 : 57845 : if (!allows_reg && allows_mem)
7970 : 1280 : mark_addressable (TREE_VALUE (link));
7971 : :
7972 : 57845 : tree orig = TREE_VALUE (link);
7973 : 107033 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
7974 : : is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7975 : : fb_lvalue | fb_mayfail);
7976 : 57845 : if (tret == GS_ERROR)
7977 : : {
7978 : 15 : if (orig != error_mark_node)
7979 : 0 : error ("invalid lvalue in %<asm%> output %d", i);
7980 : : ret = tret;
7981 : : }
7982 : :
7983 : : /* If the gimplified operand is a register we do not allow memory. */
7984 : 57845 : if (allows_reg
7985 : 56559 : && allows_mem
7986 : 63566 : && (is_gimple_reg (TREE_VALUE (link))
7987 : 269 : || (handled_component_p (TREE_VALUE (link))
7988 : 36 : && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
7989 : 5453 : allows_mem = 0;
7990 : :
7991 : : /* If the constraint does not allow memory make sure we gimplify
7992 : : it to a register if it is not already but its base is. This
7993 : : happens for complex and vector components. */
7994 : 57845 : if (!allows_mem)
7995 : : {
7996 : 56297 : tree op = TREE_VALUE (link);
7997 : 56297 : if (! is_gimple_val (op)
7998 : 20684 : && is_gimple_reg_type (TREE_TYPE (op))
7999 : 76980 : && is_gimple_reg (get_base_address (op)))
8000 : : {
8001 : 19 : tree tem = create_tmp_reg (TREE_TYPE (op));
8002 : 19 : tree ass;
8003 : 19 : if (is_inout)
8004 : : {
8005 : 7 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
8006 : : tem, unshare_expr (op));
8007 : 7 : gimplify_and_add (ass, pre_p);
8008 : : }
8009 : 19 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
8010 : 19 : gimplify_and_add (ass, post_p);
8011 : :
8012 : 19 : TREE_VALUE (link) = tem;
8013 : 19 : tret = GS_OK;
8014 : : }
8015 : : }
8016 : :
8017 : 57845 : vec_safe_push (outputs, link);
8018 : 57845 : TREE_CHAIN (link) = NULL_TREE;
8019 : :
8020 : 57845 : if (is_inout)
8021 : : {
8022 : : /* An input/output operand. To give the optimizers more
8023 : : flexibility, split it into separate input and output
8024 : : operands. */
8025 : 8657 : tree input;
8026 : : /* Buffer big enough to format a 32-bit UINT_MAX into. */
8027 : 8657 : char buf[11];
8028 : :
8029 : : /* Turn the in/out constraint into an output constraint. */
8030 : 8657 : char *p = xstrdup (constraint);
8031 : 8657 : p[0] = '=';
8032 : 8657 : TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
8033 : 8657 : TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
8034 : :
8035 : : /* And add a matching input constraint. */
8036 : 8657 : if (allows_reg)
8037 : : {
8038 : 8158 : sprintf (buf, "%u", i);
8039 : :
8040 : : /* If there are multiple alternatives in the constraint,
8041 : : handle each of them individually. Those that allow register
8042 : : will be replaced with operand number, the others will stay
8043 : : unchanged. */
8044 : 8158 : if (strchr (p, ',') != NULL)
8045 : : {
8046 : 100 : size_t len = 0, buflen = strlen (buf);
8047 : 100 : char *beg, *end, *str, *dst;
8048 : :
8049 : 100 : for (beg = p + 1;;)
8050 : : {
8051 : 200 : end = strchr (beg, ',');
8052 : 200 : if (end == NULL)
8053 : 100 : end = strchr (beg, '\0');
8054 : 200 : if ((size_t) (end - beg) < buflen)
8055 : 28 : len += buflen + 1;
8056 : : else
8057 : 172 : len += end - beg + 1;
8058 : 200 : if (*end)
8059 : 100 : beg = end + 1;
8060 : : else
8061 : : break;
8062 : : }
8063 : :
8064 : 100 : str = (char *) alloca (len);
8065 : 100 : for (beg = p + 1, dst = str;;)
8066 : : {
8067 : 200 : const char *tem;
8068 : 200 : bool mem_p, reg_p, inout_p;
8069 : :
8070 : 200 : end = strchr (beg, ',');
8071 : 200 : if (end)
8072 : 100 : *end = '\0';
8073 : 200 : beg[-1] = '=';
8074 : 200 : tem = beg - 1;
8075 : 200 : parse_output_constraint (&tem, i, 0, 0, &mem_p, ®_p,
8076 : : &inout_p, nullptr);
8077 : 200 : if (dst != str)
8078 : 100 : *dst++ = ',';
8079 : 200 : if (reg_p)
8080 : : {
8081 : 115 : memcpy (dst, buf, buflen);
8082 : 115 : dst += buflen;
8083 : : }
8084 : : else
8085 : : {
8086 : 85 : if (end)
8087 : 0 : len = end - beg;
8088 : : else
8089 : 85 : len = strlen (beg);
8090 : 85 : memcpy (dst, beg, len);
8091 : 85 : dst += len;
8092 : : }
8093 : 200 : if (end)
8094 : 100 : beg = end + 1;
8095 : : else
8096 : : break;
8097 : 100 : }
8098 : 100 : *dst = '\0';
8099 : 100 : input = build_string (dst - str, str);
8100 : : }
8101 : : else
8102 : 8058 : input = build_string (strlen (buf), buf);
8103 : : }
8104 : : else
8105 : 499 : input = build_string (constraint_len - 1, constraint + 1);
8106 : :
8107 : 8657 : free (p);
8108 : :
8109 : 8657 : input = build_tree_list (build_tree_list (NULL_TREE, input),
8110 : 8657 : unshare_expr (TREE_VALUE (link)));
8111 : 8657 : ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
8112 : : }
8113 : : }
8114 : :
8115 : : /* After all output operands have been gimplified, verify that each output
8116 : : operand is used at most once in case of hard register constraints. Thus,
8117 : : error out in cases like
8118 : : asm ("" : "={0}" (x), "={1}" (x));
8119 : : or even for
8120 : : asm ("" : "=r" (x), "={1}" (x));
8121 : :
8122 : : FIXME: Ideally we would also error out for cases like
8123 : : int x;
8124 : : asm ("" : "=r" (x), "=r" (x));
8125 : : However, since code like that was previously accepted, erroring out now might
8126 : : break existing code. On the other hand, we already error out for register
8127 : : asm like
8128 : : register int x asm ("0");
8129 : : asm ("" : "=r" (x), "=r" (x));
8130 : : Thus, maybe it wouldn't be too bad to also error out in the former
8131 : : non-register-asm case.
8132 : : */
8133 : 153203 : for (unsigned i = 0; i < vec_safe_length (outputs); ++i)
8134 : : {
8135 : 57844 : tree link = (*outputs)[i];
8136 : 57844 : tree op1 = TREE_VALUE (link);
8137 : 57844 : const char *constraint
8138 : 57844 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8139 : 57844 : if (strchr (constraint, '{') != nullptr)
8140 : 226 : for (unsigned j = 0; j < vec_safe_length (outputs); ++j)
8141 : : {
8142 : 137 : if (i == j)
8143 : 90 : continue;
8144 : 47 : tree link2 = (*outputs)[j];
8145 : 47 : tree op2 = TREE_VALUE (link2);
8146 : 47 : if (op1 == op2)
8147 : : {
8148 : 2 : error ("multiple outputs to lvalue %qE", op2);
8149 : 2 : return GS_ERROR;
8150 : : }
8151 : : }
8152 : : }
8153 : :
8154 : 95359 : link_next = NULL_TREE;
8155 : 95359 : int input_num = 0;
8156 : 134945 : for (link = ASM_INPUTS (expr); link; ++input_num, ++i, link = link_next)
8157 : : {
8158 : 39595 : link_next = TREE_CHAIN (link);
8159 : 39595 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8160 : 39595 : reg_info.operand = TREE_VALUE (link);
8161 : 39595 : bool ok = parse_input_constraint (&constraint, input_num, 0, noutputs, 0,
8162 : : oconstraints, &allows_mem, &allows_reg,
8163 : : ®_info);
8164 : 39595 : if (!ok)
8165 : : {
8166 : 28 : ret = GS_ERROR;
8167 : 28 : is_inout = false;
8168 : : }
8169 : :
8170 : : /* If we can't make copies, we can only accept memory. */
8171 : 39595 : tree intype = TREE_TYPE (TREE_VALUE (link));
8172 : 39595 : if (intype != error_mark_node
8173 : 39595 : && (TREE_ADDRESSABLE (intype)
8174 : 39532 : || !COMPLETE_TYPE_P (intype)
8175 : 39493 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype))))
8176 : : {
8177 : 255 : if (allows_mem)
8178 : 246 : allows_reg = 0;
8179 : : else
8180 : : {
8181 : 9 : error ("impossible constraint in %<asm%>");
8182 : 9 : error ("non-memory input %d must stay in memory", i);
8183 : 9 : return GS_ERROR;
8184 : : }
8185 : : }
8186 : :
8187 : : /* If the operand is a memory input, it should be an lvalue. */
8188 : 39586 : if (!allows_reg && allows_mem)
8189 : : {
8190 : 1641 : tree inputv = TREE_VALUE (link);
8191 : 1641 : STRIP_NOPS (inputv);
8192 : 1641 : if (TREE_CODE (inputv) == PREDECREMENT_EXPR
8193 : : || TREE_CODE (inputv) == PREINCREMENT_EXPR
8194 : : || TREE_CODE (inputv) == POSTDECREMENT_EXPR
8195 : 1641 : || TREE_CODE (inputv) == POSTINCREMENT_EXPR
8196 : 1629 : || TREE_CODE (inputv) == MODIFY_EXPR
8197 : 3268 : || VOID_TYPE_P (TREE_TYPE (inputv)))
8198 : 38 : TREE_VALUE (link) = error_mark_node;
8199 : 1641 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8200 : : is_gimple_lvalue, fb_lvalue | fb_mayfail);
8201 : 1641 : if (tret != GS_ERROR)
8202 : : {
8203 : : /* Unlike output operands, memory inputs are not guaranteed
8204 : : to be lvalues by the FE, and while the expressions are
8205 : : marked addressable there, if it is e.g. a statement
8206 : : expression, temporaries in it might not end up being
8207 : : addressable. They might be already used in the IL and thus
8208 : : it is too late to make them addressable now though. */
8209 : 1585 : tree x = TREE_VALUE (link);
8210 : 1726 : while (handled_component_p (x))
8211 : 141 : x = TREE_OPERAND (x, 0);
8212 : 1585 : if (TREE_CODE (x) == MEM_REF
8213 : 1585 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
8214 : 0 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
8215 : 1585 : if ((VAR_P (x)
8216 : : || TREE_CODE (x) == PARM_DECL
8217 : : || TREE_CODE (x) == RESULT_DECL)
8218 : 1196 : && !TREE_ADDRESSABLE (x)
8219 : 23 : && is_gimple_reg (x))
8220 : : {
8221 : 17 : warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
8222 : 17 : input_location), 0,
8223 : : "memory input %d is not directly addressable",
8224 : : i);
8225 : 17 : prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
8226 : : }
8227 : : }
8228 : 1641 : mark_addressable (TREE_VALUE (link));
8229 : 1641 : if (tret == GS_ERROR)
8230 : : {
8231 : 56 : if (inputv != error_mark_node)
8232 : 46 : error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
8233 : : "memory input %d is not directly addressable", i);
8234 : : ret = tret;
8235 : : }
8236 : : }
8237 : : else
8238 : : {
8239 : 37945 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8240 : : is_gimple_asm_val, fb_rvalue);
8241 : 37945 : if (tret == GS_ERROR)
8242 : 91 : ret = tret;
8243 : : }
8244 : :
8245 : 39586 : TREE_CHAIN (link) = NULL_TREE;
8246 : 39586 : vec_safe_push (inputs, link);
8247 : : }
8248 : :
8249 : 95350 : link_next = NULL_TREE;
8250 : 96123 : for (link = ASM_LABELS (expr); link; link = link_next)
8251 : : {
8252 : 773 : link_next = TREE_CHAIN (link);
8253 : 773 : TREE_CHAIN (link) = NULL_TREE;
8254 : 773 : vec_safe_push (labels, link);
8255 : : }
8256 : :
8257 : : /* Do not add ASMs with errors to the gimple IL stream. */
8258 : 95350 : if (ret != GS_ERROR)
8259 : : {
8260 : 95215 : stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
8261 : : inputs, outputs, clobbers, labels);
8262 : :
8263 : : /* asm is volatile if it was marked by the user as volatile or
8264 : : there are no outputs or this is an asm goto. */
8265 : 190430 : gimple_asm_set_volatile (stmt,
8266 : 95215 : ASM_VOLATILE_P (expr)
8267 : 7395 : || noutputs == 0
8268 : 102596 : || labels);
8269 : 95215 : gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
8270 : 95215 : gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
8271 : :
8272 : 95215 : gimplify_seq_add_stmt (pre_p, stmt);
8273 : : }
8274 : :
8275 : : return ret;
8276 : 95373 : }
8277 : :
8278 : : /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
8279 : : GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
8280 : : gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
8281 : : return to this function.
8282 : :
8283 : : FIXME should we complexify the prequeue handling instead? Or use flags
8284 : : for all the cleanups and let the optimizer tighten them up? The current
8285 : : code seems pretty fragile; it will break on a cleanup within any
8286 : : non-conditional nesting. But any such nesting would be broken, anyway;
8287 : : we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
8288 : : and continues out of it. We can do that at the RTL level, though, so
8289 : : having an optimizer to tighten up try/finally regions would be a Good
8290 : : Thing. */
8291 : :
8292 : : static enum gimplify_status
8293 : 5456335 : gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
8294 : : {
8295 : 5456335 : gimple_stmt_iterator iter;
8296 : 5456335 : gimple_seq body_sequence = NULL;
8297 : :
8298 : 5456335 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
8299 : :
8300 : : /* We only care about the number of conditions between the innermost
8301 : : CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
8302 : : any cleanups collected outside the CLEANUP_POINT_EXPR. */
8303 : 5456335 : int old_conds = gimplify_ctxp->conditions;
8304 : 5456335 : gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
8305 : 5456335 : bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
8306 : 5456335 : gimplify_ctxp->conditions = 0;
8307 : 5456335 : gimplify_ctxp->conditional_cleanups = NULL;
8308 : 5456335 : gimplify_ctxp->in_cleanup_point_expr = true;
8309 : :
8310 : 5456335 : gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
8311 : :
8312 : 5456335 : gimplify_ctxp->conditions = old_conds;
8313 : 5456335 : gimplify_ctxp->conditional_cleanups = old_cleanups;
8314 : 5456335 : gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
8315 : :
8316 : 27937583 : for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
8317 : : {
8318 : 17066154 : gimple *wce = gsi_stmt (iter);
8319 : :
8320 : 17066154 : if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
8321 : : {
8322 : 606040 : if (gsi_one_before_end_p (iter))
8323 : : {
8324 : : /* Note that gsi_insert_seq_before and gsi_remove do not
8325 : : scan operands, unlike some other sequence mutators. */
8326 : 3177 : if (!gimple_wce_cleanup_eh_only (wce))
8327 : 3177 : gsi_insert_seq_before_without_update (&iter,
8328 : : gimple_wce_cleanup (wce),
8329 : : GSI_SAME_STMT);
8330 : 3177 : gsi_remove (&iter, true);
8331 : 3177 : break;
8332 : : }
8333 : : else
8334 : : {
8335 : 602863 : gtry *gtry;
8336 : 602863 : gimple_seq seq;
8337 : 602863 : enum gimple_try_flags kind;
8338 : :
8339 : 602863 : if (gimple_wce_cleanup_eh_only (wce))
8340 : : kind = GIMPLE_TRY_CATCH;
8341 : : else
8342 : 589933 : kind = GIMPLE_TRY_FINALLY;
8343 : 602863 : seq = gsi_split_seq_after (iter);
8344 : :
8345 : 602863 : gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
8346 : : /* Do not use gsi_replace here, as it may scan operands.
8347 : : We want to do a simple structural modification only. */
8348 : 602863 : gsi_set_stmt (&iter, gtry);
8349 : 1205726 : iter = gsi_start (gtry->eval);
8350 : : }
8351 : : }
8352 : : else
8353 : 16460114 : gsi_next (&iter);
8354 : : }
8355 : :
8356 : 5456335 : gimplify_seq_add_seq (pre_p, body_sequence);
8357 : 5456335 : if (temp)
8358 : : {
8359 : 343014 : *expr_p = temp;
8360 : 343014 : return GS_OK;
8361 : : }
8362 : : else
8363 : : {
8364 : 5113321 : *expr_p = NULL;
8365 : 5113321 : return GS_ALL_DONE;
8366 : : }
8367 : : }
8368 : :
8369 : : /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
8370 : : is the cleanup action required. EH_ONLY is true if the cleanup should
8371 : : only be executed if an exception is thrown, not on normal exit.
8372 : : If FORCE_UNCOND is true perform the cleanup unconditionally; this is
8373 : : only valid for clobbers. */
8374 : :
8375 : : static void
8376 : 611641 : gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
8377 : : bool force_uncond = false)
8378 : : {
8379 : 611641 : gimple *wce;
8380 : 611641 : gimple_seq cleanup_stmts = NULL;
8381 : :
8382 : : /* Errors can result in improperly nested cleanups. Which results in
8383 : : confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
8384 : 611641 : if (seen_error ())
8385 : 5601 : return;
8386 : :
8387 : 606040 : if (gimple_conditional_context ())
8388 : : {
8389 : : /* If we're in a conditional context, this is more complex. We only
8390 : : want to run the cleanup if we actually ran the initialization that
8391 : : necessitates it, but we want to run it after the end of the
8392 : : conditional context. So we wrap the try/finally around the
8393 : : condition and use a flag to determine whether or not to actually
8394 : : run the destructor. Thus
8395 : :
8396 : : test ? f(A()) : 0
8397 : :
8398 : : becomes (approximately)
8399 : :
8400 : : flag = 0;
8401 : : try {
8402 : : if (test) { A::A(temp); flag = 1; val = f(temp); }
8403 : : else { val = 0; }
8404 : : } finally {
8405 : : if (flag) A::~A(temp);
8406 : : }
8407 : : val
8408 : : */
8409 : 12721 : if (force_uncond)
8410 : : {
8411 : 11848 : gimplify_stmt (&cleanup, &cleanup_stmts);
8412 : 11848 : wce = gimple_build_wce (cleanup_stmts);
8413 : 11848 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8414 : : }
8415 : : else
8416 : : {
8417 : 873 : tree flag = create_tmp_var (boolean_type_node, "cleanup");
8418 : 873 : gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
8419 : 873 : gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
8420 : :
8421 : 873 : cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
8422 : 873 : gimplify_stmt (&cleanup, &cleanup_stmts);
8423 : 873 : wce = gimple_build_wce (cleanup_stmts);
8424 : 873 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8425 : :
8426 : 873 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
8427 : 873 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8428 : 873 : gimplify_seq_add_stmt (pre_p, ftrue);
8429 : :
8430 : : /* Because of this manipulation, and the EH edges that jump
8431 : : threading cannot redirect, the temporary (VAR) will appear
8432 : : to be used uninitialized. Don't warn. */
8433 : 873 : suppress_warning (var, OPT_Wuninitialized);
8434 : : }
8435 : : }
8436 : : else
8437 : : {
8438 : 593319 : gimplify_stmt (&cleanup, &cleanup_stmts);
8439 : 593319 : wce = gimple_build_wce (cleanup_stmts);
8440 : 593319 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8441 : 593319 : gimplify_seq_add_stmt (pre_p, wce);
8442 : : }
8443 : : }
8444 : :
8445 : : /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
8446 : :
8447 : : static enum gimplify_status
8448 : 806952 : gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8449 : : {
8450 : 806952 : tree targ = *expr_p;
8451 : 806952 : tree temp = TARGET_EXPR_SLOT (targ);
8452 : 806952 : tree init = TARGET_EXPR_INITIAL (targ);
8453 : 806952 : enum gimplify_status ret;
8454 : :
8455 : 806952 : bool unpoison_empty_seq = false;
8456 : 806952 : gimple_stmt_iterator unpoison_it;
8457 : :
8458 : 806952 : if (init)
8459 : : {
8460 : 770475 : gimple_seq init_pre_p = NULL;
8461 : :
8462 : : /* TARGET_EXPR temps aren't part of the enclosing block, so add it
8463 : : to the temps list. Handle also variable length TARGET_EXPRs. */
8464 : 770475 : if (!poly_int_tree_p (DECL_SIZE (temp)))
8465 : : {
8466 : 42 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
8467 : 1 : gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
8468 : : /* FIXME: this is correct only when the size of the type does
8469 : : not depend on expressions evaluated in init. */
8470 : 42 : gimplify_vla_decl (temp, &init_pre_p);
8471 : : }
8472 : : else
8473 : : {
8474 : : /* Save location where we need to place unpoisoning. It's possible
8475 : : that a variable will be converted to needs_to_live_in_memory. */
8476 : 770433 : unpoison_it = gsi_last (*pre_p);
8477 : 770433 : unpoison_empty_seq = gsi_end_p (unpoison_it);
8478 : :
8479 : 770433 : gimple_add_tmp_var (temp);
8480 : : }
8481 : :
8482 : : /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
8483 : : expression is supposed to initialize the slot. */
8484 : 770475 : if (VOID_TYPE_P (TREE_TYPE (init)))
8485 : 334013 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8486 : : fb_none);
8487 : : else
8488 : : {
8489 : 436462 : tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
8490 : 436462 : init = init_expr;
8491 : 436462 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8492 : : fb_none);
8493 : 436462 : init = NULL;
8494 : 436462 : ggc_free (init_expr);
8495 : : }
8496 : 770475 : if (ret == GS_ERROR)
8497 : : {
8498 : : /* PR c++/28266 Make sure this is expanded only once. */
8499 : 15 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8500 : 15 : return GS_ERROR;
8501 : : }
8502 : :
8503 : 770460 : if (init)
8504 : 0 : gimplify_and_add (init, &init_pre_p);
8505 : :
8506 : : /* Add a clobber for the temporary going out of scope, like
8507 : : gimplify_bind_expr. But only if we did not promote the
8508 : : temporary to static storage. */
8509 : 770460 : if (gimplify_ctxp->in_cleanup_point_expr
8510 : 622055 : && !TREE_STATIC (temp)
8511 : 1391017 : && needs_to_live_in_memory (temp))
8512 : : {
8513 : 470305 : if (flag_stack_reuse == SR_ALL)
8514 : : {
8515 : 469869 : tree clobber = build_clobber (TREE_TYPE (temp),
8516 : : CLOBBER_STORAGE_END);
8517 : 469869 : clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
8518 : 469869 : gimple_push_cleanup (temp, clobber, false, pre_p, true);
8519 : : }
8520 : 470305 : if (asan_poisoned_variables
8521 : 436 : && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
8522 : 436 : && !TREE_STATIC (temp)
8523 : 436 : && dbg_cnt (asan_use_after_scope)
8524 : 470741 : && !gimplify_omp_ctxp)
8525 : : {
8526 : 428 : tree asan_cleanup = build_asan_poison_call_expr (temp);
8527 : 428 : if (asan_cleanup)
8528 : : {
8529 : 428 : if (unpoison_empty_seq)
8530 : 216 : unpoison_it = gsi_start (*pre_p);
8531 : :
8532 : 428 : asan_poison_variable (temp, false, &unpoison_it,
8533 : : unpoison_empty_seq);
8534 : 428 : gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
8535 : : }
8536 : : }
8537 : : }
8538 : :
8539 : 770460 : gimple_seq_add_seq (pre_p, init_pre_p);
8540 : :
8541 : : /* If needed, push the cleanup for the temp. */
8542 : 770460 : if (TARGET_EXPR_CLEANUP (targ))
8543 : 141344 : gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
8544 : 141344 : CLEANUP_EH_ONLY (targ), pre_p);
8545 : :
8546 : : /* Only expand this once. */
8547 : 770460 : TREE_OPERAND (targ, 3) = init;
8548 : 770460 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8549 : : }
8550 : : else
8551 : : /* We should have expanded this before. */
8552 : 36477 : gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
8553 : :
8554 : 806937 : *expr_p = temp;
8555 : 806937 : return GS_OK;
8556 : : }
8557 : :
8558 : : /* Gimplification of expression trees. */
8559 : :
8560 : : /* Gimplify an expression which appears at statement context. The
8561 : : corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
8562 : : NULL, a new sequence is allocated.
8563 : :
8564 : : Return true if we actually added a statement to the queue. */
8565 : :
8566 : : bool
8567 : 101792945 : gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
8568 : : {
8569 : 101792945 : gimple_seq_node last;
8570 : :
8571 : 101792945 : last = gimple_seq_last (*seq_p);
8572 : 101792945 : gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
8573 : 101792945 : return last != gimple_seq_last (*seq_p);
8574 : : }
8575 : :
8576 : : /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
8577 : : to CTX. If entries already exist, force them to be some flavor of private.
8578 : : If there is no enclosing parallel, do nothing. */
8579 : :
8580 : : void
8581 : 124099 : omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
8582 : : {
8583 : 124099 : splay_tree_node n;
8584 : :
8585 : 124099 : if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
8586 : : return;
8587 : :
8588 : 36472 : do
8589 : : {
8590 : 36472 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8591 : 36472 : if (n != NULL)
8592 : : {
8593 : 4269 : if (n->value & GOVD_SHARED)
8594 : 183 : n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
8595 : 4086 : else if (n->value & GOVD_MAP)
8596 : 55 : n->value |= GOVD_MAP_TO_ONLY;
8597 : : else
8598 : : return;
8599 : : }
8600 : 32203 : else if ((ctx->region_type & ORT_TARGET) != 0)
8601 : : {
8602 : 6775 : if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
8603 : 4020 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8604 : : else
8605 : 2755 : omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
8606 : : }
8607 : 25428 : else if (ctx->region_type != ORT_WORKSHARE
8608 : : && ctx->region_type != ORT_TASKGROUP
8609 : 23619 : && ctx->region_type != ORT_SIMD
8610 : 22995 : && ctx->region_type != ORT_ACC
8611 : 22885 : && !(ctx->region_type & ORT_TARGET_DATA))
8612 : 14304 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8613 : :
8614 : 32441 : ctx = ctx->outer_context;
8615 : : }
8616 : 32441 : while (ctx);
8617 : : }
8618 : :
8619 : : /* Similarly for each of the type sizes of TYPE. */
8620 : :
8621 : : static void
8622 : 40651 : omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
8623 : : {
8624 : 40651 : if (type == NULL || type == error_mark_node)
8625 : : return;
8626 : 40647 : type = TYPE_MAIN_VARIANT (type);
8627 : :
8628 : 40647 : if (ctx->privatized_types->add (type))
8629 : : return;
8630 : :
8631 : 30901 : switch (TREE_CODE (type))
8632 : : {
8633 : 13073 : case INTEGER_TYPE:
8634 : 13073 : case ENUMERAL_TYPE:
8635 : 13073 : case BOOLEAN_TYPE:
8636 : 13073 : case REAL_TYPE:
8637 : 13073 : case FIXED_POINT_TYPE:
8638 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
8639 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
8640 : 13073 : break;
8641 : :
8642 : 5384 : case ARRAY_TYPE:
8643 : 5384 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8644 : 5384 : omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
8645 : 5384 : break;
8646 : :
8647 : 2507 : case RECORD_TYPE:
8648 : 2507 : case UNION_TYPE:
8649 : 2507 : case QUAL_UNION_TYPE:
8650 : 2507 : {
8651 : 2507 : tree field;
8652 : 26710 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8653 : 24203 : if (TREE_CODE (field) == FIELD_DECL)
8654 : : {
8655 : 9004 : omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
8656 : 9004 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
8657 : : }
8658 : : }
8659 : : break;
8660 : :
8661 : 9289 : case POINTER_TYPE:
8662 : 9289 : case REFERENCE_TYPE:
8663 : 9289 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8664 : 9289 : break;
8665 : :
8666 : : default:
8667 : : break;
8668 : : }
8669 : :
8670 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
8671 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
8672 : 30901 : lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
8673 : : }
8674 : :
8675 : : /* Add an entry for DECL in the OMP context CTX with FLAGS. */
8676 : :
8677 : : static void
8678 : 701921 : omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
8679 : : {
8680 : 701921 : splay_tree_node n;
8681 : 701921 : unsigned int nflags;
8682 : 701921 : tree t;
8683 : :
8684 : 701921 : if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
8685 : : return;
8686 : :
8687 : : /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
8688 : : there are constructors involved somewhere. Exception is a shared clause,
8689 : : there is nothing privatized in that case. */
8690 : 701776 : if ((flags & GOVD_SHARED) == 0
8691 : 701776 : && (TREE_ADDRESSABLE (TREE_TYPE (decl))
8692 : 641610 : || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
8693 : 7762 : flags |= GOVD_SEEN;
8694 : :
8695 : 701776 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8696 : 701776 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8697 : : {
8698 : : /* We shouldn't be re-adding the decl with the same data
8699 : : sharing class. */
8700 : 821 : gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
8701 : 821 : nflags = n->value | flags;
8702 : : /* The only combination of data sharing classes we should see is
8703 : : FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
8704 : : reduction variables to be used in data sharing clauses. */
8705 : 821 : gcc_assert ((ctx->region_type & ORT_ACC) != 0
8706 : : || ((nflags & GOVD_DATA_SHARE_CLASS)
8707 : : == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
8708 : : || (flags & GOVD_DATA_SHARE_CLASS) == 0);
8709 : 821 : n->value = nflags;
8710 : 821 : return;
8711 : : }
8712 : :
8713 : : /* When adding a variable-sized variable, we have to handle all sorts
8714 : : of additional bits of data: the pointer replacement variable, and
8715 : : the parameters of the type. */
8716 : 700955 : if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
8717 : : {
8718 : : /* Add the pointer replacement variable as PRIVATE if the variable
8719 : : replacement is private, else FIRSTPRIVATE since we'll need the
8720 : : address of the original variable either for SHARED, or for the
8721 : : copy into or out of the context. */
8722 : 1312 : if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
8723 : : {
8724 : 1222 : if (flags & GOVD_MAP)
8725 : : nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
8726 : 515 : else if (flags & GOVD_PRIVATE)
8727 : : nflags = GOVD_PRIVATE;
8728 : 431 : else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
8729 : 48 : && (flags & GOVD_FIRSTPRIVATE))
8730 : 423 : || (ctx->region_type == ORT_TARGET_DATA
8731 : 6 : && (flags & GOVD_DATA_SHARE_CLASS) == 0))
8732 : : nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
8733 : : else
8734 : 1222 : nflags = GOVD_FIRSTPRIVATE;
8735 : 1222 : nflags |= flags & GOVD_SEEN;
8736 : 1222 : t = DECL_VALUE_EXPR (decl);
8737 : 1222 : gcc_assert (INDIRECT_REF_P (t));
8738 : 1222 : t = TREE_OPERAND (t, 0);
8739 : 1222 : gcc_assert (DECL_P (t));
8740 : 1222 : omp_add_variable (ctx, t, nflags);
8741 : : }
8742 : :
8743 : : /* Add all of the variable and type parameters (which should have
8744 : : been gimplified to a formal temporary) as FIRSTPRIVATE. */
8745 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
8746 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
8747 : 1312 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8748 : :
8749 : : /* The variable-sized variable itself is never SHARED, only some form
8750 : : of PRIVATE. The sharing would take place via the pointer variable
8751 : : which we remapped above. */
8752 : 1312 : if (flags & GOVD_SHARED)
8753 : 276 : flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
8754 : 276 : | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
8755 : :
8756 : : /* We're going to make use of the TYPE_SIZE_UNIT at least in the
8757 : : alloca statement we generate for the variable, so make sure it
8758 : : is available. This isn't automatically needed for the SHARED
8759 : : case, since we won't be allocating local storage then.
8760 : : For local variables TYPE_SIZE_UNIT might not be gimplified yet,
8761 : : in this case omp_notice_variable will be called later
8762 : : on when it is gimplified. */
8763 : 1036 : else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
8764 : 1036 : && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
8765 : 267 : omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
8766 : : }
8767 : 699643 : else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
8768 : 699643 : && omp_privatize_by_reference (decl))
8769 : : {
8770 : 10278 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8771 : :
8772 : : /* Similar to the direct variable sized case above, we'll need the
8773 : : size of references being privatized. */
8774 : 10278 : if ((flags & GOVD_SHARED) == 0)
8775 : : {
8776 : 6487 : t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
8777 : 6487 : if (t && DECL_P (t))
8778 : 1337 : omp_notice_variable (ctx, t, true);
8779 : : }
8780 : : }
8781 : :
8782 : 700955 : if (n != NULL)
8783 : 1809 : n->value |= flags;
8784 : : else
8785 : 699146 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
8786 : :
8787 : : /* For reductions clauses in OpenACC loop directives, by default create a
8788 : : copy clause on the enclosing parallel construct for carrying back the
8789 : : results. */
8790 : 700955 : if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
8791 : : {
8792 : 4717 : struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
8793 : 6456 : while (outer_ctx)
8794 : : {
8795 : 5789 : n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
8796 : 5789 : if (n != NULL)
8797 : : {
8798 : : /* Ignore local variables and explicitly declared clauses. */
8799 : 3991 : if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
8800 : : break;
8801 : 444 : else if (outer_ctx->region_type == ORT_ACC_KERNELS)
8802 : : {
8803 : : /* According to the OpenACC spec, such a reduction variable
8804 : : should already have a copy map on a kernels construct,
8805 : : verify that here. */
8806 : 144 : gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
8807 : : && (n->value & GOVD_MAP));
8808 : : }
8809 : 300 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8810 : : {
8811 : : /* Remove firstprivate and make it a copy map. */
8812 : 150 : n->value &= ~GOVD_FIRSTPRIVATE;
8813 : 150 : n->value |= GOVD_MAP;
8814 : : }
8815 : : }
8816 : 1798 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8817 : : {
8818 : 503 : splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
8819 : : GOVD_MAP | GOVD_SEEN);
8820 : 503 : break;
8821 : : }
8822 : 1739 : outer_ctx = outer_ctx->outer_context;
8823 : : }
8824 : : }
8825 : : }
8826 : :
8827 : : /* Notice a threadprivate variable DECL used in OMP context CTX.
8828 : : This just prints out diagnostics about threadprivate variable uses
8829 : : in untied tasks. If DECL2 is non-NULL, prevent this warning
8830 : : on that variable. */
8831 : :
8832 : : static bool
8833 : 14183 : omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
8834 : : tree decl2)
8835 : : {
8836 : 14183 : splay_tree_node n;
8837 : 14183 : struct gimplify_omp_ctx *octx;
8838 : :
8839 : 28798 : for (octx = ctx; octx; octx = octx->outer_context)
8840 : 14615 : if ((octx->region_type & ORT_TARGET) != 0
8841 : 14615 : || octx->order_concurrent)
8842 : : {
8843 : 112 : n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
8844 : 112 : if (n == NULL)
8845 : : {
8846 : 20 : if (octx->order_concurrent)
8847 : : {
8848 : 20 : error ("threadprivate variable %qE used in a region with"
8849 : 20 : " %<order(concurrent)%> clause", DECL_NAME (decl));
8850 : 20 : inform (octx->location, "enclosing region");
8851 : : }
8852 : : else
8853 : : {
8854 : 0 : error ("threadprivate variable %qE used in target region",
8855 : 0 : DECL_NAME (decl));
8856 : 0 : inform (octx->location, "enclosing target region");
8857 : : }
8858 : 20 : splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
8859 : : }
8860 : 112 : if (decl2)
8861 : 0 : splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
8862 : : }
8863 : :
8864 : 14183 : if (ctx->region_type != ORT_UNTIED_TASK)
8865 : : return false;
8866 : 37 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8867 : 37 : if (n == NULL)
8868 : : {
8869 : 6 : error ("threadprivate variable %qE used in untied task",
8870 : 6 : DECL_NAME (decl));
8871 : 6 : inform (ctx->location, "enclosing task");
8872 : 6 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
8873 : : }
8874 : 37 : if (decl2)
8875 : 4 : splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
8876 : : return false;
8877 : : }
8878 : :
8879 : : /* Return true if global var DECL is device resident. */
8880 : :
8881 : : static bool
8882 : 652 : device_resident_p (tree decl)
8883 : : {
8884 : 652 : tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
8885 : :
8886 : 652 : if (!attr)
8887 : : return false;
8888 : :
8889 : 0 : for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
8890 : : {
8891 : 0 : tree c = TREE_VALUE (t);
8892 : 0 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
8893 : : return true;
8894 : : }
8895 : :
8896 : : return false;
8897 : : }
8898 : :
8899 : : /* Return true if DECL has an ACC DECLARE attribute. */
8900 : :
8901 : : static bool
8902 : 9132 : is_oacc_declared (tree decl)
8903 : : {
8904 : 9132 : tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
8905 : 9132 : tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
8906 : 9132 : return declared != NULL_TREE;
8907 : : }
8908 : :
8909 : : /* Determine outer default flags for DECL mentioned in an OMP region
8910 : : but not declared in an enclosing clause.
8911 : :
8912 : : ??? Some compiler-generated variables (like SAVE_EXPRs) could be
8913 : : remapped firstprivate instead of shared. To some extent this is
8914 : : addressed in omp_firstprivatize_type_sizes, but not
8915 : : effectively. */
8916 : :
8917 : : static unsigned
8918 : 47836 : omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
8919 : : bool in_code, unsigned flags)
8920 : : {
8921 : 47836 : enum omp_clause_default_kind default_kind = ctx->default_kind;
8922 : 47836 : enum omp_clause_default_kind kind;
8923 : :
8924 : 47836 : kind = lang_hooks.decls.omp_predetermined_sharing (decl);
8925 : 47836 : if (ctx->region_type & ORT_TASK)
8926 : : {
8927 : 3980 : tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
8928 : :
8929 : : /* The event-handle specified by a detach clause should always be firstprivate,
8930 : : regardless of the current default. */
8931 : 4192 : if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
8932 : : kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
8933 : : }
8934 : 47836 : if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8935 : : default_kind = kind;
8936 : 45033 : else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
8937 : : default_kind = OMP_CLAUSE_DEFAULT_SHARED;
8938 : : /* For C/C++ default({,first}private), variables with static storage duration
8939 : : declared in a namespace or global scope and referenced in construct
8940 : : must be explicitly specified, i.e. acts as default(none). */
8941 : 45032 : else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
8942 : 45032 : || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
8943 : 360 : && VAR_P (decl)
8944 : 321 : && is_global_var (decl)
8945 : 184 : && (DECL_FILE_SCOPE_P (decl)
8946 : 104 : || (DECL_CONTEXT (decl)
8947 : 104 : && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
8948 : 45160 : && !lang_GNU_Fortran ())
8949 : : default_kind = OMP_CLAUSE_DEFAULT_NONE;
8950 : :
8951 : 47707 : switch (default_kind)
8952 : : {
8953 : 267 : case OMP_CLAUSE_DEFAULT_NONE:
8954 : 267 : {
8955 : 267 : const char *rtype;
8956 : :
8957 : 267 : if (ctx->region_type & ORT_PARALLEL)
8958 : : rtype = "parallel";
8959 : 108 : else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
8960 : : rtype = "taskloop";
8961 : 72 : else if (ctx->region_type & ORT_TASK)
8962 : : rtype = "task";
8963 : 36 : else if (ctx->region_type & ORT_TEAMS)
8964 : : rtype = "teams";
8965 : : else
8966 : 0 : gcc_unreachable ();
8967 : :
8968 : 267 : error ("%qE not specified in enclosing %qs",
8969 : 267 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
8970 : 267 : inform (ctx->location, "enclosing %qs", rtype);
8971 : : }
8972 : : /* FALLTHRU */
8973 : 44201 : case OMP_CLAUSE_DEFAULT_SHARED:
8974 : 44201 : flags |= GOVD_SHARED;
8975 : 44201 : break;
8976 : 155 : case OMP_CLAUSE_DEFAULT_PRIVATE:
8977 : 155 : flags |= GOVD_PRIVATE;
8978 : 155 : break;
8979 : 266 : case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
8980 : 266 : flags |= GOVD_FIRSTPRIVATE;
8981 : 266 : break;
8982 : 3214 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8983 : : /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
8984 : 3214 : gcc_assert ((ctx->region_type & ORT_TASK) != 0);
8985 : 3214 : if (struct gimplify_omp_ctx *octx = ctx->outer_context)
8986 : : {
8987 : 2158 : omp_notice_variable (octx, decl, in_code);
8988 : 2986 : for (; octx; octx = octx->outer_context)
8989 : : {
8990 : 2930 : splay_tree_node n2;
8991 : :
8992 : 2930 : n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
8993 : 2930 : if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
8994 : 5 : && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
8995 : 5 : continue;
8996 : 2925 : if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
8997 : : {
8998 : 896 : flags |= GOVD_FIRSTPRIVATE;
8999 : 896 : goto found_outer;
9000 : : }
9001 : 2029 : if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
9002 : : {
9003 : 1206 : flags |= GOVD_SHARED;
9004 : 1206 : goto found_outer;
9005 : : }
9006 : : }
9007 : : }
9008 : :
9009 : 1112 : if (TREE_CODE (decl) == PARM_DECL
9010 : 1112 : || (!is_global_var (decl)
9011 : 296 : && DECL_CONTEXT (decl) == current_function_decl))
9012 : 735 : flags |= GOVD_FIRSTPRIVATE;
9013 : : else
9014 : 377 : flags |= GOVD_SHARED;
9015 : 47836 : found_outer:
9016 : : break;
9017 : :
9018 : 0 : default:
9019 : 0 : gcc_unreachable ();
9020 : : }
9021 : :
9022 : 47836 : return flags;
9023 : : }
9024 : :
9025 : : /* Return string name for types of OpenACC constructs from ORT_* values. */
9026 : :
9027 : : static const char *
9028 : 570 : oacc_region_type_name (enum omp_region_type region_type)
9029 : : {
9030 : 570 : switch (region_type)
9031 : : {
9032 : : case ORT_ACC_DATA:
9033 : : return "data";
9034 : 140 : case ORT_ACC_PARALLEL:
9035 : 140 : return "parallel";
9036 : 140 : case ORT_ACC_KERNELS:
9037 : 140 : return "kernels";
9038 : 140 : case ORT_ACC_SERIAL:
9039 : 140 : return "serial";
9040 : 0 : default:
9041 : 0 : gcc_unreachable ();
9042 : : }
9043 : : }
9044 : :
9045 : : /* Determine outer default flags for DECL mentioned in an OACC region
9046 : : but not declared in an enclosing clause. */
9047 : :
9048 : : static unsigned
9049 : 8870 : oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
9050 : : {
9051 : 8870 : struct gimplify_omp_ctx *ctx_default = ctx;
9052 : : /* If no 'default' clause appears on this compute construct... */
9053 : 8870 : if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
9054 : : {
9055 : : /* ..., see if one appears on a lexically containing 'data'
9056 : : construct. */
9057 : 9649 : while ((ctx_default = ctx_default->outer_context))
9058 : : {
9059 : 1534 : if (ctx_default->region_type == ORT_ACC_DATA
9060 : 1534 : && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
9061 : : break;
9062 : : }
9063 : : /* If not, reset. */
9064 : 8291 : if (!ctx_default)
9065 : 8694 : ctx_default = ctx;
9066 : : }
9067 : :
9068 : 8870 : bool on_device = false;
9069 : 8870 : bool is_private = false;
9070 : 8870 : bool declared = is_oacc_declared (decl);
9071 : 8870 : tree type = TREE_TYPE (decl);
9072 : :
9073 : 8870 : if (omp_privatize_by_reference (decl))
9074 : 338 : type = TREE_TYPE (type);
9075 : :
9076 : : /* For Fortran COMMON blocks, only used variables in those blocks are
9077 : : transfered and remapped. The block itself will have a private clause to
9078 : : avoid transfering the data twice.
9079 : : The hook evaluates to false by default. For a variable in Fortran's COMMON
9080 : : or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
9081 : : the variables in such a COMMON/EQUIVALENCE block shall be privatized not
9082 : : the whole block. For C++ and Fortran, it can also be true under certain
9083 : : other conditions, if DECL_HAS_VALUE_EXPR. */
9084 : 8870 : if (RECORD_OR_UNION_TYPE_P (type))
9085 : 982 : is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
9086 : :
9087 : 8870 : if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
9088 : 8870 : && is_global_var (decl)
9089 : 652 : && device_resident_p (decl)
9090 : 8870 : && !is_private)
9091 : : {
9092 : 0 : on_device = true;
9093 : 0 : flags |= GOVD_MAP_TO_ONLY;
9094 : : }
9095 : :
9096 : 8870 : switch (ctx->region_type)
9097 : : {
9098 : 1491 : case ORT_ACC_KERNELS:
9099 : 1491 : if (is_private)
9100 : 0 : flags |= GOVD_FIRSTPRIVATE;
9101 : 1491 : else if (AGGREGATE_TYPE_P (type))
9102 : : {
9103 : : /* Aggregates default to 'present_or_copy', or 'present'. */
9104 : 429 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9105 : 411 : flags |= GOVD_MAP;
9106 : : else
9107 : 18 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9108 : : }
9109 : : else
9110 : : /* Scalars default to 'copy'. */
9111 : 1062 : flags |= GOVD_MAP | GOVD_MAP_FORCE;
9112 : :
9113 : : break;
9114 : :
9115 : 7379 : case ORT_ACC_PARALLEL:
9116 : 7379 : case ORT_ACC_SERIAL:
9117 : 7379 : if (is_private)
9118 : 0 : flags |= GOVD_FIRSTPRIVATE;
9119 : 7379 : else if (on_device || declared)
9120 : 17 : flags |= GOVD_MAP;
9121 : 7362 : else if (AGGREGATE_TYPE_P (type))
9122 : : {
9123 : : /* Aggregates default to 'present_or_copy', or 'present'. */
9124 : 3697 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9125 : 3431 : flags |= GOVD_MAP;
9126 : : else
9127 : 266 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9128 : : }
9129 : : else
9130 : : /* Scalars default to 'firstprivate'. */
9131 : 3665 : flags |= GOVD_FIRSTPRIVATE;
9132 : :
9133 : : break;
9134 : :
9135 : 0 : default:
9136 : 0 : gcc_unreachable ();
9137 : : }
9138 : :
9139 : 8870 : if (DECL_ARTIFICIAL (decl))
9140 : : ; /* We can get compiler-generated decls, and should not complain
9141 : : about them. */
9142 : 8562 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
9143 : : {
9144 : 420 : error ("%qE not specified in enclosing OpenACC %qs construct",
9145 : 210 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
9146 : : oacc_region_type_name (ctx->region_type));
9147 : 210 : if (ctx_default != ctx)
9148 : 150 : inform (ctx->location, "enclosing OpenACC %qs construct and",
9149 : : oacc_region_type_name (ctx->region_type));
9150 : 210 : inform (ctx_default->location,
9151 : : "enclosing OpenACC %qs construct with %qs clause",
9152 : : oacc_region_type_name (ctx_default->region_type),
9153 : : "default(none)");
9154 : : }
9155 : 8352 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
9156 : : ; /* Handled above. */
9157 : : else
9158 : 7819 : gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
9159 : :
9160 : 8870 : return flags;
9161 : : }
9162 : :
9163 : : /* Record the fact that DECL was used within the OMP context CTX.
9164 : : IN_CODE is true when real code uses DECL, and false when we should
9165 : : merely emit default(none) errors. Return true if DECL is going to
9166 : : be remapped and thus DECL shouldn't be gimplified into its
9167 : : DECL_VALUE_EXPR (if any). */
9168 : :
9169 : : static bool
9170 : 3828109 : omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
9171 : : {
9172 : 3828109 : splay_tree_node n;
9173 : 3828109 : unsigned flags = in_code ? GOVD_SEEN : 0;
9174 : 3828109 : bool ret = false, shared;
9175 : :
9176 : 3828109 : if (error_operand_p (decl))
9177 : : return false;
9178 : :
9179 : 3828109 : if (DECL_ARTIFICIAL (decl))
9180 : : {
9181 : 2192217 : tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
9182 : 2192217 : if (attr)
9183 : 531 : decl = TREE_VALUE (TREE_VALUE (attr));
9184 : : }
9185 : :
9186 : 3828109 : if (ctx->region_type == ORT_NONE)
9187 : 156 : return lang_hooks.decls.omp_disregard_value_expr (decl, false);
9188 : :
9189 : 3827953 : if (is_global_var (decl))
9190 : : {
9191 : : /* Threadprivate variables are predetermined. */
9192 : 469895 : if (DECL_THREAD_LOCAL_P (decl))
9193 : 13565 : return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
9194 : :
9195 : 456330 : if (DECL_HAS_VALUE_EXPR_P (decl))
9196 : : {
9197 : 4380 : if (ctx->region_type & ORT_ACC)
9198 : : /* For OpenACC, defer expansion of value to avoid transfering
9199 : : privatized common block data instead of im-/explicitly transfered
9200 : : variables which are in common blocks. */
9201 : : ;
9202 : : else
9203 : : {
9204 : 2104 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
9205 : :
9206 : 2104 : if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
9207 : 618 : return omp_notice_threadprivate_variable (ctx, decl, value);
9208 : : }
9209 : : }
9210 : :
9211 : 455712 : if (gimplify_omp_ctxp->outer_context == NULL
9212 : 83665 : && VAR_P (decl)
9213 : 539377 : && oacc_get_fn_attrib (current_function_decl))
9214 : : {
9215 : 765 : location_t loc = DECL_SOURCE_LOCATION (decl);
9216 : :
9217 : 765 : if (lookup_attribute ("omp declare target link",
9218 : 765 : DECL_ATTRIBUTES (decl)))
9219 : : {
9220 : 48 : error_at (loc,
9221 : : "%qE with %<link%> clause used in %<routine%> function",
9222 : 24 : DECL_NAME (decl));
9223 : 24 : return false;
9224 : : }
9225 : 741 : else if (!lookup_attribute ("omp declare target",
9226 : 741 : DECL_ATTRIBUTES (decl)))
9227 : : {
9228 : 132 : error_at (loc,
9229 : : "%qE requires a %<declare%> directive for use "
9230 : 66 : "in a %<routine%> function", DECL_NAME (decl));
9231 : 66 : return false;
9232 : : }
9233 : : }
9234 : : }
9235 : :
9236 : 3813680 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9237 : 3813680 : if ((ctx->region_type & ORT_TARGET) != 0)
9238 : : {
9239 : 773833 : if (n == NULL)
9240 : : {
9241 : 44883 : unsigned nflags = flags;
9242 : 44883 : if ((ctx->region_type & ORT_ACC) == 0)
9243 : : {
9244 : 33897 : bool is_declare_target = false;
9245 : 33897 : if (is_global_var (decl)
9246 : 33897 : && varpool_node::get_create (decl)->offloadable)
9247 : : {
9248 : 7004 : struct gimplify_omp_ctx *octx;
9249 : 7004 : for (octx = ctx->outer_context;
9250 : 7012 : octx; octx = octx->outer_context)
9251 : : {
9252 : 9 : n = splay_tree_lookup (octx->variables,
9253 : : (splay_tree_key)decl);
9254 : 9 : if (n
9255 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
9256 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9257 : : break;
9258 : : }
9259 : 7004 : is_declare_target = octx == NULL;
9260 : : }
9261 : 7004 : if (!is_declare_target)
9262 : : {
9263 : 26894 : int gdmk;
9264 : 26894 : enum omp_clause_defaultmap_kind kind;
9265 : 26894 : if (lang_hooks.decls.omp_allocatable_p (decl))
9266 : : gdmk = GDMK_ALLOCATABLE;
9267 : 26538 : else if (lang_hooks.decls.omp_scalar_target_p (decl))
9268 : : gdmk = GDMK_SCALAR_TARGET;
9269 : 26463 : else if (lang_hooks.decls.omp_scalar_p (decl, false))
9270 : : gdmk = GDMK_SCALAR;
9271 : 11495 : else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9272 : 11495 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9273 : 3137 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9274 : : == POINTER_TYPE)))
9275 : : gdmk = GDMK_POINTER;
9276 : : else
9277 : : gdmk = GDMK_AGGREGATE;
9278 : 26894 : kind = lang_hooks.decls.omp_predetermined_mapping (decl);
9279 : 26894 : if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
9280 : : {
9281 : 944 : if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
9282 : 101 : nflags |= GOVD_FIRSTPRIVATE;
9283 : 843 : else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
9284 : 843 : nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
9285 : : else
9286 : 0 : gcc_unreachable ();
9287 : : }
9288 : 25950 : else if (ctx->defaultmap[gdmk] == 0)
9289 : : {
9290 : 75 : tree d = lang_hooks.decls.omp_report_decl (decl);
9291 : 75 : error ("%qE not specified in enclosing %<target%>",
9292 : 75 : DECL_NAME (d));
9293 : 75 : inform (ctx->location, "enclosing %<target%>");
9294 : : }
9295 : 25875 : else if (ctx->defaultmap[gdmk]
9296 : 25875 : & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
9297 : 8938 : nflags |= ctx->defaultmap[gdmk];
9298 : 16937 : else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
9299 : : {
9300 : 42 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9301 : 42 : nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
9302 : : }
9303 : : else
9304 : : {
9305 : 16895 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9306 : 16895 : nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
9307 : : }
9308 : : }
9309 : : }
9310 : :
9311 : 44883 : struct gimplify_omp_ctx *octx = ctx->outer_context;
9312 : 44883 : if ((ctx->region_type & ORT_ACC) && octx)
9313 : : {
9314 : : /* Look in outer OpenACC contexts, to see if there's a
9315 : : data attribute for this variable. */
9316 : 3536 : omp_notice_variable (octx, decl, in_code);
9317 : :
9318 : 5308 : for (; octx; octx = octx->outer_context)
9319 : : {
9320 : 3884 : if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
9321 : : break;
9322 : 3884 : splay_tree_node n2
9323 : 3884 : = splay_tree_lookup (octx->variables,
9324 : : (splay_tree_key) decl);
9325 : 3884 : if (n2)
9326 : : {
9327 : 2112 : if (octx->region_type == ORT_ACC_HOST_DATA)
9328 : 4 : error ("variable %qE declared in enclosing "
9329 : 4 : "%<host_data%> region", DECL_NAME (decl));
9330 : 2112 : nflags |= GOVD_MAP;
9331 : 2112 : if (octx->region_type == ORT_ACC_DATA
9332 : 2108 : && (n2->value & GOVD_MAP_0LEN_ARRAY))
9333 : 288 : nflags |= GOVD_MAP_0LEN_ARRAY;
9334 : 2112 : goto found_outer;
9335 : : }
9336 : : }
9337 : : }
9338 : :
9339 : 42771 : if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
9340 : : | GOVD_MAP_ALLOC_ONLY)) == flags)
9341 : : {
9342 : 32847 : tree type = TREE_TYPE (decl);
9343 : 32847 : location_t loc = DECL_SOURCE_LOCATION (decl);
9344 : :
9345 : 32847 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9346 : 32847 : && omp_privatize_by_reference (decl))
9347 : 29 : type = TREE_TYPE (type);
9348 : :
9349 : 32847 : if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
9350 : : /* Check if TYPE can appear in a target region.
9351 : : verify_type_context has already issued an error if it
9352 : : can't. */
9353 : 0 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9354 : 32847 : else if (!omp_mappable_type (type))
9355 : : {
9356 : 8 : error ("%qD referenced in target region does not have "
9357 : : "a mappable type", decl);
9358 : 8 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9359 : : }
9360 : : else
9361 : : {
9362 : 32839 : if ((ctx->region_type & ORT_ACC) != 0)
9363 : 8870 : nflags = oacc_default_clause (ctx, decl, flags);
9364 : : else
9365 : 23969 : nflags |= GOVD_MAP;
9366 : : }
9367 : : }
9368 : 9924 : found_outer:
9369 : 44883 : omp_add_variable (ctx, decl, nflags);
9370 : 44883 : if (ctx->region_type & ORT_ACC)
9371 : : /* For OpenACC, as remarked above, defer expansion. */
9372 : : shared = false;
9373 : : else
9374 : 33897 : shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9375 : 44883 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9376 : : }
9377 : : else
9378 : : {
9379 : 728950 : if (ctx->region_type & ORT_ACC)
9380 : : /* For OpenACC, as remarked above, defer expansion. */
9381 : : shared = false;
9382 : : else
9383 : 432630 : shared = ((n->value | flags)
9384 : 432630 : & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9385 : 728950 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9386 : : /* If nothing changed, there's nothing left to do. */
9387 : 728950 : if ((n->value & flags) == flags)
9388 : : return ret;
9389 : 15757 : flags |= n->value;
9390 : 15757 : n->value = flags;
9391 : : }
9392 : 60640 : goto do_outer;
9393 : : }
9394 : :
9395 : 3039847 : if (n == NULL)
9396 : : {
9397 : 1180604 : if (ctx->region_type == ORT_WORKSHARE
9398 : : || ctx->region_type == ORT_TASKGROUP
9399 : 560379 : || ctx->region_type == ORT_SIMD
9400 : 288661 : || ctx->region_type == ORT_ACC
9401 : 92299 : || (ctx->region_type & ORT_TARGET_DATA) != 0)
9402 : 1132768 : goto do_outer;
9403 : :
9404 : 47836 : flags = omp_default_clause (ctx, decl, in_code, flags);
9405 : :
9406 : 47836 : if ((flags & GOVD_PRIVATE)
9407 : 47836 : && lang_hooks.decls.omp_private_outer_ref (decl))
9408 : 6 : flags |= GOVD_PRIVATE_OUTER_REF;
9409 : :
9410 : 47836 : omp_add_variable (ctx, decl, flags);
9411 : :
9412 : 47836 : shared = (flags & GOVD_SHARED) != 0;
9413 : 47836 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9414 : 47836 : goto do_outer;
9415 : : }
9416 : :
9417 : : /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
9418 : : lb, b or incr expressions, those shouldn't be turned into simd arrays. */
9419 : 1859243 : if (ctx->region_type == ORT_SIMD
9420 : 151155 : && ctx->in_for_exprs
9421 : 70 : && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
9422 : : == GOVD_PRIVATE))
9423 : 1859243 : flags &= ~GOVD_SEEN;
9424 : :
9425 : 1859243 : if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
9426 : 31694 : && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
9427 : 1890898 : && DECL_SIZE (decl))
9428 : : {
9429 : 31654 : tree size;
9430 : 31654 : if (!poly_int_tree_p (DECL_SIZE (decl)))
9431 : : {
9432 : 140 : splay_tree_node n2;
9433 : 140 : tree t = DECL_VALUE_EXPR (decl);
9434 : 140 : gcc_assert (INDIRECT_REF_P (t));
9435 : 140 : t = TREE_OPERAND (t, 0);
9436 : 140 : gcc_assert (DECL_P (t));
9437 : 140 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9438 : 140 : n2->value |= GOVD_SEEN;
9439 : : }
9440 : 31514 : else if (omp_privatize_by_reference (decl)
9441 : 4205 : && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
9442 : 35682 : && !poly_int_tree_p (size))
9443 : : {
9444 : 1324 : splay_tree_node n2;
9445 : 1324 : gcc_assert (DECL_P (size));
9446 : 1324 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
9447 : 1324 : if (n2)
9448 : 617 : omp_notice_variable (ctx, size, true);
9449 : : }
9450 : : }
9451 : :
9452 : 1859243 : if (ctx->region_type & ORT_ACC)
9453 : : /* For OpenACC, as remarked above, defer expansion. */
9454 : : shared = false;
9455 : : else
9456 : 1677410 : shared = ((flags | n->value) & GOVD_SHARED) != 0;
9457 : 1859243 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9458 : :
9459 : : /* If nothing changed, there's nothing left to do. */
9460 : 1859243 : if ((n->value & flags) == flags)
9461 : : return ret;
9462 : 31655 : flags |= n->value;
9463 : 31655 : n->value = flags;
9464 : :
9465 : 1272899 : do_outer:
9466 : : /* If the variable is private in the current context, then we don't
9467 : : need to propagate anything to an outer context. */
9468 : 1272899 : if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
9469 : : return ret;
9470 : 1262601 : if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9471 : : == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9472 : : return ret;
9473 : 1262541 : if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9474 : : | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9475 : : == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9476 : : return ret;
9477 : 1262541 : if (ctx->outer_context
9478 : 1262541 : && omp_notice_variable (ctx->outer_context, decl, in_code))
9479 : : return true;
9480 : : return ret;
9481 : : }
9482 : :
9483 : : /* Verify that DECL is private within CTX. If there's specific information
9484 : : to the contrary in the innermost scope, generate an error. */
9485 : :
9486 : : static bool
9487 : 50094 : omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
9488 : : {
9489 : 95799 : splay_tree_node n;
9490 : :
9491 : 95799 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9492 : 95799 : if (n != NULL)
9493 : : {
9494 : 17958 : if (n->value & GOVD_SHARED)
9495 : : {
9496 : 407 : if (ctx == gimplify_omp_ctxp)
9497 : : {
9498 : 0 : if (simd)
9499 : 0 : error ("iteration variable %qE is predetermined linear",
9500 : 0 : DECL_NAME (decl));
9501 : : else
9502 : 0 : error ("iteration variable %qE should be private",
9503 : 0 : DECL_NAME (decl));
9504 : 0 : n->value = GOVD_PRIVATE;
9505 : 0 : return true;
9506 : : }
9507 : : else
9508 : : return false;
9509 : : }
9510 : 17551 : else if ((n->value & GOVD_EXPLICIT) != 0
9511 : 9689 : && (ctx == gimplify_omp_ctxp
9512 : 326 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9513 : 206 : && gimplify_omp_ctxp->outer_context == ctx)))
9514 : : {
9515 : 9569 : if ((n->value & GOVD_FIRSTPRIVATE) != 0)
9516 : 4 : error ("iteration variable %qE should not be firstprivate",
9517 : 4 : DECL_NAME (decl));
9518 : 9565 : else if ((n->value & GOVD_REDUCTION) != 0)
9519 : 8 : error ("iteration variable %qE should not be reduction",
9520 : 8 : DECL_NAME (decl));
9521 : 9557 : else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
9522 : 58 : error ("iteration variable %qE should not be linear",
9523 : 58 : DECL_NAME (decl));
9524 : : }
9525 : 17551 : return (ctx == gimplify_omp_ctxp
9526 : 17551 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9527 : 19415 : && gimplify_omp_ctxp->outer_context == ctx));
9528 : : }
9529 : :
9530 : 77841 : if (ctx->region_type != ORT_WORKSHARE
9531 : : && ctx->region_type != ORT_TASKGROUP
9532 : 49435 : && ctx->region_type != ORT_SIMD
9533 : 36190 : && ctx->region_type != ORT_ACC)
9534 : : return false;
9535 : 53851 : else if (ctx->outer_context)
9536 : : return omp_is_private (ctx->outer_context, decl, simd);
9537 : : return false;
9538 : : }
9539 : :
9540 : : /* Return true if DECL is private within a parallel region
9541 : : that binds to the current construct's context or in parallel
9542 : : region's REDUCTION clause. */
9543 : :
9544 : : static bool
9545 : 11529 : omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
9546 : : {
9547 : 11601 : splay_tree_node n;
9548 : :
9549 : 11601 : do
9550 : : {
9551 : 11601 : ctx = ctx->outer_context;
9552 : 11601 : if (ctx == NULL)
9553 : : {
9554 : 2764 : if (is_global_var (decl))
9555 : : return false;
9556 : :
9557 : : /* References might be private, but might be shared too,
9558 : : when checking for copyprivate, assume they might be
9559 : : private, otherwise assume they might be shared. */
9560 : 1290 : if (copyprivate)
9561 : : return true;
9562 : :
9563 : 1244 : if (omp_privatize_by_reference (decl))
9564 : : return false;
9565 : :
9566 : : /* Treat C++ privatized non-static data members outside
9567 : : of the privatization the same. */
9568 : 1198 : if (omp_member_access_dummy_var (decl))
9569 : : return false;
9570 : :
9571 : : return true;
9572 : : }
9573 : :
9574 : 8837 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9575 : :
9576 : 8837 : if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
9577 : 1291 : && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
9578 : : {
9579 : 242 : if ((ctx->region_type & ORT_TARGET_DATA) != 0
9580 : 238 : || n == NULL
9581 : 230 : || (n->value & GOVD_MAP) == 0)
9582 : 12 : continue;
9583 : : return false;
9584 : : }
9585 : :
9586 : 7546 : if (n != NULL)
9587 : : {
9588 : 5346 : if ((n->value & GOVD_LOCAL) != 0
9589 : 5346 : && omp_member_access_dummy_var (decl))
9590 : : return false;
9591 : 5307 : return (n->value & GOVD_SHARED) == 0;
9592 : : }
9593 : :
9594 : 3249 : if (ctx->region_type == ORT_WORKSHARE
9595 : : || ctx->region_type == ORT_TASKGROUP
9596 : 3193 : || ctx->region_type == ORT_SIMD
9597 : 3189 : || ctx->region_type == ORT_ACC)
9598 : 60 : continue;
9599 : :
9600 : : break;
9601 : : }
9602 : : while (1);
9603 : : return false;
9604 : : }
9605 : :
9606 : : /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
9607 : :
9608 : : static tree
9609 : 3148 : find_decl_expr (tree *tp, int *walk_subtrees, void *data)
9610 : : {
9611 : 3148 : tree t = *tp;
9612 : :
9613 : : /* If this node has been visited, unmark it and keep looking. */
9614 : 3148 : if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
9615 : : return t;
9616 : :
9617 : 2724 : if (IS_TYPE_OR_DECL_P (t))
9618 : 452 : *walk_subtrees = 0;
9619 : : return NULL_TREE;
9620 : : }
9621 : :
9622 : :
9623 : : /* Gimplify the affinity clause but effectively ignore it.
9624 : : Generate:
9625 : : var = begin;
9626 : : if ((step > 1) ? var <= end : var > end)
9627 : : locatator_var_expr; */
9628 : :
9629 : : static void
9630 : 468 : gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
9631 : : {
9632 : 468 : tree last_iter = NULL_TREE;
9633 : 468 : tree last_bind = NULL_TREE;
9634 : 468 : tree label = NULL_TREE;
9635 : 468 : tree *last_body = NULL;
9636 : 1203 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9637 : 735 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
9638 : : {
9639 : 735 : tree t = OMP_CLAUSE_DECL (c);
9640 : 735 : if (OMP_ITERATOR_DECL_P (t))
9641 : : {
9642 : 389 : if (TREE_VALUE (t) == null_pointer_node)
9643 : 201 : continue;
9644 : 188 : if (TREE_PURPOSE (t) != last_iter)
9645 : : {
9646 : 127 : if (last_bind)
9647 : : {
9648 : 9 : append_to_statement_list (label, last_body);
9649 : 9 : gimplify_and_add (last_bind, pre_p);
9650 : 9 : last_bind = NULL_TREE;
9651 : : }
9652 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9653 : : {
9654 : 147 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9655 : : is_gimple_val, fb_rvalue) == GS_ERROR
9656 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9657 : : is_gimple_val, fb_rvalue) == GS_ERROR
9658 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9659 : : is_gimple_val, fb_rvalue) == GS_ERROR
9660 : 294 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9661 : : is_gimple_val, fb_rvalue)
9662 : : == GS_ERROR))
9663 : 0 : return;
9664 : : }
9665 : 127 : last_iter = TREE_PURPOSE (t);
9666 : 127 : tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9667 : 127 : last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
9668 : : NULL, block);
9669 : 127 : last_body = &BIND_EXPR_BODY (last_bind);
9670 : 127 : tree cond = NULL_TREE;
9671 : 127 : location_t loc = OMP_CLAUSE_LOCATION (c);
9672 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9673 : : {
9674 : 147 : tree var = TREE_VEC_ELT (it, 0);
9675 : 147 : tree begin = TREE_VEC_ELT (it, 1);
9676 : 147 : tree end = TREE_VEC_ELT (it, 2);
9677 : 147 : tree step = TREE_VEC_ELT (it, 3);
9678 : 147 : loc = DECL_SOURCE_LOCATION (var);
9679 : 147 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9680 : : var, begin);
9681 : 147 : append_to_statement_list_force (tem, last_body);
9682 : :
9683 : 147 : tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9684 : 147 : step, build_zero_cst (TREE_TYPE (step)));
9685 : 147 : tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
9686 : : var, end);
9687 : 147 : tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9688 : : var, end);
9689 : 147 : cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
9690 : : cond1, cond2, cond3);
9691 : 147 : if (cond)
9692 : 20 : cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
9693 : : boolean_type_node, cond, cond1);
9694 : : else
9695 : : cond = cond1;
9696 : : }
9697 : 127 : tree cont_label = create_artificial_label (loc);
9698 : 127 : label = build1 (LABEL_EXPR, void_type_node, cont_label);
9699 : 127 : tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9700 : : void_node,
9701 : : build_and_jump (&cont_label));
9702 : 127 : append_to_statement_list_force (tem, last_body);
9703 : : }
9704 : 188 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
9705 : : {
9706 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
9707 : : last_body);
9708 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
9709 : : }
9710 : 188 : if (error_operand_p (TREE_VALUE (t)))
9711 : : return;
9712 : 188 : append_to_statement_list_force (TREE_VALUE (t), last_body);
9713 : 188 : TREE_VALUE (t) = null_pointer_node;
9714 : : }
9715 : : else
9716 : : {
9717 : 346 : if (last_bind)
9718 : : {
9719 : 9 : append_to_statement_list (label, last_body);
9720 : 9 : gimplify_and_add (last_bind, pre_p);
9721 : 9 : last_bind = NULL_TREE;
9722 : : }
9723 : 346 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
9724 : : {
9725 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
9726 : : NULL, is_gimple_val, fb_rvalue);
9727 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
9728 : : }
9729 : 346 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
9730 : : return;
9731 : 346 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
9732 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
9733 : : return;
9734 : 346 : gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
9735 : : }
9736 : : }
9737 : 468 : if (last_bind)
9738 : : {
9739 : 109 : append_to_statement_list (label, last_body);
9740 : 109 : gimplify_and_add (last_bind, pre_p);
9741 : : }
9742 : : return;
9743 : : }
9744 : :
9745 : : /* Returns a tree expression containing the total iteration count of the
9746 : : OpenMP iterator IT. */
9747 : :
9748 : : static tree
9749 : 320 : compute_omp_iterator_count (tree it, gimple_seq *pre_p)
9750 : : {
9751 : 320 : tree tcnt = size_one_node;
9752 : 706 : for (; it; it = TREE_CHAIN (it))
9753 : : {
9754 : 386 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9755 : : is_gimple_val, fb_rvalue) == GS_ERROR
9756 : 386 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9757 : : is_gimple_val, fb_rvalue) == GS_ERROR
9758 : 386 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9759 : : is_gimple_val, fb_rvalue) == GS_ERROR
9760 : 772 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9761 : : is_gimple_val, fb_rvalue) == GS_ERROR))
9762 : 0 : return NULL_TREE;
9763 : 386 : tree var = TREE_VEC_ELT (it, 0);
9764 : 386 : tree begin = TREE_VEC_ELT (it, 1);
9765 : 386 : tree end = TREE_VEC_ELT (it, 2);
9766 : 386 : tree step = TREE_VEC_ELT (it, 3);
9767 : 386 : tree orig_step = TREE_VEC_ELT (it, 4);
9768 : 386 : tree type = TREE_TYPE (var);
9769 : 386 : tree stype = TREE_TYPE (step);
9770 : 386 : location_t loc = DECL_SOURCE_LOCATION (var);
9771 : 386 : tree endmbegin;
9772 : : /* Compute count for this iterator as
9773 : : orig_step > 0
9774 : : ? (begin < end ? (end - begin + (step - 1)) / step : 0)
9775 : : : (begin > end ? (end - begin + (step + 1)) / step : 0)
9776 : : and compute product of those for the entire clause. */
9777 : 386 : if (POINTER_TYPE_P (type))
9778 : 42 : endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR, stype, end, begin);
9779 : : else
9780 : 344 : endmbegin = fold_build2_loc (loc, MINUS_EXPR, type, end, begin);
9781 : 386 : tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype, step,
9782 : : build_int_cst (stype, 1));
9783 : 386 : tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
9784 : : build_int_cst (stype, 1));
9785 : 386 : tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
9786 : : unshare_expr (endmbegin), stepm1);
9787 : 386 : pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, pos, step);
9788 : 386 : tree neg = fold_build2_loc (loc, PLUS_EXPR, stype, endmbegin, stepp1);
9789 : 386 : if (TYPE_UNSIGNED (stype))
9790 : : {
9791 : 31 : neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
9792 : 31 : step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
9793 : : }
9794 : 386 : neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, neg, step);
9795 : 386 : step = NULL_TREE;
9796 : 386 : tree cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, begin, end);
9797 : 386 : pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
9798 : : build_int_cst (stype, 0));
9799 : 386 : cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, end, begin);
9800 : 386 : neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
9801 : : build_int_cst (stype, 0));
9802 : 386 : tree osteptype = TREE_TYPE (orig_step);
9803 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9804 : : build_int_cst (osteptype, 0));
9805 : 386 : tree cnt = fold_build3_loc (loc, COND_EXPR, stype, cond, pos, neg);
9806 : 386 : cnt = fold_convert_loc (loc, sizetype, cnt);
9807 : 386 : if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
9808 : : fb_rvalue) == GS_ERROR)
9809 : : return NULL_TREE;
9810 : 386 : tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
9811 : : }
9812 : 320 : if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
9813 : : return NULL_TREE;
9814 : :
9815 : 320 : return tcnt;
9816 : : }
9817 : :
9818 : : /* Build loops iterating over the space defined by the OpenMP iterator IT.
9819 : : Returns a pointer to the BIND_EXPR_BODY in the innermost loop body.
9820 : : LAST_BIND is set to point to the BIND_EXPR containing the whole loop. */
9821 : :
9822 : : static tree *
9823 : 320 : build_omp_iterator_loop (tree it, gimple_seq *pre_p, tree *last_bind)
9824 : : {
9825 : 320 : if (*last_bind)
9826 : 31 : gimplify_and_add (*last_bind, pre_p);
9827 : 320 : tree block = TREE_VEC_ELT (it, 5);
9828 : 320 : *last_bind = build3 (BIND_EXPR, void_type_node,
9829 : 320 : BLOCK_VARS (block), NULL, block);
9830 : 320 : TREE_SIDE_EFFECTS (*last_bind) = 1;
9831 : 320 : tree *p = &BIND_EXPR_BODY (*last_bind);
9832 : 706 : for (; it; it = TREE_CHAIN (it))
9833 : : {
9834 : 386 : tree var = TREE_VEC_ELT (it, 0);
9835 : 386 : tree begin = TREE_VEC_ELT (it, 1);
9836 : 386 : tree end = TREE_VEC_ELT (it, 2);
9837 : 386 : tree step = TREE_VEC_ELT (it, 3);
9838 : 386 : tree orig_step = TREE_VEC_ELT (it, 4);
9839 : 386 : tree type = TREE_TYPE (var);
9840 : 386 : location_t loc = DECL_SOURCE_LOCATION (var);
9841 : : /* Emit:
9842 : : var = begin;
9843 : : goto cond_label;
9844 : : beg_label:
9845 : : ...
9846 : : var = var + step;
9847 : : cond_label:
9848 : : if (orig_step > 0) {
9849 : : if (var < end) goto beg_label;
9850 : : } else {
9851 : : if (var > end) goto beg_label;
9852 : : }
9853 : : for each iterator, with inner iterators added to
9854 : : the ... above. */
9855 : 386 : tree beg_label = create_artificial_label (loc);
9856 : 386 : tree cond_label = NULL_TREE;
9857 : 386 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, begin);
9858 : 386 : append_to_statement_list_force (tem, p);
9859 : 386 : tem = build_and_jump (&cond_label);
9860 : 386 : append_to_statement_list_force (tem, p);
9861 : 386 : tem = build1 (LABEL_EXPR, void_type_node, beg_label);
9862 : 386 : append_to_statement_list (tem, p);
9863 : 386 : tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9864 : : NULL_TREE, NULL_TREE);
9865 : 386 : TREE_SIDE_EFFECTS (bind) = 1;
9866 : 386 : SET_EXPR_LOCATION (bind, loc);
9867 : 386 : append_to_statement_list_force (bind, p);
9868 : 386 : if (POINTER_TYPE_P (type))
9869 : 42 : tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
9870 : : var, fold_convert_loc (loc, sizetype, step));
9871 : : else
9872 : 344 : tem = build2_loc (loc, PLUS_EXPR, type, var, step);
9873 : 386 : tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, tem);
9874 : 386 : append_to_statement_list_force (tem, p);
9875 : 386 : tem = build1 (LABEL_EXPR, void_type_node, cond_label);
9876 : 386 : append_to_statement_list (tem, p);
9877 : 386 : tree cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, var, end);
9878 : 386 : tree pos = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9879 : : build_and_jump (&beg_label), void_node);
9880 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, var, end);
9881 : 386 : tree neg = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9882 : : build_and_jump (&beg_label), void_node);
9883 : 386 : tree osteptype = TREE_TYPE (orig_step);
9884 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9885 : : build_int_cst (osteptype, 0));
9886 : 386 : tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, pos, neg);
9887 : 386 : append_to_statement_list_force (tem, p);
9888 : 386 : p = &BIND_EXPR_BODY (bind);
9889 : : }
9890 : :
9891 : 320 : return p;
9892 : : }
9893 : :
9894 : :
9895 : : /* Callback for walk_tree to find a VAR_DECL (stored in DATA) in the
9896 : : tree TP. */
9897 : :
9898 : : static tree
9899 : 2306 : find_var_decl (tree *tp, int *, void *data)
9900 : : {
9901 : 2306 : if (*tp == (tree) data)
9902 : 206 : return *tp;
9903 : :
9904 : : return NULL_TREE;
9905 : : }
9906 : :
9907 : : /* Returns an element-by-element copy of OMP iterator tree IT. */
9908 : :
9909 : : static tree
9910 : 198 : copy_omp_iterator (tree it, int elem_count = -1)
9911 : : {
9912 : 198 : if (elem_count < 0)
9913 : 60 : elem_count = TREE_VEC_LENGTH (it);
9914 : 198 : tree new_it = make_tree_vec (elem_count);
9915 : 1386 : for (int i = 0; i < TREE_VEC_LENGTH (it); i++)
9916 : 1188 : TREE_VEC_ELT (new_it, i) = TREE_VEC_ELT (it, i);
9917 : :
9918 : 198 : return new_it;
9919 : : }
9920 : :
9921 : : /* Helper function for walk_tree in remap_omp_iterator_var. */
9922 : :
9923 : : static tree
9924 : 871 : remap_omp_iterator_var_1 (tree *tp, int *, void *data)
9925 : : {
9926 : 871 : tree old_var = ((tree *) data)[0];
9927 : 871 : tree new_var = ((tree *) data)[1];
9928 : :
9929 : 871 : if (*tp == old_var)
9930 : 92 : *tp = new_var;
9931 : 871 : return NULL_TREE;
9932 : : }
9933 : :
9934 : : /* Replace instances of OLD_VAR in TP with NEW_VAR. */
9935 : :
9936 : : static void
9937 : 184 : remap_omp_iterator_var (tree *tp, tree old_var, tree new_var)
9938 : : {
9939 : 184 : tree vars[2] = { old_var, new_var };
9940 : 184 : walk_tree (tp, remap_omp_iterator_var_1, vars, NULL);
9941 : 184 : }
9942 : :
9943 : : /* Scan through all clauses using OpenMP iterators in LIST_P. If any
9944 : : clauses have iterators with variables that are not used by the clause
9945 : : decl or size, issue a warning and replace the iterator with a copy with
9946 : : the unused variables removed. */
9947 : :
9948 : : static void
9949 : 24517 : remove_unused_omp_iterator_vars (tree *list_p)
9950 : : {
9951 : 24517 : auto_vec< vec<tree> > iter_vars;
9952 : 24517 : auto_vec<tree> new_iterators;
9953 : :
9954 : 65922 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9955 : : {
9956 : 41405 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
9957 : 41349 : continue;
9958 : 146 : auto_vec<tree> vars;
9959 : 146 : bool need_new_iterators = false;
9960 : 416 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
9961 : : {
9962 : 270 : tree var = TREE_VEC_ELT (it, 0);
9963 : 270 : tree t = walk_tree (&OMP_CLAUSE_DECL (c), find_var_decl, var, NULL);
9964 : 270 : if (t == NULL_TREE)
9965 : 126 : t = walk_tree (&OMP_CLAUSE_SIZE (c), find_var_decl, var, NULL);
9966 : 126 : if (t == NULL_TREE)
9967 : : {
9968 : 64 : need_new_iterators = true;
9969 : 64 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9970 : 44 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO
9971 : 28 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FROM))
9972 : 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
9973 : 92 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
9974 : 40 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
9975 : : "iterator variable %qE not used in clause "
9976 : 40 : "expression", DECL_NAME (var));
9977 : : }
9978 : : else
9979 : 206 : vars.safe_push (var);
9980 : : }
9981 : 146 : if (!need_new_iterators)
9982 : 82 : continue;
9983 : 64 : if (need_new_iterators && vars.is_empty ())
9984 : : {
9985 : : /* No iteration variables are used in the clause - remove the
9986 : : iterator from the clause. */
9987 : 8 : OMP_CLAUSE_ITERATORS (c) = NULL_TREE;
9988 : 8 : continue;
9989 : : }
9990 : :
9991 : : /* If a new iterator has been created for the current set of used
9992 : : iterator variables, then use that as the iterator. Otherwise,
9993 : : create a new iterator for the current iterator variable set. */
9994 : : unsigned i;
9995 : 92 : for (i = 0; i < iter_vars.length (); i++)
9996 : : {
9997 : 112 : if (vars.length () != iter_vars[i].length ())
9998 : 0 : continue;
9999 : : bool identical_p = true;
10000 : 136 : for (unsigned j = 0; j < vars.length () && identical_p; j++)
10001 : 80 : identical_p = vars[j] == iter_vars[i][j];
10002 : :
10003 : 56 : if (identical_p)
10004 : : break;
10005 : : }
10006 : 56 : if (i < iter_vars.length ())
10007 : 20 : OMP_CLAUSE_ITERATORS (c) = new_iterators[i];
10008 : : else
10009 : : {
10010 : 36 : tree new_iters = NULL_TREE;
10011 : 36 : tree *new_iters_p = &new_iters;
10012 : 36 : tree new_vars = NULL_TREE;
10013 : 36 : tree *new_vars_p = &new_vars;
10014 : 36 : i = 0;
10015 : 112 : for (tree it = OMP_CLAUSE_ITERATORS (c); it && i < vars.length();
10016 : 76 : it = TREE_CHAIN (it))
10017 : : {
10018 : 76 : tree var = TREE_VEC_ELT (it, 0);
10019 : 76 : if (var == vars[i])
10020 : : {
10021 : 60 : *new_iters_p = copy_omp_iterator (it);
10022 : 60 : *new_vars_p = build_decl (OMP_CLAUSE_LOCATION (c), VAR_DECL,
10023 : 60 : DECL_NAME (var), TREE_TYPE (var));
10024 : 60 : DECL_ARTIFICIAL (*new_vars_p) = 1;
10025 : 60 : DECL_CONTEXT (*new_vars_p) = DECL_CONTEXT (var);
10026 : 60 : TREE_VEC_ELT (*new_iters_p, 0) = *new_vars_p;
10027 : 60 : new_iters_p = &TREE_CHAIN (*new_iters_p);
10028 : 60 : new_vars_p = &DECL_CHAIN (*new_vars_p);
10029 : 60 : i++;
10030 : : }
10031 : : }
10032 : 36 : tree new_block = make_node (BLOCK);
10033 : 36 : BLOCK_VARS (new_block) = new_vars;
10034 : 36 : TREE_VEC_ELT (new_iters, 5) = new_block;
10035 : 36 : new_iterators.safe_push (new_iters);
10036 : 36 : iter_vars.safe_push (vars.copy ());
10037 : 36 : OMP_CLAUSE_ITERATORS (c) = new_iters;
10038 : : }
10039 : :
10040 : : /* Remap clause to use the new variables. */
10041 : 56 : i = 0;
10042 : 148 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
10043 : : {
10044 : 92 : tree old_var = vars[i++];
10045 : 92 : tree new_var = TREE_VEC_ELT (it, 0);
10046 : 92 : remap_omp_iterator_var (&OMP_CLAUSE_DECL (c), old_var, new_var);
10047 : 92 : remap_omp_iterator_var (&OMP_CLAUSE_SIZE (c), old_var, new_var);
10048 : : }
10049 : 146 : }
10050 : :
10051 : 24553 : for (unsigned i = 0; i < iter_vars.length (); i++)
10052 : 36 : iter_vars[i].release ();
10053 : 24517 : }
10054 : :
10055 : 140 : struct iterator_loop_info_t
10056 : : {
10057 : : tree bind;
10058 : : tree count;
10059 : : tree index;
10060 : : tree body_label;
10061 : : auto_vec<tree> clauses;
10062 : : };
10063 : :
10064 : : typedef hash_map<tree, iterator_loop_info_t> iterator_loop_info_map_t;
10065 : :
10066 : : /* Builds a loop to expand any OpenMP iterators in the clauses in LIST_P,
10067 : : reusing any previously built loops if they use the same set of iterators.
10068 : : Generated Gimple statements are placed into LOOPS_SEQ_P. The clause
10069 : : iterators are updated with information on how and where to insert code into
10070 : : the loop body. */
10071 : :
10072 : : static void
10073 : 24517 : build_omp_iterators_loops (tree *list_p, gimple_seq *loops_seq_p)
10074 : : {
10075 : 24517 : iterator_loop_info_map_t loops;
10076 : :
10077 : 65922 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10078 : : {
10079 : 41405 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10080 : 41267 : continue;
10081 : :
10082 : 138 : bool built_p;
10083 : 138 : iterator_loop_info_t &loop
10084 : 138 : = loops.get_or_insert (OMP_CLAUSE_ITERATORS (c), &built_p);
10085 : :
10086 : 138 : if (!built_p)
10087 : : {
10088 : 70 : loop.count = compute_omp_iterator_count (OMP_CLAUSE_ITERATORS (c),
10089 : : loops_seq_p);
10090 : 70 : if (!loop.count)
10091 : 0 : continue;
10092 : 70 : if (integer_zerop (loop.count))
10093 : 8 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
10094 : : "iteration count is zero");
10095 : :
10096 : 70 : loop.bind = NULL_TREE;
10097 : 70 : tree *body = build_omp_iterator_loop (OMP_CLAUSE_ITERATORS (c),
10098 : : loops_seq_p, &loop.bind);
10099 : :
10100 : 70 : loop.index = create_tmp_var (sizetype);
10101 : 70 : SET_EXPR_LOCATION (loop.bind, OMP_CLAUSE_LOCATION (c));
10102 : :
10103 : : /* BEFORE LOOP: */
10104 : : /* idx = -1; */
10105 : : /* This should be initialized to before the individual elements,
10106 : : as idx is pre-incremented in the loop body. */
10107 : 70 : gimple *assign = gimple_build_assign (loop.index, size_int (-1));
10108 : 70 : gimple_seq_add_stmt (loops_seq_p, assign);
10109 : :
10110 : : /* IN LOOP BODY: */
10111 : : /* Create a label so we can find this point later. */
10112 : 70 : loop.body_label = create_artificial_label (OMP_CLAUSE_LOCATION (c));
10113 : 70 : tree tem = build1 (LABEL_EXPR, void_type_node, loop.body_label);
10114 : 70 : append_to_statement_list_force (tem, body);
10115 : :
10116 : : /* idx += 2; */
10117 : 70 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10118 : : void_type_node, loop.index,
10119 : : size_binop (PLUS_EXPR, loop.index, size_int (2)));
10120 : 70 : append_to_statement_list_force (tem, body);
10121 : : }
10122 : :
10123 : : /* Create array to hold expanded values. */
10124 : 138 : tree last_count_2 = size_binop (MULT_EXPR, loop.count, size_int (2));
10125 : 138 : tree arr_length = size_binop (PLUS_EXPR, last_count_2, size_int (1));
10126 : 138 : tree elems = NULL_TREE;
10127 : 138 : if (TREE_CONSTANT (arr_length))
10128 : : {
10129 : 138 : tree type = build_array_type (ptr_type_node,
10130 : : build_index_type (arr_length));
10131 : 138 : elems = create_tmp_var_raw (type, "omp_iter_data");
10132 : 138 : TREE_ADDRESSABLE (elems) = 1;
10133 : 138 : gimple_add_tmp_var (elems);
10134 : : }
10135 : : else
10136 : : {
10137 : : /* Handle dynamic sizes. */
10138 : 0 : sorry ("dynamic iterator sizes not implemented yet");
10139 : : }
10140 : :
10141 : : /* BEFORE LOOP: */
10142 : : /* elems[0] = count; */
10143 : 138 : tree lhs = build4 (ARRAY_REF, ptr_type_node, elems, size_int (0),
10144 : : NULL_TREE, NULL_TREE);
10145 : 138 : tree tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10146 : : void_type_node, lhs, loop.count);
10147 : 138 : gimplify_and_add (tem, loops_seq_p);
10148 : :
10149 : : /* Make a copy of the iterator with extra info at the end. */
10150 : 138 : int elem_count = TREE_VEC_LENGTH (OMP_CLAUSE_ITERATORS (c));
10151 : 138 : tree new_iterator = copy_omp_iterator (OMP_CLAUSE_ITERATORS (c),
10152 : : elem_count + 3);
10153 : 138 : TREE_VEC_ELT (new_iterator, elem_count) = loop.body_label;
10154 : 138 : TREE_VEC_ELT (new_iterator, elem_count + 1) = elems;
10155 : 138 : TREE_VEC_ELT (new_iterator, elem_count + 2) = loop.index;
10156 : 138 : TREE_CHAIN (new_iterator) = TREE_CHAIN (OMP_CLAUSE_ITERATORS (c));
10157 : 138 : OMP_CLAUSE_ITERATORS (c) = new_iterator;
10158 : :
10159 : 138 : loop.clauses.safe_push (c);
10160 : : }
10161 : :
10162 : : /* Now gimplify and add all the loops that were built. */
10163 : 24587 : for (hash_map<tree, iterator_loop_info_t>::iterator it = loops.begin ();
10164 : 49174 : it != loops.end (); ++it)
10165 : 70 : gimplify_and_add ((*it).second.bind, loops_seq_p);
10166 : 24517 : }
10167 : :
10168 : : /* Helper function for enter_omp_iterator_loop_context. */
10169 : :
10170 : : static gimple_seq *
10171 : 1591 : enter_omp_iterator_loop_context_1 (tree iterator, gimple_seq *loops_seq_p)
10172 : : {
10173 : : /* Drill into the nested bind expressions to get to the loop body. */
10174 : 1591 : for (gimple_stmt_iterator gsi = gsi_start (*loops_seq_p);
10175 : 10066 : !gsi_end_p (gsi); gsi_next (&gsi))
10176 : : {
10177 : 9645 : gimple *stmt = gsi_stmt (gsi);
10178 : :
10179 : 9645 : switch (gimple_code (stmt))
10180 : : {
10181 : 1261 : case GIMPLE_BIND:
10182 : 1261 : {
10183 : 1261 : gbind *bind_stmt = as_a<gbind *> (stmt);
10184 : 1261 : gimple_push_bind_expr (bind_stmt);
10185 : 1261 : gimple_seq *bind_body_p = gimple_bind_body_ptr (bind_stmt);
10186 : 1261 : gimple_seq *seq =
10187 : 1261 : enter_omp_iterator_loop_context_1 (iterator, bind_body_p);
10188 : 1261 : if (seq)
10189 : : return seq;
10190 : 421 : gimple_pop_bind_expr ();
10191 : : }
10192 : 421 : break;
10193 : 0 : case GIMPLE_TRY:
10194 : 0 : {
10195 : 0 : gimple_seq *try_eval_p = gimple_try_eval_ptr (stmt);
10196 : 0 : gimple_seq *seq =
10197 : 0 : enter_omp_iterator_loop_context_1 (iterator, try_eval_p);
10198 : 0 : if (seq)
10199 : : return seq;
10200 : : }
10201 : : break;
10202 : 1793 : case GIMPLE_LABEL:
10203 : 1793 : {
10204 : 1793 : glabel *label_stmt = as_a<glabel *> (stmt);
10205 : 1793 : tree label = gimple_label_label (label_stmt);
10206 : 1793 : if (label == TREE_VEC_ELT (iterator, 6))
10207 : : return loops_seq_p;
10208 : : }
10209 : : break;
10210 : : default:
10211 : : break;
10212 : : }
10213 : : }
10214 : :
10215 : : return NULL;
10216 : : }
10217 : :
10218 : : /* Enter the Gimplification context in LOOPS_SEQ_P for the iterator loop
10219 : : associated with OpenMP clause C. Returns the gimple_seq for the loop body
10220 : : if C has OpenMP iterators, or ALT_SEQ_P if not. */
10221 : :
10222 : : static gimple_seq *
10223 : 61561 : enter_omp_iterator_loop_context (tree c, gimple_seq *loops_seq_p,
10224 : : gimple_seq *alt_seq_p)
10225 : : {
10226 : 61561 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10227 : : return alt_seq_p;
10228 : :
10229 : 330 : push_gimplify_context ();
10230 : :
10231 : 330 : gimple_seq *seq = enter_omp_iterator_loop_context_1 (OMP_CLAUSE_ITERATORS (c),
10232 : : loops_seq_p);
10233 : 330 : gcc_assert (seq);
10234 : : return seq;
10235 : : }
10236 : :
10237 : : /* Enter the Gimplification context in STMT for the iterator loop associated
10238 : : with OpenMP clause C. Returns the gimple_seq for the loop body if C has
10239 : : OpenMP iterators, or ALT_SEQ_P if not. */
10240 : :
10241 : : gimple_seq *
10242 : 192 : enter_omp_iterator_loop_context (tree c, gomp_target *stmt,
10243 : : gimple_seq *alt_seq_p)
10244 : : {
10245 : 192 : gimple_seq *loops_seq_p = gimple_omp_target_iterator_loops_ptr (stmt);
10246 : 192 : return enter_omp_iterator_loop_context (c, loops_seq_p, alt_seq_p);
10247 : : }
10248 : :
10249 : : /* Exit the Gimplification context for the OpenMP clause C. */
10250 : :
10251 : : void
10252 : 61561 : exit_omp_iterator_loop_context (tree c)
10253 : : {
10254 : 61561 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10255 : : return;
10256 : 1170 : while (!gimplify_ctxp->bind_expr_stack.is_empty ())
10257 : 840 : gimple_pop_bind_expr ();
10258 : 330 : pop_gimplify_context (NULL);
10259 : : }
10260 : :
10261 : : /* If *LIST_P contains any OpenMP depend clauses with iterators,
10262 : : lower all the depend clauses by populating corresponding depend
10263 : : array. Returns 0 if there are no such depend clauses, or
10264 : : 2 if all depend clauses should be removed, 1 otherwise. */
10265 : :
10266 : : static int
10267 : 1898 : gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
10268 : : {
10269 : 1898 : tree c;
10270 : 1898 : gimple *g;
10271 : 1898 : size_t n[5] = { 0, 0, 0, 0, 0 };
10272 : 1898 : bool unused[5];
10273 : 1898 : tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
10274 : 1898 : tree last_iter = NULL_TREE, last_count = NULL_TREE;
10275 : 1898 : size_t i, j;
10276 : 1898 : location_t first_loc = UNKNOWN_LOCATION;
10277 : :
10278 : 6236 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10279 : 4338 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10280 : : {
10281 : 2200 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10282 : : {
10283 : : case OMP_CLAUSE_DEPEND_IN:
10284 : : i = 2;
10285 : : break;
10286 : : case OMP_CLAUSE_DEPEND_OUT:
10287 : : case OMP_CLAUSE_DEPEND_INOUT:
10288 : : i = 0;
10289 : : break;
10290 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10291 : : i = 1;
10292 : : break;
10293 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
10294 : : i = 3;
10295 : : break;
10296 : : case OMP_CLAUSE_DEPEND_INOUTSET:
10297 : : i = 4;
10298 : : break;
10299 : 0 : default:
10300 : 0 : gcc_unreachable ();
10301 : : }
10302 : 2200 : tree t = OMP_CLAUSE_DECL (c);
10303 : 2200 : if (first_loc == UNKNOWN_LOCATION)
10304 : 1898 : first_loc = OMP_CLAUSE_LOCATION (c);
10305 : 2200 : if (OMP_ITERATOR_DECL_P (t))
10306 : : {
10307 : 297 : if (TREE_PURPOSE (t) != last_iter)
10308 : : {
10309 : 250 : tree tcnt = compute_omp_iterator_count (TREE_PURPOSE (t),
10310 : : pre_p);
10311 : 250 : if (!tcnt)
10312 : : return 2;
10313 : 250 : last_iter = TREE_PURPOSE (t);
10314 : 250 : last_count = tcnt;
10315 : : }
10316 : 297 : if (counts[i] == NULL_TREE)
10317 : 243 : counts[i] = last_count;
10318 : : else
10319 : 54 : counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
10320 : : PLUS_EXPR, counts[i], last_count);
10321 : : }
10322 : : else
10323 : 1903 : n[i]++;
10324 : : }
10325 : 10601 : for (i = 0; i < 5; i++)
10326 : 8908 : if (counts[i])
10327 : : break;
10328 : 1898 : if (i == 5)
10329 : : return 0;
10330 : :
10331 : 205 : tree total = size_zero_node;
10332 : 1230 : for (i = 0; i < 5; i++)
10333 : : {
10334 : 1025 : unused[i] = counts[i] == NULL_TREE && n[i] == 0;
10335 : 1025 : if (counts[i] == NULL_TREE)
10336 : 782 : counts[i] = size_zero_node;
10337 : 1025 : if (n[i])
10338 : 44 : counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
10339 : 1025 : if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
10340 : : fb_rvalue) == GS_ERROR)
10341 : : return 2;
10342 : 1025 : total = size_binop (PLUS_EXPR, total, counts[i]);
10343 : : }
10344 : :
10345 : 205 : if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
10346 : : == GS_ERROR)
10347 : : return 2;
10348 : 205 : bool is_old = unused[1] && unused[3] && unused[4];
10349 : 205 : tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
10350 : : size_int (is_old ? 1 : 4));
10351 : 205 : if (!unused[4])
10352 : 3 : totalpx = size_binop (PLUS_EXPR, totalpx,
10353 : : size_binop (MULT_EXPR, counts[4], size_int (2)));
10354 : 205 : tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
10355 : 205 : tree array = create_tmp_var_raw (type);
10356 : 205 : TREE_ADDRESSABLE (array) = 1;
10357 : 205 : if (!poly_int_tree_p (totalpx))
10358 : : {
10359 : 81 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
10360 : 81 : gimplify_type_sizes (TREE_TYPE (array), pre_p);
10361 : 81 : if (gimplify_omp_ctxp)
10362 : : {
10363 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10364 : : while (ctx
10365 : 20 : && (ctx->region_type == ORT_WORKSHARE
10366 : : || ctx->region_type == ORT_TASKGROUP
10367 : 20 : || ctx->region_type == ORT_SIMD
10368 : 20 : || ctx->region_type == ORT_ACC))
10369 : 0 : ctx = ctx->outer_context;
10370 : 20 : if (ctx)
10371 : 20 : omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
10372 : : }
10373 : 81 : gimplify_vla_decl (array, pre_p);
10374 : : }
10375 : : else
10376 : 124 : gimple_add_tmp_var (array);
10377 : 205 : tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
10378 : : NULL_TREE);
10379 : 205 : tree tem;
10380 : 205 : if (!is_old)
10381 : : {
10382 : 31 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10383 : : build_int_cst (ptr_type_node, 0));
10384 : 31 : gimplify_and_add (tem, pre_p);
10385 : 31 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
10386 : : NULL_TREE);
10387 : : }
10388 : 205 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10389 : : fold_convert (ptr_type_node, total));
10390 : 205 : gimplify_and_add (tem, pre_p);
10391 : 801 : for (i = 1; i < (is_old ? 2 : 4); i++)
10392 : : {
10393 : 267 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
10394 : : NULL_TREE, NULL_TREE);
10395 : 267 : tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
10396 : 267 : gimplify_and_add (tem, pre_p);
10397 : : }
10398 : :
10399 : : tree cnts[6];
10400 : 717 : for (j = 5; j; j--)
10401 : 717 : if (!unused[j - 1])
10402 : : break;
10403 : 1230 : for (i = 0; i < 5; i++)
10404 : : {
10405 : 1025 : if (i && (i >= j || unused[i - 1]))
10406 : : {
10407 : 770 : cnts[i] = cnts[i - 1];
10408 : 770 : continue;
10409 : : }
10410 : 255 : cnts[i] = create_tmp_var (sizetype);
10411 : 255 : if (i == 0)
10412 : 236 : g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
10413 : : else
10414 : : {
10415 : 50 : tree t;
10416 : 50 : if (is_old)
10417 : 38 : t = size_binop (PLUS_EXPR, counts[0], size_int (2));
10418 : : else
10419 : 12 : t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
10420 : 50 : if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
10421 : : == GS_ERROR)
10422 : 0 : return 2;
10423 : 50 : g = gimple_build_assign (cnts[i], t);
10424 : : }
10425 : 255 : gimple_seq_add_stmt (pre_p, g);
10426 : : }
10427 : 205 : if (unused[4])
10428 : 202 : cnts[5] = NULL_TREE;
10429 : : else
10430 : : {
10431 : 3 : tree t = size_binop (PLUS_EXPR, total, size_int (5));
10432 : 3 : cnts[5] = create_tmp_var (sizetype);
10433 : 3 : g = gimple_build_assign (cnts[i], t);
10434 : 3 : gimple_seq_add_stmt (pre_p, g);
10435 : : }
10436 : :
10437 : 205 : last_iter = NULL_TREE;
10438 : 205 : tree last_bind = NULL_TREE;
10439 : 205 : tree *last_body = NULL;
10440 : 563 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10441 : 358 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10442 : : {
10443 : 343 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10444 : : {
10445 : : case OMP_CLAUSE_DEPEND_IN:
10446 : : i = 2;
10447 : : break;
10448 : : case OMP_CLAUSE_DEPEND_OUT:
10449 : : case OMP_CLAUSE_DEPEND_INOUT:
10450 : : i = 0;
10451 : : break;
10452 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10453 : : i = 1;
10454 : : break;
10455 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
10456 : : i = 3;
10457 : : break;
10458 : : case OMP_CLAUSE_DEPEND_INOUTSET:
10459 : : i = 4;
10460 : : break;
10461 : 0 : default:
10462 : 0 : gcc_unreachable ();
10463 : : }
10464 : 343 : tree t = OMP_CLAUSE_DECL (c);
10465 : 343 : if (OMP_ITERATOR_DECL_P (t))
10466 : : {
10467 : 297 : if (TREE_PURPOSE (t) != last_iter)
10468 : : {
10469 : 250 : last_body = build_omp_iterator_loop (TREE_PURPOSE (t), pre_p,
10470 : : &last_bind);
10471 : 250 : SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
10472 : : }
10473 : 297 : last_iter = TREE_PURPOSE (t);
10474 : 297 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
10475 : : {
10476 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
10477 : : 0), last_body);
10478 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
10479 : : }
10480 : 297 : if (error_operand_p (TREE_VALUE (t)))
10481 : : return 2;
10482 : 297 : if (TREE_VALUE (t) != null_pointer_node)
10483 : 291 : TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
10484 : 297 : if (i == 4)
10485 : : {
10486 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10487 : : NULL_TREE, NULL_TREE);
10488 : 3 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10489 : : NULL_TREE, NULL_TREE);
10490 : 3 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10491 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10492 : : void_type_node, r, r2);
10493 : 3 : append_to_statement_list_force (tem, last_body);
10494 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10495 : : void_type_node, cnts[i],
10496 : : size_binop (PLUS_EXPR, cnts[i],
10497 : : size_int (1)));
10498 : 3 : append_to_statement_list_force (tem, last_body);
10499 : 3 : i = 5;
10500 : : }
10501 : 297 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10502 : : NULL_TREE, NULL_TREE);
10503 : 594 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10504 : 297 : void_type_node, r, TREE_VALUE (t));
10505 : 297 : append_to_statement_list_force (tem, last_body);
10506 : 297 : if (i == 5)
10507 : : {
10508 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array,
10509 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10510 : : NULL_TREE, NULL_TREE);
10511 : 3 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10512 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10513 : : void_type_node, r, tem);
10514 : 3 : append_to_statement_list_force (tem, last_body);
10515 : : }
10516 : 297 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10517 : : void_type_node, cnts[i],
10518 : 297 : size_binop (PLUS_EXPR, cnts[i],
10519 : : size_int (1 + (i == 5))));
10520 : 297 : append_to_statement_list_force (tem, last_body);
10521 : 297 : TREE_VALUE (t) = null_pointer_node;
10522 : : }
10523 : : else
10524 : : {
10525 : 46 : if (last_bind)
10526 : : {
10527 : 18 : gimplify_and_add (last_bind, pre_p);
10528 : 18 : last_bind = NULL_TREE;
10529 : : }
10530 : 46 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
10531 : : {
10532 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
10533 : : NULL, is_gimple_val, fb_rvalue);
10534 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
10535 : : }
10536 : 46 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
10537 : : return 2;
10538 : 46 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
10539 : 46 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
10540 : 46 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
10541 : : is_gimple_val, fb_rvalue) == GS_ERROR)
10542 : : return 2;
10543 : 46 : if (i == 4)
10544 : : {
10545 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10546 : : NULL_TREE, NULL_TREE);
10547 : 0 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10548 : : NULL_TREE, NULL_TREE);
10549 : 0 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10550 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
10551 : 0 : gimplify_and_add (tem, pre_p);
10552 : 0 : g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
10553 : : cnts[i],
10554 : : size_int (1)));
10555 : 0 : gimple_seq_add_stmt (pre_p, g);
10556 : 0 : i = 5;
10557 : : }
10558 : 46 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10559 : : NULL_TREE, NULL_TREE);
10560 : 46 : tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
10561 : 46 : gimplify_and_add (tem, pre_p);
10562 : 46 : if (i == 5)
10563 : : {
10564 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array,
10565 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10566 : : NULL_TREE, NULL_TREE);
10567 : 0 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10568 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
10569 : 0 : append_to_statement_list_force (tem, last_body);
10570 : 0 : gimplify_and_add (tem, pre_p);
10571 : : }
10572 : 46 : g = gimple_build_assign (cnts[i],
10573 : 46 : size_binop (PLUS_EXPR, cnts[i],
10574 : : size_int (1 + (i == 5))));
10575 : 46 : gimple_seq_add_stmt (pre_p, g);
10576 : : }
10577 : : }
10578 : 205 : if (last_bind)
10579 : 201 : gimplify_and_add (last_bind, pre_p);
10580 : 205 : tree cond = boolean_false_node;
10581 : 205 : if (is_old)
10582 : : {
10583 : 174 : if (!unused[0])
10584 : 88 : cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
10585 : : size_binop_loc (first_loc, PLUS_EXPR, counts[0],
10586 : : size_int (2)));
10587 : 174 : if (!unused[2])
10588 : 124 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10589 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10590 : : cnts[2],
10591 : : size_binop_loc (first_loc, PLUS_EXPR,
10592 : : totalpx,
10593 : : size_int (1))));
10594 : : }
10595 : : else
10596 : : {
10597 : 31 : tree prev = size_int (5);
10598 : 186 : for (i = 0; i < 5; i++)
10599 : : {
10600 : 155 : if (unused[i])
10601 : 112 : continue;
10602 : 43 : prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
10603 : 43 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10604 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10605 : : cnts[i], unshare_expr (prev)));
10606 : : }
10607 : : }
10608 : 205 : tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
10609 : : build_call_expr_loc (first_loc,
10610 : : builtin_decl_explicit (BUILT_IN_TRAP),
10611 : : 0), void_node);
10612 : 205 : gimplify_and_add (tem, pre_p);
10613 : 205 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
10614 : 205 : OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
10615 : 205 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
10616 : 205 : OMP_CLAUSE_CHAIN (c) = *list_p;
10617 : 205 : *list_p = c;
10618 : 205 : return 1;
10619 : : }
10620 : :
10621 : : /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor. */
10622 : :
10623 : : static bool
10624 : 113490 : omp_map_clause_descriptor_p (tree c)
10625 : : {
10626 : 113490 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10627 : : return false;
10628 : :
10629 : 113488 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
10630 : : return true;
10631 : :
10632 : 74915 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
10633 : 69243 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
10634 : 75355 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
10635 : 4042 : return true;
10636 : :
10637 : : return false;
10638 : : }
10639 : :
10640 : : /* For a set of mappings describing an array section pointed to by a struct
10641 : : (or derived type, etc.) component, create an "alloc" or "release" node to
10642 : : insert into a list following a GOMP_MAP_STRUCT node. For some types of
10643 : : mapping (e.g. Fortran arrays with descriptors), an additional mapping may
10644 : : be created that is inserted into the list of mapping nodes attached to the
10645 : : directive being processed -- not part of the sorted list of nodes after
10646 : : GOMP_MAP_STRUCT.
10647 : :
10648 : : CODE is the code of the directive being processed. GRP_START and GRP_END
10649 : : are the first and last of two or three nodes representing this array section
10650 : : mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
10651 : : GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER). EXTRA_NODE is
10652 : : filled with the additional node described above, if needed.
10653 : :
10654 : : This function does not add the new nodes to any lists itself. It is the
10655 : : responsibility of the caller to do that. */
10656 : :
10657 : : static tree
10658 : 1718 : build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
10659 : : tree *extra_node)
10660 : : {
10661 : 1525 : enum gomp_map_kind mkind
10662 : 1718 : = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
10663 : 1718 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
10664 : :
10665 : 1718 : gcc_assert (grp_start != grp_end);
10666 : :
10667 : 1718 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10668 : 1718 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
10669 : 1718 : OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
10670 : 1718 : OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
10671 : 1718 : tree grp_mid = NULL_TREE;
10672 : 1718 : if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
10673 : 185 : grp_mid = OMP_CLAUSE_CHAIN (grp_start);
10674 : :
10675 : 185 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
10676 : 0 : OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
10677 : : else
10678 : 1718 : OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
10679 : :
10680 : 1718 : if (grp_mid
10681 : 185 : && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
10682 : 1903 : && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
10683 : : {
10684 : 0 : tree c3
10685 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10686 : 0 : OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
10687 : 0 : OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
10688 : 0 : OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
10689 : 0 : OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
10690 : :
10691 : 0 : *extra_node = c3;
10692 : : }
10693 : : else
10694 : 1718 : *extra_node = NULL_TREE;
10695 : :
10696 : 1718 : return c2;
10697 : : }
10698 : :
10699 : : /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
10700 : : and set *BITPOSP and *POFFSETP to the bit offset of the access.
10701 : : If BASE_REF is non-NULL and the containing object is a reference, set
10702 : : *BASE_REF to that reference before dereferencing the object.
10703 : : If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
10704 : : has array type, else return NULL. */
10705 : :
10706 : : static tree
10707 : 6542 : extract_base_bit_offset (tree base, poly_int64 *bitposp,
10708 : : poly_offset_int *poffsetp,
10709 : : bool *variable_offset)
10710 : : {
10711 : 6542 : tree offset;
10712 : 6542 : poly_int64 bitsize, bitpos;
10713 : 6542 : machine_mode mode;
10714 : 6542 : int unsignedp, reversep, volatilep = 0;
10715 : 6542 : poly_offset_int poffset;
10716 : :
10717 : 6542 : STRIP_NOPS (base);
10718 : :
10719 : 6542 : base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
10720 : : &unsignedp, &reversep, &volatilep);
10721 : :
10722 : 6542 : STRIP_NOPS (base);
10723 : :
10724 : 6542 : if (offset && poly_int_tree_p (offset))
10725 : : {
10726 : 0 : poffset = wi::to_poly_offset (offset);
10727 : 0 : *variable_offset = false;
10728 : : }
10729 : : else
10730 : : {
10731 : 6542 : poffset = 0;
10732 : 6542 : *variable_offset = (offset != NULL_TREE);
10733 : : }
10734 : :
10735 : 6542 : if (maybe_ne (bitpos, 0))
10736 : 4939 : poffset += bits_to_bytes_round_down (bitpos);
10737 : :
10738 : 6542 : *bitposp = bitpos;
10739 : 6542 : *poffsetp = poffset;
10740 : :
10741 : 6542 : return base;
10742 : : }
10743 : :
10744 : : /* Used for topological sorting of mapping groups. UNVISITED means we haven't
10745 : : started processing the group yet. The TEMPORARY mark is used when we first
10746 : : encounter a group on a depth-first traversal, and the PERMANENT mark is used
10747 : : when we have processed all the group's children (i.e. all the base pointers
10748 : : referred to by the group's mapping nodes, recursively). */
10749 : :
10750 : : enum omp_tsort_mark {
10751 : : UNVISITED,
10752 : : TEMPORARY,
10753 : : PERMANENT
10754 : : };
10755 : :
10756 : : /* Hash for trees based on operand_equal_p. Like tree_operand_hash
10757 : : but ignores side effects in the equality comparisons. */
10758 : :
10759 : : struct tree_operand_hash_no_se : tree_operand_hash
10760 : : {
10761 : : static inline bool equal (const value_type &,
10762 : : const compare_type &);
10763 : : };
10764 : :
10765 : : inline bool
10766 : 355492 : tree_operand_hash_no_se::equal (const value_type &t1,
10767 : : const compare_type &t2)
10768 : : {
10769 : 355492 : return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
10770 : : }
10771 : :
10772 : : /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
10773 : : clause. */
10774 : :
10775 : : struct omp_mapping_group {
10776 : : tree *grp_start;
10777 : : tree grp_end;
10778 : : omp_tsort_mark mark;
10779 : : /* If we've removed the group but need to reindex, mark the group as
10780 : : deleted. */
10781 : : bool deleted;
10782 : : /* The group points to an already-created "GOMP_MAP_STRUCT
10783 : : GOMP_MAP_ATTACH_DETACH" pair. */
10784 : : bool reprocess_struct;
10785 : : /* The group should use "zero-length" allocations for pointers that are not
10786 : : mapped "to" on the same directive. */
10787 : : bool fragile;
10788 : : struct omp_mapping_group *sibling;
10789 : : struct omp_mapping_group *next;
10790 : : };
10791 : :
10792 : : DEBUG_FUNCTION void
10793 : 0 : debug_mapping_group (omp_mapping_group *grp)
10794 : : {
10795 : 0 : tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
10796 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
10797 : 0 : debug_generic_expr (*grp->grp_start);
10798 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
10799 : 0 : }
10800 : :
10801 : : /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
10802 : : isn't one. */
10803 : :
10804 : : static tree
10805 : 32957 : omp_get_base_pointer (tree expr)
10806 : : {
10807 : 32957 : while (TREE_CODE (expr) == ARRAY_REF
10808 : 38247 : || TREE_CODE (expr) == COMPONENT_REF)
10809 : 5290 : expr = TREE_OPERAND (expr, 0);
10810 : :
10811 : 32957 : if (INDIRECT_REF_P (expr)
10812 : 32957 : || (TREE_CODE (expr) == MEM_REF
10813 : 0 : && integer_zerop (TREE_OPERAND (expr, 1))))
10814 : : {
10815 : 9645 : expr = TREE_OPERAND (expr, 0);
10816 : 9682 : while (TREE_CODE (expr) == COMPOUND_EXPR)
10817 : 37 : expr = TREE_OPERAND (expr, 1);
10818 : 9645 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
10819 : 837 : expr = TREE_OPERAND (expr, 0);
10820 : 9645 : if (TREE_CODE (expr) == SAVE_EXPR)
10821 : 31 : expr = TREE_OPERAND (expr, 0);
10822 : 9645 : STRIP_NOPS (expr);
10823 : 9645 : return expr;
10824 : : }
10825 : :
10826 : : return NULL_TREE;
10827 : : }
10828 : :
10829 : : /* An attach or detach operation depends directly on the address being
10830 : : attached/detached. Return that address, or none if there are no
10831 : : attachments/detachments. */
10832 : :
10833 : : static tree
10834 : 15064 : omp_get_attachment (omp_mapping_group *grp)
10835 : : {
10836 : 15064 : tree node = *grp->grp_start;
10837 : :
10838 : 15064 : switch (OMP_CLAUSE_MAP_KIND (node))
10839 : : {
10840 : 12554 : case GOMP_MAP_TO:
10841 : 12554 : case GOMP_MAP_FROM:
10842 : 12554 : case GOMP_MAP_TOFROM:
10843 : 12554 : case GOMP_MAP_ALWAYS_FROM:
10844 : 12554 : case GOMP_MAP_ALWAYS_TO:
10845 : 12554 : case GOMP_MAP_ALWAYS_TOFROM:
10846 : 12554 : case GOMP_MAP_FORCE_FROM:
10847 : 12554 : case GOMP_MAP_FORCE_TO:
10848 : 12554 : case GOMP_MAP_FORCE_TOFROM:
10849 : 12554 : case GOMP_MAP_FORCE_PRESENT:
10850 : 12554 : case GOMP_MAP_PRESENT_ALLOC:
10851 : 12554 : case GOMP_MAP_PRESENT_FROM:
10852 : 12554 : case GOMP_MAP_PRESENT_TO:
10853 : 12554 : case GOMP_MAP_PRESENT_TOFROM:
10854 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
10855 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_TO:
10856 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10857 : 12554 : case GOMP_MAP_ALLOC:
10858 : 12554 : case GOMP_MAP_RELEASE:
10859 : 12554 : case GOMP_MAP_DELETE:
10860 : 12554 : case GOMP_MAP_FORCE_ALLOC:
10861 : 12554 : if (node == grp->grp_end)
10862 : : return NULL_TREE;
10863 : :
10864 : 5985 : node = OMP_CLAUSE_CHAIN (node);
10865 : 5985 : if (node && omp_map_clause_descriptor_p (node))
10866 : : {
10867 : 1002 : gcc_assert (node != grp->grp_end);
10868 : 1002 : node = OMP_CLAUSE_CHAIN (node);
10869 : : }
10870 : 5985 : if (node)
10871 : 5985 : switch (OMP_CLAUSE_MAP_KIND (node))
10872 : : {
10873 : : case GOMP_MAP_POINTER:
10874 : : case GOMP_MAP_ALWAYS_POINTER:
10875 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10876 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10877 : : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10878 : : return NULL_TREE;
10879 : :
10880 : 2087 : case GOMP_MAP_ATTACH_DETACH:
10881 : 2087 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10882 : 2087 : case GOMP_MAP_DETACH:
10883 : 2087 : return OMP_CLAUSE_DECL (node);
10884 : :
10885 : 0 : default:
10886 : 0 : internal_error ("unexpected mapping node");
10887 : : }
10888 : 0 : return error_mark_node;
10889 : :
10890 : 0 : case GOMP_MAP_TO_PSET:
10891 : 0 : gcc_assert (node != grp->grp_end);
10892 : 0 : node = OMP_CLAUSE_CHAIN (node);
10893 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10894 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10895 : 0 : return OMP_CLAUSE_DECL (node);
10896 : : else
10897 : 0 : internal_error ("unexpected mapping node");
10898 : : return error_mark_node;
10899 : :
10900 : 447 : case GOMP_MAP_ATTACH:
10901 : 447 : case GOMP_MAP_DETACH:
10902 : 447 : node = OMP_CLAUSE_CHAIN (node);
10903 : 447 : if (!node || *grp->grp_start == grp->grp_end)
10904 : 447 : return OMP_CLAUSE_DECL (*grp->grp_start);
10905 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10906 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10907 : 0 : return OMP_CLAUSE_DECL (*grp->grp_start);
10908 : : else
10909 : 0 : internal_error ("unexpected mapping node");
10910 : : return error_mark_node;
10911 : :
10912 : : case GOMP_MAP_STRUCT:
10913 : : case GOMP_MAP_STRUCT_UNORD:
10914 : : case GOMP_MAP_FORCE_DEVICEPTR:
10915 : : case GOMP_MAP_DEVICE_RESIDENT:
10916 : : case GOMP_MAP_LINK:
10917 : : case GOMP_MAP_IF_PRESENT:
10918 : : case GOMP_MAP_FIRSTPRIVATE:
10919 : : case GOMP_MAP_FIRSTPRIVATE_INT:
10920 : : case GOMP_MAP_USE_DEVICE_PTR:
10921 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10922 : : return NULL_TREE;
10923 : :
10924 : 0 : default:
10925 : 0 : internal_error ("unexpected mapping node");
10926 : : }
10927 : :
10928 : : return error_mark_node;
10929 : : }
10930 : :
10931 : : /* Given a pointer START_P to the start of a group of related (e.g. pointer)
10932 : : mappings, return the chain pointer to the end of that group in the list. */
10933 : :
10934 : : static tree *
10935 : 104782 : omp_group_last (tree *start_p)
10936 : : {
10937 : 104782 : tree c = *start_p, nc, *grp_last_p = start_p;
10938 : :
10939 : 104782 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
10940 : :
10941 : 104782 : nc = OMP_CLAUSE_CHAIN (c);
10942 : :
10943 : 183251 : if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
10944 : : return grp_last_p;
10945 : :
10946 : 66392 : switch (OMP_CLAUSE_MAP_KIND (c))
10947 : : {
10948 : : default:
10949 : : while (nc
10950 : 107051 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10951 : 224557 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10952 : 99889 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10953 : 89719 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
10954 : 79015 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
10955 : 53138 : || (OMP_CLAUSE_MAP_KIND (nc)
10956 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10957 : 53122 : || (OMP_CLAUSE_MAP_KIND (nc)
10958 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
10959 : 52755 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
10960 : 52700 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
10961 : 50796 : || omp_map_clause_descriptor_p (nc)))
10962 : : {
10963 : 62930 : tree nc2 = OMP_CLAUSE_CHAIN (nc);
10964 : 62930 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
10965 : : {
10966 : : /* In the specific case we're doing "exit data" on an array
10967 : : slice of a reference-to-pointer struct component, we will see
10968 : : DETACH followed by ATTACH_DETACH here. We want to treat that
10969 : : as a single group. In other cases DETACH might represent a
10970 : : stand-alone "detach" clause, so we don't want to consider
10971 : : that part of the group. */
10972 : 55 : if (nc2
10973 : 16 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10974 : 71 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
10975 : 0 : goto consume_two_nodes;
10976 : : else
10977 : : break;
10978 : : }
10979 : 62875 : if (nc2
10980 : 45911 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10981 : 39398 : && (OMP_CLAUSE_MAP_KIND (nc)
10982 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10983 : 62891 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
10984 : : {
10985 : 16 : consume_two_nodes:
10986 : 16 : grp_last_p = &OMP_CLAUSE_CHAIN (nc);
10987 : 16 : c = nc2;
10988 : 16 : nc = OMP_CLAUSE_CHAIN (nc2);
10989 : : }
10990 : : else
10991 : : {
10992 : 62859 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
10993 : 62859 : c = nc;
10994 : 62859 : nc = nc2;
10995 : : }
10996 : : }
10997 : : break;
10998 : :
10999 : 195 : case GOMP_MAP_ATTACH:
11000 : 195 : case GOMP_MAP_DETACH:
11001 : : /* This is a weird artifact of how directives are parsed: bare attach or
11002 : : detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
11003 : : FIRSTPRIVATE_REFERENCE node. FIXME. */
11004 : 195 : if (nc
11005 : 195 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11006 : 195 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11007 : 195 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
11008 : 0 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11009 : : break;
11010 : :
11011 : 27 : case GOMP_MAP_TO_PSET:
11012 : 27 : if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11013 : 27 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
11014 : 6 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
11015 : 27 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11016 : : break;
11017 : :
11018 : 5026 : case GOMP_MAP_STRUCT:
11019 : 5026 : case GOMP_MAP_STRUCT_UNORD:
11020 : 5026 : {
11021 : 5026 : unsigned HOST_WIDE_INT num_mappings
11022 : 5026 : = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
11023 : 5026 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
11024 : 4688 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11025 : 9232 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
11026 : 1134 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11027 : 12263 : for (unsigned i = 0; i < num_mappings; i++)
11028 : 7237 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11029 : : }
11030 : : break;
11031 : : }
11032 : :
11033 : : return grp_last_p;
11034 : : }
11035 : :
11036 : : /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
11037 : : OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
11038 : : associated GOMP_MAP_POINTER mappings). Return a vector of omp_mapping_group
11039 : : if we have more than one such group, else return NULL. */
11040 : :
11041 : : static void
11042 : 86470 : omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
11043 : : tree gather_sentinel)
11044 : : {
11045 : 86470 : for (tree *cp = list_p;
11046 : 255958 : *cp && *cp != gather_sentinel;
11047 : 169488 : cp = &OMP_CLAUSE_CHAIN (*cp))
11048 : : {
11049 : 169488 : if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
11050 : 94835 : continue;
11051 : :
11052 : 74653 : tree *grp_last_p = omp_group_last (cp);
11053 : 74653 : omp_mapping_group grp;
11054 : :
11055 : 74653 : grp.grp_start = cp;
11056 : 74653 : grp.grp_end = *grp_last_p;
11057 : 74653 : grp.mark = UNVISITED;
11058 : 74653 : grp.sibling = NULL;
11059 : 74653 : grp.deleted = false;
11060 : 74653 : grp.reprocess_struct = false;
11061 : 74653 : grp.fragile = false;
11062 : 74653 : grp.next = NULL;
11063 : 74653 : groups->safe_push (grp);
11064 : :
11065 : 74653 : cp = grp_last_p;
11066 : : }
11067 : 86470 : }
11068 : :
11069 : : static vec<omp_mapping_group> *
11070 : 86152 : omp_gather_mapping_groups (tree *list_p)
11071 : : {
11072 : 86152 : vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
11073 : :
11074 : 86152 : omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
11075 : :
11076 : 86152 : if (groups->length () > 0)
11077 : : return groups;
11078 : : else
11079 : : {
11080 : 43934 : delete groups;
11081 : 43934 : return NULL;
11082 : : }
11083 : : }
11084 : :
11085 : : /* A pointer mapping group GRP may define a block of memory starting at some
11086 : : base address, and maybe also define a firstprivate pointer or firstprivate
11087 : : reference that points to that block. The return value is a node containing
11088 : : the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
11089 : : If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
11090 : : return the number of consecutive chained nodes in CHAINED. */
11091 : :
11092 : : static tree
11093 : 75047 : omp_group_base (omp_mapping_group *grp, unsigned int *chained,
11094 : : tree *firstprivate)
11095 : : {
11096 : 75047 : tree node = *grp->grp_start;
11097 : :
11098 : 75047 : *firstprivate = NULL_TREE;
11099 : 75047 : *chained = 1;
11100 : :
11101 : 75047 : switch (OMP_CLAUSE_MAP_KIND (node))
11102 : : {
11103 : 71307 : case GOMP_MAP_TO:
11104 : 71307 : case GOMP_MAP_FROM:
11105 : 71307 : case GOMP_MAP_TOFROM:
11106 : 71307 : case GOMP_MAP_ALWAYS_FROM:
11107 : 71307 : case GOMP_MAP_ALWAYS_TO:
11108 : 71307 : case GOMP_MAP_ALWAYS_TOFROM:
11109 : 71307 : case GOMP_MAP_FORCE_FROM:
11110 : 71307 : case GOMP_MAP_FORCE_TO:
11111 : 71307 : case GOMP_MAP_FORCE_TOFROM:
11112 : 71307 : case GOMP_MAP_FORCE_PRESENT:
11113 : 71307 : case GOMP_MAP_PRESENT_ALLOC:
11114 : 71307 : case GOMP_MAP_PRESENT_FROM:
11115 : 71307 : case GOMP_MAP_PRESENT_TO:
11116 : 71307 : case GOMP_MAP_PRESENT_TOFROM:
11117 : 71307 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
11118 : 71307 : case GOMP_MAP_ALWAYS_PRESENT_TO:
11119 : 71307 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
11120 : 71307 : case GOMP_MAP_ALLOC:
11121 : 71307 : case GOMP_MAP_RELEASE:
11122 : 71307 : case GOMP_MAP_DELETE:
11123 : 71307 : case GOMP_MAP_FORCE_ALLOC:
11124 : 71307 : case GOMP_MAP_IF_PRESENT:
11125 : 71307 : if (node == grp->grp_end)
11126 : : return node;
11127 : :
11128 : 32693 : node = OMP_CLAUSE_CHAIN (node);
11129 : 32693 : if (!node)
11130 : 0 : internal_error ("unexpected mapping node");
11131 : 32693 : if (omp_map_clause_descriptor_p (node))
11132 : : {
11133 : 9678 : if (node == grp->grp_end)
11134 : 0 : return *grp->grp_start;
11135 : 9678 : node = OMP_CLAUSE_CHAIN (node);
11136 : : }
11137 : 32693 : switch (OMP_CLAUSE_MAP_KIND (node))
11138 : : {
11139 : 22980 : case GOMP_MAP_POINTER:
11140 : 22980 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11141 : 22980 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11142 : 22980 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11143 : 22980 : *firstprivate = OMP_CLAUSE_DECL (node);
11144 : 22980 : return *grp->grp_start;
11145 : :
11146 : 9713 : case GOMP_MAP_ALWAYS_POINTER:
11147 : 9713 : case GOMP_MAP_ATTACH_DETACH:
11148 : 9713 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11149 : 9713 : case GOMP_MAP_DETACH:
11150 : 9713 : return *grp->grp_start;
11151 : :
11152 : 0 : default:
11153 : 0 : internal_error ("unexpected mapping node");
11154 : : }
11155 : : return error_mark_node;
11156 : :
11157 : 18 : case GOMP_MAP_TO_PSET:
11158 : 18 : gcc_assert (node != grp->grp_end);
11159 : 18 : node = OMP_CLAUSE_CHAIN (node);
11160 : 18 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
11161 : 18 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
11162 : : return NULL_TREE;
11163 : : else
11164 : 0 : internal_error ("unexpected mapping node");
11165 : : return error_mark_node;
11166 : :
11167 : 1015 : case GOMP_MAP_ATTACH:
11168 : 1015 : case GOMP_MAP_DETACH:
11169 : 1015 : node = OMP_CLAUSE_CHAIN (node);
11170 : 1015 : if (!node || *grp->grp_start == grp->grp_end)
11171 : : return NULL_TREE;
11172 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11173 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11174 : : {
11175 : : /* We're mapping the base pointer itself in a bare attach or detach
11176 : : node. This is a side effect of how parsing works, and the mapping
11177 : : will be removed anyway (at least for enter/exit data directives).
11178 : : We should ignore the mapping here. FIXME. */
11179 : : return NULL_TREE;
11180 : : }
11181 : : else
11182 : 0 : internal_error ("unexpected mapping node");
11183 : : return error_mark_node;
11184 : :
11185 : 2423 : case GOMP_MAP_STRUCT:
11186 : 2423 : case GOMP_MAP_STRUCT_UNORD:
11187 : 2423 : {
11188 : 2423 : unsigned HOST_WIDE_INT num_mappings
11189 : 2423 : = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
11190 : 2423 : node = OMP_CLAUSE_CHAIN (node);
11191 : 2423 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11192 : 2423 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11193 : : {
11194 : 393 : *firstprivate = OMP_CLAUSE_DECL (node);
11195 : 393 : node = OMP_CLAUSE_CHAIN (node);
11196 : : }
11197 : 2030 : else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
11198 : 314 : node = OMP_CLAUSE_CHAIN (node);
11199 : 2423 : *chained = num_mappings;
11200 : 2423 : return node;
11201 : : }
11202 : :
11203 : : case GOMP_MAP_FORCE_DEVICEPTR:
11204 : : case GOMP_MAP_DEVICE_RESIDENT:
11205 : : case GOMP_MAP_LINK:
11206 : : case GOMP_MAP_FIRSTPRIVATE:
11207 : : case GOMP_MAP_FIRSTPRIVATE_INT:
11208 : : case GOMP_MAP_USE_DEVICE_PTR:
11209 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11210 : : return NULL_TREE;
11211 : :
11212 : 0 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11213 : 0 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11214 : 0 : case GOMP_MAP_POINTER:
11215 : 0 : case GOMP_MAP_ALWAYS_POINTER:
11216 : 0 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11217 : : /* These shouldn't appear by themselves. */
11218 : 0 : if (!seen_error ())
11219 : 0 : internal_error ("unexpected pointer mapping node");
11220 : 0 : return error_mark_node;
11221 : :
11222 : 0 : default:
11223 : 0 : gcc_unreachable ();
11224 : : }
11225 : :
11226 : : return error_mark_node;
11227 : : }
11228 : :
11229 : : /* Given a vector of omp_mapping_groups, build a hash table so we can look up
11230 : : nodes by tree_operand_hash_no_se. */
11231 : :
11232 : : static void
11233 : 42702 : omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
11234 : : omp_mapping_group *> *grpmap,
11235 : : vec<omp_mapping_group> *groups,
11236 : : tree reindex_sentinel)
11237 : : {
11238 : 42702 : omp_mapping_group *grp;
11239 : 42702 : unsigned int i;
11240 : 42702 : bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
11241 : :
11242 : 118203 : FOR_EACH_VEC_ELT (*groups, i, grp)
11243 : : {
11244 : 75501 : if (reindexing && *grp->grp_start == reindex_sentinel)
11245 : 75501 : above_hwm = true;
11246 : :
11247 : 75501 : if (reindexing && !above_hwm)
11248 : 52128 : continue;
11249 : :
11250 : 75201 : if (grp->reprocess_struct)
11251 : 154 : continue;
11252 : :
11253 : 75047 : tree fpp;
11254 : 75047 : unsigned int chained;
11255 : 75047 : tree node = omp_group_base (grp, &chained, &fpp);
11256 : :
11257 : 75047 : if (node == error_mark_node || (!node && !fpp))
11258 : 1317 : continue;
11259 : :
11260 : : for (unsigned j = 0;
11261 : 148537 : node && j < chained;
11262 : 74807 : node = OMP_CLAUSE_CHAIN (node), j++)
11263 : : {
11264 : 74807 : tree decl = OMP_CLAUSE_DECL (node);
11265 : : /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
11266 : : meaning node-hash lookups don't work. This is a workaround for
11267 : : that, but ideally we should just create the INDIRECT_REF at
11268 : : source instead. FIXME. */
11269 : 74807 : if (TREE_CODE (decl) == MEM_REF
11270 : 74807 : && integer_zerop (TREE_OPERAND (decl, 1)))
11271 : 0 : decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
11272 : :
11273 : 74807 : omp_mapping_group **prev = grpmap->get (decl);
11274 : :
11275 : 74807 : if (prev && *prev == grp)
11276 : : /* Empty. */;
11277 : 74807 : else if (prev)
11278 : : {
11279 : : /* Mapping the same thing twice is normally diagnosed as an error,
11280 : : but can happen under some circumstances, e.g. in pr99928-16.c,
11281 : : the directive:
11282 : :
11283 : : #pragma omp target simd reduction(+:a[:3]) \
11284 : : map(always, tofrom: a[:6])
11285 : : ...
11286 : :
11287 : : will result in two "a[0]" mappings (of different sizes). */
11288 : :
11289 : 140 : grp->sibling = (*prev)->sibling;
11290 : 140 : (*prev)->sibling = grp;
11291 : : }
11292 : : else
11293 : 74667 : grpmap->put (decl, grp);
11294 : : }
11295 : :
11296 : 73730 : if (!fpp)
11297 : 50357 : continue;
11298 : :
11299 : 23373 : omp_mapping_group **prev = grpmap->get (fpp);
11300 : 23373 : if (prev && *prev != grp)
11301 : : {
11302 : 12 : grp->sibling = (*prev)->sibling;
11303 : 12 : (*prev)->sibling = grp;
11304 : : }
11305 : : else
11306 : 23361 : grpmap->put (fpp, grp);
11307 : : }
11308 : 42702 : }
11309 : :
11310 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11311 : 42218 : omp_index_mapping_groups (vec<omp_mapping_group> *groups)
11312 : : {
11313 : 42218 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11314 : 42218 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11315 : :
11316 : 42218 : omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
11317 : :
11318 : 42218 : return grpmap;
11319 : : }
11320 : :
11321 : : /* Rebuild group map from partially-processed clause list (during
11322 : : omp_build_struct_sibling_lists). We have already processed nodes up until
11323 : : a high-water mark (HWM). This is a bit tricky because the list is being
11324 : : reordered as it is scanned, but we know:
11325 : :
11326 : : 1. The list after HWM has not been touched yet, so we can reindex it safely.
11327 : :
11328 : : 2. The list before and including HWM has been altered, but remains
11329 : : well-formed throughout the sibling-list building operation.
11330 : :
11331 : : so, we can do the reindex operation in two parts, on the processed and
11332 : : then the unprocessed halves of the list. */
11333 : :
11334 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11335 : 318 : omp_reindex_mapping_groups (tree *list_p,
11336 : : vec<omp_mapping_group> *groups,
11337 : : vec<omp_mapping_group> *processed_groups,
11338 : : tree sentinel)
11339 : : {
11340 : 318 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11341 : 318 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11342 : :
11343 : 318 : processed_groups->truncate (0);
11344 : :
11345 : 318 : omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
11346 : 318 : omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
11347 : 318 : if (sentinel)
11348 : 166 : omp_index_mapping_groups_1 (grpmap, groups, sentinel);
11349 : :
11350 : 318 : return grpmap;
11351 : : }
11352 : :
11353 : : /* Find the immediately-containing struct for a component ref (etc.)
11354 : : expression EXPR. */
11355 : :
11356 : : static tree
11357 : 37431 : omp_containing_struct (tree expr)
11358 : : {
11359 : 37431 : tree expr0 = expr;
11360 : :
11361 : 37431 : STRIP_NOPS (expr);
11362 : :
11363 : : /* Note: don't strip NOPs unless we're also stripping off array refs or a
11364 : : component ref. */
11365 : 37431 : if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
11366 : : return expr0;
11367 : :
11368 : 18580 : while (TREE_CODE (expr) == ARRAY_REF)
11369 : 2142 : expr = TREE_OPERAND (expr, 0);
11370 : :
11371 : 16438 : if (TREE_CODE (expr) == COMPONENT_REF)
11372 : 15517 : expr = TREE_OPERAND (expr, 0);
11373 : :
11374 : : return expr;
11375 : : }
11376 : :
11377 : : /* Return TRUE if DECL describes a component that is part of a whole structure
11378 : : that is mapped elsewhere in GRPMAP. *MAPPED_BY_GROUP is set to the group
11379 : : that maps that structure, if present. */
11380 : :
11381 : : static bool
11382 : 21987 : omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
11383 : : omp_mapping_group *> *grpmap,
11384 : : tree decl,
11385 : : omp_mapping_group **mapped_by_group)
11386 : : {
11387 : 21987 : tree wsdecl = NULL_TREE;
11388 : :
11389 : 21987 : *mapped_by_group = NULL;
11390 : :
11391 : 37431 : while (true)
11392 : : {
11393 : 37431 : wsdecl = omp_containing_struct (decl);
11394 : 37431 : if (wsdecl == decl)
11395 : : break;
11396 : 16438 : omp_mapping_group **wholestruct = grpmap->get (wsdecl);
11397 : 16438 : if (!wholestruct
11398 : 15444 : && TREE_CODE (wsdecl) == MEM_REF
11399 : 16438 : && integer_zerop (TREE_OPERAND (wsdecl, 1)))
11400 : : {
11401 : 0 : tree deref = TREE_OPERAND (wsdecl, 0);
11402 : 0 : deref = build_fold_indirect_ref (deref);
11403 : 0 : wholestruct = grpmap->get (deref);
11404 : : }
11405 : 16438 : if (wholestruct)
11406 : : {
11407 : 994 : *mapped_by_group = *wholestruct;
11408 : 994 : return true;
11409 : : }
11410 : : decl = wsdecl;
11411 : : }
11412 : :
11413 : : return false;
11414 : : }
11415 : :
11416 : : /* Helper function for omp_tsort_mapping_groups. Returns TRUE on success, or
11417 : : FALSE on error. */
11418 : :
11419 : : static bool
11420 : 18255 : omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
11421 : : vec<omp_mapping_group> *groups,
11422 : : hash_map<tree_operand_hash_no_se,
11423 : : omp_mapping_group *> *grpmap,
11424 : : omp_mapping_group *grp)
11425 : : {
11426 : 18255 : if (grp->mark == PERMANENT)
11427 : : return true;
11428 : 15064 : if (grp->mark == TEMPORARY)
11429 : : {
11430 : 0 : fprintf (stderr, "when processing group:\n");
11431 : 0 : debug_mapping_group (grp);
11432 : 0 : internal_error ("base pointer cycle detected");
11433 : : return false;
11434 : : }
11435 : 15064 : grp->mark = TEMPORARY;
11436 : :
11437 : 15064 : tree attaches_to = omp_get_attachment (grp);
11438 : :
11439 : 15064 : if (attaches_to)
11440 : : {
11441 : 2534 : omp_mapping_group **basep = grpmap->get (attaches_to);
11442 : :
11443 : 2534 : if (basep && *basep != grp)
11444 : : {
11445 : 2920 : for (omp_mapping_group *w = *basep; w; w = w->sibling)
11446 : 1460 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11447 : : return false;
11448 : : }
11449 : : }
11450 : :
11451 : 15064 : tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
11452 : :
11453 : 20540 : while (decl)
11454 : : {
11455 : 20540 : tree base = omp_get_base_pointer (decl);
11456 : :
11457 : 20540 : if (!base)
11458 : : break;
11459 : :
11460 : 8053 : omp_mapping_group **innerp = grpmap->get (base);
11461 : 8053 : omp_mapping_group *wholestruct;
11462 : :
11463 : : /* We should treat whole-structure mappings as if all (pointer, in this
11464 : : case) members are mapped as individual list items. Check if we have
11465 : : such a whole-structure mapping, if we don't have an explicit reference
11466 : : to the pointer member itself. */
11467 : 8053 : if (!innerp
11468 : 3318 : && TREE_CODE (base) == COMPONENT_REF
11469 : 9444 : && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
11470 : : innerp = &wholestruct;
11471 : :
11472 : 8053 : if (innerp && *innerp != grp)
11473 : : {
11474 : 5154 : for (omp_mapping_group *w = *innerp; w; w = w->sibling)
11475 : 2577 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11476 : 0 : return false;
11477 : : break;
11478 : : }
11479 : :
11480 : 5476 : decl = base;
11481 : : }
11482 : :
11483 : 15064 : grp->mark = PERMANENT;
11484 : :
11485 : : /* Emit grp to output list. */
11486 : :
11487 : 15064 : **outlist = grp;
11488 : 15064 : *outlist = &grp->next;
11489 : :
11490 : 15064 : return true;
11491 : : }
11492 : :
11493 : : /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
11494 : : before mappings that use those pointers. This is an implementation of the
11495 : : depth-first search algorithm, described e.g. at:
11496 : :
11497 : : https://en.wikipedia.org/wiki/Topological_sorting
11498 : : */
11499 : :
11500 : : static omp_mapping_group *
11501 : 7701 : omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
11502 : : hash_map<tree_operand_hash_no_se, omp_mapping_group *>
11503 : : *grpmap,
11504 : : bool enter_exit_data)
11505 : : {
11506 : 7701 : omp_mapping_group *grp, *outlist = NULL, **cursor;
11507 : 7701 : unsigned int i;
11508 : 7701 : bool saw_runtime_implicit = false;
11509 : :
11510 : 7701 : cursor = &outlist;
11511 : :
11512 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11513 : : {
11514 : 15064 : if (grp->mark != PERMANENT)
11515 : : {
11516 : 14218 : if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11517 : : {
11518 : 238 : saw_runtime_implicit = true;
11519 : 238 : continue;
11520 : : }
11521 : 13980 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11522 : : return NULL;
11523 : : }
11524 : : }
11525 : :
11526 : 7701 : if (!saw_runtime_implicit)
11527 : 7555 : return outlist;
11528 : :
11529 : 629 : FOR_EACH_VEC_ELT (*groups, i, grp)
11530 : : {
11531 : 483 : if (grp->mark != PERMANENT
11532 : 483 : && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11533 : : {
11534 : : /* Clear the flag for enter/exit data because it is currently
11535 : : meaningless for those operations in libgomp. */
11536 : 238 : if (enter_exit_data)
11537 : 41 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
11538 : :
11539 : 238 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11540 : : return NULL;
11541 : : }
11542 : : }
11543 : :
11544 : 146 : return outlist;
11545 : : }
11546 : :
11547 : : /* Split INLIST into three parts:
11548 : :
11549 : : - "present" alloc/to/from groups
11550 : : - other to/from groups
11551 : : - other alloc/release/delete groups
11552 : :
11553 : : These sub-lists are then concatenated together to form the final list.
11554 : : Each sub-list retains the order of the original list.
11555 : : Note that ATTACH nodes are later moved to the end of the list in
11556 : : gimplify_adjust_omp_clauses, for target regions. */
11557 : :
11558 : : static omp_mapping_group *
11559 : 7701 : omp_segregate_mapping_groups (omp_mapping_group *inlist)
11560 : : {
11561 : 7701 : omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
11562 : 7701 : omp_mapping_group *p_groups = NULL;
11563 : 7701 : omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
11564 : 7701 : omp_mapping_group **p_tail = &p_groups;
11565 : :
11566 : 22765 : for (omp_mapping_group *w = inlist; w;)
11567 : : {
11568 : 15064 : tree c = *w->grp_start;
11569 : 15064 : omp_mapping_group *next = w->next;
11570 : :
11571 : 15064 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
11572 : :
11573 : 15064 : switch (OMP_CLAUSE_MAP_KIND (c))
11574 : : {
11575 : 1673 : case GOMP_MAP_ALLOC:
11576 : 1673 : case GOMP_MAP_RELEASE:
11577 : 1673 : case GOMP_MAP_DELETE:
11578 : 1673 : *ard_tail = w;
11579 : 1673 : w->next = NULL;
11580 : 1673 : ard_tail = &w->next;
11581 : 1673 : break;
11582 : :
11583 : : /* These map types are all semantically identical, so are moved into a
11584 : : single group. They will each be changed into GOMP_MAP_FORCE_PRESENT
11585 : : in gimplify_adjust_omp_clauses. */
11586 : 123 : case GOMP_MAP_PRESENT_ALLOC:
11587 : 123 : case GOMP_MAP_PRESENT_FROM:
11588 : 123 : case GOMP_MAP_PRESENT_TO:
11589 : 123 : case GOMP_MAP_PRESENT_TOFROM:
11590 : 123 : *p_tail = w;
11591 : 123 : w->next = NULL;
11592 : 123 : p_tail = &w->next;
11593 : 123 : break;
11594 : :
11595 : 13268 : default:
11596 : 13268 : *tf_tail = w;
11597 : 13268 : w->next = NULL;
11598 : 13268 : tf_tail = &w->next;
11599 : : }
11600 : :
11601 : : w = next;
11602 : : }
11603 : :
11604 : : /* Now splice the lists together... */
11605 : 7701 : *tf_tail = ard_groups;
11606 : 7701 : *p_tail = tf_groups;
11607 : :
11608 : 7701 : return p_groups;
11609 : : }
11610 : :
11611 : : /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
11612 : : those groups based on the output list of omp_tsort_mapping_groups --
11613 : : singly-linked, threaded through each element's NEXT pointer starting at
11614 : : HEAD. Each list element appears exactly once in that linked list.
11615 : :
11616 : : Each element of GROUPS may correspond to one or several mapping nodes.
11617 : : Node groups are kept together, and in the reordered list, the positions of
11618 : : the original groups are reused for the positions of the reordered list.
11619 : : Hence if we have e.g.
11620 : :
11621 : : {to ptr ptr} firstprivate {tofrom ptr} ...
11622 : : ^ ^ ^
11623 : : first group non-"map" second group
11624 : :
11625 : : and say the second group contains a base pointer for the first so must be
11626 : : moved before it, the resulting list will contain:
11627 : :
11628 : : {tofrom ptr} firstprivate {to ptr ptr} ...
11629 : : ^ prev. second group ^ prev. first group
11630 : : */
11631 : :
11632 : : static tree *
11633 : 7701 : omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
11634 : : omp_mapping_group *head,
11635 : : tree *list_p)
11636 : : {
11637 : 7701 : omp_mapping_group *grp;
11638 : 7701 : unsigned int i;
11639 : 7701 : unsigned numgroups = groups->length ();
11640 : 7701 : auto_vec<tree> old_heads (numgroups);
11641 : 7701 : auto_vec<tree *> old_headps (numgroups);
11642 : 7701 : auto_vec<tree> new_heads (numgroups);
11643 : 7701 : auto_vec<tree> old_succs (numgroups);
11644 : 7701 : bool map_at_start = (list_p == (*groups)[0].grp_start);
11645 : :
11646 : 7701 : tree *new_grp_tail = NULL;
11647 : :
11648 : : /* Stash the start & end nodes of each mapping group before we start
11649 : : modifying the list. */
11650 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11651 : : {
11652 : 15064 : old_headps.quick_push (grp->grp_start);
11653 : 15064 : old_heads.quick_push (*grp->grp_start);
11654 : 15064 : old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
11655 : : }
11656 : :
11657 : : /* And similarly, the heads of the groups in the order we want to rearrange
11658 : : the list to. */
11659 : 22765 : for (omp_mapping_group *w = head; w; w = w->next)
11660 : 15064 : new_heads.quick_push (*w->grp_start);
11661 : :
11662 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11663 : : {
11664 : 15064 : gcc_assert (head);
11665 : :
11666 : 15064 : if (new_grp_tail && old_succs[i - 1] == old_heads[i])
11667 : : {
11668 : : /* a {b c d} {e f g} h i j (original)
11669 : : -->
11670 : : a {k l m} {e f g} h i j (inserted new group on last iter)
11671 : : -->
11672 : : a {k l m} {n o p} h i j (this time, chain last group to new one)
11673 : : ^new_grp_tail
11674 : : */
11675 : 6741 : *new_grp_tail = new_heads[i];
11676 : : }
11677 : 8323 : else if (new_grp_tail)
11678 : : {
11679 : : /* a {b c d} e {f g h} i j k (original)
11680 : : -->
11681 : : a {l m n} e {f g h} i j k (gap after last iter's group)
11682 : : -->
11683 : : a {l m n} e {o p q} h i j (chain last group to old successor)
11684 : : ^new_grp_tail
11685 : : */
11686 : 622 : *new_grp_tail = old_succs[i - 1];
11687 : 622 : *old_headps[i] = new_heads[i];
11688 : : }
11689 : : else
11690 : : {
11691 : : /* The first inserted group -- point to new group, and leave end
11692 : : open.
11693 : : a {b c d} e f
11694 : : -->
11695 : : a {g h i...
11696 : : */
11697 : 7701 : *grp->grp_start = new_heads[i];
11698 : : }
11699 : :
11700 : 15064 : new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
11701 : :
11702 : 15064 : head = head->next;
11703 : : }
11704 : :
11705 : 7701 : if (new_grp_tail)
11706 : 7701 : *new_grp_tail = old_succs[numgroups - 1];
11707 : :
11708 : 7701 : gcc_assert (!head);
11709 : :
11710 : 10787 : return map_at_start ? (*groups)[0].grp_start : list_p;
11711 : 7701 : }
11712 : :
11713 : : /* DECL is supposed to have lastprivate semantics in the outer contexts
11714 : : of combined/composite constructs, starting with OCTX.
11715 : : Add needed lastprivate, shared or map clause if no data sharing or
11716 : : mapping clause are present. IMPLICIT_P is true if it is an implicit
11717 : : clause (IV on simd), in which case the lastprivate will not be
11718 : : copied to some constructs. */
11719 : :
11720 : : static void
11721 : 14651 : omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
11722 : : tree decl, bool implicit_p)
11723 : : {
11724 : 14651 : struct gimplify_omp_ctx *orig_octx = octx;
11725 : 26970 : for (; octx; octx = octx->outer_context)
11726 : : {
11727 : 25897 : if ((octx->region_type == ORT_COMBINED_PARALLEL
11728 : 20865 : || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
11729 : 25328 : && splay_tree_lookup (octx->variables,
11730 : : (splay_tree_key) decl) == NULL)
11731 : : {
11732 : 3883 : omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
11733 : 3883 : continue;
11734 : : }
11735 : 18379 : if ((octx->region_type & ORT_TASK) != 0
11736 : 575 : && octx->combined_loop
11737 : 18660 : && splay_tree_lookup (octx->variables,
11738 : : (splay_tree_key) decl) == NULL)
11739 : : {
11740 : 248 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11741 : 248 : continue;
11742 : : }
11743 : 22721 : if (implicit_p
11744 : 13627 : && octx->region_type == ORT_WORKSHARE
11745 : 10407 : && octx->combined_loop
11746 : 10302 : && splay_tree_lookup (octx->variables,
11747 : : (splay_tree_key) decl) == NULL
11748 : 10302 : && octx->outer_context
11749 : 8909 : && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
11750 : 22728 : && splay_tree_lookup (octx->outer_context->variables,
11751 : : (splay_tree_key) decl) == NULL)
11752 : : {
11753 : 4838 : octx = octx->outer_context;
11754 : 4838 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11755 : 4838 : continue;
11756 : : }
11757 : 5567 : if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
11758 : 7478 : && octx->combined_loop
11759 : 7291 : && splay_tree_lookup (octx->variables,
11760 : : (splay_tree_key) decl) == NULL
11761 : 19066 : && !omp_check_private (octx, decl, false))
11762 : : {
11763 : 3350 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11764 : 3350 : continue;
11765 : : }
11766 : 9695 : if (octx->region_type == ORT_COMBINED_TARGET)
11767 : : {
11768 : 2004 : splay_tree_node n = splay_tree_lookup (octx->variables,
11769 : : (splay_tree_key) decl);
11770 : 2004 : if (n == NULL)
11771 : : {
11772 : 1982 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11773 : 1982 : octx = octx->outer_context;
11774 : : }
11775 : 22 : else if (!implicit_p
11776 : 22 : && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
11777 : : {
11778 : 12 : n->value &= ~(GOVD_FIRSTPRIVATE
11779 : : | GOVD_FIRSTPRIVATE_IMPLICIT
11780 : : | GOVD_EXPLICIT);
11781 : 12 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11782 : 12 : octx = octx->outer_context;
11783 : : }
11784 : : }
11785 : : break;
11786 : : }
11787 : 14651 : if (octx && (implicit_p || octx != orig_octx))
11788 : 4056 : omp_notice_variable (octx, decl, true);
11789 : 14651 : }
11790 : :
11791 : : /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
11792 : : a "FIRSTPRIVATE" mapping. Return the one that isn't firstprivate, etc. */
11793 : :
11794 : : static omp_mapping_group *
11795 : 5304 : omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
11796 : : omp_mapping_group *> *grpmap,
11797 : : tree decl, bool allow_deleted = false)
11798 : : {
11799 : 5304 : omp_mapping_group **to_group_p = grpmap->get (decl);
11800 : :
11801 : 5304 : if (!to_group_p)
11802 : : return NULL;
11803 : :
11804 : 2326 : omp_mapping_group *to_group = *to_group_p;
11805 : :
11806 : 3548 : for (; to_group; to_group = to_group->sibling)
11807 : : {
11808 : 2363 : tree grp_end = to_group->grp_end;
11809 : 2363 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11810 : : {
11811 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11812 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11813 : : break;
11814 : :
11815 : 1143 : default:
11816 : 1143 : if (allow_deleted || !to_group->deleted)
11817 : : return to_group;
11818 : : }
11819 : : }
11820 : :
11821 : : return NULL;
11822 : : }
11823 : :
11824 : : /* Return TRUE if the directive (whose clauses are described by the hash table
11825 : : of mapping groups, GRPMAP) maps DECL explicitly. If TO_SPECIFICALLY is
11826 : : true, only count TO mappings. If ALLOW_DELETED is true, ignore the
11827 : : "deleted" flag for groups. If CONTAINED_IN_STRUCT is true, also return
11828 : : TRUE if DECL is mapped as a member of a whole-struct mapping. */
11829 : :
11830 : : static bool
11831 : 3881 : omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
11832 : : omp_mapping_group *> *grpmap,
11833 : : tree decl, omp_mapping_group **base_group,
11834 : : bool to_specifically, bool allow_deleted,
11835 : : bool contained_in_struct)
11836 : : {
11837 : 3881 : omp_mapping_group *decl_group
11838 : 3881 : = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
11839 : :
11840 : 3881 : *base_group = NULL;
11841 : :
11842 : 3881 : if (decl_group)
11843 : : {
11844 : 1024 : tree grp_first = *decl_group->grp_start;
11845 : : /* We might be called during omp_build_struct_sibling_lists, when
11846 : : GOMP_MAP_STRUCT might have been inserted at the start of the group.
11847 : : Skip over that, and also possibly the node after it. */
11848 : 1024 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
11849 : 1024 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
11850 : : {
11851 : 6 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11852 : 6 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
11853 : 6 : || (OMP_CLAUSE_MAP_KIND (grp_first)
11854 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11855 : 12 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
11856 : 0 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11857 : : }
11858 : 1024 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11859 : 1024 : if (!to_specifically
11860 : 574 : || GOMP_MAP_COPY_TO_P (first_kind)
11861 : 365 : || first_kind == GOMP_MAP_ALLOC)
11862 : : {
11863 : 959 : *base_group = decl_group;
11864 : 959 : return true;
11865 : : }
11866 : : }
11867 : :
11868 : 2922 : if (contained_in_struct
11869 : 2922 : && omp_mapped_by_containing_struct (grpmap, decl, base_group))
11870 : : return true;
11871 : :
11872 : : return false;
11873 : : }
11874 : :
11875 : : /* If we have mappings INNER and OUTER, where INNER is a component access and
11876 : : OUTER is a mapping of the whole containing struct, check that the mappings
11877 : : are compatible. We'll be deleting the inner mapping, so we need to make
11878 : : sure the outer mapping does (at least) the same transfers to/from the device
11879 : : as the inner mapping. */
11880 : :
11881 : : bool
11882 : 172 : omp_check_mapping_compatibility (location_t loc,
11883 : : omp_mapping_group *outer,
11884 : : omp_mapping_group *inner)
11885 : : {
11886 : 172 : tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
11887 : :
11888 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
11889 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
11890 : :
11891 : 172 : enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
11892 : 172 : enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
11893 : :
11894 : 172 : if (outer_kind == inner_kind)
11895 : : return true;
11896 : :
11897 : 70 : switch (outer_kind)
11898 : : {
11899 : 0 : case GOMP_MAP_ALWAYS_TO:
11900 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11901 : 0 : || inner_kind == GOMP_MAP_ALLOC
11902 : 0 : || inner_kind == GOMP_MAP_TO)
11903 : : return true;
11904 : : break;
11905 : :
11906 : 0 : case GOMP_MAP_ALWAYS_FROM:
11907 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11908 : 0 : || inner_kind == GOMP_MAP_RELEASE
11909 : : || inner_kind == GOMP_MAP_FROM)
11910 : : return true;
11911 : : break;
11912 : :
11913 : 10 : case GOMP_MAP_TO:
11914 : 10 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11915 : 10 : || inner_kind == GOMP_MAP_ALLOC)
11916 : : return true;
11917 : : break;
11918 : :
11919 : 8 : case GOMP_MAP_FROM:
11920 : 8 : if (inner_kind == GOMP_MAP_RELEASE
11921 : 8 : || inner_kind == GOMP_MAP_FORCE_PRESENT)
11922 : : return true;
11923 : : break;
11924 : :
11925 : 32 : case GOMP_MAP_ALWAYS_TOFROM:
11926 : 32 : case GOMP_MAP_TOFROM:
11927 : 32 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11928 : 32 : || inner_kind == GOMP_MAP_ALLOC
11929 : : || inner_kind == GOMP_MAP_TO
11930 : 24 : || inner_kind == GOMP_MAP_FROM
11931 : 12 : || inner_kind == GOMP_MAP_TOFROM)
11932 : : return true;
11933 : : break;
11934 : :
11935 : 28 : default:
11936 : 28 : ;
11937 : : }
11938 : :
11939 : 84 : error_at (loc, "data movement for component %qE is not compatible with "
11940 : 28 : "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
11941 : 28 : OMP_CLAUSE_DECL (first_outer));
11942 : :
11943 : 28 : return false;
11944 : : }
11945 : :
11946 : : /* This function handles several cases where clauses on a mapping directive
11947 : : can interact with each other.
11948 : :
11949 : : If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
11950 : : on the same directive, change the mapping of the first node to
11951 : : ATTACH_DETACH. We should have detected that this will happen already in
11952 : : c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
11953 : : as addressable. (If we didn't, bail out.)
11954 : :
11955 : : If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
11956 : : mapping the base pointer also, we may need to change the mapping type to
11957 : : ATTACH_DETACH and synthesize an alloc node for the reference itself.
11958 : :
11959 : : If we have an ATTACH_DETACH node, this is an array section with a pointer
11960 : : base. If we're mapping the base on the same directive too, we can drop its
11961 : : mapping. However, if we have a reference to pointer, make other appropriate
11962 : : adjustments to the mapping nodes instead.
11963 : :
11964 : : If we have an ATTACH_DETACH node with a Fortran pointer-set (array
11965 : : descriptor) mapping for a derived-type component, and we're also mapping the
11966 : : whole of the derived-type variable on another clause, the pointer-set
11967 : : mapping is removed.
11968 : :
11969 : : If we have a component access but we're also mapping the whole of the
11970 : : containing struct, drop the former access.
11971 : :
11972 : : If the expression is a component access, and we're also mapping a base
11973 : : pointer used in that component access in the same expression, change the
11974 : : mapping type of the latter to ALLOC (ready for processing by
11975 : : omp_build_struct_sibling_lists). */
11976 : :
11977 : : void
11978 : 7701 : omp_resolve_clause_dependencies (enum tree_code code,
11979 : : vec<omp_mapping_group> *groups,
11980 : : hash_map<tree_operand_hash_no_se,
11981 : : omp_mapping_group *> *grpmap)
11982 : : {
11983 : 7701 : int i;
11984 : 7701 : omp_mapping_group *grp;
11985 : 7701 : bool repair_chain = false;
11986 : :
11987 : 21920 : FOR_EACH_VEC_ELT (*groups, i, grp)
11988 : : {
11989 : 14219 : tree grp_end = grp->grp_end;
11990 : 14219 : tree decl = OMP_CLAUSE_DECL (grp_end);
11991 : :
11992 : 14219 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11993 : :
11994 : 14219 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11995 : : {
11996 : 1111 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11997 : 1111 : {
11998 : 1111 : omp_mapping_group *to_group
11999 : 1111 : = omp_get_nonfirstprivate_group (grpmap, decl);
12000 : :
12001 : 1111 : if (!to_group || to_group == grp)
12002 : 1086 : continue;
12003 : :
12004 : 25 : tree grp_first = *to_group->grp_start;
12005 : 25 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12006 : :
12007 : 25 : if ((GOMP_MAP_COPY_TO_P (first_kind)
12008 : 7 : || first_kind == GOMP_MAP_ALLOC)
12009 : 50 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12010 : : != GOMP_MAP_FIRSTPRIVATE_POINTER))
12011 : : {
12012 : 25 : gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
12013 : 25 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12014 : : }
12015 : : }
12016 : : break;
12017 : :
12018 : 156 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
12019 : 156 : {
12020 : 156 : tree ptr = build_fold_indirect_ref (decl);
12021 : :
12022 : 156 : omp_mapping_group *to_group
12023 : 156 : = omp_get_nonfirstprivate_group (grpmap, ptr);
12024 : :
12025 : 156 : if (!to_group || to_group == grp)
12026 : 152 : continue;
12027 : :
12028 : 4 : tree grp_first = *to_group->grp_start;
12029 : 4 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12030 : :
12031 : 4 : if (GOMP_MAP_COPY_TO_P (first_kind)
12032 : 4 : || first_kind == GOMP_MAP_ALLOC)
12033 : : {
12034 : 4 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12035 : 4 : OMP_CLAUSE_DECL (grp_end) = ptr;
12036 : 4 : if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
12037 : 4 : == to_group->grp_end)
12038 : 4 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12039 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
12040 : : {
12041 : 0 : gcc_assert (TREE_ADDRESSABLE
12042 : : (OMP_CLAUSE_DECL (to_group->grp_end)));
12043 : 0 : OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
12044 : : GOMP_MAP_ATTACH_DETACH);
12045 : :
12046 : 0 : location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
12047 : 0 : tree alloc
12048 : 0 : = build_omp_clause (loc, OMP_CLAUSE_MAP);
12049 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
12050 : 0 : tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
12051 : : (to_group->grp_end));
12052 : 0 : tree char_ptr_type = build_pointer_type (char_type_node);
12053 : 0 : OMP_CLAUSE_DECL (alloc)
12054 : 0 : = build2 (MEM_REF, char_type_node,
12055 : : tmp,
12056 : : build_int_cst (char_ptr_type, 0));
12057 : 0 : OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
12058 : :
12059 : 0 : OMP_CLAUSE_CHAIN (alloc)
12060 : 0 : = OMP_CLAUSE_CHAIN (*to_group->grp_start);
12061 : 0 : OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
12062 : : }
12063 : : }
12064 : : }
12065 : : break;
12066 : :
12067 : : case GOMP_MAP_ATTACH_DETACH:
12068 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
12069 : : {
12070 : 2064 : tree base_ptr, referenced_ptr_node = NULL_TREE;
12071 : :
12072 : 2064 : while (TREE_CODE (decl) == ARRAY_REF)
12073 : 0 : decl = TREE_OPERAND (decl, 0);
12074 : :
12075 : 2064 : if (TREE_CODE (decl) == INDIRECT_REF)
12076 : 13 : decl = TREE_OPERAND (decl, 0);
12077 : :
12078 : : /* Only component accesses. */
12079 : 2064 : if (DECL_P (decl))
12080 : 217 : continue;
12081 : :
12082 : : /* We want the pointer itself when checking if the base pointer is
12083 : : mapped elsewhere in the same directive -- if we have a
12084 : : reference to the pointer, don't use that. */
12085 : :
12086 : 1847 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
12087 : 1847 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
12088 : : {
12089 : 394 : referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
12090 : 394 : base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
12091 : : }
12092 : : else
12093 : : base_ptr = decl;
12094 : :
12095 : 1669 : gomp_map_kind zlas_kind
12096 : 1847 : = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
12097 : 1847 : ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
12098 : :
12099 : 1847 : if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
12100 : : {
12101 : : /* If we map the base TO, and we're doing an attachment, we can
12102 : : skip the TO mapping altogether and create an ALLOC mapping
12103 : : instead, since the attachment will overwrite the device
12104 : : pointer in that location immediately anyway. Otherwise,
12105 : : change our mapping to
12106 : : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
12107 : : attachment target has not been copied to the device already
12108 : : by some earlier directive. */
12109 : :
12110 : 1423 : bool base_mapped_to = false;
12111 : :
12112 : 1423 : omp_mapping_group *base_group;
12113 : :
12114 : 1423 : if (omp_directive_maps_explicitly (grpmap, base_ptr,
12115 : : &base_group, false, true,
12116 : : false))
12117 : : {
12118 : 450 : if (referenced_ptr_node)
12119 : : {
12120 : 129 : base_mapped_to = true;
12121 : 129 : if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
12122 : : == GOMP_MAP_ATTACH_DETACH)
12123 : 129 : && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
12124 : : == base_group->grp_end))
12125 : : {
12126 : 258 : OMP_CLAUSE_CHAIN (*base_group->grp_start)
12127 : 129 : = OMP_CLAUSE_CHAIN (base_group->grp_end);
12128 : 129 : base_group->grp_end = *base_group->grp_start;
12129 : 129 : repair_chain = true;
12130 : : }
12131 : : }
12132 : : else
12133 : : {
12134 : 321 : base_group->deleted = true;
12135 : 321 : OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
12136 : : }
12137 : : }
12138 : :
12139 : : /* We're dealing with a reference to a pointer, and we are
12140 : : attaching both the reference and the pointer. We know the
12141 : : reference itself is on the target, because we are going to
12142 : : create an ALLOC node for it in accumulate_sibling_list. The
12143 : : pointer might be on the target already or it might not, but
12144 : : if it isn't then it's not an error, so use
12145 : : GOMP_MAP_ATTACH_ZLAS for it. */
12146 : 1423 : if (!base_mapped_to && referenced_ptr_node)
12147 : 56 : OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
12148 : :
12149 : 1423 : omp_mapping_group *struct_group;
12150 : 1423 : tree desc;
12151 : 1423 : if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
12152 : 1423 : && omp_map_clause_descriptor_p (desc)
12153 : 2017 : && omp_mapped_by_containing_struct (grpmap, decl,
12154 : : &struct_group))
12155 : : /* If we have a pointer set but we're mapping (or unmapping)
12156 : : the whole of the containing struct, we can remove the
12157 : : pointer set mapping. */
12158 : 15 : OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
12159 : : }
12160 : 424 : else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
12161 : 424 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
12162 : : == ARRAY_TYPE)
12163 : 562 : && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
12164 : : (*grp->grp_start))
12165 : 92 : OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
12166 : : }
12167 : : break;
12168 : :
12169 : : case GOMP_MAP_ATTACH:
12170 : : /* Ignore standalone attach here. */
12171 : : break;
12172 : :
12173 : 10825 : default:
12174 : 10825 : {
12175 : 10825 : omp_mapping_group *struct_group;
12176 : 10825 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12177 : 10825 : && *grp->grp_start == grp_end)
12178 : : {
12179 : 94 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12180 : : struct_group, grp);
12181 : : /* Remove the whole of this mapping -- redundant. */
12182 : 94 : grp->deleted = true;
12183 : : }
12184 : :
12185 : : tree base = decl;
12186 : 12417 : while ((base = omp_get_base_pointer (base)))
12187 : : {
12188 : 1592 : omp_mapping_group *base_group;
12189 : :
12190 : 1592 : if (omp_directive_maps_explicitly (grpmap, base, &base_group,
12191 : : true, true, false))
12192 : : {
12193 : 342 : tree grp_first = *base_group->grp_start;
12194 : 342 : OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
12195 : : }
12196 : : }
12197 : : }
12198 : : }
12199 : : }
12200 : :
12201 : 7701 : if (repair_chain)
12202 : : {
12203 : : /* Group start pointers may have become detached from the
12204 : : OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
12205 : : end of those groups. Fix that now. */
12206 : : tree *new_next = NULL;
12207 : 716 : FOR_EACH_VEC_ELT (*groups, i, grp)
12208 : : {
12209 : 587 : if (new_next)
12210 : 458 : grp->grp_start = new_next;
12211 : :
12212 : 587 : new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
12213 : : }
12214 : : }
12215 : 7701 : }
12216 : :
12217 : : /* Similar to omp_resolve_clause_dependencies, but for OpenACC. The only
12218 : : clause dependencies we handle for now are struct element mappings and
12219 : : whole-struct mappings on the same directive, and duplicate clause
12220 : : detection. */
12221 : :
12222 : : void
12223 : 9453 : oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
12224 : : hash_map<tree_operand_hash_no_se,
12225 : : omp_mapping_group *> *grpmap)
12226 : : {
12227 : 9453 : int i;
12228 : 9453 : omp_mapping_group *grp;
12229 : 9453 : hash_set<tree_operand_hash> *seen_components = NULL;
12230 : 9453 : hash_set<tree_operand_hash> *shown_error = NULL;
12231 : :
12232 : 24622 : FOR_EACH_VEC_ELT (*groups, i, grp)
12233 : : {
12234 : 15169 : tree grp_end = grp->grp_end;
12235 : 15169 : tree decl = OMP_CLAUSE_DECL (grp_end);
12236 : :
12237 : 15169 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
12238 : :
12239 : 15169 : if (DECL_P (grp_end))
12240 : 14647 : continue;
12241 : :
12242 : 15169 : tree c = OMP_CLAUSE_DECL (*grp->grp_start);
12243 : 16794 : while (TREE_CODE (c) == ARRAY_REF)
12244 : 1625 : c = TREE_OPERAND (c, 0);
12245 : 15169 : if (TREE_CODE (c) != COMPONENT_REF)
12246 : 14647 : continue;
12247 : 522 : if (!seen_components)
12248 : 479 : seen_components = new hash_set<tree_operand_hash> ();
12249 : 522 : if (!shown_error)
12250 : 479 : shown_error = new hash_set<tree_operand_hash> ();
12251 : 522 : if (seen_components->contains (c)
12252 : 522 : && !shown_error->contains (c))
12253 : : {
12254 : 10 : error_at (OMP_CLAUSE_LOCATION (grp_end),
12255 : : "%qE appears more than once in map clauses",
12256 : 5 : OMP_CLAUSE_DECL (grp_end));
12257 : 5 : shown_error->add (c);
12258 : : }
12259 : : else
12260 : 517 : seen_components->add (c);
12261 : :
12262 : 522 : omp_mapping_group *struct_group;
12263 : 522 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12264 : 522 : && *grp->grp_start == grp_end)
12265 : : {
12266 : 78 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12267 : : struct_group, grp);
12268 : : /* Remove the whole of this mapping -- redundant. */
12269 : 78 : grp->deleted = true;
12270 : : }
12271 : : }
12272 : :
12273 : 9453 : if (seen_components)
12274 : 479 : delete seen_components;
12275 : 9453 : if (shown_error)
12276 : 479 : delete shown_error;
12277 : 9453 : }
12278 : :
12279 : : /* Link node NEWNODE so it is pointed to by chain INSERT_AT. NEWNODE's chain
12280 : : is linked to the previous node pointed to by INSERT_AT. */
12281 : :
12282 : : static tree *
12283 : 1076 : omp_siblist_insert_node_after (tree newnode, tree *insert_at)
12284 : : {
12285 : 1076 : OMP_CLAUSE_CHAIN (newnode) = *insert_at;
12286 : 1076 : *insert_at = newnode;
12287 : 1076 : return &OMP_CLAUSE_CHAIN (newnode);
12288 : : }
12289 : :
12290 : : /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
12291 : : pointed to by chain MOVE_AFTER instead. */
12292 : :
12293 : : static void
12294 : 863 : omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
12295 : : {
12296 : 863 : gcc_assert (node == *old_pos);
12297 : 863 : *old_pos = OMP_CLAUSE_CHAIN (node);
12298 : 863 : OMP_CLAUSE_CHAIN (node) = *move_after;
12299 : 863 : *move_after = node;
12300 : 863 : }
12301 : :
12302 : : /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
12303 : : LAST_NODE to after MOVE_AFTER chain. Similar to below function, but no
12304 : : new nodes are prepended to the list before splicing into the new position.
12305 : : Return the position we should continue scanning the list at, or NULL to
12306 : : stay where we were. */
12307 : :
12308 : : static tree *
12309 : 254 : omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
12310 : : tree *move_after)
12311 : : {
12312 : 254 : if (first_ptr == move_after)
12313 : : return NULL;
12314 : :
12315 : 243 : tree tmp = *first_ptr;
12316 : 243 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12317 : 243 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12318 : 243 : *move_after = tmp;
12319 : :
12320 : 243 : return first_ptr;
12321 : : }
12322 : :
12323 : : /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
12324 : : [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
12325 : : pointer MOVE_AFTER.
12326 : :
12327 : : The latter list was previously part of the OMP clause list, and the former
12328 : : (prepended) part is comprised of new nodes.
12329 : :
12330 : : We start with a list of nodes starting with a struct mapping node. We
12331 : : rearrange the list so that new nodes starting from FIRST_NEW and whose last
12332 : : node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
12333 : : the group of mapping nodes we are currently processing (from the chain
12334 : : FIRST_PTR to LAST_NODE). The return value is the pointer to the next chain
12335 : : we should continue processing from, or NULL to stay where we were.
12336 : :
12337 : : The transformation (in the case where MOVE_AFTER and FIRST_PTR are
12338 : : different) is worked through below. Here we are processing LAST_NODE, and
12339 : : FIRST_PTR points at the preceding mapping clause:
12340 : :
12341 : : #. mapping node chain
12342 : : ---------------------------------------------------
12343 : : A. struct_node [->B]
12344 : : B. comp_1 [->C]
12345 : : C. comp_2 [->D (move_after)]
12346 : : D. map_to_3 [->E]
12347 : : E. attach_3 [->F (first_ptr)]
12348 : : F. map_to_4 [->G (continue_at)]
12349 : : G. attach_4 (last_node) [->H]
12350 : : H. ...
12351 : :
12352 : : *last_new_tail = *first_ptr;
12353 : :
12354 : : I. new_node (first_new) [->F (last_new_tail)]
12355 : :
12356 : : *first_ptr = OMP_CLAUSE_CHAIN (last_node)
12357 : :
12358 : : #. mapping node chain
12359 : : ----------------------------------------------------
12360 : : A. struct_node [->B]
12361 : : B. comp_1 [->C]
12362 : : C. comp_2 [->D (move_after)]
12363 : : D. map_to_3 [->E]
12364 : : E. attach_3 [->H (first_ptr)]
12365 : : F. map_to_4 [->G (continue_at)]
12366 : : G. attach_4 (last_node) [->H]
12367 : : H. ...
12368 : :
12369 : : I. new_node (first_new) [->F (last_new_tail)]
12370 : :
12371 : : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12372 : :
12373 : : #. mapping node chain
12374 : : ---------------------------------------------------
12375 : : A. struct_node [->B]
12376 : : B. comp_1 [->C]
12377 : : C. comp_2 [->D (move_after)]
12378 : : D. map_to_3 [->E]
12379 : : E. attach_3 [->H (continue_at)]
12380 : : F. map_to_4 [->G]
12381 : : G. attach_4 (last_node) [->D]
12382 : : H. ...
12383 : :
12384 : : I. new_node (first_new) [->F (last_new_tail)]
12385 : :
12386 : : *move_after = first_new;
12387 : :
12388 : : #. mapping node chain
12389 : : ---------------------------------------------------
12390 : : A. struct_node [->B]
12391 : : B. comp_1 [->C]
12392 : : C. comp_2 [->I (move_after)]
12393 : : D. map_to_3 [->E]
12394 : : E. attach_3 [->H (continue_at)]
12395 : : F. map_to_4 [->G]
12396 : : G. attach_4 (last_node) [->D]
12397 : : H. ...
12398 : : I. new_node (first_new) [->F (last_new_tail)]
12399 : :
12400 : : or, in order:
12401 : :
12402 : : #. mapping node chain
12403 : : ---------------------------------------------------
12404 : : A. struct_node [->B]
12405 : : B. comp_1 [->C]
12406 : : C. comp_2 [->I (move_after)]
12407 : : I. new_node (first_new) [->F (last_new_tail)]
12408 : : F. map_to_4 [->G]
12409 : : G. attach_4 (last_node) [->D]
12410 : : D. map_to_3 [->E]
12411 : : E. attach_3 [->H (continue_at)]
12412 : : H. ...
12413 : : */
12414 : :
12415 : : static tree *
12416 : 71 : omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
12417 : : tree *first_ptr, tree last_node,
12418 : : tree *move_after)
12419 : : {
12420 : 71 : tree *continue_at = NULL;
12421 : 71 : *last_new_tail = *first_ptr;
12422 : 71 : if (first_ptr == move_after)
12423 : 12 : *move_after = first_new;
12424 : : else
12425 : : {
12426 : 59 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12427 : 59 : continue_at = first_ptr;
12428 : 59 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12429 : 59 : *move_after = first_new;
12430 : : }
12431 : 71 : return continue_at;
12432 : : }
12433 : :
12434 : : static omp_addr_token *
12435 : 10057 : omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
12436 : : {
12437 : 10057 : using namespace omp_addr_tokenizer;
12438 : 10057 : int idx = addr_tokens.length () - 1;
12439 : 10057 : gcc_assert (idx >= 0);
12440 : 10057 : if (addr_tokens[idx]->type != ACCESS_METHOD)
12441 : : return addr_tokens[idx];
12442 : 10084 : while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
12443 : : idx--;
12444 : 10057 : return addr_tokens[idx];
12445 : : }
12446 : :
12447 : : /* Mapping struct members causes an additional set of nodes to be created,
12448 : : starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
12449 : : number of members being mapped, in order of ascending position (address or
12450 : : bitwise).
12451 : :
12452 : : We scan through the list of mapping clauses, calling this function for each
12453 : : struct member mapping we find, and build up the list of mappings after the
12454 : : initial GOMP_MAP_STRUCT node. For pointer members, these will be
12455 : : newly-created ALLOC nodes. For non-pointer members, the existing mapping is
12456 : : moved into place in the sorted list.
12457 : :
12458 : : struct {
12459 : : int *a;
12460 : : int *b;
12461 : : int c;
12462 : : int *d;
12463 : : };
12464 : :
12465 : : #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
12466 : : struct.d[0:n])
12467 : :
12468 : : GOMP_MAP_STRUCT (4)
12469 : : [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
12470 : : GOMP_MAP_ALLOC (struct.a)
12471 : : GOMP_MAP_ALLOC (struct.b)
12472 : : GOMP_MAP_TO (struct.c)
12473 : : GOMP_MAP_ALLOC (struct.d)
12474 : : ...
12475 : :
12476 : : In the case where we are mapping references to pointers, or in Fortran if
12477 : : we are mapping an array with a descriptor, additional nodes may be created
12478 : : after the struct node list also.
12479 : :
12480 : : The return code is either a pointer to the next node to process (if the
12481 : : list has been rearranged), else NULL to continue with the next node in the
12482 : : original list. */
12483 : :
12484 : : static tree *
12485 : 4050 : omp_accumulate_sibling_list (enum omp_region_type region_type,
12486 : : enum tree_code code,
12487 : : hash_map<tree_operand_hash, tree>
12488 : : *&struct_map_to_clause,
12489 : : hash_map<tree_operand_hash_no_se,
12490 : : omp_mapping_group *> *group_map,
12491 : : tree *grp_start_p, tree grp_end,
12492 : : vec<omp_addr_token *> &addr_tokens, tree **inner,
12493 : : bool *fragile_p, bool reprocessing_struct,
12494 : : tree **added_tail)
12495 : : {
12496 : 4050 : using namespace omp_addr_tokenizer;
12497 : 4050 : poly_offset_int coffset;
12498 : 4050 : poly_int64 cbitpos;
12499 : 4050 : tree ocd = OMP_CLAUSE_DECL (grp_end);
12500 : 4050 : bool openmp = !(region_type & ORT_ACC);
12501 : 4050 : bool target = (region_type & ORT_TARGET) != 0;
12502 : 4050 : tree *continue_at = NULL;
12503 : :
12504 : 4363 : while (TREE_CODE (ocd) == ARRAY_REF)
12505 : 313 : ocd = TREE_OPERAND (ocd, 0);
12506 : :
12507 : 4050 : if (*fragile_p)
12508 : : {
12509 : 156 : omp_mapping_group *to_group
12510 : 156 : = omp_get_nonfirstprivate_group (group_map, ocd, true);
12511 : :
12512 : 156 : if (to_group)
12513 : : return NULL;
12514 : : }
12515 : :
12516 : 3962 : omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
12517 : 3962 : if (last_token->type == ACCESS_METHOD)
12518 : : {
12519 : 3962 : switch (last_token->u.access_kind)
12520 : : {
12521 : 735 : case ACCESS_REF:
12522 : 735 : case ACCESS_REF_TO_POINTER:
12523 : 735 : case ACCESS_REF_TO_POINTER_OFFSET:
12524 : 735 : case ACCESS_INDEXED_REF_TO_ARRAY:
12525 : : /* We may see either a bare reference or a dereferenced
12526 : : "convert_from_reference"-like one here. Handle either way. */
12527 : 735 : if (TREE_CODE (ocd) == INDIRECT_REF)
12528 : 64 : ocd = TREE_OPERAND (ocd, 0);
12529 : 735 : gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
12530 : : break;
12531 : :
12532 : : default:
12533 : : ;
12534 : : }
12535 : : }
12536 : :
12537 : 3962 : bool variable_offset;
12538 : 3962 : tree base
12539 : 3962 : = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset);
12540 : :
12541 : 3962 : int base_token;
12542 : 19982 : for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
12543 : : {
12544 : 16020 : if (addr_tokens[base_token]->type == ARRAY_BASE
12545 : 16020 : || addr_tokens[base_token]->type == STRUCTURE_BASE)
12546 : : break;
12547 : : }
12548 : :
12549 : : /* The two expressions in the assertion below aren't quite the same: if we
12550 : : have 'struct_base_decl access_indexed_array' for something like
12551 : : "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
12552 : : will be "myvar[2]" -- the actual base of the structure.
12553 : : The former interpretation leads to a strange situation where we get
12554 : : struct(myvar) alloc(myvar[2].ptr1)
12555 : : That is, the array of structures is kind of treated as one big structure
12556 : : for the purposes of gathering sibling lists, etc. */
12557 : : /* gcc_assert (base == addr_tokens[base_token]->expr); */
12558 : :
12559 : 3962 : bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
12560 : : == GOMP_MAP_ATTACH_DETACH)
12561 : 3962 : || (OMP_CLAUSE_MAP_KIND (grp_end)
12562 : 3962 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
12563 : 3962 : bool has_descriptor = false;
12564 : 3962 : if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12565 : : {
12566 : 2421 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
12567 : 2421 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
12568 : : has_descriptor = true;
12569 : : }
12570 : :
12571 : 3962 : if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
12572 : : {
12573 : 2705 : enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
12574 : :
12575 : 2705 : if (struct_map_to_clause == NULL)
12576 : 2277 : struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
12577 : :
12578 : 2705 : if (variable_offset)
12579 : 108 : str_kind = GOMP_MAP_STRUCT_UNORD;
12580 : :
12581 : 2705 : tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
12582 : :
12583 : 2705 : OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
12584 : 2705 : OMP_CLAUSE_DECL (l) = unshare_expr (base);
12585 : 2705 : OMP_CLAUSE_SIZE (l) = size_int (1);
12586 : :
12587 : 2705 : struct_map_to_clause->put (base, l);
12588 : :
12589 : : /* On first iterating through the clause list, we insert the struct node
12590 : : just before the component access node that triggers the initial
12591 : : omp_accumulate_sibling_list call for a particular sibling list (and
12592 : : it then forms the first entry in that list). When reprocessing
12593 : : struct bases that are themselves component accesses, we insert the
12594 : : struct node on an off-side list to avoid inserting the new
12595 : : GOMP_MAP_STRUCT into the middle of the old one. */
12596 : 2705 : tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
12597 : :
12598 : 2705 : if (has_descriptor)
12599 : : {
12600 : 474 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12601 : 474 : if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
12602 : 97 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12603 : 474 : tree sc = *insert_node_pos;
12604 : 474 : OMP_CLAUSE_CHAIN (l) = desc;
12605 : 474 : OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
12606 : 474 : OMP_CLAUSE_CHAIN (desc) = sc;
12607 : 474 : *insert_node_pos = l;
12608 : : }
12609 : 2231 : else if (attach_detach)
12610 : : {
12611 : 1385 : tree extra_node;
12612 : 1385 : tree alloc_node
12613 : 1385 : = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
12614 : : &extra_node);
12615 : 1385 : tree *tail;
12616 : 1385 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12617 : :
12618 : 1385 : if (extra_node)
12619 : : {
12620 : 0 : OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
12621 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12622 : 0 : tail = &OMP_CLAUSE_CHAIN (extra_node);
12623 : : }
12624 : : else
12625 : : {
12626 : 1385 : OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
12627 : 1385 : tail = &OMP_CLAUSE_CHAIN (alloc_node);
12628 : : }
12629 : :
12630 : : /* For OpenMP semantics, we don't want to implicitly allocate
12631 : : space for the pointer here for non-compute regions (e.g. "enter
12632 : : data"). A FRAGILE_P node is only being created so that
12633 : : omp-low.cc is able to rewrite the struct properly.
12634 : : For references (to pointers), we want to actually allocate the
12635 : : space for the reference itself in the sorted list following the
12636 : : struct node.
12637 : : For pointers, we want to allocate space if we had an explicit
12638 : : mapping of the attachment point, but not otherwise. */
12639 : 1385 : if (*fragile_p
12640 : 1385 : || (openmp
12641 : : && !target
12642 : : && attach_detach
12643 : 222 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12644 : 76 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12645 : : {
12646 : 121 : if (!lang_GNU_Fortran ())
12647 : : /* In Fortran, pointers are dereferenced automatically, but may
12648 : : be unassociated. So we still want to allocate space for the
12649 : : pointer (as the base for an attach operation that should be
12650 : : present in the same directive's clause list also). */
12651 : 103 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12652 : 121 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12653 : : }
12654 : :
12655 : 1385 : *insert_node_pos = l;
12656 : :
12657 : 1385 : if (reprocessing_struct)
12658 : : {
12659 : : /* When reprocessing a struct node group used as the base of a
12660 : : subcomponent access, if we have a reference-to-pointer base,
12661 : : we will see:
12662 : : struct(**ptr) attach(*ptr)
12663 : : whereas for a non-reprocess-struct group, we see, e.g.:
12664 : : tofrom(**ptr) attach(*ptr) attach(ptr)
12665 : : and we create the "alloc" for the second "attach", i.e.
12666 : : for the reference itself. When reprocessing a struct group we
12667 : : thus change the pointer attachment into a reference attachment
12668 : : by stripping the indirection. (The attachment of the
12669 : : referenced pointer must happen elsewhere, either on the same
12670 : : directive, or otherwise.) */
12671 : 180 : tree adecl = OMP_CLAUSE_DECL (alloc_node);
12672 : :
12673 : 180 : if ((TREE_CODE (adecl) == INDIRECT_REF
12674 : 148 : || (TREE_CODE (adecl) == MEM_REF
12675 : 0 : && integer_zerop (TREE_OPERAND (adecl, 1))))
12676 : 32 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
12677 : : == REFERENCE_TYPE)
12678 : 212 : && (TREE_CODE (TREE_TYPE (TREE_TYPE
12679 : : (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
12680 : 32 : OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
12681 : :
12682 : 180 : *added_tail = tail;
12683 : : }
12684 : : }
12685 : : else
12686 : : {
12687 : 846 : gcc_assert (*grp_start_p == grp_end);
12688 : 846 : if (reprocessing_struct)
12689 : : {
12690 : : /* If we don't have an attach/detach node, this is a
12691 : : "target data" directive or similar, not an offload region.
12692 : : Synthesize an "alloc" node using just the initiating
12693 : : GOMP_MAP_STRUCT decl. */
12694 : 16 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12695 : 32 : || code == OACC_EXIT_DATA)
12696 : 32 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12697 : 32 : tree alloc_node
12698 : 32 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12699 : : OMP_CLAUSE_MAP);
12700 : 32 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12701 : 32 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12702 : 64 : OMP_CLAUSE_SIZE (alloc_node)
12703 : 32 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12704 : :
12705 : 32 : OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
12706 : 32 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12707 : 32 : *insert_node_pos = l;
12708 : 32 : *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
12709 : : }
12710 : : else
12711 : 814 : grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
12712 : : }
12713 : :
12714 : 2705 : unsigned last_access = base_token + 1;
12715 : :
12716 : 2705 : while (last_access + 1 < addr_tokens.length ()
12717 : 2873 : && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
12718 : : last_access++;
12719 : :
12720 : 2705 : if ((region_type & ORT_TARGET)
12721 : 2705 : && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
12722 : : {
12723 : 1652 : bool base_ref = false;
12724 : 1652 : access_method_kinds access_kind
12725 : 1652 : = addr_tokens[last_access]->u.access_kind;
12726 : :
12727 : 1652 : switch (access_kind)
12728 : : {
12729 : : case ACCESS_DIRECT:
12730 : : case ACCESS_INDEXED_ARRAY:
12731 : 1004 : return NULL;
12732 : :
12733 : 403 : case ACCESS_REF:
12734 : 403 : case ACCESS_REF_TO_POINTER:
12735 : 403 : case ACCESS_REF_TO_POINTER_OFFSET:
12736 : 403 : case ACCESS_INDEXED_REF_TO_ARRAY:
12737 : 403 : base_ref = true;
12738 : 403 : break;
12739 : :
12740 : 866 : default:
12741 : 866 : ;
12742 : : }
12743 : 866 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12744 : : OMP_CLAUSE_MAP);
12745 : 866 : enum gomp_map_kind mkind;
12746 : 866 : omp_mapping_group *decl_group;
12747 : 866 : tree use_base;
12748 : 866 : switch (access_kind)
12749 : : {
12750 : 463 : case ACCESS_POINTER:
12751 : 463 : case ACCESS_POINTER_OFFSET:
12752 : 463 : use_base = addr_tokens[last_access]->expr;
12753 : 463 : break;
12754 : 198 : case ACCESS_REF_TO_POINTER:
12755 : 198 : case ACCESS_REF_TO_POINTER_OFFSET:
12756 : 198 : use_base
12757 : 198 : = build_fold_indirect_ref (addr_tokens[last_access]->expr);
12758 : 198 : break;
12759 : 205 : default:
12760 : 205 : use_base = addr_tokens[base_token]->expr;
12761 : : }
12762 : 866 : bool mapped_to_p
12763 : 866 : = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
12764 : : true, false, true);
12765 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12766 : 866 : && DECL_P (addr_tokens[last_access]->expr)
12767 : 1372 : && !mapped_to_p)
12768 : 444 : mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
12769 : : : GOMP_MAP_FIRSTPRIVATE_POINTER;
12770 : : else
12771 : : mkind = GOMP_MAP_ATTACH_DETACH;
12772 : :
12773 : 866 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
12774 : : /* If we have a reference to pointer base, we want to attach the
12775 : : pointer here, not the reference. The reference attachment happens
12776 : : elsewhere. */
12777 : 866 : bool ref_to_ptr
12778 : 866 : = (access_kind == ACCESS_REF_TO_POINTER
12779 : 866 : || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
12780 : 866 : tree sdecl = addr_tokens[last_access]->expr;
12781 : 866 : tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
12782 : : : sdecl;
12783 : : /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
12784 : : want to use the reference itself for the decl, but we
12785 : : still want to use the pointer to calculate the bias. */
12786 : 866 : OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
12787 : 866 : ? sdecl_ptr : sdecl;
12788 : 866 : sdecl = sdecl_ptr;
12789 : 866 : tree baddr = build_fold_addr_expr (base);
12790 : 866 : baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12791 : : ptrdiff_type_node, baddr);
12792 : 866 : tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12793 : : ptrdiff_type_node, sdecl);
12794 : 866 : OMP_CLAUSE_SIZE (c2)
12795 : 866 : = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
12796 : : ptrdiff_type_node, baddr, decladdr);
12797 : : /* Insert after struct node. */
12798 : 866 : OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
12799 : 866 : OMP_CLAUSE_CHAIN (l) = c2;
12800 : :
12801 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12802 : 866 : && (addr_tokens[base_token]->u.structure_base_kind
12803 : : == BASE_COMPONENT_EXPR)
12804 : 286 : && mkind == GOMP_MAP_ATTACH_DETACH
12805 : 1152 : && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
12806 : : {
12807 : 218 : *inner = insert_node_pos;
12808 : 218 : if (openmp)
12809 : 166 : *fragile_p = true;
12810 : 218 : return NULL;
12811 : : }
12812 : : }
12813 : :
12814 : 1701 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12815 : 1701 : && (addr_tokens[base_token]->u.structure_base_kind
12816 : : == BASE_COMPONENT_EXPR)
12817 : 2016 : && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
12818 : 100 : *inner = insert_node_pos;
12819 : :
12820 : 1701 : return NULL;
12821 : : }
12822 : 1257 : else if (struct_map_to_clause)
12823 : : {
12824 : 1257 : tree *osc = struct_map_to_clause->get (base);
12825 : 1257 : tree *sc = NULL, *scp = NULL;
12826 : 1257 : bool unordered = false;
12827 : :
12828 : 1257 : if (osc && OMP_CLAUSE_MAP_KIND (*osc) == GOMP_MAP_STRUCT_UNORD)
12829 : : unordered = true;
12830 : :
12831 : 1257 : unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
12832 : 1257 : sc = &OMP_CLAUSE_CHAIN (*osc);
12833 : : /* The struct mapping might be immediately followed by a
12834 : : FIRSTPRIVATE_POINTER, FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
12835 : : if it's an indirect access or a reference, or if the structure base
12836 : : is not a decl. The FIRSTPRIVATE_* nodes are removed in omp-low.cc
12837 : : after they have been processed there, and ATTACH_DETACH nodes are
12838 : : recomputed and moved out of the GOMP_MAP_STRUCT construct once
12839 : : sibling list building is complete. */
12840 : 1257 : if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
12841 : 1212 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
12842 : 2424 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
12843 : 201 : sc = &OMP_CLAUSE_CHAIN (*sc);
12844 : 3453 : for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
12845 : 2580 : if (attach_detach && sc == grp_start_p)
12846 : : break;
12847 : 2580 : else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
12848 : 182 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
12849 : 2762 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
12850 : : break;
12851 : : else
12852 : : {
12853 : 2580 : tree sc_decl = OMP_CLAUSE_DECL (*sc);
12854 : 2580 : poly_offset_int offset;
12855 : 2580 : poly_int64 bitpos;
12856 : :
12857 : 2580 : if (TREE_CODE (sc_decl) == ARRAY_REF)
12858 : : {
12859 : 366 : while (TREE_CODE (sc_decl) == ARRAY_REF)
12860 : 184 : sc_decl = TREE_OPERAND (sc_decl, 0);
12861 : 182 : if (TREE_CODE (sc_decl) != COMPONENT_REF
12862 : 182 : || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
12863 : : break;
12864 : : }
12865 : 2398 : else if (INDIRECT_REF_P (sc_decl)
12866 : 0 : && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
12867 : 2398 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
12868 : : == REFERENCE_TYPE))
12869 : 0 : sc_decl = TREE_OPERAND (sc_decl, 0);
12870 : :
12871 : 2580 : bool variable_offset2;
12872 : 2580 : tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
12873 : : &variable_offset2);
12874 : 2580 : if (!base2 || !operand_equal_p (base2, base, 0))
12875 : : break;
12876 : 2580 : if (scp)
12877 : 220 : continue;
12878 : 2360 : if (variable_offset2)
12879 : : {
12880 : 48 : OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
12881 : 48 : unordered = true;
12882 : 48 : break;
12883 : : }
12884 : 2312 : else if ((region_type & ORT_ACC) != 0)
12885 : : {
12886 : : /* For OpenACC, allow (ignore) duplicate struct accesses in
12887 : : the middle of a mapping clause, e.g. "mystruct->foo" in:
12888 : : copy(mystruct->foo->bar) copy(mystruct->foo->qux). */
12889 : 223 : if (reprocessing_struct
12890 : 8 : && known_eq (coffset, offset)
12891 : 223 : && known_eq (cbitpos, bitpos))
12892 : 21 : return NULL;
12893 : : }
12894 : 2089 : else if (known_eq (coffset, offset)
12895 : 2089 : && known_eq (cbitpos, bitpos))
12896 : : {
12897 : : /* Having two struct members at the same offset doesn't work,
12898 : : so make sure we don't. (We're allowed to ignore this.
12899 : : Should we report the error?) */
12900 : : /*error_at (OMP_CLAUSE_LOCATION (grp_end),
12901 : : "duplicate struct member %qE in map clauses",
12902 : : OMP_CLAUSE_DECL (grp_end));*/
12903 : : return NULL;
12904 : : }
12905 : 2291 : if (maybe_lt (coffset, offset)
12906 : 4001 : || (known_eq (coffset, offset)
12907 : 2 : && maybe_lt (cbitpos, bitpos)))
12908 : : {
12909 : 581 : if (attach_detach)
12910 : : scp = sc;
12911 : : else
12912 : : break;
12913 : : }
12914 : : }
12915 : :
12916 : : /* If this is an unordered struct, just insert the new element at the
12917 : : end of the list. */
12918 : 1236 : if (unordered)
12919 : : {
12920 : 102 : for (; i < elems; i++)
12921 : 54 : sc = &OMP_CLAUSE_CHAIN (*sc);
12922 : : scp = NULL;
12923 : : }
12924 : :
12925 : 1236 : OMP_CLAUSE_SIZE (*osc)
12926 : 1236 : = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
12927 : :
12928 : 1236 : if (reprocessing_struct)
12929 : : {
12930 : : /* If we're reprocessing a struct node, we don't want to do most of
12931 : : the list manipulation below. We only need to handle the (pointer
12932 : : or reference) attach/detach case. */
12933 : 8 : tree extra_node, alloc_node;
12934 : 8 : if (has_descriptor)
12935 : 0 : gcc_unreachable ();
12936 : 8 : else if (attach_detach)
12937 : 8 : alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12938 : : grp_end, &extra_node);
12939 : : else
12940 : : {
12941 : : /* If we don't have an attach/detach node, this is a
12942 : : "target data" directive or similar, not an offload region.
12943 : : Synthesize an "alloc" node using just the initiating
12944 : : GOMP_MAP_STRUCT decl. */
12945 : 0 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12946 : 0 : || code == OACC_EXIT_DATA)
12947 : 0 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12948 : 0 : alloc_node
12949 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12950 : : OMP_CLAUSE_MAP);
12951 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12952 : 0 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12953 : 0 : OMP_CLAUSE_SIZE (alloc_node)
12954 : 0 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12955 : : }
12956 : :
12957 : 8 : if (scp)
12958 : 0 : omp_siblist_insert_node_after (alloc_node, scp);
12959 : : else
12960 : : {
12961 : 8 : tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
12962 : 8 : if (sc == *added_tail)
12963 : 8 : *added_tail = new_end;
12964 : : }
12965 : :
12966 : 8 : return NULL;
12967 : : }
12968 : :
12969 : 1228 : if (has_descriptor)
12970 : : {
12971 : 339 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12972 : 339 : if (code == OMP_TARGET_EXIT_DATA
12973 : 339 : || code == OACC_EXIT_DATA)
12974 : 19 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12975 : 678 : omp_siblist_move_node_after (desc,
12976 : 339 : &OMP_CLAUSE_CHAIN (*grp_start_p),
12977 : : scp ? scp : sc);
12978 : : }
12979 : 889 : else if (attach_detach)
12980 : : {
12981 : 325 : tree cl = NULL_TREE, extra_node;
12982 : 325 : tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12983 : : grp_end, &extra_node);
12984 : 325 : tree *tail_chain = NULL;
12985 : :
12986 : 325 : if (*fragile_p
12987 : 325 : || (openmp
12988 : : && !target
12989 : : && attach_detach
12990 : 62 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12991 : 23 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12992 : : {
12993 : 6 : if (!lang_GNU_Fortran ())
12994 : 6 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12995 : 6 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12996 : : }
12997 : :
12998 : : /* Here, we have:
12999 : :
13000 : : grp_end : the last (or only) node in this group.
13001 : : grp_start_p : pointer to the first node in a pointer mapping group
13002 : : up to and including GRP_END.
13003 : : sc : pointer to the chain for the end of the struct component
13004 : : list.
13005 : : scp : pointer to the chain for the sorted position at which we
13006 : : should insert in the middle of the struct component list
13007 : : (else NULL to insert at end).
13008 : : alloc_node : the "alloc" node for the structure (pointer-type)
13009 : : component. We insert at SCP (if present), else SC
13010 : : (the end of the struct component list).
13011 : : extra_node : a newly-synthesized node for an additional indirect
13012 : : pointer mapping or a Fortran pointer set, if needed.
13013 : : cl : first node to prepend before grp_start_p.
13014 : : tail_chain : pointer to chain of last prepended node.
13015 : :
13016 : : The general idea is we move the nodes for this struct mapping
13017 : : together: the alloc node goes into the sorted list directly after
13018 : : the struct mapping, and any extra nodes (together with the nodes
13019 : : mapping arrays pointed to by struct components) get moved after
13020 : : that list. When SCP is NULL, we insert the nodes at SC, i.e. at
13021 : : the end of the struct component mapping list. It's important that
13022 : : the alloc_node comes first in that case because it's part of the
13023 : : sorted component mapping list (but subsequent nodes are not!). */
13024 : :
13025 : 325 : if (scp)
13026 : 254 : omp_siblist_insert_node_after (alloc_node, scp);
13027 : :
13028 : : /* Make [cl,tail_chain] a list of the alloc node (if we haven't
13029 : : already inserted it) and the extra_node (if it is present). The
13030 : : list can be empty if we added alloc_node above and there is no
13031 : : extra node. */
13032 : 254 : if (scp && extra_node)
13033 : : {
13034 : 0 : cl = extra_node;
13035 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13036 : : }
13037 : 325 : else if (extra_node)
13038 : : {
13039 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
13040 : 0 : cl = alloc_node;
13041 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13042 : : }
13043 : 325 : else if (!scp)
13044 : : {
13045 : 71 : cl = alloc_node;
13046 : 71 : tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
13047 : : }
13048 : :
13049 : 325 : continue_at
13050 : 71 : = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
13051 : : grp_start_p, grp_end,
13052 : : sc)
13053 : 254 : : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
13054 : : }
13055 : 564 : else if (*sc != grp_end)
13056 : : {
13057 : 524 : gcc_assert (*grp_start_p == grp_end);
13058 : :
13059 : : /* We are moving the current node back to a previous struct node:
13060 : : the node that used to point to the current node will now point to
13061 : : the next node. */
13062 : 524 : continue_at = grp_start_p;
13063 : : /* In the non-pointer case, the mapping clause itself is moved into
13064 : : the correct position in the struct component list, which in this
13065 : : case is just SC. */
13066 : 524 : omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
13067 : : }
13068 : : }
13069 : : return continue_at;
13070 : : }
13071 : :
13072 : : /* Scan through GROUPS, and create sorted structure sibling lists without
13073 : : gimplifying. */
13074 : :
13075 : : static bool
13076 : 17154 : omp_build_struct_sibling_lists (enum tree_code code,
13077 : : enum omp_region_type region_type,
13078 : : vec<omp_mapping_group> *groups,
13079 : : hash_map<tree_operand_hash_no_se,
13080 : : omp_mapping_group *> **grpmap,
13081 : : tree *list_p)
13082 : : {
13083 : 17154 : using namespace omp_addr_tokenizer;
13084 : 17154 : unsigned i;
13085 : 17154 : omp_mapping_group *grp;
13086 : 17154 : hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
13087 : 17154 : bool success = true;
13088 : 17154 : tree *new_next = NULL;
13089 : 34308 : tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
13090 : 17154 : tree added_nodes = NULL_TREE;
13091 : 17154 : tree *added_tail = &added_nodes;
13092 : 17154 : auto_vec<omp_mapping_group> pre_hwm_groups;
13093 : :
13094 : 64014 : FOR_EACH_VEC_ELT (*groups, i, grp)
13095 : : {
13096 : 29706 : tree c = grp->grp_end;
13097 : 29706 : tree decl = OMP_CLAUSE_DECL (c);
13098 : 29706 : tree grp_end = grp->grp_end;
13099 : 29706 : auto_vec<omp_addr_token *> addr_tokens;
13100 : 29706 : tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
13101 : :
13102 : 29706 : if (new_next && !grp->reprocess_struct)
13103 : 510 : grp->grp_start = new_next;
13104 : :
13105 : 29706 : new_next = NULL;
13106 : :
13107 : 29706 : tree *grp_start_p = grp->grp_start;
13108 : :
13109 : 29706 : if (DECL_P (decl))
13110 : 20377 : continue;
13111 : :
13112 : : /* Skip groups we marked for deletion in
13113 : : {omp,oacc}_resolve_clause_dependencies. */
13114 : 9329 : if (grp->deleted)
13115 : 462 : continue;
13116 : :
13117 : 8867 : if (OMP_CLAUSE_CHAIN (*grp_start_p)
13118 : 8867 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
13119 : : {
13120 : : /* Don't process an array descriptor that isn't inside a derived type
13121 : : as a struct (the GOMP_MAP_POINTER following will have the form
13122 : : "var.data", but such mappings are handled specially). */
13123 : 5407 : tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
13124 : 5407 : if (omp_map_clause_descriptor_p (grpmid)
13125 : 9039 : && DECL_P (OMP_CLAUSE_DECL (grpmid)))
13126 : 2772 : continue;
13127 : : }
13128 : :
13129 : : tree expr = decl;
13130 : :
13131 : 7122 : while (TREE_CODE (expr) == ARRAY_REF)
13132 : 1027 : expr = TREE_OPERAND (expr, 0);
13133 : :
13134 : 6095 : if (!omp_parse_expr (addr_tokens, expr))
13135 : 0 : continue;
13136 : :
13137 : 6095 : omp_addr_token *last_token
13138 : 6095 : = omp_first_chained_access_token (addr_tokens);
13139 : :
13140 : : /* A mapping of a reference to a pointer member that doesn't specify an
13141 : : array section, etc., like this:
13142 : : *mystruct.ref_to_ptr
13143 : : should not be processed by the struct sibling-list handling code --
13144 : : it just transfers the referenced pointer.
13145 : :
13146 : : In contrast, the quite similar-looking construct:
13147 : : *mystruct.ptr
13148 : : which is equivalent to e.g.
13149 : : mystruct.ptr[0]
13150 : : *does* trigger sibling-list processing.
13151 : :
13152 : : An exception for the former case is for "fragile" groups where the
13153 : : reference itself is not handled otherwise; this is subject to special
13154 : : handling in omp_accumulate_sibling_list also. */
13155 : :
13156 : 6095 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
13157 : 2853 : && last_token->type == ACCESS_METHOD
13158 : 2853 : && last_token->u.access_kind == ACCESS_REF
13159 : 6379 : && !grp->fragile)
13160 : 219 : continue;
13161 : :
13162 : 5876 : tree d = decl;
13163 : 5876 : if (TREE_CODE (d) == ARRAY_REF)
13164 : : {
13165 : 1998 : while (TREE_CODE (d) == ARRAY_REF)
13166 : 1010 : d = TREE_OPERAND (d, 0);
13167 : 988 : if (TREE_CODE (d) == COMPONENT_REF
13168 : 988 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
13169 : : decl = d;
13170 : : }
13171 : 5876 : if (d == decl
13172 : 5199 : && INDIRECT_REF_P (decl)
13173 : 717 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
13174 : 105 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
13175 : : == REFERENCE_TYPE)
13176 : 5973 : && (OMP_CLAUSE_MAP_KIND (c)
13177 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
13178 : 97 : decl = TREE_OPERAND (decl, 0);
13179 : :
13180 : 5876 : STRIP_NOPS (decl);
13181 : :
13182 : 5876 : if (TREE_CODE (decl) != COMPONENT_REF)
13183 : 1339 : continue;
13184 : :
13185 : : /* If we're mapping the whole struct in another node, skip adding this
13186 : : node to a sibling list. */
13187 : 4537 : omp_mapping_group *wholestruct;
13188 : 4537 : if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
13189 : : &wholestruct))
13190 : 125 : continue;
13191 : :
13192 : 4412 : if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
13193 : 4412 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
13194 : 4308 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
13195 : : && code != OACC_UPDATE
13196 : 8653 : && code != OMP_TARGET_UPDATE)
13197 : : {
13198 : 4050 : if (error_operand_p (decl))
13199 : : {
13200 : 0 : success = false;
13201 : 0 : goto error_out;
13202 : : }
13203 : :
13204 : 4050 : tree stype = TREE_TYPE (decl);
13205 : 4050 : if (TREE_CODE (stype) == REFERENCE_TYPE)
13206 : 768 : stype = TREE_TYPE (stype);
13207 : 4050 : if (TYPE_SIZE_UNIT (stype) == NULL
13208 : 4050 : || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
13209 : : {
13210 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
13211 : : "mapping field %qE of variable length "
13212 : 0 : "structure", OMP_CLAUSE_DECL (c));
13213 : 0 : success = false;
13214 : 0 : goto error_out;
13215 : : }
13216 : :
13217 : 4050 : tree *inner = NULL;
13218 : 4050 : bool fragile_p = grp->fragile;
13219 : :
13220 : 4050 : new_next
13221 : 8100 : = omp_accumulate_sibling_list (region_type, code,
13222 : : struct_map_to_clause, *grpmap,
13223 : : grp_start_p, grp_end, addr_tokens,
13224 : : &inner, &fragile_p,
13225 : 4050 : grp->reprocess_struct, &added_tail);
13226 : :
13227 : 4050 : if (inner)
13228 : : {
13229 : 318 : omp_mapping_group newgrp;
13230 : 318 : newgrp.grp_start = inner;
13231 : 318 : if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
13232 : : == GOMP_MAP_ATTACH_DETACH)
13233 : 286 : newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
13234 : : else
13235 : 32 : newgrp.grp_end = *inner;
13236 : 318 : newgrp.mark = UNVISITED;
13237 : 318 : newgrp.sibling = NULL;
13238 : 318 : newgrp.deleted = false;
13239 : 318 : newgrp.reprocess_struct = true;
13240 : 318 : newgrp.fragile = fragile_p;
13241 : 318 : newgrp.next = NULL;
13242 : 318 : groups->safe_push (newgrp);
13243 : :
13244 : : /* !!! Growing GROUPS might invalidate the pointers in the group
13245 : : map. Rebuild it here. This is a bit inefficient, but
13246 : : shouldn't happen very often. */
13247 : 636 : delete (*grpmap);
13248 : 318 : *grpmap
13249 : 318 : = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
13250 : : sentinel);
13251 : : }
13252 : : }
13253 : 29706 : }
13254 : :
13255 : : /* Delete groups marked for deletion above. At this point the order of the
13256 : : groups may no longer correspond to the order of the underlying list,
13257 : : which complicates this a little. First clear out OMP_CLAUSE_DECL for
13258 : : deleted nodes... */
13259 : :
13260 : 46860 : FOR_EACH_VEC_ELT (*groups, i, grp)
13261 : 29706 : if (grp->deleted)
13262 : 462 : for (tree d = *grp->grp_start;
13263 : 924 : d != OMP_CLAUSE_CHAIN (grp->grp_end);
13264 : 462 : d = OMP_CLAUSE_CHAIN (d))
13265 : 462 : OMP_CLAUSE_DECL (d) = NULL_TREE;
13266 : :
13267 : : /* ...then sweep through the list removing the now-empty nodes. */
13268 : :
13269 : : tail = list_p;
13270 : 90766 : while (*tail)
13271 : : {
13272 : 73612 : if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
13273 : 73612 : && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
13274 : 462 : *tail = OMP_CLAUSE_CHAIN (*tail);
13275 : : else
13276 : 73150 : tail = &OMP_CLAUSE_CHAIN (*tail);
13277 : : }
13278 : :
13279 : : /* Tack on the struct nodes added during nested struct reprocessing. */
13280 : 17154 : if (added_nodes)
13281 : : {
13282 : 192 : *tail = added_nodes;
13283 : 192 : tail = added_tail;
13284 : : }
13285 : :
13286 : : /* Now we have finished building the struct sibling lists, reprocess
13287 : : newly-added "attach" nodes: we need the address of the first
13288 : : mapped element of each struct sibling list for the bias of the attach
13289 : : operation -- not necessarily the base address of the whole struct. */
13290 : 17154 : if (struct_map_to_clause)
13291 : 4982 : for (hash_map<tree_operand_hash, tree>::iterator iter
13292 : 2277 : = struct_map_to_clause->begin ();
13293 : 7259 : iter != struct_map_to_clause->end ();
13294 : 2705 : ++iter)
13295 : : {
13296 : 2705 : tree struct_node = (*iter).second;
13297 : 2705 : gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
13298 : 2705 : tree attach = OMP_CLAUSE_CHAIN (struct_node);
13299 : :
13300 : 2705 : if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
13301 : 2705 : || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
13302 : 2283 : continue;
13303 : :
13304 : 422 : OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
13305 : :
13306 : : /* Sanity check: the standalone attach node will not work if we have
13307 : : an "enter data" operation (because for those, variables need to be
13308 : : mapped separately and attach nodes must be grouped together with the
13309 : : base they attach to). We should only have created the
13310 : : ATTACH_DETACH node after GOMP_MAP_STRUCT for a target region, so
13311 : : this should never be true. */
13312 : 422 : gcc_assert ((region_type & ORT_TARGET) != 0);
13313 : :
13314 : : /* This is the first sorted node in the struct sibling list. Use it
13315 : : to recalculate the correct bias to use.
13316 : : (&first_node - attach_decl).
13317 : : For GOMP_MAP_STRUCT_UNORD, we need e.g. the
13318 : : min(min(min(first,second),third),fourth) element, because the
13319 : : elements aren't in any particular order. */
13320 : 422 : tree lowest_addr;
13321 : 422 : if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
13322 : : {
13323 : 4 : tree first_node = OMP_CLAUSE_CHAIN (attach);
13324 : 4 : unsigned HOST_WIDE_INT num_mappings
13325 : 4 : = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
13326 : 4 : lowest_addr = OMP_CLAUSE_DECL (first_node);
13327 : 4 : lowest_addr = build_fold_addr_expr (lowest_addr);
13328 : 4 : lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
13329 : 4 : tree next_node = OMP_CLAUSE_CHAIN (first_node);
13330 : 8 : while (num_mappings > 1)
13331 : : {
13332 : 4 : tree tmp = OMP_CLAUSE_DECL (next_node);
13333 : 4 : tmp = build_fold_addr_expr (tmp);
13334 : 4 : tmp = fold_convert (pointer_sized_int_node, tmp);
13335 : 4 : lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
13336 : : lowest_addr, tmp);
13337 : 4 : next_node = OMP_CLAUSE_CHAIN (next_node);
13338 : 4 : num_mappings--;
13339 : : }
13340 : 4 : lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
13341 : : }
13342 : : else
13343 : : {
13344 : 418 : tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
13345 : 418 : first_node = build_fold_addr_expr (first_node);
13346 : 418 : lowest_addr = fold_convert (ptrdiff_type_node, first_node);
13347 : : }
13348 : 422 : tree attach_decl = OMP_CLAUSE_DECL (attach);
13349 : 422 : attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
13350 : 422 : OMP_CLAUSE_SIZE (attach)
13351 : 422 : = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
13352 : : attach_decl);
13353 : :
13354 : : /* Remove GOMP_MAP_ATTACH node from after struct node. */
13355 : 422 : OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
13356 : : /* ...and re-insert it at the end of our clause list. */
13357 : 422 : *tail = attach;
13358 : 422 : OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
13359 : 422 : tail = &OMP_CLAUSE_CHAIN (attach);
13360 : : }
13361 : :
13362 : 14877 : error_out:
13363 : 17154 : if (struct_map_to_clause)
13364 : 2277 : delete struct_map_to_clause;
13365 : :
13366 : 17154 : return success;
13367 : 17154 : }
13368 : :
13369 : : struct instantiate_mapper_info
13370 : : {
13371 : : tree *mapper_clauses_p;
13372 : : struct gimplify_omp_ctx *omp_ctx;
13373 : : gimple_seq *pre_p;
13374 : : };
13375 : :
13376 : : /* Helper function for omp_instantiate_mapper. */
13377 : :
13378 : : static tree
13379 : 1272 : remap_mapper_decl_1 (tree *tp, int *walk_subtrees, void *data)
13380 : : {
13381 : 1272 : copy_body_data *id = (copy_body_data *) data;
13382 : :
13383 : 1272 : if (DECL_P (*tp))
13384 : : {
13385 : 564 : tree replacement = remap_decl (*tp, id);
13386 : 564 : if (*tp != replacement)
13387 : : {
13388 : 289 : *tp = unshare_expr (replacement);
13389 : 289 : *walk_subtrees = 0;
13390 : : }
13391 : : }
13392 : :
13393 : 1272 : return NULL_TREE;
13394 : : }
13395 : :
13396 : : /* A copy_decl implementation (for use with tree-inline.cc functions) that
13397 : : only transform decls or SSA names that are part of a map we already
13398 : : prepared. */
13399 : :
13400 : : static tree
13401 : 94 : omp_mapper_copy_decl (tree var, copy_body_data *cb)
13402 : : {
13403 : 94 : tree *repl = cb->decl_map->get (var);
13404 : :
13405 : 94 : if (repl)
13406 : 0 : return *repl;
13407 : :
13408 : 94 : return var;
13409 : : }
13410 : :
13411 : : static tree *
13412 : 80 : omp_instantiate_mapper (gimple_seq *pre_p,
13413 : : hash_map<omp_name_type<tree>, tree> *implicit_mappers,
13414 : : tree mapperfn, tree expr, enum gomp_map_kind outer_kind,
13415 : : tree *mapper_clauses_p)
13416 : : {
13417 : 80 : tree mapper_name = NULL_TREE;
13418 : 80 : tree mapper = lang_hooks.decls.omp_extract_mapper_directive (mapperfn);
13419 : 80 : gcc_assert (TREE_CODE (mapper) == OMP_DECLARE_MAPPER);
13420 : :
13421 : 80 : tree clause = OMP_DECLARE_MAPPER_CLAUSES (mapper);
13422 : 80 : tree dummy_var = OMP_DECLARE_MAPPER_DECL (mapper);
13423 : :
13424 : : /* The "extraction map" is used to map the mapper variable in the "declare
13425 : : mapper" directive, and also any temporary variables that have been created
13426 : : as part of expanding the mapper function's body (which are expanded as a
13427 : : "bind" expression in the pre_p sequence). */
13428 : 80 : hash_map<tree, tree> extraction_map;
13429 : :
13430 : 80 : extraction_map.put (dummy_var, expr);
13431 : 80 : extraction_map.put (expr, expr);
13432 : :
13433 : : /* This copy_body_data is only used to remap the decls in the
13434 : : OMP_DECLARE_MAPPER tree node expansion itself. All relevant decls should
13435 : : already be in the current function. */
13436 : 80 : copy_body_data id;
13437 : 80 : memset (&id, 0, sizeof (id));
13438 : 80 : id.src_fn = current_function_decl;
13439 : 80 : id.dst_fn = current_function_decl;
13440 : 80 : id.src_cfun = cfun;
13441 : 80 : id.decl_map = &extraction_map;
13442 : 80 : id.copy_decl = omp_mapper_copy_decl;
13443 : 80 : id.transform_call_graph_edges = CB_CGE_DUPLICATE; // ???
13444 : 80 : id.transform_new_cfg = true; // ???
13445 : :
13446 : 238 : for (; clause; clause = OMP_CLAUSE_CHAIN (clause))
13447 : : {
13448 : 158 : enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (clause);
13449 : 158 : tree *nested_mapper_p = NULL;
13450 : :
13451 : 158 : if (map_kind == GOMP_MAP_PUSH_MAPPER_NAME)
13452 : : {
13453 : 0 : mapper_name = OMP_CLAUSE_DECL (clause);
13454 : 23 : continue;
13455 : : }
13456 : 158 : else if (map_kind == GOMP_MAP_POP_MAPPER_NAME)
13457 : : {
13458 : 0 : mapper_name = NULL_TREE;
13459 : 0 : continue;
13460 : : }
13461 : :
13462 : 158 : tree decl = OMP_CLAUSE_DECL (clause);
13463 : 158 : tree unshared, type;
13464 : 158 : bool nonunit_array_with_mapper = false;
13465 : :
13466 : 158 : if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
13467 : : {
13468 : 62 : location_t loc = OMP_CLAUSE_LOCATION (clause);
13469 : 62 : tree tmp = lang_hooks.decls.omp_map_array_section (loc, decl);
13470 : 62 : if (tmp == decl)
13471 : : {
13472 : 48 : unshared = unshare_expr (clause);
13473 : 48 : nonunit_array_with_mapper = true;
13474 : 48 : type = TREE_TYPE (TREE_TYPE (decl));
13475 : : }
13476 : : else
13477 : : {
13478 : 14 : unshared = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
13479 : 14 : OMP_CLAUSE_CODE (clause));
13480 : 14 : OMP_CLAUSE_DECL (unshared) = tmp;
13481 : 14 : OMP_CLAUSE_SIZE (unshared)
13482 : 28 : = DECL_P (tmp) ? DECL_SIZE_UNIT (tmp)
13483 : 14 : : TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13484 : 14 : type = TREE_TYPE (tmp);
13485 : : }
13486 : : }
13487 : : else
13488 : : {
13489 : 96 : unshared = unshare_expr (clause);
13490 : 96 : type = TREE_TYPE (decl);
13491 : : }
13492 : :
13493 : 158 : walk_tree (&unshared, remap_mapper_decl_1, &id, NULL);
13494 : :
13495 : 158 : if (OMP_CLAUSE_MAP_KIND (unshared) == GOMP_MAP_UNSET)
13496 : 28 : OMP_CLAUSE_SET_MAP_KIND (unshared, outer_kind);
13497 : :
13498 : 158 : decl = OMP_CLAUSE_DECL (unshared);
13499 : 158 : type = TYPE_MAIN_VARIANT (type);
13500 : :
13501 : 158 : nested_mapper_p = implicit_mappers->get ({ mapper_name, type });
13502 : :
13503 : 158 : if (nested_mapper_p && *nested_mapper_p != mapperfn)
13504 : : {
13505 : 23 : if (nonunit_array_with_mapper)
13506 : : {
13507 : 8 : sorry ("user-defined mapper with non-unit length array section");
13508 : 8 : continue;
13509 : : }
13510 : :
13511 : 15 : if (map_kind == GOMP_MAP_UNSET)
13512 : 0 : map_kind = outer_kind;
13513 : :
13514 : 15 : mapper_clauses_p
13515 : 15 : = omp_instantiate_mapper (pre_p, implicit_mappers,
13516 : : *nested_mapper_p, decl, map_kind,
13517 : : mapper_clauses_p);
13518 : 15 : continue;
13519 : : }
13520 : :
13521 : 135 : *mapper_clauses_p = unshared;
13522 : 135 : mapper_clauses_p = &OMP_CLAUSE_CHAIN (unshared);
13523 : : }
13524 : :
13525 : 80 : return mapper_clauses_p;
13526 : 80 : }
13527 : :
13528 : : static int
13529 : 124892 : omp_instantiate_implicit_mappers (splay_tree_node n, void *data)
13530 : : {
13531 : 124892 : tree decl = (tree) n->key;
13532 : 124892 : instantiate_mapper_info *im_info = (instantiate_mapper_info *) data;
13533 : 124892 : gimplify_omp_ctx *ctx = im_info->omp_ctx;
13534 : 124892 : tree *mapper_p = NULL;
13535 : 124892 : tree type = TREE_TYPE (decl);
13536 : 124892 : bool ref_p = false;
13537 : 124892 : unsigned flags = n->value;
13538 : :
13539 : 124892 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
13540 : : return 0;
13541 : 23004 : if ((flags & GOVD_SEEN) == 0)
13542 : : return 0;
13543 : : /* If we already have clauses pertaining to a struct variable, then we don't
13544 : : want to implicitly invoke a user-defined mapper. */
13545 : 19636 : if ((flags & GOVD_EXPLICIT) != 0 && AGGREGATE_TYPE_P (TREE_TYPE (decl)))
13546 : : return 0;
13547 : :
13548 : 19636 : if (TREE_CODE (type) == REFERENCE_TYPE)
13549 : : {
13550 : 896 : ref_p = true;
13551 : 896 : type = TREE_TYPE (type);
13552 : : }
13553 : :
13554 : 19636 : type = TYPE_MAIN_VARIANT (type);
13555 : :
13556 : 19636 : if (DECL_P (decl) && type && AGGREGATE_TYPE_P (type))
13557 : : {
13558 : 5264 : gcc_assert (ctx);
13559 : 5264 : mapper_p = ctx->implicit_mappers->get ({ NULL_TREE, type });
13560 : : }
13561 : :
13562 : 5264 : if (mapper_p)
13563 : : {
13564 : : /* If we have a reference, map the pointed-to object rather than the
13565 : : reference itself. */
13566 : 65 : if (ref_p)
13567 : 2 : decl = build_fold_indirect_ref (decl);
13568 : :
13569 : 65 : im_info->mapper_clauses_p
13570 : 65 : = omp_instantiate_mapper (im_info->pre_p, ctx->implicit_mappers,
13571 : : *mapper_p, decl, GOMP_MAP_TOFROM,
13572 : : im_info->mapper_clauses_p);
13573 : : /* Make sure we don't map the same variable implicitly in
13574 : : gimplify_adjust_omp_clauses_1 also. */
13575 : 65 : n->value |= GOVD_EXPLICIT;
13576 : : }
13577 : :
13578 : : return 0;
13579 : : }
13580 : :
13581 : : /* Scan the OMP clauses in *LIST_P, installing mappings into a new
13582 : : and previous omp contexts. */
13583 : :
13584 : : static void
13585 : 129470 : gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
13586 : : enum omp_region_type region_type,
13587 : : enum tree_code code,
13588 : : gimple_seq *loops_seq_p = NULL)
13589 : : {
13590 : 129470 : using namespace omp_addr_tokenizer;
13591 : 129470 : struct gimplify_omp_ctx *ctx, *outer_ctx;
13592 : 129470 : tree c;
13593 : 129470 : tree *orig_list_p = list_p;
13594 : 129470 : int handled_depend_iterators = -1;
13595 : 129470 : int nowait = -1;
13596 : :
13597 : 129470 : ctx = new_omp_context (region_type);
13598 : 129470 : ctx->code = code;
13599 : 129470 : outer_ctx = ctx->outer_context;
13600 : 129470 : if (code == OMP_TARGET)
13601 : : {
13602 : 12589 : if (!lang_GNU_Fortran ())
13603 : 10602 : ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
13604 : 12589 : ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
13605 : 25178 : ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
13606 : 12589 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
13607 : : }
13608 : 129470 : if (!lang_GNU_Fortran ())
13609 : 98937 : switch (code)
13610 : : {
13611 : 17958 : case OMP_TARGET:
13612 : 17958 : case OMP_TARGET_DATA:
13613 : 17958 : case OMP_TARGET_ENTER_DATA:
13614 : 17958 : case OMP_TARGET_EXIT_DATA:
13615 : 17958 : case OACC_DECLARE:
13616 : 17958 : case OACC_HOST_DATA:
13617 : 17958 : case OACC_PARALLEL:
13618 : 17958 : case OACC_KERNELS:
13619 : 17958 : ctx->target_firstprivatize_array_bases = true;
13620 : : default:
13621 : : break;
13622 : : }
13623 : :
13624 : 129470 : vec<omp_mapping_group> *groups = NULL;
13625 : 129470 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
13626 : 129470 : unsigned grpnum = 0;
13627 : 129470 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
13628 : :
13629 : 129470 : if (code == OMP_TARGET
13630 : 129470 : || code == OMP_TARGET_DATA
13631 : 129470 : || code == OMP_TARGET_ENTER_DATA
13632 : : || code == OMP_TARGET_EXIT_DATA
13633 : : || code == OACC_DATA
13634 : : || code == OACC_KERNELS
13635 : : || code == OACC_PARALLEL
13636 : : || code == OACC_SERIAL
13637 : : || code == OACC_ENTER_DATA
13638 : : || code == OACC_EXIT_DATA
13639 : : || code == OACC_UPDATE
13640 : : || code == OACC_DECLARE)
13641 : : {
13642 : 32651 : groups = omp_gather_mapping_groups (list_p);
13643 : :
13644 : 32651 : if (groups)
13645 : 17363 : grpmap = omp_index_mapping_groups (groups);
13646 : : }
13647 : :
13648 : 331467 : while ((c = *list_p) != NULL)
13649 : : {
13650 : 201997 : bool remove = false;
13651 : 201997 : bool notice_outer = true;
13652 : 201997 : bool map_descriptor;
13653 : 201997 : const char *check_non_private = NULL;
13654 : 201997 : unsigned int flags;
13655 : 201997 : tree decl;
13656 : 201997 : auto_vec<omp_addr_token *, 10> addr_tokens;
13657 : 201997 : tree op = NULL_TREE;
13658 : 201997 : location_t loc = OMP_CLAUSE_LOCATION (c);
13659 : :
13660 : 237479 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
13661 : : {
13662 : : grp_start_p = NULL;
13663 : : grp_end = NULL_TREE;
13664 : : }
13665 : :
13666 : 201997 : if (code == OMP_TARGET
13667 : : || code == OMP_TARGET_DATA
13668 : : || code == OMP_TARGET_ENTER_DATA
13669 : 171792 : || code == OMP_TARGET_EXIT_DATA)
13670 : : /* Do some target-specific type checks for map operands. */
13671 : 31751 : switch (OMP_CLAUSE_CODE (c))
13672 : : {
13673 : 22841 : case OMP_CLAUSE_MAP:
13674 : 22841 : op = OMP_CLAUSE_OPERAND (c, 0);
13675 : 22841 : verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
13676 : 22841 : break;
13677 : 129 : case OMP_CLAUSE_PRIVATE:
13678 : 129 : op = OMP_CLAUSE_OPERAND (c, 0);
13679 : 129 : verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
13680 : 129 : break;
13681 : 1230 : case OMP_CLAUSE_FIRSTPRIVATE:
13682 : 1230 : op = OMP_CLAUSE_OPERAND (c, 0);
13683 : 1230 : verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
13684 : 1230 : break;
13685 : 2906 : case OMP_CLAUSE_IS_DEVICE_PTR:
13686 : 2906 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13687 : 2906 : case OMP_CLAUSE_USE_DEVICE_PTR:
13688 : 2906 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13689 : 2906 : op = OMP_CLAUSE_OPERAND (c, 0);
13690 : 2906 : verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
13691 : 2906 : break;
13692 : : default:
13693 : : break;
13694 : : }
13695 : :
13696 : 201997 : switch (OMP_CLAUSE_CODE (c))
13697 : : {
13698 : 12141 : case OMP_CLAUSE_PRIVATE:
13699 : 12141 : flags = GOVD_PRIVATE | GOVD_EXPLICIT;
13700 : 12141 : if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
13701 : : {
13702 : 165 : flags |= GOVD_PRIVATE_OUTER_REF;
13703 : 165 : OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
13704 : : }
13705 : : else
13706 : : notice_outer = false;
13707 : 12141 : goto do_add;
13708 : 5510 : case OMP_CLAUSE_SHARED:
13709 : 5510 : flags = GOVD_SHARED | GOVD_EXPLICIT;
13710 : 5510 : goto do_add;
13711 : 7834 : case OMP_CLAUSE_FIRSTPRIVATE:
13712 : 7834 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13713 : 7834 : check_non_private = "firstprivate";
13714 : 7834 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13715 : : {
13716 : 380 : gcc_assert (code == OMP_TARGET);
13717 : : flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
13718 : : }
13719 : 7834 : goto do_add;
13720 : 7325 : case OMP_CLAUSE_LASTPRIVATE:
13721 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13722 : 496 : switch (code)
13723 : : {
13724 : 25 : case OMP_DISTRIBUTE:
13725 : 25 : error_at (OMP_CLAUSE_LOCATION (c),
13726 : : "conditional %<lastprivate%> clause on "
13727 : : "%qs construct", "distribute");
13728 : 25 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13729 : 25 : break;
13730 : 13 : case OMP_TASKLOOP:
13731 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
13732 : : "conditional %<lastprivate%> clause on "
13733 : : "%qs construct", "taskloop");
13734 : 13 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13735 : 13 : break;
13736 : : default:
13737 : : break;
13738 : : }
13739 : 7325 : flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
13740 : 7325 : if (code != OMP_LOOP)
13741 : 6974 : check_non_private = "lastprivate";
13742 : 7325 : decl = OMP_CLAUSE_DECL (c);
13743 : 7325 : if (error_operand_p (decl))
13744 : 0 : goto do_add;
13745 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
13746 : 7325 : && !lang_hooks.decls.omp_scalar_p (decl, true))
13747 : : {
13748 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
13749 : : "non-scalar variable %qD in conditional "
13750 : : "%<lastprivate%> clause", decl);
13751 : 5 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13752 : : }
13753 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13754 : 453 : flags |= GOVD_LASTPRIVATE_CONDITIONAL;
13755 : 7325 : omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
13756 : : false);
13757 : 7325 : goto do_add;
13758 : 15294 : case OMP_CLAUSE_REDUCTION:
13759 : 15294 : if (OMP_CLAUSE_REDUCTION_TASK (c))
13760 : : {
13761 : 594 : if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13762 : : {
13763 : 408 : if (nowait == -1)
13764 : 293 : nowait = omp_find_clause (*list_p,
13765 : 293 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
13766 : 408 : if (nowait
13767 : 15 : && (outer_ctx == NULL
13768 : 0 : || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
13769 : : {
13770 : 15 : error_at (OMP_CLAUSE_LOCATION (c),
13771 : : "%<task%> reduction modifier on a construct "
13772 : : "with a %<nowait%> clause");
13773 : 15 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13774 : : }
13775 : : }
13776 : 186 : else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
13777 : : {
13778 : 40 : error_at (OMP_CLAUSE_LOCATION (c),
13779 : : "invalid %<task%> reduction modifier on construct "
13780 : : "other than %<parallel%>, %qs, %<sections%> or "
13781 : 20 : "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
13782 : 20 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13783 : : }
13784 : : }
13785 : 15294 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
13786 : 831 : switch (code)
13787 : : {
13788 : 4 : case OMP_SECTIONS:
13789 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13790 : : "%<inscan%> %<reduction%> clause on "
13791 : : "%qs construct", "sections");
13792 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13793 : 4 : break;
13794 : 4 : case OMP_PARALLEL:
13795 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13796 : : "%<inscan%> %<reduction%> clause on "
13797 : : "%qs construct", "parallel");
13798 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13799 : 4 : break;
13800 : 4 : case OMP_TEAMS:
13801 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13802 : : "%<inscan%> %<reduction%> clause on "
13803 : : "%qs construct", "teams");
13804 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13805 : 4 : break;
13806 : 4 : case OMP_TASKLOOP:
13807 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13808 : : "%<inscan%> %<reduction%> clause on "
13809 : : "%qs construct", "taskloop");
13810 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13811 : 4 : break;
13812 : 4 : case OMP_SCOPE:
13813 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13814 : : "%<inscan%> %<reduction%> clause on "
13815 : : "%qs construct", "scope");
13816 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13817 : 4 : break;
13818 : : default:
13819 : : break;
13820 : : }
13821 : : /* FALLTHRU */
13822 : 17894 : case OMP_CLAUSE_IN_REDUCTION:
13823 : 17894 : case OMP_CLAUSE_TASK_REDUCTION:
13824 : 17894 : flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
13825 : : /* OpenACC permits reductions on private variables. */
13826 : 17894 : if (!(region_type & ORT_ACC)
13827 : : /* taskgroup is actually not a worksharing region. */
13828 : 12374 : && code != OMP_TASKGROUP)
13829 : 11844 : check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
13830 : 17894 : decl = OMP_CLAUSE_DECL (c);
13831 : 17894 : if (TREE_CODE (decl) == MEM_REF)
13832 : : {
13833 : 2614 : tree type = TREE_TYPE (decl);
13834 : 2614 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
13835 : 2614 : gimplify_ctxp->into_ssa = false;
13836 : 2614 : if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
13837 : : NULL, is_gimple_val, fb_rvalue, false)
13838 : : == GS_ERROR)
13839 : : {
13840 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13841 : 0 : remove = true;
13842 : 0 : break;
13843 : : }
13844 : 2614 : gimplify_ctxp->into_ssa = saved_into_ssa;
13845 : 2614 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13846 : 2614 : if (DECL_P (v))
13847 : : {
13848 : 571 : omp_firstprivatize_variable (ctx, v);
13849 : 571 : omp_notice_variable (ctx, v, true);
13850 : : }
13851 : 2614 : decl = TREE_OPERAND (decl, 0);
13852 : 2614 : if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
13853 : : {
13854 : 477 : gimplify_ctxp->into_ssa = false;
13855 : 477 : if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
13856 : : NULL, is_gimple_val, fb_rvalue, false)
13857 : : == GS_ERROR)
13858 : : {
13859 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13860 : 0 : remove = true;
13861 : 0 : break;
13862 : : }
13863 : 477 : gimplify_ctxp->into_ssa = saved_into_ssa;
13864 : 477 : v = TREE_OPERAND (decl, 1);
13865 : 477 : if (DECL_P (v))
13866 : : {
13867 : 477 : omp_firstprivatize_variable (ctx, v);
13868 : 477 : omp_notice_variable (ctx, v, true);
13869 : : }
13870 : 477 : decl = TREE_OPERAND (decl, 0);
13871 : : }
13872 : 2614 : if (TREE_CODE (decl) == ADDR_EXPR
13873 : 1409 : || TREE_CODE (decl) == INDIRECT_REF)
13874 : 1375 : decl = TREE_OPERAND (decl, 0);
13875 : : }
13876 : 17894 : goto do_add_decl;
13877 : 2668 : case OMP_CLAUSE_LINEAR:
13878 : 2668 : if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
13879 : : is_gimple_val, fb_rvalue) == GS_ERROR)
13880 : : {
13881 : : remove = true;
13882 : : break;
13883 : : }
13884 : : else
13885 : : {
13886 : 2668 : if (code == OMP_SIMD
13887 : 2668 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13888 : : {
13889 : 1154 : struct gimplify_omp_ctx *octx = outer_ctx;
13890 : 1154 : if (octx
13891 : 741 : && octx->region_type == ORT_WORKSHARE
13892 : 500 : && octx->combined_loop
13893 : 500 : && !octx->distribute)
13894 : : {
13895 : 492 : if (octx->outer_context
13896 : 430 : && (octx->outer_context->region_type
13897 : : == ORT_COMBINED_PARALLEL))
13898 : 393 : octx = octx->outer_context->outer_context;
13899 : : else
13900 : : octx = octx->outer_context;
13901 : : }
13902 : 905 : if (octx
13903 : 316 : && octx->region_type == ORT_WORKSHARE
13904 : 16 : && octx->combined_loop
13905 : 16 : && octx->distribute)
13906 : : {
13907 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
13908 : : "%<linear%> clause for variable other than "
13909 : : "loop iterator specified on construct "
13910 : : "combined with %<distribute%>");
13911 : 16 : remove = true;
13912 : 16 : break;
13913 : : }
13914 : : }
13915 : : /* For combined #pragma omp parallel for simd, need to put
13916 : : lastprivate and perhaps firstprivate too on the
13917 : : parallel. Similarly for #pragma omp for simd. */
13918 : : struct gimplify_omp_ctx *octx = outer_ctx;
13919 : : bool taskloop_seen = false;
13920 : : decl = NULL_TREE;
13921 : 3443 : do
13922 : : {
13923 : 3443 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13924 : 3443 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13925 : : break;
13926 : 3383 : decl = OMP_CLAUSE_DECL (c);
13927 : 3383 : if (error_operand_p (decl))
13928 : : {
13929 : : decl = NULL_TREE;
13930 : : break;
13931 : : }
13932 : 3383 : flags = GOVD_SEEN;
13933 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13934 : 1672 : flags |= GOVD_FIRSTPRIVATE;
13935 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13936 : 3383 : flags |= GOVD_LASTPRIVATE;
13937 : 3383 : if (octx
13938 : 2506 : && octx->region_type == ORT_WORKSHARE
13939 : 1213 : && octx->combined_loop)
13940 : : {
13941 : 1207 : if (octx->outer_context
13942 : 1037 : && (octx->outer_context->region_type
13943 : : == ORT_COMBINED_PARALLEL))
13944 : : octx = octx->outer_context;
13945 : 476 : else if (omp_check_private (octx, decl, false))
13946 : : break;
13947 : : }
13948 : : else if (octx
13949 : 1299 : && (octx->region_type & ORT_TASK) != 0
13950 : 307 : && octx->combined_loop)
13951 : : taskloop_seen = true;
13952 : : else if (octx
13953 : 996 : && octx->region_type == ORT_COMBINED_PARALLEL
13954 : 301 : && ((ctx->region_type == ORT_WORKSHARE
13955 : 201 : && octx == outer_ctx)
13956 : 100 : || taskloop_seen))
13957 : : flags = GOVD_SEEN | GOVD_SHARED;
13958 : : else if (octx
13959 : 695 : && ((octx->region_type & ORT_COMBINED_TEAMS)
13960 : : == ORT_COMBINED_TEAMS))
13961 : : flags = GOVD_SEEN | GOVD_SHARED;
13962 : 540 : else if (octx
13963 : 540 : && octx->region_type == ORT_COMBINED_TARGET)
13964 : : {
13965 : 195 : if (flags & GOVD_LASTPRIVATE)
13966 : 195 : flags = GOVD_SEEN | GOVD_MAP;
13967 : : }
13968 : : else
13969 : : break;
13970 : 2071 : splay_tree_node on
13971 : 2071 : = splay_tree_lookup (octx->variables,
13972 : : (splay_tree_key) decl);
13973 : 2071 : if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
13974 : : {
13975 : : octx = NULL;
13976 : : break;
13977 : : }
13978 : 2067 : omp_add_variable (octx, decl, flags);
13979 : 2067 : if (octx->outer_context == NULL)
13980 : : break;
13981 : : octx = octx->outer_context;
13982 : : }
13983 : : while (1);
13984 : 2652 : if (octx
13985 : 2652 : && decl
13986 : 2652 : && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13987 : 728 : || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
13988 : 1711 : omp_notice_variable (octx, decl, true);
13989 : : }
13990 : 2652 : flags = GOVD_LINEAR | GOVD_EXPLICIT;
13991 : 2652 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13992 : 2652 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13993 : : {
13994 : : notice_outer = false;
13995 : : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
13996 : : }
13997 : 2652 : goto do_add;
13998 : :
13999 : 48411 : case OMP_CLAUSE_MAP:
14000 : 48411 : if (!grp_start_p)
14001 : : {
14002 : 29515 : grp_start_p = list_p;
14003 : 29515 : grp_end = (*groups)[grpnum].grp_end;
14004 : 29515 : grpnum++;
14005 : : }
14006 : 48411 : decl = OMP_CLAUSE_DECL (c);
14007 : :
14008 : 48411 : if (error_operand_p (decl))
14009 : : {
14010 : : remove = true;
14011 : : break;
14012 : : }
14013 : :
14014 : 48411 : if (!omp_parse_expr (addr_tokens, decl))
14015 : : {
14016 : : remove = true;
14017 : : break;
14018 : : }
14019 : :
14020 : 48411 : if (remove)
14021 : : break;
14022 : 48411 : if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
14023 : : {
14024 : : struct gimplify_omp_ctx *octx;
14025 : 1169 : for (octx = outer_ctx; octx; octx = octx->outer_context)
14026 : : {
14027 : 1169 : if (octx->region_type != ORT_ACC_HOST_DATA)
14028 : : break;
14029 : 12 : splay_tree_node n2
14030 : 12 : = splay_tree_lookup (octx->variables,
14031 : : (splay_tree_key) decl);
14032 : 12 : if (n2)
14033 : 4 : error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
14034 : : "declared in enclosing %<host_data%> region",
14035 : 4 : DECL_NAME (decl));
14036 : : }
14037 : : }
14038 : :
14039 : 48411 : map_descriptor = false;
14040 : :
14041 : : /* This condition checks if we're mapping an array descriptor that
14042 : : isn't inside a derived type -- these have special handling, and
14043 : : are not handled as structs in omp_build_struct_sibling_lists.
14044 : : See that function for further details. */
14045 : 48411 : if (*grp_start_p != grp_end
14046 : 32171 : && OMP_CLAUSE_CHAIN (*grp_start_p)
14047 : 80582 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
14048 : : {
14049 : 15475 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
14050 : 15475 : if (omp_map_clause_descriptor_p (grp_mid)
14051 : 29183 : && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
14052 : : map_descriptor = true;
14053 : : }
14054 : 32936 : else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
14055 : 32936 : && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
14056 : 32297 : || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
14057 : 33717 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
14058 : : map_descriptor = true;
14059 : :
14060 : : /* Adding the decl for a struct access: we haven't created
14061 : : GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
14062 : : whether they will be created in gimplify_adjust_omp_clauses.
14063 : : NOTE: Technically we should probably look through DECL_VALUE_EXPR
14064 : : here because something that looks like a DECL_P may actually be a
14065 : : struct access, e.g. variables in a lambda closure
14066 : : (__closure->__foo) or class members (this->foo). Currently in both
14067 : : those cases we map the whole of the containing object (directly in
14068 : : the C++ FE) though, so struct nodes are not created. */
14069 : 48411 : if (c == grp_end
14070 : 29515 : && addr_tokens[0]->type == STRUCTURE_BASE
14071 : 7625 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14072 : 56036 : && !map_descriptor)
14073 : : {
14074 : 4856 : gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
14075 : : /* If we got to this struct via a chain of pointers, maybe we
14076 : : want to map it implicitly instead. */
14077 : 4856 : if (omp_access_chain_p (addr_tokens, 1))
14078 : : break;
14079 : 4696 : omp_mapping_group *wholestruct;
14080 : 4696 : if (!(region_type & ORT_ACC)
14081 : 8115 : && omp_mapped_by_containing_struct (grpmap,
14082 : 3419 : OMP_CLAUSE_DECL (c),
14083 : : &wholestruct))
14084 : : break;
14085 : 4512 : decl = addr_tokens[1]->expr;
14086 : 4512 : if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
14087 : : break;
14088 : : /* Standalone attach or detach clauses for a struct element
14089 : : should not inhibit implicit mapping of the whole struct. */
14090 : 2770 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14091 : 2770 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14092 : : break;
14093 : 2615 : flags = GOVD_MAP | GOVD_EXPLICIT;
14094 : :
14095 : 2615 : gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
14096 : : || TREE_ADDRESSABLE (decl));
14097 : 2615 : goto do_add_decl;
14098 : : }
14099 : :
14100 : 43555 : if (!DECL_P (decl))
14101 : : {
14102 : 19806 : tree d = decl, *pd;
14103 : 19806 : if (TREE_CODE (d) == ARRAY_REF)
14104 : : {
14105 : 4890 : while (TREE_CODE (d) == ARRAY_REF)
14106 : 2502 : d = TREE_OPERAND (d, 0);
14107 : 2388 : if (TREE_CODE (d) == COMPONENT_REF
14108 : 2388 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
14109 : : decl = d;
14110 : : }
14111 : 19806 : pd = &OMP_CLAUSE_DECL (c);
14112 : 19806 : if (d == decl
14113 : 17464 : && TREE_CODE (decl) == INDIRECT_REF
14114 : 13347 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
14115 : 1069 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
14116 : : == REFERENCE_TYPE)
14117 : 20457 : && (OMP_CLAUSE_MAP_KIND (c)
14118 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
14119 : : {
14120 : 647 : pd = &TREE_OPERAND (decl, 0);
14121 : 647 : decl = TREE_OPERAND (decl, 0);
14122 : : }
14123 : :
14124 : 19806 : if (addr_tokens[0]->type == STRUCTURE_BASE
14125 : 10276 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14126 : 10276 : && addr_tokens[1]->type == ACCESS_METHOD
14127 : 10276 : && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
14128 : 9779 : || (addr_tokens[1]->u.access_kind
14129 : : == ACCESS_POINTER_OFFSET))
14130 : 20385 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
14131 : : {
14132 : 0 : tree base = addr_tokens[1]->expr;
14133 : 0 : splay_tree_node n
14134 : 0 : = splay_tree_lookup (ctx->variables,
14135 : : (splay_tree_key) base);
14136 : 0 : n->value |= GOVD_SEEN;
14137 : : }
14138 : :
14139 : 19806 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
14140 : : {
14141 : : /* Don't gimplify *pd fully at this point, as the base
14142 : : will need to be adjusted during omp lowering. */
14143 : 88 : auto_vec<tree, 10> expr_stack;
14144 : 88 : tree *p = pd;
14145 : 88 : while (handled_component_p (*p)
14146 : : || TREE_CODE (*p) == INDIRECT_REF
14147 : : || TREE_CODE (*p) == ADDR_EXPR
14148 : : || TREE_CODE (*p) == MEM_REF
14149 : 224 : || TREE_CODE (*p) == NON_LVALUE_EXPR)
14150 : : {
14151 : 136 : expr_stack.safe_push (*p);
14152 : 136 : p = &TREE_OPERAND (*p, 0);
14153 : : }
14154 : 312 : for (int i = expr_stack.length () - 1; i >= 0; i--)
14155 : : {
14156 : 136 : tree t = expr_stack[i];
14157 : 136 : if (TREE_CODE (t) == ARRAY_REF
14158 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14159 : : {
14160 : 56 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14161 : : {
14162 : 56 : tree low = unshare_expr (array_ref_low_bound (t));
14163 : 56 : if (!is_gimple_min_invariant (low))
14164 : : {
14165 : 0 : TREE_OPERAND (t, 2) = low;
14166 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14167 : : pre_p, NULL,
14168 : : is_gimple_reg,
14169 : : fb_rvalue) == GS_ERROR)
14170 : 0 : remove = true;
14171 : : }
14172 : : }
14173 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14174 : : NULL, is_gimple_reg,
14175 : : fb_rvalue) == GS_ERROR)
14176 : 0 : remove = true;
14177 : 56 : if (TREE_OPERAND (t, 3) == NULL_TREE)
14178 : : {
14179 : 56 : tree elmt_size = array_ref_element_size (t);
14180 : 56 : if (!is_gimple_min_invariant (elmt_size))
14181 : : {
14182 : 0 : elmt_size = unshare_expr (elmt_size);
14183 : 0 : tree elmt_type
14184 : 0 : = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
14185 : : 0)));
14186 : 0 : tree factor
14187 : 0 : = size_int (TYPE_ALIGN_UNIT (elmt_type));
14188 : 0 : elmt_size
14189 : 0 : = size_binop (EXACT_DIV_EXPR, elmt_size,
14190 : : factor);
14191 : 0 : TREE_OPERAND (t, 3) = elmt_size;
14192 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 3),
14193 : : pre_p, NULL,
14194 : : is_gimple_reg,
14195 : : fb_rvalue) == GS_ERROR)
14196 : 0 : remove = true;
14197 : : }
14198 : : }
14199 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
14200 : : NULL, is_gimple_reg,
14201 : : fb_rvalue) == GS_ERROR)
14202 : 0 : remove = true;
14203 : : }
14204 : 80 : else if (TREE_CODE (t) == COMPONENT_REF)
14205 : : {
14206 : 0 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14207 : : {
14208 : 0 : tree offset = component_ref_field_offset (t);
14209 : 0 : if (!is_gimple_min_invariant (offset))
14210 : : {
14211 : 0 : offset = unshare_expr (offset);
14212 : 0 : tree field = TREE_OPERAND (t, 1);
14213 : 0 : tree factor
14214 : 0 : = size_int (DECL_OFFSET_ALIGN (field)
14215 : : / BITS_PER_UNIT);
14216 : 0 : offset = size_binop (EXACT_DIV_EXPR, offset,
14217 : : factor);
14218 : 0 : TREE_OPERAND (t, 2) = offset;
14219 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14220 : : pre_p, NULL,
14221 : : is_gimple_reg,
14222 : : fb_rvalue) == GS_ERROR)
14223 : 0 : remove = true;
14224 : : }
14225 : : }
14226 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14227 : : NULL, is_gimple_reg,
14228 : : fb_rvalue) == GS_ERROR)
14229 : 0 : remove = true;
14230 : : }
14231 : : }
14232 : 224 : for (; expr_stack.length () > 0; )
14233 : : {
14234 : 136 : tree t = expr_stack.pop ();
14235 : :
14236 : 136 : if (TREE_CODE (t) == ARRAY_REF
14237 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14238 : : {
14239 : 56 : if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
14240 : 56 : && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
14241 : : NULL, is_gimple_val,
14242 : : fb_rvalue) == GS_ERROR)
14243 : 144 : remove = true;
14244 : : }
14245 : : }
14246 : 88 : }
14247 : : break;
14248 : : }
14249 : :
14250 : 23749 : if ((code == OMP_TARGET
14251 : : || code == OMP_TARGET_DATA
14252 : : || code == OMP_TARGET_ENTER_DATA
14253 : 14127 : || code == OMP_TARGET_EXIT_DATA)
14254 : 24307 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
14255 : : {
14256 : : /* If we have attach/detach but the decl we have is a pointer to
14257 : : pointer, we're probably mapping the "base level" array
14258 : : implicitly. Make sure we don't add the decl as if we mapped
14259 : : it explicitly. That is,
14260 : :
14261 : : int **arr;
14262 : : [...]
14263 : : #pragma omp target map(arr[a][b:c])
14264 : :
14265 : : should *not* map "arr" explicitly. That way we get a
14266 : : zero-length "alloc" mapping for it, and assuming it's been
14267 : : mapped by some previous directive, etc., things work as they
14268 : : should. */
14269 : :
14270 : 204 : tree basetype = TREE_TYPE (addr_tokens[0]->expr);
14271 : :
14272 : 204 : if (TREE_CODE (basetype) == REFERENCE_TYPE)
14273 : 34 : basetype = TREE_TYPE (basetype);
14274 : :
14275 : 204 : if (code == OMP_TARGET
14276 : 54 : && addr_tokens[0]->type == ARRAY_BASE
14277 : 54 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14278 : 54 : && TREE_CODE (basetype) == POINTER_TYPE
14279 : 258 : && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
14280 : : break;
14281 : : }
14282 : :
14283 : 23727 : flags = GOVD_MAP | GOVD_EXPLICIT;
14284 : 23727 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
14285 : 23616 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
14286 : 23112 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
14287 : 46813 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
14288 : : flags |= GOVD_MAP_ALWAYS_TO;
14289 : :
14290 : 23727 : goto do_add;
14291 : :
14292 : 468 : case OMP_CLAUSE_AFFINITY:
14293 : 468 : gimplify_omp_affinity (list_p, pre_p);
14294 : 468 : remove = true;
14295 : 468 : break;
14296 : 8 : case OMP_CLAUSE_DOACROSS:
14297 : 8 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
14298 : : {
14299 : 4 : tree deps = OMP_CLAUSE_DECL (c);
14300 : 8 : while (deps && TREE_CODE (deps) == TREE_LIST)
14301 : : {
14302 : 4 : if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
14303 : 4 : && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
14304 : 0 : gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
14305 : : pre_p, NULL, is_gimple_val, fb_rvalue);
14306 : 4 : deps = TREE_CHAIN (deps);
14307 : : }
14308 : : }
14309 : : else
14310 : 4 : gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
14311 : : == OMP_CLAUSE_DOACROSS_SOURCE);
14312 : : break;
14313 : 2200 : case OMP_CLAUSE_DEPEND:
14314 : 2200 : if (handled_depend_iterators == -1)
14315 : 1898 : handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
14316 : 2200 : if (handled_depend_iterators)
14317 : : {
14318 : 343 : if (handled_depend_iterators == 2)
14319 : 0 : remove = true;
14320 : : break;
14321 : : }
14322 : 1857 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
14323 : : {
14324 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
14325 : : NULL, is_gimple_val, fb_rvalue);
14326 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
14327 : : }
14328 : 1857 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
14329 : : {
14330 : : remove = true;
14331 : : break;
14332 : : }
14333 : 1857 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
14334 : : {
14335 : 1826 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
14336 : 1826 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
14337 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14338 : : {
14339 : : remove = true;
14340 : : break;
14341 : : }
14342 : : }
14343 : 1857 : if (code == OMP_TASK)
14344 : 1399 : ctx->has_depend = true;
14345 : : break;
14346 : :
14347 : 8224 : case OMP_CLAUSE_TO:
14348 : 8224 : case OMP_CLAUSE_FROM:
14349 : 8224 : case OMP_CLAUSE__CACHE_:
14350 : 8224 : decl = OMP_CLAUSE_DECL (c);
14351 : 8224 : if (error_operand_p (decl))
14352 : : {
14353 : : remove = true;
14354 : : break;
14355 : : }
14356 : 8224 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
14357 : 7435 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
14358 : 684 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
14359 : 8224 : gimple_seq *seq_p;
14360 : 8224 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
14361 : 8224 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
14362 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14363 : : {
14364 : 0 : remove = true;
14365 : 0 : exit_omp_iterator_loop_context (c);
14366 : 0 : break;
14367 : : }
14368 : 8224 : if (!DECL_P (decl))
14369 : : {
14370 : 2037 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
14371 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
14372 : 0 : remove = true;
14373 : 2037 : exit_omp_iterator_loop_context (c);
14374 : 2037 : break;
14375 : : }
14376 : 6187 : exit_omp_iterator_loop_context (c);
14377 : 6187 : goto do_notice;
14378 : :
14379 : 125 : case OMP_CLAUSE__MAPPER_BINDING_:
14380 : 125 : {
14381 : 125 : tree name = OMP_CLAUSE__MAPPER_BINDING__ID (c);
14382 : 125 : tree var = OMP_CLAUSE__MAPPER_BINDING__DECL (c);
14383 : 125 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (var));
14384 : 125 : tree fndecl = OMP_CLAUSE__MAPPER_BINDING__MAPPER (c);
14385 : 125 : ctx->implicit_mappers->put ({ name, type }, fndecl);
14386 : 125 : remove = true;
14387 : 125 : break;
14388 : : }
14389 : :
14390 : 2118 : case OMP_CLAUSE_USE_DEVICE_PTR:
14391 : 2118 : case OMP_CLAUSE_USE_DEVICE_ADDR:
14392 : 2118 : flags = GOVD_EXPLICIT;
14393 : 2118 : goto do_add;
14394 : :
14395 : 543 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
14396 : 543 : decl = OMP_CLAUSE_DECL (c);
14397 : 543 : while (TREE_CODE (decl) == INDIRECT_REF
14398 : 592 : || TREE_CODE (decl) == ARRAY_REF)
14399 : 49 : decl = TREE_OPERAND (decl, 0);
14400 : 543 : flags = GOVD_EXPLICIT;
14401 : 543 : goto do_add_decl;
14402 : :
14403 : 498 : case OMP_CLAUSE_IS_DEVICE_PTR:
14404 : 498 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
14405 : 498 : goto do_add;
14406 : :
14407 : 61995 : do_add:
14408 : 61995 : decl = OMP_CLAUSE_DECL (c);
14409 : 83047 : do_add_decl:
14410 : 83047 : if (error_operand_p (decl))
14411 : : {
14412 : : remove = true;
14413 : : break;
14414 : : }
14415 : 83039 : if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
14416 : : {
14417 : 1756 : tree t = omp_member_access_dummy_var (decl);
14418 : 1756 : if (t)
14419 : : {
14420 : 662 : tree v = DECL_VALUE_EXPR (decl);
14421 : 662 : DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
14422 : 662 : if (outer_ctx)
14423 : 134 : omp_notice_variable (outer_ctx, t, true);
14424 : : }
14425 : : }
14426 : 83039 : if (code == OACC_DATA
14427 : 2404 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14428 : 85443 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14429 : 308 : flags |= GOVD_MAP_0LEN_ARRAY;
14430 : 83039 : omp_add_variable (ctx, decl, flags);
14431 : 83039 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14432 : 67745 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14433 : 65675 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14434 : 85639 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14435 : : {
14436 : 2087 : struct gimplify_omp_ctx *pctx
14437 : 2147 : = code == OMP_TARGET ? outer_ctx : ctx;
14438 : 2147 : if (pctx)
14439 : 2117 : omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
14440 : : GOVD_LOCAL | GOVD_SEEN);
14441 : 2117 : if (pctx
14442 : 2117 : && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
14443 : 632 : && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14444 : : find_decl_expr,
14445 : : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14446 : : NULL) == NULL_TREE)
14447 : 208 : omp_add_variable (pctx,
14448 : 208 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14449 : : GOVD_LOCAL | GOVD_SEEN);
14450 : 2147 : gimplify_omp_ctxp = pctx;
14451 : 2147 : push_gimplify_context ();
14452 : :
14453 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
14454 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
14455 : :
14456 : 2147 : gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
14457 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
14458 : 2147 : pop_gimplify_context
14459 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
14460 : 2147 : push_gimplify_context ();
14461 : 4294 : gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
14462 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
14463 : 2147 : pop_gimplify_context
14464 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
14465 : 2147 : OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
14466 : 2147 : OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
14467 : :
14468 : 2147 : gimplify_omp_ctxp = outer_ctx;
14469 : : }
14470 : 80892 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14471 : 80892 : && OMP_CLAUSE_LASTPRIVATE_STMT (c))
14472 : : {
14473 : 303 : gimplify_omp_ctxp = ctx;
14474 : 303 : push_gimplify_context ();
14475 : 303 : if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
14476 : : {
14477 : 303 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14478 : : NULL, NULL);
14479 : 303 : TREE_SIDE_EFFECTS (bind) = 1;
14480 : 303 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
14481 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
14482 : : }
14483 : 606 : gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
14484 : 303 : &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
14485 : 303 : pop_gimplify_context
14486 : 303 : (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
14487 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
14488 : :
14489 : 303 : gimplify_omp_ctxp = outer_ctx;
14490 : : }
14491 : 80589 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14492 : 80589 : && OMP_CLAUSE_LINEAR_STMT (c))
14493 : : {
14494 : 60 : gimplify_omp_ctxp = ctx;
14495 : 60 : push_gimplify_context ();
14496 : 60 : if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
14497 : : {
14498 : 60 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14499 : : NULL, NULL);
14500 : 60 : TREE_SIDE_EFFECTS (bind) = 1;
14501 : 60 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
14502 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = bind;
14503 : : }
14504 : 120 : gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
14505 : 60 : &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
14506 : 60 : pop_gimplify_context
14507 : 60 : (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
14508 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
14509 : :
14510 : 60 : gimplify_omp_ctxp = outer_ctx;
14511 : : }
14512 : 83039 : if (notice_outer)
14513 : 71003 : goto do_notice;
14514 : : break;
14515 : :
14516 : 906 : case OMP_CLAUSE_COPYIN:
14517 : 906 : case OMP_CLAUSE_COPYPRIVATE:
14518 : 906 : decl = OMP_CLAUSE_DECL (c);
14519 : 906 : if (error_operand_p (decl))
14520 : : {
14521 : : remove = true;
14522 : : break;
14523 : : }
14524 : 906 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
14525 : : && !remove
14526 : 906 : && !omp_check_private (ctx, decl, true))
14527 : : {
14528 : 35 : remove = true;
14529 : 35 : if (is_global_var (decl))
14530 : : {
14531 : 30 : if (DECL_THREAD_LOCAL_P (decl))
14532 : : remove = false;
14533 : 10 : else if (DECL_HAS_VALUE_EXPR_P (decl))
14534 : : {
14535 : 2 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
14536 : :
14537 : 2 : if (value
14538 : 2 : && DECL_P (value)
14539 : 4 : && DECL_THREAD_LOCAL_P (value))
14540 : : remove = false;
14541 : : }
14542 : : }
14543 : : if (remove)
14544 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
14545 : : "copyprivate variable %qE is not threadprivate"
14546 : 13 : " or private in outer context", DECL_NAME (decl));
14547 : : }
14548 : 78096 : do_notice:
14549 : 78096 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14550 : 62802 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14551 : 54968 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14552 : 30453 : && outer_ctx
14553 : 17560 : && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
14554 : 16233 : || (region_type == ORT_WORKSHARE
14555 : 3884 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14556 : 1087 : && (OMP_CLAUSE_REDUCTION_INSCAN (c)
14557 : 912 : || code == OMP_LOOP)))
14558 : 80029 : && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
14559 : 845 : || (code == OMP_LOOP
14560 : 138 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14561 : 138 : && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
14562 : : == ORT_COMBINED_TEAMS))))
14563 : : {
14564 : 1209 : splay_tree_node on
14565 : 1209 : = splay_tree_lookup (outer_ctx->variables,
14566 : : (splay_tree_key)decl);
14567 : 1209 : if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
14568 : : {
14569 : 891 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14570 : 705 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14571 : 1003 : && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
14572 : 56 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14573 : 0 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
14574 : : == POINTER_TYPE))))
14575 : 56 : omp_firstprivatize_variable (outer_ctx, decl);
14576 : : else
14577 : : {
14578 : 835 : omp_add_variable (outer_ctx, decl,
14579 : : GOVD_SEEN | GOVD_SHARED);
14580 : 835 : if (outer_ctx->outer_context)
14581 : 235 : omp_notice_variable (outer_ctx->outer_context, decl,
14582 : : true);
14583 : : }
14584 : : }
14585 : : }
14586 : 77496 : if (outer_ctx)
14587 : 29603 : omp_notice_variable (outer_ctx, decl, true);
14588 : 78096 : if (check_non_private
14589 : 26652 : && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
14590 : 4747 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14591 : 1641 : || decl == OMP_CLAUSE_DECL (c)
14592 : 224 : || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14593 : 224 : && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14594 : : == ADDR_EXPR
14595 : 113 : || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14596 : : == POINTER_PLUS_EXPR
14597 : 22 : && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
14598 : : (OMP_CLAUSE_DECL (c), 0), 0))
14599 : : == ADDR_EXPR)))))
14600 : 82741 : && omp_check_private (ctx, decl, false))
14601 : : {
14602 : 58 : error ("%s variable %qE is private in outer context",
14603 : 29 : check_non_private, DECL_NAME (decl));
14604 : 29 : remove = true;
14605 : : }
14606 : : break;
14607 : :
14608 : 190 : case OMP_CLAUSE_DETACH:
14609 : 190 : flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
14610 : 190 : goto do_add;
14611 : :
14612 : 4092 : case OMP_CLAUSE_IF:
14613 : 4092 : if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
14614 : 4092 : && OMP_CLAUSE_IF_MODIFIER (c) != code)
14615 : : {
14616 : : const char *p[2];
14617 : 168 : for (int i = 0; i < 2; i++)
14618 : 112 : switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
14619 : : {
14620 : 8 : case VOID_CST: p[i] = "cancel"; break;
14621 : 24 : case OMP_PARALLEL: p[i] = "parallel"; break;
14622 : 4 : case OMP_SIMD: p[i] = "simd"; break;
14623 : 12 : case OMP_TASK: p[i] = "task"; break;
14624 : 12 : case OMP_TASKLOOP: p[i] = "taskloop"; break;
14625 : 8 : case OMP_TARGET_DATA: p[i] = "target data"; break;
14626 : 12 : case OMP_TARGET: p[i] = "target"; break;
14627 : 12 : case OMP_TARGET_UPDATE: p[i] = "target update"; break;
14628 : 8 : case OMP_TARGET_ENTER_DATA:
14629 : 8 : p[i] = "target enter data"; break;
14630 : 12 : case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
14631 : 0 : default: gcc_unreachable ();
14632 : : }
14633 : 56 : error_at (OMP_CLAUSE_LOCATION (c),
14634 : : "expected %qs %<if%> clause modifier rather than %qs",
14635 : : p[0], p[1]);
14636 : 56 : remove = true;
14637 : : }
14638 : : /* Fall through. */
14639 : :
14640 : 4798 : case OMP_CLAUSE_SELF:
14641 : 4798 : case OMP_CLAUSE_FINAL:
14642 : 4798 : OMP_CLAUSE_OPERAND (c, 0)
14643 : 9596 : = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
14644 : : /* Fall through. */
14645 : :
14646 : 5718 : case OMP_CLAUSE_NUM_TEAMS:
14647 : 5718 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
14648 : 920 : && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14649 : 5963 : && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14650 : : {
14651 : 208 : if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14652 : : {
14653 : : remove = true;
14654 : : break;
14655 : : }
14656 : 208 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14657 : 416 : = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
14658 : : pre_p, NULL, true);
14659 : : }
14660 : : /* Fall through. */
14661 : :
14662 : 21738 : case OMP_CLAUSE_SCHEDULE:
14663 : 21738 : case OMP_CLAUSE_NUM_THREADS:
14664 : 21738 : case OMP_CLAUSE_THREAD_LIMIT:
14665 : 21738 : case OMP_CLAUSE_DIST_SCHEDULE:
14666 : 21738 : case OMP_CLAUSE_DEVICE:
14667 : 21738 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14668 : 21738 : && OMP_CLAUSE_DEVICE_ANCESTOR (c))
14669 : : {
14670 : 125 : if (code != OMP_TARGET)
14671 : : {
14672 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
14673 : : "%<device%> clause with %<ancestor%> is only "
14674 : : "allowed on %<target%> construct");
14675 : 20 : remove = true;
14676 : 20 : break;
14677 : : }
14678 : :
14679 : 105 : tree clauses = *orig_list_p;
14680 : 330 : for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
14681 : 236 : if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
14682 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
14683 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
14684 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
14685 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
14686 : : )
14687 : : {
14688 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
14689 : : "with %<ancestor%>, only the %<device%>, "
14690 : : "%<firstprivate%>, %<private%>, %<defaultmap%>, "
14691 : : "and %<map%> clauses may appear on the "
14692 : : "construct");
14693 : 11 : remove = true;
14694 : 11 : break;
14695 : : }
14696 : : }
14697 : 21613 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14698 : 21613 : && code == OMP_DISPATCH)
14699 : : {
14700 : 272 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
14701 : 272 : gimplify_ctxp->into_ssa = false;
14702 : 272 : if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
14703 : : is_gimple_val, fb_rvalue)
14704 : : == GS_ERROR)
14705 : : remove = true;
14706 : 272 : else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
14707 : 55 : omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
14708 : : GOVD_SHARED | GOVD_SEEN);
14709 : 272 : gimplify_ctxp->into_ssa = saved_into_ssa;
14710 : 272 : break;
14711 : : }
14712 : : /* Fall through. */
14713 : :
14714 : 31151 : case OMP_CLAUSE_PRIORITY:
14715 : 31151 : case OMP_CLAUSE_GRAINSIZE:
14716 : 31151 : case OMP_CLAUSE_NUM_TASKS:
14717 : 31151 : case OMP_CLAUSE_FILTER:
14718 : 31151 : case OMP_CLAUSE_HINT:
14719 : 31151 : case OMP_CLAUSE_ASYNC:
14720 : 31151 : case OMP_CLAUSE_WAIT:
14721 : 31151 : case OMP_CLAUSE_NUM_GANGS:
14722 : 31151 : case OMP_CLAUSE_NUM_WORKERS:
14723 : 31151 : case OMP_CLAUSE_VECTOR_LENGTH:
14724 : 31151 : case OMP_CLAUSE_WORKER:
14725 : 31151 : case OMP_CLAUSE_VECTOR:
14726 : 31151 : if (OMP_CLAUSE_OPERAND (c, 0)
14727 : 31151 : && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
14728 : : {
14729 : 7711 : if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
14730 : : {
14731 : : remove = true;
14732 : : break;
14733 : : }
14734 : : /* All these clauses care about value, not a particular decl,
14735 : : so try to force it into a SSA_NAME or fresh temporary. */
14736 : 7704 : OMP_CLAUSE_OPERAND (c, 0)
14737 : 15408 : = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
14738 : : pre_p, NULL, true);
14739 : : }
14740 : : break;
14741 : :
14742 : 2331 : case OMP_CLAUSE_GANG:
14743 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
14744 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14745 : 0 : remove = true;
14746 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
14747 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14748 : 0 : remove = true;
14749 : : break;
14750 : :
14751 : : case OMP_CLAUSE_NOWAIT:
14752 : 200598 : nowait = 1;
14753 : : break;
14754 : :
14755 : : case OMP_CLAUSE_ORDERED:
14756 : : case OMP_CLAUSE_UNTIED:
14757 : : case OMP_CLAUSE_COLLAPSE:
14758 : : case OMP_CLAUSE_TILE:
14759 : : case OMP_CLAUSE_AUTO:
14760 : : case OMP_CLAUSE_SEQ:
14761 : : case OMP_CLAUSE_INDEPENDENT:
14762 : : case OMP_CLAUSE_MERGEABLE:
14763 : : case OMP_CLAUSE_PROC_BIND:
14764 : : case OMP_CLAUSE_SAFELEN:
14765 : : case OMP_CLAUSE_SIMDLEN:
14766 : : case OMP_CLAUSE_NOGROUP:
14767 : : case OMP_CLAUSE_THREADS:
14768 : : case OMP_CLAUSE_SIMD:
14769 : : case OMP_CLAUSE_BIND:
14770 : : case OMP_CLAUSE_IF_PRESENT:
14771 : : case OMP_CLAUSE_FINALIZE:
14772 : : case OMP_CLAUSE_INTEROP:
14773 : : case OMP_CLAUSE_INIT:
14774 : : case OMP_CLAUSE_USE:
14775 : : case OMP_CLAUSE_DESTROY:
14776 : : break;
14777 : :
14778 : 3969 : case OMP_CLAUSE_ORDER:
14779 : 3969 : ctx->order_concurrent = true;
14780 : 3969 : break;
14781 : :
14782 : 1014 : case OMP_CLAUSE_DEFAULTMAP:
14783 : 1014 : enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
14784 : 1014 : switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
14785 : : {
14786 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
14787 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
14788 : : gdmkmin = GDMK_SCALAR;
14789 : : gdmkmax = GDMK_POINTER;
14790 : : break;
14791 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14792 : : gdmkmin = GDMK_SCALAR;
14793 : : gdmkmax = GDMK_SCALAR_TARGET;
14794 : : break;
14795 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14796 : : gdmkmin = gdmkmax = GDMK_AGGREGATE;
14797 : : break;
14798 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
14799 : : gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
14800 : : break;
14801 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14802 : : gdmkmin = gdmkmax = GDMK_POINTER;
14803 : : break;
14804 : 0 : default:
14805 : 0 : gcc_unreachable ();
14806 : : }
14807 : 4487 : for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
14808 : 3473 : switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
14809 : : {
14810 : 91 : case OMP_CLAUSE_DEFAULTMAP_ALLOC:
14811 : 91 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
14812 : 91 : break;
14813 : 121 : case OMP_CLAUSE_DEFAULTMAP_TO:
14814 : 121 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
14815 : 121 : break;
14816 : 28 : case OMP_CLAUSE_DEFAULTMAP_FROM:
14817 : 28 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
14818 : 28 : break;
14819 : 671 : case OMP_CLAUSE_DEFAULTMAP_TOFROM:
14820 : 671 : ctx->defaultmap[gdmk] = GOVD_MAP;
14821 : 671 : break;
14822 : 380 : case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
14823 : 380 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14824 : 380 : break;
14825 : 2079 : case OMP_CLAUSE_DEFAULTMAP_NONE:
14826 : 2079 : ctx->defaultmap[gdmk] = 0;
14827 : 2079 : break;
14828 : 45 : case OMP_CLAUSE_DEFAULTMAP_PRESENT:
14829 : 45 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
14830 : 45 : break;
14831 : 58 : case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
14832 : 58 : switch (gdmk)
14833 : : {
14834 : 11 : case GDMK_SCALAR:
14835 : 11 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14836 : 11 : break;
14837 : 11 : case GDMK_SCALAR_TARGET:
14838 : 11 : ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
14839 : 11 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
14840 : 11 : break;
14841 : 14 : case GDMK_AGGREGATE:
14842 : 14 : case GDMK_ALLOCATABLE:
14843 : 14 : ctx->defaultmap[gdmk] = GOVD_MAP;
14844 : 14 : break;
14845 : 22 : case GDMK_POINTER:
14846 : 22 : ctx->defaultmap[gdmk] = GOVD_MAP;
14847 : 22 : if (!lang_GNU_Fortran ())
14848 : 14 : ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
14849 : : break;
14850 : : default:
14851 : : gcc_unreachable ();
14852 : : }
14853 : : break;
14854 : 0 : default:
14855 : 0 : gcc_unreachable ();
14856 : : }
14857 : : break;
14858 : :
14859 : 824 : case OMP_CLAUSE_ALIGNED:
14860 : 824 : decl = OMP_CLAUSE_DECL (c);
14861 : 824 : if (error_operand_p (decl))
14862 : : {
14863 : : remove = true;
14864 : : break;
14865 : : }
14866 : 824 : if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
14867 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14868 : : {
14869 : : remove = true;
14870 : : break;
14871 : : }
14872 : 824 : if (!is_global_var (decl)
14873 : 824 : && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14874 : 592 : omp_add_variable (ctx, decl, GOVD_ALIGNED);
14875 : : break;
14876 : :
14877 : 489 : case OMP_CLAUSE_NONTEMPORAL:
14878 : 489 : decl = OMP_CLAUSE_DECL (c);
14879 : 489 : if (error_operand_p (decl))
14880 : : {
14881 : : remove = true;
14882 : : break;
14883 : : }
14884 : 489 : omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
14885 : 489 : break;
14886 : :
14887 : 3541 : case OMP_CLAUSE_ALLOCATE:
14888 : 3541 : decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
14889 : 3541 : if (decl
14890 : 1580 : && TREE_CODE (decl) == INTEGER_CST
14891 : 3570 : && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREADS)
14892 : 3570 : && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
14893 : 35 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
14894 : : "allocator with access trait set to %<thread%> "
14895 : : "results in undefined behavior for %qs directive",
14896 : : code == OMP_TARGET ? "target"
14897 : : : (code == OMP_TASK
14898 : 15 : ? "task" : "taskloop"));
14899 : 3541 : decl = OMP_CLAUSE_DECL (c);
14900 : 3541 : if (error_operand_p (decl))
14901 : : {
14902 : : remove = true;
14903 : : break;
14904 : : }
14905 : 3541 : if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
14906 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14907 : : {
14908 : : remove = true;
14909 : : break;
14910 : : }
14911 : 3541 : else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
14912 : 3541 : || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
14913 : : == INTEGER_CST))
14914 : : ;
14915 : 491 : else if (code == OMP_TASKLOOP
14916 : 491 : || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
14917 : 66 : OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
14918 : 132 : = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
14919 : : pre_p, NULL, false);
14920 : : break;
14921 : :
14922 : 4349 : case OMP_CLAUSE_DEFAULT:
14923 : 4349 : ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
14924 : 4349 : break;
14925 : :
14926 : 640 : case OMP_CLAUSE_INCLUSIVE:
14927 : 640 : case OMP_CLAUSE_EXCLUSIVE:
14928 : 640 : decl = OMP_CLAUSE_DECL (c);
14929 : 640 : {
14930 : 640 : splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
14931 : : (splay_tree_key) decl);
14932 : 640 : if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
14933 : : {
14934 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
14935 : : "%qD specified in %qs clause but not in %<inscan%> "
14936 : : "%<reduction%> clause on the containing construct",
14937 : 5 : decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14938 : 5 : remove = true;
14939 : : }
14940 : : else
14941 : : {
14942 : 635 : n->value |= GOVD_REDUCTION_INSCAN;
14943 : 635 : if (outer_ctx->region_type == ORT_SIMD
14944 : 520 : && outer_ctx->outer_context
14945 : 107 : && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
14946 : : {
14947 : 107 : n = splay_tree_lookup (outer_ctx->outer_context->variables,
14948 : : (splay_tree_key) decl);
14949 : 107 : if (n && (n->value & GOVD_REDUCTION) != 0)
14950 : 107 : n->value |= GOVD_REDUCTION_INSCAN;
14951 : : }
14952 : : }
14953 : : }
14954 : : break;
14955 : :
14956 : 97 : case OMP_CLAUSE_NOVARIANTS:
14957 : 97 : OMP_CLAUSE_NOVARIANTS_EXPR (c)
14958 : 97 : = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
14959 : 97 : break;
14960 : 115 : case OMP_CLAUSE_NOCONTEXT:
14961 : 115 : OMP_CLAUSE_NOCONTEXT_EXPR (c)
14962 : 115 : = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
14963 : 115 : break;
14964 : 0 : case OMP_CLAUSE_NOHOST:
14965 : 0 : default:
14966 : 0 : gcc_unreachable ();
14967 : : }
14968 : :
14969 : 1399 : if (code == OACC_DATA
14970 : 5128 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14971 : 205414 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14972 : 4508 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
14973 : : remove = true;
14974 : 201689 : if (remove)
14975 : 1066 : *list_p = OMP_CLAUSE_CHAIN (c);
14976 : : else
14977 : 200931 : list_p = &OMP_CLAUSE_CHAIN (c);
14978 : 201997 : }
14979 : :
14980 : 129470 : if (groups)
14981 : : {
14982 : 34726 : delete grpmap;
14983 : 17363 : delete groups;
14984 : : }
14985 : :
14986 : 129470 : ctx->clauses = *orig_list_p;
14987 : 129470 : gimplify_omp_ctxp = ctx;
14988 : 129470 : }
14989 : :
14990 : : /* Return true if DECL is a candidate for shared to firstprivate
14991 : : optimization. We only consider non-addressable scalars, not
14992 : : too big, and not references. */
14993 : :
14994 : : static bool
14995 : 403838 : omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
14996 : : {
14997 : 403838 : if (TREE_ADDRESSABLE (decl))
14998 : : return false;
14999 : 355374 : tree type = TREE_TYPE (decl);
15000 : 355374 : if (!is_gimple_reg_type (type)
15001 : 330661 : || TREE_CODE (type) == REFERENCE_TYPE
15002 : 680456 : || TREE_ADDRESSABLE (type))
15003 : : return false;
15004 : : /* Don't optimize too large decls, as each thread/task will have
15005 : : its own. */
15006 : 325082 : HOST_WIDE_INT len = int_size_in_bytes (type);
15007 : 325082 : if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
15008 : : return false;
15009 : 325065 : if (omp_privatize_by_reference (decl))
15010 : : return false;
15011 : : return true;
15012 : : }
15013 : :
15014 : : /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
15015 : : For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
15016 : : GOVD_WRITTEN in outer contexts. */
15017 : :
15018 : : static void
15019 : 302234 : omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
15020 : : {
15021 : 464105 : for (; ctx; ctx = ctx->outer_context)
15022 : : {
15023 : 447131 : splay_tree_node n = splay_tree_lookup (ctx->variables,
15024 : : (splay_tree_key) decl);
15025 : 447131 : if (n == NULL)
15026 : 158295 : continue;
15027 : 288836 : else if (n->value & GOVD_SHARED)
15028 : : {
15029 : 9093 : n->value |= GOVD_WRITTEN;
15030 : 9093 : return;
15031 : : }
15032 : 279743 : else if (n->value & GOVD_DATA_SHARE_CLASS)
15033 : : return;
15034 : : }
15035 : : }
15036 : :
15037 : : /* Helper callback for walk_gimple_seq to discover possible stores
15038 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15039 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15040 : : for those. */
15041 : :
15042 : : static tree
15043 : 1122579 : omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
15044 : : {
15045 : 1122579 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
15046 : :
15047 : 1122579 : *walk_subtrees = 0;
15048 : 1122579 : if (!wi->is_lhs)
15049 : : return NULL_TREE;
15050 : :
15051 : 320947 : tree op = *tp;
15052 : 399648 : do
15053 : : {
15054 : 399648 : if (handled_component_p (op))
15055 : 78701 : op = TREE_OPERAND (op, 0);
15056 : 320947 : else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
15057 : 320947 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
15058 : 0 : op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
15059 : : else
15060 : : break;
15061 : : }
15062 : : while (1);
15063 : 320947 : if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
15064 : 58994 : return NULL_TREE;
15065 : :
15066 : 261953 : omp_mark_stores (gimplify_omp_ctxp, op);
15067 : 261953 : return NULL_TREE;
15068 : : }
15069 : :
15070 : : /* Helper callback for walk_gimple_seq to discover possible stores
15071 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15072 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15073 : : for those. */
15074 : :
15075 : : static tree
15076 : 624563 : omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
15077 : : bool *handled_ops_p,
15078 : : struct walk_stmt_info *wi)
15079 : : {
15080 : 624563 : gimple *stmt = gsi_stmt (*gsi_p);
15081 : 624563 : switch (gimple_code (stmt))
15082 : : {
15083 : : /* Don't recurse on OpenMP constructs for which
15084 : : gimplify_adjust_omp_clauses already handled the bodies,
15085 : : except handle gimple_omp_for_pre_body. */
15086 : 26731 : case GIMPLE_OMP_FOR:
15087 : 26731 : *handled_ops_p = true;
15088 : 26731 : if (gimple_omp_for_pre_body (stmt))
15089 : 1387 : walk_gimple_seq (gimple_omp_for_pre_body (stmt),
15090 : : omp_find_stores_stmt, omp_find_stores_op, wi);
15091 : : break;
15092 : 8585 : case GIMPLE_OMP_PARALLEL:
15093 : 8585 : case GIMPLE_OMP_TASK:
15094 : 8585 : case GIMPLE_OMP_SECTIONS:
15095 : 8585 : case GIMPLE_OMP_SINGLE:
15096 : 8585 : case GIMPLE_OMP_SCOPE:
15097 : 8585 : case GIMPLE_OMP_TARGET:
15098 : 8585 : case GIMPLE_OMP_TEAMS:
15099 : 8585 : case GIMPLE_OMP_CRITICAL:
15100 : 8585 : *handled_ops_p = true;
15101 : 8585 : break;
15102 : : default:
15103 : : break;
15104 : : }
15105 : 624563 : return NULL_TREE;
15106 : : }
15107 : :
15108 : : struct gimplify_adjust_omp_clauses_data
15109 : : {
15110 : : tree *list_p;
15111 : : gimple_seq *pre_p;
15112 : : };
15113 : :
15114 : : /* For all variables that were not actually used within the context,
15115 : : remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
15116 : :
15117 : : static int
15118 : 662865 : gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
15119 : : {
15120 : 662865 : tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
15121 : 662865 : gimple_seq *pre_p
15122 : : = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
15123 : 662865 : tree decl = (tree) n->key;
15124 : 662865 : unsigned flags = n->value;
15125 : 662865 : enum omp_clause_code code;
15126 : 662865 : tree clause;
15127 : 662865 : bool private_debug;
15128 : :
15129 : 662865 : if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
15130 : 132828 : && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
15131 : : flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
15132 : 662740 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
15133 : : return 0;
15134 : 177729 : if ((flags & GOVD_SEEN) == 0)
15135 : : return 0;
15136 : 156963 : if (flags & GOVD_DEBUG_PRIVATE)
15137 : : {
15138 : 256 : gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
15139 : : private_debug = true;
15140 : : }
15141 : 156707 : else if (flags & GOVD_MAP)
15142 : : private_debug = false;
15143 : : else
15144 : 138658 : private_debug
15145 : 138658 : = lang_hooks.decls.omp_private_debug_clause (decl,
15146 : 138658 : !!(flags & GOVD_SHARED));
15147 : 138658 : if (private_debug)
15148 : : code = OMP_CLAUSE_PRIVATE;
15149 : 156585 : else if (flags & GOVD_MAP)
15150 : : {
15151 : 18049 : code = OMP_CLAUSE_MAP;
15152 : 18049 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15153 : 18049 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15154 : : {
15155 : 2 : error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
15156 : 2 : return 0;
15157 : : }
15158 : 18047 : if (VAR_P (decl)
15159 : 16111 : && DECL_IN_CONSTANT_POOL (decl)
15160 : 18048 : && !lookup_attribute ("omp declare target",
15161 : 1 : DECL_ATTRIBUTES (decl)))
15162 : : {
15163 : 1 : tree id = get_identifier ("omp declare target");
15164 : 1 : DECL_ATTRIBUTES (decl)
15165 : 1 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
15166 : 1 : varpool_node *node = varpool_node::get (decl);
15167 : 1 : if (node)
15168 : : {
15169 : 1 : node->offloadable = 1;
15170 : 1 : if (ENABLE_OFFLOADING)
15171 : : g->have_offload = true;
15172 : : }
15173 : : }
15174 : : }
15175 : 138536 : else if (flags & GOVD_SHARED)
15176 : : {
15177 : 49010 : if (is_global_var (decl))
15178 : : {
15179 : 16107 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15180 : 25287 : while (ctx != NULL)
15181 : : {
15182 : 17593 : splay_tree_node on
15183 : 17593 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15184 : 17593 : if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
15185 : : | GOVD_PRIVATE | GOVD_REDUCTION
15186 : : | GOVD_LINEAR | GOVD_MAP)) != 0)
15187 : : break;
15188 : 9180 : ctx = ctx->outer_context;
15189 : : }
15190 : 16107 : if (ctx == NULL)
15191 : : return 0;
15192 : : }
15193 : 41316 : code = OMP_CLAUSE_SHARED;
15194 : : /* Don't optimize shared into firstprivate for read-only vars
15195 : : on tasks with depend clause, we shouldn't try to copy them
15196 : : until the dependencies are satisfied. */
15197 : 41316 : if (gimplify_omp_ctxp->has_depend)
15198 : 350 : flags |= GOVD_WRITTEN;
15199 : : }
15200 : 89526 : else if (flags & GOVD_PRIVATE)
15201 : : code = OMP_CLAUSE_PRIVATE;
15202 : 31085 : else if (flags & GOVD_FIRSTPRIVATE)
15203 : : {
15204 : 21682 : code = OMP_CLAUSE_FIRSTPRIVATE;
15205 : 21682 : if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
15206 : 13683 : && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15207 : 31684 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15208 : : {
15209 : 1 : error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
15210 : : "%<target%> construct", decl);
15211 : 1 : return 0;
15212 : : }
15213 : : }
15214 : 9403 : else if (flags & GOVD_LASTPRIVATE)
15215 : : code = OMP_CLAUSE_LASTPRIVATE;
15216 : 241 : else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
15217 : : return 0;
15218 : 118 : else if (flags & GOVD_CONDTEMP)
15219 : : {
15220 : 118 : code = OMP_CLAUSE__CONDTEMP_;
15221 : 118 : gimple_add_tmp_var (decl);
15222 : : }
15223 : : else
15224 : 0 : gcc_unreachable ();
15225 : :
15226 : 139981 : if (((flags & GOVD_LASTPRIVATE)
15227 : 139291 : || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
15228 : 155189 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15229 : 15194 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15230 : :
15231 : 149143 : tree chain = *list_p;
15232 : 149143 : clause = build_omp_clause (input_location, code);
15233 : 149143 : OMP_CLAUSE_DECL (clause) = decl;
15234 : 149143 : OMP_CLAUSE_CHAIN (clause) = chain;
15235 : 149143 : if (private_debug)
15236 : 378 : OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
15237 : 148765 : else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
15238 : 6 : OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
15239 : 148759 : else if (code == OMP_CLAUSE_SHARED
15240 : 41316 : && (flags & GOVD_WRITTEN) == 0
15241 : 184029 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15242 : 20986 : OMP_CLAUSE_SHARED_READONLY (clause) = 1;
15243 : 127773 : else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
15244 : 21681 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
15245 : 106092 : else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
15246 : : {
15247 : 644 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
15248 : 644 : OMP_CLAUSE_DECL (nc) = decl;
15249 : 644 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
15250 : 644 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
15251 : 15 : OMP_CLAUSE_DECL (clause)
15252 : 30 : = build_fold_indirect_ref_loc (input_location, decl);
15253 : 644 : OMP_CLAUSE_DECL (clause)
15254 : 644 : = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
15255 : : build_int_cst (build_pointer_type (char_type_node), 0));
15256 : 644 : OMP_CLAUSE_SIZE (clause) = size_zero_node;
15257 : 644 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15258 : 644 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
15259 : 644 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
15260 : 644 : tree dtype = TREE_TYPE (decl);
15261 : 644 : if (TREE_CODE (dtype) == REFERENCE_TYPE)
15262 : 15 : dtype = TREE_TYPE (dtype);
15263 : : /* FIRSTPRIVATE_POINTER doesn't work well if we have a
15264 : : multiply-indirected pointer. If we have a reference to a pointer to
15265 : : a pointer, it's possible that this should really be
15266 : : GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
15267 : : moment, so stick with this. (See PR113279 and testcases
15268 : : baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice). */
15269 : 644 : if (TREE_CODE (dtype) == POINTER_TYPE
15270 : 644 : && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
15271 : 19 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15272 : : else
15273 : 625 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15274 : 644 : OMP_CLAUSE_CHAIN (nc) = chain;
15275 : 644 : OMP_CLAUSE_CHAIN (clause) = nc;
15276 : 644 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15277 : 644 : gimplify_omp_ctxp = ctx->outer_context;
15278 : 644 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
15279 : : pre_p, NULL, is_gimple_val, fb_rvalue);
15280 : 644 : gimplify_omp_ctxp = ctx;
15281 : 644 : }
15282 : 17403 : else if (code == OMP_CLAUSE_MAP)
15283 : : {
15284 : 17403 : int kind;
15285 : : /* Not all combinations of these GOVD_MAP flags are actually valid. */
15286 : 17403 : switch (flags & (GOVD_MAP_TO_ONLY
15287 : : | GOVD_MAP_FORCE
15288 : : | GOVD_MAP_FORCE_PRESENT
15289 : : | GOVD_MAP_ALLOC_ONLY
15290 : : | GOVD_MAP_FROM_ONLY))
15291 : : {
15292 : : case 0:
15293 : : kind = GOMP_MAP_TOFROM;
15294 : : break;
15295 : 1062 : case GOVD_MAP_FORCE:
15296 : 1062 : kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
15297 : 1062 : break;
15298 : 868 : case GOVD_MAP_TO_ONLY:
15299 : 868 : kind = GOMP_MAP_TO;
15300 : 868 : break;
15301 : 16 : case GOVD_MAP_FROM_ONLY:
15302 : 16 : kind = GOMP_MAP_FROM;
15303 : 16 : break;
15304 : 37 : case GOVD_MAP_ALLOC_ONLY:
15305 : 37 : kind = GOMP_MAP_ALLOC;
15306 : 37 : break;
15307 : 0 : case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
15308 : 0 : kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
15309 : 0 : break;
15310 : : case GOVD_MAP_FORCE_PRESENT:
15311 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
15312 : : break;
15313 : : case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
15314 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
15315 : : break;
15316 : 0 : default:
15317 : 0 : gcc_unreachable ();
15318 : : }
15319 : 17403 : OMP_CLAUSE_SET_MAP_KIND (clause, kind);
15320 : : /* Setting of the implicit flag for the runtime is currently disabled for
15321 : : OpenACC. */
15322 : 17403 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
15323 : 9838 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
15324 : 17403 : if (DECL_SIZE (decl)
15325 : 17403 : && !poly_int_tree_p (DECL_SIZE (decl)))
15326 : : {
15327 : 535 : tree decl2 = DECL_VALUE_EXPR (decl);
15328 : 535 : gcc_assert (INDIRECT_REF_P (decl2));
15329 : 535 : decl2 = TREE_OPERAND (decl2, 0);
15330 : 535 : gcc_assert (DECL_P (decl2));
15331 : 535 : tree mem = build_simple_mem_ref (decl2);
15332 : 535 : OMP_CLAUSE_DECL (clause) = mem;
15333 : 535 : OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15334 : 535 : if (gimplify_omp_ctxp->outer_context)
15335 : : {
15336 : 446 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15337 : 446 : omp_notice_variable (ctx, decl2, true);
15338 : 446 : omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
15339 : : }
15340 : 535 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15341 : : OMP_CLAUSE_MAP);
15342 : 535 : OMP_CLAUSE_DECL (nc) = decl;
15343 : 535 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15344 : 535 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
15345 : 535 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15346 : : else
15347 : 0 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15348 : 535 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15349 : 535 : OMP_CLAUSE_CHAIN (clause) = nc;
15350 : : }
15351 : 16868 : else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
15352 : 16868 : && omp_privatize_by_reference (decl))
15353 : : {
15354 : 29 : OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
15355 : 29 : OMP_CLAUSE_SIZE (clause)
15356 : 29 : = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
15357 : 29 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15358 : 29 : gimplify_omp_ctxp = ctx->outer_context;
15359 : 29 : gimplify_expr (&OMP_CLAUSE_SIZE (clause),
15360 : : pre_p, NULL, is_gimple_val, fb_rvalue);
15361 : 29 : gimplify_omp_ctxp = ctx;
15362 : 29 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15363 : : OMP_CLAUSE_MAP);
15364 : 29 : OMP_CLAUSE_DECL (nc) = decl;
15365 : 29 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15366 : 29 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
15367 : 29 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15368 : 29 : OMP_CLAUSE_CHAIN (clause) = nc;
15369 : : }
15370 : : else
15371 : 16839 : OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
15372 : : }
15373 : 149143 : if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
15374 : : {
15375 : 690 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
15376 : 690 : OMP_CLAUSE_DECL (nc) = decl;
15377 : 690 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
15378 : 690 : OMP_CLAUSE_CHAIN (nc) = chain;
15379 : 690 : OMP_CLAUSE_CHAIN (clause) = nc;
15380 : 690 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15381 : 690 : gimplify_omp_ctxp = ctx->outer_context;
15382 : 690 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
15383 : 690 : (ctx->region_type & ORT_ACC) != 0);
15384 : 690 : gimplify_omp_ctxp = ctx;
15385 : : }
15386 : 149143 : *list_p = clause;
15387 : 149143 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15388 : 149143 : gimplify_omp_ctxp = ctx->outer_context;
15389 : : /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
15390 : : in simd. Those are only added for the local vars inside of simd body
15391 : : and they don't need to be e.g. default constructible. */
15392 : 149143 : if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
15393 : 141122 : lang_hooks.decls.omp_finish_clause (clause, pre_p,
15394 : 141122 : (ctx->region_type & ORT_ACC) != 0);
15395 : 149143 : if (gimplify_omp_ctxp)
15396 : 184139 : for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
15397 : 93484 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
15398 : 93484 : && DECL_P (OMP_CLAUSE_SIZE (clause)))
15399 : 1243 : omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
15400 : : true);
15401 : 149143 : gimplify_omp_ctxp = ctx;
15402 : 149143 : return 0;
15403 : : }
15404 : :
15405 : : static void
15406 : 128012 : gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
15407 : : enum tree_code code,
15408 : : gimple_seq *loops_seq_p = NULL)
15409 : : {
15410 : 128012 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15411 : 128012 : tree *orig_list_p = list_p;
15412 : 128012 : tree c, decl;
15413 : 128012 : bool has_inscan_reductions = false;
15414 : :
15415 : 128012 : if (body)
15416 : : {
15417 : : struct gimplify_omp_ctx *octx;
15418 : 223367 : for (octx = ctx; octx; octx = octx->outer_context)
15419 : 172959 : if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
15420 : : break;
15421 : 110787 : if (octx)
15422 : : {
15423 : 60379 : struct walk_stmt_info wi;
15424 : 60379 : memset (&wi, 0, sizeof (wi));
15425 : 60379 : walk_gimple_seq (body, omp_find_stores_stmt,
15426 : : omp_find_stores_op, &wi);
15427 : : }
15428 : : }
15429 : :
15430 : 128012 : if (ctx->add_safelen1)
15431 : : {
15432 : : /* If there are VLAs in the body of simd loop, prevent
15433 : : vectorization. */
15434 : 2 : gcc_assert (ctx->region_type == ORT_SIMD);
15435 : 2 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
15436 : 2 : OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
15437 : 2 : OMP_CLAUSE_CHAIN (c) = *list_p;
15438 : 2 : *list_p = c;
15439 : 2 : list_p = &OMP_CLAUSE_CHAIN (c);
15440 : : }
15441 : :
15442 : 128012 : if (ctx->region_type == ORT_WORKSHARE
15443 : 39293 : && ctx->outer_context
15444 : 27895 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
15445 : : {
15446 : 24952 : for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
15447 : 12585 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15448 : 12585 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
15449 : : {
15450 : 125 : decl = OMP_CLAUSE_DECL (c);
15451 : 125 : splay_tree_node n
15452 : 125 : = splay_tree_lookup (ctx->outer_context->variables,
15453 : : (splay_tree_key) decl);
15454 : 125 : gcc_checking_assert (!splay_tree_lookup (ctx->variables,
15455 : : (splay_tree_key) decl));
15456 : 125 : omp_add_variable (ctx, decl, n->value);
15457 : 125 : tree c2 = copy_node (c);
15458 : 125 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15459 : 125 : *list_p = c2;
15460 : 125 : if ((n->value & GOVD_FIRSTPRIVATE) == 0)
15461 : 103 : continue;
15462 : 22 : c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15463 : : OMP_CLAUSE_FIRSTPRIVATE);
15464 : 22 : OMP_CLAUSE_DECL (c2) = decl;
15465 : 22 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15466 : 22 : *list_p = c2;
15467 : : }
15468 : : }
15469 : :
15470 : 128012 : if (code == OMP_TARGET
15471 : 128012 : || code == OMP_TARGET_DATA
15472 : 128012 : || code == OMP_TARGET_ENTER_DATA
15473 : 112791 : || code == OMP_TARGET_EXIT_DATA)
15474 : : {
15475 : 15869 : tree mapper_clauses = NULL_TREE;
15476 : 15869 : instantiate_mapper_info im_info;
15477 : :
15478 : 15869 : im_info.mapper_clauses_p = &mapper_clauses;
15479 : 15869 : im_info.omp_ctx = ctx;
15480 : 15869 : im_info.pre_p = pre_p;
15481 : :
15482 : 15869 : splay_tree_foreach (ctx->variables,
15483 : : omp_instantiate_implicit_mappers,
15484 : : (void *) &im_info);
15485 : :
15486 : 15869 : if (mapper_clauses)
15487 : : {
15488 : 45 : mapper_clauses
15489 : 45 : = lang_hooks.decls.omp_finish_mapper_clauses (mapper_clauses);
15490 : :
15491 : : /* Stick the implicitly-expanded mapper clauses at the end of the
15492 : : clause list. */
15493 : 45 : tree *tail = list_p;
15494 : 139 : while (*tail)
15495 : 94 : tail = &OMP_CLAUSE_CHAIN (*tail);
15496 : 45 : *tail = mapper_clauses;
15497 : : }
15498 : :
15499 : 15869 : vec<omp_mapping_group> *groups;
15500 : 15869 : groups = omp_gather_mapping_groups (list_p);
15501 : 15869 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
15502 : :
15503 : 15869 : if (groups)
15504 : : {
15505 : 7701 : grpmap = omp_index_mapping_groups (groups);
15506 : :
15507 : 7701 : omp_resolve_clause_dependencies (code, groups, grpmap);
15508 : 7701 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15509 : : &grpmap, list_p);
15510 : :
15511 : 7701 : omp_mapping_group *outlist = NULL;
15512 : :
15513 : 15402 : delete grpmap;
15514 : 7701 : delete groups;
15515 : :
15516 : : /* Rebuild now we have struct sibling lists. */
15517 : 7701 : groups = omp_gather_mapping_groups (list_p);
15518 : 7701 : grpmap = omp_index_mapping_groups (groups);
15519 : :
15520 : 7701 : bool enter_exit = (code == OMP_TARGET_ENTER_DATA
15521 : 7701 : || code == OMP_TARGET_EXIT_DATA);
15522 : :
15523 : 7701 : outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
15524 : 7701 : outlist = omp_segregate_mapping_groups (outlist);
15525 : 7701 : list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
15526 : :
15527 : 7701 : delete grpmap;
15528 : 7701 : delete groups;
15529 : : }
15530 : 15869 : }
15531 : 112143 : else if (ctx->region_type & ORT_ACC)
15532 : : {
15533 : 29931 : vec<omp_mapping_group> *groups;
15534 : 29931 : groups = omp_gather_mapping_groups (list_p);
15535 : 29931 : if (groups)
15536 : : {
15537 : 9453 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
15538 : 9453 : grpmap = omp_index_mapping_groups (groups);
15539 : :
15540 : 9453 : oacc_resolve_clause_dependencies (groups, grpmap);
15541 : 9453 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15542 : : &grpmap, list_p);
15543 : :
15544 : 9453 : delete groups;
15545 : 18906 : delete grpmap;
15546 : : }
15547 : : }
15548 : :
15549 : 128012 : tree attach_list = NULL_TREE;
15550 : 128012 : tree *attach_tail = &attach_list;
15551 : :
15552 : 128012 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
15553 : :
15554 : 371257 : while ((c = *list_p) != NULL)
15555 : : {
15556 : 243245 : splay_tree_node n;
15557 : 243245 : bool remove = false;
15558 : 243245 : bool move_attach = false;
15559 : :
15560 : 283448 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
15561 : : grp_end = NULL_TREE;
15562 : :
15563 : 243245 : switch (OMP_CLAUSE_CODE (c))
15564 : : {
15565 : 7850 : case OMP_CLAUSE_FIRSTPRIVATE:
15566 : 7850 : if ((ctx->region_type & ORT_TARGET)
15567 : 1836 : && (ctx->region_type & ORT_ACC) == 0
15568 : 9080 : && TYPE_ATOMIC (strip_array_types
15569 : : (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
15570 : : {
15571 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15572 : : "%<_Atomic%> %qD in %<firstprivate%> clause on "
15573 : 2 : "%<target%> construct", OMP_CLAUSE_DECL (c));
15574 : 2 : remove = true;
15575 : 2 : break;
15576 : : }
15577 : 7848 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
15578 : : {
15579 : 380 : decl = OMP_CLAUSE_DECL (c);
15580 : 380 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15581 : 380 : if ((n->value & GOVD_MAP) != 0)
15582 : : {
15583 : : remove = true;
15584 : : break;
15585 : : }
15586 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
15587 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
15588 : : }
15589 : : /* FALLTHRU */
15590 : 36510 : case OMP_CLAUSE_PRIVATE:
15591 : 36510 : case OMP_CLAUSE_SHARED:
15592 : 36510 : case OMP_CLAUSE_LINEAR:
15593 : 36510 : decl = OMP_CLAUSE_DECL (c);
15594 : 36510 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15595 : 36510 : remove = !(n->value & GOVD_SEEN);
15596 : 36510 : if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
15597 : 48 : && code == OMP_PARALLEL
15598 : 36532 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15599 : : remove = true;
15600 : 36488 : if (! remove)
15601 : : {
15602 : 31916 : bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
15603 : 31916 : if ((n->value & GOVD_DEBUG_PRIVATE)
15604 : 31916 : || lang_hooks.decls.omp_private_debug_clause (decl, shared))
15605 : : {
15606 : 83 : gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
15607 : : || ((n->value & GOVD_DATA_SHARE_CLASS)
15608 : : == GOVD_SHARED));
15609 : 83 : OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
15610 : 83 : OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
15611 : : }
15612 : 31916 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15613 : 4129 : && ctx->has_depend
15614 : 32474 : && DECL_P (decl))
15615 : 558 : n->value |= GOVD_WRITTEN;
15616 : 31916 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15617 : 4129 : && (n->value & GOVD_WRITTEN) == 0
15618 : 3018 : && DECL_P (decl)
15619 : 34934 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15620 : 526 : OMP_CLAUSE_SHARED_READONLY (c) = 1;
15621 : 31390 : else if (DECL_P (decl)
15622 : 31390 : && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15623 : 3603 : && (n->value & GOVD_WRITTEN) != 0)
15624 : 30279 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15625 : 9024 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
15626 : 38420 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15627 : 5726 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15628 : : }
15629 : : else
15630 : 4594 : n->value &= ~GOVD_EXPLICIT;
15631 : : break;
15632 : :
15633 : 12999 : case OMP_CLAUSE_LASTPRIVATE:
15634 : : /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
15635 : : accurately reflect the presence of a FIRSTPRIVATE clause. */
15636 : 12999 : decl = OMP_CLAUSE_DECL (c);
15637 : 12999 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15638 : 12999 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
15639 : 12999 : = (n->value & GOVD_FIRSTPRIVATE) != 0;
15640 : 12999 : if (code == OMP_DISTRIBUTE
15641 : 12999 : && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
15642 : : {
15643 : 4 : remove = true;
15644 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15645 : : "same variable used in %<firstprivate%> and "
15646 : : "%<lastprivate%> clauses on %<distribute%> "
15647 : : "construct");
15648 : : }
15649 : 12999 : if (!remove
15650 : 12995 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15651 : 12995 : && DECL_P (decl)
15652 : 12995 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15653 : 11402 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15654 : 12999 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
15655 : : remove = true;
15656 : : break;
15657 : :
15658 : 824 : case OMP_CLAUSE_ALIGNED:
15659 : 824 : decl = OMP_CLAUSE_DECL (c);
15660 : 824 : if (!is_global_var (decl))
15661 : : {
15662 : 738 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15663 : 738 : remove = n == NULL || !(n->value & GOVD_SEEN);
15664 : 88 : if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15665 : : {
15666 : 88 : struct gimplify_omp_ctx *octx;
15667 : 88 : if (n != NULL
15668 : 88 : && (n->value & (GOVD_DATA_SHARE_CLASS
15669 : : & ~GOVD_FIRSTPRIVATE)))
15670 : : remove = true;
15671 : : else
15672 : 100 : for (octx = ctx->outer_context; octx;
15673 : 12 : octx = octx->outer_context)
15674 : : {
15675 : 24 : n = splay_tree_lookup (octx->variables,
15676 : : (splay_tree_key) decl);
15677 : 24 : if (n == NULL)
15678 : 12 : continue;
15679 : 12 : if (n->value & GOVD_LOCAL)
15680 : : break;
15681 : : /* We have to avoid assigning a shared variable
15682 : : to itself when trying to add
15683 : : __builtin_assume_aligned. */
15684 : 12 : if (n->value & GOVD_SHARED)
15685 : : {
15686 : : remove = true;
15687 : : break;
15688 : : }
15689 : : }
15690 : : }
15691 : : }
15692 : 86 : else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
15693 : : {
15694 : 83 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15695 : 83 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
15696 : : remove = true;
15697 : : }
15698 : : break;
15699 : :
15700 : 538 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
15701 : 538 : decl = OMP_CLAUSE_DECL (c);
15702 : 538 : while (INDIRECT_REF_P (decl)
15703 : 587 : || TREE_CODE (decl) == ARRAY_REF)
15704 : 49 : decl = TREE_OPERAND (decl, 0);
15705 : 538 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15706 : 538 : remove = n == NULL || !(n->value & GOVD_SEEN);
15707 : : break;
15708 : :
15709 : 898 : case OMP_CLAUSE_IS_DEVICE_PTR:
15710 : 898 : case OMP_CLAUSE_NONTEMPORAL:
15711 : 898 : decl = OMP_CLAUSE_DECL (c);
15712 : 898 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15713 : 898 : remove = n == NULL || !(n->value & GOVD_SEEN);
15714 : : break;
15715 : :
15716 : 53480 : case OMP_CLAUSE_MAP:
15717 : 53480 : decl = OMP_CLAUSE_DECL (c);
15718 : 53480 : if (!grp_end)
15719 : : {
15720 : 30129 : grp_start_p = list_p;
15721 : 30129 : grp_end = *omp_group_last (grp_start_p);
15722 : : }
15723 : 53480 : switch (OMP_CLAUSE_MAP_KIND (c))
15724 : : {
15725 : 123 : case GOMP_MAP_PRESENT_ALLOC:
15726 : 123 : case GOMP_MAP_PRESENT_TO:
15727 : 123 : case GOMP_MAP_PRESENT_FROM:
15728 : 123 : case GOMP_MAP_PRESENT_TOFROM:
15729 : 123 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
15730 : 123 : break;
15731 : : default:
15732 : : break;
15733 : : }
15734 : 53480 : switch (code)
15735 : : {
15736 : 4569 : case OACC_DATA:
15737 : 4569 : if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
15738 : : break;
15739 : : /* Fallthrough. */
15740 : 17109 : case OACC_HOST_DATA:
15741 : 17109 : case OACC_ENTER_DATA:
15742 : 17109 : case OACC_EXIT_DATA:
15743 : 17109 : case OMP_TARGET_DATA:
15744 : 17109 : case OMP_TARGET_ENTER_DATA:
15745 : 17109 : case OMP_TARGET_EXIT_DATA:
15746 : 17109 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15747 : 17109 : || (OMP_CLAUSE_MAP_KIND (c)
15748 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15749 : : /* For target {,enter ,exit }data only the array slice is
15750 : : mapped, but not the pointer to it. */
15751 : : remove = true;
15752 : 17109 : if (code == OMP_TARGET_EXIT_DATA
15753 : 17109 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
15754 : 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
15755 : : remove = true;
15756 : : break;
15757 : : case OMP_TARGET:
15758 : : break;
15759 : : default:
15760 : : break;
15761 : : }
15762 : 17107 : if (remove)
15763 : : break;
15764 : 53145 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15765 : : {
15766 : : /* Sanity check: attach/detach map kinds use the size as a bias,
15767 : : and it's never right to use the decl size for such
15768 : : mappings. */
15769 : 16845 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15770 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15771 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
15772 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15773 : : && (OMP_CLAUSE_MAP_KIND (c)
15774 : : != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
15775 : 20626 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
15776 : 3781 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
15777 : : }
15778 : 53145 : gimplify_omp_ctxp = ctx->outer_context;
15779 : 53145 : gimple_seq *seq_p;
15780 : 53145 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
15781 : 53145 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
15782 : : is_gimple_val, fb_rvalue) == GS_ERROR)
15783 : : {
15784 : 0 : gimplify_omp_ctxp = ctx;
15785 : 0 : remove = true;
15786 : 0 : goto end_adjust_omp_map_clause;
15787 : : }
15788 : 53145 : else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15789 : 50355 : || (OMP_CLAUSE_MAP_KIND (c)
15790 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15791 : 49946 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15792 : 59164 : && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
15793 : : {
15794 : 842 : OMP_CLAUSE_SIZE (c)
15795 : 842 : = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), seq_p, NULL,
15796 : : false);
15797 : 842 : if ((ctx->region_type & ORT_TARGET) != 0)
15798 : 643 : omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
15799 : : GOVD_FIRSTPRIVATE | GOVD_SEEN);
15800 : : }
15801 : 53145 : gimplify_omp_ctxp = ctx;
15802 : : /* Data clauses associated with reductions must be
15803 : : compatible with present_or_copy. Warn and adjust the clause
15804 : : if that is not the case. */
15805 : 53145 : if (ctx->region_type == ORT_ACC_PARALLEL
15806 : 44165 : || ctx->region_type == ORT_ACC_SERIAL)
15807 : : {
15808 : 9536 : tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
15809 : 9536 : n = NULL;
15810 : :
15811 : 9536 : if (DECL_P (t))
15812 : 7771 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
15813 : :
15814 : 7771 : if (n && (n->value & GOVD_REDUCTION))
15815 : : {
15816 : 809 : enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
15817 : :
15818 : 809 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15819 : 809 : if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
15820 : 159 : && kind != GOMP_MAP_FORCE_PRESENT
15821 : 159 : && kind != GOMP_MAP_POINTER)
15822 : : {
15823 : 120 : warning_at (OMP_CLAUSE_LOCATION (c), 0,
15824 : : "incompatible data clause with reduction "
15825 : : "on %qE; promoting to %<present_or_copy%>",
15826 : 120 : DECL_NAME (t));
15827 : 120 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
15828 : : }
15829 : : }
15830 : : }
15831 : 53145 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15832 : 50548 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15833 : 53253 : && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
15834 : : {
15835 : 412 : remove = true;
15836 : 412 : goto end_adjust_omp_map_clause;
15837 : : }
15838 : : /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
15839 : : a variable captured in a lambda closure), look through that now
15840 : : before the DECL_P check below. (A code other than COMPONENT_REF,
15841 : : i.e. INDIRECT_REF, will be a VLA/variable-length array
15842 : : section. A global var may be a variable in a common block. We
15843 : : don't want to do this here for either of those.) */
15844 : 52733 : if ((ctx->region_type & ORT_ACC) == 0
15845 : 26540 : && DECL_P (decl)
15846 : 11709 : && !is_global_var (decl)
15847 : 10418 : && DECL_HAS_VALUE_EXPR_P (decl)
15848 : 52923 : && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
15849 : 0 : decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
15850 : 52733 : if (TREE_CODE (decl) == TARGET_EXPR)
15851 : : {
15852 : 0 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
15853 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
15854 : 25250 : remove = true;
15855 : : }
15856 : 52733 : else if (!DECL_P (decl))
15857 : : {
15858 : 27483 : if ((ctx->region_type & ORT_TARGET) != 0
15859 : 27483 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15860 : : {
15861 : 24 : if (INDIRECT_REF_P (decl)
15862 : 0 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15863 : 24 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15864 : : == REFERENCE_TYPE))
15865 : 0 : decl = TREE_OPERAND (decl, 0);
15866 : 24 : if (TREE_CODE (decl) == COMPONENT_REF)
15867 : : {
15868 : 0 : while (TREE_CODE (decl) == COMPONENT_REF)
15869 : 0 : decl = TREE_OPERAND (decl, 0);
15870 : 0 : if (DECL_P (decl))
15871 : : {
15872 : 0 : n = splay_tree_lookup (ctx->variables,
15873 : : (splay_tree_key) decl);
15874 : 0 : if (!(n->value & GOVD_SEEN))
15875 : 27483 : remove = true;
15876 : : }
15877 : : }
15878 : : }
15879 : :
15880 : 27483 : tree d = decl, *pd;
15881 : 27483 : if (TREE_CODE (d) == ARRAY_REF)
15882 : : {
15883 : 5594 : while (TREE_CODE (d) == ARRAY_REF)
15884 : 2857 : d = TREE_OPERAND (d, 0);
15885 : 2737 : if (TREE_CODE (d) == COMPONENT_REF
15886 : 2737 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
15887 : : decl = d;
15888 : : }
15889 : 27483 : pd = &OMP_CLAUSE_DECL (c);
15890 : 27483 : if (d == decl
15891 : 25103 : && TREE_CODE (decl) == INDIRECT_REF
15892 : 14858 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15893 : 1470 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15894 : : == REFERENCE_TYPE)
15895 : 28283 : && (OMP_CLAUSE_MAP_KIND (c)
15896 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
15897 : : {
15898 : 796 : pd = &TREE_OPERAND (decl, 0);
15899 : 796 : decl = TREE_OPERAND (decl, 0);
15900 : : }
15901 : :
15902 : 27483 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15903 : 2591 : switch (code)
15904 : : {
15905 : 427 : case OACC_ENTER_DATA:
15906 : 427 : case OACC_EXIT_DATA:
15907 : 427 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
15908 : : == ARRAY_TYPE)
15909 : : remove = true;
15910 : 417 : else if (code == OACC_ENTER_DATA)
15911 : 263 : goto change_to_attach;
15912 : : /* Fallthrough. */
15913 : 347 : case OMP_TARGET_EXIT_DATA:
15914 : 347 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
15915 : 347 : break;
15916 : 71 : case OACC_UPDATE:
15917 : : /* An "attach/detach" operation on an update directive
15918 : : should behave as a GOMP_MAP_ALWAYS_POINTER. Note that
15919 : : both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
15920 : : kinds depend on the previous mapping (for non-TARGET
15921 : : regions). */
15922 : 71 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
15923 : 71 : break;
15924 : 2173 : default:
15925 : 2173 : change_to_attach:
15926 : 2173 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
15927 : 2173 : if ((ctx->region_type & ORT_TARGET) != 0)
15928 : 1769 : move_attach = true;
15929 : : }
15930 : 24892 : else if ((ctx->region_type & ORT_TARGET) != 0
15931 : 24892 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15932 : 11485 : || (OMP_CLAUSE_MAP_KIND (c)
15933 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15934 : : move_attach = true;
15935 : :
15936 : : /* If we have e.g. map(struct: *var), don't gimplify the
15937 : : argument since omp-low.cc wants to see the decl itself. */
15938 : 27483 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
15939 : 1135 : goto end_adjust_omp_map_clause;
15940 : :
15941 : : /* We've already partly gimplified this in
15942 : : gimplify_scan_omp_clauses. Don't do any more. */
15943 : 26348 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
15944 : 88 : goto end_adjust_omp_map_clause;
15945 : :
15946 : 26260 : gimplify_omp_ctxp = ctx->outer_context;
15947 : 26260 : if (gimplify_expr (pd, seq_p, NULL, is_gimple_lvalue,
15948 : : fb_lvalue) == GS_ERROR)
15949 : 0 : remove = true;
15950 : 26260 : gimplify_omp_ctxp = ctx;
15951 : 26260 : goto end_adjust_omp_map_clause;
15952 : : }
15953 : :
15954 : 25250 : if ((code == OMP_TARGET
15955 : : || code == OMP_TARGET_DATA
15956 : : || code == OMP_TARGET_ENTER_DATA
15957 : 14351 : || code == OMP_TARGET_EXIT_DATA)
15958 : 25798 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15959 : : {
15960 : 229 : bool firstprivatize = false;
15961 : :
15962 : 234 : for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
15963 : 5 : octx = octx->outer_context)
15964 : : {
15965 : 6 : splay_tree_node n
15966 : 12 : = splay_tree_lookup (octx->variables,
15967 : 6 : (splay_tree_key) OMP_CLAUSE_DECL (c));
15968 : : /* If this is contained in an outer OpenMP region as a
15969 : : firstprivate value, remove the attach/detach. */
15970 : 6 : if (n && (n->value & GOVD_FIRSTPRIVATE))
15971 : : {
15972 : : firstprivatize = true;
15973 : : break;
15974 : : }
15975 : : }
15976 : :
15977 : 229 : enum gomp_map_kind map_kind;
15978 : 229 : if (firstprivatize)
15979 : : map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
15980 : 228 : else if (code == OMP_TARGET_EXIT_DATA)
15981 : : map_kind = GOMP_MAP_DETACH;
15982 : : else
15983 : 180 : map_kind = GOMP_MAP_ATTACH;
15984 : 229 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15985 : : }
15986 : 25021 : else if ((ctx->region_type & ORT_ACC) != 0
15987 : 25021 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15988 : : {
15989 : 0 : enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
15990 : 0 : ? GOMP_MAP_DETACH
15991 : : : GOMP_MAP_ATTACH);
15992 : 0 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15993 : : }
15994 : :
15995 : 25250 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15996 : 25250 : if ((ctx->region_type & ORT_TARGET) != 0
15997 : 15627 : && !(n->value & GOVD_SEEN)
15998 : 1415 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
15999 : 26422 : && (!is_global_var (decl)
16000 : 75 : || !lookup_attribute ("omp declare target link",
16001 : 75 : DECL_ATTRIBUTES (decl))))
16002 : : {
16003 : 1160 : remove = true;
16004 : : /* For struct element mapping, if struct is never referenced
16005 : : in target block and none of the mapping has always modifier,
16006 : : remove all the struct element mappings, which immediately
16007 : : follow the GOMP_MAP_STRUCT map clause. */
16008 : 1160 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
16009 : 1160 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
16010 : : {
16011 : 28 : HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
16012 : 76 : while (cnt--)
16013 : 48 : OMP_CLAUSE_CHAIN (c)
16014 : 48 : = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
16015 : : }
16016 : : }
16017 : 24090 : else if (DECL_SIZE (decl)
16018 : 24066 : && !poly_int_tree_p (DECL_SIZE (decl))
16019 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
16020 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
16021 : 24141 : && (OMP_CLAUSE_MAP_KIND (c)
16022 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
16023 : : {
16024 : : /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
16025 : : for these, TREE_CODE (DECL_SIZE (decl)) will always be
16026 : : INTEGER_CST. */
16027 : 51 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
16028 : :
16029 : 51 : tree decl2 = DECL_VALUE_EXPR (decl);
16030 : 51 : gcc_assert (INDIRECT_REF_P (decl2));
16031 : 51 : decl2 = TREE_OPERAND (decl2, 0);
16032 : 51 : gcc_assert (DECL_P (decl2));
16033 : 51 : tree mem = build_simple_mem_ref (decl2);
16034 : 51 : OMP_CLAUSE_DECL (c) = mem;
16035 : 51 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16036 : 51 : if (ctx->outer_context)
16037 : : {
16038 : 15 : omp_notice_variable (ctx->outer_context, decl2, true);
16039 : 30 : omp_notice_variable (ctx->outer_context,
16040 : 15 : OMP_CLAUSE_SIZE (c), true);
16041 : : }
16042 : 51 : if (((ctx->region_type & ORT_TARGET) != 0
16043 : 24 : || !ctx->target_firstprivatize_array_bases)
16044 : 34 : && ((n->value & GOVD_SEEN) == 0
16045 : 30 : || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
16046 : : {
16047 : 34 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16048 : : OMP_CLAUSE_MAP);
16049 : 34 : OMP_CLAUSE_DECL (nc) = decl;
16050 : 34 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
16051 : 34 : if (ctx->target_firstprivatize_array_bases)
16052 : 27 : OMP_CLAUSE_SET_MAP_KIND (nc,
16053 : : GOMP_MAP_FIRSTPRIVATE_POINTER);
16054 : : else
16055 : 7 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
16056 : 34 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
16057 : 34 : OMP_CLAUSE_CHAIN (c) = nc;
16058 : 34 : c = nc;
16059 : : }
16060 : : }
16061 : : else
16062 : : {
16063 : 24039 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16064 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16065 : 24039 : gcc_assert ((n->value & GOVD_SEEN) == 0
16066 : : || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16067 : : == 0));
16068 : : }
16069 : :
16070 : : /* If we have a target region, we can push all the attaches to the
16071 : : end of the list (we may have standalone "attach" operations
16072 : : synthesized for GOMP_MAP_STRUCT nodes that must be processed after
16073 : : the attachment point AND the pointed-to block have been mapped).
16074 : : If we have something else, e.g. "enter data", we need to keep
16075 : : "attach" nodes together with the previous node they attach to so
16076 : : that separate "exit data" operations work properly (see
16077 : : libgomp/target.c). */
16078 : 25250 : if ((ctx->region_type & ORT_TARGET) != 0
16079 : 25250 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16080 : 15511 : || (OMP_CLAUSE_MAP_KIND (c)
16081 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
16082 : : move_attach = true;
16083 : :
16084 : 53145 : end_adjust_omp_map_clause:
16085 : 53145 : exit_omp_iterator_loop_context (c);
16086 : 53145 : break;
16087 : :
16088 : 8224 : case OMP_CLAUSE_TO:
16089 : 8224 : case OMP_CLAUSE_FROM:
16090 : 8224 : case OMP_CLAUSE__CACHE_:
16091 : 8224 : decl = OMP_CLAUSE_DECL (c);
16092 : 8224 : if (!DECL_P (decl))
16093 : : break;
16094 : 6187 : if (DECL_SIZE (decl)
16095 : 6187 : && !poly_int_tree_p (DECL_SIZE (decl)))
16096 : : {
16097 : 4 : tree decl2 = DECL_VALUE_EXPR (decl);
16098 : 4 : gcc_assert (INDIRECT_REF_P (decl2));
16099 : 4 : decl2 = TREE_OPERAND (decl2, 0);
16100 : 4 : gcc_assert (DECL_P (decl2));
16101 : 4 : tree mem = build_simple_mem_ref (decl2);
16102 : 4 : OMP_CLAUSE_DECL (c) = mem;
16103 : 4 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16104 : 4 : if (ctx->outer_context)
16105 : : {
16106 : 4 : omp_notice_variable (ctx->outer_context, decl2, true);
16107 : 4 : omp_notice_variable (ctx->outer_context,
16108 : 4 : OMP_CLAUSE_SIZE (c), true);
16109 : : }
16110 : : }
16111 : 6183 : else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16112 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16113 : : break;
16114 : :
16115 : 15289 : case OMP_CLAUSE_REDUCTION:
16116 : 15289 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
16117 : : {
16118 : 811 : decl = OMP_CLAUSE_DECL (c);
16119 : 811 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16120 : 811 : if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
16121 : : {
16122 : 69 : remove = true;
16123 : 69 : error_at (OMP_CLAUSE_LOCATION (c),
16124 : : "%qD specified in %<inscan%> %<reduction%> clause "
16125 : : "but not in %<scan%> directive clause", decl);
16126 : 69 : break;
16127 : : }
16128 : : has_inscan_reductions = true;
16129 : : }
16130 : : /* FALLTHRU */
16131 : 17820 : case OMP_CLAUSE_IN_REDUCTION:
16132 : 17820 : case OMP_CLAUSE_TASK_REDUCTION:
16133 : 17820 : decl = OMP_CLAUSE_DECL (c);
16134 : : /* OpenACC reductions need a present_or_copy data clause.
16135 : : Add one if necessary. Emit error when the reduction is private. */
16136 : 17820 : if (ctx->region_type == ORT_ACC_PARALLEL
16137 : 17288 : || ctx->region_type == ORT_ACC_SERIAL)
16138 : : {
16139 : 803 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16140 : 803 : if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16141 : : {
16142 : 13 : remove = true;
16143 : 13 : error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
16144 : 13 : "reduction on %qE", DECL_NAME (decl));
16145 : : }
16146 : 790 : else if ((n->value & GOVD_MAP) == 0)
16147 : : {
16148 : 524 : tree next = OMP_CLAUSE_CHAIN (c);
16149 : 524 : tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
16150 : 524 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
16151 : 524 : OMP_CLAUSE_DECL (nc) = decl;
16152 : 524 : OMP_CLAUSE_CHAIN (c) = nc;
16153 : 524 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
16154 : 524 : (ctx->region_type
16155 : : & ORT_ACC) != 0);
16156 : 550 : while (1)
16157 : : {
16158 : 537 : OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
16159 : 537 : if (OMP_CLAUSE_CHAIN (nc) == NULL)
16160 : : break;
16161 : 13 : nc = OMP_CLAUSE_CHAIN (nc);
16162 : : }
16163 : 524 : OMP_CLAUSE_CHAIN (nc) = next;
16164 : 524 : n->value |= GOVD_MAP;
16165 : : }
16166 : : }
16167 : 17820 : if (DECL_P (decl)
16168 : 17820 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
16169 : 7959 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
16170 : : break;
16171 : :
16172 : 3541 : case OMP_CLAUSE_ALLOCATE:
16173 : 3541 : decl = OMP_CLAUSE_DECL (c);
16174 : 3541 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16175 : 3541 : if (n != NULL && !(n->value & GOVD_SEEN))
16176 : : {
16177 : 1176 : if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
16178 : : != 0
16179 : 1176 : && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
16180 : : remove = true;
16181 : : }
16182 : : if (!remove
16183 : 2365 : && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
16184 : 1153 : && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
16185 : 471 : && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
16186 : 347 : || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
16187 : 287 : || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
16188 : : {
16189 : 202 : tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
16190 : 202 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
16191 : 202 : if (n == NULL)
16192 : : {
16193 : 82 : enum omp_clause_default_kind default_kind
16194 : : = ctx->default_kind;
16195 : 82 : ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
16196 : 82 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16197 : : true);
16198 : 82 : ctx->default_kind = default_kind;
16199 : : }
16200 : : else
16201 : 120 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16202 : : true);
16203 : : }
16204 : : break;
16205 : :
16206 : : case OMP_CLAUSE_COPYIN:
16207 : : case OMP_CLAUSE_COPYPRIVATE:
16208 : : case OMP_CLAUSE_IF:
16209 : : case OMP_CLAUSE_SELF:
16210 : : case OMP_CLAUSE_NUM_THREADS:
16211 : : case OMP_CLAUSE_NUM_TEAMS:
16212 : : case OMP_CLAUSE_THREAD_LIMIT:
16213 : : case OMP_CLAUSE_DIST_SCHEDULE:
16214 : : case OMP_CLAUSE_DEVICE:
16215 : : case OMP_CLAUSE_SCHEDULE:
16216 : : case OMP_CLAUSE_NOWAIT:
16217 : : case OMP_CLAUSE_ORDERED:
16218 : : case OMP_CLAUSE_DEFAULT:
16219 : : case OMP_CLAUSE_UNTIED:
16220 : : case OMP_CLAUSE_COLLAPSE:
16221 : : case OMP_CLAUSE_FINAL:
16222 : : case OMP_CLAUSE_MERGEABLE:
16223 : : case OMP_CLAUSE_PROC_BIND:
16224 : : case OMP_CLAUSE_SAFELEN:
16225 : : case OMP_CLAUSE_SIMDLEN:
16226 : : case OMP_CLAUSE_DEPEND:
16227 : : case OMP_CLAUSE_DOACROSS:
16228 : : case OMP_CLAUSE_PRIORITY:
16229 : : case OMP_CLAUSE_GRAINSIZE:
16230 : : case OMP_CLAUSE_NUM_TASKS:
16231 : : case OMP_CLAUSE_NOGROUP:
16232 : : case OMP_CLAUSE_THREADS:
16233 : : case OMP_CLAUSE_SIMD:
16234 : : case OMP_CLAUSE_FILTER:
16235 : : case OMP_CLAUSE_HINT:
16236 : : case OMP_CLAUSE_DEFAULTMAP:
16237 : : case OMP_CLAUSE_ORDER:
16238 : : case OMP_CLAUSE_BIND:
16239 : : case OMP_CLAUSE_DETACH:
16240 : : case OMP_CLAUSE_USE_DEVICE_PTR:
16241 : : case OMP_CLAUSE_USE_DEVICE_ADDR:
16242 : : case OMP_CLAUSE_ASYNC:
16243 : : case OMP_CLAUSE_WAIT:
16244 : : case OMP_CLAUSE_INDEPENDENT:
16245 : : case OMP_CLAUSE_NUM_GANGS:
16246 : : case OMP_CLAUSE_NUM_WORKERS:
16247 : : case OMP_CLAUSE_VECTOR_LENGTH:
16248 : : case OMP_CLAUSE_GANG:
16249 : : case OMP_CLAUSE_WORKER:
16250 : : case OMP_CLAUSE_VECTOR:
16251 : : case OMP_CLAUSE_AUTO:
16252 : : case OMP_CLAUSE_SEQ:
16253 : : case OMP_CLAUSE_TILE:
16254 : : case OMP_CLAUSE_IF_PRESENT:
16255 : : case OMP_CLAUSE_FINALIZE:
16256 : : case OMP_CLAUSE_INCLUSIVE:
16257 : : case OMP_CLAUSE_EXCLUSIVE:
16258 : : break;
16259 : :
16260 : 0 : case OMP_CLAUSE_NOHOST:
16261 : 0 : default:
16262 : 0 : gcc_unreachable ();
16263 : : }
16264 : :
16265 : 126139 : if (remove)
16266 : 9601 : *list_p = OMP_CLAUSE_CHAIN (c);
16267 : 233644 : else if (move_attach)
16268 : : {
16269 : : /* Remove attach node from here, separate out into its own list. */
16270 : 2313 : *attach_tail = c;
16271 : 2313 : *list_p = OMP_CLAUSE_CHAIN (c);
16272 : 2313 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16273 : 2313 : attach_tail = &OMP_CLAUSE_CHAIN (c);
16274 : : }
16275 : : else
16276 : 231331 : list_p = &OMP_CLAUSE_CHAIN (c);
16277 : : }
16278 : :
16279 : : /* Splice attach nodes at the end of the list. */
16280 : 128012 : if (attach_list)
16281 : : {
16282 : 1101 : *list_p = attach_list;
16283 : 1101 : list_p = attach_tail;
16284 : : }
16285 : :
16286 : : /* Add in any implicit data sharing. */
16287 : 128012 : struct gimplify_adjust_omp_clauses_data data;
16288 : 128012 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
16289 : : {
16290 : : /* OpenMP. Implicit clauses are added at the start of the clause list,
16291 : : but after any non-map clauses. */
16292 : : tree *implicit_add_list_p = orig_list_p;
16293 : 248974 : while (*implicit_add_list_p
16294 : 248974 : && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
16295 : 150893 : implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
16296 : 98081 : data.list_p = implicit_add_list_p;
16297 : : }
16298 : : else
16299 : : /* OpenACC. */
16300 : 29931 : data.list_p = list_p;
16301 : 128012 : data.pre_p = pre_p;
16302 : 128012 : splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
16303 : :
16304 : 128012 : if (has_inscan_reductions)
16305 : 2431 : for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
16306 : 1822 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16307 : 1822 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
16308 : : {
16309 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16310 : : "%<inscan%> %<reduction%> clause used together with "
16311 : : "%<linear%> clause for a variable other than loop "
16312 : : "iterator");
16313 : 5 : break;
16314 : : }
16315 : :
16316 : 128012 : gimplify_omp_ctxp = ctx->outer_context;
16317 : 128012 : delete_omp_context (ctx);
16318 : 128012 : }
16319 : :
16320 : : /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
16321 : : * -1 if run-time evaluation is needed. */
16322 : :
16323 : : int
16324 : 242752 : omp_has_novariants (void)
16325 : : {
16326 : 242752 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16327 : 242752 : if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
16328 : : {
16329 : 3278 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
16330 : 3278 : if (c != NULL_TREE)
16331 : : {
16332 : 287 : if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16333 : : return 1;
16334 : 171 : else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16335 : : return 0;
16336 : : else
16337 : : return -1;
16338 : : }
16339 : : return 0;
16340 : : }
16341 : : return 0;
16342 : : }
16343 : :
16344 : : /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
16345 : : * -1 if run-time evaluation is needed. */
16346 : :
16347 : : static int
16348 : 1891 : omp_has_nocontext (void)
16349 : : {
16350 : 1891 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16351 : 1891 : if (ctx != NULL && ctx->code == OMP_DISPATCH)
16352 : : {
16353 : 1891 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
16354 : 1891 : if (c != NULL_TREE)
16355 : : {
16356 : 256 : if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16357 : : return 1;
16358 : 94 : else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16359 : : return 0;
16360 : : else
16361 : : return -1;
16362 : : }
16363 : : return 0;
16364 : : }
16365 : : return 0;
16366 : : }
16367 : :
16368 : : /* Collect a list of traits for enclosing constructs in the current
16369 : : OpenMP context. The list is in the same format as the trait selector
16370 : : list of construct trait sets built by the front ends.
16371 : :
16372 : : Per the OpenMP specification, the construct trait set includes constructs
16373 : : up to an enclosing "target" construct. If there is no "target" construct,
16374 : : then additional things may be added to the construct trait set (simd for
16375 : : simd clones, additional constructs associated with "declare variant",
16376 : : the target trait for "declare target"); those are not handled here.
16377 : : In particular simd clones are not known during gimplification so
16378 : : matching/scoring of context selectors that might involve them needs
16379 : : to be deferred to the omp_device_lower pass. */
16380 : :
16381 : : tree
16382 : 1990 : omp_get_construct_context (void)
16383 : : {
16384 : 1990 : tree result = NULL_TREE;
16385 : 3947 : for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
16386 : : {
16387 : 2038 : if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
16388 : : == ORT_TARGET)
16389 : 81 : && ctx->code == OMP_TARGET)
16390 : : {
16391 : 81 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
16392 : : NULL_TREE, NULL_TREE, result);
16393 : : /* We're not interested in any outer constructs. */
16394 : 81 : break;
16395 : : }
16396 : 1957 : else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
16397 : 210 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
16398 : : NULL_TREE, NULL_TREE, result);
16399 : 1747 : else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
16400 : 71 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
16401 : : NULL_TREE, NULL_TREE, result);
16402 : 1676 : else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
16403 : 164 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
16404 : : NULL_TREE, NULL_TREE, result);
16405 : 1512 : else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
16406 : 1273 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
16407 : : NULL_TREE, NULL_TREE, result);
16408 : 239 : else if (ctx->region_type == ORT_SIMD
16409 : 32 : && ctx->code == OMP_SIMD
16410 : 271 : && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
16411 : : {
16412 : 32 : tree props = NULL_TREE;
16413 : 32 : tree *last = &props;
16414 : 72 : for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
16415 : 40 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
16416 : 32 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
16417 : 72 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
16418 : : {
16419 : 8 : *last = unshare_expr (c);
16420 : 8 : last = &(OMP_CLAUSE_CHAIN (c));
16421 : : }
16422 : 32 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
16423 : : NULL_TREE, props, result);
16424 : : }
16425 : 207 : else if (ctx->region_type == ORT_WORKSHARE
16426 : 5 : && ctx->code == OMP_LOOP
16427 : 0 : && ctx->outer_context
16428 : 0 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
16429 : 0 : && ctx->outer_context->outer_context
16430 : 0 : && ctx->outer_context->outer_context->code == OMP_LOOP
16431 : 0 : && ctx->outer_context->outer_context->distribute)
16432 : 1957 : ctx = ctx->outer_context->outer_context;
16433 : 1957 : ctx = ctx->outer_context;
16434 : : }
16435 : :
16436 : 1990 : return result;
16437 : : }
16438 : :
16439 : : /* Gimplify OACC_CACHE. */
16440 : :
16441 : : static void
16442 : 708 : gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
16443 : : {
16444 : 708 : tree expr = *expr_p;
16445 : :
16446 : 708 : gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
16447 : : OACC_CACHE);
16448 : 708 : gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
16449 : : OACC_CACHE);
16450 : :
16451 : : /* TODO: Do something sensible with this information. */
16452 : :
16453 : 708 : *expr_p = NULL_TREE;
16454 : 708 : }
16455 : :
16456 : : /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
16457 : : if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
16458 : : kind. The entry kind will replace the one in CLAUSE, while the exit
16459 : : kind will be used in a new omp_clause and returned to the caller. */
16460 : :
16461 : : static tree
16462 : 190 : gimplify_oacc_declare_1 (tree clause)
16463 : : {
16464 : 190 : HOST_WIDE_INT kind, new_op;
16465 : 190 : bool ret = false;
16466 : 190 : tree c = NULL;
16467 : :
16468 : 190 : kind = OMP_CLAUSE_MAP_KIND (clause);
16469 : :
16470 : 190 : switch (kind)
16471 : : {
16472 : : case GOMP_MAP_ALLOC:
16473 : : new_op = GOMP_MAP_RELEASE;
16474 : : ret = true;
16475 : : break;
16476 : :
16477 : 29 : case GOMP_MAP_FROM:
16478 : 29 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
16479 : 29 : new_op = GOMP_MAP_FROM;
16480 : 29 : ret = true;
16481 : 29 : break;
16482 : :
16483 : 40 : case GOMP_MAP_TOFROM:
16484 : 40 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
16485 : 40 : new_op = GOMP_MAP_FROM;
16486 : 40 : ret = true;
16487 : 40 : break;
16488 : :
16489 : : case GOMP_MAP_DEVICE_RESIDENT:
16490 : : case GOMP_MAP_FORCE_DEVICEPTR:
16491 : : case GOMP_MAP_FORCE_PRESENT:
16492 : : case GOMP_MAP_LINK:
16493 : : case GOMP_MAP_POINTER:
16494 : : case GOMP_MAP_TO:
16495 : : break;
16496 : :
16497 : 0 : default:
16498 : 0 : gcc_unreachable ();
16499 : 69 : break;
16500 : : }
16501 : :
16502 : 69 : if (ret)
16503 : : {
16504 : 116 : c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
16505 : 116 : OMP_CLAUSE_SET_MAP_KIND (c, new_op);
16506 : 116 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
16507 : : }
16508 : :
16509 : 190 : return c;
16510 : : }
16511 : :
16512 : : /* Gimplify OACC_DECLARE. */
16513 : :
16514 : : static void
16515 : 254 : gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
16516 : : {
16517 : 254 : tree expr = *expr_p;
16518 : 254 : gomp_target *stmt;
16519 : 254 : tree clauses, t, decl;
16520 : :
16521 : 254 : clauses = OACC_DECLARE_CLAUSES (expr);
16522 : :
16523 : 254 : gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
16524 : 254 : gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
16525 : :
16526 : 516 : for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16527 : : {
16528 : 262 : decl = OMP_CLAUSE_DECL (t);
16529 : :
16530 : 262 : if (TREE_CODE (decl) == MEM_REF)
16531 : 8 : decl = TREE_OPERAND (decl, 0);
16532 : :
16533 : 262 : if (VAR_P (decl) && !is_oacc_declared (decl))
16534 : : {
16535 : 262 : tree attr = get_identifier ("oacc declare target");
16536 : 262 : DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
16537 : 262 : DECL_ATTRIBUTES (decl));
16538 : : }
16539 : :
16540 : 262 : if (VAR_P (decl)
16541 : 262 : && !is_global_var (decl)
16542 : 452 : && DECL_CONTEXT (decl) == current_function_decl)
16543 : : {
16544 : 190 : tree c = gimplify_oacc_declare_1 (t);
16545 : 190 : if (c)
16546 : : {
16547 : 116 : if (oacc_declare_returns == NULL)
16548 : 40 : oacc_declare_returns = new hash_map<tree, tree>;
16549 : :
16550 : 116 : oacc_declare_returns->put (decl, c);
16551 : : }
16552 : : }
16553 : :
16554 : 262 : if (gimplify_omp_ctxp)
16555 : 72 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
16556 : : }
16557 : :
16558 : 254 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
16559 : : clauses);
16560 : :
16561 : 254 : gimplify_seq_add_stmt (pre_p, stmt);
16562 : :
16563 : 254 : *expr_p = NULL_TREE;
16564 : 254 : }
16565 : :
16566 : : /* Gimplify the contents of an OMP_PARALLEL statement. This involves
16567 : : gimplification of the body, as well as scanning the body for used
16568 : : variables. We need to do this scan now, because variable-sized
16569 : : decls will be decomposed during gimplification. */
16570 : :
16571 : : static void
16572 : 18214 : gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
16573 : : {
16574 : 18214 : tree expr = *expr_p;
16575 : 18214 : gimple *g;
16576 : 18214 : gimple_seq body = NULL;
16577 : :
16578 : 36428 : gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
16579 : 18214 : OMP_PARALLEL_COMBINED (expr)
16580 : : ? ORT_COMBINED_PARALLEL
16581 : : : ORT_PARALLEL, OMP_PARALLEL);
16582 : :
16583 : 18214 : push_gimplify_context ();
16584 : :
16585 : 18214 : g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
16586 : 18214 : if (gimple_code (g) == GIMPLE_BIND)
16587 : 18214 : pop_gimplify_context (g);
16588 : : else
16589 : 0 : pop_gimplify_context (NULL);
16590 : :
16591 : 18214 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
16592 : : OMP_PARALLEL);
16593 : :
16594 : 36428 : g = gimple_build_omp_parallel (body,
16595 : 18214 : OMP_PARALLEL_CLAUSES (expr),
16596 : : NULL_TREE, NULL_TREE);
16597 : 18214 : if (OMP_PARALLEL_COMBINED (expr))
16598 : 12554 : gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
16599 : 18214 : gimplify_seq_add_stmt (pre_p, g);
16600 : 18214 : *expr_p = NULL_TREE;
16601 : 18214 : }
16602 : :
16603 : : /* Gimplify the contents of an OMP_TASK statement. This involves
16604 : : gimplification of the body, as well as scanning the body for used
16605 : : variables. We need to do this scan now, because variable-sized
16606 : : decls will be decomposed during gimplification. */
16607 : :
16608 : : static void
16609 : 4014 : gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
16610 : : {
16611 : 4014 : tree expr = *expr_p;
16612 : 4014 : gimple *g;
16613 : 4014 : gimple_seq body = NULL;
16614 : 4014 : bool nowait = false;
16615 : 4014 : bool has_depend = false;
16616 : :
16617 : 4014 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16618 : : {
16619 : 236 : for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
16620 : 150 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
16621 : : {
16622 : 108 : has_depend = true;
16623 : 108 : if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
16624 : : {
16625 : 9 : error_at (OMP_CLAUSE_LOCATION (c),
16626 : : "%<mutexinoutset%> kind in %<depend%> clause on a "
16627 : : "%<taskwait%> construct");
16628 : 9 : break;
16629 : : }
16630 : : }
16631 : 42 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
16632 : 42 : nowait = true;
16633 : 95 : if (nowait && !has_depend)
16634 : : {
16635 : 5 : error_at (EXPR_LOCATION (expr),
16636 : : "%<taskwait%> construct with %<nowait%> clause but no "
16637 : : "%<depend%> clauses");
16638 : 5 : *expr_p = NULL_TREE;
16639 : 5 : return;
16640 : : }
16641 : : }
16642 : :
16643 : 12027 : gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
16644 : 4009 : omp_find_clause (OMP_TASK_CLAUSES (expr),
16645 : : OMP_CLAUSE_UNTIED)
16646 : : ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
16647 : :
16648 : 4009 : if (OMP_TASK_BODY (expr))
16649 : : {
16650 : 3919 : push_gimplify_context ();
16651 : :
16652 : 3919 : g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
16653 : 3919 : if (gimple_code (g) == GIMPLE_BIND)
16654 : 3919 : pop_gimplify_context (g);
16655 : : else
16656 : 0 : pop_gimplify_context (NULL);
16657 : : }
16658 : :
16659 : 4009 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
16660 : : OMP_TASK);
16661 : :
16662 : 8018 : g = gimple_build_omp_task (body,
16663 : 4009 : OMP_TASK_CLAUSES (expr),
16664 : : NULL_TREE, NULL_TREE,
16665 : : NULL_TREE, NULL_TREE, NULL_TREE);
16666 : 4009 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16667 : 90 : gimple_omp_task_set_taskwait_p (g, true);
16668 : 4009 : gimplify_seq_add_stmt (pre_p, g);
16669 : 4009 : *expr_p = NULL_TREE;
16670 : : }
16671 : :
16672 : : /* Helper function for gimplify_omp_for. If *TP is not a gimple constant,
16673 : : force it into a temporary initialized in PRE_P and add firstprivate clause
16674 : : to ORIG_FOR_STMT. */
16675 : :
16676 : : static void
16677 : 4385 : gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
16678 : : tree orig_for_stmt)
16679 : : {
16680 : 4385 : if (*tp == NULL || is_gimple_constant (*tp))
16681 : : return;
16682 : :
16683 : 776 : if (TREE_CODE (*tp) == SAVE_EXPR)
16684 : 135 : gimplify_save_expr (tp, pre_p, NULL);
16685 : : else
16686 : 641 : *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
16687 : : /* Reference to pointer conversion is considered useless,
16688 : : but is significant for firstprivate clause. Force it
16689 : : here. */
16690 : 776 : if (type
16691 : 684 : && TREE_CODE (type) == POINTER_TYPE
16692 : 866 : && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
16693 : : {
16694 : 12 : tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
16695 : 12 : tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
16696 : 12 : gimplify_and_add (m, pre_p);
16697 : 12 : *tp = v;
16698 : : }
16699 : :
16700 : 776 : tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16701 : 776 : OMP_CLAUSE_DECL (c) = *tp;
16702 : 776 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
16703 : 776 : OMP_FOR_CLAUSES (orig_for_stmt) = c;
16704 : : }
16705 : :
16706 : : /* Helper function of gimplify_omp_for, find OMP_ORDERED with
16707 : : null OMP_ORDERED_BODY inside of OMP_FOR's body. */
16708 : :
16709 : : static tree
16710 : 16453 : find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
16711 : : {
16712 : 16453 : switch (TREE_CODE (*tp))
16713 : : {
16714 : 896 : case OMP_ORDERED:
16715 : 896 : if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
16716 : : return *tp;
16717 : : break;
16718 : 23 : case OMP_SIMD:
16719 : 23 : case OMP_PARALLEL:
16720 : 23 : case OMP_TARGET:
16721 : 23 : *walk_subtrees = 0;
16722 : 23 : break;
16723 : : default:
16724 : : break;
16725 : : }
16726 : : return NULL_TREE;
16727 : : }
16728 : :
16729 : : /* Gimplify standalone loop transforming directive which has the
16730 : : transformations applied already. So, all that is needed is gimplify
16731 : : the remaining loops as normal loops. */
16732 : :
16733 : : static enum gimplify_status
16734 : 2264 : gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
16735 : : {
16736 : 2264 : tree for_stmt = *expr_p;
16737 : :
16738 : 2264 : if (OMP_FOR_PRE_BODY (for_stmt))
16739 : 759 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
16740 : :
16741 : 2264 : gimple_seq pre_body = NULL, post_body = NULL;
16742 : 5203 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16743 : : {
16744 : 2939 : if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
16745 : 1707 : continue;
16746 : 1283 : tree iters = NULL_TREE;
16747 : 1283 : if (i == 0
16748 : 826 : && TREE_CODE (for_stmt) == OMP_UNROLL
16749 : 1771 : && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
16750 : : {
16751 : 311 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
16752 : 269 : iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
16753 : : else
16754 : 42 : iters = build_int_cst (integer_type_node, 8);
16755 : : }
16756 : 1283 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16757 : 1283 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16758 : 1283 : tree decl = TREE_OPERAND (t, 0);
16759 : 1283 : gcc_assert (DECL_P (decl));
16760 : 1283 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16761 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
16762 : 1283 : if (DECL_ARTIFICIAL (decl)
16763 : 876 : && TREE_PRIVATE (t)
16764 : 867 : && gimplify_omp_ctxp
16765 : 1613 : && gimplify_omp_ctxp->region_type != ORT_NONE)
16766 : : {
16767 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16768 : 504 : do
16769 : : {
16770 : 504 : splay_tree_node n
16771 : 504 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16772 : 504 : if (n != NULL)
16773 : : break;
16774 : 346 : else if (ctx->region_type != ORT_WORKSHARE
16775 : : && ctx->region_type != ORT_TASKGROUP
16776 : 62 : && ctx->region_type != ORT_SIMD
16777 : 38 : && ctx->region_type != ORT_ACC
16778 : 38 : && !(ctx->region_type & ORT_TARGET_DATA))
16779 : : {
16780 : 38 : omp_add_variable (ctx, decl, GOVD_PRIVATE);
16781 : 38 : break;
16782 : : }
16783 : 308 : ctx = ctx->outer_context;
16784 : : }
16785 : 308 : while (ctx);
16786 : : }
16787 : 1283 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16788 : : {
16789 : 23 : gcc_assert (seen_error ());
16790 : 23 : continue;
16791 : : }
16792 : 1260 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16793 : : fb_rvalue);
16794 : 1260 : gimplify_and_add (t, &pre_body);
16795 : 1260 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16796 : 1260 : gcc_assert (TREE_OPERAND (t, 0) == decl);
16797 : 1260 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16798 : : {
16799 : 28 : gcc_assert (seen_error ());
16800 : 28 : continue;
16801 : : }
16802 : 1232 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16803 : : fb_rvalue);
16804 : 1232 : tree l1 = create_artificial_label (UNKNOWN_LOCATION);
16805 : 1232 : tree l2 = create_artificial_label (UNKNOWN_LOCATION);
16806 : 1232 : tree l3 = create_artificial_label (UNKNOWN_LOCATION);
16807 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
16808 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
16809 : 1232 : gimple_seq this_post_body = NULL;
16810 : 1232 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16811 : 1232 : if (TREE_CODE (t) == MODIFY_EXPR)
16812 : : {
16813 : 975 : t = TREE_OPERAND (t, 1);
16814 : 975 : if (TREE_CODE (t) == PLUS_EXPR
16815 : 975 : && TREE_OPERAND (t, 1) == decl)
16816 : : {
16817 : 0 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
16818 : 0 : TREE_OPERAND (t, 0) = decl;
16819 : : }
16820 : 975 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16821 : : fb_rvalue);
16822 : : }
16823 : 1232 : gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
16824 : : &this_post_body);
16825 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
16826 : 1232 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16827 : 1232 : gcond *cond = NULL;
16828 : 1232 : tree d = decl;
16829 : 1232 : gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
16830 : 1232 : if (iters && tree_fits_uhwi_p (iters))
16831 : : {
16832 : 299 : unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
16833 : 299 : if ((unsigned HOST_WIDE_INT) (int) niters == niters
16834 : 299 : && (int) niters > 0)
16835 : : {
16836 : 299 : t = build2 (TREE_CODE (t), boolean_type_node, d,
16837 : 299 : TREE_OPERAND (t, 1));
16838 : 299 : t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
16839 : : build_int_cst (integer_type_node,
16840 : : annot_expr_unroll_kind),
16841 : 299 : build_int_cst (integer_type_node, niters));
16842 : 299 : gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
16843 : : fb_rvalue);
16844 : 299 : cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
16845 : : l1, l3);
16846 : : }
16847 : : }
16848 : 299 : if (cond == NULL)
16849 : 933 : cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
16850 : : l1, l3);
16851 : 1232 : gimplify_seq_add_stmt (&this_post_body, cond);
16852 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
16853 : 1232 : gimplify_seq_add_seq (&this_post_body, post_body);
16854 : 1232 : post_body = this_post_body;
16855 : : }
16856 : 2264 : gimplify_seq_add_seq (pre_p, pre_body);
16857 : 2264 : gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
16858 : 2264 : gimplify_seq_add_seq (pre_p, post_body);
16859 : :
16860 : 2264 : *expr_p = NULL_TREE;
16861 : 2264 : return GS_ALL_DONE;
16862 : : }
16863 : :
16864 : : /* Gimplify the gross structure of an OMP_FOR statement. */
16865 : :
16866 : : static enum gimplify_status
16867 : 58109 : gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
16868 : : {
16869 : 58109 : tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
16870 : 58109 : enum gimplify_status ret = GS_ALL_DONE;
16871 : 58109 : enum gimplify_status tret;
16872 : 58109 : gomp_for *gfor;
16873 : 58109 : gimple_seq for_body, for_pre_body;
16874 : 58109 : int i;
16875 : 58109 : bitmap has_decl_expr = NULL;
16876 : 58109 : enum omp_region_type ort = ORT_WORKSHARE;
16877 : 58109 : bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
16878 : :
16879 : 58109 : orig_for_stmt = for_stmt = *expr_p;
16880 : :
16881 : 58109 : bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
16882 : 58109 : != NULL_TREE);
16883 : 58180 : while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16884 : : {
16885 : 17761 : tree *data[4] = { NULL, NULL, NULL, NULL };
16886 : 17761 : gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
16887 : 17761 : inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
16888 : : find_combined_omp_for, data, NULL);
16889 : 17761 : if (inner_for_stmt == NULL_TREE)
16890 : : {
16891 : 36 : gcc_assert (seen_error ());
16892 : 36 : *expr_p = NULL_TREE;
16893 : 3743 : return GS_ERROR;
16894 : : }
16895 : 17725 : gcc_assert (inner_for_stmt == *data[3]);
16896 : 17725 : omp_maybe_apply_loop_xforms (data[3],
16897 : 17725 : data[2]
16898 : 4270 : ? OMP_FOR_CLAUSES (*data[2])
16899 : 13455 : : TREE_CODE (for_stmt) == OMP_FOR
16900 : 13455 : ? OMP_FOR_CLAUSES (for_stmt)
16901 : : : NULL_TREE);
16902 : 17725 : if (inner_for_stmt != *data[3])
16903 : 71 : continue;
16904 : 17654 : if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
16905 : : {
16906 : 2 : append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
16907 : : &OMP_FOR_PRE_BODY (for_stmt));
16908 : 2 : OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
16909 : : }
16910 : 17654 : if (OMP_FOR_PRE_BODY (inner_for_stmt))
16911 : : {
16912 : 7224 : append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
16913 : : &OMP_FOR_PRE_BODY (for_stmt));
16914 : 7224 : OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
16915 : : }
16916 : :
16917 : 17654 : if (data[0])
16918 : : {
16919 : : /* We have some statements or variable declarations in between
16920 : : the composite construct directives. Move them around the
16921 : : inner_for_stmt. */
16922 : 3707 : data[0] = expr_p;
16923 : 14828 : for (i = 0; i < 3; i++)
16924 : 11121 : if (data[i])
16925 : : {
16926 : 7037 : tree t = *data[i];
16927 : 7037 : if (i < 2 && data[i + 1] == &OMP_BODY (t))
16928 : 1002 : data[i + 1] = data[i];
16929 : 7037 : *data[i] = OMP_BODY (t);
16930 : 7037 : tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
16931 : : NULL_TREE, make_node (BLOCK));
16932 : 7037 : OMP_BODY (t) = body;
16933 : 7037 : append_to_statement_list_force (inner_for_stmt,
16934 : : &BIND_EXPR_BODY (body));
16935 : 7037 : *data[3] = t;
16936 : 7037 : data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
16937 : 7037 : gcc_assert (*data[3] == inner_for_stmt);
16938 : : }
16939 : : return GS_OK;
16940 : : }
16941 : :
16942 : 36750 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16943 : 22803 : if (!loop_p
16944 : 21948 : && OMP_FOR_ORIG_DECLS (inner_for_stmt)
16945 : 11124 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16946 : : i)) == TREE_LIST
16947 : 22871 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16948 : : i)))
16949 : : {
16950 : 40 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16951 : : /* Class iterators aren't allowed on OMP_SIMD, so the only
16952 : : case we need to solve is distribute parallel for. They are
16953 : : allowed on the loop construct, but that is already handled
16954 : : in gimplify_omp_loop. */
16955 : 40 : gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
16956 : : && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
16957 : : && data[1]);
16958 : 40 : tree orig_decl = TREE_PURPOSE (orig);
16959 : 40 : tree last = TREE_VALUE (orig);
16960 : 40 : tree *pc;
16961 : 40 : for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
16962 : 74 : *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
16963 : 42 : if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16964 : 35 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
16965 : 43 : && OMP_CLAUSE_DECL (*pc) == orig_decl)
16966 : : break;
16967 : 40 : if (*pc == NULL_TREE)
16968 : : {
16969 : 32 : tree *spc;
16970 : 32 : for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
16971 : 98 : *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
16972 : 67 : if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
16973 : 67 : && OMP_CLAUSE_DECL (*spc) == orig_decl)
16974 : : break;
16975 : 32 : if (*spc)
16976 : : {
16977 : 1 : tree c = *spc;
16978 : 1 : *spc = OMP_CLAUSE_CHAIN (c);
16979 : 1 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16980 : 1 : *pc = c;
16981 : : }
16982 : : }
16983 : 40 : if (*pc == NULL_TREE)
16984 : : ;
16985 : 9 : else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
16986 : : {
16987 : : /* private clause will appear only on inner_for_stmt.
16988 : : Change it into firstprivate, and add private clause
16989 : : on for_stmt. */
16990 : 8 : tree c = copy_node (*pc);
16991 : 8 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16992 : 8 : OMP_FOR_CLAUSES (for_stmt) = c;
16993 : 8 : OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
16994 : 8 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16995 : : }
16996 : : else
16997 : : {
16998 : : /* lastprivate clause will appear on both inner_for_stmt
16999 : : and for_stmt. Add firstprivate clause to
17000 : : inner_for_stmt. */
17001 : 1 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
17002 : : OMP_CLAUSE_FIRSTPRIVATE);
17003 : 1 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
17004 : 1 : OMP_CLAUSE_CHAIN (c) = *pc;
17005 : 1 : *pc = c;
17006 : 1 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
17007 : : }
17008 : 40 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17009 : : OMP_CLAUSE_FIRSTPRIVATE);
17010 : 40 : OMP_CLAUSE_DECL (c) = last;
17011 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17012 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17013 : 40 : c = build_omp_clause (UNKNOWN_LOCATION,
17014 : 40 : *pc ? OMP_CLAUSE_SHARED
17015 : : : OMP_CLAUSE_FIRSTPRIVATE);
17016 : 40 : OMP_CLAUSE_DECL (c) = orig_decl;
17017 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17018 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17019 : : }
17020 : : /* Similarly, take care of C++ range for temporaries, those should
17021 : : be firstprivate on OMP_PARALLEL if any. */
17022 : 13947 : if (data[1])
17023 : 17540 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
17024 : 11120 : if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
17025 : 5753 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17026 : : i)) == TREE_LIST
17027 : 11196 : && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17028 : : i)))
17029 : : {
17030 : 49 : tree orig
17031 : 49 : = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
17032 : 49 : tree v = TREE_CHAIN (orig);
17033 : 49 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17034 : : OMP_CLAUSE_FIRSTPRIVATE);
17035 : : /* First add firstprivate clause for the __for_end artificial
17036 : : decl. */
17037 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
17038 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17039 : : == REFERENCE_TYPE)
17040 : 0 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17041 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17042 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17043 : 49 : if (TREE_VEC_ELT (v, 0))
17044 : : {
17045 : : /* And now the same for __for_range artificial decl if it
17046 : : exists. */
17047 : 49 : c = build_omp_clause (UNKNOWN_LOCATION,
17048 : : OMP_CLAUSE_FIRSTPRIVATE);
17049 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
17050 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17051 : : == REFERENCE_TYPE)
17052 : 49 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17053 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17054 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17055 : : }
17056 : : }
17057 : 13947 : break;
17058 : : }
17059 : 54366 : if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
17060 : : {
17061 : 40419 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
17062 : 40419 : if (*expr_p != for_stmt)
17063 : : return GS_OK;
17064 : : }
17065 : :
17066 : 53484 : switch (TREE_CODE (for_stmt))
17067 : : {
17068 : 18178 : case OMP_FOR:
17069 : 30391 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17070 : : {
17071 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17072 : : OMP_CLAUSE_SCHEDULE))
17073 : 47 : error_at (EXPR_LOCATION (for_stmt),
17074 : : "%qs clause may not appear on non-rectangular %qs",
17075 : 27 : "schedule", lang_GNU_Fortran () ? "do" : "for");
17076 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
17077 : 34 : error_at (EXPR_LOCATION (for_stmt),
17078 : : "%qs clause may not appear on non-rectangular %qs",
17079 : 18 : "ordered", lang_GNU_Fortran () ? "do" : "for");
17080 : : }
17081 : : break;
17082 : 8244 : case OMP_DISTRIBUTE:
17083 : 9155 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
17084 : 8244 : && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17085 : : OMP_CLAUSE_DIST_SCHEDULE))
17086 : 29 : error_at (EXPR_LOCATION (for_stmt),
17087 : : "%qs clause may not appear on non-rectangular %qs",
17088 : : "dist_schedule", "distribute");
17089 : : break;
17090 : : case OACC_LOOP:
17091 : : ort = ORT_ACC;
17092 : : break;
17093 : 1579 : case OMP_TASKLOOP:
17094 : 2509 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17095 : : {
17096 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17097 : : OMP_CLAUSE_GRAINSIZE))
17098 : 11 : error_at (EXPR_LOCATION (for_stmt),
17099 : : "%qs clause may not appear on non-rectangular %qs",
17100 : : "grainsize", "taskloop");
17101 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17102 : : OMP_CLAUSE_NUM_TASKS))
17103 : 6 : error_at (EXPR_LOCATION (for_stmt),
17104 : : "%qs clause may not appear on non-rectangular %qs",
17105 : : "num_tasks", "taskloop");
17106 : : }
17107 : 1579 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
17108 : : ort = ORT_UNTIED_TASKLOOP;
17109 : : else
17110 : : ort = ORT_TASKLOOP;
17111 : : break;
17112 : 10642 : case OMP_SIMD:
17113 : 10642 : ort = ORT_SIMD;
17114 : 10642 : break;
17115 : 2264 : case OMP_TILE:
17116 : 2264 : case OMP_UNROLL:
17117 : 2264 : gcc_assert (inner_for_stmt == NULL_TREE);
17118 : 2264 : return gimplify_omp_loop_xform (expr_p, pre_p);
17119 : 0 : default:
17120 : 0 : gcc_unreachable ();
17121 : : }
17122 : :
17123 : : /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
17124 : : clause for the IV. */
17125 : 10689 : if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17126 : : {
17127 : 7829 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
17128 : 7829 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17129 : 7829 : decl = TREE_OPERAND (t, 0);
17130 : 18659 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
17131 : 11944 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17132 : 11944 : && OMP_CLAUSE_DECL (c) == decl)
17133 : : {
17134 : 1114 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17135 : 1114 : break;
17136 : : }
17137 : : }
17138 : :
17139 : 51220 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
17140 : 51544 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
17141 : 1903 : loop_p && TREE_CODE (for_stmt) != OMP_SIMD
17142 : : ? OMP_LOOP : TREE_CODE (for_stmt));
17143 : :
17144 : 51220 : if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
17145 : 8244 : gimplify_omp_ctxp->distribute = true;
17146 : :
17147 : : /* Handle OMP_FOR_INIT. */
17148 : 51220 : for_pre_body = NULL;
17149 : 51220 : if ((ort == ORT_SIMD
17150 : 40578 : || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
17151 : 61947 : && OMP_FOR_PRE_BODY (for_stmt))
17152 : : {
17153 : 6776 : has_decl_expr = BITMAP_ALLOC (NULL);
17154 : 6776 : if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
17155 : 6776 : && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
17156 : : {
17157 : 18 : t = OMP_FOR_PRE_BODY (for_stmt);
17158 : 18 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17159 : : }
17160 : 6758 : else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
17161 : : {
17162 : 6756 : tree_stmt_iterator si;
17163 : 11206 : for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
17164 : 4450 : tsi_next (&si))
17165 : : {
17166 : 4450 : t = tsi_stmt (si);
17167 : 4450 : if (TREE_CODE (t) == DECL_EXPR
17168 : 4450 : && VAR_P (DECL_EXPR_DECL (t)))
17169 : 4377 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17170 : : }
17171 : : }
17172 : : }
17173 : 51220 : if (OMP_FOR_PRE_BODY (for_stmt))
17174 : : {
17175 : 21005 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
17176 : 20284 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17177 : : else
17178 : : {
17179 : 721 : struct gimplify_omp_ctx ctx;
17180 : 721 : memset (&ctx, 0, sizeof (ctx));
17181 : 721 : ctx.region_type = ORT_NONE;
17182 : 721 : gimplify_omp_ctxp = &ctx;
17183 : 721 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17184 : 721 : gimplify_omp_ctxp = NULL;
17185 : : }
17186 : : }
17187 : 51220 : OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
17188 : :
17189 : 51220 : if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
17190 : 13947 : for_stmt = inner_for_stmt;
17191 : :
17192 : : /* For taskloop, need to gimplify the start, end and step before the
17193 : : taskloop, outside of the taskloop omp context. */
17194 : 51220 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17195 : : {
17196 : 3444 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17197 : : {
17198 : 1865 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17199 : 1865 : gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
17200 : 1865 : ? pre_p : &for_pre_body);
17201 : 1865 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
17202 : 1865 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17203 : : {
17204 : 34 : tree v = TREE_OPERAND (t, 1);
17205 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17206 : : for_pre_p, orig_for_stmt);
17207 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17208 : : for_pre_p, orig_for_stmt);
17209 : : }
17210 : : else
17211 : 1831 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17212 : : orig_for_stmt);
17213 : :
17214 : : /* Handle OMP_FOR_COND. */
17215 : 1865 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17216 : 1865 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17217 : : {
17218 : 31 : tree v = TREE_OPERAND (t, 1);
17219 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17220 : : for_pre_p, orig_for_stmt);
17221 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17222 : : for_pre_p, orig_for_stmt);
17223 : : }
17224 : : else
17225 : 1834 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17226 : : orig_for_stmt);
17227 : :
17228 : : /* Handle OMP_FOR_INCR. */
17229 : 1865 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17230 : 1865 : if (TREE_CODE (t) == MODIFY_EXPR)
17231 : : {
17232 : 590 : decl = TREE_OPERAND (t, 0);
17233 : 590 : t = TREE_OPERAND (t, 1);
17234 : 590 : tree *tp = &TREE_OPERAND (t, 1);
17235 : 590 : if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
17236 : 22 : tp = &TREE_OPERAND (t, 0);
17237 : :
17238 : 590 : gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
17239 : : orig_for_stmt);
17240 : : }
17241 : : }
17242 : :
17243 : 1579 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
17244 : : OMP_TASKLOOP);
17245 : : }
17246 : :
17247 : 51220 : if (orig_for_stmt != for_stmt)
17248 : 13947 : gimplify_omp_ctxp->combined_loop = true;
17249 : :
17250 : 51220 : for_body = NULL;
17251 : 51220 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17252 : : == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
17253 : 51220 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17254 : : == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
17255 : :
17256 : 51220 : tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
17257 : 51220 : bool is_doacross = false;
17258 : 51220 : if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
17259 : : find_standalone_omp_ordered, NULL))
17260 : : {
17261 : 526 : OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
17262 : 526 : is_doacross = true;
17263 : 526 : int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
17264 : 526 : gimplify_omp_ctxp->loop_iter_var.create (len * 2);
17265 : 2190 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
17266 : 1664 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
17267 : : {
17268 : 10 : error_at (OMP_CLAUSE_LOCATION (*pc),
17269 : : "%<linear%> clause may not be specified together "
17270 : : "with %<ordered%> clause if stand-alone %<ordered%> "
17271 : : "construct is nested in it");
17272 : 10 : *pc = OMP_CLAUSE_CHAIN (*pc);
17273 : : }
17274 : : else
17275 : 1654 : pc = &OMP_CLAUSE_CHAIN (*pc);
17276 : : }
17277 : 51220 : int collapse = 1, tile = 0;
17278 : 51220 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
17279 : 51220 : if (c)
17280 : 13659 : collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
17281 : 51220 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
17282 : 51220 : if (c)
17283 : 384 : tile = list_length (OMP_CLAUSE_TILE_LIST (c));
17284 : 51220 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
17285 : 51220 : hash_set<tree> *allocate_uids = NULL;
17286 : 51220 : if (c)
17287 : : {
17288 : 275 : allocate_uids = new hash_set<tree>;
17289 : 1682 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17290 : 1132 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
17291 : 478 : allocate_uids->add (OMP_CLAUSE_DECL (c));
17292 : : }
17293 : 124117 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17294 : : {
17295 : 72897 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17296 : 72897 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17297 : 72897 : decl = TREE_OPERAND (t, 0);
17298 : 72897 : gcc_assert (DECL_P (decl));
17299 : 72897 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
17300 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
17301 : 72897 : if (is_doacross)
17302 : : {
17303 : 1649 : if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
17304 : : {
17305 : 956 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17306 : 956 : if (TREE_CODE (orig_decl) == TREE_LIST)
17307 : : {
17308 : 15 : orig_decl = TREE_PURPOSE (orig_decl);
17309 : 15 : if (!orig_decl)
17310 : 0 : orig_decl = decl;
17311 : : }
17312 : 956 : gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
17313 : : }
17314 : : else
17315 : 693 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17316 : 1649 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17317 : : }
17318 : :
17319 : 72897 : if (for_stmt == orig_for_stmt)
17320 : : {
17321 : 50094 : tree orig_decl = decl;
17322 : 50094 : if (OMP_FOR_ORIG_DECLS (for_stmt))
17323 : : {
17324 : 23925 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17325 : 23925 : if (TREE_CODE (orig_decl) == TREE_LIST)
17326 : : {
17327 : : orig_decl = TREE_PURPOSE (orig_decl);
17328 : : if (!orig_decl)
17329 : : orig_decl = decl;
17330 : : }
17331 : : }
17332 : 50094 : if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
17333 : 36 : error_at (EXPR_LOCATION (for_stmt),
17334 : : "threadprivate iteration variable %qD", orig_decl);
17335 : : }
17336 : :
17337 : : /* Make sure the iteration variable is private. */
17338 : 72897 : tree c = NULL_TREE;
17339 : 72897 : tree c2 = NULL_TREE;
17340 : 72897 : if (orig_for_stmt != for_stmt)
17341 : : {
17342 : : /* Preserve this information until we gimplify the inner simd. */
17343 : 22803 : if (has_decl_expr
17344 : 22803 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17345 : 3396 : TREE_PRIVATE (t) = 1;
17346 : : }
17347 : 50094 : else if (ort == ORT_SIMD)
17348 : : {
17349 : 15700 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17350 : : (splay_tree_key) decl);
17351 : 15700 : omp_is_private (gimplify_omp_ctxp, decl,
17352 : 15700 : 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17353 : : != 1));
17354 : 15700 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
17355 : : {
17356 : 2512 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17357 : 2512 : if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
17358 : 0 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17359 : : OMP_CLAUSE_LASTPRIVATE);
17360 : 0 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17361 : : OMP_CLAUSE_LASTPRIVATE))
17362 : 0 : if (OMP_CLAUSE_DECL (c3) == decl)
17363 : : {
17364 : 0 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17365 : : "conditional %<lastprivate%> on loop "
17366 : : "iterator %qD ignored", decl);
17367 : 0 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17368 : 0 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17369 : : }
17370 : : }
17371 : 13188 : else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
17372 : : {
17373 : 5631 : c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17374 : 5631 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17375 : 5631 : unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
17376 : 5631 : if ((has_decl_expr
17377 : 1306 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17378 : 6315 : || TREE_PRIVATE (t))
17379 : : {
17380 : 2277 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17381 : 2277 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17382 : : }
17383 : 5631 : struct gimplify_omp_ctx *outer
17384 : 5631 : = gimplify_omp_ctxp->outer_context;
17385 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17386 : : {
17387 : 2433 : if (outer->region_type == ORT_WORKSHARE
17388 : 2193 : && outer->combined_loop)
17389 : : {
17390 : 2142 : n = splay_tree_lookup (outer->variables,
17391 : : (splay_tree_key)decl);
17392 : 2142 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17393 : : {
17394 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17395 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17396 : : }
17397 : : else
17398 : : {
17399 : 2142 : struct gimplify_omp_ctx *octx = outer->outer_context;
17400 : 2142 : if (octx
17401 : 1714 : && octx->region_type == ORT_COMBINED_PARALLEL
17402 : 1476 : && octx->outer_context
17403 : 1209 : && (octx->outer_context->region_type
17404 : : == ORT_WORKSHARE)
17405 : 1079 : && octx->outer_context->combined_loop)
17406 : : {
17407 : 1079 : octx = octx->outer_context;
17408 : 1079 : n = splay_tree_lookup (octx->variables,
17409 : : (splay_tree_key)decl);
17410 : 1079 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17411 : : {
17412 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17413 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17414 : : }
17415 : : }
17416 : : }
17417 : : }
17418 : : }
17419 : :
17420 : 5631 : OMP_CLAUSE_DECL (c) = decl;
17421 : 5631 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17422 : 5631 : OMP_FOR_CLAUSES (for_stmt) = c;
17423 : 5631 : omp_add_variable (gimplify_omp_ctxp, decl, flags);
17424 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17425 : 2433 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17426 : : true);
17427 : : }
17428 : : else
17429 : : {
17430 : 7557 : bool lastprivate
17431 : : = (!has_decl_expr
17432 : 7557 : || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
17433 : 7557 : if (TREE_PRIVATE (t))
17434 : 1771 : lastprivate = false;
17435 : 7557 : if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
17436 : : {
17437 : 507 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17438 : 507 : if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
17439 : : lastprivate = false;
17440 : : }
17441 : :
17442 : 7557 : struct gimplify_omp_ctx *outer
17443 : 7557 : = gimplify_omp_ctxp->outer_context;
17444 : 7557 : if (outer && lastprivate)
17445 : 4893 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17446 : : true);
17447 : :
17448 : 9547 : c = build_omp_clause (input_location,
17449 : : lastprivate ? OMP_CLAUSE_LASTPRIVATE
17450 : : : OMP_CLAUSE_PRIVATE);
17451 : 7557 : OMP_CLAUSE_DECL (c) = decl;
17452 : 7557 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17453 : 7557 : OMP_FOR_CLAUSES (for_stmt) = c;
17454 : 7557 : omp_add_variable (gimplify_omp_ctxp, decl,
17455 : : (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
17456 : : | GOVD_EXPLICIT | GOVD_SEEN);
17457 : 7557 : c = NULL_TREE;
17458 : : }
17459 : : }
17460 : 34394 : else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
17461 : : {
17462 : 8715 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17463 : 8715 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17464 : : (splay_tree_key) decl);
17465 : 8715 : if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
17466 : 80 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17467 : : OMP_CLAUSE_LASTPRIVATE);
17468 : 80 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17469 : : OMP_CLAUSE_LASTPRIVATE))
17470 : 40 : if (OMP_CLAUSE_DECL (c3) == decl)
17471 : : {
17472 : 40 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17473 : : "conditional %<lastprivate%> on loop "
17474 : : "iterator %qD ignored", decl);
17475 : 40 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17476 : 40 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17477 : : }
17478 : : }
17479 : : else
17480 : 25679 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
17481 : :
17482 : : /* If DECL is not a gimple register, create a temporary variable to act
17483 : : as an iteration counter. This is valid, since DECL cannot be
17484 : : modified in the body of the loop. Similarly for any iteration vars
17485 : : in simd with collapse > 1 where the iterator vars must be
17486 : : lastprivate. And similarly for vars mentioned in allocate clauses. */
17487 : 72897 : if (orig_for_stmt != for_stmt)
17488 : 22803 : var = decl;
17489 : 50094 : else if (!is_gimple_reg (decl)
17490 : 46822 : || (ort == ORT_SIMD
17491 : 14101 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
17492 : 89876 : || (allocate_uids && allocate_uids->contains (decl)))
17493 : : {
17494 : 10344 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17495 : : /* Make sure omp_add_variable is not called on it prematurely.
17496 : : We call it ourselves a few lines later. */
17497 : 10344 : gimplify_omp_ctxp = NULL;
17498 : 10344 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17499 : 10344 : gimplify_omp_ctxp = ctx;
17500 : 10344 : TREE_OPERAND (t, 0) = var;
17501 : :
17502 : 10344 : gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
17503 : :
17504 : 10344 : if (ort == ORT_SIMD
17505 : 10344 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17506 : : {
17507 : 768 : c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17508 : 768 : OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
17509 : 768 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
17510 : 768 : OMP_CLAUSE_DECL (c2) = var;
17511 : 768 : OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
17512 : 768 : OMP_FOR_CLAUSES (for_stmt) = c2;
17513 : 768 : omp_add_variable (gimplify_omp_ctxp, var,
17514 : : GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
17515 : 768 : if (c == NULL_TREE)
17516 : : {
17517 : 518 : c = c2;
17518 : 518 : c2 = NULL_TREE;
17519 : : }
17520 : : }
17521 : : else
17522 : 9576 : omp_add_variable (gimplify_omp_ctxp, var,
17523 : : GOVD_PRIVATE | GOVD_SEEN);
17524 : : }
17525 : : else
17526 : : var = decl;
17527 : :
17528 : 72897 : gimplify_omp_ctxp->in_for_exprs = true;
17529 : 72897 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17530 : : {
17531 : 719 : tree lb = TREE_OPERAND (t, 1);
17532 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
17533 : : is_gimple_val, fb_rvalue, false);
17534 : 719 : ret = MIN (ret, tret);
17535 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
17536 : : is_gimple_val, fb_rvalue, false);
17537 : : }
17538 : : else
17539 : 72178 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17540 : : is_gimple_val, fb_rvalue, false);
17541 : 72897 : gimplify_omp_ctxp->in_for_exprs = false;
17542 : 72897 : ret = MIN (ret, tret);
17543 : 72897 : if (ret == GS_ERROR)
17544 : : return ret;
17545 : :
17546 : : /* Handle OMP_FOR_COND. */
17547 : 72897 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17548 : 72897 : gcc_assert (COMPARISON_CLASS_P (t));
17549 : 72897 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17550 : :
17551 : 72897 : gimplify_omp_ctxp->in_for_exprs = true;
17552 : 72897 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17553 : : {
17554 : 599 : tree ub = TREE_OPERAND (t, 1);
17555 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
17556 : : is_gimple_val, fb_rvalue, false);
17557 : 599 : ret = MIN (ret, tret);
17558 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
17559 : : is_gimple_val, fb_rvalue, false);
17560 : : }
17561 : : else
17562 : 72298 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17563 : : is_gimple_val, fb_rvalue, false);
17564 : 72897 : gimplify_omp_ctxp->in_for_exprs = false;
17565 : 72897 : ret = MIN (ret, tret);
17566 : :
17567 : : /* Handle OMP_FOR_INCR. */
17568 : 72897 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17569 : 72897 : switch (TREE_CODE (t))
17570 : : {
17571 : 35682 : case PREINCREMENT_EXPR:
17572 : 35682 : case POSTINCREMENT_EXPR:
17573 : 35682 : {
17574 : 35682 : tree decl = TREE_OPERAND (t, 0);
17575 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17576 : : called to massage things appropriately. */
17577 : 35682 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17578 : :
17579 : 35682 : if (orig_for_stmt != for_stmt)
17580 : : break;
17581 : 25296 : t = build_int_cst (TREE_TYPE (decl), 1);
17582 : 25296 : if (c)
17583 : 4466 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17584 : 25296 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17585 : 25296 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17586 : 25296 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17587 : 25296 : break;
17588 : : }
17589 : :
17590 : 3826 : case PREDECREMENT_EXPR:
17591 : 3826 : case POSTDECREMENT_EXPR:
17592 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17593 : : called to massage things appropriately. */
17594 : 3826 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17595 : 3826 : if (orig_for_stmt != for_stmt)
17596 : : break;
17597 : 1988 : t = build_int_cst (TREE_TYPE (decl), -1);
17598 : 1988 : if (c)
17599 : 112 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17600 : 1988 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17601 : 1988 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17602 : 1988 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17603 : 1988 : break;
17604 : :
17605 : 33389 : case MODIFY_EXPR:
17606 : 33389 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17607 : 33389 : TREE_OPERAND (t, 0) = var;
17608 : :
17609 : 33389 : t = TREE_OPERAND (t, 1);
17610 : 33389 : switch (TREE_CODE (t))
17611 : : {
17612 : 26464 : case PLUS_EXPR:
17613 : 26464 : if (TREE_OPERAND (t, 1) == decl)
17614 : : {
17615 : 215 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
17616 : 215 : TREE_OPERAND (t, 0) = var;
17617 : 215 : break;
17618 : : }
17619 : :
17620 : : /* Fallthru. */
17621 : 33174 : case MINUS_EXPR:
17622 : 33174 : case POINTER_PLUS_EXPR:
17623 : 33174 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17624 : 33174 : TREE_OPERAND (t, 0) = var;
17625 : 33174 : break;
17626 : 0 : default:
17627 : 0 : gcc_unreachable ();
17628 : : }
17629 : :
17630 : 33389 : gimplify_omp_ctxp->in_for_exprs = true;
17631 : 33389 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17632 : : is_gimple_val, fb_rvalue, false);
17633 : 33389 : ret = MIN (ret, tret);
17634 : 33389 : if (c)
17635 : : {
17636 : 1571 : tree step = TREE_OPERAND (t, 1);
17637 : 1571 : tree stept = TREE_TYPE (decl);
17638 : 1571 : if (POINTER_TYPE_P (stept))
17639 : 280 : stept = sizetype;
17640 : 1571 : step = fold_convert (stept, step);
17641 : 1571 : if (TREE_CODE (t) == MINUS_EXPR)
17642 : 295 : step = fold_build1 (NEGATE_EXPR, stept, step);
17643 : 1571 : OMP_CLAUSE_LINEAR_STEP (c) = step;
17644 : 1571 : if (step != TREE_OPERAND (t, 1))
17645 : : {
17646 : 295 : tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
17647 : : &for_pre_body, NULL,
17648 : : is_gimple_val, fb_rvalue, false);
17649 : 295 : ret = MIN (ret, tret);
17650 : : }
17651 : : }
17652 : 33389 : gimplify_omp_ctxp->in_for_exprs = false;
17653 : 33389 : break;
17654 : :
17655 : 0 : default:
17656 : 0 : gcc_unreachable ();
17657 : : }
17658 : :
17659 : 72897 : if (c2)
17660 : : {
17661 : 250 : gcc_assert (c);
17662 : 250 : OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
17663 : : }
17664 : :
17665 : 72897 : if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
17666 : : {
17667 : 86413 : for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
17668 : 64926 : if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17669 : 16204 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
17670 : 55925 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17671 : 2155 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
17672 : 1371 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
17673 : 75276 : && OMP_CLAUSE_DECL (c) == decl)
17674 : : {
17675 : 7525 : if (is_doacross && (collapse == 1 || i >= collapse))
17676 : : t = var;
17677 : : else
17678 : : {
17679 : 7452 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17680 : 7452 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17681 : 7452 : gcc_assert (TREE_OPERAND (t, 0) == var);
17682 : 7452 : t = TREE_OPERAND (t, 1);
17683 : 7452 : gcc_assert (TREE_CODE (t) == PLUS_EXPR
17684 : : || TREE_CODE (t) == MINUS_EXPR
17685 : : || TREE_CODE (t) == POINTER_PLUS_EXPR);
17686 : 7452 : gcc_assert (TREE_OPERAND (t, 0) == var);
17687 : 14802 : t = build2 (TREE_CODE (t), TREE_TYPE (decl),
17688 : : is_doacross ? var : decl,
17689 : 7452 : TREE_OPERAND (t, 1));
17690 : : }
17691 : 7525 : gimple_seq *seq;
17692 : 7525 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
17693 : 6931 : seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
17694 : : else
17695 : 594 : seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
17696 : 7525 : push_gimplify_context ();
17697 : 7525 : gimplify_assign (decl, t, seq);
17698 : 7525 : gimple *bind = NULL;
17699 : 7525 : if (gimplify_ctxp->temps)
17700 : : {
17701 : 2070 : bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
17702 : 2070 : *seq = NULL;
17703 : 2070 : gimplify_seq_add_stmt (seq, bind);
17704 : : }
17705 : 7525 : pop_gimplify_context (bind);
17706 : : }
17707 : : }
17708 : 72897 : if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
17709 : 2609 : for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17710 : : {
17711 : 1197 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17712 : 1197 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17713 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17714 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17715 : 336 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17716 : 1197 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17717 : 1197 : gcc_assert (COMPARISON_CLASS_P (t));
17718 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17719 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17720 : 343 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17721 : : }
17722 : : }
17723 : :
17724 : 51220 : BITMAP_FREE (has_decl_expr);
17725 : 51495 : delete allocate_uids;
17726 : :
17727 : 51220 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17728 : 49641 : || (loop_p && orig_for_stmt == for_stmt))
17729 : : {
17730 : 2636 : push_gimplify_context ();
17731 : 2636 : if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
17732 : : {
17733 : 1888 : OMP_FOR_BODY (orig_for_stmt)
17734 : 1888 : = build3 (BIND_EXPR, void_type_node, NULL,
17735 : 1888 : OMP_FOR_BODY (orig_for_stmt), NULL);
17736 : 1888 : TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
17737 : : }
17738 : : }
17739 : :
17740 : 51220 : gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
17741 : : &for_body);
17742 : :
17743 : 51220 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17744 : 49641 : || (loop_p && orig_for_stmt == for_stmt))
17745 : : {
17746 : 2636 : if (gimple_code (g) == GIMPLE_BIND)
17747 : 2636 : pop_gimplify_context (g);
17748 : : else
17749 : 0 : pop_gimplify_context (NULL);
17750 : : }
17751 : :
17752 : 51220 : if (orig_for_stmt != for_stmt)
17753 : 36750 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17754 : : {
17755 : 22803 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17756 : 22803 : decl = TREE_OPERAND (t, 0);
17757 : 22803 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17758 : 22803 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17759 : 772 : gimplify_omp_ctxp = ctx->outer_context;
17760 : 22803 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17761 : 22803 : gimplify_omp_ctxp = ctx;
17762 : 22803 : omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
17763 : 22803 : TREE_OPERAND (t, 0) = var;
17764 : 22803 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17765 : 22803 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17766 : 22803 : TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
17767 : 22803 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17768 : 791 : for (int j = i + 1;
17769 : 791 : j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17770 : : {
17771 : 377 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17772 : 377 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17773 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17774 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17775 : : {
17776 : 133 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17777 : 133 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17778 : : }
17779 : 377 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17780 : 377 : gcc_assert (COMPARISON_CLASS_P (t));
17781 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17782 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17783 : : {
17784 : 67 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17785 : 67 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17786 : : }
17787 : : }
17788 : : }
17789 : :
17790 : 51220 : gimplify_adjust_omp_clauses (pre_p, for_body,
17791 : : &OMP_FOR_CLAUSES (orig_for_stmt),
17792 : 51220 : TREE_CODE (orig_for_stmt));
17793 : :
17794 : 51220 : int kind;
17795 : 51220 : switch (TREE_CODE (orig_for_stmt))
17796 : : {
17797 : : case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
17798 : 10642 : case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
17799 : 8244 : case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
17800 : 1579 : case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
17801 : 12577 : case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
17802 : 0 : default:
17803 : 0 : gcc_unreachable ();
17804 : : }
17805 : 51220 : if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
17806 : : {
17807 : 1057 : gimplify_seq_add_seq (pre_p, for_pre_body);
17808 : 1057 : for_pre_body = NULL;
17809 : : }
17810 : 51220 : gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
17811 : 51220 : TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
17812 : : for_pre_body);
17813 : 51220 : if (orig_for_stmt != for_stmt)
17814 : 13947 : gimple_omp_for_set_combined_p (gfor, true);
17815 : 51220 : if (gimplify_omp_ctxp
17816 : 42632 : && (gimplify_omp_ctxp->combined_loop
17817 : 35105 : || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
17818 : 12317 : && gimplify_omp_ctxp->outer_context
17819 : 7887 : && gimplify_omp_ctxp->outer_context->combined_loop)))
17820 : : {
17821 : 13947 : gimple_omp_for_set_combined_into_p (gfor, true);
17822 : 13947 : if (gimplify_omp_ctxp->combined_loop)
17823 : 7527 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
17824 : : else
17825 : 6420 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
17826 : : }
17827 : :
17828 : 124117 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17829 : : {
17830 : 72897 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17831 : 72897 : gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
17832 : 72897 : gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
17833 : 72897 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17834 : 72897 : gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
17835 : 72897 : gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
17836 : 72897 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17837 : 72897 : gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
17838 : : }
17839 : :
17840 : : /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
17841 : : constructs with GIMPLE_OMP_TASK sandwiched in between them.
17842 : : The outer taskloop stands for computing the number of iterations,
17843 : : counts for collapsed loops and holding taskloop specific clauses.
17844 : : The task construct stands for the effect of data sharing on the
17845 : : explicit task it creates and the inner taskloop stands for expansion
17846 : : of the static loop inside of the explicit task construct. */
17847 : 51220 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17848 : : {
17849 : 1579 : tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
17850 : 1579 : tree task_clauses = NULL_TREE;
17851 : 1579 : tree c = *gfor_clauses_ptr;
17852 : 1579 : tree *gtask_clauses_ptr = &task_clauses;
17853 : 1579 : tree outer_for_clauses = NULL_TREE;
17854 : 1579 : tree *gforo_clauses_ptr = &outer_for_clauses;
17855 : 1579 : bitmap lastprivate_uids = NULL;
17856 : 1579 : if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
17857 : : {
17858 : 36 : c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
17859 : 36 : if (c)
17860 : : {
17861 : 18 : lastprivate_uids = BITMAP_ALLOC (NULL);
17862 : 54 : for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
17863 : : OMP_CLAUSE_LASTPRIVATE))
17864 : 18 : bitmap_set_bit (lastprivate_uids,
17865 : 18 : DECL_UID (OMP_CLAUSE_DECL (c)));
17866 : : }
17867 : 36 : c = *gfor_clauses_ptr;
17868 : : }
17869 : 12185 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17870 : 10606 : switch (OMP_CLAUSE_CODE (c))
17871 : : {
17872 : : /* These clauses are allowed on task, move them there. */
17873 : 6190 : case OMP_CLAUSE_SHARED:
17874 : 6190 : case OMP_CLAUSE_FIRSTPRIVATE:
17875 : 6190 : case OMP_CLAUSE_DEFAULT:
17876 : 6190 : case OMP_CLAUSE_IF:
17877 : 6190 : case OMP_CLAUSE_UNTIED:
17878 : 6190 : case OMP_CLAUSE_FINAL:
17879 : 6190 : case OMP_CLAUSE_MERGEABLE:
17880 : 6190 : case OMP_CLAUSE_PRIORITY:
17881 : 6190 : case OMP_CLAUSE_REDUCTION:
17882 : 6190 : case OMP_CLAUSE_IN_REDUCTION:
17883 : 6190 : *gtask_clauses_ptr = c;
17884 : 6190 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17885 : 6190 : break;
17886 : 2061 : case OMP_CLAUSE_PRIVATE:
17887 : 2061 : if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
17888 : : {
17889 : : /* We want private on outer for and firstprivate
17890 : : on task. */
17891 : 26 : *gtask_clauses_ptr
17892 : 26 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17893 : : OMP_CLAUSE_FIRSTPRIVATE);
17894 : 26 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17895 : 26 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17896 : : openacc);
17897 : 26 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17898 : 26 : *gforo_clauses_ptr = c;
17899 : 26 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17900 : : }
17901 : : else
17902 : : {
17903 : 2035 : *gtask_clauses_ptr = c;
17904 : 2035 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17905 : : }
17906 : : break;
17907 : : /* These clauses go into outer taskloop clauses. */
17908 : 529 : case OMP_CLAUSE_GRAINSIZE:
17909 : 529 : case OMP_CLAUSE_NUM_TASKS:
17910 : 529 : case OMP_CLAUSE_NOGROUP:
17911 : 529 : *gforo_clauses_ptr = c;
17912 : 529 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17913 : 529 : break;
17914 : : /* Collapse clause we duplicate on both taskloops. */
17915 : 580 : case OMP_CLAUSE_COLLAPSE:
17916 : 580 : *gfor_clauses_ptr = c;
17917 : 580 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17918 : 580 : *gforo_clauses_ptr = copy_node (c);
17919 : 580 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17920 : 580 : break;
17921 : : /* For lastprivate, keep the clause on inner taskloop, and add
17922 : : a shared clause on task. If the same decl is also firstprivate,
17923 : : add also firstprivate clause on the inner taskloop. */
17924 : 1184 : case OMP_CLAUSE_LASTPRIVATE:
17925 : 1184 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17926 : : {
17927 : : /* For taskloop C++ lastprivate IVs, we want:
17928 : : 1) private on outer taskloop
17929 : : 2) firstprivate and shared on task
17930 : : 3) lastprivate on inner taskloop */
17931 : 38 : *gtask_clauses_ptr
17932 : 38 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17933 : : OMP_CLAUSE_FIRSTPRIVATE);
17934 : 38 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17935 : 38 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17936 : : openacc);
17937 : 38 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17938 : 38 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
17939 : 38 : *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17940 : : OMP_CLAUSE_PRIVATE);
17941 : 38 : OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
17942 : 38 : OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
17943 : 38 : TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
17944 : 38 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17945 : : }
17946 : 1184 : *gfor_clauses_ptr = c;
17947 : 1184 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17948 : 1184 : *gtask_clauses_ptr
17949 : 1184 : = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
17950 : 1184 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17951 : 1184 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
17952 : 309 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
17953 : 1184 : gtask_clauses_ptr
17954 : 1184 : = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17955 : 1184 : break;
17956 : : /* Allocate clause we duplicate on task and inner taskloop
17957 : : if the decl is lastprivate, otherwise just put on task. */
17958 : 62 : case OMP_CLAUSE_ALLOCATE:
17959 : 62 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
17960 : 62 : && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
17961 : : {
17962 : : /* Additionally, put firstprivate clause on task
17963 : : for the allocator if it is not constant. */
17964 : 34 : *gtask_clauses_ptr
17965 : 34 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17966 : : OMP_CLAUSE_FIRSTPRIVATE);
17967 : 34 : OMP_CLAUSE_DECL (*gtask_clauses_ptr)
17968 : 34 : = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
17969 : 34 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17970 : : }
17971 : 62 : if (lastprivate_uids
17972 : 97 : && bitmap_bit_p (lastprivate_uids,
17973 : 35 : DECL_UID (OMP_CLAUSE_DECL (c))))
17974 : : {
17975 : 17 : *gfor_clauses_ptr = c;
17976 : 17 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17977 : 17 : *gtask_clauses_ptr = copy_node (c);
17978 : 17 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17979 : : }
17980 : : else
17981 : : {
17982 : 45 : *gtask_clauses_ptr = c;
17983 : 45 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17984 : : }
17985 : : break;
17986 : 0 : default:
17987 : 0 : gcc_unreachable ();
17988 : : }
17989 : 1579 : *gfor_clauses_ptr = NULL_TREE;
17990 : 1579 : *gtask_clauses_ptr = NULL_TREE;
17991 : 1579 : *gforo_clauses_ptr = NULL_TREE;
17992 : 1579 : BITMAP_FREE (lastprivate_uids);
17993 : 1579 : gimple_set_location (gfor, input_location);
17994 : 1579 : g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
17995 : 1579 : g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
17996 : : NULL_TREE, NULL_TREE, NULL_TREE);
17997 : 1579 : gimple_set_location (g, input_location);
17998 : 1579 : gimple_omp_task_set_taskloop_p (g, true);
17999 : 1579 : g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
18000 : 1579 : gomp_for *gforo
18001 : 1579 : = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
18002 : : gimple_omp_for_collapse (gfor),
18003 : : gimple_omp_for_pre_body (gfor));
18004 : 1579 : gimple_omp_for_set_pre_body (gfor, NULL);
18005 : 1579 : gimple_omp_for_set_combined_p (gforo, true);
18006 : 1579 : gimple_omp_for_set_combined_into_p (gfor, true);
18007 : 3444 : for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
18008 : : {
18009 : 1865 : tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
18010 : 1865 : tree v = create_tmp_var (type);
18011 : 1865 : gimple_omp_for_set_index (gforo, i, v);
18012 : 1865 : t = unshare_expr (gimple_omp_for_initial (gfor, i));
18013 : 1865 : gimple_omp_for_set_initial (gforo, i, t);
18014 : 1865 : gimple_omp_for_set_cond (gforo, i,
18015 : : gimple_omp_for_cond (gfor, i));
18016 : 1865 : t = unshare_expr (gimple_omp_for_final (gfor, i));
18017 : 1865 : gimple_omp_for_set_final (gforo, i, t);
18018 : 1865 : t = unshare_expr (gimple_omp_for_incr (gfor, i));
18019 : 1865 : gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
18020 : 1865 : TREE_OPERAND (t, 0) = v;
18021 : 1865 : gimple_omp_for_set_incr (gforo, i, t);
18022 : 1865 : t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18023 : 1865 : OMP_CLAUSE_DECL (t) = v;
18024 : 1865 : OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
18025 : 1865 : gimple_omp_for_set_clauses (gforo, t);
18026 : 1865 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
18027 : : {
18028 : 90 : tree *p1 = NULL, *p2 = NULL;
18029 : 90 : t = gimple_omp_for_initial (gforo, i);
18030 : 90 : if (TREE_CODE (t) == TREE_VEC)
18031 : 34 : p1 = &TREE_VEC_ELT (t, 0);
18032 : 90 : t = gimple_omp_for_final (gforo, i);
18033 : 90 : if (TREE_CODE (t) == TREE_VEC)
18034 : : {
18035 : 31 : if (p1)
18036 : 23 : p2 = &TREE_VEC_ELT (t, 0);
18037 : : else
18038 : 8 : p1 = &TREE_VEC_ELT (t, 0);
18039 : : }
18040 : 90 : if (p1)
18041 : : {
18042 : : int j;
18043 : 58 : for (j = 0; j < i; j++)
18044 : 58 : if (*p1 == gimple_omp_for_index (gfor, j))
18045 : : {
18046 : 42 : *p1 = gimple_omp_for_index (gforo, j);
18047 : 42 : if (p2)
18048 : 23 : *p2 = *p1;
18049 : : break;
18050 : : }
18051 : 42 : gcc_assert (j < i);
18052 : : }
18053 : : }
18054 : : }
18055 : 1579 : gimplify_seq_add_stmt (pre_p, gforo);
18056 : : }
18057 : : else
18058 : 49641 : gimplify_seq_add_stmt (pre_p, gfor);
18059 : :
18060 : 51220 : if (TREE_CODE (orig_for_stmt) == OMP_FOR)
18061 : : {
18062 : 18178 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
18063 : 18178 : unsigned lastprivate_conditional = 0;
18064 : 18178 : while (ctx
18065 : 18214 : && (ctx->region_type == ORT_TARGET_DATA
18066 : 14201 : || ctx->region_type == ORT_TASKGROUP))
18067 : 36 : ctx = ctx->outer_context;
18068 : 18178 : if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
18069 : 13941 : for (tree c = gimple_omp_for_clauses (gfor);
18070 : 63193 : c; c = OMP_CLAUSE_CHAIN (c))
18071 : 49252 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18072 : 49252 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18073 : 158 : ++lastprivate_conditional;
18074 : 13941 : if (lastprivate_conditional)
18075 : : {
18076 : 118 : struct omp_for_data fd;
18077 : 118 : omp_extract_for_data (gfor, &fd, NULL);
18078 : 118 : tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
18079 : 118 : lastprivate_conditional);
18080 : 118 : tree var = create_tmp_var_raw (type);
18081 : 118 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
18082 : 118 : OMP_CLAUSE_DECL (c) = var;
18083 : 118 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18084 : 118 : gimple_omp_for_set_clauses (gfor, c);
18085 : 118 : omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
18086 : : }
18087 : : }
18088 : 33042 : else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
18089 : : {
18090 : 10642 : unsigned lastprivate_conditional = 0;
18091 : 49078 : for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
18092 : 38436 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18093 : 38436 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18094 : 119 : ++lastprivate_conditional;
18095 : 10642 : if (lastprivate_conditional)
18096 : : {
18097 : 91 : struct omp_for_data fd;
18098 : 91 : omp_extract_for_data (gfor, &fd, NULL);
18099 : 91 : tree type = unsigned_type_for (fd.iter_type);
18100 : 301 : while (lastprivate_conditional--)
18101 : : {
18102 : 119 : tree c = build_omp_clause (UNKNOWN_LOCATION,
18103 : : OMP_CLAUSE__CONDTEMP_);
18104 : 119 : OMP_CLAUSE_DECL (c) = create_tmp_var (type);
18105 : 119 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18106 : 119 : gimple_omp_for_set_clauses (gfor, c);
18107 : : }
18108 : : }
18109 : : }
18110 : :
18111 : 51220 : if (ret != GS_ALL_DONE)
18112 : : return GS_ERROR;
18113 : 51220 : *expr_p = NULL_TREE;
18114 : 51220 : return GS_ALL_DONE;
18115 : : }
18116 : :
18117 : : /* Helper for gimplify_omp_loop, called through walk_tree. */
18118 : :
18119 : : static tree
18120 : 294 : note_no_context_vars (tree *tp, int *, void *data)
18121 : : {
18122 : 294 : if (VAR_P (*tp)
18123 : 56 : && DECL_CONTEXT (*tp) == NULL_TREE
18124 : 309 : && !is_global_var (*tp))
18125 : : {
18126 : 15 : vec<tree> *d = (vec<tree> *) data;
18127 : 15 : d->safe_push (*tp);
18128 : 15 : DECL_CONTEXT (*tp) = current_function_decl;
18129 : : }
18130 : 294 : return NULL_TREE;
18131 : : }
18132 : :
18133 : : /* Gimplify the gross structure of an OMP_LOOP statement. */
18134 : :
18135 : : static enum gimplify_status
18136 : 1057 : gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
18137 : : {
18138 : 1057 : tree for_stmt = *expr_p;
18139 : 1057 : tree clauses = OMP_FOR_CLAUSES (for_stmt);
18140 : 1057 : struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
18141 : 1057 : enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
18142 : 1057 : int i;
18143 : :
18144 : 1057 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
18145 : 1057 : if (*expr_p != for_stmt)
18146 : : return GS_OK;
18147 : :
18148 : : /* If order is not present, the behavior is as if order(concurrent)
18149 : : appeared. */
18150 : 1057 : tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
18151 : 1057 : if (order == NULL_TREE)
18152 : : {
18153 : 804 : order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
18154 : 804 : OMP_CLAUSE_CHAIN (order) = clauses;
18155 : 804 : OMP_FOR_CLAUSES (for_stmt) = clauses = order;
18156 : : }
18157 : :
18158 : 1057 : tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
18159 : 1057 : if (bind == NULL_TREE)
18160 : : {
18161 : 579 : if (!flag_openmp) /* flag_openmp_simd */
18162 : : ;
18163 : 562 : else if (octx && (octx->region_type & ORT_TEAMS) != 0)
18164 : : kind = OMP_CLAUSE_BIND_TEAMS;
18165 : 337 : else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
18166 : : kind = OMP_CLAUSE_BIND_PARALLEL;
18167 : : else
18168 : : {
18169 : 187 : for (; octx; octx = octx->outer_context)
18170 : : {
18171 : 79 : if ((octx->region_type & ORT_ACC) != 0
18172 : 79 : || octx->region_type == ORT_NONE
18173 : 79 : || octx->region_type == ORT_IMPLICIT_TARGET)
18174 : 0 : continue;
18175 : : break;
18176 : : }
18177 : 187 : if (octx == NULL && !in_omp_construct)
18178 : 4 : error_at (EXPR_LOCATION (for_stmt),
18179 : : "%<bind%> clause not specified on a %<loop%> "
18180 : : "construct not nested inside another OpenMP construct");
18181 : : }
18182 : 579 : bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
18183 : 579 : OMP_CLAUSE_CHAIN (bind) = clauses;
18184 : 579 : OMP_CLAUSE_BIND_KIND (bind) = kind;
18185 : 579 : OMP_FOR_CLAUSES (for_stmt) = bind;
18186 : : }
18187 : : else
18188 : 478 : switch (OMP_CLAUSE_BIND_KIND (bind))
18189 : : {
18190 : : case OMP_CLAUSE_BIND_THREAD:
18191 : : break;
18192 : 166 : case OMP_CLAUSE_BIND_PARALLEL:
18193 : 166 : if (!flag_openmp) /* flag_openmp_simd */
18194 : : {
18195 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18196 : 0 : break;
18197 : : }
18198 : 240 : for (; octx; octx = octx->outer_context)
18199 : 78 : if (octx->region_type == ORT_SIMD
18200 : 78 : && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
18201 : : {
18202 : 4 : error_at (EXPR_LOCATION (for_stmt),
18203 : : "%<bind(parallel)%> on a %<loop%> construct nested "
18204 : : "inside %<simd%> construct");
18205 : 4 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18206 : 4 : break;
18207 : : }
18208 : : kind = OMP_CLAUSE_BIND_PARALLEL;
18209 : : break;
18210 : 158 : case OMP_CLAUSE_BIND_TEAMS:
18211 : 158 : if (!flag_openmp) /* flag_openmp_simd */
18212 : : {
18213 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18214 : 0 : break;
18215 : : }
18216 : 158 : if ((octx
18217 : 128 : && octx->region_type != ORT_IMPLICIT_TARGET
18218 : 111 : && octx->region_type != ORT_NONE
18219 : 111 : && (octx->region_type & ORT_TEAMS) == 0)
18220 : 106 : || in_omp_construct)
18221 : : {
18222 : 64 : error_at (EXPR_LOCATION (for_stmt),
18223 : : "%<bind(teams)%> on a %<loop%> region not strictly "
18224 : : "nested inside of a %<teams%> region");
18225 : 64 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18226 : 64 : break;
18227 : : }
18228 : : kind = OMP_CLAUSE_BIND_TEAMS;
18229 : : break;
18230 : 0 : default:
18231 : 0 : gcc_unreachable ();
18232 : : }
18233 : :
18234 : 4268 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
18235 : 3211 : switch (OMP_CLAUSE_CODE (*pc))
18236 : : {
18237 : 343 : case OMP_CLAUSE_REDUCTION:
18238 : 343 : if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
18239 : : {
18240 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18241 : : "%<inscan%> %<reduction%> clause on "
18242 : : "%qs construct", "loop");
18243 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
18244 : : }
18245 : 343 : if (OMP_CLAUSE_REDUCTION_TASK (*pc))
18246 : : {
18247 : 8 : error_at (OMP_CLAUSE_LOCATION (*pc),
18248 : : "invalid %<task%> reduction modifier on construct "
18249 : : "other than %<parallel%>, %qs or %<sections%>",
18250 : 4 : lang_GNU_Fortran () ? "do" : "for");
18251 : 4 : OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
18252 : : }
18253 : 343 : pc = &OMP_CLAUSE_CHAIN (*pc);
18254 : 343 : break;
18255 : : case OMP_CLAUSE_LASTPRIVATE:
18256 : 293 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18257 : : {
18258 : 289 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
18259 : 289 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
18260 : 289 : if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
18261 : : break;
18262 : 26 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18263 : 25 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18264 : : i)) == TREE_LIST
18265 : 48 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18266 : : i)))
18267 : : {
18268 : 22 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18269 : 22 : if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
18270 : : break;
18271 : : }
18272 : : }
18273 : 289 : if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
18274 : : {
18275 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18276 : : "%<lastprivate%> clause on a %<loop%> construct refers "
18277 : : "to a variable %qD which is not the loop iterator",
18278 : 4 : OMP_CLAUSE_DECL (*pc));
18279 : 4 : *pc = OMP_CLAUSE_CHAIN (*pc);
18280 : 4 : break;
18281 : : }
18282 : 285 : pc = &OMP_CLAUSE_CHAIN (*pc);
18283 : 285 : break;
18284 : 2579 : default:
18285 : 2579 : pc = &OMP_CLAUSE_CHAIN (*pc);
18286 : 2579 : break;
18287 : : }
18288 : :
18289 : 1057 : TREE_SET_CODE (for_stmt, OMP_SIMD);
18290 : :
18291 : 1057 : int last;
18292 : 1057 : switch (kind)
18293 : : {
18294 : : case OMP_CLAUSE_BIND_THREAD: last = 0; break;
18295 : 424 : case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
18296 : 211 : case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
18297 : : }
18298 : 1903 : for (int pass = 1; pass <= last; pass++)
18299 : : {
18300 : 846 : if (pass == 2)
18301 : : {
18302 : 211 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
18303 : : make_node (BLOCK));
18304 : 211 : append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
18305 : 211 : *expr_p = make_node (OMP_PARALLEL);
18306 : 211 : TREE_TYPE (*expr_p) = void_type_node;
18307 : 211 : OMP_PARALLEL_BODY (*expr_p) = bind;
18308 : 211 : OMP_PARALLEL_COMBINED (*expr_p) = 1;
18309 : 211 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
18310 : 211 : tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
18311 : 425 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18312 : 214 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18313 : 214 : && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
18314 : : == TREE_LIST))
18315 : : {
18316 : 16 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18317 : 16 : if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
18318 : : {
18319 : 13 : *pc = build_omp_clause (UNKNOWN_LOCATION,
18320 : : OMP_CLAUSE_FIRSTPRIVATE);
18321 : 13 : OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
18322 : 13 : pc = &OMP_CLAUSE_CHAIN (*pc);
18323 : : }
18324 : : }
18325 : : }
18326 : 846 : tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
18327 : 846 : tree *pc = &OMP_FOR_CLAUSES (t);
18328 : 846 : TREE_TYPE (t) = void_type_node;
18329 : 846 : OMP_FOR_BODY (t) = *expr_p;
18330 : 846 : SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
18331 : 3914 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
18332 : 3068 : switch (OMP_CLAUSE_CODE (c))
18333 : : {
18334 : 1958 : case OMP_CLAUSE_BIND:
18335 : 1958 : case OMP_CLAUSE_ORDER:
18336 : 1958 : case OMP_CLAUSE_COLLAPSE:
18337 : 1958 : *pc = copy_node (c);
18338 : 1958 : pc = &OMP_CLAUSE_CHAIN (*pc);
18339 : 1958 : break;
18340 : : case OMP_CLAUSE_PRIVATE:
18341 : : case OMP_CLAUSE_FIRSTPRIVATE:
18342 : : /* Only needed on innermost. */
18343 : : break;
18344 : 351 : case OMP_CLAUSE_LASTPRIVATE:
18345 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
18346 : : {
18347 : 7 : *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18348 : : OMP_CLAUSE_FIRSTPRIVATE);
18349 : 7 : OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
18350 : 7 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18351 : 7 : pc = &OMP_CLAUSE_CHAIN (*pc);
18352 : : }
18353 : 351 : *pc = copy_node (c);
18354 : 351 : OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
18355 : 351 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18356 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
18357 : : {
18358 : 22 : if (pass != last)
18359 : 7 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
18360 : : else
18361 : 15 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18362 : 22 : OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
18363 : : }
18364 : 351 : pc = &OMP_CLAUSE_CHAIN (*pc);
18365 : 351 : break;
18366 : 463 : case OMP_CLAUSE_REDUCTION:
18367 : 463 : *pc = copy_node (c);
18368 : 463 : OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
18369 : 463 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18370 : 463 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
18371 : : {
18372 : 15 : auto_vec<tree> no_context_vars;
18373 : 15 : int walk_subtrees = 0;
18374 : 15 : note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18375 : : &walk_subtrees, &no_context_vars);
18376 : 15 : if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
18377 : 0 : note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
18378 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
18379 : : note_no_context_vars,
18380 : : &no_context_vars);
18381 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
18382 : : note_no_context_vars,
18383 : : &no_context_vars);
18384 : :
18385 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
18386 : 15 : = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
18387 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18388 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
18389 : 0 : = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
18390 : :
18391 : 15 : hash_map<tree, tree> decl_map;
18392 : 15 : decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
18393 : 15 : decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18394 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
18395 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18396 : 0 : decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
18397 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
18398 : :
18399 : 15 : copy_body_data id;
18400 : 15 : memset (&id, 0, sizeof (id));
18401 : 15 : id.src_fn = current_function_decl;
18402 : 15 : id.dst_fn = current_function_decl;
18403 : 15 : id.src_cfun = cfun;
18404 : 15 : id.decl_map = &decl_map;
18405 : 15 : id.copy_decl = copy_decl_no_change;
18406 : 15 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
18407 : 15 : id.transform_new_cfg = true;
18408 : 15 : id.transform_return_to_modify = false;
18409 : 15 : id.eh_lp_nr = 0;
18410 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
18411 : : &id, NULL);
18412 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
18413 : : &id, NULL);
18414 : :
18415 : 60 : for (tree d : no_context_vars)
18416 : : {
18417 : 15 : DECL_CONTEXT (d) = NULL_TREE;
18418 : 15 : DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
18419 : : }
18420 : 15 : }
18421 : : else
18422 : : {
18423 : 448 : OMP_CLAUSE_REDUCTION_INIT (*pc)
18424 : 448 : = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
18425 : 448 : OMP_CLAUSE_REDUCTION_MERGE (*pc)
18426 : 896 : = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
18427 : : }
18428 : 463 : pc = &OMP_CLAUSE_CHAIN (*pc);
18429 : 463 : break;
18430 : 0 : default:
18431 : 0 : gcc_unreachable ();
18432 : : }
18433 : 846 : *pc = NULL_TREE;
18434 : 846 : *expr_p = t;
18435 : : }
18436 : 1057 : return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
18437 : : }
18438 : :
18439 : :
18440 : : /* Helper function of optimize_target_teams, find OMP_TEAMS inside
18441 : : of OMP_TARGET's body. */
18442 : :
18443 : : static tree
18444 : 80615 : find_omp_teams (tree *tp, int *walk_subtrees, void *)
18445 : : {
18446 : 80615 : *walk_subtrees = 0;
18447 : 80615 : switch (TREE_CODE (*tp))
18448 : : {
18449 : : case OMP_TEAMS:
18450 : : return *tp;
18451 : 23923 : case BIND_EXPR:
18452 : 23923 : case STATEMENT_LIST:
18453 : 23923 : *walk_subtrees = 1;
18454 : 23923 : break;
18455 : : default:
18456 : : break;
18457 : : }
18458 : : return NULL_TREE;
18459 : : }
18460 : :
18461 : : /* Helper function of optimize_target_teams, determine if the expression
18462 : : can be computed safely before the target construct on the host. */
18463 : :
18464 : : static tree
18465 : 1311 : computable_teams_clause (tree *tp, int *walk_subtrees, void *)
18466 : : {
18467 : 1831 : splay_tree_node n;
18468 : :
18469 : 1831 : if (TYPE_P (*tp))
18470 : : {
18471 : 0 : *walk_subtrees = 0;
18472 : 0 : return NULL_TREE;
18473 : : }
18474 : 1831 : switch (TREE_CODE (*tp))
18475 : : {
18476 : 990 : case VAR_DECL:
18477 : 990 : case PARM_DECL:
18478 : 990 : case RESULT_DECL:
18479 : 990 : *walk_subtrees = 0;
18480 : 990 : if (error_operand_p (*tp)
18481 : 990 : || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
18482 : 990 : || DECL_HAS_VALUE_EXPR_P (*tp)
18483 : 990 : || DECL_THREAD_LOCAL_P (*tp)
18484 : 990 : || TREE_SIDE_EFFECTS (*tp)
18485 : 1980 : || TREE_THIS_VOLATILE (*tp))
18486 : 0 : return *tp;
18487 : 990 : if (is_global_var (*tp)
18488 : 990 : && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
18489 : 0 : || lookup_attribute ("omp declare target link",
18490 : 0 : DECL_ATTRIBUTES (*tp))))
18491 : 16 : return *tp;
18492 : 974 : if (VAR_P (*tp)
18493 : 668 : && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
18494 : 50 : && !is_global_var (*tp)
18495 : 1024 : && decl_function_context (*tp) == current_function_decl)
18496 : 50 : return *tp;
18497 : 1848 : n = splay_tree_lookup (gimplify_omp_ctxp->variables,
18498 : 924 : (splay_tree_key) *tp);
18499 : 924 : if (n == NULL)
18500 : : {
18501 : 292 : if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
18502 : : return NULL_TREE;
18503 : 24 : return *tp;
18504 : : }
18505 : 632 : else if (n->value & GOVD_LOCAL)
18506 : 0 : return *tp;
18507 : 632 : else if (n->value & GOVD_FIRSTPRIVATE)
18508 : : return NULL_TREE;
18509 : 112 : else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18510 : : == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18511 : : return NULL_TREE;
18512 : 96 : return *tp;
18513 : 76 : case INTEGER_CST:
18514 : 76 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18515 : : return *tp;
18516 : : return NULL_TREE;
18517 : 520 : case TARGET_EXPR:
18518 : 520 : if (TARGET_EXPR_INITIAL (*tp)
18519 : 520 : || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
18520 : : return *tp;
18521 : 520 : return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
18522 : 520 : walk_subtrees, NULL);
18523 : : /* Allow some reasonable subset of integral arithmetics. */
18524 : 193 : case PLUS_EXPR:
18525 : 193 : case MINUS_EXPR:
18526 : 193 : case MULT_EXPR:
18527 : 193 : case TRUNC_DIV_EXPR:
18528 : 193 : case CEIL_DIV_EXPR:
18529 : 193 : case FLOOR_DIV_EXPR:
18530 : 193 : case ROUND_DIV_EXPR:
18531 : 193 : case TRUNC_MOD_EXPR:
18532 : 193 : case CEIL_MOD_EXPR:
18533 : 193 : case FLOOR_MOD_EXPR:
18534 : 193 : case ROUND_MOD_EXPR:
18535 : 193 : case RDIV_EXPR:
18536 : 193 : case EXACT_DIV_EXPR:
18537 : 193 : case MIN_EXPR:
18538 : 193 : case MAX_EXPR:
18539 : 193 : case LSHIFT_EXPR:
18540 : 193 : case RSHIFT_EXPR:
18541 : 193 : case BIT_IOR_EXPR:
18542 : 193 : case BIT_XOR_EXPR:
18543 : 193 : case BIT_AND_EXPR:
18544 : 193 : case NEGATE_EXPR:
18545 : 193 : case ABS_EXPR:
18546 : 193 : case BIT_NOT_EXPR:
18547 : 193 : case NON_LVALUE_EXPR:
18548 : 193 : CASE_CONVERT:
18549 : 193 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18550 : : return *tp;
18551 : : return NULL_TREE;
18552 : : /* And disallow anything else, except for comparisons. */
18553 : 52 : default:
18554 : 52 : if (COMPARISON_CLASS_P (*tp))
18555 : : return NULL_TREE;
18556 : : return *tp;
18557 : : }
18558 : : }
18559 : :
18560 : : /* Try to determine if the num_teams and/or thread_limit expressions
18561 : : can have their values determined already before entering the
18562 : : target construct.
18563 : : INTEGER_CSTs trivially are,
18564 : : integral decls that are firstprivate (explicitly or implicitly)
18565 : : or explicitly map(always, to:) or map(always, tofrom:) on the target
18566 : : region too, and expressions involving simple arithmetics on those
18567 : : too, function calls are not ok, dereferencing something neither etc.
18568 : : Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
18569 : : EXPR based on what we find:
18570 : : 0 stands for clause not specified at all, use implementation default
18571 : : -1 stands for value that can't be determined easily before entering
18572 : : the target construct.
18573 : : -2 means that no explicit teams construct was specified
18574 : : If teams construct is not present at all, use 1 for num_teams
18575 : : and 0 for thread_limit (only one team is involved, and the thread
18576 : : limit is implementation defined. */
18577 : :
18578 : : static void
18579 : 12589 : optimize_target_teams (tree target, gimple_seq *pre_p)
18580 : : {
18581 : 12589 : tree body = OMP_BODY (target);
18582 : 12589 : tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
18583 : 12589 : tree num_teams_lower = NULL_TREE;
18584 : 12589 : tree num_teams_upper = integer_zero_node;
18585 : 12589 : tree thread_limit = integer_zero_node;
18586 : 12589 : location_t num_teams_loc = EXPR_LOCATION (target);
18587 : 12589 : location_t thread_limit_loc = EXPR_LOCATION (target);
18588 : 12589 : tree c, *p, expr;
18589 : 12589 : struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
18590 : :
18591 : 12589 : if (teams == NULL_TREE)
18592 : 6487 : num_teams_upper = build_int_cst (integer_type_node, -2);
18593 : : else
18594 : 10573 : for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
18595 : : {
18596 : 4471 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
18597 : : {
18598 : 617 : p = &num_teams_upper;
18599 : 617 : num_teams_loc = OMP_CLAUSE_LOCATION (c);
18600 : 617 : if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
18601 : : {
18602 : 148 : expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
18603 : 148 : if (TREE_CODE (expr) == INTEGER_CST)
18604 : 21 : num_teams_lower = expr;
18605 : 127 : else if (walk_tree (&expr, computable_teams_clause,
18606 : : NULL, NULL))
18607 : 19 : num_teams_lower = integer_minus_one_node;
18608 : : else
18609 : : {
18610 : 108 : num_teams_lower = expr;
18611 : 108 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18612 : 108 : if (gimplify_expr (&num_teams_lower, pre_p, NULL,
18613 : : is_gimple_val, fb_rvalue, false)
18614 : : == GS_ERROR)
18615 : : {
18616 : 0 : gimplify_omp_ctxp = target_ctx;
18617 : 0 : num_teams_lower = integer_minus_one_node;
18618 : : }
18619 : : else
18620 : : {
18621 : 108 : gimplify_omp_ctxp = target_ctx;
18622 : 108 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18623 : 28 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
18624 : 56 : = num_teams_lower;
18625 : : }
18626 : : }
18627 : : }
18628 : : }
18629 : 3854 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18630 : : {
18631 : 471 : p = &thread_limit;
18632 : 471 : thread_limit_loc = OMP_CLAUSE_LOCATION (c);
18633 : : }
18634 : : else
18635 : 3383 : continue;
18636 : 1088 : expr = OMP_CLAUSE_OPERAND (c, 0);
18637 : 1088 : if (TREE_CODE (expr) == INTEGER_CST)
18638 : : {
18639 : 173 : *p = expr;
18640 : 173 : continue;
18641 : : }
18642 : 915 : if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
18643 : : {
18644 : 219 : *p = integer_minus_one_node;
18645 : 219 : continue;
18646 : : }
18647 : 696 : *p = expr;
18648 : 696 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18649 : 696 : if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
18650 : : == GS_ERROR)
18651 : : {
18652 : 0 : gimplify_omp_ctxp = target_ctx;
18653 : 0 : *p = integer_minus_one_node;
18654 : 0 : continue;
18655 : : }
18656 : 696 : gimplify_omp_ctxp = target_ctx;
18657 : 696 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18658 : 48 : OMP_CLAUSE_OPERAND (c, 0) = *p;
18659 : : }
18660 : 12589 : if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
18661 : : {
18662 : 12321 : c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
18663 : 12321 : OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
18664 : 12321 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18665 : 12321 : OMP_TARGET_CLAUSES (target) = c;
18666 : : }
18667 : 12589 : c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
18668 : 12589 : OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
18669 : 12589 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
18670 : 12589 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18671 : 12589 : OMP_TARGET_CLAUSES (target) = c;
18672 : 12589 : }
18673 : :
18674 : : /* Gimplify the gross structure of several OMP constructs. */
18675 : :
18676 : : static void
18677 : 38958 : gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
18678 : : {
18679 : 38958 : tree expr = *expr_p;
18680 : 38958 : gimple *stmt;
18681 : 38958 : gimple_seq body = NULL;
18682 : 38958 : enum omp_region_type ort;
18683 : :
18684 : 38958 : switch (TREE_CODE (expr))
18685 : : {
18686 : : case OMP_SECTIONS:
18687 : : case OMP_SINGLE:
18688 : : ort = ORT_WORKSHARE;
18689 : : break;
18690 : 214 : case OMP_SCOPE:
18691 : 214 : ort = ORT_TASKGROUP;
18692 : 214 : break;
18693 : 12589 : case OMP_TARGET:
18694 : 12589 : ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
18695 : : break;
18696 : 2564 : case OACC_KERNELS:
18697 : 2564 : ort = ORT_ACC_KERNELS;
18698 : 2564 : break;
18699 : 7903 : case OACC_PARALLEL:
18700 : 7903 : ort = ORT_ACC_PARALLEL;
18701 : 7903 : break;
18702 : 1041 : case OACC_SERIAL:
18703 : 1041 : ort = ORT_ACC_SERIAL;
18704 : 1041 : break;
18705 : 1953 : case OACC_DATA:
18706 : 1953 : ort = ORT_ACC_DATA;
18707 : 1953 : break;
18708 : 1863 : case OMP_TARGET_DATA:
18709 : 1863 : ort = ORT_TARGET_DATA;
18710 : 1863 : break;
18711 : 8813 : case OMP_TEAMS:
18712 : 8813 : ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
18713 : 8813 : if (gimplify_omp_ctxp == NULL
18714 : 6142 : || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
18715 : 2671 : ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
18716 : : break;
18717 : 118 : case OACC_HOST_DATA:
18718 : 118 : ort = ORT_ACC_HOST_DATA;
18719 : 118 : break;
18720 : 0 : default:
18721 : 0 : gcc_unreachable ();
18722 : : }
18723 : :
18724 : 38958 : gimple_seq iterator_loops_seq = NULL;
18725 : 38958 : if (TREE_CODE (expr) == OMP_TARGET)
18726 : : {
18727 : 12589 : remove_unused_omp_iterator_vars (&OMP_CLAUSES (expr));
18728 : 12589 : build_omp_iterators_loops (&OMP_CLAUSES (expr), &iterator_loops_seq);
18729 : : }
18730 : :
18731 : 38958 : bool save_in_omp_construct = in_omp_construct;
18732 : 38958 : if ((ort & ORT_ACC) == 0)
18733 : 25379 : in_omp_construct = false;
18734 : 38958 : gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
18735 : 38958 : TREE_CODE (expr), &iterator_loops_seq);
18736 : 38958 : if (TREE_CODE (expr) == OMP_TARGET)
18737 : 12589 : optimize_target_teams (expr, pre_p);
18738 : 38958 : if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
18739 : 10927 : || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18740 : : {
18741 : 30702 : push_gimplify_context ();
18742 : 30702 : gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
18743 : 30702 : if (gimple_code (g) == GIMPLE_BIND)
18744 : 30702 : pop_gimplify_context (g);
18745 : : else
18746 : 0 : pop_gimplify_context (NULL);
18747 : 30702 : if ((ort & ORT_TARGET_DATA) != 0)
18748 : : {
18749 : 3934 : enum built_in_function end_ix;
18750 : 3934 : switch (TREE_CODE (expr))
18751 : : {
18752 : : case OACC_DATA:
18753 : : case OACC_HOST_DATA:
18754 : : end_ix = BUILT_IN_GOACC_DATA_END;
18755 : : break;
18756 : 1863 : case OMP_TARGET_DATA:
18757 : 1863 : end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
18758 : 1863 : break;
18759 : 0 : default:
18760 : 0 : gcc_unreachable ();
18761 : : }
18762 : 3934 : tree fn = builtin_decl_explicit (end_ix);
18763 : 3934 : g = gimple_build_call (fn, 0);
18764 : 3934 : gimple_seq cleanup = NULL;
18765 : 3934 : gimple_seq_add_stmt (&cleanup, g);
18766 : 3934 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
18767 : 3934 : body = NULL;
18768 : 3934 : gimple_seq_add_stmt (&body, g);
18769 : : }
18770 : : }
18771 : : else
18772 : 8256 : gimplify_and_add (OMP_BODY (expr), &body);
18773 : 38958 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
18774 : 38958 : TREE_CODE (expr), &iterator_loops_seq);
18775 : 38958 : in_omp_construct = save_in_omp_construct;
18776 : :
18777 : 38958 : switch (TREE_CODE (expr))
18778 : : {
18779 : 1953 : case OACC_DATA:
18780 : 3906 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
18781 : 1953 : OMP_CLAUSES (expr));
18782 : 1953 : break;
18783 : 118 : case OACC_HOST_DATA:
18784 : 118 : if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
18785 : : {
18786 : 128 : for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18787 : 95 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
18788 : 57 : OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
18789 : : }
18790 : :
18791 : 236 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
18792 : 118 : OMP_CLAUSES (expr));
18793 : 118 : break;
18794 : 2564 : case OACC_KERNELS:
18795 : 5128 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
18796 : 2564 : OMP_CLAUSES (expr));
18797 : 2564 : break;
18798 : 7903 : case OACC_PARALLEL:
18799 : 15806 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
18800 : 7903 : OMP_CLAUSES (expr));
18801 : 7903 : break;
18802 : 1041 : case OACC_SERIAL:
18803 : 2082 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
18804 : 1041 : OMP_CLAUSES (expr));
18805 : 1041 : break;
18806 : 626 : case OMP_SECTIONS:
18807 : 626 : stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
18808 : 626 : break;
18809 : 1274 : case OMP_SINGLE:
18810 : 1274 : stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
18811 : 1274 : break;
18812 : 214 : case OMP_SCOPE:
18813 : 214 : stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
18814 : 214 : break;
18815 : 12589 : case OMP_TARGET:
18816 : 25178 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
18817 : 12589 : OMP_CLAUSES (expr), iterator_loops_seq);
18818 : 12589 : break;
18819 : 1863 : case OMP_TARGET_DATA:
18820 : : /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
18821 : : to be evaluated before the use_device_{ptr,addr} clauses if they
18822 : : refer to the same variables. */
18823 : 1863 : {
18824 : 1863 : tree use_device_clauses;
18825 : 1863 : tree *pc, *uc = &use_device_clauses;
18826 : 9916 : for (pc = &OMP_CLAUSES (expr); *pc; )
18827 : 8053 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
18828 : 8053 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
18829 : : {
18830 : 1959 : *uc = *pc;
18831 : 1959 : *pc = OMP_CLAUSE_CHAIN (*pc);
18832 : 1959 : uc = &OMP_CLAUSE_CHAIN (*uc);
18833 : : }
18834 : : else
18835 : 6094 : pc = &OMP_CLAUSE_CHAIN (*pc);
18836 : 1863 : *uc = NULL_TREE;
18837 : 1863 : *pc = use_device_clauses;
18838 : 1863 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
18839 : 1863 : OMP_CLAUSES (expr));
18840 : : }
18841 : 1863 : break;
18842 : 8813 : case OMP_TEAMS:
18843 : 8813 : stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
18844 : 8813 : if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18845 : 2671 : gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
18846 : : break;
18847 : 0 : default:
18848 : 0 : gcc_unreachable ();
18849 : : }
18850 : :
18851 : 38958 : gimplify_seq_add_stmt (pre_p, stmt);
18852 : 38958 : *expr_p = NULL_TREE;
18853 : 38958 : }
18854 : :
18855 : : /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
18856 : : target update constructs. */
18857 : :
18858 : : static void
18859 : 11928 : gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
18860 : : {
18861 : 11928 : tree expr = *expr_p;
18862 : 11928 : int kind;
18863 : 11928 : gomp_target *stmt;
18864 : 11928 : enum omp_region_type ort = ORT_WORKSHARE;
18865 : :
18866 : 11928 : switch (TREE_CODE (expr))
18867 : : {
18868 : : case OACC_ENTER_DATA:
18869 : : kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
18870 : : ort = ORT_ACC;
18871 : : break;
18872 : : case OACC_EXIT_DATA:
18873 : : kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
18874 : : ort = ORT_ACC;
18875 : : break;
18876 : : case OACC_UPDATE:
18877 : : kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
18878 : : ort = ORT_ACC;
18879 : : break;
18880 : : case OMP_TARGET_UPDATE:
18881 : : kind = GF_OMP_TARGET_KIND_UPDATE;
18882 : : break;
18883 : : case OMP_TARGET_ENTER_DATA:
18884 : : kind = GF_OMP_TARGET_KIND_ENTER_DATA;
18885 : : break;
18886 : : case OMP_TARGET_EXIT_DATA:
18887 : : kind = GF_OMP_TARGET_KIND_EXIT_DATA;
18888 : : break;
18889 : 0 : default:
18890 : 0 : gcc_unreachable ();
18891 : : }
18892 : :
18893 : 11928 : gimple_seq iterator_loops_seq = NULL;
18894 : 11928 : remove_unused_omp_iterator_vars (&OMP_STANDALONE_CLAUSES (expr));
18895 : 11928 : build_omp_iterators_loops (&OMP_STANDALONE_CLAUSES (expr),
18896 : : &iterator_loops_seq);
18897 : :
18898 : 11928 : gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
18899 : 11928 : ort, TREE_CODE (expr), &iterator_loops_seq);
18900 : 11928 : gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
18901 : 11928 : TREE_CODE (expr), &iterator_loops_seq);
18902 : 11928 : if (TREE_CODE (expr) == OACC_UPDATE
18903 : 11928 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18904 : : OMP_CLAUSE_IF_PRESENT))
18905 : : {
18906 : : /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
18907 : : clause. */
18908 : 123 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18909 : 97 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18910 : 47 : switch (OMP_CLAUSE_MAP_KIND (c))
18911 : : {
18912 : 14 : case GOMP_MAP_FORCE_TO:
18913 : 14 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
18914 : 14 : break;
18915 : 24 : case GOMP_MAP_FORCE_FROM:
18916 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
18917 : 24 : break;
18918 : : default:
18919 : : break;
18920 : : }
18921 : : }
18922 : 11902 : else if (TREE_CODE (expr) == OACC_EXIT_DATA
18923 : 11902 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18924 : : OMP_CLAUSE_FINALIZE))
18925 : : {
18926 : : /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
18927 : : semantics. */
18928 : 69 : bool have_clause = false;
18929 : 253 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18930 : 184 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18931 : 111 : switch (OMP_CLAUSE_MAP_KIND (c))
18932 : : {
18933 : 41 : case GOMP_MAP_FROM:
18934 : 41 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
18935 : 41 : have_clause = true;
18936 : 41 : break;
18937 : 43 : case GOMP_MAP_RELEASE:
18938 : 43 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
18939 : 43 : have_clause = true;
18940 : 43 : break;
18941 : : case GOMP_MAP_TO_PSET:
18942 : : /* Fortran arrays with descriptors must map that descriptor when
18943 : : doing standalone "attach" operations (in OpenACC). In that
18944 : : case GOMP_MAP_TO_PSET appears by itself with no preceding
18945 : : clause (see trans-openmp.cc:gfc_trans_omp_clauses). */
18946 : : break;
18947 : 2 : case GOMP_MAP_POINTER:
18948 : : /* TODO PR92929: we may see these here, but they'll always follow
18949 : : one of the clauses above, and will be handled by libgomp as
18950 : : one group, so no handling required here. */
18951 : 2 : gcc_assert (have_clause);
18952 : : break;
18953 : 24 : case GOMP_MAP_DETACH:
18954 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
18955 : 24 : have_clause = false;
18956 : 24 : break;
18957 : : case GOMP_MAP_STRUCT:
18958 : : case GOMP_MAP_STRUCT_UNORD:
18959 : 24 : have_clause = false;
18960 : : break;
18961 : 0 : default:
18962 : 0 : gcc_unreachable ();
18963 : : }
18964 : : }
18965 : 11928 : stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr),
18966 : : iterator_loops_seq);
18967 : :
18968 : 11928 : gimplify_seq_add_stmt (pre_p, stmt);
18969 : 11928 : *expr_p = NULL_TREE;
18970 : 11928 : }
18971 : :
18972 : : /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
18973 : : stabilized the lhs of the atomic operation as *ADDR. Return true if
18974 : : EXPR is this stabilized form. */
18975 : :
18976 : : static bool
18977 : 36820 : goa_lhs_expr_p (tree expr, tree addr)
18978 : : {
18979 : : /* Also include casts to other type variants. The C front end is fond
18980 : : of adding these for e.g. volatile variables. This is like
18981 : : STRIP_TYPE_NOPS but includes the main variant lookup. */
18982 : 36820 : STRIP_USELESS_TYPE_CONVERSION (expr);
18983 : :
18984 : 36820 : if (INDIRECT_REF_P (expr))
18985 : : {
18986 : 5020 : expr = TREE_OPERAND (expr, 0);
18987 : 5020 : while (expr != addr
18988 : 92 : && (CONVERT_EXPR_P (expr)
18989 : 92 : || TREE_CODE (expr) == NON_LVALUE_EXPR)
18990 : 0 : && TREE_CODE (expr) == TREE_CODE (addr)
18991 : 5020 : && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
18992 : : {
18993 : 0 : expr = TREE_OPERAND (expr, 0);
18994 : 0 : addr = TREE_OPERAND (addr, 0);
18995 : : }
18996 : 5020 : if (expr == addr)
18997 : : return true;
18998 : 92 : return (TREE_CODE (addr) == ADDR_EXPR
18999 : 62 : && TREE_CODE (expr) == ADDR_EXPR
19000 : 92 : && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
19001 : : }
19002 : 31800 : if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
19003 : : return true;
19004 : : return false;
19005 : : }
19006 : :
19007 : : /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
19008 : : expression does not involve the lhs, evaluate it into a temporary.
19009 : : Return 1 if the lhs appeared as a subexpression, 0 if it did not,
19010 : : or -1 if an error was encountered. */
19011 : :
19012 : : static int
19013 : 36820 : goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
19014 : : tree lhs_var, tree &target_expr, bool rhs, int depth)
19015 : : {
19016 : 36820 : tree expr = *expr_p;
19017 : 36820 : int saw_lhs = 0;
19018 : :
19019 : 36820 : if (goa_lhs_expr_p (expr, lhs_addr))
19020 : : {
19021 : 9191 : if (pre_p)
19022 : 8533 : *expr_p = lhs_var;
19023 : 9191 : return 1;
19024 : : }
19025 : 27629 : if (is_gimple_val (expr))
19026 : : return 0;
19027 : :
19028 : : /* Maximum depth of lhs in expression is for the
19029 : : __builtin_clear_padding (...), __builtin_clear_padding (...),
19030 : : __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs; */
19031 : 17297 : if (++depth > 7)
19032 : 16 : goto finish;
19033 : :
19034 : 17281 : switch (TREE_CODE_CLASS (TREE_CODE (expr)))
19035 : : {
19036 : 9660 : case tcc_binary:
19037 : 9660 : case tcc_comparison:
19038 : 9660 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
19039 : : lhs_var, target_expr, true, depth);
19040 : : /* FALLTHRU */
19041 : 11707 : case tcc_unary:
19042 : 11707 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
19043 : : lhs_var, target_expr, true, depth);
19044 : 11707 : break;
19045 : 2982 : case tcc_expression:
19046 : 2982 : switch (TREE_CODE (expr))
19047 : : {
19048 : 836 : case TRUTH_ANDIF_EXPR:
19049 : 836 : case TRUTH_ORIF_EXPR:
19050 : 836 : case TRUTH_AND_EXPR:
19051 : 836 : case TRUTH_OR_EXPR:
19052 : 836 : case TRUTH_XOR_EXPR:
19053 : 836 : case BIT_INSERT_EXPR:
19054 : 836 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19055 : : lhs_addr, lhs_var, target_expr, true,
19056 : : depth);
19057 : : /* FALLTHRU */
19058 : 888 : case TRUTH_NOT_EXPR:
19059 : 888 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19060 : : lhs_addr, lhs_var, target_expr, true,
19061 : : depth);
19062 : 888 : break;
19063 : 550 : case MODIFY_EXPR:
19064 : 550 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19065 : : target_expr, true, depth))
19066 : : break;
19067 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19068 : : lhs_addr, lhs_var, target_expr, true,
19069 : : depth);
19070 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19071 : : lhs_addr, lhs_var, target_expr, false,
19072 : : depth);
19073 : 544 : break;
19074 : : /* FALLTHRU */
19075 : 164 : case ADDR_EXPR:
19076 : 164 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19077 : : target_expr, true, depth))
19078 : : break;
19079 : 138 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19080 : : lhs_addr, lhs_var, target_expr, false,
19081 : : depth);
19082 : 138 : break;
19083 : : case COMPOUND_EXPR:
19084 : : /* Break out any preevaluations from cp_build_modify_expr. */
19085 : 76 : for (; TREE_CODE (expr) == COMPOUND_EXPR;
19086 : 38 : expr = TREE_OPERAND (expr, 1))
19087 : : {
19088 : : /* Special-case __builtin_clear_padding call before
19089 : : __builtin_memcmp. */
19090 : 38 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
19091 : : {
19092 : 2 : tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
19093 : 2 : if (fndecl
19094 : 2 : && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
19095 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
19096 : 2 : && (!pre_p
19097 : 0 : || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
19098 : : lhs_addr, lhs_var,
19099 : : target_expr, true, depth)))
19100 : : {
19101 : 0 : if (pre_p)
19102 : 0 : *expr_p = expr;
19103 : 0 : saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
19104 : : pre_p, lhs_addr, lhs_var,
19105 : : target_expr, true, depth);
19106 : 0 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
19107 : : pre_p, lhs_addr, lhs_var,
19108 : : target_expr, rhs, depth);
19109 : 0 : return saw_lhs;
19110 : : }
19111 : : }
19112 : :
19113 : 38 : if (pre_p)
19114 : 35 : gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
19115 : : }
19116 : 38 : if (!pre_p)
19117 : 3 : return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
19118 : 3 : target_expr, rhs, depth);
19119 : 35 : *expr_p = expr;
19120 : 35 : return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
19121 : 35 : target_expr, rhs, depth);
19122 : 441 : case COND_EXPR:
19123 : 441 : if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
19124 : : lhs_var, target_expr, true, depth))
19125 : : break;
19126 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19127 : : lhs_addr, lhs_var, target_expr, true,
19128 : : depth);
19129 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19130 : : lhs_addr, lhs_var, target_expr, true,
19131 : : depth);
19132 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
19133 : : lhs_addr, lhs_var, target_expr, true,
19134 : : depth);
19135 : 428 : break;
19136 : 880 : case TARGET_EXPR:
19137 : 880 : if (TARGET_EXPR_INITIAL (expr))
19138 : : {
19139 : 880 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
19140 : : lhs_var, target_expr, true,
19141 : : depth))
19142 : : break;
19143 : 702 : if (expr == target_expr)
19144 : : saw_lhs = 1;
19145 : : else
19146 : : {
19147 : 702 : saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
19148 : : pre_p, lhs_addr, lhs_var,
19149 : : target_expr, true, depth);
19150 : 702 : if (saw_lhs && target_expr == NULL_TREE && pre_p)
19151 : 26 : target_expr = expr;
19152 : : }
19153 : : }
19154 : : break;
19155 : : default:
19156 : : break;
19157 : : }
19158 : : break;
19159 : 565 : case tcc_reference:
19160 : 565 : if (TREE_CODE (expr) == BIT_FIELD_REF
19161 : 499 : || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
19162 : 362 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19163 : : lhs_addr, lhs_var, target_expr, true,
19164 : : depth);
19165 : : break;
19166 : 58 : case tcc_vl_exp:
19167 : 58 : if (TREE_CODE (expr) == CALL_EXPR)
19168 : : {
19169 : 58 : if (tree fndecl = get_callee_fndecl (expr))
19170 : 58 : if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
19171 : : BUILT_IN_MEMCMP))
19172 : : {
19173 : 56 : int nargs = call_expr_nargs (expr);
19174 : 224 : for (int i = 0; i < nargs; i++)
19175 : 168 : saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
19176 : : pre_p, lhs_addr, lhs_var,
19177 : : target_expr, true, depth);
19178 : : }
19179 : : }
19180 : : break;
19181 : : default:
19182 : : break;
19183 : : }
19184 : :
19185 : 17259 : finish:
19186 : 17259 : if (saw_lhs == 0 && pre_p)
19187 : : {
19188 : 3498 : enum gimplify_status gs;
19189 : 3498 : if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
19190 : : {
19191 : 0 : gimplify_stmt (&expr, pre_p);
19192 : 0 : return saw_lhs;
19193 : : }
19194 : 3498 : else if (rhs)
19195 : 3320 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
19196 : : else
19197 : 178 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
19198 : 3498 : if (gs != GS_ALL_DONE)
19199 : 36820 : saw_lhs = -1;
19200 : : }
19201 : :
19202 : : return saw_lhs;
19203 : : }
19204 : :
19205 : : /* Gimplify an OMP_ATOMIC statement. */
19206 : :
19207 : : static enum gimplify_status
19208 : 10249 : gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
19209 : : {
19210 : 10249 : tree addr = TREE_OPERAND (*expr_p, 0);
19211 : 10249 : tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
19212 : 10249 : ? NULL : TREE_OPERAND (*expr_p, 1);
19213 : 10249 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
19214 : 10249 : tree tmp_load;
19215 : 10249 : gomp_atomic_load *loadstmt;
19216 : 10249 : gomp_atomic_store *storestmt;
19217 : 10249 : tree target_expr = NULL_TREE;
19218 : :
19219 : 10249 : tmp_load = create_tmp_reg (type);
19220 : 10249 : if (rhs
19221 : 10249 : && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
19222 : : true, 0) < 0)
19223 : : return GS_ERROR;
19224 : :
19225 : 10249 : if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
19226 : : != GS_ALL_DONE)
19227 : : return GS_ERROR;
19228 : :
19229 : 10249 : loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
19230 : 10249 : OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19231 : 10249 : gimplify_seq_add_stmt (pre_p, loadstmt);
19232 : 10249 : if (rhs)
19233 : : {
19234 : : /* BIT_INSERT_EXPR is not valid for non-integral bitfield
19235 : : representatives. Use BIT_FIELD_REF on the lhs instead. */
19236 : 9068 : tree rhsarg = rhs;
19237 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19238 : 428 : rhsarg = TREE_OPERAND (rhs, 1);
19239 : 9068 : if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
19240 : 9068 : && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
19241 : : {
19242 : 32 : tree bitpos = TREE_OPERAND (rhsarg, 2);
19243 : 32 : tree op1 = TREE_OPERAND (rhsarg, 1);
19244 : 32 : tree bitsize;
19245 : 32 : tree tmp_store = tmp_load;
19246 : 32 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
19247 : 12 : tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
19248 : 32 : if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
19249 : 32 : bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
19250 : : else
19251 : 0 : bitsize = TYPE_SIZE (TREE_TYPE (op1));
19252 : 32 : gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
19253 : 32 : tree t = build2_loc (EXPR_LOCATION (rhsarg),
19254 : : MODIFY_EXPR, void_type_node,
19255 : 32 : build3_loc (EXPR_LOCATION (rhsarg),
19256 : 32 : BIT_FIELD_REF, TREE_TYPE (op1),
19257 : : tmp_store, bitsize, bitpos), op1);
19258 : 32 : if (TREE_CODE (rhs) == COND_EXPR)
19259 : 16 : t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
19260 : 16 : TREE_OPERAND (rhs, 0), t, void_node);
19261 : 32 : gimplify_and_add (t, pre_p);
19262 : 32 : rhs = tmp_store;
19263 : : }
19264 : 9068 : bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
19265 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19266 : 412 : gimplify_ctxp->allow_rhs_cond_expr = true;
19267 : 9068 : enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
19268 : : is_gimple_val, fb_rvalue);
19269 : 9068 : gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
19270 : 9068 : if (gs != GS_ALL_DONE)
19271 : : return GS_ERROR;
19272 : : }
19273 : :
19274 : 10249 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
19275 : 1181 : rhs = tmp_load;
19276 : 10249 : storestmt
19277 : 10249 : = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19278 : 10249 : if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
19279 : : {
19280 : 37 : gimple_omp_atomic_set_weak (loadstmt);
19281 : 37 : gimple_omp_atomic_set_weak (storestmt);
19282 : : }
19283 : 10249 : gimplify_seq_add_stmt (pre_p, storestmt);
19284 : 10249 : switch (TREE_CODE (*expr_p))
19285 : : {
19286 : 2065 : case OMP_ATOMIC_READ:
19287 : 2065 : case OMP_ATOMIC_CAPTURE_OLD:
19288 : 2065 : *expr_p = tmp_load;
19289 : 2065 : gimple_omp_atomic_set_need_value (loadstmt);
19290 : 2065 : break;
19291 : 868 : case OMP_ATOMIC_CAPTURE_NEW:
19292 : 868 : *expr_p = rhs;
19293 : 868 : gimple_omp_atomic_set_need_value (storestmt);
19294 : 868 : break;
19295 : 7316 : default:
19296 : 7316 : *expr_p = NULL;
19297 : 7316 : break;
19298 : : }
19299 : :
19300 : : return GS_ALL_DONE;
19301 : : }
19302 : :
19303 : : /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
19304 : : body, and adding some EH bits. */
19305 : :
19306 : : static enum gimplify_status
19307 : 478 : gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
19308 : : {
19309 : 478 : tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
19310 : 478 : gimple *body_stmt;
19311 : 478 : gtransaction *trans_stmt;
19312 : 478 : gimple_seq body = NULL;
19313 : 478 : int subcode = 0;
19314 : :
19315 : : /* Wrap the transaction body in a BIND_EXPR so we have a context
19316 : : where to put decls for OMP. */
19317 : 478 : if (TREE_CODE (tbody) != BIND_EXPR)
19318 : : {
19319 : 439 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
19320 : 439 : TREE_SIDE_EFFECTS (bind) = 1;
19321 : 439 : SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
19322 : 439 : TRANSACTION_EXPR_BODY (expr) = bind;
19323 : : }
19324 : :
19325 : 478 : push_gimplify_context ();
19326 : 478 : temp = voidify_wrapper_expr (*expr_p, NULL);
19327 : :
19328 : 478 : body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
19329 : 478 : pop_gimplify_context (body_stmt);
19330 : :
19331 : 478 : trans_stmt = gimple_build_transaction (body);
19332 : 478 : if (TRANSACTION_EXPR_OUTER (expr))
19333 : : subcode = GTMA_IS_OUTER;
19334 : 447 : else if (TRANSACTION_EXPR_RELAXED (expr))
19335 : 81 : subcode = GTMA_IS_RELAXED;
19336 : 478 : gimple_transaction_set_subcode (trans_stmt, subcode);
19337 : :
19338 : 478 : gimplify_seq_add_stmt (pre_p, trans_stmt);
19339 : :
19340 : 478 : if (temp)
19341 : : {
19342 : 76 : *expr_p = temp;
19343 : 76 : return GS_OK;
19344 : : }
19345 : :
19346 : 402 : *expr_p = NULL_TREE;
19347 : 402 : return GS_ALL_DONE;
19348 : : }
19349 : :
19350 : : /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
19351 : : is the OMP_BODY of the original EXPR (which has already been
19352 : : gimplified so it's not present in the EXPR).
19353 : :
19354 : : Return the gimplified GIMPLE_OMP_ORDERED tuple. */
19355 : :
19356 : : static gimple *
19357 : 1940 : gimplify_omp_ordered (tree expr, gimple_seq body)
19358 : : {
19359 : 1940 : tree c, decls;
19360 : 1940 : int failures = 0;
19361 : 1940 : unsigned int i;
19362 : 1940 : tree source_c = NULL_TREE;
19363 : 1940 : tree sink_c = NULL_TREE;
19364 : :
19365 : 1940 : if (gimplify_omp_ctxp)
19366 : : {
19367 : 3271 : for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
19368 : 1505 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19369 : 1505 : && gimplify_omp_ctxp->loop_iter_var.is_empty ())
19370 : : {
19371 : 74 : error_at (OMP_CLAUSE_LOCATION (c),
19372 : : "%<ordered%> construct with %qs clause must be "
19373 : : "closely nested inside a loop with %<ordered%> clause",
19374 : 74 : OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
19375 : 74 : failures++;
19376 : : }
19377 : 1431 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19378 : 1431 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
19379 : : {
19380 : 637 : bool fail = false;
19381 : 637 : sink_c = c;
19382 : 637 : if (OMP_CLAUSE_DECL (c) == NULL_TREE)
19383 : 72 : continue; /* omp_cur_iteration - 1 */
19384 : 565 : for (decls = OMP_CLAUSE_DECL (c), i = 0;
19385 : 3191 : decls && TREE_CODE (decls) == TREE_LIST;
19386 : 2626 : decls = TREE_CHAIN (decls), ++i)
19387 : 2626 : if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
19388 : 4 : continue;
19389 : 5244 : else if (TREE_VALUE (decls)
19390 : 2622 : != gimplify_omp_ctxp->loop_iter_var[2 * i])
19391 : : {
19392 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
19393 : : "variable %qE is not an iteration "
19394 : : "of outermost loop %d, expected %qE",
19395 : 8 : TREE_VALUE (decls), i + 1,
19396 : 8 : gimplify_omp_ctxp->loop_iter_var[2 * i]);
19397 : 8 : fail = true;
19398 : 8 : failures++;
19399 : : }
19400 : : else
19401 : 5228 : TREE_VALUE (decls)
19402 : 2614 : = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
19403 : 1126 : if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
19404 : : {
19405 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
19406 : : "number of variables in %qs clause with "
19407 : : "%<sink%> modifier does not match number of "
19408 : : "iteration variables",
19409 : 16 : OMP_CLAUSE_DOACROSS_DEPEND (c)
19410 : : ? "depend" : "doacross");
19411 : 16 : failures++;
19412 : : }
19413 : : }
19414 : 794 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19415 : 794 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
19416 : : {
19417 : 470 : if (source_c)
19418 : : {
19419 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
19420 : : "more than one %qs clause with %<source%> "
19421 : : "modifier on an %<ordered%> construct",
19422 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c)
19423 : : ? "depend" : "doacross");
19424 : 4 : failures++;
19425 : : }
19426 : : else
19427 : : source_c = c;
19428 : : }
19429 : : }
19430 : 1940 : if (source_c && sink_c)
19431 : : {
19432 : 4 : error_at (OMP_CLAUSE_LOCATION (source_c),
19433 : : "%qs clause with %<source%> modifier specified "
19434 : : "together with %qs clauses with %<sink%> modifier "
19435 : : "on the same construct",
19436 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
19437 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
19438 : 4 : failures++;
19439 : : }
19440 : :
19441 : 1940 : if (failures)
19442 : 102 : return gimple_build_nop ();
19443 : 1838 : return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
19444 : : }
19445 : :
19446 : : /* Gimplify an OMP_INTEROP statement. */
19447 : :
19448 : : static enum gimplify_status
19449 : 612 : gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
19450 : : {
19451 : 612 : tree expr = *expr_p;
19452 : :
19453 : 612 : gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
19454 : : OMP_INTEROP);
19455 : 612 : gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
19456 : 612 : gimplify_seq_add_stmt (pre_p, stmt);
19457 : 612 : *expr_p = NULL_TREE;
19458 : 612 : return GS_ALL_DONE;
19459 : : }
19460 : :
19461 : : /* Callback for walk_tree to find an IFN_GOMP_DISPATCH. */
19462 : :
19463 : : static tree
19464 : 3082 : find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
19465 : : {
19466 : 3082 : tree t = *tp;
19467 : :
19468 : 3082 : if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
19469 : 1408 : return *(tree *) modify ? *(tree *) modify : *tp;
19470 : :
19471 : 2236 : if (TREE_CODE (t) == MODIFY_EXPR)
19472 : 507 : *(tree *) modify = *tp;
19473 : :
19474 : : return NULL_TREE;
19475 : : }
19476 : :
19477 : : /* Gimplify an OMP_DISPATCH construct. */
19478 : :
19479 : : static enum gimplify_status
19480 : 846 : gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
19481 : : {
19482 : 846 : tree expr = *expr_p;
19483 : 846 : gimple_seq body = NULL;
19484 : :
19485 : 846 : gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
19486 : : OMP_DISPATCH);
19487 : 846 : push_gimplify_context ();
19488 : :
19489 : : // If device clause, adjust ICV
19490 : 846 : tree device
19491 : 846 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
19492 : : // If no device clause exists but an interop clause with a single list
19493 : : // item, use it to obtain the device number.
19494 : 846 : if (device)
19495 : 272 : device = OMP_CLAUSE_DEVICE_ID (device);
19496 : : else
19497 : : {
19498 : 574 : tree first_interop_obj
19499 : 574 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
19500 : 574 : if (first_interop_obj)
19501 : 96 : for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
19502 : 8 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
19503 : : {
19504 : : first_interop_obj = NULL_TREE;
19505 : : break;
19506 : : }
19507 : 95 : if (first_interop_obj)
19508 : : {
19509 : 88 : device = create_tmp_var (integer_type_node);
19510 : 88 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
19511 : : OMP_CLAUSE_DEVICE);
19512 : 88 : OMP_CLAUSE_DEVICE_ID (c) = device;
19513 : 88 : TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
19514 : 88 : TREE_CHAIN (first_interop_obj) = c;
19515 : 88 : first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
19516 : : /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL); */
19517 : 88 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
19518 : 88 : fn = build_call_expr (fn, 3, first_interop_obj,
19519 : : build_int_cst (integer_type_node, -5),
19520 : : null_pointer_node);
19521 : 88 : gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
19522 : : }
19523 : : }
19524 : 846 : tree saved_device_icv = NULL_TREE;
19525 : 846 : if (device
19526 : 846 : && (TREE_CODE (device) != INTEGER_CST
19527 : 703 : || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
19528 : : {
19529 : : // Save current default-device-var ICV
19530 : 360 : saved_device_icv = create_tmp_var (integer_type_node);
19531 : 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
19532 : 360 : gcall *call = gimple_build_call (fn, 0);
19533 : 360 : gimple_call_set_lhs (call, saved_device_icv);
19534 : 360 : gimplify_seq_add_stmt (&body, call);
19535 : :
19536 : : // Set default device
19537 : 360 : fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19538 : 360 : call = gimple_build_call (fn, 1, device);
19539 : 360 : gimplify_seq_add_stmt (&body, call);
19540 : : }
19541 : :
19542 : : // If the novariants and nocontext clauses are not compile-time constants,
19543 : : // we need to generate code for all possible cases:
19544 : : // if (novariants) // implies nocontext
19545 : : // base()
19546 : : // else if (nocontext)
19547 : : // variant1()
19548 : : // else
19549 : : // variant2()
19550 : 846 : tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
19551 : 846 : if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
19552 : 28 : dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
19553 : 846 : tree dispatch_body = *dispatch_body_p;
19554 : :
19555 : : // Look for IFN_GOMP_DISPATCH and extract the base function call
19556 : 846 : tree base_call_expr = NULL_TREE;
19557 : 846 : if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
19558 : 243 : for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
19559 : 187 : tsi_next (&tsi))
19560 : : {
19561 : 243 : tree modify = NULL_TREE;
19562 : 243 : tree stmt = tsi_stmt (tsi);
19563 : 243 : base_call_expr
19564 : 243 : = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
19565 : 243 : if (base_call_expr != NULL_TREE)
19566 : : break;
19567 : : }
19568 : : else
19569 : : {
19570 : 790 : tree modify = NULL_TREE;
19571 : 790 : base_call_expr
19572 : 790 : = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
19573 : : }
19574 : 846 : gcc_assert (base_call_expr != NULL_TREE);
19575 : :
19576 : 846 : tree dst = NULL_TREE;
19577 : 846 : if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
19578 : : {
19579 : 284 : dst = TREE_OPERAND (base_call_expr, 0);
19580 : 284 : base_call_expr = TREE_OPERAND (base_call_expr, 1);
19581 : : }
19582 : :
19583 : 874 : while (TREE_CODE (base_call_expr) == FLOAT_EXPR
19584 : : || TREE_CODE (base_call_expr) == CONVERT_EXPR
19585 : : || TREE_CODE (base_call_expr) == COMPLEX_EXPR
19586 : : || TREE_CODE (base_call_expr) == INDIRECT_REF
19587 : 874 : || TREE_CODE (base_call_expr) == NOP_EXPR)
19588 : 28 : base_call_expr = TREE_OPERAND (base_call_expr, 0);
19589 : :
19590 : 846 : gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
19591 : 846 : base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
19592 : :
19593 : 846 : tree base_fndecl = get_callee_fndecl (base_call_expr);
19594 : 846 : if (base_fndecl != NULL_TREE)
19595 : : {
19596 : 838 : if (DECL_VIRTUAL_P (base_fndecl))
19597 : : {
19598 : 6 : error_at (
19599 : 3 : EXPR_LOCATION (base_call_expr),
19600 : : "%qD is a virtual function but only a direct call is allowed "
19601 : : "in a dispatch construct",
19602 : 3 : DECL_NAME (base_fndecl));
19603 : : }
19604 : :
19605 : : /* We are not actually going to expand the variant call or use
19606 : : the result of omp_get_dynamic candidates here; only check that
19607 : : it does not trivially resolve to a call to the base function
19608 : : so that we can avoid some extra work in building code that's
19609 : : not needed in that case. */
19610 : 838 : tree construct_context = omp_get_construct_context ();
19611 : 838 : vec<struct omp_variant> all_candidates
19612 : 838 : = omp_declare_variant_candidates (base_fndecl, construct_context);
19613 : 838 : gcc_assert (!all_candidates.is_empty ());
19614 : 838 : vec<struct omp_variant> candidates
19615 : 838 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19616 : 838 : tree variant_fndecl
19617 : 1664 : = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
19618 : :
19619 : 838 : if (base_fndecl != variant_fndecl
19620 : 838 : && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
19621 : : {
19622 : 22 : tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
19623 : 22 : novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
19624 : 59 : for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
19625 : : {
19626 : 37 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
19627 : 37 : && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
19628 : : {
19629 : 16 : gcc_assert (novariants_cond == NULL_TREE);
19630 : 16 : novariants_clause = c;
19631 : 16 : novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
19632 : : }
19633 : 21 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
19634 : 21 : && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
19635 : : {
19636 : 16 : gcc_assert (nocontext_cond == NULL_TREE);
19637 : 16 : nocontext_clause = c;
19638 : 16 : nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
19639 : : }
19640 : : }
19641 : 22 : gcc_assert (novariants_cond != NULL_TREE
19642 : : || nocontext_cond != NULL_TREE);
19643 : :
19644 : 22 : enum gimplify_status ret
19645 : 22 : = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
19646 : : fb_rvalue);
19647 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19648 : 0 : return ret;
19649 : 22 : ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
19650 : : fb_rvalue);
19651 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19652 : : return ret;
19653 : :
19654 : 22 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19655 : :
19656 : 22 : if (novariants_cond != NULL_TREE)
19657 : : {
19658 : 16 : tree base_label = create_artificial_label (UNKNOWN_LOCATION);
19659 : 16 : tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
19660 : 16 : gcond *novariants_cond_stmt
19661 : 16 : = gimple_build_cond_from_tree (novariants_cond, base_label,
19662 : : cond_label);
19663 : 16 : gimplify_seq_add_stmt (&body, novariants_cond_stmt);
19664 : :
19665 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
19666 : 16 : tree base_call_expr2 = copy_node (base_call_expr);
19667 : 16 : base_call_expr2
19668 : 16 : = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
19669 : : IFN_GOMP_DISPATCH,
19670 : 16 : TREE_TYPE (base_call_expr2), 1,
19671 : : base_call_expr2);
19672 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19673 : : {
19674 : 16 : base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19675 : : base_call_expr2);
19676 : : }
19677 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19678 : 16 : = boolean_true_node;
19679 : 16 : gimplify_and_add (base_call_expr2, &body);
19680 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19681 : :
19682 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19683 : 16 : = boolean_false_node;
19684 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
19685 : : }
19686 : :
19687 : 22 : if (nocontext_cond != NULL_TREE)
19688 : : {
19689 : 16 : tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
19690 : 16 : tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
19691 : 16 : gcond *nocontext_cond_stmt
19692 : 16 : = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
19693 : : variant2_label);
19694 : 16 : gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
19695 : :
19696 : 32 : gimplify_seq_add_stmt (&body,
19697 : 16 : gimple_build_label (variant1_label));
19698 : 16 : tree variant_call_expr = copy_node (base_call_expr);
19699 : 32 : variant_call_expr = build_call_expr_internal_loc (
19700 : 16 : EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
19701 : 16 : TREE_TYPE (variant_call_expr), 1, variant_call_expr);
19702 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19703 : : {
19704 : 16 : variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19705 : : variant_call_expr);
19706 : : }
19707 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
19708 : 16 : gimplify_and_add (variant_call_expr, &body);
19709 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19710 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
19711 : 32 : gimplify_seq_add_stmt (&body,
19712 : 16 : gimple_build_label (variant2_label));
19713 : : }
19714 : :
19715 : 22 : tree variant_call_expr = base_call_expr;
19716 : 22 : variant_call_expr
19717 : 22 : = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
19718 : : IFN_GOMP_DISPATCH,
19719 : 22 : TREE_TYPE (variant_call_expr), 1,
19720 : : variant_call_expr);
19721 : 22 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19722 : : {
19723 : 22 : variant_call_expr
19724 : 22 : = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
19725 : : }
19726 : 22 : gimplify_and_add (variant_call_expr, &body);
19727 : 22 : gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
19728 : : }
19729 : : else
19730 : 816 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19731 : : }
19732 : : else
19733 : 8 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19734 : :
19735 : : // Restore default-device-var ICV
19736 : 846 : if (saved_device_icv != NULL_TREE)
19737 : : {
19738 : 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19739 : 360 : gcall *call = gimple_build_call (fn, 1, saved_device_icv);
19740 : 360 : gimplify_seq_add_stmt (&body, call);
19741 : : }
19742 : :
19743 : : // Wrap dispatch body into a bind
19744 : 846 : gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
19745 : 846 : pop_gimplify_context (bind);
19746 : :
19747 : : // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
19748 : : // call to gimplify_adjust_omp_clauses
19749 : 846 : gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
19750 : 846 : if (ctx != NULL)
19751 : : {
19752 : 846 : gcc_assert (ctx->code == OMP_DISPATCH);
19753 : 846 : gimplify_omp_ctxp = ctx->outer_context;
19754 : 846 : delete_omp_context (ctx);
19755 : : }
19756 : :
19757 : : // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
19758 : : // has been handled above, and depend as the front end handled it by inserting
19759 : : // taskwait.
19760 : 846 : tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
19761 : 1433 : for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
19762 : : {
19763 : 985 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
19764 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
19765 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
19766 : : {
19767 : 398 : *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
19768 : 398 : break;
19769 : : }
19770 : : else
19771 : 587 : dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
19772 : : }
19773 : :
19774 : 846 : gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
19775 : 846 : gimplify_seq_add_stmt (pre_p, stmt);
19776 : 846 : *expr_p = NULL_TREE;
19777 : 846 : return GS_ALL_DONE;
19778 : : }
19779 : :
19780 : : /* Expand a metadirective that has been resolved at gimplification time
19781 : : into the candidate directive variants in CANDIDATES. */
19782 : :
19783 : : static enum gimplify_status
19784 : 158 : expand_omp_metadirective (vec<struct omp_variant> &candidates,
19785 : : gimple_seq *pre_p)
19786 : : {
19787 : 158 : auto_vec<tree> selectors;
19788 : 158 : auto_vec<tree> directive_labels;
19789 : 158 : auto_vec<gimple_seq> directive_bodies;
19790 : 158 : tree body_label = NULL_TREE;
19791 : 158 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19792 : :
19793 : : /* Construct bodies for each candidate. */
19794 : 381 : for (unsigned i = 0; i < candidates.length(); i++)
19795 : : {
19796 : 223 : struct omp_variant &candidate = candidates[i];
19797 : 223 : gimple_seq body = NULL;
19798 : :
19799 : 223 : selectors.safe_push (omp_dynamic_cond (candidate.selector,
19800 : : find_supercontext ()));
19801 : 223 : directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19802 : :
19803 : 223 : gimplify_seq_add_stmt (&body,
19804 : 223 : gimple_build_label (directive_labels.last ()));
19805 : 223 : if (candidate.alternative != NULL_TREE)
19806 : 223 : gimplify_stmt (&candidate.alternative, &body);
19807 : 223 : if (candidate.body != NULL_TREE)
19808 : : {
19809 : 34 : if (body_label != NULL_TREE)
19810 : 0 : gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
19811 : : else
19812 : : {
19813 : 34 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19814 : 34 : gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
19815 : 34 : gimplify_stmt (&candidate.body, &body);
19816 : : }
19817 : : }
19818 : :
19819 : 223 : directive_bodies.safe_push (body);
19820 : : }
19821 : :
19822 : 158 : auto_vec<tree> cond_labels;
19823 : :
19824 : 158 : cond_labels.safe_push (NULL_TREE);
19825 : 352 : for (unsigned i = 1; i < candidates.length () - 1; i++)
19826 : 18 : cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19827 : 158 : if (candidates.length () > 1)
19828 : 47 : cond_labels.safe_push (directive_labels.last ());
19829 : :
19830 : : /* Generate conditionals to test each dynamic selector in turn, executing
19831 : : the directive candidate if successful. */
19832 : 446 : for (unsigned i = 0; i < candidates.length () - 1; i++)
19833 : : {
19834 : 65 : if (i != 0)
19835 : 18 : gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
19836 : :
19837 : 65 : enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
19838 : : is_gimple_val, fb_rvalue);
19839 : 65 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19840 : : return ret;
19841 : :
19842 : 65 : gcond *cond_stmt
19843 : 65 : = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
19844 : 65 : cond_labels[i + 1]);
19845 : :
19846 : 65 : gimplify_seq_add_stmt (pre_p, cond_stmt);
19847 : 65 : gimplify_seq_add_seq (pre_p, directive_bodies[i]);
19848 : 65 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
19849 : : }
19850 : :
19851 : 158 : gimplify_seq_add_seq (pre_p, directive_bodies.last ());
19852 : 158 : gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
19853 : :
19854 : 158 : return GS_ALL_DONE;
19855 : 158 : }
19856 : :
19857 : : /* Expand a variant construct that requires late resolution in the ompdevlow
19858 : : pass. It's a bit easier to do this in tree form and then gimplify that,
19859 : : than to emit gimple. The output is going to look something like:
19860 : :
19861 : : switch_var = OMP_NEXT_VARIANT (0, state);
19862 : : loop_label:
19863 : : switch (switch_var)
19864 : : {
19865 : : case 1:
19866 : : if (dynamic_selector_predicate_1)
19867 : : {
19868 : : alternative_1;
19869 : : goto end_label;
19870 : : }
19871 : : else
19872 : : {
19873 : : switch_var = OMP_NEXT_VARIANT (1, state);
19874 : : goto loop_label;
19875 : : }
19876 : : case 2:
19877 : : ...
19878 : : }
19879 : : end_label:
19880 : :
19881 : : OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
19882 : : index of the next variant to try, after late resolution. */
19883 : :
19884 : : static tree
19885 : 16 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
19886 : : tree construct_context)
19887 : : {
19888 : 16 : tree body_label = NULL_TREE;
19889 : 16 : tree standalone_body = NULL_TREE;
19890 : 16 : tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
19891 : 16 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19892 : 32 : tree selectors = make_tree_vec (all_candidates.length ());
19893 : 16 : tree switch_body = NULL_TREE;
19894 : 16 : tree switch_var = create_tmp_var (integer_type_node, "variant");
19895 : 16 : tree state = tree_cons (NULL_TREE, construct_context, selectors);
19896 : :
19897 : 92 : for (unsigned int i = 0; i < all_candidates.length (); i++)
19898 : : {
19899 : 76 : tree selector = all_candidates[i].selector;
19900 : 76 : tree alternative = all_candidates[i].alternative;
19901 : 76 : tree body = all_candidates[i].body;
19902 : 76 : TREE_VEC_ELT (selectors, i) = selector;
19903 : :
19904 : : /* Case label. Numbering is 1-based. */
19905 : 76 : tree case_val = build_int_cst (integer_type_node, i + 1);
19906 : 76 : tree case_label
19907 : 76 : = build_case_label (case_val, NULL_TREE,
19908 : : create_artificial_label (UNKNOWN_LOCATION));
19909 : 76 : append_to_statement_list (case_label, &switch_body);
19910 : :
19911 : : /* The actual body of the variant. */
19912 : 76 : tree variant_body = NULL_TREE;
19913 : 76 : append_to_statement_list (alternative, &variant_body);
19914 : :
19915 : 76 : if (body != NULL_TREE)
19916 : : {
19917 : 0 : if (standalone_body == NULL)
19918 : : {
19919 : 0 : standalone_body = body;
19920 : 0 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19921 : : }
19922 : 0 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19923 : : body_label),
19924 : : &variant_body);
19925 : : }
19926 : : else
19927 : 76 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19928 : : end_label),
19929 : : &variant_body);
19930 : :
19931 : : /* If this is a dynamic selector, wrap variant_body with a conditional.
19932 : : If the predicate doesn't match, the else clause sets switch_var and
19933 : : jumps to loop_var to try again. */
19934 : 76 : tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
19935 : 76 : if (dynamic_selector)
19936 : : {
19937 : 20 : tree else_stmt = NULL_TREE;
19938 : 20 : tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
19939 : : case_val, state);
19940 : 20 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19941 : : switch_var, next),
19942 : : &else_stmt);
19943 : 20 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19944 : : loop_label),
19945 : : &else_stmt);
19946 : 20 : variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
19947 : : variant_body, else_stmt);
19948 : : }
19949 : 76 : append_to_statement_list (variant_body, &switch_body);
19950 : : }
19951 : :
19952 : : /* Put it all together. */
19953 : 16 : tree result = NULL_TREE;
19954 : 16 : tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
19955 : : state);
19956 : 16 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19957 : : switch_var, first),
19958 : : &result);
19959 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
19960 : : &result);
19961 : 16 : append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
19962 : : switch_var, switch_body),
19963 : : &result);
19964 : 16 : if (standalone_body)
19965 : : {
19966 : 0 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
19967 : : body_label),
19968 : : &result);
19969 : 0 : append_to_statement_list (standalone_body, &result);
19970 : : }
19971 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
19972 : : &result);
19973 : 16 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
19974 : 16 : return result;
19975 : : }
19976 : :
19977 : :
19978 : : /* Gimplify an OMP_METADIRECTIVE construct. EXPR is the tree version.
19979 : : The metadirective will be resolved at this point if possible, otherwise
19980 : : a GIMPLE_OMP_VARIANT_CONSTRUCT is created. */
19981 : :
19982 : : static enum gimplify_status
19983 : 162 : gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
19984 : : bool (*) (tree), fallback_t)
19985 : : {
19986 : : /* Try to resolve the metadirective. */
19987 : 162 : tree construct_context = omp_get_construct_context ();
19988 : 162 : vec<struct omp_variant> all_candidates
19989 : 162 : = omp_metadirective_candidates (*expr_p, construct_context);
19990 : 162 : vec<struct omp_variant> candidates
19991 : 162 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19992 : 162 : if (!candidates.is_empty ())
19993 : 158 : return expand_omp_metadirective (candidates, pre_p);
19994 : :
19995 : : /* The metadirective cannot be resolved yet. Turn it into a loop with
19996 : : a nested switch statement, using OMP_NEXT_VARIANT to set the control
19997 : : variable for the switch. */
19998 : 4 : *expr_p = expand_late_variant_directive (all_candidates, construct_context);
19999 : 4 : return GS_OK;
20000 : : }
20001 : :
20002 : : /* Gimplify an OMP_DECLARE_MAPPER node (by just removing it). */
20003 : :
20004 : : static enum gimplify_status
20005 : 0 : gimplify_omp_declare_mapper (tree *expr_p)
20006 : : {
20007 : 0 : *expr_p = NULL_TREE;
20008 : 0 : return GS_ALL_DONE;
20009 : : }
20010 : :
20011 : : /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
20012 : : expression produces a value to be used as an operand inside a GIMPLE
20013 : : statement, the value will be stored back in *EXPR_P. This value will
20014 : : be a tree of class tcc_declaration, tcc_constant, tcc_reference or
20015 : : an SSA_NAME. The corresponding sequence of GIMPLE statements is
20016 : : emitted in PRE_P and POST_P.
20017 : :
20018 : : Additionally, this process may overwrite parts of the input
20019 : : expression during gimplification. Ideally, it should be
20020 : : possible to do non-destructive gimplification.
20021 : :
20022 : : EXPR_P points to the GENERIC expression to convert to GIMPLE. If
20023 : : the expression needs to evaluate to a value to be used as
20024 : : an operand in a GIMPLE statement, this value will be stored in
20025 : : *EXPR_P on exit. This happens when the caller specifies one
20026 : : of fb_lvalue or fb_rvalue fallback flags.
20027 : :
20028 : : PRE_P will contain the sequence of GIMPLE statements corresponding
20029 : : to the evaluation of EXPR and all the side-effects that must
20030 : : be executed before the main expression. On exit, the last
20031 : : statement of PRE_P is the core statement being gimplified. For
20032 : : instance, when gimplifying 'if (++a)' the last statement in
20033 : : PRE_P will be 'if (t.1)' where t.1 is the result of
20034 : : pre-incrementing 'a'.
20035 : :
20036 : : POST_P will contain the sequence of GIMPLE statements corresponding
20037 : : to the evaluation of all the side-effects that must be executed
20038 : : after the main expression. If this is NULL, the post
20039 : : side-effects are stored at the end of PRE_P.
20040 : :
20041 : : The reason why the output is split in two is to handle post
20042 : : side-effects explicitly. In some cases, an expression may have
20043 : : inner and outer post side-effects which need to be emitted in
20044 : : an order different from the one given by the recursive
20045 : : traversal. For instance, for the expression (*p--)++ the post
20046 : : side-effects of '--' must actually occur *after* the post
20047 : : side-effects of '++'. However, gimplification will first visit
20048 : : the inner expression, so if a separate POST sequence was not
20049 : : used, the resulting sequence would be:
20050 : :
20051 : : 1 t.1 = *p
20052 : : 2 p = p - 1
20053 : : 3 t.2 = t.1 + 1
20054 : : 4 *p = t.2
20055 : :
20056 : : However, the post-decrement operation in line #2 must not be
20057 : : evaluated until after the store to *p at line #4, so the
20058 : : correct sequence should be:
20059 : :
20060 : : 1 t.1 = *p
20061 : : 2 t.2 = t.1 + 1
20062 : : 3 *p = t.2
20063 : : 4 p = p - 1
20064 : :
20065 : : So, by specifying a separate post queue, it is possible
20066 : : to emit the post side-effects in the correct order.
20067 : : If POST_P is NULL, an internal queue will be used. Before
20068 : : returning to the caller, the sequence POST_P is appended to
20069 : : the main output sequence PRE_P.
20070 : :
20071 : : GIMPLE_TEST_F points to a function that takes a tree T and
20072 : : returns nonzero if T is in the GIMPLE form requested by the
20073 : : caller. The GIMPLE predicates are in gimple.cc.
20074 : :
20075 : : FALLBACK tells the function what sort of a temporary we want if
20076 : : gimplification cannot produce an expression that complies with
20077 : : GIMPLE_TEST_F.
20078 : :
20079 : : fb_none means that no temporary should be generated
20080 : : fb_rvalue means that an rvalue is OK to generate
20081 : : fb_lvalue means that an lvalue is OK to generate
20082 : : fb_either means that either is OK, but an lvalue is preferable.
20083 : : fb_mayfail means that gimplification may fail (in which case
20084 : : GS_ERROR will be returned)
20085 : :
20086 : : The return value is either GS_ERROR or GS_ALL_DONE, since this
20087 : : function iterates until EXPR is completely gimplified or an error
20088 : : occurs. */
20089 : :
20090 : : enum gimplify_status
20091 : 512133670 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
20092 : : bool (*gimple_test_f) (tree), fallback_t fallback)
20093 : : {
20094 : 512133670 : tree tmp;
20095 : 512133670 : gimple_seq internal_pre = NULL;
20096 : 512133670 : gimple_seq internal_post = NULL;
20097 : 512133670 : tree save_expr;
20098 : 512133670 : bool is_statement;
20099 : 512133670 : location_t saved_location;
20100 : 512133670 : enum gimplify_status ret;
20101 : 512133670 : gimple_stmt_iterator pre_last_gsi, post_last_gsi;
20102 : 512133670 : tree label;
20103 : :
20104 : 512133670 : save_expr = *expr_p;
20105 : 512133670 : if (save_expr == NULL_TREE)
20106 : : return GS_ALL_DONE;
20107 : :
20108 : : /* If we are gimplifying a top-level statement, PRE_P must be valid. */
20109 : 461403419 : is_statement = gimple_test_f == is_gimple_stmt;
20110 : 461403419 : if (is_statement)
20111 : 100694066 : gcc_assert (pre_p);
20112 : :
20113 : : /* Consistency checks. */
20114 : 461403419 : if (gimple_test_f == is_gimple_reg)
20115 : 6511332 : gcc_assert (fallback & (fb_rvalue | fb_lvalue));
20116 : 454892087 : else if (gimple_test_f == is_gimple_val
20117 : 336425412 : || gimple_test_f == is_gimple_call_addr
20118 : 320115856 : || gimple_test_f == is_gimple_condexpr_for_cond
20119 : 314578556 : || gimple_test_f == is_gimple_mem_rhs
20120 : 314435770 : || gimple_test_f == is_gimple_mem_rhs_or_call
20121 : 303356104 : || gimple_test_f == is_gimple_reg_rhs
20122 : 302758007 : || gimple_test_f == is_gimple_reg_rhs_or_call
20123 : 231875239 : || gimple_test_f == is_gimple_asm_val
20124 : 231837272 : || gimple_test_f == is_gimple_mem_ref_addr)
20125 : 238697842 : gcc_assert (fallback & fb_rvalue);
20126 : 216194245 : else if (gimple_test_f == is_gimple_min_lval
20127 : 186565745 : || gimple_test_f == is_gimple_lvalue)
20128 : 82552283 : gcc_assert (fallback & fb_lvalue);
20129 : 133641962 : else if (gimple_test_f == is_gimple_addressable)
20130 : 32947896 : gcc_assert (fallback & fb_either);
20131 : 100694066 : else if (gimple_test_f == is_gimple_stmt)
20132 : 100694066 : gcc_assert (fallback == fb_none);
20133 : : else
20134 : : {
20135 : : /* We should have recognized the GIMPLE_TEST_F predicate to
20136 : : know what kind of fallback to use in case a temporary is
20137 : : needed to hold the value or address of *EXPR_P. */
20138 : 0 : gcc_unreachable ();
20139 : : }
20140 : :
20141 : : /* We used to check the predicate here and return immediately if it
20142 : : succeeds. This is wrong; the design is for gimplification to be
20143 : : idempotent, and for the predicates to only test for valid forms, not
20144 : : whether they are fully simplified. */
20145 : 461403419 : if (pre_p == NULL)
20146 : 0 : pre_p = &internal_pre;
20147 : :
20148 : 461403419 : if (post_p == NULL)
20149 : 186202018 : post_p = &internal_post;
20150 : :
20151 : : /* Remember the last statements added to PRE_P and POST_P. Every
20152 : : new statement added by the gimplification helpers needs to be
20153 : : annotated with location information. To centralize the
20154 : : responsibility, we remember the last statement that had been
20155 : : added to both queues before gimplifying *EXPR_P. If
20156 : : gimplification produces new statements in PRE_P and POST_P, those
20157 : : statements will be annotated with the same location information
20158 : : as *EXPR_P. */
20159 : 461403419 : pre_last_gsi = gsi_last (*pre_p);
20160 : 461403419 : post_last_gsi = gsi_last (*post_p);
20161 : :
20162 : 461403419 : saved_location = input_location;
20163 : 461403419 : if (save_expr != error_mark_node
20164 : 461403419 : && EXPR_HAS_LOCATION (*expr_p))
20165 : 172854021 : input_location = EXPR_LOCATION (*expr_p);
20166 : :
20167 : : /* Loop over the specific gimplifiers until the toplevel node
20168 : : remains the same. */
20169 : 480156270 : do
20170 : : {
20171 : : /* Strip away as many useless type conversions as possible
20172 : : at the toplevel. */
20173 : 480156270 : STRIP_USELESS_TYPE_CONVERSION (*expr_p);
20174 : :
20175 : : /* Remember the expr. */
20176 : 480156270 : save_expr = *expr_p;
20177 : :
20178 : : /* Die, die, die, my darling. */
20179 : 480156270 : if (error_operand_p (save_expr))
20180 : : {
20181 : : ret = GS_ERROR;
20182 : : break;
20183 : : }
20184 : :
20185 : : /* Do any language-specific gimplification. */
20186 : 480152564 : ret = ((enum gimplify_status)
20187 : 480152564 : lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
20188 : 480152564 : if (ret == GS_OK)
20189 : : {
20190 : 28956501 : if (*expr_p == NULL_TREE)
20191 : : break;
20192 : 28956501 : if (*expr_p != save_expr)
20193 : 5002916 : continue;
20194 : : }
20195 : 451196063 : else if (ret != GS_UNHANDLED)
20196 : : break;
20197 : :
20198 : : /* Make sure that all the cases set 'ret' appropriately. */
20199 : 474524627 : ret = GS_UNHANDLED;
20200 : 474524627 : switch (TREE_CODE (*expr_p))
20201 : : {
20202 : : /* First deal with the special cases. */
20203 : :
20204 : 1133765 : case POSTINCREMENT_EXPR:
20205 : 1133765 : case POSTDECREMENT_EXPR:
20206 : 1133765 : case PREINCREMENT_EXPR:
20207 : 1133765 : case PREDECREMENT_EXPR:
20208 : 2267530 : ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
20209 : : fallback != fb_none,
20210 : 1133765 : TREE_TYPE (*expr_p));
20211 : 1133765 : break;
20212 : :
20213 : 643370 : case VIEW_CONVERT_EXPR:
20214 : 643370 : if ((fallback & fb_rvalue)
20215 : 643253 : && is_gimple_reg_type (TREE_TYPE (*expr_p))
20216 : 1110569 : && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
20217 : : {
20218 : 456506 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20219 : : post_p, is_gimple_val, fb_rvalue);
20220 : 456506 : recalculate_side_effects (*expr_p);
20221 : 456506 : break;
20222 : : }
20223 : : /* Fallthru. */
20224 : :
20225 : 29566230 : case ARRAY_REF:
20226 : 29566230 : case ARRAY_RANGE_REF:
20227 : 29566230 : case REALPART_EXPR:
20228 : 29566230 : case IMAGPART_EXPR:
20229 : 29566230 : case COMPONENT_REF:
20230 : 29566230 : ret = gimplify_compound_lval (expr_p, pre_p, post_p,
20231 : : fallback ? fallback : fb_rvalue);
20232 : 29566230 : break;
20233 : :
20234 : 6151014 : case COND_EXPR:
20235 : 6151014 : ret = gimplify_cond_expr (expr_p, pre_p, fallback);
20236 : :
20237 : : /* C99 code may assign to an array in a structure value of a
20238 : : conditional expression, and this has undefined behavior
20239 : : only on execution, so create a temporary if an lvalue is
20240 : : required. */
20241 : 6151014 : if (fallback == fb_lvalue)
20242 : : {
20243 : 7 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20244 : 7 : mark_addressable (*expr_p);
20245 : 7 : ret = GS_OK;
20246 : : }
20247 : : break;
20248 : :
20249 : 16686568 : case CALL_EXPR:
20250 : 16686568 : ret = gimplify_call_expr (expr_p, pre_p, fallback);
20251 : :
20252 : : /* C99 code may assign to an array in a structure returned
20253 : : from a function, and this has undefined behavior only on
20254 : : execution, so create a temporary if an lvalue is
20255 : : required. */
20256 : 16686568 : if (fallback == fb_lvalue)
20257 : : {
20258 : 14253 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20259 : 14253 : mark_addressable (*expr_p);
20260 : 14253 : ret = GS_OK;
20261 : : }
20262 : : break;
20263 : :
20264 : 0 : case TREE_LIST:
20265 : 0 : gcc_unreachable ();
20266 : :
20267 : 0 : case OMP_ARRAY_SECTION:
20268 : 0 : gcc_unreachable ();
20269 : :
20270 : 482628 : case COMPOUND_EXPR:
20271 : 482628 : ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
20272 : 482628 : break;
20273 : :
20274 : 36549 : case COMPOUND_LITERAL_EXPR:
20275 : 36549 : ret = gimplify_compound_literal_expr (expr_p, pre_p,
20276 : : gimple_test_f, fallback);
20277 : 36549 : break;
20278 : :
20279 : 49562338 : case MODIFY_EXPR:
20280 : 49562338 : case INIT_EXPR:
20281 : 49562338 : ret = gimplify_modify_expr (expr_p, pre_p, post_p,
20282 : : fallback != fb_none);
20283 : 49562338 : break;
20284 : :
20285 : 114630 : case TRUTH_ANDIF_EXPR:
20286 : 114630 : case TRUTH_ORIF_EXPR:
20287 : 114630 : {
20288 : : /* Preserve the original type of the expression and the
20289 : : source location of the outer expression. */
20290 : 114630 : tree org_type = TREE_TYPE (*expr_p);
20291 : 114630 : *expr_p = gimple_boolify (*expr_p);
20292 : 114630 : *expr_p = build3_loc (input_location, COND_EXPR,
20293 : : org_type, *expr_p,
20294 : : fold_convert_loc
20295 : : (input_location,
20296 : : org_type, boolean_true_node),
20297 : : fold_convert_loc
20298 : : (input_location,
20299 : : org_type, boolean_false_node));
20300 : 114630 : ret = GS_OK;
20301 : 114630 : break;
20302 : : }
20303 : :
20304 : 225207 : case TRUTH_NOT_EXPR:
20305 : 225207 : {
20306 : 225207 : tree type = TREE_TYPE (*expr_p);
20307 : : /* The parsers are careful to generate TRUTH_NOT_EXPR
20308 : : only with operands that are always zero or one.
20309 : : We do not fold here but handle the only interesting case
20310 : : manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
20311 : 225207 : *expr_p = gimple_boolify (*expr_p);
20312 : 225207 : if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
20313 : 225207 : *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
20314 : 225207 : TREE_TYPE (*expr_p),
20315 : 225207 : TREE_OPERAND (*expr_p, 0));
20316 : : else
20317 : 0 : *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
20318 : 0 : TREE_TYPE (*expr_p),
20319 : 0 : TREE_OPERAND (*expr_p, 0),
20320 : 0 : build_int_cst (TREE_TYPE (*expr_p), 1));
20321 : 225207 : if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
20322 : 6468 : *expr_p = fold_convert_loc (input_location, type, *expr_p);
20323 : : ret = GS_OK;
20324 : : break;
20325 : : }
20326 : :
20327 : 33028312 : case ADDR_EXPR:
20328 : 33028312 : ret = gimplify_addr_expr (expr_p, pre_p, post_p);
20329 : 33028312 : break;
20330 : :
20331 : 5236 : case ANNOTATE_EXPR:
20332 : 5236 : {
20333 : 5236 : tree cond = TREE_OPERAND (*expr_p, 0);
20334 : 5236 : tree kind = TREE_OPERAND (*expr_p, 1);
20335 : 5236 : tree data = TREE_OPERAND (*expr_p, 2);
20336 : 5236 : tree type = TREE_TYPE (cond);
20337 : 5236 : if (!INTEGRAL_TYPE_P (type))
20338 : : {
20339 : 0 : *expr_p = cond;
20340 : 0 : ret = GS_OK;
20341 : 0 : break;
20342 : : }
20343 : 5236 : tree tmp = create_tmp_var (type);
20344 : 5236 : gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
20345 : 5236 : gcall *call
20346 : 5236 : = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
20347 : 5236 : gimple_call_set_lhs (call, tmp);
20348 : 5236 : gimplify_seq_add_stmt (pre_p, call);
20349 : 5236 : *expr_p = tmp;
20350 : 5236 : ret = GS_ALL_DONE;
20351 : 5236 : break;
20352 : : }
20353 : :
20354 : 50798 : case VA_ARG_EXPR:
20355 : 50798 : ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
20356 : 50798 : break;
20357 : :
20358 : 16973493 : CASE_CONVERT:
20359 : 16973493 : if (IS_EMPTY_STMT (*expr_p))
20360 : : {
20361 : : ret = GS_ALL_DONE;
20362 : : break;
20363 : : }
20364 : :
20365 : 15283226 : if (VOID_TYPE_P (TREE_TYPE (*expr_p))
20366 : 15283226 : || fallback == fb_none)
20367 : : {
20368 : : /* Just strip a conversion to void (or in void context) and
20369 : : try again. */
20370 : 2718020 : *expr_p = TREE_OPERAND (*expr_p, 0);
20371 : 2718020 : ret = GS_OK;
20372 : 2718020 : break;
20373 : : }
20374 : :
20375 : 12565206 : ret = gimplify_conversion (expr_p);
20376 : 12565206 : if (ret == GS_ERROR)
20377 : : break;
20378 : 12565206 : if (*expr_p != save_expr)
20379 : : break;
20380 : : /* FALLTHRU */
20381 : :
20382 : 12690701 : case FIX_TRUNC_EXPR:
20383 : : /* unary_expr: ... | '(' cast ')' val | ... */
20384 : 12690701 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20385 : : is_gimple_val, fb_rvalue);
20386 : 12690701 : recalculate_side_effects (*expr_p);
20387 : 12690701 : break;
20388 : :
20389 : 6454456 : case INDIRECT_REF:
20390 : 6454456 : {
20391 : 6454456 : bool volatilep = TREE_THIS_VOLATILE (*expr_p);
20392 : 6454456 : bool notrap = TREE_THIS_NOTRAP (*expr_p);
20393 : 6454456 : tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
20394 : :
20395 : 6454456 : *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
20396 : 6454456 : if (*expr_p != save_expr)
20397 : : {
20398 : : ret = GS_OK;
20399 : : break;
20400 : : }
20401 : :
20402 : 6434063 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20403 : : is_gimple_reg, fb_rvalue);
20404 : 6434063 : if (ret == GS_ERROR)
20405 : : break;
20406 : :
20407 : 6434062 : recalculate_side_effects (*expr_p);
20408 : 12868124 : *expr_p = fold_build2_loc (input_location, MEM_REF,
20409 : 6434062 : TREE_TYPE (*expr_p),
20410 : 6434062 : TREE_OPERAND (*expr_p, 0),
20411 : : build_int_cst (saved_ptr_type, 0));
20412 : 6434062 : TREE_THIS_VOLATILE (*expr_p) = volatilep;
20413 : 6434062 : TREE_THIS_NOTRAP (*expr_p) = notrap;
20414 : 6434062 : ret = GS_OK;
20415 : 6434062 : break;
20416 : : }
20417 : :
20418 : : /* We arrive here through the various re-gimplifcation paths. */
20419 : 16194778 : case MEM_REF:
20420 : : /* First try re-folding the whole thing. */
20421 : 16194778 : tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
20422 : : TREE_OPERAND (*expr_p, 0),
20423 : : TREE_OPERAND (*expr_p, 1));
20424 : 16194778 : if (tmp)
20425 : : {
20426 : 6530 : REF_REVERSE_STORAGE_ORDER (tmp)
20427 : 3265 : = REF_REVERSE_STORAGE_ORDER (*expr_p);
20428 : 3265 : *expr_p = tmp;
20429 : 3265 : recalculate_side_effects (*expr_p);
20430 : 3265 : ret = GS_OK;
20431 : 3265 : break;
20432 : : }
20433 : : /* Avoid re-gimplifying the address operand if it is already
20434 : : in suitable form. Re-gimplifying would mark the address
20435 : : operand addressable. Always gimplify when not in SSA form
20436 : : as we still may have to gimplify decls with value-exprs. */
20437 : 16191513 : if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
20438 : 17274685 : || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
20439 : : {
20440 : 15163123 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20441 : : is_gimple_mem_ref_addr, fb_rvalue);
20442 : 15163123 : if (ret == GS_ERROR)
20443 : : break;
20444 : : }
20445 : 16191513 : recalculate_side_effects (*expr_p);
20446 : 16191513 : ret = GS_ALL_DONE;
20447 : 16191513 : break;
20448 : :
20449 : : /* Constants need not be gimplified. */
20450 : 42709676 : case INTEGER_CST:
20451 : 42709676 : case REAL_CST:
20452 : 42709676 : case FIXED_CST:
20453 : 42709676 : case STRING_CST:
20454 : 42709676 : case COMPLEX_CST:
20455 : 42709676 : case VECTOR_CST:
20456 : : /* Drop the overflow flag on constants, we do not want
20457 : : that in the GIMPLE IL. */
20458 : 42709676 : if (TREE_OVERFLOW_P (*expr_p))
20459 : 1154 : *expr_p = drop_tree_overflow (*expr_p);
20460 : : ret = GS_ALL_DONE;
20461 : : break;
20462 : :
20463 : 116122 : case CONST_DECL:
20464 : : /* If we require an lvalue, such as for ADDR_EXPR, retain the
20465 : : CONST_DECL node. Otherwise the decl is replaceable by its
20466 : : value. */
20467 : : /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
20468 : 116122 : if (fallback & fb_lvalue)
20469 : : ret = GS_ALL_DONE;
20470 : : else
20471 : : {
20472 : 3562 : *expr_p = DECL_INITIAL (*expr_p);
20473 : 3562 : ret = GS_OK;
20474 : : }
20475 : : break;
20476 : :
20477 : 6443146 : case DECL_EXPR:
20478 : 6443146 : ret = gimplify_decl_expr (expr_p, pre_p);
20479 : 6443146 : break;
20480 : :
20481 : 5958534 : case BIND_EXPR:
20482 : 5958534 : ret = gimplify_bind_expr (expr_p, pre_p);
20483 : 5958534 : break;
20484 : :
20485 : 194582 : case LOOP_EXPR:
20486 : 194582 : ret = gimplify_loop_expr (expr_p, pre_p);
20487 : 194582 : break;
20488 : :
20489 : 51843 : case SWITCH_EXPR:
20490 : 51843 : ret = gimplify_switch_expr (expr_p, pre_p);
20491 : 51843 : break;
20492 : :
20493 : 3095 : case EXIT_EXPR:
20494 : 3095 : ret = gimplify_exit_expr (expr_p);
20495 : 3095 : break;
20496 : :
20497 : 1098182 : case GOTO_EXPR:
20498 : : /* If the target is not LABEL, then it is a computed jump
20499 : : and the target needs to be gimplified. */
20500 : 1098182 : if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
20501 : : {
20502 : 1099 : ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
20503 : : NULL, is_gimple_val, fb_rvalue);
20504 : 1099 : if (ret == GS_ERROR)
20505 : : break;
20506 : : }
20507 : 2196362 : gimplify_seq_add_stmt (pre_p,
20508 : 1098181 : gimple_build_goto (GOTO_DESTINATION (*expr_p)));
20509 : 1098181 : ret = GS_ALL_DONE;
20510 : 1098181 : break;
20511 : :
20512 : 105812 : case PREDICT_EXPR:
20513 : 423248 : gimplify_seq_add_stmt (pre_p,
20514 : 105812 : gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
20515 : 105812 : PREDICT_EXPR_OUTCOME (*expr_p)));
20516 : 105812 : ret = GS_ALL_DONE;
20517 : 105812 : break;
20518 : :
20519 : 2510213 : case LABEL_EXPR:
20520 : 2510213 : ret = gimplify_label_expr (expr_p, pre_p);
20521 : 2510213 : label = LABEL_EXPR_LABEL (*expr_p);
20522 : 2510213 : gcc_assert (decl_function_context (label) == current_function_decl);
20523 : :
20524 : : /* If the label is used in a goto statement, or address of the label
20525 : : is taken, we need to unpoison all variables that were seen so far.
20526 : : Doing so would prevent us from reporting a false positives. */
20527 : 2510213 : if (asan_poisoned_variables
20528 : 3807 : && asan_used_labels != NULL
20529 : 1213 : && asan_used_labels->contains (label)
20530 : 2511027 : && !gimplify_omp_ctxp)
20531 : 806 : asan_poison_variables (asan_poisoned_variables, false, pre_p);
20532 : : break;
20533 : :
20534 : 1061903 : case CASE_LABEL_EXPR:
20535 : 1061903 : ret = gimplify_case_label_expr (expr_p, pre_p);
20536 : :
20537 : 1061903 : if (gimplify_ctxp->live_switch_vars)
20538 : 1061716 : asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
20539 : : pre_p);
20540 : : break;
20541 : :
20542 : 2238806 : case RETURN_EXPR:
20543 : 2238806 : ret = gimplify_return_expr (*expr_p, pre_p);
20544 : 2238806 : break;
20545 : :
20546 : 945460 : case CONSTRUCTOR:
20547 : : /* Don't reduce this in place; let gimplify_init_constructor work its
20548 : : magic. Buf if we're just elaborating this for side effects, just
20549 : : gimplify any element that has side-effects. */
20550 : 945460 : if (fallback == fb_none)
20551 : : {
20552 : 343 : unsigned HOST_WIDE_INT ix;
20553 : 343 : tree val;
20554 : 343 : tree temp = NULL_TREE;
20555 : 359 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
20556 : 16 : if (TREE_SIDE_EFFECTS (val))
20557 : 4 : append_to_statement_list (val, &temp);
20558 : :
20559 : 343 : *expr_p = temp;
20560 : 343 : ret = temp ? GS_OK : GS_ALL_DONE;
20561 : : }
20562 : : /* C99 code may assign to an array in a constructed
20563 : : structure or union, and this has undefined behavior only
20564 : : on execution, so create a temporary if an lvalue is
20565 : : required. */
20566 : 945117 : else if (fallback == fb_lvalue)
20567 : : {
20568 : 12 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20569 : 12 : mark_addressable (*expr_p);
20570 : 12 : ret = GS_OK;
20571 : : }
20572 : : else
20573 : : ret = GS_ALL_DONE;
20574 : : break;
20575 : :
20576 : : /* The following are special cases that are not handled by the
20577 : : original GIMPLE grammar. */
20578 : :
20579 : : /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
20580 : : eliminated. */
20581 : 424525 : case SAVE_EXPR:
20582 : 424525 : ret = gimplify_save_expr (expr_p, pre_p, post_p);
20583 : 424525 : break;
20584 : :
20585 : 418957 : case BIT_FIELD_REF:
20586 : 418957 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20587 : : post_p, is_gimple_lvalue, fb_either);
20588 : 418957 : recalculate_side_effects (*expr_p);
20589 : 418957 : break;
20590 : :
20591 : 1527 : case TARGET_MEM_REF:
20592 : 1527 : {
20593 : 1527 : enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
20594 : :
20595 : 1527 : if (TMR_BASE (*expr_p))
20596 : 1527 : r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
20597 : : post_p, is_gimple_mem_ref_addr, fb_either);
20598 : 1527 : if (TMR_INDEX (*expr_p))
20599 : 1119 : r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
20600 : : post_p, is_gimple_val, fb_rvalue);
20601 : 1527 : if (TMR_INDEX2 (*expr_p))
20602 : 60 : r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
20603 : : post_p, is_gimple_val, fb_rvalue);
20604 : : /* TMR_STEP and TMR_OFFSET are always integer constants. */
20605 : 1527 : ret = MIN (r0, r1);
20606 : : }
20607 : : break;
20608 : :
20609 : 0 : case NON_LVALUE_EXPR:
20610 : : /* This should have been stripped above. */
20611 : 0 : gcc_unreachable ();
20612 : :
20613 : 95373 : case ASM_EXPR:
20614 : 95373 : ret = gimplify_asm_expr (expr_p, pre_p, post_p);
20615 : 95373 : break;
20616 : :
20617 : 523775 : case TRY_FINALLY_EXPR:
20618 : 523775 : case TRY_CATCH_EXPR:
20619 : 523775 : {
20620 : 523775 : gimple_seq eval, cleanup;
20621 : 523775 : gtry *try_;
20622 : :
20623 : : /* Calls to destructors are generated automatically in FINALLY/CATCH
20624 : : block. They should have location as UNKNOWN_LOCATION. However,
20625 : : gimplify_call_expr will reset these call stmts to input_location
20626 : : if it finds stmt's location is unknown. To prevent resetting for
20627 : : destructors, we set the input_location to unknown.
20628 : : Note that this only affects the destructor calls in FINALLY/CATCH
20629 : : block, and will automatically reset to its original value by the
20630 : : end of gimplify_expr. */
20631 : 523775 : input_location = UNKNOWN_LOCATION;
20632 : 523775 : eval = cleanup = NULL;
20633 : 523775 : gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
20634 : 523775 : bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
20635 : 523775 : if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20636 : 523775 : && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
20637 : : {
20638 : 86 : gimple_seq n = NULL, e = NULL;
20639 : 86 : gimplify_ctxp->in_handler_expr = true;
20640 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20641 : : 0), &n);
20642 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20643 : : 1), &e);
20644 : 86 : if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
20645 : : {
20646 : 85 : geh_else *stmt = gimple_build_eh_else (n, e);
20647 : 85 : gimple_seq_add_stmt (&cleanup, stmt);
20648 : : }
20649 : : }
20650 : : else
20651 : : {
20652 : 523689 : gimplify_ctxp->in_handler_expr = true;
20653 : 523689 : gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
20654 : : }
20655 : 523775 : gimplify_ctxp->in_handler_expr = save_in_handler_expr;
20656 : : /* Don't create bogus GIMPLE_TRY with empty cleanup. */
20657 : 523775 : if (gimple_seq_empty_p (cleanup))
20658 : : {
20659 : 27779 : gimple_seq_add_seq (pre_p, eval);
20660 : 27779 : ret = GS_ALL_DONE;
20661 : 27779 : break;
20662 : : }
20663 : 495996 : try_ = gimple_build_try (eval, cleanup,
20664 : 495996 : TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20665 : : ? GIMPLE_TRY_FINALLY
20666 : : : GIMPLE_TRY_CATCH);
20667 : 495996 : if (EXPR_HAS_LOCATION (save_expr))
20668 : 915084 : gimple_set_location (try_, EXPR_LOCATION (save_expr));
20669 : 38454 : else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
20670 : 28159 : gimple_set_location (try_, saved_location);
20671 : 495996 : if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
20672 : 184616 : gimple_try_set_catch_is_cleanup (try_,
20673 : 184616 : TRY_CATCH_IS_CLEANUP (*expr_p));
20674 : 495996 : gimplify_seq_add_stmt (pre_p, try_);
20675 : 495996 : ret = GS_ALL_DONE;
20676 : 495996 : break;
20677 : : }
20678 : :
20679 : 5456335 : case CLEANUP_POINT_EXPR:
20680 : 5456335 : ret = gimplify_cleanup_point_expr (expr_p, pre_p);
20681 : 5456335 : break;
20682 : :
20683 : 806952 : case TARGET_EXPR:
20684 : 806952 : ret = gimplify_target_expr (expr_p, pre_p, post_p);
20685 : 806952 : break;
20686 : :
20687 : 39417 : case CATCH_EXPR:
20688 : 39417 : {
20689 : 39417 : gimple *c;
20690 : 39417 : gimple_seq handler = NULL;
20691 : 39417 : gimplify_and_add (CATCH_BODY (*expr_p), &handler);
20692 : 39417 : c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
20693 : 39417 : gimplify_seq_add_stmt (pre_p, c);
20694 : 39417 : ret = GS_ALL_DONE;
20695 : 39417 : break;
20696 : : }
20697 : :
20698 : 4669 : case EH_FILTER_EXPR:
20699 : 4669 : {
20700 : 4669 : gimple *ehf;
20701 : 4669 : gimple_seq failure = NULL;
20702 : :
20703 : 4669 : gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
20704 : 4669 : ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
20705 : 4669 : copy_warning (ehf, *expr_p);
20706 : 4669 : gimplify_seq_add_stmt (pre_p, ehf);
20707 : 4669 : ret = GS_ALL_DONE;
20708 : 4669 : break;
20709 : : }
20710 : :
20711 : 50818 : case OBJ_TYPE_REF:
20712 : 50818 : {
20713 : 50818 : enum gimplify_status r0, r1;
20714 : 50818 : r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
20715 : : post_p, is_gimple_val, fb_rvalue);
20716 : 50818 : r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
20717 : : post_p, is_gimple_val, fb_rvalue);
20718 : 50818 : TREE_SIDE_EFFECTS (*expr_p) = 0;
20719 : 50818 : ret = MIN (r0, r1);
20720 : : }
20721 : : break;
20722 : :
20723 : 40494 : case LABEL_DECL:
20724 : : /* We get here when taking the address of a label. We mark
20725 : : the label as "forced"; meaning it can never be removed and
20726 : : it is a potential target for any computed goto. */
20727 : 40494 : FORCED_LABEL (*expr_p) = 1;
20728 : 40494 : ret = GS_ALL_DONE;
20729 : 40494 : break;
20730 : :
20731 : 8679403 : case STATEMENT_LIST:
20732 : 8679403 : ret = gimplify_statement_list (expr_p, pre_p);
20733 : 8679403 : break;
20734 : :
20735 : 1553 : case WITH_SIZE_EXPR:
20736 : 1553 : {
20737 : 1835 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20738 : : post_p == &internal_post ? NULL : post_p,
20739 : : gimple_test_f, fallback);
20740 : 1553 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20741 : : is_gimple_val, fb_rvalue);
20742 : 1553 : ret = GS_ALL_DONE;
20743 : : }
20744 : 1553 : break;
20745 : :
20746 : 107982323 : case VAR_DECL:
20747 : 107982323 : case PARM_DECL:
20748 : 107982323 : ret = gimplify_var_or_parm_decl (expr_p);
20749 : 107982323 : break;
20750 : :
20751 : 344130 : case RESULT_DECL:
20752 : : /* When within an OMP context, notice uses of variables. */
20753 : 344130 : if (gimplify_omp_ctxp)
20754 : 1351 : omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
20755 : : /* Handlers can refer to the function result; if that has been
20756 : : moved, we need to track it. */
20757 : 344130 : if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
20758 : 48 : *expr_p = gimplify_ctxp->return_temp;
20759 : : ret = GS_ALL_DONE;
20760 : : break;
20761 : :
20762 : 0 : case DEBUG_EXPR_DECL:
20763 : 0 : gcc_unreachable ();
20764 : :
20765 : 2418430 : case DEBUG_BEGIN_STMT:
20766 : 4836860 : gimplify_seq_add_stmt (pre_p,
20767 : : gimple_build_debug_begin_stmt
20768 : 2418430 : (TREE_BLOCK (*expr_p),
20769 : 2418430 : EXPR_LOCATION (*expr_p)));
20770 : 2418430 : ret = GS_ALL_DONE;
20771 : 2418430 : *expr_p = NULL;
20772 : 2418430 : break;
20773 : :
20774 : : case SSA_NAME:
20775 : : /* Allow callbacks into the gimplifier during optimization. */
20776 : : ret = GS_ALL_DONE;
20777 : : break;
20778 : :
20779 : 18214 : case OMP_PARALLEL:
20780 : 18214 : gimplify_omp_parallel (expr_p, pre_p);
20781 : 18214 : ret = GS_ALL_DONE;
20782 : 18214 : break;
20783 : :
20784 : 4014 : case OMP_TASK:
20785 : 4014 : gimplify_omp_task (expr_p, pre_p);
20786 : 4014 : ret = GS_ALL_DONE;
20787 : 4014 : break;
20788 : :
20789 : 10672 : case OMP_SIMD:
20790 : 10672 : {
20791 : : /* Temporarily disable into_ssa, as scan_omp_simd
20792 : : which calls copy_gimple_seq_and_replace_locals can't deal
20793 : : with SSA_NAMEs defined outside of the body properly. */
20794 : 10672 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
20795 : 10672 : gimplify_ctxp->into_ssa = false;
20796 : 10672 : ret = gimplify_omp_for (expr_p, pre_p);
20797 : 10672 : gimplify_ctxp->into_ssa = saved_into_ssa;
20798 : 10672 : break;
20799 : : }
20800 : :
20801 : 47437 : case OMP_FOR:
20802 : 47437 : case OMP_DISTRIBUTE:
20803 : 47437 : case OMP_TASKLOOP:
20804 : 47437 : case OMP_TILE:
20805 : 47437 : case OMP_UNROLL:
20806 : 47437 : case OACC_LOOP:
20807 : 47437 : ret = gimplify_omp_for (expr_p, pre_p);
20808 : 47437 : break;
20809 : :
20810 : 1057 : case OMP_LOOP:
20811 : 1057 : ret = gimplify_omp_loop (expr_p, pre_p);
20812 : 1057 : break;
20813 : :
20814 : 708 : case OACC_CACHE:
20815 : 708 : gimplify_oacc_cache (expr_p, pre_p);
20816 : 708 : ret = GS_ALL_DONE;
20817 : 708 : break;
20818 : :
20819 : 254 : case OACC_DECLARE:
20820 : 254 : gimplify_oacc_declare (expr_p, pre_p);
20821 : 254 : ret = GS_ALL_DONE;
20822 : 254 : break;
20823 : :
20824 : 38958 : case OACC_HOST_DATA:
20825 : 38958 : case OACC_DATA:
20826 : 38958 : case OACC_KERNELS:
20827 : 38958 : case OACC_PARALLEL:
20828 : 38958 : case OACC_SERIAL:
20829 : 38958 : case OMP_SCOPE:
20830 : 38958 : case OMP_SECTIONS:
20831 : 38958 : case OMP_SINGLE:
20832 : 38958 : case OMP_TARGET:
20833 : 38958 : case OMP_TARGET_DATA:
20834 : 38958 : case OMP_TEAMS:
20835 : 38958 : gimplify_omp_workshare (expr_p, pre_p);
20836 : 38958 : ret = GS_ALL_DONE;
20837 : 38958 : break;
20838 : :
20839 : 11928 : case OACC_ENTER_DATA:
20840 : 11928 : case OACC_EXIT_DATA:
20841 : 11928 : case OACC_UPDATE:
20842 : 11928 : case OMP_TARGET_UPDATE:
20843 : 11928 : case OMP_TARGET_ENTER_DATA:
20844 : 11928 : case OMP_TARGET_EXIT_DATA:
20845 : 11928 : gimplify_omp_target_update (expr_p, pre_p);
20846 : 11928 : ret = GS_ALL_DONE;
20847 : 11928 : break;
20848 : :
20849 : 6981 : case OMP_SECTION:
20850 : 6981 : case OMP_STRUCTURED_BLOCK:
20851 : 6981 : case OMP_MASTER:
20852 : 6981 : case OMP_MASKED:
20853 : 6981 : case OMP_ORDERED:
20854 : 6981 : case OMP_CRITICAL:
20855 : 6981 : case OMP_SCAN:
20856 : 6981 : {
20857 : 6981 : gimple_seq body = NULL;
20858 : 6981 : gimple *g;
20859 : 6981 : bool saved_in_omp_construct = in_omp_construct;
20860 : :
20861 : 6981 : in_omp_construct = true;
20862 : 6981 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20863 : 6981 : in_omp_construct = saved_in_omp_construct;
20864 : 6981 : switch (TREE_CODE (*expr_p))
20865 : : {
20866 : 1271 : case OMP_SECTION:
20867 : 1271 : g = gimple_build_omp_section (body);
20868 : 1271 : break;
20869 : 774 : case OMP_STRUCTURED_BLOCK:
20870 : 774 : g = gimple_build_omp_structured_block (body);
20871 : 774 : break;
20872 : 886 : case OMP_MASTER:
20873 : 886 : g = gimple_build_omp_master (body);
20874 : 886 : break;
20875 : 1940 : case OMP_ORDERED:
20876 : 1940 : g = gimplify_omp_ordered (*expr_p, body);
20877 : 1940 : if (OMP_BODY (*expr_p) == NULL_TREE
20878 : 1940 : && gimple_code (g) == GIMPLE_OMP_ORDERED)
20879 : 1015 : gimple_omp_ordered_standalone (g);
20880 : : break;
20881 : 446 : case OMP_MASKED:
20882 : 446 : gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
20883 : : pre_p, ORT_WORKSHARE, OMP_MASKED);
20884 : 446 : gimplify_adjust_omp_clauses (pre_p, body,
20885 : : &OMP_MASKED_CLAUSES (*expr_p),
20886 : : OMP_MASKED);
20887 : 892 : g = gimple_build_omp_masked (body,
20888 : 446 : OMP_MASKED_CLAUSES (*expr_p));
20889 : 446 : break;
20890 : 546 : case OMP_CRITICAL:
20891 : 546 : gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
20892 : : pre_p, ORT_WORKSHARE, OMP_CRITICAL);
20893 : 546 : gimplify_adjust_omp_clauses (pre_p, body,
20894 : : &OMP_CRITICAL_CLAUSES (*expr_p),
20895 : : OMP_CRITICAL);
20896 : 1638 : g = gimple_build_omp_critical (body,
20897 : 546 : OMP_CRITICAL_NAME (*expr_p),
20898 : 546 : OMP_CRITICAL_CLAUSES (*expr_p));
20899 : 546 : break;
20900 : 1118 : case OMP_SCAN:
20901 : 1118 : gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
20902 : : pre_p, ORT_WORKSHARE, OMP_SCAN);
20903 : 1118 : gimplify_adjust_omp_clauses (pre_p, body,
20904 : : &OMP_SCAN_CLAUSES (*expr_p),
20905 : : OMP_SCAN);
20906 : 1118 : g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
20907 : 1118 : break;
20908 : 0 : default:
20909 : 0 : gcc_unreachable ();
20910 : : }
20911 : 6981 : gimplify_seq_add_stmt (pre_p, g);
20912 : 6981 : ret = GS_ALL_DONE;
20913 : 6981 : break;
20914 : : }
20915 : :
20916 : 611 : case OMP_TASKGROUP:
20917 : 611 : {
20918 : 611 : gimple_seq body = NULL;
20919 : :
20920 : 611 : tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
20921 : 611 : bool saved_in_omp_construct = in_omp_construct;
20922 : 611 : gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
20923 : : OMP_TASKGROUP);
20924 : 611 : gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
20925 : :
20926 : 611 : in_omp_construct = true;
20927 : 611 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20928 : 611 : in_omp_construct = saved_in_omp_construct;
20929 : 611 : gimple_seq cleanup = NULL;
20930 : 611 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
20931 : 611 : gimple *g = gimple_build_call (fn, 0);
20932 : 611 : gimple_seq_add_stmt (&cleanup, g);
20933 : 611 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
20934 : 611 : body = NULL;
20935 : 611 : gimple_seq_add_stmt (&body, g);
20936 : 611 : g = gimple_build_omp_taskgroup (body, *pclauses);
20937 : 611 : gimplify_seq_add_stmt (pre_p, g);
20938 : 611 : ret = GS_ALL_DONE;
20939 : 611 : break;
20940 : : }
20941 : :
20942 : 612 : case OMP_INTEROP:
20943 : 612 : ret = gimplify_omp_interop (expr_p, pre_p);
20944 : 612 : break;
20945 : 10249 : case OMP_ATOMIC:
20946 : 10249 : case OMP_ATOMIC_READ:
20947 : 10249 : case OMP_ATOMIC_CAPTURE_OLD:
20948 : 10249 : case OMP_ATOMIC_CAPTURE_NEW:
20949 : 10249 : ret = gimplify_omp_atomic (expr_p, pre_p);
20950 : 10249 : break;
20951 : :
20952 : 846 : case OMP_DISPATCH:
20953 : 846 : ret = gimplify_omp_dispatch (expr_p, pre_p);
20954 : 846 : break;
20955 : :
20956 : 162 : case OMP_METADIRECTIVE:
20957 : 162 : ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
20958 : : gimple_test_f, fallback);
20959 : 162 : break;
20960 : :
20961 : 36 : case OMP_NEXT_VARIANT:
20962 : 36 : case OMP_TARGET_DEVICE_MATCHES:
20963 : : /* These are placeholders for constants. There's nothing to do with
20964 : : them here but we must mark the containing function as needing
20965 : : to run the ompdevlow pass to resolve them. Note that
20966 : : OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
20967 : : the front ends. */
20968 : 36 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
20969 : 36 : ret = GS_ALL_DONE;
20970 : 36 : break;
20971 : :
20972 : 0 : case OMP_DECLARE_MAPPER:
20973 : 0 : ret = gimplify_omp_declare_mapper (expr_p);
20974 : 0 : break;
20975 : :
20976 : 478 : case TRANSACTION_EXPR:
20977 : 478 : ret = gimplify_transaction (expr_p, pre_p);
20978 : 478 : break;
20979 : :
20980 : 296954 : case TRUTH_AND_EXPR:
20981 : 296954 : case TRUTH_OR_EXPR:
20982 : 296954 : case TRUTH_XOR_EXPR:
20983 : 296954 : {
20984 : 296954 : tree orig_type = TREE_TYPE (*expr_p);
20985 : 296954 : tree new_type, xop0, xop1;
20986 : 296954 : *expr_p = gimple_boolify (*expr_p);
20987 : 296954 : new_type = TREE_TYPE (*expr_p);
20988 : 296954 : if (!useless_type_conversion_p (orig_type, new_type))
20989 : : {
20990 : 1122 : *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
20991 : 1122 : ret = GS_OK;
20992 : 1122 : break;
20993 : : }
20994 : :
20995 : : /* Boolified binary truth expressions are semantically equivalent
20996 : : to bitwise binary expressions. Canonicalize them to the
20997 : : bitwise variant. */
20998 : 295832 : switch (TREE_CODE (*expr_p))
20999 : : {
21000 : 175811 : case TRUTH_AND_EXPR:
21001 : 175811 : TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
21002 : 175811 : break;
21003 : 119432 : case TRUTH_OR_EXPR:
21004 : 119432 : TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
21005 : 119432 : break;
21006 : 589 : case TRUTH_XOR_EXPR:
21007 : 589 : TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
21008 : 589 : break;
21009 : : default:
21010 : : break;
21011 : : }
21012 : : /* Now make sure that operands have compatible type to
21013 : : expression's new_type. */
21014 : 295832 : xop0 = TREE_OPERAND (*expr_p, 0);
21015 : 295832 : xop1 = TREE_OPERAND (*expr_p, 1);
21016 : 295832 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
21017 : 326 : TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
21018 : : new_type,
21019 : : xop0);
21020 : 295832 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
21021 : 366 : TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
21022 : : new_type,
21023 : : xop1);
21024 : : /* Continue classified as tcc_binary. */
21025 : 295832 : goto expr_2;
21026 : : }
21027 : :
21028 : 9435 : case VEC_COND_EXPR:
21029 : 9435 : goto expr_3;
21030 : :
21031 : 132610 : case VEC_PERM_EXPR:
21032 : : /* Classified as tcc_expression. */
21033 : 132610 : goto expr_3;
21034 : :
21035 : 48 : case BIT_INSERT_EXPR:
21036 : : /* Argument 3 is a constant. */
21037 : 48 : goto expr_2;
21038 : :
21039 : 3826220 : case POINTER_PLUS_EXPR:
21040 : 3826220 : {
21041 : 3826220 : enum gimplify_status r0, r1;
21042 : 3826220 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21043 : : post_p, is_gimple_val, fb_rvalue);
21044 : 3826220 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21045 : : post_p, is_gimple_val, fb_rvalue);
21046 : 3826220 : recalculate_side_effects (*expr_p);
21047 : 3826220 : ret = MIN (r0, r1);
21048 : : break;
21049 : : }
21050 : :
21051 : 43796945 : default:
21052 : 43796945 : switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
21053 : : {
21054 : 6924478 : case tcc_comparison:
21055 : : /* Handle comparison of objects of non scalar mode aggregates
21056 : : with a call to memcmp. It would be nice to only have to do
21057 : : this for variable-sized objects, but then we'd have to allow
21058 : : the same nest of reference nodes we allow for MODIFY_EXPR and
21059 : : that's too complex.
21060 : :
21061 : : Compare scalar mode aggregates as scalar mode values. Using
21062 : : memcmp for them would be very inefficient at best, and is
21063 : : plain wrong if bitfields are involved. */
21064 : 6924478 : if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
21065 : : ret = GS_ERROR;
21066 : : else
21067 : : {
21068 : 6924478 : tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
21069 : :
21070 : : /* Vector comparisons need no boolification. */
21071 : 6924478 : if (TREE_CODE (type) == VECTOR_TYPE)
21072 : 11463 : goto expr_2;
21073 : 6913015 : else if (!AGGREGATE_TYPE_P (type))
21074 : : {
21075 : 6912999 : tree org_type = TREE_TYPE (*expr_p);
21076 : 6912999 : *expr_p = gimple_boolify (*expr_p);
21077 : 6912999 : if (!useless_type_conversion_p (org_type,
21078 : 6912999 : TREE_TYPE (*expr_p)))
21079 : : {
21080 : 53289 : *expr_p = fold_convert_loc (input_location,
21081 : : org_type, *expr_p);
21082 : 53289 : ret = GS_OK;
21083 : : }
21084 : : else
21085 : 6859710 : goto expr_2;
21086 : : }
21087 : 16 : else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
21088 : 16 : ret = gimplify_scalar_mode_aggregate_compare (expr_p);
21089 : : else
21090 : 0 : ret = gimplify_variable_sized_compare (expr_p);
21091 : : }
21092 : : break;
21093 : :
21094 : : /* If *EXPR_P does not need to be special-cased, handle it
21095 : : according to its class. */
21096 : 1569422 : case tcc_unary:
21097 : 1569422 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21098 : : post_p, is_gimple_val, fb_rvalue);
21099 : 1569422 : break;
21100 : :
21101 : 25906825 : case tcc_binary:
21102 : 25906825 : expr_2:
21103 : 25906825 : {
21104 : 25906825 : enum gimplify_status r0, r1;
21105 : :
21106 : 25906825 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21107 : : post_p, is_gimple_val, fb_rvalue);
21108 : 25906825 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21109 : : post_p, is_gimple_val, fb_rvalue);
21110 : :
21111 : 25906825 : ret = MIN (r0, r1);
21112 : : break;
21113 : : }
21114 : :
21115 : 142045 : expr_3:
21116 : 142045 : {
21117 : 142045 : enum gimplify_status r0, r1, r2;
21118 : :
21119 : 142045 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21120 : : post_p, is_gimple_val, fb_rvalue);
21121 : 142045 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21122 : : post_p, is_gimple_val, fb_rvalue);
21123 : 142045 : r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
21124 : : post_p, is_gimple_val, fb_rvalue);
21125 : :
21126 : 142045 : ret = MIN (MIN (r0, r1), r2);
21127 : : break;
21128 : : }
21129 : :
21130 : 16563273 : case tcc_declaration:
21131 : 16563273 : case tcc_constant:
21132 : 16563273 : ret = GS_ALL_DONE;
21133 : 16563273 : goto dont_recalculate;
21134 : :
21135 : 0 : default:
21136 : 0 : gcc_unreachable ();
21137 : : }
21138 : :
21139 : 27671597 : recalculate_side_effects (*expr_p);
21140 : :
21141 : 474524627 : dont_recalculate:
21142 : : break;
21143 : : }
21144 : :
21145 : 474524627 : gcc_assert (*expr_p || ret != GS_OK);
21146 : : }
21147 : 479527543 : while (ret == GS_OK);
21148 : :
21149 : : /* If we encountered an error_mark somewhere nested inside, either
21150 : : stub out the statement or propagate the error back out. */
21151 : 461403419 : if (ret == GS_ERROR)
21152 : : {
21153 : 5539 : if (is_statement)
21154 : 4011 : *expr_p = NULL;
21155 : 5539 : goto out;
21156 : : }
21157 : :
21158 : : /* This was only valid as a return value from the langhook, which
21159 : : we handled. Make sure it doesn't escape from any other context. */
21160 : 461397880 : gcc_assert (ret != GS_UNHANDLED);
21161 : :
21162 : 461397880 : if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
21163 : : {
21164 : : /* We aren't looking for a value, and we don't have a valid
21165 : : statement. If it doesn't have side-effects, throw it away.
21166 : : We can also get here with code such as "*&&L;", where L is
21167 : : a LABEL_DECL that is marked as FORCED_LABEL. */
21168 : 1165870 : if (TREE_CODE (*expr_p) == LABEL_DECL
21169 : 1165870 : || !TREE_SIDE_EFFECTS (*expr_p))
21170 : 1164849 : *expr_p = NULL;
21171 : 1021 : else if (!TREE_THIS_VOLATILE (*expr_p))
21172 : : {
21173 : : /* This is probably a _REF that contains something nested that
21174 : : has side effects. Recurse through the operands to find it. */
21175 : 0 : enum tree_code code = TREE_CODE (*expr_p);
21176 : :
21177 : 0 : switch (code)
21178 : : {
21179 : 0 : case COMPONENT_REF:
21180 : 0 : case REALPART_EXPR:
21181 : 0 : case IMAGPART_EXPR:
21182 : 0 : case VIEW_CONVERT_EXPR:
21183 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21184 : : gimple_test_f, fallback);
21185 : 0 : break;
21186 : :
21187 : 0 : case ARRAY_REF:
21188 : 0 : case ARRAY_RANGE_REF:
21189 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21190 : : gimple_test_f, fallback);
21191 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
21192 : : gimple_test_f, fallback);
21193 : 0 : break;
21194 : :
21195 : 0 : default:
21196 : : /* Anything else with side-effects must be converted to
21197 : : a valid statement before we get here. */
21198 : 0 : gcc_unreachable ();
21199 : : }
21200 : :
21201 : 0 : *expr_p = NULL;
21202 : : }
21203 : 1021 : else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
21204 : 971 : && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
21205 : 1980 : && !is_empty_type (TREE_TYPE (*expr_p)))
21206 : : {
21207 : : /* Historically, the compiler has treated a bare reference
21208 : : to a non-BLKmode volatile lvalue as forcing a load. */
21209 : 893 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
21210 : :
21211 : : /* Normally, we do not want to create a temporary for a
21212 : : TREE_ADDRESSABLE type because such a type should not be
21213 : : copied by bitwise-assignment. However, we make an
21214 : : exception here, as all we are doing here is ensuring that
21215 : : we read the bytes that make up the type. We use
21216 : : create_tmp_var_raw because create_tmp_var will abort when
21217 : : given a TREE_ADDRESSABLE type. */
21218 : 893 : tree tmp = create_tmp_var_raw (type, "vol");
21219 : 893 : gimple_add_tmp_var (tmp);
21220 : 893 : gimplify_assign (tmp, *expr_p, pre_p);
21221 : 893 : *expr_p = NULL;
21222 : : }
21223 : : else
21224 : : /* We can't do anything useful with a volatile reference to
21225 : : an incomplete type, so just throw it away. Likewise for
21226 : : a BLKmode type, since any implicit inner load should
21227 : : already have been turned into an explicit one by the
21228 : : gimplification process. */
21229 : 128 : *expr_p = NULL;
21230 : : }
21231 : :
21232 : : /* If we are gimplifying at the statement level, we're done. Tack
21233 : : everything together and return. */
21234 : 461397880 : if (fallback == fb_none || is_statement)
21235 : : {
21236 : : /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
21237 : : it out for GC to reclaim it. */
21238 : 100690055 : *expr_p = NULL_TREE;
21239 : :
21240 : 100690055 : if (!gimple_seq_empty_p (internal_pre)
21241 : 100690055 : || !gimple_seq_empty_p (internal_post))
21242 : : {
21243 : 19 : gimplify_seq_add_seq (&internal_pre, internal_post);
21244 : 19 : gimplify_seq_add_seq (pre_p, internal_pre);
21245 : : }
21246 : :
21247 : : /* The result of gimplifying *EXPR_P is going to be the last few
21248 : : statements in *PRE_P and *POST_P. Add location information
21249 : : to all the statements that were added by the gimplification
21250 : : helpers. */
21251 : 100690055 : if (!gimple_seq_empty_p (*pre_p))
21252 : 98185958 : annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
21253 : :
21254 : 100690055 : if (!gimple_seq_empty_p (*post_p))
21255 : 19 : annotate_all_with_location_after (*post_p, post_last_gsi,
21256 : : input_location);
21257 : :
21258 : 100690055 : goto out;
21259 : : }
21260 : :
21261 : : #ifdef ENABLE_GIMPLE_CHECKING
21262 : 360707825 : if (*expr_p)
21263 : : {
21264 : 360707825 : enum tree_code code = TREE_CODE (*expr_p);
21265 : : /* These expressions should already be in gimple IR form. */
21266 : 360707825 : gcc_assert (code != MODIFY_EXPR
21267 : : && code != ASM_EXPR
21268 : : && code != BIND_EXPR
21269 : : && code != CATCH_EXPR
21270 : : && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
21271 : : && code != EH_FILTER_EXPR
21272 : : && code != GOTO_EXPR
21273 : : && code != LABEL_EXPR
21274 : : && code != LOOP_EXPR
21275 : : && code != SWITCH_EXPR
21276 : : && code != TRY_FINALLY_EXPR
21277 : : && code != EH_ELSE_EXPR
21278 : : && code != OACC_PARALLEL
21279 : : && code != OACC_KERNELS
21280 : : && code != OACC_SERIAL
21281 : : && code != OACC_DATA
21282 : : && code != OACC_HOST_DATA
21283 : : && code != OACC_DECLARE
21284 : : && code != OACC_UPDATE
21285 : : && code != OACC_ENTER_DATA
21286 : : && code != OACC_EXIT_DATA
21287 : : && code != OACC_CACHE
21288 : : && code != OMP_CRITICAL
21289 : : && code != OMP_FOR
21290 : : && code != OACC_LOOP
21291 : : && code != OMP_MASTER
21292 : : && code != OMP_MASKED
21293 : : && code != OMP_TASKGROUP
21294 : : && code != OMP_ORDERED
21295 : : && code != OMP_PARALLEL
21296 : : && code != OMP_SCAN
21297 : : && code != OMP_SECTIONS
21298 : : && code != OMP_SECTION
21299 : : && code != OMP_STRUCTURED_BLOCK
21300 : : && code != OMP_SINGLE
21301 : : && code != OMP_SCOPE
21302 : : && code != OMP_DISPATCH);
21303 : : }
21304 : : #endif
21305 : :
21306 : : /* Otherwise we're gimplifying a subexpression, so the resulting
21307 : : value is interesting. If it's a valid operand that matches
21308 : : GIMPLE_TEST_F, we're done. Unless we are handling some
21309 : : post-effects internally; if that's the case, we need to copy into
21310 : : a temporary before adding the post-effects to POST_P. */
21311 : 360707825 : if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
21312 : 334976373 : goto out;
21313 : :
21314 : : /* Otherwise, we need to create a new temporary for the gimplified
21315 : : expression. */
21316 : :
21317 : : /* We can't return an lvalue if we have an internal postqueue. The
21318 : : object the lvalue refers to would (probably) be modified by the
21319 : : postqueue; we need to copy the value out first, which means an
21320 : : rvalue. */
21321 : 25731452 : if ((fallback & fb_lvalue)
21322 : 585427 : && gimple_seq_empty_p (internal_post)
21323 : 26316879 : && is_gimple_addressable (*expr_p))
21324 : : {
21325 : : /* An lvalue will do. Take the address of the expression, store it
21326 : : in a temporary, and replace the expression with an INDIRECT_REF of
21327 : : that temporary. */
21328 : 15 : tree ref_alias_type = reference_alias_ptr_type (*expr_p);
21329 : 15 : unsigned int ref_align = get_object_alignment (*expr_p);
21330 : 15 : tree ref_type = TREE_TYPE (*expr_p);
21331 : 15 : tmp = build_fold_addr_expr_loc (input_location, *expr_p);
21332 : 15 : gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
21333 : 15 : if (TYPE_ALIGN (ref_type) != ref_align)
21334 : 1 : ref_type = build_aligned_type (ref_type, ref_align);
21335 : 15 : *expr_p = build2 (MEM_REF, ref_type,
21336 : : tmp, build_zero_cst (ref_alias_type));
21337 : : }
21338 : 25731437 : else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
21339 : : {
21340 : : /* An rvalue will do. Assign the gimplified expression into a
21341 : : new temporary TMP and replace the original expression with
21342 : : TMP. First, make sure that the expression has a type so that
21343 : : it can be assigned into a temporary. */
21344 : 25731429 : gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
21345 : 25731429 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
21346 : : }
21347 : : else
21348 : : {
21349 : : #ifdef ENABLE_GIMPLE_CHECKING
21350 : 8 : if (!(fallback & fb_mayfail))
21351 : : {
21352 : 0 : fprintf (stderr, "gimplification failed:\n");
21353 : 0 : print_generic_expr (stderr, *expr_p);
21354 : 0 : debug_tree (*expr_p);
21355 : 0 : internal_error ("gimplification failed");
21356 : : }
21357 : : #endif
21358 : 8 : gcc_assert (fallback & fb_mayfail);
21359 : :
21360 : : /* If this is an asm statement, and the user asked for the
21361 : : impossible, don't die. Fail and let gimplify_asm_expr
21362 : : issue an error. */
21363 : 8 : ret = GS_ERROR;
21364 : 8 : goto out;
21365 : : }
21366 : :
21367 : : /* Make sure the temporary matches our predicate. */
21368 : 25731444 : gcc_assert ((*gimple_test_f) (*expr_p));
21369 : :
21370 : 25731444 : if (!gimple_seq_empty_p (internal_post))
21371 : : {
21372 : 0 : annotate_all_with_location (internal_post, input_location);
21373 : 0 : gimplify_seq_add_seq (pre_p, internal_post);
21374 : : }
21375 : :
21376 : 25731444 : out:
21377 : 461403419 : input_location = saved_location;
21378 : 461403419 : return ret;
21379 : : }
21380 : :
21381 : : /* Like gimplify_expr but make sure the gimplified result is not itself
21382 : : a SSA name (but a decl if it were). Temporaries required by
21383 : : evaluating *EXPR_P may be still SSA names. */
21384 : :
21385 : : static enum gimplify_status
21386 : 33352205 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
21387 : : bool (*gimple_test_f) (tree), fallback_t fallback,
21388 : : bool allow_ssa)
21389 : : {
21390 : 33352205 : enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
21391 : : gimple_test_f, fallback);
21392 : 33352205 : if (! allow_ssa
21393 : 264949 : && TREE_CODE (*expr_p) == SSA_NAME)
21394 : 67729 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
21395 : 33352205 : return ret;
21396 : : }
21397 : :
21398 : : /* Look through TYPE for variable-sized objects and gimplify each such
21399 : : size that we find. Add to LIST_P any statements generated. */
21400 : :
21401 : : void
21402 : 8169922 : gimplify_type_sizes (tree type, gimple_seq *list_p)
21403 : : {
21404 : 8169922 : if (type == NULL || type == error_mark_node)
21405 : : return;
21406 : :
21407 : 8169676 : const bool ignored_p
21408 : 8169676 : = TYPE_NAME (type)
21409 : 4027146 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21410 : 11977671 : && DECL_IGNORED_P (TYPE_NAME (type));
21411 : 8169676 : tree t;
21412 : :
21413 : : /* We first do the main variant, then copy into any other variants. */
21414 : 8169676 : type = TYPE_MAIN_VARIANT (type);
21415 : :
21416 : : /* Avoid infinite recursion. */
21417 : 8169676 : if (TYPE_SIZES_GIMPLIFIED (type))
21418 : : return;
21419 : :
21420 : 2449907 : TYPE_SIZES_GIMPLIFIED (type) = 1;
21421 : :
21422 : 2449907 : switch (TREE_CODE (type))
21423 : : {
21424 : 483490 : case INTEGER_TYPE:
21425 : 483490 : case ENUMERAL_TYPE:
21426 : 483490 : case BOOLEAN_TYPE:
21427 : 483490 : case REAL_TYPE:
21428 : 483490 : case FIXED_POINT_TYPE:
21429 : 483490 : gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
21430 : 483490 : gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
21431 : :
21432 : 8438243 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21433 : : {
21434 : 7954753 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
21435 : 7954753 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
21436 : : }
21437 : : break;
21438 : :
21439 : 179833 : case ARRAY_TYPE:
21440 : : /* These types may not have declarations, so handle them here. */
21441 : 179833 : gimplify_type_sizes (TREE_TYPE (type), list_p);
21442 : 179833 : gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
21443 : : /* Ensure VLA bounds aren't removed, for -O0 they should be variables
21444 : : with assigned stack slots, for -O1+ -g they should be tracked
21445 : : by VTA. */
21446 : 179833 : if (!ignored_p
21447 : 179833 : && TYPE_DOMAIN (type)
21448 : 359441 : && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
21449 : : {
21450 : 179608 : t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
21451 : 179608 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21452 : 0 : DECL_IGNORED_P (t) = 0;
21453 : 179608 : t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21454 : 179608 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21455 : 18683 : DECL_IGNORED_P (t) = 0;
21456 : : }
21457 : : break;
21458 : :
21459 : 491822 : case RECORD_TYPE:
21460 : 491822 : case UNION_TYPE:
21461 : 491822 : case QUAL_UNION_TYPE:
21462 : 10661800 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
21463 : 10169978 : if (TREE_CODE (field) == FIELD_DECL)
21464 : : {
21465 : 1079307 : gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
21466 : : /* Likewise, ensure variable offsets aren't removed. */
21467 : 1079307 : if (!ignored_p
21468 : 1079307 : && (t = DECL_FIELD_OFFSET (field))
21469 : 1079295 : && VAR_P (t)
21470 : 1079469 : && DECL_ARTIFICIAL (t))
21471 : 162 : DECL_IGNORED_P (t) = 0;
21472 : 1079307 : gimplify_one_sizepos (&DECL_SIZE (field), list_p);
21473 : 1079307 : gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
21474 : 1079307 : gimplify_type_sizes (TREE_TYPE (field), list_p);
21475 : : }
21476 : : break;
21477 : :
21478 : : case POINTER_TYPE:
21479 : : case REFERENCE_TYPE:
21480 : : /* We used to recurse on the pointed-to type here, which turned out to
21481 : : be incorrect because its definition might refer to variables not
21482 : : yet initialized at this point if a forward declaration is involved.
21483 : :
21484 : : It was actually useful for anonymous pointed-to types to ensure
21485 : : that the sizes evaluation dominates every possible later use of the
21486 : : values. Restricting to such types here would be safe since there
21487 : : is no possible forward declaration around, but would introduce an
21488 : : undesirable middle-end semantic to anonymity. We then defer to
21489 : : front-ends the responsibility of ensuring that the sizes are
21490 : : evaluated both early and late enough, e.g. by attaching artificial
21491 : : type declarations to the tree. */
21492 : : break;
21493 : :
21494 : : default:
21495 : : break;
21496 : : }
21497 : :
21498 : 2449907 : gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
21499 : 2449907 : gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
21500 : :
21501 : 14450994 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21502 : : {
21503 : 12001087 : TYPE_SIZE (t) = TYPE_SIZE (type);
21504 : 12001087 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
21505 : 12001087 : TYPE_SIZES_GIMPLIFIED (t) = 1;
21506 : : }
21507 : : }
21508 : :
21509 : : /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
21510 : : a size or position, has had all of its SAVE_EXPRs evaluated.
21511 : : We add any required statements to *STMT_P. */
21512 : :
21513 : : void
21514 : 9122605 : gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
21515 : : {
21516 : 9122605 : tree expr = *expr_p;
21517 : :
21518 : : /* We don't do anything if the value isn't there, is constant, or contains
21519 : : A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
21520 : : a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
21521 : : will want to replace it with a new variable, but that will cause problems
21522 : : if this type is from outside the function. It's OK to have that here. */
21523 : 9122605 : if (expr == NULL_TREE
21524 : 9126375 : || is_gimple_constant (expr)
21525 : 79620 : || VAR_P (expr)
21526 : 9198455 : || CONTAINS_PLACEHOLDER_P (expr))
21527 : 9046755 : return;
21528 : :
21529 : 75850 : *expr_p = unshare_expr (expr);
21530 : :
21531 : : /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
21532 : : if the def vanishes. */
21533 : 75850 : gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
21534 : :
21535 : : /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
21536 : : FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
21537 : : as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
21538 : 75850 : if (is_gimple_constant (*expr_p))
21539 : 533 : *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
21540 : : }
21541 : :
21542 : : /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
21543 : : containing the sequence of corresponding GIMPLE statements. If DO_PARMS
21544 : : is true, also gimplify the parameters. */
21545 : :
21546 : : gbind *
21547 : 2923921 : gimplify_body (tree fndecl, bool do_parms)
21548 : : {
21549 : 2923921 : location_t saved_location = input_location;
21550 : 2923921 : gimple_seq parm_stmts, parm_cleanup = NULL, seq;
21551 : 2923921 : gimple *outer_stmt;
21552 : 2923921 : gbind *outer_bind;
21553 : :
21554 : 2923921 : timevar_push (TV_TREE_GIMPLIFY);
21555 : :
21556 : 2923921 : init_tree_ssa (cfun);
21557 : :
21558 : : /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
21559 : : gimplification. */
21560 : 2923921 : default_rtl_profile ();
21561 : :
21562 : 2923921 : gcc_assert (gimplify_ctxp == NULL);
21563 : 2923921 : push_gimplify_context (true);
21564 : :
21565 : 2923921 : if (flag_openacc || flag_openmp)
21566 : : {
21567 : 53622 : gcc_assert (gimplify_omp_ctxp == NULL);
21568 : 53622 : if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
21569 : 9788 : gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
21570 : : }
21571 : :
21572 : : /* Unshare most shared trees in the body and in that of any nested functions.
21573 : : It would seem we don't have to do this for nested functions because
21574 : : they are supposed to be output and then the outer function gimplified
21575 : : first, but the g++ front end doesn't always do it that way. */
21576 : 2923921 : unshare_body (fndecl);
21577 : 2923921 : unvisit_body (fndecl);
21578 : :
21579 : : /* Make sure input_location isn't set to something weird. */
21580 : 2923921 : input_location = DECL_SOURCE_LOCATION (fndecl);
21581 : :
21582 : : /* Resolve callee-copies. This has to be done before processing
21583 : : the body so that DECL_VALUE_EXPR gets processed correctly. */
21584 : 2923921 : parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
21585 : :
21586 : : /* Gimplify the function's body. */
21587 : 2923921 : seq = NULL;
21588 : 2923921 : gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
21589 : 2923921 : outer_stmt = gimple_seq_first_nondebug_stmt (seq);
21590 : 2923921 : if (!outer_stmt)
21591 : : {
21592 : 25116 : outer_stmt = gimple_build_nop ();
21593 : 25116 : gimplify_seq_add_stmt (&seq, outer_stmt);
21594 : : }
21595 : :
21596 : : /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
21597 : : not the case, wrap everything in a GIMPLE_BIND to make it so. */
21598 : 2923921 : if (gimple_code (outer_stmt) == GIMPLE_BIND
21599 : 2923921 : && (gimple_seq_first_nondebug_stmt (seq)
21600 : 1545935 : == gimple_seq_last_nondebug_stmt (seq)))
21601 : : {
21602 : 1481970 : outer_bind = as_a <gbind *> (outer_stmt);
21603 : 1481970 : if (gimple_seq_first_stmt (seq) != outer_stmt
21604 : 1481970 : || gimple_seq_last_stmt (seq) != outer_stmt)
21605 : : {
21606 : : /* If there are debug stmts before or after outer_stmt, move them
21607 : : inside of outer_bind body. */
21608 : 1 : gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
21609 : 1 : gimple_seq second_seq = NULL;
21610 : 1 : if (gimple_seq_first_stmt (seq) != outer_stmt
21611 : 2 : && gimple_seq_last_stmt (seq) != outer_stmt)
21612 : : {
21613 : 0 : second_seq = gsi_split_seq_after (gsi);
21614 : 0 : gsi_remove (&gsi, false);
21615 : : }
21616 : 1 : else if (gimple_seq_first_stmt (seq) != outer_stmt)
21617 : 1 : gsi_remove (&gsi, false);
21618 : : else
21619 : : {
21620 : 0 : gsi_remove (&gsi, false);
21621 : 0 : second_seq = seq;
21622 : 0 : seq = NULL;
21623 : : }
21624 : 1 : gimple_seq_add_seq_without_update (&seq,
21625 : : gimple_bind_body (outer_bind));
21626 : 1 : gimple_seq_add_seq_without_update (&seq, second_seq);
21627 : 1 : gimple_bind_set_body (outer_bind, seq);
21628 : : }
21629 : : }
21630 : : else
21631 : 1441951 : outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
21632 : :
21633 : 2923921 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21634 : :
21635 : : /* If we had callee-copies statements, insert them at the beginning
21636 : : of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters. */
21637 : 2923921 : if (!gimple_seq_empty_p (parm_stmts))
21638 : : {
21639 : 41 : tree parm;
21640 : :
21641 : 41 : gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
21642 : 41 : if (parm_cleanup)
21643 : : {
21644 : 0 : gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
21645 : : GIMPLE_TRY_FINALLY);
21646 : 0 : parm_stmts = NULL;
21647 : 0 : gimple_seq_add_stmt (&parm_stmts, g);
21648 : : }
21649 : 41 : gimple_bind_set_body (outer_bind, parm_stmts);
21650 : :
21651 : 41 : for (parm = DECL_ARGUMENTS (current_function_decl);
21652 : 101 : parm; parm = DECL_CHAIN (parm))
21653 : 60 : if (DECL_HAS_VALUE_EXPR_P (parm))
21654 : : {
21655 : 0 : DECL_HAS_VALUE_EXPR_P (parm) = 0;
21656 : 0 : DECL_IGNORED_P (parm) = 0;
21657 : : }
21658 : : }
21659 : :
21660 : 2923921 : if ((flag_openacc || flag_openmp || flag_openmp_simd)
21661 : 55856 : && gimplify_omp_ctxp)
21662 : : {
21663 : 9841 : delete_omp_context (gimplify_omp_ctxp);
21664 : 9841 : gimplify_omp_ctxp = NULL;
21665 : : }
21666 : :
21667 : 2923921 : pop_gimplify_context (outer_bind);
21668 : 2923921 : gcc_assert (gimplify_ctxp == NULL);
21669 : :
21670 : 2923921 : if (flag_checking && !seen_error ())
21671 : 2872975 : verify_gimple_in_seq (gimple_bind_body (outer_bind));
21672 : :
21673 : 2923921 : timevar_pop (TV_TREE_GIMPLIFY);
21674 : 2923921 : input_location = saved_location;
21675 : :
21676 : 2923921 : return outer_bind;
21677 : : }
21678 : :
21679 : : typedef char *char_p; /* For DEF_VEC_P. */
21680 : :
21681 : : /* Return whether we should exclude FNDECL from instrumentation. */
21682 : :
21683 : : static bool
21684 : 39 : flag_instrument_functions_exclude_p (tree fndecl)
21685 : : {
21686 : 39 : vec<char_p> *v;
21687 : :
21688 : 39 : v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
21689 : 40 : if (v && v->length () > 0)
21690 : : {
21691 : 1 : const char *name;
21692 : 1 : int i;
21693 : 1 : char *s;
21694 : :
21695 : 1 : name = lang_hooks.decl_printable_name (fndecl, 1);
21696 : 2 : FOR_EACH_VEC_ELT (*v, i, s)
21697 : 1 : if (strstr (name, s) != NULL)
21698 : 2 : return true;
21699 : : }
21700 : :
21701 : 38 : v = (vec<char_p> *) flag_instrument_functions_exclude_files;
21702 : 39 : if (v && v->length () > 0)
21703 : : {
21704 : 1 : const char *name;
21705 : 1 : int i;
21706 : 1 : char *s;
21707 : :
21708 : 1 : name = DECL_SOURCE_FILE (fndecl);
21709 : 1 : FOR_EACH_VEC_ELT (*v, i, s)
21710 : 1 : if (strstr (name, s) != NULL)
21711 : 2 : return true;
21712 : : }
21713 : :
21714 : : return false;
21715 : : }
21716 : :
21717 : : /* Build a call to the instrumentation function FNCODE and add it to SEQ.
21718 : : If COND_VAR is not NULL, it is a boolean variable guarding the call to
21719 : : the instrumentation function. IF STMT is not NULL, it is a statement
21720 : : to be executed just before the call to the instrumentation function. */
21721 : :
21722 : : static void
21723 : 74 : build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
21724 : : tree cond_var, gimple *stmt)
21725 : : {
21726 : : /* The instrumentation hooks aren't going to call the instrumented
21727 : : function and the address they receive is expected to be matchable
21728 : : against symbol addresses. Make sure we don't create a trampoline,
21729 : : in case the current function is nested. */
21730 : 74 : tree this_fn_addr = build_fold_addr_expr (current_function_decl);
21731 : 74 : TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
21732 : :
21733 : 74 : tree label_true, label_false;
21734 : 74 : if (cond_var)
21735 : : {
21736 : 16 : label_true = create_artificial_label (UNKNOWN_LOCATION);
21737 : 16 : label_false = create_artificial_label (UNKNOWN_LOCATION);
21738 : 16 : gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
21739 : : label_true, label_false);
21740 : 16 : gimplify_seq_add_stmt (seq, cond);
21741 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
21742 : 16 : gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
21743 : : NOT_TAKEN));
21744 : : }
21745 : :
21746 : 74 : if (stmt)
21747 : 8 : gimplify_seq_add_stmt (seq, stmt);
21748 : :
21749 : 74 : tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
21750 : 74 : gcall *call = gimple_build_call (x, 1, integer_zero_node);
21751 : 74 : tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
21752 : 74 : gimple_call_set_lhs (call, tmp_var);
21753 : 74 : gimplify_seq_add_stmt (seq, call);
21754 : 74 : x = builtin_decl_implicit (fncode);
21755 : 74 : call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
21756 : 74 : gimplify_seq_add_stmt (seq, call);
21757 : :
21758 : 74 : if (cond_var)
21759 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
21760 : 74 : }
21761 : :
21762 : : /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
21763 : : node for the function we want to gimplify.
21764 : :
21765 : : Return the sequence of GIMPLE statements corresponding to the body
21766 : : of FNDECL. */
21767 : :
21768 : : void
21769 : 2923403 : gimplify_function_tree (tree fndecl)
21770 : : {
21771 : 2923403 : gimple_seq seq;
21772 : 2923403 : gbind *bind;
21773 : :
21774 : 2923403 : gcc_assert (!gimple_body (fndecl));
21775 : :
21776 : 2923403 : if (DECL_STRUCT_FUNCTION (fndecl))
21777 : 2919892 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
21778 : : else
21779 : 3511 : push_struct_function (fndecl);
21780 : :
21781 : 2923403 : reset_cond_uid ();
21782 : 2923403 : if (cond_uids)
21783 : : {
21784 : 65 : delete cond_uids;
21785 : 65 : cond_uids = NULL;
21786 : : }
21787 : :
21788 : : /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
21789 : : if necessary. */
21790 : 2923403 : cfun->curr_properties |= PROP_gimple_lva;
21791 : :
21792 : 2923403 : if (asan_sanitize_use_after_scope ())
21793 : 8234 : asan_poisoned_variables = new hash_set<tree> ();
21794 : 2923403 : if (flag_openmp)
21795 : 45947 : omp_resolved_variant_calls = new hash_set<tree> ();
21796 : :
21797 : 2923403 : bind = gimplify_body (fndecl, true);
21798 : :
21799 : 2923403 : if (omp_resolved_variant_calls)
21800 : : {
21801 : 45947 : delete omp_resolved_variant_calls;
21802 : 45947 : omp_resolved_variant_calls = NULL;
21803 : : }
21804 : 2923403 : if (asan_poisoned_variables)
21805 : : {
21806 : 8234 : delete asan_poisoned_variables;
21807 : 8234 : asan_poisoned_variables = NULL;
21808 : : }
21809 : :
21810 : : /* The tree body of the function is no longer needed, replace it
21811 : : with the new GIMPLE body. */
21812 : 2923403 : seq = NULL;
21813 : 2923403 : gimple_seq_add_stmt (&seq, bind);
21814 : 2923403 : gimple_set_body (fndecl, seq);
21815 : :
21816 : : /* If we're instrumenting function entry/exit, then prepend the call to
21817 : : the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
21818 : : catch the exit hook. */
21819 : : /* ??? Add some way to ignore exceptions for this TFE. */
21820 : 2923403 : if (flag_instrument_function_entry_exit
21821 : 91 : && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
21822 : : /* Do not instrument extern inline functions. */
21823 : 40 : && !(DECL_DECLARED_INLINE_P (fndecl)
21824 : 5 : && DECL_EXTERNAL (fndecl)
21825 : 1 : && DECL_DISREGARD_INLINE_LIMITS (fndecl))
21826 : 2923442 : && !flag_instrument_functions_exclude_p (fndecl))
21827 : : {
21828 : 37 : gimple_seq body = NULL, cleanup = NULL;
21829 : 37 : gassign *assign;
21830 : 37 : tree cond_var;
21831 : :
21832 : : /* If -finstrument-functions-once is specified, generate:
21833 : :
21834 : : static volatile bool C.0 = false;
21835 : : bool tmp_called;
21836 : :
21837 : : tmp_called = C.0;
21838 : : if (!tmp_called)
21839 : : {
21840 : : C.0 = true;
21841 : : [call profiling enter function]
21842 : : }
21843 : :
21844 : : without specific protection for data races. */
21845 : 37 : if (flag_instrument_function_entry_exit > 1)
21846 : : {
21847 : 8 : tree first_var
21848 : 8 : = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
21849 : : VAR_DECL,
21850 : : create_tmp_var_name ("C"),
21851 : : boolean_type_node);
21852 : 8 : DECL_ARTIFICIAL (first_var) = 1;
21853 : 8 : DECL_IGNORED_P (first_var) = 1;
21854 : 8 : TREE_STATIC (first_var) = 1;
21855 : 8 : TREE_THIS_VOLATILE (first_var) = 1;
21856 : 8 : TREE_USED (first_var) = 1;
21857 : 8 : DECL_INITIAL (first_var) = boolean_false_node;
21858 : 8 : varpool_node::add (first_var);
21859 : :
21860 : 8 : cond_var = create_tmp_var (boolean_type_node, "tmp_called");
21861 : 8 : assign = gimple_build_assign (cond_var, first_var);
21862 : 8 : gimplify_seq_add_stmt (&body, assign);
21863 : :
21864 : 8 : assign = gimple_build_assign (first_var, boolean_true_node);
21865 : : }
21866 : :
21867 : : else
21868 : : {
21869 : : cond_var = NULL_TREE;
21870 : : assign = NULL;
21871 : : }
21872 : :
21873 : 37 : build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
21874 : : cond_var, assign);
21875 : :
21876 : : /* If -finstrument-functions-once is specified, generate:
21877 : :
21878 : : if (!tmp_called)
21879 : : [call profiling exit function]
21880 : :
21881 : : without specific protection for data races. */
21882 : 37 : build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
21883 : : cond_var, NULL);
21884 : :
21885 : 37 : gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
21886 : 37 : gimplify_seq_add_stmt (&body, tf);
21887 : 37 : gbind *new_bind = gimple_build_bind (NULL, body, NULL);
21888 : :
21889 : : /* Replace the current function body with the body
21890 : : wrapped in the try/finally TF. */
21891 : 37 : seq = NULL;
21892 : 37 : gimple_seq_add_stmt (&seq, new_bind);
21893 : 37 : gimple_set_body (fndecl, seq);
21894 : 37 : bind = new_bind;
21895 : : }
21896 : :
21897 : 2923403 : if (sanitize_flags_p (SANITIZE_THREAD)
21898 : 2923403 : && param_tsan_instrument_func_entry_exit)
21899 : : {
21900 : 1058 : gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
21901 : 1058 : gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
21902 : 1058 : gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
21903 : : /* Replace the current function body with the body
21904 : : wrapped in the try/finally TF. */
21905 : 1058 : seq = NULL;
21906 : 1058 : gimple_seq_add_stmt (&seq, new_bind);
21907 : 1058 : gimple_set_body (fndecl, seq);
21908 : : }
21909 : :
21910 : 2923403 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21911 : 2923403 : cfun->curr_properties |= PROP_gimple_any;
21912 : :
21913 : 2923403 : pop_cfun ();
21914 : :
21915 : 2923403 : dump_function (TDI_gimple, fndecl);
21916 : 2923403 : }
21917 : :
21918 : : /* Return a dummy expression of type TYPE in order to keep going after an
21919 : : error. */
21920 : :
21921 : : static tree
21922 : 30 : dummy_object (tree type)
21923 : : {
21924 : 30 : tree t = build_int_cst (build_pointer_type (type), 0);
21925 : 30 : return build2 (MEM_REF, type, t, t);
21926 : : }
21927 : :
21928 : : /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
21929 : : builtin function, but a very special sort of operator. */
21930 : :
21931 : : enum gimplify_status
21932 : 50798 : gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
21933 : : gimple_seq *post_p ATTRIBUTE_UNUSED)
21934 : : {
21935 : 50798 : tree promoted_type, have_va_type;
21936 : 50798 : tree valist = TREE_OPERAND (*expr_p, 0);
21937 : 50798 : tree type = TREE_TYPE (*expr_p);
21938 : 50798 : tree t, tag, aptag;
21939 : 50798 : location_t loc = EXPR_LOCATION (*expr_p);
21940 : :
21941 : : /* Verify that valist is of the proper type. */
21942 : 50798 : have_va_type = TREE_TYPE (valist);
21943 : 50798 : if (have_va_type == error_mark_node)
21944 : : return GS_ERROR;
21945 : 50779 : have_va_type = targetm.canonical_va_list_type (have_va_type);
21946 : 50779 : if (have_va_type == NULL_TREE
21947 : 50779 : && POINTER_TYPE_P (TREE_TYPE (valist)))
21948 : : /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg. */
21949 : 258 : have_va_type
21950 : 258 : = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
21951 : 50779 : gcc_assert (have_va_type != NULL_TREE);
21952 : :
21953 : : /* Generate a diagnostic for requesting data of a type that cannot
21954 : : be passed through `...' due to type promotion at the call site. */
21955 : 50779 : if ((promoted_type = lang_hooks.types.type_promotes_to (type))
21956 : : != type)
21957 : : {
21958 : 30 : static bool gave_help;
21959 : 30 : bool warned;
21960 : : /* Use the expansion point to handle cases such as passing bool (defined
21961 : : in a system header) through `...'. */
21962 : 30 : location_t xloc
21963 : 30 : = expansion_point_location_if_in_system_header (loc);
21964 : :
21965 : : /* Unfortunately, this is merely undefined, rather than a constraint
21966 : : violation, so we cannot make this an error. If this call is never
21967 : : executed, the program is still strictly conforming. */
21968 : 30 : auto_diagnostic_group d;
21969 : 30 : warned = warning_at (xloc, 0,
21970 : : "%qT is promoted to %qT when passed through %<...%>",
21971 : : type, promoted_type);
21972 : 30 : if (!gave_help && warned)
21973 : : {
21974 : 15 : gave_help = true;
21975 : 15 : inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
21976 : : promoted_type, type);
21977 : : }
21978 : :
21979 : : /* We can, however, treat "undefined" any way we please.
21980 : : Call abort to encourage the user to fix the program. */
21981 : 23 : if (warned)
21982 : 23 : inform (xloc, "if this code is reached, the program will abort");
21983 : : /* Before the abort, allow the evaluation of the va_list
21984 : : expression to exit or longjmp. */
21985 : 30 : gimplify_and_add (valist, pre_p);
21986 : 60 : t = build_call_expr_loc (loc,
21987 : : builtin_decl_implicit (BUILT_IN_TRAP), 0);
21988 : 30 : gimplify_and_add (t, pre_p);
21989 : :
21990 : : /* This is dead code, but go ahead and finish so that the
21991 : : mode of the result comes out right. */
21992 : 30 : *expr_p = dummy_object (type);
21993 : 30 : return GS_ALL_DONE;
21994 : 30 : }
21995 : :
21996 : 50749 : tag = build_int_cst (build_pointer_type (type), 0);
21997 : 50749 : aptag = build_int_cst (TREE_TYPE (valist), 0);
21998 : :
21999 : 50749 : *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
22000 : : valist, tag, aptag);
22001 : :
22002 : : /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
22003 : : needs to be expanded. */
22004 : 50749 : cfun->curr_properties &= ~PROP_gimple_lva;
22005 : :
22006 : 50749 : return GS_OK;
22007 : : }
22008 : :
22009 : : /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
22010 : :
22011 : : DST/SRC are the destination and source respectively. You can pass
22012 : : ungimplified trees in DST or SRC, in which case they will be
22013 : : converted to a gimple operand if necessary.
22014 : :
22015 : : This function returns the newly created GIMPLE_ASSIGN tuple. */
22016 : :
22017 : : gimple *
22018 : 736516 : gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
22019 : : {
22020 : 736516 : tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
22021 : 736516 : gimplify_and_add (t, seq_p);
22022 : 736516 : ggc_free (t);
22023 : 736516 : return gimple_seq_last_stmt (*seq_p);
22024 : : }
22025 : :
22026 : : inline hashval_t
22027 : 1560023 : gimplify_hasher::hash (const elt_t *p)
22028 : : {
22029 : 1560023 : tree t = p->val;
22030 : 1560023 : return iterative_hash_expr (t, 0);
22031 : : }
22032 : :
22033 : : inline bool
22034 : 581740 : gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
22035 : : {
22036 : 581740 : tree t1 = p1->val;
22037 : 581740 : tree t2 = p2->val;
22038 : 581740 : enum tree_code code = TREE_CODE (t1);
22039 : :
22040 : 581740 : if (TREE_CODE (t2) != code
22041 : 581740 : || TREE_TYPE (t1) != TREE_TYPE (t2))
22042 : : return false;
22043 : :
22044 : 355375 : if (!operand_equal_p (t1, t2, 0))
22045 : : return false;
22046 : :
22047 : : return true;
22048 : : }
|