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 : 5770680 : next_cond_uid ()
93 : : {
94 : 5770680 : 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 : 2980432 : reset_cond_uid ()
103 : : {
104 : 2980432 : 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 : 1225328 : tree_associate_condition_with_expr (tree stmt, unsigned uid)
114 : : {
115 : 1225328 : 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 : 92738242 : gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
316 : : {
317 : 92738242 : gimple_seq_add_stmt_without_update (seq_p, gs);
318 : 6675302 : }
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 : 9593811 : gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
328 : : {
329 : 9593811 : gimple_stmt_iterator si;
330 : :
331 : 9593811 : if (src == NULL)
332 : 4022617 : return;
333 : :
334 : 5571194 : si = gsi_last (*dst_p);
335 : 5571194 : 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 : 9092247 : ctx_alloc (void)
348 : : {
349 : 9092247 : struct gimplify_ctx * c = ctx_pool;
350 : :
351 : 9092247 : if (c)
352 : 8830213 : ctx_pool = c->prev_context;
353 : : else
354 : 262034 : c = XNEW (struct gimplify_ctx);
355 : :
356 : 9092247 : memset (c, '\0', sizeof (*c));
357 : 9092247 : return c;
358 : : }
359 : :
360 : : /* Put gimplify context C back into the pool. */
361 : :
362 : : static inline void
363 : 9092243 : ctx_free (struct gimplify_ctx *c)
364 : : {
365 : 9092243 : c->prev_context = ctx_pool;
366 : 9092243 : ctx_pool = c;
367 : : }
368 : :
369 : : /* Free allocated ctx stack memory. */
370 : :
371 : : void
372 : 230430 : free_gimplify_stack (void)
373 : : {
374 : 230430 : struct gimplify_ctx *c;
375 : :
376 : 462814 : while ((c = ctx_pool))
377 : : {
378 : 232384 : ctx_pool = c->prev_context;
379 : 232384 : free (c);
380 : : }
381 : 230430 : }
382 : :
383 : :
384 : : /* Set up a context for the gimplifier. */
385 : :
386 : : void
387 : 9092247 : push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
388 : : {
389 : 9092247 : struct gimplify_ctx *c = ctx_alloc ();
390 : :
391 : 9092247 : c->prev_context = gimplify_ctxp;
392 : 9092247 : gimplify_ctxp = c;
393 : 9092247 : gimplify_ctxp->into_ssa = in_ssa;
394 : 9092247 : gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
395 : 9092247 : }
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 : 9092243 : pop_gimplify_context (gimple *body)
405 : : {
406 : 9092243 : struct gimplify_ctx *c = gimplify_ctxp;
407 : :
408 : 9092243 : gcc_assert (c
409 : : && (!c->bind_expr_stack.exists ()
410 : : || c->bind_expr_stack.is_empty ()));
411 : 9092243 : c->bind_expr_stack.release ();
412 : 9092243 : gimplify_ctxp = c->prev_context;
413 : :
414 : 9092243 : if (body)
415 : 3104196 : declare_vars (c->temps, body, false);
416 : : else
417 : 5988047 : record_vars (c->temps);
418 : :
419 : 9092243 : delete c->temp_htab;
420 : 9092243 : c->temp_htab = NULL;
421 : 9092243 : ctx_free (c);
422 : 9092243 : }
423 : :
424 : : /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
425 : :
426 : : static void
427 : 5998718 : gimple_push_bind_expr (gbind *bind_stmt)
428 : : {
429 : 5998718 : gimplify_ctxp->bind_expr_stack.reserve (8);
430 : 5998718 : gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
431 : 5998718 : }
432 : :
433 : : /* Pop the first element off the stack of bindings. */
434 : :
435 : : static void
436 : 5998718 : gimple_pop_bind_expr (void)
437 : : {
438 : 0 : gimplify_ctxp->bind_expr_stack.pop ();
439 : 1256 : }
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 : 337 : gimple_bind_expr_stack (void)
453 : : {
454 : 337 : 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 : 2897481 : gimple_conditional_context (void)
462 : : {
463 : 2897481 : return gimplify_ctxp->conditions > 0;
464 : : }
465 : :
466 : : /* Note that we've entered a COND_EXPR. */
467 : :
468 : : static void
469 : 5771115 : gimple_push_condition (void)
470 : : {
471 : : #ifdef ENABLE_GIMPLE_CHECKING
472 : 5771115 : if (gimplify_ctxp->conditions == 0)
473 : 3796151 : gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
474 : : #endif
475 : 5771115 : ++(gimplify_ctxp->conditions);
476 : 5771115 : }
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 : 5771115 : gimple_pop_condition (gimple_seq *pre_p)
483 : : {
484 : 5771115 : int conds = --(gimplify_ctxp->conditions);
485 : :
486 : 5771115 : gcc_assert (conds >= 0);
487 : 5771115 : if (conds == 0)
488 : : {
489 : 3796151 : gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
490 : 3796151 : gimplify_ctxp->conditional_cleanups = NULL;
491 : : }
492 : 5771115 : }
493 : :
494 : : /* A stable comparison routine for use with splay trees and DECLs. */
495 : :
496 : : static int
497 : 17904974 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
498 : : {
499 : 17904974 : tree a = (tree) xa;
500 : 17904974 : tree b = (tree) xb;
501 : :
502 : 17904974 : 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 : 139287 : new_omp_context (enum omp_region_type region_type)
509 : : {
510 : 139287 : struct gimplify_omp_ctx *c;
511 : :
512 : 139287 : c = XCNEW (struct gimplify_omp_ctx);
513 : 139287 : c->outer_context = gimplify_omp_ctxp;
514 : 139287 : c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
515 : 139287 : c->implicit_mappers = new hash_map<omp_name_type<tree>, tree>;
516 : 139287 : c->privatized_types = new hash_set<tree>;
517 : 139287 : c->location = input_location;
518 : 139287 : c->region_type = region_type;
519 : 139287 : if ((region_type & ORT_TASK) == 0)
520 : 133084 : c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
521 : : else
522 : 6203 : c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
523 : 139287 : c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
524 : 139287 : c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
525 : 139287 : c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
526 : 139287 : c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
527 : 139287 : c->defaultmap[GDMK_POINTER] = GOVD_MAP;
528 : :
529 : 139287 : return c;
530 : : }
531 : :
532 : : /* Destroy an omp construct that deals with variable remapping. */
533 : :
534 : : static void
535 : 138728 : delete_omp_context (struct gimplify_omp_ctx *c)
536 : : {
537 : 138728 : splay_tree_delete (c->variables);
538 : 277456 : delete c->privatized_types;
539 : 277456 : delete c->implicit_mappers;
540 : 138728 : c->loop_iter_var.release ();
541 : 138728 : XDELETE (c);
542 : 138728 : }
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 : 38043921 : gimplify_and_add (tree t, gimple_seq *seq_p)
553 : : {
554 : 38043921 : gimplify_stmt (&t, seq_p);
555 : 38043921 : }
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 : 104663 : gimplify_and_return_first (tree t, gimple_seq *seq_p)
563 : : {
564 : 104663 : gimple_stmt_iterator last = gsi_last (*seq_p);
565 : :
566 : 104663 : gimplify_and_add (t, seq_p);
567 : :
568 : 104663 : if (!gsi_end_p (last))
569 : : {
570 : 4871 : gsi_next (&last);
571 : 4871 : return gsi_stmt (last);
572 : : }
573 : : else
574 : 99792 : 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 : 97432227 : is_gimple_reg_rhs_or_call (tree t)
598 : : {
599 : 71529454 : return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
600 : 97432227 : || 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 : 14328147 : 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 : 14328147 : if (is_gimple_reg_type (TREE_TYPE (t)))
613 : 10964273 : return is_gimple_val (t);
614 : : else
615 : 3363874 : return (is_gimple_val (t)
616 : 1644980 : || is_gimple_lvalue (t)
617 : 1113893 : || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
618 : 4477287 : || 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 : 2130010 : create_tmp_from_val (tree val)
626 : : {
627 : : /* Drop all qualifiers and address-space information from the value type. */
628 : 2130010 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
629 : 2130010 : tree var = create_tmp_var (type, get_name (val));
630 : 2130010 : 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 : 2359246 : lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
638 : : {
639 : 2359246 : tree ret;
640 : :
641 : : /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P. */
642 : 2359246 : 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 : 2359246 : if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
650 : : {
651 : 1086763 : ret = create_tmp_from_val (val);
652 : 1086763 : DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
653 : : }
654 : : else
655 : : {
656 : 1272483 : elt_t elt, *elt_p;
657 : 1272483 : elt_t **slot;
658 : :
659 : 1272483 : elt.val = val;
660 : 1272483 : if (!gimplify_ctxp->temp_htab)
661 : 391237 : gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
662 : 1272483 : slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
663 : 1272483 : if (*slot == NULL)
664 : : {
665 : 1043247 : elt_p = XNEW (elt_t);
666 : 1043247 : elt_p->val = val;
667 : 1043247 : elt_p->temp = ret = create_tmp_from_val (val);
668 : 1043247 : *slot = elt_p;
669 : : }
670 : : else
671 : : {
672 : 229236 : elt_p = *slot;
673 : 229236 : ret = elt_p->temp;
674 : : }
675 : : }
676 : :
677 : 2359246 : return ret;
678 : : }
679 : :
680 : : /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
681 : :
682 : : static tree
683 : 26438460 : 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 : 26438460 : 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 : 26438460 : gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
691 : : fb_rvalue);
692 : :
693 : 26438460 : if (allow_ssa
694 : 26176954 : && gimplify_ctxp->into_ssa
695 : 51196873 : && is_gimple_reg_type (TREE_TYPE (val)))
696 : : {
697 : 24079215 : t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
698 : 24079215 : if (! gimple_in_ssa_p (cfun))
699 : : {
700 : 20728175 : const char *name = get_name (val);
701 : 20728175 : if (name)
702 : 6800730 : SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
703 : : }
704 : : }
705 : : else
706 : 2359245 : t = lookup_tmp_var (val, is_formal, not_gimple_reg);
707 : :
708 : 26438460 : mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
709 : :
710 : 26438460 : SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
711 : :
712 : : /* gimplify_modify_expr might want to reduce this further. */
713 : 26438460 : gimplify_and_add (mod, pre_p);
714 : 26438460 : 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 : 26438460 : 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 : 25941358 : get_formal_tmp_var (tree val, gimple_seq *pre_p)
738 : : {
739 : 25941358 : 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 : 485091 : get_initialized_tmp_var (tree val, gimple_seq *pre_p,
747 : : gimple_seq *post_p /* = NULL */,
748 : : bool allow_ssa /* = true */)
749 : : {
750 : 485091 : 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 : 3111951 : declare_vars (tree vars, gimple *gs, bool debug_info)
758 : : {
759 : 3111951 : tree last = vars;
760 : 3111951 : if (last)
761 : : {
762 : 1936480 : tree temps, block;
763 : :
764 : 1936480 : gbind *scope = as_a <gbind *> (gs);
765 : :
766 : 1936480 : temps = nreverse (last);
767 : :
768 : 1936480 : block = gimple_bind_block (scope);
769 : 1936480 : gcc_assert (!block || TREE_CODE (block) == BLOCK);
770 : 1936480 : if (!block || !debug_info)
771 : : {
772 : 1932891 : DECL_CHAIN (last) = gimple_bind_vars (scope);
773 : 1932891 : 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 : 3589 : if (BLOCK_VARS (block))
782 : 3396 : 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 : 3111951 : }
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 : 33024 : gimple_add_tmp_var_fn (struct function *fn, tree tmp)
821 : : {
822 : 33024 : 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 : 33024 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
828 : 0 : force_constant_size (tmp);
829 : :
830 : 33024 : DECL_CONTEXT (tmp) = fn->decl;
831 : 33024 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
832 : :
833 : 33024 : record_vars_into (tmp, fn->decl);
834 : 33024 : }
835 : :
836 : : /* Push the temporary variable TMP into the current binding. */
837 : :
838 : : void
839 : 16388034 : gimple_add_tmp_var (tree tmp)
840 : : {
841 : 16388034 : 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 : 16388034 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
847 : 0 : force_constant_size (tmp);
848 : :
849 : 16388034 : DECL_CONTEXT (tmp) = current_function_decl;
850 : 16388034 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
851 : :
852 : 16388034 : if (gimplify_ctxp)
853 : : {
854 : 5652933 : DECL_CHAIN (tmp) = gimplify_ctxp->temps;
855 : 5652933 : gimplify_ctxp->temps = tmp;
856 : :
857 : : /* Mark temporaries local within the nearest enclosing parallel. */
858 : 5652933 : if (gimplify_omp_ctxp)
859 : : {
860 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
861 : 546184 : int flag = GOVD_LOCAL | GOVD_SEEN;
862 : : while (ctx
863 : 546184 : && (ctx->region_type == ORT_WORKSHARE
864 : : || ctx->region_type == ORT_TASKGROUP
865 : 398704 : || ctx->region_type == ORT_SIMD
866 : 348548 : || 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 : 341533 : if (ctx)
883 : 314544 : omp_add_variable (ctx, tmp, flag);
884 : : }
885 : : }
886 : 10735101 : else if (cfun)
887 : 10735101 : 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 : 16388034 : }
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 : 2256628935 : mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
945 : : {
946 : 2256628935 : tree t = *tp;
947 : 2256628935 : 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 : 2256628935 : if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
952 : : {
953 : 8609441 : if (data && !((hash_set<tree> *)data)->add (t))
954 : : ;
955 : : else
956 : 8609441 : *walk_subtrees = 0;
957 : : }
958 : :
959 : : /* Stop at types, decls, constants like copy_tree_r. */
960 : 2248019494 : else if (TREE_CODE_CLASS (code) == tcc_type
961 : : || TREE_CODE_CLASS (code) == tcc_declaration
962 : 2248019494 : || TREE_CODE_CLASS (code) == tcc_constant)
963 : 1362468715 : *walk_subtrees = 0;
964 : :
965 : : /* Cope with the statement expression extension. */
966 : 885550779 : else if (code == STATEMENT_LIST)
967 : : ;
968 : :
969 : : /* Leave the bulk of the work to copy_tree_r itself. */
970 : : else
971 : 885501145 : copy_tree_r (tp, walk_subtrees, NULL);
972 : :
973 : 2256628935 : 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 : 282804472 : copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
982 : : {
983 : 282804472 : tree t = *tp;
984 : 282804472 : 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 : 282804472 : if (TREE_CODE_CLASS (code) == tcc_type
991 : : || TREE_CODE_CLASS (code) == tcc_declaration
992 : 282804472 : || TREE_CODE_CLASS (code) == tcc_constant)
993 : : {
994 : 133645883 : if (TREE_VISITED (t))
995 : 81791827 : *walk_subtrees = 0;
996 : : else
997 : 51854056 : TREE_VISITED (t) = 1;
998 : : }
999 : :
1000 : : /* If this node has been visited already, unshare it and don't look
1001 : : any deeper. */
1002 : 149158589 : else if (TREE_VISITED (t))
1003 : : {
1004 : 1670007 : walk_tree (tp, mostly_copy_tree_r, data, NULL);
1005 : 1670007 : *walk_subtrees = 0;
1006 : : }
1007 : :
1008 : : /* Otherwise, mark the node as visited and keep looking. */
1009 : : else
1010 : 147488582 : TREE_VISITED (t) = 1;
1011 : :
1012 : 282804472 : 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 : 9017667 : copy_if_shared (tree *tp, void *data)
1020 : : {
1021 : 9017667 : walk_tree (tp, copy_if_shared_r, data, NULL);
1022 : 9017667 : }
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 : 3005889 : unshare_body (tree fndecl)
1029 : : {
1030 : 3005889 : 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 : 3005889 : hash_set<tree> *visited
1034 : 3005889 : = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
1035 : :
1036 : 3005889 : copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
1037 : 3005889 : copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
1038 : 3005889 : copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1039 : :
1040 : 3009185 : delete visited;
1041 : :
1042 : 3005889 : if (cgn)
1043 : 6054974 : for (cgn = first_nested_function (cgn); cgn;
1044 : 24939 : cgn = next_nested_function (cgn))
1045 : 24939 : unshare_body (cgn->decl);
1046 : 3005889 : }
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 : 282804046 : unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1053 : : {
1054 : 282804046 : tree t = *tp;
1055 : :
1056 : : /* If this node has been visited, unmark it and keep looking. */
1057 : 282804046 : if (TREE_VISITED (t))
1058 : 199346992 : TREE_VISITED (t) = 0;
1059 : :
1060 : : /* Otherwise, don't look any deeper. */
1061 : : else
1062 : 83457054 : *walk_subtrees = 0;
1063 : :
1064 : 282804046 : return NULL_TREE;
1065 : : }
1066 : :
1067 : : /* Unmark the visited trees rooted at *TP. */
1068 : :
1069 : : static inline void
1070 : 9017667 : unmark_visited (tree *tp)
1071 : : {
1072 : 9017667 : walk_tree (tp, unmark_visited_r, NULL, NULL);
1073 : 9017667 : }
1074 : :
1075 : : /* Likewise, but mark all trees as not visited. */
1076 : :
1077 : : static void
1078 : 3005889 : unvisit_body (tree fndecl)
1079 : : {
1080 : 3005889 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
1081 : :
1082 : 3005889 : unmark_visited (&DECL_SAVED_TREE (fndecl));
1083 : 3005889 : unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1084 : 3005889 : unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1085 : :
1086 : 3005889 : if (cgn)
1087 : 3027487 : for (cgn = first_nested_function (cgn);
1088 : 3027487 : cgn; cgn = next_nested_function (cgn))
1089 : 24939 : unvisit_body (cgn->decl);
1090 : 3005889 : }
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 : 1231273410 : unshare_expr (tree expr)
1098 : : {
1099 : 1231273410 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1100 : 1231273410 : return expr;
1101 : : }
1102 : :
1103 : : /* Worker for unshare_expr_without_location. */
1104 : :
1105 : : static tree
1106 : 8473212 : prune_expr_location (tree *tp, int *walk_subtrees, void *)
1107 : : {
1108 : 8473212 : if (EXPR_P (*tp))
1109 : 3971824 : SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1110 : : else
1111 : 4501388 : *walk_subtrees = 0;
1112 : 8473212 : return NULL_TREE;
1113 : : }
1114 : :
1115 : : /* Similar to unshare_expr but also prune all expression locations
1116 : : from EXPR. */
1117 : :
1118 : : tree
1119 : 21142693 : unshare_expr_without_location (tree expr)
1120 : : {
1121 : 21142693 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1122 : 21142693 : if (EXPR_P (expr))
1123 : 2741650 : walk_tree (&expr, prune_expr_location, NULL, NULL);
1124 : 21142693 : 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 : 1186634 : rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1134 : : {
1135 : 1186634 : if (!expr)
1136 : : return or_else;
1137 : :
1138 : 1186634 : if (EXPR_HAS_LOCATION (expr))
1139 : 831076 : return EXPR_LOCATION (expr);
1140 : :
1141 : 355558 : 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 : 355558 : 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 : 514824 : rexpr_has_location (tree expr)
1164 : : {
1165 : 306074 : 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 : 20927424 : voidify_wrapper_expr (tree wrapper, tree temp)
1176 : : {
1177 : 20927424 : tree type = TREE_TYPE (wrapper);
1178 : 20927424 : 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 : 776018 : for (p = &wrapper; p && *p; )
1185 : : {
1186 : 776018 : 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 : 360995 : case CLEANUP_POINT_EXPR:
1196 : 360995 : case TRY_FINALLY_EXPR:
1197 : 360995 : case TRY_CATCH_EXPR:
1198 : 360995 : TREE_SIDE_EFFECTS (*p) = 1;
1199 : 360995 : TREE_TYPE (*p) = void_type_node;
1200 : 360995 : p = &TREE_OPERAND (*p, 0);
1201 : 360995 : break;
1202 : :
1203 : 19016 : case STATEMENT_LIST:
1204 : 19016 : {
1205 : 19016 : tree_stmt_iterator i = tsi_last (*p);
1206 : 19016 : TREE_SIDE_EFFECTS (*p) = 1;
1207 : 19016 : TREE_TYPE (*p) = void_type_node;
1208 : 19016 : p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1209 : : }
1210 : 19016 : break;
1211 : :
1212 : : case COMPOUND_EXPR:
1213 : : /* Advance to the last statement. Set all container types to
1214 : : void. */
1215 : 49334 : for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1216 : : {
1217 : 24670 : TREE_SIDE_EFFECTS (*p) = 1;
1218 : 24670 : 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 : 368190 : 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 : 368190 : 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 : 368157 : goto out;
1239 : : }
1240 : : }
1241 : :
1242 : 0 : out:
1243 : 368157 : if (p == NULL || IS_EMPTY_STMT (*p))
1244 : : temp = NULL_TREE;
1245 : 368157 : 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 : 366476 : temp = create_tmp_var (type, "retval");
1257 : 366476 : *p = build2 (INIT_EXPR, type, temp, *p);
1258 : : }
1259 : :
1260 : 368157 : 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 : 7793 : build_stack_save_restore (gcall **save, gcall **restore)
1271 : : {
1272 : 7793 : tree tmp_var;
1273 : :
1274 : 15586 : *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1275 : 7793 : tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1276 : 7793 : gimple_call_set_lhs (*save, tmp_var);
1277 : :
1278 : 7793 : *restore
1279 : 7793 : = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1280 : : 1, tmp_var);
1281 : 7793 : }
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 : 4731 : asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1309 : : bool before)
1310 : : {
1311 : 4731 : tree unit_size = DECL_SIZE_UNIT (decl);
1312 : 4731 : tree base = build_fold_addr_expr (decl);
1313 : :
1314 : : /* Do not poison variables that have size equal to zero. */
1315 : 4731 : if (zerop (unit_size))
1316 : 4731 : return;
1317 : :
1318 : : /* It's necessary to have all stack variables aligned to ASAN granularity
1319 : : bytes. */
1320 : 4717 : gcc_assert (!hwasan_sanitize_p () || hwasan_sanitize_stack_p ());
1321 : 4717 : unsigned shadow_granularity
1322 : 4717 : = hwasan_sanitize_p () ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY;
1323 : 4717 : if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
1324 : 4515 : SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
1325 : :
1326 : 4717 : HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1327 : :
1328 : 4717 : gimple *g
1329 : 4717 : = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1330 : 4717 : build_int_cst (integer_type_node, flags),
1331 : : base, unit_size);
1332 : :
1333 : 4717 : if (before)
1334 : 2472 : gsi_insert_before (it, g, GSI_NEW_STMT);
1335 : : else
1336 : 2245 : 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 : 4303 : asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1345 : : {
1346 : 4303 : gimple_stmt_iterator it = gsi_last (*seq_p);
1347 : 4303 : bool before = false;
1348 : :
1349 : 4303 : if (gsi_end_p (it))
1350 : 2264 : before = true;
1351 : :
1352 : 4303 : asan_poison_variable (decl, poison, &it, before);
1353 : 4303 : }
1354 : :
1355 : : /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1356 : :
1357 : : static int
1358 : 134 : sort_by_decl_uid (const void *a, const void *b)
1359 : : {
1360 : 134 : const tree *t1 = (const tree *)a;
1361 : 134 : const tree *t2 = (const tree *)b;
1362 : :
1363 : 134 : int uid1 = DECL_UID (*t1);
1364 : 134 : int uid2 = DECL_UID (*t2);
1365 : :
1366 : 134 : if (uid1 < uid2)
1367 : : return -1;
1368 : 51 : 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 : 1063521 : asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1380 : : {
1381 : 1063521 : unsigned c = variables->elements ();
1382 : 1063521 : if (c == 0)
1383 : 1063345 : 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 : 5997462 : gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1414 : : {
1415 : 5997462 : tree bind_expr = *expr_p;
1416 : 5997462 : bool old_keep_stack = gimplify_ctxp->keep_stack;
1417 : 5997462 : bool old_save_stack = gimplify_ctxp->save_stack;
1418 : 5997462 : tree t;
1419 : 5997462 : gbind *bind_stmt;
1420 : 5997462 : gimple_seq body, cleanup;
1421 : 5997462 : gcall *stack_save;
1422 : 5997462 : location_t start_locus = 0, end_locus = 0;
1423 : 5997462 : tree ret_clauses = NULL;
1424 : :
1425 : 5997462 : tree temp = voidify_wrapper_expr (bind_expr, NULL);
1426 : :
1427 : : /* Mark variables seen in this bind expr. */
1428 : 12768080 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1429 : : {
1430 : 6770618 : if (VAR_P (t))
1431 : : {
1432 : 6104155 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1433 : 6104155 : tree attr;
1434 : :
1435 : 6104155 : if (flag_openmp
1436 : 233976 : && !is_global_var (t)
1437 : 226088 : && !TREE_STATIC (t)
1438 : 226088 : && DECL_CONTEXT (t) == current_function_decl
1439 : 226088 : && TREE_USED (t)
1440 : 6328705 : && (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 : 6104155 : if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1626 : : {
1627 : 95632 : if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1628 : 95632 : || splay_tree_lookup (ctx->variables,
1629 : : (splay_tree_key) t) == NULL)
1630 : : {
1631 : 95632 : int flag = GOVD_LOCAL;
1632 : 95632 : 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 : 95632 : 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 : 95632 : 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 : 6104155 : DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1672 : :
1673 : 6104155 : if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1674 : 1081 : cfun->has_local_explicit_reg_vars = true;
1675 : : }
1676 : : }
1677 : :
1678 : 11994924 : bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1679 : 5997462 : BIND_EXPR_BLOCK (bind_expr));
1680 : 5997462 : gimple_push_bind_expr (bind_stmt);
1681 : :
1682 : 5997462 : gimplify_ctxp->keep_stack = false;
1683 : 5997462 : gimplify_ctxp->save_stack = false;
1684 : :
1685 : : /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1686 : 5997462 : body = NULL;
1687 : 5997462 : gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1688 : 5997462 : 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 : 5997462 : if (BIND_EXPR_BLOCK (bind_expr))
1696 : : {
1697 : 5867120 : end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1698 : 5867120 : start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1699 : : }
1700 : 5867120 : if (start_locus == 0)
1701 : 5997462 : start_locus = EXPR_LOCATION (bind_expr);
1702 : :
1703 : 5997462 : cleanup = NULL;
1704 : 5997462 : stack_save = NULL;
1705 : :
1706 : : /* Add clobbers for all variables that go out of scope. */
1707 : 12768080 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1708 : : {
1709 : 6770618 : if (VAR_P (t)
1710 : 6104155 : && !is_global_var (t)
1711 : 12661138 : && DECL_CONTEXT (t) == current_function_decl)
1712 : : {
1713 : 5890520 : if (flag_openmp
1714 : 226081 : && DECL_HAS_VALUE_EXPR_P (t)
1715 : 1016 : && TREE_USED (t)
1716 : 5891520 : && 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 : 5890520 : if (!DECL_HARD_REGISTER (t)
1733 : 5889439 : && !TREE_THIS_VOLATILE (t)
1734 : 5850546 : && !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 : 5747720 : && !is_gimple_reg (t)
1739 : 7128213 : && flag_stack_reuse != SR_NONE)
1740 : : {
1741 : 1234614 : tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
1742 : 1234614 : gimple *clobber_stmt;
1743 : 1234614 : clobber_stmt = gimple_build_assign (t, clobber);
1744 : 1234614 : gimple_set_location (clobber_stmt, end_locus);
1745 : 1234614 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1746 : : }
1747 : :
1748 : 5890520 : 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 : 6770618 : if (asan_poisoned_variables != NULL
1777 : 6770618 : && asan_poisoned_variables->contains (t))
1778 : : {
1779 : 2053 : asan_poisoned_variables->remove (t);
1780 : 2053 : asan_poison_variable (t, true, &cleanup);
1781 : : }
1782 : :
1783 : 6770618 : if (gimplify_ctxp->live_switch_vars != NULL
1784 : 6770618 : && 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 : 5997462 : if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1791 : : {
1792 : 7793 : gcall *stack_restore;
1793 : :
1794 : : /* Save stack on entry and restore it on exit. Add a try_finally
1795 : : block to achieve this. */
1796 : 7793 : build_stack_save_restore (&stack_save, &stack_restore);
1797 : :
1798 : 7793 : gimple_set_location (stack_save, start_locus);
1799 : 7793 : gimple_set_location (stack_restore, end_locus);
1800 : :
1801 : 7793 : gimplify_seq_add_stmt (&cleanup, stack_restore);
1802 : : }
1803 : :
1804 : 5997462 : 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 : 5997462 : if (cleanup)
1815 : : {
1816 : 752785 : gtry *gs;
1817 : 752785 : gimple_seq new_body;
1818 : :
1819 : 752785 : new_body = NULL;
1820 : 752785 : gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1821 : : GIMPLE_TRY_FINALLY);
1822 : :
1823 : 752785 : if (stack_save)
1824 : 7793 : gimplify_seq_add_stmt (&new_body, stack_save);
1825 : 752785 : gimplify_seq_add_stmt (&new_body, gs);
1826 : 752785 : gimple_bind_set_body (bind_stmt, new_body);
1827 : : }
1828 : :
1829 : : /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1830 : 5997462 : if (!gimplify_ctxp->keep_stack)
1831 : 5981088 : gimplify_ctxp->keep_stack = old_keep_stack;
1832 : 5997462 : gimplify_ctxp->save_stack = old_save_stack;
1833 : :
1834 : 5997462 : gimple_pop_bind_expr ();
1835 : :
1836 : 5997462 : gimplify_seq_add_stmt (pre_p, bind_stmt);
1837 : :
1838 : 5997462 : if (temp)
1839 : : {
1840 : 1709 : *expr_p = temp;
1841 : 1709 : return GS_OK;
1842 : : }
1843 : :
1844 : 5995753 : *expr_p = NULL_TREE;
1845 : 5995753 : return GS_ALL_DONE;
1846 : : }
1847 : :
1848 : : /* Maybe add early return predict statement to PRE_P sequence. */
1849 : :
1850 : : static void
1851 : 2271491 : maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1852 : : {
1853 : : /* If we are not in a conditional context, add PREDICT statement. */
1854 : 2271491 : if (gimple_conditional_context ())
1855 : : {
1856 : 447987 : gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1857 : : NOT_TAKEN);
1858 : 447987 : gimplify_seq_add_stmt (pre_p, predict);
1859 : : }
1860 : 2271491 : }
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 : 2271497 : gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1871 : : {
1872 : 2271497 : greturn *ret;
1873 : 2271497 : tree ret_expr = TREE_OPERAND (stmt, 0);
1874 : 2271497 : tree result_decl, result;
1875 : :
1876 : 2271497 : if (ret_expr == error_mark_node)
1877 : : return GS_ERROR;
1878 : :
1879 : 2271491 : if (!ret_expr
1880 : 2180332 : || TREE_CODE (ret_expr) == RESULT_DECL)
1881 : : {
1882 : 106459 : maybe_add_early_return_predict_stmt (pre_p);
1883 : 106459 : greturn *ret = gimple_build_return (ret_expr);
1884 : 106459 : copy_warning (ret, stmt);
1885 : 106459 : gimplify_seq_add_stmt (pre_p, ret);
1886 : 106459 : return GS_ALL_DONE;
1887 : : }
1888 : :
1889 : 2165032 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1890 : : result_decl = NULL_TREE;
1891 : 2164711 : 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 : 5332 : result_decl = DECL_RESULT (current_function_decl);
1896 : : /* But crash if we end up trying to modify ret_expr below. */
1897 : 5332 : ret_expr = NULL_TREE;
1898 : : }
1899 : : else
1900 : : {
1901 : 2159379 : result_decl = TREE_OPERAND (ret_expr, 0);
1902 : :
1903 : : /* See through a return by reference. */
1904 : 2159379 : if (INDIRECT_REF_P (result_decl))
1905 : 45069 : result_decl = TREE_OPERAND (result_decl, 0);
1906 : :
1907 : 2159379 : 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 : 5332 : if (!result_decl)
1921 : : result = NULL_TREE;
1922 : 2164711 : else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1923 : : {
1924 : 183800 : 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 : 1980911 : else if (gimplify_ctxp->return_temp)
1937 : : result = gimplify_ctxp->return_temp;
1938 : : else
1939 : : {
1940 : 1483949 : 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 : 1483949 : suppress_warning (result, OPT_Wuninitialized);
1947 : :
1948 : 1483949 : 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 : 2165032 : if (result != result_decl)
1954 : 1980911 : TREE_OPERAND (ret_expr, 0) = result;
1955 : :
1956 : 2165032 : gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1957 : :
1958 : 2165032 : maybe_add_early_return_predict_stmt (pre_p);
1959 : 2165032 : ret = gimple_build_return (result);
1960 : 2165032 : copy_warning (ret, stmt);
1961 : 2165032 : gimplify_seq_add_stmt (pre_p, ret);
1962 : :
1963 : 2165032 : return GS_ALL_DONE;
1964 : : }
1965 : :
1966 : : /* Gimplify a variable-length array DECL. */
1967 : :
1968 : : static void
1969 : 8871 : 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 : 8871 : tree t, addr, ptr_type;
1974 : :
1975 : 8871 : gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1976 : 8871 : 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 : 8871 : 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 : 8866 : ptr_type = build_pointer_type (TREE_TYPE (decl));
1988 : 8866 : addr = create_tmp_var (ptr_type, get_name (decl));
1989 : 8866 : DECL_IGNORED_P (addr) = 0;
1990 : 8866 : t = build_fold_indirect_ref (addr);
1991 : 8866 : TREE_THIS_NOTRAP (t) = 1;
1992 : 8866 : SET_DECL_VALUE_EXPR (decl, t);
1993 : 8866 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1994 : :
1995 : 8866 : t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1996 : 8866 : max_int_size_in_bytes (TREE_TYPE (decl)));
1997 : : /* The call has been built for a variable-sized object. */
1998 : 8866 : CALL_ALLOCA_FOR_VAR_P (t) = 1;
1999 : 8866 : t = fold_convert (ptr_type, t);
2000 : 8866 : t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
2001 : :
2002 : 8866 : gimplify_and_add (t, seq_p);
2003 : :
2004 : : /* Record the dynamic allocation associated with DECL if requested. */
2005 : 8866 : 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 : 868906 : force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2014 : : {
2015 : 868906 : if (TYPE_P (*tp))
2016 : 0 : *walk_subtrees = 0;
2017 : 868906 : 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 : 868906 : 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 : 596 : gimple_add_init_for_auto_var (tree decl,
2036 : : enum auto_init_type init_type,
2037 : : gimple_seq *seq_p)
2038 : : {
2039 : 596 : gcc_assert (auto_var_p (decl));
2040 : 596 : gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
2041 : :
2042 : 596 : const location_t loc = DECL_SOURCE_LOCATION (decl);
2043 : 596 : tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
2044 : 596 : tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
2045 : 596 : tree decl_name;
2046 : :
2047 : 596 : if (DECL_NAME (decl))
2048 : 593 : 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 : 596 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
2057 : 596 : TREE_TYPE (decl), 3,
2058 : : decl_size, init_type_node,
2059 : : decl_name);
2060 : :
2061 : 596 : gimplify_assign (decl, call, seq_p);
2062 : 596 : }
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 : 112 : gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
2078 : : gimple_seq *seq_p)
2079 : : {
2080 : 112 : tree addr_of_decl = NULL_TREE;
2081 : 112 : tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
2082 : :
2083 : 112 : 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 : 112 : mark_addressable (decl);
2094 : 112 : addr_of_decl = build_fold_addr_expr (decl);
2095 : : }
2096 : :
2097 : 112 : gimple *call = gimple_build_call (fn, 2, addr_of_decl,
2098 : 112 : build_one_cst (TREE_TYPE (addr_of_decl)));
2099 : 112 : gimplify_seq_add_stmt (seq_p, call);
2100 : 112 : }
2101 : :
2102 : : /* Return true if the DECL need to be automaticly initialized by the
2103 : : compiler. */
2104 : : static bool
2105 : 3491437 : is_var_need_auto_init (tree decl)
2106 : : {
2107 : 3491437 : if (auto_var_p (decl)
2108 : 3394857 : && (TREE_CODE (decl) != VAR_DECL
2109 : 3392941 : || !DECL_HARD_REGISTER (decl))
2110 : 3394155 : && (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2111 : 655 : && (!lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl)))
2112 : 647 : && !OPAQUE_TYPE_P (TREE_TYPE (decl))
2113 : 3492084 : && !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 : 6569491 : gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
2123 : : {
2124 : 6569491 : tree stmt = *stmt_p;
2125 : 6569491 : tree decl = DECL_EXPR_DECL (stmt);
2126 : :
2127 : 6569491 : *stmt_p = NULL_TREE;
2128 : :
2129 : 6569491 : if (TREE_TYPE (decl) == error_mark_node)
2130 : : return GS_ERROR;
2131 : :
2132 : 6569420 : if ((TREE_CODE (decl) == TYPE_DECL
2133 : 6526326 : || VAR_P (decl))
2134 : 13092733 : && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
2135 : : {
2136 : 700272 : gimplify_type_sizes (TREE_TYPE (decl), seq_p);
2137 : 700272 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
2138 : 22801 : 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 : 6569420 : if (TREE_CODE (decl) == TYPE_DECL
2144 : 43094 : && DECL_ORIGINAL_TYPE (decl)
2145 : 6574741 : && !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 : 6569420 : if (VAR_P (decl) && !DECL_EXTERNAL (decl))
2153 : : {
2154 : 6523313 : tree init = DECL_INITIAL (decl);
2155 : 6523313 : 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 : 6523313 : bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
2162 : :
2163 : 6523313 : poly_uint64 size;
2164 : 6523313 : if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
2165 : 6523313 : || (!TREE_STATIC (decl)
2166 : 6397082 : && flag_stack_check == GENERIC_STACK_CHECK
2167 : 228 : && maybe_gt (size,
2168 : : (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
2169 : : {
2170 : 8748 : gimplify_vla_decl (decl, seq_p);
2171 : 8748 : is_vla = true;
2172 : : }
2173 : :
2174 : 6523313 : if (asan_poisoned_variables
2175 : 4820 : && !is_vla
2176 : 4634 : && TREE_ADDRESSABLE (decl)
2177 : 2306 : && !TREE_STATIC (decl)
2178 : 2096 : && !DECL_HAS_VALUE_EXPR_P (decl)
2179 : 2061 : && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
2180 : 2061 : && dbg_cnt (asan_use_after_scope)
2181 : 2061 : && !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 : 6525366 : && (DECL_SEEN_IN_BIND_EXPR_P (decl)
2187 : 0 : || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
2188 : : {
2189 : 2053 : asan_poisoned_variables->add (decl);
2190 : 2053 : asan_poison_variable (decl, false, seq_p);
2191 : 2053 : 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 : 6523313 : if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
2200 : 6523313 : && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
2201 : 18134 : gimple_add_tmp_var (decl);
2202 : :
2203 : 6523313 : if (init && init != error_mark_node)
2204 : : {
2205 : 3687893 : if (!TREE_STATIC (decl))
2206 : : {
2207 : 3657814 : DECL_INITIAL (decl) = NULL_TREE;
2208 : 3657814 : init = build2 (INIT_EXPR, void_type_node, decl, init);
2209 : 3657814 : gimplify_and_add (init, seq_p);
2210 : 3657814 : ggc_free (init);
2211 : : /* Clear TREE_READONLY if we really have an initialization. */
2212 : 3657814 : if (!DECL_INITIAL (decl)
2213 : 3657814 : && !omp_privatize_by_reference (decl))
2214 : 3639162 : 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 : 30079 : 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 : 2835420 : else if (is_var_need_auto_init (decl)
2225 : 2835420 : && !decl_had_value_expr_p)
2226 : : {
2227 : 596 : 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 : 596 : if (flag_auto_var_init == AUTO_INIT_PATTERN
2243 : 183 : && !is_gimple_reg (decl)
2244 : 693 : && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
2245 : 74 : 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 : 194817 : gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
2258 : : {
2259 : 194817 : tree saved_label = gimplify_ctxp->exit_label;
2260 : 194817 : tree start_label = create_artificial_label (UNKNOWN_LOCATION);
2261 : :
2262 : 194817 : gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
2263 : :
2264 : 194817 : gimplify_ctxp->exit_label = NULL_TREE;
2265 : :
2266 : 194817 : gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
2267 : :
2268 : 194817 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
2269 : :
2270 : 194817 : if (gimplify_ctxp->exit_label)
2271 : 6190 : gimplify_seq_add_stmt (pre_p,
2272 : 3095 : gimple_build_label (gimplify_ctxp->exit_label));
2273 : :
2274 : 194817 : gimplify_ctxp->exit_label = saved_label;
2275 : :
2276 : 194817 : *expr_p = NULL;
2277 : 194817 : 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 : 8764928 : gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
2285 : : {
2286 : 8764928 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
2287 : :
2288 : 8764928 : tree_stmt_iterator i = tsi_start (*expr_p);
2289 : :
2290 : 55848265 : while (!tsi_end_p (i))
2291 : : {
2292 : 38318409 : gimplify_stmt (tsi_stmt_ptr (i), pre_p);
2293 : 38318409 : tsi_delink (&i);
2294 : : }
2295 : :
2296 : 8764928 : if (temp)
2297 : : {
2298 : 15939 : *expr_p = temp;
2299 : 15939 : 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 : 52607 : 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 : 52607 : gimple *stmt = gsi_stmt (*gsi_p);
2353 : 52607 : bool unreachable_issued = wi->info != NULL;
2354 : :
2355 : 52607 : *handled_ops_p = true;
2356 : 52607 : 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 : 48089 : case GIMPLE_LABEL:
2387 : : /* Stop till the first Label. */
2388 : 48089 : 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 : 52036 : 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 : 51976 : || lang_GNU_Fortran ()
2438 : 100465 : || seq == NULL)
2439 : 3762 : return;
2440 : :
2441 : 48274 : struct walk_stmt_info wi;
2442 : :
2443 : 48274 : memset (&wi, 0, sizeof (wi));
2444 : 48274 : 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 : 52036 : maybe_warn_implicit_fallthrough (gimple_seq seq)
2829 : : {
2830 : 52036 : if (!warn_implicit_fallthrough)
2831 : 48074 : 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 : 3368799 : expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2848 : : struct walk_stmt_info *wi)
2849 : : {
2850 : 3368799 : gimple *stmt = gsi_stmt (*gsi_p);
2851 : :
2852 : 3368799 : *handled_ops_p = true;
2853 : 3368799 : switch (gimple_code (stmt))
2854 : : {
2855 : 155250 : case GIMPLE_TRY:
2856 : 155250 : case GIMPLE_BIND:
2857 : 155250 : case GIMPLE_CATCH:
2858 : 155250 : case GIMPLE_EH_FILTER:
2859 : 155250 : case GIMPLE_TRANSACTION:
2860 : : /* Walk the sub-statements. */
2861 : 155250 : *handled_ops_p = false;
2862 : 155250 : break;
2863 : 270843 : case GIMPLE_CALL:
2864 : 270843 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2865 : 270843 : 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 : 2942706 : default:
2940 : 2942706 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2941 : 2942706 : break;
2942 : : }
2943 : 3368799 : return NULL_TREE;
2944 : : }
2945 : :
2946 : : /* Expand all FALLTHROUGH () calls in SEQ. */
2947 : :
2948 : : static void
2949 : 50709 : expand_FALLTHROUGH (gimple_seq *seq_p)
2950 : : {
2951 : 50709 : auto_urlify_attributes sentinel;
2952 : :
2953 : 50709 : struct walk_stmt_info wi;
2954 : 50709 : location_t loc[2];
2955 : 50709 : memset (&wi, 0, sizeof (wi));
2956 : 50709 : loc[0] = UNKNOWN_LOCATION;
2957 : 50709 : loc[1] = UNKNOWN_LOCATION;
2958 : 50709 : wi.info = (void *) &loc[0];
2959 : 50709 : walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2960 : 50709 : 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 : 50709 : }
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 : 52036 : gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2973 : : {
2974 : 52036 : tree switch_expr = *expr_p;
2975 : 52036 : gimple_seq switch_body_seq = NULL;
2976 : 52036 : enum gimplify_status ret;
2977 : 52036 : tree index_type = TREE_TYPE (switch_expr);
2978 : 52036 : if (index_type == NULL_TREE)
2979 : 11293 : index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2980 : :
2981 : 52036 : ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2982 : : fb_rvalue);
2983 : 52036 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
2984 : : return ret;
2985 : :
2986 : 52036 : if (SWITCH_BODY (switch_expr))
2987 : : {
2988 : 52036 : vec<tree> labels;
2989 : 52036 : vec<tree> saved_labels;
2990 : 52036 : hash_set<tree> *saved_live_switch_vars = NULL;
2991 : 52036 : tree default_case = NULL_TREE;
2992 : 52036 : 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 : 52036 : saved_labels = gimplify_ctxp->case_labels;
2997 : 52036 : gimplify_ctxp->case_labels.create (8);
2998 : :
2999 : : /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
3000 : 52036 : saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
3001 : 52036 : tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
3002 : 52036 : if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
3003 : 51768 : gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
3004 : : else
3005 : 268 : gimplify_ctxp->live_switch_vars = NULL;
3006 : :
3007 : 52036 : bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
3008 : 52036 : gimplify_ctxp->in_switch_expr = true;
3009 : :
3010 : 52036 : gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
3011 : :
3012 : 52036 : gimplify_ctxp->in_switch_expr = old_in_switch_expr;
3013 : 52036 : maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
3014 : 52036 : maybe_warn_implicit_fallthrough (switch_body_seq);
3015 : : /* Only do this for the outermost GIMPLE_SWITCH. */
3016 : 52036 : if (!gimplify_ctxp->in_switch_expr)
3017 : 50709 : expand_FALLTHROUGH (&switch_body_seq);
3018 : :
3019 : 52036 : labels = gimplify_ctxp->case_labels;
3020 : 52036 : gimplify_ctxp->case_labels = saved_labels;
3021 : :
3022 : 52036 : if (gimplify_ctxp->live_switch_vars)
3023 : : {
3024 : 51768 : gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
3025 : 51768 : delete gimplify_ctxp->live_switch_vars;
3026 : : }
3027 : 52036 : gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
3028 : :
3029 : 52036 : preprocess_case_label_vec_for_gimple (labels, index_type,
3030 : : &default_case);
3031 : :
3032 : 52036 : bool add_bind = false;
3033 : 52036 : if (!default_case)
3034 : : {
3035 : 14631 : glabel *new_default;
3036 : :
3037 : 14631 : default_case
3038 : 14631 : = build_case_label (NULL_TREE, NULL_TREE,
3039 : : create_artificial_label (UNKNOWN_LOCATION));
3040 : 14631 : if (old_in_switch_expr)
3041 : : {
3042 : 768 : SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
3043 : 768 : add_bind = true;
3044 : : }
3045 : 14631 : new_default = gimple_build_label (CASE_LABEL (default_case));
3046 : 14631 : gimplify_seq_add_stmt (&switch_body_seq, new_default);
3047 : : }
3048 : 37405 : 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 : 52036 : add_bind = true;
3056 : : }
3057 : : }
3058 : :
3059 : 52036 : switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
3060 : : default_case, labels);
3061 : 52036 : 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 : 52036 : if (add_bind)
3068 : : {
3069 : 1069 : gimple_seq bind_body = NULL;
3070 : 1069 : gimplify_seq_add_stmt (&bind_body, switch_stmt);
3071 : 1069 : gimple_seq_add_seq (&bind_body, switch_body_seq);
3072 : 1069 : gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
3073 : 1069 : gimple_set_location (bind, EXPR_LOCATION (switch_expr));
3074 : 1069 : gimplify_seq_add_stmt (pre_p, bind);
3075 : : }
3076 : : else
3077 : : {
3078 : 50967 : gimplify_seq_add_stmt (pre_p, switch_stmt);
3079 : 50967 : gimplify_seq_add_seq (pre_p, switch_body_seq);
3080 : : }
3081 : 52036 : labels.release ();
3082 : : }
3083 : : else
3084 : 0 : gcc_unreachable ();
3085 : :
3086 : 52036 : return GS_ALL_DONE;
3087 : : }
3088 : :
3089 : : /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
3090 : :
3091 : : static enum gimplify_status
3092 : 2541044 : gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
3093 : : {
3094 : 2541044 : gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3095 : : == current_function_decl);
3096 : :
3097 : 2541044 : tree label = LABEL_EXPR_LABEL (*expr_p);
3098 : 2541044 : glabel *label_stmt = gimple_build_label (label);
3099 : 2541044 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3100 : 2541044 : gimplify_seq_add_stmt (pre_p, label_stmt);
3101 : :
3102 : 2541044 : 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 : 2541023 : 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 : 2541044 : return GS_ALL_DONE;
3110 : : }
3111 : :
3112 : : /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
3113 : :
3114 : : static enum gimplify_status
3115 : 1062902 : gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
3116 : : {
3117 : 1062902 : struct gimplify_ctx *ctxp;
3118 : 1062902 : 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 : 1062912 : for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
3125 : 1062912 : if (ctxp->case_labels.exists ())
3126 : : break;
3127 : :
3128 : 1062902 : tree label = CASE_LABEL (*expr_p);
3129 : 1062902 : label_stmt = gimple_build_label (label);
3130 : 1062902 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3131 : 1062902 : ctxp->case_labels.safe_push (*expr_p);
3132 : 1062902 : gimplify_seq_add_stmt (pre_p, label_stmt);
3133 : :
3134 : 1062902 : 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 : 1062886 : 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 : 1062902 : 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 : 1805663 : build_and_jump (tree *label_p)
3149 : : {
3150 : 1805663 : if (label_p == NULL)
3151 : : /* If there's nowhere to jump, just fall through. */
3152 : : return NULL_TREE;
3153 : :
3154 : 1223295 : if (*label_p == NULL_TREE)
3155 : : {
3156 : 702000 : tree label = create_artificial_label (UNKNOWN_LOCATION);
3157 : 702000 : *label_p = label;
3158 : : }
3159 : :
3160 : 1223295 : 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 : 18488407 : canonicalize_component_ref (tree *expr_p)
3192 : : {
3193 : 18488407 : tree expr = *expr_p;
3194 : 18488407 : tree type;
3195 : :
3196 : 18488407 : gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
3197 : :
3198 : 18488407 : if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
3199 : 7386118 : type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
3200 : : else
3201 : 11102289 : 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 : 18488407 : if (TREE_TYPE (expr) != type)
3207 : : {
3208 : : #ifdef ENABLE_TYPES_CHECKING
3209 : 879634 : tree old_type = TREE_TYPE (expr);
3210 : : #endif
3211 : 879634 : int type_quals;
3212 : :
3213 : : /* We need to preserve qualifiers and propagate them from
3214 : : operand 0. */
3215 : 879634 : type_quals = TYPE_QUALS (type)
3216 : 879634 : | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
3217 : 879634 : if (TYPE_QUALS (type) != type_quals)
3218 : 879398 : type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
3219 : :
3220 : : /* Set the type of the COMPONENT_REF to the underlying type. */
3221 : 879634 : 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 : 879634 : gcc_assert (useless_type_conversion_p (old_type, type));
3227 : : #endif
3228 : : }
3229 : 18488407 : }
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 : 518168 : canonicalize_addr_expr (tree *expr_p)
3244 : : {
3245 : 518168 : tree expr = *expr_p;
3246 : 518168 : tree addr_expr = TREE_OPERAND (expr, 0);
3247 : 518168 : tree datype, ddatype, pddatype;
3248 : :
3249 : : /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
3250 : 1035990 : if (!POINTER_TYPE_P (TREE_TYPE (expr))
3251 : 518168 : || 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 : 12671391 : gimplify_conversion (tree *expr_p)
3290 : : {
3291 : 12671391 : location_t loc = EXPR_LOCATION (*expr_p);
3292 : 12671391 : gcc_assert (CONVERT_EXPR_P (*expr_p));
3293 : :
3294 : : /* Then strip away all but the outermost conversion. */
3295 : 12671391 : STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
3296 : :
3297 : : /* And remove the outermost conversion if it's useless. */
3298 : 12671391 : 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 : 12671391 : if (CONVERT_EXPR_P (*expr_p))
3304 : : {
3305 : 12671391 : 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 : 12671391 : if (TREE_CODE (sub) == COMPONENT_REF)
3311 : 269423 : 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 : 12401968 : else if (TREE_CODE (sub) == ADDR_EXPR)
3316 : 518168 : 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 : 12671391 : 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 : 12671391 : if (TREE_CODE (*expr_p) == CONVERT_EXPR)
3327 : 232753 : TREE_SET_CODE (*expr_p, NOP_EXPR);
3328 : :
3329 : 12671391 : 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 : 127100242 : gimplify_var_or_parm_decl (tree *expr_p)
3337 : : {
3338 : 127100242 : 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 : 127100242 : if (VAR_P (decl)
3349 : 98197956 : && !DECL_SEEN_IN_BIND_EXPR_P (decl)
3350 : 17062249 : && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
3351 : 127155473 : && 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 : 127100176 : 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 : 127094386 : if (DECL_HAS_VALUE_EXPR_P (decl))
3363 : : {
3364 : 436736 : *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
3365 : 436736 : 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 : 110685623 : recalculate_side_effects (tree t)
3375 : : {
3376 : 110685623 : enum tree_code code = TREE_CODE (t);
3377 : 110685623 : int len = TREE_OPERAND_LENGTH (t);
3378 : 110685623 : int i;
3379 : :
3380 : 110685623 : switch (TREE_CODE_CLASS (code))
3381 : : {
3382 : 667388 : case tcc_expression:
3383 : 667388 : 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 : 110685621 : case tcc_comparison: /* a comparison expression */
3402 : 110685621 : case tcc_unary: /* a unary arithmetic expression */
3403 : 110685621 : case tcc_binary: /* a binary arithmetic expression */
3404 : 110685621 : case tcc_reference: /* a reference */
3405 : 110685621 : case tcc_vl_exp: /* a function call */
3406 : 110685621 : TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3407 : 359771074 : for (i = 0; i < len; ++i)
3408 : : {
3409 : 249085453 : tree op = TREE_OPERAND (t, i);
3410 : 249085453 : if (op && TREE_SIDE_EFFECTS (op))
3411 : 1297880 : 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 : 29815087 : gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3451 : : fallback_t fallback)
3452 : : {
3453 : 29815087 : tree *p;
3454 : 29815087 : enum gimplify_status ret = GS_ALL_DONE, tret;
3455 : 29815087 : int i;
3456 : 29815087 : location_t loc = EXPR_LOCATION (*expr_p);
3457 : 29815087 : 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 : 29815087 : auto_vec<tree, 10> expr_stack;
3462 : :
3463 : : /* We can handle anything that get_inner_reference can deal with. */
3464 : 71747458 : for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
3465 : : {
3466 : 41932371 : restart:
3467 : : /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
3468 : 71806745 : if (TREE_CODE (*p) == INDIRECT_REF)
3469 : 4018572 : *p = fold_indirect_ref_loc (loc, *p);
3470 : :
3471 : 71806745 : if (handled_component_p (*p))
3472 : : ;
3473 : : /* Expand DECL_VALUE_EXPR now. In some cases that may expose
3474 : : additional COMPONENT_REFs. */
3475 : 13371836 : else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
3476 : 31327131 : && gimplify_var_or_parm_decl (p) == GS_OK)
3477 : 59287 : goto restart;
3478 : : else
3479 : : break;
3480 : :
3481 : 41932371 : expr_stack.safe_push (*p);
3482 : : }
3483 : :
3484 : 29815087 : 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 : 29815087 : bool need_non_reg = false;
3506 : 71747455 : for (i = expr_stack.length () - 1; i >= 0; i--)
3507 : : {
3508 : 41932371 : tree t = expr_stack[i];
3509 : :
3510 : 41932371 : if (error_operand_p (TREE_OPERAND (t, 0)))
3511 : : return GS_ERROR;
3512 : :
3513 : 41932368 : 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 : 8680461 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3519 : : {
3520 : 8613283 : tree low = unshare_expr (array_ref_low_bound (t));
3521 : 8613283 : if (!is_gimple_min_invariant (low))
3522 : : {
3523 : 0 : TREE_OPERAND (t, 2) = low;
3524 : : }
3525 : : }
3526 : :
3527 : 8680461 : if (TREE_OPERAND (t, 3) == NULL_TREE)
3528 : : {
3529 : 8663386 : tree elmt_size = array_ref_element_size (t);
3530 : 8663386 : 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 : 33251907 : else if (TREE_CODE (t) == COMPONENT_REF)
3547 : : {
3548 : : /* Set the field offset into T and gimplify it. */
3549 : 31966695 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3550 : : {
3551 : 31966316 : tree offset = component_ref_field_offset (t);
3552 : 31966316 : 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 : 1285212 : 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 : 40924223 : 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 : 29815084 : tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3579 : : fallback | fb_lvalue);
3580 : 29815084 : ret = MIN (ret, tret);
3581 : 29815084 : 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 : 29815084 : if (need_non_reg && (fallback & fb_rvalue))
3588 : 22236843 : 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 : 29815084 : bool reg_operations = is_gimple_reg (*p);
3597 : 71747452 : for (; expr_stack.length () > 0; )
3598 : : {
3599 : 41932368 : tree t = expr_stack.pop ();
3600 : :
3601 : 41932368 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3602 : : {
3603 : 8680461 : gcc_assert (!reg_operations);
3604 : :
3605 : : /* Gimplify the low bound and element type size. */
3606 : 8680461 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3607 : : is_gimple_reg, fb_rvalue);
3608 : 8680461 : ret = MIN (ret, tret);
3609 : :
3610 : 8680461 : tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
3611 : : is_gimple_reg, fb_rvalue);
3612 : 8680461 : ret = MIN (ret, tret);
3613 : :
3614 : : /* Gimplify the dimension. */
3615 : 8680461 : tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3616 : : is_gimple_val, fb_rvalue);
3617 : 8680461 : ret = MIN (ret, tret);
3618 : : }
3619 : 33251907 : else if (TREE_CODE (t) == COMPONENT_REF)
3620 : : {
3621 : 31966695 : gcc_assert (!reg_operations);
3622 : :
3623 : 31966695 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3624 : : is_gimple_reg, fb_rvalue);
3625 : 31966695 : ret = MIN (ret, tret);
3626 : : }
3627 : 1285212 : else if (reg_operations)
3628 : : {
3629 : 751166 : tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
3630 : : is_gimple_val, fb_rvalue);
3631 : 1285212 : ret = MIN (ret, tret);
3632 : : }
3633 : :
3634 : 41932368 : 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 : 41932368 : recalculate_side_effects (t);
3641 : : }
3642 : :
3643 : : /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3644 : 29815084 : if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3645 : : {
3646 : 18218984 : canonicalize_component_ref (expr_p);
3647 : : }
3648 : :
3649 : 29815084 : expr_stack.release ();
3650 : :
3651 : 29815084 : gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3652 : :
3653 : : return ret;
3654 : 29815087 : }
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 : 1142091 : gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3672 : : bool want_value, tree arith_type)
3673 : : {
3674 : 1142091 : enum tree_code code;
3675 : 1142091 : tree lhs, lvalue, rhs, t1;
3676 : 1142091 : gimple_seq post = NULL, *orig_post_p = post_p;
3677 : 1142091 : bool postfix;
3678 : 1142091 : enum tree_code arith_code;
3679 : 1142091 : enum gimplify_status ret;
3680 : 1142091 : location_t loc = EXPR_LOCATION (*expr_p);
3681 : :
3682 : 1142091 : code = TREE_CODE (*expr_p);
3683 : :
3684 : 1142091 : gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3685 : : || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3686 : :
3687 : : /* Prefix or postfix? */
3688 : 1142091 : 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 : 397310 : if (postfix)
3697 : 1142091 : post_p = &post;
3698 : :
3699 : : /* Add or subtract? */
3700 : 1142091 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3701 : : arith_code = PLUS_EXPR;
3702 : : else
3703 : 49861 : arith_code = MINUS_EXPR;
3704 : :
3705 : : /* Gimplify the LHS into a GIMPLE lvalue. */
3706 : 1142091 : lvalue = TREE_OPERAND (*expr_p, 0);
3707 : 1142091 : ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3708 : 1142091 : if (ret == GS_ERROR)
3709 : : return ret;
3710 : :
3711 : : /* Extract the operands to the arithmetic operation. */
3712 : 1142089 : lhs = lvalue;
3713 : 1142089 : 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 : 1142089 : if (postfix)
3718 : : {
3719 : 190635 : ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3720 : 190635 : if (ret == GS_ERROR)
3721 : : return ret;
3722 : :
3723 : 190635 : lhs = get_initialized_tmp_var (lhs, pre_p);
3724 : : }
3725 : :
3726 : : /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3727 : 1142089 : if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3728 : : {
3729 : 244892 : rhs = convert_to_ptrofftype_loc (loc, rhs);
3730 : 244892 : if (arith_code == MINUS_EXPR)
3731 : 10883 : rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3732 : 244892 : t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3733 : : }
3734 : : else
3735 : 897197 : 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 : 1142089 : if (postfix)
3741 : : {
3742 : 190635 : gimplify_assign (lvalue, t1, pre_p);
3743 : 190635 : gimplify_seq_add_seq (orig_post_p, post);
3744 : 190635 : *expr_p = lhs;
3745 : 190635 : return GS_ALL_DONE;
3746 : : }
3747 : : else
3748 : : {
3749 : 951454 : *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3750 : 951454 : 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 : 81005139 : maybe_with_size_expr (tree *expr_p)
3758 : : {
3759 : 81005139 : tree expr = *expr_p;
3760 : 81005139 : tree type = TREE_TYPE (expr);
3761 : 81005139 : tree size;
3762 : :
3763 : : /* If we've already wrapped this or the type is error_mark_node, we can't do
3764 : : anything. */
3765 : 81005139 : if (TREE_CODE (expr) == WITH_SIZE_EXPR
3766 : 81005014 : || type == error_mark_node)
3767 : : return;
3768 : :
3769 : : /* If the size isn't known or is a constant, we have nothing to do. */
3770 : 81004731 : size = TYPE_SIZE_UNIT (type);
3771 : 81004731 : 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 : 33628786 : gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3787 : : bool allow_ssa)
3788 : : {
3789 : 33628786 : bool (*test) (tree);
3790 : 33628786 : 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 : 33628786 : if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3798 : : test = is_gimple_val, fb = fb_rvalue;
3799 : : else
3800 : : {
3801 : 1600154 : test = is_gimple_lvalue, fb = fb_either;
3802 : : /* Also strip a TARGET_EXPR that would force an extra copy. */
3803 : 1600154 : if (TREE_CODE (*arg_p) == TARGET_EXPR)
3804 : : {
3805 : 345016 : tree init = TARGET_EXPR_INITIAL (*arg_p);
3806 : 345016 : if (init
3807 : 345013 : && !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 : 648197 : && TREE_CODE (init) != CONSTRUCTOR)
3823 : 273268 : *arg_p = init;
3824 : : }
3825 : : }
3826 : :
3827 : : /* If this is a variable sized type, we must remember the size. */
3828 : 33628786 : 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 : 33628786 : 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 : 33628786 : 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 : 58949530 : maybe_fold_stmt (gimple_stmt_iterator *gsi)
3848 : : {
3849 : 58949530 : struct gimplify_omp_ctx *ctx;
3850 : 59475547 : for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3851 : 1176859 : 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 : 58298688 : if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3858 : : return false;
3859 : 55935947 : 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 : 337 : find_supercontext (void)
3871 : : {
3872 : 337 : vec<gbind *>stack = gimple_bind_expr_stack ();
3873 : 642 : for (int i = stack.length () - 1; i >= 0; i++)
3874 : : {
3875 : 305 : gbind *b = stack[i];
3876 : 305 : 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 : 1098 : 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 : 1098 : gcc_assert (omp_resolved_variant_calls != NULL);
4362 : 1098 : if (omp_resolved_variant_calls->contains (expr))
4363 : 102 : return expr;
4364 : :
4365 : 996 : tree fndecl = get_callee_fndecl (expr);
4366 : 996 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
4367 : 996 : location_t loc = EXPR_LOCATION (expr);
4368 : 996 : tree construct_context = omp_get_construct_context ();
4369 : 996 : vec<struct omp_variant> all_candidates
4370 : 996 : = omp_declare_variant_candidates (fndecl, construct_context);
4371 : 996 : gcc_assert (!all_candidates.is_empty ());
4372 : 996 : vec<struct omp_variant> candidates
4373 : 996 : = 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 : 996 : if (!candidates.is_empty ())
4379 : : {
4380 : 984 : int n = candidates.length ();
4381 : 984 : tree tail = NULL_TREE;
4382 : :
4383 : 2006 : for (int i = n - 1; i >= 0; i--)
4384 : : {
4385 : 1022 : if (tail)
4386 : 38 : gcc_assert (candidates[i].dynamic_selector);
4387 : : else
4388 : 984 : gcc_assert (!candidates[i].dynamic_selector);
4389 : 1022 : if (candidates[i].alternative == fndecl)
4390 : : {
4391 : : /* We should only get the original function back as the
4392 : : default. */
4393 : 203 : gcc_assert (!tail);
4394 : 203 : omp_resolved_variant_calls->add (expr);
4395 : 203 : 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 : 819 : tree thiscall = (tail || omp_dispatch_p
4404 : 819 : ? unshare_expr (expr) : expr);
4405 : 819 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
4406 : 819 : candidates[i].alternative);
4407 : 819 : if (omp_dispatch_p)
4408 : 471 : thiscall = modify_call_for_omp_dispatch (thiscall,
4409 : : dispatch_clauses,
4410 : : want_value,
4411 : : pointerize);
4412 : 819 : if (!tail)
4413 : : tail = thiscall;
4414 : : else
4415 : 76 : tail = build3 (COND_EXPR, TREE_TYPE (expr),
4416 : 38 : 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 : 1098 : gimplify_variant_call_expr (tree expr, fallback_t fallback,
4498 : : bool omp_dispatch_p)
4499 : : {
4500 : 1098 : tree type = TREE_TYPE (expr);
4501 : 1098 : bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
4502 : 487 : 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 : 487 : if (want_value
4506 : 487 : && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
4507 : 487 : || TREE_ADDRESSABLE (type)))
4508 : 0 : pointerize = true;
4509 : :
4510 : 1610 : return expand_variant_call_expr (expr, want_value, pointerize,
4511 : : omp_dispatch_p,
4512 : : (omp_dispatch_p
4513 : 512 : ? gimplify_omp_ctxp->clauses
4514 : 1098 : : 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 : 16941834 : gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4538 : : {
4539 : 16941834 : bool want_value = (fallback != fb_none);
4540 : 16941834 : tree fndecl, parms, p, fnptrtype;
4541 : 16941834 : enum gimplify_status ret;
4542 : 16941834 : int i, nargs;
4543 : 16941834 : gcall *call;
4544 : 16941834 : bool builtin_va_start_p = false, omp_dispatch_p = false;
4545 : 16941834 : location_t loc = EXPR_LOCATION (*expr_p);
4546 : :
4547 : 16941834 : 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 : 16941834 : if (! EXPR_HAS_LOCATION (*expr_p))
4552 : 568480 : SET_EXPR_LOCATION (*expr_p, input_location);
4553 : :
4554 : : /* Gimplify internal functions created in the FEs. */
4555 : 16941834 : if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
4556 : : {
4557 : 416321 : enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
4558 : 416321 : 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 : 415443 : 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 : 16526391 : fndecl = get_callee_fndecl (*expr_p);
4656 : 16526391 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4657 : 3957666 : switch (DECL_FUNCTION_CODE (fndecl))
4658 : : {
4659 : 95862 : 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 : 95862 : if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
4665 : 8866 : gimplify_ctxp->save_stack = true;
4666 : : else
4667 : 86996 : gimplify_ctxp->keep_stack = true;
4668 : : break;
4669 : :
4670 : 20690 : case BUILT_IN_VA_START:
4671 : 20690 : {
4672 : 20690 : builtin_va_start_p = true;
4673 : 20690 : 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 : 20689 : 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 : 16525875 : if (fndecl && fndecl_built_in_p (fndecl))
4711 : : {
4712 : 4189584 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4713 : 4189584 : 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 : 16525158 : fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
4725 : :
4726 : : /* Handle "declare variant" resolution and arglist processing. */
4727 : 16525158 : if (flag_openmp
4728 : 439837 : && fndecl
4729 : 439135 : && cfun
4730 : 439135 : && (cfun->curr_properties & PROP_gimple_any) == 0
4731 : 242242 : && !omp_has_novariants ()
4732 : 16767294 : && lookup_attribute ("omp declare variant base",
4733 : 242136 : DECL_ATTRIBUTES (fndecl)))
4734 : : {
4735 : 1098 : tree orig = *expr_p;
4736 : 1098 : *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 : 1098 : 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 : 16524669 : ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
4751 : : is_gimple_call_addr, fb_rvalue);
4752 : :
4753 : 16524669 : if (ret == GS_ERROR)
4754 : : return GS_ERROR;
4755 : :
4756 : 16524667 : nargs = call_expr_nargs (*expr_p);
4757 : :
4758 : : /* Get argument types for verification. */
4759 : 16524667 : fndecl = get_callee_fndecl (*expr_p);
4760 : 16524667 : parms = NULL_TREE;
4761 : 16524667 : if (fndecl)
4762 : 16303676 : parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4763 : : else
4764 : 220991 : parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
4765 : :
4766 : 32828343 : if (fndecl && DECL_ARGUMENTS (fndecl))
4767 : : p = DECL_ARGUMENTS (fndecl);
4768 : 7283227 : else if (parms)
4769 : : p = parms;
4770 : : else
4771 : : p = NULL_TREE;
4772 : 43770642 : 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 : 16524667 : if (!p
4779 : 16524667 : && i < nargs
4780 : 16524667 : && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
4781 : : {
4782 : 6882 : tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
4783 : 6882 : tree last_arg_fndecl = get_callee_fndecl (last_arg);
4784 : :
4785 : 6882 : if (last_arg_fndecl
4786 : 6882 : && 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 : 16524667 : bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
4814 : :
4815 : :
4816 : : /* Gimplify the function arguments. */
4817 : 16524667 : if (nargs > 0)
4818 : : {
4819 : 14338469 : for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
4820 : 43028852 : PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
4821 : 28690383 : PUSH_ARGS_REVERSED ? i-- : i++)
4822 : : {
4823 : 28690383 : enum gimplify_status t;
4824 : :
4825 : : /* Avoid gimplifying the second argument to va_start, which needs to
4826 : : be the plain PARM_DECL. */
4827 : 28690383 : if ((i != 1) || !builtin_va_start_p)
4828 : : {
4829 : 28669702 : tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
4830 : :
4831 : 28669702 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4832 : 6685 : gimplify_omp_ctxp->in_call_args = true;
4833 : 28669702 : t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
4834 : 28669702 : !returns_twice);
4835 : 28669702 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4836 : 6685 : gimplify_omp_ctxp->in_call_args = false;
4837 : :
4838 : 28669702 : if (t == GS_ERROR)
4839 : 28690383 : ret = GS_ERROR;
4840 : : }
4841 : : }
4842 : : }
4843 : :
4844 : : /* Gimplify the static chain. */
4845 : 16524667 : 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 : 16524667 : ret = GS_ERROR;
4856 : : }
4857 : : }
4858 : :
4859 : : /* Verify the function result. */
4860 : 16524667 : if (want_value && fndecl
4861 : 16524667 : && 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 : 16524667 : if (ret != GS_ERROR)
4869 : : {
4870 : 16524384 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4871 : :
4872 : 16524384 : 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 : 16512872 : if (TREE_CODE (*expr_p) == CALL_EXPR)
4891 : : {
4892 : 16512872 : int flags = call_expr_flags (*expr_p);
4893 : 16512872 : if (flags & (ECF_CONST | ECF_PURE)
4894 : : /* An infinite loop is considered a side effect. */
4895 : 2456674 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
4896 : 2042612 : 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 : 16512872 : 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 : 6302687 : gimple_stmt_iterator gsi;
4912 : 6302687 : call = gimple_build_call_from_tree (*expr_p, fnptrtype);
4913 : 6302687 : notice_special_calls (call);
4914 : 6302687 : gimplify_seq_add_stmt (pre_p, call);
4915 : 6302687 : gsi = gsi_last (*pre_p);
4916 : 6302687 : maybe_fold_stmt (&gsi);
4917 : 6302687 : *expr_p = NULL_TREE;
4918 : : }
4919 : : else
4920 : : /* Remember the original function type. */
4921 : 10210185 : CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
4922 : 10210185 : 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 : 1330304 : shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
4945 : : location_t locus, unsigned condition_uid)
4946 : : {
4947 : 1330304 : tree local_label = NULL_TREE;
4948 : 1330304 : 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 : 1330304 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
4954 : : {
4955 : 126719 : 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 : 126719 : if (false_label_p == NULL)
4964 : 4001 : false_label_p = &local_label;
4965 : :
4966 : : /* Keep the original source location on the first 'if'. */
4967 : 126719 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
4968 : : condition_uid);
4969 : 126719 : append_to_statement_list (t, &expr);
4970 : :
4971 : : /* Set the source location of the && on the second 'if'. */
4972 : 126719 : new_locus = rexpr_location (pred, locus);
4973 : 126719 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4974 : : new_locus, condition_uid);
4975 : 126719 : append_to_statement_list (t, &expr);
4976 : : }
4977 : 1203585 : else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
4978 : : {
4979 : 382290 : 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 : 382290 : if (true_label_p == NULL)
4988 : 229407 : true_label_p = &local_label;
4989 : :
4990 : : /* Keep the original source location on the first 'if'. */
4991 : 382290 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
4992 : : condition_uid);
4993 : 382290 : append_to_statement_list (t, &expr);
4994 : :
4995 : : /* Set the source location of the || on the second 'if'. */
4996 : 382290 : new_locus = rexpr_location (pred, locus);
4997 : 382290 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4998 : : new_locus, condition_uid);
4999 : 382290 : append_to_statement_list (t, &expr);
5000 : : }
5001 : 821295 : else if (TREE_CODE (pred) == COND_EXPR
5002 : 848 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
5003 : 822143 : && !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 : 820453 : expr = build3 (COND_EXPR, void_type_node, pred,
5030 : : build_and_jump (true_label_p),
5031 : : build_and_jump (false_label_p));
5032 : 820453 : SET_EXPR_LOCATION (expr, locus);
5033 : 820453 : tree_associate_condition_with_expr (expr, condition_uid);
5034 : : }
5035 : :
5036 : 1330304 : if (local_label)
5037 : : {
5038 : 233408 : t = build1 (LABEL_EXPR, void_type_node, local_label);
5039 : 233408 : append_to_statement_list (t, &expr);
5040 : : }
5041 : :
5042 : 1330304 : 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 : 11456380 : find_goto (tree expr)
5051 : : {
5052 : 11781604 : if (!expr)
5053 : : return NULL_TREE;
5054 : :
5055 : 9843671 : if (TREE_CODE (expr) == GOTO_EXPR)
5056 : : return expr;
5057 : :
5058 : 7849693 : if (TREE_CODE (expr) != STATEMENT_LIST)
5059 : : return NULL_TREE;
5060 : :
5061 : 1019611 : tree_stmt_iterator i = tsi_start (expr);
5062 : :
5063 : 1365856 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
5064 : 346245 : tsi_next (&i);
5065 : :
5066 : 10375184 : if (!tsi_one_before_end_p (i))
5067 : : return NULL_TREE;
5068 : :
5069 : 325224 : 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 : 11456380 : find_goto_label (tree expr)
5077 : : {
5078 : 11456380 : tree dest = find_goto (expr);
5079 : 13450358 : if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
5080 : 1993867 : 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 : 571136 : tag_shortcut_cond (tree pred, unsigned condition_uid)
5094 : : {
5095 : 609366 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
5096 : 609366 : || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5097 : : {
5098 : 561218 : tree fst = TREE_OPERAND (pred, 0);
5099 : 561218 : tree lst = TREE_OPERAND (pred, 1);
5100 : :
5101 : 561218 : if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
5102 : 561218 : || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
5103 : 167785 : tag_shortcut_cond (fst, condition_uid);
5104 : 393433 : else if (TREE_CODE (fst) == COND_EXPR)
5105 : 244 : tree_associate_condition_with_expr (fst, condition_uid);
5106 : :
5107 : 561218 : if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
5108 : 561218 : || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
5109 : : tag_shortcut_cond (lst, condition_uid);
5110 : 522988 : else if (TREE_CODE (lst) == COND_EXPR)
5111 : 438 : tree_associate_condition_with_expr (lst, condition_uid);
5112 : : }
5113 : 571136 : }
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 : 403351 : shortcut_cond_expr (tree expr, unsigned condition_uid)
5122 : : {
5123 : 403351 : tree pred = TREE_OPERAND (expr, 0);
5124 : 403351 : tree then_ = TREE_OPERAND (expr, 1);
5125 : 403351 : tree else_ = TREE_OPERAND (expr, 2);
5126 : 403351 : tree true_label, false_label, end_label, t;
5127 : 403351 : tree *true_label_p;
5128 : 403351 : tree *false_label_p;
5129 : 403351 : bool emit_end, emit_false, jump_over_else;
5130 : 403351 : bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
5131 : 403351 : bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
5132 : :
5133 : 403351 : tag_shortcut_cond (pred, condition_uid);
5134 : :
5135 : : /* First do simple transformations. */
5136 : 403351 : 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 : 285784 : while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
5143 : : {
5144 : : /* Keep the original source location on the first 'if'. */
5145 : 49012 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5146 : 49012 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5147 : : /* Set the source location of the && on the second 'if'. */
5148 : 49012 : if (rexpr_has_location (pred))
5149 : 47992 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5150 : 49012 : then_ = shortcut_cond_expr (expr, condition_uid);
5151 : 49012 : then_se = then_ && TREE_SIDE_EFFECTS (then_);
5152 : 49012 : pred = TREE_OPERAND (pred, 0);
5153 : 49012 : expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
5154 : 49012 : SET_EXPR_LOCATION (expr, locus);
5155 : : }
5156 : : }
5157 : :
5158 : 403351 : 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 : 26415 : 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 : 403351 : tree_associate_condition_with_expr (expr, condition_uid);
5182 : :
5183 : : /* If we're done, great. */
5184 : 403351 : if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
5185 : 403351 : && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
5186 : 92749 : 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 : 310602 : 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 : 310602 : if (tree then_goto = find_goto_label (then_))
5202 : : {
5203 : 4601 : true_label = GOTO_DESTINATION (then_goto);
5204 : 4601 : then_ = NULL;
5205 : 4601 : then_se = false;
5206 : : }
5207 : :
5208 : 310602 : if (tree else_goto = find_goto_label (else_))
5209 : : {
5210 : 4200 : false_label = GOTO_DESTINATION (else_goto);
5211 : 4200 : else_ = NULL;
5212 : 4200 : else_se = false;
5213 : : }
5214 : :
5215 : : /* If we aren't hijacking a label for the 'then' branch, it falls through. */
5216 : 310602 : if (true_label)
5217 : : true_label_p = &true_label;
5218 : : else
5219 : 306001 : true_label_p = NULL;
5220 : :
5221 : : /* The 'else' branch also needs a label if it contains interesting code. */
5222 : 310602 : 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 : 149224 : if (!then_se && !else_se)
5229 : 8530 : return shortcut_cond_r (pred, true_label_p, false_label_p,
5230 : 12795 : EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
5231 : :
5232 : : /* If our last subexpression already has a terminal label, reuse it. */
5233 : 306337 : if (else_se)
5234 : 161378 : t = expr_last (else_);
5235 : 144959 : else if (then_se)
5236 : 144959 : t = expr_last (then_);
5237 : : else
5238 : : t = NULL;
5239 : 306337 : if (t && TREE_CODE (t) == LABEL_EXPR)
5240 : 993 : 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 : 306337 : if (!false_label_p)
5245 : 144716 : false_label_p = &end_label;
5246 : :
5247 : : /* We only want to emit these labels if we aren't hijacking them. */
5248 : 306337 : emit_end = (end_label == NULL_TREE);
5249 : 306337 : 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 : 306337 : jump_over_else = block_may_fallthru (then_);
5260 : :
5261 : 612051 : pred = shortcut_cond_r (pred, true_label_p, false_label_p,
5262 : 612051 : EXPR_LOC_OR_LOC (expr, input_location),
5263 : : condition_uid);
5264 : :
5265 : 306337 : expr = NULL;
5266 : 306337 : append_to_statement_list (pred, &expr);
5267 : :
5268 : 306337 : append_to_statement_list (then_, &expr);
5269 : 306337 : if (else_se)
5270 : : {
5271 : 161378 : if (jump_over_else)
5272 : : {
5273 : 158926 : tree last = expr_last (expr);
5274 : 158926 : t = build_and_jump (&end_label);
5275 : 158926 : if (rexpr_has_location (last))
5276 : 38137 : SET_EXPR_LOCATION (t, rexpr_location (last));
5277 : 158926 : append_to_statement_list (t, &expr);
5278 : : }
5279 : 161378 : if (emit_false)
5280 : : {
5281 : 161378 : t = build1 (LABEL_EXPR, void_type_node, false_label);
5282 : 161378 : append_to_statement_list (t, &expr);
5283 : : }
5284 : 161378 : append_to_statement_list (else_, &expr);
5285 : : }
5286 : 306337 : if (emit_end && end_label)
5287 : : {
5288 : 302654 : t = build1 (LABEL_EXPR, void_type_node, end_label);
5289 : 302654 : append_to_statement_list (t, &expr);
5290 : : }
5291 : :
5292 : 306337 : return expr;
5293 : : }
5294 : :
5295 : : /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
5296 : :
5297 : : tree
5298 : 16413758 : gimple_boolify (tree expr)
5299 : : {
5300 : 16413758 : tree type = TREE_TYPE (expr);
5301 : 16413758 : location_t loc = EXPR_LOCATION (expr);
5302 : :
5303 : 16413758 : if (TREE_CODE (expr) == NE_EXPR
5304 : 5648398 : && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
5305 : 17344612 : && integer_zerop (TREE_OPERAND (expr, 1)))
5306 : : {
5307 : 479368 : tree call = TREE_OPERAND (expr, 0);
5308 : 479368 : 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 : 479368 : if (fn
5313 : 478795 : && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
5314 : 608530 : && call_expr_nargs (call) == 2)
5315 : : {
5316 : 129162 : tree arg = CALL_EXPR_ARG (call, 0);
5317 : 129162 : if (arg)
5318 : : {
5319 : 129162 : if (TREE_CODE (arg) == NOP_EXPR
5320 : 129162 : && TREE_TYPE (arg) == TREE_TYPE (call))
5321 : 62537 : arg = TREE_OPERAND (arg, 0);
5322 : 129162 : if (truth_value_p (TREE_CODE (arg)))
5323 : : {
5324 : 106971 : arg = gimple_boolify (arg);
5325 : 106971 : CALL_EXPR_ARG (call, 0)
5326 : 213942 : = fold_convert_loc (loc, TREE_TYPE (call), arg);
5327 : : }
5328 : : }
5329 : : }
5330 : : }
5331 : :
5332 : 16413758 : switch (TREE_CODE (expr))
5333 : : {
5334 : 1234813 : case TRUTH_AND_EXPR:
5335 : 1234813 : case TRUTH_OR_EXPR:
5336 : 1234813 : case TRUTH_XOR_EXPR:
5337 : 1234813 : case TRUTH_ANDIF_EXPR:
5338 : 1234813 : case TRUTH_ORIF_EXPR:
5339 : : /* Also boolify the arguments of truth exprs. */
5340 : 1234813 : TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
5341 : : /* FALLTHRU */
5342 : :
5343 : 1651569 : case TRUTH_NOT_EXPR:
5344 : 1651569 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5345 : :
5346 : : /* These expressions always produce boolean results. */
5347 : 1651569 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5348 : 303917 : TREE_TYPE (expr) = boolean_type_node;
5349 : : return expr;
5350 : :
5351 : 4981 : case ANNOTATE_EXPR:
5352 : 4981 : switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
5353 : : {
5354 : 4981 : case annot_expr_ivdep_kind:
5355 : 4981 : case annot_expr_unroll_kind:
5356 : 4981 : case annot_expr_no_vector_kind:
5357 : 4981 : case annot_expr_vector_kind:
5358 : 4981 : case annot_expr_parallel_kind:
5359 : 4981 : case annot_expr_maybe_infinite_kind:
5360 : 4981 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5361 : 4981 : 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 : 14757208 : default:
5369 : 14757208 : if (COMPARISON_CLASS_P (expr))
5370 : : {
5371 : : /* These expressions always produce boolean results. */
5372 : 12917000 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5373 : 1555616 : TREE_TYPE (expr) = boolean_type_node;
5374 : 12917000 : 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 : 1840208 : if (TREE_CODE (type) == BOOLEAN_TYPE)
5379 : : return expr;
5380 : 198356 : 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 : 46890 : gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
5389 : : {
5390 : 46890 : tree expr = *expr_p, cond;
5391 : 46890 : enum gimplify_status ret, tret;
5392 : 46890 : enum tree_code code;
5393 : :
5394 : 46890 : 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 : 46890 : code = TREE_CODE (cond);
5399 : 46890 : if (code == TRUTH_ANDIF_EXPR)
5400 : 32 : TREE_SET_CODE (cond, TRUTH_AND_EXPR);
5401 : 46858 : else if (code == TRUTH_ORIF_EXPR)
5402 : 4585 : TREE_SET_CODE (cond, TRUTH_OR_EXPR);
5403 : 46890 : ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
5404 : 46890 : COND_EXPR_COND (*expr_p) = cond;
5405 : :
5406 : 46890 : tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
5407 : : is_gimple_val, fb_rvalue);
5408 : 46890 : ret = MIN (ret, tret);
5409 : 46890 : tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
5410 : : is_gimple_val, fb_rvalue);
5411 : :
5412 : 46890 : 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 : 13215277 : generic_expr_could_trap_p (tree expr)
5421 : : {
5422 : 13215277 : unsigned i, n;
5423 : :
5424 : 13215277 : if (!expr || is_gimple_val (expr))
5425 : 5429798 : return false;
5426 : :
5427 : 7785479 : if (!EXPR_P (expr) || tree_could_trap_p (expr))
5428 : 2417706 : return true;
5429 : :
5430 : 5367773 : n = TREE_OPERAND_LENGTH (expr);
5431 : 12070632 : for (i = 0; i < n; i++)
5432 : 8986803 : 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 : 5417588 : gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
5445 : : unsigned uid)
5446 : : {
5447 : 5417588 : 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 : 6190817 : gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
5472 : : {
5473 : 6190817 : tree expr = *expr_p;
5474 : 6190817 : tree type = TREE_TYPE (expr);
5475 : 6190817 : location_t loc = EXPR_LOCATION (expr);
5476 : 6190817 : tree tmp, arm1, arm2;
5477 : 6190817 : enum gimplify_status ret;
5478 : 6190817 : tree label_true, label_false, label_cont;
5479 : 6190817 : bool have_then_clause_p, have_else_clause_p;
5480 : 6190817 : gcond *cond_stmt;
5481 : 6190817 : enum tree_code pred_code;
5482 : 6190817 : gimple_seq seq = NULL;
5483 : :
5484 : : /* If this COND_EXPR has a value, copy the values into a temporary within
5485 : : the arms. */
5486 : 6190817 : if (!VOID_TYPE_P (type))
5487 : : {
5488 : 418938 : tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
5489 : 418938 : 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 : 418938 : if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
5494 : 418931 : && !TREE_ADDRESSABLE (type))
5495 : : {
5496 : 418931 : 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 : 46890 : && !TREE_SIDE_EFFECTS (then_)
5500 : 46890 : && !generic_expr_could_trap_p (then_)
5501 : 46890 : && !TREE_SIDE_EFFECTS (else_)
5502 : 465821 : && !generic_expr_could_trap_p (else_))
5503 : 46890 : return gimplify_pure_cond_expr (expr_p, pre_p);
5504 : :
5505 : 372041 : tmp = create_tmp_var (type, "iftmp");
5506 : 372041 : 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 : 372048 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5530 : 290488 : TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
5531 : :
5532 : : /* Similarly, build the new else clause, `tmp = else_;'. */
5533 : 372048 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5534 : 361429 : TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
5535 : :
5536 : 372048 : TREE_TYPE (expr) = void_type_node;
5537 : 372048 : recalculate_side_effects (expr);
5538 : :
5539 : : /* Move the COND_EXPR to the prequeue. */
5540 : 372048 : gimplify_stmt (&expr, pre_p);
5541 : :
5542 : 372048 : *expr_p = result;
5543 : 372048 : return GS_ALL_DONE;
5544 : : }
5545 : :
5546 : : /* Remove any COMPOUND_EXPR so the following cases will be caught. */
5547 : 5787381 : STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
5548 : 5771879 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
5549 : 80333 : gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
5550 : :
5551 : : /* Make sure the condition has BOOLEAN_TYPE. */
5552 : 5771879 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5553 : :
5554 : : /* Break apart && and || conditions. */
5555 : 5771879 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
5556 : 5771879 : || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
5557 : : {
5558 : 353527 : expr = shortcut_cond_expr (expr, next_cond_uid ());
5559 : :
5560 : 353527 : if (expr != *expr_p)
5561 : : {
5562 : 353527 : *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 : 353527 : gimple_push_condition ();
5569 : 353527 : gimplify_stmt (expr_p, &seq);
5570 : 353527 : gimple_pop_condition (pre_p);
5571 : 353527 : gimple_seq_add_seq (pre_p, seq);
5572 : :
5573 : 353527 : return GS_ALL_DONE;
5574 : : }
5575 : : }
5576 : :
5577 : : /* Now do the normal gimplification. */
5578 : :
5579 : : /* Gimplify condition. */
5580 : 5418352 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
5581 : : is_gimple_condexpr_for_cond, fb_rvalue);
5582 : 5418352 : if (ret == GS_ERROR)
5583 : : return GS_ERROR;
5584 : 5417588 : gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
5585 : :
5586 : 5417588 : gimple_push_condition ();
5587 : :
5588 : 5417588 : have_then_clause_p = have_else_clause_p = false;
5589 : 5417588 : label_true = find_goto_label (TREE_OPERAND (expr, 1));
5590 : 5417588 : if (label_true
5591 : 1264212 : && 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 : 6681800 : && (optimize
5596 : 233332 : || !EXPR_HAS_LOCATION (expr)
5597 : 206569 : || !rexpr_has_location (label_true)
5598 : 74898 : || EXPR_LOCATION (expr) == rexpr_location (label_true)))
5599 : : {
5600 : 1263070 : have_then_clause_p = true;
5601 : 1263070 : label_true = GOTO_DESTINATION (label_true);
5602 : : }
5603 : : else
5604 : 4154518 : label_true = create_artificial_label (UNKNOWN_LOCATION);
5605 : 5417588 : label_false = find_goto_label (TREE_OPERAND (expr, 2));
5606 : 5417588 : if (label_false
5607 : 720854 : && 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 : 6138442 : && (optimize
5612 : 100354 : || !EXPR_HAS_LOCATION (expr)
5613 : 99505 : || !rexpr_has_location (label_false)
5614 : 121 : || EXPR_LOCATION (expr) == rexpr_location (label_false)))
5615 : : {
5616 : 720745 : have_else_clause_p = true;
5617 : 720745 : label_false = GOTO_DESTINATION (label_false);
5618 : : }
5619 : : else
5620 : 4696843 : label_false = create_artificial_label (UNKNOWN_LOCATION);
5621 : :
5622 : 5417588 : unsigned cond_uid = 0;
5623 : 5417588 : if (cond_uids)
5624 : 455 : if (unsigned *v = cond_uids->get (expr))
5625 : 435 : cond_uid = *v;
5626 : 435 : if (cond_uid == 0)
5627 : 5417153 : cond_uid = next_cond_uid ();
5628 : :
5629 : 5417588 : gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
5630 : : &arm2);
5631 : 5417588 : cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
5632 : : label_false);
5633 : 5417588 : gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
5634 : 5417588 : gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
5635 : 5417588 : copy_warning (cond_stmt, COND_EXPR_COND (expr));
5636 : 5417588 : gimplify_seq_add_stmt (&seq, cond_stmt);
5637 : 5417588 : gimple_stmt_iterator gsi = gsi_last (seq);
5638 : 5417588 : maybe_fold_stmt (&gsi);
5639 : :
5640 : 5417588 : label_cont = NULL_TREE;
5641 : 5417588 : if (!have_then_clause_p)
5642 : : {
5643 : : /* For if (...) {} else { code; } put label_true after
5644 : : the else block. */
5645 : 4154518 : if (TREE_OPERAND (expr, 1) == NULL_TREE
5646 : 205411 : && !have_else_clause_p
5647 : 4155303 : && 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 : 4153733 : bool then_side_effects
5659 : 4153733 : = (TREE_OPERAND (expr, 1)
5660 : 4153733 : && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
5661 : 4153733 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
5662 : 4153733 : 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 : 4153733 : if (!have_else_clause_p
5668 : 3947952 : && TREE_OPERAND (expr, 2) != NULL_TREE
5669 : 6898686 : && gimple_seq_may_fallthru (seq))
5670 : : {
5671 : 1920607 : gimple *g;
5672 : 1920607 : 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 : 1920607 : if (integer_zerop (COND_EXPR_COND (expr)))
5678 : : {
5679 : 66941 : UNUSED_LABEL_P (label_true) = 1;
5680 : 66941 : if (!then_side_effects)
5681 : 11453 : UNUSED_LABEL_P (label_cont) = 1;
5682 : : }
5683 : :
5684 : 1920607 : 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 : 1920607 : gimple_set_do_not_emit_location (g);
5693 : :
5694 : 1920607 : gimplify_seq_add_stmt (&seq, g);
5695 : : }
5696 : : }
5697 : : }
5698 : 5417588 : 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 : 4696843 : if (integer_nonzerop (COND_EXPR_COND (expr))
5704 : 4696843 : && (TREE_OPERAND (expr, 2) == NULL_TREE
5705 : 56385 : || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
5706 : 6902 : UNUSED_LABEL_P (label_false) = 1;
5707 : 4696843 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
5708 : 4696843 : have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
5709 : : }
5710 : 5417588 : if (label_cont)
5711 : 1921392 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
5712 : :
5713 : 5417588 : gimple_pop_condition (pre_p);
5714 : 5417588 : gimple_seq_add_seq (pre_p, seq);
5715 : :
5716 : 5417588 : if (ret == GS_ERROR)
5717 : : ; /* Do nothing. */
5718 : 5417588 : 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 : 5417588 : *expr_p = NULL;
5728 : 5417588 : 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 : 55681792 : prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
5743 : : {
5744 : 61085749 : while (handled_component_p (*expr_p))
5745 : 5403957 : expr_p = &TREE_OPERAND (*expr_p, 0);
5746 : :
5747 : : /* Do not allow an SSA name as the temporary. */
5748 : 55681792 : if (is_gimple_reg (*expr_p))
5749 : 12011 : *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
5750 : 55681792 : }
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 : 408796 : 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 : 408796 : enum gimplify_status one;
5914 : :
5915 : : /* If the value is constant, then there's nothing to pre-evaluate. */
5916 : 408796 : 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 : 192938 : 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 : 423 : 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 : 423 : tree loop_entry_label, loop_exit_label, fall_thru_label;
6004 : 423 : tree var, var_type, cref, tmp;
6005 : :
6006 : 423 : loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
6007 : 423 : loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
6008 : 423 : fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
6009 : :
6010 : : /* Create and initialize the index variable. */
6011 : 423 : var_type = TREE_TYPE (upper);
6012 : 423 : var = create_tmp_var (var_type);
6013 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
6014 : :
6015 : : /* Add the loop entry label. */
6016 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
6017 : :
6018 : : /* Build the reference. */
6019 : 423 : 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 : 423 : 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 : 312 : if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
6033 : : != GS_ERROR)
6034 : 312 : 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 : 846 : gimplify_seq_add_stmt (pre_p,
6039 : 423 : gimple_build_cond (EQ_EXPR, var, upper,
6040 : : loop_exit_label, fall_thru_label));
6041 : :
6042 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
6043 : :
6044 : : /* Otherwise, increment the index var... */
6045 : 423 : tmp = build2 (PLUS_EXPR, var_type, var,
6046 : : fold_convert (var_type, integer_one_node));
6047 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
6048 : :
6049 : : /* ...and jump back to the loop entry. */
6050 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
6051 : :
6052 : : /* Add the loop exit label. */
6053 : 423 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
6054 : 423 : }
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 : 1237059 : gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
6064 : : gimple_seq *pre_p, bool cleared)
6065 : : {
6066 : 1237059 : tree array_elt_type = NULL;
6067 : 1237059 : unsigned HOST_WIDE_INT ix;
6068 : 1237059 : tree purpose, value;
6069 : :
6070 : 1237059 : if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
6071 : 138145 : array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
6072 : :
6073 : 4637685 : FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
6074 : : {
6075 : 3400626 : tree cref;
6076 : :
6077 : : /* NULL values are created above for gimplification errors. */
6078 : 3400626 : if (value == NULL)
6079 : 362895 : continue;
6080 : :
6081 : 3400626 : if (cleared && initializer_zerop (value))
6082 : 362310 : 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 : 3038316 : 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 : 3038316 : if (!TREE_SIDE_EFFECTS (value)
6094 : 2743974 : && TREE_CODE (purpose) == FIELD_DECL
6095 : 5322090 : && 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 : 3038154 : if (TREE_CODE (purpose) == RANGE_EXPR)
6101 : : {
6102 : 433 : tree lower = TREE_OPERAND (purpose, 0);
6103 : 433 : 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 : 433 : if (simple_cst_equal (lower, upper))
6108 : : purpose = upper;
6109 : : else
6110 : : {
6111 : 423 : gimplify_init_ctor_eval_range (object, lower, upper, value,
6112 : : array_elt_type, pre_p, cleared);
6113 : 423 : continue;
6114 : : }
6115 : : }
6116 : :
6117 : 3037731 : if (array_elt_type)
6118 : : {
6119 : : /* Do not use bitsizetype for ARRAY_REF indices. */
6120 : 551651 : if (TYPE_DOMAIN (TREE_TYPE (object)))
6121 : 551651 : purpose
6122 : 551651 : = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
6123 : : purpose);
6124 : 551651 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6125 : : purpose, NULL_TREE, NULL_TREE);
6126 : : }
6127 : : else
6128 : : {
6129 : 2486080 : gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
6130 : 2486080 : cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
6131 : : unshare_expr (object), purpose, NULL_TREE);
6132 : : }
6133 : :
6134 : 3037731 : if (TREE_CODE (value) == CONSTRUCTOR
6135 : 3037731 : && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
6136 : 296260 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6137 : : pre_p, cleared);
6138 : 2741471 : 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 : 2741440 : tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
6186 : 2741440 : gimplify_and_add (init, pre_p);
6187 : 2741440 : ggc_free (init);
6188 : : }
6189 : : }
6190 : 1237059 : }
6191 : :
6192 : : /* Return the appropriate RHS predicate for this LHS. */
6193 : :
6194 : : gimple_predicate
6195 : 48444447 : rhs_predicate_for (tree lhs)
6196 : : {
6197 : 48444447 : if (is_gimple_reg (lhs))
6198 : : return is_gimple_reg_rhs_or_call;
6199 : : else
6200 : 11442124 : 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 : 47233500 : initial_rhs_predicate_for (tree lhs)
6208 : : {
6209 : 47233500 : if (is_gimple_reg_type (TREE_TYPE (lhs)))
6210 : : return is_gimple_reg_rhs_or_call;
6211 : : else
6212 : 2320891 : 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 : 36561 : gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
6221 : : bool (*gimple_test_f) (tree),
6222 : : fallback_t fallback)
6223 : : {
6224 : 36561 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
6225 : 36561 : tree decl = DECL_EXPR_DECL (decl_s);
6226 : 36561 : 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 : 36561 : if (TREE_ADDRESSABLE (*expr_p))
6231 : 507 : 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 : 1699 : 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 : 1699 : if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
6256 : 2 : gimple_add_tmp_var (decl);
6257 : :
6258 : 1699 : gimplify_and_add (decl_s, pre_p);
6259 : 1699 : *expr_p = decl;
6260 : 1699 : 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 : 1829734 : optimize_compound_literals_in_ctor (tree orig_ctor)
6268 : : {
6269 : 1829734 : tree ctor = orig_ctor;
6270 : 1829734 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6271 : 1829734 : unsigned int idx, num = vec_safe_length (elts);
6272 : :
6273 : 6166240 : for (idx = 0; idx < num; idx++)
6274 : : {
6275 : 4336506 : tree value = (*elts)[idx].value;
6276 : 4336506 : tree newval = value;
6277 : 4336506 : if (TREE_CODE (value) == CONSTRUCTOR)
6278 : 531320 : newval = optimize_compound_literals_in_ctor (value);
6279 : 3805186 : 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 : 4336506 : if (newval == value)
6292 : 4336450 : 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 : 1829734 : 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 : 1298366 : gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6320 : : bool want_value, bool notify_temp_creation)
6321 : : {
6322 : 1298366 : tree object, ctor, type;
6323 : 1298366 : enum gimplify_status ret;
6324 : 1298366 : vec<constructor_elt, va_gc> *elts;
6325 : 1298366 : bool cleared = false;
6326 : 1298366 : bool is_empty_ctor = false;
6327 : 1298366 : bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
6328 : :
6329 : 1298366 : gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
6330 : :
6331 : 1298366 : if (!notify_temp_creation)
6332 : : {
6333 : 1290240 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6334 : : is_gimple_lvalue, fb_lvalue);
6335 : 1290240 : if (ret == GS_ERROR)
6336 : : return ret;
6337 : : }
6338 : :
6339 : 1298366 : object = TREE_OPERAND (*expr_p, 0);
6340 : 1298366 : ctor = TREE_OPERAND (*expr_p, 1)
6341 : 1298366 : = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
6342 : 1298366 : type = TREE_TYPE (ctor);
6343 : 1298366 : elts = CONSTRUCTOR_ELTS (ctor);
6344 : 1298366 : ret = GS_ALL_DONE;
6345 : :
6346 : 1298366 : switch (TREE_CODE (type))
6347 : : {
6348 : 1291822 : case RECORD_TYPE:
6349 : 1291822 : case UNION_TYPE:
6350 : 1291822 : case QUAL_UNION_TYPE:
6351 : 1291822 : case ARRAY_TYPE:
6352 : 1291822 : {
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 : 1291822 : 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 : 1291822 : 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 : 1291822 : const bool ensure_single_access
6364 : 1291822 : = TREE_THIS_VOLATILE (object)
6365 : 222 : && !TREE_ADDRESSABLE (type)
6366 : 1292010 : && vec_safe_length (elts) > 1;
6367 : 1291822 : struct gimplify_init_ctor_preeval_data preeval_data;
6368 : 1291822 : HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
6369 : 1291822 : HOST_WIDE_INT num_unique_nonzero_elements;
6370 : 1291822 : int complete_p;
6371 : 1291822 : 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 : 1291822 : if (vec_safe_is_empty (elts))
6377 : : {
6378 : 324139 : if (notify_temp_creation)
6379 : 9693 : 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 : 324091 : if (VAR_P (object))
6384 : 183502 : TREE_READONLY (object) = 0;
6385 : :
6386 : : is_empty_ctor = true;
6387 : 325965 : 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 : 967683 : valid_const_initializer
6394 : 967683 : = 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 : 967683 : if (valid_const_initializer
6401 : 591773 : && num_nonzero_elements > 1
6402 : 441074 : && TREE_READONLY (object)
6403 : 4777 : && VAR_P (object)
6404 : 4391 : && !DECL_REGISTER (object)
6405 : 4384 : && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
6406 : 4181 : || 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 : 967683 : && (num_unique_nonzero_elements
6412 : 1874 : > 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 : 1874 : if (notify_temp_creation)
6417 : : return GS_ERROR;
6418 : :
6419 : 1874 : DECL_INITIAL (object) = ctor;
6420 : 1874 : TREE_STATIC (object) = 1;
6421 : 1874 : if (!DECL_NAME (object) || DECL_NAMELESS (object))
6422 : 1692 : DECL_NAME (object) = create_tmp_var_name ("C");
6423 : 1874 : 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 : 1874 : lhd_set_decl_assembler_name (object);
6433 : :
6434 : 1874 : *expr_p = NULL_TREE;
6435 : 1874 : 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 : 965809 : if (VAR_P (object) && !notify_temp_creation)
6441 : 765120 : 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 : 965809 : 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 : 965792 : 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 : 178230 : cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
6465 : 787562 : else if (num_ctor_elements - num_nonzero_elements
6466 : 787562 : > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
6467 : 787562 : && 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 : 786658 : 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 : 786653 : else if (complete_p <= 0
6481 : 14268 : && !TREE_ADDRESSABLE (ctor)
6482 : 14254 : && !TREE_THIS_VOLATILE (object)
6483 : 14251 : && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
6484 : 793230 : && 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 : 965809 : if (valid_const_initializer
6499 : 589899 : && complete_p
6500 : 426294 : && !(cleared || num_nonzero_elements == 0)
6501 : 332964 : && !TREE_ADDRESSABLE (type))
6502 : : {
6503 : 331828 : HOST_WIDE_INT size = int_size_in_bytes (type);
6504 : 331828 : 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 : 331828 : 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 : 331828 : if (DECL_P (object))
6519 : 318218 : align = DECL_ALIGN (object);
6520 : : else
6521 : 13610 : 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 : 331828 : if (size > 0
6527 : 331820 : && 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 : 300880 : && (num_unique_nonzero_elements
6533 : 300880 : > num_nonzero_elements / unique_nonzero_ratio
6534 : 67 : || size <= min_unique_size)
6535 : 632641 : && (size < num_nonzero_elements
6536 : 300707 : || !can_move_by_pieces (size, align)))
6537 : : {
6538 : 2828 : if (notify_temp_creation)
6539 : : return GS_ERROR;
6540 : :
6541 : 1416 : walk_tree (&ctor, force_labels_r, NULL, NULL);
6542 : 1416 : ctor = tree_output_constant_def (ctor);
6543 : 1416 : if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
6544 : 0 : ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
6545 : 1416 : 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 : 1416 : 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 : 962981 : 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 : 962824 : 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 : 956164 : if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
6578 : : {
6579 : 212856 : preeval_data.lhs_base_decl = get_base_address (object);
6580 : 212856 : if (!DECL_P (preeval_data.lhs_base_decl))
6581 : 40786 : preeval_data.lhs_base_decl = NULL;
6582 : 212856 : preeval_data.lhs_alias_set = get_alias_set (object);
6583 : :
6584 : 212856 : gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
6585 : : pre_p, post_p, &preeval_data);
6586 : : }
6587 : :
6588 : 956164 : bool ctor_has_side_effects_p
6589 : 956164 : = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
6590 : :
6591 : 956164 : 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 : 184940 : CONSTRUCTOR_ELTS (ctor) = NULL;
6597 : 184940 : TREE_SIDE_EFFECTS (ctor) = 0;
6598 : 184940 : object = unshare_expr (object);
6599 : 184940 : 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 : 184940 : if (!cleared
6606 : 184940 : || num_nonzero_elements > 0
6607 : 15847 : || ctor_has_side_effects_p)
6608 : 940688 : gimplify_init_ctor_eval (object, elts, pre_p, cleared);
6609 : :
6610 : 956164 : *expr_p = NULL_TREE;
6611 : : }
6612 : 956164 : 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 : 1298366 : 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 : 1288673 : 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 : 1288672 : if (*expr_p
6717 : : /* If the type is an empty type, we don't need to emit the
6718 : : assignment. */
6719 : 1288672 : && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
6720 : : {
6721 : 326828 : tree lhs = TREE_OPERAND (*expr_p, 0);
6722 : 326828 : tree rhs = TREE_OPERAND (*expr_p, 1);
6723 : 326828 : if (want_value && object == lhs)
6724 : 12 : lhs = unshare_expr (lhs);
6725 : 326828 : gassign *init = gimple_build_assign (lhs, rhs);
6726 : 326828 : gimplify_seq_add_stmt (pre_p, init);
6727 : : }
6728 : 1288672 : if (want_value)
6729 : : {
6730 : 19 : *expr_p = object;
6731 : 19 : ret = GS_OK;
6732 : : }
6733 : : else
6734 : : {
6735 : 1288653 : *expr_p = NULL;
6736 : 1288653 : 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 : 1288672 : if (is_init_expr
6752 : 917425 : && !is_gimple_reg (object)
6753 : 913446 : && clear_padding_type_may_have_padding_p (type)
6754 : 873922 : && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
6755 : 217905 : || !AGGREGATE_TYPE_P (type))
6756 : 1944689 : && 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 : 534464 : 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 : 96499108 : 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 : 96499108 : enum gimplify_status ret = GS_UNHANDLED;
6785 : 97165874 : bool changed;
6786 : :
6787 : 95723837 : do
6788 : : {
6789 : 97165874 : changed = false;
6790 : 97165874 : switch (TREE_CODE (*from_p))
6791 : : {
6792 : 11927515 : 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 : 11927515 : if (TREE_READONLY (*from_p)
6802 : 102235 : && DECL_INITIAL (*from_p)
6803 : 65688 : && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
6804 : 8126 : && !TREE_THIS_VOLATILE (*from_p)
6805 : 11935641 : && (!TREE_THIS_VOLATILE (*to_p)
6806 : 6 : || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
6807 : 6 : && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
6808 : : {
6809 : 8126 : tree old_from = *from_p;
6810 : 8126 : enum gimplify_status subret;
6811 : :
6812 : : /* Move the constructor into the RHS. */
6813 : 8126 : *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 : 8126 : subret = gimplify_init_constructor (expr_p, NULL, NULL,
6818 : : false, true);
6819 : 8126 : if (subret == GS_ERROR)
6820 : : {
6821 : : /* If so, revert the change. */
6822 : 1418 : *from_p = old_from;
6823 : : }
6824 : : else
6825 : : {
6826 : : ret = GS_OK;
6827 : : changed = true;
6828 : : }
6829 : : }
6830 : : break;
6831 : 539675 : case INDIRECT_REF:
6832 : 539675 : 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 : 1068928 : if (tree t
6845 : 534464 : = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
6846 : : {
6847 : 139077 : bool volatile_p = TREE_THIS_VOLATILE (*from_p);
6848 : 139077 : 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 : 139077 : *from_p = t;
6857 : 139077 : ret = GS_OK;
6858 : 139077 : changed = true;
6859 : : }
6860 : : break;
6861 : :
6862 : 217592 : case TARGET_EXPR:
6863 : 217592 : {
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 : 217592 : tree init = TARGET_EXPR_INITIAL (*from_p);
6875 : :
6876 : 217592 : if (init
6877 : 216876 : && (TREE_CODE (*expr_p) != MODIFY_EXPR
6878 : 67429 : || !TARGET_EXPR_NO_ELIDE (*from_p))
6879 : 434178 : && !VOID_TYPE_P (TREE_TYPE (init)))
6880 : : {
6881 : 191628 : *from_p = init;
6882 : 191628 : ret = GS_OK;
6883 : 191628 : changed = true;
6884 : : }
6885 : : }
6886 : : break;
6887 : :
6888 : 329144 : case COMPOUND_EXPR:
6889 : : /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
6890 : : caught. */
6891 : 329144 : gimplify_compound_expr (from_p, pre_p, true);
6892 : 329144 : ret = GS_OK;
6893 : 329144 : changed = true;
6894 : 329144 : break;
6895 : :
6896 : 1312068 : 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 : 1312068 : if (ret != GS_UNHANDLED)
6900 : : break;
6901 : :
6902 : : /* If we're initializing from a CONSTRUCTOR, break this into
6903 : : individual MODIFY_EXPRs. */
6904 : 1290240 : ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
6905 : : false);
6906 : 1290240 : return ret;
6907 : :
6908 : 228119 : 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 : 228119 : if (!is_gimple_reg_type (TREE_TYPE (*from_p))
6916 : 228119 : || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
6917 : : {
6918 : : /* This code should mirror the code in gimplify_cond_expr. */
6919 : 145954 : enum tree_code code = TREE_CODE (*expr_p);
6920 : 145954 : tree cond = *from_p;
6921 : 145954 : tree result = *to_p;
6922 : :
6923 : 145954 : ret = gimplify_expr (&result, pre_p, post_p,
6924 : : is_gimple_lvalue, fb_lvalue);
6925 : 145954 : if (ret != GS_ERROR)
6926 : 145954 : 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 : 145954 : if (VAR_P (result)
6933 : 144906 : && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
6934 : 281787 : && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6935 : 135738 : TREE_READONLY (result) = 0;
6936 : 145954 : if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
6937 : 136745 : TREE_OPERAND (cond, 1)
6938 : 273490 : = build2 (code, void_type_node, result,
6939 : 136745 : TREE_OPERAND (cond, 1));
6940 : 145954 : if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6941 : 145859 : TREE_OPERAND (cond, 2)
6942 : 291718 : = build2 (code, void_type_node, unshare_expr (result),
6943 : 145859 : TREE_OPERAND (cond, 2));
6944 : :
6945 : 145954 : TREE_TYPE (cond) = void_type_node;
6946 : 145954 : recalculate_side_effects (cond);
6947 : :
6948 : 145954 : if (want_value)
6949 : : {
6950 : 66 : gimplify_and_add (cond, pre_p);
6951 : 66 : *expr_p = unshare_expr (result);
6952 : : }
6953 : : else
6954 : 145888 : *expr_p = cond;
6955 : 145954 : return ret;
6956 : : }
6957 : : break;
6958 : :
6959 : 9791284 : 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 : 9791284 : if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
6963 : 9791284 : && aggregate_value_p (*from_p, *from_p))
6964 : : {
6965 : 251056 : bool use_target;
6966 : :
6967 : 251056 : 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 : 250313 : else if (TREE_CODE (*to_p) == RESULT_DECL
6972 : 5135 : && DECL_NAME (*to_p) == NULL_TREE
6973 : 255312 : && needs_to_live_in_memory (*to_p))
6974 : : use_target = true;
6975 : 245314 : else if (is_gimple_reg_type (TREE_TYPE (*to_p))
6976 : 245314 : || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
6977 : : /* Don't force regs into memory. */
6978 : : use_target = false;
6979 : 231262 : 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 : 13764 : 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 : 13736 : else if (TREE_CODE (*to_p) != SSA_NAME
6990 : 13736 : && (!is_gimple_variable (*to_p)
6991 : 10692 : || 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 : 224594 : CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
7005 : 224594 : 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 : 9351970 : 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 : 9351970 : if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
7056 : 9351970 : && 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 : 4162 : case COMPOUND_LITERAL_EXPR:
7065 : 4162 : {
7066 : 4162 : tree complit = TREE_OPERAND (*expr_p, 1);
7067 : 4162 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
7068 : 4162 : tree decl = DECL_EXPR_DECL (decl_s);
7069 : 4162 : 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 : 4162 : if (!TREE_ADDRESSABLE (complit)
7075 : 4162 : && !TREE_ADDRESSABLE (decl)
7076 : 4162 : && init)
7077 : : {
7078 : 4162 : *expr_p = copy_node (*expr_p);
7079 : 4162 : TREE_OPERAND (*expr_p, 1) = init;
7080 : 4162 : 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 : 19501420 : is_gimple_stmt (tree t)
7098 : : {
7099 : 19501420 : const enum tree_code code = TREE_CODE (t);
7100 : :
7101 : 19501420 : switch (code)
7102 : : {
7103 : 1705820 : case NOP_EXPR:
7104 : : /* The only valid NOP_EXPR is the empty statement. */
7105 : 1705820 : 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 : 50061993 : gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7236 : : bool want_value)
7237 : : {
7238 : 50061993 : tree *from_p = &TREE_OPERAND (*expr_p, 1);
7239 : 50061993 : tree *to_p = &TREE_OPERAND (*expr_p, 0);
7240 : 50061993 : enum gimplify_status ret = GS_UNHANDLED;
7241 : 50061993 : gimple *assign;
7242 : 50061993 : location_t loc = EXPR_LOCATION (*expr_p);
7243 : 50061993 : gimple_stmt_iterator gsi;
7244 : :
7245 : 50061993 : if (error_operand_p (*from_p) || error_operand_p (*to_p))
7246 : : return GS_ERROR;
7247 : :
7248 : 50061961 : 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 : 50061961 : if (TREE_CLOBBER_P (*from_p))
7254 : : {
7255 : 795935 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7256 : 795935 : if (ret == GS_ERROR)
7257 : : return ret;
7258 : 795935 : gcc_assert (!want_value);
7259 : 795935 : if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
7260 : : {
7261 : 213 : tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
7262 : : pre_p, post_p);
7263 : 213 : *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
7264 : : }
7265 : 795935 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
7266 : 795935 : *expr_p = NULL;
7267 : 795935 : return GS_ALL_DONE;
7268 : : }
7269 : :
7270 : : /* Convert initialization from an empty variable-size CONSTRUCTOR to
7271 : : memset. */
7272 : 49266026 : if (TREE_TYPE (*from_p) != error_mark_node
7273 : 49266026 : && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
7274 : 49266026 : && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
7275 : 1410 : && TREE_CODE (*from_p) == CONSTRUCTOR
7276 : 49266110 : && 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 : 49265959 : if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
7289 : : {
7290 : 13427230 : STRIP_USELESS_TYPE_CONVERSION (*from_p);
7291 : 13427230 : 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 : 49265959 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7297 : : want_value);
7298 : 49265959 : 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 : 47235552 : if (is_empty_type (TREE_TYPE (*from_p))
7306 : 14077 : && !want_value
7307 : : /* Don't do this for calls that return addressable types, expand_call
7308 : : relies on those having a lhs. */
7309 : 47249622 : && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
7310 : 12018 : && 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 : 47233500 : 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 : 47233500 : gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
7342 : 47233500 : ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
7343 : 47233500 : 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 : 47233215 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
7354 : 47233215 : if (saved_into_ssa
7355 : 44928583 : && TREE_CODE (*from_p) == CALL_EXPR
7356 : 52202239 : && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
7357 : 1481 : gimplify_ctxp->into_ssa = false;
7358 : 47233215 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7359 : 47233215 : gimplify_ctxp->into_ssa = saved_into_ssa;
7360 : 47233215 : 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 : 47233149 : gimple_predicate final_pred = rhs_predicate_for (*to_p);
7366 : 47233149 : if (final_pred != initial_pred)
7367 : : {
7368 : 8086584 : ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
7369 : 8086584 : 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 : 47233149 : 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 : 47233149 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7398 : : want_value);
7399 : 47233149 : 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 : 47232289 : 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 : 47231215 : if ((TREE_CODE (*to_p) == REALPART_EXPR
7426 : 47231215 : || TREE_CODE (*to_p) == IMAGPART_EXPR)
7427 : 47231215 : && 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 : 47229255 : if (!gimplify_ctxp->into_ssa
7434 : 2303733 : && VAR_P (*from_p)
7435 : 456422 : && DECL_IGNORED_P (*from_p)
7436 : 334203 : && DECL_P (*to_p)
7437 : 125114 : && !DECL_IGNORED_P (*to_p)
7438 : 29324 : && decl_function_context (*to_p) == current_function_decl
7439 : 47253115 : && 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 : 47229255 : if (want_value && TREE_THIS_VOLATILE (*to_p))
7449 : 773 : *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
7450 : :
7451 : 47229255 : 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 : 4669732 : gcall *call_stmt;
7456 : 4669732 : if (CALL_EXPR_FN (*from_p) == NULL_TREE)
7457 : : {
7458 : : /* Gimplify internal functions created in the FEs. */
7459 : 176845 : int nargs = call_expr_nargs (*from_p), i;
7460 : 176845 : enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
7461 : 176845 : auto_vec<tree> vargs (nargs);
7462 : :
7463 : 903776 : for (i = 0; i < nargs; i++)
7464 : : {
7465 : 550086 : gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
7466 : 550086 : EXPR_LOCATION (*from_p));
7467 : 550086 : vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
7468 : : }
7469 : 176845 : call_stmt = gimple_build_call_internal_vec (ifn, vargs);
7470 : 176845 : gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
7471 : 353690 : gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
7472 : 176845 : }
7473 : : else
7474 : : {
7475 : 4492887 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
7476 : 4492887 : CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
7477 : 4492887 : STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
7478 : 4492887 : tree fndecl = get_callee_fndecl (*from_p);
7479 : 4492887 : if (fndecl
7480 : 4377961 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
7481 : 4620412 : && call_expr_nargs (*from_p) == 3)
7482 : 63834 : call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
7483 : 63834 : CALL_EXPR_ARG (*from_p, 0),
7484 : 63834 : CALL_EXPR_ARG (*from_p, 1),
7485 : 63834 : CALL_EXPR_ARG (*from_p, 2));
7486 : : else
7487 : : {
7488 : 4429053 : call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
7489 : : }
7490 : : }
7491 : 4669732 : notice_special_calls (call_stmt);
7492 : 4669732 : if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
7493 : 4669614 : 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 : 42559523 : assign = gimple_build_assign (*to_p, *from_p);
7505 : 42559523 : gimple_set_location (assign, EXPR_LOCATION (*expr_p));
7506 : 42559523 : if (COMPARISON_CLASS_P (*from_p))
7507 : 1220349 : copy_warning (assign, *from_p);
7508 : : }
7509 : :
7510 : 47229255 : if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
7511 : : {
7512 : : /* We should have got an SSA name from the start. */
7513 : 34987303 : gcc_assert (TREE_CODE (*to_p) == SSA_NAME
7514 : : || ! gimple_in_ssa_p (cfun));
7515 : : }
7516 : :
7517 : 47229255 : gimplify_seq_add_stmt (pre_p, assign);
7518 : 47229255 : gsi = gsi_last (*pre_p);
7519 : 47229255 : maybe_fold_stmt (&gsi);
7520 : :
7521 : 47229255 : if (want_value)
7522 : : {
7523 : 646473 : *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
7524 : 646473 : return GS_OK;
7525 : : }
7526 : : else
7527 : 46582782 : *expr_p = NULL;
7528 : :
7529 : 46582782 : 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 : 1096374 : gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
7630 : : {
7631 : 1096374 : tree t = *expr_p;
7632 : :
7633 : 1176689 : do
7634 : : {
7635 : 1176689 : tree *sub_p = &TREE_OPERAND (t, 0);
7636 : :
7637 : 1176689 : if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
7638 : 203011 : gimplify_compound_expr (sub_p, pre_p, false);
7639 : : else
7640 : 973678 : gimplify_stmt (sub_p, pre_p);
7641 : :
7642 : 1176689 : t = TREE_OPERAND (t, 1);
7643 : : }
7644 : 1176689 : while (TREE_CODE (t) == COMPOUND_EXPR);
7645 : :
7646 : 1096374 : *expr_p = t;
7647 : 1096374 : if (want_value)
7648 : : return GS_OK;
7649 : : else
7650 : : {
7651 : 597495 : gimplify_stmt (expr_p, pre_p);
7652 : 597495 : 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 : 425072 : gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7665 : : {
7666 : 425072 : enum gimplify_status ret = GS_ALL_DONE;
7667 : 425072 : tree val;
7668 : :
7669 : 425072 : gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
7670 : 425072 : val = TREE_OPERAND (*expr_p, 0);
7671 : :
7672 : 425072 : 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 : 425070 : 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 : 161293 : 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 : 322586 : val = get_initialized_tmp_var (val, pre_p, post_p,
7692 : 322586 : gimple_in_ssa_p (cfun));
7693 : :
7694 : 161293 : TREE_OPERAND (*expr_p, 0) = val;
7695 : 161293 : SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
7696 : : }
7697 : :
7698 : 425070 : *expr_p = val;
7699 : :
7700 : 425070 : 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 : 33524475 : gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7718 : : {
7719 : 33524475 : tree expr = *expr_p;
7720 : 33524475 : tree op0 = TREE_OPERAND (expr, 0);
7721 : 33524475 : enum gimplify_status ret;
7722 : 33524475 : location_t loc = EXPR_LOCATION (*expr_p);
7723 : :
7724 : 33524475 : switch (TREE_CODE (op0))
7725 : : {
7726 : 96793 : case INDIRECT_REF:
7727 : 96793 : 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 : 96793 : {
7738 : 96793 : tree op00 = TREE_OPERAND (op0, 0);
7739 : 96793 : tree t_expr = TREE_TYPE (expr);
7740 : 96793 : tree t_op00 = TREE_TYPE (op00);
7741 : :
7742 : 96793 : if (!useless_type_conversion_p (t_expr, t_op00))
7743 : 0 : op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
7744 : 96793 : *expr_p = op00;
7745 : 96793 : ret = GS_OK;
7746 : : }
7747 : 96793 : 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 : 62703 : case MEM_REF:
7769 : 62703 : if (integer_zerop (TREE_OPERAND (op0, 1)))
7770 : 35694 : goto do_indirect_ref;
7771 : :
7772 : : /* fall through */
7773 : :
7774 : 33443860 : 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 : 33443860 : if (TREE_CODE (op0) == FUNCTION_DECL
7779 : 16645139 : && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
7780 : 37401061 : && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
7781 : 1286207 : 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 : 33443860 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
7790 : : is_gimple_addressable, fb_either);
7791 : 33443860 : 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 : 33443858 : prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
7797 : :
7798 : 33443858 : op0 = TREE_OPERAND (expr, 0);
7799 : :
7800 : : /* For various reasons, the gimplification of the expression
7801 : : may have made a new INDIRECT_REF. */
7802 : 33443858 : if (INDIRECT_REF_P (op0)
7803 : 33443858 : || (TREE_CODE (op0) == MEM_REF
7804 : 43187 : && integer_zerop (TREE_OPERAND (op0, 1))))
7805 : 16178 : goto do_indirect_ref;
7806 : :
7807 : 33427680 : 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 : 33427680 : if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
7813 : 147082 : *expr_p = build_fold_addr_expr (op0);
7814 : :
7815 : : /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
7816 : 33427680 : if (TREE_CODE (*expr_p) == ADDR_EXPR)
7817 : 33427679 : 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 : 33427680 : 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 : 33524475 : return ret;
7828 : : }
7829 : :
7830 : : /* Return the number of times character C occurs in string S. */
7831 : :
7832 : : static int
7833 : 88288 : num_occurrences (int c, const char *s)
7834 : : {
7835 : 88288 : int n = 0;
7836 : 240301 : while (*s)
7837 : 152013 : n += (*s++ == c);
7838 : 88288 : 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 : 190302 : num_alternatives (const_tree link)
7847 : : {
7848 : 190302 : if (link == nullptr)
7849 : : return 0;
7850 : :
7851 : 45601 : const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7852 : 45601 : int num = num_occurrences (',', constraint);
7853 : :
7854 : 45601 : if (num + 1 > MAX_RECOG_ALTERNATIVES)
7855 : : return -1;
7856 : :
7857 : 88288 : for (link = TREE_CHAIN (link); link; link = TREE_CHAIN (link))
7858 : : {
7859 : 42687 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7860 : 85374 : if (num_occurrences (',', constraint) != num)
7861 : : return -1;
7862 : : }
7863 : 45601 : 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 : 95151 : gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7871 : : {
7872 : 95151 : tree expr;
7873 : 95151 : int noutputs;
7874 : 95151 : const char **oconstraints;
7875 : 95151 : int i;
7876 : 95151 : tree link;
7877 : 95151 : const char *constraint;
7878 : 95151 : bool allows_mem, allows_reg, is_inout;
7879 : 95151 : enum gimplify_status ret, tret;
7880 : 95151 : gasm *stmt;
7881 : 95151 : vec<tree, va_gc> *inputs;
7882 : 95151 : vec<tree, va_gc> *outputs;
7883 : 95151 : vec<tree, va_gc> *clobbers;
7884 : 95151 : vec<tree, va_gc> *labels;
7885 : 95151 : tree link_next;
7886 : :
7887 : 95151 : expr = *expr_p;
7888 : 95151 : noutputs = list_length (ASM_OUTPUTS (expr));
7889 : 95151 : oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
7890 : :
7891 : 95151 : inputs = NULL;
7892 : 95151 : outputs = NULL;
7893 : 95151 : clobbers = NULL;
7894 : 95151 : labels = NULL;
7895 : :
7896 : 95151 : int num_alternatives_out = num_alternatives (ASM_OUTPUTS (expr));
7897 : 95151 : int num_alternatives_in = num_alternatives (ASM_INPUTS (expr));
7898 : 95151 : if (num_alternatives_out == -1 || num_alternatives_in == -1
7899 : 95151 : || (num_alternatives_out > 0 && num_alternatives_in > 0
7900 : 16154 : && 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 : 95151 : int num_alternatives = MAX (num_alternatives_out, num_alternatives_in);
7907 : :
7908 : 95151 : gimplify_reg_info reg_info (num_alternatives, noutputs);
7909 : :
7910 : 95151 : link_next = NULL_TREE;
7911 : 189628 : 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 : 95151 : ret = GS_ALL_DONE;
7927 : 95151 : link_next = NULL_TREE;
7928 : 152591 : for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
7929 : : {
7930 : 57473 : bool ok;
7931 : 57473 : size_t constraint_len;
7932 : :
7933 : 57473 : if (error_operand_p (TREE_VALUE (link)))
7934 : : return GS_ERROR;
7935 : 57452 : link_next = TREE_CHAIN (link);
7936 : :
7937 : 114904 : oconstraints[i]
7938 : 57452 : = constraint
7939 : 57452 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7940 : 57452 : constraint_len = strlen (constraint);
7941 : 57452 : if (constraint_len == 0)
7942 : 0 : continue;
7943 : :
7944 : 57452 : reg_info.operand = TREE_VALUE (link);
7945 : 57452 : ok = parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
7946 : : &allows_reg, &is_inout, ®_info);
7947 : 57452 : if (!ok)
7948 : : {
7949 : 7 : ret = GS_ERROR;
7950 : 7 : is_inout = false;
7951 : : }
7952 : :
7953 : : /* If we can't make copies, we can only accept memory.
7954 : : Similarly for VLAs. */
7955 : 57452 : tree outtype = TREE_TYPE (TREE_VALUE (link));
7956 : 57452 : if (TREE_ADDRESSABLE (outtype)
7957 : 57437 : || !COMPLETE_TYPE_P (outtype)
7958 : 114864 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype)))
7959 : : {
7960 : 276 : if (allows_mem)
7961 : 264 : allows_reg = 0;
7962 : : else
7963 : : {
7964 : 12 : error ("impossible constraint in %<asm%>");
7965 : 12 : error ("non-memory output %d must stay in memory", i);
7966 : 12 : return GS_ERROR;
7967 : : }
7968 : : }
7969 : :
7970 : 57440 : if (!allows_reg && allows_mem)
7971 : 1313 : mark_addressable (TREE_VALUE (link));
7972 : :
7973 : 57440 : tree orig = TREE_VALUE (link);
7974 : 106224 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
7975 : : is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7976 : : fb_lvalue | fb_mayfail);
7977 : 57440 : if (tret == GS_ERROR)
7978 : : {
7979 : 0 : if (orig != error_mark_node)
7980 : 0 : error ("invalid lvalue in %<asm%> output %d", i);
7981 : : ret = tret;
7982 : : }
7983 : :
7984 : : /* If the gimplified operand is a register we do not allow memory. */
7985 : 57440 : if (allows_reg
7986 : 56124 : && allows_mem
7987 : 63199 : && (is_gimple_reg (TREE_VALUE (link))
7988 : 267 : || (handled_component_p (TREE_VALUE (link))
7989 : 36 : && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
7990 : 5493 : allows_mem = 0;
7991 : :
7992 : : /* If the constraint does not allow memory make sure we gimplify
7993 : : it to a register if it is not already but its base is. This
7994 : : happens for complex and vector components. */
7995 : 57440 : if (!allows_mem)
7996 : : {
7997 : 55861 : tree op = TREE_VALUE (link);
7998 : 55861 : if (! is_gimple_val (op)
7999 : 20450 : && is_gimple_reg_type (TREE_TYPE (op))
8000 : 76310 : && is_gimple_reg (get_base_address (op)))
8001 : : {
8002 : 19 : tree tem = create_tmp_reg (TREE_TYPE (op));
8003 : 19 : tree ass;
8004 : 19 : if (is_inout)
8005 : : {
8006 : 7 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
8007 : : tem, unshare_expr (op));
8008 : 7 : gimplify_and_add (ass, pre_p);
8009 : : }
8010 : 19 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
8011 : 19 : gimplify_and_add (ass, post_p);
8012 : :
8013 : 19 : TREE_VALUE (link) = tem;
8014 : 19 : tret = GS_OK;
8015 : : }
8016 : : }
8017 : :
8018 : 57440 : vec_safe_push (outputs, link);
8019 : 57440 : TREE_CHAIN (link) = NULL_TREE;
8020 : :
8021 : 57440 : if (is_inout)
8022 : : {
8023 : : /* An input/output operand. To give the optimizers more
8024 : : flexibility, split it into separate input and output
8025 : : operands. */
8026 : 8656 : tree input;
8027 : : /* Buffer big enough to format a 32-bit UINT_MAX into. */
8028 : 8656 : char buf[11];
8029 : :
8030 : : /* Turn the in/out constraint into an output constraint. */
8031 : 8656 : char *p = xstrdup (constraint);
8032 : 8656 : p[0] = '=';
8033 : 8656 : TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
8034 : 8656 : TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
8035 : :
8036 : : /* And add a matching input constraint. */
8037 : 8656 : if (allows_reg)
8038 : : {
8039 : 8158 : sprintf (buf, "%u", i);
8040 : :
8041 : : /* If there are multiple alternatives in the constraint,
8042 : : handle each of them individually. Those that allow register
8043 : : will be replaced with operand number, the others will stay
8044 : : unchanged. */
8045 : 8158 : if (strchr (p, ',') != NULL)
8046 : : {
8047 : 100 : size_t len = 0, buflen = strlen (buf);
8048 : 100 : char *beg, *end, *str, *dst;
8049 : :
8050 : 100 : for (beg = p + 1;;)
8051 : : {
8052 : 200 : end = strchr (beg, ',');
8053 : 200 : if (end == NULL)
8054 : 100 : end = strchr (beg, '\0');
8055 : 200 : if ((size_t) (end - beg) < buflen)
8056 : 28 : len += buflen + 1;
8057 : : else
8058 : 172 : len += end - beg + 1;
8059 : 200 : if (*end)
8060 : 100 : beg = end + 1;
8061 : : else
8062 : : break;
8063 : : }
8064 : :
8065 : 100 : str = (char *) alloca (len);
8066 : 100 : for (beg = p + 1, dst = str;;)
8067 : : {
8068 : 200 : const char *tem;
8069 : 200 : bool mem_p, reg_p, inout_p;
8070 : :
8071 : 200 : end = strchr (beg, ',');
8072 : 200 : if (end)
8073 : 100 : *end = '\0';
8074 : 200 : beg[-1] = '=';
8075 : 200 : tem = beg - 1;
8076 : 200 : parse_output_constraint (&tem, i, 0, 0, &mem_p, ®_p,
8077 : : &inout_p, nullptr);
8078 : 200 : if (dst != str)
8079 : 100 : *dst++ = ',';
8080 : 200 : if (reg_p)
8081 : : {
8082 : 115 : memcpy (dst, buf, buflen);
8083 : 115 : dst += buflen;
8084 : : }
8085 : : else
8086 : : {
8087 : 85 : if (end)
8088 : 0 : len = end - beg;
8089 : : else
8090 : 85 : len = strlen (beg);
8091 : 85 : memcpy (dst, beg, len);
8092 : 85 : dst += len;
8093 : : }
8094 : 200 : if (end)
8095 : 100 : beg = end + 1;
8096 : : else
8097 : : break;
8098 : 100 : }
8099 : 100 : *dst = '\0';
8100 : 100 : input = build_string (dst - str, str);
8101 : : }
8102 : : else
8103 : 8058 : input = build_string (strlen (buf), buf);
8104 : : }
8105 : : else
8106 : 498 : input = build_string (constraint_len - 1, constraint + 1);
8107 : :
8108 : 8656 : free (p);
8109 : :
8110 : 8656 : input = build_tree_list (build_tree_list (NULL_TREE, input),
8111 : 8656 : unshare_expr (TREE_VALUE (link)));
8112 : 8656 : ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
8113 : : }
8114 : : }
8115 : :
8116 : : /* After all output operands have been gimplified, verify that each output
8117 : : operand is used at most once in case of hard register constraints. Thus,
8118 : : error out in cases like
8119 : : asm ("" : "={0}" (x), "={1}" (x));
8120 : : or even for
8121 : : asm ("" : "=r" (x), "={1}" (x));
8122 : :
8123 : : FIXME: Ideally we would also error out for cases like
8124 : : int x;
8125 : : asm ("" : "=r" (x), "=r" (x));
8126 : : However, since code like that was previously accepted, erroring out now might
8127 : : break existing code. On the other hand, we already error out for register
8128 : : asm like
8129 : : register int x asm ("0");
8130 : : asm ("" : "=r" (x), "=r" (x));
8131 : : Thus, maybe it wouldn't be too bad to also error out in the former
8132 : : non-register-asm case.
8133 : : */
8134 : 152554 : for (unsigned i = 0; i < vec_safe_length (outputs); ++i)
8135 : : {
8136 : 57438 : tree link = (*outputs)[i];
8137 : 57438 : tree op1 = TREE_VALUE (link);
8138 : 57438 : const char *constraint
8139 : 57438 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8140 : 57438 : if (strchr (constraint, '{') != nullptr)
8141 : 226 : for (unsigned j = 0; j < vec_safe_length (outputs); ++j)
8142 : : {
8143 : 137 : if (i == j)
8144 : 90 : continue;
8145 : 47 : tree link2 = (*outputs)[j];
8146 : 47 : tree op2 = TREE_VALUE (link2);
8147 : 47 : if (op1 == op2)
8148 : : {
8149 : 2 : error ("multiple outputs to lvalue %qE", op2);
8150 : 2 : return GS_ERROR;
8151 : : }
8152 : : }
8153 : : }
8154 : :
8155 : 95116 : link_next = NULL_TREE;
8156 : 95116 : int input_num = 0;
8157 : 134529 : for (link = ASM_INPUTS (expr); link; ++input_num, ++i, link = link_next)
8158 : : {
8159 : 39467 : if (error_operand_p (TREE_VALUE (link)))
8160 : : return GS_ERROR;
8161 : 39422 : link_next = TREE_CHAIN (link);
8162 : 39422 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8163 : 39422 : reg_info.operand = TREE_VALUE (link);
8164 : 39422 : bool ok = parse_input_constraint (&constraint, input_num, 0, noutputs, 0,
8165 : : oconstraints, &allows_mem, &allows_reg,
8166 : : ®_info);
8167 : 39422 : if (!ok)
8168 : : {
8169 : 27 : ret = GS_ERROR;
8170 : 27 : is_inout = false;
8171 : : }
8172 : :
8173 : : /* If we can't make copies, we can only accept memory. */
8174 : 39422 : tree intype = TREE_TYPE (TREE_VALUE (link));
8175 : 39422 : if (TREE_ADDRESSABLE (intype)
8176 : 39404 : || !COMPLETE_TYPE_P (intype)
8177 : 78787 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype)))
8178 : : {
8179 : 285 : if (allows_mem)
8180 : 276 : allows_reg = 0;
8181 : : else
8182 : : {
8183 : 9 : error ("impossible constraint in %<asm%>");
8184 : 9 : error ("non-memory input %d must stay in memory", i);
8185 : 9 : return GS_ERROR;
8186 : : }
8187 : : }
8188 : :
8189 : : /* If the operand is a memory input, it should be an lvalue. */
8190 : 39413 : if (!allows_reg && allows_mem)
8191 : : {
8192 : 1650 : tree inputv = TREE_VALUE (link);
8193 : 1650 : STRIP_NOPS (inputv);
8194 : 1650 : if (TREE_CODE (inputv) == PREDECREMENT_EXPR
8195 : : || TREE_CODE (inputv) == PREINCREMENT_EXPR
8196 : : || TREE_CODE (inputv) == POSTDECREMENT_EXPR
8197 : 1650 : || TREE_CODE (inputv) == POSTINCREMENT_EXPR
8198 : 1638 : || TREE_CODE (inputv) == MODIFY_EXPR
8199 : 3286 : || VOID_TYPE_P (TREE_TYPE (inputv)))
8200 : 38 : TREE_VALUE (link) = error_mark_node;
8201 : 1650 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8202 : : is_gimple_lvalue, fb_lvalue | fb_mayfail);
8203 : 1650 : if (tret != GS_ERROR)
8204 : : {
8205 : : /* Unlike output operands, memory inputs are not guaranteed
8206 : : to be lvalues by the FE, and while the expressions are
8207 : : marked addressable there, if it is e.g. a statement
8208 : : expression, temporaries in it might not end up being
8209 : : addressable. They might be already used in the IL and thus
8210 : : it is too late to make them addressable now though. */
8211 : 1604 : tree x = TREE_VALUE (link);
8212 : 1745 : while (handled_component_p (x))
8213 : 141 : x = TREE_OPERAND (x, 0);
8214 : 1604 : if (TREE_CODE (x) == MEM_REF
8215 : 1604 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
8216 : 0 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
8217 : 1604 : if ((VAR_P (x)
8218 : : || TREE_CODE (x) == PARM_DECL
8219 : : || TREE_CODE (x) == RESULT_DECL)
8220 : 1175 : && !TREE_ADDRESSABLE (x)
8221 : 23 : && is_gimple_reg (x))
8222 : : {
8223 : 17 : warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
8224 : 17 : input_location), 0,
8225 : : "memory input %d is not directly addressable",
8226 : : i);
8227 : 17 : prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
8228 : : }
8229 : : }
8230 : 1650 : mark_addressable (TREE_VALUE (link));
8231 : 1650 : if (tret == GS_ERROR)
8232 : : {
8233 : 46 : if (inputv != error_mark_node)
8234 : 46 : error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
8235 : : "memory input %d is not directly addressable", i);
8236 : : ret = tret;
8237 : : }
8238 : : }
8239 : : else
8240 : : {
8241 : 37763 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8242 : : is_gimple_asm_val, fb_rvalue);
8243 : 37763 : if (tret == GS_ERROR)
8244 : 46 : ret = tret;
8245 : : }
8246 : :
8247 : 39413 : TREE_CHAIN (link) = NULL_TREE;
8248 : 39413 : vec_safe_push (inputs, link);
8249 : : }
8250 : :
8251 : 95062 : link_next = NULL_TREE;
8252 : 95859 : for (link = ASM_LABELS (expr); link; link = link_next)
8253 : : {
8254 : 797 : link_next = TREE_CHAIN (link);
8255 : 797 : TREE_CHAIN (link) = NULL_TREE;
8256 : 797 : vec_safe_push (labels, link);
8257 : : }
8258 : :
8259 : : /* Do not add ASMs with errors to the gimple IL stream. */
8260 : 95062 : if (ret != GS_ERROR)
8261 : : {
8262 : 94986 : stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
8263 : : inputs, outputs, clobbers, labels);
8264 : :
8265 : : /* asm is volatile if it was marked by the user as volatile or
8266 : : there are no outputs or this is an asm goto. */
8267 : 189972 : gimple_asm_set_volatile (stmt,
8268 : 94986 : ASM_VOLATILE_P (expr)
8269 : 7383 : || noutputs == 0
8270 : 102355 : || labels);
8271 : 94986 : gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
8272 : 94986 : gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
8273 : :
8274 : 94986 : gimplify_seq_add_stmt (pre_p, stmt);
8275 : : }
8276 : :
8277 : : return ret;
8278 : 95151 : }
8279 : :
8280 : : /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
8281 : : GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
8282 : : gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
8283 : : return to this function.
8284 : :
8285 : : FIXME should we complexify the prequeue handling instead? Or use flags
8286 : : for all the cleanups and let the optimizer tighten them up? The current
8287 : : code seems pretty fragile; it will break on a cleanup within any
8288 : : non-conditional nesting. But any such nesting would be broken, anyway;
8289 : : we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
8290 : : and continues out of it. We can do that at the RTL level, though, so
8291 : : having an optimizer to tighten up try/finally regions would be a Good
8292 : : Thing. */
8293 : :
8294 : : static enum gimplify_status
8295 : 5548939 : gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
8296 : : {
8297 : 5548939 : gimple_stmt_iterator iter;
8298 : 5548939 : gimple_seq body_sequence = NULL;
8299 : :
8300 : 5548939 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
8301 : :
8302 : : /* We only care about the number of conditions between the innermost
8303 : : CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
8304 : : any cleanups collected outside the CLEANUP_POINT_EXPR. */
8305 : 5548939 : int old_conds = gimplify_ctxp->conditions;
8306 : 5548939 : gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
8307 : 5548939 : bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
8308 : 5548939 : gimplify_ctxp->conditions = 0;
8309 : 5548939 : gimplify_ctxp->conditional_cleanups = NULL;
8310 : 5548939 : gimplify_ctxp->in_cleanup_point_expr = true;
8311 : :
8312 : 5548939 : gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
8313 : :
8314 : 5548939 : gimplify_ctxp->conditions = old_conds;
8315 : 5548939 : gimplify_ctxp->conditional_cleanups = old_cleanups;
8316 : 5548939 : gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
8317 : :
8318 : 28369525 : for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
8319 : : {
8320 : 17313238 : gimple *wce = gsi_stmt (iter);
8321 : :
8322 : 17313238 : if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
8323 : : {
8324 : 625990 : if (gsi_one_before_end_p (iter))
8325 : : {
8326 : : /* Note that gsi_insert_seq_before and gsi_remove do not
8327 : : scan operands, unlike some other sequence mutators. */
8328 : 3167 : if (!gimple_wce_cleanup_eh_only (wce))
8329 : 3167 : gsi_insert_seq_before_without_update (&iter,
8330 : : gimple_wce_cleanup (wce),
8331 : : GSI_SAME_STMT);
8332 : 3167 : gsi_remove (&iter, true);
8333 : 3167 : break;
8334 : : }
8335 : : else
8336 : : {
8337 : 622823 : gtry *gtry;
8338 : 622823 : gimple_seq seq;
8339 : 622823 : enum gimple_try_flags kind;
8340 : :
8341 : 622823 : if (gimple_wce_cleanup_eh_only (wce))
8342 : : kind = GIMPLE_TRY_CATCH;
8343 : : else
8344 : 609764 : kind = GIMPLE_TRY_FINALLY;
8345 : 622823 : seq = gsi_split_seq_after (iter);
8346 : :
8347 : 622823 : gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
8348 : : /* Do not use gsi_replace here, as it may scan operands.
8349 : : We want to do a simple structural modification only. */
8350 : 622823 : gsi_set_stmt (&iter, gtry);
8351 : 1245646 : iter = gsi_start (gtry->eval);
8352 : : }
8353 : : }
8354 : : else
8355 : 16687248 : gsi_next (&iter);
8356 : : }
8357 : :
8358 : 5548939 : gimplify_seq_add_seq (pre_p, body_sequence);
8359 : 5548939 : if (temp)
8360 : : {
8361 : 348719 : *expr_p = temp;
8362 : 348719 : return GS_OK;
8363 : : }
8364 : : else
8365 : : {
8366 : 5200220 : *expr_p = NULL;
8367 : 5200220 : return GS_ALL_DONE;
8368 : : }
8369 : : }
8370 : :
8371 : : /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
8372 : : is the cleanup action required. EH_ONLY is true if the cleanup should
8373 : : only be executed if an exception is thrown, not on normal exit.
8374 : : If FORCE_UNCOND is true perform the cleanup unconditionally; this is
8375 : : only valid for clobbers. */
8376 : :
8377 : : static void
8378 : 631610 : gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
8379 : : bool force_uncond = false)
8380 : : {
8381 : 631610 : gimple *wce;
8382 : 631610 : gimple_seq cleanup_stmts = NULL;
8383 : :
8384 : : /* Errors can result in improperly nested cleanups. Which results in
8385 : : confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
8386 : 631610 : if (seen_error ())
8387 : 5620 : return;
8388 : :
8389 : 625990 : if (gimple_conditional_context ())
8390 : : {
8391 : : /* If we're in a conditional context, this is more complex. We only
8392 : : want to run the cleanup if we actually ran the initialization that
8393 : : necessitates it, but we want to run it after the end of the
8394 : : conditional context. So we wrap the try/finally around the
8395 : : condition and use a flag to determine whether or not to actually
8396 : : run the destructor. Thus
8397 : :
8398 : : test ? f(A()) : 0
8399 : :
8400 : : becomes (approximately)
8401 : :
8402 : : flag = 0;
8403 : : try {
8404 : : if (test) { A::A(temp); flag = 1; val = f(temp); }
8405 : : else { val = 0; }
8406 : : } finally {
8407 : : if (flag) A::~A(temp);
8408 : : }
8409 : : val
8410 : : */
8411 : 13117 : if (force_uncond)
8412 : : {
8413 : 12244 : gimplify_stmt (&cleanup, &cleanup_stmts);
8414 : 12244 : wce = gimple_build_wce (cleanup_stmts);
8415 : 12244 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8416 : : }
8417 : : else
8418 : : {
8419 : 873 : tree flag = create_tmp_var (boolean_type_node, "cleanup");
8420 : 873 : gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
8421 : 873 : gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
8422 : :
8423 : 873 : cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
8424 : 873 : gimplify_stmt (&cleanup, &cleanup_stmts);
8425 : 873 : wce = gimple_build_wce (cleanup_stmts);
8426 : 873 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8427 : :
8428 : 873 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
8429 : 873 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8430 : 873 : gimplify_seq_add_stmt (pre_p, ftrue);
8431 : :
8432 : : /* Because of this manipulation, and the EH edges that jump
8433 : : threading cannot redirect, the temporary (VAR) will appear
8434 : : to be used uninitialized. Don't warn. */
8435 : 873 : suppress_warning (var, OPT_Wuninitialized);
8436 : : }
8437 : : }
8438 : : else
8439 : : {
8440 : 612873 : gimplify_stmt (&cleanup, &cleanup_stmts);
8441 : 612873 : wce = gimple_build_wce (cleanup_stmts);
8442 : 612873 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8443 : 612873 : gimplify_seq_add_stmt (pre_p, wce);
8444 : : }
8445 : : }
8446 : :
8447 : : /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
8448 : :
8449 : : static enum gimplify_status
8450 : 820359 : gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8451 : : {
8452 : 820359 : tree targ = *expr_p;
8453 : 820359 : tree temp = TARGET_EXPR_SLOT (targ);
8454 : 820359 : tree init = TARGET_EXPR_INITIAL (targ);
8455 : 820359 : enum gimplify_status ret;
8456 : :
8457 : 820359 : bool unpoison_empty_seq = false;
8458 : 820359 : gimple_stmt_iterator unpoison_it;
8459 : :
8460 : 820359 : if (init)
8461 : : {
8462 : 783864 : gimple_seq init_pre_p = NULL;
8463 : :
8464 : : /* TARGET_EXPR temps aren't part of the enclosing block, so add it
8465 : : to the temps list. Handle also variable length TARGET_EXPRs. */
8466 : 783864 : if (!poly_int_tree_p (DECL_SIZE (temp)))
8467 : : {
8468 : 42 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
8469 : 1 : gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
8470 : : /* FIXME: this is correct only when the size of the type does
8471 : : not depend on expressions evaluated in init. */
8472 : 42 : gimplify_vla_decl (temp, &init_pre_p);
8473 : : }
8474 : : else
8475 : : {
8476 : : /* Save location where we need to place unpoisoning. It's possible
8477 : : that a variable will be converted to needs_to_live_in_memory. */
8478 : 783822 : unpoison_it = gsi_last (*pre_p);
8479 : 783822 : unpoison_empty_seq = gsi_end_p (unpoison_it);
8480 : :
8481 : 783822 : gimple_add_tmp_var (temp);
8482 : : }
8483 : :
8484 : : /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
8485 : : expression is supposed to initialize the slot. */
8486 : 783864 : if (VOID_TYPE_P (TREE_TYPE (init)))
8487 : 343613 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8488 : : fb_none);
8489 : : else
8490 : : {
8491 : 440251 : tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
8492 : 440251 : init = init_expr;
8493 : 440251 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8494 : : fb_none);
8495 : 440251 : init = NULL;
8496 : 440251 : ggc_free (init_expr);
8497 : : }
8498 : 783864 : if (ret == GS_ERROR)
8499 : : {
8500 : : /* PR c++/28266 Make sure this is expanded only once. */
8501 : 15 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8502 : 15 : return GS_ERROR;
8503 : : }
8504 : :
8505 : 783849 : if (init)
8506 : 0 : gimplify_and_add (init, &init_pre_p);
8507 : :
8508 : : /* Add a clobber for the temporary going out of scope, like
8509 : : gimplify_bind_expr. But only if we did not promote the
8510 : : temporary to static storage. */
8511 : 783849 : if (gimplify_ctxp->in_cleanup_point_expr
8512 : 635442 : && !TREE_STATIC (temp)
8513 : 1417788 : && needs_to_live_in_memory (temp))
8514 : : {
8515 : 482084 : if (flag_stack_reuse == SR_ALL)
8516 : : {
8517 : 481648 : tree clobber = build_clobber (TREE_TYPE (temp),
8518 : : CLOBBER_STORAGE_END);
8519 : 481648 : clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
8520 : 481648 : gimple_push_cleanup (temp, clobber, false, pre_p, true);
8521 : : }
8522 : 482084 : if (asan_poisoned_variables
8523 : 436 : && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
8524 : 436 : && !TREE_STATIC (temp)
8525 : 436 : && dbg_cnt (asan_use_after_scope)
8526 : 482520 : && !gimplify_omp_ctxp)
8527 : : {
8528 : 428 : tree asan_cleanup = build_asan_poison_call_expr (temp);
8529 : 428 : if (asan_cleanup)
8530 : : {
8531 : 428 : if (unpoison_empty_seq)
8532 : 216 : unpoison_it = gsi_start (*pre_p);
8533 : :
8534 : 428 : asan_poison_variable (temp, false, &unpoison_it,
8535 : : unpoison_empty_seq);
8536 : 428 : gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
8537 : : }
8538 : : }
8539 : : }
8540 : :
8541 : 783849 : gimple_seq_add_seq (pre_p, init_pre_p);
8542 : :
8543 : : /* If needed, push the cleanup for the temp. */
8544 : 783849 : if (TARGET_EXPR_CLEANUP (targ))
8545 : 149534 : gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
8546 : 149534 : CLEANUP_EH_ONLY (targ), pre_p);
8547 : :
8548 : : /* Only expand this once. */
8549 : 783849 : TREE_OPERAND (targ, 3) = init;
8550 : 783849 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8551 : : }
8552 : : else
8553 : : /* We should have expanded this before. */
8554 : 36495 : gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
8555 : :
8556 : 820344 : *expr_p = temp;
8557 : 820344 : return GS_OK;
8558 : : }
8559 : :
8560 : : /* Gimplification of expression trees. */
8561 : :
8562 : : /* Gimplify an expression which appears at statement context. The
8563 : : corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
8564 : : NULL, a new sequence is allocated.
8565 : :
8566 : : Return true if we actually added a statement to the queue. */
8567 : :
8568 : : bool
8569 : 102907227 : gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
8570 : : {
8571 : 102907227 : gimple_seq_node last;
8572 : :
8573 : 102907227 : last = gimple_seq_last (*seq_p);
8574 : 102907227 : gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
8575 : 102907227 : return last != gimple_seq_last (*seq_p);
8576 : : }
8577 : :
8578 : : /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
8579 : : to CTX. If entries already exist, force them to be some flavor of private.
8580 : : If there is no enclosing parallel, do nothing. */
8581 : :
8582 : : void
8583 : 124099 : omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
8584 : : {
8585 : 124099 : splay_tree_node n;
8586 : :
8587 : 124099 : if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
8588 : : return;
8589 : :
8590 : 36472 : do
8591 : : {
8592 : 36472 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8593 : 36472 : if (n != NULL)
8594 : : {
8595 : 4269 : if (n->value & GOVD_SHARED)
8596 : 183 : n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
8597 : 4086 : else if (n->value & GOVD_MAP)
8598 : 55 : n->value |= GOVD_MAP_TO_ONLY;
8599 : : else
8600 : : return;
8601 : : }
8602 : 32203 : else if ((ctx->region_type & ORT_TARGET) != 0)
8603 : : {
8604 : 6775 : if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
8605 : 4020 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8606 : : else
8607 : 2755 : omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
8608 : : }
8609 : 25428 : else if (ctx->region_type != ORT_WORKSHARE
8610 : : && ctx->region_type != ORT_TASKGROUP
8611 : 23619 : && ctx->region_type != ORT_SIMD
8612 : 22995 : && ctx->region_type != ORT_ACC
8613 : 22885 : && !(ctx->region_type & ORT_TARGET_DATA))
8614 : 14304 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8615 : :
8616 : 32441 : ctx = ctx->outer_context;
8617 : : }
8618 : 32441 : while (ctx);
8619 : : }
8620 : :
8621 : : /* Similarly for each of the type sizes of TYPE. */
8622 : :
8623 : : static void
8624 : 40651 : omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
8625 : : {
8626 : 40651 : if (type == NULL || type == error_mark_node)
8627 : : return;
8628 : 40647 : type = TYPE_MAIN_VARIANT (type);
8629 : :
8630 : 40647 : if (ctx->privatized_types->add (type))
8631 : : return;
8632 : :
8633 : 30901 : switch (TREE_CODE (type))
8634 : : {
8635 : 13073 : case INTEGER_TYPE:
8636 : 13073 : case ENUMERAL_TYPE:
8637 : 13073 : case BOOLEAN_TYPE:
8638 : 13073 : case REAL_TYPE:
8639 : 13073 : case FIXED_POINT_TYPE:
8640 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
8641 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
8642 : 13073 : break;
8643 : :
8644 : 5384 : case ARRAY_TYPE:
8645 : 5384 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8646 : 5384 : omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
8647 : 5384 : break;
8648 : :
8649 : 2507 : case RECORD_TYPE:
8650 : 2507 : case UNION_TYPE:
8651 : 2507 : case QUAL_UNION_TYPE:
8652 : 2507 : {
8653 : 2507 : tree field;
8654 : 26710 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8655 : 24203 : if (TREE_CODE (field) == FIELD_DECL)
8656 : : {
8657 : 9004 : omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
8658 : 9004 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
8659 : : }
8660 : : }
8661 : : break;
8662 : :
8663 : 9289 : case POINTER_TYPE:
8664 : 9289 : case REFERENCE_TYPE:
8665 : 9289 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8666 : 9289 : break;
8667 : :
8668 : : default:
8669 : : break;
8670 : : }
8671 : :
8672 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
8673 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
8674 : 30901 : lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
8675 : : }
8676 : :
8677 : : /* Add an entry for DECL in the OMP context CTX with FLAGS. */
8678 : :
8679 : : static void
8680 : 701998 : omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
8681 : : {
8682 : 701998 : splay_tree_node n;
8683 : 701998 : unsigned int nflags;
8684 : 701998 : tree t;
8685 : :
8686 : 701998 : if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
8687 : : return;
8688 : :
8689 : : /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
8690 : : there are constructors involved somewhere. Exception is a shared clause,
8691 : : there is nothing privatized in that case. */
8692 : 701853 : if ((flags & GOVD_SHARED) == 0
8693 : 701853 : && (TREE_ADDRESSABLE (TREE_TYPE (decl))
8694 : 641683 : || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
8695 : 7772 : flags |= GOVD_SEEN;
8696 : :
8697 : 701853 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8698 : 701853 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8699 : : {
8700 : : /* We shouldn't be re-adding the decl with the same data
8701 : : sharing class. */
8702 : 824 : gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
8703 : 824 : nflags = n->value | flags;
8704 : : /* The only combination of data sharing classes we should see is
8705 : : FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
8706 : : reduction variables to be used in data sharing clauses. */
8707 : 824 : gcc_assert ((ctx->region_type & ORT_ACC) != 0
8708 : : || ((nflags & GOVD_DATA_SHARE_CLASS)
8709 : : == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
8710 : : || (flags & GOVD_DATA_SHARE_CLASS) == 0);
8711 : 824 : n->value = nflags;
8712 : 824 : return;
8713 : : }
8714 : :
8715 : : /* When adding a variable-sized variable, we have to handle all sorts
8716 : : of additional bits of data: the pointer replacement variable, and
8717 : : the parameters of the type. */
8718 : 701029 : if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
8719 : : {
8720 : : /* Add the pointer replacement variable as PRIVATE if the variable
8721 : : replacement is private, else FIRSTPRIVATE since we'll need the
8722 : : address of the original variable either for SHARED, or for the
8723 : : copy into or out of the context. */
8724 : 1312 : if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
8725 : : {
8726 : 1222 : if (flags & GOVD_MAP)
8727 : : nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
8728 : 515 : else if (flags & GOVD_PRIVATE)
8729 : : nflags = GOVD_PRIVATE;
8730 : 431 : else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
8731 : 48 : && (flags & GOVD_FIRSTPRIVATE))
8732 : 423 : || (ctx->region_type == ORT_TARGET_DATA
8733 : 6 : && (flags & GOVD_DATA_SHARE_CLASS) == 0))
8734 : : nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
8735 : : else
8736 : 1222 : nflags = GOVD_FIRSTPRIVATE;
8737 : 1222 : nflags |= flags & GOVD_SEEN;
8738 : 1222 : t = DECL_VALUE_EXPR (decl);
8739 : 1222 : gcc_assert (INDIRECT_REF_P (t));
8740 : 1222 : t = TREE_OPERAND (t, 0);
8741 : 1222 : gcc_assert (DECL_P (t));
8742 : 1222 : omp_add_variable (ctx, t, nflags);
8743 : : }
8744 : :
8745 : : /* Add all of the variable and type parameters (which should have
8746 : : been gimplified to a formal temporary) as FIRSTPRIVATE. */
8747 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
8748 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
8749 : 1312 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8750 : :
8751 : : /* The variable-sized variable itself is never SHARED, only some form
8752 : : of PRIVATE. The sharing would take place via the pointer variable
8753 : : which we remapped above. */
8754 : 1312 : if (flags & GOVD_SHARED)
8755 : 276 : flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
8756 : 276 : | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
8757 : :
8758 : : /* We're going to make use of the TYPE_SIZE_UNIT at least in the
8759 : : alloca statement we generate for the variable, so make sure it
8760 : : is available. This isn't automatically needed for the SHARED
8761 : : case, since we won't be allocating local storage then.
8762 : : For local variables TYPE_SIZE_UNIT might not be gimplified yet,
8763 : : in this case omp_notice_variable will be called later
8764 : : on when it is gimplified. */
8765 : 1036 : else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
8766 : 1036 : && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
8767 : 267 : omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
8768 : : }
8769 : 699717 : else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
8770 : 699717 : && omp_privatize_by_reference (decl))
8771 : : {
8772 : 10278 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8773 : :
8774 : : /* Similar to the direct variable sized case above, we'll need the
8775 : : size of references being privatized. */
8776 : 10278 : if ((flags & GOVD_SHARED) == 0)
8777 : : {
8778 : 6487 : t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
8779 : 6487 : if (t && DECL_P (t))
8780 : 1337 : omp_notice_variable (ctx, t, true);
8781 : : }
8782 : : }
8783 : :
8784 : 701029 : if (n != NULL)
8785 : 1809 : n->value |= flags;
8786 : : else
8787 : 699220 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
8788 : :
8789 : : /* For reductions clauses in OpenACC loop directives, by default create a
8790 : : copy clause on the enclosing parallel construct for carrying back the
8791 : : results. */
8792 : 701029 : if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
8793 : : {
8794 : 4717 : struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
8795 : 6456 : while (outer_ctx)
8796 : : {
8797 : 5789 : n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
8798 : 5789 : if (n != NULL)
8799 : : {
8800 : : /* Ignore local variables and explicitly declared clauses. */
8801 : 3991 : if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
8802 : : break;
8803 : 444 : else if (outer_ctx->region_type == ORT_ACC_KERNELS)
8804 : : {
8805 : : /* According to the OpenACC spec, such a reduction variable
8806 : : should already have a copy map on a kernels construct,
8807 : : verify that here. */
8808 : 144 : gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
8809 : : && (n->value & GOVD_MAP));
8810 : : }
8811 : 300 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8812 : : {
8813 : : /* Remove firstprivate and make it a copy map. */
8814 : 150 : n->value &= ~GOVD_FIRSTPRIVATE;
8815 : 150 : n->value |= GOVD_MAP;
8816 : : }
8817 : : }
8818 : 1798 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8819 : : {
8820 : 503 : splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
8821 : : GOVD_MAP | GOVD_SEEN);
8822 : 503 : break;
8823 : : }
8824 : 1739 : outer_ctx = outer_ctx->outer_context;
8825 : : }
8826 : : }
8827 : : }
8828 : :
8829 : : /* Notice a threadprivate variable DECL used in OMP context CTX.
8830 : : This just prints out diagnostics about threadprivate variable uses
8831 : : in untied tasks. If DECL2 is non-NULL, prevent this warning
8832 : : on that variable. */
8833 : :
8834 : : static bool
8835 : 14183 : omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
8836 : : tree decl2)
8837 : : {
8838 : 14183 : splay_tree_node n;
8839 : 14183 : struct gimplify_omp_ctx *octx;
8840 : :
8841 : 28798 : for (octx = ctx; octx; octx = octx->outer_context)
8842 : 14615 : if ((octx->region_type & ORT_TARGET) != 0
8843 : 14615 : || octx->order_concurrent)
8844 : : {
8845 : 112 : n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
8846 : 112 : if (n == NULL)
8847 : : {
8848 : 20 : if (octx->order_concurrent)
8849 : : {
8850 : 20 : error ("threadprivate variable %qE used in a region with"
8851 : 20 : " %<order(concurrent)%> clause", DECL_NAME (decl));
8852 : 20 : inform (octx->location, "enclosing region");
8853 : : }
8854 : : else
8855 : : {
8856 : 0 : error ("threadprivate variable %qE used in target region",
8857 : 0 : DECL_NAME (decl));
8858 : 0 : inform (octx->location, "enclosing target region");
8859 : : }
8860 : 20 : splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
8861 : : }
8862 : 112 : if (decl2)
8863 : 0 : splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
8864 : : }
8865 : :
8866 : 14183 : if (ctx->region_type != ORT_UNTIED_TASK)
8867 : : return false;
8868 : 37 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8869 : 37 : if (n == NULL)
8870 : : {
8871 : 6 : error ("threadprivate variable %qE used in untied task",
8872 : 6 : DECL_NAME (decl));
8873 : 6 : inform (ctx->location, "enclosing task");
8874 : 6 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
8875 : : }
8876 : 37 : if (decl2)
8877 : 4 : splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
8878 : : return false;
8879 : : }
8880 : :
8881 : : /* Return true if global var DECL is device resident. */
8882 : :
8883 : : static bool
8884 : 652 : device_resident_p (tree decl)
8885 : : {
8886 : 652 : tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
8887 : :
8888 : 652 : if (!attr)
8889 : : return false;
8890 : :
8891 : 0 : for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
8892 : : {
8893 : 0 : tree c = TREE_VALUE (t);
8894 : 0 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
8895 : : return true;
8896 : : }
8897 : :
8898 : : return false;
8899 : : }
8900 : :
8901 : : /* Return true if DECL has an ACC DECLARE attribute. */
8902 : :
8903 : : static bool
8904 : 9132 : is_oacc_declared (tree decl)
8905 : : {
8906 : 9132 : tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
8907 : 9132 : tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
8908 : 9132 : return declared != NULL_TREE;
8909 : : }
8910 : :
8911 : : /* Determine outer default flags for DECL mentioned in an OMP region
8912 : : but not declared in an enclosing clause.
8913 : :
8914 : : ??? Some compiler-generated variables (like SAVE_EXPRs) could be
8915 : : remapped firstprivate instead of shared. To some extent this is
8916 : : addressed in omp_firstprivatize_type_sizes, but not
8917 : : effectively. */
8918 : :
8919 : : static unsigned
8920 : 47839 : omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
8921 : : bool in_code, unsigned flags)
8922 : : {
8923 : 47839 : enum omp_clause_default_kind default_kind = ctx->default_kind;
8924 : 47839 : enum omp_clause_default_kind kind;
8925 : :
8926 : 47839 : kind = lang_hooks.decls.omp_predetermined_sharing (decl);
8927 : 47839 : if (ctx->region_type & ORT_TASK)
8928 : : {
8929 : 3980 : tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
8930 : :
8931 : : /* The event-handle specified by a detach clause should always be firstprivate,
8932 : : regardless of the current default. */
8933 : 4192 : if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
8934 : : kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
8935 : : }
8936 : 47839 : if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8937 : : default_kind = kind;
8938 : 45033 : else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
8939 : : default_kind = OMP_CLAUSE_DEFAULT_SHARED;
8940 : : /* For C/C++ default({,first}private), variables with static storage duration
8941 : : declared in a namespace or global scope and referenced in construct
8942 : : must be explicitly specified, i.e. acts as default(none). */
8943 : 45032 : else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
8944 : 45032 : || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
8945 : 360 : && VAR_P (decl)
8946 : 321 : && is_global_var (decl)
8947 : 184 : && (DECL_FILE_SCOPE_P (decl)
8948 : 104 : || (DECL_CONTEXT (decl)
8949 : 104 : && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
8950 : 45160 : && !lang_GNU_Fortran ())
8951 : : default_kind = OMP_CLAUSE_DEFAULT_NONE;
8952 : :
8953 : 47710 : switch (default_kind)
8954 : : {
8955 : 267 : case OMP_CLAUSE_DEFAULT_NONE:
8956 : 267 : {
8957 : 267 : const char *rtype;
8958 : :
8959 : 267 : if (ctx->region_type & ORT_PARALLEL)
8960 : : rtype = "parallel";
8961 : 108 : else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
8962 : : rtype = "taskloop";
8963 : 72 : else if (ctx->region_type & ORT_TASK)
8964 : : rtype = "task";
8965 : 36 : else if (ctx->region_type & ORT_TEAMS)
8966 : : rtype = "teams";
8967 : : else
8968 : 0 : gcc_unreachable ();
8969 : :
8970 : 267 : error ("%qE not specified in enclosing %qs",
8971 : 267 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
8972 : 267 : inform (ctx->location, "enclosing %qs", rtype);
8973 : : }
8974 : : /* FALLTHRU */
8975 : 44201 : case OMP_CLAUSE_DEFAULT_SHARED:
8976 : 44201 : flags |= GOVD_SHARED;
8977 : 44201 : break;
8978 : 155 : case OMP_CLAUSE_DEFAULT_PRIVATE:
8979 : 155 : flags |= GOVD_PRIVATE;
8980 : 155 : break;
8981 : 269 : case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
8982 : 269 : flags |= GOVD_FIRSTPRIVATE;
8983 : 269 : break;
8984 : 3214 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8985 : : /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
8986 : 3214 : gcc_assert ((ctx->region_type & ORT_TASK) != 0);
8987 : 3214 : if (struct gimplify_omp_ctx *octx = ctx->outer_context)
8988 : : {
8989 : 2158 : omp_notice_variable (octx, decl, in_code);
8990 : 2986 : for (; octx; octx = octx->outer_context)
8991 : : {
8992 : 2930 : splay_tree_node n2;
8993 : :
8994 : 2930 : n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
8995 : 2930 : if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
8996 : 5 : && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
8997 : 5 : continue;
8998 : 2925 : if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
8999 : : {
9000 : 896 : flags |= GOVD_FIRSTPRIVATE;
9001 : 896 : goto found_outer;
9002 : : }
9003 : 2029 : if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
9004 : : {
9005 : 1206 : flags |= GOVD_SHARED;
9006 : 1206 : goto found_outer;
9007 : : }
9008 : : }
9009 : : }
9010 : :
9011 : 1112 : if (TREE_CODE (decl) == PARM_DECL
9012 : 1112 : || (!is_global_var (decl)
9013 : 296 : && DECL_CONTEXT (decl) == current_function_decl))
9014 : 735 : flags |= GOVD_FIRSTPRIVATE;
9015 : : else
9016 : 377 : flags |= GOVD_SHARED;
9017 : 47839 : found_outer:
9018 : : break;
9019 : :
9020 : 0 : default:
9021 : 0 : gcc_unreachable ();
9022 : : }
9023 : :
9024 : 47839 : return flags;
9025 : : }
9026 : :
9027 : : /* Return string name for types of OpenACC constructs from ORT_* values. */
9028 : :
9029 : : static const char *
9030 : 570 : oacc_region_type_name (enum omp_region_type region_type)
9031 : : {
9032 : 570 : switch (region_type)
9033 : : {
9034 : : case ORT_ACC_DATA:
9035 : : return "data";
9036 : 140 : case ORT_ACC_PARALLEL:
9037 : 140 : return "parallel";
9038 : 140 : case ORT_ACC_KERNELS:
9039 : 140 : return "kernels";
9040 : 140 : case ORT_ACC_SERIAL:
9041 : 140 : return "serial";
9042 : 0 : default:
9043 : 0 : gcc_unreachable ();
9044 : : }
9045 : : }
9046 : :
9047 : : /* Determine outer default flags for DECL mentioned in an OACC region
9048 : : but not declared in an enclosing clause. */
9049 : :
9050 : : static unsigned
9051 : 8870 : oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
9052 : : {
9053 : 8870 : struct gimplify_omp_ctx *ctx_default = ctx;
9054 : : /* If no 'default' clause appears on this compute construct... */
9055 : 8870 : if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
9056 : : {
9057 : : /* ..., see if one appears on a lexically containing 'data'
9058 : : construct. */
9059 : 9649 : while ((ctx_default = ctx_default->outer_context))
9060 : : {
9061 : 1534 : if (ctx_default->region_type == ORT_ACC_DATA
9062 : 1534 : && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
9063 : : break;
9064 : : }
9065 : : /* If not, reset. */
9066 : 8291 : if (!ctx_default)
9067 : 8694 : ctx_default = ctx;
9068 : : }
9069 : :
9070 : 8870 : bool on_device = false;
9071 : 8870 : bool is_private = false;
9072 : 8870 : bool declared = is_oacc_declared (decl);
9073 : 8870 : tree type = TREE_TYPE (decl);
9074 : :
9075 : 8870 : if (omp_privatize_by_reference (decl))
9076 : 338 : type = TREE_TYPE (type);
9077 : :
9078 : : /* For Fortran COMMON blocks, only used variables in those blocks are
9079 : : transfered and remapped. The block itself will have a private clause to
9080 : : avoid transfering the data twice.
9081 : : The hook evaluates to false by default. For a variable in Fortran's COMMON
9082 : : or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
9083 : : the variables in such a COMMON/EQUIVALENCE block shall be privatized not
9084 : : the whole block. For C++ and Fortran, it can also be true under certain
9085 : : other conditions, if DECL_HAS_VALUE_EXPR. */
9086 : 8870 : if (RECORD_OR_UNION_TYPE_P (type))
9087 : 982 : is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
9088 : :
9089 : 8870 : if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
9090 : 8870 : && is_global_var (decl)
9091 : 652 : && device_resident_p (decl)
9092 : 8870 : && !is_private)
9093 : : {
9094 : 0 : on_device = true;
9095 : 0 : flags |= GOVD_MAP_TO_ONLY;
9096 : : }
9097 : :
9098 : 8870 : switch (ctx->region_type)
9099 : : {
9100 : 1491 : case ORT_ACC_KERNELS:
9101 : 1491 : if (is_private)
9102 : 0 : flags |= GOVD_FIRSTPRIVATE;
9103 : 1491 : else if (AGGREGATE_TYPE_P (type))
9104 : : {
9105 : : /* Aggregates default to 'present_or_copy', or 'present'. */
9106 : 429 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9107 : 411 : flags |= GOVD_MAP;
9108 : : else
9109 : 18 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9110 : : }
9111 : : else
9112 : : /* Scalars default to 'copy'. */
9113 : 1062 : flags |= GOVD_MAP | GOVD_MAP_FORCE;
9114 : :
9115 : : break;
9116 : :
9117 : 7379 : case ORT_ACC_PARALLEL:
9118 : 7379 : case ORT_ACC_SERIAL:
9119 : 7379 : if (is_private)
9120 : 0 : flags |= GOVD_FIRSTPRIVATE;
9121 : 7379 : else if (on_device || declared)
9122 : 17 : flags |= GOVD_MAP;
9123 : 7362 : else if (AGGREGATE_TYPE_P (type))
9124 : : {
9125 : : /* Aggregates default to 'present_or_copy', or 'present'. */
9126 : 3697 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9127 : 3431 : flags |= GOVD_MAP;
9128 : : else
9129 : 266 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9130 : : }
9131 : : else
9132 : : /* Scalars default to 'firstprivate'. */
9133 : 3665 : flags |= GOVD_FIRSTPRIVATE;
9134 : :
9135 : : break;
9136 : :
9137 : 0 : default:
9138 : 0 : gcc_unreachable ();
9139 : : }
9140 : :
9141 : 8870 : if (DECL_ARTIFICIAL (decl))
9142 : : ; /* We can get compiler-generated decls, and should not complain
9143 : : about them. */
9144 : 8562 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
9145 : : {
9146 : 420 : error ("%qE not specified in enclosing OpenACC %qs construct",
9147 : 210 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
9148 : : oacc_region_type_name (ctx->region_type));
9149 : 210 : if (ctx_default != ctx)
9150 : 150 : inform (ctx->location, "enclosing OpenACC %qs construct and",
9151 : : oacc_region_type_name (ctx->region_type));
9152 : 210 : inform (ctx_default->location,
9153 : : "enclosing OpenACC %qs construct with %qs clause",
9154 : : oacc_region_type_name (ctx_default->region_type),
9155 : : "default(none)");
9156 : : }
9157 : 8352 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
9158 : : ; /* Handled above. */
9159 : : else
9160 : 7819 : gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
9161 : :
9162 : 8870 : return flags;
9163 : : }
9164 : :
9165 : : /* Record the fact that DECL was used within the OMP context CTX.
9166 : : IN_CODE is true when real code uses DECL, and false when we should
9167 : : merely emit default(none) errors. Return true if DECL is going to
9168 : : be remapped and thus DECL shouldn't be gimplified into its
9169 : : DECL_VALUE_EXPR (if any). */
9170 : :
9171 : : static bool
9172 : 3828207 : omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
9173 : : {
9174 : 3828207 : splay_tree_node n;
9175 : 3828207 : unsigned flags = in_code ? GOVD_SEEN : 0;
9176 : 3828207 : bool ret = false, shared;
9177 : :
9178 : 3828207 : if (error_operand_p (decl))
9179 : : return false;
9180 : :
9181 : 3828207 : if (DECL_ARTIFICIAL (decl))
9182 : : {
9183 : 2192205 : tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
9184 : 2192205 : if (attr)
9185 : 531 : decl = TREE_VALUE (TREE_VALUE (attr));
9186 : : }
9187 : :
9188 : 3828207 : if (ctx->region_type == ORT_NONE)
9189 : 156 : return lang_hooks.decls.omp_disregard_value_expr (decl, false);
9190 : :
9191 : 3828051 : if (is_global_var (decl))
9192 : : {
9193 : : /* Threadprivate variables are predetermined. */
9194 : 469933 : if (DECL_THREAD_LOCAL_P (decl))
9195 : 13565 : return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
9196 : :
9197 : 456368 : if (DECL_HAS_VALUE_EXPR_P (decl))
9198 : : {
9199 : 4380 : if (ctx->region_type & ORT_ACC)
9200 : : /* For OpenACC, defer expansion of value to avoid transfering
9201 : : privatized common block data instead of im-/explicitly transfered
9202 : : variables which are in common blocks. */
9203 : : ;
9204 : : else
9205 : : {
9206 : 2104 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
9207 : :
9208 : 2104 : if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
9209 : 618 : return omp_notice_threadprivate_variable (ctx, decl, value);
9210 : : }
9211 : : }
9212 : :
9213 : 455750 : if (gimplify_omp_ctxp->outer_context == NULL
9214 : 83703 : && VAR_P (decl)
9215 : 539453 : && oacc_get_fn_attrib (current_function_decl))
9216 : : {
9217 : 765 : location_t loc = DECL_SOURCE_LOCATION (decl);
9218 : :
9219 : 765 : if (lookup_attribute ("omp declare target link",
9220 : 765 : DECL_ATTRIBUTES (decl)))
9221 : : {
9222 : 48 : error_at (loc,
9223 : : "%qE with %<link%> clause used in %<routine%> function",
9224 : 24 : DECL_NAME (decl));
9225 : 24 : return false;
9226 : : }
9227 : 741 : else if (!lookup_attribute ("omp declare target",
9228 : 741 : DECL_ATTRIBUTES (decl)))
9229 : : {
9230 : 132 : error_at (loc,
9231 : : "%qE requires a %<declare%> directive for use "
9232 : 66 : "in a %<routine%> function", DECL_NAME (decl));
9233 : 66 : return false;
9234 : : }
9235 : : }
9236 : : }
9237 : :
9238 : 3813778 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9239 : 3813778 : if ((ctx->region_type & ORT_TARGET) != 0)
9240 : : {
9241 : 773886 : if (n == NULL)
9242 : : {
9243 : 44906 : unsigned nflags = flags;
9244 : 44906 : if ((ctx->region_type & ORT_ACC) == 0)
9245 : : {
9246 : 33920 : bool is_declare_target = false;
9247 : 33920 : if (is_global_var (decl)
9248 : 33920 : && varpool_node::get_create (decl)->offloadable)
9249 : : {
9250 : 7004 : struct gimplify_omp_ctx *octx;
9251 : 7004 : for (octx = ctx->outer_context;
9252 : 7012 : octx; octx = octx->outer_context)
9253 : : {
9254 : 9 : n = splay_tree_lookup (octx->variables,
9255 : : (splay_tree_key)decl);
9256 : 9 : if (n
9257 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
9258 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9259 : : break;
9260 : : }
9261 : 7004 : is_declare_target = octx == NULL;
9262 : : }
9263 : 7004 : if (!is_declare_target)
9264 : : {
9265 : 26917 : int gdmk;
9266 : 26917 : enum omp_clause_defaultmap_kind kind;
9267 : 26917 : if (lang_hooks.decls.omp_allocatable_p (decl))
9268 : : gdmk = GDMK_ALLOCATABLE;
9269 : 26561 : else if (lang_hooks.decls.omp_scalar_target_p (decl))
9270 : : gdmk = GDMK_SCALAR_TARGET;
9271 : 26486 : else if (lang_hooks.decls.omp_scalar_p (decl, false))
9272 : : gdmk = GDMK_SCALAR;
9273 : 11525 : else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9274 : 11525 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9275 : 3164 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9276 : : == POINTER_TYPE)))
9277 : : gdmk = GDMK_POINTER;
9278 : : else
9279 : : gdmk = GDMK_AGGREGATE;
9280 : 26917 : kind = lang_hooks.decls.omp_predetermined_mapping (decl);
9281 : 26917 : if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
9282 : : {
9283 : 944 : if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
9284 : 101 : nflags |= GOVD_FIRSTPRIVATE;
9285 : 843 : else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
9286 : 843 : nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
9287 : : else
9288 : 0 : gcc_unreachable ();
9289 : : }
9290 : 25973 : else if (ctx->defaultmap[gdmk] == 0)
9291 : : {
9292 : 75 : tree d = lang_hooks.decls.omp_report_decl (decl);
9293 : 75 : error ("%qE not specified in enclosing %<target%>",
9294 : 75 : DECL_NAME (d));
9295 : 75 : inform (ctx->location, "enclosing %<target%>");
9296 : : }
9297 : 25898 : else if (ctx->defaultmap[gdmk]
9298 : 25898 : & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
9299 : 8938 : nflags |= ctx->defaultmap[gdmk];
9300 : 16960 : else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
9301 : : {
9302 : 42 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9303 : 42 : nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
9304 : : }
9305 : : else
9306 : : {
9307 : 16918 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9308 : 16918 : nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
9309 : : }
9310 : : }
9311 : : }
9312 : :
9313 : 44906 : struct gimplify_omp_ctx *octx = ctx->outer_context;
9314 : 44906 : if ((ctx->region_type & ORT_ACC) && octx)
9315 : : {
9316 : : /* Look in outer OpenACC contexts, to see if there's a
9317 : : data attribute for this variable. */
9318 : 3536 : omp_notice_variable (octx, decl, in_code);
9319 : :
9320 : 5308 : for (; octx; octx = octx->outer_context)
9321 : : {
9322 : 3884 : if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
9323 : : break;
9324 : 3884 : splay_tree_node n2
9325 : 3884 : = splay_tree_lookup (octx->variables,
9326 : : (splay_tree_key) decl);
9327 : 3884 : if (n2)
9328 : : {
9329 : 2112 : if (octx->region_type == ORT_ACC_HOST_DATA)
9330 : 4 : error ("variable %qE declared in enclosing "
9331 : 4 : "%<host_data%> region", DECL_NAME (decl));
9332 : 2112 : nflags |= GOVD_MAP;
9333 : 2112 : if (octx->region_type == ORT_ACC_DATA
9334 : 2108 : && (n2->value & GOVD_MAP_0LEN_ARRAY))
9335 : 288 : nflags |= GOVD_MAP_0LEN_ARRAY;
9336 : 2112 : goto found_outer;
9337 : : }
9338 : : }
9339 : : }
9340 : :
9341 : 42794 : if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
9342 : : | GOVD_MAP_ALLOC_ONLY)) == flags)
9343 : : {
9344 : 32870 : tree type = TREE_TYPE (decl);
9345 : 32870 : location_t loc = DECL_SOURCE_LOCATION (decl);
9346 : :
9347 : 32870 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9348 : 32870 : && omp_privatize_by_reference (decl))
9349 : 29 : type = TREE_TYPE (type);
9350 : :
9351 : 32870 : if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
9352 : : /* Check if TYPE can appear in a target region.
9353 : : verify_type_context has already issued an error if it
9354 : : can't. */
9355 : 0 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9356 : 32870 : else if (!omp_mappable_type (type))
9357 : : {
9358 : 8 : error ("%qD referenced in target region does not have "
9359 : : "a mappable type", decl);
9360 : 8 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9361 : : }
9362 : : else
9363 : : {
9364 : 32862 : if ((ctx->region_type & ORT_ACC) != 0)
9365 : 8870 : nflags = oacc_default_clause (ctx, decl, flags);
9366 : : else
9367 : 23992 : nflags |= GOVD_MAP;
9368 : : }
9369 : : }
9370 : 9924 : found_outer:
9371 : 44906 : omp_add_variable (ctx, decl, nflags);
9372 : 44906 : if (ctx->region_type & ORT_ACC)
9373 : : /* For OpenACC, as remarked above, defer expansion. */
9374 : : shared = false;
9375 : : else
9376 : 33920 : shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9377 : 44906 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9378 : : }
9379 : : else
9380 : : {
9381 : 728980 : if (ctx->region_type & ORT_ACC)
9382 : : /* For OpenACC, as remarked above, defer expansion. */
9383 : : shared = false;
9384 : : else
9385 : 432660 : shared = ((n->value | flags)
9386 : 432660 : & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9387 : 728980 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9388 : : /* If nothing changed, there's nothing left to do. */
9389 : 728980 : if ((n->value & flags) == flags)
9390 : : return ret;
9391 : 15757 : flags |= n->value;
9392 : 15757 : n->value = flags;
9393 : : }
9394 : 60663 : goto do_outer;
9395 : : }
9396 : :
9397 : 3039892 : if (n == NULL)
9398 : : {
9399 : 1180607 : if (ctx->region_type == ORT_WORKSHARE
9400 : : || ctx->region_type == ORT_TASKGROUP
9401 : 560382 : || ctx->region_type == ORT_SIMD
9402 : 288664 : || ctx->region_type == ORT_ACC
9403 : 92302 : || (ctx->region_type & ORT_TARGET_DATA) != 0)
9404 : 1132768 : goto do_outer;
9405 : :
9406 : 47839 : flags = omp_default_clause (ctx, decl, in_code, flags);
9407 : :
9408 : 47839 : if ((flags & GOVD_PRIVATE)
9409 : 47839 : && lang_hooks.decls.omp_private_outer_ref (decl))
9410 : 6 : flags |= GOVD_PRIVATE_OUTER_REF;
9411 : :
9412 : 47839 : omp_add_variable (ctx, decl, flags);
9413 : :
9414 : 47839 : shared = (flags & GOVD_SHARED) != 0;
9415 : 47839 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9416 : 47839 : goto do_outer;
9417 : : }
9418 : :
9419 : : /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
9420 : : lb, b or incr expressions, those shouldn't be turned into simd arrays. */
9421 : 1859285 : if (ctx->region_type == ORT_SIMD
9422 : 151155 : && ctx->in_for_exprs
9423 : 70 : && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
9424 : : == GOVD_PRIVATE))
9425 : 1859285 : flags &= ~GOVD_SEEN;
9426 : :
9427 : 1859285 : if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
9428 : 31697 : && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
9429 : 1890943 : && DECL_SIZE (decl))
9430 : : {
9431 : 31657 : tree size;
9432 : 31657 : if (!poly_int_tree_p (DECL_SIZE (decl)))
9433 : : {
9434 : 140 : splay_tree_node n2;
9435 : 140 : tree t = DECL_VALUE_EXPR (decl);
9436 : 140 : gcc_assert (INDIRECT_REF_P (t));
9437 : 140 : t = TREE_OPERAND (t, 0);
9438 : 140 : gcc_assert (DECL_P (t));
9439 : 140 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9440 : 140 : n2->value |= GOVD_SEEN;
9441 : : }
9442 : 31517 : else if (omp_privatize_by_reference (decl)
9443 : 4205 : && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
9444 : 35685 : && !poly_int_tree_p (size))
9445 : : {
9446 : 1324 : splay_tree_node n2;
9447 : 1324 : gcc_assert (DECL_P (size));
9448 : 1324 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
9449 : 1324 : if (n2)
9450 : 617 : omp_notice_variable (ctx, size, true);
9451 : : }
9452 : : }
9453 : :
9454 : 1859285 : if (ctx->region_type & ORT_ACC)
9455 : : /* For OpenACC, as remarked above, defer expansion. */
9456 : : shared = false;
9457 : : else
9458 : 1677452 : shared = ((flags | n->value) & GOVD_SHARED) != 0;
9459 : 1859285 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9460 : :
9461 : : /* If nothing changed, there's nothing left to do. */
9462 : 1859285 : if ((n->value & flags) == flags)
9463 : : return ret;
9464 : 31658 : flags |= n->value;
9465 : 31658 : n->value = flags;
9466 : :
9467 : 1272928 : do_outer:
9468 : : /* If the variable is private in the current context, then we don't
9469 : : need to propagate anything to an outer context. */
9470 : 1272928 : if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
9471 : : return ret;
9472 : 1262630 : if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9473 : : == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9474 : : return ret;
9475 : 1262570 : if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9476 : : | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9477 : : == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9478 : : return ret;
9479 : 1262570 : if (ctx->outer_context
9480 : 1262570 : && omp_notice_variable (ctx->outer_context, decl, in_code))
9481 : : return true;
9482 : : return ret;
9483 : : }
9484 : :
9485 : : /* Verify that DECL is private within CTX. If there's specific information
9486 : : to the contrary in the innermost scope, generate an error. */
9487 : :
9488 : : static bool
9489 : 50097 : omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
9490 : : {
9491 : 95802 : splay_tree_node n;
9492 : :
9493 : 95802 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9494 : 95802 : if (n != NULL)
9495 : : {
9496 : 17958 : if (n->value & GOVD_SHARED)
9497 : : {
9498 : 407 : if (ctx == gimplify_omp_ctxp)
9499 : : {
9500 : 0 : if (simd)
9501 : 0 : error ("iteration variable %qE is predetermined linear",
9502 : 0 : DECL_NAME (decl));
9503 : : else
9504 : 0 : error ("iteration variable %qE should be private",
9505 : 0 : DECL_NAME (decl));
9506 : 0 : n->value = GOVD_PRIVATE;
9507 : 0 : return true;
9508 : : }
9509 : : else
9510 : : return false;
9511 : : }
9512 : 17551 : else if ((n->value & GOVD_EXPLICIT) != 0
9513 : 9689 : && (ctx == gimplify_omp_ctxp
9514 : 326 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9515 : 206 : && gimplify_omp_ctxp->outer_context == ctx)))
9516 : : {
9517 : 9569 : if ((n->value & GOVD_FIRSTPRIVATE) != 0)
9518 : 4 : error ("iteration variable %qE should not be firstprivate",
9519 : 4 : DECL_NAME (decl));
9520 : 9565 : else if ((n->value & GOVD_REDUCTION) != 0)
9521 : 8 : error ("iteration variable %qE should not be reduction",
9522 : 8 : DECL_NAME (decl));
9523 : 9557 : else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
9524 : 58 : error ("iteration variable %qE should not be linear",
9525 : 58 : DECL_NAME (decl));
9526 : : }
9527 : 17551 : return (ctx == gimplify_omp_ctxp
9528 : 17551 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9529 : 19415 : && gimplify_omp_ctxp->outer_context == ctx));
9530 : : }
9531 : :
9532 : 77844 : if (ctx->region_type != ORT_WORKSHARE
9533 : : && ctx->region_type != ORT_TASKGROUP
9534 : 49438 : && ctx->region_type != ORT_SIMD
9535 : 36193 : && ctx->region_type != ORT_ACC)
9536 : : return false;
9537 : 53851 : else if (ctx->outer_context)
9538 : : return omp_is_private (ctx->outer_context, decl, simd);
9539 : : return false;
9540 : : }
9541 : :
9542 : : /* Return true if DECL is private within a parallel region
9543 : : that binds to the current construct's context or in parallel
9544 : : region's REDUCTION clause. */
9545 : :
9546 : : static bool
9547 : 11529 : omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
9548 : : {
9549 : 11601 : splay_tree_node n;
9550 : :
9551 : 11601 : do
9552 : : {
9553 : 11601 : ctx = ctx->outer_context;
9554 : 11601 : if (ctx == NULL)
9555 : : {
9556 : 2764 : if (is_global_var (decl))
9557 : : return false;
9558 : :
9559 : : /* References might be private, but might be shared too,
9560 : : when checking for copyprivate, assume they might be
9561 : : private, otherwise assume they might be shared. */
9562 : 1290 : if (copyprivate)
9563 : : return true;
9564 : :
9565 : 1244 : if (omp_privatize_by_reference (decl))
9566 : : return false;
9567 : :
9568 : : /* Treat C++ privatized non-static data members outside
9569 : : of the privatization the same. */
9570 : 1198 : if (omp_member_access_dummy_var (decl))
9571 : : return false;
9572 : :
9573 : : return true;
9574 : : }
9575 : :
9576 : 8837 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9577 : :
9578 : 8837 : if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
9579 : 1291 : && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
9580 : : {
9581 : 242 : if ((ctx->region_type & ORT_TARGET_DATA) != 0
9582 : 238 : || n == NULL
9583 : 230 : || (n->value & GOVD_MAP) == 0)
9584 : 12 : continue;
9585 : : return false;
9586 : : }
9587 : :
9588 : 7546 : if (n != NULL)
9589 : : {
9590 : 5346 : if ((n->value & GOVD_LOCAL) != 0
9591 : 5346 : && omp_member_access_dummy_var (decl))
9592 : : return false;
9593 : 5307 : return (n->value & GOVD_SHARED) == 0;
9594 : : }
9595 : :
9596 : 3249 : if (ctx->region_type == ORT_WORKSHARE
9597 : : || ctx->region_type == ORT_TASKGROUP
9598 : 3193 : || ctx->region_type == ORT_SIMD
9599 : 3189 : || ctx->region_type == ORT_ACC)
9600 : 60 : continue;
9601 : :
9602 : : break;
9603 : : }
9604 : : while (1);
9605 : : return false;
9606 : : }
9607 : :
9608 : : /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
9609 : :
9610 : : static tree
9611 : 3148 : find_decl_expr (tree *tp, int *walk_subtrees, void *data)
9612 : : {
9613 : 3148 : tree t = *tp;
9614 : :
9615 : : /* If this node has been visited, unmark it and keep looking. */
9616 : 3148 : if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
9617 : : return t;
9618 : :
9619 : 2724 : if (IS_TYPE_OR_DECL_P (t))
9620 : 452 : *walk_subtrees = 0;
9621 : : return NULL_TREE;
9622 : : }
9623 : :
9624 : :
9625 : : /* Gimplify the affinity clause but effectively ignore it.
9626 : : Generate:
9627 : : var = begin;
9628 : : if ((step > 1) ? var <= end : var > end)
9629 : : locatator_var_expr; */
9630 : :
9631 : : static void
9632 : 468 : gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
9633 : : {
9634 : 468 : tree last_iter = NULL_TREE;
9635 : 468 : tree last_bind = NULL_TREE;
9636 : 468 : tree label = NULL_TREE;
9637 : 468 : tree *last_body = NULL;
9638 : 1203 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9639 : 735 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
9640 : : {
9641 : 735 : tree t = OMP_CLAUSE_DECL (c);
9642 : 735 : if (OMP_ITERATOR_DECL_P (t))
9643 : : {
9644 : 389 : if (TREE_VALUE (t) == null_pointer_node)
9645 : 201 : continue;
9646 : 188 : if (TREE_PURPOSE (t) != last_iter)
9647 : : {
9648 : 127 : if (last_bind)
9649 : : {
9650 : 9 : append_to_statement_list (label, last_body);
9651 : 9 : gimplify_and_add (last_bind, pre_p);
9652 : 9 : last_bind = NULL_TREE;
9653 : : }
9654 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9655 : : {
9656 : 147 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9657 : : is_gimple_val, fb_rvalue) == GS_ERROR
9658 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9659 : : is_gimple_val, fb_rvalue) == GS_ERROR
9660 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9661 : : is_gimple_val, fb_rvalue) == GS_ERROR
9662 : 294 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9663 : : is_gimple_val, fb_rvalue)
9664 : : == GS_ERROR))
9665 : 0 : return;
9666 : : }
9667 : 127 : last_iter = TREE_PURPOSE (t);
9668 : 127 : tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9669 : 127 : last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
9670 : : NULL, block);
9671 : 127 : last_body = &BIND_EXPR_BODY (last_bind);
9672 : 127 : tree cond = NULL_TREE;
9673 : 127 : location_t loc = OMP_CLAUSE_LOCATION (c);
9674 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9675 : : {
9676 : 147 : tree var = TREE_VEC_ELT (it, 0);
9677 : 147 : tree begin = TREE_VEC_ELT (it, 1);
9678 : 147 : tree end = TREE_VEC_ELT (it, 2);
9679 : 147 : tree step = TREE_VEC_ELT (it, 3);
9680 : 147 : loc = DECL_SOURCE_LOCATION (var);
9681 : 147 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9682 : : var, begin);
9683 : 147 : append_to_statement_list_force (tem, last_body);
9684 : :
9685 : 147 : tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9686 : 147 : step, build_zero_cst (TREE_TYPE (step)));
9687 : 147 : tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
9688 : : var, end);
9689 : 147 : tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9690 : : var, end);
9691 : 147 : cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
9692 : : cond1, cond2, cond3);
9693 : 147 : if (cond)
9694 : 20 : cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
9695 : : boolean_type_node, cond, cond1);
9696 : : else
9697 : : cond = cond1;
9698 : : }
9699 : 127 : tree cont_label = create_artificial_label (loc);
9700 : 127 : label = build1 (LABEL_EXPR, void_type_node, cont_label);
9701 : 127 : tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9702 : : void_node,
9703 : : build_and_jump (&cont_label));
9704 : 127 : append_to_statement_list_force (tem, last_body);
9705 : : }
9706 : 188 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
9707 : : {
9708 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
9709 : : last_body);
9710 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
9711 : : }
9712 : 188 : if (error_operand_p (TREE_VALUE (t)))
9713 : : return;
9714 : 188 : append_to_statement_list_force (TREE_VALUE (t), last_body);
9715 : 188 : TREE_VALUE (t) = null_pointer_node;
9716 : : }
9717 : : else
9718 : : {
9719 : 346 : if (last_bind)
9720 : : {
9721 : 9 : append_to_statement_list (label, last_body);
9722 : 9 : gimplify_and_add (last_bind, pre_p);
9723 : 9 : last_bind = NULL_TREE;
9724 : : }
9725 : 346 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
9726 : : {
9727 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
9728 : : NULL, is_gimple_val, fb_rvalue);
9729 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
9730 : : }
9731 : 346 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
9732 : : return;
9733 : 346 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
9734 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
9735 : : return;
9736 : 346 : gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
9737 : : }
9738 : : }
9739 : 468 : if (last_bind)
9740 : : {
9741 : 109 : append_to_statement_list (label, last_body);
9742 : 109 : gimplify_and_add (last_bind, pre_p);
9743 : : }
9744 : : return;
9745 : : }
9746 : :
9747 : : /* Returns a tree expression containing the total iteration count of the
9748 : : OpenMP iterator IT. */
9749 : :
9750 : : static tree
9751 : 320 : compute_omp_iterator_count (tree it, gimple_seq *pre_p)
9752 : : {
9753 : 320 : tree tcnt = size_one_node;
9754 : 706 : for (; it; it = TREE_CHAIN (it))
9755 : : {
9756 : 386 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9757 : : is_gimple_val, fb_rvalue) == GS_ERROR
9758 : 386 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9759 : : is_gimple_val, fb_rvalue) == GS_ERROR
9760 : 386 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9761 : : is_gimple_val, fb_rvalue) == GS_ERROR
9762 : 772 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9763 : : is_gimple_val, fb_rvalue) == GS_ERROR))
9764 : 0 : return NULL_TREE;
9765 : 386 : tree var = TREE_VEC_ELT (it, 0);
9766 : 386 : tree begin = TREE_VEC_ELT (it, 1);
9767 : 386 : tree end = TREE_VEC_ELT (it, 2);
9768 : 386 : tree step = TREE_VEC_ELT (it, 3);
9769 : 386 : tree orig_step = TREE_VEC_ELT (it, 4);
9770 : 386 : tree type = TREE_TYPE (var);
9771 : 386 : tree stype = TREE_TYPE (step);
9772 : 386 : location_t loc = DECL_SOURCE_LOCATION (var);
9773 : 386 : tree endmbegin;
9774 : : /* Compute count for this iterator as
9775 : : orig_step > 0
9776 : : ? (begin < end ? (end - begin + (step - 1)) / step : 0)
9777 : : : (begin > end ? (end - begin + (step + 1)) / step : 0)
9778 : : and compute product of those for the entire clause. */
9779 : 386 : if (POINTER_TYPE_P (type))
9780 : 42 : endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR, stype, end, begin);
9781 : : else
9782 : 344 : endmbegin = fold_build2_loc (loc, MINUS_EXPR, type, end, begin);
9783 : 386 : tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype, step,
9784 : : build_int_cst (stype, 1));
9785 : 386 : tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
9786 : : build_int_cst (stype, 1));
9787 : 386 : tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
9788 : : unshare_expr (endmbegin), stepm1);
9789 : 386 : pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, pos, step);
9790 : 386 : tree neg = fold_build2_loc (loc, PLUS_EXPR, stype, endmbegin, stepp1);
9791 : 386 : if (TYPE_UNSIGNED (stype))
9792 : : {
9793 : 31 : neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
9794 : 31 : step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
9795 : : }
9796 : 386 : neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype, neg, step);
9797 : 386 : step = NULL_TREE;
9798 : 386 : tree cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, begin, end);
9799 : 386 : pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
9800 : : build_int_cst (stype, 0));
9801 : 386 : cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, end, begin);
9802 : 386 : neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
9803 : : build_int_cst (stype, 0));
9804 : 386 : tree osteptype = TREE_TYPE (orig_step);
9805 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9806 : : build_int_cst (osteptype, 0));
9807 : 386 : tree cnt = fold_build3_loc (loc, COND_EXPR, stype, cond, pos, neg);
9808 : 386 : cnt = fold_convert_loc (loc, sizetype, cnt);
9809 : 386 : if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
9810 : : fb_rvalue) == GS_ERROR)
9811 : : return NULL_TREE;
9812 : 386 : tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
9813 : : }
9814 : 320 : if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
9815 : : return NULL_TREE;
9816 : :
9817 : 320 : return tcnt;
9818 : : }
9819 : :
9820 : : /* Build loops iterating over the space defined by the OpenMP iterator IT.
9821 : : Returns a pointer to the BIND_EXPR_BODY in the innermost loop body.
9822 : : LAST_BIND is set to point to the BIND_EXPR containing the whole loop. */
9823 : :
9824 : : static tree *
9825 : 320 : build_omp_iterator_loop (tree it, gimple_seq *pre_p, tree *last_bind)
9826 : : {
9827 : 320 : if (*last_bind)
9828 : 31 : gimplify_and_add (*last_bind, pre_p);
9829 : 320 : tree block = TREE_VEC_ELT (it, 5);
9830 : 320 : *last_bind = build3 (BIND_EXPR, void_type_node,
9831 : 320 : BLOCK_VARS (block), NULL, block);
9832 : 320 : TREE_SIDE_EFFECTS (*last_bind) = 1;
9833 : 320 : tree *p = &BIND_EXPR_BODY (*last_bind);
9834 : 706 : for (; it; it = TREE_CHAIN (it))
9835 : : {
9836 : 386 : tree var = TREE_VEC_ELT (it, 0);
9837 : 386 : tree begin = TREE_VEC_ELT (it, 1);
9838 : 386 : tree end = TREE_VEC_ELT (it, 2);
9839 : 386 : tree step = TREE_VEC_ELT (it, 3);
9840 : 386 : tree orig_step = TREE_VEC_ELT (it, 4);
9841 : 386 : tree type = TREE_TYPE (var);
9842 : 386 : location_t loc = DECL_SOURCE_LOCATION (var);
9843 : : /* Emit:
9844 : : var = begin;
9845 : : goto cond_label;
9846 : : beg_label:
9847 : : ...
9848 : : var = var + step;
9849 : : cond_label:
9850 : : if (orig_step > 0) {
9851 : : if (var < end) goto beg_label;
9852 : : } else {
9853 : : if (var > end) goto beg_label;
9854 : : }
9855 : : for each iterator, with inner iterators added to
9856 : : the ... above. */
9857 : 386 : tree beg_label = create_artificial_label (loc);
9858 : 386 : tree cond_label = NULL_TREE;
9859 : 386 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, begin);
9860 : 386 : append_to_statement_list_force (tem, p);
9861 : 386 : tem = build_and_jump (&cond_label);
9862 : 386 : append_to_statement_list_force (tem, p);
9863 : 386 : tem = build1 (LABEL_EXPR, void_type_node, beg_label);
9864 : 386 : append_to_statement_list (tem, p);
9865 : 386 : tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9866 : : NULL_TREE, NULL_TREE);
9867 : 386 : TREE_SIDE_EFFECTS (bind) = 1;
9868 : 386 : SET_EXPR_LOCATION (bind, loc);
9869 : 386 : append_to_statement_list_force (bind, p);
9870 : 386 : if (POINTER_TYPE_P (type))
9871 : 42 : tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
9872 : : var, fold_convert_loc (loc, sizetype, step));
9873 : : else
9874 : 344 : tem = build2_loc (loc, PLUS_EXPR, type, var, step);
9875 : 386 : tem = build2_loc (loc, MODIFY_EXPR, void_type_node, var, tem);
9876 : 386 : append_to_statement_list_force (tem, p);
9877 : 386 : tem = build1 (LABEL_EXPR, void_type_node, cond_label);
9878 : 386 : append_to_statement_list (tem, p);
9879 : 386 : tree cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node, var, end);
9880 : 386 : tree pos = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9881 : : build_and_jump (&beg_label), void_node);
9882 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, var, end);
9883 : 386 : tree neg = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9884 : : build_and_jump (&beg_label), void_node);
9885 : 386 : tree osteptype = TREE_TYPE (orig_step);
9886 : 386 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node, orig_step,
9887 : : build_int_cst (osteptype, 0));
9888 : 386 : tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, pos, neg);
9889 : 386 : append_to_statement_list_force (tem, p);
9890 : 386 : p = &BIND_EXPR_BODY (bind);
9891 : : }
9892 : :
9893 : 320 : return p;
9894 : : }
9895 : :
9896 : :
9897 : : /* Callback for walk_tree to find a VAR_DECL (stored in DATA) in the
9898 : : tree TP. */
9899 : :
9900 : : static tree
9901 : 2306 : find_var_decl (tree *tp, int *, void *data)
9902 : : {
9903 : 2306 : if (*tp == (tree) data)
9904 : 206 : return *tp;
9905 : :
9906 : : return NULL_TREE;
9907 : : }
9908 : :
9909 : : /* Returns an element-by-element copy of OMP iterator tree IT. */
9910 : :
9911 : : static tree
9912 : 198 : copy_omp_iterator (tree it, int elem_count = -1)
9913 : : {
9914 : 198 : if (elem_count < 0)
9915 : 60 : elem_count = TREE_VEC_LENGTH (it);
9916 : 198 : tree new_it = make_tree_vec (elem_count);
9917 : 1386 : for (int i = 0; i < TREE_VEC_LENGTH (it); i++)
9918 : 1188 : TREE_VEC_ELT (new_it, i) = TREE_VEC_ELT (it, i);
9919 : :
9920 : 198 : return new_it;
9921 : : }
9922 : :
9923 : : /* Helper function for walk_tree in remap_omp_iterator_var. */
9924 : :
9925 : : static tree
9926 : 871 : remap_omp_iterator_var_1 (tree *tp, int *, void *data)
9927 : : {
9928 : 871 : tree old_var = ((tree *) data)[0];
9929 : 871 : tree new_var = ((tree *) data)[1];
9930 : :
9931 : 871 : if (*tp == old_var)
9932 : 92 : *tp = new_var;
9933 : 871 : return NULL_TREE;
9934 : : }
9935 : :
9936 : : /* Replace instances of OLD_VAR in TP with NEW_VAR. */
9937 : :
9938 : : static void
9939 : 184 : remap_omp_iterator_var (tree *tp, tree old_var, tree new_var)
9940 : : {
9941 : 184 : tree vars[2] = { old_var, new_var };
9942 : 184 : walk_tree (tp, remap_omp_iterator_var_1, vars, NULL);
9943 : 184 : }
9944 : :
9945 : : /* Scan through all clauses using OpenMP iterators in LIST_P. If any
9946 : : clauses have iterators with variables that are not used by the clause
9947 : : decl or size, issue a warning and replace the iterator with a copy with
9948 : : the unused variables removed. */
9949 : :
9950 : : static void
9951 : 24533 : remove_unused_omp_iterator_vars (tree *list_p)
9952 : : {
9953 : 24533 : auto_vec< vec<tree> > iter_vars;
9954 : 24533 : auto_vec<tree> new_iterators;
9955 : :
9956 : 65970 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9957 : : {
9958 : 41437 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
9959 : 41381 : continue;
9960 : 146 : auto_vec<tree> vars;
9961 : 146 : bool need_new_iterators = false;
9962 : 416 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
9963 : : {
9964 : 270 : tree var = TREE_VEC_ELT (it, 0);
9965 : 270 : tree t = walk_tree (&OMP_CLAUSE_DECL (c), find_var_decl, var, NULL);
9966 : 270 : if (t == NULL_TREE)
9967 : 126 : t = walk_tree (&OMP_CLAUSE_SIZE (c), find_var_decl, var, NULL);
9968 : 126 : if (t == NULL_TREE)
9969 : : {
9970 : 64 : need_new_iterators = true;
9971 : 64 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9972 : 44 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO
9973 : 28 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FROM))
9974 : 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
9975 : 92 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
9976 : 40 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
9977 : : "iterator variable %qE not used in clause "
9978 : 40 : "expression", DECL_NAME (var));
9979 : : }
9980 : : else
9981 : 206 : vars.safe_push (var);
9982 : : }
9983 : 146 : if (!need_new_iterators)
9984 : 82 : continue;
9985 : 64 : if (need_new_iterators && vars.is_empty ())
9986 : : {
9987 : : /* No iteration variables are used in the clause - remove the
9988 : : iterator from the clause. */
9989 : 8 : OMP_CLAUSE_ITERATORS (c) = NULL_TREE;
9990 : 8 : continue;
9991 : : }
9992 : :
9993 : : /* If a new iterator has been created for the current set of used
9994 : : iterator variables, then use that as the iterator. Otherwise,
9995 : : create a new iterator for the current iterator variable set. */
9996 : : unsigned i;
9997 : 92 : for (i = 0; i < iter_vars.length (); i++)
9998 : : {
9999 : 112 : if (vars.length () != iter_vars[i].length ())
10000 : 0 : continue;
10001 : : bool identical_p = true;
10002 : 136 : for (unsigned j = 0; j < vars.length () && identical_p; j++)
10003 : 80 : identical_p = vars[j] == iter_vars[i][j];
10004 : :
10005 : 56 : if (identical_p)
10006 : : break;
10007 : : }
10008 : 56 : if (i < iter_vars.length ())
10009 : 20 : OMP_CLAUSE_ITERATORS (c) = new_iterators[i];
10010 : : else
10011 : : {
10012 : 36 : tree new_iters = NULL_TREE;
10013 : 36 : tree *new_iters_p = &new_iters;
10014 : 36 : tree new_vars = NULL_TREE;
10015 : 36 : tree *new_vars_p = &new_vars;
10016 : 36 : i = 0;
10017 : 112 : for (tree it = OMP_CLAUSE_ITERATORS (c); it && i < vars.length();
10018 : 76 : it = TREE_CHAIN (it))
10019 : : {
10020 : 76 : tree var = TREE_VEC_ELT (it, 0);
10021 : 76 : if (var == vars[i])
10022 : : {
10023 : 60 : *new_iters_p = copy_omp_iterator (it);
10024 : 60 : *new_vars_p = build_decl (OMP_CLAUSE_LOCATION (c), VAR_DECL,
10025 : 60 : DECL_NAME (var), TREE_TYPE (var));
10026 : 60 : DECL_ARTIFICIAL (*new_vars_p) = 1;
10027 : 60 : DECL_CONTEXT (*new_vars_p) = DECL_CONTEXT (var);
10028 : 60 : TREE_VEC_ELT (*new_iters_p, 0) = *new_vars_p;
10029 : 60 : new_iters_p = &TREE_CHAIN (*new_iters_p);
10030 : 60 : new_vars_p = &DECL_CHAIN (*new_vars_p);
10031 : 60 : i++;
10032 : : }
10033 : : }
10034 : 36 : tree new_block = make_node (BLOCK);
10035 : 36 : BLOCK_VARS (new_block) = new_vars;
10036 : 36 : TREE_VEC_ELT (new_iters, 5) = new_block;
10037 : 36 : new_iterators.safe_push (new_iters);
10038 : 36 : iter_vars.safe_push (vars.copy ());
10039 : 36 : OMP_CLAUSE_ITERATORS (c) = new_iters;
10040 : : }
10041 : :
10042 : : /* Remap clause to use the new variables. */
10043 : 56 : i = 0;
10044 : 148 : for (tree it = OMP_CLAUSE_ITERATORS (c); it; it = TREE_CHAIN (it))
10045 : : {
10046 : 92 : tree old_var = vars[i++];
10047 : 92 : tree new_var = TREE_VEC_ELT (it, 0);
10048 : 92 : remap_omp_iterator_var (&OMP_CLAUSE_DECL (c), old_var, new_var);
10049 : 92 : remap_omp_iterator_var (&OMP_CLAUSE_SIZE (c), old_var, new_var);
10050 : : }
10051 : 146 : }
10052 : :
10053 : 24569 : for (unsigned i = 0; i < iter_vars.length (); i++)
10054 : 36 : iter_vars[i].release ();
10055 : 24533 : }
10056 : :
10057 : 140 : struct iterator_loop_info_t
10058 : : {
10059 : : tree bind;
10060 : : tree count;
10061 : : tree index;
10062 : : tree body_label;
10063 : : auto_vec<tree> clauses;
10064 : : };
10065 : :
10066 : : typedef hash_map<tree, iterator_loop_info_t> iterator_loop_info_map_t;
10067 : :
10068 : : /* Builds a loop to expand any OpenMP iterators in the clauses in LIST_P,
10069 : : reusing any previously built loops if they use the same set of iterators.
10070 : : Generated Gimple statements are placed into LOOPS_SEQ_P. The clause
10071 : : iterators are updated with information on how and where to insert code into
10072 : : the loop body. */
10073 : :
10074 : : static void
10075 : 24533 : build_omp_iterators_loops (tree *list_p, gimple_seq *loops_seq_p)
10076 : : {
10077 : 24533 : iterator_loop_info_map_t loops;
10078 : :
10079 : 65970 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10080 : : {
10081 : 41437 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10082 : 41299 : continue;
10083 : :
10084 : 138 : bool built_p;
10085 : 138 : iterator_loop_info_t &loop
10086 : 138 : = loops.get_or_insert (OMP_CLAUSE_ITERATORS (c), &built_p);
10087 : :
10088 : 138 : if (!built_p)
10089 : : {
10090 : 70 : loop.count = compute_omp_iterator_count (OMP_CLAUSE_ITERATORS (c),
10091 : : loops_seq_p);
10092 : 70 : if (!loop.count)
10093 : 0 : continue;
10094 : 70 : if (integer_zerop (loop.count))
10095 : 8 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
10096 : : "iteration count is zero");
10097 : :
10098 : 70 : loop.bind = NULL_TREE;
10099 : 70 : tree *body = build_omp_iterator_loop (OMP_CLAUSE_ITERATORS (c),
10100 : : loops_seq_p, &loop.bind);
10101 : :
10102 : 70 : loop.index = create_tmp_var (sizetype);
10103 : 70 : SET_EXPR_LOCATION (loop.bind, OMP_CLAUSE_LOCATION (c));
10104 : :
10105 : : /* BEFORE LOOP: */
10106 : : /* idx = -1; */
10107 : : /* This should be initialized to before the individual elements,
10108 : : as idx is pre-incremented in the loop body. */
10109 : 70 : gimple *assign = gimple_build_assign (loop.index, size_int (-1));
10110 : 70 : gimple_seq_add_stmt (loops_seq_p, assign);
10111 : :
10112 : : /* IN LOOP BODY: */
10113 : : /* Create a label so we can find this point later. */
10114 : 70 : loop.body_label = create_artificial_label (OMP_CLAUSE_LOCATION (c));
10115 : 70 : tree tem = build1 (LABEL_EXPR, void_type_node, loop.body_label);
10116 : 70 : append_to_statement_list_force (tem, body);
10117 : :
10118 : : /* idx += 2; */
10119 : 70 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10120 : : void_type_node, loop.index,
10121 : : size_binop (PLUS_EXPR, loop.index, size_int (2)));
10122 : 70 : append_to_statement_list_force (tem, body);
10123 : : }
10124 : :
10125 : : /* Create array to hold expanded values. */
10126 : 138 : tree last_count_2 = size_binop (MULT_EXPR, loop.count, size_int (2));
10127 : 138 : tree arr_length = size_binop (PLUS_EXPR, last_count_2, size_int (1));
10128 : 138 : tree elems = NULL_TREE;
10129 : 138 : if (TREE_CONSTANT (arr_length))
10130 : : {
10131 : 138 : tree type = build_array_type (ptr_type_node,
10132 : : build_index_type (arr_length));
10133 : 138 : elems = create_tmp_var_raw (type, "omp_iter_data");
10134 : 138 : TREE_ADDRESSABLE (elems) = 1;
10135 : 138 : gimple_add_tmp_var (elems);
10136 : : }
10137 : : else
10138 : : {
10139 : : /* Handle dynamic sizes. */
10140 : 0 : sorry ("dynamic iterator sizes not implemented yet");
10141 : : }
10142 : :
10143 : : /* BEFORE LOOP: */
10144 : : /* elems[0] = count; */
10145 : 138 : tree lhs = build4 (ARRAY_REF, ptr_type_node, elems, size_int (0),
10146 : : NULL_TREE, NULL_TREE);
10147 : 138 : tree tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10148 : : void_type_node, lhs, loop.count);
10149 : 138 : gimplify_and_add (tem, loops_seq_p);
10150 : :
10151 : : /* Make a copy of the iterator with extra info at the end. */
10152 : 138 : int elem_count = TREE_VEC_LENGTH (OMP_CLAUSE_ITERATORS (c));
10153 : 138 : tree new_iterator = copy_omp_iterator (OMP_CLAUSE_ITERATORS (c),
10154 : : elem_count + 3);
10155 : 138 : TREE_VEC_ELT (new_iterator, elem_count) = loop.body_label;
10156 : 138 : TREE_VEC_ELT (new_iterator, elem_count + 1) = elems;
10157 : 138 : TREE_VEC_ELT (new_iterator, elem_count + 2) = loop.index;
10158 : 138 : TREE_CHAIN (new_iterator) = TREE_CHAIN (OMP_CLAUSE_ITERATORS (c));
10159 : 138 : OMP_CLAUSE_ITERATORS (c) = new_iterator;
10160 : :
10161 : 138 : loop.clauses.safe_push (c);
10162 : : }
10163 : :
10164 : : /* Now gimplify and add all the loops that were built. */
10165 : 24603 : for (hash_map<tree, iterator_loop_info_t>::iterator it = loops.begin ();
10166 : 49206 : it != loops.end (); ++it)
10167 : 70 : gimplify_and_add ((*it).second.bind, loops_seq_p);
10168 : 24533 : }
10169 : :
10170 : : /* Helper function for enter_omp_iterator_loop_context. */
10171 : :
10172 : : static gimple_seq *
10173 : 1586 : enter_omp_iterator_loop_context_1 (tree iterator, gimple_seq *loops_seq_p)
10174 : : {
10175 : : /* Drill into the nested bind expressions to get to the loop body. */
10176 : 1586 : for (gimple_stmt_iterator gsi = gsi_start (*loops_seq_p);
10177 : 9923 : !gsi_end_p (gsi); gsi_next (&gsi))
10178 : : {
10179 : 9507 : gimple *stmt = gsi_stmt (gsi);
10180 : :
10181 : 9507 : switch (gimple_code (stmt))
10182 : : {
10183 : 1256 : case GIMPLE_BIND:
10184 : 1256 : {
10185 : 1256 : gbind *bind_stmt = as_a<gbind *> (stmt);
10186 : 1256 : gimple_push_bind_expr (bind_stmt);
10187 : 1256 : gimple_seq *bind_body_p = gimple_bind_body_ptr (bind_stmt);
10188 : 1256 : gimple_seq *seq =
10189 : 1256 : enter_omp_iterator_loop_context_1 (iterator, bind_body_p);
10190 : 1256 : if (seq)
10191 : : return seq;
10192 : 416 : gimple_pop_bind_expr ();
10193 : : }
10194 : 416 : break;
10195 : 0 : case GIMPLE_TRY:
10196 : 0 : {
10197 : 0 : gimple_seq *try_eval_p = gimple_try_eval_ptr (stmt);
10198 : 0 : gimple_seq *seq =
10199 : 0 : enter_omp_iterator_loop_context_1 (iterator, try_eval_p);
10200 : 0 : if (seq)
10201 : : return seq;
10202 : : }
10203 : : break;
10204 : 1790 : case GIMPLE_LABEL:
10205 : 1790 : {
10206 : 1790 : glabel *label_stmt = as_a<glabel *> (stmt);
10207 : 1790 : tree label = gimple_label_label (label_stmt);
10208 : 1790 : if (label == TREE_VEC_ELT (iterator, 6))
10209 : : return loops_seq_p;
10210 : : }
10211 : : break;
10212 : : default:
10213 : : break;
10214 : : }
10215 : : }
10216 : :
10217 : : return NULL;
10218 : : }
10219 : :
10220 : : /* Enter the Gimplification context in LOOPS_SEQ_P for the iterator loop
10221 : : associated with OpenMP clause C. Returns the gimple_seq for the loop body
10222 : : if C has OpenMP iterators, or ALT_SEQ_P if not. */
10223 : :
10224 : : static gimple_seq *
10225 : 61577 : enter_omp_iterator_loop_context (tree c, gimple_seq *loops_seq_p,
10226 : : gimple_seq *alt_seq_p)
10227 : : {
10228 : 61577 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10229 : : return alt_seq_p;
10230 : :
10231 : 330 : push_gimplify_context ();
10232 : :
10233 : 330 : gimple_seq *seq = enter_omp_iterator_loop_context_1 (OMP_CLAUSE_ITERATORS (c),
10234 : : loops_seq_p);
10235 : 330 : gcc_assert (seq);
10236 : : return seq;
10237 : : }
10238 : :
10239 : : /* Enter the Gimplification context in STMT for the iterator loop associated
10240 : : with OpenMP clause C. Returns the gimple_seq for the loop body if C has
10241 : : OpenMP iterators, or ALT_SEQ_P if not. */
10242 : :
10243 : : gimple_seq *
10244 : 192 : enter_omp_iterator_loop_context (tree c, gomp_target *stmt,
10245 : : gimple_seq *alt_seq_p)
10246 : : {
10247 : 192 : gimple_seq *loops_seq_p = gimple_omp_target_iterator_loops_ptr (stmt);
10248 : 192 : return enter_omp_iterator_loop_context (c, loops_seq_p, alt_seq_p);
10249 : : }
10250 : :
10251 : : /* Exit the Gimplification context for the OpenMP clause C. */
10252 : :
10253 : : void
10254 : 61577 : exit_omp_iterator_loop_context (tree c)
10255 : : {
10256 : 61577 : if (!OMP_CLAUSE_HAS_ITERATORS (c))
10257 : : return;
10258 : 1170 : while (!gimplify_ctxp->bind_expr_stack.is_empty ())
10259 : 840 : gimple_pop_bind_expr ();
10260 : 330 : pop_gimplify_context (NULL);
10261 : : }
10262 : :
10263 : : /* If *LIST_P contains any OpenMP depend clauses with iterators,
10264 : : lower all the depend clauses by populating corresponding depend
10265 : : array. Returns 0 if there are no such depend clauses, or
10266 : : 2 if all depend clauses should be removed, 1 otherwise. */
10267 : :
10268 : : static int
10269 : 1898 : gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
10270 : : {
10271 : 1898 : tree c;
10272 : 1898 : gimple *g;
10273 : 1898 : size_t n[5] = { 0, 0, 0, 0, 0 };
10274 : 1898 : bool unused[5];
10275 : 1898 : tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
10276 : 1898 : tree last_iter = NULL_TREE, last_count = NULL_TREE;
10277 : 1898 : size_t i, j;
10278 : 1898 : location_t first_loc = UNKNOWN_LOCATION;
10279 : :
10280 : 6236 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10281 : 4338 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10282 : : {
10283 : 2200 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10284 : : {
10285 : : case OMP_CLAUSE_DEPEND_IN:
10286 : : i = 2;
10287 : : break;
10288 : : case OMP_CLAUSE_DEPEND_OUT:
10289 : : case OMP_CLAUSE_DEPEND_INOUT:
10290 : : i = 0;
10291 : : break;
10292 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10293 : : i = 1;
10294 : : break;
10295 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
10296 : : i = 3;
10297 : : break;
10298 : : case OMP_CLAUSE_DEPEND_INOUTSET:
10299 : : i = 4;
10300 : : break;
10301 : 0 : default:
10302 : 0 : gcc_unreachable ();
10303 : : }
10304 : 2200 : tree t = OMP_CLAUSE_DECL (c);
10305 : 2200 : if (first_loc == UNKNOWN_LOCATION)
10306 : 1898 : first_loc = OMP_CLAUSE_LOCATION (c);
10307 : 2200 : if (OMP_ITERATOR_DECL_P (t))
10308 : : {
10309 : 297 : if (TREE_PURPOSE (t) != last_iter)
10310 : : {
10311 : 250 : tree tcnt = compute_omp_iterator_count (TREE_PURPOSE (t),
10312 : : pre_p);
10313 : 250 : if (!tcnt)
10314 : : return 2;
10315 : 250 : last_iter = TREE_PURPOSE (t);
10316 : 250 : last_count = tcnt;
10317 : : }
10318 : 297 : if (counts[i] == NULL_TREE)
10319 : 243 : counts[i] = last_count;
10320 : : else
10321 : 54 : counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
10322 : : PLUS_EXPR, counts[i], last_count);
10323 : : }
10324 : : else
10325 : 1903 : n[i]++;
10326 : : }
10327 : 10601 : for (i = 0; i < 5; i++)
10328 : 8908 : if (counts[i])
10329 : : break;
10330 : 1898 : if (i == 5)
10331 : : return 0;
10332 : :
10333 : 205 : tree total = size_zero_node;
10334 : 1230 : for (i = 0; i < 5; i++)
10335 : : {
10336 : 1025 : unused[i] = counts[i] == NULL_TREE && n[i] == 0;
10337 : 1025 : if (counts[i] == NULL_TREE)
10338 : 782 : counts[i] = size_zero_node;
10339 : 1025 : if (n[i])
10340 : 44 : counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
10341 : 1025 : if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
10342 : : fb_rvalue) == GS_ERROR)
10343 : : return 2;
10344 : 1025 : total = size_binop (PLUS_EXPR, total, counts[i]);
10345 : : }
10346 : :
10347 : 205 : if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
10348 : : == GS_ERROR)
10349 : : return 2;
10350 : 205 : bool is_old = unused[1] && unused[3] && unused[4];
10351 : 205 : tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
10352 : : size_int (is_old ? 1 : 4));
10353 : 205 : if (!unused[4])
10354 : 3 : totalpx = size_binop (PLUS_EXPR, totalpx,
10355 : : size_binop (MULT_EXPR, counts[4], size_int (2)));
10356 : 205 : tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
10357 : 205 : tree array = create_tmp_var_raw (type);
10358 : 205 : TREE_ADDRESSABLE (array) = 1;
10359 : 205 : if (!poly_int_tree_p (totalpx))
10360 : : {
10361 : 81 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
10362 : 81 : gimplify_type_sizes (TREE_TYPE (array), pre_p);
10363 : 81 : if (gimplify_omp_ctxp)
10364 : : {
10365 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10366 : : while (ctx
10367 : 20 : && (ctx->region_type == ORT_WORKSHARE
10368 : : || ctx->region_type == ORT_TASKGROUP
10369 : 20 : || ctx->region_type == ORT_SIMD
10370 : 20 : || ctx->region_type == ORT_ACC))
10371 : 0 : ctx = ctx->outer_context;
10372 : 20 : if (ctx)
10373 : 20 : omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
10374 : : }
10375 : 81 : gimplify_vla_decl (array, pre_p);
10376 : : }
10377 : : else
10378 : 124 : gimple_add_tmp_var (array);
10379 : 205 : tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
10380 : : NULL_TREE);
10381 : 205 : tree tem;
10382 : 205 : if (!is_old)
10383 : : {
10384 : 31 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10385 : : build_int_cst (ptr_type_node, 0));
10386 : 31 : gimplify_and_add (tem, pre_p);
10387 : 31 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
10388 : : NULL_TREE);
10389 : : }
10390 : 205 : tem = build2 (MODIFY_EXPR, void_type_node, r,
10391 : : fold_convert (ptr_type_node, total));
10392 : 205 : gimplify_and_add (tem, pre_p);
10393 : 801 : for (i = 1; i < (is_old ? 2 : 4); i++)
10394 : : {
10395 : 267 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
10396 : : NULL_TREE, NULL_TREE);
10397 : 267 : tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
10398 : 267 : gimplify_and_add (tem, pre_p);
10399 : : }
10400 : :
10401 : : tree cnts[6];
10402 : 717 : for (j = 5; j; j--)
10403 : 717 : if (!unused[j - 1])
10404 : : break;
10405 : 1230 : for (i = 0; i < 5; i++)
10406 : : {
10407 : 1025 : if (i && (i >= j || unused[i - 1]))
10408 : : {
10409 : 770 : cnts[i] = cnts[i - 1];
10410 : 770 : continue;
10411 : : }
10412 : 255 : cnts[i] = create_tmp_var (sizetype);
10413 : 255 : if (i == 0)
10414 : 236 : g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
10415 : : else
10416 : : {
10417 : 50 : tree t;
10418 : 50 : if (is_old)
10419 : 38 : t = size_binop (PLUS_EXPR, counts[0], size_int (2));
10420 : : else
10421 : 12 : t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
10422 : 50 : if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
10423 : : == GS_ERROR)
10424 : 0 : return 2;
10425 : 50 : g = gimple_build_assign (cnts[i], t);
10426 : : }
10427 : 255 : gimple_seq_add_stmt (pre_p, g);
10428 : : }
10429 : 205 : if (unused[4])
10430 : 202 : cnts[5] = NULL_TREE;
10431 : : else
10432 : : {
10433 : 3 : tree t = size_binop (PLUS_EXPR, total, size_int (5));
10434 : 3 : cnts[5] = create_tmp_var (sizetype);
10435 : 3 : g = gimple_build_assign (cnts[i], t);
10436 : 3 : gimple_seq_add_stmt (pre_p, g);
10437 : : }
10438 : :
10439 : 205 : last_iter = NULL_TREE;
10440 : 205 : tree last_bind = NULL_TREE;
10441 : 205 : tree *last_body = NULL;
10442 : 563 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
10443 : 358 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
10444 : : {
10445 : 343 : switch (OMP_CLAUSE_DEPEND_KIND (c))
10446 : : {
10447 : : case OMP_CLAUSE_DEPEND_IN:
10448 : : i = 2;
10449 : : break;
10450 : : case OMP_CLAUSE_DEPEND_OUT:
10451 : : case OMP_CLAUSE_DEPEND_INOUT:
10452 : : i = 0;
10453 : : break;
10454 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
10455 : : i = 1;
10456 : : break;
10457 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
10458 : : i = 3;
10459 : : break;
10460 : : case OMP_CLAUSE_DEPEND_INOUTSET:
10461 : : i = 4;
10462 : : break;
10463 : 0 : default:
10464 : 0 : gcc_unreachable ();
10465 : : }
10466 : 343 : tree t = OMP_CLAUSE_DECL (c);
10467 : 343 : if (OMP_ITERATOR_DECL_P (t))
10468 : : {
10469 : 297 : if (TREE_PURPOSE (t) != last_iter)
10470 : : {
10471 : 250 : last_body = build_omp_iterator_loop (TREE_PURPOSE (t), pre_p,
10472 : : &last_bind);
10473 : 250 : SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
10474 : : }
10475 : 297 : last_iter = TREE_PURPOSE (t);
10476 : 297 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
10477 : : {
10478 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
10479 : : 0), last_body);
10480 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
10481 : : }
10482 : 297 : if (error_operand_p (TREE_VALUE (t)))
10483 : : return 2;
10484 : 297 : if (TREE_VALUE (t) != null_pointer_node)
10485 : 291 : TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
10486 : 297 : if (i == 4)
10487 : : {
10488 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10489 : : NULL_TREE, NULL_TREE);
10490 : 3 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10491 : : NULL_TREE, NULL_TREE);
10492 : 3 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10493 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10494 : : void_type_node, r, r2);
10495 : 3 : append_to_statement_list_force (tem, last_body);
10496 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10497 : : void_type_node, cnts[i],
10498 : : size_binop (PLUS_EXPR, cnts[i],
10499 : : size_int (1)));
10500 : 3 : append_to_statement_list_force (tem, last_body);
10501 : 3 : i = 5;
10502 : : }
10503 : 297 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10504 : : NULL_TREE, NULL_TREE);
10505 : 594 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10506 : 297 : void_type_node, r, TREE_VALUE (t));
10507 : 297 : append_to_statement_list_force (tem, last_body);
10508 : 297 : if (i == 5)
10509 : : {
10510 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array,
10511 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10512 : : NULL_TREE, NULL_TREE);
10513 : 3 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10514 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10515 : : void_type_node, r, tem);
10516 : 3 : append_to_statement_list_force (tem, last_body);
10517 : : }
10518 : 297 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10519 : : void_type_node, cnts[i],
10520 : 297 : size_binop (PLUS_EXPR, cnts[i],
10521 : : size_int (1 + (i == 5))));
10522 : 297 : append_to_statement_list_force (tem, last_body);
10523 : 297 : TREE_VALUE (t) = null_pointer_node;
10524 : : }
10525 : : else
10526 : : {
10527 : 46 : if (last_bind)
10528 : : {
10529 : 18 : gimplify_and_add (last_bind, pre_p);
10530 : 18 : last_bind = NULL_TREE;
10531 : : }
10532 : 46 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
10533 : : {
10534 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
10535 : : NULL, is_gimple_val, fb_rvalue);
10536 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
10537 : : }
10538 : 46 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
10539 : : return 2;
10540 : 46 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
10541 : 46 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
10542 : 46 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
10543 : : is_gimple_val, fb_rvalue) == GS_ERROR)
10544 : : return 2;
10545 : 46 : if (i == 4)
10546 : : {
10547 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10548 : : NULL_TREE, NULL_TREE);
10549 : 0 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10550 : : NULL_TREE, NULL_TREE);
10551 : 0 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10552 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
10553 : 0 : gimplify_and_add (tem, pre_p);
10554 : 0 : g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
10555 : : cnts[i],
10556 : : size_int (1)));
10557 : 0 : gimple_seq_add_stmt (pre_p, g);
10558 : 0 : i = 5;
10559 : : }
10560 : 46 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10561 : : NULL_TREE, NULL_TREE);
10562 : 46 : tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
10563 : 46 : gimplify_and_add (tem, pre_p);
10564 : 46 : if (i == 5)
10565 : : {
10566 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array,
10567 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10568 : : NULL_TREE, NULL_TREE);
10569 : 0 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10570 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
10571 : 0 : append_to_statement_list_force (tem, last_body);
10572 : 0 : gimplify_and_add (tem, pre_p);
10573 : : }
10574 : 46 : g = gimple_build_assign (cnts[i],
10575 : 46 : size_binop (PLUS_EXPR, cnts[i],
10576 : : size_int (1 + (i == 5))));
10577 : 46 : gimple_seq_add_stmt (pre_p, g);
10578 : : }
10579 : : }
10580 : 205 : if (last_bind)
10581 : 201 : gimplify_and_add (last_bind, pre_p);
10582 : 205 : tree cond = boolean_false_node;
10583 : 205 : if (is_old)
10584 : : {
10585 : 174 : if (!unused[0])
10586 : 88 : cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
10587 : : size_binop_loc (first_loc, PLUS_EXPR, counts[0],
10588 : : size_int (2)));
10589 : 174 : if (!unused[2])
10590 : 124 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10591 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10592 : : cnts[2],
10593 : : size_binop_loc (first_loc, PLUS_EXPR,
10594 : : totalpx,
10595 : : size_int (1))));
10596 : : }
10597 : : else
10598 : : {
10599 : 31 : tree prev = size_int (5);
10600 : 186 : for (i = 0; i < 5; i++)
10601 : : {
10602 : 155 : if (unused[i])
10603 : 112 : continue;
10604 : 43 : prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
10605 : 43 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10606 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10607 : : cnts[i], unshare_expr (prev)));
10608 : : }
10609 : : }
10610 : 205 : tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
10611 : : build_call_expr_loc (first_loc,
10612 : : builtin_decl_explicit (BUILT_IN_TRAP),
10613 : : 0), void_node);
10614 : 205 : gimplify_and_add (tem, pre_p);
10615 : 205 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
10616 : 205 : OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
10617 : 205 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
10618 : 205 : OMP_CLAUSE_CHAIN (c) = *list_p;
10619 : 205 : *list_p = c;
10620 : 205 : return 1;
10621 : : }
10622 : :
10623 : : /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor. */
10624 : :
10625 : : static bool
10626 : 113490 : omp_map_clause_descriptor_p (tree c)
10627 : : {
10628 : 113490 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10629 : : return false;
10630 : :
10631 : 113488 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
10632 : : return true;
10633 : :
10634 : 74915 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
10635 : 69243 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
10636 : 75355 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
10637 : 4042 : return true;
10638 : :
10639 : : return false;
10640 : : }
10641 : :
10642 : : /* For a set of mappings describing an array section pointed to by a struct
10643 : : (or derived type, etc.) component, create an "alloc" or "release" node to
10644 : : insert into a list following a GOMP_MAP_STRUCT node. For some types of
10645 : : mapping (e.g. Fortran arrays with descriptors), an additional mapping may
10646 : : be created that is inserted into the list of mapping nodes attached to the
10647 : : directive being processed -- not part of the sorted list of nodes after
10648 : : GOMP_MAP_STRUCT.
10649 : :
10650 : : CODE is the code of the directive being processed. GRP_START and GRP_END
10651 : : are the first and last of two or three nodes representing this array section
10652 : : mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
10653 : : GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER). EXTRA_NODE is
10654 : : filled with the additional node described above, if needed.
10655 : :
10656 : : This function does not add the new nodes to any lists itself. It is the
10657 : : responsibility of the caller to do that. */
10658 : :
10659 : : static tree
10660 : 1718 : build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
10661 : : tree *extra_node)
10662 : : {
10663 : 1525 : enum gomp_map_kind mkind
10664 : 1718 : = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
10665 : 1718 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
10666 : :
10667 : 1718 : gcc_assert (grp_start != grp_end);
10668 : :
10669 : 1718 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10670 : 1718 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
10671 : 1718 : OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
10672 : 1718 : OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
10673 : 1718 : tree grp_mid = NULL_TREE;
10674 : 1718 : if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
10675 : 185 : grp_mid = OMP_CLAUSE_CHAIN (grp_start);
10676 : :
10677 : 185 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
10678 : 0 : OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
10679 : : else
10680 : 1718 : OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
10681 : :
10682 : 1718 : if (grp_mid
10683 : 185 : && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
10684 : 1903 : && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
10685 : : {
10686 : 0 : tree c3
10687 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10688 : 0 : OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
10689 : 0 : OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
10690 : 0 : OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
10691 : 0 : OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
10692 : :
10693 : 0 : *extra_node = c3;
10694 : : }
10695 : : else
10696 : 1718 : *extra_node = NULL_TREE;
10697 : :
10698 : 1718 : return c2;
10699 : : }
10700 : :
10701 : : /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
10702 : : and set *BITPOSP and *POFFSETP to the bit offset of the access.
10703 : : If BASE_REF is non-NULL and the containing object is a reference, set
10704 : : *BASE_REF to that reference before dereferencing the object.
10705 : : If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
10706 : : has array type, else return NULL. */
10707 : :
10708 : : static tree
10709 : 6542 : extract_base_bit_offset (tree base, poly_int64 *bitposp,
10710 : : poly_offset_int *poffsetp,
10711 : : bool *variable_offset)
10712 : : {
10713 : 6542 : tree offset;
10714 : 6542 : poly_int64 bitsize, bitpos;
10715 : 6542 : machine_mode mode;
10716 : 6542 : int unsignedp, reversep, volatilep = 0;
10717 : 6542 : poly_offset_int poffset;
10718 : :
10719 : 6542 : STRIP_NOPS (base);
10720 : :
10721 : 6542 : base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
10722 : : &unsignedp, &reversep, &volatilep);
10723 : :
10724 : 6542 : STRIP_NOPS (base);
10725 : :
10726 : 6542 : if (offset && poly_int_tree_p (offset))
10727 : : {
10728 : 0 : poffset = wi::to_poly_offset (offset);
10729 : 0 : *variable_offset = false;
10730 : : }
10731 : : else
10732 : : {
10733 : 6542 : poffset = 0;
10734 : 6542 : *variable_offset = (offset != NULL_TREE);
10735 : : }
10736 : :
10737 : 6542 : if (maybe_ne (bitpos, 0))
10738 : 4939 : poffset += bits_to_bytes_round_down (bitpos);
10739 : :
10740 : 6542 : *bitposp = bitpos;
10741 : 6542 : *poffsetp = poffset;
10742 : :
10743 : 6542 : return base;
10744 : : }
10745 : :
10746 : : /* Used for topological sorting of mapping groups. UNVISITED means we haven't
10747 : : started processing the group yet. The TEMPORARY mark is used when we first
10748 : : encounter a group on a depth-first traversal, and the PERMANENT mark is used
10749 : : when we have processed all the group's children (i.e. all the base pointers
10750 : : referred to by the group's mapping nodes, recursively). */
10751 : :
10752 : : enum omp_tsort_mark {
10753 : : UNVISITED,
10754 : : TEMPORARY,
10755 : : PERMANENT
10756 : : };
10757 : :
10758 : : /* Hash for trees based on operand_equal_p. Like tree_operand_hash
10759 : : but ignores side effects in the equality comparisons. */
10760 : :
10761 : : struct tree_operand_hash_no_se : tree_operand_hash
10762 : : {
10763 : : static inline bool equal (const value_type &,
10764 : : const compare_type &);
10765 : : };
10766 : :
10767 : : inline bool
10768 : 358184 : tree_operand_hash_no_se::equal (const value_type &t1,
10769 : : const compare_type &t2)
10770 : : {
10771 : 358184 : return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
10772 : : }
10773 : :
10774 : : /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
10775 : : clause. */
10776 : :
10777 : : struct omp_mapping_group {
10778 : : tree *grp_start;
10779 : : tree grp_end;
10780 : : omp_tsort_mark mark;
10781 : : /* If we've removed the group but need to reindex, mark the group as
10782 : : deleted. */
10783 : : bool deleted;
10784 : : /* The group points to an already-created "GOMP_MAP_STRUCT
10785 : : GOMP_MAP_ATTACH_DETACH" pair. */
10786 : : bool reprocess_struct;
10787 : : /* The group should use "zero-length" allocations for pointers that are not
10788 : : mapped "to" on the same directive. */
10789 : : bool fragile;
10790 : : struct omp_mapping_group *sibling;
10791 : : struct omp_mapping_group *next;
10792 : : };
10793 : :
10794 : : DEBUG_FUNCTION void
10795 : 0 : debug_mapping_group (omp_mapping_group *grp)
10796 : : {
10797 : 0 : tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
10798 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
10799 : 0 : debug_generic_expr (*grp->grp_start);
10800 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
10801 : 0 : }
10802 : :
10803 : : /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
10804 : : isn't one. */
10805 : :
10806 : : static tree
10807 : 32957 : omp_get_base_pointer (tree expr)
10808 : : {
10809 : 32957 : while (TREE_CODE (expr) == ARRAY_REF
10810 : 38247 : || TREE_CODE (expr) == COMPONENT_REF)
10811 : 5290 : expr = TREE_OPERAND (expr, 0);
10812 : :
10813 : 32957 : if (INDIRECT_REF_P (expr)
10814 : 32957 : || (TREE_CODE (expr) == MEM_REF
10815 : 0 : && integer_zerop (TREE_OPERAND (expr, 1))))
10816 : : {
10817 : 9645 : expr = TREE_OPERAND (expr, 0);
10818 : 9682 : while (TREE_CODE (expr) == COMPOUND_EXPR)
10819 : 37 : expr = TREE_OPERAND (expr, 1);
10820 : 9645 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
10821 : 837 : expr = TREE_OPERAND (expr, 0);
10822 : 9645 : if (TREE_CODE (expr) == SAVE_EXPR)
10823 : 31 : expr = TREE_OPERAND (expr, 0);
10824 : 9645 : STRIP_NOPS (expr);
10825 : 9645 : return expr;
10826 : : }
10827 : :
10828 : : return NULL_TREE;
10829 : : }
10830 : :
10831 : : /* An attach or detach operation depends directly on the address being
10832 : : attached/detached. Return that address, or none if there are no
10833 : : attachments/detachments. */
10834 : :
10835 : : static tree
10836 : 15064 : omp_get_attachment (omp_mapping_group *grp)
10837 : : {
10838 : 15064 : tree node = *grp->grp_start;
10839 : :
10840 : 15064 : switch (OMP_CLAUSE_MAP_KIND (node))
10841 : : {
10842 : 12554 : case GOMP_MAP_TO:
10843 : 12554 : case GOMP_MAP_FROM:
10844 : 12554 : case GOMP_MAP_TOFROM:
10845 : 12554 : case GOMP_MAP_ALWAYS_FROM:
10846 : 12554 : case GOMP_MAP_ALWAYS_TO:
10847 : 12554 : case GOMP_MAP_ALWAYS_TOFROM:
10848 : 12554 : case GOMP_MAP_FORCE_FROM:
10849 : 12554 : case GOMP_MAP_FORCE_TO:
10850 : 12554 : case GOMP_MAP_FORCE_TOFROM:
10851 : 12554 : case GOMP_MAP_FORCE_PRESENT:
10852 : 12554 : case GOMP_MAP_PRESENT_ALLOC:
10853 : 12554 : case GOMP_MAP_PRESENT_FROM:
10854 : 12554 : case GOMP_MAP_PRESENT_TO:
10855 : 12554 : case GOMP_MAP_PRESENT_TOFROM:
10856 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
10857 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_TO:
10858 : 12554 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10859 : 12554 : case GOMP_MAP_ALLOC:
10860 : 12554 : case GOMP_MAP_RELEASE:
10861 : 12554 : case GOMP_MAP_DELETE:
10862 : 12554 : case GOMP_MAP_FORCE_ALLOC:
10863 : 12554 : if (node == grp->grp_end)
10864 : : return NULL_TREE;
10865 : :
10866 : 5985 : node = OMP_CLAUSE_CHAIN (node);
10867 : 5985 : if (node && omp_map_clause_descriptor_p (node))
10868 : : {
10869 : 1002 : gcc_assert (node != grp->grp_end);
10870 : 1002 : node = OMP_CLAUSE_CHAIN (node);
10871 : : }
10872 : 5985 : if (node)
10873 : 5985 : switch (OMP_CLAUSE_MAP_KIND (node))
10874 : : {
10875 : : case GOMP_MAP_POINTER:
10876 : : case GOMP_MAP_ALWAYS_POINTER:
10877 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10878 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10879 : : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10880 : : return NULL_TREE;
10881 : :
10882 : 2087 : case GOMP_MAP_ATTACH_DETACH:
10883 : 2087 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10884 : 2087 : case GOMP_MAP_DETACH:
10885 : 2087 : return OMP_CLAUSE_DECL (node);
10886 : :
10887 : 0 : default:
10888 : 0 : internal_error ("unexpected mapping node");
10889 : : }
10890 : 0 : return error_mark_node;
10891 : :
10892 : 0 : case GOMP_MAP_TO_PSET:
10893 : 0 : gcc_assert (node != grp->grp_end);
10894 : 0 : node = OMP_CLAUSE_CHAIN (node);
10895 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10896 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10897 : 0 : return OMP_CLAUSE_DECL (node);
10898 : : else
10899 : 0 : internal_error ("unexpected mapping node");
10900 : : return error_mark_node;
10901 : :
10902 : 447 : case GOMP_MAP_ATTACH:
10903 : 447 : case GOMP_MAP_DETACH:
10904 : 447 : node = OMP_CLAUSE_CHAIN (node);
10905 : 447 : if (!node || *grp->grp_start == grp->grp_end)
10906 : 447 : return OMP_CLAUSE_DECL (*grp->grp_start);
10907 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10908 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10909 : 0 : return OMP_CLAUSE_DECL (*grp->grp_start);
10910 : : else
10911 : 0 : internal_error ("unexpected mapping node");
10912 : : return error_mark_node;
10913 : :
10914 : : case GOMP_MAP_STRUCT:
10915 : : case GOMP_MAP_STRUCT_UNORD:
10916 : : case GOMP_MAP_FORCE_DEVICEPTR:
10917 : : case GOMP_MAP_DEVICE_RESIDENT:
10918 : : case GOMP_MAP_LINK:
10919 : : case GOMP_MAP_IF_PRESENT:
10920 : : case GOMP_MAP_FIRSTPRIVATE:
10921 : : case GOMP_MAP_FIRSTPRIVATE_INT:
10922 : : case GOMP_MAP_USE_DEVICE_PTR:
10923 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10924 : : return NULL_TREE;
10925 : :
10926 : 0 : default:
10927 : 0 : internal_error ("unexpected mapping node");
10928 : : }
10929 : :
10930 : : return error_mark_node;
10931 : : }
10932 : :
10933 : : /* Given a pointer START_P to the start of a group of related (e.g. pointer)
10934 : : mappings, return the chain pointer to the end of that group in the list. */
10935 : :
10936 : : static tree *
10937 : 104830 : omp_group_last (tree *start_p)
10938 : : {
10939 : 104830 : tree c = *start_p, nc, *grp_last_p = start_p;
10940 : :
10941 : 104830 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
10942 : :
10943 : 104830 : nc = OMP_CLAUSE_CHAIN (c);
10944 : :
10945 : 183335 : if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
10946 : : return grp_last_p;
10947 : :
10948 : 66392 : switch (OMP_CLAUSE_MAP_KIND (c))
10949 : : {
10950 : : default:
10951 : : while (nc
10952 : 107051 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10953 : 224557 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10954 : 99889 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10955 : 89719 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
10956 : 79015 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
10957 : 53138 : || (OMP_CLAUSE_MAP_KIND (nc)
10958 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10959 : 53122 : || (OMP_CLAUSE_MAP_KIND (nc)
10960 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
10961 : 52755 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
10962 : 52700 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
10963 : 50796 : || omp_map_clause_descriptor_p (nc)))
10964 : : {
10965 : 62930 : tree nc2 = OMP_CLAUSE_CHAIN (nc);
10966 : 62930 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
10967 : : {
10968 : : /* In the specific case we're doing "exit data" on an array
10969 : : slice of a reference-to-pointer struct component, we will see
10970 : : DETACH followed by ATTACH_DETACH here. We want to treat that
10971 : : as a single group. In other cases DETACH might represent a
10972 : : stand-alone "detach" clause, so we don't want to consider
10973 : : that part of the group. */
10974 : 55 : if (nc2
10975 : 16 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10976 : 71 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
10977 : 0 : goto consume_two_nodes;
10978 : : else
10979 : : break;
10980 : : }
10981 : 62875 : if (nc2
10982 : 45911 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10983 : 39398 : && (OMP_CLAUSE_MAP_KIND (nc)
10984 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10985 : 62891 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
10986 : : {
10987 : 16 : consume_two_nodes:
10988 : 16 : grp_last_p = &OMP_CLAUSE_CHAIN (nc);
10989 : 16 : c = nc2;
10990 : 16 : nc = OMP_CLAUSE_CHAIN (nc2);
10991 : : }
10992 : : else
10993 : : {
10994 : 62859 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
10995 : 62859 : c = nc;
10996 : 62859 : nc = nc2;
10997 : : }
10998 : : }
10999 : : break;
11000 : :
11001 : 195 : case GOMP_MAP_ATTACH:
11002 : 195 : case GOMP_MAP_DETACH:
11003 : : /* This is a weird artifact of how directives are parsed: bare attach or
11004 : : detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
11005 : : FIRSTPRIVATE_REFERENCE node. FIXME. */
11006 : 195 : if (nc
11007 : 195 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11008 : 195 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11009 : 195 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
11010 : 0 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11011 : : break;
11012 : :
11013 : 27 : case GOMP_MAP_TO_PSET:
11014 : 27 : if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
11015 : 27 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
11016 : 6 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
11017 : 27 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
11018 : : break;
11019 : :
11020 : 5026 : case GOMP_MAP_STRUCT:
11021 : 5026 : case GOMP_MAP_STRUCT_UNORD:
11022 : 5026 : {
11023 : 5026 : unsigned HOST_WIDE_INT num_mappings
11024 : 5026 : = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
11025 : 5026 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
11026 : 4688 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
11027 : 9232 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
11028 : 1134 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11029 : 12263 : for (unsigned i = 0; i < num_mappings; i++)
11030 : 7237 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
11031 : : }
11032 : : break;
11033 : : }
11034 : :
11035 : : return grp_last_p;
11036 : : }
11037 : :
11038 : : /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
11039 : : OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
11040 : : associated GOMP_MAP_POINTER mappings). Return a vector of omp_mapping_group
11041 : : if we have more than one such group, else return NULL. */
11042 : :
11043 : : static void
11044 : 86502 : omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
11045 : : tree gather_sentinel)
11046 : : {
11047 : 86502 : for (tree *cp = list_p;
11048 : 256054 : *cp && *cp != gather_sentinel;
11049 : 169552 : cp = &OMP_CLAUSE_CHAIN (*cp))
11050 : : {
11051 : 169552 : if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
11052 : 94867 : continue;
11053 : :
11054 : 74685 : tree *grp_last_p = omp_group_last (cp);
11055 : 74685 : omp_mapping_group grp;
11056 : :
11057 : 74685 : grp.grp_start = cp;
11058 : 74685 : grp.grp_end = *grp_last_p;
11059 : 74685 : grp.mark = UNVISITED;
11060 : 74685 : grp.sibling = NULL;
11061 : 74685 : grp.deleted = false;
11062 : 74685 : grp.reprocess_struct = false;
11063 : 74685 : grp.fragile = false;
11064 : 74685 : grp.next = NULL;
11065 : 74685 : groups->safe_push (grp);
11066 : :
11067 : 74685 : cp = grp_last_p;
11068 : : }
11069 : 86502 : }
11070 : :
11071 : : static vec<omp_mapping_group> *
11072 : 86184 : omp_gather_mapping_groups (tree *list_p)
11073 : : {
11074 : 86184 : vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
11075 : :
11076 : 86184 : omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
11077 : :
11078 : 86184 : if (groups->length () > 0)
11079 : : return groups;
11080 : : else
11081 : : {
11082 : 43934 : delete groups;
11083 : 43934 : return NULL;
11084 : : }
11085 : : }
11086 : :
11087 : : /* A pointer mapping group GRP may define a block of memory starting at some
11088 : : base address, and maybe also define a firstprivate pointer or firstprivate
11089 : : reference that points to that block. The return value is a node containing
11090 : : the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
11091 : : If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
11092 : : return the number of consecutive chained nodes in CHAINED. */
11093 : :
11094 : : static tree
11095 : 75079 : omp_group_base (omp_mapping_group *grp, unsigned int *chained,
11096 : : tree *firstprivate)
11097 : : {
11098 : 75079 : tree node = *grp->grp_start;
11099 : :
11100 : 75079 : *firstprivate = NULL_TREE;
11101 : 75079 : *chained = 1;
11102 : :
11103 : 75079 : switch (OMP_CLAUSE_MAP_KIND (node))
11104 : : {
11105 : 71339 : case GOMP_MAP_TO:
11106 : 71339 : case GOMP_MAP_FROM:
11107 : 71339 : case GOMP_MAP_TOFROM:
11108 : 71339 : case GOMP_MAP_ALWAYS_FROM:
11109 : 71339 : case GOMP_MAP_ALWAYS_TO:
11110 : 71339 : case GOMP_MAP_ALWAYS_TOFROM:
11111 : 71339 : case GOMP_MAP_FORCE_FROM:
11112 : 71339 : case GOMP_MAP_FORCE_TO:
11113 : 71339 : case GOMP_MAP_FORCE_TOFROM:
11114 : 71339 : case GOMP_MAP_FORCE_PRESENT:
11115 : 71339 : case GOMP_MAP_PRESENT_ALLOC:
11116 : 71339 : case GOMP_MAP_PRESENT_FROM:
11117 : 71339 : case GOMP_MAP_PRESENT_TO:
11118 : 71339 : case GOMP_MAP_PRESENT_TOFROM:
11119 : 71339 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
11120 : 71339 : case GOMP_MAP_ALWAYS_PRESENT_TO:
11121 : 71339 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
11122 : 71339 : case GOMP_MAP_ALLOC:
11123 : 71339 : case GOMP_MAP_RELEASE:
11124 : 71339 : case GOMP_MAP_DELETE:
11125 : 71339 : case GOMP_MAP_FORCE_ALLOC:
11126 : 71339 : case GOMP_MAP_IF_PRESENT:
11127 : 71339 : if (node == grp->grp_end)
11128 : : return node;
11129 : :
11130 : 32693 : node = OMP_CLAUSE_CHAIN (node);
11131 : 32693 : if (!node)
11132 : 0 : internal_error ("unexpected mapping node");
11133 : 32693 : if (omp_map_clause_descriptor_p (node))
11134 : : {
11135 : 9678 : if (node == grp->grp_end)
11136 : 0 : return *grp->grp_start;
11137 : 9678 : node = OMP_CLAUSE_CHAIN (node);
11138 : : }
11139 : 32693 : switch (OMP_CLAUSE_MAP_KIND (node))
11140 : : {
11141 : 22980 : case GOMP_MAP_POINTER:
11142 : 22980 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11143 : 22980 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11144 : 22980 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11145 : 22980 : *firstprivate = OMP_CLAUSE_DECL (node);
11146 : 22980 : return *grp->grp_start;
11147 : :
11148 : 9713 : case GOMP_MAP_ALWAYS_POINTER:
11149 : 9713 : case GOMP_MAP_ATTACH_DETACH:
11150 : 9713 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11151 : 9713 : case GOMP_MAP_DETACH:
11152 : 9713 : return *grp->grp_start;
11153 : :
11154 : 0 : default:
11155 : 0 : internal_error ("unexpected mapping node");
11156 : : }
11157 : : return error_mark_node;
11158 : :
11159 : 18 : case GOMP_MAP_TO_PSET:
11160 : 18 : gcc_assert (node != grp->grp_end);
11161 : 18 : node = OMP_CLAUSE_CHAIN (node);
11162 : 18 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
11163 : 18 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
11164 : : return NULL_TREE;
11165 : : else
11166 : 0 : internal_error ("unexpected mapping node");
11167 : : return error_mark_node;
11168 : :
11169 : 1015 : case GOMP_MAP_ATTACH:
11170 : 1015 : case GOMP_MAP_DETACH:
11171 : 1015 : node = OMP_CLAUSE_CHAIN (node);
11172 : 1015 : if (!node || *grp->grp_start == grp->grp_end)
11173 : : return NULL_TREE;
11174 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11175 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11176 : : {
11177 : : /* We're mapping the base pointer itself in a bare attach or detach
11178 : : node. This is a side effect of how parsing works, and the mapping
11179 : : will be removed anyway (at least for enter/exit data directives).
11180 : : We should ignore the mapping here. FIXME. */
11181 : : return NULL_TREE;
11182 : : }
11183 : : else
11184 : 0 : internal_error ("unexpected mapping node");
11185 : : return error_mark_node;
11186 : :
11187 : 2423 : case GOMP_MAP_STRUCT:
11188 : 2423 : case GOMP_MAP_STRUCT_UNORD:
11189 : 2423 : {
11190 : 2423 : unsigned HOST_WIDE_INT num_mappings
11191 : 2423 : = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
11192 : 2423 : node = OMP_CLAUSE_CHAIN (node);
11193 : 2423 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
11194 : 2423 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11195 : : {
11196 : 393 : *firstprivate = OMP_CLAUSE_DECL (node);
11197 : 393 : node = OMP_CLAUSE_CHAIN (node);
11198 : : }
11199 : 2030 : else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
11200 : 314 : node = OMP_CLAUSE_CHAIN (node);
11201 : 2423 : *chained = num_mappings;
11202 : 2423 : return node;
11203 : : }
11204 : :
11205 : : case GOMP_MAP_FORCE_DEVICEPTR:
11206 : : case GOMP_MAP_DEVICE_RESIDENT:
11207 : : case GOMP_MAP_LINK:
11208 : : case GOMP_MAP_FIRSTPRIVATE:
11209 : : case GOMP_MAP_FIRSTPRIVATE_INT:
11210 : : case GOMP_MAP_USE_DEVICE_PTR:
11211 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11212 : : return NULL_TREE;
11213 : :
11214 : 0 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11215 : 0 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11216 : 0 : case GOMP_MAP_POINTER:
11217 : 0 : case GOMP_MAP_ALWAYS_POINTER:
11218 : 0 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
11219 : : /* These shouldn't appear by themselves. */
11220 : 0 : if (!seen_error ())
11221 : 0 : internal_error ("unexpected pointer mapping node");
11222 : 0 : return error_mark_node;
11223 : :
11224 : 0 : default:
11225 : 0 : gcc_unreachable ();
11226 : : }
11227 : :
11228 : : return error_mark_node;
11229 : : }
11230 : :
11231 : : /* Given a vector of omp_mapping_groups, build a hash table so we can look up
11232 : : nodes by tree_operand_hash_no_se. */
11233 : :
11234 : : static void
11235 : 42734 : omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
11236 : : omp_mapping_group *> *grpmap,
11237 : : vec<omp_mapping_group> *groups,
11238 : : tree reindex_sentinel)
11239 : : {
11240 : 42734 : omp_mapping_group *grp;
11241 : 42734 : unsigned int i;
11242 : 42734 : bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
11243 : :
11244 : 118267 : FOR_EACH_VEC_ELT (*groups, i, grp)
11245 : : {
11246 : 75533 : if (reindexing && *grp->grp_start == reindex_sentinel)
11247 : 75533 : above_hwm = true;
11248 : :
11249 : 75533 : if (reindexing && !above_hwm)
11250 : 52160 : continue;
11251 : :
11252 : 75233 : if (grp->reprocess_struct)
11253 : 154 : continue;
11254 : :
11255 : 75079 : tree fpp;
11256 : 75079 : unsigned int chained;
11257 : 75079 : tree node = omp_group_base (grp, &chained, &fpp);
11258 : :
11259 : 75079 : if (node == error_mark_node || (!node && !fpp))
11260 : 1317 : continue;
11261 : :
11262 : : for (unsigned j = 0;
11263 : 148601 : node && j < chained;
11264 : 74839 : node = OMP_CLAUSE_CHAIN (node), j++)
11265 : : {
11266 : 74839 : tree decl = OMP_CLAUSE_DECL (node);
11267 : : /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
11268 : : meaning node-hash lookups don't work. This is a workaround for
11269 : : that, but ideally we should just create the INDIRECT_REF at
11270 : : source instead. FIXME. */
11271 : 74839 : if (TREE_CODE (decl) == MEM_REF
11272 : 74839 : && integer_zerop (TREE_OPERAND (decl, 1)))
11273 : 0 : decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
11274 : :
11275 : 74839 : omp_mapping_group **prev = grpmap->get (decl);
11276 : :
11277 : 74839 : if (prev && *prev == grp)
11278 : : /* Empty. */;
11279 : 74839 : else if (prev)
11280 : : {
11281 : : /* Mapping the same thing twice is normally diagnosed as an error,
11282 : : but can happen under some circumstances, e.g. in pr99928-16.c,
11283 : : the directive:
11284 : :
11285 : : #pragma omp target simd reduction(+:a[:3]) \
11286 : : map(always, tofrom: a[:6])
11287 : : ...
11288 : :
11289 : : will result in two "a[0]" mappings (of different sizes). */
11290 : :
11291 : 140 : grp->sibling = (*prev)->sibling;
11292 : 140 : (*prev)->sibling = grp;
11293 : : }
11294 : : else
11295 : 74699 : grpmap->put (decl, grp);
11296 : : }
11297 : :
11298 : 73762 : if (!fpp)
11299 : 50389 : continue;
11300 : :
11301 : 23373 : omp_mapping_group **prev = grpmap->get (fpp);
11302 : 23373 : if (prev && *prev != grp)
11303 : : {
11304 : 12 : grp->sibling = (*prev)->sibling;
11305 : 12 : (*prev)->sibling = grp;
11306 : : }
11307 : : else
11308 : 23361 : grpmap->put (fpp, grp);
11309 : : }
11310 : 42734 : }
11311 : :
11312 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11313 : 42250 : omp_index_mapping_groups (vec<omp_mapping_group> *groups)
11314 : : {
11315 : 42250 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11316 : 42250 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11317 : :
11318 : 42250 : omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
11319 : :
11320 : 42250 : return grpmap;
11321 : : }
11322 : :
11323 : : /* Rebuild group map from partially-processed clause list (during
11324 : : omp_build_struct_sibling_lists). We have already processed nodes up until
11325 : : a high-water mark (HWM). This is a bit tricky because the list is being
11326 : : reordered as it is scanned, but we know:
11327 : :
11328 : : 1. The list after HWM has not been touched yet, so we can reindex it safely.
11329 : :
11330 : : 2. The list before and including HWM has been altered, but remains
11331 : : well-formed throughout the sibling-list building operation.
11332 : :
11333 : : so, we can do the reindex operation in two parts, on the processed and
11334 : : then the unprocessed halves of the list. */
11335 : :
11336 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
11337 : 318 : omp_reindex_mapping_groups (tree *list_p,
11338 : : vec<omp_mapping_group> *groups,
11339 : : vec<omp_mapping_group> *processed_groups,
11340 : : tree sentinel)
11341 : : {
11342 : 318 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
11343 : 318 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
11344 : :
11345 : 318 : processed_groups->truncate (0);
11346 : :
11347 : 318 : omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
11348 : 318 : omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
11349 : 318 : if (sentinel)
11350 : 166 : omp_index_mapping_groups_1 (grpmap, groups, sentinel);
11351 : :
11352 : 318 : return grpmap;
11353 : : }
11354 : :
11355 : : /* Find the immediately-containing struct for a component ref (etc.)
11356 : : expression EXPR. */
11357 : :
11358 : : static tree
11359 : 37431 : omp_containing_struct (tree expr)
11360 : : {
11361 : 37431 : tree expr0 = expr;
11362 : :
11363 : 37431 : STRIP_NOPS (expr);
11364 : :
11365 : : /* Note: don't strip NOPs unless we're also stripping off array refs or a
11366 : : component ref. */
11367 : 37431 : if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
11368 : : return expr0;
11369 : :
11370 : 18580 : while (TREE_CODE (expr) == ARRAY_REF)
11371 : 2142 : expr = TREE_OPERAND (expr, 0);
11372 : :
11373 : 16438 : if (TREE_CODE (expr) == COMPONENT_REF)
11374 : 15517 : expr = TREE_OPERAND (expr, 0);
11375 : :
11376 : : return expr;
11377 : : }
11378 : :
11379 : : /* Return TRUE if DECL describes a component that is part of a whole structure
11380 : : that is mapped elsewhere in GRPMAP. *MAPPED_BY_GROUP is set to the group
11381 : : that maps that structure, if present. */
11382 : :
11383 : : static bool
11384 : 21987 : omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
11385 : : omp_mapping_group *> *grpmap,
11386 : : tree decl,
11387 : : omp_mapping_group **mapped_by_group)
11388 : : {
11389 : 21987 : tree wsdecl = NULL_TREE;
11390 : :
11391 : 21987 : *mapped_by_group = NULL;
11392 : :
11393 : 37431 : while (true)
11394 : : {
11395 : 37431 : wsdecl = omp_containing_struct (decl);
11396 : 37431 : if (wsdecl == decl)
11397 : : break;
11398 : 16438 : omp_mapping_group **wholestruct = grpmap->get (wsdecl);
11399 : 16438 : if (!wholestruct
11400 : 15444 : && TREE_CODE (wsdecl) == MEM_REF
11401 : 16438 : && integer_zerop (TREE_OPERAND (wsdecl, 1)))
11402 : : {
11403 : 0 : tree deref = TREE_OPERAND (wsdecl, 0);
11404 : 0 : deref = build_fold_indirect_ref (deref);
11405 : 0 : wholestruct = grpmap->get (deref);
11406 : : }
11407 : 16438 : if (wholestruct)
11408 : : {
11409 : 994 : *mapped_by_group = *wholestruct;
11410 : 994 : return true;
11411 : : }
11412 : : decl = wsdecl;
11413 : : }
11414 : :
11415 : : return false;
11416 : : }
11417 : :
11418 : : /* Helper function for omp_tsort_mapping_groups. Returns TRUE on success, or
11419 : : FALSE on error. */
11420 : :
11421 : : static bool
11422 : 18255 : omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
11423 : : vec<omp_mapping_group> *groups,
11424 : : hash_map<tree_operand_hash_no_se,
11425 : : omp_mapping_group *> *grpmap,
11426 : : omp_mapping_group *grp)
11427 : : {
11428 : 18255 : if (grp->mark == PERMANENT)
11429 : : return true;
11430 : 15064 : if (grp->mark == TEMPORARY)
11431 : : {
11432 : 0 : fprintf (stderr, "when processing group:\n");
11433 : 0 : debug_mapping_group (grp);
11434 : 0 : internal_error ("base pointer cycle detected");
11435 : : return false;
11436 : : }
11437 : 15064 : grp->mark = TEMPORARY;
11438 : :
11439 : 15064 : tree attaches_to = omp_get_attachment (grp);
11440 : :
11441 : 15064 : if (attaches_to)
11442 : : {
11443 : 2534 : omp_mapping_group **basep = grpmap->get (attaches_to);
11444 : :
11445 : 2534 : if (basep && *basep != grp)
11446 : : {
11447 : 2920 : for (omp_mapping_group *w = *basep; w; w = w->sibling)
11448 : 1460 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11449 : : return false;
11450 : : }
11451 : : }
11452 : :
11453 : 15064 : tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
11454 : :
11455 : 20540 : while (decl)
11456 : : {
11457 : 20540 : tree base = omp_get_base_pointer (decl);
11458 : :
11459 : 20540 : if (!base)
11460 : : break;
11461 : :
11462 : 8053 : omp_mapping_group **innerp = grpmap->get (base);
11463 : 8053 : omp_mapping_group *wholestruct;
11464 : :
11465 : : /* We should treat whole-structure mappings as if all (pointer, in this
11466 : : case) members are mapped as individual list items. Check if we have
11467 : : such a whole-structure mapping, if we don't have an explicit reference
11468 : : to the pointer member itself. */
11469 : 8053 : if (!innerp
11470 : 3318 : && TREE_CODE (base) == COMPONENT_REF
11471 : 9444 : && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
11472 : : innerp = &wholestruct;
11473 : :
11474 : 8053 : if (innerp && *innerp != grp)
11475 : : {
11476 : 5154 : for (omp_mapping_group *w = *innerp; w; w = w->sibling)
11477 : 2577 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11478 : 0 : return false;
11479 : : break;
11480 : : }
11481 : :
11482 : 5476 : decl = base;
11483 : : }
11484 : :
11485 : 15064 : grp->mark = PERMANENT;
11486 : :
11487 : : /* Emit grp to output list. */
11488 : :
11489 : 15064 : **outlist = grp;
11490 : 15064 : *outlist = &grp->next;
11491 : :
11492 : 15064 : return true;
11493 : : }
11494 : :
11495 : : /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
11496 : : before mappings that use those pointers. This is an implementation of the
11497 : : depth-first search algorithm, described e.g. at:
11498 : :
11499 : : https://en.wikipedia.org/wiki/Topological_sorting
11500 : : */
11501 : :
11502 : : static omp_mapping_group *
11503 : 7701 : omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
11504 : : hash_map<tree_operand_hash_no_se, omp_mapping_group *>
11505 : : *grpmap,
11506 : : bool enter_exit_data)
11507 : : {
11508 : 7701 : omp_mapping_group *grp, *outlist = NULL, **cursor;
11509 : 7701 : unsigned int i;
11510 : 7701 : bool saw_runtime_implicit = false;
11511 : :
11512 : 7701 : cursor = &outlist;
11513 : :
11514 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11515 : : {
11516 : 15064 : if (grp->mark != PERMANENT)
11517 : : {
11518 : 14218 : if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11519 : : {
11520 : 238 : saw_runtime_implicit = true;
11521 : 238 : continue;
11522 : : }
11523 : 13980 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11524 : : return NULL;
11525 : : }
11526 : : }
11527 : :
11528 : 7701 : if (!saw_runtime_implicit)
11529 : 7555 : return outlist;
11530 : :
11531 : 629 : FOR_EACH_VEC_ELT (*groups, i, grp)
11532 : : {
11533 : 483 : if (grp->mark != PERMANENT
11534 : 483 : && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11535 : : {
11536 : : /* Clear the flag for enter/exit data because it is currently
11537 : : meaningless for those operations in libgomp. */
11538 : 238 : if (enter_exit_data)
11539 : 41 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
11540 : :
11541 : 238 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11542 : : return NULL;
11543 : : }
11544 : : }
11545 : :
11546 : 146 : return outlist;
11547 : : }
11548 : :
11549 : : /* Split INLIST into three parts:
11550 : :
11551 : : - "present" alloc/to/from groups
11552 : : - other to/from groups
11553 : : - other alloc/release/delete groups
11554 : :
11555 : : These sub-lists are then concatenated together to form the final list.
11556 : : Each sub-list retains the order of the original list.
11557 : : Note that ATTACH nodes are later moved to the end of the list in
11558 : : gimplify_adjust_omp_clauses, for target regions. */
11559 : :
11560 : : static omp_mapping_group *
11561 : 7701 : omp_segregate_mapping_groups (omp_mapping_group *inlist)
11562 : : {
11563 : 7701 : omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
11564 : 7701 : omp_mapping_group *p_groups = NULL;
11565 : 7701 : omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
11566 : 7701 : omp_mapping_group **p_tail = &p_groups;
11567 : :
11568 : 22765 : for (omp_mapping_group *w = inlist; w;)
11569 : : {
11570 : 15064 : tree c = *w->grp_start;
11571 : 15064 : omp_mapping_group *next = w->next;
11572 : :
11573 : 15064 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
11574 : :
11575 : 15064 : switch (OMP_CLAUSE_MAP_KIND (c))
11576 : : {
11577 : 1673 : case GOMP_MAP_ALLOC:
11578 : 1673 : case GOMP_MAP_RELEASE:
11579 : 1673 : case GOMP_MAP_DELETE:
11580 : 1673 : *ard_tail = w;
11581 : 1673 : w->next = NULL;
11582 : 1673 : ard_tail = &w->next;
11583 : 1673 : break;
11584 : :
11585 : : /* These map types are all semantically identical, so are moved into a
11586 : : single group. They will each be changed into GOMP_MAP_FORCE_PRESENT
11587 : : in gimplify_adjust_omp_clauses. */
11588 : 123 : case GOMP_MAP_PRESENT_ALLOC:
11589 : 123 : case GOMP_MAP_PRESENT_FROM:
11590 : 123 : case GOMP_MAP_PRESENT_TO:
11591 : 123 : case GOMP_MAP_PRESENT_TOFROM:
11592 : 123 : *p_tail = w;
11593 : 123 : w->next = NULL;
11594 : 123 : p_tail = &w->next;
11595 : 123 : break;
11596 : :
11597 : 13268 : default:
11598 : 13268 : *tf_tail = w;
11599 : 13268 : w->next = NULL;
11600 : 13268 : tf_tail = &w->next;
11601 : : }
11602 : :
11603 : : w = next;
11604 : : }
11605 : :
11606 : : /* Now splice the lists together... */
11607 : 7701 : *tf_tail = ard_groups;
11608 : 7701 : *p_tail = tf_groups;
11609 : :
11610 : 7701 : return p_groups;
11611 : : }
11612 : :
11613 : : /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
11614 : : those groups based on the output list of omp_tsort_mapping_groups --
11615 : : singly-linked, threaded through each element's NEXT pointer starting at
11616 : : HEAD. Each list element appears exactly once in that linked list.
11617 : :
11618 : : Each element of GROUPS may correspond to one or several mapping nodes.
11619 : : Node groups are kept together, and in the reordered list, the positions of
11620 : : the original groups are reused for the positions of the reordered list.
11621 : : Hence if we have e.g.
11622 : :
11623 : : {to ptr ptr} firstprivate {tofrom ptr} ...
11624 : : ^ ^ ^
11625 : : first group non-"map" second group
11626 : :
11627 : : and say the second group contains a base pointer for the first so must be
11628 : : moved before it, the resulting list will contain:
11629 : :
11630 : : {tofrom ptr} firstprivate {to ptr ptr} ...
11631 : : ^ prev. second group ^ prev. first group
11632 : : */
11633 : :
11634 : : static tree *
11635 : 7701 : omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
11636 : : omp_mapping_group *head,
11637 : : tree *list_p)
11638 : : {
11639 : 7701 : omp_mapping_group *grp;
11640 : 7701 : unsigned int i;
11641 : 7701 : unsigned numgroups = groups->length ();
11642 : 7701 : auto_vec<tree> old_heads (numgroups);
11643 : 7701 : auto_vec<tree *> old_headps (numgroups);
11644 : 7701 : auto_vec<tree> new_heads (numgroups);
11645 : 7701 : auto_vec<tree> old_succs (numgroups);
11646 : 7701 : bool map_at_start = (list_p == (*groups)[0].grp_start);
11647 : :
11648 : 7701 : tree *new_grp_tail = NULL;
11649 : :
11650 : : /* Stash the start & end nodes of each mapping group before we start
11651 : : modifying the list. */
11652 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11653 : : {
11654 : 15064 : old_headps.quick_push (grp->grp_start);
11655 : 15064 : old_heads.quick_push (*grp->grp_start);
11656 : 15064 : old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
11657 : : }
11658 : :
11659 : : /* And similarly, the heads of the groups in the order we want to rearrange
11660 : : the list to. */
11661 : 22765 : for (omp_mapping_group *w = head; w; w = w->next)
11662 : 15064 : new_heads.quick_push (*w->grp_start);
11663 : :
11664 : 22765 : FOR_EACH_VEC_ELT (*groups, i, grp)
11665 : : {
11666 : 15064 : gcc_assert (head);
11667 : :
11668 : 15064 : if (new_grp_tail && old_succs[i - 1] == old_heads[i])
11669 : : {
11670 : : /* a {b c d} {e f g} h i j (original)
11671 : : -->
11672 : : a {k l m} {e f g} h i j (inserted new group on last iter)
11673 : : -->
11674 : : a {k l m} {n o p} h i j (this time, chain last group to new one)
11675 : : ^new_grp_tail
11676 : : */
11677 : 6741 : *new_grp_tail = new_heads[i];
11678 : : }
11679 : 8323 : else if (new_grp_tail)
11680 : : {
11681 : : /* a {b c d} e {f g h} i j k (original)
11682 : : -->
11683 : : a {l m n} e {f g h} i j k (gap after last iter's group)
11684 : : -->
11685 : : a {l m n} e {o p q} h i j (chain last group to old successor)
11686 : : ^new_grp_tail
11687 : : */
11688 : 622 : *new_grp_tail = old_succs[i - 1];
11689 : 622 : *old_headps[i] = new_heads[i];
11690 : : }
11691 : : else
11692 : : {
11693 : : /* The first inserted group -- point to new group, and leave end
11694 : : open.
11695 : : a {b c d} e f
11696 : : -->
11697 : : a {g h i...
11698 : : */
11699 : 7701 : *grp->grp_start = new_heads[i];
11700 : : }
11701 : :
11702 : 15064 : new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
11703 : :
11704 : 15064 : head = head->next;
11705 : : }
11706 : :
11707 : 7701 : if (new_grp_tail)
11708 : 7701 : *new_grp_tail = old_succs[numgroups - 1];
11709 : :
11710 : 7701 : gcc_assert (!head);
11711 : :
11712 : 10787 : return map_at_start ? (*groups)[0].grp_start : list_p;
11713 : 7701 : }
11714 : :
11715 : : /* DECL is supposed to have lastprivate semantics in the outer contexts
11716 : : of combined/composite constructs, starting with OCTX.
11717 : : Add needed lastprivate, shared or map clause if no data sharing or
11718 : : mapping clause are present. IMPLICIT_P is true if it is an implicit
11719 : : clause (IV on simd), in which case the lastprivate will not be
11720 : : copied to some constructs. */
11721 : :
11722 : : static void
11723 : 14654 : omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
11724 : : tree decl, bool implicit_p)
11725 : : {
11726 : 14654 : struct gimplify_omp_ctx *orig_octx = octx;
11727 : 26973 : for (; octx; octx = octx->outer_context)
11728 : : {
11729 : 25900 : if ((octx->region_type == ORT_COMBINED_PARALLEL
11730 : 20868 : || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
11731 : 25331 : && splay_tree_lookup (octx->variables,
11732 : : (splay_tree_key) decl) == NULL)
11733 : : {
11734 : 3883 : omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
11735 : 3883 : continue;
11736 : : }
11737 : 18382 : if ((octx->region_type & ORT_TASK) != 0
11738 : 575 : && octx->combined_loop
11739 : 18663 : && splay_tree_lookup (octx->variables,
11740 : : (splay_tree_key) decl) == NULL)
11741 : : {
11742 : 248 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11743 : 248 : continue;
11744 : : }
11745 : 22724 : if (implicit_p
11746 : 13627 : && octx->region_type == ORT_WORKSHARE
11747 : 10407 : && octx->combined_loop
11748 : 10302 : && splay_tree_lookup (octx->variables,
11749 : : (splay_tree_key) decl) == NULL
11750 : 10302 : && octx->outer_context
11751 : 8909 : && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
11752 : 22731 : && splay_tree_lookup (octx->outer_context->variables,
11753 : : (splay_tree_key) decl) == NULL)
11754 : : {
11755 : 4838 : octx = octx->outer_context;
11756 : 4838 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11757 : 4838 : continue;
11758 : : }
11759 : 5570 : if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
11760 : 7478 : && octx->combined_loop
11761 : 7291 : && splay_tree_lookup (octx->variables,
11762 : : (splay_tree_key) decl) == NULL
11763 : 19069 : && !omp_check_private (octx, decl, false))
11764 : : {
11765 : 3350 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11766 : 3350 : continue;
11767 : : }
11768 : 9698 : if (octx->region_type == ORT_COMBINED_TARGET)
11769 : : {
11770 : 2004 : splay_tree_node n = splay_tree_lookup (octx->variables,
11771 : : (splay_tree_key) decl);
11772 : 2004 : if (n == NULL)
11773 : : {
11774 : 1982 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11775 : 1982 : octx = octx->outer_context;
11776 : : }
11777 : 22 : else if (!implicit_p
11778 : 22 : && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
11779 : : {
11780 : 12 : n->value &= ~(GOVD_FIRSTPRIVATE
11781 : : | GOVD_FIRSTPRIVATE_IMPLICIT
11782 : : | GOVD_EXPLICIT);
11783 : 12 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11784 : 12 : octx = octx->outer_context;
11785 : : }
11786 : : }
11787 : : break;
11788 : : }
11789 : 14654 : if (octx && (implicit_p || octx != orig_octx))
11790 : 4056 : omp_notice_variable (octx, decl, true);
11791 : 14654 : }
11792 : :
11793 : : /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
11794 : : a "FIRSTPRIVATE" mapping. Return the one that isn't firstprivate, etc. */
11795 : :
11796 : : static omp_mapping_group *
11797 : 5304 : omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
11798 : : omp_mapping_group *> *grpmap,
11799 : : tree decl, bool allow_deleted = false)
11800 : : {
11801 : 5304 : omp_mapping_group **to_group_p = grpmap->get (decl);
11802 : :
11803 : 5304 : if (!to_group_p)
11804 : : return NULL;
11805 : :
11806 : 2326 : omp_mapping_group *to_group = *to_group_p;
11807 : :
11808 : 3548 : for (; to_group; to_group = to_group->sibling)
11809 : : {
11810 : 2363 : tree grp_end = to_group->grp_end;
11811 : 2363 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11812 : : {
11813 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11814 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11815 : : break;
11816 : :
11817 : 1143 : default:
11818 : 1143 : if (allow_deleted || !to_group->deleted)
11819 : : return to_group;
11820 : : }
11821 : : }
11822 : :
11823 : : return NULL;
11824 : : }
11825 : :
11826 : : /* Return TRUE if the directive (whose clauses are described by the hash table
11827 : : of mapping groups, GRPMAP) maps DECL explicitly. If TO_SPECIFICALLY is
11828 : : true, only count TO mappings. If ALLOW_DELETED is true, ignore the
11829 : : "deleted" flag for groups. If CONTAINED_IN_STRUCT is true, also return
11830 : : TRUE if DECL is mapped as a member of a whole-struct mapping. */
11831 : :
11832 : : static bool
11833 : 3881 : omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
11834 : : omp_mapping_group *> *grpmap,
11835 : : tree decl, omp_mapping_group **base_group,
11836 : : bool to_specifically, bool allow_deleted,
11837 : : bool contained_in_struct)
11838 : : {
11839 : 3881 : omp_mapping_group *decl_group
11840 : 3881 : = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
11841 : :
11842 : 3881 : *base_group = NULL;
11843 : :
11844 : 3881 : if (decl_group)
11845 : : {
11846 : 1024 : tree grp_first = *decl_group->grp_start;
11847 : : /* We might be called during omp_build_struct_sibling_lists, when
11848 : : GOMP_MAP_STRUCT might have been inserted at the start of the group.
11849 : : Skip over that, and also possibly the node after it. */
11850 : 1024 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
11851 : 1024 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
11852 : : {
11853 : 6 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11854 : 6 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
11855 : 6 : || (OMP_CLAUSE_MAP_KIND (grp_first)
11856 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11857 : 12 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
11858 : 0 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11859 : : }
11860 : 1024 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11861 : 1024 : if (!to_specifically
11862 : 574 : || GOMP_MAP_COPY_TO_P (first_kind)
11863 : 365 : || first_kind == GOMP_MAP_ALLOC)
11864 : : {
11865 : 959 : *base_group = decl_group;
11866 : 959 : return true;
11867 : : }
11868 : : }
11869 : :
11870 : 2922 : if (contained_in_struct
11871 : 2922 : && omp_mapped_by_containing_struct (grpmap, decl, base_group))
11872 : : return true;
11873 : :
11874 : : return false;
11875 : : }
11876 : :
11877 : : /* If we have mappings INNER and OUTER, where INNER is a component access and
11878 : : OUTER is a mapping of the whole containing struct, check that the mappings
11879 : : are compatible. We'll be deleting the inner mapping, so we need to make
11880 : : sure the outer mapping does (at least) the same transfers to/from the device
11881 : : as the inner mapping. */
11882 : :
11883 : : bool
11884 : 172 : omp_check_mapping_compatibility (location_t loc,
11885 : : omp_mapping_group *outer,
11886 : : omp_mapping_group *inner)
11887 : : {
11888 : 172 : tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
11889 : :
11890 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
11891 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
11892 : :
11893 : 172 : enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
11894 : 172 : enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
11895 : :
11896 : 172 : if (outer_kind == inner_kind)
11897 : : return true;
11898 : :
11899 : 70 : switch (outer_kind)
11900 : : {
11901 : 0 : case GOMP_MAP_ALWAYS_TO:
11902 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11903 : 0 : || inner_kind == GOMP_MAP_ALLOC
11904 : 0 : || inner_kind == GOMP_MAP_TO)
11905 : : return true;
11906 : : break;
11907 : :
11908 : 0 : case GOMP_MAP_ALWAYS_FROM:
11909 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11910 : 0 : || inner_kind == GOMP_MAP_RELEASE
11911 : : || inner_kind == GOMP_MAP_FROM)
11912 : : return true;
11913 : : break;
11914 : :
11915 : 10 : case GOMP_MAP_TO:
11916 : 10 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11917 : 10 : || inner_kind == GOMP_MAP_ALLOC)
11918 : : return true;
11919 : : break;
11920 : :
11921 : 8 : case GOMP_MAP_FROM:
11922 : 8 : if (inner_kind == GOMP_MAP_RELEASE
11923 : 8 : || inner_kind == GOMP_MAP_FORCE_PRESENT)
11924 : : return true;
11925 : : break;
11926 : :
11927 : 32 : case GOMP_MAP_ALWAYS_TOFROM:
11928 : 32 : case GOMP_MAP_TOFROM:
11929 : 32 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11930 : 32 : || inner_kind == GOMP_MAP_ALLOC
11931 : : || inner_kind == GOMP_MAP_TO
11932 : 24 : || inner_kind == GOMP_MAP_FROM
11933 : 12 : || inner_kind == GOMP_MAP_TOFROM)
11934 : : return true;
11935 : : break;
11936 : :
11937 : 28 : default:
11938 : 28 : ;
11939 : : }
11940 : :
11941 : 84 : error_at (loc, "data movement for component %qE is not compatible with "
11942 : 28 : "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
11943 : 28 : OMP_CLAUSE_DECL (first_outer));
11944 : :
11945 : 28 : return false;
11946 : : }
11947 : :
11948 : : /* This function handles several cases where clauses on a mapping directive
11949 : : can interact with each other.
11950 : :
11951 : : If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
11952 : : on the same directive, change the mapping of the first node to
11953 : : ATTACH_DETACH. We should have detected that this will happen already in
11954 : : c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
11955 : : as addressable. (If we didn't, bail out.)
11956 : :
11957 : : If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
11958 : : mapping the base pointer also, we may need to change the mapping type to
11959 : : ATTACH_DETACH and synthesize an alloc node for the reference itself.
11960 : :
11961 : : If we have an ATTACH_DETACH node, this is an array section with a pointer
11962 : : base. If we're mapping the base on the same directive too, we can drop its
11963 : : mapping. However, if we have a reference to pointer, make other appropriate
11964 : : adjustments to the mapping nodes instead.
11965 : :
11966 : : If we have an ATTACH_DETACH node with a Fortran pointer-set (array
11967 : : descriptor) mapping for a derived-type component, and we're also mapping the
11968 : : whole of the derived-type variable on another clause, the pointer-set
11969 : : mapping is removed.
11970 : :
11971 : : If we have a component access but we're also mapping the whole of the
11972 : : containing struct, drop the former access.
11973 : :
11974 : : If the expression is a component access, and we're also mapping a base
11975 : : pointer used in that component access in the same expression, change the
11976 : : mapping type of the latter to ALLOC (ready for processing by
11977 : : omp_build_struct_sibling_lists). */
11978 : :
11979 : : void
11980 : 7701 : omp_resolve_clause_dependencies (enum tree_code code,
11981 : : vec<omp_mapping_group> *groups,
11982 : : hash_map<tree_operand_hash_no_se,
11983 : : omp_mapping_group *> *grpmap)
11984 : : {
11985 : 7701 : int i;
11986 : 7701 : omp_mapping_group *grp;
11987 : 7701 : bool repair_chain = false;
11988 : :
11989 : 21920 : FOR_EACH_VEC_ELT (*groups, i, grp)
11990 : : {
11991 : 14219 : tree grp_end = grp->grp_end;
11992 : 14219 : tree decl = OMP_CLAUSE_DECL (grp_end);
11993 : :
11994 : 14219 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11995 : :
11996 : 14219 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11997 : : {
11998 : 1111 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11999 : 1111 : {
12000 : 1111 : omp_mapping_group *to_group
12001 : 1111 : = omp_get_nonfirstprivate_group (grpmap, decl);
12002 : :
12003 : 1111 : if (!to_group || to_group == grp)
12004 : 1086 : continue;
12005 : :
12006 : 25 : tree grp_first = *to_group->grp_start;
12007 : 25 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12008 : :
12009 : 25 : if ((GOMP_MAP_COPY_TO_P (first_kind)
12010 : 7 : || first_kind == GOMP_MAP_ALLOC)
12011 : 50 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12012 : : != GOMP_MAP_FIRSTPRIVATE_POINTER))
12013 : : {
12014 : 25 : gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
12015 : 25 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12016 : : }
12017 : : }
12018 : : break;
12019 : :
12020 : 156 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
12021 : 156 : {
12022 : 156 : tree ptr = build_fold_indirect_ref (decl);
12023 : :
12024 : 156 : omp_mapping_group *to_group
12025 : 156 : = omp_get_nonfirstprivate_group (grpmap, ptr);
12026 : :
12027 : 156 : if (!to_group || to_group == grp)
12028 : 152 : continue;
12029 : :
12030 : 4 : tree grp_first = *to_group->grp_start;
12031 : 4 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
12032 : :
12033 : 4 : if (GOMP_MAP_COPY_TO_P (first_kind)
12034 : 4 : || first_kind == GOMP_MAP_ALLOC)
12035 : : {
12036 : 4 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
12037 : 4 : OMP_CLAUSE_DECL (grp_end) = ptr;
12038 : 4 : if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
12039 : 4 : == to_group->grp_end)
12040 : 4 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
12041 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
12042 : : {
12043 : 0 : gcc_assert (TREE_ADDRESSABLE
12044 : : (OMP_CLAUSE_DECL (to_group->grp_end)));
12045 : 0 : OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
12046 : : GOMP_MAP_ATTACH_DETACH);
12047 : :
12048 : 0 : location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
12049 : 0 : tree alloc
12050 : 0 : = build_omp_clause (loc, OMP_CLAUSE_MAP);
12051 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
12052 : 0 : tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
12053 : : (to_group->grp_end));
12054 : 0 : tree char_ptr_type = build_pointer_type (char_type_node);
12055 : 0 : OMP_CLAUSE_DECL (alloc)
12056 : 0 : = build2 (MEM_REF, char_type_node,
12057 : : tmp,
12058 : : build_int_cst (char_ptr_type, 0));
12059 : 0 : OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
12060 : :
12061 : 0 : OMP_CLAUSE_CHAIN (alloc)
12062 : 0 : = OMP_CLAUSE_CHAIN (*to_group->grp_start);
12063 : 0 : OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
12064 : : }
12065 : : }
12066 : : }
12067 : : break;
12068 : :
12069 : : case GOMP_MAP_ATTACH_DETACH:
12070 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
12071 : : {
12072 : 2064 : tree base_ptr, referenced_ptr_node = NULL_TREE;
12073 : :
12074 : 2064 : while (TREE_CODE (decl) == ARRAY_REF)
12075 : 0 : decl = TREE_OPERAND (decl, 0);
12076 : :
12077 : 2064 : if (TREE_CODE (decl) == INDIRECT_REF)
12078 : 13 : decl = TREE_OPERAND (decl, 0);
12079 : :
12080 : : /* Only component accesses. */
12081 : 2064 : if (DECL_P (decl))
12082 : 217 : continue;
12083 : :
12084 : : /* We want the pointer itself when checking if the base pointer is
12085 : : mapped elsewhere in the same directive -- if we have a
12086 : : reference to the pointer, don't use that. */
12087 : :
12088 : 1847 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
12089 : 1847 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
12090 : : {
12091 : 394 : referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
12092 : 394 : base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
12093 : : }
12094 : : else
12095 : : base_ptr = decl;
12096 : :
12097 : 1669 : gomp_map_kind zlas_kind
12098 : 1847 : = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
12099 : 1847 : ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
12100 : :
12101 : 1847 : if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
12102 : : {
12103 : : /* If we map the base TO, and we're doing an attachment, we can
12104 : : skip the TO mapping altogether and create an ALLOC mapping
12105 : : instead, since the attachment will overwrite the device
12106 : : pointer in that location immediately anyway. Otherwise,
12107 : : change our mapping to
12108 : : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
12109 : : attachment target has not been copied to the device already
12110 : : by some earlier directive. */
12111 : :
12112 : 1423 : bool base_mapped_to = false;
12113 : :
12114 : 1423 : omp_mapping_group *base_group;
12115 : :
12116 : 1423 : if (omp_directive_maps_explicitly (grpmap, base_ptr,
12117 : : &base_group, false, true,
12118 : : false))
12119 : : {
12120 : 450 : if (referenced_ptr_node)
12121 : : {
12122 : 129 : base_mapped_to = true;
12123 : 129 : if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
12124 : : == GOMP_MAP_ATTACH_DETACH)
12125 : 129 : && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
12126 : : == base_group->grp_end))
12127 : : {
12128 : 258 : OMP_CLAUSE_CHAIN (*base_group->grp_start)
12129 : 129 : = OMP_CLAUSE_CHAIN (base_group->grp_end);
12130 : 129 : base_group->grp_end = *base_group->grp_start;
12131 : 129 : repair_chain = true;
12132 : : }
12133 : : }
12134 : : else
12135 : : {
12136 : 321 : base_group->deleted = true;
12137 : 321 : OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
12138 : : }
12139 : : }
12140 : :
12141 : : /* We're dealing with a reference to a pointer, and we are
12142 : : attaching both the reference and the pointer. We know the
12143 : : reference itself is on the target, because we are going to
12144 : : create an ALLOC node for it in accumulate_sibling_list. The
12145 : : pointer might be on the target already or it might not, but
12146 : : if it isn't then it's not an error, so use
12147 : : GOMP_MAP_ATTACH_ZLAS for it. */
12148 : 1423 : if (!base_mapped_to && referenced_ptr_node)
12149 : 56 : OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
12150 : :
12151 : 1423 : omp_mapping_group *struct_group;
12152 : 1423 : tree desc;
12153 : 1423 : if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
12154 : 1423 : && omp_map_clause_descriptor_p (desc)
12155 : 2017 : && omp_mapped_by_containing_struct (grpmap, decl,
12156 : : &struct_group))
12157 : : /* If we have a pointer set but we're mapping (or unmapping)
12158 : : the whole of the containing struct, we can remove the
12159 : : pointer set mapping. */
12160 : 15 : OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
12161 : : }
12162 : 424 : else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
12163 : 424 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
12164 : : == ARRAY_TYPE)
12165 : 562 : && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
12166 : : (*grp->grp_start))
12167 : 92 : OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
12168 : : }
12169 : : break;
12170 : :
12171 : : case GOMP_MAP_ATTACH:
12172 : : /* Ignore standalone attach here. */
12173 : : break;
12174 : :
12175 : 10825 : default:
12176 : 10825 : {
12177 : 10825 : omp_mapping_group *struct_group;
12178 : 10825 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12179 : 10825 : && *grp->grp_start == grp_end)
12180 : : {
12181 : 94 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12182 : : struct_group, grp);
12183 : : /* Remove the whole of this mapping -- redundant. */
12184 : 94 : grp->deleted = true;
12185 : : }
12186 : :
12187 : : tree base = decl;
12188 : 12417 : while ((base = omp_get_base_pointer (base)))
12189 : : {
12190 : 1592 : omp_mapping_group *base_group;
12191 : :
12192 : 1592 : if (omp_directive_maps_explicitly (grpmap, base, &base_group,
12193 : : true, true, false))
12194 : : {
12195 : 342 : tree grp_first = *base_group->grp_start;
12196 : 342 : OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
12197 : : }
12198 : : }
12199 : : }
12200 : : }
12201 : : }
12202 : :
12203 : 7701 : if (repair_chain)
12204 : : {
12205 : : /* Group start pointers may have become detached from the
12206 : : OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
12207 : : end of those groups. Fix that now. */
12208 : : tree *new_next = NULL;
12209 : 716 : FOR_EACH_VEC_ELT (*groups, i, grp)
12210 : : {
12211 : 587 : if (new_next)
12212 : 458 : grp->grp_start = new_next;
12213 : :
12214 : 587 : new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
12215 : : }
12216 : : }
12217 : 7701 : }
12218 : :
12219 : : /* Similar to omp_resolve_clause_dependencies, but for OpenACC. The only
12220 : : clause dependencies we handle for now are struct element mappings and
12221 : : whole-struct mappings on the same directive, and duplicate clause
12222 : : detection. */
12223 : :
12224 : : void
12225 : 9469 : oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
12226 : : hash_map<tree_operand_hash_no_se,
12227 : : omp_mapping_group *> *grpmap)
12228 : : {
12229 : 9469 : int i;
12230 : 9469 : omp_mapping_group *grp;
12231 : 9469 : hash_set<tree_operand_hash> *seen_components = NULL;
12232 : 9469 : hash_set<tree_operand_hash> *shown_error = NULL;
12233 : :
12234 : 24654 : FOR_EACH_VEC_ELT (*groups, i, grp)
12235 : : {
12236 : 15185 : tree grp_end = grp->grp_end;
12237 : 15185 : tree decl = OMP_CLAUSE_DECL (grp_end);
12238 : :
12239 : 15185 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
12240 : :
12241 : 15185 : if (DECL_P (grp_end))
12242 : 14663 : continue;
12243 : :
12244 : 15185 : tree c = OMP_CLAUSE_DECL (*grp->grp_start);
12245 : 16810 : while (TREE_CODE (c) == ARRAY_REF)
12246 : 1625 : c = TREE_OPERAND (c, 0);
12247 : 15185 : if (TREE_CODE (c) != COMPONENT_REF)
12248 : 14663 : continue;
12249 : 522 : if (!seen_components)
12250 : 479 : seen_components = new hash_set<tree_operand_hash> ();
12251 : 522 : if (!shown_error)
12252 : 479 : shown_error = new hash_set<tree_operand_hash> ();
12253 : 522 : if (seen_components->contains (c)
12254 : 522 : && !shown_error->contains (c))
12255 : : {
12256 : 10 : error_at (OMP_CLAUSE_LOCATION (grp_end),
12257 : : "%qE appears more than once in map clauses",
12258 : 5 : OMP_CLAUSE_DECL (grp_end));
12259 : 5 : shown_error->add (c);
12260 : : }
12261 : : else
12262 : 517 : seen_components->add (c);
12263 : :
12264 : 522 : omp_mapping_group *struct_group;
12265 : 522 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
12266 : 522 : && *grp->grp_start == grp_end)
12267 : : {
12268 : 78 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
12269 : : struct_group, grp);
12270 : : /* Remove the whole of this mapping -- redundant. */
12271 : 78 : grp->deleted = true;
12272 : : }
12273 : : }
12274 : :
12275 : 9469 : if (seen_components)
12276 : 479 : delete seen_components;
12277 : 9469 : if (shown_error)
12278 : 479 : delete shown_error;
12279 : 9469 : }
12280 : :
12281 : : /* Link node NEWNODE so it is pointed to by chain INSERT_AT. NEWNODE's chain
12282 : : is linked to the previous node pointed to by INSERT_AT. */
12283 : :
12284 : : static tree *
12285 : 1076 : omp_siblist_insert_node_after (tree newnode, tree *insert_at)
12286 : : {
12287 : 1076 : OMP_CLAUSE_CHAIN (newnode) = *insert_at;
12288 : 1076 : *insert_at = newnode;
12289 : 1076 : return &OMP_CLAUSE_CHAIN (newnode);
12290 : : }
12291 : :
12292 : : /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
12293 : : pointed to by chain MOVE_AFTER instead. */
12294 : :
12295 : : static void
12296 : 863 : omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
12297 : : {
12298 : 863 : gcc_assert (node == *old_pos);
12299 : 863 : *old_pos = OMP_CLAUSE_CHAIN (node);
12300 : 863 : OMP_CLAUSE_CHAIN (node) = *move_after;
12301 : 863 : *move_after = node;
12302 : 863 : }
12303 : :
12304 : : /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
12305 : : LAST_NODE to after MOVE_AFTER chain. Similar to below function, but no
12306 : : new nodes are prepended to the list before splicing into the new position.
12307 : : Return the position we should continue scanning the list at, or NULL to
12308 : : stay where we were. */
12309 : :
12310 : : static tree *
12311 : 254 : omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
12312 : : tree *move_after)
12313 : : {
12314 : 254 : if (first_ptr == move_after)
12315 : : return NULL;
12316 : :
12317 : 243 : tree tmp = *first_ptr;
12318 : 243 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12319 : 243 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12320 : 243 : *move_after = tmp;
12321 : :
12322 : 243 : return first_ptr;
12323 : : }
12324 : :
12325 : : /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
12326 : : [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
12327 : : pointer MOVE_AFTER.
12328 : :
12329 : : The latter list was previously part of the OMP clause list, and the former
12330 : : (prepended) part is comprised of new nodes.
12331 : :
12332 : : We start with a list of nodes starting with a struct mapping node. We
12333 : : rearrange the list so that new nodes starting from FIRST_NEW and whose last
12334 : : node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
12335 : : the group of mapping nodes we are currently processing (from the chain
12336 : : FIRST_PTR to LAST_NODE). The return value is the pointer to the next chain
12337 : : we should continue processing from, or NULL to stay where we were.
12338 : :
12339 : : The transformation (in the case where MOVE_AFTER and FIRST_PTR are
12340 : : different) is worked through below. Here we are processing LAST_NODE, and
12341 : : FIRST_PTR points at the preceding mapping clause:
12342 : :
12343 : : #. mapping node chain
12344 : : ---------------------------------------------------
12345 : : A. struct_node [->B]
12346 : : B. comp_1 [->C]
12347 : : C. comp_2 [->D (move_after)]
12348 : : D. map_to_3 [->E]
12349 : : E. attach_3 [->F (first_ptr)]
12350 : : F. map_to_4 [->G (continue_at)]
12351 : : G. attach_4 (last_node) [->H]
12352 : : H. ...
12353 : :
12354 : : *last_new_tail = *first_ptr;
12355 : :
12356 : : I. new_node (first_new) [->F (last_new_tail)]
12357 : :
12358 : : *first_ptr = OMP_CLAUSE_CHAIN (last_node)
12359 : :
12360 : : #. mapping node chain
12361 : : ----------------------------------------------------
12362 : : A. struct_node [->B]
12363 : : B. comp_1 [->C]
12364 : : C. comp_2 [->D (move_after)]
12365 : : D. map_to_3 [->E]
12366 : : E. attach_3 [->H (first_ptr)]
12367 : : F. map_to_4 [->G (continue_at)]
12368 : : G. attach_4 (last_node) [->H]
12369 : : H. ...
12370 : :
12371 : : I. new_node (first_new) [->F (last_new_tail)]
12372 : :
12373 : : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12374 : :
12375 : : #. mapping node chain
12376 : : ---------------------------------------------------
12377 : : A. struct_node [->B]
12378 : : B. comp_1 [->C]
12379 : : C. comp_2 [->D (move_after)]
12380 : : D. map_to_3 [->E]
12381 : : E. attach_3 [->H (continue_at)]
12382 : : F. map_to_4 [->G]
12383 : : G. attach_4 (last_node) [->D]
12384 : : H. ...
12385 : :
12386 : : I. new_node (first_new) [->F (last_new_tail)]
12387 : :
12388 : : *move_after = first_new;
12389 : :
12390 : : #. mapping node chain
12391 : : ---------------------------------------------------
12392 : : A. struct_node [->B]
12393 : : B. comp_1 [->C]
12394 : : C. comp_2 [->I (move_after)]
12395 : : D. map_to_3 [->E]
12396 : : E. attach_3 [->H (continue_at)]
12397 : : F. map_to_4 [->G]
12398 : : G. attach_4 (last_node) [->D]
12399 : : H. ...
12400 : : I. new_node (first_new) [->F (last_new_tail)]
12401 : :
12402 : : or, in order:
12403 : :
12404 : : #. mapping node chain
12405 : : ---------------------------------------------------
12406 : : A. struct_node [->B]
12407 : : B. comp_1 [->C]
12408 : : C. comp_2 [->I (move_after)]
12409 : : I. new_node (first_new) [->F (last_new_tail)]
12410 : : F. map_to_4 [->G]
12411 : : G. attach_4 (last_node) [->D]
12412 : : D. map_to_3 [->E]
12413 : : E. attach_3 [->H (continue_at)]
12414 : : H. ...
12415 : : */
12416 : :
12417 : : static tree *
12418 : 71 : omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
12419 : : tree *first_ptr, tree last_node,
12420 : : tree *move_after)
12421 : : {
12422 : 71 : tree *continue_at = NULL;
12423 : 71 : *last_new_tail = *first_ptr;
12424 : 71 : if (first_ptr == move_after)
12425 : 12 : *move_after = first_new;
12426 : : else
12427 : : {
12428 : 59 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
12429 : 59 : continue_at = first_ptr;
12430 : 59 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
12431 : 59 : *move_after = first_new;
12432 : : }
12433 : 71 : return continue_at;
12434 : : }
12435 : :
12436 : : static omp_addr_token *
12437 : 10057 : omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
12438 : : {
12439 : 10057 : using namespace omp_addr_tokenizer;
12440 : 10057 : int idx = addr_tokens.length () - 1;
12441 : 10057 : gcc_assert (idx >= 0);
12442 : 10057 : if (addr_tokens[idx]->type != ACCESS_METHOD)
12443 : : return addr_tokens[idx];
12444 : 10084 : while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
12445 : : idx--;
12446 : 10057 : return addr_tokens[idx];
12447 : : }
12448 : :
12449 : : /* Mapping struct members causes an additional set of nodes to be created,
12450 : : starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
12451 : : number of members being mapped, in order of ascending position (address or
12452 : : bitwise).
12453 : :
12454 : : We scan through the list of mapping clauses, calling this function for each
12455 : : struct member mapping we find, and build up the list of mappings after the
12456 : : initial GOMP_MAP_STRUCT node. For pointer members, these will be
12457 : : newly-created ALLOC nodes. For non-pointer members, the existing mapping is
12458 : : moved into place in the sorted list.
12459 : :
12460 : : struct {
12461 : : int *a;
12462 : : int *b;
12463 : : int c;
12464 : : int *d;
12465 : : };
12466 : :
12467 : : #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
12468 : : struct.d[0:n])
12469 : :
12470 : : GOMP_MAP_STRUCT (4)
12471 : : [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
12472 : : GOMP_MAP_ALLOC (struct.a)
12473 : : GOMP_MAP_ALLOC (struct.b)
12474 : : GOMP_MAP_TO (struct.c)
12475 : : GOMP_MAP_ALLOC (struct.d)
12476 : : ...
12477 : :
12478 : : In the case where we are mapping references to pointers, or in Fortran if
12479 : : we are mapping an array with a descriptor, additional nodes may be created
12480 : : after the struct node list also.
12481 : :
12482 : : The return code is either a pointer to the next node to process (if the
12483 : : list has been rearranged), else NULL to continue with the next node in the
12484 : : original list. */
12485 : :
12486 : : static tree *
12487 : 4050 : omp_accumulate_sibling_list (enum omp_region_type region_type,
12488 : : enum tree_code code,
12489 : : hash_map<tree_operand_hash, tree>
12490 : : *&struct_map_to_clause,
12491 : : hash_map<tree_operand_hash_no_se,
12492 : : omp_mapping_group *> *group_map,
12493 : : tree *grp_start_p, tree grp_end,
12494 : : vec<omp_addr_token *> &addr_tokens, tree **inner,
12495 : : bool *fragile_p, bool reprocessing_struct,
12496 : : tree **added_tail)
12497 : : {
12498 : 4050 : using namespace omp_addr_tokenizer;
12499 : 4050 : poly_offset_int coffset;
12500 : 4050 : poly_int64 cbitpos;
12501 : 4050 : tree ocd = OMP_CLAUSE_DECL (grp_end);
12502 : 4050 : bool openmp = !(region_type & ORT_ACC);
12503 : 4050 : bool target = (region_type & ORT_TARGET) != 0;
12504 : 4050 : tree *continue_at = NULL;
12505 : :
12506 : 4363 : while (TREE_CODE (ocd) == ARRAY_REF)
12507 : 313 : ocd = TREE_OPERAND (ocd, 0);
12508 : :
12509 : 4050 : if (*fragile_p)
12510 : : {
12511 : 156 : omp_mapping_group *to_group
12512 : 156 : = omp_get_nonfirstprivate_group (group_map, ocd, true);
12513 : :
12514 : 156 : if (to_group)
12515 : : return NULL;
12516 : : }
12517 : :
12518 : 3962 : omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
12519 : 3962 : if (last_token->type == ACCESS_METHOD)
12520 : : {
12521 : 3962 : switch (last_token->u.access_kind)
12522 : : {
12523 : 735 : case ACCESS_REF:
12524 : 735 : case ACCESS_REF_TO_POINTER:
12525 : 735 : case ACCESS_REF_TO_POINTER_OFFSET:
12526 : 735 : case ACCESS_INDEXED_REF_TO_ARRAY:
12527 : : /* We may see either a bare reference or a dereferenced
12528 : : "convert_from_reference"-like one here. Handle either way. */
12529 : 735 : if (TREE_CODE (ocd) == INDIRECT_REF)
12530 : 64 : ocd = TREE_OPERAND (ocd, 0);
12531 : 735 : gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
12532 : : break;
12533 : :
12534 : : default:
12535 : : ;
12536 : : }
12537 : : }
12538 : :
12539 : 3962 : bool variable_offset;
12540 : 3962 : tree base
12541 : 3962 : = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset);
12542 : :
12543 : 3962 : int base_token;
12544 : 19982 : for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
12545 : : {
12546 : 16020 : if (addr_tokens[base_token]->type == ARRAY_BASE
12547 : 16020 : || addr_tokens[base_token]->type == STRUCTURE_BASE)
12548 : : break;
12549 : : }
12550 : :
12551 : : /* The two expressions in the assertion below aren't quite the same: if we
12552 : : have 'struct_base_decl access_indexed_array' for something like
12553 : : "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
12554 : : will be "myvar[2]" -- the actual base of the structure.
12555 : : The former interpretation leads to a strange situation where we get
12556 : : struct(myvar) alloc(myvar[2].ptr1)
12557 : : That is, the array of structures is kind of treated as one big structure
12558 : : for the purposes of gathering sibling lists, etc. */
12559 : : /* gcc_assert (base == addr_tokens[base_token]->expr); */
12560 : :
12561 : 3962 : bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
12562 : : == GOMP_MAP_ATTACH_DETACH)
12563 : 3962 : || (OMP_CLAUSE_MAP_KIND (grp_end)
12564 : 3962 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
12565 : 3962 : bool has_descriptor = false;
12566 : 3962 : if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12567 : : {
12568 : 2421 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
12569 : 2421 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
12570 : : has_descriptor = true;
12571 : : }
12572 : :
12573 : 3962 : if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
12574 : : {
12575 : 2705 : enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
12576 : :
12577 : 2705 : if (struct_map_to_clause == NULL)
12578 : 2277 : struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
12579 : :
12580 : 2705 : if (variable_offset)
12581 : 108 : str_kind = GOMP_MAP_STRUCT_UNORD;
12582 : :
12583 : 2705 : tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
12584 : :
12585 : 2705 : OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
12586 : 2705 : OMP_CLAUSE_DECL (l) = unshare_expr (base);
12587 : 2705 : OMP_CLAUSE_SIZE (l) = size_int (1);
12588 : :
12589 : 2705 : struct_map_to_clause->put (base, l);
12590 : :
12591 : : /* On first iterating through the clause list, we insert the struct node
12592 : : just before the component access node that triggers the initial
12593 : : omp_accumulate_sibling_list call for a particular sibling list (and
12594 : : it then forms the first entry in that list). When reprocessing
12595 : : struct bases that are themselves component accesses, we insert the
12596 : : struct node on an off-side list to avoid inserting the new
12597 : : GOMP_MAP_STRUCT into the middle of the old one. */
12598 : 2705 : tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
12599 : :
12600 : 2705 : if (has_descriptor)
12601 : : {
12602 : 474 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12603 : 474 : if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
12604 : 97 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12605 : 474 : tree sc = *insert_node_pos;
12606 : 474 : OMP_CLAUSE_CHAIN (l) = desc;
12607 : 474 : OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
12608 : 474 : OMP_CLAUSE_CHAIN (desc) = sc;
12609 : 474 : *insert_node_pos = l;
12610 : : }
12611 : 2231 : else if (attach_detach)
12612 : : {
12613 : 1385 : tree extra_node;
12614 : 1385 : tree alloc_node
12615 : 1385 : = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
12616 : : &extra_node);
12617 : 1385 : tree *tail;
12618 : 1385 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12619 : :
12620 : 1385 : if (extra_node)
12621 : : {
12622 : 0 : OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
12623 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12624 : 0 : tail = &OMP_CLAUSE_CHAIN (extra_node);
12625 : : }
12626 : : else
12627 : : {
12628 : 1385 : OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
12629 : 1385 : tail = &OMP_CLAUSE_CHAIN (alloc_node);
12630 : : }
12631 : :
12632 : : /* For OpenMP semantics, we don't want to implicitly allocate
12633 : : space for the pointer here for non-compute regions (e.g. "enter
12634 : : data"). A FRAGILE_P node is only being created so that
12635 : : omp-low.cc is able to rewrite the struct properly.
12636 : : For references (to pointers), we want to actually allocate the
12637 : : space for the reference itself in the sorted list following the
12638 : : struct node.
12639 : : For pointers, we want to allocate space if we had an explicit
12640 : : mapping of the attachment point, but not otherwise. */
12641 : 1385 : if (*fragile_p
12642 : 1385 : || (openmp
12643 : : && !target
12644 : : && attach_detach
12645 : 222 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12646 : 76 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12647 : : {
12648 : 121 : if (!lang_GNU_Fortran ())
12649 : : /* In Fortran, pointers are dereferenced automatically, but may
12650 : : be unassociated. So we still want to allocate space for the
12651 : : pointer (as the base for an attach operation that should be
12652 : : present in the same directive's clause list also). */
12653 : 103 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12654 : 121 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12655 : : }
12656 : :
12657 : 1385 : *insert_node_pos = l;
12658 : :
12659 : 1385 : if (reprocessing_struct)
12660 : : {
12661 : : /* When reprocessing a struct node group used as the base of a
12662 : : subcomponent access, if we have a reference-to-pointer base,
12663 : : we will see:
12664 : : struct(**ptr) attach(*ptr)
12665 : : whereas for a non-reprocess-struct group, we see, e.g.:
12666 : : tofrom(**ptr) attach(*ptr) attach(ptr)
12667 : : and we create the "alloc" for the second "attach", i.e.
12668 : : for the reference itself. When reprocessing a struct group we
12669 : : thus change the pointer attachment into a reference attachment
12670 : : by stripping the indirection. (The attachment of the
12671 : : referenced pointer must happen elsewhere, either on the same
12672 : : directive, or otherwise.) */
12673 : 180 : tree adecl = OMP_CLAUSE_DECL (alloc_node);
12674 : :
12675 : 180 : if ((TREE_CODE (adecl) == INDIRECT_REF
12676 : 148 : || (TREE_CODE (adecl) == MEM_REF
12677 : 0 : && integer_zerop (TREE_OPERAND (adecl, 1))))
12678 : 32 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
12679 : : == REFERENCE_TYPE)
12680 : 212 : && (TREE_CODE (TREE_TYPE (TREE_TYPE
12681 : : (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
12682 : 32 : OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
12683 : :
12684 : 180 : *added_tail = tail;
12685 : : }
12686 : : }
12687 : : else
12688 : : {
12689 : 846 : gcc_assert (*grp_start_p == grp_end);
12690 : 846 : if (reprocessing_struct)
12691 : : {
12692 : : /* If we don't have an attach/detach node, this is a
12693 : : "target data" directive or similar, not an offload region.
12694 : : Synthesize an "alloc" node using just the initiating
12695 : : GOMP_MAP_STRUCT decl. */
12696 : 16 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12697 : 32 : || code == OACC_EXIT_DATA)
12698 : 32 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12699 : 32 : tree alloc_node
12700 : 32 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12701 : : OMP_CLAUSE_MAP);
12702 : 32 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12703 : 32 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12704 : 64 : OMP_CLAUSE_SIZE (alloc_node)
12705 : 32 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12706 : :
12707 : 32 : OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
12708 : 32 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12709 : 32 : *insert_node_pos = l;
12710 : 32 : *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
12711 : : }
12712 : : else
12713 : 814 : grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
12714 : : }
12715 : :
12716 : 2705 : unsigned last_access = base_token + 1;
12717 : :
12718 : 2705 : while (last_access + 1 < addr_tokens.length ()
12719 : 2873 : && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
12720 : : last_access++;
12721 : :
12722 : 2705 : if ((region_type & ORT_TARGET)
12723 : 2705 : && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
12724 : : {
12725 : 1652 : bool base_ref = false;
12726 : 1652 : access_method_kinds access_kind
12727 : 1652 : = addr_tokens[last_access]->u.access_kind;
12728 : :
12729 : 1652 : switch (access_kind)
12730 : : {
12731 : : case ACCESS_DIRECT:
12732 : : case ACCESS_INDEXED_ARRAY:
12733 : 1004 : return NULL;
12734 : :
12735 : 403 : case ACCESS_REF:
12736 : 403 : case ACCESS_REF_TO_POINTER:
12737 : 403 : case ACCESS_REF_TO_POINTER_OFFSET:
12738 : 403 : case ACCESS_INDEXED_REF_TO_ARRAY:
12739 : 403 : base_ref = true;
12740 : 403 : break;
12741 : :
12742 : 866 : default:
12743 : 866 : ;
12744 : : }
12745 : 866 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12746 : : OMP_CLAUSE_MAP);
12747 : 866 : enum gomp_map_kind mkind;
12748 : 866 : omp_mapping_group *decl_group;
12749 : 866 : tree use_base;
12750 : 866 : switch (access_kind)
12751 : : {
12752 : 463 : case ACCESS_POINTER:
12753 : 463 : case ACCESS_POINTER_OFFSET:
12754 : 463 : use_base = addr_tokens[last_access]->expr;
12755 : 463 : break;
12756 : 198 : case ACCESS_REF_TO_POINTER:
12757 : 198 : case ACCESS_REF_TO_POINTER_OFFSET:
12758 : 198 : use_base
12759 : 198 : = build_fold_indirect_ref (addr_tokens[last_access]->expr);
12760 : 198 : break;
12761 : 205 : default:
12762 : 205 : use_base = addr_tokens[base_token]->expr;
12763 : : }
12764 : 866 : bool mapped_to_p
12765 : 866 : = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
12766 : : true, false, true);
12767 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12768 : 866 : && DECL_P (addr_tokens[last_access]->expr)
12769 : 1372 : && !mapped_to_p)
12770 : 444 : mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
12771 : : : GOMP_MAP_FIRSTPRIVATE_POINTER;
12772 : : else
12773 : : mkind = GOMP_MAP_ATTACH_DETACH;
12774 : :
12775 : 866 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
12776 : : /* If we have a reference to pointer base, we want to attach the
12777 : : pointer here, not the reference. The reference attachment happens
12778 : : elsewhere. */
12779 : 866 : bool ref_to_ptr
12780 : 866 : = (access_kind == ACCESS_REF_TO_POINTER
12781 : 866 : || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
12782 : 866 : tree sdecl = addr_tokens[last_access]->expr;
12783 : 866 : tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
12784 : : : sdecl;
12785 : : /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
12786 : : want to use the reference itself for the decl, but we
12787 : : still want to use the pointer to calculate the bias. */
12788 : 866 : OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
12789 : 866 : ? sdecl_ptr : sdecl;
12790 : 866 : sdecl = sdecl_ptr;
12791 : 866 : tree baddr = build_fold_addr_expr (base);
12792 : 866 : baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12793 : : ptrdiff_type_node, baddr);
12794 : 866 : tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12795 : : ptrdiff_type_node, sdecl);
12796 : 866 : OMP_CLAUSE_SIZE (c2)
12797 : 866 : = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
12798 : : ptrdiff_type_node, baddr, decladdr);
12799 : : /* Insert after struct node. */
12800 : 866 : OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
12801 : 866 : OMP_CLAUSE_CHAIN (l) = c2;
12802 : :
12803 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12804 : 866 : && (addr_tokens[base_token]->u.structure_base_kind
12805 : : == BASE_COMPONENT_EXPR)
12806 : 286 : && mkind == GOMP_MAP_ATTACH_DETACH
12807 : 1152 : && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
12808 : : {
12809 : 218 : *inner = insert_node_pos;
12810 : 218 : if (openmp)
12811 : 166 : *fragile_p = true;
12812 : 218 : return NULL;
12813 : : }
12814 : : }
12815 : :
12816 : 1701 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12817 : 1701 : && (addr_tokens[base_token]->u.structure_base_kind
12818 : : == BASE_COMPONENT_EXPR)
12819 : 2016 : && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
12820 : 100 : *inner = insert_node_pos;
12821 : :
12822 : 1701 : return NULL;
12823 : : }
12824 : 1257 : else if (struct_map_to_clause)
12825 : : {
12826 : 1257 : tree *osc = struct_map_to_clause->get (base);
12827 : 1257 : tree *sc = NULL, *scp = NULL;
12828 : 1257 : bool unordered = false;
12829 : :
12830 : 1257 : if (osc && OMP_CLAUSE_MAP_KIND (*osc) == GOMP_MAP_STRUCT_UNORD)
12831 : : unordered = true;
12832 : :
12833 : 1257 : unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
12834 : 1257 : sc = &OMP_CLAUSE_CHAIN (*osc);
12835 : : /* The struct mapping might be immediately followed by a
12836 : : FIRSTPRIVATE_POINTER, FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
12837 : : if it's an indirect access or a reference, or if the structure base
12838 : : is not a decl. The FIRSTPRIVATE_* nodes are removed in omp-low.cc
12839 : : after they have been processed there, and ATTACH_DETACH nodes are
12840 : : recomputed and moved out of the GOMP_MAP_STRUCT construct once
12841 : : sibling list building is complete. */
12842 : 1257 : if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
12843 : 1212 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
12844 : 2424 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
12845 : 201 : sc = &OMP_CLAUSE_CHAIN (*sc);
12846 : 3453 : for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
12847 : 2580 : if (attach_detach && sc == grp_start_p)
12848 : : break;
12849 : 2580 : else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
12850 : 182 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
12851 : 2762 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
12852 : : break;
12853 : : else
12854 : : {
12855 : 2580 : tree sc_decl = OMP_CLAUSE_DECL (*sc);
12856 : 2580 : poly_offset_int offset;
12857 : 2580 : poly_int64 bitpos;
12858 : :
12859 : 2580 : if (TREE_CODE (sc_decl) == ARRAY_REF)
12860 : : {
12861 : 366 : while (TREE_CODE (sc_decl) == ARRAY_REF)
12862 : 184 : sc_decl = TREE_OPERAND (sc_decl, 0);
12863 : 182 : if (TREE_CODE (sc_decl) != COMPONENT_REF
12864 : 182 : || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
12865 : : break;
12866 : : }
12867 : 2398 : else if (INDIRECT_REF_P (sc_decl)
12868 : 0 : && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
12869 : 2398 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
12870 : : == REFERENCE_TYPE))
12871 : 0 : sc_decl = TREE_OPERAND (sc_decl, 0);
12872 : :
12873 : 2580 : bool variable_offset2;
12874 : 2580 : tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
12875 : : &variable_offset2);
12876 : 2580 : if (!base2 || !operand_equal_p (base2, base, 0))
12877 : : break;
12878 : 2580 : if (scp)
12879 : 220 : continue;
12880 : 2360 : if (variable_offset2)
12881 : : {
12882 : 48 : OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
12883 : 48 : unordered = true;
12884 : 48 : break;
12885 : : }
12886 : 2312 : else if ((region_type & ORT_ACC) != 0)
12887 : : {
12888 : : /* For OpenACC, allow (ignore) duplicate struct accesses in
12889 : : the middle of a mapping clause, e.g. "mystruct->foo" in:
12890 : : copy(mystruct->foo->bar) copy(mystruct->foo->qux). */
12891 : 223 : if (reprocessing_struct
12892 : 8 : && known_eq (coffset, offset)
12893 : 223 : && known_eq (cbitpos, bitpos))
12894 : 21 : return NULL;
12895 : : }
12896 : 2089 : else if (known_eq (coffset, offset)
12897 : 2089 : && known_eq (cbitpos, bitpos))
12898 : : {
12899 : : /* Having two struct members at the same offset doesn't work,
12900 : : so make sure we don't. (We're allowed to ignore this.
12901 : : Should we report the error?) */
12902 : : /*error_at (OMP_CLAUSE_LOCATION (grp_end),
12903 : : "duplicate struct member %qE in map clauses",
12904 : : OMP_CLAUSE_DECL (grp_end));*/
12905 : : return NULL;
12906 : : }
12907 : 2291 : if (maybe_lt (coffset, offset)
12908 : 4001 : || (known_eq (coffset, offset)
12909 : 2 : && maybe_lt (cbitpos, bitpos)))
12910 : : {
12911 : 581 : if (attach_detach)
12912 : : scp = sc;
12913 : : else
12914 : : break;
12915 : : }
12916 : : }
12917 : :
12918 : : /* If this is an unordered struct, just insert the new element at the
12919 : : end of the list. */
12920 : 1236 : if (unordered)
12921 : : {
12922 : 102 : for (; i < elems; i++)
12923 : 54 : sc = &OMP_CLAUSE_CHAIN (*sc);
12924 : : scp = NULL;
12925 : : }
12926 : :
12927 : 1236 : OMP_CLAUSE_SIZE (*osc)
12928 : 1236 : = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
12929 : :
12930 : 1236 : if (reprocessing_struct)
12931 : : {
12932 : : /* If we're reprocessing a struct node, we don't want to do most of
12933 : : the list manipulation below. We only need to handle the (pointer
12934 : : or reference) attach/detach case. */
12935 : 8 : tree extra_node, alloc_node;
12936 : 8 : if (has_descriptor)
12937 : 0 : gcc_unreachable ();
12938 : 8 : else if (attach_detach)
12939 : 8 : alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12940 : : grp_end, &extra_node);
12941 : : else
12942 : : {
12943 : : /* If we don't have an attach/detach node, this is a
12944 : : "target data" directive or similar, not an offload region.
12945 : : Synthesize an "alloc" node using just the initiating
12946 : : GOMP_MAP_STRUCT decl. */
12947 : 0 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12948 : 0 : || code == OACC_EXIT_DATA)
12949 : 0 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12950 : 0 : alloc_node
12951 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12952 : : OMP_CLAUSE_MAP);
12953 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12954 : 0 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12955 : 0 : OMP_CLAUSE_SIZE (alloc_node)
12956 : 0 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12957 : : }
12958 : :
12959 : 8 : if (scp)
12960 : 0 : omp_siblist_insert_node_after (alloc_node, scp);
12961 : : else
12962 : : {
12963 : 8 : tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
12964 : 8 : if (sc == *added_tail)
12965 : 8 : *added_tail = new_end;
12966 : : }
12967 : :
12968 : 8 : return NULL;
12969 : : }
12970 : :
12971 : 1228 : if (has_descriptor)
12972 : : {
12973 : 339 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12974 : 339 : if (code == OMP_TARGET_EXIT_DATA
12975 : 339 : || code == OACC_EXIT_DATA)
12976 : 19 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12977 : 678 : omp_siblist_move_node_after (desc,
12978 : 339 : &OMP_CLAUSE_CHAIN (*grp_start_p),
12979 : : scp ? scp : sc);
12980 : : }
12981 : 889 : else if (attach_detach)
12982 : : {
12983 : 325 : tree cl = NULL_TREE, extra_node;
12984 : 325 : tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12985 : : grp_end, &extra_node);
12986 : 325 : tree *tail_chain = NULL;
12987 : :
12988 : 325 : if (*fragile_p
12989 : 325 : || (openmp
12990 : : && !target
12991 : : && attach_detach
12992 : 62 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12993 : 23 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12994 : : {
12995 : 6 : if (!lang_GNU_Fortran ())
12996 : 6 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12997 : 6 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12998 : : }
12999 : :
13000 : : /* Here, we have:
13001 : :
13002 : : grp_end : the last (or only) node in this group.
13003 : : grp_start_p : pointer to the first node in a pointer mapping group
13004 : : up to and including GRP_END.
13005 : : sc : pointer to the chain for the end of the struct component
13006 : : list.
13007 : : scp : pointer to the chain for the sorted position at which we
13008 : : should insert in the middle of the struct component list
13009 : : (else NULL to insert at end).
13010 : : alloc_node : the "alloc" node for the structure (pointer-type)
13011 : : component. We insert at SCP (if present), else SC
13012 : : (the end of the struct component list).
13013 : : extra_node : a newly-synthesized node for an additional indirect
13014 : : pointer mapping or a Fortran pointer set, if needed.
13015 : : cl : first node to prepend before grp_start_p.
13016 : : tail_chain : pointer to chain of last prepended node.
13017 : :
13018 : : The general idea is we move the nodes for this struct mapping
13019 : : together: the alloc node goes into the sorted list directly after
13020 : : the struct mapping, and any extra nodes (together with the nodes
13021 : : mapping arrays pointed to by struct components) get moved after
13022 : : that list. When SCP is NULL, we insert the nodes at SC, i.e. at
13023 : : the end of the struct component mapping list. It's important that
13024 : : the alloc_node comes first in that case because it's part of the
13025 : : sorted component mapping list (but subsequent nodes are not!). */
13026 : :
13027 : 325 : if (scp)
13028 : 254 : omp_siblist_insert_node_after (alloc_node, scp);
13029 : :
13030 : : /* Make [cl,tail_chain] a list of the alloc node (if we haven't
13031 : : already inserted it) and the extra_node (if it is present). The
13032 : : list can be empty if we added alloc_node above and there is no
13033 : : extra node. */
13034 : 254 : if (scp && extra_node)
13035 : : {
13036 : 0 : cl = extra_node;
13037 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13038 : : }
13039 : 325 : else if (extra_node)
13040 : : {
13041 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
13042 : 0 : cl = alloc_node;
13043 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
13044 : : }
13045 : 325 : else if (!scp)
13046 : : {
13047 : 71 : cl = alloc_node;
13048 : 71 : tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
13049 : : }
13050 : :
13051 : 325 : continue_at
13052 : 71 : = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
13053 : : grp_start_p, grp_end,
13054 : : sc)
13055 : 254 : : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
13056 : : }
13057 : 564 : else if (*sc != grp_end)
13058 : : {
13059 : 524 : gcc_assert (*grp_start_p == grp_end);
13060 : :
13061 : : /* We are moving the current node back to a previous struct node:
13062 : : the node that used to point to the current node will now point to
13063 : : the next node. */
13064 : 524 : continue_at = grp_start_p;
13065 : : /* In the non-pointer case, the mapping clause itself is moved into
13066 : : the correct position in the struct component list, which in this
13067 : : case is just SC. */
13068 : 524 : omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
13069 : : }
13070 : : }
13071 : : return continue_at;
13072 : : }
13073 : :
13074 : : /* Scan through GROUPS, and create sorted structure sibling lists without
13075 : : gimplifying. */
13076 : :
13077 : : static bool
13078 : 17170 : omp_build_struct_sibling_lists (enum tree_code code,
13079 : : enum omp_region_type region_type,
13080 : : vec<omp_mapping_group> *groups,
13081 : : hash_map<tree_operand_hash_no_se,
13082 : : omp_mapping_group *> **grpmap,
13083 : : tree *list_p)
13084 : : {
13085 : 17170 : using namespace omp_addr_tokenizer;
13086 : 17170 : unsigned i;
13087 : 17170 : omp_mapping_group *grp;
13088 : 17170 : hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
13089 : 17170 : bool success = true;
13090 : 17170 : tree *new_next = NULL;
13091 : 34340 : tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
13092 : 17170 : tree added_nodes = NULL_TREE;
13093 : 17170 : tree *added_tail = &added_nodes;
13094 : 17170 : auto_vec<omp_mapping_group> pre_hwm_groups;
13095 : :
13096 : 64062 : FOR_EACH_VEC_ELT (*groups, i, grp)
13097 : : {
13098 : 29722 : tree c = grp->grp_end;
13099 : 29722 : tree decl = OMP_CLAUSE_DECL (c);
13100 : 29722 : tree grp_end = grp->grp_end;
13101 : 29722 : auto_vec<omp_addr_token *> addr_tokens;
13102 : 29722 : tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
13103 : :
13104 : 29722 : if (new_next && !grp->reprocess_struct)
13105 : 510 : grp->grp_start = new_next;
13106 : :
13107 : 29722 : new_next = NULL;
13108 : :
13109 : 29722 : tree *grp_start_p = grp->grp_start;
13110 : :
13111 : 29722 : if (DECL_P (decl))
13112 : 20393 : continue;
13113 : :
13114 : : /* Skip groups we marked for deletion in
13115 : : {omp,oacc}_resolve_clause_dependencies. */
13116 : 9329 : if (grp->deleted)
13117 : 462 : continue;
13118 : :
13119 : 8867 : if (OMP_CLAUSE_CHAIN (*grp_start_p)
13120 : 8867 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
13121 : : {
13122 : : /* Don't process an array descriptor that isn't inside a derived type
13123 : : as a struct (the GOMP_MAP_POINTER following will have the form
13124 : : "var.data", but such mappings are handled specially). */
13125 : 5407 : tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
13126 : 5407 : if (omp_map_clause_descriptor_p (grpmid)
13127 : 9039 : && DECL_P (OMP_CLAUSE_DECL (grpmid)))
13128 : 2772 : continue;
13129 : : }
13130 : :
13131 : : tree expr = decl;
13132 : :
13133 : 7122 : while (TREE_CODE (expr) == ARRAY_REF)
13134 : 1027 : expr = TREE_OPERAND (expr, 0);
13135 : :
13136 : 6095 : if (!omp_parse_expr (addr_tokens, expr))
13137 : 0 : continue;
13138 : :
13139 : 6095 : omp_addr_token *last_token
13140 : 6095 : = omp_first_chained_access_token (addr_tokens);
13141 : :
13142 : : /* A mapping of a reference to a pointer member that doesn't specify an
13143 : : array section, etc., like this:
13144 : : *mystruct.ref_to_ptr
13145 : : should not be processed by the struct sibling-list handling code --
13146 : : it just transfers the referenced pointer.
13147 : :
13148 : : In contrast, the quite similar-looking construct:
13149 : : *mystruct.ptr
13150 : : which is equivalent to e.g.
13151 : : mystruct.ptr[0]
13152 : : *does* trigger sibling-list processing.
13153 : :
13154 : : An exception for the former case is for "fragile" groups where the
13155 : : reference itself is not handled otherwise; this is subject to special
13156 : : handling in omp_accumulate_sibling_list also. */
13157 : :
13158 : 6095 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
13159 : 2853 : && last_token->type == ACCESS_METHOD
13160 : 2853 : && last_token->u.access_kind == ACCESS_REF
13161 : 6379 : && !grp->fragile)
13162 : 219 : continue;
13163 : :
13164 : 5876 : tree d = decl;
13165 : 5876 : if (TREE_CODE (d) == ARRAY_REF)
13166 : : {
13167 : 1998 : while (TREE_CODE (d) == ARRAY_REF)
13168 : 1010 : d = TREE_OPERAND (d, 0);
13169 : 988 : if (TREE_CODE (d) == COMPONENT_REF
13170 : 988 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
13171 : : decl = d;
13172 : : }
13173 : 5876 : if (d == decl
13174 : 5199 : && INDIRECT_REF_P (decl)
13175 : 717 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
13176 : 105 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
13177 : : == REFERENCE_TYPE)
13178 : 5973 : && (OMP_CLAUSE_MAP_KIND (c)
13179 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
13180 : 97 : decl = TREE_OPERAND (decl, 0);
13181 : :
13182 : 5876 : STRIP_NOPS (decl);
13183 : :
13184 : 5876 : if (TREE_CODE (decl) != COMPONENT_REF)
13185 : 1339 : continue;
13186 : :
13187 : : /* If we're mapping the whole struct in another node, skip adding this
13188 : : node to a sibling list. */
13189 : 4537 : omp_mapping_group *wholestruct;
13190 : 4537 : if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
13191 : : &wholestruct))
13192 : 125 : continue;
13193 : :
13194 : 4412 : if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
13195 : 4412 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
13196 : 4308 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
13197 : : && code != OACC_UPDATE
13198 : 8653 : && code != OMP_TARGET_UPDATE)
13199 : : {
13200 : 4050 : if (error_operand_p (decl))
13201 : : {
13202 : 0 : success = false;
13203 : 0 : goto error_out;
13204 : : }
13205 : :
13206 : 4050 : tree stype = TREE_TYPE (decl);
13207 : 4050 : if (TREE_CODE (stype) == REFERENCE_TYPE)
13208 : 768 : stype = TREE_TYPE (stype);
13209 : 4050 : if (TYPE_SIZE_UNIT (stype) == NULL
13210 : 4050 : || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
13211 : : {
13212 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
13213 : : "mapping field %qE of variable length "
13214 : 0 : "structure", OMP_CLAUSE_DECL (c));
13215 : 0 : success = false;
13216 : 0 : goto error_out;
13217 : : }
13218 : :
13219 : 4050 : tree *inner = NULL;
13220 : 4050 : bool fragile_p = grp->fragile;
13221 : :
13222 : 4050 : new_next
13223 : 8100 : = omp_accumulate_sibling_list (region_type, code,
13224 : : struct_map_to_clause, *grpmap,
13225 : : grp_start_p, grp_end, addr_tokens,
13226 : : &inner, &fragile_p,
13227 : 4050 : grp->reprocess_struct, &added_tail);
13228 : :
13229 : 4050 : if (inner)
13230 : : {
13231 : 318 : omp_mapping_group newgrp;
13232 : 318 : newgrp.grp_start = inner;
13233 : 318 : if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
13234 : : == GOMP_MAP_ATTACH_DETACH)
13235 : 286 : newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
13236 : : else
13237 : 32 : newgrp.grp_end = *inner;
13238 : 318 : newgrp.mark = UNVISITED;
13239 : 318 : newgrp.sibling = NULL;
13240 : 318 : newgrp.deleted = false;
13241 : 318 : newgrp.reprocess_struct = true;
13242 : 318 : newgrp.fragile = fragile_p;
13243 : 318 : newgrp.next = NULL;
13244 : 318 : groups->safe_push (newgrp);
13245 : :
13246 : : /* !!! Growing GROUPS might invalidate the pointers in the group
13247 : : map. Rebuild it here. This is a bit inefficient, but
13248 : : shouldn't happen very often. */
13249 : 636 : delete (*grpmap);
13250 : 318 : *grpmap
13251 : 318 : = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
13252 : : sentinel);
13253 : : }
13254 : : }
13255 : 29722 : }
13256 : :
13257 : : /* Delete groups marked for deletion above. At this point the order of the
13258 : : groups may no longer correspond to the order of the underlying list,
13259 : : which complicates this a little. First clear out OMP_CLAUSE_DECL for
13260 : : deleted nodes... */
13261 : :
13262 : 46892 : FOR_EACH_VEC_ELT (*groups, i, grp)
13263 : 29722 : if (grp->deleted)
13264 : 462 : for (tree d = *grp->grp_start;
13265 : 924 : d != OMP_CLAUSE_CHAIN (grp->grp_end);
13266 : 462 : d = OMP_CLAUSE_CHAIN (d))
13267 : 462 : OMP_CLAUSE_DECL (d) = NULL_TREE;
13268 : :
13269 : : /* ...then sweep through the list removing the now-empty nodes. */
13270 : :
13271 : : tail = list_p;
13272 : 90814 : while (*tail)
13273 : : {
13274 : 73644 : if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
13275 : 73644 : && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
13276 : 462 : *tail = OMP_CLAUSE_CHAIN (*tail);
13277 : : else
13278 : 73182 : tail = &OMP_CLAUSE_CHAIN (*tail);
13279 : : }
13280 : :
13281 : : /* Tack on the struct nodes added during nested struct reprocessing. */
13282 : 17170 : if (added_nodes)
13283 : : {
13284 : 192 : *tail = added_nodes;
13285 : 192 : tail = added_tail;
13286 : : }
13287 : :
13288 : : /* Now we have finished building the struct sibling lists, reprocess
13289 : : newly-added "attach" nodes: we need the address of the first
13290 : : mapped element of each struct sibling list for the bias of the attach
13291 : : operation -- not necessarily the base address of the whole struct. */
13292 : 17170 : if (struct_map_to_clause)
13293 : 4982 : for (hash_map<tree_operand_hash, tree>::iterator iter
13294 : 2277 : = struct_map_to_clause->begin ();
13295 : 7259 : iter != struct_map_to_clause->end ();
13296 : 2705 : ++iter)
13297 : : {
13298 : 2705 : tree struct_node = (*iter).second;
13299 : 2705 : gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
13300 : 2705 : tree attach = OMP_CLAUSE_CHAIN (struct_node);
13301 : :
13302 : 2705 : if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
13303 : 2705 : || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
13304 : 2283 : continue;
13305 : :
13306 : 422 : OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
13307 : :
13308 : : /* Sanity check: the standalone attach node will not work if we have
13309 : : an "enter data" operation (because for those, variables need to be
13310 : : mapped separately and attach nodes must be grouped together with the
13311 : : base they attach to). We should only have created the
13312 : : ATTACH_DETACH node after GOMP_MAP_STRUCT for a target region, so
13313 : : this should never be true. */
13314 : 422 : gcc_assert ((region_type & ORT_TARGET) != 0);
13315 : :
13316 : : /* This is the first sorted node in the struct sibling list. Use it
13317 : : to recalculate the correct bias to use.
13318 : : (&first_node - attach_decl).
13319 : : For GOMP_MAP_STRUCT_UNORD, we need e.g. the
13320 : : min(min(min(first,second),third),fourth) element, because the
13321 : : elements aren't in any particular order. */
13322 : 422 : tree lowest_addr;
13323 : 422 : if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
13324 : : {
13325 : 4 : tree first_node = OMP_CLAUSE_CHAIN (attach);
13326 : 4 : unsigned HOST_WIDE_INT num_mappings
13327 : 4 : = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
13328 : 4 : lowest_addr = OMP_CLAUSE_DECL (first_node);
13329 : 4 : lowest_addr = build_fold_addr_expr (lowest_addr);
13330 : 4 : lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
13331 : 4 : tree next_node = OMP_CLAUSE_CHAIN (first_node);
13332 : 8 : while (num_mappings > 1)
13333 : : {
13334 : 4 : tree tmp = OMP_CLAUSE_DECL (next_node);
13335 : 4 : tmp = build_fold_addr_expr (tmp);
13336 : 4 : tmp = fold_convert (pointer_sized_int_node, tmp);
13337 : 4 : lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
13338 : : lowest_addr, tmp);
13339 : 4 : next_node = OMP_CLAUSE_CHAIN (next_node);
13340 : 4 : num_mappings--;
13341 : : }
13342 : 4 : lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
13343 : : }
13344 : : else
13345 : : {
13346 : 418 : tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
13347 : 418 : first_node = build_fold_addr_expr (first_node);
13348 : 418 : lowest_addr = fold_convert (ptrdiff_type_node, first_node);
13349 : : }
13350 : 422 : tree attach_decl = OMP_CLAUSE_DECL (attach);
13351 : 422 : attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
13352 : 422 : OMP_CLAUSE_SIZE (attach)
13353 : 422 : = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
13354 : : attach_decl);
13355 : :
13356 : : /* Remove GOMP_MAP_ATTACH node from after struct node. */
13357 : 422 : OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
13358 : : /* ...and re-insert it at the end of our clause list. */
13359 : 422 : *tail = attach;
13360 : 422 : OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
13361 : 422 : tail = &OMP_CLAUSE_CHAIN (attach);
13362 : : }
13363 : :
13364 : 14893 : error_out:
13365 : 17170 : if (struct_map_to_clause)
13366 : 2277 : delete struct_map_to_clause;
13367 : :
13368 : 17170 : return success;
13369 : 17170 : }
13370 : :
13371 : : struct instantiate_mapper_info
13372 : : {
13373 : : tree *mapper_clauses_p;
13374 : : struct gimplify_omp_ctx *omp_ctx;
13375 : : gimple_seq *pre_p;
13376 : : };
13377 : :
13378 : : /* Helper function for omp_instantiate_mapper. */
13379 : :
13380 : : static tree
13381 : 1272 : remap_mapper_decl_1 (tree *tp, int *walk_subtrees, void *data)
13382 : : {
13383 : 1272 : copy_body_data *id = (copy_body_data *) data;
13384 : :
13385 : 1272 : if (DECL_P (*tp))
13386 : : {
13387 : 564 : tree replacement = remap_decl (*tp, id);
13388 : 564 : if (*tp != replacement)
13389 : : {
13390 : 289 : *tp = unshare_expr (replacement);
13391 : 289 : *walk_subtrees = 0;
13392 : : }
13393 : : }
13394 : :
13395 : 1272 : return NULL_TREE;
13396 : : }
13397 : :
13398 : : /* A copy_decl implementation (for use with tree-inline.cc functions) that
13399 : : only transform decls or SSA names that are part of a map we already
13400 : : prepared. */
13401 : :
13402 : : static tree
13403 : 94 : omp_mapper_copy_decl (tree var, copy_body_data *cb)
13404 : : {
13405 : 94 : tree *repl = cb->decl_map->get (var);
13406 : :
13407 : 94 : if (repl)
13408 : 0 : return *repl;
13409 : :
13410 : 94 : return var;
13411 : : }
13412 : :
13413 : : static tree *
13414 : 80 : omp_instantiate_mapper (gimple_seq *pre_p,
13415 : : hash_map<omp_name_type<tree>, tree> *implicit_mappers,
13416 : : tree mapperfn, tree expr, enum gomp_map_kind outer_kind,
13417 : : tree *mapper_clauses_p)
13418 : : {
13419 : 80 : tree mapper_name = NULL_TREE;
13420 : 80 : tree mapper = lang_hooks.decls.omp_extract_mapper_directive (mapperfn);
13421 : 80 : gcc_assert (TREE_CODE (mapper) == OMP_DECLARE_MAPPER);
13422 : :
13423 : 80 : tree clause = OMP_DECLARE_MAPPER_CLAUSES (mapper);
13424 : 80 : tree dummy_var = OMP_DECLARE_MAPPER_DECL (mapper);
13425 : :
13426 : : /* The "extraction map" is used to map the mapper variable in the "declare
13427 : : mapper" directive, and also any temporary variables that have been created
13428 : : as part of expanding the mapper function's body (which are expanded as a
13429 : : "bind" expression in the pre_p sequence). */
13430 : 80 : hash_map<tree, tree> extraction_map;
13431 : :
13432 : 80 : extraction_map.put (dummy_var, expr);
13433 : 80 : extraction_map.put (expr, expr);
13434 : :
13435 : : /* This copy_body_data is only used to remap the decls in the
13436 : : OMP_DECLARE_MAPPER tree node expansion itself. All relevant decls should
13437 : : already be in the current function. */
13438 : 80 : copy_body_data id;
13439 : 80 : memset (&id, 0, sizeof (id));
13440 : 80 : id.src_fn = current_function_decl;
13441 : 80 : id.dst_fn = current_function_decl;
13442 : 80 : id.src_cfun = cfun;
13443 : 80 : id.decl_map = &extraction_map;
13444 : 80 : id.copy_decl = omp_mapper_copy_decl;
13445 : 80 : id.transform_call_graph_edges = CB_CGE_DUPLICATE; // ???
13446 : 80 : id.transform_new_cfg = true; // ???
13447 : :
13448 : 238 : for (; clause; clause = OMP_CLAUSE_CHAIN (clause))
13449 : : {
13450 : 158 : enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (clause);
13451 : 158 : tree *nested_mapper_p = NULL;
13452 : :
13453 : 158 : if (map_kind == GOMP_MAP_PUSH_MAPPER_NAME)
13454 : : {
13455 : 0 : mapper_name = OMP_CLAUSE_DECL (clause);
13456 : 23 : continue;
13457 : : }
13458 : 158 : else if (map_kind == GOMP_MAP_POP_MAPPER_NAME)
13459 : : {
13460 : 0 : mapper_name = NULL_TREE;
13461 : 0 : continue;
13462 : : }
13463 : :
13464 : 158 : tree decl = OMP_CLAUSE_DECL (clause);
13465 : 158 : tree unshared, type;
13466 : 158 : bool nonunit_array_with_mapper = false;
13467 : :
13468 : 158 : if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
13469 : : {
13470 : 62 : location_t loc = OMP_CLAUSE_LOCATION (clause);
13471 : 62 : tree tmp = lang_hooks.decls.omp_map_array_section (loc, decl);
13472 : 62 : if (tmp == decl)
13473 : : {
13474 : 48 : unshared = unshare_expr (clause);
13475 : 48 : nonunit_array_with_mapper = true;
13476 : 48 : type = TREE_TYPE (TREE_TYPE (decl));
13477 : : }
13478 : : else
13479 : : {
13480 : 14 : unshared = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
13481 : 14 : OMP_CLAUSE_CODE (clause));
13482 : 14 : OMP_CLAUSE_DECL (unshared) = tmp;
13483 : 14 : OMP_CLAUSE_SIZE (unshared)
13484 : 28 : = DECL_P (tmp) ? DECL_SIZE_UNIT (tmp)
13485 : 14 : : TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13486 : 14 : type = TREE_TYPE (tmp);
13487 : : }
13488 : : }
13489 : : else
13490 : : {
13491 : 96 : unshared = unshare_expr (clause);
13492 : 96 : type = TREE_TYPE (decl);
13493 : : }
13494 : :
13495 : 158 : walk_tree (&unshared, remap_mapper_decl_1, &id, NULL);
13496 : :
13497 : 158 : if (OMP_CLAUSE_MAP_KIND (unshared) == GOMP_MAP_UNSET)
13498 : 28 : OMP_CLAUSE_SET_MAP_KIND (unshared, outer_kind);
13499 : :
13500 : 158 : decl = OMP_CLAUSE_DECL (unshared);
13501 : 158 : type = TYPE_MAIN_VARIANT (type);
13502 : :
13503 : 158 : nested_mapper_p = implicit_mappers->get ({ mapper_name, type });
13504 : :
13505 : 158 : if (nested_mapper_p && *nested_mapper_p != mapperfn)
13506 : : {
13507 : 23 : if (nonunit_array_with_mapper)
13508 : : {
13509 : 8 : sorry ("user-defined mapper with non-unit length array section");
13510 : 8 : continue;
13511 : : }
13512 : :
13513 : 15 : if (map_kind == GOMP_MAP_UNSET)
13514 : 0 : map_kind = outer_kind;
13515 : :
13516 : 15 : mapper_clauses_p
13517 : 15 : = omp_instantiate_mapper (pre_p, implicit_mappers,
13518 : : *nested_mapper_p, decl, map_kind,
13519 : : mapper_clauses_p);
13520 : 15 : continue;
13521 : : }
13522 : :
13523 : 135 : *mapper_clauses_p = unshared;
13524 : 135 : mapper_clauses_p = &OMP_CLAUSE_CHAIN (unshared);
13525 : : }
13526 : :
13527 : 80 : return mapper_clauses_p;
13528 : 80 : }
13529 : :
13530 : : static int
13531 : 124892 : omp_instantiate_implicit_mappers (splay_tree_node n, void *data)
13532 : : {
13533 : 124892 : tree decl = (tree) n->key;
13534 : 124892 : instantiate_mapper_info *im_info = (instantiate_mapper_info *) data;
13535 : 124892 : gimplify_omp_ctx *ctx = im_info->omp_ctx;
13536 : 124892 : tree *mapper_p = NULL;
13537 : 124892 : tree type = TREE_TYPE (decl);
13538 : 124892 : bool ref_p = false;
13539 : 124892 : unsigned flags = n->value;
13540 : :
13541 : 124892 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
13542 : : return 0;
13543 : 23004 : if ((flags & GOVD_SEEN) == 0)
13544 : : return 0;
13545 : : /* If we already have clauses pertaining to a struct variable, then we don't
13546 : : want to implicitly invoke a user-defined mapper. */
13547 : 19636 : if ((flags & GOVD_EXPLICIT) != 0 && AGGREGATE_TYPE_P (TREE_TYPE (decl)))
13548 : : return 0;
13549 : :
13550 : 19636 : if (TREE_CODE (type) == REFERENCE_TYPE)
13551 : : {
13552 : 896 : ref_p = true;
13553 : 896 : type = TREE_TYPE (type);
13554 : : }
13555 : :
13556 : 19636 : type = TYPE_MAIN_VARIANT (type);
13557 : :
13558 : 19636 : if (DECL_P (decl) && type && AGGREGATE_TYPE_P (type))
13559 : : {
13560 : 5264 : gcc_assert (ctx);
13561 : 5264 : mapper_p = ctx->implicit_mappers->get ({ NULL_TREE, type });
13562 : : }
13563 : :
13564 : 5264 : if (mapper_p)
13565 : : {
13566 : : /* If we have a reference, map the pointed-to object rather than the
13567 : : reference itself. */
13568 : 65 : if (ref_p)
13569 : 2 : decl = build_fold_indirect_ref (decl);
13570 : :
13571 : 65 : im_info->mapper_clauses_p
13572 : 65 : = omp_instantiate_mapper (im_info->pre_p, ctx->implicit_mappers,
13573 : : *mapper_p, decl, GOMP_MAP_TOFROM,
13574 : : im_info->mapper_clauses_p);
13575 : : /* Make sure we don't map the same variable implicitly in
13576 : : gimplify_adjust_omp_clauses_1 also. */
13577 : 65 : n->value |= GOVD_EXPLICIT;
13578 : : }
13579 : :
13580 : : return 0;
13581 : : }
13582 : :
13583 : : /* Scan the OMP clauses in *LIST_P, installing mappings into a new
13584 : : and previous omp contexts. */
13585 : :
13586 : : static void
13587 : 129492 : gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
13588 : : enum omp_region_type region_type,
13589 : : enum tree_code code,
13590 : : gimple_seq *loops_seq_p = NULL)
13591 : : {
13592 : 129492 : using namespace omp_addr_tokenizer;
13593 : 129492 : struct gimplify_omp_ctx *ctx, *outer_ctx;
13594 : 129492 : tree c;
13595 : 129492 : tree *orig_list_p = list_p;
13596 : 129492 : int handled_depend_iterators = -1;
13597 : 129492 : int nowait = -1;
13598 : :
13599 : 129492 : ctx = new_omp_context (region_type);
13600 : 129492 : ctx->code = code;
13601 : 129492 : outer_ctx = ctx->outer_context;
13602 : 129492 : if (code == OMP_TARGET)
13603 : : {
13604 : 12589 : if (!lang_GNU_Fortran ())
13605 : 10602 : ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
13606 : 12589 : ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
13607 : 25178 : ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
13608 : 12589 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
13609 : : }
13610 : 129492 : if (!lang_GNU_Fortran ())
13611 : 98943 : switch (code)
13612 : : {
13613 : 17958 : case OMP_TARGET:
13614 : 17958 : case OMP_TARGET_DATA:
13615 : 17958 : case OMP_TARGET_ENTER_DATA:
13616 : 17958 : case OMP_TARGET_EXIT_DATA:
13617 : 17958 : case OACC_DECLARE:
13618 : 17958 : case OACC_HOST_DATA:
13619 : 17958 : case OACC_PARALLEL:
13620 : 17958 : case OACC_KERNELS:
13621 : 17958 : ctx->target_firstprivatize_array_bases = true;
13622 : : default:
13623 : : break;
13624 : : }
13625 : :
13626 : 129492 : vec<omp_mapping_group> *groups = NULL;
13627 : 129492 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
13628 : 129492 : unsigned grpnum = 0;
13629 : 129492 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
13630 : :
13631 : 129492 : if (code == OMP_TARGET
13632 : 129492 : || code == OMP_TARGET_DATA
13633 : 129492 : || code == OMP_TARGET_ENTER_DATA
13634 : : || code == OMP_TARGET_EXIT_DATA
13635 : : || code == OACC_DATA
13636 : : || code == OACC_KERNELS
13637 : : || code == OACC_PARALLEL
13638 : : || code == OACC_SERIAL
13639 : : || code == OACC_ENTER_DATA
13640 : : || code == OACC_EXIT_DATA
13641 : : || code == OACC_UPDATE
13642 : : || code == OACC_DECLARE)
13643 : : {
13644 : 32667 : groups = omp_gather_mapping_groups (list_p);
13645 : :
13646 : 32667 : if (groups)
13647 : 17379 : grpmap = omp_index_mapping_groups (groups);
13648 : : }
13649 : :
13650 : 331533 : while ((c = *list_p) != NULL)
13651 : : {
13652 : 202041 : bool remove = false;
13653 : 202041 : bool notice_outer = true;
13654 : 202041 : bool map_descriptor;
13655 : 202041 : const char *check_non_private = NULL;
13656 : 202041 : unsigned int flags;
13657 : 202041 : tree decl;
13658 : 202041 : auto_vec<omp_addr_token *, 10> addr_tokens;
13659 : 202041 : tree op = NULL_TREE;
13660 : 202041 : location_t loc = OMP_CLAUSE_LOCATION (c);
13661 : :
13662 : 237535 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
13663 : : {
13664 : : grp_start_p = NULL;
13665 : : grp_end = NULL_TREE;
13666 : : }
13667 : :
13668 : 202041 : if (code == OMP_TARGET
13669 : : || code == OMP_TARGET_DATA
13670 : : || code == OMP_TARGET_ENTER_DATA
13671 : 171836 : || code == OMP_TARGET_EXIT_DATA)
13672 : : /* Do some target-specific type checks for map operands. */
13673 : 31751 : switch (OMP_CLAUSE_CODE (c))
13674 : : {
13675 : 22841 : case OMP_CLAUSE_MAP:
13676 : 22841 : op = OMP_CLAUSE_OPERAND (c, 0);
13677 : 22841 : verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
13678 : 22841 : break;
13679 : 129 : case OMP_CLAUSE_PRIVATE:
13680 : 129 : op = OMP_CLAUSE_OPERAND (c, 0);
13681 : 129 : verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
13682 : 129 : break;
13683 : 1230 : case OMP_CLAUSE_FIRSTPRIVATE:
13684 : 1230 : op = OMP_CLAUSE_OPERAND (c, 0);
13685 : 1230 : verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
13686 : 1230 : break;
13687 : 2906 : case OMP_CLAUSE_IS_DEVICE_PTR:
13688 : 2906 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13689 : 2906 : case OMP_CLAUSE_USE_DEVICE_PTR:
13690 : 2906 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13691 : 2906 : op = OMP_CLAUSE_OPERAND (c, 0);
13692 : 2906 : verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
13693 : 2906 : break;
13694 : : default:
13695 : : break;
13696 : : }
13697 : :
13698 : 202041 : switch (OMP_CLAUSE_CODE (c))
13699 : : {
13700 : 12141 : case OMP_CLAUSE_PRIVATE:
13701 : 12141 : flags = GOVD_PRIVATE | GOVD_EXPLICIT;
13702 : 12141 : if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
13703 : : {
13704 : 165 : flags |= GOVD_PRIVATE_OUTER_REF;
13705 : 165 : OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
13706 : : }
13707 : : else
13708 : : notice_outer = false;
13709 : 12141 : goto do_add;
13710 : 5510 : case OMP_CLAUSE_SHARED:
13711 : 5510 : flags = GOVD_SHARED | GOVD_EXPLICIT;
13712 : 5510 : goto do_add;
13713 : 7843 : case OMP_CLAUSE_FIRSTPRIVATE:
13714 : 7843 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13715 : 7843 : check_non_private = "firstprivate";
13716 : 7843 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13717 : : {
13718 : 380 : gcc_assert (code == OMP_TARGET);
13719 : : flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
13720 : : }
13721 : 7843 : goto do_add;
13722 : 7328 : case OMP_CLAUSE_LASTPRIVATE:
13723 : 7328 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13724 : 496 : switch (code)
13725 : : {
13726 : 25 : case OMP_DISTRIBUTE:
13727 : 25 : error_at (OMP_CLAUSE_LOCATION (c),
13728 : : "conditional %<lastprivate%> clause on "
13729 : : "%qs construct", "distribute");
13730 : 25 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13731 : 25 : break;
13732 : 13 : case OMP_TASKLOOP:
13733 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
13734 : : "conditional %<lastprivate%> clause on "
13735 : : "%qs construct", "taskloop");
13736 : 13 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13737 : 13 : break;
13738 : : default:
13739 : : break;
13740 : : }
13741 : 7328 : flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
13742 : 7328 : if (code != OMP_LOOP)
13743 : 6977 : check_non_private = "lastprivate";
13744 : 7328 : decl = OMP_CLAUSE_DECL (c);
13745 : 7328 : if (error_operand_p (decl))
13746 : 0 : goto do_add;
13747 : 7328 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
13748 : 7328 : && !lang_hooks.decls.omp_scalar_p (decl, true))
13749 : : {
13750 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
13751 : : "non-scalar variable %qD in conditional "
13752 : : "%<lastprivate%> clause", decl);
13753 : 5 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13754 : : }
13755 : 7328 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13756 : 453 : flags |= GOVD_LASTPRIVATE_CONDITIONAL;
13757 : 7328 : omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
13758 : : false);
13759 : 7328 : goto do_add;
13760 : 15294 : case OMP_CLAUSE_REDUCTION:
13761 : 15294 : if (OMP_CLAUSE_REDUCTION_TASK (c))
13762 : : {
13763 : 594 : if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13764 : : {
13765 : 408 : if (nowait == -1)
13766 : 293 : nowait = omp_find_clause (*list_p,
13767 : 293 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
13768 : 408 : if (nowait
13769 : 15 : && (outer_ctx == NULL
13770 : 0 : || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
13771 : : {
13772 : 15 : error_at (OMP_CLAUSE_LOCATION (c),
13773 : : "%<task%> reduction modifier on a construct "
13774 : : "with a %<nowait%> clause");
13775 : 15 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13776 : : }
13777 : : }
13778 : 186 : else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
13779 : : {
13780 : 40 : error_at (OMP_CLAUSE_LOCATION (c),
13781 : : "invalid %<task%> reduction modifier on construct "
13782 : : "other than %<parallel%>, %qs, %<sections%> or "
13783 : 20 : "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
13784 : 20 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13785 : : }
13786 : : }
13787 : 15294 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
13788 : 831 : switch (code)
13789 : : {
13790 : 4 : case OMP_SECTIONS:
13791 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13792 : : "%<inscan%> %<reduction%> clause on "
13793 : : "%qs construct", "sections");
13794 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13795 : 4 : break;
13796 : 4 : case OMP_PARALLEL:
13797 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13798 : : "%<inscan%> %<reduction%> clause on "
13799 : : "%qs construct", "parallel");
13800 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13801 : 4 : break;
13802 : 4 : case OMP_TEAMS:
13803 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13804 : : "%<inscan%> %<reduction%> clause on "
13805 : : "%qs construct", "teams");
13806 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13807 : 4 : break;
13808 : 4 : case OMP_TASKLOOP:
13809 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13810 : : "%<inscan%> %<reduction%> clause on "
13811 : : "%qs construct", "taskloop");
13812 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13813 : 4 : break;
13814 : 4 : case OMP_SCOPE:
13815 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13816 : : "%<inscan%> %<reduction%> clause on "
13817 : : "%qs construct", "scope");
13818 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13819 : 4 : break;
13820 : : default:
13821 : : break;
13822 : : }
13823 : : /* FALLTHRU */
13824 : 17894 : case OMP_CLAUSE_IN_REDUCTION:
13825 : 17894 : case OMP_CLAUSE_TASK_REDUCTION:
13826 : 17894 : flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
13827 : : /* OpenACC permits reductions on private variables. */
13828 : 17894 : if (!(region_type & ORT_ACC)
13829 : : /* taskgroup is actually not a worksharing region. */
13830 : 12374 : && code != OMP_TASKGROUP)
13831 : 11844 : check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
13832 : 17894 : decl = OMP_CLAUSE_DECL (c);
13833 : 17894 : if (TREE_CODE (decl) == MEM_REF)
13834 : : {
13835 : 2614 : tree type = TREE_TYPE (decl);
13836 : 2614 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
13837 : 2614 : gimplify_ctxp->into_ssa = false;
13838 : 2614 : if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
13839 : : NULL, is_gimple_val, fb_rvalue, false)
13840 : : == GS_ERROR)
13841 : : {
13842 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13843 : 0 : remove = true;
13844 : 0 : break;
13845 : : }
13846 : 2614 : gimplify_ctxp->into_ssa = saved_into_ssa;
13847 : 2614 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13848 : 2614 : if (DECL_P (v))
13849 : : {
13850 : 571 : omp_firstprivatize_variable (ctx, v);
13851 : 571 : omp_notice_variable (ctx, v, true);
13852 : : }
13853 : 2614 : decl = TREE_OPERAND (decl, 0);
13854 : 2614 : if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
13855 : : {
13856 : 477 : gimplify_ctxp->into_ssa = false;
13857 : 477 : if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
13858 : : NULL, is_gimple_val, fb_rvalue, false)
13859 : : == GS_ERROR)
13860 : : {
13861 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13862 : 0 : remove = true;
13863 : 0 : break;
13864 : : }
13865 : 477 : gimplify_ctxp->into_ssa = saved_into_ssa;
13866 : 477 : v = TREE_OPERAND (decl, 1);
13867 : 477 : if (DECL_P (v))
13868 : : {
13869 : 477 : omp_firstprivatize_variable (ctx, v);
13870 : 477 : omp_notice_variable (ctx, v, true);
13871 : : }
13872 : 477 : decl = TREE_OPERAND (decl, 0);
13873 : : }
13874 : 2614 : if (TREE_CODE (decl) == ADDR_EXPR
13875 : 1409 : || TREE_CODE (decl) == INDIRECT_REF)
13876 : 1375 : decl = TREE_OPERAND (decl, 0);
13877 : : }
13878 : 17894 : goto do_add_decl;
13879 : 2668 : case OMP_CLAUSE_LINEAR:
13880 : 2668 : if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
13881 : : is_gimple_val, fb_rvalue) == GS_ERROR)
13882 : : {
13883 : : remove = true;
13884 : : break;
13885 : : }
13886 : : else
13887 : : {
13888 : 2668 : if (code == OMP_SIMD
13889 : 2668 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13890 : : {
13891 : 1154 : struct gimplify_omp_ctx *octx = outer_ctx;
13892 : 1154 : if (octx
13893 : 741 : && octx->region_type == ORT_WORKSHARE
13894 : 500 : && octx->combined_loop
13895 : 500 : && !octx->distribute)
13896 : : {
13897 : 492 : if (octx->outer_context
13898 : 430 : && (octx->outer_context->region_type
13899 : : == ORT_COMBINED_PARALLEL))
13900 : 393 : octx = octx->outer_context->outer_context;
13901 : : else
13902 : : octx = octx->outer_context;
13903 : : }
13904 : 905 : if (octx
13905 : 316 : && octx->region_type == ORT_WORKSHARE
13906 : 16 : && octx->combined_loop
13907 : 16 : && octx->distribute)
13908 : : {
13909 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
13910 : : "%<linear%> clause for variable other than "
13911 : : "loop iterator specified on construct "
13912 : : "combined with %<distribute%>");
13913 : 16 : remove = true;
13914 : 16 : break;
13915 : : }
13916 : : }
13917 : : /* For combined #pragma omp parallel for simd, need to put
13918 : : lastprivate and perhaps firstprivate too on the
13919 : : parallel. Similarly for #pragma omp for simd. */
13920 : : struct gimplify_omp_ctx *octx = outer_ctx;
13921 : : bool taskloop_seen = false;
13922 : : decl = NULL_TREE;
13923 : 3443 : do
13924 : : {
13925 : 3443 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13926 : 3443 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13927 : : break;
13928 : 3383 : decl = OMP_CLAUSE_DECL (c);
13929 : 3383 : if (error_operand_p (decl))
13930 : : {
13931 : : decl = NULL_TREE;
13932 : : break;
13933 : : }
13934 : 3383 : flags = GOVD_SEEN;
13935 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13936 : 1672 : flags |= GOVD_FIRSTPRIVATE;
13937 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13938 : 3383 : flags |= GOVD_LASTPRIVATE;
13939 : 3383 : if (octx
13940 : 2506 : && octx->region_type == ORT_WORKSHARE
13941 : 1213 : && octx->combined_loop)
13942 : : {
13943 : 1207 : if (octx->outer_context
13944 : 1037 : && (octx->outer_context->region_type
13945 : : == ORT_COMBINED_PARALLEL))
13946 : : octx = octx->outer_context;
13947 : 476 : else if (omp_check_private (octx, decl, false))
13948 : : break;
13949 : : }
13950 : : else if (octx
13951 : 1299 : && (octx->region_type & ORT_TASK) != 0
13952 : 307 : && octx->combined_loop)
13953 : : taskloop_seen = true;
13954 : : else if (octx
13955 : 996 : && octx->region_type == ORT_COMBINED_PARALLEL
13956 : 301 : && ((ctx->region_type == ORT_WORKSHARE
13957 : 201 : && octx == outer_ctx)
13958 : 100 : || taskloop_seen))
13959 : : flags = GOVD_SEEN | GOVD_SHARED;
13960 : : else if (octx
13961 : 695 : && ((octx->region_type & ORT_COMBINED_TEAMS)
13962 : : == ORT_COMBINED_TEAMS))
13963 : : flags = GOVD_SEEN | GOVD_SHARED;
13964 : 540 : else if (octx
13965 : 540 : && octx->region_type == ORT_COMBINED_TARGET)
13966 : : {
13967 : 195 : if (flags & GOVD_LASTPRIVATE)
13968 : 195 : flags = GOVD_SEEN | GOVD_MAP;
13969 : : }
13970 : : else
13971 : : break;
13972 : 2071 : splay_tree_node on
13973 : 2071 : = splay_tree_lookup (octx->variables,
13974 : : (splay_tree_key) decl);
13975 : 2071 : if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
13976 : : {
13977 : : octx = NULL;
13978 : : break;
13979 : : }
13980 : 2067 : omp_add_variable (octx, decl, flags);
13981 : 2067 : if (octx->outer_context == NULL)
13982 : : break;
13983 : : octx = octx->outer_context;
13984 : : }
13985 : : while (1);
13986 : 2652 : if (octx
13987 : 2652 : && decl
13988 : 2652 : && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13989 : 728 : || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
13990 : 1711 : omp_notice_variable (octx, decl, true);
13991 : : }
13992 : 2652 : flags = GOVD_LINEAR | GOVD_EXPLICIT;
13993 : 2652 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13994 : 2652 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13995 : : {
13996 : : notice_outer = false;
13997 : : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
13998 : : }
13999 : 2652 : goto do_add;
14000 : :
14001 : 48427 : case OMP_CLAUSE_MAP:
14002 : 48427 : if (!grp_start_p)
14003 : : {
14004 : 29531 : grp_start_p = list_p;
14005 : 29531 : grp_end = (*groups)[grpnum].grp_end;
14006 : 29531 : grpnum++;
14007 : : }
14008 : 48427 : decl = OMP_CLAUSE_DECL (c);
14009 : :
14010 : 48427 : if (error_operand_p (decl))
14011 : : {
14012 : : remove = true;
14013 : : break;
14014 : : }
14015 : :
14016 : 48427 : if (!omp_parse_expr (addr_tokens, decl))
14017 : : {
14018 : : remove = true;
14019 : : break;
14020 : : }
14021 : :
14022 : 48427 : if (remove)
14023 : : break;
14024 : 48427 : if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
14025 : : {
14026 : : struct gimplify_omp_ctx *octx;
14027 : 1169 : for (octx = outer_ctx; octx; octx = octx->outer_context)
14028 : : {
14029 : 1169 : if (octx->region_type != ORT_ACC_HOST_DATA)
14030 : : break;
14031 : 12 : splay_tree_node n2
14032 : 12 : = splay_tree_lookup (octx->variables,
14033 : : (splay_tree_key) decl);
14034 : 12 : if (n2)
14035 : 4 : error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
14036 : : "declared in enclosing %<host_data%> region",
14037 : 4 : DECL_NAME (decl));
14038 : : }
14039 : : }
14040 : :
14041 : 48427 : map_descriptor = false;
14042 : :
14043 : : /* This condition checks if we're mapping an array descriptor that
14044 : : isn't inside a derived type -- these have special handling, and
14045 : : are not handled as structs in omp_build_struct_sibling_lists.
14046 : : See that function for further details. */
14047 : 48427 : if (*grp_start_p != grp_end
14048 : 32171 : && OMP_CLAUSE_CHAIN (*grp_start_p)
14049 : 80598 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
14050 : : {
14051 : 15475 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
14052 : 15475 : if (omp_map_clause_descriptor_p (grp_mid)
14053 : 29183 : && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
14054 : : map_descriptor = true;
14055 : : }
14056 : 32952 : else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
14057 : 32952 : && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
14058 : 32305 : || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
14059 : 33741 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
14060 : : map_descriptor = true;
14061 : :
14062 : : /* Adding the decl for a struct access: we haven't created
14063 : : GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
14064 : : whether they will be created in gimplify_adjust_omp_clauses.
14065 : : NOTE: Technically we should probably look through DECL_VALUE_EXPR
14066 : : here because something that looks like a DECL_P may actually be a
14067 : : struct access, e.g. variables in a lambda closure
14068 : : (__closure->__foo) or class members (this->foo). Currently in both
14069 : : those cases we map the whole of the containing object (directly in
14070 : : the C++ FE) though, so struct nodes are not created. */
14071 : 48427 : if (c == grp_end
14072 : 29531 : && addr_tokens[0]->type == STRUCTURE_BASE
14073 : 7625 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14074 : 56052 : && !map_descriptor)
14075 : : {
14076 : 4856 : gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
14077 : : /* If we got to this struct via a chain of pointers, maybe we
14078 : : want to map it implicitly instead. */
14079 : 4856 : if (omp_access_chain_p (addr_tokens, 1))
14080 : : break;
14081 : 4696 : omp_mapping_group *wholestruct;
14082 : 4696 : if (!(region_type & ORT_ACC)
14083 : 8115 : && omp_mapped_by_containing_struct (grpmap,
14084 : 3419 : OMP_CLAUSE_DECL (c),
14085 : : &wholestruct))
14086 : : break;
14087 : 4512 : decl = addr_tokens[1]->expr;
14088 : 4512 : if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
14089 : : break;
14090 : : /* Standalone attach or detach clauses for a struct element
14091 : : should not inhibit implicit mapping of the whole struct. */
14092 : 2770 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14093 : 2770 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14094 : : break;
14095 : 2615 : flags = GOVD_MAP | GOVD_EXPLICIT;
14096 : :
14097 : 2615 : gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
14098 : : || TREE_ADDRESSABLE (decl));
14099 : 2615 : goto do_add_decl;
14100 : : }
14101 : :
14102 : 43571 : if (!DECL_P (decl))
14103 : : {
14104 : 19806 : tree d = decl, *pd;
14105 : 19806 : if (TREE_CODE (d) == ARRAY_REF)
14106 : : {
14107 : 4890 : while (TREE_CODE (d) == ARRAY_REF)
14108 : 2502 : d = TREE_OPERAND (d, 0);
14109 : 2388 : if (TREE_CODE (d) == COMPONENT_REF
14110 : 2388 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
14111 : : decl = d;
14112 : : }
14113 : 19806 : pd = &OMP_CLAUSE_DECL (c);
14114 : 19806 : if (d == decl
14115 : 17464 : && TREE_CODE (decl) == INDIRECT_REF
14116 : 13347 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
14117 : 1069 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
14118 : : == REFERENCE_TYPE)
14119 : 20457 : && (OMP_CLAUSE_MAP_KIND (c)
14120 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
14121 : : {
14122 : 647 : pd = &TREE_OPERAND (decl, 0);
14123 : 647 : decl = TREE_OPERAND (decl, 0);
14124 : : }
14125 : :
14126 : 19806 : if (addr_tokens[0]->type == STRUCTURE_BASE
14127 : 10276 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14128 : 10276 : && addr_tokens[1]->type == ACCESS_METHOD
14129 : 10276 : && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
14130 : 9779 : || (addr_tokens[1]->u.access_kind
14131 : : == ACCESS_POINTER_OFFSET))
14132 : 20385 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
14133 : : {
14134 : 0 : tree base = addr_tokens[1]->expr;
14135 : 0 : splay_tree_node n
14136 : 0 : = splay_tree_lookup (ctx->variables,
14137 : : (splay_tree_key) base);
14138 : 0 : n->value |= GOVD_SEEN;
14139 : : }
14140 : :
14141 : 19806 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
14142 : : {
14143 : : /* Don't gimplify *pd fully at this point, as the base
14144 : : will need to be adjusted during omp lowering. */
14145 : 88 : auto_vec<tree, 10> expr_stack;
14146 : 88 : tree *p = pd;
14147 : 88 : while (handled_component_p (*p)
14148 : : || TREE_CODE (*p) == INDIRECT_REF
14149 : : || TREE_CODE (*p) == ADDR_EXPR
14150 : : || TREE_CODE (*p) == MEM_REF
14151 : 224 : || TREE_CODE (*p) == NON_LVALUE_EXPR)
14152 : : {
14153 : 136 : expr_stack.safe_push (*p);
14154 : 136 : p = &TREE_OPERAND (*p, 0);
14155 : : }
14156 : 312 : for (int i = expr_stack.length () - 1; i >= 0; i--)
14157 : : {
14158 : 136 : tree t = expr_stack[i];
14159 : 136 : if (TREE_CODE (t) == ARRAY_REF
14160 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14161 : : {
14162 : 56 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14163 : : {
14164 : 56 : tree low = unshare_expr (array_ref_low_bound (t));
14165 : 56 : if (!is_gimple_min_invariant (low))
14166 : : {
14167 : 0 : TREE_OPERAND (t, 2) = low;
14168 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14169 : : pre_p, NULL,
14170 : : is_gimple_reg,
14171 : : fb_rvalue) == GS_ERROR)
14172 : 0 : remove = true;
14173 : : }
14174 : : }
14175 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14176 : : NULL, is_gimple_reg,
14177 : : fb_rvalue) == GS_ERROR)
14178 : 0 : remove = true;
14179 : 56 : if (TREE_OPERAND (t, 3) == NULL_TREE)
14180 : : {
14181 : 56 : tree elmt_size = array_ref_element_size (t);
14182 : 56 : if (!is_gimple_min_invariant (elmt_size))
14183 : : {
14184 : 0 : elmt_size = unshare_expr (elmt_size);
14185 : 0 : tree elmt_type
14186 : 0 : = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
14187 : : 0)));
14188 : 0 : tree factor
14189 : 0 : = size_int (TYPE_ALIGN_UNIT (elmt_type));
14190 : 0 : elmt_size
14191 : 0 : = size_binop (EXACT_DIV_EXPR, elmt_size,
14192 : : factor);
14193 : 0 : TREE_OPERAND (t, 3) = elmt_size;
14194 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 3),
14195 : : pre_p, NULL,
14196 : : is_gimple_reg,
14197 : : fb_rvalue) == GS_ERROR)
14198 : 0 : remove = true;
14199 : : }
14200 : : }
14201 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
14202 : : NULL, is_gimple_reg,
14203 : : fb_rvalue) == GS_ERROR)
14204 : 0 : remove = true;
14205 : : }
14206 : 80 : else if (TREE_CODE (t) == COMPONENT_REF)
14207 : : {
14208 : 0 : if (TREE_OPERAND (t, 2) == NULL_TREE)
14209 : : {
14210 : 0 : tree offset = component_ref_field_offset (t);
14211 : 0 : if (!is_gimple_min_invariant (offset))
14212 : : {
14213 : 0 : offset = unshare_expr (offset);
14214 : 0 : tree field = TREE_OPERAND (t, 1);
14215 : 0 : tree factor
14216 : 0 : = size_int (DECL_OFFSET_ALIGN (field)
14217 : : / BITS_PER_UNIT);
14218 : 0 : offset = size_binop (EXACT_DIV_EXPR, offset,
14219 : : factor);
14220 : 0 : TREE_OPERAND (t, 2) = offset;
14221 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
14222 : : pre_p, NULL,
14223 : : is_gimple_reg,
14224 : : fb_rvalue) == GS_ERROR)
14225 : 0 : remove = true;
14226 : : }
14227 : : }
14228 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
14229 : : NULL, is_gimple_reg,
14230 : : fb_rvalue) == GS_ERROR)
14231 : 0 : remove = true;
14232 : : }
14233 : : }
14234 : 224 : for (; expr_stack.length () > 0; )
14235 : : {
14236 : 136 : tree t = expr_stack.pop ();
14237 : :
14238 : 136 : if (TREE_CODE (t) == ARRAY_REF
14239 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
14240 : : {
14241 : 56 : if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
14242 : 56 : && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
14243 : : NULL, is_gimple_val,
14244 : : fb_rvalue) == GS_ERROR)
14245 : 144 : remove = true;
14246 : : }
14247 : : }
14248 : 88 : }
14249 : : break;
14250 : : }
14251 : :
14252 : 23765 : if ((code == OMP_TARGET
14253 : : || code == OMP_TARGET_DATA
14254 : : || code == OMP_TARGET_ENTER_DATA
14255 : 14143 : || code == OMP_TARGET_EXIT_DATA)
14256 : 24323 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
14257 : : {
14258 : : /* If we have attach/detach but the decl we have is a pointer to
14259 : : pointer, we're probably mapping the "base level" array
14260 : : implicitly. Make sure we don't add the decl as if we mapped
14261 : : it explicitly. That is,
14262 : :
14263 : : int **arr;
14264 : : [...]
14265 : : #pragma omp target map(arr[a][b:c])
14266 : :
14267 : : should *not* map "arr" explicitly. That way we get a
14268 : : zero-length "alloc" mapping for it, and assuming it's been
14269 : : mapped by some previous directive, etc., things work as they
14270 : : should. */
14271 : :
14272 : 204 : tree basetype = TREE_TYPE (addr_tokens[0]->expr);
14273 : :
14274 : 204 : if (TREE_CODE (basetype) == REFERENCE_TYPE)
14275 : 34 : basetype = TREE_TYPE (basetype);
14276 : :
14277 : 204 : if (code == OMP_TARGET
14278 : 54 : && addr_tokens[0]->type == ARRAY_BASE
14279 : 54 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
14280 : 54 : && TREE_CODE (basetype) == POINTER_TYPE
14281 : 258 : && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
14282 : : break;
14283 : : }
14284 : :
14285 : 23743 : flags = GOVD_MAP | GOVD_EXPLICIT;
14286 : 23743 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
14287 : 23632 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
14288 : 23128 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
14289 : 46845 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
14290 : : flags |= GOVD_MAP_ALWAYS_TO;
14291 : :
14292 : 23743 : goto do_add;
14293 : :
14294 : 468 : case OMP_CLAUSE_AFFINITY:
14295 : 468 : gimplify_omp_affinity (list_p, pre_p);
14296 : 468 : remove = true;
14297 : 468 : break;
14298 : 8 : case OMP_CLAUSE_DOACROSS:
14299 : 8 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
14300 : : {
14301 : 4 : tree deps = OMP_CLAUSE_DECL (c);
14302 : 8 : while (deps && TREE_CODE (deps) == TREE_LIST)
14303 : : {
14304 : 4 : if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
14305 : 4 : && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
14306 : 0 : gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
14307 : : pre_p, NULL, is_gimple_val, fb_rvalue);
14308 : 4 : deps = TREE_CHAIN (deps);
14309 : : }
14310 : : }
14311 : : else
14312 : 4 : gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
14313 : : == OMP_CLAUSE_DOACROSS_SOURCE);
14314 : : break;
14315 : 2200 : case OMP_CLAUSE_DEPEND:
14316 : 2200 : if (handled_depend_iterators == -1)
14317 : 1898 : handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
14318 : 2200 : if (handled_depend_iterators)
14319 : : {
14320 : 343 : if (handled_depend_iterators == 2)
14321 : 0 : remove = true;
14322 : : break;
14323 : : }
14324 : 1857 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
14325 : : {
14326 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
14327 : : NULL, is_gimple_val, fb_rvalue);
14328 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
14329 : : }
14330 : 1857 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
14331 : : {
14332 : : remove = true;
14333 : : break;
14334 : : }
14335 : 1857 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
14336 : : {
14337 : 1826 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
14338 : 1826 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
14339 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14340 : : {
14341 : : remove = true;
14342 : : break;
14343 : : }
14344 : : }
14345 : 1857 : if (code == OMP_TASK)
14346 : 1399 : ctx->has_depend = true;
14347 : : break;
14348 : :
14349 : 8224 : case OMP_CLAUSE_TO:
14350 : 8224 : case OMP_CLAUSE_FROM:
14351 : 8224 : case OMP_CLAUSE__CACHE_:
14352 : 8224 : decl = OMP_CLAUSE_DECL (c);
14353 : 8224 : if (error_operand_p (decl))
14354 : : {
14355 : : remove = true;
14356 : : break;
14357 : : }
14358 : 8224 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
14359 : 7435 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
14360 : 684 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
14361 : 8224 : gimple_seq *seq_p;
14362 : 8224 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
14363 : 8224 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
14364 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14365 : : {
14366 : 0 : remove = true;
14367 : 0 : exit_omp_iterator_loop_context (c);
14368 : 0 : break;
14369 : : }
14370 : 8224 : if (!DECL_P (decl))
14371 : : {
14372 : 2037 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
14373 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
14374 : 0 : remove = true;
14375 : 2037 : exit_omp_iterator_loop_context (c);
14376 : 2037 : break;
14377 : : }
14378 : 6187 : exit_omp_iterator_loop_context (c);
14379 : 6187 : goto do_notice;
14380 : :
14381 : 125 : case OMP_CLAUSE__MAPPER_BINDING_:
14382 : 125 : {
14383 : 125 : tree name = OMP_CLAUSE__MAPPER_BINDING__ID (c);
14384 : 125 : tree var = OMP_CLAUSE__MAPPER_BINDING__DECL (c);
14385 : 125 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (var));
14386 : 125 : tree fndecl = OMP_CLAUSE__MAPPER_BINDING__MAPPER (c);
14387 : 125 : ctx->implicit_mappers->put ({ name, type }, fndecl);
14388 : 125 : remove = true;
14389 : 125 : break;
14390 : : }
14391 : :
14392 : 2118 : case OMP_CLAUSE_USE_DEVICE_PTR:
14393 : 2118 : case OMP_CLAUSE_USE_DEVICE_ADDR:
14394 : 2118 : flags = GOVD_EXPLICIT;
14395 : 2118 : goto do_add;
14396 : :
14397 : 543 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
14398 : 543 : decl = OMP_CLAUSE_DECL (c);
14399 : 543 : while (TREE_CODE (decl) == INDIRECT_REF
14400 : 592 : || TREE_CODE (decl) == ARRAY_REF)
14401 : 49 : decl = TREE_OPERAND (decl, 0);
14402 : 543 : flags = GOVD_EXPLICIT;
14403 : 543 : goto do_add_decl;
14404 : :
14405 : 498 : case OMP_CLAUSE_IS_DEVICE_PTR:
14406 : 498 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
14407 : 498 : goto do_add;
14408 : :
14409 : 62023 : do_add:
14410 : 62023 : decl = OMP_CLAUSE_DECL (c);
14411 : 83075 : do_add_decl:
14412 : 83075 : if (error_operand_p (decl))
14413 : : {
14414 : : remove = true;
14415 : : break;
14416 : : }
14417 : 83067 : if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
14418 : : {
14419 : 1765 : tree t = omp_member_access_dummy_var (decl);
14420 : 1765 : if (t)
14421 : : {
14422 : 668 : tree v = DECL_VALUE_EXPR (decl);
14423 : 668 : DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
14424 : 668 : if (outer_ctx)
14425 : 140 : omp_notice_variable (outer_ctx, t, true);
14426 : : }
14427 : : }
14428 : 83067 : if (code == OACC_DATA
14429 : 2404 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14430 : 85471 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14431 : 308 : flags |= GOVD_MAP_0LEN_ARRAY;
14432 : 83067 : omp_add_variable (ctx, decl, flags);
14433 : 83067 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14434 : 67773 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14435 : 65703 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14436 : 85667 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14437 : : {
14438 : 2087 : struct gimplify_omp_ctx *pctx
14439 : 2147 : = code == OMP_TARGET ? outer_ctx : ctx;
14440 : 2147 : if (pctx)
14441 : 2117 : omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
14442 : : GOVD_LOCAL | GOVD_SEEN);
14443 : 2117 : if (pctx
14444 : 2117 : && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
14445 : 632 : && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14446 : : find_decl_expr,
14447 : : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14448 : : NULL) == NULL_TREE)
14449 : 208 : omp_add_variable (pctx,
14450 : 208 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
14451 : : GOVD_LOCAL | GOVD_SEEN);
14452 : 2147 : gimplify_omp_ctxp = pctx;
14453 : 2147 : push_gimplify_context ();
14454 : :
14455 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
14456 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
14457 : :
14458 : 2147 : gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
14459 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
14460 : 2147 : pop_gimplify_context
14461 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
14462 : 2147 : push_gimplify_context ();
14463 : 4294 : gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
14464 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
14465 : 2147 : pop_gimplify_context
14466 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
14467 : 2147 : OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
14468 : 2147 : OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
14469 : :
14470 : 2147 : gimplify_omp_ctxp = outer_ctx;
14471 : : }
14472 : 80920 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14473 : 80920 : && OMP_CLAUSE_LASTPRIVATE_STMT (c))
14474 : : {
14475 : 303 : gimplify_omp_ctxp = ctx;
14476 : 303 : push_gimplify_context ();
14477 : 303 : if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
14478 : : {
14479 : 303 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14480 : : NULL, NULL);
14481 : 303 : TREE_SIDE_EFFECTS (bind) = 1;
14482 : 303 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
14483 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
14484 : : }
14485 : 606 : gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
14486 : 303 : &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
14487 : 303 : pop_gimplify_context
14488 : 303 : (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
14489 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
14490 : :
14491 : 303 : gimplify_omp_ctxp = outer_ctx;
14492 : : }
14493 : 80617 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14494 : 80617 : && OMP_CLAUSE_LINEAR_STMT (c))
14495 : : {
14496 : 60 : gimplify_omp_ctxp = ctx;
14497 : 60 : push_gimplify_context ();
14498 : 60 : if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
14499 : : {
14500 : 60 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14501 : : NULL, NULL);
14502 : 60 : TREE_SIDE_EFFECTS (bind) = 1;
14503 : 60 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
14504 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = bind;
14505 : : }
14506 : 120 : gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
14507 : 60 : &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
14508 : 60 : pop_gimplify_context
14509 : 60 : (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
14510 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
14511 : :
14512 : 60 : gimplify_omp_ctxp = outer_ctx;
14513 : : }
14514 : 83067 : if (notice_outer)
14515 : 71031 : goto do_notice;
14516 : : break;
14517 : :
14518 : 906 : case OMP_CLAUSE_COPYIN:
14519 : 906 : case OMP_CLAUSE_COPYPRIVATE:
14520 : 906 : decl = OMP_CLAUSE_DECL (c);
14521 : 906 : if (error_operand_p (decl))
14522 : : {
14523 : : remove = true;
14524 : : break;
14525 : : }
14526 : 906 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
14527 : : && !remove
14528 : 906 : && !omp_check_private (ctx, decl, true))
14529 : : {
14530 : 35 : remove = true;
14531 : 35 : if (is_global_var (decl))
14532 : : {
14533 : 30 : if (DECL_THREAD_LOCAL_P (decl))
14534 : : remove = false;
14535 : 10 : else if (DECL_HAS_VALUE_EXPR_P (decl))
14536 : : {
14537 : 2 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
14538 : :
14539 : 2 : if (value
14540 : 2 : && DECL_P (value)
14541 : 4 : && DECL_THREAD_LOCAL_P (value))
14542 : : remove = false;
14543 : : }
14544 : : }
14545 : : if (remove)
14546 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
14547 : : "copyprivate variable %qE is not threadprivate"
14548 : 13 : " or private in outer context", DECL_NAME (decl));
14549 : : }
14550 : 78124 : do_notice:
14551 : 78124 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14552 : 62830 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14553 : 54987 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14554 : 30465 : && outer_ctx
14555 : 17572 : && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
14556 : 16233 : || (region_type == ORT_WORKSHARE
14557 : 3884 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14558 : 1087 : && (OMP_CLAUSE_REDUCTION_INSCAN (c)
14559 : 912 : || code == OMP_LOOP)))
14560 : 80069 : && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
14561 : 857 : || (code == OMP_LOOP
14562 : 138 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14563 : 138 : && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
14564 : : == ORT_COMBINED_TEAMS))))
14565 : : {
14566 : 1209 : splay_tree_node on
14567 : 1209 : = splay_tree_lookup (outer_ctx->variables,
14568 : : (splay_tree_key)decl);
14569 : 1209 : if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
14570 : : {
14571 : 891 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14572 : 705 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14573 : 1003 : && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
14574 : 56 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14575 : 0 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
14576 : : == POINTER_TYPE))))
14577 : 56 : omp_firstprivatize_variable (outer_ctx, decl);
14578 : : else
14579 : : {
14580 : 835 : omp_add_variable (outer_ctx, decl,
14581 : : GOVD_SEEN | GOVD_SHARED);
14582 : 835 : if (outer_ctx->outer_context)
14583 : 235 : omp_notice_variable (outer_ctx->outer_context, decl,
14584 : : true);
14585 : : }
14586 : : }
14587 : : }
14588 : 77524 : if (outer_ctx)
14589 : 29615 : omp_notice_variable (outer_ctx, decl, true);
14590 : 78124 : if (check_non_private
14591 : 26664 : && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
14592 : 4747 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14593 : 1641 : || decl == OMP_CLAUSE_DECL (c)
14594 : 224 : || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14595 : 224 : && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14596 : : == ADDR_EXPR
14597 : 113 : || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14598 : : == POINTER_PLUS_EXPR
14599 : 22 : && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
14600 : : (OMP_CLAUSE_DECL (c), 0), 0))
14601 : : == ADDR_EXPR)))))
14602 : 82769 : && omp_check_private (ctx, decl, false))
14603 : : {
14604 : 58 : error ("%s variable %qE is private in outer context",
14605 : 29 : check_non_private, DECL_NAME (decl));
14606 : 29 : remove = true;
14607 : : }
14608 : : break;
14609 : :
14610 : 190 : case OMP_CLAUSE_DETACH:
14611 : 190 : flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
14612 : 190 : goto do_add;
14613 : :
14614 : 4092 : case OMP_CLAUSE_IF:
14615 : 4092 : if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
14616 : 4092 : && OMP_CLAUSE_IF_MODIFIER (c) != code)
14617 : : {
14618 : : const char *p[2];
14619 : 168 : for (int i = 0; i < 2; i++)
14620 : 112 : switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
14621 : : {
14622 : 8 : case VOID_CST: p[i] = "cancel"; break;
14623 : 24 : case OMP_PARALLEL: p[i] = "parallel"; break;
14624 : 4 : case OMP_SIMD: p[i] = "simd"; break;
14625 : 12 : case OMP_TASK: p[i] = "task"; break;
14626 : 12 : case OMP_TASKLOOP: p[i] = "taskloop"; break;
14627 : 8 : case OMP_TARGET_DATA: p[i] = "target data"; break;
14628 : 12 : case OMP_TARGET: p[i] = "target"; break;
14629 : 12 : case OMP_TARGET_UPDATE: p[i] = "target update"; break;
14630 : 8 : case OMP_TARGET_ENTER_DATA:
14631 : 8 : p[i] = "target enter data"; break;
14632 : 12 : case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
14633 : 0 : default: gcc_unreachable ();
14634 : : }
14635 : 56 : error_at (OMP_CLAUSE_LOCATION (c),
14636 : : "expected %qs %<if%> clause modifier rather than %qs",
14637 : : p[0], p[1]);
14638 : 56 : remove = true;
14639 : : }
14640 : : /* Fall through. */
14641 : :
14642 : 4798 : case OMP_CLAUSE_SELF:
14643 : 4798 : case OMP_CLAUSE_FINAL:
14644 : 4798 : OMP_CLAUSE_OPERAND (c, 0)
14645 : 9596 : = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
14646 : : /* Fall through. */
14647 : :
14648 : 5718 : case OMP_CLAUSE_NUM_TEAMS:
14649 : 5718 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
14650 : 920 : && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14651 : 5963 : && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14652 : : {
14653 : 208 : if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14654 : : {
14655 : : remove = true;
14656 : : break;
14657 : : }
14658 : 208 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14659 : 416 : = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
14660 : : pre_p, NULL, true);
14661 : : }
14662 : : /* Fall through. */
14663 : :
14664 : 21738 : case OMP_CLAUSE_SCHEDULE:
14665 : 21738 : case OMP_CLAUSE_NUM_THREADS:
14666 : 21738 : case OMP_CLAUSE_THREAD_LIMIT:
14667 : 21738 : case OMP_CLAUSE_DIST_SCHEDULE:
14668 : 21738 : case OMP_CLAUSE_DEVICE:
14669 : 21738 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14670 : 21738 : && OMP_CLAUSE_DEVICE_ANCESTOR (c))
14671 : : {
14672 : 125 : if (code != OMP_TARGET)
14673 : : {
14674 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
14675 : : "%<device%> clause with %<ancestor%> is only "
14676 : : "allowed on %<target%> construct");
14677 : 20 : remove = true;
14678 : 20 : break;
14679 : : }
14680 : :
14681 : 105 : tree clauses = *orig_list_p;
14682 : 330 : for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
14683 : 236 : if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
14684 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
14685 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
14686 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
14687 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
14688 : : )
14689 : : {
14690 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
14691 : : "with %<ancestor%>, only the %<device%>, "
14692 : : "%<firstprivate%>, %<private%>, %<defaultmap%>, "
14693 : : "and %<map%> clauses may appear on the "
14694 : : "construct");
14695 : 11 : remove = true;
14696 : 11 : break;
14697 : : }
14698 : : }
14699 : 21613 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14700 : 21613 : && code == OMP_DISPATCH)
14701 : : {
14702 : 272 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
14703 : 272 : gimplify_ctxp->into_ssa = false;
14704 : 272 : if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
14705 : : is_gimple_val, fb_rvalue)
14706 : : == GS_ERROR)
14707 : : remove = true;
14708 : 272 : else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
14709 : 55 : omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
14710 : : GOVD_SHARED | GOVD_SEEN);
14711 : 272 : gimplify_ctxp->into_ssa = saved_into_ssa;
14712 : 272 : break;
14713 : : }
14714 : : /* Fall through. */
14715 : :
14716 : 31159 : case OMP_CLAUSE_PRIORITY:
14717 : 31159 : case OMP_CLAUSE_GRAINSIZE:
14718 : 31159 : case OMP_CLAUSE_NUM_TASKS:
14719 : 31159 : case OMP_CLAUSE_FILTER:
14720 : 31159 : case OMP_CLAUSE_HINT:
14721 : 31159 : case OMP_CLAUSE_ASYNC:
14722 : 31159 : case OMP_CLAUSE_WAIT:
14723 : 31159 : case OMP_CLAUSE_NUM_GANGS:
14724 : 31159 : case OMP_CLAUSE_NUM_WORKERS:
14725 : 31159 : case OMP_CLAUSE_VECTOR_LENGTH:
14726 : 31159 : case OMP_CLAUSE_WORKER:
14727 : 31159 : case OMP_CLAUSE_VECTOR:
14728 : 31159 : if (OMP_CLAUSE_OPERAND (c, 0)
14729 : 31159 : && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
14730 : : {
14731 : 7719 : if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
14732 : : {
14733 : : remove = true;
14734 : : break;
14735 : : }
14736 : : /* All these clauses care about value, not a particular decl,
14737 : : so try to force it into a SSA_NAME or fresh temporary. */
14738 : 7712 : OMP_CLAUSE_OPERAND (c, 0)
14739 : 15424 : = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
14740 : : pre_p, NULL, true);
14741 : : }
14742 : : break;
14743 : :
14744 : 2331 : case OMP_CLAUSE_GANG:
14745 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
14746 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14747 : 0 : remove = true;
14748 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
14749 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14750 : 0 : remove = true;
14751 : : break;
14752 : :
14753 : : case OMP_CLAUSE_NOWAIT:
14754 : 200642 : nowait = 1;
14755 : : break;
14756 : :
14757 : : case OMP_CLAUSE_ORDERED:
14758 : : case OMP_CLAUSE_UNTIED:
14759 : : case OMP_CLAUSE_COLLAPSE:
14760 : : case OMP_CLAUSE_TILE:
14761 : : case OMP_CLAUSE_AUTO:
14762 : : case OMP_CLAUSE_SEQ:
14763 : : case OMP_CLAUSE_INDEPENDENT:
14764 : : case OMP_CLAUSE_MERGEABLE:
14765 : : case OMP_CLAUSE_PROC_BIND:
14766 : : case OMP_CLAUSE_SAFELEN:
14767 : : case OMP_CLAUSE_SIMDLEN:
14768 : : case OMP_CLAUSE_NOGROUP:
14769 : : case OMP_CLAUSE_THREADS:
14770 : : case OMP_CLAUSE_SIMD:
14771 : : case OMP_CLAUSE_BIND:
14772 : : case OMP_CLAUSE_IF_PRESENT:
14773 : : case OMP_CLAUSE_FINALIZE:
14774 : : case OMP_CLAUSE_INTEROP:
14775 : : case OMP_CLAUSE_INIT:
14776 : : case OMP_CLAUSE_USE:
14777 : : case OMP_CLAUSE_DESTROY:
14778 : : break;
14779 : :
14780 : 3969 : case OMP_CLAUSE_ORDER:
14781 : 3969 : ctx->order_concurrent = true;
14782 : 3969 : break;
14783 : :
14784 : 1014 : case OMP_CLAUSE_DEFAULTMAP:
14785 : 1014 : enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
14786 : 1014 : switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
14787 : : {
14788 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
14789 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
14790 : : gdmkmin = GDMK_SCALAR;
14791 : : gdmkmax = GDMK_POINTER;
14792 : : break;
14793 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14794 : : gdmkmin = GDMK_SCALAR;
14795 : : gdmkmax = GDMK_SCALAR_TARGET;
14796 : : break;
14797 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14798 : : gdmkmin = gdmkmax = GDMK_AGGREGATE;
14799 : : break;
14800 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
14801 : : gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
14802 : : break;
14803 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14804 : : gdmkmin = gdmkmax = GDMK_POINTER;
14805 : : break;
14806 : 0 : default:
14807 : 0 : gcc_unreachable ();
14808 : : }
14809 : 4487 : for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
14810 : 3473 : switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
14811 : : {
14812 : 91 : case OMP_CLAUSE_DEFAULTMAP_ALLOC:
14813 : 91 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
14814 : 91 : break;
14815 : 121 : case OMP_CLAUSE_DEFAULTMAP_TO:
14816 : 121 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
14817 : 121 : break;
14818 : 28 : case OMP_CLAUSE_DEFAULTMAP_FROM:
14819 : 28 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
14820 : 28 : break;
14821 : 671 : case OMP_CLAUSE_DEFAULTMAP_TOFROM:
14822 : 671 : ctx->defaultmap[gdmk] = GOVD_MAP;
14823 : 671 : break;
14824 : 380 : case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
14825 : 380 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14826 : 380 : break;
14827 : 2079 : case OMP_CLAUSE_DEFAULTMAP_NONE:
14828 : 2079 : ctx->defaultmap[gdmk] = 0;
14829 : 2079 : break;
14830 : 45 : case OMP_CLAUSE_DEFAULTMAP_PRESENT:
14831 : 45 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
14832 : 45 : break;
14833 : 58 : case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
14834 : 58 : switch (gdmk)
14835 : : {
14836 : 11 : case GDMK_SCALAR:
14837 : 11 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14838 : 11 : break;
14839 : 11 : case GDMK_SCALAR_TARGET:
14840 : 11 : ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
14841 : 11 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
14842 : 11 : break;
14843 : 14 : case GDMK_AGGREGATE:
14844 : 14 : case GDMK_ALLOCATABLE:
14845 : 14 : ctx->defaultmap[gdmk] = GOVD_MAP;
14846 : 14 : break;
14847 : 22 : case GDMK_POINTER:
14848 : 22 : ctx->defaultmap[gdmk] = GOVD_MAP;
14849 : 22 : if (!lang_GNU_Fortran ())
14850 : 14 : ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
14851 : : break;
14852 : : default:
14853 : : gcc_unreachable ();
14854 : : }
14855 : : break;
14856 : 0 : default:
14857 : 0 : gcc_unreachable ();
14858 : : }
14859 : : break;
14860 : :
14861 : 824 : case OMP_CLAUSE_ALIGNED:
14862 : 824 : decl = OMP_CLAUSE_DECL (c);
14863 : 824 : if (error_operand_p (decl))
14864 : : {
14865 : : remove = true;
14866 : : break;
14867 : : }
14868 : 824 : if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
14869 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14870 : : {
14871 : : remove = true;
14872 : : break;
14873 : : }
14874 : 824 : if (!is_global_var (decl)
14875 : 824 : && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14876 : 592 : omp_add_variable (ctx, decl, GOVD_ALIGNED);
14877 : : break;
14878 : :
14879 : 489 : case OMP_CLAUSE_NONTEMPORAL:
14880 : 489 : decl = OMP_CLAUSE_DECL (c);
14881 : 489 : if (error_operand_p (decl))
14882 : : {
14883 : : remove = true;
14884 : : break;
14885 : : }
14886 : 489 : omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
14887 : 489 : break;
14888 : :
14889 : 3541 : case OMP_CLAUSE_ALLOCATE:
14890 : 3541 : decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
14891 : 3541 : if (decl
14892 : 1580 : && TREE_CODE (decl) == INTEGER_CST
14893 : 3570 : && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREADS)
14894 : 3570 : && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
14895 : 35 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
14896 : : "allocator with access trait set to %<thread%> "
14897 : : "results in undefined behavior for %qs directive",
14898 : : code == OMP_TARGET ? "target"
14899 : : : (code == OMP_TASK
14900 : 15 : ? "task" : "taskloop"));
14901 : 3541 : decl = OMP_CLAUSE_DECL (c);
14902 : 3541 : if (error_operand_p (decl))
14903 : : {
14904 : : remove = true;
14905 : : break;
14906 : : }
14907 : 3541 : if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
14908 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14909 : : {
14910 : : remove = true;
14911 : : break;
14912 : : }
14913 : 3541 : else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
14914 : 3541 : || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
14915 : : == INTEGER_CST))
14916 : : ;
14917 : 491 : else if (code == OMP_TASKLOOP
14918 : 491 : || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
14919 : 66 : OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
14920 : 132 : = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
14921 : : pre_p, NULL, false);
14922 : : break;
14923 : :
14924 : 4349 : case OMP_CLAUSE_DEFAULT:
14925 : 4349 : ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
14926 : 4349 : break;
14927 : :
14928 : 640 : case OMP_CLAUSE_INCLUSIVE:
14929 : 640 : case OMP_CLAUSE_EXCLUSIVE:
14930 : 640 : decl = OMP_CLAUSE_DECL (c);
14931 : 640 : {
14932 : 640 : splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
14933 : : (splay_tree_key) decl);
14934 : 640 : if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
14935 : : {
14936 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
14937 : : "%qD specified in %qs clause but not in %<inscan%> "
14938 : : "%<reduction%> clause on the containing construct",
14939 : 5 : decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14940 : 5 : remove = true;
14941 : : }
14942 : : else
14943 : : {
14944 : 635 : n->value |= GOVD_REDUCTION_INSCAN;
14945 : 635 : if (outer_ctx->region_type == ORT_SIMD
14946 : 520 : && outer_ctx->outer_context
14947 : 107 : && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
14948 : : {
14949 : 107 : n = splay_tree_lookup (outer_ctx->outer_context->variables,
14950 : : (splay_tree_key) decl);
14951 : 107 : if (n && (n->value & GOVD_REDUCTION) != 0)
14952 : 107 : n->value |= GOVD_REDUCTION_INSCAN;
14953 : : }
14954 : : }
14955 : : }
14956 : : break;
14957 : :
14958 : 97 : case OMP_CLAUSE_NOVARIANTS:
14959 : 97 : OMP_CLAUSE_NOVARIANTS_EXPR (c)
14960 : 97 : = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
14961 : 97 : break;
14962 : 115 : case OMP_CLAUSE_NOCONTEXT:
14963 : 115 : OMP_CLAUSE_NOCONTEXT_EXPR (c)
14964 : 115 : = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
14965 : 115 : break;
14966 : 0 : case OMP_CLAUSE_NOHOST:
14967 : 0 : default:
14968 : 0 : gcc_unreachable ();
14969 : : }
14970 : :
14971 : 1399 : if (code == OACC_DATA
14972 : 5128 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14973 : 205458 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14974 : 4508 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
14975 : : remove = true;
14976 : 201733 : if (remove)
14977 : 1066 : *list_p = OMP_CLAUSE_CHAIN (c);
14978 : : else
14979 : 200975 : list_p = &OMP_CLAUSE_CHAIN (c);
14980 : 202041 : }
14981 : :
14982 : 129492 : if (groups)
14983 : : {
14984 : 34758 : delete grpmap;
14985 : 17379 : delete groups;
14986 : : }
14987 : :
14988 : 129492 : ctx->clauses = *orig_list_p;
14989 : 129492 : gimplify_omp_ctxp = ctx;
14990 : 129492 : }
14991 : :
14992 : : /* Return true if DECL is a candidate for shared to firstprivate
14993 : : optimization. We only consider non-addressable scalars, not
14994 : : too big, and not references. */
14995 : :
14996 : : static bool
14997 : 403850 : omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
14998 : : {
14999 : 403850 : if (TREE_ADDRESSABLE (decl))
15000 : : return false;
15001 : 355386 : tree type = TREE_TYPE (decl);
15002 : 355386 : if (!is_gimple_reg_type (type)
15003 : 330673 : || TREE_CODE (type) == REFERENCE_TYPE
15004 : 680480 : || TREE_ADDRESSABLE (type))
15005 : : return false;
15006 : : /* Don't optimize too large decls, as each thread/task will have
15007 : : its own. */
15008 : 325094 : HOST_WIDE_INT len = int_size_in_bytes (type);
15009 : 325094 : if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
15010 : : return false;
15011 : 325077 : if (omp_privatize_by_reference (decl))
15012 : : return false;
15013 : : return true;
15014 : : }
15015 : :
15016 : : /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
15017 : : For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
15018 : : GOVD_WRITTEN in outer contexts. */
15019 : :
15020 : : static void
15021 : 302246 : omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
15022 : : {
15023 : 464117 : for (; ctx; ctx = ctx->outer_context)
15024 : : {
15025 : 447143 : splay_tree_node n = splay_tree_lookup (ctx->variables,
15026 : : (splay_tree_key) decl);
15027 : 447143 : if (n == NULL)
15028 : 158295 : continue;
15029 : 288848 : else if (n->value & GOVD_SHARED)
15030 : : {
15031 : 9093 : n->value |= GOVD_WRITTEN;
15032 : 9093 : return;
15033 : : }
15034 : 279755 : else if (n->value & GOVD_DATA_SHARE_CLASS)
15035 : : return;
15036 : : }
15037 : : }
15038 : :
15039 : : /* Helper callback for walk_gimple_seq to discover possible stores
15040 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15041 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15042 : : for those. */
15043 : :
15044 : : static tree
15045 : 1122606 : omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
15046 : : {
15047 : 1122606 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
15048 : :
15049 : 1122606 : *walk_subtrees = 0;
15050 : 1122606 : if (!wi->is_lhs)
15051 : : return NULL_TREE;
15052 : :
15053 : 320956 : tree op = *tp;
15054 : 399657 : do
15055 : : {
15056 : 399657 : if (handled_component_p (op))
15057 : 78701 : op = TREE_OPERAND (op, 0);
15058 : 320956 : else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
15059 : 320956 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
15060 : 0 : op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
15061 : : else
15062 : : break;
15063 : : }
15064 : : while (1);
15065 : 320956 : if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
15066 : 58994 : return NULL_TREE;
15067 : :
15068 : 261962 : omp_mark_stores (gimplify_omp_ctxp, op);
15069 : 261962 : return NULL_TREE;
15070 : : }
15071 : :
15072 : : /* Helper callback for walk_gimple_seq to discover possible stores
15073 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
15074 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
15075 : : for those. */
15076 : :
15077 : : static tree
15078 : 624590 : omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
15079 : : bool *handled_ops_p,
15080 : : struct walk_stmt_info *wi)
15081 : : {
15082 : 624590 : gimple *stmt = gsi_stmt (*gsi_p);
15083 : 624590 : switch (gimple_code (stmt))
15084 : : {
15085 : : /* Don't recurse on OpenMP constructs for which
15086 : : gimplify_adjust_omp_clauses already handled the bodies,
15087 : : except handle gimple_omp_for_pre_body. */
15088 : 26734 : case GIMPLE_OMP_FOR:
15089 : 26734 : *handled_ops_p = true;
15090 : 26734 : if (gimple_omp_for_pre_body (stmt))
15091 : 1390 : walk_gimple_seq (gimple_omp_for_pre_body (stmt),
15092 : : omp_find_stores_stmt, omp_find_stores_op, wi);
15093 : : break;
15094 : 8585 : case GIMPLE_OMP_PARALLEL:
15095 : 8585 : case GIMPLE_OMP_TASK:
15096 : 8585 : case GIMPLE_OMP_SECTIONS:
15097 : 8585 : case GIMPLE_OMP_SINGLE:
15098 : 8585 : case GIMPLE_OMP_SCOPE:
15099 : 8585 : case GIMPLE_OMP_TARGET:
15100 : 8585 : case GIMPLE_OMP_TEAMS:
15101 : 8585 : case GIMPLE_OMP_CRITICAL:
15102 : 8585 : *handled_ops_p = true;
15103 : 8585 : break;
15104 : : default:
15105 : : break;
15106 : : }
15107 : 624590 : return NULL_TREE;
15108 : : }
15109 : :
15110 : : struct gimplify_adjust_omp_clauses_data
15111 : : {
15112 : : tree *list_p;
15113 : : gimple_seq *pre_p;
15114 : : };
15115 : :
15116 : : /* For all variables that were not actually used within the context,
15117 : : remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
15118 : :
15119 : : static int
15120 : 662911 : gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
15121 : : {
15122 : 662911 : tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
15123 : 662911 : gimple_seq *pre_p
15124 : : = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
15125 : 662911 : tree decl = (tree) n->key;
15126 : 662911 : unsigned flags = n->value;
15127 : 662911 : enum omp_clause_code code;
15128 : 662911 : tree clause;
15129 : 662911 : bool private_debug;
15130 : :
15131 : 662911 : if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
15132 : 132828 : && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
15133 : : flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
15134 : 662786 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
15135 : : return 0;
15136 : 177738 : if ((flags & GOVD_SEEN) == 0)
15137 : : return 0;
15138 : 156969 : if (flags & GOVD_DEBUG_PRIVATE)
15139 : : {
15140 : 256 : gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
15141 : : private_debug = true;
15142 : : }
15143 : 156713 : else if (flags & GOVD_MAP)
15144 : : private_debug = false;
15145 : : else
15146 : 138664 : private_debug
15147 : 138664 : = lang_hooks.decls.omp_private_debug_clause (decl,
15148 : 138664 : !!(flags & GOVD_SHARED));
15149 : 138664 : if (private_debug)
15150 : : code = OMP_CLAUSE_PRIVATE;
15151 : 156591 : else if (flags & GOVD_MAP)
15152 : : {
15153 : 18049 : code = OMP_CLAUSE_MAP;
15154 : 18049 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15155 : 18049 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15156 : : {
15157 : 2 : error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
15158 : 2 : return 0;
15159 : : }
15160 : 18047 : if (VAR_P (decl)
15161 : 16111 : && DECL_IN_CONSTANT_POOL (decl)
15162 : 18048 : && !lookup_attribute ("omp declare target",
15163 : 1 : DECL_ATTRIBUTES (decl)))
15164 : : {
15165 : 1 : tree id = get_identifier ("omp declare target");
15166 : 1 : DECL_ATTRIBUTES (decl)
15167 : 1 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
15168 : 1 : varpool_node *node = varpool_node::get (decl);
15169 : 1 : if (node)
15170 : : {
15171 : 1 : node->offloadable = 1;
15172 : 1 : if (ENABLE_OFFLOADING)
15173 : : g->have_offload = true;
15174 : : }
15175 : : }
15176 : : }
15177 : 138542 : else if (flags & GOVD_SHARED)
15178 : : {
15179 : 49010 : if (is_global_var (decl))
15180 : : {
15181 : 16107 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15182 : 25287 : while (ctx != NULL)
15183 : : {
15184 : 17593 : splay_tree_node on
15185 : 17593 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15186 : 17593 : if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
15187 : : | GOVD_PRIVATE | GOVD_REDUCTION
15188 : : | GOVD_LINEAR | GOVD_MAP)) != 0)
15189 : : break;
15190 : 9180 : ctx = ctx->outer_context;
15191 : : }
15192 : 16107 : if (ctx == NULL)
15193 : : return 0;
15194 : : }
15195 : 41316 : code = OMP_CLAUSE_SHARED;
15196 : : /* Don't optimize shared into firstprivate for read-only vars
15197 : : on tasks with depend clause, we shouldn't try to copy them
15198 : : until the dependencies are satisfied. */
15199 : 41316 : if (gimplify_omp_ctxp->has_depend)
15200 : 350 : flags |= GOVD_WRITTEN;
15201 : : }
15202 : 89532 : else if (flags & GOVD_PRIVATE)
15203 : : code = OMP_CLAUSE_PRIVATE;
15204 : 31088 : else if (flags & GOVD_FIRSTPRIVATE)
15205 : : {
15206 : 21685 : code = OMP_CLAUSE_FIRSTPRIVATE;
15207 : 21685 : if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
15208 : 13683 : && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
15209 : 31687 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
15210 : : {
15211 : 1 : error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
15212 : : "%<target%> construct", decl);
15213 : 1 : return 0;
15214 : : }
15215 : : }
15216 : 9403 : else if (flags & GOVD_LASTPRIVATE)
15217 : : code = OMP_CLAUSE_LASTPRIVATE;
15218 : 241 : else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
15219 : : return 0;
15220 : 118 : else if (flags & GOVD_CONDTEMP)
15221 : : {
15222 : 118 : code = OMP_CLAUSE__CONDTEMP_;
15223 : 118 : gimple_add_tmp_var (decl);
15224 : : }
15225 : : else
15226 : 0 : gcc_unreachable ();
15227 : :
15228 : 139987 : if (((flags & GOVD_LASTPRIVATE)
15229 : 139297 : || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
15230 : 155195 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15231 : 15194 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15232 : :
15233 : 149149 : tree chain = *list_p;
15234 : 149149 : clause = build_omp_clause (input_location, code);
15235 : 149149 : OMP_CLAUSE_DECL (clause) = decl;
15236 : 149149 : OMP_CLAUSE_CHAIN (clause) = chain;
15237 : 149149 : if (private_debug)
15238 : 378 : OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
15239 : 148771 : else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
15240 : 6 : OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
15241 : 148765 : else if (code == OMP_CLAUSE_SHARED
15242 : 41316 : && (flags & GOVD_WRITTEN) == 0
15243 : 184035 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15244 : 20986 : OMP_CLAUSE_SHARED_READONLY (clause) = 1;
15245 : 127779 : else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
15246 : 21684 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
15247 : 106095 : else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
15248 : : {
15249 : 644 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
15250 : 644 : OMP_CLAUSE_DECL (nc) = decl;
15251 : 644 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
15252 : 644 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
15253 : 15 : OMP_CLAUSE_DECL (clause)
15254 : 30 : = build_fold_indirect_ref_loc (input_location, decl);
15255 : 644 : OMP_CLAUSE_DECL (clause)
15256 : 644 : = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
15257 : : build_int_cst (build_pointer_type (char_type_node), 0));
15258 : 644 : OMP_CLAUSE_SIZE (clause) = size_zero_node;
15259 : 644 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15260 : 644 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
15261 : 644 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
15262 : 644 : tree dtype = TREE_TYPE (decl);
15263 : 644 : if (TREE_CODE (dtype) == REFERENCE_TYPE)
15264 : 15 : dtype = TREE_TYPE (dtype);
15265 : : /* FIRSTPRIVATE_POINTER doesn't work well if we have a
15266 : : multiply-indirected pointer. If we have a reference to a pointer to
15267 : : a pointer, it's possible that this should really be
15268 : : GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
15269 : : moment, so stick with this. (See PR113279 and testcases
15270 : : baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice). */
15271 : 644 : if (TREE_CODE (dtype) == POINTER_TYPE
15272 : 644 : && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
15273 : 19 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15274 : : else
15275 : 625 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15276 : 644 : OMP_CLAUSE_CHAIN (nc) = chain;
15277 : 644 : OMP_CLAUSE_CHAIN (clause) = nc;
15278 : 644 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15279 : 644 : gimplify_omp_ctxp = ctx->outer_context;
15280 : 644 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
15281 : : pre_p, NULL, is_gimple_val, fb_rvalue);
15282 : 644 : gimplify_omp_ctxp = ctx;
15283 : 644 : }
15284 : 17403 : else if (code == OMP_CLAUSE_MAP)
15285 : : {
15286 : 17403 : int kind;
15287 : : /* Not all combinations of these GOVD_MAP flags are actually valid. */
15288 : 17403 : switch (flags & (GOVD_MAP_TO_ONLY
15289 : : | GOVD_MAP_FORCE
15290 : : | GOVD_MAP_FORCE_PRESENT
15291 : : | GOVD_MAP_ALLOC_ONLY
15292 : : | GOVD_MAP_FROM_ONLY))
15293 : : {
15294 : : case 0:
15295 : : kind = GOMP_MAP_TOFROM;
15296 : : break;
15297 : 1062 : case GOVD_MAP_FORCE:
15298 : 1062 : kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
15299 : 1062 : break;
15300 : 868 : case GOVD_MAP_TO_ONLY:
15301 : 868 : kind = GOMP_MAP_TO;
15302 : 868 : break;
15303 : 16 : case GOVD_MAP_FROM_ONLY:
15304 : 16 : kind = GOMP_MAP_FROM;
15305 : 16 : break;
15306 : 37 : case GOVD_MAP_ALLOC_ONLY:
15307 : 37 : kind = GOMP_MAP_ALLOC;
15308 : 37 : break;
15309 : 0 : case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
15310 : 0 : kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
15311 : 0 : break;
15312 : : case GOVD_MAP_FORCE_PRESENT:
15313 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
15314 : : break;
15315 : : case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
15316 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
15317 : : break;
15318 : 0 : default:
15319 : 0 : gcc_unreachable ();
15320 : : }
15321 : 17403 : OMP_CLAUSE_SET_MAP_KIND (clause, kind);
15322 : : /* Setting of the implicit flag for the runtime is currently disabled for
15323 : : OpenACC. */
15324 : 17403 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
15325 : 9838 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
15326 : 17403 : if (DECL_SIZE (decl)
15327 : 17403 : && !poly_int_tree_p (DECL_SIZE (decl)))
15328 : : {
15329 : 535 : tree decl2 = DECL_VALUE_EXPR (decl);
15330 : 535 : gcc_assert (INDIRECT_REF_P (decl2));
15331 : 535 : decl2 = TREE_OPERAND (decl2, 0);
15332 : 535 : gcc_assert (DECL_P (decl2));
15333 : 535 : tree mem = build_simple_mem_ref (decl2);
15334 : 535 : OMP_CLAUSE_DECL (clause) = mem;
15335 : 535 : OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15336 : 535 : if (gimplify_omp_ctxp->outer_context)
15337 : : {
15338 : 446 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
15339 : 446 : omp_notice_variable (ctx, decl2, true);
15340 : 446 : omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
15341 : : }
15342 : 535 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15343 : : OMP_CLAUSE_MAP);
15344 : 535 : OMP_CLAUSE_DECL (nc) = decl;
15345 : 535 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15346 : 535 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
15347 : 535 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
15348 : : else
15349 : 0 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15350 : 535 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15351 : 535 : OMP_CLAUSE_CHAIN (clause) = nc;
15352 : : }
15353 : 16868 : else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
15354 : 16868 : && omp_privatize_by_reference (decl))
15355 : : {
15356 : 29 : OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
15357 : 29 : OMP_CLAUSE_SIZE (clause)
15358 : 29 : = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
15359 : 29 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15360 : 29 : gimplify_omp_ctxp = ctx->outer_context;
15361 : 29 : gimplify_expr (&OMP_CLAUSE_SIZE (clause),
15362 : : pre_p, NULL, is_gimple_val, fb_rvalue);
15363 : 29 : gimplify_omp_ctxp = ctx;
15364 : 29 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
15365 : : OMP_CLAUSE_MAP);
15366 : 29 : OMP_CLAUSE_DECL (nc) = decl;
15367 : 29 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15368 : 29 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
15369 : 29 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
15370 : 29 : OMP_CLAUSE_CHAIN (clause) = nc;
15371 : : }
15372 : : else
15373 : 16839 : OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
15374 : : }
15375 : 149149 : if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
15376 : : {
15377 : 690 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
15378 : 690 : OMP_CLAUSE_DECL (nc) = decl;
15379 : 690 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
15380 : 690 : OMP_CLAUSE_CHAIN (nc) = chain;
15381 : 690 : OMP_CLAUSE_CHAIN (clause) = nc;
15382 : 690 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15383 : 690 : gimplify_omp_ctxp = ctx->outer_context;
15384 : 690 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
15385 : 690 : (ctx->region_type & ORT_ACC) != 0);
15386 : 690 : gimplify_omp_ctxp = ctx;
15387 : : }
15388 : 149149 : *list_p = clause;
15389 : 149149 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15390 : 149149 : gimplify_omp_ctxp = ctx->outer_context;
15391 : : /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
15392 : : in simd. Those are only added for the local vars inside of simd body
15393 : : and they don't need to be e.g. default constructible. */
15394 : 149149 : if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
15395 : 141128 : lang_hooks.decls.omp_finish_clause (clause, pre_p,
15396 : 141128 : (ctx->region_type & ORT_ACC) != 0);
15397 : 149149 : if (gimplify_omp_ctxp)
15398 : 184145 : for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
15399 : 93487 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
15400 : 93487 : && DECL_P (OMP_CLAUSE_SIZE (clause)))
15401 : 1243 : omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
15402 : : true);
15403 : 149149 : gimplify_omp_ctxp = ctx;
15404 : 149149 : return 0;
15405 : : }
15406 : :
15407 : : static void
15408 : 128034 : gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
15409 : : enum tree_code code,
15410 : : gimple_seq *loops_seq_p = NULL)
15411 : : {
15412 : 128034 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15413 : 128034 : tree *orig_list_p = list_p;
15414 : 128034 : tree c, decl;
15415 : 128034 : bool has_inscan_reductions = false;
15416 : :
15417 : 128034 : if (body)
15418 : : {
15419 : : struct gimplify_omp_ctx *octx;
15420 : 223373 : for (octx = ctx; octx; octx = octx->outer_context)
15421 : 172965 : if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
15422 : : break;
15423 : 110793 : if (octx)
15424 : : {
15425 : 60385 : struct walk_stmt_info wi;
15426 : 60385 : memset (&wi, 0, sizeof (wi));
15427 : 60385 : walk_gimple_seq (body, omp_find_stores_stmt,
15428 : : omp_find_stores_op, &wi);
15429 : : }
15430 : : }
15431 : :
15432 : 128034 : if (ctx->add_safelen1)
15433 : : {
15434 : : /* If there are VLAs in the body of simd loop, prevent
15435 : : vectorization. */
15436 : 2 : gcc_assert (ctx->region_type == ORT_SIMD);
15437 : 2 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
15438 : 2 : OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
15439 : 2 : OMP_CLAUSE_CHAIN (c) = *list_p;
15440 : 2 : *list_p = c;
15441 : 2 : list_p = &OMP_CLAUSE_CHAIN (c);
15442 : : }
15443 : :
15444 : 128034 : if (ctx->region_type == ORT_WORKSHARE
15445 : 39293 : && ctx->outer_context
15446 : 27895 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
15447 : : {
15448 : 24952 : for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
15449 : 12585 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15450 : 12585 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
15451 : : {
15452 : 125 : decl = OMP_CLAUSE_DECL (c);
15453 : 125 : splay_tree_node n
15454 : 125 : = splay_tree_lookup (ctx->outer_context->variables,
15455 : : (splay_tree_key) decl);
15456 : 125 : gcc_checking_assert (!splay_tree_lookup (ctx->variables,
15457 : : (splay_tree_key) decl));
15458 : 125 : omp_add_variable (ctx, decl, n->value);
15459 : 125 : tree c2 = copy_node (c);
15460 : 125 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15461 : 125 : *list_p = c2;
15462 : 125 : if ((n->value & GOVD_FIRSTPRIVATE) == 0)
15463 : 103 : continue;
15464 : 22 : c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15465 : : OMP_CLAUSE_FIRSTPRIVATE);
15466 : 22 : OMP_CLAUSE_DECL (c2) = decl;
15467 : 22 : OMP_CLAUSE_CHAIN (c2) = *list_p;
15468 : 22 : *list_p = c2;
15469 : : }
15470 : : }
15471 : :
15472 : 128034 : if (code == OMP_TARGET
15473 : 128034 : || code == OMP_TARGET_DATA
15474 : 128034 : || code == OMP_TARGET_ENTER_DATA
15475 : 112813 : || code == OMP_TARGET_EXIT_DATA)
15476 : : {
15477 : 15869 : tree mapper_clauses = NULL_TREE;
15478 : 15869 : instantiate_mapper_info im_info;
15479 : :
15480 : 15869 : im_info.mapper_clauses_p = &mapper_clauses;
15481 : 15869 : im_info.omp_ctx = ctx;
15482 : 15869 : im_info.pre_p = pre_p;
15483 : :
15484 : 15869 : splay_tree_foreach (ctx->variables,
15485 : : omp_instantiate_implicit_mappers,
15486 : : (void *) &im_info);
15487 : :
15488 : 15869 : if (mapper_clauses)
15489 : : {
15490 : 45 : mapper_clauses
15491 : 45 : = lang_hooks.decls.omp_finish_mapper_clauses (mapper_clauses);
15492 : :
15493 : : /* Stick the implicitly-expanded mapper clauses at the end of the
15494 : : clause list. */
15495 : 45 : tree *tail = list_p;
15496 : 139 : while (*tail)
15497 : 94 : tail = &OMP_CLAUSE_CHAIN (*tail);
15498 : 45 : *tail = mapper_clauses;
15499 : : }
15500 : :
15501 : 15869 : vec<omp_mapping_group> *groups;
15502 : 15869 : groups = omp_gather_mapping_groups (list_p);
15503 : 15869 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
15504 : :
15505 : 15869 : if (groups)
15506 : : {
15507 : 7701 : grpmap = omp_index_mapping_groups (groups);
15508 : :
15509 : 7701 : omp_resolve_clause_dependencies (code, groups, grpmap);
15510 : 7701 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15511 : : &grpmap, list_p);
15512 : :
15513 : 7701 : omp_mapping_group *outlist = NULL;
15514 : :
15515 : 15402 : delete grpmap;
15516 : 7701 : delete groups;
15517 : :
15518 : : /* Rebuild now we have struct sibling lists. */
15519 : 7701 : groups = omp_gather_mapping_groups (list_p);
15520 : 7701 : grpmap = omp_index_mapping_groups (groups);
15521 : :
15522 : 7701 : bool enter_exit = (code == OMP_TARGET_ENTER_DATA
15523 : 7701 : || code == OMP_TARGET_EXIT_DATA);
15524 : :
15525 : 7701 : outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
15526 : 7701 : outlist = omp_segregate_mapping_groups (outlist);
15527 : 7701 : list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
15528 : :
15529 : 7701 : delete grpmap;
15530 : 7701 : delete groups;
15531 : : }
15532 : 15869 : }
15533 : 112165 : else if (ctx->region_type & ORT_ACC)
15534 : : {
15535 : 29947 : vec<omp_mapping_group> *groups;
15536 : 29947 : groups = omp_gather_mapping_groups (list_p);
15537 : 29947 : if (groups)
15538 : : {
15539 : 9469 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
15540 : 9469 : grpmap = omp_index_mapping_groups (groups);
15541 : :
15542 : 9469 : oacc_resolve_clause_dependencies (groups, grpmap);
15543 : 9469 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15544 : : &grpmap, list_p);
15545 : :
15546 : 9469 : delete groups;
15547 : 18938 : delete grpmap;
15548 : : }
15549 : : }
15550 : :
15551 : 128034 : tree attach_list = NULL_TREE;
15552 : 128034 : tree *attach_tail = &attach_list;
15553 : :
15554 : 128034 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
15555 : :
15556 : 371323 : while ((c = *list_p) != NULL)
15557 : : {
15558 : 243289 : splay_tree_node n;
15559 : 243289 : bool remove = false;
15560 : 243289 : bool move_attach = false;
15561 : :
15562 : 283504 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
15563 : : grp_end = NULL_TREE;
15564 : :
15565 : 243289 : switch (OMP_CLAUSE_CODE (c))
15566 : : {
15567 : 7859 : case OMP_CLAUSE_FIRSTPRIVATE:
15568 : 7859 : if ((ctx->region_type & ORT_TARGET)
15569 : 1836 : && (ctx->region_type & ORT_ACC) == 0
15570 : 9089 : && TYPE_ATOMIC (strip_array_types
15571 : : (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
15572 : : {
15573 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15574 : : "%<_Atomic%> %qD in %<firstprivate%> clause on "
15575 : 2 : "%<target%> construct", OMP_CLAUSE_DECL (c));
15576 : 2 : remove = true;
15577 : 2 : break;
15578 : : }
15579 : 7857 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
15580 : : {
15581 : 380 : decl = OMP_CLAUSE_DECL (c);
15582 : 380 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15583 : 380 : if ((n->value & GOVD_MAP) != 0)
15584 : : {
15585 : : remove = true;
15586 : : break;
15587 : : }
15588 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
15589 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
15590 : : }
15591 : : /* FALLTHRU */
15592 : 36519 : case OMP_CLAUSE_PRIVATE:
15593 : 36519 : case OMP_CLAUSE_SHARED:
15594 : 36519 : case OMP_CLAUSE_LINEAR:
15595 : 36519 : decl = OMP_CLAUSE_DECL (c);
15596 : 36519 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15597 : 36519 : remove = !(n->value & GOVD_SEEN);
15598 : 36519 : if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
15599 : 48 : && code == OMP_PARALLEL
15600 : 36541 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15601 : : remove = true;
15602 : 36497 : if (! remove)
15603 : : {
15604 : 31922 : bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
15605 : 31922 : if ((n->value & GOVD_DEBUG_PRIVATE)
15606 : 31922 : || lang_hooks.decls.omp_private_debug_clause (decl, shared))
15607 : : {
15608 : 83 : gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
15609 : : || ((n->value & GOVD_DATA_SHARE_CLASS)
15610 : : == GOVD_SHARED));
15611 : 83 : OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
15612 : 83 : OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
15613 : : }
15614 : 31922 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15615 : 4129 : && ctx->has_depend
15616 : 32480 : && DECL_P (decl))
15617 : 558 : n->value |= GOVD_WRITTEN;
15618 : 31922 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15619 : 4129 : && (n->value & GOVD_WRITTEN) == 0
15620 : 3018 : && DECL_P (decl)
15621 : 34940 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15622 : 526 : OMP_CLAUSE_SHARED_READONLY (c) = 1;
15623 : 31396 : else if (DECL_P (decl)
15624 : 31396 : && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15625 : 3603 : && (n->value & GOVD_WRITTEN) != 0)
15626 : 30285 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15627 : 9024 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
15628 : 38426 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15629 : 5726 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15630 : : }
15631 : : else
15632 : 4597 : n->value &= ~GOVD_EXPLICIT;
15633 : : break;
15634 : :
15635 : 13002 : case OMP_CLAUSE_LASTPRIVATE:
15636 : : /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
15637 : : accurately reflect the presence of a FIRSTPRIVATE clause. */
15638 : 13002 : decl = OMP_CLAUSE_DECL (c);
15639 : 13002 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15640 : 13002 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
15641 : 13002 : = (n->value & GOVD_FIRSTPRIVATE) != 0;
15642 : 13002 : if (code == OMP_DISTRIBUTE
15643 : 13002 : && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
15644 : : {
15645 : 4 : remove = true;
15646 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15647 : : "same variable used in %<firstprivate%> and "
15648 : : "%<lastprivate%> clauses on %<distribute%> "
15649 : : "construct");
15650 : : }
15651 : 13002 : if (!remove
15652 : 12998 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15653 : 12998 : && DECL_P (decl)
15654 : 12998 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15655 : 11405 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15656 : 13002 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
15657 : : remove = true;
15658 : : break;
15659 : :
15660 : 824 : case OMP_CLAUSE_ALIGNED:
15661 : 824 : decl = OMP_CLAUSE_DECL (c);
15662 : 824 : if (!is_global_var (decl))
15663 : : {
15664 : 738 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15665 : 738 : remove = n == NULL || !(n->value & GOVD_SEEN);
15666 : 88 : if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15667 : : {
15668 : 88 : struct gimplify_omp_ctx *octx;
15669 : 88 : if (n != NULL
15670 : 88 : && (n->value & (GOVD_DATA_SHARE_CLASS
15671 : : & ~GOVD_FIRSTPRIVATE)))
15672 : : remove = true;
15673 : : else
15674 : 100 : for (octx = ctx->outer_context; octx;
15675 : 12 : octx = octx->outer_context)
15676 : : {
15677 : 24 : n = splay_tree_lookup (octx->variables,
15678 : : (splay_tree_key) decl);
15679 : 24 : if (n == NULL)
15680 : 12 : continue;
15681 : 12 : if (n->value & GOVD_LOCAL)
15682 : : break;
15683 : : /* We have to avoid assigning a shared variable
15684 : : to itself when trying to add
15685 : : __builtin_assume_aligned. */
15686 : 12 : if (n->value & GOVD_SHARED)
15687 : : {
15688 : : remove = true;
15689 : : break;
15690 : : }
15691 : : }
15692 : : }
15693 : : }
15694 : 86 : else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
15695 : : {
15696 : 83 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15697 : 83 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
15698 : : remove = true;
15699 : : }
15700 : : break;
15701 : :
15702 : 538 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
15703 : 538 : decl = OMP_CLAUSE_DECL (c);
15704 : 538 : while (INDIRECT_REF_P (decl)
15705 : 587 : || TREE_CODE (decl) == ARRAY_REF)
15706 : 49 : decl = TREE_OPERAND (decl, 0);
15707 : 538 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15708 : 538 : remove = n == NULL || !(n->value & GOVD_SEEN);
15709 : : break;
15710 : :
15711 : 898 : case OMP_CLAUSE_IS_DEVICE_PTR:
15712 : 898 : case OMP_CLAUSE_NONTEMPORAL:
15713 : 898 : decl = OMP_CLAUSE_DECL (c);
15714 : 898 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15715 : 898 : remove = n == NULL || !(n->value & GOVD_SEEN);
15716 : : break;
15717 : :
15718 : 53496 : case OMP_CLAUSE_MAP:
15719 : 53496 : decl = OMP_CLAUSE_DECL (c);
15720 : 53496 : if (!grp_end)
15721 : : {
15722 : 30145 : grp_start_p = list_p;
15723 : 30145 : grp_end = *omp_group_last (grp_start_p);
15724 : : }
15725 : 53496 : switch (OMP_CLAUSE_MAP_KIND (c))
15726 : : {
15727 : 123 : case GOMP_MAP_PRESENT_ALLOC:
15728 : 123 : case GOMP_MAP_PRESENT_TO:
15729 : 123 : case GOMP_MAP_PRESENT_FROM:
15730 : 123 : case GOMP_MAP_PRESENT_TOFROM:
15731 : 123 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
15732 : 123 : break;
15733 : : default:
15734 : : break;
15735 : : }
15736 : 53496 : switch (code)
15737 : : {
15738 : 4569 : case OACC_DATA:
15739 : 4569 : if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
15740 : : break;
15741 : : /* Fallthrough. */
15742 : 17125 : case OACC_HOST_DATA:
15743 : 17125 : case OACC_ENTER_DATA:
15744 : 17125 : case OACC_EXIT_DATA:
15745 : 17125 : case OMP_TARGET_DATA:
15746 : 17125 : case OMP_TARGET_ENTER_DATA:
15747 : 17125 : case OMP_TARGET_EXIT_DATA:
15748 : 17125 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15749 : 17125 : || (OMP_CLAUSE_MAP_KIND (c)
15750 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15751 : : /* For target {,enter ,exit }data only the array slice is
15752 : : mapped, but not the pointer to it. */
15753 : : remove = true;
15754 : 17125 : if (code == OMP_TARGET_EXIT_DATA
15755 : 17125 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
15756 : 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
15757 : : remove = true;
15758 : : break;
15759 : : case OMP_TARGET:
15760 : : break;
15761 : : default:
15762 : : break;
15763 : : }
15764 : 17123 : if (remove)
15765 : : break;
15766 : 53161 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15767 : : {
15768 : : /* Sanity check: attach/detach map kinds use the size as a bias,
15769 : : and it's never right to use the decl size for such
15770 : : mappings. */
15771 : 16861 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15772 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15773 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
15774 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15775 : : && (OMP_CLAUSE_MAP_KIND (c)
15776 : : != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
15777 : 20642 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
15778 : 3781 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
15779 : : }
15780 : 53161 : gimplify_omp_ctxp = ctx->outer_context;
15781 : 53161 : gimple_seq *seq_p;
15782 : 53161 : seq_p = enter_omp_iterator_loop_context (c, loops_seq_p, pre_p);
15783 : 53161 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), seq_p, NULL,
15784 : : is_gimple_val, fb_rvalue) == GS_ERROR)
15785 : : {
15786 : 0 : gimplify_omp_ctxp = ctx;
15787 : 0 : remove = true;
15788 : 0 : goto end_adjust_omp_map_clause;
15789 : : }
15790 : 53161 : else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15791 : 50371 : || (OMP_CLAUSE_MAP_KIND (c)
15792 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15793 : 49962 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15794 : 59180 : && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
15795 : : {
15796 : 842 : OMP_CLAUSE_SIZE (c)
15797 : 842 : = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), seq_p, NULL,
15798 : : false);
15799 : 842 : if ((ctx->region_type & ORT_TARGET) != 0)
15800 : 643 : omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
15801 : : GOVD_FIRSTPRIVATE | GOVD_SEEN);
15802 : : }
15803 : 53161 : gimplify_omp_ctxp = ctx;
15804 : : /* Data clauses associated with reductions must be
15805 : : compatible with present_or_copy. Warn and adjust the clause
15806 : : if that is not the case. */
15807 : 53161 : if (ctx->region_type == ORT_ACC_PARALLEL
15808 : 44181 : || ctx->region_type == ORT_ACC_SERIAL)
15809 : : {
15810 : 9536 : tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
15811 : 9536 : n = NULL;
15812 : :
15813 : 9536 : if (DECL_P (t))
15814 : 7771 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
15815 : :
15816 : 7771 : if (n && (n->value & GOVD_REDUCTION))
15817 : : {
15818 : 809 : enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
15819 : :
15820 : 809 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15821 : 809 : if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
15822 : 159 : && kind != GOMP_MAP_FORCE_PRESENT
15823 : 159 : && kind != GOMP_MAP_POINTER)
15824 : : {
15825 : 120 : warning_at (OMP_CLAUSE_LOCATION (c), 0,
15826 : : "incompatible data clause with reduction "
15827 : : "on %qE; promoting to %<present_or_copy%>",
15828 : 120 : DECL_NAME (t));
15829 : 120 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
15830 : : }
15831 : : }
15832 : : }
15833 : 53161 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15834 : 50564 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15835 : 53269 : && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
15836 : : {
15837 : 412 : remove = true;
15838 : 412 : goto end_adjust_omp_map_clause;
15839 : : }
15840 : : /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
15841 : : a variable captured in a lambda closure), look through that now
15842 : : before the DECL_P check below. (A code other than COMPONENT_REF,
15843 : : i.e. INDIRECT_REF, will be a VLA/variable-length array
15844 : : section. A global var may be a variable in a common block. We
15845 : : don't want to do this here for either of those.) */
15846 : 52749 : if ((ctx->region_type & ORT_ACC) == 0
15847 : 26540 : && DECL_P (decl)
15848 : 11709 : && !is_global_var (decl)
15849 : 10418 : && DECL_HAS_VALUE_EXPR_P (decl)
15850 : 52939 : && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
15851 : 0 : decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
15852 : 52749 : if (TREE_CODE (decl) == TARGET_EXPR)
15853 : : {
15854 : 0 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), seq_p, NULL,
15855 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
15856 : 25266 : remove = true;
15857 : : }
15858 : 52749 : else if (!DECL_P (decl))
15859 : : {
15860 : 27483 : if ((ctx->region_type & ORT_TARGET) != 0
15861 : 27483 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15862 : : {
15863 : 24 : if (INDIRECT_REF_P (decl)
15864 : 0 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15865 : 24 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15866 : : == REFERENCE_TYPE))
15867 : 0 : decl = TREE_OPERAND (decl, 0);
15868 : 24 : if (TREE_CODE (decl) == COMPONENT_REF)
15869 : : {
15870 : 0 : while (TREE_CODE (decl) == COMPONENT_REF)
15871 : 0 : decl = TREE_OPERAND (decl, 0);
15872 : 0 : if (DECL_P (decl))
15873 : : {
15874 : 0 : n = splay_tree_lookup (ctx->variables,
15875 : : (splay_tree_key) decl);
15876 : 0 : if (!(n->value & GOVD_SEEN))
15877 : 27483 : remove = true;
15878 : : }
15879 : : }
15880 : : }
15881 : :
15882 : 27483 : tree d = decl, *pd;
15883 : 27483 : if (TREE_CODE (d) == ARRAY_REF)
15884 : : {
15885 : 5594 : while (TREE_CODE (d) == ARRAY_REF)
15886 : 2857 : d = TREE_OPERAND (d, 0);
15887 : 2737 : if (TREE_CODE (d) == COMPONENT_REF
15888 : 2737 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
15889 : : decl = d;
15890 : : }
15891 : 27483 : pd = &OMP_CLAUSE_DECL (c);
15892 : 27483 : if (d == decl
15893 : 25103 : && TREE_CODE (decl) == INDIRECT_REF
15894 : 14858 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15895 : 1470 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15896 : : == REFERENCE_TYPE)
15897 : 28283 : && (OMP_CLAUSE_MAP_KIND (c)
15898 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
15899 : : {
15900 : 796 : pd = &TREE_OPERAND (decl, 0);
15901 : 796 : decl = TREE_OPERAND (decl, 0);
15902 : : }
15903 : :
15904 : 27483 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15905 : 2591 : switch (code)
15906 : : {
15907 : 427 : case OACC_ENTER_DATA:
15908 : 427 : case OACC_EXIT_DATA:
15909 : 427 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
15910 : : == ARRAY_TYPE)
15911 : : remove = true;
15912 : 417 : else if (code == OACC_ENTER_DATA)
15913 : 263 : goto change_to_attach;
15914 : : /* Fallthrough. */
15915 : 347 : case OMP_TARGET_EXIT_DATA:
15916 : 347 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
15917 : 347 : break;
15918 : 71 : case OACC_UPDATE:
15919 : : /* An "attach/detach" operation on an update directive
15920 : : should behave as a GOMP_MAP_ALWAYS_POINTER. Note that
15921 : : both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
15922 : : kinds depend on the previous mapping (for non-TARGET
15923 : : regions). */
15924 : 71 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
15925 : 71 : break;
15926 : 2173 : default:
15927 : 2173 : change_to_attach:
15928 : 2173 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
15929 : 2173 : if ((ctx->region_type & ORT_TARGET) != 0)
15930 : 1769 : move_attach = true;
15931 : : }
15932 : 24892 : else if ((ctx->region_type & ORT_TARGET) != 0
15933 : 24892 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15934 : 11485 : || (OMP_CLAUSE_MAP_KIND (c)
15935 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15936 : : move_attach = true;
15937 : :
15938 : : /* If we have e.g. map(struct: *var), don't gimplify the
15939 : : argument since omp-low.cc wants to see the decl itself. */
15940 : 27483 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
15941 : 1135 : goto end_adjust_omp_map_clause;
15942 : :
15943 : : /* We've already partly gimplified this in
15944 : : gimplify_scan_omp_clauses. Don't do any more. */
15945 : 26348 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
15946 : 88 : goto end_adjust_omp_map_clause;
15947 : :
15948 : 26260 : gimplify_omp_ctxp = ctx->outer_context;
15949 : 26260 : if (gimplify_expr (pd, seq_p, NULL, is_gimple_lvalue,
15950 : : fb_lvalue) == GS_ERROR)
15951 : 0 : remove = true;
15952 : 26260 : gimplify_omp_ctxp = ctx;
15953 : 26260 : goto end_adjust_omp_map_clause;
15954 : : }
15955 : :
15956 : 25266 : if ((code == OMP_TARGET
15957 : : || code == OMP_TARGET_DATA
15958 : : || code == OMP_TARGET_ENTER_DATA
15959 : 14367 : || code == OMP_TARGET_EXIT_DATA)
15960 : 25814 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15961 : : {
15962 : 229 : bool firstprivatize = false;
15963 : :
15964 : 234 : for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
15965 : 5 : octx = octx->outer_context)
15966 : : {
15967 : 6 : splay_tree_node n
15968 : 12 : = splay_tree_lookup (octx->variables,
15969 : 6 : (splay_tree_key) OMP_CLAUSE_DECL (c));
15970 : : /* If this is contained in an outer OpenMP region as a
15971 : : firstprivate value, remove the attach/detach. */
15972 : 6 : if (n && (n->value & GOVD_FIRSTPRIVATE))
15973 : : {
15974 : : firstprivatize = true;
15975 : : break;
15976 : : }
15977 : : }
15978 : :
15979 : 229 : enum gomp_map_kind map_kind;
15980 : 229 : if (firstprivatize)
15981 : : map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
15982 : 228 : else if (code == OMP_TARGET_EXIT_DATA)
15983 : : map_kind = GOMP_MAP_DETACH;
15984 : : else
15985 : 180 : map_kind = GOMP_MAP_ATTACH;
15986 : 229 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15987 : : }
15988 : 25037 : else if ((ctx->region_type & ORT_ACC) != 0
15989 : 25037 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15990 : : {
15991 : 0 : enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
15992 : 0 : ? GOMP_MAP_DETACH
15993 : : : GOMP_MAP_ATTACH);
15994 : 0 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15995 : : }
15996 : :
15997 : 25266 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15998 : 25266 : if ((ctx->region_type & ORT_TARGET) != 0
15999 : 15627 : && !(n->value & GOVD_SEEN)
16000 : 1415 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
16001 : 26438 : && (!is_global_var (decl)
16002 : 75 : || !lookup_attribute ("omp declare target link",
16003 : 75 : DECL_ATTRIBUTES (decl))))
16004 : : {
16005 : 1160 : remove = true;
16006 : : /* For struct element mapping, if struct is never referenced
16007 : : in target block and none of the mapping has always modifier,
16008 : : remove all the struct element mappings, which immediately
16009 : : follow the GOMP_MAP_STRUCT map clause. */
16010 : 1160 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
16011 : 1160 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
16012 : : {
16013 : 28 : HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
16014 : 76 : while (cnt--)
16015 : 48 : OMP_CLAUSE_CHAIN (c)
16016 : 48 : = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
16017 : : }
16018 : : }
16019 : 24106 : else if (DECL_SIZE (decl)
16020 : 24082 : && !poly_int_tree_p (DECL_SIZE (decl))
16021 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
16022 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
16023 : 24157 : && (OMP_CLAUSE_MAP_KIND (c)
16024 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
16025 : : {
16026 : : /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
16027 : : for these, TREE_CODE (DECL_SIZE (decl)) will always be
16028 : : INTEGER_CST. */
16029 : 51 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
16030 : :
16031 : 51 : tree decl2 = DECL_VALUE_EXPR (decl);
16032 : 51 : gcc_assert (INDIRECT_REF_P (decl2));
16033 : 51 : decl2 = TREE_OPERAND (decl2, 0);
16034 : 51 : gcc_assert (DECL_P (decl2));
16035 : 51 : tree mem = build_simple_mem_ref (decl2);
16036 : 51 : OMP_CLAUSE_DECL (c) = mem;
16037 : 51 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16038 : 51 : if (ctx->outer_context)
16039 : : {
16040 : 15 : omp_notice_variable (ctx->outer_context, decl2, true);
16041 : 30 : omp_notice_variable (ctx->outer_context,
16042 : 15 : OMP_CLAUSE_SIZE (c), true);
16043 : : }
16044 : 51 : if (((ctx->region_type & ORT_TARGET) != 0
16045 : 24 : || !ctx->target_firstprivatize_array_bases)
16046 : 34 : && ((n->value & GOVD_SEEN) == 0
16047 : 30 : || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
16048 : : {
16049 : 34 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16050 : : OMP_CLAUSE_MAP);
16051 : 34 : OMP_CLAUSE_DECL (nc) = decl;
16052 : 34 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
16053 : 34 : if (ctx->target_firstprivatize_array_bases)
16054 : 27 : OMP_CLAUSE_SET_MAP_KIND (nc,
16055 : : GOMP_MAP_FIRSTPRIVATE_POINTER);
16056 : : else
16057 : 7 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
16058 : 34 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
16059 : 34 : OMP_CLAUSE_CHAIN (c) = nc;
16060 : 34 : c = nc;
16061 : : }
16062 : : }
16063 : : else
16064 : : {
16065 : 24055 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16066 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16067 : 24055 : gcc_assert ((n->value & GOVD_SEEN) == 0
16068 : : || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16069 : : == 0));
16070 : : }
16071 : :
16072 : : /* If we have a target region, we can push all the attaches to the
16073 : : end of the list (we may have standalone "attach" operations
16074 : : synthesized for GOMP_MAP_STRUCT nodes that must be processed after
16075 : : the attachment point AND the pointed-to block have been mapped).
16076 : : If we have something else, e.g. "enter data", we need to keep
16077 : : "attach" nodes together with the previous node they attach to so
16078 : : that separate "exit data" operations work properly (see
16079 : : libgomp/target.c). */
16080 : 25266 : if ((ctx->region_type & ORT_TARGET) != 0
16081 : 25266 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16082 : 15511 : || (OMP_CLAUSE_MAP_KIND (c)
16083 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
16084 : : move_attach = true;
16085 : :
16086 : 53161 : end_adjust_omp_map_clause:
16087 : 53161 : exit_omp_iterator_loop_context (c);
16088 : 53161 : break;
16089 : :
16090 : 8224 : case OMP_CLAUSE_TO:
16091 : 8224 : case OMP_CLAUSE_FROM:
16092 : 8224 : case OMP_CLAUSE__CACHE_:
16093 : 8224 : decl = OMP_CLAUSE_DECL (c);
16094 : 8224 : if (!DECL_P (decl))
16095 : : break;
16096 : 6187 : if (DECL_SIZE (decl)
16097 : 6187 : && !poly_int_tree_p (DECL_SIZE (decl)))
16098 : : {
16099 : 4 : tree decl2 = DECL_VALUE_EXPR (decl);
16100 : 4 : gcc_assert (INDIRECT_REF_P (decl2));
16101 : 4 : decl2 = TREE_OPERAND (decl2, 0);
16102 : 4 : gcc_assert (DECL_P (decl2));
16103 : 4 : tree mem = build_simple_mem_ref (decl2);
16104 : 4 : OMP_CLAUSE_DECL (c) = mem;
16105 : 4 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
16106 : 4 : if (ctx->outer_context)
16107 : : {
16108 : 4 : omp_notice_variable (ctx->outer_context, decl2, true);
16109 : 4 : omp_notice_variable (ctx->outer_context,
16110 : 4 : OMP_CLAUSE_SIZE (c), true);
16111 : : }
16112 : : }
16113 : 6183 : else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
16114 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
16115 : : break;
16116 : :
16117 : 15289 : case OMP_CLAUSE_REDUCTION:
16118 : 15289 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
16119 : : {
16120 : 811 : decl = OMP_CLAUSE_DECL (c);
16121 : 811 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16122 : 811 : if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
16123 : : {
16124 : 69 : remove = true;
16125 : 69 : error_at (OMP_CLAUSE_LOCATION (c),
16126 : : "%qD specified in %<inscan%> %<reduction%> clause "
16127 : : "but not in %<scan%> directive clause", decl);
16128 : 69 : break;
16129 : : }
16130 : : has_inscan_reductions = true;
16131 : : }
16132 : : /* FALLTHRU */
16133 : 17820 : case OMP_CLAUSE_IN_REDUCTION:
16134 : 17820 : case OMP_CLAUSE_TASK_REDUCTION:
16135 : 17820 : decl = OMP_CLAUSE_DECL (c);
16136 : : /* OpenACC reductions need a present_or_copy data clause.
16137 : : Add one if necessary. Emit error when the reduction is private. */
16138 : 17820 : if (ctx->region_type == ORT_ACC_PARALLEL
16139 : 17288 : || ctx->region_type == ORT_ACC_SERIAL)
16140 : : {
16141 : 803 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16142 : 803 : if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
16143 : : {
16144 : 13 : remove = true;
16145 : 13 : error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
16146 : 13 : "reduction on %qE", DECL_NAME (decl));
16147 : : }
16148 : 790 : else if ((n->value & GOVD_MAP) == 0)
16149 : : {
16150 : 524 : tree next = OMP_CLAUSE_CHAIN (c);
16151 : 524 : tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
16152 : 524 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
16153 : 524 : OMP_CLAUSE_DECL (nc) = decl;
16154 : 524 : OMP_CLAUSE_CHAIN (c) = nc;
16155 : 524 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
16156 : 524 : (ctx->region_type
16157 : : & ORT_ACC) != 0);
16158 : 550 : while (1)
16159 : : {
16160 : 537 : OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
16161 : 537 : if (OMP_CLAUSE_CHAIN (nc) == NULL)
16162 : : break;
16163 : 13 : nc = OMP_CLAUSE_CHAIN (nc);
16164 : : }
16165 : 524 : OMP_CLAUSE_CHAIN (nc) = next;
16166 : 524 : n->value |= GOVD_MAP;
16167 : : }
16168 : : }
16169 : 17820 : if (DECL_P (decl)
16170 : 17820 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
16171 : 7959 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
16172 : : break;
16173 : :
16174 : 3541 : case OMP_CLAUSE_ALLOCATE:
16175 : 3541 : decl = OMP_CLAUSE_DECL (c);
16176 : 3541 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16177 : 3541 : if (n != NULL && !(n->value & GOVD_SEEN))
16178 : : {
16179 : 1176 : if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
16180 : : != 0
16181 : 1176 : && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
16182 : : remove = true;
16183 : : }
16184 : : if (!remove
16185 : 2365 : && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
16186 : 1153 : && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
16187 : 471 : && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
16188 : 347 : || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
16189 : 287 : || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
16190 : : {
16191 : 202 : tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
16192 : 202 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
16193 : 202 : if (n == NULL)
16194 : : {
16195 : 82 : enum omp_clause_default_kind default_kind
16196 : : = ctx->default_kind;
16197 : 82 : ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
16198 : 82 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16199 : : true);
16200 : 82 : ctx->default_kind = default_kind;
16201 : : }
16202 : : else
16203 : 120 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
16204 : : true);
16205 : : }
16206 : : break;
16207 : :
16208 : : case OMP_CLAUSE_COPYIN:
16209 : : case OMP_CLAUSE_COPYPRIVATE:
16210 : : case OMP_CLAUSE_IF:
16211 : : case OMP_CLAUSE_SELF:
16212 : : case OMP_CLAUSE_NUM_THREADS:
16213 : : case OMP_CLAUSE_NUM_TEAMS:
16214 : : case OMP_CLAUSE_THREAD_LIMIT:
16215 : : case OMP_CLAUSE_DIST_SCHEDULE:
16216 : : case OMP_CLAUSE_DEVICE:
16217 : : case OMP_CLAUSE_SCHEDULE:
16218 : : case OMP_CLAUSE_NOWAIT:
16219 : : case OMP_CLAUSE_ORDERED:
16220 : : case OMP_CLAUSE_DEFAULT:
16221 : : case OMP_CLAUSE_UNTIED:
16222 : : case OMP_CLAUSE_COLLAPSE:
16223 : : case OMP_CLAUSE_FINAL:
16224 : : case OMP_CLAUSE_MERGEABLE:
16225 : : case OMP_CLAUSE_PROC_BIND:
16226 : : case OMP_CLAUSE_SAFELEN:
16227 : : case OMP_CLAUSE_SIMDLEN:
16228 : : case OMP_CLAUSE_DEPEND:
16229 : : case OMP_CLAUSE_DOACROSS:
16230 : : case OMP_CLAUSE_PRIORITY:
16231 : : case OMP_CLAUSE_GRAINSIZE:
16232 : : case OMP_CLAUSE_NUM_TASKS:
16233 : : case OMP_CLAUSE_NOGROUP:
16234 : : case OMP_CLAUSE_THREADS:
16235 : : case OMP_CLAUSE_SIMD:
16236 : : case OMP_CLAUSE_FILTER:
16237 : : case OMP_CLAUSE_HINT:
16238 : : case OMP_CLAUSE_DEFAULTMAP:
16239 : : case OMP_CLAUSE_ORDER:
16240 : : case OMP_CLAUSE_BIND:
16241 : : case OMP_CLAUSE_DETACH:
16242 : : case OMP_CLAUSE_USE_DEVICE_PTR:
16243 : : case OMP_CLAUSE_USE_DEVICE_ADDR:
16244 : : case OMP_CLAUSE_ASYNC:
16245 : : case OMP_CLAUSE_WAIT:
16246 : : case OMP_CLAUSE_INDEPENDENT:
16247 : : case OMP_CLAUSE_NUM_GANGS:
16248 : : case OMP_CLAUSE_NUM_WORKERS:
16249 : : case OMP_CLAUSE_VECTOR_LENGTH:
16250 : : case OMP_CLAUSE_GANG:
16251 : : case OMP_CLAUSE_WORKER:
16252 : : case OMP_CLAUSE_VECTOR:
16253 : : case OMP_CLAUSE_AUTO:
16254 : : case OMP_CLAUSE_SEQ:
16255 : : case OMP_CLAUSE_TILE:
16256 : : case OMP_CLAUSE_IF_PRESENT:
16257 : : case OMP_CLAUSE_FINALIZE:
16258 : : case OMP_CLAUSE_INCLUSIVE:
16259 : : case OMP_CLAUSE_EXCLUSIVE:
16260 : : break;
16261 : :
16262 : 0 : case OMP_CLAUSE_NOHOST:
16263 : 0 : default:
16264 : 0 : gcc_unreachable ();
16265 : : }
16266 : :
16267 : 126167 : if (remove)
16268 : 9604 : *list_p = OMP_CLAUSE_CHAIN (c);
16269 : 233685 : else if (move_attach)
16270 : : {
16271 : : /* Remove attach node from here, separate out into its own list. */
16272 : 2313 : *attach_tail = c;
16273 : 2313 : *list_p = OMP_CLAUSE_CHAIN (c);
16274 : 2313 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16275 : 2313 : attach_tail = &OMP_CLAUSE_CHAIN (c);
16276 : : }
16277 : : else
16278 : 231372 : list_p = &OMP_CLAUSE_CHAIN (c);
16279 : : }
16280 : :
16281 : : /* Splice attach nodes at the end of the list. */
16282 : 128034 : if (attach_list)
16283 : : {
16284 : 1101 : *list_p = attach_list;
16285 : 1101 : list_p = attach_tail;
16286 : : }
16287 : :
16288 : : /* Add in any implicit data sharing. */
16289 : 128034 : struct gimplify_adjust_omp_clauses_data data;
16290 : 128034 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
16291 : : {
16292 : : /* OpenMP. Implicit clauses are added at the start of the clause list,
16293 : : but after any non-map clauses. */
16294 : : tree *implicit_add_list_p = orig_list_p;
16295 : 248989 : while (*implicit_add_list_p
16296 : 248989 : && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
16297 : 150902 : implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
16298 : 98087 : data.list_p = implicit_add_list_p;
16299 : : }
16300 : : else
16301 : : /* OpenACC. */
16302 : 29947 : data.list_p = list_p;
16303 : 128034 : data.pre_p = pre_p;
16304 : 128034 : splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
16305 : :
16306 : 128034 : if (has_inscan_reductions)
16307 : 2431 : for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
16308 : 1822 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16309 : 1822 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
16310 : : {
16311 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16312 : : "%<inscan%> %<reduction%> clause used together with "
16313 : : "%<linear%> clause for a variable other than loop "
16314 : : "iterator");
16315 : 5 : break;
16316 : : }
16317 : :
16318 : 128034 : gimplify_omp_ctxp = ctx->outer_context;
16319 : 128034 : delete_omp_context (ctx);
16320 : 128034 : }
16321 : :
16322 : : /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
16323 : : * -1 if run-time evaluation is needed. */
16324 : :
16325 : : int
16326 : 242727 : omp_has_novariants (void)
16327 : : {
16328 : 242727 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16329 : 242727 : if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
16330 : : {
16331 : 3278 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
16332 : 3278 : if (c != NULL_TREE)
16333 : : {
16334 : 287 : if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16335 : : return 1;
16336 : 171 : else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
16337 : : return 0;
16338 : : else
16339 : : return -1;
16340 : : }
16341 : : return 0;
16342 : : }
16343 : : return 0;
16344 : : }
16345 : :
16346 : : /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
16347 : : * -1 if run-time evaluation is needed. */
16348 : :
16349 : : static int
16350 : 1891 : omp_has_nocontext (void)
16351 : : {
16352 : 1891 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16353 : 1891 : if (ctx != NULL && ctx->code == OMP_DISPATCH)
16354 : : {
16355 : 1891 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
16356 : 1891 : if (c != NULL_TREE)
16357 : : {
16358 : 256 : if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16359 : : return 1;
16360 : 94 : else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
16361 : : return 0;
16362 : : else
16363 : : return -1;
16364 : : }
16365 : : return 0;
16366 : : }
16367 : : return 0;
16368 : : }
16369 : :
16370 : : /* Collect a list of traits for enclosing constructs in the current
16371 : : OpenMP context. The list is in the same format as the trait selector
16372 : : list of construct trait sets built by the front ends.
16373 : :
16374 : : Per the OpenMP specification, the construct trait set includes constructs
16375 : : up to an enclosing "target" construct. If there is no "target" construct,
16376 : : then additional things may be added to the construct trait set (simd for
16377 : : simd clones, additional constructs associated with "declare variant",
16378 : : the target trait for "declare target"); those are not handled here.
16379 : : In particular simd clones are not known during gimplification so
16380 : : matching/scoring of context selectors that might involve them needs
16381 : : to be deferred to the omp_device_lower pass. */
16382 : :
16383 : : tree
16384 : 1996 : omp_get_construct_context (void)
16385 : : {
16386 : 1996 : tree result = NULL_TREE;
16387 : 3953 : for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
16388 : : {
16389 : 2038 : if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
16390 : : == ORT_TARGET)
16391 : 81 : && ctx->code == OMP_TARGET)
16392 : : {
16393 : 81 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
16394 : : NULL_TREE, NULL_TREE, result);
16395 : : /* We're not interested in any outer constructs. */
16396 : 81 : break;
16397 : : }
16398 : 1957 : else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
16399 : 210 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
16400 : : NULL_TREE, NULL_TREE, result);
16401 : 1747 : else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
16402 : 71 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
16403 : : NULL_TREE, NULL_TREE, result);
16404 : 1676 : else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
16405 : 164 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
16406 : : NULL_TREE, NULL_TREE, result);
16407 : 1512 : else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
16408 : 1273 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
16409 : : NULL_TREE, NULL_TREE, result);
16410 : 239 : else if (ctx->region_type == ORT_SIMD
16411 : 32 : && ctx->code == OMP_SIMD
16412 : 271 : && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
16413 : : {
16414 : 32 : tree props = NULL_TREE;
16415 : 32 : tree *last = &props;
16416 : 72 : for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
16417 : 40 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
16418 : 32 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
16419 : 72 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
16420 : : {
16421 : 8 : *last = unshare_expr (c);
16422 : 8 : last = &(OMP_CLAUSE_CHAIN (c));
16423 : : }
16424 : 32 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
16425 : : NULL_TREE, props, result);
16426 : : }
16427 : 207 : else if (ctx->region_type == ORT_WORKSHARE
16428 : 5 : && ctx->code == OMP_LOOP
16429 : 0 : && ctx->outer_context
16430 : 0 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
16431 : 0 : && ctx->outer_context->outer_context
16432 : 0 : && ctx->outer_context->outer_context->code == OMP_LOOP
16433 : 0 : && ctx->outer_context->outer_context->distribute)
16434 : 1957 : ctx = ctx->outer_context->outer_context;
16435 : 1957 : ctx = ctx->outer_context;
16436 : : }
16437 : :
16438 : 1996 : return result;
16439 : : }
16440 : :
16441 : : /* Gimplify OACC_CACHE. */
16442 : :
16443 : : static void
16444 : 708 : gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
16445 : : {
16446 : 708 : tree expr = *expr_p;
16447 : :
16448 : 708 : gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
16449 : : OACC_CACHE);
16450 : 708 : gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
16451 : : OACC_CACHE);
16452 : :
16453 : : /* TODO: Do something sensible with this information. */
16454 : :
16455 : 708 : *expr_p = NULL_TREE;
16456 : 708 : }
16457 : :
16458 : : /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
16459 : : if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
16460 : : kind. The entry kind will replace the one in CLAUSE, while the exit
16461 : : kind will be used in a new omp_clause and returned to the caller. */
16462 : :
16463 : : static tree
16464 : 190 : gimplify_oacc_declare_1 (tree clause)
16465 : : {
16466 : 190 : HOST_WIDE_INT kind, new_op;
16467 : 190 : bool ret = false;
16468 : 190 : tree c = NULL;
16469 : :
16470 : 190 : kind = OMP_CLAUSE_MAP_KIND (clause);
16471 : :
16472 : 190 : switch (kind)
16473 : : {
16474 : : case GOMP_MAP_ALLOC:
16475 : : new_op = GOMP_MAP_RELEASE;
16476 : : ret = true;
16477 : : break;
16478 : :
16479 : 29 : case GOMP_MAP_FROM:
16480 : 29 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
16481 : 29 : new_op = GOMP_MAP_FROM;
16482 : 29 : ret = true;
16483 : 29 : break;
16484 : :
16485 : 40 : case GOMP_MAP_TOFROM:
16486 : 40 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
16487 : 40 : new_op = GOMP_MAP_FROM;
16488 : 40 : ret = true;
16489 : 40 : break;
16490 : :
16491 : : case GOMP_MAP_DEVICE_RESIDENT:
16492 : : case GOMP_MAP_FORCE_DEVICEPTR:
16493 : : case GOMP_MAP_FORCE_PRESENT:
16494 : : case GOMP_MAP_LINK:
16495 : : case GOMP_MAP_POINTER:
16496 : : case GOMP_MAP_TO:
16497 : : break;
16498 : :
16499 : 0 : default:
16500 : 0 : gcc_unreachable ();
16501 : 69 : break;
16502 : : }
16503 : :
16504 : 69 : if (ret)
16505 : : {
16506 : 116 : c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
16507 : 116 : OMP_CLAUSE_SET_MAP_KIND (c, new_op);
16508 : 116 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
16509 : : }
16510 : :
16511 : 190 : return c;
16512 : : }
16513 : :
16514 : : /* Gimplify OACC_DECLARE. */
16515 : :
16516 : : static void
16517 : 254 : gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
16518 : : {
16519 : 254 : tree expr = *expr_p;
16520 : 254 : gomp_target *stmt;
16521 : 254 : tree clauses, t, decl;
16522 : :
16523 : 254 : clauses = OACC_DECLARE_CLAUSES (expr);
16524 : :
16525 : 254 : gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
16526 : 254 : gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
16527 : :
16528 : 516 : for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16529 : : {
16530 : 262 : decl = OMP_CLAUSE_DECL (t);
16531 : :
16532 : 262 : if (TREE_CODE (decl) == MEM_REF)
16533 : 8 : decl = TREE_OPERAND (decl, 0);
16534 : :
16535 : 262 : if (VAR_P (decl) && !is_oacc_declared (decl))
16536 : : {
16537 : 262 : tree attr = get_identifier ("oacc declare target");
16538 : 262 : DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
16539 : 262 : DECL_ATTRIBUTES (decl));
16540 : : }
16541 : :
16542 : 262 : if (VAR_P (decl)
16543 : 262 : && !is_global_var (decl)
16544 : 452 : && DECL_CONTEXT (decl) == current_function_decl)
16545 : : {
16546 : 190 : tree c = gimplify_oacc_declare_1 (t);
16547 : 190 : if (c)
16548 : : {
16549 : 116 : if (oacc_declare_returns == NULL)
16550 : 40 : oacc_declare_returns = new hash_map<tree, tree>;
16551 : :
16552 : 116 : oacc_declare_returns->put (decl, c);
16553 : : }
16554 : : }
16555 : :
16556 : 262 : if (gimplify_omp_ctxp)
16557 : 72 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
16558 : : }
16559 : :
16560 : 254 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
16561 : : clauses);
16562 : :
16563 : 254 : gimplify_seq_add_stmt (pre_p, stmt);
16564 : :
16565 : 254 : *expr_p = NULL_TREE;
16566 : 254 : }
16567 : :
16568 : : /* Gimplify the contents of an OMP_PARALLEL statement. This involves
16569 : : gimplification of the body, as well as scanning the body for used
16570 : : variables. We need to do this scan now, because variable-sized
16571 : : decls will be decomposed during gimplification. */
16572 : :
16573 : : static void
16574 : 18217 : gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
16575 : : {
16576 : 18217 : tree expr = *expr_p;
16577 : 18217 : gimple *g;
16578 : 18217 : gimple_seq body = NULL;
16579 : :
16580 : 36434 : gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
16581 : 18217 : OMP_PARALLEL_COMBINED (expr)
16582 : : ? ORT_COMBINED_PARALLEL
16583 : : : ORT_PARALLEL, OMP_PARALLEL);
16584 : :
16585 : 18217 : push_gimplify_context ();
16586 : :
16587 : 18217 : g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
16588 : 18217 : if (gimple_code (g) == GIMPLE_BIND)
16589 : 18217 : pop_gimplify_context (g);
16590 : : else
16591 : 0 : pop_gimplify_context (NULL);
16592 : :
16593 : 18217 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
16594 : : OMP_PARALLEL);
16595 : :
16596 : 36434 : g = gimple_build_omp_parallel (body,
16597 : 18217 : OMP_PARALLEL_CLAUSES (expr),
16598 : : NULL_TREE, NULL_TREE);
16599 : 18217 : if (OMP_PARALLEL_COMBINED (expr))
16600 : 12554 : gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
16601 : 18217 : gimplify_seq_add_stmt (pre_p, g);
16602 : 18217 : *expr_p = NULL_TREE;
16603 : 18217 : }
16604 : :
16605 : : /* Gimplify the contents of an OMP_TASK statement. This involves
16606 : : gimplification of the body, as well as scanning the body for used
16607 : : variables. We need to do this scan now, because variable-sized
16608 : : decls will be decomposed during gimplification. */
16609 : :
16610 : : static void
16611 : 4014 : gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
16612 : : {
16613 : 4014 : tree expr = *expr_p;
16614 : 4014 : gimple *g;
16615 : 4014 : gimple_seq body = NULL;
16616 : 4014 : bool nowait = false;
16617 : 4014 : bool has_depend = false;
16618 : :
16619 : 4014 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16620 : : {
16621 : 236 : for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
16622 : 150 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
16623 : : {
16624 : 108 : has_depend = true;
16625 : 108 : if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
16626 : : {
16627 : 9 : error_at (OMP_CLAUSE_LOCATION (c),
16628 : : "%<mutexinoutset%> kind in %<depend%> clause on a "
16629 : : "%<taskwait%> construct");
16630 : 9 : break;
16631 : : }
16632 : : }
16633 : 42 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
16634 : 42 : nowait = true;
16635 : 95 : if (nowait && !has_depend)
16636 : : {
16637 : 5 : error_at (EXPR_LOCATION (expr),
16638 : : "%<taskwait%> construct with %<nowait%> clause but no "
16639 : : "%<depend%> clauses");
16640 : 5 : *expr_p = NULL_TREE;
16641 : 5 : return;
16642 : : }
16643 : : }
16644 : :
16645 : 12027 : gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
16646 : 4009 : omp_find_clause (OMP_TASK_CLAUSES (expr),
16647 : : OMP_CLAUSE_UNTIED)
16648 : : ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
16649 : :
16650 : 4009 : if (OMP_TASK_BODY (expr))
16651 : : {
16652 : 3919 : push_gimplify_context ();
16653 : :
16654 : 3919 : g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
16655 : 3919 : if (gimple_code (g) == GIMPLE_BIND)
16656 : 3919 : pop_gimplify_context (g);
16657 : : else
16658 : 0 : pop_gimplify_context (NULL);
16659 : : }
16660 : :
16661 : 4009 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
16662 : : OMP_TASK);
16663 : :
16664 : 8018 : g = gimple_build_omp_task (body,
16665 : 4009 : OMP_TASK_CLAUSES (expr),
16666 : : NULL_TREE, NULL_TREE,
16667 : : NULL_TREE, NULL_TREE, NULL_TREE);
16668 : 4009 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16669 : 90 : gimple_omp_task_set_taskwait_p (g, true);
16670 : 4009 : gimplify_seq_add_stmt (pre_p, g);
16671 : 4009 : *expr_p = NULL_TREE;
16672 : : }
16673 : :
16674 : : /* Helper function for gimplify_omp_for. If *TP is not a gimple constant,
16675 : : force it into a temporary initialized in PRE_P and add firstprivate clause
16676 : : to ORIG_FOR_STMT. */
16677 : :
16678 : : static void
16679 : 4391 : gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
16680 : : tree orig_for_stmt)
16681 : : {
16682 : 4391 : if (*tp == NULL || is_gimple_constant (*tp))
16683 : : return;
16684 : :
16685 : 779 : if (TREE_CODE (*tp) == SAVE_EXPR)
16686 : 135 : gimplify_save_expr (tp, pre_p, NULL);
16687 : : else
16688 : 644 : *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
16689 : : /* Reference to pointer conversion is considered useless,
16690 : : but is significant for firstprivate clause. Force it
16691 : : here. */
16692 : 779 : if (type
16693 : 687 : && TREE_CODE (type) == POINTER_TYPE
16694 : 869 : && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
16695 : : {
16696 : 12 : tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
16697 : 12 : tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
16698 : 12 : gimplify_and_add (m, pre_p);
16699 : 12 : *tp = v;
16700 : : }
16701 : :
16702 : 779 : tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16703 : 779 : OMP_CLAUSE_DECL (c) = *tp;
16704 : 779 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
16705 : 779 : OMP_FOR_CLAUSES (orig_for_stmt) = c;
16706 : : }
16707 : :
16708 : : /* Helper function of gimplify_omp_for, find OMP_ORDERED with
16709 : : null OMP_ORDERED_BODY inside of OMP_FOR's body. */
16710 : :
16711 : : static tree
16712 : 16453 : find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
16713 : : {
16714 : 16453 : switch (TREE_CODE (*tp))
16715 : : {
16716 : 896 : case OMP_ORDERED:
16717 : 896 : if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
16718 : : return *tp;
16719 : : break;
16720 : 23 : case OMP_SIMD:
16721 : 23 : case OMP_PARALLEL:
16722 : 23 : case OMP_TARGET:
16723 : 23 : *walk_subtrees = 0;
16724 : 23 : break;
16725 : : default:
16726 : : break;
16727 : : }
16728 : : return NULL_TREE;
16729 : : }
16730 : :
16731 : : /* Gimplify standalone loop transforming directive which has the
16732 : : transformations applied already. So, all that is needed is gimplify
16733 : : the remaining loops as normal loops. */
16734 : :
16735 : : static enum gimplify_status
16736 : 2264 : gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
16737 : : {
16738 : 2264 : tree for_stmt = *expr_p;
16739 : :
16740 : 2264 : if (OMP_FOR_PRE_BODY (for_stmt))
16741 : 759 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
16742 : :
16743 : 2264 : gimple_seq pre_body = NULL, post_body = NULL;
16744 : 5203 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16745 : : {
16746 : 2939 : if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
16747 : 1707 : continue;
16748 : 1283 : tree iters = NULL_TREE;
16749 : 1283 : if (i == 0
16750 : 826 : && TREE_CODE (for_stmt) == OMP_UNROLL
16751 : 1771 : && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
16752 : : {
16753 : 311 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
16754 : 269 : iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
16755 : : else
16756 : 42 : iters = build_int_cst (integer_type_node, 8);
16757 : : }
16758 : 1283 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16759 : 1283 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16760 : 1283 : tree decl = TREE_OPERAND (t, 0);
16761 : 1283 : gcc_assert (DECL_P (decl));
16762 : 1283 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16763 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
16764 : 1283 : if (DECL_ARTIFICIAL (decl)
16765 : 876 : && TREE_PRIVATE (t)
16766 : 867 : && gimplify_omp_ctxp
16767 : 1613 : && gimplify_omp_ctxp->region_type != ORT_NONE)
16768 : : {
16769 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16770 : 504 : do
16771 : : {
16772 : 504 : splay_tree_node n
16773 : 504 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16774 : 504 : if (n != NULL)
16775 : : break;
16776 : 346 : else if (ctx->region_type != ORT_WORKSHARE
16777 : : && ctx->region_type != ORT_TASKGROUP
16778 : 62 : && ctx->region_type != ORT_SIMD
16779 : 38 : && ctx->region_type != ORT_ACC
16780 : 38 : && !(ctx->region_type & ORT_TARGET_DATA))
16781 : : {
16782 : 38 : omp_add_variable (ctx, decl, GOVD_PRIVATE);
16783 : 38 : break;
16784 : : }
16785 : 308 : ctx = ctx->outer_context;
16786 : : }
16787 : 308 : while (ctx);
16788 : : }
16789 : 1283 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16790 : : {
16791 : 23 : gcc_assert (seen_error ());
16792 : 23 : continue;
16793 : : }
16794 : 1260 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16795 : : fb_rvalue);
16796 : 1260 : gimplify_and_add (t, &pre_body);
16797 : 1260 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16798 : 1260 : gcc_assert (TREE_OPERAND (t, 0) == decl);
16799 : 1260 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16800 : : {
16801 : 28 : gcc_assert (seen_error ());
16802 : 28 : continue;
16803 : : }
16804 : 1232 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16805 : : fb_rvalue);
16806 : 1232 : tree l1 = create_artificial_label (UNKNOWN_LOCATION);
16807 : 1232 : tree l2 = create_artificial_label (UNKNOWN_LOCATION);
16808 : 1232 : tree l3 = create_artificial_label (UNKNOWN_LOCATION);
16809 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
16810 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
16811 : 1232 : gimple_seq this_post_body = NULL;
16812 : 1232 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16813 : 1232 : if (TREE_CODE (t) == MODIFY_EXPR)
16814 : : {
16815 : 975 : t = TREE_OPERAND (t, 1);
16816 : 975 : if (TREE_CODE (t) == PLUS_EXPR
16817 : 975 : && TREE_OPERAND (t, 1) == decl)
16818 : : {
16819 : 0 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
16820 : 0 : TREE_OPERAND (t, 0) = decl;
16821 : : }
16822 : 975 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16823 : : fb_rvalue);
16824 : : }
16825 : 1232 : gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
16826 : : &this_post_body);
16827 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
16828 : 1232 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16829 : 1232 : gcond *cond = NULL;
16830 : 1232 : tree d = decl;
16831 : 1232 : gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
16832 : 1232 : if (iters && tree_fits_uhwi_p (iters))
16833 : : {
16834 : 299 : unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
16835 : 299 : if ((unsigned HOST_WIDE_INT) (int) niters == niters
16836 : 299 : && (int) niters > 0)
16837 : : {
16838 : 299 : t = build2 (TREE_CODE (t), boolean_type_node, d,
16839 : 299 : TREE_OPERAND (t, 1));
16840 : 299 : t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
16841 : : build_int_cst (integer_type_node,
16842 : : annot_expr_unroll_kind),
16843 : 299 : build_int_cst (integer_type_node, niters));
16844 : 299 : gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
16845 : : fb_rvalue);
16846 : 299 : cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
16847 : : l1, l3);
16848 : : }
16849 : : }
16850 : 299 : if (cond == NULL)
16851 : 933 : cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
16852 : : l1, l3);
16853 : 1232 : gimplify_seq_add_stmt (&this_post_body, cond);
16854 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
16855 : 1232 : gimplify_seq_add_seq (&this_post_body, post_body);
16856 : 1232 : post_body = this_post_body;
16857 : : }
16858 : 2264 : gimplify_seq_add_seq (pre_p, pre_body);
16859 : 2264 : gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
16860 : 2264 : gimplify_seq_add_seq (pre_p, post_body);
16861 : :
16862 : 2264 : *expr_p = NULL_TREE;
16863 : 2264 : return GS_ALL_DONE;
16864 : : }
16865 : :
16866 : : /* Gimplify the gross structure of an OMP_FOR statement. */
16867 : :
16868 : : static enum gimplify_status
16869 : 58112 : gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
16870 : : {
16871 : 58112 : tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
16872 : 58112 : enum gimplify_status ret = GS_ALL_DONE;
16873 : 58112 : enum gimplify_status tret;
16874 : 58112 : gomp_for *gfor;
16875 : 58112 : gimple_seq for_body, for_pre_body;
16876 : 58112 : int i;
16877 : 58112 : bitmap has_decl_expr = NULL;
16878 : 58112 : enum omp_region_type ort = ORT_WORKSHARE;
16879 : 58112 : bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
16880 : :
16881 : 58112 : orig_for_stmt = for_stmt = *expr_p;
16882 : :
16883 : 58112 : bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
16884 : 58112 : != NULL_TREE);
16885 : 58183 : while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16886 : : {
16887 : 17761 : tree *data[4] = { NULL, NULL, NULL, NULL };
16888 : 17761 : gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
16889 : 17761 : inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
16890 : : find_combined_omp_for, data, NULL);
16891 : 17761 : if (inner_for_stmt == NULL_TREE)
16892 : : {
16893 : 36 : gcc_assert (seen_error ());
16894 : 36 : *expr_p = NULL_TREE;
16895 : 3743 : return GS_ERROR;
16896 : : }
16897 : 17725 : gcc_assert (inner_for_stmt == *data[3]);
16898 : 17725 : omp_maybe_apply_loop_xforms (data[3],
16899 : 17725 : data[2]
16900 : 4270 : ? OMP_FOR_CLAUSES (*data[2])
16901 : 13455 : : TREE_CODE (for_stmt) == OMP_FOR
16902 : 13455 : ? OMP_FOR_CLAUSES (for_stmt)
16903 : : : NULL_TREE);
16904 : 17725 : if (inner_for_stmt != *data[3])
16905 : 71 : continue;
16906 : 17654 : if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
16907 : : {
16908 : 2 : append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
16909 : : &OMP_FOR_PRE_BODY (for_stmt));
16910 : 2 : OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
16911 : : }
16912 : 17654 : if (OMP_FOR_PRE_BODY (inner_for_stmt))
16913 : : {
16914 : 7224 : append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
16915 : : &OMP_FOR_PRE_BODY (for_stmt));
16916 : 7224 : OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
16917 : : }
16918 : :
16919 : 17654 : if (data[0])
16920 : : {
16921 : : /* We have some statements or variable declarations in between
16922 : : the composite construct directives. Move them around the
16923 : : inner_for_stmt. */
16924 : 3707 : data[0] = expr_p;
16925 : 14828 : for (i = 0; i < 3; i++)
16926 : 11121 : if (data[i])
16927 : : {
16928 : 7037 : tree t = *data[i];
16929 : 7037 : if (i < 2 && data[i + 1] == &OMP_BODY (t))
16930 : 1002 : data[i + 1] = data[i];
16931 : 7037 : *data[i] = OMP_BODY (t);
16932 : 7037 : tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
16933 : : NULL_TREE, make_node (BLOCK));
16934 : 7037 : OMP_BODY (t) = body;
16935 : 7037 : append_to_statement_list_force (inner_for_stmt,
16936 : : &BIND_EXPR_BODY (body));
16937 : 7037 : *data[3] = t;
16938 : 7037 : data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
16939 : 7037 : gcc_assert (*data[3] == inner_for_stmt);
16940 : : }
16941 : : return GS_OK;
16942 : : }
16943 : :
16944 : 36750 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16945 : 22803 : if (!loop_p
16946 : 21948 : && OMP_FOR_ORIG_DECLS (inner_for_stmt)
16947 : 11124 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16948 : : i)) == TREE_LIST
16949 : 22871 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16950 : : i)))
16951 : : {
16952 : 40 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16953 : : /* Class iterators aren't allowed on OMP_SIMD, so the only
16954 : : case we need to solve is distribute parallel for. They are
16955 : : allowed on the loop construct, but that is already handled
16956 : : in gimplify_omp_loop. */
16957 : 40 : gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
16958 : : && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
16959 : : && data[1]);
16960 : 40 : tree orig_decl = TREE_PURPOSE (orig);
16961 : 40 : tree last = TREE_VALUE (orig);
16962 : 40 : tree *pc;
16963 : 40 : for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
16964 : 74 : *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
16965 : 42 : if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16966 : 35 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
16967 : 43 : && OMP_CLAUSE_DECL (*pc) == orig_decl)
16968 : : break;
16969 : 40 : if (*pc == NULL_TREE)
16970 : : {
16971 : 32 : tree *spc;
16972 : 32 : for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
16973 : 98 : *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
16974 : 67 : if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
16975 : 67 : && OMP_CLAUSE_DECL (*spc) == orig_decl)
16976 : : break;
16977 : 32 : if (*spc)
16978 : : {
16979 : 1 : tree c = *spc;
16980 : 1 : *spc = OMP_CLAUSE_CHAIN (c);
16981 : 1 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16982 : 1 : *pc = c;
16983 : : }
16984 : : }
16985 : 40 : if (*pc == NULL_TREE)
16986 : : ;
16987 : 9 : else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
16988 : : {
16989 : : /* private clause will appear only on inner_for_stmt.
16990 : : Change it into firstprivate, and add private clause
16991 : : on for_stmt. */
16992 : 8 : tree c = copy_node (*pc);
16993 : 8 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16994 : 8 : OMP_FOR_CLAUSES (for_stmt) = c;
16995 : 8 : OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
16996 : 8 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16997 : : }
16998 : : else
16999 : : {
17000 : : /* lastprivate clause will appear on both inner_for_stmt
17001 : : and for_stmt. Add firstprivate clause to
17002 : : inner_for_stmt. */
17003 : 1 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
17004 : : OMP_CLAUSE_FIRSTPRIVATE);
17005 : 1 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
17006 : 1 : OMP_CLAUSE_CHAIN (c) = *pc;
17007 : 1 : *pc = c;
17008 : 1 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
17009 : : }
17010 : 40 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17011 : : OMP_CLAUSE_FIRSTPRIVATE);
17012 : 40 : OMP_CLAUSE_DECL (c) = last;
17013 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17014 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17015 : 40 : c = build_omp_clause (UNKNOWN_LOCATION,
17016 : 40 : *pc ? OMP_CLAUSE_SHARED
17017 : : : OMP_CLAUSE_FIRSTPRIVATE);
17018 : 40 : OMP_CLAUSE_DECL (c) = orig_decl;
17019 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17020 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17021 : : }
17022 : : /* Similarly, take care of C++ range for temporaries, those should
17023 : : be firstprivate on OMP_PARALLEL if any. */
17024 : 13947 : if (data[1])
17025 : 17540 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
17026 : 11120 : if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
17027 : 5753 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17028 : : i)) == TREE_LIST
17029 : 11196 : && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
17030 : : i)))
17031 : : {
17032 : 49 : tree orig
17033 : 49 : = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
17034 : 49 : tree v = TREE_CHAIN (orig);
17035 : 49 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17036 : : OMP_CLAUSE_FIRSTPRIVATE);
17037 : : /* First add firstprivate clause for the __for_end artificial
17038 : : decl. */
17039 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
17040 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17041 : : == REFERENCE_TYPE)
17042 : 0 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17043 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17044 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17045 : 49 : if (TREE_VEC_ELT (v, 0))
17046 : : {
17047 : : /* And now the same for __for_range artificial decl if it
17048 : : exists. */
17049 : 49 : c = build_omp_clause (UNKNOWN_LOCATION,
17050 : : OMP_CLAUSE_FIRSTPRIVATE);
17051 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
17052 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
17053 : : == REFERENCE_TYPE)
17054 : 49 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
17055 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
17056 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
17057 : : }
17058 : : }
17059 : 13947 : break;
17060 : : }
17061 : 54369 : if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
17062 : : {
17063 : 40422 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
17064 : 40422 : if (*expr_p != for_stmt)
17065 : : return GS_OK;
17066 : : }
17067 : :
17068 : 53487 : switch (TREE_CODE (for_stmt))
17069 : : {
17070 : 18178 : case OMP_FOR:
17071 : 30391 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17072 : : {
17073 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17074 : : OMP_CLAUSE_SCHEDULE))
17075 : 47 : error_at (EXPR_LOCATION (for_stmt),
17076 : : "%qs clause may not appear on non-rectangular %qs",
17077 : 27 : "schedule", lang_GNU_Fortran () ? "do" : "for");
17078 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
17079 : 34 : error_at (EXPR_LOCATION (for_stmt),
17080 : : "%qs clause may not appear on non-rectangular %qs",
17081 : 18 : "ordered", lang_GNU_Fortran () ? "do" : "for");
17082 : : }
17083 : : break;
17084 : 8244 : case OMP_DISTRIBUTE:
17085 : 9155 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
17086 : 8244 : && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17087 : : OMP_CLAUSE_DIST_SCHEDULE))
17088 : 29 : error_at (EXPR_LOCATION (for_stmt),
17089 : : "%qs clause may not appear on non-rectangular %qs",
17090 : : "dist_schedule", "distribute");
17091 : : break;
17092 : : case OACC_LOOP:
17093 : : ort = ORT_ACC;
17094 : : break;
17095 : 1582 : case OMP_TASKLOOP:
17096 : 2515 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
17097 : : {
17098 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17099 : : OMP_CLAUSE_GRAINSIZE))
17100 : 11 : error_at (EXPR_LOCATION (for_stmt),
17101 : : "%qs clause may not appear on non-rectangular %qs",
17102 : : "grainsize", "taskloop");
17103 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17104 : : OMP_CLAUSE_NUM_TASKS))
17105 : 6 : error_at (EXPR_LOCATION (for_stmt),
17106 : : "%qs clause may not appear on non-rectangular %qs",
17107 : : "num_tasks", "taskloop");
17108 : : }
17109 : 1582 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
17110 : : ort = ORT_UNTIED_TASKLOOP;
17111 : : else
17112 : : ort = ORT_TASKLOOP;
17113 : : break;
17114 : 10642 : case OMP_SIMD:
17115 : 10642 : ort = ORT_SIMD;
17116 : 10642 : break;
17117 : 2264 : case OMP_TILE:
17118 : 2264 : case OMP_UNROLL:
17119 : 2264 : gcc_assert (inner_for_stmt == NULL_TREE);
17120 : 2264 : return gimplify_omp_loop_xform (expr_p, pre_p);
17121 : 0 : default:
17122 : 0 : gcc_unreachable ();
17123 : : }
17124 : :
17125 : : /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
17126 : : clause for the IV. */
17127 : 10689 : if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17128 : : {
17129 : 7829 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
17130 : 7829 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17131 : 7829 : decl = TREE_OPERAND (t, 0);
17132 : 18659 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
17133 : 11944 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17134 : 11944 : && OMP_CLAUSE_DECL (c) == decl)
17135 : : {
17136 : 1114 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17137 : 1114 : break;
17138 : : }
17139 : : }
17140 : :
17141 : 51223 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
17142 : 51544 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
17143 : 1903 : loop_p && TREE_CODE (for_stmt) != OMP_SIMD
17144 : : ? OMP_LOOP : TREE_CODE (for_stmt));
17145 : :
17146 : 51223 : if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
17147 : 8244 : gimplify_omp_ctxp->distribute = true;
17148 : :
17149 : : /* Handle OMP_FOR_INIT. */
17150 : 51223 : for_pre_body = NULL;
17151 : 51223 : if ((ort == ORT_SIMD
17152 : 40581 : || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
17153 : 61950 : && OMP_FOR_PRE_BODY (for_stmt))
17154 : : {
17155 : 6776 : has_decl_expr = BITMAP_ALLOC (NULL);
17156 : 6776 : if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
17157 : 6776 : && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
17158 : : {
17159 : 18 : t = OMP_FOR_PRE_BODY (for_stmt);
17160 : 18 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17161 : : }
17162 : 6758 : else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
17163 : : {
17164 : 6756 : tree_stmt_iterator si;
17165 : 11206 : for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
17166 : 4450 : tsi_next (&si))
17167 : : {
17168 : 4450 : t = tsi_stmt (si);
17169 : 4450 : if (TREE_CODE (t) == DECL_EXPR
17170 : 4450 : && VAR_P (DECL_EXPR_DECL (t)))
17171 : 4377 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
17172 : : }
17173 : : }
17174 : : }
17175 : 51223 : if (OMP_FOR_PRE_BODY (for_stmt))
17176 : : {
17177 : 21008 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
17178 : 20287 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17179 : : else
17180 : : {
17181 : 721 : struct gimplify_omp_ctx ctx;
17182 : 721 : memset (&ctx, 0, sizeof (ctx));
17183 : 721 : ctx.region_type = ORT_NONE;
17184 : 721 : gimplify_omp_ctxp = &ctx;
17185 : 721 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
17186 : 721 : gimplify_omp_ctxp = NULL;
17187 : : }
17188 : : }
17189 : 51223 : OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
17190 : :
17191 : 51223 : if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
17192 : 13947 : for_stmt = inner_for_stmt;
17193 : :
17194 : : /* For taskloop, need to gimplify the start, end and step before the
17195 : : taskloop, outside of the taskloop omp context. */
17196 : 51223 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17197 : : {
17198 : 3450 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17199 : : {
17200 : 1868 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17201 : 1868 : gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
17202 : 1868 : ? pre_p : &for_pre_body);
17203 : 1868 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
17204 : 1868 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17205 : : {
17206 : 34 : tree v = TREE_OPERAND (t, 1);
17207 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17208 : : for_pre_p, orig_for_stmt);
17209 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17210 : : for_pre_p, orig_for_stmt);
17211 : : }
17212 : : else
17213 : 1834 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17214 : : orig_for_stmt);
17215 : :
17216 : : /* Handle OMP_FOR_COND. */
17217 : 1868 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17218 : 1868 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17219 : : {
17220 : 31 : tree v = TREE_OPERAND (t, 1);
17221 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
17222 : : for_pre_p, orig_for_stmt);
17223 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
17224 : : for_pre_p, orig_for_stmt);
17225 : : }
17226 : : else
17227 : 1837 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
17228 : : orig_for_stmt);
17229 : :
17230 : : /* Handle OMP_FOR_INCR. */
17231 : 1868 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17232 : 1868 : if (TREE_CODE (t) == MODIFY_EXPR)
17233 : : {
17234 : 590 : decl = TREE_OPERAND (t, 0);
17235 : 590 : t = TREE_OPERAND (t, 1);
17236 : 590 : tree *tp = &TREE_OPERAND (t, 1);
17237 : 590 : if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
17238 : 22 : tp = &TREE_OPERAND (t, 0);
17239 : :
17240 : 590 : gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
17241 : : orig_for_stmt);
17242 : : }
17243 : : }
17244 : :
17245 : 1582 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
17246 : : OMP_TASKLOOP);
17247 : : }
17248 : :
17249 : 51223 : if (orig_for_stmt != for_stmt)
17250 : 13947 : gimplify_omp_ctxp->combined_loop = true;
17251 : :
17252 : 51223 : for_body = NULL;
17253 : 51223 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17254 : : == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
17255 : 51223 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17256 : : == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
17257 : :
17258 : 51223 : tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
17259 : 51223 : bool is_doacross = false;
17260 : 51223 : if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
17261 : : find_standalone_omp_ordered, NULL))
17262 : : {
17263 : 526 : OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
17264 : 526 : is_doacross = true;
17265 : 526 : int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
17266 : 526 : gimplify_omp_ctxp->loop_iter_var.create (len * 2);
17267 : 2190 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
17268 : 1664 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
17269 : : {
17270 : 10 : error_at (OMP_CLAUSE_LOCATION (*pc),
17271 : : "%<linear%> clause may not be specified together "
17272 : : "with %<ordered%> clause if stand-alone %<ordered%> "
17273 : : "construct is nested in it");
17274 : 10 : *pc = OMP_CLAUSE_CHAIN (*pc);
17275 : : }
17276 : : else
17277 : 1654 : pc = &OMP_CLAUSE_CHAIN (*pc);
17278 : : }
17279 : 51223 : int collapse = 1, tile = 0;
17280 : 51223 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
17281 : 51223 : if (c)
17282 : 13659 : collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
17283 : 51223 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
17284 : 51223 : if (c)
17285 : 384 : tile = list_length (OMP_CLAUSE_TILE_LIST (c));
17286 : 51223 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
17287 : 51223 : hash_set<tree> *allocate_uids = NULL;
17288 : 51223 : if (c)
17289 : : {
17290 : 275 : allocate_uids = new hash_set<tree>;
17291 : 1682 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17292 : 1132 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
17293 : 478 : allocate_uids->add (OMP_CLAUSE_DECL (c));
17294 : : }
17295 : 124123 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17296 : : {
17297 : 72900 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17298 : 72900 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17299 : 72900 : decl = TREE_OPERAND (t, 0);
17300 : 72900 : gcc_assert (DECL_P (decl));
17301 : 72900 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
17302 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
17303 : 72900 : if (is_doacross)
17304 : : {
17305 : 1649 : if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
17306 : : {
17307 : 956 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17308 : 956 : if (TREE_CODE (orig_decl) == TREE_LIST)
17309 : : {
17310 : 15 : orig_decl = TREE_PURPOSE (orig_decl);
17311 : 15 : if (!orig_decl)
17312 : 0 : orig_decl = decl;
17313 : : }
17314 : 956 : gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
17315 : : }
17316 : : else
17317 : 693 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17318 : 1649 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
17319 : : }
17320 : :
17321 : 72900 : if (for_stmt == orig_for_stmt)
17322 : : {
17323 : 50097 : tree orig_decl = decl;
17324 : 50097 : if (OMP_FOR_ORIG_DECLS (for_stmt))
17325 : : {
17326 : 23928 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17327 : 23928 : if (TREE_CODE (orig_decl) == TREE_LIST)
17328 : : {
17329 : : orig_decl = TREE_PURPOSE (orig_decl);
17330 : : if (!orig_decl)
17331 : : orig_decl = decl;
17332 : : }
17333 : : }
17334 : 50097 : if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
17335 : 36 : error_at (EXPR_LOCATION (for_stmt),
17336 : : "threadprivate iteration variable %qD", orig_decl);
17337 : : }
17338 : :
17339 : : /* Make sure the iteration variable is private. */
17340 : 72900 : tree c = NULL_TREE;
17341 : 72900 : tree c2 = NULL_TREE;
17342 : 72900 : if (orig_for_stmt != for_stmt)
17343 : : {
17344 : : /* Preserve this information until we gimplify the inner simd. */
17345 : 22803 : if (has_decl_expr
17346 : 22803 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17347 : 3396 : TREE_PRIVATE (t) = 1;
17348 : : }
17349 : 50097 : else if (ort == ORT_SIMD)
17350 : : {
17351 : 15700 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17352 : : (splay_tree_key) decl);
17353 : 15700 : omp_is_private (gimplify_omp_ctxp, decl,
17354 : 15700 : 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
17355 : : != 1));
17356 : 15700 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
17357 : : {
17358 : 2512 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17359 : 2512 : if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
17360 : 0 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17361 : : OMP_CLAUSE_LASTPRIVATE);
17362 : 0 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17363 : : OMP_CLAUSE_LASTPRIVATE))
17364 : 0 : if (OMP_CLAUSE_DECL (c3) == decl)
17365 : : {
17366 : 0 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17367 : : "conditional %<lastprivate%> on loop "
17368 : : "iterator %qD ignored", decl);
17369 : 0 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17370 : 0 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17371 : : }
17372 : : }
17373 : 13188 : else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
17374 : : {
17375 : 5631 : c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17376 : 5631 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
17377 : 5631 : unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
17378 : 5631 : if ((has_decl_expr
17379 : 1306 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
17380 : 6315 : || TREE_PRIVATE (t))
17381 : : {
17382 : 2277 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17383 : 2277 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17384 : : }
17385 : 5631 : struct gimplify_omp_ctx *outer
17386 : 5631 : = gimplify_omp_ctxp->outer_context;
17387 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17388 : : {
17389 : 2433 : if (outer->region_type == ORT_WORKSHARE
17390 : 2193 : && outer->combined_loop)
17391 : : {
17392 : 2142 : n = splay_tree_lookup (outer->variables,
17393 : : (splay_tree_key)decl);
17394 : 2142 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17395 : : {
17396 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17397 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17398 : : }
17399 : : else
17400 : : {
17401 : 2142 : struct gimplify_omp_ctx *octx = outer->outer_context;
17402 : 2142 : if (octx
17403 : 1714 : && octx->region_type == ORT_COMBINED_PARALLEL
17404 : 1476 : && octx->outer_context
17405 : 1209 : && (octx->outer_context->region_type
17406 : : == ORT_WORKSHARE)
17407 : 1079 : && octx->outer_context->combined_loop)
17408 : : {
17409 : 1079 : octx = octx->outer_context;
17410 : 1079 : n = splay_tree_lookup (octx->variables,
17411 : : (splay_tree_key)decl);
17412 : 1079 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
17413 : : {
17414 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
17415 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
17416 : : }
17417 : : }
17418 : : }
17419 : : }
17420 : : }
17421 : :
17422 : 5631 : OMP_CLAUSE_DECL (c) = decl;
17423 : 5631 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17424 : 5631 : OMP_FOR_CLAUSES (for_stmt) = c;
17425 : 5631 : omp_add_variable (gimplify_omp_ctxp, decl, flags);
17426 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
17427 : 2433 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17428 : : true);
17429 : : }
17430 : : else
17431 : : {
17432 : 7557 : bool lastprivate
17433 : : = (!has_decl_expr
17434 : 7557 : || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
17435 : 7557 : if (TREE_PRIVATE (t))
17436 : 1771 : lastprivate = false;
17437 : 7557 : if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
17438 : : {
17439 : 507 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17440 : 507 : if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
17441 : : lastprivate = false;
17442 : : }
17443 : :
17444 : 7557 : struct gimplify_omp_ctx *outer
17445 : 7557 : = gimplify_omp_ctxp->outer_context;
17446 : 7557 : if (outer && lastprivate)
17447 : 4893 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
17448 : : true);
17449 : :
17450 : 9547 : c = build_omp_clause (input_location,
17451 : : lastprivate ? OMP_CLAUSE_LASTPRIVATE
17452 : : : OMP_CLAUSE_PRIVATE);
17453 : 7557 : OMP_CLAUSE_DECL (c) = decl;
17454 : 7557 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
17455 : 7557 : OMP_FOR_CLAUSES (for_stmt) = c;
17456 : 7557 : omp_add_variable (gimplify_omp_ctxp, decl,
17457 : : (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
17458 : : | GOVD_EXPLICIT | GOVD_SEEN);
17459 : 7557 : c = NULL_TREE;
17460 : : }
17461 : : }
17462 : 34397 : else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
17463 : : {
17464 : 8715 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
17465 : 8715 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
17466 : : (splay_tree_key) decl);
17467 : 8715 : if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
17468 : 80 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
17469 : : OMP_CLAUSE_LASTPRIVATE);
17470 : 80 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
17471 : : OMP_CLAUSE_LASTPRIVATE))
17472 : 40 : if (OMP_CLAUSE_DECL (c3) == decl)
17473 : : {
17474 : 40 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
17475 : : "conditional %<lastprivate%> on loop "
17476 : : "iterator %qD ignored", decl);
17477 : 40 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
17478 : 40 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
17479 : : }
17480 : : }
17481 : : else
17482 : 25682 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
17483 : :
17484 : : /* If DECL is not a gimple register, create a temporary variable to act
17485 : : as an iteration counter. This is valid, since DECL cannot be
17486 : : modified in the body of the loop. Similarly for any iteration vars
17487 : : in simd with collapse > 1 where the iterator vars must be
17488 : : lastprivate. And similarly for vars mentioned in allocate clauses. */
17489 : 72900 : if (orig_for_stmt != for_stmt)
17490 : 22803 : var = decl;
17491 : 50097 : else if (!is_gimple_reg (decl)
17492 : 46825 : || (ort == ORT_SIMD
17493 : 14101 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
17494 : 89882 : || (allocate_uids && allocate_uids->contains (decl)))
17495 : : {
17496 : 10344 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17497 : : /* Make sure omp_add_variable is not called on it prematurely.
17498 : : We call it ourselves a few lines later. */
17499 : 10344 : gimplify_omp_ctxp = NULL;
17500 : 10344 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17501 : 10344 : gimplify_omp_ctxp = ctx;
17502 : 10344 : TREE_OPERAND (t, 0) = var;
17503 : :
17504 : 10344 : gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
17505 : :
17506 : 10344 : if (ort == ORT_SIMD
17507 : 10344 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17508 : : {
17509 : 768 : c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17510 : 768 : OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
17511 : 768 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
17512 : 768 : OMP_CLAUSE_DECL (c2) = var;
17513 : 768 : OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
17514 : 768 : OMP_FOR_CLAUSES (for_stmt) = c2;
17515 : 768 : omp_add_variable (gimplify_omp_ctxp, var,
17516 : : GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
17517 : 768 : if (c == NULL_TREE)
17518 : : {
17519 : 518 : c = c2;
17520 : 518 : c2 = NULL_TREE;
17521 : : }
17522 : : }
17523 : : else
17524 : 9576 : omp_add_variable (gimplify_omp_ctxp, var,
17525 : : GOVD_PRIVATE | GOVD_SEEN);
17526 : : }
17527 : : else
17528 : : var = decl;
17529 : :
17530 : 72900 : gimplify_omp_ctxp->in_for_exprs = true;
17531 : 72900 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17532 : : {
17533 : 719 : tree lb = TREE_OPERAND (t, 1);
17534 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
17535 : : is_gimple_val, fb_rvalue, false);
17536 : 719 : ret = MIN (ret, tret);
17537 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
17538 : : is_gimple_val, fb_rvalue, false);
17539 : : }
17540 : : else
17541 : 72181 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17542 : : is_gimple_val, fb_rvalue, false);
17543 : 72900 : gimplify_omp_ctxp->in_for_exprs = false;
17544 : 72900 : ret = MIN (ret, tret);
17545 : 72900 : if (ret == GS_ERROR)
17546 : : return ret;
17547 : :
17548 : : /* Handle OMP_FOR_COND. */
17549 : 72900 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17550 : 72900 : gcc_assert (COMPARISON_CLASS_P (t));
17551 : 72900 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17552 : :
17553 : 72900 : gimplify_omp_ctxp->in_for_exprs = true;
17554 : 72900 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17555 : : {
17556 : 599 : tree ub = TREE_OPERAND (t, 1);
17557 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
17558 : : is_gimple_val, fb_rvalue, false);
17559 : 599 : ret = MIN (ret, tret);
17560 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
17561 : : is_gimple_val, fb_rvalue, false);
17562 : : }
17563 : : else
17564 : 72301 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17565 : : is_gimple_val, fb_rvalue, false);
17566 : 72900 : gimplify_omp_ctxp->in_for_exprs = false;
17567 : 72900 : ret = MIN (ret, tret);
17568 : :
17569 : : /* Handle OMP_FOR_INCR. */
17570 : 72900 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17571 : 72900 : switch (TREE_CODE (t))
17572 : : {
17573 : 35685 : case PREINCREMENT_EXPR:
17574 : 35685 : case POSTINCREMENT_EXPR:
17575 : 35685 : {
17576 : 35685 : tree decl = TREE_OPERAND (t, 0);
17577 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17578 : : called to massage things appropriately. */
17579 : 35685 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17580 : :
17581 : 35685 : if (orig_for_stmt != for_stmt)
17582 : : break;
17583 : 25299 : t = build_int_cst (TREE_TYPE (decl), 1);
17584 : 25299 : if (c)
17585 : 4466 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17586 : 25299 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17587 : 25299 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17588 : 25299 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17589 : 25299 : break;
17590 : : }
17591 : :
17592 : 3826 : case PREDECREMENT_EXPR:
17593 : 3826 : case POSTDECREMENT_EXPR:
17594 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17595 : : called to massage things appropriately. */
17596 : 3826 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17597 : 3826 : if (orig_for_stmt != for_stmt)
17598 : : break;
17599 : 1988 : t = build_int_cst (TREE_TYPE (decl), -1);
17600 : 1988 : if (c)
17601 : 112 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17602 : 1988 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17603 : 1988 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17604 : 1988 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17605 : 1988 : break;
17606 : :
17607 : 33389 : case MODIFY_EXPR:
17608 : 33389 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17609 : 33389 : TREE_OPERAND (t, 0) = var;
17610 : :
17611 : 33389 : t = TREE_OPERAND (t, 1);
17612 : 33389 : switch (TREE_CODE (t))
17613 : : {
17614 : 26464 : case PLUS_EXPR:
17615 : 26464 : if (TREE_OPERAND (t, 1) == decl)
17616 : : {
17617 : 215 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
17618 : 215 : TREE_OPERAND (t, 0) = var;
17619 : 215 : break;
17620 : : }
17621 : :
17622 : : /* Fallthru. */
17623 : 33174 : case MINUS_EXPR:
17624 : 33174 : case POINTER_PLUS_EXPR:
17625 : 33174 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17626 : 33174 : TREE_OPERAND (t, 0) = var;
17627 : 33174 : break;
17628 : 0 : default:
17629 : 0 : gcc_unreachable ();
17630 : : }
17631 : :
17632 : 33389 : gimplify_omp_ctxp->in_for_exprs = true;
17633 : 33389 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17634 : : is_gimple_val, fb_rvalue, false);
17635 : 33389 : ret = MIN (ret, tret);
17636 : 33389 : if (c)
17637 : : {
17638 : 1571 : tree step = TREE_OPERAND (t, 1);
17639 : 1571 : tree stept = TREE_TYPE (decl);
17640 : 1571 : if (POINTER_TYPE_P (stept))
17641 : 280 : stept = sizetype;
17642 : 1571 : step = fold_convert (stept, step);
17643 : 1571 : if (TREE_CODE (t) == MINUS_EXPR)
17644 : 295 : step = fold_build1 (NEGATE_EXPR, stept, step);
17645 : 1571 : OMP_CLAUSE_LINEAR_STEP (c) = step;
17646 : 1571 : if (step != TREE_OPERAND (t, 1))
17647 : : {
17648 : 295 : tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
17649 : : &for_pre_body, NULL,
17650 : : is_gimple_val, fb_rvalue, false);
17651 : 295 : ret = MIN (ret, tret);
17652 : : }
17653 : : }
17654 : 33389 : gimplify_omp_ctxp->in_for_exprs = false;
17655 : 33389 : break;
17656 : :
17657 : 0 : default:
17658 : 0 : gcc_unreachable ();
17659 : : }
17660 : :
17661 : 72900 : if (c2)
17662 : : {
17663 : 250 : gcc_assert (c);
17664 : 250 : OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
17665 : : }
17666 : :
17667 : 72900 : if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
17668 : : {
17669 : 86413 : for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
17670 : 64926 : if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17671 : 16204 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
17672 : 55925 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17673 : 2155 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
17674 : 1371 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
17675 : 75276 : && OMP_CLAUSE_DECL (c) == decl)
17676 : : {
17677 : 7525 : if (is_doacross && (collapse == 1 || i >= collapse))
17678 : : t = var;
17679 : : else
17680 : : {
17681 : 7452 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17682 : 7452 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17683 : 7452 : gcc_assert (TREE_OPERAND (t, 0) == var);
17684 : 7452 : t = TREE_OPERAND (t, 1);
17685 : 7452 : gcc_assert (TREE_CODE (t) == PLUS_EXPR
17686 : : || TREE_CODE (t) == MINUS_EXPR
17687 : : || TREE_CODE (t) == POINTER_PLUS_EXPR);
17688 : 7452 : gcc_assert (TREE_OPERAND (t, 0) == var);
17689 : 14802 : t = build2 (TREE_CODE (t), TREE_TYPE (decl),
17690 : : is_doacross ? var : decl,
17691 : 7452 : TREE_OPERAND (t, 1));
17692 : : }
17693 : 7525 : gimple_seq *seq;
17694 : 7525 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
17695 : 6931 : seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
17696 : : else
17697 : 594 : seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
17698 : 7525 : push_gimplify_context ();
17699 : 7525 : gimplify_assign (decl, t, seq);
17700 : 7525 : gimple *bind = NULL;
17701 : 7525 : if (gimplify_ctxp->temps)
17702 : : {
17703 : 2070 : bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
17704 : 2070 : *seq = NULL;
17705 : 2070 : gimplify_seq_add_stmt (seq, bind);
17706 : : }
17707 : 7525 : pop_gimplify_context (bind);
17708 : : }
17709 : : }
17710 : 72900 : if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
17711 : 2609 : for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17712 : : {
17713 : 1197 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17714 : 1197 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17715 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17716 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17717 : 336 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17718 : 1197 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17719 : 1197 : gcc_assert (COMPARISON_CLASS_P (t));
17720 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17721 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17722 : 343 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17723 : : }
17724 : : }
17725 : :
17726 : 51223 : BITMAP_FREE (has_decl_expr);
17727 : 51498 : delete allocate_uids;
17728 : :
17729 : 51223 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17730 : 49641 : || (loop_p && orig_for_stmt == for_stmt))
17731 : : {
17732 : 2639 : push_gimplify_context ();
17733 : 2639 : if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
17734 : : {
17735 : 1891 : OMP_FOR_BODY (orig_for_stmt)
17736 : 1891 : = build3 (BIND_EXPR, void_type_node, NULL,
17737 : 1891 : OMP_FOR_BODY (orig_for_stmt), NULL);
17738 : 1891 : TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
17739 : : }
17740 : : }
17741 : :
17742 : 51223 : gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
17743 : : &for_body);
17744 : :
17745 : 51223 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17746 : 49641 : || (loop_p && orig_for_stmt == for_stmt))
17747 : : {
17748 : 2639 : if (gimple_code (g) == GIMPLE_BIND)
17749 : 2639 : pop_gimplify_context (g);
17750 : : else
17751 : 0 : pop_gimplify_context (NULL);
17752 : : }
17753 : :
17754 : 51223 : if (orig_for_stmt != for_stmt)
17755 : 36750 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17756 : : {
17757 : 22803 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17758 : 22803 : decl = TREE_OPERAND (t, 0);
17759 : 22803 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17760 : 22803 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17761 : 772 : gimplify_omp_ctxp = ctx->outer_context;
17762 : 22803 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17763 : 22803 : gimplify_omp_ctxp = ctx;
17764 : 22803 : omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
17765 : 22803 : TREE_OPERAND (t, 0) = var;
17766 : 22803 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17767 : 22803 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17768 : 22803 : TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
17769 : 22803 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17770 : 791 : for (int j = i + 1;
17771 : 791 : j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17772 : : {
17773 : 377 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17774 : 377 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17775 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17776 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17777 : : {
17778 : 133 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17779 : 133 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17780 : : }
17781 : 377 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17782 : 377 : gcc_assert (COMPARISON_CLASS_P (t));
17783 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17784 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17785 : : {
17786 : 67 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17787 : 67 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17788 : : }
17789 : : }
17790 : : }
17791 : :
17792 : 51223 : gimplify_adjust_omp_clauses (pre_p, for_body,
17793 : : &OMP_FOR_CLAUSES (orig_for_stmt),
17794 : 51223 : TREE_CODE (orig_for_stmt));
17795 : :
17796 : 51223 : int kind;
17797 : 51223 : switch (TREE_CODE (orig_for_stmt))
17798 : : {
17799 : : case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
17800 : 10642 : case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
17801 : 8244 : case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
17802 : 1582 : case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
17803 : 12577 : case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
17804 : 0 : default:
17805 : 0 : gcc_unreachable ();
17806 : : }
17807 : 51223 : if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
17808 : : {
17809 : 1057 : gimplify_seq_add_seq (pre_p, for_pre_body);
17810 : 1057 : for_pre_body = NULL;
17811 : : }
17812 : 51223 : gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
17813 : 51223 : TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
17814 : : for_pre_body);
17815 : 51223 : if (orig_for_stmt != for_stmt)
17816 : 13947 : gimple_omp_for_set_combined_p (gfor, true);
17817 : 51223 : if (gimplify_omp_ctxp
17818 : 42635 : && (gimplify_omp_ctxp->combined_loop
17819 : 35108 : || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
17820 : 12317 : && gimplify_omp_ctxp->outer_context
17821 : 7887 : && gimplify_omp_ctxp->outer_context->combined_loop)))
17822 : : {
17823 : 13947 : gimple_omp_for_set_combined_into_p (gfor, true);
17824 : 13947 : if (gimplify_omp_ctxp->combined_loop)
17825 : 7527 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
17826 : : else
17827 : 6420 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
17828 : : }
17829 : :
17830 : 124123 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17831 : : {
17832 : 72900 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17833 : 72900 : gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
17834 : 72900 : gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
17835 : 72900 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17836 : 72900 : gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
17837 : 72900 : gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
17838 : 72900 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17839 : 72900 : gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
17840 : : }
17841 : :
17842 : : /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
17843 : : constructs with GIMPLE_OMP_TASK sandwiched in between them.
17844 : : The outer taskloop stands for computing the number of iterations,
17845 : : counts for collapsed loops and holding taskloop specific clauses.
17846 : : The task construct stands for the effect of data sharing on the
17847 : : explicit task it creates and the inner taskloop stands for expansion
17848 : : of the static loop inside of the explicit task construct. */
17849 : 51223 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17850 : : {
17851 : 1582 : tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
17852 : 1582 : tree task_clauses = NULL_TREE;
17853 : 1582 : tree c = *gfor_clauses_ptr;
17854 : 1582 : tree *gtask_clauses_ptr = &task_clauses;
17855 : 1582 : tree outer_for_clauses = NULL_TREE;
17856 : 1582 : tree *gforo_clauses_ptr = &outer_for_clauses;
17857 : 1582 : bitmap lastprivate_uids = NULL;
17858 : 1582 : if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
17859 : : {
17860 : 36 : c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
17861 : 36 : if (c)
17862 : : {
17863 : 18 : lastprivate_uids = BITMAP_ALLOC (NULL);
17864 : 54 : for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
17865 : : OMP_CLAUSE_LASTPRIVATE))
17866 : 18 : bitmap_set_bit (lastprivate_uids,
17867 : 18 : DECL_UID (OMP_CLAUSE_DECL (c)));
17868 : : }
17869 : 36 : c = *gfor_clauses_ptr;
17870 : : }
17871 : 12200 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17872 : 10618 : switch (OMP_CLAUSE_CODE (c))
17873 : : {
17874 : : /* These clauses are allowed on task, move them there. */
17875 : 6196 : case OMP_CLAUSE_SHARED:
17876 : 6196 : case OMP_CLAUSE_FIRSTPRIVATE:
17877 : 6196 : case OMP_CLAUSE_DEFAULT:
17878 : 6196 : case OMP_CLAUSE_IF:
17879 : 6196 : case OMP_CLAUSE_UNTIED:
17880 : 6196 : case OMP_CLAUSE_FINAL:
17881 : 6196 : case OMP_CLAUSE_MERGEABLE:
17882 : 6196 : case OMP_CLAUSE_PRIORITY:
17883 : 6196 : case OMP_CLAUSE_REDUCTION:
17884 : 6196 : case OMP_CLAUSE_IN_REDUCTION:
17885 : 6196 : *gtask_clauses_ptr = c;
17886 : 6196 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17887 : 6196 : break;
17888 : 2064 : case OMP_CLAUSE_PRIVATE:
17889 : 2064 : if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
17890 : : {
17891 : : /* We want private on outer for and firstprivate
17892 : : on task. */
17893 : 26 : *gtask_clauses_ptr
17894 : 26 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17895 : : OMP_CLAUSE_FIRSTPRIVATE);
17896 : 26 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17897 : 26 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17898 : : openacc);
17899 : 26 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17900 : 26 : *gforo_clauses_ptr = c;
17901 : 26 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17902 : : }
17903 : : else
17904 : : {
17905 : 2038 : *gtask_clauses_ptr = c;
17906 : 2038 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17907 : : }
17908 : : break;
17909 : : /* These clauses go into outer taskloop clauses. */
17910 : 529 : case OMP_CLAUSE_GRAINSIZE:
17911 : 529 : case OMP_CLAUSE_NUM_TASKS:
17912 : 529 : case OMP_CLAUSE_NOGROUP:
17913 : 529 : *gforo_clauses_ptr = c;
17914 : 529 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17915 : 529 : break;
17916 : : /* Collapse clause we duplicate on both taskloops. */
17917 : 580 : case OMP_CLAUSE_COLLAPSE:
17918 : 580 : *gfor_clauses_ptr = c;
17919 : 580 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17920 : 580 : *gforo_clauses_ptr = copy_node (c);
17921 : 580 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17922 : 580 : break;
17923 : : /* For lastprivate, keep the clause on inner taskloop, and add
17924 : : a shared clause on task. If the same decl is also firstprivate,
17925 : : add also firstprivate clause on the inner taskloop. */
17926 : 1187 : case OMP_CLAUSE_LASTPRIVATE:
17927 : 1187 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17928 : : {
17929 : : /* For taskloop C++ lastprivate IVs, we want:
17930 : : 1) private on outer taskloop
17931 : : 2) firstprivate and shared on task
17932 : : 3) lastprivate on inner taskloop */
17933 : 38 : *gtask_clauses_ptr
17934 : 38 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17935 : : OMP_CLAUSE_FIRSTPRIVATE);
17936 : 38 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17937 : 38 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17938 : : openacc);
17939 : 38 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17940 : 38 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
17941 : 38 : *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17942 : : OMP_CLAUSE_PRIVATE);
17943 : 38 : OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
17944 : 38 : OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
17945 : 38 : TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
17946 : 38 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17947 : : }
17948 : 1187 : *gfor_clauses_ptr = c;
17949 : 1187 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17950 : 1187 : *gtask_clauses_ptr
17951 : 1187 : = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
17952 : 1187 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17953 : 1187 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
17954 : 312 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
17955 : 1187 : gtask_clauses_ptr
17956 : 1187 : = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17957 : 1187 : break;
17958 : : /* Allocate clause we duplicate on task and inner taskloop
17959 : : if the decl is lastprivate, otherwise just put on task. */
17960 : 62 : case OMP_CLAUSE_ALLOCATE:
17961 : 62 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
17962 : 62 : && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
17963 : : {
17964 : : /* Additionally, put firstprivate clause on task
17965 : : for the allocator if it is not constant. */
17966 : 34 : *gtask_clauses_ptr
17967 : 34 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17968 : : OMP_CLAUSE_FIRSTPRIVATE);
17969 : 34 : OMP_CLAUSE_DECL (*gtask_clauses_ptr)
17970 : 34 : = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
17971 : 34 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17972 : : }
17973 : 62 : if (lastprivate_uids
17974 : 97 : && bitmap_bit_p (lastprivate_uids,
17975 : 35 : DECL_UID (OMP_CLAUSE_DECL (c))))
17976 : : {
17977 : 17 : *gfor_clauses_ptr = c;
17978 : 17 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17979 : 17 : *gtask_clauses_ptr = copy_node (c);
17980 : 17 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17981 : : }
17982 : : else
17983 : : {
17984 : 45 : *gtask_clauses_ptr = c;
17985 : 45 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17986 : : }
17987 : : break;
17988 : 0 : default:
17989 : 0 : gcc_unreachable ();
17990 : : }
17991 : 1582 : *gfor_clauses_ptr = NULL_TREE;
17992 : 1582 : *gtask_clauses_ptr = NULL_TREE;
17993 : 1582 : *gforo_clauses_ptr = NULL_TREE;
17994 : 1582 : BITMAP_FREE (lastprivate_uids);
17995 : 1582 : gimple_set_location (gfor, input_location);
17996 : 1582 : g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
17997 : 1582 : g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
17998 : : NULL_TREE, NULL_TREE, NULL_TREE);
17999 : 1582 : gimple_set_location (g, input_location);
18000 : 1582 : gimple_omp_task_set_taskloop_p (g, true);
18001 : 1582 : g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
18002 : 1582 : gomp_for *gforo
18003 : 1582 : = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
18004 : : gimple_omp_for_collapse (gfor),
18005 : : gimple_omp_for_pre_body (gfor));
18006 : 1582 : gimple_omp_for_set_pre_body (gfor, NULL);
18007 : 1582 : gimple_omp_for_set_combined_p (gforo, true);
18008 : 1582 : gimple_omp_for_set_combined_into_p (gfor, true);
18009 : 3450 : for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
18010 : : {
18011 : 1868 : tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
18012 : 1868 : tree v = create_tmp_var (type);
18013 : 1868 : gimple_omp_for_set_index (gforo, i, v);
18014 : 1868 : t = unshare_expr (gimple_omp_for_initial (gfor, i));
18015 : 1868 : gimple_omp_for_set_initial (gforo, i, t);
18016 : 1868 : gimple_omp_for_set_cond (gforo, i,
18017 : : gimple_omp_for_cond (gfor, i));
18018 : 1868 : t = unshare_expr (gimple_omp_for_final (gfor, i));
18019 : 1868 : gimple_omp_for_set_final (gforo, i, t);
18020 : 1868 : t = unshare_expr (gimple_omp_for_incr (gfor, i));
18021 : 1868 : gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
18022 : 1868 : TREE_OPERAND (t, 0) = v;
18023 : 1868 : gimple_omp_for_set_incr (gforo, i, t);
18024 : 1868 : t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18025 : 1868 : OMP_CLAUSE_DECL (t) = v;
18026 : 1868 : OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
18027 : 1868 : gimple_omp_for_set_clauses (gforo, t);
18028 : 1868 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
18029 : : {
18030 : 90 : tree *p1 = NULL, *p2 = NULL;
18031 : 90 : t = gimple_omp_for_initial (gforo, i);
18032 : 90 : if (TREE_CODE (t) == TREE_VEC)
18033 : 34 : p1 = &TREE_VEC_ELT (t, 0);
18034 : 90 : t = gimple_omp_for_final (gforo, i);
18035 : 90 : if (TREE_CODE (t) == TREE_VEC)
18036 : : {
18037 : 31 : if (p1)
18038 : 23 : p2 = &TREE_VEC_ELT (t, 0);
18039 : : else
18040 : 8 : p1 = &TREE_VEC_ELT (t, 0);
18041 : : }
18042 : 90 : if (p1)
18043 : : {
18044 : : int j;
18045 : 58 : for (j = 0; j < i; j++)
18046 : 58 : if (*p1 == gimple_omp_for_index (gfor, j))
18047 : : {
18048 : 42 : *p1 = gimple_omp_for_index (gforo, j);
18049 : 42 : if (p2)
18050 : 23 : *p2 = *p1;
18051 : : break;
18052 : : }
18053 : 42 : gcc_assert (j < i);
18054 : : }
18055 : : }
18056 : : }
18057 : 1582 : gimplify_seq_add_stmt (pre_p, gforo);
18058 : : }
18059 : : else
18060 : 49641 : gimplify_seq_add_stmt (pre_p, gfor);
18061 : :
18062 : 51223 : if (TREE_CODE (orig_for_stmt) == OMP_FOR)
18063 : : {
18064 : 18178 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
18065 : 18178 : unsigned lastprivate_conditional = 0;
18066 : 18178 : while (ctx
18067 : 18214 : && (ctx->region_type == ORT_TARGET_DATA
18068 : 14201 : || ctx->region_type == ORT_TASKGROUP))
18069 : 36 : ctx = ctx->outer_context;
18070 : 18178 : if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
18071 : 13941 : for (tree c = gimple_omp_for_clauses (gfor);
18072 : 63193 : c; c = OMP_CLAUSE_CHAIN (c))
18073 : 49252 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18074 : 49252 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18075 : 158 : ++lastprivate_conditional;
18076 : 13941 : if (lastprivate_conditional)
18077 : : {
18078 : 118 : struct omp_for_data fd;
18079 : 118 : omp_extract_for_data (gfor, &fd, NULL);
18080 : 118 : tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
18081 : 118 : lastprivate_conditional);
18082 : 118 : tree var = create_tmp_var_raw (type);
18083 : 118 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
18084 : 118 : OMP_CLAUSE_DECL (c) = var;
18085 : 118 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18086 : 118 : gimple_omp_for_set_clauses (gfor, c);
18087 : 118 : omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
18088 : : }
18089 : : }
18090 : 33045 : else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
18091 : : {
18092 : 10642 : unsigned lastprivate_conditional = 0;
18093 : 49078 : for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
18094 : 38436 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
18095 : 38436 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
18096 : 119 : ++lastprivate_conditional;
18097 : 10642 : if (lastprivate_conditional)
18098 : : {
18099 : 91 : struct omp_for_data fd;
18100 : 91 : omp_extract_for_data (gfor, &fd, NULL);
18101 : 91 : tree type = unsigned_type_for (fd.iter_type);
18102 : 301 : while (lastprivate_conditional--)
18103 : : {
18104 : 119 : tree c = build_omp_clause (UNKNOWN_LOCATION,
18105 : : OMP_CLAUSE__CONDTEMP_);
18106 : 119 : OMP_CLAUSE_DECL (c) = create_tmp_var (type);
18107 : 119 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
18108 : 119 : gimple_omp_for_set_clauses (gfor, c);
18109 : : }
18110 : : }
18111 : : }
18112 : :
18113 : 51223 : if (ret != GS_ALL_DONE)
18114 : : return GS_ERROR;
18115 : 51223 : *expr_p = NULL_TREE;
18116 : 51223 : return GS_ALL_DONE;
18117 : : }
18118 : :
18119 : : /* Helper for gimplify_omp_loop, called through walk_tree. */
18120 : :
18121 : : static tree
18122 : 294 : note_no_context_vars (tree *tp, int *, void *data)
18123 : : {
18124 : 294 : if (VAR_P (*tp)
18125 : 56 : && DECL_CONTEXT (*tp) == NULL_TREE
18126 : 309 : && !is_global_var (*tp))
18127 : : {
18128 : 15 : vec<tree> *d = (vec<tree> *) data;
18129 : 15 : d->safe_push (*tp);
18130 : 15 : DECL_CONTEXT (*tp) = current_function_decl;
18131 : : }
18132 : 294 : return NULL_TREE;
18133 : : }
18134 : :
18135 : : /* Gimplify the gross structure of an OMP_LOOP statement. */
18136 : :
18137 : : static enum gimplify_status
18138 : 1057 : gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
18139 : : {
18140 : 1057 : tree for_stmt = *expr_p;
18141 : 1057 : tree clauses = OMP_FOR_CLAUSES (for_stmt);
18142 : 1057 : struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
18143 : 1057 : enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
18144 : 1057 : int i;
18145 : :
18146 : 1057 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
18147 : 1057 : if (*expr_p != for_stmt)
18148 : : return GS_OK;
18149 : :
18150 : : /* If order is not present, the behavior is as if order(concurrent)
18151 : : appeared. */
18152 : 1057 : tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
18153 : 1057 : if (order == NULL_TREE)
18154 : : {
18155 : 804 : order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
18156 : 804 : OMP_CLAUSE_CHAIN (order) = clauses;
18157 : 804 : OMP_FOR_CLAUSES (for_stmt) = clauses = order;
18158 : : }
18159 : :
18160 : 1057 : tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
18161 : 1057 : if (bind == NULL_TREE)
18162 : : {
18163 : 579 : if (!flag_openmp) /* flag_openmp_simd */
18164 : : ;
18165 : 562 : else if (octx && (octx->region_type & ORT_TEAMS) != 0)
18166 : : kind = OMP_CLAUSE_BIND_TEAMS;
18167 : 337 : else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
18168 : : kind = OMP_CLAUSE_BIND_PARALLEL;
18169 : : else
18170 : : {
18171 : 187 : for (; octx; octx = octx->outer_context)
18172 : : {
18173 : 79 : if ((octx->region_type & ORT_ACC) != 0
18174 : 79 : || octx->region_type == ORT_NONE
18175 : 79 : || octx->region_type == ORT_IMPLICIT_TARGET)
18176 : 0 : continue;
18177 : : break;
18178 : : }
18179 : 187 : if (octx == NULL && !in_omp_construct)
18180 : 4 : error_at (EXPR_LOCATION (for_stmt),
18181 : : "%<bind%> clause not specified on a %<loop%> "
18182 : : "construct not nested inside another OpenMP construct");
18183 : : }
18184 : 579 : bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
18185 : 579 : OMP_CLAUSE_CHAIN (bind) = clauses;
18186 : 579 : OMP_CLAUSE_BIND_KIND (bind) = kind;
18187 : 579 : OMP_FOR_CLAUSES (for_stmt) = bind;
18188 : : }
18189 : : else
18190 : 478 : switch (OMP_CLAUSE_BIND_KIND (bind))
18191 : : {
18192 : : case OMP_CLAUSE_BIND_THREAD:
18193 : : break;
18194 : 166 : case OMP_CLAUSE_BIND_PARALLEL:
18195 : 166 : if (!flag_openmp) /* flag_openmp_simd */
18196 : : {
18197 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18198 : 0 : break;
18199 : : }
18200 : 240 : for (; octx; octx = octx->outer_context)
18201 : 78 : if (octx->region_type == ORT_SIMD
18202 : 78 : && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
18203 : : {
18204 : 4 : error_at (EXPR_LOCATION (for_stmt),
18205 : : "%<bind(parallel)%> on a %<loop%> construct nested "
18206 : : "inside %<simd%> construct");
18207 : 4 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18208 : 4 : break;
18209 : : }
18210 : : kind = OMP_CLAUSE_BIND_PARALLEL;
18211 : : break;
18212 : 158 : case OMP_CLAUSE_BIND_TEAMS:
18213 : 158 : if (!flag_openmp) /* flag_openmp_simd */
18214 : : {
18215 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18216 : 0 : break;
18217 : : }
18218 : 158 : if ((octx
18219 : 128 : && octx->region_type != ORT_IMPLICIT_TARGET
18220 : 111 : && octx->region_type != ORT_NONE
18221 : 111 : && (octx->region_type & ORT_TEAMS) == 0)
18222 : 106 : || in_omp_construct)
18223 : : {
18224 : 64 : error_at (EXPR_LOCATION (for_stmt),
18225 : : "%<bind(teams)%> on a %<loop%> region not strictly "
18226 : : "nested inside of a %<teams%> region");
18227 : 64 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
18228 : 64 : break;
18229 : : }
18230 : : kind = OMP_CLAUSE_BIND_TEAMS;
18231 : : break;
18232 : 0 : default:
18233 : 0 : gcc_unreachable ();
18234 : : }
18235 : :
18236 : 4268 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
18237 : 3211 : switch (OMP_CLAUSE_CODE (*pc))
18238 : : {
18239 : 343 : case OMP_CLAUSE_REDUCTION:
18240 : 343 : if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
18241 : : {
18242 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18243 : : "%<inscan%> %<reduction%> clause on "
18244 : : "%qs construct", "loop");
18245 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
18246 : : }
18247 : 343 : if (OMP_CLAUSE_REDUCTION_TASK (*pc))
18248 : : {
18249 : 8 : error_at (OMP_CLAUSE_LOCATION (*pc),
18250 : : "invalid %<task%> reduction modifier on construct "
18251 : : "other than %<parallel%>, %qs or %<sections%>",
18252 : 4 : lang_GNU_Fortran () ? "do" : "for");
18253 : 4 : OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
18254 : : }
18255 : 343 : pc = &OMP_CLAUSE_CHAIN (*pc);
18256 : 343 : break;
18257 : : case OMP_CLAUSE_LASTPRIVATE:
18258 : 293 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18259 : : {
18260 : 289 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
18261 : 289 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
18262 : 289 : if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
18263 : : break;
18264 : 26 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18265 : 25 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18266 : : i)) == TREE_LIST
18267 : 48 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
18268 : : i)))
18269 : : {
18270 : 22 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18271 : 22 : if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
18272 : : break;
18273 : : }
18274 : : }
18275 : 289 : if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
18276 : : {
18277 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
18278 : : "%<lastprivate%> clause on a %<loop%> construct refers "
18279 : : "to a variable %qD which is not the loop iterator",
18280 : 4 : OMP_CLAUSE_DECL (*pc));
18281 : 4 : *pc = OMP_CLAUSE_CHAIN (*pc);
18282 : 4 : break;
18283 : : }
18284 : 285 : pc = &OMP_CLAUSE_CHAIN (*pc);
18285 : 285 : break;
18286 : 2579 : default:
18287 : 2579 : pc = &OMP_CLAUSE_CHAIN (*pc);
18288 : 2579 : break;
18289 : : }
18290 : :
18291 : 1057 : TREE_SET_CODE (for_stmt, OMP_SIMD);
18292 : :
18293 : 1057 : int last;
18294 : 1057 : switch (kind)
18295 : : {
18296 : : case OMP_CLAUSE_BIND_THREAD: last = 0; break;
18297 : 424 : case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
18298 : 211 : case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
18299 : : }
18300 : 1903 : for (int pass = 1; pass <= last; pass++)
18301 : : {
18302 : 846 : if (pass == 2)
18303 : : {
18304 : 211 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
18305 : : make_node (BLOCK));
18306 : 211 : append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
18307 : 211 : *expr_p = make_node (OMP_PARALLEL);
18308 : 211 : TREE_TYPE (*expr_p) = void_type_node;
18309 : 211 : OMP_PARALLEL_BODY (*expr_p) = bind;
18310 : 211 : OMP_PARALLEL_COMBINED (*expr_p) = 1;
18311 : 211 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
18312 : 211 : tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
18313 : 425 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
18314 : 214 : if (OMP_FOR_ORIG_DECLS (for_stmt)
18315 : 214 : && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
18316 : : == TREE_LIST))
18317 : : {
18318 : 16 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
18319 : 16 : if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
18320 : : {
18321 : 13 : *pc = build_omp_clause (UNKNOWN_LOCATION,
18322 : : OMP_CLAUSE_FIRSTPRIVATE);
18323 : 13 : OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
18324 : 13 : pc = &OMP_CLAUSE_CHAIN (*pc);
18325 : : }
18326 : : }
18327 : : }
18328 : 846 : tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
18329 : 846 : tree *pc = &OMP_FOR_CLAUSES (t);
18330 : 846 : TREE_TYPE (t) = void_type_node;
18331 : 846 : OMP_FOR_BODY (t) = *expr_p;
18332 : 846 : SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
18333 : 3914 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
18334 : 3068 : switch (OMP_CLAUSE_CODE (c))
18335 : : {
18336 : 1958 : case OMP_CLAUSE_BIND:
18337 : 1958 : case OMP_CLAUSE_ORDER:
18338 : 1958 : case OMP_CLAUSE_COLLAPSE:
18339 : 1958 : *pc = copy_node (c);
18340 : 1958 : pc = &OMP_CLAUSE_CHAIN (*pc);
18341 : 1958 : break;
18342 : : case OMP_CLAUSE_PRIVATE:
18343 : : case OMP_CLAUSE_FIRSTPRIVATE:
18344 : : /* Only needed on innermost. */
18345 : : break;
18346 : 351 : case OMP_CLAUSE_LASTPRIVATE:
18347 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
18348 : : {
18349 : 7 : *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18350 : : OMP_CLAUSE_FIRSTPRIVATE);
18351 : 7 : OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
18352 : 7 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18353 : 7 : pc = &OMP_CLAUSE_CHAIN (*pc);
18354 : : }
18355 : 351 : *pc = copy_node (c);
18356 : 351 : OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
18357 : 351 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18358 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
18359 : : {
18360 : 22 : if (pass != last)
18361 : 7 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
18362 : : else
18363 : 15 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
18364 : 22 : OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
18365 : : }
18366 : 351 : pc = &OMP_CLAUSE_CHAIN (*pc);
18367 : 351 : break;
18368 : 463 : case OMP_CLAUSE_REDUCTION:
18369 : 463 : *pc = copy_node (c);
18370 : 463 : OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
18371 : 463 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
18372 : 463 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
18373 : : {
18374 : 15 : auto_vec<tree> no_context_vars;
18375 : 15 : int walk_subtrees = 0;
18376 : 15 : note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18377 : : &walk_subtrees, &no_context_vars);
18378 : 15 : if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
18379 : 0 : note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
18380 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
18381 : : note_no_context_vars,
18382 : : &no_context_vars);
18383 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
18384 : : note_no_context_vars,
18385 : : &no_context_vars);
18386 : :
18387 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
18388 : 15 : = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
18389 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18390 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
18391 : 0 : = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
18392 : :
18393 : 15 : hash_map<tree, tree> decl_map;
18394 : 15 : decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
18395 : 15 : decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
18396 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
18397 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
18398 : 0 : decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
18399 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
18400 : :
18401 : 15 : copy_body_data id;
18402 : 15 : memset (&id, 0, sizeof (id));
18403 : 15 : id.src_fn = current_function_decl;
18404 : 15 : id.dst_fn = current_function_decl;
18405 : 15 : id.src_cfun = cfun;
18406 : 15 : id.decl_map = &decl_map;
18407 : 15 : id.copy_decl = copy_decl_no_change;
18408 : 15 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
18409 : 15 : id.transform_new_cfg = true;
18410 : 15 : id.transform_return_to_modify = false;
18411 : 15 : id.eh_lp_nr = 0;
18412 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
18413 : : &id, NULL);
18414 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
18415 : : &id, NULL);
18416 : :
18417 : 60 : for (tree d : no_context_vars)
18418 : : {
18419 : 15 : DECL_CONTEXT (d) = NULL_TREE;
18420 : 15 : DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
18421 : : }
18422 : 15 : }
18423 : : else
18424 : : {
18425 : 448 : OMP_CLAUSE_REDUCTION_INIT (*pc)
18426 : 448 : = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
18427 : 448 : OMP_CLAUSE_REDUCTION_MERGE (*pc)
18428 : 896 : = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
18429 : : }
18430 : 463 : pc = &OMP_CLAUSE_CHAIN (*pc);
18431 : 463 : break;
18432 : 0 : default:
18433 : 0 : gcc_unreachable ();
18434 : : }
18435 : 846 : *pc = NULL_TREE;
18436 : 846 : *expr_p = t;
18437 : : }
18438 : 1057 : return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
18439 : : }
18440 : :
18441 : :
18442 : : /* Helper function of optimize_target_teams, find OMP_TEAMS inside
18443 : : of OMP_TARGET's body. */
18444 : :
18445 : : static tree
18446 : 80615 : find_omp_teams (tree *tp, int *walk_subtrees, void *)
18447 : : {
18448 : 80615 : *walk_subtrees = 0;
18449 : 80615 : switch (TREE_CODE (*tp))
18450 : : {
18451 : : case OMP_TEAMS:
18452 : : return *tp;
18453 : 23923 : case BIND_EXPR:
18454 : 23923 : case STATEMENT_LIST:
18455 : 23923 : *walk_subtrees = 1;
18456 : 23923 : break;
18457 : : default:
18458 : : break;
18459 : : }
18460 : : return NULL_TREE;
18461 : : }
18462 : :
18463 : : /* Helper function of optimize_target_teams, determine if the expression
18464 : : can be computed safely before the target construct on the host. */
18465 : :
18466 : : static tree
18467 : 1311 : computable_teams_clause (tree *tp, int *walk_subtrees, void *)
18468 : : {
18469 : 1831 : splay_tree_node n;
18470 : :
18471 : 1831 : if (TYPE_P (*tp))
18472 : : {
18473 : 0 : *walk_subtrees = 0;
18474 : 0 : return NULL_TREE;
18475 : : }
18476 : 1831 : switch (TREE_CODE (*tp))
18477 : : {
18478 : 990 : case VAR_DECL:
18479 : 990 : case PARM_DECL:
18480 : 990 : case RESULT_DECL:
18481 : 990 : *walk_subtrees = 0;
18482 : 990 : if (error_operand_p (*tp)
18483 : 990 : || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
18484 : 990 : || DECL_HAS_VALUE_EXPR_P (*tp)
18485 : 990 : || DECL_THREAD_LOCAL_P (*tp)
18486 : 990 : || TREE_SIDE_EFFECTS (*tp)
18487 : 1980 : || TREE_THIS_VOLATILE (*tp))
18488 : 0 : return *tp;
18489 : 990 : if (is_global_var (*tp)
18490 : 990 : && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
18491 : 0 : || lookup_attribute ("omp declare target link",
18492 : 0 : DECL_ATTRIBUTES (*tp))))
18493 : 16 : return *tp;
18494 : 974 : if (VAR_P (*tp)
18495 : 668 : && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
18496 : 50 : && !is_global_var (*tp)
18497 : 1024 : && decl_function_context (*tp) == current_function_decl)
18498 : 50 : return *tp;
18499 : 1848 : n = splay_tree_lookup (gimplify_omp_ctxp->variables,
18500 : 924 : (splay_tree_key) *tp);
18501 : 924 : if (n == NULL)
18502 : : {
18503 : 292 : if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
18504 : : return NULL_TREE;
18505 : 24 : return *tp;
18506 : : }
18507 : 632 : else if (n->value & GOVD_LOCAL)
18508 : 0 : return *tp;
18509 : 632 : else if (n->value & GOVD_FIRSTPRIVATE)
18510 : : return NULL_TREE;
18511 : 112 : else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18512 : : == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18513 : : return NULL_TREE;
18514 : 96 : return *tp;
18515 : 76 : case INTEGER_CST:
18516 : 76 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18517 : : return *tp;
18518 : : return NULL_TREE;
18519 : 520 : case TARGET_EXPR:
18520 : 520 : if (TARGET_EXPR_INITIAL (*tp)
18521 : 520 : || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
18522 : : return *tp;
18523 : 520 : return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
18524 : 520 : walk_subtrees, NULL);
18525 : : /* Allow some reasonable subset of integral arithmetics. */
18526 : 193 : case PLUS_EXPR:
18527 : 193 : case MINUS_EXPR:
18528 : 193 : case MULT_EXPR:
18529 : 193 : case TRUNC_DIV_EXPR:
18530 : 193 : case CEIL_DIV_EXPR:
18531 : 193 : case FLOOR_DIV_EXPR:
18532 : 193 : case ROUND_DIV_EXPR:
18533 : 193 : case TRUNC_MOD_EXPR:
18534 : 193 : case CEIL_MOD_EXPR:
18535 : 193 : case FLOOR_MOD_EXPR:
18536 : 193 : case ROUND_MOD_EXPR:
18537 : 193 : case RDIV_EXPR:
18538 : 193 : case EXACT_DIV_EXPR:
18539 : 193 : case MIN_EXPR:
18540 : 193 : case MAX_EXPR:
18541 : 193 : case LSHIFT_EXPR:
18542 : 193 : case RSHIFT_EXPR:
18543 : 193 : case BIT_IOR_EXPR:
18544 : 193 : case BIT_XOR_EXPR:
18545 : 193 : case BIT_AND_EXPR:
18546 : 193 : case NEGATE_EXPR:
18547 : 193 : case ABS_EXPR:
18548 : 193 : case BIT_NOT_EXPR:
18549 : 193 : case NON_LVALUE_EXPR:
18550 : 193 : CASE_CONVERT:
18551 : 193 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18552 : : return *tp;
18553 : : return NULL_TREE;
18554 : : /* And disallow anything else, except for comparisons. */
18555 : 52 : default:
18556 : 52 : if (COMPARISON_CLASS_P (*tp))
18557 : : return NULL_TREE;
18558 : : return *tp;
18559 : : }
18560 : : }
18561 : :
18562 : : /* Try to determine if the num_teams and/or thread_limit expressions
18563 : : can have their values determined already before entering the
18564 : : target construct.
18565 : : INTEGER_CSTs trivially are,
18566 : : integral decls that are firstprivate (explicitly or implicitly)
18567 : : or explicitly map(always, to:) or map(always, tofrom:) on the target
18568 : : region too, and expressions involving simple arithmetics on those
18569 : : too, function calls are not ok, dereferencing something neither etc.
18570 : : Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
18571 : : EXPR based on what we find:
18572 : : 0 stands for clause not specified at all, use implementation default
18573 : : -1 stands for value that can't be determined easily before entering
18574 : : the target construct.
18575 : : -2 means that no explicit teams construct was specified
18576 : : If teams construct is not present at all, use 1 for num_teams
18577 : : and 0 for thread_limit (only one team is involved, and the thread
18578 : : limit is implementation defined. */
18579 : :
18580 : : static void
18581 : 12589 : optimize_target_teams (tree target, gimple_seq *pre_p)
18582 : : {
18583 : 12589 : tree body = OMP_BODY (target);
18584 : 12589 : tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
18585 : 12589 : tree num_teams_lower = NULL_TREE;
18586 : 12589 : tree num_teams_upper = integer_zero_node;
18587 : 12589 : tree thread_limit = integer_zero_node;
18588 : 12589 : location_t num_teams_loc = EXPR_LOCATION (target);
18589 : 12589 : location_t thread_limit_loc = EXPR_LOCATION (target);
18590 : 12589 : tree c, *p, expr;
18591 : 12589 : struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
18592 : :
18593 : 12589 : if (teams == NULL_TREE)
18594 : 6487 : num_teams_upper = build_int_cst (integer_type_node, -2);
18595 : : else
18596 : 10573 : for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
18597 : : {
18598 : 4471 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
18599 : : {
18600 : 617 : p = &num_teams_upper;
18601 : 617 : num_teams_loc = OMP_CLAUSE_LOCATION (c);
18602 : 617 : if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
18603 : : {
18604 : 148 : expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
18605 : 148 : if (TREE_CODE (expr) == INTEGER_CST)
18606 : 21 : num_teams_lower = expr;
18607 : 127 : else if (walk_tree (&expr, computable_teams_clause,
18608 : : NULL, NULL))
18609 : 19 : num_teams_lower = integer_minus_one_node;
18610 : : else
18611 : : {
18612 : 108 : num_teams_lower = expr;
18613 : 108 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18614 : 108 : if (gimplify_expr (&num_teams_lower, pre_p, NULL,
18615 : : is_gimple_val, fb_rvalue, false)
18616 : : == GS_ERROR)
18617 : : {
18618 : 0 : gimplify_omp_ctxp = target_ctx;
18619 : 0 : num_teams_lower = integer_minus_one_node;
18620 : : }
18621 : : else
18622 : : {
18623 : 108 : gimplify_omp_ctxp = target_ctx;
18624 : 108 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18625 : 28 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
18626 : 56 : = num_teams_lower;
18627 : : }
18628 : : }
18629 : : }
18630 : : }
18631 : 3854 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18632 : : {
18633 : 471 : p = &thread_limit;
18634 : 471 : thread_limit_loc = OMP_CLAUSE_LOCATION (c);
18635 : : }
18636 : : else
18637 : 3383 : continue;
18638 : 1088 : expr = OMP_CLAUSE_OPERAND (c, 0);
18639 : 1088 : if (TREE_CODE (expr) == INTEGER_CST)
18640 : : {
18641 : 173 : *p = expr;
18642 : 173 : continue;
18643 : : }
18644 : 915 : if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
18645 : : {
18646 : 219 : *p = integer_minus_one_node;
18647 : 219 : continue;
18648 : : }
18649 : 696 : *p = expr;
18650 : 696 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18651 : 696 : if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
18652 : : == GS_ERROR)
18653 : : {
18654 : 0 : gimplify_omp_ctxp = target_ctx;
18655 : 0 : *p = integer_minus_one_node;
18656 : 0 : continue;
18657 : : }
18658 : 696 : gimplify_omp_ctxp = target_ctx;
18659 : 696 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18660 : 48 : OMP_CLAUSE_OPERAND (c, 0) = *p;
18661 : : }
18662 : 12589 : if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
18663 : : {
18664 : 12321 : c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
18665 : 12321 : OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
18666 : 12321 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18667 : 12321 : OMP_TARGET_CLAUSES (target) = c;
18668 : : }
18669 : 12589 : c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
18670 : 12589 : OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
18671 : 12589 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
18672 : 12589 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18673 : 12589 : OMP_TARGET_CLAUSES (target) = c;
18674 : 12589 : }
18675 : :
18676 : : /* Gimplify the gross structure of several OMP constructs. */
18677 : :
18678 : : static void
18679 : 38958 : gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
18680 : : {
18681 : 38958 : tree expr = *expr_p;
18682 : 38958 : gimple *stmt;
18683 : 38958 : gimple_seq body = NULL;
18684 : 38958 : enum omp_region_type ort;
18685 : :
18686 : 38958 : switch (TREE_CODE (expr))
18687 : : {
18688 : : case OMP_SECTIONS:
18689 : : case OMP_SINGLE:
18690 : : ort = ORT_WORKSHARE;
18691 : : break;
18692 : 214 : case OMP_SCOPE:
18693 : 214 : ort = ORT_TASKGROUP;
18694 : 214 : break;
18695 : 12589 : case OMP_TARGET:
18696 : 12589 : ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
18697 : : break;
18698 : 2564 : case OACC_KERNELS:
18699 : 2564 : ort = ORT_ACC_KERNELS;
18700 : 2564 : break;
18701 : 7903 : case OACC_PARALLEL:
18702 : 7903 : ort = ORT_ACC_PARALLEL;
18703 : 7903 : break;
18704 : 1041 : case OACC_SERIAL:
18705 : 1041 : ort = ORT_ACC_SERIAL;
18706 : 1041 : break;
18707 : 1953 : case OACC_DATA:
18708 : 1953 : ort = ORT_ACC_DATA;
18709 : 1953 : break;
18710 : 1863 : case OMP_TARGET_DATA:
18711 : 1863 : ort = ORT_TARGET_DATA;
18712 : 1863 : break;
18713 : 8813 : case OMP_TEAMS:
18714 : 8813 : ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
18715 : 8813 : if (gimplify_omp_ctxp == NULL
18716 : 6142 : || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
18717 : 2671 : ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
18718 : : break;
18719 : 118 : case OACC_HOST_DATA:
18720 : 118 : ort = ORT_ACC_HOST_DATA;
18721 : 118 : break;
18722 : 0 : default:
18723 : 0 : gcc_unreachable ();
18724 : : }
18725 : :
18726 : 38958 : gimple_seq iterator_loops_seq = NULL;
18727 : 38958 : if (TREE_CODE (expr) == OMP_TARGET)
18728 : : {
18729 : 12589 : remove_unused_omp_iterator_vars (&OMP_CLAUSES (expr));
18730 : 12589 : build_omp_iterators_loops (&OMP_CLAUSES (expr), &iterator_loops_seq);
18731 : : }
18732 : :
18733 : 38958 : bool save_in_omp_construct = in_omp_construct;
18734 : 38958 : if ((ort & ORT_ACC) == 0)
18735 : 25379 : in_omp_construct = false;
18736 : 38958 : gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
18737 : 38958 : TREE_CODE (expr), &iterator_loops_seq);
18738 : 38958 : if (TREE_CODE (expr) == OMP_TARGET)
18739 : 12589 : optimize_target_teams (expr, pre_p);
18740 : 38958 : if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
18741 : 10927 : || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18742 : : {
18743 : 30702 : push_gimplify_context ();
18744 : 30702 : gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
18745 : 30702 : if (gimple_code (g) == GIMPLE_BIND)
18746 : 30702 : pop_gimplify_context (g);
18747 : : else
18748 : 0 : pop_gimplify_context (NULL);
18749 : 30702 : if ((ort & ORT_TARGET_DATA) != 0)
18750 : : {
18751 : 3934 : enum built_in_function end_ix;
18752 : 3934 : switch (TREE_CODE (expr))
18753 : : {
18754 : : case OACC_DATA:
18755 : : case OACC_HOST_DATA:
18756 : : end_ix = BUILT_IN_GOACC_DATA_END;
18757 : : break;
18758 : 1863 : case OMP_TARGET_DATA:
18759 : 1863 : end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
18760 : 1863 : break;
18761 : 0 : default:
18762 : 0 : gcc_unreachable ();
18763 : : }
18764 : 3934 : tree fn = builtin_decl_explicit (end_ix);
18765 : 3934 : g = gimple_build_call (fn, 0);
18766 : 3934 : gimple_seq cleanup = NULL;
18767 : 3934 : gimple_seq_add_stmt (&cleanup, g);
18768 : 3934 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
18769 : 3934 : body = NULL;
18770 : 3934 : gimple_seq_add_stmt (&body, g);
18771 : : }
18772 : : }
18773 : : else
18774 : 8256 : gimplify_and_add (OMP_BODY (expr), &body);
18775 : 38958 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
18776 : 38958 : TREE_CODE (expr), &iterator_loops_seq);
18777 : 38958 : in_omp_construct = save_in_omp_construct;
18778 : :
18779 : 38958 : switch (TREE_CODE (expr))
18780 : : {
18781 : 1953 : case OACC_DATA:
18782 : 3906 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
18783 : 1953 : OMP_CLAUSES (expr));
18784 : 1953 : break;
18785 : 118 : case OACC_HOST_DATA:
18786 : 118 : if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
18787 : : {
18788 : 128 : for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18789 : 95 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
18790 : 57 : OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
18791 : : }
18792 : :
18793 : 236 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
18794 : 118 : OMP_CLAUSES (expr));
18795 : 118 : break;
18796 : 2564 : case OACC_KERNELS:
18797 : 5128 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
18798 : 2564 : OMP_CLAUSES (expr));
18799 : 2564 : break;
18800 : 7903 : case OACC_PARALLEL:
18801 : 15806 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
18802 : 7903 : OMP_CLAUSES (expr));
18803 : 7903 : break;
18804 : 1041 : case OACC_SERIAL:
18805 : 2082 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
18806 : 1041 : OMP_CLAUSES (expr));
18807 : 1041 : break;
18808 : 626 : case OMP_SECTIONS:
18809 : 626 : stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
18810 : 626 : break;
18811 : 1274 : case OMP_SINGLE:
18812 : 1274 : stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
18813 : 1274 : break;
18814 : 214 : case OMP_SCOPE:
18815 : 214 : stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
18816 : 214 : break;
18817 : 12589 : case OMP_TARGET:
18818 : 25178 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
18819 : 12589 : OMP_CLAUSES (expr), iterator_loops_seq);
18820 : 12589 : break;
18821 : 1863 : case OMP_TARGET_DATA:
18822 : : /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
18823 : : to be evaluated before the use_device_{ptr,addr} clauses if they
18824 : : refer to the same variables. */
18825 : 1863 : {
18826 : 1863 : tree use_device_clauses;
18827 : 1863 : tree *pc, *uc = &use_device_clauses;
18828 : 9916 : for (pc = &OMP_CLAUSES (expr); *pc; )
18829 : 8053 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
18830 : 8053 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
18831 : : {
18832 : 1959 : *uc = *pc;
18833 : 1959 : *pc = OMP_CLAUSE_CHAIN (*pc);
18834 : 1959 : uc = &OMP_CLAUSE_CHAIN (*uc);
18835 : : }
18836 : : else
18837 : 6094 : pc = &OMP_CLAUSE_CHAIN (*pc);
18838 : 1863 : *uc = NULL_TREE;
18839 : 1863 : *pc = use_device_clauses;
18840 : 1863 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
18841 : 1863 : OMP_CLAUSES (expr));
18842 : : }
18843 : 1863 : break;
18844 : 8813 : case OMP_TEAMS:
18845 : 8813 : stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
18846 : 8813 : if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18847 : 2671 : gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
18848 : : break;
18849 : 0 : default:
18850 : 0 : gcc_unreachable ();
18851 : : }
18852 : :
18853 : 38958 : gimplify_seq_add_stmt (pre_p, stmt);
18854 : 38958 : *expr_p = NULL_TREE;
18855 : 38958 : }
18856 : :
18857 : : /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
18858 : : target update constructs. */
18859 : :
18860 : : static void
18861 : 11944 : gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
18862 : : {
18863 : 11944 : tree expr = *expr_p;
18864 : 11944 : int kind;
18865 : 11944 : gomp_target *stmt;
18866 : 11944 : enum omp_region_type ort = ORT_WORKSHARE;
18867 : :
18868 : 11944 : switch (TREE_CODE (expr))
18869 : : {
18870 : : case OACC_ENTER_DATA:
18871 : : kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
18872 : : ort = ORT_ACC;
18873 : : break;
18874 : : case OACC_EXIT_DATA:
18875 : : kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
18876 : : ort = ORT_ACC;
18877 : : break;
18878 : : case OACC_UPDATE:
18879 : : kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
18880 : : ort = ORT_ACC;
18881 : : break;
18882 : : case OMP_TARGET_UPDATE:
18883 : : kind = GF_OMP_TARGET_KIND_UPDATE;
18884 : : break;
18885 : : case OMP_TARGET_ENTER_DATA:
18886 : : kind = GF_OMP_TARGET_KIND_ENTER_DATA;
18887 : : break;
18888 : : case OMP_TARGET_EXIT_DATA:
18889 : : kind = GF_OMP_TARGET_KIND_EXIT_DATA;
18890 : : break;
18891 : 0 : default:
18892 : 0 : gcc_unreachable ();
18893 : : }
18894 : :
18895 : 11944 : gimple_seq iterator_loops_seq = NULL;
18896 : 11944 : remove_unused_omp_iterator_vars (&OMP_STANDALONE_CLAUSES (expr));
18897 : 11944 : build_omp_iterators_loops (&OMP_STANDALONE_CLAUSES (expr),
18898 : : &iterator_loops_seq);
18899 : :
18900 : 11944 : gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
18901 : 11944 : ort, TREE_CODE (expr), &iterator_loops_seq);
18902 : 11944 : gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
18903 : 11944 : TREE_CODE (expr), &iterator_loops_seq);
18904 : 11944 : if (TREE_CODE (expr) == OACC_UPDATE
18905 : 11944 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18906 : : OMP_CLAUSE_IF_PRESENT))
18907 : : {
18908 : : /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
18909 : : clause. */
18910 : 123 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18911 : 97 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18912 : 47 : switch (OMP_CLAUSE_MAP_KIND (c))
18913 : : {
18914 : 14 : case GOMP_MAP_FORCE_TO:
18915 : 14 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
18916 : 14 : break;
18917 : 24 : case GOMP_MAP_FORCE_FROM:
18918 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
18919 : 24 : break;
18920 : : default:
18921 : : break;
18922 : : }
18923 : : }
18924 : 11918 : else if (TREE_CODE (expr) == OACC_EXIT_DATA
18925 : 11918 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18926 : : OMP_CLAUSE_FINALIZE))
18927 : : {
18928 : : /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
18929 : : semantics. */
18930 : 77 : bool have_clause = false;
18931 : 281 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18932 : 204 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18933 : 119 : switch (OMP_CLAUSE_MAP_KIND (c))
18934 : : {
18935 : 45 : case GOMP_MAP_FROM:
18936 : 45 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
18937 : 45 : have_clause = true;
18938 : 45 : break;
18939 : 47 : case GOMP_MAP_RELEASE:
18940 : 47 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
18941 : 47 : have_clause = true;
18942 : 47 : break;
18943 : : case GOMP_MAP_TO_PSET:
18944 : : /* Fortran arrays with descriptors must map that descriptor when
18945 : : doing standalone "attach" operations (in OpenACC). In that
18946 : : case GOMP_MAP_TO_PSET appears by itself with no preceding
18947 : : clause (see trans-openmp.cc:gfc_trans_omp_clauses). */
18948 : : break;
18949 : 2 : case GOMP_MAP_POINTER:
18950 : : /* TODO PR92929: we may see these here, but they'll always follow
18951 : : one of the clauses above, and will be handled by libgomp as
18952 : : one group, so no handling required here. */
18953 : 2 : gcc_assert (have_clause);
18954 : : break;
18955 : 24 : case GOMP_MAP_DETACH:
18956 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
18957 : 24 : have_clause = false;
18958 : 24 : break;
18959 : : case GOMP_MAP_STRUCT:
18960 : : case GOMP_MAP_STRUCT_UNORD:
18961 : 24 : have_clause = false;
18962 : : break;
18963 : 0 : default:
18964 : 0 : gcc_unreachable ();
18965 : : }
18966 : : }
18967 : 11944 : stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr),
18968 : : iterator_loops_seq);
18969 : :
18970 : 11944 : gimplify_seq_add_stmt (pre_p, stmt);
18971 : 11944 : *expr_p = NULL_TREE;
18972 : 11944 : }
18973 : :
18974 : : /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
18975 : : stabilized the lhs of the atomic operation as *ADDR. Return true if
18976 : : EXPR is this stabilized form. */
18977 : :
18978 : : static bool
18979 : 36820 : goa_lhs_expr_p (tree expr, tree addr)
18980 : : {
18981 : : /* Also include casts to other type variants. The C front end is fond
18982 : : of adding these for e.g. volatile variables. This is like
18983 : : STRIP_TYPE_NOPS but includes the main variant lookup. */
18984 : 36820 : STRIP_USELESS_TYPE_CONVERSION (expr);
18985 : :
18986 : 36820 : if (INDIRECT_REF_P (expr))
18987 : : {
18988 : 5020 : expr = TREE_OPERAND (expr, 0);
18989 : 5020 : while (expr != addr
18990 : 92 : && (CONVERT_EXPR_P (expr)
18991 : 92 : || TREE_CODE (expr) == NON_LVALUE_EXPR)
18992 : 0 : && TREE_CODE (expr) == TREE_CODE (addr)
18993 : 5020 : && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
18994 : : {
18995 : 0 : expr = TREE_OPERAND (expr, 0);
18996 : 0 : addr = TREE_OPERAND (addr, 0);
18997 : : }
18998 : 5020 : if (expr == addr)
18999 : : return true;
19000 : 92 : return (TREE_CODE (addr) == ADDR_EXPR
19001 : 62 : && TREE_CODE (expr) == ADDR_EXPR
19002 : 92 : && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
19003 : : }
19004 : 31800 : if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
19005 : : return true;
19006 : : return false;
19007 : : }
19008 : :
19009 : : /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
19010 : : expression does not involve the lhs, evaluate it into a temporary.
19011 : : Return 1 if the lhs appeared as a subexpression, 0 if it did not,
19012 : : or -1 if an error was encountered. */
19013 : :
19014 : : static int
19015 : 36820 : goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
19016 : : tree lhs_var, tree &target_expr, bool rhs, int depth)
19017 : : {
19018 : 36820 : tree expr = *expr_p;
19019 : 36820 : int saw_lhs = 0;
19020 : :
19021 : 36820 : if (goa_lhs_expr_p (expr, lhs_addr))
19022 : : {
19023 : 9191 : if (pre_p)
19024 : 8533 : *expr_p = lhs_var;
19025 : 9191 : return 1;
19026 : : }
19027 : 27629 : if (is_gimple_val (expr))
19028 : : return 0;
19029 : :
19030 : : /* Maximum depth of lhs in expression is for the
19031 : : __builtin_clear_padding (...), __builtin_clear_padding (...),
19032 : : __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs; */
19033 : 17297 : if (++depth > 7)
19034 : 16 : goto finish;
19035 : :
19036 : 17281 : switch (TREE_CODE_CLASS (TREE_CODE (expr)))
19037 : : {
19038 : 9660 : case tcc_binary:
19039 : 9660 : case tcc_comparison:
19040 : 9660 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
19041 : : lhs_var, target_expr, true, depth);
19042 : : /* FALLTHRU */
19043 : 11707 : case tcc_unary:
19044 : 11707 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
19045 : : lhs_var, target_expr, true, depth);
19046 : 11707 : break;
19047 : 2982 : case tcc_expression:
19048 : 2982 : switch (TREE_CODE (expr))
19049 : : {
19050 : 836 : case TRUTH_ANDIF_EXPR:
19051 : 836 : case TRUTH_ORIF_EXPR:
19052 : 836 : case TRUTH_AND_EXPR:
19053 : 836 : case TRUTH_OR_EXPR:
19054 : 836 : case TRUTH_XOR_EXPR:
19055 : 836 : case BIT_INSERT_EXPR:
19056 : 836 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19057 : : lhs_addr, lhs_var, target_expr, true,
19058 : : depth);
19059 : : /* FALLTHRU */
19060 : 888 : case TRUTH_NOT_EXPR:
19061 : 888 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19062 : : lhs_addr, lhs_var, target_expr, true,
19063 : : depth);
19064 : 888 : break;
19065 : 550 : case MODIFY_EXPR:
19066 : 550 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19067 : : target_expr, true, depth))
19068 : : break;
19069 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19070 : : lhs_addr, lhs_var, target_expr, true,
19071 : : depth);
19072 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19073 : : lhs_addr, lhs_var, target_expr, false,
19074 : : depth);
19075 : 544 : break;
19076 : : /* FALLTHRU */
19077 : 164 : case ADDR_EXPR:
19078 : 164 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
19079 : : target_expr, true, depth))
19080 : : break;
19081 : 138 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19082 : : lhs_addr, lhs_var, target_expr, false,
19083 : : depth);
19084 : 138 : break;
19085 : : case COMPOUND_EXPR:
19086 : : /* Break out any preevaluations from cp_build_modify_expr. */
19087 : 76 : for (; TREE_CODE (expr) == COMPOUND_EXPR;
19088 : 38 : expr = TREE_OPERAND (expr, 1))
19089 : : {
19090 : : /* Special-case __builtin_clear_padding call before
19091 : : __builtin_memcmp. */
19092 : 38 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
19093 : : {
19094 : 2 : tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
19095 : 2 : if (fndecl
19096 : 2 : && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
19097 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
19098 : 2 : && (!pre_p
19099 : 0 : || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
19100 : : lhs_addr, lhs_var,
19101 : : target_expr, true, depth)))
19102 : : {
19103 : 0 : if (pre_p)
19104 : 0 : *expr_p = expr;
19105 : 0 : saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
19106 : : pre_p, lhs_addr, lhs_var,
19107 : : target_expr, true, depth);
19108 : 0 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
19109 : : pre_p, lhs_addr, lhs_var,
19110 : : target_expr, rhs, depth);
19111 : 0 : return saw_lhs;
19112 : : }
19113 : : }
19114 : :
19115 : 38 : if (pre_p)
19116 : 35 : gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
19117 : : }
19118 : 38 : if (!pre_p)
19119 : 3 : return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
19120 : 3 : target_expr, rhs, depth);
19121 : 35 : *expr_p = expr;
19122 : 35 : return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
19123 : 35 : target_expr, rhs, depth);
19124 : 441 : case COND_EXPR:
19125 : 441 : if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
19126 : : lhs_var, target_expr, true, depth))
19127 : : break;
19128 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19129 : : lhs_addr, lhs_var, target_expr, true,
19130 : : depth);
19131 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
19132 : : lhs_addr, lhs_var, target_expr, true,
19133 : : depth);
19134 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
19135 : : lhs_addr, lhs_var, target_expr, true,
19136 : : depth);
19137 : 428 : break;
19138 : 880 : case TARGET_EXPR:
19139 : 880 : if (TARGET_EXPR_INITIAL (expr))
19140 : : {
19141 : 880 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
19142 : : lhs_var, target_expr, true,
19143 : : depth))
19144 : : break;
19145 : 702 : if (expr == target_expr)
19146 : : saw_lhs = 1;
19147 : : else
19148 : : {
19149 : 702 : saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
19150 : : pre_p, lhs_addr, lhs_var,
19151 : : target_expr, true, depth);
19152 : 702 : if (saw_lhs && target_expr == NULL_TREE && pre_p)
19153 : 26 : target_expr = expr;
19154 : : }
19155 : : }
19156 : : break;
19157 : : default:
19158 : : break;
19159 : : }
19160 : : break;
19161 : 565 : case tcc_reference:
19162 : 565 : if (TREE_CODE (expr) == BIT_FIELD_REF
19163 : 499 : || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
19164 : 362 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
19165 : : lhs_addr, lhs_var, target_expr, true,
19166 : : depth);
19167 : : break;
19168 : 58 : case tcc_vl_exp:
19169 : 58 : if (TREE_CODE (expr) == CALL_EXPR)
19170 : : {
19171 : 58 : if (tree fndecl = get_callee_fndecl (expr))
19172 : 58 : if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
19173 : : BUILT_IN_MEMCMP))
19174 : : {
19175 : 56 : int nargs = call_expr_nargs (expr);
19176 : 224 : for (int i = 0; i < nargs; i++)
19177 : 168 : saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
19178 : : pre_p, lhs_addr, lhs_var,
19179 : : target_expr, true, depth);
19180 : : }
19181 : : }
19182 : : break;
19183 : : default:
19184 : : break;
19185 : : }
19186 : :
19187 : 17259 : finish:
19188 : 17259 : if (saw_lhs == 0 && pre_p)
19189 : : {
19190 : 3498 : enum gimplify_status gs;
19191 : 3498 : if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
19192 : : {
19193 : 0 : gimplify_stmt (&expr, pre_p);
19194 : 0 : return saw_lhs;
19195 : : }
19196 : 3498 : else if (rhs)
19197 : 3320 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
19198 : : else
19199 : 178 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
19200 : 3498 : if (gs != GS_ALL_DONE)
19201 : 36820 : saw_lhs = -1;
19202 : : }
19203 : :
19204 : : return saw_lhs;
19205 : : }
19206 : :
19207 : : /* Gimplify an OMP_ATOMIC statement. */
19208 : :
19209 : : static enum gimplify_status
19210 : 10249 : gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
19211 : : {
19212 : 10249 : tree addr = TREE_OPERAND (*expr_p, 0);
19213 : 10249 : tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
19214 : 10249 : ? NULL : TREE_OPERAND (*expr_p, 1);
19215 : 10249 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
19216 : 10249 : tree tmp_load;
19217 : 10249 : gomp_atomic_load *loadstmt;
19218 : 10249 : gomp_atomic_store *storestmt;
19219 : 10249 : tree target_expr = NULL_TREE;
19220 : :
19221 : 10249 : tmp_load = create_tmp_reg (type);
19222 : 10249 : if (rhs
19223 : 10249 : && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
19224 : : true, 0) < 0)
19225 : : return GS_ERROR;
19226 : :
19227 : 10249 : if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
19228 : : != GS_ALL_DONE)
19229 : : return GS_ERROR;
19230 : :
19231 : 10249 : loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
19232 : 10249 : OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19233 : 10249 : gimplify_seq_add_stmt (pre_p, loadstmt);
19234 : 10249 : if (rhs)
19235 : : {
19236 : : /* BIT_INSERT_EXPR is not valid for non-integral bitfield
19237 : : representatives. Use BIT_FIELD_REF on the lhs instead. */
19238 : 9068 : tree rhsarg = rhs;
19239 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19240 : 428 : rhsarg = TREE_OPERAND (rhs, 1);
19241 : 9068 : if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
19242 : 9068 : && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
19243 : : {
19244 : 32 : tree bitpos = TREE_OPERAND (rhsarg, 2);
19245 : 32 : tree op1 = TREE_OPERAND (rhsarg, 1);
19246 : 32 : tree bitsize;
19247 : 32 : tree tmp_store = tmp_load;
19248 : 32 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
19249 : 12 : tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
19250 : 32 : if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
19251 : 32 : bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
19252 : : else
19253 : 0 : bitsize = TYPE_SIZE (TREE_TYPE (op1));
19254 : 32 : gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
19255 : 32 : tree t = build2_loc (EXPR_LOCATION (rhsarg),
19256 : : MODIFY_EXPR, void_type_node,
19257 : 32 : build3_loc (EXPR_LOCATION (rhsarg),
19258 : 32 : BIT_FIELD_REF, TREE_TYPE (op1),
19259 : : tmp_store, bitsize, bitpos), op1);
19260 : 32 : if (TREE_CODE (rhs) == COND_EXPR)
19261 : 16 : t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
19262 : 16 : TREE_OPERAND (rhs, 0), t, void_node);
19263 : 32 : gimplify_and_add (t, pre_p);
19264 : 32 : rhs = tmp_store;
19265 : : }
19266 : 9068 : bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
19267 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
19268 : 412 : gimplify_ctxp->allow_rhs_cond_expr = true;
19269 : 9068 : enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
19270 : : is_gimple_val, fb_rvalue);
19271 : 9068 : gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
19272 : 9068 : if (gs != GS_ALL_DONE)
19273 : : return GS_ERROR;
19274 : : }
19275 : :
19276 : 10249 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
19277 : 1181 : rhs = tmp_load;
19278 : 10249 : storestmt
19279 : 10249 : = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
19280 : 10249 : if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
19281 : : {
19282 : 37 : gimple_omp_atomic_set_weak (loadstmt);
19283 : 37 : gimple_omp_atomic_set_weak (storestmt);
19284 : : }
19285 : 10249 : gimplify_seq_add_stmt (pre_p, storestmt);
19286 : 10249 : switch (TREE_CODE (*expr_p))
19287 : : {
19288 : 2065 : case OMP_ATOMIC_READ:
19289 : 2065 : case OMP_ATOMIC_CAPTURE_OLD:
19290 : 2065 : *expr_p = tmp_load;
19291 : 2065 : gimple_omp_atomic_set_need_value (loadstmt);
19292 : 2065 : break;
19293 : 868 : case OMP_ATOMIC_CAPTURE_NEW:
19294 : 868 : *expr_p = rhs;
19295 : 868 : gimple_omp_atomic_set_need_value (storestmt);
19296 : 868 : break;
19297 : 7316 : default:
19298 : 7316 : *expr_p = NULL;
19299 : 7316 : break;
19300 : : }
19301 : :
19302 : : return GS_ALL_DONE;
19303 : : }
19304 : :
19305 : : /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
19306 : : body, and adding some EH bits. */
19307 : :
19308 : : static enum gimplify_status
19309 : 478 : gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
19310 : : {
19311 : 478 : tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
19312 : 478 : gimple *body_stmt;
19313 : 478 : gtransaction *trans_stmt;
19314 : 478 : gimple_seq body = NULL;
19315 : 478 : int subcode = 0;
19316 : :
19317 : : /* Wrap the transaction body in a BIND_EXPR so we have a context
19318 : : where to put decls for OMP. */
19319 : 478 : if (TREE_CODE (tbody) != BIND_EXPR)
19320 : : {
19321 : 439 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
19322 : 439 : TREE_SIDE_EFFECTS (bind) = 1;
19323 : 439 : SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
19324 : 439 : TRANSACTION_EXPR_BODY (expr) = bind;
19325 : : }
19326 : :
19327 : 478 : push_gimplify_context ();
19328 : 478 : temp = voidify_wrapper_expr (*expr_p, NULL);
19329 : :
19330 : 478 : body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
19331 : 478 : pop_gimplify_context (body_stmt);
19332 : :
19333 : 478 : trans_stmt = gimple_build_transaction (body);
19334 : 478 : if (TRANSACTION_EXPR_OUTER (expr))
19335 : : subcode = GTMA_IS_OUTER;
19336 : 447 : else if (TRANSACTION_EXPR_RELAXED (expr))
19337 : 81 : subcode = GTMA_IS_RELAXED;
19338 : 478 : gimple_transaction_set_subcode (trans_stmt, subcode);
19339 : :
19340 : 478 : gimplify_seq_add_stmt (pre_p, trans_stmt);
19341 : :
19342 : 478 : if (temp)
19343 : : {
19344 : 76 : *expr_p = temp;
19345 : 76 : return GS_OK;
19346 : : }
19347 : :
19348 : 402 : *expr_p = NULL_TREE;
19349 : 402 : return GS_ALL_DONE;
19350 : : }
19351 : :
19352 : : /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
19353 : : is the OMP_BODY of the original EXPR (which has already been
19354 : : gimplified so it's not present in the EXPR).
19355 : :
19356 : : Return the gimplified GIMPLE_OMP_ORDERED tuple. */
19357 : :
19358 : : static gimple *
19359 : 1940 : gimplify_omp_ordered (tree expr, gimple_seq body)
19360 : : {
19361 : 1940 : tree c, decls;
19362 : 1940 : int failures = 0;
19363 : 1940 : unsigned int i;
19364 : 1940 : tree source_c = NULL_TREE;
19365 : 1940 : tree sink_c = NULL_TREE;
19366 : :
19367 : 1940 : if (gimplify_omp_ctxp)
19368 : : {
19369 : 3271 : for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
19370 : 1505 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19371 : 1505 : && gimplify_omp_ctxp->loop_iter_var.is_empty ())
19372 : : {
19373 : 74 : error_at (OMP_CLAUSE_LOCATION (c),
19374 : : "%<ordered%> construct with %qs clause must be "
19375 : : "closely nested inside a loop with %<ordered%> clause",
19376 : 74 : OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
19377 : 74 : failures++;
19378 : : }
19379 : 1431 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19380 : 1431 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
19381 : : {
19382 : 637 : bool fail = false;
19383 : 637 : sink_c = c;
19384 : 637 : if (OMP_CLAUSE_DECL (c) == NULL_TREE)
19385 : 72 : continue; /* omp_cur_iteration - 1 */
19386 : 565 : for (decls = OMP_CLAUSE_DECL (c), i = 0;
19387 : 3191 : decls && TREE_CODE (decls) == TREE_LIST;
19388 : 2626 : decls = TREE_CHAIN (decls), ++i)
19389 : 2626 : if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
19390 : 4 : continue;
19391 : 5244 : else if (TREE_VALUE (decls)
19392 : 2622 : != gimplify_omp_ctxp->loop_iter_var[2 * i])
19393 : : {
19394 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
19395 : : "variable %qE is not an iteration "
19396 : : "of outermost loop %d, expected %qE",
19397 : 8 : TREE_VALUE (decls), i + 1,
19398 : 8 : gimplify_omp_ctxp->loop_iter_var[2 * i]);
19399 : 8 : fail = true;
19400 : 8 : failures++;
19401 : : }
19402 : : else
19403 : 5228 : TREE_VALUE (decls)
19404 : 2614 : = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
19405 : 1126 : if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
19406 : : {
19407 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
19408 : : "number of variables in %qs clause with "
19409 : : "%<sink%> modifier does not match number of "
19410 : : "iteration variables",
19411 : 16 : OMP_CLAUSE_DOACROSS_DEPEND (c)
19412 : : ? "depend" : "doacross");
19413 : 16 : failures++;
19414 : : }
19415 : : }
19416 : 794 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
19417 : 794 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
19418 : : {
19419 : 470 : if (source_c)
19420 : : {
19421 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
19422 : : "more than one %qs clause with %<source%> "
19423 : : "modifier on an %<ordered%> construct",
19424 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c)
19425 : : ? "depend" : "doacross");
19426 : 4 : failures++;
19427 : : }
19428 : : else
19429 : : source_c = c;
19430 : : }
19431 : : }
19432 : 1940 : if (source_c && sink_c)
19433 : : {
19434 : 4 : error_at (OMP_CLAUSE_LOCATION (source_c),
19435 : : "%qs clause with %<source%> modifier specified "
19436 : : "together with %qs clauses with %<sink%> modifier "
19437 : : "on the same construct",
19438 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
19439 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
19440 : 4 : failures++;
19441 : : }
19442 : :
19443 : 1940 : if (failures)
19444 : 102 : return gimple_build_nop ();
19445 : 1838 : return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
19446 : : }
19447 : :
19448 : : /* Gimplify an OMP_INTEROP statement. */
19449 : :
19450 : : static enum gimplify_status
19451 : 612 : gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
19452 : : {
19453 : 612 : tree expr = *expr_p;
19454 : :
19455 : 612 : gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
19456 : : OMP_INTEROP);
19457 : 612 : gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
19458 : 612 : gimplify_seq_add_stmt (pre_p, stmt);
19459 : 612 : *expr_p = NULL_TREE;
19460 : 612 : return GS_ALL_DONE;
19461 : : }
19462 : :
19463 : : /* Callback for walk_tree to find an IFN_GOMP_DISPATCH. */
19464 : :
19465 : : static tree
19466 : 3082 : find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
19467 : : {
19468 : 3082 : tree t = *tp;
19469 : :
19470 : 3082 : if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
19471 : 1408 : return *(tree *) modify ? *(tree *) modify : *tp;
19472 : :
19473 : 2236 : if (TREE_CODE (t) == MODIFY_EXPR)
19474 : 507 : *(tree *) modify = *tp;
19475 : :
19476 : : return NULL_TREE;
19477 : : }
19478 : :
19479 : : /* Gimplify an OMP_DISPATCH construct. */
19480 : :
19481 : : static enum gimplify_status
19482 : 846 : gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
19483 : : {
19484 : 846 : tree expr = *expr_p;
19485 : 846 : gimple_seq body = NULL;
19486 : :
19487 : 846 : gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
19488 : : OMP_DISPATCH);
19489 : 846 : push_gimplify_context ();
19490 : :
19491 : : // If device clause, adjust ICV
19492 : 846 : tree device
19493 : 846 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
19494 : : // If no device clause exists but an interop clause with a single list
19495 : : // item, use it to obtain the device number.
19496 : 846 : if (device)
19497 : 272 : device = OMP_CLAUSE_DEVICE_ID (device);
19498 : : else
19499 : : {
19500 : 574 : tree first_interop_obj
19501 : 574 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
19502 : 574 : if (first_interop_obj)
19503 : 96 : for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
19504 : 8 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
19505 : : {
19506 : : first_interop_obj = NULL_TREE;
19507 : : break;
19508 : : }
19509 : 95 : if (first_interop_obj)
19510 : : {
19511 : 88 : device = create_tmp_var (integer_type_node);
19512 : 88 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
19513 : : OMP_CLAUSE_DEVICE);
19514 : 88 : OMP_CLAUSE_DEVICE_ID (c) = device;
19515 : 88 : TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
19516 : 88 : TREE_CHAIN (first_interop_obj) = c;
19517 : 88 : first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
19518 : : /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL); */
19519 : 88 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
19520 : 88 : fn = build_call_expr (fn, 3, first_interop_obj,
19521 : : build_int_cst (integer_type_node, -5),
19522 : : null_pointer_node);
19523 : 88 : gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
19524 : : }
19525 : : }
19526 : 846 : tree saved_device_icv = NULL_TREE;
19527 : 846 : if (device
19528 : 846 : && (TREE_CODE (device) != INTEGER_CST
19529 : 703 : || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
19530 : : {
19531 : : // Save current default-device-var ICV
19532 : 360 : saved_device_icv = create_tmp_var (integer_type_node);
19533 : 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
19534 : 360 : gcall *call = gimple_build_call (fn, 0);
19535 : 360 : gimple_call_set_lhs (call, saved_device_icv);
19536 : 360 : gimplify_seq_add_stmt (&body, call);
19537 : :
19538 : : // Set default device
19539 : 360 : fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19540 : 360 : call = gimple_build_call (fn, 1, device);
19541 : 360 : gimplify_seq_add_stmt (&body, call);
19542 : : }
19543 : :
19544 : : // If the novariants and nocontext clauses are not compile-time constants,
19545 : : // we need to generate code for all possible cases:
19546 : : // if (novariants) // implies nocontext
19547 : : // base()
19548 : : // else if (nocontext)
19549 : : // variant1()
19550 : : // else
19551 : : // variant2()
19552 : 846 : tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
19553 : 846 : if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
19554 : 28 : dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
19555 : 846 : tree dispatch_body = *dispatch_body_p;
19556 : :
19557 : : // Look for IFN_GOMP_DISPATCH and extract the base function call
19558 : 846 : tree base_call_expr = NULL_TREE;
19559 : 846 : if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
19560 : 243 : for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
19561 : 187 : tsi_next (&tsi))
19562 : : {
19563 : 243 : tree modify = NULL_TREE;
19564 : 243 : tree stmt = tsi_stmt (tsi);
19565 : 243 : base_call_expr
19566 : 243 : = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
19567 : 243 : if (base_call_expr != NULL_TREE)
19568 : : break;
19569 : : }
19570 : : else
19571 : : {
19572 : 790 : tree modify = NULL_TREE;
19573 : 790 : base_call_expr
19574 : 790 : = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
19575 : : }
19576 : 846 : gcc_assert (base_call_expr != NULL_TREE);
19577 : :
19578 : 846 : tree dst = NULL_TREE;
19579 : 846 : if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
19580 : : {
19581 : 284 : dst = TREE_OPERAND (base_call_expr, 0);
19582 : 284 : base_call_expr = TREE_OPERAND (base_call_expr, 1);
19583 : : }
19584 : :
19585 : 874 : while (TREE_CODE (base_call_expr) == FLOAT_EXPR
19586 : : || TREE_CODE (base_call_expr) == CONVERT_EXPR
19587 : : || TREE_CODE (base_call_expr) == COMPLEX_EXPR
19588 : : || TREE_CODE (base_call_expr) == INDIRECT_REF
19589 : 874 : || TREE_CODE (base_call_expr) == NOP_EXPR)
19590 : 28 : base_call_expr = TREE_OPERAND (base_call_expr, 0);
19591 : :
19592 : 846 : gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
19593 : 846 : base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
19594 : :
19595 : 846 : tree base_fndecl = get_callee_fndecl (base_call_expr);
19596 : 846 : if (base_fndecl != NULL_TREE)
19597 : : {
19598 : 838 : if (DECL_VIRTUAL_P (base_fndecl))
19599 : : {
19600 : 6 : error_at (
19601 : 3 : EXPR_LOCATION (base_call_expr),
19602 : : "%qD is a virtual function but only a direct call is allowed "
19603 : : "in a dispatch construct",
19604 : 3 : DECL_NAME (base_fndecl));
19605 : : }
19606 : :
19607 : : /* We are not actually going to expand the variant call or use
19608 : : the result of omp_get_dynamic candidates here; only check that
19609 : : it does not trivially resolve to a call to the base function
19610 : : so that we can avoid some extra work in building code that's
19611 : : not needed in that case. */
19612 : 838 : tree construct_context = omp_get_construct_context ();
19613 : 838 : vec<struct omp_variant> all_candidates
19614 : 838 : = omp_declare_variant_candidates (base_fndecl, construct_context);
19615 : 838 : gcc_assert (!all_candidates.is_empty ());
19616 : 838 : vec<struct omp_variant> candidates
19617 : 838 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19618 : 838 : tree variant_fndecl
19619 : 1664 : = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
19620 : :
19621 : 838 : if (base_fndecl != variant_fndecl
19622 : 838 : && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
19623 : : {
19624 : 22 : tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
19625 : 22 : novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
19626 : 59 : for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
19627 : : {
19628 : 37 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
19629 : 37 : && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
19630 : : {
19631 : 16 : gcc_assert (novariants_cond == NULL_TREE);
19632 : 16 : novariants_clause = c;
19633 : 16 : novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
19634 : : }
19635 : 21 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
19636 : 21 : && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
19637 : : {
19638 : 16 : gcc_assert (nocontext_cond == NULL_TREE);
19639 : 16 : nocontext_clause = c;
19640 : 16 : nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
19641 : : }
19642 : : }
19643 : 22 : gcc_assert (novariants_cond != NULL_TREE
19644 : : || nocontext_cond != NULL_TREE);
19645 : :
19646 : 22 : enum gimplify_status ret
19647 : 22 : = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
19648 : : fb_rvalue);
19649 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19650 : 0 : return ret;
19651 : 22 : ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
19652 : : fb_rvalue);
19653 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19654 : : return ret;
19655 : :
19656 : 22 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19657 : :
19658 : 22 : if (novariants_cond != NULL_TREE)
19659 : : {
19660 : 16 : tree base_label = create_artificial_label (UNKNOWN_LOCATION);
19661 : 16 : tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
19662 : 16 : gcond *novariants_cond_stmt
19663 : 16 : = gimple_build_cond_from_tree (novariants_cond, base_label,
19664 : : cond_label);
19665 : 16 : gimplify_seq_add_stmt (&body, novariants_cond_stmt);
19666 : :
19667 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
19668 : 16 : tree base_call_expr2 = copy_node (base_call_expr);
19669 : 16 : base_call_expr2
19670 : 16 : = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
19671 : : IFN_GOMP_DISPATCH,
19672 : 16 : TREE_TYPE (base_call_expr2), 1,
19673 : : base_call_expr2);
19674 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19675 : : {
19676 : 16 : base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19677 : : base_call_expr2);
19678 : : }
19679 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19680 : 16 : = boolean_true_node;
19681 : 16 : gimplify_and_add (base_call_expr2, &body);
19682 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19683 : :
19684 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19685 : 16 : = boolean_false_node;
19686 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
19687 : : }
19688 : :
19689 : 22 : if (nocontext_cond != NULL_TREE)
19690 : : {
19691 : 16 : tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
19692 : 16 : tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
19693 : 16 : gcond *nocontext_cond_stmt
19694 : 16 : = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
19695 : : variant2_label);
19696 : 16 : gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
19697 : :
19698 : 32 : gimplify_seq_add_stmt (&body,
19699 : 16 : gimple_build_label (variant1_label));
19700 : 16 : tree variant_call_expr = copy_node (base_call_expr);
19701 : 32 : variant_call_expr = build_call_expr_internal_loc (
19702 : 16 : EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
19703 : 16 : TREE_TYPE (variant_call_expr), 1, variant_call_expr);
19704 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19705 : : {
19706 : 16 : variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19707 : : variant_call_expr);
19708 : : }
19709 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
19710 : 16 : gimplify_and_add (variant_call_expr, &body);
19711 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19712 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
19713 : 32 : gimplify_seq_add_stmt (&body,
19714 : 16 : gimple_build_label (variant2_label));
19715 : : }
19716 : :
19717 : 22 : tree variant_call_expr = base_call_expr;
19718 : 22 : variant_call_expr
19719 : 22 : = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
19720 : : IFN_GOMP_DISPATCH,
19721 : 22 : TREE_TYPE (variant_call_expr), 1,
19722 : : variant_call_expr);
19723 : 22 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19724 : : {
19725 : 22 : variant_call_expr
19726 : 22 : = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
19727 : : }
19728 : 22 : gimplify_and_add (variant_call_expr, &body);
19729 : 22 : gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
19730 : : }
19731 : : else
19732 : 816 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19733 : : }
19734 : : else
19735 : 8 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19736 : :
19737 : : // Restore default-device-var ICV
19738 : 846 : if (saved_device_icv != NULL_TREE)
19739 : : {
19740 : 360 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19741 : 360 : gcall *call = gimple_build_call (fn, 1, saved_device_icv);
19742 : 360 : gimplify_seq_add_stmt (&body, call);
19743 : : }
19744 : :
19745 : : // Wrap dispatch body into a bind
19746 : 846 : gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
19747 : 846 : pop_gimplify_context (bind);
19748 : :
19749 : : // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
19750 : : // call to gimplify_adjust_omp_clauses
19751 : 846 : gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
19752 : 846 : if (ctx != NULL)
19753 : : {
19754 : 846 : gcc_assert (ctx->code == OMP_DISPATCH);
19755 : 846 : gimplify_omp_ctxp = ctx->outer_context;
19756 : 846 : delete_omp_context (ctx);
19757 : : }
19758 : :
19759 : : // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
19760 : : // has been handled above, and depend as the front end handled it by inserting
19761 : : // taskwait.
19762 : 846 : tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
19763 : 1433 : for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
19764 : : {
19765 : 985 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
19766 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
19767 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
19768 : : {
19769 : 398 : *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
19770 : 398 : break;
19771 : : }
19772 : : else
19773 : 587 : dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
19774 : : }
19775 : :
19776 : 846 : gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
19777 : 846 : gimplify_seq_add_stmt (pre_p, stmt);
19778 : 846 : *expr_p = NULL_TREE;
19779 : 846 : return GS_ALL_DONE;
19780 : : }
19781 : :
19782 : : /* Expand a metadirective that has been resolved at gimplification time
19783 : : into the candidate directive variants in CANDIDATES. */
19784 : :
19785 : : static enum gimplify_status
19786 : 158 : expand_omp_metadirective (vec<struct omp_variant> &candidates,
19787 : : gimple_seq *pre_p)
19788 : : {
19789 : 158 : auto_vec<tree> selectors;
19790 : 158 : auto_vec<tree> directive_labels;
19791 : 158 : auto_vec<gimple_seq> directive_bodies;
19792 : 158 : tree body_label = NULL_TREE;
19793 : 158 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19794 : :
19795 : : /* Construct bodies for each candidate. */
19796 : 381 : for (unsigned i = 0; i < candidates.length(); i++)
19797 : : {
19798 : 223 : struct omp_variant &candidate = candidates[i];
19799 : 223 : gimple_seq body = NULL;
19800 : :
19801 : 223 : selectors.safe_push (omp_dynamic_cond (candidate.selector,
19802 : : find_supercontext ()));
19803 : 223 : directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19804 : :
19805 : 223 : gimplify_seq_add_stmt (&body,
19806 : 223 : gimple_build_label (directive_labels.last ()));
19807 : 223 : if (candidate.alternative != NULL_TREE)
19808 : 223 : gimplify_stmt (&candidate.alternative, &body);
19809 : 223 : if (candidate.body != NULL_TREE)
19810 : : {
19811 : 34 : if (body_label != NULL_TREE)
19812 : 0 : gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
19813 : : else
19814 : : {
19815 : 34 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19816 : 34 : gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
19817 : 34 : gimplify_stmt (&candidate.body, &body);
19818 : : }
19819 : : }
19820 : :
19821 : 223 : directive_bodies.safe_push (body);
19822 : : }
19823 : :
19824 : 158 : auto_vec<tree> cond_labels;
19825 : :
19826 : 158 : cond_labels.safe_push (NULL_TREE);
19827 : 352 : for (unsigned i = 1; i < candidates.length () - 1; i++)
19828 : 18 : cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19829 : 158 : if (candidates.length () > 1)
19830 : 47 : cond_labels.safe_push (directive_labels.last ());
19831 : :
19832 : : /* Generate conditionals to test each dynamic selector in turn, executing
19833 : : the directive candidate if successful. */
19834 : 446 : for (unsigned i = 0; i < candidates.length () - 1; i++)
19835 : : {
19836 : 65 : if (i != 0)
19837 : 18 : gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
19838 : :
19839 : 65 : enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
19840 : : is_gimple_val, fb_rvalue);
19841 : 65 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19842 : : return ret;
19843 : :
19844 : 65 : gcond *cond_stmt
19845 : 65 : = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
19846 : 65 : cond_labels[i + 1]);
19847 : :
19848 : 65 : gimplify_seq_add_stmt (pre_p, cond_stmt);
19849 : 65 : gimplify_seq_add_seq (pre_p, directive_bodies[i]);
19850 : 65 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
19851 : : }
19852 : :
19853 : 158 : gimplify_seq_add_seq (pre_p, directive_bodies.last ());
19854 : 158 : gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
19855 : :
19856 : 158 : return GS_ALL_DONE;
19857 : 158 : }
19858 : :
19859 : : /* Expand a variant construct that requires late resolution in the ompdevlow
19860 : : pass. It's a bit easier to do this in tree form and then gimplify that,
19861 : : than to emit gimple. The output is going to look something like:
19862 : :
19863 : : switch_var = OMP_NEXT_VARIANT (0, state);
19864 : : loop_label:
19865 : : switch (switch_var)
19866 : : {
19867 : : case 1:
19868 : : if (dynamic_selector_predicate_1)
19869 : : {
19870 : : alternative_1;
19871 : : goto end_label;
19872 : : }
19873 : : else
19874 : : {
19875 : : switch_var = OMP_NEXT_VARIANT (1, state);
19876 : : goto loop_label;
19877 : : }
19878 : : case 2:
19879 : : ...
19880 : : }
19881 : : end_label:
19882 : :
19883 : : OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
19884 : : index of the next variant to try, after late resolution. */
19885 : :
19886 : : static tree
19887 : 16 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
19888 : : tree construct_context)
19889 : : {
19890 : 16 : tree body_label = NULL_TREE;
19891 : 16 : tree standalone_body = NULL_TREE;
19892 : 16 : tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
19893 : 16 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19894 : 32 : tree selectors = make_tree_vec (all_candidates.length ());
19895 : 16 : tree switch_body = NULL_TREE;
19896 : 16 : tree switch_var = create_tmp_var (integer_type_node, "variant");
19897 : 16 : tree state = tree_cons (NULL_TREE, construct_context, selectors);
19898 : :
19899 : 92 : for (unsigned int i = 0; i < all_candidates.length (); i++)
19900 : : {
19901 : 76 : tree selector = all_candidates[i].selector;
19902 : 76 : tree alternative = all_candidates[i].alternative;
19903 : 76 : tree body = all_candidates[i].body;
19904 : 76 : TREE_VEC_ELT (selectors, i) = selector;
19905 : :
19906 : : /* Case label. Numbering is 1-based. */
19907 : 76 : tree case_val = build_int_cst (integer_type_node, i + 1);
19908 : 76 : tree case_label
19909 : 76 : = build_case_label (case_val, NULL_TREE,
19910 : : create_artificial_label (UNKNOWN_LOCATION));
19911 : 76 : append_to_statement_list (case_label, &switch_body);
19912 : :
19913 : : /* The actual body of the variant. */
19914 : 76 : tree variant_body = NULL_TREE;
19915 : 76 : append_to_statement_list (alternative, &variant_body);
19916 : :
19917 : 76 : if (body != NULL_TREE)
19918 : : {
19919 : 0 : if (standalone_body == NULL)
19920 : : {
19921 : 0 : standalone_body = body;
19922 : 0 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19923 : : }
19924 : 0 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19925 : : body_label),
19926 : : &variant_body);
19927 : : }
19928 : : else
19929 : 76 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19930 : : end_label),
19931 : : &variant_body);
19932 : :
19933 : : /* If this is a dynamic selector, wrap variant_body with a conditional.
19934 : : If the predicate doesn't match, the else clause sets switch_var and
19935 : : jumps to loop_var to try again. */
19936 : 76 : tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
19937 : 76 : if (dynamic_selector)
19938 : : {
19939 : 20 : tree else_stmt = NULL_TREE;
19940 : 20 : tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
19941 : : case_val, state);
19942 : 20 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19943 : : switch_var, next),
19944 : : &else_stmt);
19945 : 20 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19946 : : loop_label),
19947 : : &else_stmt);
19948 : 20 : variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
19949 : : variant_body, else_stmt);
19950 : : }
19951 : 76 : append_to_statement_list (variant_body, &switch_body);
19952 : : }
19953 : :
19954 : : /* Put it all together. */
19955 : 16 : tree result = NULL_TREE;
19956 : 16 : tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
19957 : : state);
19958 : 16 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19959 : : switch_var, first),
19960 : : &result);
19961 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
19962 : : &result);
19963 : 16 : append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
19964 : : switch_var, switch_body),
19965 : : &result);
19966 : 16 : if (standalone_body)
19967 : : {
19968 : 0 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
19969 : : body_label),
19970 : : &result);
19971 : 0 : append_to_statement_list (standalone_body, &result);
19972 : : }
19973 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
19974 : : &result);
19975 : 16 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
19976 : 16 : return result;
19977 : : }
19978 : :
19979 : :
19980 : : /* Gimplify an OMP_METADIRECTIVE construct. EXPR is the tree version.
19981 : : The metadirective will be resolved at this point if possible, otherwise
19982 : : a GIMPLE_OMP_VARIANT_CONSTRUCT is created. */
19983 : :
19984 : : static enum gimplify_status
19985 : 162 : gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
19986 : : bool (*) (tree), fallback_t)
19987 : : {
19988 : : /* Try to resolve the metadirective. */
19989 : 162 : tree construct_context = omp_get_construct_context ();
19990 : 162 : vec<struct omp_variant> all_candidates
19991 : 162 : = omp_metadirective_candidates (*expr_p, construct_context);
19992 : 162 : vec<struct omp_variant> candidates
19993 : 162 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19994 : 162 : if (!candidates.is_empty ())
19995 : 158 : return expand_omp_metadirective (candidates, pre_p);
19996 : :
19997 : : /* The metadirective cannot be resolved yet. Turn it into a loop with
19998 : : a nested switch statement, using OMP_NEXT_VARIANT to set the control
19999 : : variable for the switch. */
20000 : 4 : *expr_p = expand_late_variant_directive (all_candidates, construct_context);
20001 : 4 : return GS_OK;
20002 : : }
20003 : :
20004 : : /* Gimplify an OMP_DECLARE_MAPPER node (by just removing it). */
20005 : :
20006 : : static enum gimplify_status
20007 : 0 : gimplify_omp_declare_mapper (tree *expr_p)
20008 : : {
20009 : 0 : *expr_p = NULL_TREE;
20010 : 0 : return GS_ALL_DONE;
20011 : : }
20012 : :
20013 : : /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
20014 : : expression produces a value to be used as an operand inside a GIMPLE
20015 : : statement, the value will be stored back in *EXPR_P. This value will
20016 : : be a tree of class tcc_declaration, tcc_constant, tcc_reference or
20017 : : an SSA_NAME. The corresponding sequence of GIMPLE statements is
20018 : : emitted in PRE_P and POST_P.
20019 : :
20020 : : Additionally, this process may overwrite parts of the input
20021 : : expression during gimplification. Ideally, it should be
20022 : : possible to do non-destructive gimplification.
20023 : :
20024 : : EXPR_P points to the GENERIC expression to convert to GIMPLE. If
20025 : : the expression needs to evaluate to a value to be used as
20026 : : an operand in a GIMPLE statement, this value will be stored in
20027 : : *EXPR_P on exit. This happens when the caller specifies one
20028 : : of fb_lvalue or fb_rvalue fallback flags.
20029 : :
20030 : : PRE_P will contain the sequence of GIMPLE statements corresponding
20031 : : to the evaluation of EXPR and all the side-effects that must
20032 : : be executed before the main expression. On exit, the last
20033 : : statement of PRE_P is the core statement being gimplified. For
20034 : : instance, when gimplifying 'if (++a)' the last statement in
20035 : : PRE_P will be 'if (t.1)' where t.1 is the result of
20036 : : pre-incrementing 'a'.
20037 : :
20038 : : POST_P will contain the sequence of GIMPLE statements corresponding
20039 : : to the evaluation of all the side-effects that must be executed
20040 : : after the main expression. If this is NULL, the post
20041 : : side-effects are stored at the end of PRE_P.
20042 : :
20043 : : The reason why the output is split in two is to handle post
20044 : : side-effects explicitly. In some cases, an expression may have
20045 : : inner and outer post side-effects which need to be emitted in
20046 : : an order different from the one given by the recursive
20047 : : traversal. For instance, for the expression (*p--)++ the post
20048 : : side-effects of '--' must actually occur *after* the post
20049 : : side-effects of '++'. However, gimplification will first visit
20050 : : the inner expression, so if a separate POST sequence was not
20051 : : used, the resulting sequence would be:
20052 : :
20053 : : 1 t.1 = *p
20054 : : 2 p = p - 1
20055 : : 3 t.2 = t.1 + 1
20056 : : 4 *p = t.2
20057 : :
20058 : : However, the post-decrement operation in line #2 must not be
20059 : : evaluated until after the store to *p at line #4, so the
20060 : : correct sequence should be:
20061 : :
20062 : : 1 t.1 = *p
20063 : : 2 t.2 = t.1 + 1
20064 : : 3 *p = t.2
20065 : : 4 p = p - 1
20066 : :
20067 : : So, by specifying a separate post queue, it is possible
20068 : : to emit the post side-effects in the correct order.
20069 : : If POST_P is NULL, an internal queue will be used. Before
20070 : : returning to the caller, the sequence POST_P is appended to
20071 : : the main output sequence PRE_P.
20072 : :
20073 : : GIMPLE_TEST_F points to a function that takes a tree T and
20074 : : returns nonzero if T is in the GIMPLE form requested by the
20075 : : caller. The GIMPLE predicates are in gimple.cc.
20076 : :
20077 : : FALLBACK tells the function what sort of a temporary we want if
20078 : : gimplification cannot produce an expression that complies with
20079 : : GIMPLE_TEST_F.
20080 : :
20081 : : fb_none means that no temporary should be generated
20082 : : fb_rvalue means that an rvalue is OK to generate
20083 : : fb_lvalue means that an lvalue is OK to generate
20084 : : fb_either means that either is OK, but an lvalue is preferable.
20085 : : fb_mayfail means that gimplification may fail (in which case
20086 : : GS_ERROR will be returned)
20087 : :
20088 : : The return value is either GS_ERROR or GS_ALL_DONE, since this
20089 : : function iterates until EXPR is completely gimplified or an error
20090 : : occurs. */
20091 : :
20092 : : enum gimplify_status
20093 : 517275751 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
20094 : : bool (*gimple_test_f) (tree), fallback_t fallback)
20095 : : {
20096 : 517275751 : tree tmp;
20097 : 517275751 : gimple_seq internal_pre = NULL;
20098 : 517275751 : gimple_seq internal_post = NULL;
20099 : 517275751 : tree save_expr;
20100 : 517275751 : bool is_statement;
20101 : 517275751 : location_t saved_location;
20102 : 517275751 : enum gimplify_status ret;
20103 : 517275751 : gimple_stmt_iterator pre_last_gsi, post_last_gsi;
20104 : 517275751 : tree label;
20105 : :
20106 : 517275751 : save_expr = *expr_p;
20107 : 517275751 : if (save_expr == NULL_TREE)
20108 : : return GS_ALL_DONE;
20109 : :
20110 : : /* If we are gimplifying a top-level statement, PRE_P must be valid. */
20111 : 466159600 : is_statement = gimple_test_f == is_gimple_stmt;
20112 : 466159600 : if (is_statement)
20113 : 101816377 : gcc_assert (pre_p);
20114 : :
20115 : : /* Consistency checks. */
20116 : 466159600 : if (gimple_test_f == is_gimple_reg)
20117 : 6623365 : gcc_assert (fallback & (fb_rvalue | fb_lvalue));
20118 : 459536235 : else if (gimple_test_f == is_gimple_val
20119 : 340069239 : || gimple_test_f == is_gimple_call_addr
20120 : 323503790 : || gimple_test_f == is_gimple_condexpr_for_cond
20121 : 317930344 : || gimple_test_f == is_gimple_mem_rhs
20122 : 317787558 : || gimple_test_f == is_gimple_mem_rhs_or_call
20123 : 306580903 : || gimple_test_f == is_gimple_reg_rhs
20124 : 305979305 : || gimple_test_f == is_gimple_reg_rhs_or_call
20125 : 234465903 : || gimple_test_f == is_gimple_asm_val
20126 : 234428118 : || gimple_test_f == is_gimple_mem_ref_addr)
20127 : 240956438 : gcc_assert (fallback & fb_rvalue);
20128 : 218579797 : else if (gimple_test_f == is_gimple_min_lval
20129 : 188702429 : || gimple_test_f == is_gimple_lvalue)
20130 : 83319560 : gcc_assert (fallback & fb_lvalue);
20131 : 135260237 : else if (gimple_test_f == is_gimple_addressable)
20132 : 33443860 : gcc_assert (fallback & fb_either);
20133 : 101816377 : else if (gimple_test_f == is_gimple_stmt)
20134 : 101816377 : gcc_assert (fallback == fb_none);
20135 : : else
20136 : : {
20137 : : /* We should have recognized the GIMPLE_TEST_F predicate to
20138 : : know what kind of fallback to use in case a temporary is
20139 : : needed to hold the value or address of *EXPR_P. */
20140 : 0 : gcc_unreachable ();
20141 : : }
20142 : :
20143 : : /* We used to check the predicate here and return immediately if it
20144 : : succeeds. This is wrong; the design is for gimplification to be
20145 : : idempotent, and for the predicates to only test for valid forms, not
20146 : : whether they are fully simplified. */
20147 : 466159600 : if (pre_p == NULL)
20148 : 0 : pre_p = &internal_pre;
20149 : :
20150 : 466159600 : if (post_p == NULL)
20151 : 188353246 : post_p = &internal_post;
20152 : :
20153 : : /* Remember the last statements added to PRE_P and POST_P. Every
20154 : : new statement added by the gimplification helpers needs to be
20155 : : annotated with location information. To centralize the
20156 : : responsibility, we remember the last statement that had been
20157 : : added to both queues before gimplifying *EXPR_P. If
20158 : : gimplification produces new statements in PRE_P and POST_P, those
20159 : : statements will be annotated with the same location information
20160 : : as *EXPR_P. */
20161 : 466159600 : pre_last_gsi = gsi_last (*pre_p);
20162 : 466159600 : post_last_gsi = gsi_last (*post_p);
20163 : :
20164 : 466159600 : saved_location = input_location;
20165 : 466159600 : if (save_expr != error_mark_node
20166 : 466159600 : && EXPR_HAS_LOCATION (*expr_p))
20167 : 174592219 : input_location = EXPR_LOCATION (*expr_p);
20168 : :
20169 : : /* Loop over the specific gimplifiers until the toplevel node
20170 : : remains the same. */
20171 : 485173828 : do
20172 : : {
20173 : : /* Strip away as many useless type conversions as possible
20174 : : at the toplevel. */
20175 : 485173828 : STRIP_USELESS_TYPE_CONVERSION (*expr_p);
20176 : :
20177 : : /* Remember the expr. */
20178 : 485173828 : save_expr = *expr_p;
20179 : :
20180 : : /* Die, die, die, my darling. */
20181 : 485173828 : if (error_operand_p (save_expr))
20182 : : {
20183 : : ret = GS_ERROR;
20184 : : break;
20185 : : }
20186 : :
20187 : : /* Do any language-specific gimplification. */
20188 : 485170133 : ret = ((enum gimplify_status)
20189 : 485170133 : lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
20190 : 485170133 : if (ret == GS_OK)
20191 : : {
20192 : 29475270 : if (*expr_p == NULL_TREE)
20193 : : break;
20194 : 29475270 : if (*expr_p != save_expr)
20195 : 5087631 : continue;
20196 : : }
20197 : 455694863 : else if (ret != GS_UNHANDLED)
20198 : : break;
20199 : :
20200 : : /* Make sure that all the cases set 'ret' appropriately. */
20201 : 479443943 : ret = GS_UNHANDLED;
20202 : 479443943 : switch (TREE_CODE (*expr_p))
20203 : : {
20204 : : /* First deal with the special cases. */
20205 : :
20206 : 1136108 : case POSTINCREMENT_EXPR:
20207 : 1136108 : case POSTDECREMENT_EXPR:
20208 : 1136108 : case PREINCREMENT_EXPR:
20209 : 1136108 : case PREDECREMENT_EXPR:
20210 : 2272216 : ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
20211 : : fallback != fb_none,
20212 : 1136108 : TREE_TYPE (*expr_p));
20213 : 1136108 : break;
20214 : :
20215 : 645681 : case VIEW_CONVERT_EXPR:
20216 : 645681 : if ((fallback & fb_rvalue)
20217 : 645564 : && is_gimple_reg_type (TREE_TYPE (*expr_p))
20218 : 1113085 : && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
20219 : : {
20220 : 456444 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20221 : : post_p, is_gimple_val, fb_rvalue);
20222 : 456444 : recalculate_side_effects (*expr_p);
20223 : 456444 : break;
20224 : : }
20225 : : /* Fallthru. */
20226 : :
20227 : 29815087 : case ARRAY_REF:
20228 : 29815087 : case ARRAY_RANGE_REF:
20229 : 29815087 : case REALPART_EXPR:
20230 : 29815087 : case IMAGPART_EXPR:
20231 : 29815087 : case COMPONENT_REF:
20232 : 29815087 : ret = gimplify_compound_lval (expr_p, pre_p, post_p,
20233 : : fallback ? fallback : fb_rvalue);
20234 : 29815087 : break;
20235 : :
20236 : 6190817 : case COND_EXPR:
20237 : 6190817 : ret = gimplify_cond_expr (expr_p, pre_p, fallback);
20238 : :
20239 : : /* C99 code may assign to an array in a structure value of a
20240 : : conditional expression, and this has undefined behavior
20241 : : only on execution, so create a temporary if an lvalue is
20242 : : required. */
20243 : 6190817 : if (fallback == fb_lvalue)
20244 : : {
20245 : 7 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20246 : 7 : mark_addressable (*expr_p);
20247 : 7 : ret = GS_OK;
20248 : : }
20249 : : break;
20250 : :
20251 : 16941834 : case CALL_EXPR:
20252 : 16941834 : ret = gimplify_call_expr (expr_p, pre_p, fallback);
20253 : :
20254 : : /* C99 code may assign to an array in a structure returned
20255 : : from a function, and this has undefined behavior only on
20256 : : execution, so create a temporary if an lvalue is
20257 : : required. */
20258 : 16941834 : if (fallback == fb_lvalue)
20259 : : {
20260 : 18201 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20261 : 18201 : mark_addressable (*expr_p);
20262 : 18201 : ret = GS_OK;
20263 : : }
20264 : : break;
20265 : :
20266 : 0 : case TREE_LIST:
20267 : 0 : gcc_unreachable ();
20268 : :
20269 : 0 : case OMP_ARRAY_SECTION:
20270 : 0 : gcc_unreachable ();
20271 : :
20272 : 483886 : case COMPOUND_EXPR:
20273 : 483886 : ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
20274 : 483886 : break;
20275 : :
20276 : 36561 : case COMPOUND_LITERAL_EXPR:
20277 : 36561 : ret = gimplify_compound_literal_expr (expr_p, pre_p,
20278 : : gimple_test_f, fallback);
20279 : 36561 : break;
20280 : :
20281 : 50061993 : case MODIFY_EXPR:
20282 : 50061993 : case INIT_EXPR:
20283 : 50061993 : ret = gimplify_modify_expr (expr_p, pre_p, post_p,
20284 : : fallback != fb_none);
20285 : 50061993 : break;
20286 : :
20287 : 115860 : case TRUTH_ANDIF_EXPR:
20288 : 115860 : case TRUTH_ORIF_EXPR:
20289 : 115860 : {
20290 : : /* Preserve the original type of the expression and the
20291 : : source location of the outer expression. */
20292 : 115860 : tree org_type = TREE_TYPE (*expr_p);
20293 : 115860 : *expr_p = gimple_boolify (*expr_p);
20294 : 115860 : *expr_p = build3_loc (input_location, COND_EXPR,
20295 : : org_type, *expr_p,
20296 : : fold_convert_loc
20297 : : (input_location,
20298 : : org_type, boolean_true_node),
20299 : : fold_convert_loc
20300 : : (input_location,
20301 : : org_type, boolean_false_node));
20302 : 115860 : ret = GS_OK;
20303 : 115860 : break;
20304 : : }
20305 : :
20306 : 228785 : case TRUTH_NOT_EXPR:
20307 : 228785 : {
20308 : 228785 : tree type = TREE_TYPE (*expr_p);
20309 : : /* The parsers are careful to generate TRUTH_NOT_EXPR
20310 : : only with operands that are always zero or one.
20311 : : We do not fold here but handle the only interesting case
20312 : : manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
20313 : 228785 : *expr_p = gimple_boolify (*expr_p);
20314 : 228785 : if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
20315 : 228785 : *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
20316 : 228785 : TREE_TYPE (*expr_p),
20317 : 228785 : TREE_OPERAND (*expr_p, 0));
20318 : : else
20319 : 0 : *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
20320 : 0 : TREE_TYPE (*expr_p),
20321 : 0 : TREE_OPERAND (*expr_p, 0),
20322 : 0 : build_int_cst (TREE_TYPE (*expr_p), 1));
20323 : 228785 : if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
20324 : 6468 : *expr_p = fold_convert_loc (input_location, type, *expr_p);
20325 : : ret = GS_OK;
20326 : : break;
20327 : : }
20328 : :
20329 : 33524475 : case ADDR_EXPR:
20330 : 33524475 : ret = gimplify_addr_expr (expr_p, pre_p, post_p);
20331 : 33524475 : break;
20332 : :
20333 : 5280 : case ANNOTATE_EXPR:
20334 : 5280 : {
20335 : 5280 : tree cond = TREE_OPERAND (*expr_p, 0);
20336 : 5280 : tree kind = TREE_OPERAND (*expr_p, 1);
20337 : 5280 : tree data = TREE_OPERAND (*expr_p, 2);
20338 : 5280 : tree type = TREE_TYPE (cond);
20339 : 5280 : if (!INTEGRAL_TYPE_P (type))
20340 : : {
20341 : 0 : *expr_p = cond;
20342 : 0 : ret = GS_OK;
20343 : 0 : break;
20344 : : }
20345 : 5280 : tree tmp = create_tmp_var (type);
20346 : 5280 : gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
20347 : 5280 : gcall *call
20348 : 5280 : = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
20349 : 5280 : gimple_call_set_lhs (call, tmp);
20350 : 5280 : gimplify_seq_add_stmt (pre_p, call);
20351 : 5280 : *expr_p = tmp;
20352 : 5280 : ret = GS_ALL_DONE;
20353 : 5280 : break;
20354 : : }
20355 : :
20356 : 50810 : case VA_ARG_EXPR:
20357 : 50810 : ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
20358 : 50810 : break;
20359 : :
20360 : 17131047 : CASE_CONVERT:
20361 : 17131047 : if (IS_EMPTY_STMT (*expr_p))
20362 : : {
20363 : : ret = GS_ALL_DONE;
20364 : : break;
20365 : : }
20366 : :
20367 : 15425237 : if (VOID_TYPE_P (TREE_TYPE (*expr_p))
20368 : 15425237 : || fallback == fb_none)
20369 : : {
20370 : : /* Just strip a conversion to void (or in void context) and
20371 : : try again. */
20372 : 2753846 : *expr_p = TREE_OPERAND (*expr_p, 0);
20373 : 2753846 : ret = GS_OK;
20374 : 2753846 : break;
20375 : : }
20376 : :
20377 : 12671391 : ret = gimplify_conversion (expr_p);
20378 : 12671391 : if (ret == GS_ERROR)
20379 : : break;
20380 : 12671391 : if (*expr_p != save_expr)
20381 : : break;
20382 : : /* FALLTHRU */
20383 : :
20384 : 12796822 : case FIX_TRUNC_EXPR:
20385 : : /* unary_expr: ... | '(' cast ')' val | ... */
20386 : 12796822 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20387 : : is_gimple_val, fb_rvalue);
20388 : 12796822 : recalculate_side_effects (*expr_p);
20389 : 12796822 : break;
20390 : :
20391 : 6549524 : case INDIRECT_REF:
20392 : 6549524 : {
20393 : 6549524 : bool volatilep = TREE_THIS_VOLATILE (*expr_p);
20394 : 6549524 : bool notrap = TREE_THIS_NOTRAP (*expr_p);
20395 : 6549524 : tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
20396 : :
20397 : 6549524 : *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
20398 : 6549524 : if (*expr_p != save_expr)
20399 : : {
20400 : : ret = GS_OK;
20401 : : break;
20402 : : }
20403 : :
20404 : 6530003 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20405 : : is_gimple_reg, fb_rvalue);
20406 : 6530003 : if (ret == GS_ERROR)
20407 : : break;
20408 : :
20409 : 6530002 : recalculate_side_effects (*expr_p);
20410 : 13060004 : *expr_p = fold_build2_loc (input_location, MEM_REF,
20411 : 6530002 : TREE_TYPE (*expr_p),
20412 : 6530002 : TREE_OPERAND (*expr_p, 0),
20413 : : build_int_cst (saved_ptr_type, 0));
20414 : 6530002 : TREE_THIS_VOLATILE (*expr_p) = volatilep;
20415 : 6530002 : TREE_THIS_NOTRAP (*expr_p) = notrap;
20416 : 6530002 : ret = GS_OK;
20417 : 6530002 : break;
20418 : : }
20419 : :
20420 : : /* We arrive here through the various re-gimplifcation paths. */
20421 : 16390094 : case MEM_REF:
20422 : : /* First try re-folding the whole thing. */
20423 : 16390094 : tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
20424 : : TREE_OPERAND (*expr_p, 0),
20425 : : TREE_OPERAND (*expr_p, 1));
20426 : 16390094 : if (tmp)
20427 : : {
20428 : 6542 : REF_REVERSE_STORAGE_ORDER (tmp)
20429 : 3271 : = REF_REVERSE_STORAGE_ORDER (*expr_p);
20430 : 3271 : *expr_p = tmp;
20431 : 3271 : recalculate_side_effects (*expr_p);
20432 : 3271 : ret = GS_OK;
20433 : 3271 : break;
20434 : : }
20435 : : /* Avoid re-gimplifying the address operand if it is already
20436 : : in suitable form. Re-gimplifying would mark the address
20437 : : operand addressable. Always gimplify when not in SSA form
20438 : : as we still may have to gimplify decls with value-exprs. */
20439 : 16386823 : if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
20440 : 17475208 : || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
20441 : : {
20442 : 15353232 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20443 : : is_gimple_mem_ref_addr, fb_rvalue);
20444 : 15353232 : if (ret == GS_ERROR)
20445 : : break;
20446 : : }
20447 : 16386823 : recalculate_side_effects (*expr_p);
20448 : 16386823 : ret = GS_ALL_DONE;
20449 : 16386823 : break;
20450 : :
20451 : : /* Constants need not be gimplified. */
20452 : 43157229 : case INTEGER_CST:
20453 : 43157229 : case REAL_CST:
20454 : 43157229 : case FIXED_CST:
20455 : 43157229 : case STRING_CST:
20456 : 43157229 : case COMPLEX_CST:
20457 : 43157229 : case VECTOR_CST:
20458 : : /* Drop the overflow flag on constants, we do not want
20459 : : that in the GIMPLE IL. */
20460 : 43157229 : if (TREE_OVERFLOW_P (*expr_p))
20461 : 1162 : *expr_p = drop_tree_overflow (*expr_p);
20462 : : ret = GS_ALL_DONE;
20463 : : break;
20464 : :
20465 : 116377 : case CONST_DECL:
20466 : : /* If we require an lvalue, such as for ADDR_EXPR, retain the
20467 : : CONST_DECL node. Otherwise the decl is replaceable by its
20468 : : value. */
20469 : : /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
20470 : 116377 : if (fallback & fb_lvalue)
20471 : : ret = GS_ALL_DONE;
20472 : : else
20473 : : {
20474 : 3758 : *expr_p = DECL_INITIAL (*expr_p);
20475 : 3758 : ret = GS_OK;
20476 : : }
20477 : : break;
20478 : :
20479 : 6569491 : case DECL_EXPR:
20480 : 6569491 : ret = gimplify_decl_expr (expr_p, pre_p);
20481 : 6569491 : break;
20482 : :
20483 : 5997462 : case BIND_EXPR:
20484 : 5997462 : ret = gimplify_bind_expr (expr_p, pre_p);
20485 : 5997462 : break;
20486 : :
20487 : 194817 : case LOOP_EXPR:
20488 : 194817 : ret = gimplify_loop_expr (expr_p, pre_p);
20489 : 194817 : break;
20490 : :
20491 : 52036 : case SWITCH_EXPR:
20492 : 52036 : ret = gimplify_switch_expr (expr_p, pre_p);
20493 : 52036 : break;
20494 : :
20495 : 3095 : case EXIT_EXPR:
20496 : 3095 : ret = gimplify_exit_expr (expr_p);
20497 : 3095 : break;
20498 : :
20499 : 1112176 : case GOTO_EXPR:
20500 : : /* If the target is not LABEL, then it is a computed jump
20501 : : and the target needs to be gimplified. */
20502 : 1112176 : if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
20503 : : {
20504 : 1099 : ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
20505 : : NULL, is_gimple_val, fb_rvalue);
20506 : 1099 : if (ret == GS_ERROR)
20507 : : break;
20508 : : }
20509 : 2224350 : gimplify_seq_add_stmt (pre_p,
20510 : 1112175 : gimple_build_goto (GOTO_DESTINATION (*expr_p)));
20511 : 1112175 : ret = GS_ALL_DONE;
20512 : 1112175 : break;
20513 : :
20514 : 106109 : case PREDICT_EXPR:
20515 : 424436 : gimplify_seq_add_stmt (pre_p,
20516 : 106109 : gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
20517 : 106109 : PREDICT_EXPR_OUTCOME (*expr_p)));
20518 : 106109 : ret = GS_ALL_DONE;
20519 : 106109 : break;
20520 : :
20521 : 2541044 : case LABEL_EXPR:
20522 : 2541044 : ret = gimplify_label_expr (expr_p, pre_p);
20523 : 2541044 : label = LABEL_EXPR_LABEL (*expr_p);
20524 : 2541044 : gcc_assert (decl_function_context (label) == current_function_decl);
20525 : :
20526 : : /* If the label is used in a goto statement, or address of the label
20527 : : is taken, we need to unpoison all variables that were seen so far.
20528 : : Doing so would prevent us from reporting a false positives. */
20529 : 2541044 : if (asan_poisoned_variables
20530 : 3807 : && asan_used_labels != NULL
20531 : 1213 : && asan_used_labels->contains (label)
20532 : 2541858 : && !gimplify_omp_ctxp)
20533 : 806 : asan_poison_variables (asan_poisoned_variables, false, pre_p);
20534 : : break;
20535 : :
20536 : 1062902 : case CASE_LABEL_EXPR:
20537 : 1062902 : ret = gimplify_case_label_expr (expr_p, pre_p);
20538 : :
20539 : 1062902 : if (gimplify_ctxp->live_switch_vars)
20540 : 1062715 : asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
20541 : : pre_p);
20542 : : break;
20543 : :
20544 : 2271497 : case RETURN_EXPR:
20545 : 2271497 : ret = gimplify_return_expr (*expr_p, pre_p);
20546 : 2271497 : break;
20547 : :
20548 : 947023 : case CONSTRUCTOR:
20549 : : /* Don't reduce this in place; let gimplify_init_constructor work its
20550 : : magic. Buf if we're just elaborating this for side effects, just
20551 : : gimplify any element that has side-effects. */
20552 : 947023 : if (fallback == fb_none)
20553 : : {
20554 : 344 : unsigned HOST_WIDE_INT ix;
20555 : 344 : tree val;
20556 : 344 : tree temp = NULL_TREE;
20557 : 361 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
20558 : 17 : if (TREE_SIDE_EFFECTS (val))
20559 : 4 : append_to_statement_list (val, &temp);
20560 : :
20561 : 344 : *expr_p = temp;
20562 : 344 : ret = temp ? GS_OK : GS_ALL_DONE;
20563 : : }
20564 : : /* C99 code may assign to an array in a constructed
20565 : : structure or union, and this has undefined behavior only
20566 : : on execution, so create a temporary if an lvalue is
20567 : : required. */
20568 : 946679 : else if (fallback == fb_lvalue)
20569 : : {
20570 : 12 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20571 : 12 : mark_addressable (*expr_p);
20572 : 12 : ret = GS_OK;
20573 : : }
20574 : : else
20575 : : ret = GS_ALL_DONE;
20576 : : break;
20577 : :
20578 : : /* The following are special cases that are not handled by the
20579 : : original GIMPLE grammar. */
20580 : :
20581 : : /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
20582 : : eliminated. */
20583 : 424937 : case SAVE_EXPR:
20584 : 424937 : ret = gimplify_save_expr (expr_p, pre_p, post_p);
20585 : 424937 : break;
20586 : :
20587 : 416767 : case BIT_FIELD_REF:
20588 : 416767 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20589 : : post_p, is_gimple_lvalue, fb_either);
20590 : 416767 : recalculate_side_effects (*expr_p);
20591 : 416767 : break;
20592 : :
20593 : 1545 : case TARGET_MEM_REF:
20594 : 1545 : {
20595 : 1545 : enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
20596 : :
20597 : 1545 : if (TMR_BASE (*expr_p))
20598 : 1545 : r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
20599 : : post_p, is_gimple_mem_ref_addr, fb_either);
20600 : 1545 : if (TMR_INDEX (*expr_p))
20601 : 1137 : r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
20602 : : post_p, is_gimple_val, fb_rvalue);
20603 : 1545 : if (TMR_INDEX2 (*expr_p))
20604 : 60 : r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
20605 : : post_p, is_gimple_val, fb_rvalue);
20606 : : /* TMR_STEP and TMR_OFFSET are always integer constants. */
20607 : 1545 : ret = MIN (r0, r1);
20608 : : }
20609 : : break;
20610 : :
20611 : 0 : case NON_LVALUE_EXPR:
20612 : : /* This should have been stripped above. */
20613 : 0 : gcc_unreachable ();
20614 : :
20615 : 95151 : case ASM_EXPR:
20616 : 95151 : ret = gimplify_asm_expr (expr_p, pre_p, post_p);
20617 : 95151 : break;
20618 : :
20619 : 539188 : case TRY_FINALLY_EXPR:
20620 : 539188 : case TRY_CATCH_EXPR:
20621 : 539188 : {
20622 : 539188 : gimple_seq eval, cleanup;
20623 : 539188 : gtry *try_;
20624 : :
20625 : : /* Calls to destructors are generated automatically in FINALLY/CATCH
20626 : : block. They should have location as UNKNOWN_LOCATION. However,
20627 : : gimplify_call_expr will reset these call stmts to input_location
20628 : : if it finds stmt's location is unknown. To prevent resetting for
20629 : : destructors, we set the input_location to unknown.
20630 : : Note that this only affects the destructor calls in FINALLY/CATCH
20631 : : block, and will automatically reset to its original value by the
20632 : : end of gimplify_expr. */
20633 : 539188 : input_location = UNKNOWN_LOCATION;
20634 : 539188 : eval = cleanup = NULL;
20635 : 539188 : gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
20636 : 539188 : bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
20637 : 539188 : if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20638 : 539188 : && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
20639 : : {
20640 : 86 : gimple_seq n = NULL, e = NULL;
20641 : 86 : gimplify_ctxp->in_handler_expr = true;
20642 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20643 : : 0), &n);
20644 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20645 : : 1), &e);
20646 : 86 : if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
20647 : : {
20648 : 85 : geh_else *stmt = gimple_build_eh_else (n, e);
20649 : 85 : gimple_seq_add_stmt (&cleanup, stmt);
20650 : : }
20651 : : }
20652 : : else
20653 : : {
20654 : 539102 : gimplify_ctxp->in_handler_expr = true;
20655 : 539102 : gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
20656 : : }
20657 : 539188 : gimplify_ctxp->in_handler_expr = save_in_handler_expr;
20658 : : /* Don't create bogus GIMPLE_TRY with empty cleanup. */
20659 : 539188 : if (gimple_seq_empty_p (cleanup))
20660 : : {
20661 : 28184 : gimple_seq_add_seq (pre_p, eval);
20662 : 28184 : ret = GS_ALL_DONE;
20663 : 28184 : break;
20664 : : }
20665 : 511004 : try_ = gimple_build_try (eval, cleanup,
20666 : 511004 : TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20667 : : ? GIMPLE_TRY_FINALLY
20668 : : : GIMPLE_TRY_CATCH);
20669 : 511004 : if (EXPR_HAS_LOCATION (save_expr))
20670 : 942426 : gimple_set_location (try_, EXPR_LOCATION (save_expr));
20671 : 39791 : else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
20672 : 29452 : gimple_set_location (try_, saved_location);
20673 : 511004 : if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
20674 : 190441 : gimple_try_set_catch_is_cleanup (try_,
20675 : 190441 : TRY_CATCH_IS_CLEANUP (*expr_p));
20676 : 511004 : gimplify_seq_add_stmt (pre_p, try_);
20677 : 511004 : ret = GS_ALL_DONE;
20678 : 511004 : break;
20679 : : }
20680 : :
20681 : 5548939 : case CLEANUP_POINT_EXPR:
20682 : 5548939 : ret = gimplify_cleanup_point_expr (expr_p, pre_p);
20683 : 5548939 : break;
20684 : :
20685 : 820359 : case TARGET_EXPR:
20686 : 820359 : ret = gimplify_target_expr (expr_p, pre_p, post_p);
20687 : 820359 : break;
20688 : :
20689 : 39600 : case CATCH_EXPR:
20690 : 39600 : {
20691 : 39600 : gimple *c;
20692 : 39600 : gimple_seq handler = NULL;
20693 : 39600 : gimplify_and_add (CATCH_BODY (*expr_p), &handler);
20694 : 39600 : c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
20695 : 39600 : gimplify_seq_add_stmt (pre_p, c);
20696 : 39600 : ret = GS_ALL_DONE;
20697 : 39600 : break;
20698 : : }
20699 : :
20700 : 5769 : case EH_FILTER_EXPR:
20701 : 5769 : {
20702 : 5769 : gimple *ehf;
20703 : 5769 : gimple_seq failure = NULL;
20704 : :
20705 : 5769 : gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
20706 : 5769 : ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
20707 : 5769 : copy_warning (ehf, *expr_p);
20708 : 5769 : gimplify_seq_add_stmt (pre_p, ehf);
20709 : 5769 : ret = GS_ALL_DONE;
20710 : 5769 : break;
20711 : : }
20712 : :
20713 : 51683 : case OBJ_TYPE_REF:
20714 : 51683 : {
20715 : 51683 : enum gimplify_status r0, r1;
20716 : 51683 : r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
20717 : : post_p, is_gimple_val, fb_rvalue);
20718 : 51683 : r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
20719 : : post_p, is_gimple_val, fb_rvalue);
20720 : 51683 : TREE_SIDE_EFFECTS (*expr_p) = 0;
20721 : 51683 : ret = MIN (r0, r1);
20722 : : }
20723 : : break;
20724 : :
20725 : 40494 : case LABEL_DECL:
20726 : : /* We get here when taking the address of a label. We mark
20727 : : the label as "forced"; meaning it can never be removed and
20728 : : it is a potential target for any computed goto. */
20729 : 40494 : FORCED_LABEL (*expr_p) = 1;
20730 : 40494 : ret = GS_ALL_DONE;
20731 : 40494 : break;
20732 : :
20733 : 8764928 : case STATEMENT_LIST:
20734 : 8764928 : ret = gimplify_statement_list (expr_p, pre_p);
20735 : 8764928 : break;
20736 : :
20737 : 1553 : case WITH_SIZE_EXPR:
20738 : 1553 : {
20739 : 1835 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20740 : : post_p == &internal_post ? NULL : post_p,
20741 : : gimple_test_f, fallback);
20742 : 1553 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20743 : : is_gimple_val, fb_rvalue);
20744 : 1553 : ret = GS_ALL_DONE;
20745 : : }
20746 : 1553 : break;
20747 : :
20748 : 109144947 : case VAR_DECL:
20749 : 109144947 : case PARM_DECL:
20750 : 109144947 : ret = gimplify_var_or_parm_decl (expr_p);
20751 : 109144947 : break;
20752 : :
20753 : 349541 : case RESULT_DECL:
20754 : : /* When within an OMP context, notice uses of variables. */
20755 : 349541 : if (gimplify_omp_ctxp)
20756 : 1358 : omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
20757 : : /* Handlers can refer to the function result; if that has been
20758 : : moved, we need to track it. */
20759 : 349541 : if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
20760 : 48 : *expr_p = gimplify_ctxp->return_temp;
20761 : : ret = GS_ALL_DONE;
20762 : : break;
20763 : :
20764 : 0 : case DEBUG_EXPR_DECL:
20765 : 0 : gcc_unreachable ();
20766 : :
20767 : 2457704 : case DEBUG_BEGIN_STMT:
20768 : 4915408 : gimplify_seq_add_stmt (pre_p,
20769 : : gimple_build_debug_begin_stmt
20770 : 2457704 : (TREE_BLOCK (*expr_p),
20771 : 2457704 : EXPR_LOCATION (*expr_p)));
20772 : 2457704 : ret = GS_ALL_DONE;
20773 : 2457704 : *expr_p = NULL;
20774 : 2457704 : break;
20775 : :
20776 : : case SSA_NAME:
20777 : : /* Allow callbacks into the gimplifier during optimization. */
20778 : : ret = GS_ALL_DONE;
20779 : : break;
20780 : :
20781 : 18217 : case OMP_PARALLEL:
20782 : 18217 : gimplify_omp_parallel (expr_p, pre_p);
20783 : 18217 : ret = GS_ALL_DONE;
20784 : 18217 : break;
20785 : :
20786 : 4014 : case OMP_TASK:
20787 : 4014 : gimplify_omp_task (expr_p, pre_p);
20788 : 4014 : ret = GS_ALL_DONE;
20789 : 4014 : break;
20790 : :
20791 : 10672 : case OMP_SIMD:
20792 : 10672 : {
20793 : : /* Temporarily disable into_ssa, as scan_omp_simd
20794 : : which calls copy_gimple_seq_and_replace_locals can't deal
20795 : : with SSA_NAMEs defined outside of the body properly. */
20796 : 10672 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
20797 : 10672 : gimplify_ctxp->into_ssa = false;
20798 : 10672 : ret = gimplify_omp_for (expr_p, pre_p);
20799 : 10672 : gimplify_ctxp->into_ssa = saved_into_ssa;
20800 : 10672 : break;
20801 : : }
20802 : :
20803 : 47440 : case OMP_FOR:
20804 : 47440 : case OMP_DISTRIBUTE:
20805 : 47440 : case OMP_TASKLOOP:
20806 : 47440 : case OMP_TILE:
20807 : 47440 : case OMP_UNROLL:
20808 : 47440 : case OACC_LOOP:
20809 : 47440 : ret = gimplify_omp_for (expr_p, pre_p);
20810 : 47440 : break;
20811 : :
20812 : 1057 : case OMP_LOOP:
20813 : 1057 : ret = gimplify_omp_loop (expr_p, pre_p);
20814 : 1057 : break;
20815 : :
20816 : 708 : case OACC_CACHE:
20817 : 708 : gimplify_oacc_cache (expr_p, pre_p);
20818 : 708 : ret = GS_ALL_DONE;
20819 : 708 : break;
20820 : :
20821 : 254 : case OACC_DECLARE:
20822 : 254 : gimplify_oacc_declare (expr_p, pre_p);
20823 : 254 : ret = GS_ALL_DONE;
20824 : 254 : break;
20825 : :
20826 : 38958 : case OACC_HOST_DATA:
20827 : 38958 : case OACC_DATA:
20828 : 38958 : case OACC_KERNELS:
20829 : 38958 : case OACC_PARALLEL:
20830 : 38958 : case OACC_SERIAL:
20831 : 38958 : case OMP_SCOPE:
20832 : 38958 : case OMP_SECTIONS:
20833 : 38958 : case OMP_SINGLE:
20834 : 38958 : case OMP_TARGET:
20835 : 38958 : case OMP_TARGET_DATA:
20836 : 38958 : case OMP_TEAMS:
20837 : 38958 : gimplify_omp_workshare (expr_p, pre_p);
20838 : 38958 : ret = GS_ALL_DONE;
20839 : 38958 : break;
20840 : :
20841 : 11944 : case OACC_ENTER_DATA:
20842 : 11944 : case OACC_EXIT_DATA:
20843 : 11944 : case OACC_UPDATE:
20844 : 11944 : case OMP_TARGET_UPDATE:
20845 : 11944 : case OMP_TARGET_ENTER_DATA:
20846 : 11944 : case OMP_TARGET_EXIT_DATA:
20847 : 11944 : gimplify_omp_target_update (expr_p, pre_p);
20848 : 11944 : ret = GS_ALL_DONE;
20849 : 11944 : break;
20850 : :
20851 : 6981 : case OMP_SECTION:
20852 : 6981 : case OMP_STRUCTURED_BLOCK:
20853 : 6981 : case OMP_MASTER:
20854 : 6981 : case OMP_MASKED:
20855 : 6981 : case OMP_ORDERED:
20856 : 6981 : case OMP_CRITICAL:
20857 : 6981 : case OMP_SCAN:
20858 : 6981 : {
20859 : 6981 : gimple_seq body = NULL;
20860 : 6981 : gimple *g;
20861 : 6981 : bool saved_in_omp_construct = in_omp_construct;
20862 : :
20863 : 6981 : in_omp_construct = true;
20864 : 6981 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20865 : 6981 : in_omp_construct = saved_in_omp_construct;
20866 : 6981 : switch (TREE_CODE (*expr_p))
20867 : : {
20868 : 1271 : case OMP_SECTION:
20869 : 1271 : g = gimple_build_omp_section (body);
20870 : 1271 : break;
20871 : 774 : case OMP_STRUCTURED_BLOCK:
20872 : 774 : g = gimple_build_omp_structured_block (body);
20873 : 774 : break;
20874 : 886 : case OMP_MASTER:
20875 : 886 : g = gimple_build_omp_master (body);
20876 : 886 : break;
20877 : 1940 : case OMP_ORDERED:
20878 : 1940 : g = gimplify_omp_ordered (*expr_p, body);
20879 : 1940 : if (OMP_BODY (*expr_p) == NULL_TREE
20880 : 1940 : && gimple_code (g) == GIMPLE_OMP_ORDERED)
20881 : 1015 : gimple_omp_ordered_standalone (g);
20882 : : break;
20883 : 446 : case OMP_MASKED:
20884 : 446 : gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
20885 : : pre_p, ORT_WORKSHARE, OMP_MASKED);
20886 : 446 : gimplify_adjust_omp_clauses (pre_p, body,
20887 : : &OMP_MASKED_CLAUSES (*expr_p),
20888 : : OMP_MASKED);
20889 : 892 : g = gimple_build_omp_masked (body,
20890 : 446 : OMP_MASKED_CLAUSES (*expr_p));
20891 : 446 : break;
20892 : 546 : case OMP_CRITICAL:
20893 : 546 : gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
20894 : : pre_p, ORT_WORKSHARE, OMP_CRITICAL);
20895 : 546 : gimplify_adjust_omp_clauses (pre_p, body,
20896 : : &OMP_CRITICAL_CLAUSES (*expr_p),
20897 : : OMP_CRITICAL);
20898 : 1638 : g = gimple_build_omp_critical (body,
20899 : 546 : OMP_CRITICAL_NAME (*expr_p),
20900 : 546 : OMP_CRITICAL_CLAUSES (*expr_p));
20901 : 546 : break;
20902 : 1118 : case OMP_SCAN:
20903 : 1118 : gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
20904 : : pre_p, ORT_WORKSHARE, OMP_SCAN);
20905 : 1118 : gimplify_adjust_omp_clauses (pre_p, body,
20906 : : &OMP_SCAN_CLAUSES (*expr_p),
20907 : : OMP_SCAN);
20908 : 1118 : g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
20909 : 1118 : break;
20910 : 0 : default:
20911 : 0 : gcc_unreachable ();
20912 : : }
20913 : 6981 : gimplify_seq_add_stmt (pre_p, g);
20914 : 6981 : ret = GS_ALL_DONE;
20915 : 6981 : break;
20916 : : }
20917 : :
20918 : 611 : case OMP_TASKGROUP:
20919 : 611 : {
20920 : 611 : gimple_seq body = NULL;
20921 : :
20922 : 611 : tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
20923 : 611 : bool saved_in_omp_construct = in_omp_construct;
20924 : 611 : gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
20925 : : OMP_TASKGROUP);
20926 : 611 : gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
20927 : :
20928 : 611 : in_omp_construct = true;
20929 : 611 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20930 : 611 : in_omp_construct = saved_in_omp_construct;
20931 : 611 : gimple_seq cleanup = NULL;
20932 : 611 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
20933 : 611 : gimple *g = gimple_build_call (fn, 0);
20934 : 611 : gimple_seq_add_stmt (&cleanup, g);
20935 : 611 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
20936 : 611 : body = NULL;
20937 : 611 : gimple_seq_add_stmt (&body, g);
20938 : 611 : g = gimple_build_omp_taskgroup (body, *pclauses);
20939 : 611 : gimplify_seq_add_stmt (pre_p, g);
20940 : 611 : ret = GS_ALL_DONE;
20941 : 611 : break;
20942 : : }
20943 : :
20944 : 612 : case OMP_INTEROP:
20945 : 612 : ret = gimplify_omp_interop (expr_p, pre_p);
20946 : 612 : break;
20947 : 10249 : case OMP_ATOMIC:
20948 : 10249 : case OMP_ATOMIC_READ:
20949 : 10249 : case OMP_ATOMIC_CAPTURE_OLD:
20950 : 10249 : case OMP_ATOMIC_CAPTURE_NEW:
20951 : 10249 : ret = gimplify_omp_atomic (expr_p, pre_p);
20952 : 10249 : break;
20953 : :
20954 : 846 : case OMP_DISPATCH:
20955 : 846 : ret = gimplify_omp_dispatch (expr_p, pre_p);
20956 : 846 : break;
20957 : :
20958 : 162 : case OMP_METADIRECTIVE:
20959 : 162 : ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
20960 : : gimple_test_f, fallback);
20961 : 162 : break;
20962 : :
20963 : 36 : case OMP_NEXT_VARIANT:
20964 : 36 : case OMP_TARGET_DEVICE_MATCHES:
20965 : : /* These are placeholders for constants. There's nothing to do with
20966 : : them here but we must mark the containing function as needing
20967 : : to run the ompdevlow pass to resolve them. Note that
20968 : : OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
20969 : : the front ends. */
20970 : 36 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
20971 : 36 : ret = GS_ALL_DONE;
20972 : 36 : break;
20973 : :
20974 : 0 : case OMP_DECLARE_MAPPER:
20975 : 0 : ret = gimplify_omp_declare_mapper (expr_p);
20976 : 0 : break;
20977 : :
20978 : 478 : case TRANSACTION_EXPR:
20979 : 478 : ret = gimplify_transaction (expr_p, pre_p);
20980 : 478 : break;
20981 : :
20982 : 297707 : case TRUTH_AND_EXPR:
20983 : 297707 : case TRUTH_OR_EXPR:
20984 : 297707 : case TRUTH_XOR_EXPR:
20985 : 297707 : {
20986 : 297707 : tree orig_type = TREE_TYPE (*expr_p);
20987 : 297707 : tree new_type, xop0, xop1;
20988 : 297707 : *expr_p = gimple_boolify (*expr_p);
20989 : 297707 : new_type = TREE_TYPE (*expr_p);
20990 : 297707 : if (!useless_type_conversion_p (orig_type, new_type))
20991 : : {
20992 : 1122 : *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
20993 : 1122 : ret = GS_OK;
20994 : 1122 : break;
20995 : : }
20996 : :
20997 : : /* Boolified binary truth expressions are semantically equivalent
20998 : : to bitwise binary expressions. Canonicalize them to the
20999 : : bitwise variant. */
21000 : 296585 : switch (TREE_CODE (*expr_p))
21001 : : {
21002 : 176319 : case TRUTH_AND_EXPR:
21003 : 176319 : TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
21004 : 176319 : break;
21005 : 119677 : case TRUTH_OR_EXPR:
21006 : 119677 : TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
21007 : 119677 : break;
21008 : 589 : case TRUTH_XOR_EXPR:
21009 : 589 : TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
21010 : 589 : break;
21011 : : default:
21012 : : break;
21013 : : }
21014 : : /* Now make sure that operands have compatible type to
21015 : : expression's new_type. */
21016 : 296585 : xop0 = TREE_OPERAND (*expr_p, 0);
21017 : 296585 : xop1 = TREE_OPERAND (*expr_p, 1);
21018 : 296585 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
21019 : 326 : TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
21020 : : new_type,
21021 : : xop0);
21022 : 296585 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
21023 : 366 : TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
21024 : : new_type,
21025 : : xop1);
21026 : : /* Continue classified as tcc_binary. */
21027 : 296585 : goto expr_2;
21028 : : }
21029 : :
21030 : 9435 : case VEC_COND_EXPR:
21031 : 9435 : goto expr_3;
21032 : :
21033 : 132612 : case VEC_PERM_EXPR:
21034 : : /* Classified as tcc_expression. */
21035 : 132612 : goto expr_3;
21036 : :
21037 : 48 : case BIT_INSERT_EXPR:
21038 : : /* Argument 3 is a constant. */
21039 : 48 : goto expr_2;
21040 : :
21041 : 3856341 : case POINTER_PLUS_EXPR:
21042 : 3856341 : {
21043 : 3856341 : enum gimplify_status r0, r1;
21044 : 3856341 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21045 : : post_p, is_gimple_val, fb_rvalue);
21046 : 3856341 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21047 : : post_p, is_gimple_val, fb_rvalue);
21048 : 3856341 : recalculate_side_effects (*expr_p);
21049 : 3856341 : ret = MIN (r0, r1);
21050 : : break;
21051 : : }
21052 : :
21053 : 44179744 : default:
21054 : 44179744 : switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
21055 : : {
21056 : 6956752 : case tcc_comparison:
21057 : : /* Handle comparison of objects of non scalar mode aggregates
21058 : : with a call to memcmp. It would be nice to only have to do
21059 : : this for variable-sized objects, but then we'd have to allow
21060 : : the same nest of reference nodes we allow for MODIFY_EXPR and
21061 : : that's too complex.
21062 : :
21063 : : Compare scalar mode aggregates as scalar mode values. Using
21064 : : memcmp for them would be very inefficient at best, and is
21065 : : plain wrong if bitfields are involved. */
21066 : 6956752 : if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
21067 : : ret = GS_ERROR;
21068 : : else
21069 : : {
21070 : 6956752 : tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
21071 : :
21072 : : /* Vector comparisons need no boolification. */
21073 : 6956752 : if (TREE_CODE (type) == VECTOR_TYPE)
21074 : 11463 : goto expr_2;
21075 : 6945289 : else if (!AGGREGATE_TYPE_P (type))
21076 : : {
21077 : 6945273 : tree org_type = TREE_TYPE (*expr_p);
21078 : 6945273 : *expr_p = gimple_boolify (*expr_p);
21079 : 6945273 : if (!useless_type_conversion_p (org_type,
21080 : 6945273 : TREE_TYPE (*expr_p)))
21081 : : {
21082 : 55014 : *expr_p = fold_convert_loc (input_location,
21083 : : org_type, *expr_p);
21084 : 55014 : ret = GS_OK;
21085 : : }
21086 : : else
21087 : 6890259 : goto expr_2;
21088 : : }
21089 : 16 : else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
21090 : 16 : ret = gimplify_scalar_mode_aggregate_compare (expr_p);
21091 : : else
21092 : 0 : ret = gimplify_variable_sized_compare (expr_p);
21093 : : }
21094 : : break;
21095 : :
21096 : : /* If *EXPR_P does not need to be special-cased, handle it
21097 : : according to its class. */
21098 : 1581467 : case tcc_unary:
21099 : 1581467 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21100 : : post_p, is_gimple_val, fb_rvalue);
21101 : 1581467 : break;
21102 : :
21103 : 26010239 : case tcc_binary:
21104 : 26010239 : expr_2:
21105 : 26010239 : {
21106 : 26010239 : enum gimplify_status r0, r1;
21107 : :
21108 : 26010239 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21109 : : post_p, is_gimple_val, fb_rvalue);
21110 : 26010239 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21111 : : post_p, is_gimple_val, fb_rvalue);
21112 : :
21113 : 26010239 : ret = MIN (r0, r1);
21114 : : break;
21115 : : }
21116 : :
21117 : 142047 : expr_3:
21118 : 142047 : {
21119 : 142047 : enum gimplify_status r0, r1, r2;
21120 : :
21121 : 142047 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
21122 : : post_p, is_gimple_val, fb_rvalue);
21123 : 142047 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
21124 : : post_p, is_gimple_val, fb_rvalue);
21125 : 142047 : r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
21126 : : post_p, is_gimple_val, fb_rvalue);
21127 : :
21128 : 142047 : ret = MIN (MIN (r0, r1), r2);
21129 : : break;
21130 : : }
21131 : :
21132 : 16829641 : case tcc_declaration:
21133 : 16829641 : case tcc_constant:
21134 : 16829641 : ret = GS_ALL_DONE;
21135 : 16829641 : goto dont_recalculate;
21136 : :
21137 : 0 : default:
21138 : 0 : gcc_unreachable ();
21139 : : }
21140 : :
21141 : 27788783 : recalculate_side_effects (*expr_p);
21142 : :
21143 : 479443943 : dont_recalculate:
21144 : : break;
21145 : : }
21146 : :
21147 : 479443943 : gcc_assert (*expr_p || ret != GS_OK);
21148 : : }
21149 : 484531574 : while (ret == GS_OK);
21150 : :
21151 : : /* If we encountered an error_mark somewhere nested inside, either
21152 : : stub out the statement or propagate the error back out. */
21153 : 466159600 : if (ret == GS_ERROR)
21154 : : {
21155 : 5539 : if (is_statement)
21156 : 4071 : *expr_p = NULL;
21157 : 5539 : goto out;
21158 : : }
21159 : :
21160 : : /* This was only valid as a return value from the langhook, which
21161 : : we handled. Make sure it doesn't escape from any other context. */
21162 : 466154061 : gcc_assert (ret != GS_UNHANDLED);
21163 : :
21164 : 466154061 : if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
21165 : : {
21166 : : /* We aren't looking for a value, and we don't have a valid
21167 : : statement. If it doesn't have side-effects, throw it away.
21168 : : We can also get here with code such as "*&&L;", where L is
21169 : : a LABEL_DECL that is marked as FORCED_LABEL. */
21170 : 1171027 : if (TREE_CODE (*expr_p) == LABEL_DECL
21171 : 1171027 : || !TREE_SIDE_EFFECTS (*expr_p))
21172 : 1170006 : *expr_p = NULL;
21173 : 1021 : else if (!TREE_THIS_VOLATILE (*expr_p))
21174 : : {
21175 : : /* This is probably a _REF that contains something nested that
21176 : : has side effects. Recurse through the operands to find it. */
21177 : 0 : enum tree_code code = TREE_CODE (*expr_p);
21178 : :
21179 : 0 : switch (code)
21180 : : {
21181 : 0 : case COMPONENT_REF:
21182 : 0 : case REALPART_EXPR:
21183 : 0 : case IMAGPART_EXPR:
21184 : 0 : case VIEW_CONVERT_EXPR:
21185 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21186 : : gimple_test_f, fallback);
21187 : 0 : break;
21188 : :
21189 : 0 : case ARRAY_REF:
21190 : 0 : case ARRAY_RANGE_REF:
21191 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
21192 : : gimple_test_f, fallback);
21193 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
21194 : : gimple_test_f, fallback);
21195 : 0 : break;
21196 : :
21197 : 0 : default:
21198 : : /* Anything else with side-effects must be converted to
21199 : : a valid statement before we get here. */
21200 : 0 : gcc_unreachable ();
21201 : : }
21202 : :
21203 : 0 : *expr_p = NULL;
21204 : : }
21205 : 1021 : else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
21206 : 971 : && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
21207 : 1980 : && !is_empty_type (TREE_TYPE (*expr_p)))
21208 : : {
21209 : : /* Historically, the compiler has treated a bare reference
21210 : : to a non-BLKmode volatile lvalue as forcing a load. */
21211 : 893 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
21212 : :
21213 : : /* Normally, we do not want to create a temporary for a
21214 : : TREE_ADDRESSABLE type because such a type should not be
21215 : : copied by bitwise-assignment. However, we make an
21216 : : exception here, as all we are doing here is ensuring that
21217 : : we read the bytes that make up the type. We use
21218 : : create_tmp_var_raw because create_tmp_var will abort when
21219 : : given a TREE_ADDRESSABLE type. */
21220 : 893 : tree tmp = create_tmp_var_raw (type, "vol");
21221 : 893 : gimple_add_tmp_var (tmp);
21222 : 893 : gimplify_assign (tmp, *expr_p, pre_p);
21223 : 893 : *expr_p = NULL;
21224 : : }
21225 : : else
21226 : : /* We can't do anything useful with a volatile reference to
21227 : : an incomplete type, so just throw it away. Likewise for
21228 : : a BLKmode type, since any implicit inner load should
21229 : : already have been turned into an explicit one by the
21230 : : gimplification process. */
21231 : 128 : *expr_p = NULL;
21232 : : }
21233 : :
21234 : : /* If we are gimplifying at the statement level, we're done. Tack
21235 : : everything together and return. */
21236 : 466154061 : if (fallback == fb_none || is_statement)
21237 : : {
21238 : : /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
21239 : : it out for GC to reclaim it. */
21240 : 101812306 : *expr_p = NULL_TREE;
21241 : :
21242 : 101812306 : if (!gimple_seq_empty_p (internal_pre)
21243 : 101812306 : || !gimple_seq_empty_p (internal_post))
21244 : : {
21245 : 19 : gimplify_seq_add_seq (&internal_pre, internal_post);
21246 : 19 : gimplify_seq_add_seq (pre_p, internal_pre);
21247 : : }
21248 : :
21249 : : /* The result of gimplifying *EXPR_P is going to be the last few
21250 : : statements in *PRE_P and *POST_P. Add location information
21251 : : to all the statements that were added by the gimplification
21252 : : helpers. */
21253 : 101812306 : if (!gimple_seq_empty_p (*pre_p))
21254 : 99188307 : annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
21255 : :
21256 : 101812306 : if (!gimple_seq_empty_p (*post_p))
21257 : 19 : annotate_all_with_location_after (*post_p, post_last_gsi,
21258 : : input_location);
21259 : :
21260 : 101812306 : goto out;
21261 : : }
21262 : :
21263 : : #ifdef ENABLE_GIMPLE_CHECKING
21264 : 364341755 : if (*expr_p)
21265 : : {
21266 : 364341755 : enum tree_code code = TREE_CODE (*expr_p);
21267 : : /* These expressions should already be in gimple IR form. */
21268 : 364341755 : gcc_assert (code != MODIFY_EXPR
21269 : : && code != ASM_EXPR
21270 : : && code != BIND_EXPR
21271 : : && code != CATCH_EXPR
21272 : : && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
21273 : : && code != EH_FILTER_EXPR
21274 : : && code != GOTO_EXPR
21275 : : && code != LABEL_EXPR
21276 : : && code != LOOP_EXPR
21277 : : && code != SWITCH_EXPR
21278 : : && code != TRY_FINALLY_EXPR
21279 : : && code != EH_ELSE_EXPR
21280 : : && code != OACC_PARALLEL
21281 : : && code != OACC_KERNELS
21282 : : && code != OACC_SERIAL
21283 : : && code != OACC_DATA
21284 : : && code != OACC_HOST_DATA
21285 : : && code != OACC_DECLARE
21286 : : && code != OACC_UPDATE
21287 : : && code != OACC_ENTER_DATA
21288 : : && code != OACC_EXIT_DATA
21289 : : && code != OACC_CACHE
21290 : : && code != OMP_CRITICAL
21291 : : && code != OMP_FOR
21292 : : && code != OACC_LOOP
21293 : : && code != OMP_MASTER
21294 : : && code != OMP_MASKED
21295 : : && code != OMP_TASKGROUP
21296 : : && code != OMP_ORDERED
21297 : : && code != OMP_PARALLEL
21298 : : && code != OMP_SCAN
21299 : : && code != OMP_SECTIONS
21300 : : && code != OMP_SECTION
21301 : : && code != OMP_STRUCTURED_BLOCK
21302 : : && code != OMP_SINGLE
21303 : : && code != OMP_SCOPE
21304 : : && code != OMP_DISPATCH);
21305 : : }
21306 : : #endif
21307 : :
21308 : : /* Otherwise we're gimplifying a subexpression, so the resulting
21309 : : value is interesting. If it's a valid operand that matches
21310 : : GIMPLE_TEST_F, we're done. Unless we are handling some
21311 : : post-effects internally; if that's the case, we need to copy into
21312 : : a temporary before adding the post-effects to POST_P. */
21313 : 364341755 : if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
21314 : 338438959 : goto out;
21315 : :
21316 : : /* Otherwise, we need to create a new temporary for the gimplified
21317 : : expression. */
21318 : :
21319 : : /* We can't return an lvalue if we have an internal postqueue. The
21320 : : object the lvalue refers to would (probably) be modified by the
21321 : : postqueue; we need to copy the value out first, which means an
21322 : : rvalue. */
21323 : 25902796 : if ((fallback & fb_lvalue)
21324 : 586743 : && gimple_seq_empty_p (internal_post)
21325 : 26489539 : && is_gimple_addressable (*expr_p))
21326 : : {
21327 : : /* An lvalue will do. Take the address of the expression, store it
21328 : : in a temporary, and replace the expression with an INDIRECT_REF of
21329 : : that temporary. */
21330 : 15 : tree ref_alias_type = reference_alias_ptr_type (*expr_p);
21331 : 15 : unsigned int ref_align = get_object_alignment (*expr_p);
21332 : 15 : tree ref_type = TREE_TYPE (*expr_p);
21333 : 15 : tmp = build_fold_addr_expr_loc (input_location, *expr_p);
21334 : 15 : gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
21335 : 15 : if (TYPE_ALIGN (ref_type) != ref_align)
21336 : 1 : ref_type = build_aligned_type (ref_type, ref_align);
21337 : 15 : *expr_p = build2 (MEM_REF, ref_type,
21338 : : tmp, build_zero_cst (ref_alias_type));
21339 : : }
21340 : 25902781 : else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
21341 : : {
21342 : : /* An rvalue will do. Assign the gimplified expression into a
21343 : : new temporary TMP and replace the original expression with
21344 : : TMP. First, make sure that the expression has a type so that
21345 : : it can be assigned into a temporary. */
21346 : 25902773 : gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
21347 : 25902773 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
21348 : : }
21349 : : else
21350 : : {
21351 : : #ifdef ENABLE_GIMPLE_CHECKING
21352 : 8 : if (!(fallback & fb_mayfail))
21353 : : {
21354 : 0 : fprintf (stderr, "gimplification failed:\n");
21355 : 0 : print_generic_expr (stderr, *expr_p);
21356 : 0 : debug_tree (*expr_p);
21357 : 0 : internal_error ("gimplification failed");
21358 : : }
21359 : : #endif
21360 : 8 : gcc_assert (fallback & fb_mayfail);
21361 : :
21362 : : /* If this is an asm statement, and the user asked for the
21363 : : impossible, don't die. Fail and let gimplify_asm_expr
21364 : : issue an error. */
21365 : 8 : ret = GS_ERROR;
21366 : 8 : goto out;
21367 : : }
21368 : :
21369 : : /* Make sure the temporary matches our predicate. */
21370 : 25902788 : gcc_assert ((*gimple_test_f) (*expr_p));
21371 : :
21372 : 25902788 : if (!gimple_seq_empty_p (internal_post))
21373 : : {
21374 : 0 : annotate_all_with_location (internal_post, input_location);
21375 : 0 : gimplify_seq_add_seq (pre_p, internal_post);
21376 : : }
21377 : :
21378 : 25902788 : out:
21379 : 466159600 : input_location = saved_location;
21380 : 466159600 : return ret;
21381 : : }
21382 : :
21383 : : /* Like gimplify_expr but make sure the gimplified result is not itself
21384 : : a SSA name (but a decl if it were). Temporaries required by
21385 : : evaluating *EXPR_P may be still SSA names. */
21386 : :
21387 : : static enum gimplify_status
21388 : 33889515 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
21389 : : bool (*gimple_test_f) (tree), fallback_t fallback,
21390 : : bool allow_ssa)
21391 : : {
21392 : 33889515 : enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
21393 : : gimple_test_f, fallback);
21394 : 33889515 : if (! allow_ssa
21395 : 265137 : && TREE_CODE (*expr_p) == SSA_NAME)
21396 : 67909 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
21397 : 33889515 : return ret;
21398 : : }
21399 : :
21400 : : /* Look through TYPE for variable-sized objects and gimplify each such
21401 : : size that we find. Add to LIST_P any statements generated. */
21402 : :
21403 : : void
21404 : 8294753 : gimplify_type_sizes (tree type, gimple_seq *list_p)
21405 : : {
21406 : 8294753 : if (type == NULL || type == error_mark_node)
21407 : : return;
21408 : :
21409 : 8294507 : const bool ignored_p
21410 : 8294507 : = TYPE_NAME (type)
21411 : 4076959 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21412 : 12134060 : && DECL_IGNORED_P (TYPE_NAME (type));
21413 : 8294507 : tree t;
21414 : :
21415 : : /* We first do the main variant, then copy into any other variants. */
21416 : 8294507 : type = TYPE_MAIN_VARIANT (type);
21417 : :
21418 : : /* Avoid infinite recursion. */
21419 : 8294507 : if (TYPE_SIZES_GIMPLIFIED (type))
21420 : : return;
21421 : :
21422 : 2489740 : TYPE_SIZES_GIMPLIFIED (type) = 1;
21423 : :
21424 : 2489740 : switch (TREE_CODE (type))
21425 : : {
21426 : 490028 : case INTEGER_TYPE:
21427 : 490028 : case ENUMERAL_TYPE:
21428 : 490028 : case BOOLEAN_TYPE:
21429 : 490028 : case REAL_TYPE:
21430 : 490028 : case FIXED_POINT_TYPE:
21431 : 490028 : gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
21432 : 490028 : gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
21433 : :
21434 : 8657866 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21435 : : {
21436 : 8167838 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
21437 : 8167838 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
21438 : : }
21439 : : break;
21440 : :
21441 : 180751 : case ARRAY_TYPE:
21442 : : /* These types may not have declarations, so handle them here. */
21443 : 180751 : gimplify_type_sizes (TREE_TYPE (type), list_p);
21444 : 180751 : gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
21445 : : /* Ensure VLA bounds aren't removed, for -O0 they should be variables
21446 : : with assigned stack slots, for -O1+ -g they should be tracked
21447 : : by VTA. */
21448 : 180751 : if (!ignored_p
21449 : 180751 : && TYPE_DOMAIN (type)
21450 : 361277 : && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
21451 : : {
21452 : 180526 : t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
21453 : 180526 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21454 : 0 : DECL_IGNORED_P (t) = 0;
21455 : 180526 : t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21456 : 180526 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
21457 : 18737 : DECL_IGNORED_P (t) = 0;
21458 : : }
21459 : : break;
21460 : :
21461 : 498933 : case RECORD_TYPE:
21462 : 498933 : case UNION_TYPE:
21463 : 498933 : case QUAL_UNION_TYPE:
21464 : 10915511 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
21465 : 10416578 : if (TREE_CODE (field) == FIELD_DECL)
21466 : : {
21467 : 1092841 : gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
21468 : : /* Likewise, ensure variable offsets aren't removed. */
21469 : 1092841 : if (!ignored_p
21470 : 1092841 : && (t = DECL_FIELD_OFFSET (field))
21471 : 1092829 : && VAR_P (t)
21472 : 1093003 : && DECL_ARTIFICIAL (t))
21473 : 162 : DECL_IGNORED_P (t) = 0;
21474 : 1092841 : gimplify_one_sizepos (&DECL_SIZE (field), list_p);
21475 : 1092841 : gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
21476 : 1092841 : gimplify_type_sizes (TREE_TYPE (field), list_p);
21477 : : }
21478 : : break;
21479 : :
21480 : : case POINTER_TYPE:
21481 : : case REFERENCE_TYPE:
21482 : : /* We used to recurse on the pointed-to type here, which turned out to
21483 : : be incorrect because its definition might refer to variables not
21484 : : yet initialized at this point if a forward declaration is involved.
21485 : :
21486 : : It was actually useful for anonymous pointed-to types to ensure
21487 : : that the sizes evaluation dominates every possible later use of the
21488 : : values. Restricting to such types here would be safe since there
21489 : : is no possible forward declaration around, but would introduce an
21490 : : undesirable middle-end semantic to anonymity. We then defer to
21491 : : front-ends the responsibility of ensuring that the sizes are
21492 : : evaluated both early and late enough, e.g. by attaching artificial
21493 : : type declarations to the tree. */
21494 : : break;
21495 : :
21496 : : default:
21497 : : break;
21498 : : }
21499 : :
21500 : 2489740 : gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
21501 : 2489740 : gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
21502 : :
21503 : 14789471 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21504 : : {
21505 : 12299731 : TYPE_SIZE (t) = TYPE_SIZE (type);
21506 : 12299731 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
21507 : 12299731 : TYPE_SIZES_GIMPLIFIED (t) = 1;
21508 : : }
21509 : : }
21510 : :
21511 : : /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
21512 : : a size or position, has had all of its SAVE_EXPRs evaluated.
21513 : : We add any required statements to *STMT_P. */
21514 : :
21515 : : void
21516 : 9255969 : gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
21517 : : {
21518 : 9255969 : tree expr = *expr_p;
21519 : :
21520 : : /* We don't do anything if the value isn't there, is constant, or contains
21521 : : A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
21522 : : a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
21523 : : will want to replace it with a new variable, but that will cause problems
21524 : : if this type is from outside the function. It's OK to have that here. */
21525 : 9255969 : if (expr == NULL_TREE
21526 : 9259739 : || is_gimple_constant (expr)
21527 : 79802 : || VAR_P (expr)
21528 : 9332001 : || CONTAINS_PLACEHOLDER_P (expr))
21529 : 9179937 : return;
21530 : :
21531 : 76032 : *expr_p = unshare_expr (expr);
21532 : :
21533 : : /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
21534 : : if the def vanishes. */
21535 : 76032 : gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
21536 : :
21537 : : /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
21538 : : FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
21539 : : as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
21540 : 76032 : if (is_gimple_constant (*expr_p))
21541 : 533 : *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
21542 : : }
21543 : :
21544 : : /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
21545 : : containing the sequence of corresponding GIMPLE statements. If DO_PARMS
21546 : : is true, also gimplify the parameters. */
21547 : :
21548 : : gbind *
21549 : 2980950 : gimplify_body (tree fndecl, bool do_parms)
21550 : : {
21551 : 2980950 : location_t saved_location = input_location;
21552 : 2980950 : gimple_seq parm_stmts, parm_cleanup = NULL, seq;
21553 : 2980950 : gimple *outer_stmt;
21554 : 2980950 : gbind *outer_bind;
21555 : :
21556 : 2980950 : timevar_push (TV_TREE_GIMPLIFY);
21557 : :
21558 : 2980950 : init_tree_ssa (cfun);
21559 : :
21560 : : /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
21561 : : gimplification. */
21562 : 2980950 : default_rtl_profile ();
21563 : :
21564 : 2980950 : gcc_assert (gimplify_ctxp == NULL);
21565 : 2980950 : push_gimplify_context (true);
21566 : :
21567 : 2980950 : if (flag_openacc || flag_openmp)
21568 : : {
21569 : 53589 : gcc_assert (gimplify_omp_ctxp == NULL);
21570 : 53589 : if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
21571 : 9795 : gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
21572 : : }
21573 : :
21574 : : /* Unshare most shared trees in the body and in that of any nested functions.
21575 : : It would seem we don't have to do this for nested functions because
21576 : : they are supposed to be output and then the outer function gimplified
21577 : : first, but the g++ front end doesn't always do it that way. */
21578 : 2980950 : unshare_body (fndecl);
21579 : 2980950 : unvisit_body (fndecl);
21580 : :
21581 : : /* Make sure input_location isn't set to something weird. */
21582 : 2980950 : input_location = DECL_SOURCE_LOCATION (fndecl);
21583 : :
21584 : : /* Resolve callee-copies. This has to be done before processing
21585 : : the body so that DECL_VALUE_EXPR gets processed correctly. */
21586 : 2980950 : parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
21587 : :
21588 : : /* Gimplify the function's body. */
21589 : 2980950 : seq = NULL;
21590 : 2980950 : gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
21591 : 2980950 : outer_stmt = gimple_seq_first_nondebug_stmt (seq);
21592 : 2980950 : if (!outer_stmt)
21593 : : {
21594 : 25364 : outer_stmt = gimple_build_nop ();
21595 : 25364 : gimplify_seq_add_stmt (&seq, outer_stmt);
21596 : : }
21597 : :
21598 : : /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
21599 : : not the case, wrap everything in a GIMPLE_BIND to make it so. */
21600 : 2980950 : if (gimple_code (outer_stmt) == GIMPLE_BIND
21601 : 2980950 : && (gimple_seq_first_nondebug_stmt (seq)
21602 : 1567226 : == gimple_seq_last_nondebug_stmt (seq)))
21603 : : {
21604 : 1504560 : outer_bind = as_a <gbind *> (outer_stmt);
21605 : 1504560 : if (gimple_seq_first_stmt (seq) != outer_stmt
21606 : 1504560 : || gimple_seq_last_stmt (seq) != outer_stmt)
21607 : : {
21608 : : /* If there are debug stmts before or after outer_stmt, move them
21609 : : inside of outer_bind body. */
21610 : 1 : gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
21611 : 1 : gimple_seq second_seq = NULL;
21612 : 1 : if (gimple_seq_first_stmt (seq) != outer_stmt
21613 : 2 : && gimple_seq_last_stmt (seq) != outer_stmt)
21614 : : {
21615 : 0 : second_seq = gsi_split_seq_after (gsi);
21616 : 0 : gsi_remove (&gsi, false);
21617 : : }
21618 : 1 : else if (gimple_seq_first_stmt (seq) != outer_stmt)
21619 : 1 : gsi_remove (&gsi, false);
21620 : : else
21621 : : {
21622 : 0 : gsi_remove (&gsi, false);
21623 : 0 : second_seq = seq;
21624 : 0 : seq = NULL;
21625 : : }
21626 : 1 : gimple_seq_add_seq_without_update (&seq,
21627 : : gimple_bind_body (outer_bind));
21628 : 1 : gimple_seq_add_seq_without_update (&seq, second_seq);
21629 : 1 : gimple_bind_set_body (outer_bind, seq);
21630 : : }
21631 : : }
21632 : : else
21633 : 1476390 : outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
21634 : :
21635 : 2980950 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21636 : :
21637 : : /* If we had callee-copies statements, insert them at the beginning
21638 : : of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters. */
21639 : 2980950 : if (!gimple_seq_empty_p (parm_stmts))
21640 : : {
21641 : 41 : tree parm;
21642 : :
21643 : 41 : gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
21644 : 41 : if (parm_cleanup)
21645 : : {
21646 : 0 : gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
21647 : : GIMPLE_TRY_FINALLY);
21648 : 0 : parm_stmts = NULL;
21649 : 0 : gimple_seq_add_stmt (&parm_stmts, g);
21650 : : }
21651 : 41 : gimple_bind_set_body (outer_bind, parm_stmts);
21652 : :
21653 : 41 : for (parm = DECL_ARGUMENTS (current_function_decl);
21654 : 101 : parm; parm = DECL_CHAIN (parm))
21655 : 60 : if (DECL_HAS_VALUE_EXPR_P (parm))
21656 : : {
21657 : 0 : DECL_HAS_VALUE_EXPR_P (parm) = 0;
21658 : 0 : DECL_IGNORED_P (parm) = 0;
21659 : : }
21660 : : }
21661 : :
21662 : 2980950 : if ((flag_openacc || flag_openmp || flag_openmp_simd)
21663 : 55823 : && gimplify_omp_ctxp)
21664 : : {
21665 : 9848 : delete_omp_context (gimplify_omp_ctxp);
21666 : 9848 : gimplify_omp_ctxp = NULL;
21667 : : }
21668 : :
21669 : 2980950 : pop_gimplify_context (outer_bind);
21670 : 2980950 : gcc_assert (gimplify_ctxp == NULL);
21671 : :
21672 : 2980950 : if (flag_checking && !seen_error ())
21673 : 2929998 : verify_gimple_in_seq (gimple_bind_body (outer_bind));
21674 : :
21675 : 2980950 : timevar_pop (TV_TREE_GIMPLIFY);
21676 : 2980950 : input_location = saved_location;
21677 : :
21678 : 2980950 : return outer_bind;
21679 : : }
21680 : :
21681 : : typedef char *char_p; /* For DEF_VEC_P. */
21682 : :
21683 : : /* Return whether we should exclude FNDECL from instrumentation. */
21684 : :
21685 : : static bool
21686 : 39 : flag_instrument_functions_exclude_p (tree fndecl)
21687 : : {
21688 : 39 : vec<char_p> *v;
21689 : :
21690 : 39 : v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
21691 : 40 : if (v && v->length () > 0)
21692 : : {
21693 : 1 : const char *name;
21694 : 1 : int i;
21695 : 1 : char *s;
21696 : :
21697 : 1 : name = lang_hooks.decl_printable_name (fndecl, 1);
21698 : 2 : FOR_EACH_VEC_ELT (*v, i, s)
21699 : 1 : if (strstr (name, s) != NULL)
21700 : 2 : return true;
21701 : : }
21702 : :
21703 : 38 : v = (vec<char_p> *) flag_instrument_functions_exclude_files;
21704 : 39 : if (v && v->length () > 0)
21705 : : {
21706 : 1 : const char *name;
21707 : 1 : int i;
21708 : 1 : char *s;
21709 : :
21710 : 1 : name = DECL_SOURCE_FILE (fndecl);
21711 : 1 : FOR_EACH_VEC_ELT (*v, i, s)
21712 : 1 : if (strstr (name, s) != NULL)
21713 : 2 : return true;
21714 : : }
21715 : :
21716 : : return false;
21717 : : }
21718 : :
21719 : : /* Build a call to the instrumentation function FNCODE and add it to SEQ.
21720 : : If COND_VAR is not NULL, it is a boolean variable guarding the call to
21721 : : the instrumentation function. IF STMT is not NULL, it is a statement
21722 : : to be executed just before the call to the instrumentation function. */
21723 : :
21724 : : static void
21725 : 74 : build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
21726 : : tree cond_var, gimple *stmt)
21727 : : {
21728 : : /* The instrumentation hooks aren't going to call the instrumented
21729 : : function and the address they receive is expected to be matchable
21730 : : against symbol addresses. Make sure we don't create a trampoline,
21731 : : in case the current function is nested. */
21732 : 74 : tree this_fn_addr = build_fold_addr_expr (current_function_decl);
21733 : 74 : TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
21734 : :
21735 : 74 : tree label_true, label_false;
21736 : 74 : if (cond_var)
21737 : : {
21738 : 16 : label_true = create_artificial_label (UNKNOWN_LOCATION);
21739 : 16 : label_false = create_artificial_label (UNKNOWN_LOCATION);
21740 : 16 : gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
21741 : : label_true, label_false);
21742 : 16 : gimplify_seq_add_stmt (seq, cond);
21743 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
21744 : 16 : gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
21745 : : NOT_TAKEN));
21746 : : }
21747 : :
21748 : 74 : if (stmt)
21749 : 8 : gimplify_seq_add_stmt (seq, stmt);
21750 : :
21751 : 74 : tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
21752 : 74 : gcall *call = gimple_build_call (x, 1, integer_zero_node);
21753 : 74 : tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
21754 : 74 : gimple_call_set_lhs (call, tmp_var);
21755 : 74 : gimplify_seq_add_stmt (seq, call);
21756 : 74 : x = builtin_decl_implicit (fncode);
21757 : 74 : call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
21758 : 74 : gimplify_seq_add_stmt (seq, call);
21759 : :
21760 : 74 : if (cond_var)
21761 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
21762 : 74 : }
21763 : :
21764 : : /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
21765 : : node for the function we want to gimplify.
21766 : :
21767 : : Return the sequence of GIMPLE statements corresponding to the body
21768 : : of FNDECL. */
21769 : :
21770 : : void
21771 : 2980432 : gimplify_function_tree (tree fndecl)
21772 : : {
21773 : 2980432 : gimple_seq seq;
21774 : 2980432 : gbind *bind;
21775 : :
21776 : 2980432 : gcc_assert (!gimple_body (fndecl));
21777 : :
21778 : 2980432 : if (DECL_STRUCT_FUNCTION (fndecl))
21779 : 2976921 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
21780 : : else
21781 : 3511 : push_struct_function (fndecl);
21782 : :
21783 : 2980432 : reset_cond_uid ();
21784 : 2980432 : if (cond_uids)
21785 : : {
21786 : 65 : delete cond_uids;
21787 : 65 : cond_uids = NULL;
21788 : : }
21789 : :
21790 : : /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
21791 : : if necessary. */
21792 : 2980432 : cfun->curr_properties |= PROP_gimple_lva;
21793 : :
21794 : 2980432 : if (asan_sanitize_use_after_scope ())
21795 : 8159 : asan_poisoned_variables = new hash_set<tree> ();
21796 : 2980432 : if (flag_openmp)
21797 : 45929 : omp_resolved_variant_calls = new hash_set<tree> ();
21798 : :
21799 : 2980432 : bind = gimplify_body (fndecl, true);
21800 : :
21801 : 2980432 : if (omp_resolved_variant_calls)
21802 : : {
21803 : 45929 : delete omp_resolved_variant_calls;
21804 : 45929 : omp_resolved_variant_calls = NULL;
21805 : : }
21806 : 2980432 : if (asan_poisoned_variables)
21807 : : {
21808 : 8159 : delete asan_poisoned_variables;
21809 : 8159 : asan_poisoned_variables = NULL;
21810 : : }
21811 : :
21812 : : /* The tree body of the function is no longer needed, replace it
21813 : : with the new GIMPLE body. */
21814 : 2980432 : seq = NULL;
21815 : 2980432 : gimple_seq_add_stmt (&seq, bind);
21816 : 2980432 : gimple_set_body (fndecl, seq);
21817 : :
21818 : : /* If we're instrumenting function entry/exit, then prepend the call to
21819 : : the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
21820 : : catch the exit hook. */
21821 : : /* ??? Add some way to ignore exceptions for this TFE. */
21822 : 2980432 : if (flag_instrument_function_entry_exit
21823 : 91 : && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
21824 : : /* Do not instrument extern inline functions. */
21825 : 40 : && !(DECL_DECLARED_INLINE_P (fndecl)
21826 : 5 : && DECL_EXTERNAL (fndecl)
21827 : 1 : && DECL_DISREGARD_INLINE_LIMITS (fndecl))
21828 : 2980471 : && !flag_instrument_functions_exclude_p (fndecl))
21829 : : {
21830 : 37 : gimple_seq body = NULL, cleanup = NULL;
21831 : 37 : gassign *assign;
21832 : 37 : tree cond_var;
21833 : :
21834 : : /* If -finstrument-functions-once is specified, generate:
21835 : :
21836 : : static volatile bool C.0 = false;
21837 : : bool tmp_called;
21838 : :
21839 : : tmp_called = C.0;
21840 : : if (!tmp_called)
21841 : : {
21842 : : C.0 = true;
21843 : : [call profiling enter function]
21844 : : }
21845 : :
21846 : : without specific protection for data races. */
21847 : 37 : if (flag_instrument_function_entry_exit > 1)
21848 : : {
21849 : 8 : tree first_var
21850 : 8 : = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
21851 : : VAR_DECL,
21852 : : create_tmp_var_name ("C"),
21853 : : boolean_type_node);
21854 : 8 : DECL_ARTIFICIAL (first_var) = 1;
21855 : 8 : DECL_IGNORED_P (first_var) = 1;
21856 : 8 : TREE_STATIC (first_var) = 1;
21857 : 8 : TREE_THIS_VOLATILE (first_var) = 1;
21858 : 8 : TREE_USED (first_var) = 1;
21859 : 8 : DECL_INITIAL (first_var) = boolean_false_node;
21860 : 8 : varpool_node::add (first_var);
21861 : :
21862 : 8 : cond_var = create_tmp_var (boolean_type_node, "tmp_called");
21863 : 8 : assign = gimple_build_assign (cond_var, first_var);
21864 : 8 : gimplify_seq_add_stmt (&body, assign);
21865 : :
21866 : 8 : assign = gimple_build_assign (first_var, boolean_true_node);
21867 : : }
21868 : :
21869 : : else
21870 : : {
21871 : : cond_var = NULL_TREE;
21872 : : assign = NULL;
21873 : : }
21874 : :
21875 : 37 : build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
21876 : : cond_var, assign);
21877 : :
21878 : : /* If -finstrument-functions-once is specified, generate:
21879 : :
21880 : : if (!tmp_called)
21881 : : [call profiling exit function]
21882 : :
21883 : : without specific protection for data races. */
21884 : 37 : build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
21885 : : cond_var, NULL);
21886 : :
21887 : 37 : gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
21888 : 37 : gimplify_seq_add_stmt (&body, tf);
21889 : 37 : gbind *new_bind = gimple_build_bind (NULL, body, NULL);
21890 : :
21891 : : /* Replace the current function body with the body
21892 : : wrapped in the try/finally TF. */
21893 : 37 : seq = NULL;
21894 : 37 : gimple_seq_add_stmt (&seq, new_bind);
21895 : 37 : gimple_set_body (fndecl, seq);
21896 : 37 : bind = new_bind;
21897 : : }
21898 : :
21899 : 2980432 : if (sanitize_flags_p (SANITIZE_THREAD)
21900 : 2980432 : && param_tsan_instrument_func_entry_exit)
21901 : : {
21902 : 974 : gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
21903 : 974 : gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
21904 : 974 : gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
21905 : : /* Replace the current function body with the body
21906 : : wrapped in the try/finally TF. */
21907 : 974 : seq = NULL;
21908 : 974 : gimple_seq_add_stmt (&seq, new_bind);
21909 : 974 : gimple_set_body (fndecl, seq);
21910 : : }
21911 : :
21912 : 2980432 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21913 : 2980432 : cfun->curr_properties |= PROP_gimple_any;
21914 : :
21915 : 2980432 : pop_cfun ();
21916 : :
21917 : 2980432 : dump_function (TDI_gimple, fndecl);
21918 : 2980432 : }
21919 : :
21920 : : /* Return a dummy expression of type TYPE in order to keep going after an
21921 : : error. */
21922 : :
21923 : : static tree
21924 : 30 : dummy_object (tree type)
21925 : : {
21926 : 30 : tree t = build_int_cst (build_pointer_type (type), 0);
21927 : 30 : return build2 (MEM_REF, type, t, t);
21928 : : }
21929 : :
21930 : : /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
21931 : : builtin function, but a very special sort of operator. */
21932 : :
21933 : : enum gimplify_status
21934 : 50810 : gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
21935 : : gimple_seq *post_p ATTRIBUTE_UNUSED)
21936 : : {
21937 : 50810 : tree promoted_type, have_va_type;
21938 : 50810 : tree valist = TREE_OPERAND (*expr_p, 0);
21939 : 50810 : tree type = TREE_TYPE (*expr_p);
21940 : 50810 : tree t, tag, aptag;
21941 : 50810 : location_t loc = EXPR_LOCATION (*expr_p);
21942 : :
21943 : : /* Verify that valist is of the proper type. */
21944 : 50810 : have_va_type = TREE_TYPE (valist);
21945 : 50810 : if (have_va_type == error_mark_node)
21946 : : return GS_ERROR;
21947 : 50791 : have_va_type = targetm.canonical_va_list_type (have_va_type);
21948 : 50791 : if (have_va_type == NULL_TREE
21949 : 50791 : && POINTER_TYPE_P (TREE_TYPE (valist)))
21950 : : /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg. */
21951 : 258 : have_va_type
21952 : 258 : = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
21953 : 50791 : gcc_assert (have_va_type != NULL_TREE);
21954 : :
21955 : : /* Generate a diagnostic for requesting data of a type that cannot
21956 : : be passed through `...' due to type promotion at the call site. */
21957 : 50791 : if ((promoted_type = lang_hooks.types.type_promotes_to (type))
21958 : : != type)
21959 : : {
21960 : 30 : static bool gave_help;
21961 : 30 : bool warned;
21962 : : /* Use the expansion point to handle cases such as passing bool (defined
21963 : : in a system header) through `...'. */
21964 : 30 : location_t xloc
21965 : 30 : = expansion_point_location_if_in_system_header (loc);
21966 : :
21967 : : /* Unfortunately, this is merely undefined, rather than a constraint
21968 : : violation, so we cannot make this an error. If this call is never
21969 : : executed, the program is still strictly conforming. */
21970 : 30 : auto_diagnostic_group d;
21971 : 30 : warned = warning_at (xloc, 0,
21972 : : "%qT is promoted to %qT when passed through %<...%>",
21973 : : type, promoted_type);
21974 : 30 : if (!gave_help && warned)
21975 : : {
21976 : 15 : gave_help = true;
21977 : 15 : inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
21978 : : promoted_type, type);
21979 : : }
21980 : :
21981 : : /* We can, however, treat "undefined" any way we please.
21982 : : Call abort to encourage the user to fix the program. */
21983 : 23 : if (warned)
21984 : 23 : inform (xloc, "if this code is reached, the program will abort");
21985 : : /* Before the abort, allow the evaluation of the va_list
21986 : : expression to exit or longjmp. */
21987 : 30 : gimplify_and_add (valist, pre_p);
21988 : 60 : t = build_call_expr_loc (loc,
21989 : : builtin_decl_implicit (BUILT_IN_TRAP), 0);
21990 : 30 : gimplify_and_add (t, pre_p);
21991 : :
21992 : : /* This is dead code, but go ahead and finish so that the
21993 : : mode of the result comes out right. */
21994 : 30 : *expr_p = dummy_object (type);
21995 : 30 : return GS_ALL_DONE;
21996 : 30 : }
21997 : :
21998 : 50761 : tag = build_int_cst (build_pointer_type (type), 0);
21999 : 50761 : aptag = build_int_cst (TREE_TYPE (valist), 0);
22000 : :
22001 : 50761 : *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
22002 : : valist, tag, aptag);
22003 : :
22004 : : /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
22005 : : needs to be expanded. */
22006 : 50761 : cfun->curr_properties &= ~PROP_gimple_lva;
22007 : :
22008 : 50761 : return GS_OK;
22009 : : }
22010 : :
22011 : : /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
22012 : :
22013 : : DST/SRC are the destination and source respectively. You can pass
22014 : : ungimplified trees in DST or SRC, in which case they will be
22015 : : converted to a gimple operand if necessary.
22016 : :
22017 : : This function returns the newly created GIMPLE_ASSIGN tuple. */
22018 : :
22019 : : gimple *
22020 : 736624 : gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
22021 : : {
22022 : 736624 : tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
22023 : 736624 : gimplify_and_add (t, seq_p);
22024 : 736624 : ggc_free (t);
22025 : 736624 : return gimple_seq_last_stmt (*seq_p);
22026 : : }
22027 : :
22028 : : inline hashval_t
22029 : 1573026 : gimplify_hasher::hash (const elt_t *p)
22030 : : {
22031 : 1573026 : tree t = p->val;
22032 : 1573026 : return iterative_hash_expr (t, 0);
22033 : : }
22034 : :
22035 : : inline bool
22036 : 593871 : gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
22037 : : {
22038 : 593871 : tree t1 = p1->val;
22039 : 593871 : tree t2 = p2->val;
22040 : 593871 : enum tree_code code = TREE_CODE (t1);
22041 : :
22042 : 593871 : if (TREE_CODE (t2) != code
22043 : 593871 : || TREE_TYPE (t1) != TREE_TYPE (t2))
22044 : : return false;
22045 : :
22046 : 355004 : if (!operand_equal_p (t1, t2, 0))
22047 : : return false;
22048 : :
22049 : : return true;
22050 : : }
|