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 : :
75 : : /* Identifier for a basic condition, mapping it to other basic conditions of
76 : : its Boolean expression. Basic conditions given the same uid (in the same
77 : : function) are parts of the same ANDIF/ORIF expression. Used for condition
78 : : coverage. */
79 : : static unsigned nextconduid = 1;
80 : :
81 : : /* Annotated gconds so that basic conditions in the same expression map to
82 : : the same uid. This is used for condition coverage. */
83 : : static hash_map <tree, unsigned> *cond_uids;
84 : :
85 : : /* Get a fresh identifier for a new condition expression. This is used for
86 : : condition coverage. */
87 : : static unsigned
88 : 5726805 : next_cond_uid ()
89 : : {
90 : 5726805 : return nextconduid++;
91 : : }
92 : :
93 : : /* Reset the condition uid to the value it should have when compiling a new
94 : : function. 0 is already the default/untouched value, so start at non-zero.
95 : : A valid and set id should always be > 0. This is used for condition
96 : : coverage. */
97 : : static void
98 : 2896723 : reset_cond_uid ()
99 : : {
100 : 2896723 : nextconduid = 1;
101 : 0 : }
102 : :
103 : : /* Associate the condition STMT with the discriminator UID. STMTs that are
104 : : broken down with ANDIF/ORIF from the same Boolean expression should be given
105 : : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
106 : : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
107 : : for condition coverage. */
108 : : static void
109 : 1218216 : tree_associate_condition_with_expr (tree stmt, unsigned uid)
110 : : {
111 : 1218216 : if (!condition_coverage_flag)
112 : : return;
113 : :
114 : 527 : if (!cond_uids)
115 : 65 : cond_uids = new hash_map <tree, unsigned> ();
116 : :
117 : 527 : cond_uids->put (stmt, uid);
118 : : }
119 : :
120 : : /* Hash set of poisoned variables in a bind expr. */
121 : : static hash_set<tree> *asan_poisoned_variables = NULL;
122 : :
123 : : /* Hash set of already-resolved calls to OpenMP "declare variant"
124 : : functions. A call can resolve to the original function and
125 : : we don't want to repeat the resolution multiple times. */
126 : : static hash_set<tree> *omp_resolved_variant_calls = NULL;
127 : :
128 : : enum gimplify_omp_var_data
129 : : {
130 : : GOVD_SEEN = 0x000001,
131 : : GOVD_EXPLICIT = 0x000002,
132 : : GOVD_SHARED = 0x000004,
133 : : GOVD_PRIVATE = 0x000008,
134 : : GOVD_FIRSTPRIVATE = 0x000010,
135 : : GOVD_LASTPRIVATE = 0x000020,
136 : : GOVD_REDUCTION = 0x000040,
137 : : GOVD_LOCAL = 0x00080,
138 : : GOVD_MAP = 0x000100,
139 : : GOVD_DEBUG_PRIVATE = 0x000200,
140 : : GOVD_PRIVATE_OUTER_REF = 0x000400,
141 : : GOVD_LINEAR = 0x000800,
142 : : GOVD_ALIGNED = 0x001000,
143 : :
144 : : /* Flag for GOVD_MAP: don't copy back. */
145 : : GOVD_MAP_TO_ONLY = 0x002000,
146 : :
147 : : /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
148 : : GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 0x004000,
149 : :
150 : : GOVD_MAP_0LEN_ARRAY = 0x008000,
151 : :
152 : : /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
153 : : GOVD_MAP_ALWAYS_TO = 0x010000,
154 : :
155 : : /* Flag for shared vars that are or might be stored to in the region. */
156 : : GOVD_WRITTEN = 0x020000,
157 : :
158 : : /* Flag for GOVD_MAP, if it is a forced mapping. */
159 : : GOVD_MAP_FORCE = 0x040000,
160 : :
161 : : /* Flag for GOVD_MAP: must be present already. */
162 : : GOVD_MAP_FORCE_PRESENT = 0x080000,
163 : :
164 : : /* Flag for GOVD_MAP: only allocate. */
165 : : GOVD_MAP_ALLOC_ONLY = 0x100000,
166 : :
167 : : /* Flag for GOVD_MAP: only copy back. */
168 : : GOVD_MAP_FROM_ONLY = 0x200000,
169 : :
170 : : GOVD_NONTEMPORAL = 0x400000,
171 : :
172 : : /* Flag for GOVD_LASTPRIVATE: conditional modifier. */
173 : : GOVD_LASTPRIVATE_CONDITIONAL = 0x800000,
174 : :
175 : : GOVD_CONDTEMP = 0x1000000,
176 : :
177 : : /* Flag for GOVD_REDUCTION: inscan seen in {in,ex}clusive clause. */
178 : : GOVD_REDUCTION_INSCAN = 0x2000000,
179 : :
180 : : /* Flag for GOVD_FIRSTPRIVATE: OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT. */
181 : : GOVD_FIRSTPRIVATE_IMPLICIT = 0x4000000,
182 : :
183 : : GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
184 : : | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
185 : : | GOVD_LOCAL)
186 : : };
187 : :
188 : :
189 : : enum omp_region_type
190 : : {
191 : : ORT_WORKSHARE = 0x00,
192 : : ORT_TASKGROUP = 0x01,
193 : : ORT_DISPATCH = 0x02,
194 : : ORT_SIMD = 0x04,
195 : :
196 : : ORT_PARALLEL = 0x08,
197 : : ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
198 : :
199 : : ORT_TASK = 0x10,
200 : : ORT_UNTIED_TASK = ORT_TASK | 1,
201 : : ORT_TASKLOOP = ORT_TASK | 2,
202 : : ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
203 : :
204 : : ORT_TEAMS = 0x20,
205 : : ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
206 : : ORT_HOST_TEAMS = ORT_TEAMS | 2,
207 : : ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
208 : :
209 : : /* Data region. */
210 : : ORT_TARGET_DATA = 0x40,
211 : :
212 : : /* Data region with offloading. */
213 : : ORT_TARGET = 0x80,
214 : : ORT_COMBINED_TARGET = ORT_TARGET | 1,
215 : : ORT_IMPLICIT_TARGET = ORT_TARGET | 2,
216 : :
217 : : /* OpenACC variants. */
218 : : ORT_ACC = 0x100, /* A generic OpenACC region. */
219 : : ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
220 : : ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
221 : : ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
222 : : ORT_ACC_SERIAL = ORT_ACC | ORT_TARGET | 4, /* Serial construct. */
223 : : ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
224 : :
225 : : /* Dummy OpenMP region, used to disable expansion of
226 : : DECL_VALUE_EXPRs in taskloop pre body. */
227 : : ORT_NONE = 0x200
228 : : };
229 : :
230 : : /* Gimplify hashtable helper. */
231 : :
232 : : struct gimplify_hasher : free_ptr_hash <elt_t>
233 : : {
234 : : static inline hashval_t hash (const elt_t *);
235 : : static inline bool equal (const elt_t *, const elt_t *);
236 : : };
237 : :
238 : : struct gimplify_ctx
239 : : {
240 : : struct gimplify_ctx *prev_context;
241 : :
242 : : vec<gbind *> bind_expr_stack;
243 : : tree temps;
244 : : gimple_seq conditional_cleanups;
245 : : tree exit_label;
246 : : tree return_temp;
247 : :
248 : : vec<tree> case_labels;
249 : : hash_set<tree> *live_switch_vars;
250 : : /* The formal temporary table. Should this be persistent? */
251 : : hash_table<gimplify_hasher> *temp_htab;
252 : :
253 : : int conditions;
254 : : unsigned into_ssa : 1;
255 : : unsigned allow_rhs_cond_expr : 1;
256 : : unsigned in_cleanup_point_expr : 1;
257 : : unsigned keep_stack : 1;
258 : : unsigned save_stack : 1;
259 : : unsigned in_switch_expr : 1;
260 : : unsigned in_handler_expr : 1;
261 : : };
262 : :
263 : : enum gimplify_defaultmap_kind
264 : : {
265 : : GDMK_SCALAR,
266 : : GDMK_SCALAR_TARGET, /* w/ Fortran's target attr, implicit mapping, only. */
267 : : GDMK_AGGREGATE,
268 : : GDMK_ALLOCATABLE,
269 : : GDMK_POINTER
270 : : };
271 : :
272 : : struct gimplify_omp_ctx
273 : : {
274 : : struct gimplify_omp_ctx *outer_context;
275 : : splay_tree variables;
276 : : hash_map<omp_name_type<tree>, tree> *implicit_mappers;
277 : : hash_set<tree> *privatized_types;
278 : : tree clauses;
279 : : /* Iteration variables in an OMP_FOR. */
280 : : vec<tree> loop_iter_var;
281 : : location_t location;
282 : : enum omp_clause_default_kind default_kind;
283 : : enum omp_region_type region_type;
284 : : enum tree_code code;
285 : : bool combined_loop;
286 : : bool distribute;
287 : : bool target_firstprivatize_array_bases;
288 : : bool add_safelen1;
289 : : bool order_concurrent;
290 : : bool has_depend;
291 : : bool in_for_exprs;
292 : : bool in_call_args;
293 : : int defaultmap[5];
294 : : };
295 : :
296 : : static struct gimplify_ctx *gimplify_ctxp;
297 : : static struct gimplify_omp_ctx *gimplify_omp_ctxp;
298 : : static bool in_omp_construct;
299 : :
300 : : /* Forward declaration. */
301 : : static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
302 : : static hash_map<tree, tree> *oacc_declare_returns;
303 : : static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
304 : : bool (*) (tree), fallback_t, bool);
305 : : static void prepare_gimple_addressable (tree *, gimple_seq *);
306 : :
307 : : /* Shorter alias name for the above function for use in gimplify.cc
308 : : only. */
309 : :
310 : : static inline void
311 : 92008003 : gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
312 : : {
313 : 92008003 : gimple_seq_add_stmt_without_update (seq_p, gs);
314 : 6507881 : }
315 : :
316 : : /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
317 : : NULL, a new sequence is allocated. This function is
318 : : similar to gimple_seq_add_seq, but does not scan the operands.
319 : : During gimplification, we need to manipulate statement sequences
320 : : before the def/use vectors have been constructed. */
321 : :
322 : : static void
323 : 9674132 : gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
324 : : {
325 : 9674132 : gimple_stmt_iterator si;
326 : :
327 : 9674132 : if (src == NULL)
328 : 3976384 : return;
329 : :
330 : 5697748 : si = gsi_last (*dst_p);
331 : 5697748 : gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
332 : : }
333 : :
334 : :
335 : : /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
336 : : and popping gimplify contexts. */
337 : :
338 : : static struct gimplify_ctx *ctx_pool = NULL;
339 : :
340 : : /* Return a gimplify context struct from the pool. */
341 : :
342 : : static inline struct gimplify_ctx *
343 : 8877625 : ctx_alloc (void)
344 : : {
345 : 8877625 : struct gimplify_ctx * c = ctx_pool;
346 : :
347 : 8877625 : if (c)
348 : 8617923 : ctx_pool = c->prev_context;
349 : : else
350 : 259702 : c = XNEW (struct gimplify_ctx);
351 : :
352 : 8877625 : memset (c, '\0', sizeof (*c));
353 : 8877625 : return c;
354 : : }
355 : :
356 : : /* Put gimplify context C back into the pool. */
357 : :
358 : : static inline void
359 : 8877621 : ctx_free (struct gimplify_ctx *c)
360 : : {
361 : 8877621 : c->prev_context = ctx_pool;
362 : 8877621 : ctx_pool = c;
363 : : }
364 : :
365 : : /* Free allocated ctx stack memory. */
366 : :
367 : : void
368 : 228619 : free_gimplify_stack (void)
369 : : {
370 : 228619 : struct gimplify_ctx *c;
371 : :
372 : 457842 : while ((c = ctx_pool))
373 : : {
374 : 229223 : ctx_pool = c->prev_context;
375 : 229223 : free (c);
376 : : }
377 : 228619 : }
378 : :
379 : :
380 : : /* Set up a context for the gimplifier. */
381 : :
382 : : void
383 : 8877625 : push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
384 : : {
385 : 8877625 : struct gimplify_ctx *c = ctx_alloc ();
386 : :
387 : 8877625 : c->prev_context = gimplify_ctxp;
388 : 8877625 : gimplify_ctxp = c;
389 : 8877625 : gimplify_ctxp->into_ssa = in_ssa;
390 : 8877625 : gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
391 : 8877625 : }
392 : :
393 : : /* Tear down a context for the gimplifier. If BODY is non-null, then
394 : : put the temporaries into the outer BIND_EXPR. Otherwise, put them
395 : : in the local_decls.
396 : :
397 : : BODY is not a sequence, but the first tuple in a sequence. */
398 : :
399 : : void
400 : 8877621 : pop_gimplify_context (gimple *body)
401 : : {
402 : 8877621 : struct gimplify_ctx *c = gimplify_ctxp;
403 : :
404 : 8877621 : gcc_assert (c
405 : : && (!c->bind_expr_stack.exists ()
406 : : || c->bind_expr_stack.is_empty ()));
407 : 8877621 : c->bind_expr_stack.release ();
408 : 8877621 : gimplify_ctxp = c->prev_context;
409 : :
410 : 8877621 : if (body)
411 : 3020395 : declare_vars (c->temps, body, false);
412 : : else
413 : 5857226 : record_vars (c->temps);
414 : :
415 : 8877621 : delete c->temp_htab;
416 : 8877621 : c->temp_htab = NULL;
417 : 8877621 : ctx_free (c);
418 : 8877621 : }
419 : :
420 : : /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
421 : :
422 : : static void
423 : 5961975 : gimple_push_bind_expr (gbind *bind_stmt)
424 : : {
425 : 5961975 : gimplify_ctxp->bind_expr_stack.reserve (8);
426 : 5961975 : gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
427 : 5961975 : }
428 : :
429 : : /* Pop the first element off the stack of bindings. */
430 : :
431 : : static void
432 : 5961975 : gimple_pop_bind_expr (void)
433 : : {
434 : 0 : gimplify_ctxp->bind_expr_stack.pop ();
435 : 0 : }
436 : :
437 : : /* Return the first element of the stack of bindings. */
438 : :
439 : : gbind *
440 : 0 : gimple_current_bind_expr (void)
441 : : {
442 : 0 : return gimplify_ctxp->bind_expr_stack.last ();
443 : : }
444 : :
445 : : /* Return the stack of bindings created during gimplification. */
446 : :
447 : : vec<gbind *>
448 : 331 : gimple_bind_expr_stack (void)
449 : : {
450 : 331 : return gimplify_ctxp->bind_expr_stack;
451 : : }
452 : :
453 : : /* Return true iff there is a COND_EXPR between us and the innermost
454 : : CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
455 : :
456 : : static bool
457 : 2870269 : gimple_conditional_context (void)
458 : : {
459 : 2870269 : return gimplify_ctxp->conditions > 0;
460 : : }
461 : :
462 : : /* Note that we've entered a COND_EXPR. */
463 : :
464 : : static void
465 : 5727240 : gimple_push_condition (void)
466 : : {
467 : : #ifdef ENABLE_GIMPLE_CHECKING
468 : 5727240 : if (gimplify_ctxp->conditions == 0)
469 : 3756363 : gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
470 : : #endif
471 : 5727240 : ++(gimplify_ctxp->conditions);
472 : 5727240 : }
473 : :
474 : : /* Note that we've left a COND_EXPR. If we're back at unconditional scope
475 : : now, add any conditional cleanups we've seen to the prequeue. */
476 : :
477 : : static void
478 : 5727240 : gimple_pop_condition (gimple_seq *pre_p)
479 : : {
480 : 5727240 : int conds = --(gimplify_ctxp->conditions);
481 : :
482 : 5727240 : gcc_assert (conds >= 0);
483 : 5727240 : if (conds == 0)
484 : : {
485 : 3756363 : gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
486 : 3756363 : gimplify_ctxp->conditional_cleanups = NULL;
487 : : }
488 : 5727240 : }
489 : :
490 : : /* A stable comparison routine for use with splay trees and DECLs. */
491 : :
492 : : static int
493 : 17898179 : splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
494 : : {
495 : 17898179 : tree a = (tree) xa;
496 : 17898179 : tree b = (tree) xb;
497 : :
498 : 17898179 : return DECL_UID (a) - DECL_UID (b);
499 : : }
500 : :
501 : : /* Create a new omp construct that deals with variable remapping. */
502 : :
503 : : static struct gimplify_omp_ctx *
504 : 139158 : new_omp_context (enum omp_region_type region_type)
505 : : {
506 : 139158 : struct gimplify_omp_ctx *c;
507 : :
508 : 139158 : c = XCNEW (struct gimplify_omp_ctx);
509 : 139158 : c->outer_context = gimplify_omp_ctxp;
510 : 139158 : c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
511 : 139158 : c->implicit_mappers = new hash_map<omp_name_type<tree>, tree>;
512 : 139158 : c->privatized_types = new hash_set<tree>;
513 : 139158 : c->location = input_location;
514 : 139158 : c->region_type = region_type;
515 : 139158 : if ((region_type & ORT_TASK) == 0)
516 : 132958 : c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
517 : : else
518 : 6200 : c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
519 : 139158 : c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
520 : 139158 : c->defaultmap[GDMK_SCALAR_TARGET] = GOVD_MAP;
521 : 139158 : c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
522 : 139158 : c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
523 : 139158 : c->defaultmap[GDMK_POINTER] = GOVD_MAP;
524 : :
525 : 139158 : return c;
526 : : }
527 : :
528 : : /* Destroy an omp construct that deals with variable remapping. */
529 : :
530 : : static void
531 : 138599 : delete_omp_context (struct gimplify_omp_ctx *c)
532 : : {
533 : 138599 : splay_tree_delete (c->variables);
534 : 277198 : delete c->privatized_types;
535 : 277198 : delete c->implicit_mappers;
536 : 138599 : c->loop_iter_var.release ();
537 : 138599 : XDELETE (c);
538 : 138599 : }
539 : :
540 : : static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
541 : : static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
542 : :
543 : : /* Both gimplify the statement T and append it to *SEQ_P. This function
544 : : behaves exactly as gimplify_stmt, but you don't have to pass T as a
545 : : reference. */
546 : :
547 : : void
548 : 37699785 : gimplify_and_add (tree t, gimple_seq *seq_p)
549 : : {
550 : 37699785 : gimplify_stmt (&t, seq_p);
551 : 37699785 : }
552 : :
553 : : /* Gimplify statement T into sequence *SEQ_P, and return the first
554 : : tuple in the sequence of generated tuples for this statement.
555 : : Return NULL if gimplifying T produced no tuples. */
556 : :
557 : : static gimple *
558 : 104578 : gimplify_and_return_first (tree t, gimple_seq *seq_p)
559 : : {
560 : 104578 : gimple_stmt_iterator last = gsi_last (*seq_p);
561 : :
562 : 104578 : gimplify_and_add (t, seq_p);
563 : :
564 : 104578 : if (!gsi_end_p (last))
565 : : {
566 : 4870 : gsi_next (&last);
567 : 4870 : return gsi_stmt (last);
568 : : }
569 : : else
570 : 99708 : return gimple_seq_first_stmt (*seq_p);
571 : : }
572 : :
573 : : /* Returns true iff T is a valid RHS for an assignment to an un-renamed
574 : : LHS, or for a call argument. */
575 : :
576 : : static bool
577 : 235667 : is_gimple_mem_rhs (tree t)
578 : : {
579 : : /* If we're dealing with a renamable type, either source or dest must be
580 : : a renamed variable. */
581 : 235667 : if (is_gimple_reg_type (TREE_TYPE (t)))
582 : 232323 : return is_gimple_val (t);
583 : : else
584 : 3344 : return is_gimple_val (t) || is_gimple_lvalue (t);
585 : : }
586 : :
587 : : /* Return true if T is a CALL_EXPR or an expression that can be
588 : : assigned to a temporary. Note that this predicate should only be
589 : : used during gimplification. See the rationale for this in
590 : : gimplify_modify_expr. */
591 : :
592 : : static bool
593 : 96635427 : is_gimple_reg_rhs_or_call (tree t)
594 : : {
595 : 70880834 : return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
596 : 96635427 : || TREE_CODE (t) == CALL_EXPR);
597 : : }
598 : :
599 : : /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
600 : : this predicate should only be used during gimplification. See the
601 : : rationale for this in gimplify_modify_expr. */
602 : :
603 : : static bool
604 : 14228931 : is_gimple_mem_rhs_or_call (tree t)
605 : : {
606 : : /* If we're dealing with a renamable type, either source or dest must be
607 : : a renamed variable. */
608 : 14228931 : if (is_gimple_reg_type (TREE_TYPE (t)))
609 : 10809269 : return is_gimple_val (t);
610 : : else
611 : 3419662 : return (is_gimple_val (t)
612 : 1707142 : || is_gimple_lvalue (t)
613 : 1185609 : || (TREE_CODE (t) == CONSTRUCTOR && CONSTRUCTOR_NELTS (t) == 0)
614 : 4604791 : || TREE_CODE (t) == CALL_EXPR);
615 : : }
616 : :
617 : : /* Create a temporary with a name derived from VAL. Subroutine of
618 : : lookup_tmp_var; nobody else should call this function. */
619 : :
620 : : static inline tree
621 : 2121308 : create_tmp_from_val (tree val)
622 : : {
623 : : /* Drop all qualifiers and address-space information from the value type. */
624 : 2121308 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
625 : 2121308 : tree var = create_tmp_var (type, get_name (val));
626 : 2121308 : return var;
627 : : }
628 : :
629 : : /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
630 : : an existing expression temporary. If NOT_GIMPLE_REG, mark it as such. */
631 : :
632 : : static tree
633 : 2348889 : lookup_tmp_var (tree val, bool is_formal, bool not_gimple_reg)
634 : : {
635 : 2348889 : tree ret;
636 : :
637 : : /* We cannot mark a formal temporary with DECL_NOT_GIMPLE_REG_P. */
638 : 2348889 : gcc_assert (!is_formal || !not_gimple_reg);
639 : :
640 : : /* If not optimizing, never really reuse a temporary. local-alloc
641 : : won't allocate any variable that is used in more than one basic
642 : : block, which means it will go into memory, causing much extra
643 : : work in reload and final and poorer code generation, outweighing
644 : : the extra memory allocation here. */
645 : 2348889 : if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
646 : : {
647 : 1082127 : ret = create_tmp_from_val (val);
648 : 1082127 : DECL_NOT_GIMPLE_REG_P (ret) = not_gimple_reg;
649 : : }
650 : : else
651 : : {
652 : 1266762 : elt_t elt, *elt_p;
653 : 1266762 : elt_t **slot;
654 : :
655 : 1266762 : elt.val = val;
656 : 1266762 : if (!gimplify_ctxp->temp_htab)
657 : 388578 : gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
658 : 1266762 : slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
659 : 1266762 : if (*slot == NULL)
660 : : {
661 : 1039181 : elt_p = XNEW (elt_t);
662 : 1039181 : elt_p->val = val;
663 : 1039181 : elt_p->temp = ret = create_tmp_from_val (val);
664 : 1039181 : *slot = elt_p;
665 : : }
666 : : else
667 : : {
668 : 227581 : elt_p = *slot;
669 : 227581 : ret = elt_p->temp;
670 : : }
671 : : }
672 : :
673 : 2348889 : return ret;
674 : : }
675 : :
676 : : /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
677 : :
678 : : static tree
679 : 26278302 : internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
680 : : bool is_formal, bool allow_ssa, bool not_gimple_reg)
681 : : {
682 : 26278302 : tree t, mod;
683 : :
684 : : /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
685 : : can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
686 : 26278302 : gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
687 : : fb_rvalue);
688 : :
689 : 26278302 : if (allow_ssa
690 : 26027470 : && gimplify_ctxp->into_ssa
691 : 50888413 : && is_gimple_reg_type (TREE_TYPE (val)))
692 : : {
693 : 23929414 : t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
694 : 23929414 : if (! gimple_in_ssa_p (cfun))
695 : : {
696 : 20656485 : const char *name = get_name (val);
697 : 20656485 : if (name)
698 : 6841690 : SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
699 : : }
700 : : }
701 : : else
702 : 2348888 : t = lookup_tmp_var (val, is_formal, not_gimple_reg);
703 : :
704 : 26278302 : mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
705 : :
706 : 26278302 : SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
707 : :
708 : : /* gimplify_modify_expr might want to reduce this further. */
709 : 26278302 : gimplify_and_add (mod, pre_p);
710 : 26278302 : ggc_free (mod);
711 : :
712 : : /* If we failed to gimplify VAL then we can end up with the temporary
713 : : SSA name not having a definition. In this case return a decl. */
714 : 26278302 : if (TREE_CODE (t) == SSA_NAME && ! SSA_NAME_DEF_STMT (t))
715 : 1 : return lookup_tmp_var (val, is_formal, not_gimple_reg);
716 : :
717 : : return t;
718 : : }
719 : :
720 : : /* Return a formal temporary variable initialized with VAL. PRE_P is as
721 : : in gimplify_expr. Only use this function if:
722 : :
723 : : 1) The value of the unfactored expression represented by VAL will not
724 : : change between the initialization and use of the temporary, and
725 : : 2) The temporary will not be otherwise modified.
726 : :
727 : : For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
728 : : and #2 means it is inappropriate for && temps.
729 : :
730 : : For other cases, use get_initialized_tmp_var instead. */
731 : :
732 : : tree
733 : 25793146 : get_formal_tmp_var (tree val, gimple_seq *pre_p)
734 : : {
735 : 25793146 : return internal_get_tmp_var (val, pre_p, NULL, true, true, false);
736 : : }
737 : :
738 : : /* Return a temporary variable initialized with VAL. PRE_P and POST_P
739 : : are as in gimplify_expr. */
740 : :
741 : : tree
742 : 473067 : get_initialized_tmp_var (tree val, gimple_seq *pre_p,
743 : : gimple_seq *post_p /* = NULL */,
744 : : bool allow_ssa /* = true */)
745 : : {
746 : 473067 : return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa, false);
747 : : }
748 : :
749 : : /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
750 : : generate debug info for them; otherwise don't. */
751 : :
752 : : void
753 : 3028026 : declare_vars (tree vars, gimple *gs, bool debug_info)
754 : : {
755 : 3028026 : tree last = vars;
756 : 3028026 : if (last)
757 : : {
758 : 1895929 : tree temps, block;
759 : :
760 : 1895929 : gbind *scope = as_a <gbind *> (gs);
761 : :
762 : 1895929 : temps = nreverse (last);
763 : :
764 : 1895929 : block = gimple_bind_block (scope);
765 : 1895929 : gcc_assert (!block || TREE_CODE (block) == BLOCK);
766 : 1895929 : if (!block || !debug_info)
767 : : {
768 : 1892401 : DECL_CHAIN (last) = gimple_bind_vars (scope);
769 : 1892401 : gimple_bind_set_vars (scope, temps);
770 : : }
771 : : else
772 : : {
773 : : /* We need to attach the nodes both to the BIND_EXPR and to its
774 : : associated BLOCK for debugging purposes. The key point here
775 : : is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
776 : : is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
777 : 3528 : if (BLOCK_VARS (block))
778 : 3335 : BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
779 : : else
780 : : {
781 : 193 : gimple_bind_set_vars (scope,
782 : : chainon (gimple_bind_vars (scope), temps));
783 : 193 : BLOCK_VARS (block) = temps;
784 : : }
785 : : }
786 : : }
787 : 3028026 : }
788 : :
789 : : /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
790 : : for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
791 : : no such upper bound can be obtained. */
792 : :
793 : : static void
794 : 0 : force_constant_size (tree var)
795 : : {
796 : : /* The only attempt we make is by querying the maximum size of objects
797 : : of the variable's type. */
798 : :
799 : 0 : HOST_WIDE_INT max_size;
800 : :
801 : 0 : gcc_assert (VAR_P (var));
802 : :
803 : 0 : max_size = max_int_size_in_bytes (TREE_TYPE (var));
804 : :
805 : 0 : gcc_assert (max_size >= 0);
806 : :
807 : 0 : DECL_SIZE_UNIT (var)
808 : 0 : = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
809 : 0 : DECL_SIZE (var)
810 : 0 : = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
811 : 0 : }
812 : :
813 : : /* Push the temporary variable TMP into the current binding. */
814 : :
815 : : void
816 : 33127 : gimple_add_tmp_var_fn (struct function *fn, tree tmp)
817 : : {
818 : 33127 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
819 : :
820 : : /* Later processing assumes that the object size is constant, which might
821 : : not be true at this point. Force the use of a constant upper bound in
822 : : this case. */
823 : 33127 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
824 : 0 : force_constant_size (tmp);
825 : :
826 : 33127 : DECL_CONTEXT (tmp) = fn->decl;
827 : 33127 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
828 : :
829 : 33127 : record_vars_into (tmp, fn->decl);
830 : 33127 : }
831 : :
832 : : /* Push the temporary variable TMP into the current binding. */
833 : :
834 : : void
835 : 16251204 : gimple_add_tmp_var (tree tmp)
836 : : {
837 : 16251204 : gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
838 : :
839 : : /* Later processing assumes that the object size is constant, which might
840 : : not be true at this point. Force the use of a constant upper bound in
841 : : this case. */
842 : 16251204 : if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
843 : 0 : force_constant_size (tmp);
844 : :
845 : 16251204 : DECL_CONTEXT (tmp) = current_function_decl;
846 : 16251204 : DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
847 : :
848 : 16251204 : if (gimplify_ctxp)
849 : : {
850 : 5675399 : DECL_CHAIN (tmp) = gimplify_ctxp->temps;
851 : 5675399 : gimplify_ctxp->temps = tmp;
852 : :
853 : : /* Mark temporaries local within the nearest enclosing parallel. */
854 : 5675399 : if (gimplify_omp_ctxp)
855 : : {
856 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
857 : 545841 : int flag = GOVD_LOCAL | GOVD_SEEN;
858 : : while (ctx
859 : 545841 : && (ctx->region_type == ORT_WORKSHARE
860 : : || ctx->region_type == ORT_TASKGROUP
861 : 398434 : || ctx->region_type == ORT_SIMD
862 : 348282 : || ctx->region_type == ORT_ACC))
863 : : {
864 : 204599 : if (ctx->region_type == ORT_SIMD
865 : 50152 : && TREE_ADDRESSABLE (tmp)
866 : 96 : && !TREE_STATIC (tmp))
867 : : {
868 : 96 : if (TREE_CODE (DECL_SIZE_UNIT (tmp)) != INTEGER_CST)
869 : 0 : ctx->add_safelen1 = true;
870 : 96 : else if (ctx->in_for_exprs)
871 : : flag = GOVD_PRIVATE;
872 : : else
873 : : flag = GOVD_PRIVATE | GOVD_SEEN;
874 : : break;
875 : : }
876 : 204503 : ctx = ctx->outer_context;
877 : : }
878 : 341242 : if (ctx)
879 : 314278 : omp_add_variable (ctx, tmp, flag);
880 : : }
881 : : }
882 : 10575805 : else if (cfun)
883 : 10575805 : record_vars (tmp);
884 : : else
885 : : {
886 : 0 : gimple_seq body_seq;
887 : :
888 : : /* This case is for nested functions. We need to expose the locals
889 : : they create. */
890 : 0 : body_seq = gimple_body (current_function_decl);
891 : 0 : declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
892 : : }
893 : 16251204 : }
894 : :
895 : :
896 : :
897 : : /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
898 : : nodes that are referenced more than once in GENERIC functions. This is
899 : : necessary because gimplification (translation into GIMPLE) is performed
900 : : by modifying tree nodes in-place, so gimplication of a shared node in a
901 : : first context could generate an invalid GIMPLE form in a second context.
902 : :
903 : : This is achieved with a simple mark/copy/unmark algorithm that walks the
904 : : GENERIC representation top-down, marks nodes with TREE_VISITED the first
905 : : time it encounters them, duplicates them if they already have TREE_VISITED
906 : : set, and finally removes the TREE_VISITED marks it has set.
907 : :
908 : : The algorithm works only at the function level, i.e. it generates a GENERIC
909 : : representation of a function with no nodes shared within the function when
910 : : passed a GENERIC function (except for nodes that are allowed to be shared).
911 : :
912 : : At the global level, it is also necessary to unshare tree nodes that are
913 : : referenced in more than one function, for the same aforementioned reason.
914 : : This requires some cooperation from the front-end. There are 2 strategies:
915 : :
916 : : 1. Manual unsharing. The front-end needs to call unshare_expr on every
917 : : expression that might end up being shared across functions.
918 : :
919 : : 2. Deep unsharing. This is an extension of regular unsharing. Instead
920 : : of calling unshare_expr on expressions that might be shared across
921 : : functions, the front-end pre-marks them with TREE_VISITED. This will
922 : : ensure that they are unshared on the first reference within functions
923 : : when the regular unsharing algorithm runs. The counterpart is that
924 : : this algorithm must look deeper than for manual unsharing, which is
925 : : specified by LANG_HOOKS_DEEP_UNSHARING.
926 : :
927 : : If there are only few specific cases of node sharing across functions, it is
928 : : probably easier for a front-end to unshare the expressions manually. On the
929 : : contrary, if the expressions generated at the global level are as widespread
930 : : as expressions generated within functions, deep unsharing is very likely the
931 : : way to go. */
932 : :
933 : : /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
934 : : These nodes model computations that must be done once. If we were to
935 : : unshare something like SAVE_EXPR(i++), the gimplification process would
936 : : create wrong code. However, if DATA is non-null, it must hold a pointer
937 : : set that is used to unshare the subtrees of these nodes. */
938 : :
939 : : static tree
940 : 2182707024 : mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
941 : : {
942 : 2182707024 : tree t = *tp;
943 : 2182707024 : enum tree_code code = TREE_CODE (t);
944 : :
945 : : /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
946 : : copy their subtrees if we can make sure to do it only once. */
947 : 2182707024 : if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
948 : : {
949 : 8360555 : if (data && !((hash_set<tree> *)data)->add (t))
950 : : ;
951 : : else
952 : 8360555 : *walk_subtrees = 0;
953 : : }
954 : :
955 : : /* Stop at types, decls, constants like copy_tree_r. */
956 : 2174346469 : else if (TREE_CODE_CLASS (code) == tcc_type
957 : : || TREE_CODE_CLASS (code) == tcc_declaration
958 : 2174346469 : || TREE_CODE_CLASS (code) == tcc_constant)
959 : 1318013381 : *walk_subtrees = 0;
960 : :
961 : : /* Cope with the statement expression extension. */
962 : 856333088 : else if (code == STATEMENT_LIST)
963 : : ;
964 : :
965 : : /* Leave the bulk of the work to copy_tree_r itself. */
966 : : else
967 : 856283459 : copy_tree_r (tp, walk_subtrees, NULL);
968 : :
969 : 2182707024 : return NULL_TREE;
970 : : }
971 : :
972 : : /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
973 : : If *TP has been visited already, then *TP is deeply copied by calling
974 : : mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
975 : :
976 : : static tree
977 : 280241114 : copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
978 : : {
979 : 280241114 : tree t = *tp;
980 : 280241114 : enum tree_code code = TREE_CODE (t);
981 : :
982 : : /* Skip types, decls, and constants. But we do want to look at their
983 : : types and the bounds of types. Mark them as visited so we properly
984 : : unmark their subtrees on the unmark pass. If we've already seen them,
985 : : don't look down further. */
986 : 280241114 : if (TREE_CODE_CLASS (code) == tcc_type
987 : : || TREE_CODE_CLASS (code) == tcc_declaration
988 : 280241114 : || TREE_CODE_CLASS (code) == tcc_constant)
989 : : {
990 : 131991592 : if (TREE_VISITED (t))
991 : 80750790 : *walk_subtrees = 0;
992 : : else
993 : 51240802 : TREE_VISITED (t) = 1;
994 : : }
995 : :
996 : : /* If this node has been visited already, unshare it and don't look
997 : : any deeper. */
998 : 148249522 : else if (TREE_VISITED (t))
999 : : {
1000 : 1628410 : walk_tree (tp, mostly_copy_tree_r, data, NULL);
1001 : 1628410 : *walk_subtrees = 0;
1002 : : }
1003 : :
1004 : : /* Otherwise, mark the node as visited and keep looking. */
1005 : : else
1006 : 146621112 : TREE_VISITED (t) = 1;
1007 : :
1008 : 280241114 : return NULL_TREE;
1009 : : }
1010 : :
1011 : : /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
1012 : : copy_if_shared_r callback unmodified. */
1013 : :
1014 : : void
1015 : 8765685 : copy_if_shared (tree *tp, void *data)
1016 : : {
1017 : 8765685 : walk_tree (tp, copy_if_shared_r, data, NULL);
1018 : 8765685 : }
1019 : :
1020 : : /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
1021 : : any nested functions. */
1022 : :
1023 : : static void
1024 : 2921895 : unshare_body (tree fndecl)
1025 : : {
1026 : 2921895 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
1027 : : /* If the language requires deep unsharing, we need a pointer set to make
1028 : : sure we don't repeatedly unshare subtrees of unshareable nodes. */
1029 : 2921895 : hash_set<tree> *visited
1030 : 2921895 : = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
1031 : :
1032 : 2921895 : copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
1033 : 2921895 : copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
1034 : 2921895 : copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1035 : :
1036 : 2925161 : delete visited;
1037 : :
1038 : 2921895 : if (cgn)
1039 : 5886476 : for (cgn = first_nested_function (cgn); cgn;
1040 : 24654 : cgn = next_nested_function (cgn))
1041 : 24654 : unshare_body (cgn->decl);
1042 : 2921895 : }
1043 : :
1044 : : /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1045 : : Subtrees are walked until the first unvisited node is encountered. */
1046 : :
1047 : : static tree
1048 : 280240688 : unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1049 : : {
1050 : 280240688 : tree t = *tp;
1051 : :
1052 : : /* If this node has been visited, unmark it and keep looking. */
1053 : 280240688 : if (TREE_VISITED (t))
1054 : 197866208 : TREE_VISITED (t) = 0;
1055 : :
1056 : : /* Otherwise, don't look any deeper. */
1057 : : else
1058 : 82374480 : *walk_subtrees = 0;
1059 : :
1060 : 280240688 : return NULL_TREE;
1061 : : }
1062 : :
1063 : : /* Unmark the visited trees rooted at *TP. */
1064 : :
1065 : : static inline void
1066 : 8765685 : unmark_visited (tree *tp)
1067 : : {
1068 : 8765685 : walk_tree (tp, unmark_visited_r, NULL, NULL);
1069 : 8765685 : }
1070 : :
1071 : : /* Likewise, but mark all trees as not visited. */
1072 : :
1073 : : static void
1074 : 2921895 : unvisit_body (tree fndecl)
1075 : : {
1076 : 2921895 : struct cgraph_node *cgn = cgraph_node::get (fndecl);
1077 : :
1078 : 2921895 : unmark_visited (&DECL_SAVED_TREE (fndecl));
1079 : 2921895 : unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1080 : 2921895 : unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1081 : :
1082 : 2921895 : if (cgn)
1083 : 2943238 : for (cgn = first_nested_function (cgn);
1084 : 2943238 : cgn; cgn = next_nested_function (cgn))
1085 : 24654 : unvisit_body (cgn->decl);
1086 : 2921895 : }
1087 : :
1088 : : /* Unconditionally make an unshared copy of EXPR. This is used when using
1089 : : stored expressions which span multiple functions, such as BINFO_VTABLE,
1090 : : as the normal unsharing process can't tell that they're shared. */
1091 : :
1092 : : tree
1093 : 1191239714 : unshare_expr (tree expr)
1094 : : {
1095 : 1191239714 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1096 : 1191239714 : return expr;
1097 : : }
1098 : :
1099 : : /* Worker for unshare_expr_without_location. */
1100 : :
1101 : : static tree
1102 : 8372243 : prune_expr_location (tree *tp, int *walk_subtrees, void *)
1103 : : {
1104 : 8372243 : if (EXPR_P (*tp))
1105 : 3943249 : SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1106 : : else
1107 : 4428994 : *walk_subtrees = 0;
1108 : 8372243 : return NULL_TREE;
1109 : : }
1110 : :
1111 : : /* Similar to unshare_expr but also prune all expression locations
1112 : : from EXPR. */
1113 : :
1114 : : tree
1115 : 20810103 : unshare_expr_without_location (tree expr)
1116 : : {
1117 : 20810103 : walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1118 : 20810103 : if (EXPR_P (expr))
1119 : 2709334 : walk_tree (&expr, prune_expr_location, NULL, NULL);
1120 : 20810103 : return expr;
1121 : : }
1122 : :
1123 : : /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
1124 : : one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
1125 : : comprising at least one DEBUG_BEGIN_STMT followed by exactly one
1126 : : EXPR is the location of the EXPR. */
1127 : :
1128 : : static location_t
1129 : 1152371 : rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1130 : : {
1131 : 1152371 : if (!expr)
1132 : : return or_else;
1133 : :
1134 : 1152371 : if (EXPR_HAS_LOCATION (expr))
1135 : 802909 : return EXPR_LOCATION (expr);
1136 : :
1137 : 349462 : if (TREE_CODE (expr) != STATEMENT_LIST)
1138 : : return or_else;
1139 : :
1140 : 0 : tree_stmt_iterator i = tsi_start (expr);
1141 : :
1142 : 0 : bool found = false;
1143 : 0 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1144 : : {
1145 : 0 : found = true;
1146 : 0 : tsi_next (&i);
1147 : : }
1148 : :
1149 : 349462 : if (!found || !tsi_one_before_end_p (i))
1150 : : return or_else;
1151 : :
1152 : 0 : return rexpr_location (tsi_stmt (i), or_else);
1153 : : }
1154 : :
1155 : : /* Return TRUE iff EXPR (maybe recursively) has a location; see
1156 : : rexpr_location for the potential recursion. */
1157 : :
1158 : : static inline bool
1159 : 497233 : rexpr_has_location (tree expr)
1160 : : {
1161 : 294695 : return rexpr_location (expr) != UNKNOWN_LOCATION;
1162 : : }
1163 : :
1164 : :
1165 : : /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1166 : : contain statements and have a value. Assign its value to a temporary
1167 : : and give it void_type_node. Return the temporary, or NULL_TREE if
1168 : : WRAPPER was already void. */
1169 : :
1170 : : tree
1171 : 21070702 : voidify_wrapper_expr (tree wrapper, tree temp)
1172 : : {
1173 : 21070702 : tree type = TREE_TYPE (wrapper);
1174 : 21070702 : if (type && !VOID_TYPE_P (type))
1175 : : {
1176 : : tree *p;
1177 : :
1178 : : /* Set p to point to the body of the wrapper. Loop until we find
1179 : : something that isn't a wrapper. */
1180 : 905583 : for (p = &wrapper; p && *p; )
1181 : : {
1182 : 905583 : switch (TREE_CODE (*p))
1183 : : {
1184 : 3077 : case BIND_EXPR:
1185 : 3077 : TREE_SIDE_EFFECTS (*p) = 1;
1186 : 3077 : TREE_TYPE (*p) = void_type_node;
1187 : : /* For a BIND_EXPR, the body is operand 1. */
1188 : 3077 : p = &BIND_EXPR_BODY (*p);
1189 : 3077 : break;
1190 : :
1191 : 426485 : case CLEANUP_POINT_EXPR:
1192 : 426485 : case TRY_FINALLY_EXPR:
1193 : 426485 : case TRY_CATCH_EXPR:
1194 : 426485 : TREE_SIDE_EFFECTS (*p) = 1;
1195 : 426485 : TREE_TYPE (*p) = void_type_node;
1196 : 426485 : p = &TREE_OPERAND (*p, 0);
1197 : 426485 : break;
1198 : :
1199 : 18350 : case STATEMENT_LIST:
1200 : 18350 : {
1201 : 18350 : tree_stmt_iterator i = tsi_last (*p);
1202 : 18350 : TREE_SIDE_EFFECTS (*p) = 1;
1203 : 18350 : TREE_TYPE (*p) = void_type_node;
1204 : 18350 : p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1205 : : }
1206 : 18350 : break;
1207 : :
1208 : : case COMPOUND_EXPR:
1209 : : /* Advance to the last statement. Set all container types to
1210 : : void. */
1211 : 49168 : for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1212 : : {
1213 : 24587 : TREE_SIDE_EFFECTS (*p) = 1;
1214 : 24587 : TREE_TYPE (*p) = void_type_node;
1215 : : }
1216 : : break;
1217 : :
1218 : 76 : case TRANSACTION_EXPR:
1219 : 76 : TREE_SIDE_EFFECTS (*p) = 1;
1220 : 76 : TREE_TYPE (*p) = void_type_node;
1221 : 76 : p = &TRANSACTION_EXPR_BODY (*p);
1222 : 76 : break;
1223 : :
1224 : 433014 : default:
1225 : : /* Assume that any tree upon which voidify_wrapper_expr is
1226 : : directly called is a wrapper, and that its body is op0. */
1227 : 433014 : if (p == &wrapper)
1228 : : {
1229 : 33 : TREE_SIDE_EFFECTS (*p) = 1;
1230 : 33 : TREE_TYPE (*p) = void_type_node;
1231 : 33 : p = &TREE_OPERAND (*p, 0);
1232 : 33 : break;
1233 : : }
1234 : 432981 : goto out;
1235 : : }
1236 : : }
1237 : :
1238 : 0 : out:
1239 : 432981 : if (p == NULL || IS_EMPTY_STMT (*p))
1240 : : temp = NULL_TREE;
1241 : 432981 : else if (temp)
1242 : : {
1243 : : /* The wrapper is on the RHS of an assignment that we're pushing
1244 : : down. */
1245 : 1681 : gcc_assert (TREE_CODE (temp) == INIT_EXPR
1246 : : || TREE_CODE (temp) == MODIFY_EXPR);
1247 : 1681 : TREE_OPERAND (temp, 1) = *p;
1248 : 1681 : *p = temp;
1249 : : }
1250 : : else
1251 : : {
1252 : 431300 : temp = create_tmp_var (type, "retval");
1253 : 431300 : *p = build2 (INIT_EXPR, type, temp, *p);
1254 : : }
1255 : :
1256 : 432981 : return temp;
1257 : : }
1258 : :
1259 : : return NULL_TREE;
1260 : : }
1261 : :
1262 : : /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1263 : : a temporary through which they communicate. */
1264 : :
1265 : : static void
1266 : 7772 : build_stack_save_restore (gcall **save, gcall **restore)
1267 : : {
1268 : 7772 : tree tmp_var;
1269 : :
1270 : 15544 : *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1271 : 7772 : tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1272 : 7772 : gimple_call_set_lhs (*save, tmp_var);
1273 : :
1274 : 7772 : *restore
1275 : 7772 : = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1276 : : 1, tmp_var);
1277 : 7772 : }
1278 : :
1279 : : /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1280 : :
1281 : : static tree
1282 : 428 : build_asan_poison_call_expr (tree decl)
1283 : : {
1284 : : /* Do not poison variables that have size equal to zero. */
1285 : 428 : tree unit_size = DECL_SIZE_UNIT (decl);
1286 : 428 : if (zerop (unit_size))
1287 : : return NULL_TREE;
1288 : :
1289 : 428 : tree base = build_fold_addr_expr (decl);
1290 : :
1291 : 428 : return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1292 : : void_type_node, 3,
1293 : : build_int_cst (integer_type_node,
1294 : : ASAN_MARK_POISON),
1295 : : base, unit_size);
1296 : : }
1297 : :
1298 : : /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1299 : : on POISON flag, shadow memory of a DECL variable. The call will be
1300 : : put on location identified by IT iterator, where BEFORE flag drives
1301 : : position where the stmt will be put. */
1302 : :
1303 : : static void
1304 : 4111 : asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1305 : : bool before)
1306 : : {
1307 : 4111 : tree unit_size = DECL_SIZE_UNIT (decl);
1308 : 4111 : tree base = build_fold_addr_expr (decl);
1309 : :
1310 : : /* Do not poison variables that have size equal to zero. */
1311 : 4111 : if (zerop (unit_size))
1312 : 4111 : return;
1313 : :
1314 : : /* It's necessary to have all stack variables aligned to ASAN granularity
1315 : : bytes. */
1316 : 4097 : gcc_assert (!hwasan_sanitize_p () || hwasan_sanitize_stack_p ());
1317 : 4097 : unsigned shadow_granularity
1318 : 4097 : = hwasan_sanitize_p () ? HWASAN_TAG_GRANULE_SIZE : ASAN_SHADOW_GRANULARITY;
1319 : 4097 : if (DECL_ALIGN_UNIT (decl) <= shadow_granularity)
1320 : 3895 : SET_DECL_ALIGN (decl, BITS_PER_UNIT * shadow_granularity);
1321 : :
1322 : 4097 : HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1323 : :
1324 : 4097 : gimple *g
1325 : 4097 : = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1326 : 4097 : build_int_cst (integer_type_node, flags),
1327 : : base, unit_size);
1328 : :
1329 : 4097 : if (before)
1330 : 2079 : gsi_insert_before (it, g, GSI_NEW_STMT);
1331 : : else
1332 : 2018 : gsi_insert_after (it, g, GSI_NEW_STMT);
1333 : : }
1334 : :
1335 : : /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1336 : : either poisons or unpoisons a DECL. Created statement is appended
1337 : : to SEQ_P gimple sequence. */
1338 : :
1339 : : static void
1340 : 3683 : asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1341 : : {
1342 : 3683 : gimple_stmt_iterator it = gsi_last (*seq_p);
1343 : 3683 : bool before = false;
1344 : :
1345 : 3683 : if (gsi_end_p (it))
1346 : 1871 : before = true;
1347 : :
1348 : 3683 : asan_poison_variable (decl, poison, &it, before);
1349 : 3683 : }
1350 : :
1351 : : /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1352 : :
1353 : : static int
1354 : 135 : sort_by_decl_uid (const void *a, const void *b)
1355 : : {
1356 : 135 : const tree *t1 = (const tree *)a;
1357 : 135 : const tree *t2 = (const tree *)b;
1358 : :
1359 : 135 : int uid1 = DECL_UID (*t1);
1360 : 135 : int uid2 = DECL_UID (*t2);
1361 : :
1362 : 135 : if (uid1 < uid2)
1363 : : return -1;
1364 : 50 : else if (uid1 > uid2)
1365 : : return 1;
1366 : : else
1367 : 0 : return 0;
1368 : : }
1369 : :
1370 : : /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1371 : : depending on POISON flag. Created statement is appended
1372 : : to SEQ_P gimple sequence. */
1373 : :
1374 : : static void
1375 : 1079054 : asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1376 : : {
1377 : 1079054 : unsigned c = variables->elements ();
1378 : 1079054 : if (c == 0)
1379 : 1078878 : return;
1380 : :
1381 : 176 : auto_vec<tree> sorted_variables (c);
1382 : :
1383 : 176 : for (hash_set<tree>::iterator it = variables->begin ();
1384 : 570 : it != variables->end (); ++it)
1385 : 197 : sorted_variables.safe_push (*it);
1386 : :
1387 : 176 : sorted_variables.qsort (sort_by_decl_uid);
1388 : :
1389 : : unsigned i;
1390 : : tree var;
1391 : 549 : FOR_EACH_VEC_ELT (sorted_variables, i, var)
1392 : : {
1393 : 197 : asan_poison_variable (var, poison, seq_p);
1394 : :
1395 : : /* Add use_after_scope_memory attribute for the variable in order
1396 : : to prevent re-written into SSA. */
1397 : 197 : if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1398 : 197 : DECL_ATTRIBUTES (var)))
1399 : 90 : DECL_ATTRIBUTES (var)
1400 : 180 : = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1401 : : integer_one_node,
1402 : 90 : DECL_ATTRIBUTES (var));
1403 : : }
1404 : 176 : }
1405 : :
1406 : : /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1407 : :
1408 : : static enum gimplify_status
1409 : 5961975 : gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1410 : : {
1411 : 5961975 : tree bind_expr = *expr_p;
1412 : 5961975 : bool old_keep_stack = gimplify_ctxp->keep_stack;
1413 : 5961975 : bool old_save_stack = gimplify_ctxp->save_stack;
1414 : 5961975 : tree t;
1415 : 5961975 : gbind *bind_stmt;
1416 : 5961975 : gimple_seq body, cleanup;
1417 : 5961975 : gcall *stack_save;
1418 : 5961975 : location_t start_locus = 0, end_locus = 0;
1419 : 5961975 : tree ret_clauses = NULL;
1420 : :
1421 : 5961975 : tree temp = voidify_wrapper_expr (bind_expr, NULL);
1422 : :
1423 : : /* Mark variables seen in this bind expr. */
1424 : 12548873 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1425 : : {
1426 : 6586898 : if (VAR_P (t))
1427 : : {
1428 : 5962524 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1429 : 5962524 : tree attr;
1430 : :
1431 : 5962524 : if (flag_openmp
1432 : 233421 : && !is_global_var (t)
1433 : 225536 : && !TREE_STATIC (t)
1434 : 225536 : && DECL_CONTEXT (t) == current_function_decl
1435 : 225536 : && TREE_USED (t)
1436 : 6186582 : && (attr = lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1437 : : != NULL_TREE)
1438 : : {
1439 : 74 : gcc_assert (!DECL_HAS_VALUE_EXPR_P (t));
1440 : 74 : tree alloc = TREE_PURPOSE (TREE_VALUE (attr));
1441 : 74 : tree align = TREE_VALUE (TREE_VALUE (attr));
1442 : : /* Allocate directives that appear in a target region must specify
1443 : : an allocator clause unless a requires directive with the
1444 : : dynamic_allocators clause is present in the same compilation
1445 : : unit. */
1446 : 74 : bool missing_dyn_alloc = false;
1447 : 74 : if (alloc == NULL_TREE
1448 : 48 : && ((omp_requires_mask & OMP_REQUIRES_DYNAMIC_ALLOCATORS)
1449 : : == 0))
1450 : : {
1451 : : /* This comes too early for omp_discover_declare_target...,
1452 : : but should at least catch the most common cases. */
1453 : 42 : missing_dyn_alloc
1454 : 42 : = cgraph_node::get (current_function_decl)->offloadable;
1455 : 42 : for (struct gimplify_omp_ctx *ctx2 = ctx;
1456 : 48 : ctx2 && !missing_dyn_alloc; ctx2 = ctx2->outer_context)
1457 : 6 : if (ctx2->code == OMP_TARGET)
1458 : 2 : missing_dyn_alloc = true;
1459 : : }
1460 : 42 : if (missing_dyn_alloc)
1461 : 4 : error_at (DECL_SOURCE_LOCATION (t),
1462 : : "%<allocate%> directive for %qD inside a target "
1463 : : "region must specify an %<allocator%> clause", t);
1464 : : /* Skip for omp_default_mem_alloc (= 1),
1465 : : unless align is present. For C/C++, there should be always a
1466 : : statement list following if TREE_USED, except for, e.g., using
1467 : : this decl in a static_assert; in that case, only a single
1468 : : DECL_EXPR remains, which can be skipped here. */
1469 : 70 : else if (!errorcount
1470 : 55 : && (align != NULL_TREE
1471 : 55 : || alloc == NULL_TREE
1472 : 10 : || !integer_onep (alloc))
1473 : 123 : && (lang_GNU_Fortran ()
1474 : 25 : || (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1475 : : != DECL_EXPR)))
1476 : : {
1477 : : /* Fortran might already use a pointer type internally;
1478 : : use that pointer except for type(C_ptr) and type(C_funptr);
1479 : : note that normal proc pointers are rejected. */
1480 : 53 : tree type = TREE_TYPE (t);
1481 : 53 : tree tmp, v;
1482 : 53 : if (lang_GNU_Fortran ()
1483 : 28 : && POINTER_TYPE_P (type)
1484 : 8 : && TREE_TYPE (type) != void_type_node
1485 : 59 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
1486 : : {
1487 : 6 : type = TREE_TYPE (type);
1488 : 6 : v = t;
1489 : : }
1490 : : else
1491 : : {
1492 : 47 : tmp = build_pointer_type (type);
1493 : 47 : v = create_tmp_var (tmp, get_name (t));
1494 : 47 : DECL_IGNORED_P (v) = 0;
1495 : 47 : DECL_ATTRIBUTES (v)
1496 : 47 : = tree_cons (get_identifier ("omp allocate var"),
1497 : : build_tree_list (NULL_TREE, t),
1498 : : remove_attribute ("omp allocate",
1499 : 47 : DECL_ATTRIBUTES (t)));
1500 : 47 : tmp = build_fold_indirect_ref (v);
1501 : 47 : TREE_THIS_NOTRAP (tmp) = 1;
1502 : 47 : SET_DECL_VALUE_EXPR (t, tmp);
1503 : 47 : DECL_HAS_VALUE_EXPR_P (t) = 1;
1504 : : }
1505 : 53 : tree sz = TYPE_SIZE_UNIT (type);
1506 : : /* The size to use in Fortran might not match TYPE_SIZE_UNIT;
1507 : : hence, for some decls, a size variable is saved in the
1508 : : attributes; use it, if available. */
1509 : 53 : if (TREE_CHAIN (TREE_VALUE (attr))
1510 : 28 : && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))
1511 : 59 : && TREE_PURPOSE (
1512 : : TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)))))
1513 : : {
1514 : 6 : sz = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1515 : 6 : sz = TREE_PURPOSE (sz);
1516 : : }
1517 : 53 : if (alloc == NULL_TREE)
1518 : 36 : alloc = build_zero_cst (ptr_type_node);
1519 : 53 : if (align == NULL_TREE)
1520 : 42 : align = build_int_cst (size_type_node, DECL_ALIGN_UNIT (t));
1521 : : else
1522 : 11 : align = build_int_cst (size_type_node,
1523 : 11 : MAX (tree_to_uhwi (align),
1524 : : DECL_ALIGN_UNIT (t)));
1525 : 53 : location_t loc = DECL_SOURCE_LOCATION (t);
1526 : 53 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
1527 : 53 : tmp = build_call_expr_loc (loc, tmp, 3, align, sz, alloc);
1528 : 53 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1529 : 53 : fold_convert (TREE_TYPE (v), tmp));
1530 : 53 : gcc_assert (BIND_EXPR_BODY (bind_expr) != NULL_TREE);
1531 : : /* Ensure that either TREE_CHAIN (TREE_VALUE (attr) is set
1532 : : and GOMP_FREE added here or that DECL_HAS_VALUE_EXPR_P (t)
1533 : : is set, using in a condition much further below. */
1534 : 59 : gcc_assert (DECL_HAS_VALUE_EXPR_P (t)
1535 : : || TREE_CHAIN (TREE_VALUE (attr)));
1536 : 53 : if (TREE_CHAIN (TREE_VALUE (attr)))
1537 : : {
1538 : : /* Fortran is special as it does not have properly nest
1539 : : declarations in blocks. And as there is no
1540 : : initializer, there is also no expression to look for.
1541 : : Hence, the FE makes the statement list of the
1542 : : try-finally block available. We can put the GOMP_alloc
1543 : : at the top, unless an allocator or size expression
1544 : : requires to put it afterward; note that the size is
1545 : : always later in generated code; for strings, no
1546 : : size expr but still an expr might be available.
1547 : : As LTO does not handle a statement list, 'sl' has
1548 : : to be removed; done so by removing the attribute. */
1549 : 28 : DECL_ATTRIBUTES (t)
1550 : 28 : = remove_attribute ("omp allocate",
1551 : 28 : DECL_ATTRIBUTES (t));
1552 : 28 : tree sl = TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)));
1553 : 28 : tree_stmt_iterator e = tsi_start (sl);
1554 : 28 : tree needle = NULL_TREE;
1555 : 28 : if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1556 : : {
1557 : 6 : needle = TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr)));
1558 : 6 : needle = (TREE_VALUE (needle) ? TREE_VALUE (needle)
1559 : : : sz);
1560 : : }
1561 : 22 : else if (TREE_CHAIN (TREE_CHAIN (TREE_VALUE (attr))))
1562 : : needle = sz;
1563 : 22 : else if (DECL_P (alloc) && DECL_ARTIFICIAL (alloc))
1564 : : needle = alloc;
1565 : :
1566 : 10 : if (needle != NULL_TREE)
1567 : : {
1568 : 49 : while (!tsi_end_p (e))
1569 : : {
1570 : 49 : if (*e == needle
1571 : 49 : || (TREE_CODE (*e) == MODIFY_EXPR
1572 : 49 : && TREE_OPERAND (*e, 0) == needle))
1573 : : break;
1574 : 39 : ++e;
1575 : : }
1576 : 10 : gcc_assert (!tsi_end_p (e));
1577 : : }
1578 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1579 : :
1580 : : /* As the cleanup is in BIND_EXPR_BODY, GOMP_free is added
1581 : : here; for C/C++ it will be added in the 'cleanup'
1582 : : section after gimplification. But Fortran already has
1583 : : a try-finally block. */
1584 : 28 : sl = TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)));
1585 : 28 : e = tsi_last (sl);
1586 : 28 : tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1587 : 28 : tmp = build_call_expr_loc (EXPR_LOCATION (*e), tmp, 2, v,
1588 : : build_zero_cst (ptr_type_node));
1589 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1590 : 28 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1591 : 28 : tmp = fold_build2_loc (loc, MODIFY_EXPR, TREE_TYPE (v), v,
1592 : 28 : fold_convert (TREE_TYPE (v), tmp));
1593 : 28 : ++e;
1594 : 28 : tsi_link_after (&e, tmp, TSI_SAME_STMT);
1595 : : }
1596 : : else
1597 : : {
1598 : 25 : gcc_assert (TREE_CODE (BIND_EXPR_BODY (bind_expr))
1599 : : == STATEMENT_LIST);
1600 : 25 : tree_stmt_iterator e;
1601 : 25 : e = tsi_start (BIND_EXPR_BODY (bind_expr));
1602 : 113 : while (!tsi_end_p (e))
1603 : : {
1604 : 88 : if ((TREE_CODE (*e) == DECL_EXPR
1605 : 58 : && TREE_OPERAND (*e, 0) == t)
1606 : 121 : || (TREE_CODE (*e) == CLEANUP_POINT_EXPR
1607 : 0 : && (TREE_CODE (TREE_OPERAND (*e, 0))
1608 : : == DECL_EXPR)
1609 : 0 : && (TREE_OPERAND (TREE_OPERAND (*e, 0), 0)
1610 : : == t)))
1611 : : break;
1612 : 63 : ++e;
1613 : : }
1614 : 25 : gcc_assert (!tsi_end_p (e));
1615 : 25 : tsi_link_before (&e, tmp, TSI_SAME_STMT);
1616 : : }
1617 : : }
1618 : : }
1619 : :
1620 : : /* Mark variable as local. */
1621 : 5962524 : if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t))
1622 : : {
1623 : 94729 : if (! DECL_SEEN_IN_BIND_EXPR_P (t)
1624 : 94729 : || splay_tree_lookup (ctx->variables,
1625 : : (splay_tree_key) t) == NULL)
1626 : : {
1627 : 94729 : int flag = GOVD_LOCAL;
1628 : 94729 : if (ctx->region_type == ORT_SIMD
1629 : 4350 : && TREE_ADDRESSABLE (t)
1630 : 81 : && !TREE_STATIC (t))
1631 : : {
1632 : 69 : if (TREE_CODE (DECL_SIZE_UNIT (t)) != INTEGER_CST)
1633 : 2 : ctx->add_safelen1 = true;
1634 : : else
1635 : : flag = GOVD_PRIVATE;
1636 : : }
1637 : 94729 : omp_add_variable (ctx, t, flag | GOVD_SEEN);
1638 : : }
1639 : : /* Static locals inside of target construct or offloaded
1640 : : routines need to be "omp declare target". */
1641 : 94729 : if (TREE_STATIC (t))
1642 : 1266 : for (; ctx; ctx = ctx->outer_context)
1643 : 1111 : if ((ctx->region_type & ORT_TARGET) != 0)
1644 : : {
1645 : 898 : if (!lookup_attribute ("omp declare target",
1646 : 898 : DECL_ATTRIBUTES (t)))
1647 : : {
1648 : 886 : tree id = get_identifier ("omp declare target");
1649 : 886 : DECL_ATTRIBUTES (t)
1650 : 886 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
1651 : 886 : varpool_node *node = varpool_node::get (t);
1652 : 886 : if (node)
1653 : : {
1654 : 33 : node->offloadable = 1;
1655 : 33 : if (ENABLE_OFFLOADING && !DECL_EXTERNAL (t))
1656 : : {
1657 : : g->have_offload = true;
1658 : : if (!in_lto_p)
1659 : : vec_safe_push (offload_vars, t);
1660 : : }
1661 : : }
1662 : : }
1663 : : break;
1664 : : }
1665 : : }
1666 : :
1667 : 5962524 : DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1668 : :
1669 : 5962524 : if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1670 : 1079 : cfun->has_local_explicit_reg_vars = true;
1671 : : }
1672 : : }
1673 : :
1674 : 11923950 : bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1675 : 5961975 : BIND_EXPR_BLOCK (bind_expr));
1676 : 5961975 : gimple_push_bind_expr (bind_stmt);
1677 : :
1678 : 5961975 : gimplify_ctxp->keep_stack = false;
1679 : 5961975 : gimplify_ctxp->save_stack = false;
1680 : :
1681 : : /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1682 : 5961975 : body = NULL;
1683 : 5961975 : gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1684 : 5961975 : gimple_bind_set_body (bind_stmt, body);
1685 : :
1686 : : /* Source location wise, the cleanup code (stack_restore and clobbers)
1687 : : belongs to the end of the block, so propagate what we have. The
1688 : : stack_save operation belongs to the beginning of block, which we can
1689 : : infer from the bind_expr directly if the block has no explicit
1690 : : assignment. */
1691 : 5961975 : if (BIND_EXPR_BLOCK (bind_expr))
1692 : : {
1693 : 5833084 : end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1694 : 5833084 : start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1695 : : }
1696 : 5833084 : if (start_locus == 0)
1697 : 5961975 : start_locus = EXPR_LOCATION (bind_expr);
1698 : :
1699 : 5961975 : cleanup = NULL;
1700 : 5961975 : stack_save = NULL;
1701 : :
1702 : : /* Add clobbers for all variables that go out of scope. */
1703 : 12548873 : for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1704 : : {
1705 : 6586898 : if (VAR_P (t)
1706 : 5962524 : && !is_global_var (t)
1707 : 12339841 : && DECL_CONTEXT (t) == current_function_decl)
1708 : : {
1709 : 5752943 : if (flag_openmp
1710 : 225529 : && DECL_HAS_VALUE_EXPR_P (t)
1711 : 1016 : && TREE_USED (t)
1712 : 5753943 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (t)))
1713 : : {
1714 : : /* For Fortran, TREE_CHAIN (TREE_VALUE (attr)) is set, which
1715 : : causes that the GOMP_free call is already added above;
1716 : : and "omp allocate" is removed from DECL_ATTRIBUTES. */
1717 : 25 : tree v = TREE_OPERAND (DECL_VALUE_EXPR (t), 0);
1718 : 25 : tree tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
1719 : 25 : tmp = build_call_expr_loc (end_locus, tmp, 2, v,
1720 : : build_zero_cst (ptr_type_node));
1721 : 25 : gimplify_and_add (tmp, &cleanup);
1722 : 25 : gimple *clobber_stmt;
1723 : 25 : tmp = build_clobber (TREE_TYPE (v), CLOBBER_STORAGE_END);
1724 : 25 : clobber_stmt = gimple_build_assign (v, tmp);
1725 : 25 : gimple_set_location (clobber_stmt, end_locus);
1726 : 25 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1727 : : }
1728 : 5752943 : if (!DECL_HARD_REGISTER (t)
1729 : 5751864 : && !TREE_THIS_VOLATILE (t)
1730 : 5712941 : && !DECL_HAS_VALUE_EXPR_P (t)
1731 : : /* Only care for variables that have to be in memory. Others
1732 : : will be rewritten into SSA names, hence moved to the
1733 : : top-level. */
1734 : 5616392 : && !is_gimple_reg (t)
1735 : 7008592 : && flag_stack_reuse != SR_NONE)
1736 : : {
1737 : 1253060 : tree clobber = build_clobber (TREE_TYPE (t), CLOBBER_STORAGE_END);
1738 : 1253060 : gimple *clobber_stmt;
1739 : 1253060 : clobber_stmt = gimple_build_assign (t, clobber);
1740 : 1253060 : gimple_set_location (clobber_stmt, end_locus);
1741 : 1253060 : gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1742 : : }
1743 : :
1744 : 5752943 : if (flag_openacc && oacc_declare_returns != NULL)
1745 : : {
1746 : 207 : tree key = t;
1747 : 207 : if (DECL_HAS_VALUE_EXPR_P (key))
1748 : : {
1749 : 8 : key = DECL_VALUE_EXPR (key);
1750 : 8 : if (INDIRECT_REF_P (key))
1751 : 8 : key = TREE_OPERAND (key, 0);
1752 : : }
1753 : 207 : tree *c = oacc_declare_returns->get (key);
1754 : 207 : if (c != NULL)
1755 : : {
1756 : 116 : if (ret_clauses)
1757 : 64 : OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1758 : :
1759 : 116 : ret_clauses = unshare_expr (*c);
1760 : :
1761 : 116 : oacc_declare_returns->remove (key);
1762 : :
1763 : 116 : if (oacc_declare_returns->is_empty ())
1764 : : {
1765 : 40 : delete oacc_declare_returns;
1766 : 40 : oacc_declare_returns = NULL;
1767 : : }
1768 : : }
1769 : : }
1770 : : }
1771 : :
1772 : 6586898 : if (asan_poisoned_variables != NULL
1773 : 6586898 : && asan_poisoned_variables->contains (t))
1774 : : {
1775 : 1743 : asan_poisoned_variables->remove (t);
1776 : 1743 : asan_poison_variable (t, true, &cleanup);
1777 : : }
1778 : :
1779 : 6586898 : if (gimplify_ctxp->live_switch_vars != NULL
1780 : 6586898 : && gimplify_ctxp->live_switch_vars->contains (t))
1781 : 55 : gimplify_ctxp->live_switch_vars->remove (t);
1782 : : }
1783 : :
1784 : : /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1785 : : the stack space allocated to the VLAs. */
1786 : 5961975 : if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1787 : : {
1788 : 7772 : gcall *stack_restore;
1789 : :
1790 : : /* Save stack on entry and restore it on exit. Add a try_finally
1791 : : block to achieve this. */
1792 : 7772 : build_stack_save_restore (&stack_save, &stack_restore);
1793 : :
1794 : 7772 : gimple_set_location (stack_save, start_locus);
1795 : 7772 : gimple_set_location (stack_restore, end_locus);
1796 : :
1797 : 7772 : gimplify_seq_add_stmt (&cleanup, stack_restore);
1798 : : }
1799 : :
1800 : 5961975 : if (ret_clauses)
1801 : : {
1802 : 52 : gomp_target *stmt;
1803 : 52 : gimple_stmt_iterator si = gsi_start (cleanup);
1804 : :
1805 : 52 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1806 : : ret_clauses);
1807 : 52 : gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1808 : : }
1809 : :
1810 : 5961975 : if (cleanup)
1811 : : {
1812 : 767108 : gtry *gs;
1813 : 767108 : gimple_seq new_body;
1814 : :
1815 : 767108 : new_body = NULL;
1816 : 767108 : gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1817 : : GIMPLE_TRY_FINALLY);
1818 : :
1819 : 767108 : if (stack_save)
1820 : 7772 : gimplify_seq_add_stmt (&new_body, stack_save);
1821 : 767108 : gimplify_seq_add_stmt (&new_body, gs);
1822 : 767108 : gimple_bind_set_body (bind_stmt, new_body);
1823 : : }
1824 : :
1825 : : /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1826 : 5961975 : if (!gimplify_ctxp->keep_stack)
1827 : 5946352 : gimplify_ctxp->keep_stack = old_keep_stack;
1828 : 5961975 : gimplify_ctxp->save_stack = old_save_stack;
1829 : :
1830 : 5961975 : gimple_pop_bind_expr ();
1831 : :
1832 : 5961975 : gimplify_seq_add_stmt (pre_p, bind_stmt);
1833 : :
1834 : 5961975 : if (temp)
1835 : : {
1836 : 1709 : *expr_p = temp;
1837 : 1709 : return GS_OK;
1838 : : }
1839 : :
1840 : 5960266 : *expr_p = NULL_TREE;
1841 : 5960266 : return GS_ALL_DONE;
1842 : : }
1843 : :
1844 : : /* Maybe add early return predict statement to PRE_P sequence. */
1845 : :
1846 : : static void
1847 : 2233502 : maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1848 : : {
1849 : : /* If we are not in a conditional context, add PREDICT statement. */
1850 : 2233502 : if (gimple_conditional_context ())
1851 : : {
1852 : 443930 : gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1853 : : NOT_TAKEN);
1854 : 443930 : gimplify_seq_add_stmt (pre_p, predict);
1855 : : }
1856 : 2233502 : }
1857 : :
1858 : : /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1859 : : GIMPLE value, it is assigned to a new temporary and the statement is
1860 : : re-written to return the temporary.
1861 : :
1862 : : PRE_P points to the sequence where side effects that must happen before
1863 : : STMT should be stored. */
1864 : :
1865 : : static enum gimplify_status
1866 : 2233508 : gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1867 : : {
1868 : 2233508 : greturn *ret;
1869 : 2233508 : tree ret_expr = TREE_OPERAND (stmt, 0);
1870 : 2233508 : tree result_decl, result;
1871 : :
1872 : 2233508 : if (ret_expr == error_mark_node)
1873 : : return GS_ERROR;
1874 : :
1875 : 2233502 : if (!ret_expr
1876 : 2143167 : || TREE_CODE (ret_expr) == RESULT_DECL)
1877 : : {
1878 : 106077 : maybe_add_early_return_predict_stmt (pre_p);
1879 : 106077 : greturn *ret = gimple_build_return (ret_expr);
1880 : 106077 : copy_warning (ret, stmt);
1881 : 106077 : gimplify_seq_add_stmt (pre_p, ret);
1882 : 106077 : return GS_ALL_DONE;
1883 : : }
1884 : :
1885 : 2127425 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1886 : : result_decl = NULL_TREE;
1887 : 2127136 : else if (TREE_CODE (ret_expr) == COMPOUND_EXPR)
1888 : : {
1889 : : /* Used in C++ for handling EH cleanup of the return value if a local
1890 : : cleanup throws. Assume the front-end knows what it's doing. */
1891 : 4934 : result_decl = DECL_RESULT (current_function_decl);
1892 : : /* But crash if we end up trying to modify ret_expr below. */
1893 : 4934 : ret_expr = NULL_TREE;
1894 : : }
1895 : : else
1896 : : {
1897 : 2122202 : result_decl = TREE_OPERAND (ret_expr, 0);
1898 : :
1899 : : /* See through a return by reference. */
1900 : 2122202 : if (INDIRECT_REF_P (result_decl))
1901 : 43579 : result_decl = TREE_OPERAND (result_decl, 0);
1902 : :
1903 : 2122202 : gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1904 : : || TREE_CODE (ret_expr) == INIT_EXPR)
1905 : : && TREE_CODE (result_decl) == RESULT_DECL);
1906 : : }
1907 : :
1908 : : /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1909 : : Recall that aggregate_value_p is FALSE for any aggregate type that is
1910 : : returned in registers. If we're returning values in registers, then
1911 : : we don't want to extend the lifetime of the RESULT_DECL, particularly
1912 : : across another call. In addition, for those aggregates for which
1913 : : hard_function_value generates a PARALLEL, we'll die during normal
1914 : : expansion of structure assignments; there's special code in expand_return
1915 : : to handle this case that does not exist in expand_expr. */
1916 : 4934 : if (!result_decl)
1917 : : result = NULL_TREE;
1918 : 2127136 : else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1919 : : {
1920 : 179451 : if (!poly_int_tree_p (DECL_SIZE (result_decl)))
1921 : : {
1922 : 43 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1923 : 0 : gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1924 : : /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1925 : : should be effectively allocated by the caller, i.e. all calls to
1926 : : this function must be subject to the Return Slot Optimization. */
1927 : 43 : gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1928 : 43 : gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1929 : : }
1930 : : result = result_decl;
1931 : : }
1932 : 1947685 : else if (gimplify_ctxp->return_temp)
1933 : : result = gimplify_ctxp->return_temp;
1934 : : else
1935 : : {
1936 : 1450735 : result = create_tmp_reg (TREE_TYPE (result_decl));
1937 : :
1938 : : /* ??? With complex control flow (usually involving abnormal edges),
1939 : : we can wind up warning about an uninitialized value for this. Due
1940 : : to how this variable is constructed and initialized, this is never
1941 : : true. Give up and never warn. */
1942 : 1450735 : suppress_warning (result, OPT_Wuninitialized);
1943 : :
1944 : 1450735 : gimplify_ctxp->return_temp = result;
1945 : : }
1946 : :
1947 : : /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1948 : : Then gimplify the whole thing. */
1949 : 2127425 : if (result != result_decl)
1950 : 1947685 : TREE_OPERAND (ret_expr, 0) = result;
1951 : :
1952 : 2127425 : gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1953 : :
1954 : 2127425 : maybe_add_early_return_predict_stmt (pre_p);
1955 : 2127425 : ret = gimple_build_return (result);
1956 : 2127425 : copy_warning (ret, stmt);
1957 : 2127425 : gimplify_seq_add_stmt (pre_p, ret);
1958 : :
1959 : 2127425 : return GS_ALL_DONE;
1960 : : }
1961 : :
1962 : : /* Gimplify a variable-length array DECL. */
1963 : :
1964 : : static void
1965 : 8843 : gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1966 : : {
1967 : : /* This is a variable-sized decl. Simplify its size and mark it
1968 : : for deferred expansion. */
1969 : 8843 : tree t, addr, ptr_type;
1970 : :
1971 : 8843 : gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1972 : 8843 : gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1973 : :
1974 : : /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1975 : 8843 : if (DECL_HAS_VALUE_EXPR_P (decl))
1976 : : return;
1977 : :
1978 : : /* All occurrences of this decl in final gimplified code will be
1979 : : replaced by indirection. Setting DECL_VALUE_EXPR does two
1980 : : things: First, it lets the rest of the gimplifier know what
1981 : : replacement to use. Second, it lets the debug info know
1982 : : where to find the value. */
1983 : 8838 : ptr_type = build_pointer_type (TREE_TYPE (decl));
1984 : 8838 : addr = create_tmp_var (ptr_type, get_name (decl));
1985 : 8838 : DECL_IGNORED_P (addr) = 0;
1986 : 8838 : t = build_fold_indirect_ref (addr);
1987 : 8838 : TREE_THIS_NOTRAP (t) = 1;
1988 : 8838 : SET_DECL_VALUE_EXPR (decl, t);
1989 : 8838 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
1990 : :
1991 : 8838 : t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1992 : 8838 : max_int_size_in_bytes (TREE_TYPE (decl)));
1993 : : /* The call has been built for a variable-sized object. */
1994 : 8838 : CALL_ALLOCA_FOR_VAR_P (t) = 1;
1995 : 8838 : t = fold_convert (ptr_type, t);
1996 : 8838 : t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1997 : :
1998 : 8838 : gimplify_and_add (t, seq_p);
1999 : :
2000 : : /* Record the dynamic allocation associated with DECL if requested. */
2001 : 8838 : if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
2002 : 0 : record_dynamic_alloc (decl);
2003 : : }
2004 : :
2005 : : /* A helper function to be called via walk_tree. Mark all labels under *TP
2006 : : as being forced. To be called for DECL_INITIAL of static variables. */
2007 : :
2008 : : static tree
2009 : 856330 : force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2010 : : {
2011 : 856330 : if (TYPE_P (*tp))
2012 : 0 : *walk_subtrees = 0;
2013 : 856330 : if (TREE_CODE (*tp) == LABEL_DECL)
2014 : : {
2015 : 921 : FORCED_LABEL (*tp) = 1;
2016 : 921 : cfun->has_forced_label_in_static = 1;
2017 : : }
2018 : :
2019 : 856330 : return NULL_TREE;
2020 : : }
2021 : :
2022 : : /* Generate an initialization to automatic variable DECL based on INIT_TYPE.
2023 : : Build a call to internal const function DEFERRED_INIT:
2024 : : 1st argument: SIZE of the DECL;
2025 : : 2nd argument: INIT_TYPE;
2026 : : 3rd argument: NAME of the DECL;
2027 : :
2028 : : as LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL). */
2029 : :
2030 : : static void
2031 : 590 : gimple_add_init_for_auto_var (tree decl,
2032 : : enum auto_init_type init_type,
2033 : : gimple_seq *seq_p)
2034 : : {
2035 : 590 : gcc_assert (auto_var_p (decl));
2036 : 590 : gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
2037 : :
2038 : 590 : const location_t loc = DECL_SOURCE_LOCATION (decl);
2039 : 590 : tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
2040 : 590 : tree init_type_node = build_int_cst (integer_type_node, (int) init_type);
2041 : 590 : tree decl_name;
2042 : :
2043 : 590 : if (DECL_NAME (decl))
2044 : 587 : decl_name = build_string_literal (DECL_NAME (decl));
2045 : : else
2046 : : {
2047 : 3 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
2048 : 3 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
2049 : 3 : decl_name = build_string_literal (decl_name_anonymous);
2050 : : }
2051 : :
2052 : 590 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
2053 : 590 : TREE_TYPE (decl), 3,
2054 : : decl_size, init_type_node,
2055 : : decl_name);
2056 : :
2057 : 590 : gimplify_assign (decl, call, seq_p);
2058 : 590 : }
2059 : :
2060 : : /* Generate padding initialization for automatic variable DECL.
2061 : : C guarantees that brace-init with fewer initializers than members
2062 : : aggregate will initialize the rest of the aggregate as-if it were
2063 : : static initialization. In turn static initialization guarantees
2064 : : that padding is initialized to zero. So, we always initialize paddings
2065 : : to zeroes regardless INIT_TYPE.
2066 : : To do the padding initialization, we insert a call to
2067 : : __builtin_clear_padding (&decl, 0, for_auto_init = true).
2068 : : Note, we add an additional dummy argument for __builtin_clear_padding,
2069 : : 'for_auto_init' to distinguish whether this call is for automatic
2070 : : variable initialization or not.
2071 : : */
2072 : : static void
2073 : 109 : gimple_add_padding_init_for_auto_var (tree decl, bool is_vla,
2074 : : gimple_seq *seq_p)
2075 : : {
2076 : 109 : tree addr_of_decl = NULL_TREE;
2077 : 109 : tree fn = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
2078 : :
2079 : 109 : if (is_vla)
2080 : : {
2081 : : /* The temporary address variable for this vla should be
2082 : : created in gimplify_vla_decl. */
2083 : 0 : gcc_assert (DECL_HAS_VALUE_EXPR_P (decl));
2084 : 0 : gcc_assert (INDIRECT_REF_P (DECL_VALUE_EXPR (decl)));
2085 : 0 : addr_of_decl = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
2086 : : }
2087 : : else
2088 : : {
2089 : 109 : mark_addressable (decl);
2090 : 109 : addr_of_decl = build_fold_addr_expr (decl);
2091 : : }
2092 : :
2093 : 109 : gimple *call = gimple_build_call (fn, 2, addr_of_decl,
2094 : 109 : build_one_cst (TREE_TYPE (addr_of_decl)));
2095 : 109 : gimplify_seq_add_stmt (seq_p, call);
2096 : 109 : }
2097 : :
2098 : : /* Return true if the DECL need to be automaticly initialized by the
2099 : : compiler. */
2100 : : static bool
2101 : 3384235 : is_var_need_auto_init (tree decl)
2102 : : {
2103 : 3384235 : if (auto_var_p (decl)
2104 : 3291157 : && (TREE_CODE (decl) != VAR_DECL
2105 : 3289506 : || !DECL_HARD_REGISTER (decl))
2106 : 3290456 : && (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2107 : 643 : && (!lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl)))
2108 : 635 : && !OPAQUE_TYPE_P (TREE_TYPE (decl))
2109 : 3384870 : && !is_empty_type (TREE_TYPE (decl)))
2110 : : return true;
2111 : : return false;
2112 : : }
2113 : :
2114 : : /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
2115 : : and initialization explicit. */
2116 : :
2117 : : static enum gimplify_status
2118 : 6449951 : gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
2119 : : {
2120 : 6449951 : tree stmt = *stmt_p;
2121 : 6449951 : tree decl = DECL_EXPR_DECL (stmt);
2122 : :
2123 : 6449951 : *stmt_p = NULL_TREE;
2124 : :
2125 : 6449951 : if (TREE_TYPE (decl) == error_mark_node)
2126 : : return GS_ERROR;
2127 : :
2128 : 6449886 : if ((TREE_CODE (decl) == TYPE_DECL
2129 : 6407064 : || VAR_P (decl))
2130 : 12853981 : && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
2131 : : {
2132 : 687009 : gimplify_type_sizes (TREE_TYPE (decl), seq_p);
2133 : 687009 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
2134 : 21792 : gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
2135 : : }
2136 : :
2137 : : /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
2138 : : in case its size expressions contain problematic nodes like CALL_EXPR. */
2139 : 6449886 : if (TREE_CODE (decl) == TYPE_DECL
2140 : 42822 : && DECL_ORIGINAL_TYPE (decl)
2141 : 6455199 : && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
2142 : : {
2143 : 0 : gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
2144 : 0 : if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
2145 : 0 : gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
2146 : : }
2147 : :
2148 : 6449886 : if (VAR_P (decl) && !DECL_EXTERNAL (decl))
2149 : : {
2150 : 6404095 : tree init = DECL_INITIAL (decl);
2151 : 6404095 : bool is_vla = false;
2152 : : /* Check whether a decl has FE created VALUE_EXPR here BEFORE
2153 : : gimplify_vla_decl creates VALUE_EXPR for a vla decl.
2154 : : If the decl has VALUE_EXPR that was created by FE (usually
2155 : : C++FE), it's a proxy varaible, and FE already initialized
2156 : : the VALUE_EXPR of it, we should not initialize it anymore. */
2157 : 6404095 : bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
2158 : :
2159 : 6404095 : poly_uint64 size;
2160 : 6404095 : if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
2161 : 6404095 : || (!TREE_STATIC (decl)
2162 : 6280753 : && flag_stack_check == GENERIC_STACK_CHECK
2163 : 228 : && maybe_gt (size,
2164 : : (unsigned HOST_WIDE_INT) STACK_CHECK_MAX_VAR_SIZE)))
2165 : : {
2166 : 8720 : gimplify_vla_decl (decl, seq_p);
2167 : 8720 : is_vla = true;
2168 : : }
2169 : :
2170 : 6404095 : if (asan_poisoned_variables
2171 : 4343 : && !is_vla
2172 : 4157 : && TREE_ADDRESSABLE (decl)
2173 : 1996 : && !TREE_STATIC (decl)
2174 : 1786 : && !DECL_HAS_VALUE_EXPR_P (decl)
2175 : 1751 : && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
2176 : 1751 : && dbg_cnt (asan_use_after_scope)
2177 : 1751 : && !gimplify_omp_ctxp
2178 : : /* GNAT introduces temporaries to hold return values of calls in
2179 : : initializers of variables defined in other units, so the
2180 : : declaration of the variable is discarded completely. We do not
2181 : : want to issue poison calls for such dropped variables. */
2182 : 6405838 : && (DECL_SEEN_IN_BIND_EXPR_P (decl)
2183 : 0 : || (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)))
2184 : : {
2185 : 1743 : asan_poisoned_variables->add (decl);
2186 : 1743 : asan_poison_variable (decl, false, seq_p);
2187 : 1743 : if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
2188 : 55 : gimplify_ctxp->live_switch_vars->add (decl);
2189 : : }
2190 : :
2191 : : /* Some front ends do not explicitly declare all anonymous
2192 : : artificial variables. We compensate here by declaring the
2193 : : variables, though it would be better if the front ends would
2194 : : explicitly declare them. */
2195 : 6404095 : if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
2196 : 6404095 : && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
2197 : 16448 : gimple_add_tmp_var (decl);
2198 : :
2199 : 6404095 : if (init && init != error_mark_node)
2200 : : {
2201 : 3676368 : if (!TREE_STATIC (decl))
2202 : : {
2203 : 3647161 : DECL_INITIAL (decl) = NULL_TREE;
2204 : 3647161 : init = build2 (INIT_EXPR, void_type_node, decl, init);
2205 : 3647161 : gimplify_and_add (init, seq_p);
2206 : 3647161 : ggc_free (init);
2207 : : /* Clear TREE_READONLY if we really have an initialization. */
2208 : 3647161 : if (!DECL_INITIAL (decl)
2209 : 3647161 : && !omp_privatize_by_reference (decl))
2210 : 3630223 : TREE_READONLY (decl) = 0;
2211 : : }
2212 : : else
2213 : : /* We must still examine initializers for static variables
2214 : : as they may contain a label address. */
2215 : 29207 : walk_tree (&init, force_labels_r, NULL, NULL);
2216 : : }
2217 : : /* When there is no explicit initializer, if the user requested,
2218 : : We should insert an artifical initializer for this automatic
2219 : : variable. */
2220 : 2727727 : else if (is_var_need_auto_init (decl)
2221 : 2727727 : && !decl_had_value_expr_p)
2222 : : {
2223 : 590 : gimple_add_init_for_auto_var (decl,
2224 : : flag_auto_var_init,
2225 : : seq_p);
2226 : : /* The expanding of a call to the above .DEFERRED_INIT will apply
2227 : : block initialization to the whole space covered by this variable.
2228 : : As a result, all the paddings will be initialized to zeroes
2229 : : for zero initialization and 0xFE byte-repeatable patterns for
2230 : : pattern initialization.
2231 : : In order to make the paddings as zeroes for pattern init, We
2232 : : should add a call to __builtin_clear_padding to clear the
2233 : : paddings to zero in compatiple with CLANG.
2234 : : We cannot insert this call if the variable is a gimple register
2235 : : since __builtin_clear_padding will take the address of the
2236 : : variable. As a result, if a long double/_Complex long double
2237 : : variable will spilled into stack later, its padding is 0XFE. */
2238 : 590 : if (flag_auto_var_init == AUTO_INIT_PATTERN
2239 : 180 : && !is_gimple_reg (decl)
2240 : 684 : && clear_padding_type_may_have_padding_p (TREE_TYPE (decl)))
2241 : 71 : gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
2242 : : }
2243 : : }
2244 : :
2245 : : return GS_ALL_DONE;
2246 : : }
2247 : :
2248 : : /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
2249 : : and replacing the LOOP_EXPR with goto, but if the loop contains an
2250 : : EXIT_EXPR, we need to append a label for it to jump to. */
2251 : :
2252 : : static enum gimplify_status
2253 : 194002 : gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
2254 : : {
2255 : 194002 : tree saved_label = gimplify_ctxp->exit_label;
2256 : 194002 : tree start_label = create_artificial_label (UNKNOWN_LOCATION);
2257 : :
2258 : 194002 : gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
2259 : :
2260 : 194002 : gimplify_ctxp->exit_label = NULL_TREE;
2261 : :
2262 : 194002 : gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
2263 : :
2264 : 194002 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
2265 : :
2266 : 194002 : if (gimplify_ctxp->exit_label)
2267 : 6198 : gimplify_seq_add_stmt (pre_p,
2268 : 3099 : gimple_build_label (gimplify_ctxp->exit_label));
2269 : :
2270 : 194002 : gimplify_ctxp->exit_label = saved_label;
2271 : :
2272 : 194002 : *expr_p = NULL;
2273 : 194002 : return GS_ALL_DONE;
2274 : : }
2275 : :
2276 : : /* Gimplify a statement list onto a sequence. These may be created either
2277 : : by an enlightened front-end, or by shortcut_cond_expr. */
2278 : :
2279 : : static enum gimplify_status
2280 : 8850025 : gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
2281 : : {
2282 : 8850025 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
2283 : :
2284 : 8850025 : tree_stmt_iterator i = tsi_start (*expr_p);
2285 : :
2286 : 56004604 : while (!tsi_end_p (i))
2287 : : {
2288 : 38304554 : gimplify_stmt (tsi_stmt_ptr (i), pre_p);
2289 : 38304554 : tsi_delink (&i);
2290 : : }
2291 : :
2292 : 8850025 : if (temp)
2293 : : {
2294 : 15273 : *expr_p = temp;
2295 : 15273 : return GS_OK;
2296 : : }
2297 : :
2298 : : return GS_ALL_DONE;
2299 : : }
2300 : :
2301 : :
2302 : : /* Emit warning for the unreachable statment STMT if needed.
2303 : : Return the gimple itself when the warning is emitted, otherwise
2304 : : return NULL. */
2305 : : static gimple *
2306 : 188 : emit_warn_switch_unreachable (gimple *stmt)
2307 : : {
2308 : 188 : if (gimple_code (stmt) == GIMPLE_GOTO
2309 : 45 : && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
2310 : 233 : && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
2311 : : /* Don't warn for compiler-generated gotos. These occur
2312 : : in Duff's devices, for example. */
2313 : : return NULL;
2314 : 143 : else if ((flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
2315 : 143 : && ((gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2316 : 6 : || (gimple_call_builtin_p (stmt, BUILT_IN_CLEAR_PADDING)
2317 : 2 : && (bool) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)))
2318 : 4 : || (is_gimple_assign (stmt)
2319 : 4 : && gimple_assign_single_p (stmt)
2320 : 4 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2321 : 4 : && gimple_call_internal_p (
2322 : 4 : SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)),
2323 : : IFN_DEFERRED_INIT))))
2324 : : /* Don't warn for compiler-generated initializations for
2325 : : -ftrivial-auto-var-init.
2326 : : There are 3 cases:
2327 : : case 1: a call to .DEFERRED_INIT;
2328 : : case 2: a call to __builtin_clear_padding with the 2nd argument is
2329 : : present and non-zero;
2330 : : case 3: a gimple assign store right after the call to .DEFERRED_INIT
2331 : : that has the LHS of .DEFERRED_INIT as the RHS as following:
2332 : : _1 = .DEFERRED_INIT (4, 2, &"i1"[0]);
2333 : : i1 = _1. */
2334 : 12 : return NULL;
2335 : : else
2336 : 131 : warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
2337 : : "statement will never be executed");
2338 : 131 : return stmt;
2339 : : }
2340 : :
2341 : : /* Callback for walk_gimple_seq. */
2342 : :
2343 : : static tree
2344 : 51805 : warn_switch_unreachable_and_auto_init_r (gimple_stmt_iterator *gsi_p,
2345 : : bool *handled_ops_p,
2346 : : struct walk_stmt_info *wi)
2347 : : {
2348 : 51805 : gimple *stmt = gsi_stmt (*gsi_p);
2349 : 51805 : bool unreachable_issued = wi->info != NULL;
2350 : :
2351 : 51805 : *handled_ops_p = true;
2352 : 51805 : switch (gimple_code (stmt))
2353 : : {
2354 : 133 : case GIMPLE_TRY:
2355 : : /* A compiler-generated cleanup or a user-written try block.
2356 : : If it's empty, don't dive into it--that would result in
2357 : : worse location info. */
2358 : 133 : if (gimple_try_eval (stmt) == NULL)
2359 : : {
2360 : 3 : if (warn_switch_unreachable && !unreachable_issued)
2361 : 3 : wi->info = emit_warn_switch_unreachable (stmt);
2362 : :
2363 : : /* Stop when auto var init warning is not on. */
2364 : 3 : if (!warn_trivial_auto_var_init)
2365 : 3 : return integer_zero_node;
2366 : : }
2367 : : /* Fall through. */
2368 : 3009 : case GIMPLE_BIND:
2369 : 3009 : case GIMPLE_CATCH:
2370 : 3009 : case GIMPLE_EH_FILTER:
2371 : 3009 : case GIMPLE_TRANSACTION:
2372 : : /* Walk the sub-statements. */
2373 : 3009 : *handled_ops_p = false;
2374 : 3009 : break;
2375 : :
2376 : : case GIMPLE_DEBUG:
2377 : : /* Ignore these. We may generate them before declarations that
2378 : : are never executed. If there's something to warn about,
2379 : : there will be non-debug stmts too, and we'll catch those. */
2380 : : break;
2381 : :
2382 : 47757 : case GIMPLE_LABEL:
2383 : : /* Stop till the first Label. */
2384 : 47757 : return integer_zero_node;
2385 : 46 : case GIMPLE_CALL:
2386 : 46 : if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2387 : : {
2388 : 8 : *handled_ops_p = false;
2389 : 8 : break;
2390 : : }
2391 : 38 : if (warn_trivial_auto_var_init
2392 : 14 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
2393 : 52 : && gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
2394 : : {
2395 : : /* Get the variable name from the 3rd argument of call. */
2396 : 12 : tree var_name = gimple_call_arg (stmt, 2);
2397 : 12 : var_name = TREE_OPERAND (TREE_OPERAND (var_name, 0), 0);
2398 : 12 : const char *var_name_str = TREE_STRING_POINTER (var_name);
2399 : :
2400 : 12 : warning_at (gimple_location (stmt), OPT_Wtrivial_auto_var_init,
2401 : : "%qs cannot be initialized with "
2402 : : "%<-ftrivial-auto-var_init%>",
2403 : : var_name_str);
2404 : 12 : break;
2405 : : }
2406 : :
2407 : : /* Fall through. */
2408 : 185 : default:
2409 : : /* check the first "real" statement (not a decl/lexical scope/...), issue
2410 : : warning if needed. */
2411 : 185 : if (warn_switch_unreachable && !unreachable_issued)
2412 : 185 : wi->info = emit_warn_switch_unreachable (stmt);
2413 : : /* Stop when auto var init warning is not on. */
2414 : 185 : if (!warn_trivial_auto_var_init)
2415 : 179 : return integer_zero_node;
2416 : : break;
2417 : : }
2418 : : return NULL_TREE;
2419 : : }
2420 : :
2421 : :
2422 : : /* Possibly warn about unreachable statements between switch's controlling
2423 : : expression and the first case. Also warn about -ftrivial-auto-var-init
2424 : : cannot initialize the auto variable under such situation.
2425 : : SEQ is the body of a switch expression. */
2426 : :
2427 : : static void
2428 : 51698 : maybe_warn_switch_unreachable_and_auto_init (gimple_seq seq)
2429 : : {
2430 : 60 : if ((!warn_switch_unreachable && !warn_trivial_auto_var_init)
2431 : : /* This warning doesn't play well with Fortran when optimizations
2432 : : are on. */
2433 : 51638 : || lang_GNU_Fortran ()
2434 : 99795 : || seq == NULL)
2435 : 3756 : return;
2436 : :
2437 : 47942 : struct walk_stmt_info wi;
2438 : :
2439 : 47942 : memset (&wi, 0, sizeof (wi));
2440 : 47942 : walk_gimple_seq (seq, warn_switch_unreachable_and_auto_init_r, NULL, &wi);
2441 : : }
2442 : :
2443 : :
2444 : : /* A label entry that pairs label and a location. */
2445 : : struct label_entry
2446 : : {
2447 : : tree label;
2448 : : location_t loc;
2449 : : };
2450 : :
2451 : : /* Find LABEL in vector of label entries VEC. */
2452 : :
2453 : : static struct label_entry *
2454 : 11747 : find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
2455 : : {
2456 : 11747 : unsigned int i;
2457 : 11747 : struct label_entry *l;
2458 : :
2459 : 22500 : FOR_EACH_VEC_ELT (*vec, i, l)
2460 : 19227 : if (l->label == label)
2461 : : return l;
2462 : : return NULL;
2463 : : }
2464 : :
2465 : : /* Return true if LABEL, a LABEL_DECL, represents a case label
2466 : : in a vector of labels CASES. */
2467 : :
2468 : : static bool
2469 : 15180 : case_label_p (const vec<tree> *cases, tree label)
2470 : : {
2471 : 15180 : unsigned int i;
2472 : 15180 : tree l;
2473 : :
2474 : 154998 : FOR_EACH_VEC_ELT (*cases, i, l)
2475 : 154672 : if (CASE_LABEL (l) == label)
2476 : : return true;
2477 : : return false;
2478 : : }
2479 : :
2480 : : /* Find the last nondebug statement in a scope STMT. */
2481 : :
2482 : : static gimple *
2483 : 2024 : last_stmt_in_scope (gimple *stmt)
2484 : : {
2485 : 3802 : if (!stmt)
2486 : : return NULL;
2487 : :
2488 : 3791 : switch (gimple_code (stmt))
2489 : : {
2490 : 1324 : case GIMPLE_BIND:
2491 : 1324 : {
2492 : 1324 : gbind *bind = as_a <gbind *> (stmt);
2493 : 1324 : stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
2494 : 1324 : return last_stmt_in_scope (stmt);
2495 : : }
2496 : :
2497 : 617 : case GIMPLE_TRY:
2498 : 617 : {
2499 : 617 : gtry *try_stmt = as_a <gtry *> (stmt);
2500 : 617 : stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
2501 : 617 : gimple *last_eval = last_stmt_in_scope (stmt);
2502 : 617 : if (gimple_stmt_may_fallthru (last_eval)
2503 : 473 : && (last_eval == NULL
2504 : 470 : || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
2505 : 1083 : && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2506 : : {
2507 : 454 : stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
2508 : 454 : return last_stmt_in_scope (stmt);
2509 : : }
2510 : : else
2511 : : return last_eval;
2512 : : }
2513 : :
2514 : 0 : case GIMPLE_DEBUG:
2515 : 0 : gcc_unreachable ();
2516 : :
2517 : : default:
2518 : : return stmt;
2519 : : }
2520 : : }
2521 : :
2522 : : /* Collect labels that may fall through into LABELS and return the statement
2523 : : preceding another case label, or a user-defined label. Store a location
2524 : : useful to give warnings at *PREVLOC (usually the location of the returned
2525 : : statement or of its surrounding scope). */
2526 : :
2527 : : static gimple *
2528 : 19563 : collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
2529 : : auto_vec <struct label_entry> *labels,
2530 : : location_t *prevloc)
2531 : : {
2532 : 19563 : gimple *prev = NULL;
2533 : :
2534 : 19563 : *prevloc = UNKNOWN_LOCATION;
2535 : 95938 : do
2536 : : {
2537 : 95938 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
2538 : : {
2539 : : /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
2540 : : which starts on a GIMPLE_SWITCH and ends with a break label.
2541 : : Handle that as a single statement that can fall through. */
2542 : 1385 : gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
2543 : 1385 : gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
2544 : 1385 : gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
2545 : 1377 : if (last
2546 : 1377 : && gimple_code (first) == GIMPLE_SWITCH
2547 : 1479 : && gimple_code (last) == GIMPLE_LABEL)
2548 : : {
2549 : 102 : tree label = gimple_label_label (as_a <glabel *> (last));
2550 : 102 : if (SWITCH_BREAK_LABEL_P (label))
2551 : : {
2552 : 102 : prev = bind;
2553 : 102 : gsi_next (gsi_p);
2554 : 102 : continue;
2555 : : }
2556 : : }
2557 : : }
2558 : 95836 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
2559 : 95836 : || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2560 : : {
2561 : : /* Nested scope. Only look at the last statement of
2562 : : the innermost scope. */
2563 : 1407 : location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2564 : 1407 : gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2565 : 1407 : if (last)
2566 : : {
2567 : 1399 : prev = last;
2568 : : /* It might be a label without a location. Use the
2569 : : location of the scope then. */
2570 : 1399 : if (!gimple_has_location (prev))
2571 : 616 : *prevloc = bind_loc;
2572 : : }
2573 : 1407 : gsi_next (gsi_p);
2574 : 1407 : continue;
2575 : 1407 : }
2576 : :
2577 : : /* Ifs are tricky. */
2578 : 94429 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2579 : : {
2580 : 6949 : gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2581 : 6949 : tree false_lab = gimple_cond_false_label (cond_stmt);
2582 : 6949 : location_t if_loc = gimple_location (cond_stmt);
2583 : :
2584 : : /* If we have e.g.
2585 : : if (i > 1) goto <D.2259>; else goto D;
2586 : : we can't do much with the else-branch. */
2587 : 6949 : if (!DECL_ARTIFICIAL (false_lab))
2588 : : break;
2589 : :
2590 : : /* Go on until the false label, then one step back. */
2591 : 52928 : for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2592 : : {
2593 : 52928 : gimple *stmt = gsi_stmt (*gsi_p);
2594 : 52928 : if (gimple_code (stmt) == GIMPLE_LABEL
2595 : 52928 : && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2596 : : break;
2597 : : }
2598 : :
2599 : : /* Not found? Oops. */
2600 : 6949 : if (gsi_end_p (*gsi_p))
2601 : : break;
2602 : :
2603 : : /* A dead label can't fall through. */
2604 : 6949 : if (!UNUSED_LABEL_P (false_lab))
2605 : : {
2606 : 6902 : struct label_entry l = { false_lab, if_loc };
2607 : 6902 : labels->safe_push (l);
2608 : : }
2609 : :
2610 : : /* Go to the last statement of the then branch. */
2611 : 6949 : gsi_prev (gsi_p);
2612 : :
2613 : : /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2614 : : <D.1759>:
2615 : : <stmt>;
2616 : : goto <D.1761>;
2617 : : <D.1760>:
2618 : : */
2619 : 6949 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2620 : 6949 : && !gimple_has_location (gsi_stmt (*gsi_p)))
2621 : : {
2622 : : /* Look at the statement before, it might be
2623 : : attribute fallthrough, in which case don't warn. */
2624 : 1431 : gsi_prev (gsi_p);
2625 : 1431 : bool fallthru_before_dest
2626 : 1431 : = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2627 : 1431 : gsi_next (gsi_p);
2628 : 1431 : tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2629 : 1431 : if (!fallthru_before_dest)
2630 : : {
2631 : 1374 : struct label_entry l = { goto_dest, if_loc };
2632 : 1374 : labels->safe_push (l);
2633 : : }
2634 : : }
2635 : : /* This case is about
2636 : : if (1 != 0) goto <D.2022>; else goto <D.2023>;
2637 : : <D.2022>:
2638 : : n = n + 1; // #1
2639 : : <D.2023>: // #2
2640 : : <D.1988>: // #3
2641 : : where #2 is UNUSED_LABEL_P and we want to warn about #1 falling
2642 : : through to #3. So set PREV to #1. */
2643 : 5518 : else if (UNUSED_LABEL_P (false_lab))
2644 : 47 : prev = gsi_stmt (*gsi_p);
2645 : :
2646 : : /* And move back. */
2647 : 6949 : gsi_next (gsi_p);
2648 : : }
2649 : :
2650 : : /* Remember the last statement. Skip labels that are of no interest
2651 : : to us. */
2652 : 94429 : if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2653 : : {
2654 : 11529 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2655 : 11529 : if (find_label_entry (labels, label))
2656 : 94429 : prev = gsi_stmt (*gsi_p);
2657 : : }
2658 : 82900 : else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2659 : : ;
2660 : 82900 : else if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_PREDICT)
2661 : : ;
2662 : 81732 : else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2663 : 94429 : prev = gsi_stmt (*gsi_p);
2664 : 94429 : gsi_next (gsi_p);
2665 : : }
2666 : 95938 : while (!gsi_end_p (*gsi_p)
2667 : : /* Stop if we find a case or a user-defined label. */
2668 : 191876 : && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2669 : 20272 : || !gimple_has_location (gsi_stmt (*gsi_p))));
2670 : :
2671 : 19563 : if (prev && gimple_has_location (prev))
2672 : 19110 : *prevloc = gimple_location (prev);
2673 : 19563 : return prev;
2674 : : }
2675 : :
2676 : : /* Return true if the switch fallthough warning should occur. LABEL is
2677 : : the label statement that we're falling through to. */
2678 : :
2679 : : static bool
2680 : 15685 : should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2681 : : {
2682 : 15685 : gimple_stmt_iterator gsi = *gsi_p;
2683 : :
2684 : : /* Don't warn if the label is marked with a "falls through" comment. */
2685 : 15685 : if (FALLTHROUGH_LABEL_P (label))
2686 : : return false;
2687 : :
2688 : : /* Don't warn for non-case labels followed by a statement:
2689 : : case 0:
2690 : : foo ();
2691 : : label:
2692 : : bar ();
2693 : : as these are likely intentional. */
2694 : 14925 : if (!case_label_p (&gimplify_ctxp->case_labels, label))
2695 : : {
2696 : : tree l;
2697 : 326 : while (!gsi_end_p (gsi)
2698 : 318 : && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2699 : 255 : && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2700 : 581 : && !case_label_p (&gimplify_ctxp->case_labels, l))
2701 : 169 : gsi_next_nondebug (&gsi);
2702 : 157 : if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2703 : : return false;
2704 : : }
2705 : :
2706 : : /* Don't warn for terminated branches, i.e. when the subsequent case labels
2707 : : immediately breaks. */
2708 : 14854 : gsi = *gsi_p;
2709 : :
2710 : : /* Skip all immediately following labels. */
2711 : 33147 : while (!gsi_end_p (gsi)
2712 : 33147 : && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2713 : 14991 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2714 : 18293 : gsi_next_nondebug (&gsi);
2715 : :
2716 : : /* { ... something; default:; } */
2717 : 14854 : if (gsi_end_p (gsi)
2718 : : /* { ... something; default: break; } or
2719 : : { ... something; default: goto L; } */
2720 : 14790 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2721 : : /* { ... something; default: return; } */
2722 : 28561 : || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2723 : : return false;
2724 : :
2725 : : return true;
2726 : : }
2727 : :
2728 : : /* Callback for walk_gimple_seq. */
2729 : :
2730 : : static tree
2731 : 20417 : warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2732 : : struct walk_stmt_info *)
2733 : : {
2734 : 20417 : gimple *stmt = gsi_stmt (*gsi_p);
2735 : :
2736 : 20417 : *handled_ops_p = true;
2737 : 20417 : switch (gimple_code (stmt))
2738 : : {
2739 : 222 : case GIMPLE_TRY:
2740 : 222 : case GIMPLE_BIND:
2741 : 222 : case GIMPLE_CATCH:
2742 : 222 : case GIMPLE_EH_FILTER:
2743 : 222 : case GIMPLE_TRANSACTION:
2744 : : /* Walk the sub-statements. */
2745 : 222 : *handled_ops_p = false;
2746 : 222 : break;
2747 : :
2748 : : /* Find a sequence of form:
2749 : :
2750 : : GIMPLE_LABEL
2751 : : [...]
2752 : : <may fallthru stmt>
2753 : : GIMPLE_LABEL
2754 : :
2755 : : and possibly warn. */
2756 : : case GIMPLE_LABEL:
2757 : : {
2758 : : /* Found a label. Skip all immediately following labels. */
2759 : 46029 : while (!gsi_end_p (*gsi_p)
2760 : 46029 : && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2761 : 26279 : gsi_next_nondebug (gsi_p);
2762 : :
2763 : : /* There might be no more statements. */
2764 : 19750 : if (gsi_end_p (*gsi_p))
2765 : 4058 : return integer_zero_node;
2766 : :
2767 : : /* Vector of labels that fall through. */
2768 : 19563 : auto_vec <struct label_entry> labels;
2769 : 19563 : location_t prevloc;
2770 : 19563 : gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2771 : :
2772 : : /* There might be no more statements. */
2773 : 19563 : if (gsi_end_p (*gsi_p))
2774 : 3871 : return integer_zero_node;
2775 : :
2776 : 15692 : gimple *next = gsi_stmt (*gsi_p);
2777 : 15692 : tree label;
2778 : : /* If what follows is a label, then we may have a fallthrough. */
2779 : 15692 : if (gimple_code (next) == GIMPLE_LABEL
2780 : 15692 : && gimple_has_location (next)
2781 : 15692 : && (label = gimple_label_label (as_a <glabel *> (next)))
2782 : 31384 : && prev != NULL)
2783 : : {
2784 : 15685 : struct label_entry *l;
2785 : 15685 : bool warned_p = false;
2786 : 15685 : auto_diagnostic_group d;
2787 : 15685 : if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2788 : : /* Quiet. */;
2789 : 13680 : else if (gimple_code (prev) == GIMPLE_LABEL
2790 : 218 : && (label = gimple_label_label (as_a <glabel *> (prev)))
2791 : 13898 : && (l = find_label_entry (&labels, label)))
2792 : 198 : warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2793 : : "this statement may fall through");
2794 : 13482 : else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2795 : : /* Try to be clever and don't warn when the statement
2796 : : can't actually fall through. */
2797 : 13085 : && gimple_stmt_may_fallthru (prev)
2798 : 13775 : && prevloc != UNKNOWN_LOCATION)
2799 : 293 : warned_p = warning_at (prevloc,
2800 : 293 : OPT_Wimplicit_fallthrough_,
2801 : : "this statement may fall through");
2802 : 491 : if (warned_p)
2803 : 491 : inform (gimple_location (next), "here");
2804 : :
2805 : : /* Mark this label as processed so as to prevent multiple
2806 : : warnings in nested switches. */
2807 : 15685 : FALLTHROUGH_LABEL_P (label) = true;
2808 : :
2809 : : /* So that next warn_implicit_fallthrough_r will start looking for
2810 : : a new sequence starting with this label. */
2811 : 15685 : gsi_prev (gsi_p);
2812 : 15685 : }
2813 : 3871 : }
2814 : 15692 : break;
2815 : : default:
2816 : : break;
2817 : : }
2818 : : return NULL_TREE;
2819 : : }
2820 : :
2821 : : /* Warn when a switch case falls through. */
2822 : :
2823 : : static void
2824 : 51698 : maybe_warn_implicit_fallthrough (gimple_seq seq)
2825 : : {
2826 : 51698 : if (!warn_implicit_fallthrough)
2827 : 47736 : return;
2828 : :
2829 : : /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2830 : 3962 : if (!(lang_GNU_C ()
2831 : 1059 : || lang_GNU_CXX ()
2832 : 0 : || lang_GNU_OBJC ()))
2833 : : return;
2834 : :
2835 : 3962 : struct walk_stmt_info wi;
2836 : 3962 : memset (&wi, 0, sizeof (wi));
2837 : 3962 : walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2838 : : }
2839 : :
2840 : : /* Callback for walk_gimple_seq. */
2841 : :
2842 : : static tree
2843 : 3410815 : expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2844 : : struct walk_stmt_info *wi)
2845 : : {
2846 : 3410815 : gimple *stmt = gsi_stmt (*gsi_p);
2847 : :
2848 : 3410815 : *handled_ops_p = true;
2849 : 3410815 : switch (gimple_code (stmt))
2850 : : {
2851 : 155949 : case GIMPLE_TRY:
2852 : 155949 : case GIMPLE_BIND:
2853 : 155949 : case GIMPLE_CATCH:
2854 : 155949 : case GIMPLE_EH_FILTER:
2855 : 155949 : case GIMPLE_TRANSACTION:
2856 : : /* Walk the sub-statements. */
2857 : 155949 : *handled_ops_p = false;
2858 : 155949 : break;
2859 : 285805 : case GIMPLE_CALL:
2860 : 285805 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2861 : 285805 : if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2862 : : {
2863 : 5279 : location_t loc = gimple_location (stmt);
2864 : 5279 : gsi_remove (gsi_p, true);
2865 : 5279 : wi->removed_stmt = true;
2866 : :
2867 : : /* nothrow flag is added by genericize_c_loop to mark fallthrough
2868 : : statement at the end of some loop's body. Those should be
2869 : : always diagnosed, either because they indeed don't precede
2870 : : a case label or default label, or because the next statement
2871 : : is not within the same iteration statement. */
2872 : 5279 : if ((stmt->subcode & GF_CALL_NOTHROW) != 0)
2873 : : {
2874 : 12 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2875 : : "a case label or default label");
2876 : 12 : break;
2877 : : }
2878 : :
2879 : 5267 : if (gsi_end_p (*gsi_p))
2880 : : {
2881 : 32 : static_cast<location_t *>(wi->info)[0] = BUILTINS_LOCATION;
2882 : 32 : static_cast<location_t *>(wi->info)[1] = loc;
2883 : 32 : break;
2884 : : }
2885 : :
2886 : 5235 : bool found = false;
2887 : :
2888 : 5235 : gimple_stmt_iterator gsi2 = *gsi_p;
2889 : 5235 : stmt = gsi_stmt (gsi2);
2890 : 5235 : if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2891 : : {
2892 : : /* Go on until the artificial label. */
2893 : 57 : tree goto_dest = gimple_goto_dest (stmt);
2894 : 414 : for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2895 : : {
2896 : 357 : if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2897 : 357 : && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2898 : : == goto_dest)
2899 : : break;
2900 : : }
2901 : :
2902 : : /* Not found? Stop. */
2903 : 57 : if (gsi_end_p (gsi2))
2904 : : break;
2905 : :
2906 : : /* Look one past it. */
2907 : 57 : gsi_next (&gsi2);
2908 : : }
2909 : :
2910 : : /* We're looking for a case label or default label here. */
2911 : 5325 : while (!gsi_end_p (gsi2))
2912 : : {
2913 : 5325 : stmt = gsi_stmt (gsi2);
2914 : 5325 : if (gimple_code (stmt) == GIMPLE_LABEL)
2915 : : {
2916 : 5282 : tree label = gimple_label_label (as_a <glabel *> (stmt));
2917 : 5282 : if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2918 : : {
2919 : : found = true;
2920 : : break;
2921 : : }
2922 : : }
2923 : 43 : else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2924 : : ;
2925 : 43 : else if (!is_gimple_debug (stmt))
2926 : : /* Anything else is not expected. */
2927 : : break;
2928 : 90 : gsi_next (&gsi2);
2929 : : }
2930 : 5235 : if (!found)
2931 : 23 : pedwarn (loc, 0, "attribute %<fallthrough%> not preceding "
2932 : : "a case label or default label");
2933 : : }
2934 : : break;
2935 : 2969061 : default:
2936 : 2969061 : static_cast<location_t *>(wi->info)[0] = UNKNOWN_LOCATION;
2937 : 2969061 : break;
2938 : : }
2939 : 3410815 : return NULL_TREE;
2940 : : }
2941 : :
2942 : : /* Expand all FALLTHROUGH () calls in SEQ. */
2943 : :
2944 : : static void
2945 : 50376 : expand_FALLTHROUGH (gimple_seq *seq_p)
2946 : : {
2947 : 50376 : auto_urlify_attributes sentinel;
2948 : :
2949 : 50376 : struct walk_stmt_info wi;
2950 : 50376 : location_t loc[2];
2951 : 50376 : memset (&wi, 0, sizeof (wi));
2952 : 50376 : loc[0] = UNKNOWN_LOCATION;
2953 : 50376 : loc[1] = UNKNOWN_LOCATION;
2954 : 50376 : wi.info = (void *) &loc[0];
2955 : 50376 : walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2956 : 50376 : if (loc[0] != UNKNOWN_LOCATION)
2957 : : /* We've found [[fallthrough]]; at the end of a switch, which the C++
2958 : : standard says is ill-formed; see [dcl.attr.fallthrough]. */
2959 : 8 : pedwarn (loc[1], 0, "attribute %<fallthrough%> not preceding "
2960 : : "a case label or default label");
2961 : 50376 : }
2962 : :
2963 : :
2964 : : /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2965 : : branch to. */
2966 : :
2967 : : static enum gimplify_status
2968 : 51698 : gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2969 : : {
2970 : 51698 : tree switch_expr = *expr_p;
2971 : 51698 : gimple_seq switch_body_seq = NULL;
2972 : 51698 : enum gimplify_status ret;
2973 : 51698 : tree index_type = TREE_TYPE (switch_expr);
2974 : 51698 : if (index_type == NULL_TREE)
2975 : 11287 : index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2976 : :
2977 : 51698 : ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2978 : : fb_rvalue);
2979 : 51698 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
2980 : : return ret;
2981 : :
2982 : 51698 : if (SWITCH_BODY (switch_expr))
2983 : : {
2984 : 51698 : vec<tree> labels;
2985 : 51698 : vec<tree> saved_labels;
2986 : 51698 : hash_set<tree> *saved_live_switch_vars = NULL;
2987 : 51698 : tree default_case = NULL_TREE;
2988 : 51698 : gswitch *switch_stmt;
2989 : :
2990 : : /* Save old labels, get new ones from body, then restore the old
2991 : : labels. Save all the things from the switch body to append after. */
2992 : 51698 : saved_labels = gimplify_ctxp->case_labels;
2993 : 51698 : gimplify_ctxp->case_labels.create (8);
2994 : :
2995 : : /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2996 : 51698 : saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2997 : 51698 : tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2998 : 51698 : if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2999 : 51430 : gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
3000 : : else
3001 : 268 : gimplify_ctxp->live_switch_vars = NULL;
3002 : :
3003 : 51698 : bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
3004 : 51698 : gimplify_ctxp->in_switch_expr = true;
3005 : :
3006 : 51698 : gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
3007 : :
3008 : 51698 : gimplify_ctxp->in_switch_expr = old_in_switch_expr;
3009 : 51698 : maybe_warn_switch_unreachable_and_auto_init (switch_body_seq);
3010 : 51698 : maybe_warn_implicit_fallthrough (switch_body_seq);
3011 : : /* Only do this for the outermost GIMPLE_SWITCH. */
3012 : 51698 : if (!gimplify_ctxp->in_switch_expr)
3013 : 50376 : expand_FALLTHROUGH (&switch_body_seq);
3014 : :
3015 : 51698 : labels = gimplify_ctxp->case_labels;
3016 : 51698 : gimplify_ctxp->case_labels = saved_labels;
3017 : :
3018 : 51698 : if (gimplify_ctxp->live_switch_vars)
3019 : : {
3020 : 51430 : gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
3021 : 51430 : delete gimplify_ctxp->live_switch_vars;
3022 : : }
3023 : 51698 : gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
3024 : :
3025 : 51698 : preprocess_case_label_vec_for_gimple (labels, index_type,
3026 : : &default_case);
3027 : :
3028 : 51698 : bool add_bind = false;
3029 : 51698 : if (!default_case)
3030 : : {
3031 : 15097 : glabel *new_default;
3032 : :
3033 : 15097 : default_case
3034 : 15097 : = build_case_label (NULL_TREE, NULL_TREE,
3035 : : create_artificial_label (UNKNOWN_LOCATION));
3036 : 15097 : if (old_in_switch_expr)
3037 : : {
3038 : 763 : SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
3039 : 763 : add_bind = true;
3040 : : }
3041 : 15097 : new_default = gimple_build_label (CASE_LABEL (default_case));
3042 : 15097 : gimplify_seq_add_stmt (&switch_body_seq, new_default);
3043 : : }
3044 : 36601 : else if (old_in_switch_expr)
3045 : : {
3046 : 559 : gimple *last = gimple_seq_last_stmt (switch_body_seq);
3047 : 559 : if (last && gimple_code (last) == GIMPLE_LABEL)
3048 : : {
3049 : 302 : tree label = gimple_label_label (as_a <glabel *> (last));
3050 : 302 : if (SWITCH_BREAK_LABEL_P (label))
3051 : 51698 : add_bind = true;
3052 : : }
3053 : : }
3054 : :
3055 : 51698 : switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
3056 : : default_case, labels);
3057 : 51698 : gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
3058 : : /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
3059 : : ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
3060 : : wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
3061 : : so that we can easily find the start and end of the switch
3062 : : statement. */
3063 : 51698 : if (add_bind)
3064 : : {
3065 : 1064 : gimple_seq bind_body = NULL;
3066 : 1064 : gimplify_seq_add_stmt (&bind_body, switch_stmt);
3067 : 1064 : gimple_seq_add_seq (&bind_body, switch_body_seq);
3068 : 1064 : gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
3069 : 1064 : gimple_set_location (bind, EXPR_LOCATION (switch_expr));
3070 : 1064 : gimplify_seq_add_stmt (pre_p, bind);
3071 : : }
3072 : : else
3073 : : {
3074 : 50634 : gimplify_seq_add_stmt (pre_p, switch_stmt);
3075 : 50634 : gimplify_seq_add_seq (pre_p, switch_body_seq);
3076 : : }
3077 : 51698 : labels.release ();
3078 : : }
3079 : : else
3080 : 0 : gcc_unreachable ();
3081 : :
3082 : 51698 : return GS_ALL_DONE;
3083 : : }
3084 : :
3085 : : /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
3086 : :
3087 : : static enum gimplify_status
3088 : 2490647 : gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
3089 : : {
3090 : 2490647 : gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
3091 : : == current_function_decl);
3092 : :
3093 : 2490647 : tree label = LABEL_EXPR_LABEL (*expr_p);
3094 : 2490647 : glabel *label_stmt = gimple_build_label (label);
3095 : 2490647 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3096 : 2490647 : gimplify_seq_add_stmt (pre_p, label_stmt);
3097 : :
3098 : 2490647 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3099 : 21 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3100 : : NOT_TAKEN));
3101 : 2490626 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3102 : 9 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3103 : : TAKEN));
3104 : :
3105 : 2490647 : return GS_ALL_DONE;
3106 : : }
3107 : :
3108 : : /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
3109 : :
3110 : : static enum gimplify_status
3111 : 1079077 : gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
3112 : : {
3113 : 1079077 : struct gimplify_ctx *ctxp;
3114 : 1079077 : glabel *label_stmt;
3115 : :
3116 : : /* Invalid programs can play Duff's Device type games with, for example,
3117 : : #pragma omp parallel. At least in the C front end, we don't
3118 : : detect such invalid branches until after gimplification, in the
3119 : : diagnose_omp_blocks pass. */
3120 : 1079087 : for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
3121 : 1079087 : if (ctxp->case_labels.exists ())
3122 : : break;
3123 : :
3124 : 1079077 : tree label = CASE_LABEL (*expr_p);
3125 : 1079077 : label_stmt = gimple_build_label (label);
3126 : 1079077 : gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
3127 : 1079077 : ctxp->case_labels.safe_push (*expr_p);
3128 : 1079077 : gimplify_seq_add_stmt (pre_p, label_stmt);
3129 : :
3130 : 1079077 : if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
3131 : 16 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
3132 : : NOT_TAKEN));
3133 : 1079061 : else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
3134 : 20 : gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
3135 : : TAKEN));
3136 : :
3137 : 1079077 : return GS_ALL_DONE;
3138 : : }
3139 : :
3140 : : /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
3141 : : if necessary. */
3142 : :
3143 : : tree
3144 : 1784144 : build_and_jump (tree *label_p)
3145 : : {
3146 : 1784144 : if (label_p == NULL)
3147 : : /* If there's nowhere to jump, just fall through. */
3148 : : return NULL_TREE;
3149 : :
3150 : 1213131 : if (*label_p == NULL_TREE)
3151 : : {
3152 : 697488 : tree label = create_artificial_label (UNKNOWN_LOCATION);
3153 : 697488 : *label_p = label;
3154 : : }
3155 : :
3156 : 1213131 : return build1 (GOTO_EXPR, void_type_node, *label_p);
3157 : : }
3158 : :
3159 : : /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
3160 : : This also involves building a label to jump to and communicating it to
3161 : : gimplify_loop_expr through gimplify_ctxp->exit_label. */
3162 : :
3163 : : static enum gimplify_status
3164 : 3099 : gimplify_exit_expr (tree *expr_p)
3165 : : {
3166 : 3099 : tree cond = TREE_OPERAND (*expr_p, 0);
3167 : 3099 : tree expr;
3168 : :
3169 : 3099 : expr = build_and_jump (&gimplify_ctxp->exit_label);
3170 : 3099 : expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
3171 : 3099 : *expr_p = expr;
3172 : :
3173 : 3099 : return GS_OK;
3174 : : }
3175 : :
3176 : : /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
3177 : : different from its canonical type, wrap the whole thing inside a
3178 : : NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
3179 : : type.
3180 : :
3181 : : The canonical type of a COMPONENT_REF is the type of the field being
3182 : : referenced--unless the field is a bit-field which can be read directly
3183 : : in a smaller mode, in which case the canonical type is the
3184 : : sign-appropriate type corresponding to that mode. */
3185 : :
3186 : : static void
3187 : 18334698 : canonicalize_component_ref (tree *expr_p)
3188 : : {
3189 : 18334698 : tree expr = *expr_p;
3190 : 18334698 : tree type;
3191 : :
3192 : 18334698 : gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
3193 : :
3194 : 18334698 : if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
3195 : 7354766 : type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
3196 : : else
3197 : 10979932 : type = TREE_TYPE (TREE_OPERAND (expr, 1));
3198 : :
3199 : : /* One could argue that all the stuff below is not necessary for
3200 : : the non-bitfield case and declare it a FE error if type
3201 : : adjustment would be needed. */
3202 : 18334698 : if (TREE_TYPE (expr) != type)
3203 : : {
3204 : : #ifdef ENABLE_TYPES_CHECKING
3205 : 862603 : tree old_type = TREE_TYPE (expr);
3206 : : #endif
3207 : 862603 : int type_quals;
3208 : :
3209 : : /* We need to preserve qualifiers and propagate them from
3210 : : operand 0. */
3211 : 862603 : type_quals = TYPE_QUALS (type)
3212 : 862603 : | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
3213 : 862603 : if (TYPE_QUALS (type) != type_quals)
3214 : 862490 : type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
3215 : :
3216 : : /* Set the type of the COMPONENT_REF to the underlying type. */
3217 : 862603 : TREE_TYPE (expr) = type;
3218 : :
3219 : : #ifdef ENABLE_TYPES_CHECKING
3220 : : /* It is now a FE error, if the conversion from the canonical
3221 : : type to the original expression type is not useless. */
3222 : 862603 : gcc_assert (useless_type_conversion_p (old_type, type));
3223 : : #endif
3224 : : }
3225 : 18334698 : }
3226 : :
3227 : : /* If a NOP conversion is changing a pointer to array of foo to a pointer
3228 : : to foo, embed that change in the ADDR_EXPR by converting
3229 : : T array[U];
3230 : : (T *)&array
3231 : : ==>
3232 : : &array[L]
3233 : : where L is the lower bound. For simplicity, only do this for constant
3234 : : lower bound.
3235 : : The constraint is that the type of &array[L] is trivially convertible
3236 : : to T *. */
3237 : :
3238 : : static void
3239 : 520359 : canonicalize_addr_expr (tree *expr_p)
3240 : : {
3241 : 520359 : tree expr = *expr_p;
3242 : 520359 : tree addr_expr = TREE_OPERAND (expr, 0);
3243 : 520359 : tree datype, ddatype, pddatype;
3244 : :
3245 : : /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
3246 : 1040372 : if (!POINTER_TYPE_P (TREE_TYPE (expr))
3247 : 520359 : || TREE_CODE (addr_expr) != ADDR_EXPR)
3248 : : return;
3249 : :
3250 : : /* The addr_expr type should be a pointer to an array. */
3251 : 346 : datype = TREE_TYPE (TREE_TYPE (addr_expr));
3252 : 346 : if (TREE_CODE (datype) != ARRAY_TYPE)
3253 : : return;
3254 : :
3255 : : /* The pointer to element type shall be trivially convertible to
3256 : : the expression pointer type. */
3257 : 28 : ddatype = TREE_TYPE (datype);
3258 : 28 : pddatype = build_pointer_type (ddatype);
3259 : 28 : if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
3260 : : pddatype))
3261 : : return;
3262 : :
3263 : : /* The lower bound and element sizes must be constant. */
3264 : 0 : if (!TYPE_SIZE_UNIT (ddatype)
3265 : 0 : || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
3266 : 0 : || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
3267 : 0 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
3268 : : return;
3269 : :
3270 : : /* All checks succeeded. Build a new node to merge the cast. */
3271 : 0 : *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
3272 : 0 : TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
3273 : : NULL_TREE, NULL_TREE);
3274 : 0 : *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
3275 : :
3276 : : /* We can have stripped a required restrict qualifier above. */
3277 : 0 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
3278 : 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
3279 : : }
3280 : :
3281 : : /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
3282 : : underneath as appropriate. */
3283 : :
3284 : : static enum gimplify_status
3285 : 12550658 : gimplify_conversion (tree *expr_p)
3286 : : {
3287 : 12550658 : location_t loc = EXPR_LOCATION (*expr_p);
3288 : 12550658 : gcc_assert (CONVERT_EXPR_P (*expr_p));
3289 : :
3290 : : /* Then strip away all but the outermost conversion. */
3291 : 12550658 : STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
3292 : :
3293 : : /* And remove the outermost conversion if it's useless. */
3294 : 12550658 : if (tree_ssa_useless_type_conversion (*expr_p))
3295 : 0 : *expr_p = TREE_OPERAND (*expr_p, 0);
3296 : :
3297 : : /* If we still have a conversion at the toplevel,
3298 : : then canonicalize some constructs. */
3299 : 12550658 : if (CONVERT_EXPR_P (*expr_p))
3300 : : {
3301 : 12550658 : tree sub = TREE_OPERAND (*expr_p, 0);
3302 : :
3303 : : /* If a NOP conversion is changing the type of a COMPONENT_REF
3304 : : expression, then canonicalize its type now in order to expose more
3305 : : redundant conversions. */
3306 : 12550658 : if (TREE_CODE (sub) == COMPONENT_REF)
3307 : 267060 : canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
3308 : :
3309 : : /* If a NOP conversion is changing a pointer to array of foo
3310 : : to a pointer to foo, embed that change in the ADDR_EXPR. */
3311 : 12283598 : else if (TREE_CODE (sub) == ADDR_EXPR)
3312 : 520359 : canonicalize_addr_expr (expr_p);
3313 : : }
3314 : :
3315 : : /* If we have a conversion to a non-register type force the
3316 : : use of a VIEW_CONVERT_EXPR instead. */
3317 : 12550658 : if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
3318 : 90 : *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
3319 : 90 : TREE_OPERAND (*expr_p, 0));
3320 : :
3321 : : /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
3322 : 12550658 : if (TREE_CODE (*expr_p) == CONVERT_EXPR)
3323 : 235676 : TREE_SET_CODE (*expr_p, NOP_EXPR);
3324 : :
3325 : 12550658 : return GS_OK;
3326 : : }
3327 : :
3328 : : /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
3329 : : DECL_VALUE_EXPR, and it's worth re-examining things. */
3330 : :
3331 : : static enum gimplify_status
3332 : 126087814 : gimplify_var_or_parm_decl (tree *expr_p)
3333 : : {
3334 : 126087814 : tree decl = *expr_p;
3335 : :
3336 : : /* ??? If this is a local variable, and it has not been seen in any
3337 : : outer BIND_EXPR, then it's probably the result of a duplicate
3338 : : declaration, for which we've already issued an error. It would
3339 : : be really nice if the front end wouldn't leak these at all.
3340 : : Currently the only known culprit is C++ destructors, as seen
3341 : : in g++.old-deja/g++.jason/binding.C.
3342 : : Another possible culpit are size expressions for variably modified
3343 : : types which are lost in the FE or not gimplified correctly. */
3344 : 126087814 : if (VAR_P (decl)
3345 : 97694512 : && !DECL_SEEN_IN_BIND_EXPR_P (decl)
3346 : 16937117 : && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
3347 : 126141581 : && decl_function_context (decl) == current_function_decl)
3348 : : {
3349 : 0 : gcc_assert (seen_error ());
3350 : : return GS_ERROR;
3351 : : }
3352 : :
3353 : : /* When within an OMP context, notice uses of variables. */
3354 : 126087814 : if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
3355 : : return GS_ALL_DONE;
3356 : :
3357 : : /* If the decl is an alias for another expression, substitute it now. */
3358 : 126082039 : if (DECL_HAS_VALUE_EXPR_P (decl))
3359 : : {
3360 : 429670 : *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
3361 : 429670 : return GS_OK;
3362 : : }
3363 : :
3364 : : return GS_ALL_DONE;
3365 : : }
3366 : :
3367 : : /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
3368 : :
3369 : : static void
3370 : 109698548 : recalculate_side_effects (tree t)
3371 : : {
3372 : 109698548 : enum tree_code code = TREE_CODE (t);
3373 : 109698548 : int len = TREE_OPERAND_LENGTH (t);
3374 : 109698548 : int i;
3375 : :
3376 : 109698548 : switch (TREE_CODE_CLASS (code))
3377 : : {
3378 : 657215 : case tcc_expression:
3379 : 657215 : switch (code)
3380 : : {
3381 : : case INIT_EXPR:
3382 : : case MODIFY_EXPR:
3383 : : case VA_ARG_EXPR:
3384 : : case PREDECREMENT_EXPR:
3385 : : case PREINCREMENT_EXPR:
3386 : : case POSTDECREMENT_EXPR:
3387 : : case POSTINCREMENT_EXPR:
3388 : : /* All of these have side-effects, no matter what their
3389 : : operands are. */
3390 : : return;
3391 : :
3392 : : default:
3393 : : break;
3394 : : }
3395 : : /* Fall through. */
3396 : :
3397 : 109698546 : case tcc_comparison: /* a comparison expression */
3398 : 109698546 : case tcc_unary: /* a unary arithmetic expression */
3399 : 109698546 : case tcc_binary: /* a binary arithmetic expression */
3400 : 109698546 : case tcc_reference: /* a reference */
3401 : 109698546 : case tcc_vl_exp: /* a function call */
3402 : 109698546 : TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3403 : 356567866 : for (i = 0; i < len; ++i)
3404 : : {
3405 : 246869320 : tree op = TREE_OPERAND (t, i);
3406 : 246869320 : if (op && TREE_SIDE_EFFECTS (op))
3407 : 1272564 : TREE_SIDE_EFFECTS (t) = 1;
3408 : : }
3409 : : break;
3410 : :
3411 : : case tcc_constant:
3412 : : /* No side-effects. */
3413 : : return;
3414 : :
3415 : 2 : default:
3416 : 2 : if (code == SSA_NAME)
3417 : : /* No side-effects. */
3418 : : return;
3419 : 0 : gcc_unreachable ();
3420 : : }
3421 : : }
3422 : :
3423 : : /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
3424 : : node *EXPR_P.
3425 : :
3426 : : compound_lval
3427 : : : min_lval '[' val ']'
3428 : : | min_lval '.' ID
3429 : : | compound_lval '[' val ']'
3430 : : | compound_lval '.' ID
3431 : :
3432 : : This is not part of the original SIMPLE definition, which separates
3433 : : array and member references, but it seems reasonable to handle them
3434 : : together. Also, this way we don't run into problems with union
3435 : : aliasing; gcc requires that for accesses through a union to alias, the
3436 : : union reference must be explicit, which was not always the case when we
3437 : : were splitting up array and member refs.
3438 : :
3439 : : PRE_P points to the sequence where side effects that must happen before
3440 : : *EXPR_P should be stored.
3441 : :
3442 : : POST_P points to the sequence where side effects that must happen after
3443 : : *EXPR_P should be stored. */
3444 : :
3445 : : static enum gimplify_status
3446 : 29515091 : gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3447 : : fallback_t fallback)
3448 : : {
3449 : 29515091 : tree *p;
3450 : 29515091 : enum gimplify_status ret = GS_ALL_DONE, tret;
3451 : 29515091 : int i;
3452 : 29515091 : location_t loc = EXPR_LOCATION (*expr_p);
3453 : 29515091 : tree expr = *expr_p;
3454 : :
3455 : : /* Create a stack of the subexpressions so later we can walk them in
3456 : : order from inner to outer. */
3457 : 29515091 : auto_vec<tree, 10> expr_stack;
3458 : :
3459 : : /* We can handle anything that get_inner_reference can deal with. */
3460 : 71065852 : for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
3461 : : {
3462 : 41550761 : restart:
3463 : : /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
3464 : 71124598 : if (TREE_CODE (*p) == INDIRECT_REF)
3465 : 3929378 : *p = fold_indirect_ref_loc (loc, *p);
3466 : :
3467 : 71124598 : if (handled_component_p (*p))
3468 : : ;
3469 : : /* Expand DECL_VALUE_EXPR now. In some cases that may expose
3470 : : additional COMPONENT_REFs. */
3471 : 13147075 : else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
3472 : 30993627 : && gimplify_var_or_parm_decl (p) == GS_OK)
3473 : 58746 : goto restart;
3474 : : else
3475 : : break;
3476 : :
3477 : 41550761 : expr_stack.safe_push (*p);
3478 : : }
3479 : :
3480 : 29515091 : gcc_assert (expr_stack.length ());
3481 : :
3482 : : /* Now EXPR_STACK is a stack of pointers to all the refs we've
3483 : : walked through and P points to the innermost expression.
3484 : :
3485 : : Java requires that we elaborated nodes in source order. That
3486 : : means we must gimplify the inner expression followed by each of
3487 : : the indices, in order. But we can't gimplify the inner
3488 : : expression until we deal with any variable bounds, sizes, or
3489 : : positions in order to deal with PLACEHOLDER_EXPRs.
3490 : :
3491 : : The base expression may contain a statement expression that
3492 : : has declarations used in size expressions, so has to be
3493 : : gimplified before gimplifying the size expressions.
3494 : :
3495 : : So we do this in three steps. First we deal with variable
3496 : : bounds, sizes, and positions, then we gimplify the base and
3497 : : ensure it is memory if needed, then we deal with the annotations
3498 : : for any variables in the components and any indices, from left
3499 : : to right. */
3500 : :
3501 : 29515091 : bool need_non_reg = false;
3502 : 71065849 : for (i = expr_stack.length () - 1; i >= 0; i--)
3503 : : {
3504 : 41550761 : tree t = expr_stack[i];
3505 : :
3506 : 41550761 : if (error_operand_p (TREE_OPERAND (t, 0)))
3507 : : return GS_ERROR;
3508 : :
3509 : 41550758 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3510 : : {
3511 : : /* Deal with the low bound and element type size and put them into
3512 : : the ARRAY_REF. If these values are set, they have already been
3513 : : gimplified. */
3514 : 8593318 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3515 : : {
3516 : 8544489 : tree low = unshare_expr (array_ref_low_bound (t));
3517 : 8544489 : if (!is_gimple_min_invariant (low))
3518 : : {
3519 : 0 : TREE_OPERAND (t, 2) = low;
3520 : : }
3521 : : }
3522 : :
3523 : 8593318 : if (TREE_OPERAND (t, 3) == NULL_TREE)
3524 : : {
3525 : 8576243 : tree elmt_size = array_ref_element_size (t);
3526 : 8576243 : if (!is_gimple_min_invariant (elmt_size))
3527 : : {
3528 : 8420 : elmt_size = unshare_expr (elmt_size);
3529 : 8420 : tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
3530 : 8420 : tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
3531 : :
3532 : : /* Divide the element size by the alignment of the element
3533 : : type (above). */
3534 : 8420 : elmt_size = size_binop_loc (loc, EXACT_DIV_EXPR,
3535 : : elmt_size, factor);
3536 : :
3537 : 8420 : TREE_OPERAND (t, 3) = elmt_size;
3538 : : }
3539 : : }
3540 : : need_non_reg = true;
3541 : : }
3542 : 32957440 : else if (TREE_CODE (t) == COMPONENT_REF)
3543 : : {
3544 : : /* Set the field offset into T and gimplify it. */
3545 : 31683970 : if (TREE_OPERAND (t, 2) == NULL_TREE)
3546 : : {
3547 : 31683603 : tree offset = component_ref_field_offset (t);
3548 : 31683603 : if (!is_gimple_min_invariant (offset))
3549 : : {
3550 : 281 : offset = unshare_expr (offset);
3551 : 281 : tree field = TREE_OPERAND (t, 1);
3552 : 281 : tree factor
3553 : 281 : = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
3554 : :
3555 : : /* Divide the offset by its alignment. */
3556 : 281 : offset = size_binop_loc (loc, EXACT_DIV_EXPR,
3557 : : offset, factor);
3558 : :
3559 : 281 : TREE_OPERAND (t, 2) = offset;
3560 : : }
3561 : : }
3562 : : need_non_reg = true;
3563 : : }
3564 : 1273470 : else if (!is_gimple_reg_type (TREE_TYPE (t)))
3565 : : /* When the result of an operation, in particular a VIEW_CONVERT_EXPR
3566 : : is a non-register type then require the base object to be a
3567 : : non-register as well. */
3568 : 40548924 : need_non_reg = true;
3569 : : }
3570 : :
3571 : : /* Step 2 is to gimplify the base expression. Make sure lvalue is set
3572 : : so as to match the min_lval predicate. Failure to do so may result
3573 : : in the creation of large aggregate temporaries. */
3574 : 29515088 : tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
3575 : : fallback | fb_lvalue);
3576 : 29515088 : ret = MIN (ret, tret);
3577 : 29515088 : if (ret == GS_ERROR)
3578 : : return GS_ERROR;
3579 : :
3580 : : /* Step 2a: if we have component references we do not support on
3581 : : registers then make sure the base isn't a register. Of course
3582 : : we can only do so if an rvalue is OK. */
3583 : 29515088 : if (need_non_reg && (fallback & fb_rvalue))
3584 : 22072731 : prepare_gimple_addressable (p, pre_p);
3585 : :
3586 : :
3587 : : /* Step 3: gimplify size expressions and the indices and operands of
3588 : : ARRAY_REF. During this loop we also remove any useless conversions.
3589 : : If we operate on a register also make sure to properly gimplify
3590 : : to individual operations. */
3591 : :
3592 : 29515088 : bool reg_operations = is_gimple_reg (*p);
3593 : 71065846 : for (; expr_stack.length () > 0; )
3594 : : {
3595 : 41550758 : tree t = expr_stack.pop ();
3596 : :
3597 : 41550758 : if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3598 : : {
3599 : 8593318 : gcc_assert (!reg_operations);
3600 : :
3601 : : /* Gimplify the low bound and element type size. */
3602 : 8593318 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3603 : : is_gimple_reg, fb_rvalue);
3604 : 8593318 : ret = MIN (ret, tret);
3605 : :
3606 : 8593318 : tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
3607 : : is_gimple_reg, fb_rvalue);
3608 : 8593318 : ret = MIN (ret, tret);
3609 : :
3610 : : /* Gimplify the dimension. */
3611 : 8593318 : tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
3612 : : is_gimple_val, fb_rvalue);
3613 : 8593318 : ret = MIN (ret, tret);
3614 : : }
3615 : 32957440 : else if (TREE_CODE (t) == COMPONENT_REF)
3616 : : {
3617 : 31683970 : gcc_assert (!reg_operations);
3618 : :
3619 : 31683970 : tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
3620 : : is_gimple_reg, fb_rvalue);
3621 : 31683970 : ret = MIN (ret, tret);
3622 : : }
3623 : 1273470 : else if (reg_operations)
3624 : : {
3625 : 749268 : tret = gimplify_expr (&TREE_OPERAND (t, 0), pre_p, post_p,
3626 : : is_gimple_val, fb_rvalue);
3627 : 1273470 : ret = MIN (ret, tret);
3628 : : }
3629 : :
3630 : 41550758 : STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
3631 : :
3632 : : /* The innermost expression P may have originally had
3633 : : TREE_SIDE_EFFECTS set which would have caused all the outer
3634 : : expressions in *EXPR_P leading to P to also have had
3635 : : TREE_SIDE_EFFECTS set. */
3636 : 41550758 : recalculate_side_effects (t);
3637 : : }
3638 : :
3639 : : /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
3640 : 29515088 : if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3641 : : {
3642 : 18067638 : canonicalize_component_ref (expr_p);
3643 : : }
3644 : :
3645 : 29515088 : expr_stack.release ();
3646 : :
3647 : 29515088 : gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3648 : :
3649 : : return ret;
3650 : 29515091 : }
3651 : :
3652 : : /* Gimplify the self modifying expression pointed to by EXPR_P
3653 : : (++, --, +=, -=).
3654 : :
3655 : : PRE_P points to the list where side effects that must happen before
3656 : : *EXPR_P should be stored.
3657 : :
3658 : : POST_P points to the list where side effects that must happen after
3659 : : *EXPR_P should be stored.
3660 : :
3661 : : WANT_VALUE is nonzero iff we want to use the value of this expression
3662 : : in another expression.
3663 : :
3664 : : ARITH_TYPE is the type the computation should be performed in. */
3665 : :
3666 : : enum gimplify_status
3667 : 1136415 : gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3668 : : bool want_value, tree arith_type)
3669 : : {
3670 : 1136415 : enum tree_code code;
3671 : 1136415 : tree lhs, lvalue, rhs, t1;
3672 : 1136415 : gimple_seq post = NULL, *orig_post_p = post_p;
3673 : 1136415 : bool postfix;
3674 : 1136415 : enum tree_code arith_code;
3675 : 1136415 : enum gimplify_status ret;
3676 : 1136415 : location_t loc = EXPR_LOCATION (*expr_p);
3677 : :
3678 : 1136415 : code = TREE_CODE (*expr_p);
3679 : :
3680 : 1136415 : gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3681 : : || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3682 : :
3683 : : /* Prefix or postfix? */
3684 : 1136415 : if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3685 : : /* Faster to treat as prefix if result is not used. */
3686 : : postfix = want_value;
3687 : : else
3688 : : postfix = false;
3689 : :
3690 : : /* For postfix, make sure the inner expression's post side effects
3691 : : are executed after side effects from this expression. */
3692 : 396006 : if (postfix)
3693 : 1136415 : post_p = &post;
3694 : :
3695 : : /* Add or subtract? */
3696 : 1136415 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3697 : : arith_code = PLUS_EXPR;
3698 : : else
3699 : 48358 : arith_code = MINUS_EXPR;
3700 : :
3701 : : /* Gimplify the LHS into a GIMPLE lvalue. */
3702 : 1136415 : lvalue = TREE_OPERAND (*expr_p, 0);
3703 : 1136415 : ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3704 : 1136415 : if (ret == GS_ERROR)
3705 : : return ret;
3706 : :
3707 : : /* Extract the operands to the arithmetic operation. */
3708 : 1136414 : lhs = lvalue;
3709 : 1136414 : rhs = TREE_OPERAND (*expr_p, 1);
3710 : :
3711 : : /* For postfix operator, we evaluate the LHS to an rvalue and then use
3712 : : that as the result value and in the postqueue operation. */
3713 : 1136414 : if (postfix)
3714 : : {
3715 : 190653 : ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3716 : 190653 : if (ret == GS_ERROR)
3717 : : return ret;
3718 : :
3719 : 190653 : lhs = get_initialized_tmp_var (lhs, pre_p);
3720 : : }
3721 : :
3722 : : /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3723 : 1136414 : if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3724 : : {
3725 : 243200 : rhs = convert_to_ptrofftype_loc (loc, rhs);
3726 : 243200 : if (arith_code == MINUS_EXPR)
3727 : 10741 : rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3728 : 243200 : t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3729 : : }
3730 : : else
3731 : 893214 : t1 = fold_convert (TREE_TYPE (*expr_p),
3732 : : fold_build2 (arith_code, arith_type,
3733 : : fold_convert (arith_type, lhs),
3734 : : fold_convert (arith_type, rhs)));
3735 : :
3736 : 1136414 : if (postfix)
3737 : : {
3738 : 190653 : gimplify_assign (lvalue, t1, pre_p);
3739 : 190653 : gimplify_seq_add_seq (orig_post_p, post);
3740 : 190653 : *expr_p = lhs;
3741 : 190653 : return GS_ALL_DONE;
3742 : : }
3743 : : else
3744 : : {
3745 : 945761 : *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3746 : 945761 : return GS_OK;
3747 : : }
3748 : : }
3749 : :
3750 : : /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3751 : :
3752 : : static void
3753 : 80195837 : maybe_with_size_expr (tree *expr_p)
3754 : : {
3755 : 80195837 : tree expr = *expr_p;
3756 : 80195837 : tree type = TREE_TYPE (expr);
3757 : 80195837 : tree size;
3758 : :
3759 : : /* If we've already wrapped this or the type is error_mark_node, we can't do
3760 : : anything. */
3761 : 80195837 : if (TREE_CODE (expr) == WITH_SIZE_EXPR
3762 : 80195712 : || type == error_mark_node)
3763 : : return;
3764 : :
3765 : : /* If the size isn't known or is a constant, we have nothing to do. */
3766 : 80195681 : size = TYPE_SIZE_UNIT (type);
3767 : 80195681 : if (!size || poly_int_tree_p (size))
3768 : : return;
3769 : :
3770 : : /* Otherwise, make a WITH_SIZE_EXPR. */
3771 : 1494 : size = unshare_expr (size);
3772 : 1494 : size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3773 : 1494 : *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3774 : : }
3775 : :
3776 : : /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3777 : : Store any side-effects in PRE_P. CALL_LOCATION is the location of
3778 : : the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3779 : : gimplified to an SSA name. */
3780 : :
3781 : : enum gimplify_status
3782 : 33233511 : gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3783 : : bool allow_ssa)
3784 : : {
3785 : 33233511 : bool (*test) (tree);
3786 : 33233511 : fallback_t fb;
3787 : :
3788 : : /* In general, we allow lvalues for function arguments to avoid
3789 : : extra overhead of copying large aggregates out of even larger
3790 : : aggregates into temporaries only to copy the temporaries to
3791 : : the argument list. Make optimizers happy by pulling out to
3792 : : temporaries those types that fit in registers. */
3793 : 33233511 : if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3794 : : test = is_gimple_val, fb = fb_rvalue;
3795 : : else
3796 : : {
3797 : 1626550 : test = is_gimple_lvalue, fb = fb_either;
3798 : : /* Also strip a TARGET_EXPR that would force an extra copy. */
3799 : 1626550 : if (TREE_CODE (*arg_p) == TARGET_EXPR)
3800 : : {
3801 : 393926 : tree init = TARGET_EXPR_INITIAL (*arg_p);
3802 : 393926 : if (init
3803 : 393923 : && !VOID_TYPE_P (TREE_TYPE (init))
3804 : : /* Currently, due to c++/116015, it is not desirable to
3805 : : strip a TARGET_EXPR whose initializer is a {}. The
3806 : : problem is that if we do elide it, we also have to
3807 : : replace all the occurrences of the slot temporary in the
3808 : : initializer with the temporary created for the argument.
3809 : : But we do not have that temporary yet so the replacement
3810 : : would be quite awkward and it might be needed to resort
3811 : : back to a PLACEHOLDER_EXPR. Note that stripping the
3812 : : TARGET_EXPR wouldn't help anyway, as gimplify_expr would
3813 : : just allocate a temporary to store the CONSTRUCTOR into.
3814 : : (FIXME PR116375.)
3815 : :
3816 : : See convert_for_arg_passing for the C++ code that marks
3817 : : the TARGET_EXPR as eliding or not. */
3818 : 741532 : && TREE_CODE (init) != CONSTRUCTOR)
3819 : 312914 : *arg_p = init;
3820 : : }
3821 : : }
3822 : :
3823 : : /* If this is a variable sized type, we must remember the size. */
3824 : 33233511 : maybe_with_size_expr (arg_p);
3825 : :
3826 : : /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3827 : : /* Make sure arguments have the same location as the function call
3828 : : itself. */
3829 : 33233511 : protected_set_expr_location (*arg_p, call_location);
3830 : :
3831 : : /* There is a sequence point before a function call. Side effects in
3832 : : the argument list must occur before the actual call. So, when
3833 : : gimplifying arguments, force gimplify_expr to use an internal
3834 : : post queue which is then appended to the end of PRE_P. */
3835 : 33233511 : return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3836 : : }
3837 : :
3838 : : /* Don't fold inside offloading or taskreg regions: it can break code by
3839 : : adding decl references that weren't in the source. We'll do it during
3840 : : omplower pass instead. */
3841 : :
3842 : : static bool
3843 : 58377700 : maybe_fold_stmt (gimple_stmt_iterator *gsi)
3844 : : {
3845 : 58377700 : struct gimplify_omp_ctx *ctx;
3846 : 58903357 : for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3847 : 1175562 : if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3848 : : return false;
3849 : 530534 : else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3850 : : return false;
3851 : : /* Delay folding of builtins until the IL is in consistent state
3852 : : so the diagnostic machinery can do a better job. */
3853 : 57727795 : if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3854 : : return false;
3855 : 55393654 : return fold_stmt (gsi);
3856 : : }
3857 : :
3858 : : static tree
3859 : : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
3860 : : tree construct_context);
3861 : :
3862 : :
3863 : : /* Helper function for calls to omp_dynamic_cond: find the current
3864 : : enclosing block in the gimplification context. */
3865 : : static tree
3866 : 331 : find_supercontext (void)
3867 : : {
3868 : 331 : vec<gbind *>stack = gimple_bind_expr_stack ();
3869 : 633 : for (int i = stack.length () - 1; i >= 0; i++)
3870 : : {
3871 : 302 : gbind *b = stack[i];
3872 : 302 : if (b->block)
3873 : : return b->block;
3874 : : }
3875 : : return NULL_TREE;
3876 : : }
3877 : :
3878 : : /* OpenMP: Handle the append_args and adjust_args clauses of
3879 : : declare_variant for EXPR, which is a CALL_EXPR whose CALL_EXPR_FN
3880 : : is the variant, within a dispatch construct with clauses DISPATCH_CLAUSES.
3881 : : WANT_VALUE and POINTERIZE are as for expand_variant_call_expr.
3882 : :
3883 : : 'append_args' causes interop objects are added after the last regular
3884 : : (nonhidden, nonvariadic) arguments of the variant function.
3885 : : 'adjust_args' with need_device_{addr,ptr} converts the pointer target of
3886 : : a pointer from a host to a device address. This uses either the default
3887 : : device or the passed device number, which then sets the default device
3888 : : address. */
3889 : : static tree
3890 : 471 : modify_call_for_omp_dispatch (tree expr, tree dispatch_clauses,
3891 : : bool want_value, bool pointerize)
3892 : : {
3893 : 471 : location_t loc = EXPR_LOCATION (expr);
3894 : 471 : tree fndecl = get_callee_fndecl (expr);
3895 : :
3896 : : /* Skip processing if we don't get the expected call form. */
3897 : 471 : if (!fndecl)
3898 : : return expr;
3899 : :
3900 : 471 : tree init_code = NULL_TREE;
3901 : 471 : tree cleanup = NULL_TREE;
3902 : 471 : tree clobbers = NULL_TREE;
3903 : 471 : int nargs = call_expr_nargs (expr);
3904 : 471 : tree dispatch_device_num = NULL_TREE;
3905 : 471 : tree dispatch_interop = NULL_TREE;
3906 : 471 : tree dispatch_append_args = NULL_TREE;
3907 : 471 : int nfirst_args = 0;
3908 : 471 : tree dispatch_adjust_args_list
3909 : 471 : = lookup_attribute ("omp declare variant variant args",
3910 : 471 : DECL_ATTRIBUTES (fndecl));
3911 : :
3912 : 471 : if (dispatch_adjust_args_list)
3913 : : {
3914 : 389 : dispatch_adjust_args_list = TREE_VALUE (dispatch_adjust_args_list);
3915 : 389 : dispatch_append_args = TREE_CHAIN (dispatch_adjust_args_list);
3916 : 389 : if (TREE_PURPOSE (dispatch_adjust_args_list) == NULL_TREE
3917 : 389 : && TREE_VALUE (dispatch_adjust_args_list) == NULL_TREE)
3918 : : dispatch_adjust_args_list = NULL_TREE;
3919 : : }
3920 : 389 : if (dispatch_append_args)
3921 : : {
3922 : 245 : nfirst_args = tree_to_shwi (TREE_PURPOSE (dispatch_append_args));
3923 : 245 : dispatch_append_args = TREE_VALUE (dispatch_append_args);
3924 : : }
3925 : 471 : dispatch_device_num = omp_find_clause (dispatch_clauses, OMP_CLAUSE_DEVICE);
3926 : 471 : if (dispatch_device_num)
3927 : 287 : dispatch_device_num = OMP_CLAUSE_DEVICE_ID (dispatch_device_num);
3928 : 471 : dispatch_interop = omp_find_clause (dispatch_clauses, OMP_CLAUSE_INTEROP);
3929 : 471 : int nappend = 0, ninterop = 0;
3930 : 880 : for (tree t = dispatch_append_args; t; t = TREE_CHAIN (t))
3931 : 409 : nappend++;
3932 : :
3933 : : /* FIXME: error checking should be taken out of this function and
3934 : : handled before any attempt at filtering or resolution happens.
3935 : : Otherwise whether or not diagnostics appear is determined by
3936 : : GCC internals, how good the front ends are at constant-folding,
3937 : : the split between early/late resolution, etc instead of the code
3938 : : as written by the user. */
3939 : 471 : if (dispatch_interop)
3940 : 861 : for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
3941 : 605 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3942 : 409 : ninterop++;
3943 : 471 : if (dispatch_interop && !dispatch_device_num)
3944 : : {
3945 : 7 : gcc_checking_assert (ninterop > 1);
3946 : 7 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3947 : : "the %<device%> clause must be present if the %<interop%> "
3948 : : "clause has more than one list item");
3949 : : }
3950 : 471 : if (nappend < ninterop)
3951 : : {
3952 : 48 : error_at (OMP_CLAUSE_LOCATION (dispatch_interop),
3953 : : "number of list items in %<interop%> clause (%d) "
3954 : : "exceeds the number of %<append_args%> items (%d) for "
3955 : : "%<declare variant%> candidate %qD", ninterop, nappend, fndecl);
3956 : 88 : inform (dispatch_append_args
3957 : 8 : ? EXPR_LOCATION (TREE_PURPOSE (dispatch_append_args))
3958 : 40 : : DECL_SOURCE_LOCATION (fndecl),
3959 : : "%<declare variant%> candidate %qD declared here", fndecl);
3960 : 48 : ninterop = nappend;
3961 : : }
3962 : 471 : if (dispatch_append_args)
3963 : : {
3964 : 245 : tree *buffer = XALLOCAVEC (tree, nargs + nappend);
3965 : 245 : tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3966 : : /* Copy the first arguments; insert then the interop objects,
3967 : : and then copy the rest (nargs - nfirst_args) args. */
3968 : 245 : int i;
3969 : 679 : for (i = 0; i < nfirst_args; i++)
3970 : : {
3971 : 434 : arg = TREE_CHAIN (arg);
3972 : 434 : buffer[i] = CALL_EXPR_ARG (expr, i);
3973 : : }
3974 : : int j = ninterop;
3975 : 583 : for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
3976 : 338 : if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
3977 : 338 : buffer[i + --j] = OMP_CLAUSE_DECL (t);
3978 : 245 : gcc_checking_assert (j == 0);
3979 : :
3980 : : /* Do we need to create additional interop objects? */
3981 : 245 : if (ninterop < nappend)
3982 : : {
3983 : 56 : if (dispatch_device_num == NULL_TREE)
3984 : : /* Not remapping device number. */
3985 : 22 : dispatch_device_num = build_int_cst (integer_type_node,
3986 : : GOMP_DEVICE_DEFAULT_OMP_61);
3987 : 56 : int nnew = nappend - ninterop;
3988 : 56 : tree nobjs = build_int_cst (integer_type_node, nnew);
3989 : 56 : tree a, t;
3990 : :
3991 : : /* Skip to the append_args clause for the first constructed
3992 : : interop argument. */
3993 : 56 : tree apparg = dispatch_append_args;
3994 : 83 : for (j = 0; j < ninterop; j++)
3995 : 27 : apparg = TREE_CHAIN (apparg);
3996 : :
3997 : : /* omp_interop_t *objs[n]; */
3998 : 56 : tree objtype = build_pointer_type (pointer_sized_int_node);
3999 : 56 : t = build_array_type_nelts (objtype, nnew);
4000 : 56 : tree objs = create_tmp_var (t, "interopobjs");
4001 : :
4002 : : /* int target_tgtsync[n]; */
4003 : 56 : t = build_array_type_nelts (integer_type_node, nnew);
4004 : 56 : tree target_tgtsync = create_tmp_var (t, "tgt_tgtsync");
4005 : :
4006 : : /* Scan first to determine if we need a prefer_type array. */
4007 : 56 : tree prefer_type = NULL_TREE;
4008 : 56 : tree prefer_type_type = NULL_TREE;
4009 : 141 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4010 : 64 : if (TREE_VALUE (a) != NULL_TREE)
4011 : : {
4012 : : /* const char *prefer_type[n]; */
4013 : 35 : t = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4014 : 35 : prefer_type_type = build_pointer_type (t);
4015 : 35 : t = build_array_type_nelts (prefer_type_type, nnew);
4016 : 35 : prefer_type = create_tmp_var (t, "pref_type");
4017 : 35 : break;
4018 : : }
4019 : :
4020 : : /* Initialize the arrays, generating temp vars and clobbers for
4021 : : the interobject objects. (The constructed array holding the
4022 : : pointers to these objects shouldn't need clobbering as there's
4023 : : no reason for GOMP_interop to modify its contents.) */
4024 : 127 : for (j = ninterop, a = apparg; j < nappend; j++, a = TREE_CHAIN (a))
4025 : : {
4026 : : /* The allocated temporaries for the interop objects
4027 : : have type omp_interop_t, which is an integer type that
4028 : : can encode a pointer. */
4029 : 71 : tree objvar = create_tmp_var (pointer_sized_int_node, "interop");
4030 : 71 : buffer[i + j] = objvar;
4031 : 71 : TREE_ADDRESSABLE (objvar) = 1;
4032 : : /* Generate a clobber for the temporary for when we're done
4033 : : with it. */
4034 : 71 : tree c = build_clobber (pointer_sized_int_node,
4035 : : CLOBBER_OBJECT_END);
4036 : 71 : c = build2 (MODIFY_EXPR, pointer_sized_int_node, objvar, c);
4037 : 71 : if (clobbers)
4038 : 15 : clobbers = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4039 : : c, clobbers);
4040 : : else
4041 : : clobbers = c;
4042 : :
4043 : : /* objs[offset] = &objvar; */
4044 : 71 : tree offset = build_int_cst (integer_type_node, j - ninterop);
4045 : 71 : tree init = build4 (ARRAY_REF, objtype, objs, offset,
4046 : : NULL_TREE, NULL_TREE);
4047 : 71 : init = build2 (MODIFY_EXPR, objtype, init,
4048 : : build_fold_addr_expr (objvar));
4049 : 71 : if (init_code)
4050 : 15 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4051 : : init_code, init);
4052 : : else
4053 : : init_code = init;
4054 : :
4055 : : /* target_tgtsync[offset] = tgt;
4056 : : (Don't blame me, I didn't design the encoding of this
4057 : : info into the dispatch interop clause data structure,
4058 : : but the runtime wants a bit mask.) */
4059 : 71 : tree tree_tgt = TREE_OPERAND (TREE_PURPOSE (a), 0);
4060 : 71 : int tgt = 0;
4061 : 71 : if (TREE_PURPOSE (tree_tgt) == boolean_true_node)
4062 : 42 : tgt |= GOMP_INTEROP_TARGET;
4063 : 71 : if (TREE_VALUE (tree_tgt) == boolean_true_node)
4064 : 36 : tgt |= GOMP_INTEROP_TARGETSYNC;
4065 : 71 : init = build4 (ARRAY_REF, integer_type_node,
4066 : : target_tgtsync, offset, NULL_TREE, NULL_TREE);
4067 : 71 : init = build2 (MODIFY_EXPR, integer_type_node, init,
4068 : 71 : build_int_cst (integer_type_node, tgt));
4069 : 71 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4070 : : init_code, init);
4071 : :
4072 : 71 : if (prefer_type)
4073 : : {
4074 : 45 : tree pref = TREE_VALUE (a);
4075 : 45 : if (pref == NULL_TREE)
4076 : 6 : pref = null_pointer_node;
4077 : : else
4078 : 39 : pref = build_fold_addr_expr (pref);
4079 : 45 : init = build4 (ARRAY_REF, prefer_type_type, prefer_type,
4080 : : offset, NULL_TREE, NULL_TREE);
4081 : 45 : init = build2 (MODIFY_EXPR, prefer_type_type, init,
4082 : : pref);
4083 : 45 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4084 : : init_code, init);
4085 : : }
4086 : : }
4087 : :
4088 : 56 : objs = build_fold_addr_expr (objs);
4089 : 56 : target_tgtsync = build_fold_addr_expr (target_tgtsync);
4090 : 56 : prefer_type = prefer_type ? build_fold_addr_expr (prefer_type)
4091 : : : null_pointer_node;
4092 : 56 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_INTEROP);
4093 : 56 : tree create
4094 : 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4095 : : nobjs, objs, target_tgtsync, prefer_type,
4096 : : integer_zero_node, null_pointer_node,
4097 : : integer_zero_node, null_pointer_node,
4098 : : integer_zero_node, null_pointer_node);
4099 : 56 : if (init_code)
4100 : 56 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (create),
4101 : : init_code, create);
4102 : : else
4103 : : init_code = create;
4104 : :
4105 : 56 : cleanup
4106 : 56 : = build_call_expr_loc (loc, fn, 11, dispatch_device_num,
4107 : : integer_zero_node, null_pointer_node,
4108 : : null_pointer_node, null_pointer_node,
4109 : : integer_zero_node, null_pointer_node,
4110 : : nobjs, objs,
4111 : : integer_zero_node, null_pointer_node);
4112 : 56 : if (clobbers)
4113 : 56 : cleanup = build2 (COMPOUND_EXPR, TREE_TYPE (clobbers),
4114 : : cleanup, clobbers);
4115 : : }
4116 : :
4117 : 654 : for (j = 0; j < nappend; j++)
4118 : : {
4119 : : /* Fortran permits by-reference or by-value for the dummy arg
4120 : : and by-value, by-reference, ptr by-reference as actual
4121 : : argument. Handle this. */
4122 : 409 : tree obj = buffer[i + j]; // interop object
4123 : 409 : tree a2 = TREE_VALUE (arg); // parameter type
4124 : 766 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4125 : 436 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (obj))))
4126 : : {
4127 : 32 : tree t = TREE_TYPE (TREE_TYPE (obj));
4128 : 32 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (t)));
4129 : 32 : obj = fold_build1 (INDIRECT_REF, t, obj);
4130 : : }
4131 : 750 : if (POINTER_TYPE_P (TREE_TYPE (obj))
4132 : 420 : && INTEGRAL_TYPE_P (a2))
4133 : : {
4134 : 43 : tree t = TREE_TYPE (TREE_TYPE (obj));
4135 : 43 : gcc_checking_assert (INTEGRAL_TYPE_P (t));
4136 : 43 : obj = fold_build1 (INDIRECT_REF, t, obj);
4137 : : }
4138 : 497 : else if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
4139 : 461 : && POINTER_TYPE_P (a2))
4140 : : {
4141 : 18 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4142 : 18 : obj = build_fold_addr_expr (obj);
4143 : : }
4144 : 348 : else if (!INTEGRAL_TYPE_P (a2)
4145 : 348 : || !INTEGRAL_TYPE_P (TREE_TYPE (obj)))
4146 : : {
4147 : 36 : tree t = TREE_TYPE (obj);
4148 : 36 : gcc_checking_assert (POINTER_TYPE_P (t)
4149 : : && POINTER_TYPE_P (a2)
4150 : : && INTEGRAL_TYPE_P (TREE_TYPE (t))
4151 : : && INTEGRAL_TYPE_P (TREE_TYPE (a2)));
4152 : : }
4153 : 409 : buffer[i + j] = obj;
4154 : 409 : arg = TREE_CHAIN (arg);
4155 : : }
4156 : 245 : i += nappend;
4157 : 534 : for (j = nfirst_args; j < nargs; j++)
4158 : 289 : buffer[i++] = CALL_EXPR_ARG (expr, j);
4159 : 245 : nargs += nappend;
4160 : 245 : tree call = expr;
4161 : 490 : expr = build_call_array_loc (EXPR_LOCATION (expr), TREE_TYPE (call),
4162 : 245 : CALL_EXPR_FN (call), nargs, buffer);
4163 : :
4164 : : /* Copy all CALL_EXPR flags. */
4165 : 245 : CALL_EXPR_STATIC_CHAIN (expr) = CALL_EXPR_STATIC_CHAIN (call);
4166 : 245 : CALL_EXPR_TAILCALL (expr) = CALL_EXPR_TAILCALL (call);
4167 : 245 : CALL_EXPR_RETURN_SLOT_OPT (expr)
4168 : 245 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4169 : 245 : CALL_FROM_THUNK_P (expr) = CALL_FROM_THUNK_P (call);
4170 : 245 : SET_EXPR_LOCATION (expr, EXPR_LOCATION (call));
4171 : 245 : CALL_EXPR_VA_ARG_PACK (expr) = CALL_EXPR_VA_ARG_PACK (call);
4172 : : }
4173 : :
4174 : : /* Nothing to do for adjust_args? */
4175 : 471 : if (!dispatch_adjust_args_list || !TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
4176 : 260 : goto add_cleanup;
4177 : :
4178 : : /* Handle adjust_args. */
4179 : 966 : for (int i = 0; i < nargs; i++)
4180 : : {
4181 : 755 : tree *arg_p = &CALL_EXPR_ARG (expr, i);
4182 : :
4183 : : /* Nothing to do if arg is constant null pointer. */
4184 : 755 : if (integer_zerop (*arg_p))
4185 : 6 : continue;
4186 : :
4187 : : bool need_device_ptr = false;
4188 : : bool need_device_addr = false;
4189 : 2247 : for (int need_addr = 0; need_addr <= 1; need_addr++)
4190 : 2301 : for (tree arg = (need_addr
4191 : 1498 : ? TREE_VALUE (dispatch_adjust_args_list)
4192 : 1498 : : TREE_PURPOSE (dispatch_adjust_args_list));
4193 : 2301 : arg != NULL; arg = TREE_CHAIN (arg))
4194 : : {
4195 : 1152 : if (TREE_VALUE (arg)
4196 : 1152 : && TREE_CODE (TREE_VALUE (arg)) == INTEGER_CST
4197 : 2304 : && wi::eq_p (i, wi::to_wide (TREE_VALUE (arg))))
4198 : : {
4199 : 349 : if (need_addr)
4200 : : need_device_addr = true;
4201 : : else
4202 : 349 : need_device_ptr = true;
4203 : : break;
4204 : : }
4205 : : }
4206 : :
4207 : 749 : if (need_device_ptr || need_device_addr)
4208 : : {
4209 : 740 : bool is_device_ptr = false;
4210 : 740 : bool has_device_addr = false;
4211 : :
4212 : 740 : for (tree c = dispatch_clauses; c; c = TREE_CHAIN (c))
4213 : : {
4214 : 454 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR
4215 : 454 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
4216 : : {
4217 : 164 : tree decl1 = DECL_NAME (OMP_CLAUSE_DECL (c));
4218 : 164 : tree decl2 = tree_strip_nop_conversions (*arg_p);
4219 : 164 : if (TREE_CODE (decl2) == ADDR_EXPR)
4220 : 19 : decl2 = TREE_OPERAND (decl2, 0);
4221 : 164 : if (VAR_P (decl2) || TREE_CODE (decl2) == PARM_DECL)
4222 : : {
4223 : 140 : decl2 = DECL_NAME (decl2);
4224 : 140 : if (decl1 == decl2
4225 : 203 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
4226 : : {
4227 : 58 : if (need_device_addr)
4228 : 0 : warning_at (OMP_CLAUSE_LOCATION (c),
4229 : 0 : OPT_Wopenmp,
4230 : : "%<is_device_ptr%> for %qD does"
4231 : : " not imply %<has_device_addr%> "
4232 : : "required for %<need_device_addr%>",
4233 : 0 : OMP_CLAUSE_DECL (c));
4234 : : is_device_ptr = true;
4235 : : break;
4236 : : }
4237 : 82 : else if (decl1 == decl2)
4238 : : {
4239 : 5 : if (need_device_ptr)
4240 : 10 : warning_at (OMP_CLAUSE_LOCATION (c),
4241 : 5 : OPT_Wopenmp,
4242 : : "%<has_device_addr%> for %qD does"
4243 : : " not imply %<is_device_ptr%> "
4244 : : "required for %<need_device_ptr%>",
4245 : 5 : OMP_CLAUSE_DECL (c));
4246 : : has_device_addr = true;
4247 : : break;
4248 : : }
4249 : : }
4250 : : }
4251 : : }
4252 : :
4253 : 349 : if ((need_device_ptr && !is_device_ptr)
4254 : 58 : || (need_device_addr && !has_device_addr))
4255 : : {
4256 : 291 : if (dispatch_device_num == NULL_TREE)
4257 : : {
4258 : : // device_num = omp_get_default_device ()
4259 : 105 : tree fn
4260 : 105 : = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
4261 : 105 : tree call = build_call_expr (fn, 0);
4262 : 105 : dispatch_device_num = create_tmp_var_raw (TREE_TYPE (call));
4263 : 105 : tree init
4264 : 105 : = build4 (TARGET_EXPR, TREE_TYPE (call),
4265 : : dispatch_device_num, call, NULL_TREE, NULL_TREE);
4266 : 105 : if (init_code)
4267 : 0 : init_code = build2 (COMPOUND_EXPR, TREE_TYPE (init),
4268 : : init_code, init);
4269 : : else
4270 : : init_code = init;
4271 : : }
4272 : :
4273 : : // We want to emit the following statement:
4274 : : // mapped_arg = omp_get_mapped_ptr (arg,
4275 : : // device_num)
4276 : : // but arg has to be the actual pointer, not a
4277 : : // reference or a conversion expression.
4278 : 291 : tree actual_ptr
4279 : 291 : = ((TREE_CODE (*arg_p) == ADDR_EXPR)
4280 : 291 : ? TREE_OPERAND (*arg_p, 0)
4281 : 38 : : *arg_p);
4282 : 291 : if (TREE_CODE (actual_ptr) == NOP_EXPR
4283 : 291 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (actual_ptr, 0)))
4284 : : == REFERENCE_TYPE))
4285 : : {
4286 : 6 : actual_ptr = TREE_OPERAND (actual_ptr, 0);
4287 : 6 : actual_ptr = build1 (INDIRECT_REF,
4288 : 6 : TREE_TYPE (actual_ptr),
4289 : : actual_ptr);
4290 : : }
4291 : 291 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_MAPPED_PTR);
4292 : 291 : tree mapped_arg = build_call_expr_loc (loc, fn, 2, actual_ptr,
4293 : : dispatch_device_num);
4294 : :
4295 : 291 : if (TREE_CODE (*arg_p) == ADDR_EXPR
4296 : 291 : || (TREE_CODE (TREE_TYPE (actual_ptr)) == REFERENCE_TYPE))
4297 : 47 : mapped_arg = build_fold_addr_expr (mapped_arg);
4298 : 244 : else if (TREE_CODE (*arg_p) == NOP_EXPR)
4299 : 35 : mapped_arg = build1 (NOP_EXPR, TREE_TYPE (*arg_p),
4300 : : mapped_arg);
4301 : 291 : *arg_p = mapped_arg;
4302 : : }
4303 : : }
4304 : : }
4305 : :
4306 : 471 : add_cleanup:
4307 : 471 : if (cleanup)
4308 : : {
4309 : 56 : tree result = NULL_TREE;
4310 : 56 : if (want_value && pointerize)
4311 : : {
4312 : 0 : tree tmp = create_tmp_var (build_pointer_type (TREE_TYPE (expr)),
4313 : : "cleanuptmp");
4314 : 0 : result = build_simple_mem_ref (tmp);
4315 : 0 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp,
4316 : : build_fold_addr_expr (expr));
4317 : : }
4318 : 56 : else if (want_value)
4319 : : {
4320 : 18 : tree tmp = create_tmp_var (TREE_TYPE (expr), "cleanuptmp");
4321 : 18 : result = tmp;
4322 : 18 : expr = build2 (INIT_EXPR, TREE_TYPE (tmp), tmp, expr);
4323 : : }
4324 : 56 : if (init_code)
4325 : 56 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4326 : 56 : expr = build2 (TRY_FINALLY_EXPR, void_type_node, expr, cleanup);
4327 : :
4328 : 56 : if (result)
4329 : 18 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (result), expr, result);
4330 : : }
4331 : 415 : else if (init_code)
4332 : 105 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init_code, expr);
4333 : :
4334 : : return expr;
4335 : : }
4336 : :
4337 : : /* Helper function for gimplify_call_expr: handle "declare variant"
4338 : : resolution and expansion of the CALL_EXPR EXPR. WANT_VALUE is true
4339 : : if the result value of the call is needed; POINTERIZE is true if it
4340 : : also needs to be pointerized. If OMP_DISPATCH_P is true, apply
4341 : : associated transformations using DISPATCH_CLAUSES.
4342 : : This function may return either the original call or some other
4343 : : expression such as a conditional to select one of multiple calls.
4344 : :
4345 : : FIXME: this function is written to be independent of gimplifier internals
4346 : : so that it could be moved to omp-general.cc and invoked from the
4347 : : front ends instead, per PR115076. */
4348 : :
4349 : : static tree
4350 : 1086 : expand_variant_call_expr (tree expr, bool want_value, bool pointerize,
4351 : : bool omp_dispatch_p, tree dispatch_clauses)
4352 : : {
4353 : : /* If we've already processed this call, stop now. This can happen
4354 : : if the variant call resolves to the original function, or to
4355 : : a dynamic conditional that includes the default call to the original
4356 : : function. */
4357 : 1086 : gcc_assert (omp_resolved_variant_calls != NULL);
4358 : 1086 : if (omp_resolved_variant_calls->contains (expr))
4359 : 96 : return expr;
4360 : :
4361 : 990 : tree fndecl = get_callee_fndecl (expr);
4362 : 990 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (expr));
4363 : 990 : location_t loc = EXPR_LOCATION (expr);
4364 : 990 : tree construct_context = omp_get_construct_context ();
4365 : 990 : vec<struct omp_variant> all_candidates
4366 : 990 : = omp_declare_variant_candidates (fndecl, construct_context);
4367 : 990 : gcc_assert (!all_candidates.is_empty ());
4368 : 990 : vec<struct omp_variant> candidates
4369 : 990 : = omp_get_dynamic_candidates (all_candidates, construct_context);
4370 : :
4371 : : /* If the variant call could be resolved now, build a nest of COND_EXPRs
4372 : : if there are dynamic candidates, and/or a new CALL_EXPR for each
4373 : : candidate call. */
4374 : 990 : if (!candidates.is_empty ())
4375 : : {
4376 : 978 : int n = candidates.length ();
4377 : 978 : tree tail = NULL_TREE;
4378 : :
4379 : 1988 : for (int i = n - 1; i >= 0; i--)
4380 : : {
4381 : 1010 : if (tail)
4382 : 32 : gcc_assert (candidates[i].dynamic_selector);
4383 : : else
4384 : 978 : gcc_assert (!candidates[i].dynamic_selector);
4385 : 1010 : if (candidates[i].alternative == fndecl)
4386 : : {
4387 : : /* We should only get the original function back as the
4388 : : default. */
4389 : 197 : gcc_assert (!tail);
4390 : 197 : omp_resolved_variant_calls->add (expr);
4391 : 197 : tail = expr;
4392 : : }
4393 : : else
4394 : : {
4395 : : /* For the final static selector, we can re-use the old
4396 : : CALL_EXPR and just replace the function, unless it may
4397 : : need dispatch argument modification. Otherwise,
4398 : : make a copy of it. */
4399 : 813 : tree thiscall = (tail || omp_dispatch_p
4400 : 813 : ? unshare_expr (expr) : expr);
4401 : 813 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype,
4402 : 813 : candidates[i].alternative);
4403 : 813 : if (omp_dispatch_p)
4404 : 471 : thiscall = modify_call_for_omp_dispatch (thiscall,
4405 : : dispatch_clauses,
4406 : : want_value,
4407 : : pointerize);
4408 : 813 : if (!tail)
4409 : : tail = thiscall;
4410 : : else
4411 : 64 : tail = build3 (COND_EXPR, TREE_TYPE (expr),
4412 : 32 : omp_dynamic_cond (candidates[i].selector,
4413 : : find_supercontext ()),
4414 : : thiscall, tail);
4415 : : }
4416 : : }
4417 : : return tail;
4418 : : }
4419 : :
4420 : : /* If we couldn't resolve the variant call now, expand it into a loop using
4421 : : a switch and OMP_NEXT_VARIANT for dispatch. The ompdevlow pass will
4422 : : handle OMP_NEXT_VARIANT expansion. */
4423 : : else
4424 : : {
4425 : : /* If we need a usable return value, we need a temporary
4426 : : and an assignment in each alternative. This logic was borrowed
4427 : : from gimplify_cond_expr. */
4428 : 12 : tree type = TREE_TYPE (expr);
4429 : 12 : tree tmp = NULL_TREE, result = NULL_TREE;
4430 : :
4431 : 12 : if (want_value)
4432 : : {
4433 : 12 : if (pointerize)
4434 : : {
4435 : 0 : type = build_pointer_type (type);
4436 : 0 : tmp = create_tmp_var (type, "iftmp");
4437 : 0 : result = build_simple_mem_ref_loc (loc, tmp);
4438 : : }
4439 : : else
4440 : : {
4441 : 12 : tmp = create_tmp_var (type, "iftmp");
4442 : 12 : result = tmp;
4443 : : }
4444 : : }
4445 : :
4446 : : /* Preprocess the all_candidates array so that the alternative field of
4447 : : each element holds the actual function call expression and possible
4448 : : assignment, instead of just the decl for the variant function. */
4449 : 60 : for (unsigned int i = 0; i < all_candidates.length (); i++)
4450 : : {
4451 : 48 : tree decl = all_candidates[i].alternative;
4452 : 48 : tree thiscall;
4453 : :
4454 : : /* We need to turn the decl from the candidate into a function
4455 : : call and possible assignment, and stuff that in
4456 : : the directive seq of the gomp_variant. */
4457 : 48 : if (decl == fndecl)
4458 : : {
4459 : 12 : thiscall = expr;
4460 : 12 : omp_resolved_variant_calls->add (expr);
4461 : : }
4462 : : else
4463 : : {
4464 : 36 : thiscall = unshare_expr (expr);
4465 : 36 : CALL_EXPR_FN (thiscall) = build1 (ADDR_EXPR, fnptrtype, decl);
4466 : 36 : if (omp_dispatch_p)
4467 : 0 : thiscall = modify_call_for_omp_dispatch (thiscall,
4468 : : dispatch_clauses,
4469 : : want_value,
4470 : : pointerize);
4471 : : }
4472 : 48 : if (pointerize)
4473 : 0 : thiscall = build_fold_addr_expr_loc (loc, thiscall);
4474 : 48 : if (want_value)
4475 : 48 : thiscall = build2 (INIT_EXPR, type, tmp, thiscall);
4476 : 48 : all_candidates[i].alternative = thiscall;
4477 : : }
4478 : :
4479 : 12 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
4480 : 12 : tree expansion = expand_late_variant_directive (all_candidates,
4481 : : construct_context);
4482 : 12 : if (result)
4483 : 12 : expansion = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4484 : : expansion, result);
4485 : 12 : return expansion;
4486 : : }
4487 : : }
4488 : :
4489 : : /* Wrapper around expand_variant_call_expr to interface with gimplifier
4490 : : state. EXPR and OMP_DISPATCH_P are as for expand_variant_call_expr,
4491 : : FALLBACK is used to compute the WANT_VALUE and POINTERIZE arguments. */
4492 : : static tree
4493 : 1086 : gimplify_variant_call_expr (tree expr, fallback_t fallback,
4494 : : bool omp_dispatch_p)
4495 : : {
4496 : 1086 : tree type = TREE_TYPE (expr);
4497 : 1086 : bool want_value = (fallback != fb_none && !VOID_TYPE_P (type));
4498 : 475 : bool pointerize = false;
4499 : : /* If the result value must be an lvalue or the result type must
4500 : : live in memory, then we have to pointerize it if we need a temporary. */
4501 : 475 : if (want_value
4502 : 475 : && ((!(fallback & fb_rvalue) && (fallback & fb_lvalue))
4503 : 475 : || TREE_ADDRESSABLE (type)))
4504 : 0 : pointerize = true;
4505 : :
4506 : 1598 : return expand_variant_call_expr (expr, want_value, pointerize,
4507 : : omp_dispatch_p,
4508 : : (omp_dispatch_p
4509 : 512 : ? gimplify_omp_ctxp->clauses
4510 : 1086 : : NULL_TREE));
4511 : : }
4512 : :
4513 : :
4514 : : /* Helper function for gimplify_call_expr, called via walk_tree.
4515 : : Find used user labels. */
4516 : :
4517 : : static tree
4518 : 753 : find_used_user_labels (tree *tp, int *, void *)
4519 : : {
4520 : 753 : if (TREE_CODE (*tp) == LABEL_EXPR
4521 : 15 : && !DECL_ARTIFICIAL (LABEL_EXPR_LABEL (*tp))
4522 : 15 : && DECL_NAME (LABEL_EXPR_LABEL (*tp))
4523 : 768 : && TREE_USED (LABEL_EXPR_LABEL (*tp)))
4524 : 15 : return *tp;
4525 : : return NULL_TREE;
4526 : : }
4527 : :
4528 : :
4529 : : /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
4530 : : WANT_VALUE is true if the result of the call is desired. */
4531 : :
4532 : : static enum gimplify_status
4533 : 16742050 : gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
4534 : : {
4535 : 16742050 : bool want_value = (fallback != fb_none);
4536 : 16742050 : tree fndecl, parms, p, fnptrtype;
4537 : 16742050 : enum gimplify_status ret;
4538 : 16742050 : int i, nargs;
4539 : 16742050 : gcall *call;
4540 : 16742050 : bool builtin_va_start_p = false, omp_dispatch_p = false;
4541 : 16742050 : location_t loc = EXPR_LOCATION (*expr_p);
4542 : :
4543 : 16742050 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4544 : :
4545 : : /* For reliable diagnostics during inlining, it is necessary that
4546 : : every call_expr be annotated with file and line. */
4547 : 16742050 : if (! EXPR_HAS_LOCATION (*expr_p))
4548 : 556616 : SET_EXPR_LOCATION (*expr_p, input_location);
4549 : :
4550 : : /* Gimplify internal functions created in the FEs. */
4551 : 16742050 : if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
4552 : : {
4553 : 415063 : enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
4554 : 415063 : if (ifn == IFN_GOMP_DISPATCH)
4555 : : {
4556 : 877 : gcc_assert (flag_openmp
4557 : : && gimplify_omp_ctxp
4558 : : && gimplify_omp_ctxp->code == OMP_DISPATCH);
4559 : 877 : *expr_p = CALL_EXPR_ARG (*expr_p, 0);
4560 : 877 : gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
4561 : 877 : if (! EXPR_HAS_LOCATION (*expr_p))
4562 : 94 : SET_EXPR_LOCATION (*expr_p, input_location);
4563 : : omp_dispatch_p = true;
4564 : : }
4565 : : else
4566 : : {
4567 : 414186 : if (want_value)
4568 : : return GS_ALL_DONE;
4569 : :
4570 : 21582 : nargs = call_expr_nargs (*expr_p);
4571 : 21582 : auto_vec<tree> vargs (nargs);
4572 : :
4573 : 21582 : if (ifn == IFN_ASSUME)
4574 : : {
4575 : 512 : if (simple_condition_p (CALL_EXPR_ARG (*expr_p, 0)))
4576 : : {
4577 : : /* If the [[assume (cond)]]; condition is simple
4578 : : enough and can be evaluated unconditionally
4579 : : without side-effects, expand it as
4580 : : if (!cond) __builtin_unreachable (); */
4581 : 294 : tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE);
4582 : 294 : *expr_p
4583 : 588 : = build3 (COND_EXPR, void_type_node,
4584 : 294 : CALL_EXPR_ARG (*expr_p, 0), void_node,
4585 : 294 : build_call_expr_loc (EXPR_LOCATION (*expr_p),
4586 : : fndecl, 0));
4587 : 294 : return GS_OK;
4588 : : }
4589 : : /* If not optimizing, ignore the assumptions unless there
4590 : : are used user labels in it. */
4591 : 218 : if ((!optimize
4592 : 98 : && !walk_tree_without_duplicates (&CALL_EXPR_ARG (*expr_p,
4593 : : 0),
4594 : : find_used_user_labels,
4595 : : NULL))
4596 : 233 : || seen_error ())
4597 : : {
4598 : 94 : *expr_p = NULL_TREE;
4599 : 94 : return GS_ALL_DONE;
4600 : : }
4601 : : /* Temporarily, until gimple lowering, transform
4602 : : .ASSUME (cond);
4603 : : into:
4604 : : [[assume (guard)]]
4605 : : {
4606 : : guard = cond;
4607 : : }
4608 : : such that gimple lowering can outline the condition into
4609 : : a separate function easily. */
4610 : 124 : tree guard = create_tmp_var (boolean_type_node);
4611 : 124 : *expr_p = build2 (MODIFY_EXPR, void_type_node, guard,
4612 : 124 : gimple_boolify (CALL_EXPR_ARG (*expr_p, 0)));
4613 : 124 : *expr_p = build3 (BIND_EXPR, void_type_node, NULL, *expr_p, NULL);
4614 : 124 : push_gimplify_context ();
4615 : 124 : gimple_seq body = NULL;
4616 : 124 : gimple *g = gimplify_and_return_first (*expr_p, &body);
4617 : 124 : pop_gimplify_context (g);
4618 : 124 : g = gimple_build_assume (guard, body);
4619 : 124 : gimple_set_location (g, loc);
4620 : 124 : gimplify_seq_add_stmt (pre_p, g);
4621 : 124 : *expr_p = NULL_TREE;
4622 : 124 : return GS_ALL_DONE;
4623 : : }
4624 : :
4625 : 45466 : for (i = 0; i < nargs; i++)
4626 : : {
4627 : 24396 : gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
4628 : 24396 : EXPR_LOCATION (*expr_p));
4629 : 24396 : vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
4630 : : }
4631 : :
4632 : 21070 : gcall *call = gimple_build_call_internal_vec (ifn, vargs);
4633 : 21070 : gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
4634 : 21070 : gimplify_seq_add_stmt (pre_p, call);
4635 : 21070 : return GS_ALL_DONE;
4636 : 21582 : }
4637 : : }
4638 : :
4639 : : /* This may be a call to a builtin function.
4640 : :
4641 : : Builtin function calls may be transformed into different
4642 : : (and more efficient) builtin function calls under certain
4643 : : circumstances. Unfortunately, gimplification can muck things
4644 : : up enough that the builtin expanders are not aware that certain
4645 : : transformations are still valid.
4646 : :
4647 : : So we attempt transformation/gimplification of the call before
4648 : : we gimplify the CALL_EXPR. At this time we do not manage to
4649 : : transform all calls in the same manner as the expanders do, but
4650 : : we do transform most of them. */
4651 : 16327864 : fndecl = get_callee_fndecl (*expr_p);
4652 : 16327864 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4653 : 3891195 : switch (DECL_FUNCTION_CODE (fndecl))
4654 : : {
4655 : 80656 : CASE_BUILT_IN_ALLOCA:
4656 : : /* If the call has been built for a variable-sized object, then we
4657 : : want to restore the stack level when the enclosing BIND_EXPR is
4658 : : exited to reclaim the allocated space; otherwise, we precisely
4659 : : need to do the opposite and preserve the latest stack level. */
4660 : 80656 : if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
4661 : 8838 : gimplify_ctxp->save_stack = true;
4662 : : else
4663 : 71818 : gimplify_ctxp->keep_stack = true;
4664 : : break;
4665 : :
4666 : 20663 : case BUILT_IN_VA_START:
4667 : 20663 : {
4668 : 20663 : builtin_va_start_p = true;
4669 : 20663 : if (call_expr_nargs (*expr_p) < 2)
4670 : : {
4671 : 1 : error ("too few arguments to function %<va_start%>");
4672 : 1 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4673 : 1 : return GS_OK;
4674 : : }
4675 : :
4676 : 20662 : if (fold_builtin_next_arg (*expr_p, true))
4677 : : {
4678 : 8 : *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
4679 : 8 : return GS_OK;
4680 : : }
4681 : : break;
4682 : : }
4683 : :
4684 : 30 : case BUILT_IN_EH_RETURN:
4685 : 30 : cfun->calls_eh_return = true;
4686 : 30 : break;
4687 : :
4688 : 1014 : case BUILT_IN_CLEAR_PADDING:
4689 : 1014 : if (call_expr_nargs (*expr_p) == 1)
4690 : : {
4691 : : /* Remember the original type of the argument in an internal
4692 : : dummy second argument, as in GIMPLE pointer conversions are
4693 : : useless. Also mark this call as not for automatic
4694 : : initialization in the internal dummy third argument. */
4695 : 507 : p = CALL_EXPR_ARG (*expr_p, 0);
4696 : 507 : *expr_p
4697 : 507 : = build_call_expr_loc (EXPR_LOCATION (*expr_p), fndecl, 2, p,
4698 : 507 : build_zero_cst (TREE_TYPE (p)));
4699 : 507 : return GS_OK;
4700 : : }
4701 : : break;
4702 : :
4703 : : default:
4704 : : ;
4705 : : }
4706 : 16327348 : if (fndecl && fndecl_built_in_p (fndecl))
4707 : : {
4708 : 4123498 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4709 : 4123498 : if (new_tree && new_tree != *expr_p)
4710 : : {
4711 : : /* There was a transformation of this call which computes the
4712 : : same value, but in a more efficient way. Return and try
4713 : : again. */
4714 : 755 : *expr_p = new_tree;
4715 : 755 : return GS_OK;
4716 : : }
4717 : : }
4718 : :
4719 : : /* Remember the original function pointer type. */
4720 : 16326593 : fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
4721 : :
4722 : : /* Handle "declare variant" resolution and arglist processing. */
4723 : 16326593 : if (flag_openmp
4724 : 439841 : && fndecl
4725 : 439139 : && cfun
4726 : 439139 : && (cfun->curr_properties & PROP_gimple_any) == 0
4727 : 242259 : && !omp_has_novariants ()
4728 : 16568746 : && lookup_attribute ("omp declare variant base",
4729 : 242153 : DECL_ATTRIBUTES (fndecl)))
4730 : : {
4731 : 1086 : tree orig = *expr_p;
4732 : 1086 : *expr_p = gimplify_variant_call_expr (*expr_p, fallback,
4733 : : omp_dispatch_p);
4734 : :
4735 : : /* This may resolve to the same call, or the call expr with just
4736 : : the function replaced, in which case we should just continue to
4737 : : gimplify it normally. Otherwise, if we get something else back,
4738 : : stop here and re-gimplify the whole replacement expr. */
4739 : 1086 : if (*expr_p != orig)
4740 : : return GS_OK;
4741 : : }
4742 : :
4743 : : /* There is a sequence point before the call, so any side effects in
4744 : : the calling expression must occur before the actual call. Force
4745 : : gimplify_expr to use an internal post queue. */
4746 : 16326110 : ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
4747 : : is_gimple_call_addr, fb_rvalue);
4748 : :
4749 : 16326110 : if (ret == GS_ERROR)
4750 : : return GS_ERROR;
4751 : :
4752 : 16326108 : nargs = call_expr_nargs (*expr_p);
4753 : :
4754 : : /* Get argument types for verification. */
4755 : 16326108 : fndecl = get_callee_fndecl (*expr_p);
4756 : 16326108 : parms = NULL_TREE;
4757 : 16326108 : if (fndecl)
4758 : 16106535 : parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4759 : : else
4760 : 219573 : parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
4761 : :
4762 : 32432643 : if (fndecl && DECL_ARGUMENTS (fndecl))
4763 : : p = DECL_ARGUMENTS (fndecl);
4764 : 7214734 : else if (parms)
4765 : : p = parms;
4766 : : else
4767 : : p = NULL_TREE;
4768 : 43241609 : for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
4769 : : ;
4770 : :
4771 : : /* If the last argument is __builtin_va_arg_pack () and it is not
4772 : : passed as a named argument, decrease the number of CALL_EXPR
4773 : : arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
4774 : 16326108 : if (!p
4775 : 16326108 : && i < nargs
4776 : 16326108 : && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
4777 : : {
4778 : 6751 : tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
4779 : 6751 : tree last_arg_fndecl = get_callee_fndecl (last_arg);
4780 : :
4781 : 6751 : if (last_arg_fndecl
4782 : 6751 : && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
4783 : : {
4784 : 130 : tree call = *expr_p;
4785 : :
4786 : 130 : --nargs;
4787 : 260 : *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
4788 : 130 : CALL_EXPR_FN (call),
4789 : 130 : nargs, CALL_EXPR_ARGP (call));
4790 : :
4791 : : /* Copy all CALL_EXPR flags, location and block, except
4792 : : CALL_EXPR_VA_ARG_PACK flag. */
4793 : 130 : CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
4794 : 130 : CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
4795 : 130 : CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
4796 : 130 : = CALL_EXPR_RETURN_SLOT_OPT (call);
4797 : 130 : CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
4798 : 130 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
4799 : :
4800 : : /* Set CALL_EXPR_VA_ARG_PACK. */
4801 : 130 : CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
4802 : : }
4803 : : }
4804 : :
4805 : : /* If the call returns twice then after building the CFG the call
4806 : : argument computations will no longer dominate the call because
4807 : : we add an abnormal incoming edge to the call. So do not use SSA
4808 : : vars there. */
4809 : 16326108 : bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
4810 : :
4811 : :
4812 : : /* Gimplify the function arguments. */
4813 : 16326108 : if (nargs > 0)
4814 : : {
4815 : 14154323 : for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
4816 : 42516755 : PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
4817 : 28362432 : PUSH_ARGS_REVERSED ? i-- : i++)
4818 : : {
4819 : 28362432 : enum gimplify_status t;
4820 : :
4821 : : /* Avoid gimplifying the second argument to va_start, which needs to
4822 : : be the plain PARM_DECL. */
4823 : 28362432 : if ((i != 1) || !builtin_va_start_p)
4824 : : {
4825 : 28341778 : tree *arg_p = &CALL_EXPR_ARG (*expr_p, i);
4826 : :
4827 : 28341778 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4828 : 6674 : gimplify_omp_ctxp->in_call_args = true;
4829 : 28341778 : t = gimplify_arg (arg_p, pre_p, EXPR_LOCATION (*expr_p),
4830 : 28341778 : !returns_twice);
4831 : 28341778 : if (gimplify_omp_ctxp && gimplify_omp_ctxp->code == OMP_DISPATCH)
4832 : 6674 : gimplify_omp_ctxp->in_call_args = false;
4833 : :
4834 : 28341778 : if (t == GS_ERROR)
4835 : 28362432 : ret = GS_ERROR;
4836 : : }
4837 : : }
4838 : : }
4839 : :
4840 : : /* Gimplify the static chain. */
4841 : 16326108 : if (CALL_EXPR_STATIC_CHAIN (*expr_p))
4842 : : {
4843 : 28852 : if (fndecl && !DECL_STATIC_CHAIN (fndecl))
4844 : 3 : CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
4845 : : else
4846 : : {
4847 : 28173 : enum gimplify_status t;
4848 : 28173 : t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
4849 : 28173 : EXPR_LOCATION (*expr_p), ! returns_twice);
4850 : 28173 : if (t == GS_ERROR)
4851 : 16326108 : ret = GS_ERROR;
4852 : : }
4853 : : }
4854 : :
4855 : : /* Verify the function result. */
4856 : 16326108 : if (want_value && fndecl
4857 : 16326108 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
4858 : : {
4859 : 0 : error_at (loc, "using result of function returning %<void%>");
4860 : 0 : ret = GS_ERROR;
4861 : : }
4862 : :
4863 : : /* Try this again in case gimplification exposed something. */
4864 : 16326108 : if (ret != GS_ERROR)
4865 : : {
4866 : 16326077 : tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
4867 : :
4868 : 16326077 : if (new_tree && new_tree != *expr_p)
4869 : : {
4870 : : /* There was a transformation of this call which computes the
4871 : : same value, but in a more efficient way. Return and try
4872 : : again. */
4873 : 11512 : *expr_p = new_tree;
4874 : 11512 : return GS_OK;
4875 : : }
4876 : : }
4877 : : else
4878 : : {
4879 : 31 : *expr_p = error_mark_node;
4880 : 31 : return GS_ERROR;
4881 : : }
4882 : :
4883 : : /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
4884 : : decl. This allows us to eliminate redundant or useless
4885 : : calls to "const" functions. */
4886 : 16314565 : if (TREE_CODE (*expr_p) == CALL_EXPR)
4887 : : {
4888 : 16314565 : int flags = call_expr_flags (*expr_p);
4889 : 16314565 : if (flags & (ECF_CONST | ECF_PURE)
4890 : : /* An infinite loop is considered a side effect. */
4891 : 2426711 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
4892 : 2028143 : TREE_SIDE_EFFECTS (*expr_p) = 0;
4893 : : }
4894 : :
4895 : : /* If the value is not needed by the caller, emit a new GIMPLE_CALL
4896 : : and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
4897 : : form and delegate the creation of a GIMPLE_CALL to
4898 : : gimplify_modify_expr. This is always possible because when
4899 : : WANT_VALUE is true, the caller wants the result of this call into
4900 : : a temporary, which means that we will emit an INIT_EXPR in
4901 : : internal_get_tmp_var which will then be handled by
4902 : : gimplify_modify_expr. */
4903 : 16314565 : if (!want_value)
4904 : : {
4905 : : /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
4906 : : have to do is replicate it as a GIMPLE_CALL tuple. */
4907 : 6187690 : gimple_stmt_iterator gsi;
4908 : 6187690 : call = gimple_build_call_from_tree (*expr_p, fnptrtype);
4909 : 6187690 : notice_special_calls (call);
4910 : 6187690 : gimplify_seq_add_stmt (pre_p, call);
4911 : 6187690 : gsi = gsi_last (*pre_p);
4912 : 6187690 : maybe_fold_stmt (&gsi);
4913 : 6187690 : *expr_p = NULL_TREE;
4914 : : }
4915 : : else
4916 : : /* Remember the original function type. */
4917 : 10126875 : CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
4918 : 10126875 : CALL_EXPR_FN (*expr_p));
4919 : :
4920 : : return ret;
4921 : : }
4922 : :
4923 : : /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
4924 : : rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
4925 : :
4926 : : TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
4927 : : condition is true or false, respectively. If null, we should generate
4928 : : our own to skip over the evaluation of this specific expression.
4929 : :
4930 : : LOCUS is the source location of the COND_EXPR.
4931 : :
4932 : : The condition_uid is a discriminator tag for condition coverage used to map
4933 : : conditions to its corresponding full Boolean function.
4934 : :
4935 : : This function is the tree equivalent of do_jump.
4936 : :
4937 : : shortcut_cond_r should only be called by shortcut_cond_expr. */
4938 : :
4939 : : static tree
4940 : 1318149 : shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
4941 : : location_t locus, unsigned condition_uid)
4942 : : {
4943 : 1318149 : tree local_label = NULL_TREE;
4944 : 1318149 : tree t, expr = NULL;
4945 : :
4946 : : /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
4947 : : retain the shortcut semantics. Just insert the gotos here;
4948 : : shortcut_cond_expr will append the real blocks later. */
4949 : 1318149 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
4950 : : {
4951 : 117774 : location_t new_locus;
4952 : :
4953 : : /* Turn if (a && b) into
4954 : :
4955 : : if (a); else goto no;
4956 : : if (b) goto yes; else goto no;
4957 : : (no:) */
4958 : :
4959 : 117774 : if (false_label_p == NULL)
4960 : 3997 : false_label_p = &local_label;
4961 : :
4962 : : /* Keep the original source location on the first 'if'. */
4963 : 117774 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus,
4964 : : condition_uid);
4965 : 117774 : append_to_statement_list (t, &expr);
4966 : :
4967 : : /* Set the source location of the && on the second 'if'. */
4968 : 117774 : new_locus = rexpr_location (pred, locus);
4969 : 117774 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4970 : : new_locus, condition_uid);
4971 : 117774 : append_to_statement_list (t, &expr);
4972 : : }
4973 : 1200375 : else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
4974 : : {
4975 : 386206 : location_t new_locus;
4976 : :
4977 : : /* Turn if (a || b) into
4978 : :
4979 : : if (a) goto yes;
4980 : : if (b) goto yes; else goto no;
4981 : : (yes:) */
4982 : :
4983 : 386206 : if (true_label_p == NULL)
4984 : 233860 : true_label_p = &local_label;
4985 : :
4986 : : /* Keep the original source location on the first 'if'. */
4987 : 386206 : t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus,
4988 : : condition_uid);
4989 : 386206 : append_to_statement_list (t, &expr);
4990 : :
4991 : : /* Set the source location of the || on the second 'if'. */
4992 : 386206 : new_locus = rexpr_location (pred, locus);
4993 : 386206 : t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
4994 : : new_locus, condition_uid);
4995 : 386206 : append_to_statement_list (t, &expr);
4996 : : }
4997 : 814169 : else if (TREE_CODE (pred) == COND_EXPR
4998 : 764 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
4999 : 814933 : && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
5000 : : {
5001 : 758 : location_t new_locus;
5002 : :
5003 : : /* As long as we're messing with gotos, turn if (a ? b : c) into
5004 : : if (a)
5005 : : if (b) goto yes; else goto no;
5006 : : else
5007 : : if (c) goto yes; else goto no;
5008 : :
5009 : : Don't do this if one of the arms has void type, which can happen
5010 : : in C++ when the arm is throw. */
5011 : :
5012 : : /* Keep the original source location on the first 'if'. Set the source
5013 : : location of the ? on the second 'if'. */
5014 : 758 : new_locus = rexpr_location (pred, locus);
5015 : 1516 : expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
5016 : 758 : shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
5017 : : false_label_p, locus, condition_uid),
5018 : 758 : shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
5019 : : false_label_p, new_locus,
5020 : : condition_uid));
5021 : 758 : tree_associate_condition_with_expr (expr, condition_uid);
5022 : : }
5023 : : else
5024 : : {
5025 : 813411 : expr = build3 (COND_EXPR, void_type_node, pred,
5026 : : build_and_jump (true_label_p),
5027 : : build_and_jump (false_label_p));
5028 : 813411 : SET_EXPR_LOCATION (expr, locus);
5029 : 813411 : tree_associate_condition_with_expr (expr, condition_uid);
5030 : : }
5031 : :
5032 : 1318149 : if (local_label)
5033 : : {
5034 : 237857 : t = build1 (LABEL_EXPR, void_type_node, local_label);
5035 : 237857 : append_to_statement_list (t, &expr);
5036 : : }
5037 : :
5038 : 1318149 : return expr;
5039 : : }
5040 : :
5041 : : /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
5042 : : any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
5043 : : statement, if it is the last one. Otherwise, return NULL. */
5044 : :
5045 : : static tree
5046 : 11366502 : find_goto (tree expr)
5047 : : {
5048 : 11671958 : if (!expr)
5049 : : return NULL_TREE;
5050 : :
5051 : 9746759 : if (TREE_CODE (expr) == GOTO_EXPR)
5052 : : return expr;
5053 : :
5054 : 7781812 : if (TREE_CODE (expr) != STATEMENT_LIST)
5055 : : return NULL_TREE;
5056 : :
5057 : 1078647 : tree_stmt_iterator i = tsi_start (expr);
5058 : :
5059 : 1470715 : while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
5060 : 392068 : tsi_next (&i);
5061 : :
5062 : 10387534 : if (!tsi_one_before_end_p (i))
5063 : : return NULL_TREE;
5064 : :
5065 : 305456 : return find_goto (tsi_stmt (i));
5066 : : }
5067 : :
5068 : : /* Same as find_goto, except that it returns NULL if the destination
5069 : : is not a LABEL_DECL. */
5070 : :
5071 : : static inline tree
5072 : 11366502 : find_goto_label (tree expr)
5073 : : {
5074 : 11366502 : tree dest = find_goto (expr);
5075 : 13331449 : if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
5076 : 1964836 : return dest;
5077 : : return NULL_TREE;
5078 : : }
5079 : :
5080 : :
5081 : : /* Given a multi-term condition (ANDIF, ORIF), walk the predicate PRED and tag
5082 : : every basic condition with CONDITION_UID. Two basic conditions share the
5083 : : CONDITION_UID discriminator when they belong to the same predicate, which is
5084 : : used by the condition coverage. Doing this as an explicit step makes for a
5085 : : simpler implementation than weaving it into the splitting code as the
5086 : : splitting code eventually calls the entry point gimplfiy_expr which makes
5087 : : bookkeeping complicated. */
5088 : : static void
5089 : 570389 : tag_shortcut_cond (tree pred, unsigned condition_uid)
5090 : : {
5091 : 606175 : if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR
5092 : 606175 : || TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5093 : : {
5094 : 557033 : tree fst = TREE_OPERAND (pred, 0);
5095 : 557033 : tree lst = TREE_OPERAND (pred, 1);
5096 : :
5097 : 557033 : if (TREE_CODE (fst) == TRUTH_ANDIF_EXPR
5098 : 557033 : || TREE_CODE (fst) == TRUTH_ORIF_EXPR)
5099 : 166982 : tag_shortcut_cond (fst, condition_uid);
5100 : 390051 : else if (TREE_CODE (fst) == COND_EXPR)
5101 : 216 : tree_associate_condition_with_expr (fst, condition_uid);
5102 : :
5103 : 557033 : if (TREE_CODE (lst) == TRUTH_ANDIF_EXPR
5104 : 557033 : || TREE_CODE (lst) == TRUTH_ORIF_EXPR)
5105 : : tag_shortcut_cond (lst, condition_uid);
5106 : 521247 : else if (TREE_CODE (lst) == COND_EXPR)
5107 : 424 : tree_associate_condition_with_expr (lst, condition_uid);
5108 : : }
5109 : 570389 : }
5110 : :
5111 : : /* Given a conditional expression EXPR with short-circuit boolean
5112 : : predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
5113 : : predicate apart into the equivalent sequence of conditionals. CONDITION_UID
5114 : : is a the tag/discriminator for this EXPR - all basic conditions in the
5115 : : expression will be given the same CONDITION_UID. */
5116 : : static tree
5117 : 403407 : shortcut_cond_expr (tree expr, unsigned condition_uid)
5118 : : {
5119 : 403407 : tree pred = TREE_OPERAND (expr, 0);
5120 : 403407 : tree then_ = TREE_OPERAND (expr, 1);
5121 : 403407 : tree else_ = TREE_OPERAND (expr, 2);
5122 : 403407 : tree true_label, false_label, end_label, t;
5123 : 403407 : tree *true_label_p;
5124 : 403407 : tree *false_label_p;
5125 : 403407 : bool emit_end, emit_false, jump_over_else;
5126 : 403407 : bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
5127 : 403407 : bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
5128 : :
5129 : 403407 : tag_shortcut_cond (pred, condition_uid);
5130 : :
5131 : : /* First do simple transformations. */
5132 : 403407 : if (!else_se)
5133 : : {
5134 : : /* If there is no 'else', turn
5135 : : if (a && b) then c
5136 : : into
5137 : : if (a) if (b) then c. */
5138 : 294342 : while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
5139 : : {
5140 : : /* Keep the original source location on the first 'if'. */
5141 : 50066 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5142 : 50066 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5143 : : /* Set the source location of the && on the second 'if'. */
5144 : 50066 : if (rexpr_has_location (pred))
5145 : 49046 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5146 : 50066 : then_ = shortcut_cond_expr (expr, condition_uid);
5147 : 50066 : then_se = then_ && TREE_SIDE_EFFECTS (then_);
5148 : 50066 : pred = TREE_OPERAND (pred, 0);
5149 : 50066 : expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
5150 : 50066 : SET_EXPR_LOCATION (expr, locus);
5151 : : }
5152 : : }
5153 : :
5154 : 403407 : if (!then_se)
5155 : : {
5156 : : /* If there is no 'then', turn
5157 : : if (a || b); else d
5158 : : into
5159 : : if (a); else if (b); else d. */
5160 : 18547 : while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
5161 : : {
5162 : : /* Keep the original source location on the first 'if'. */
5163 : 679 : location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
5164 : 679 : TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
5165 : : /* Set the source location of the || on the second 'if'. */
5166 : 679 : if (rexpr_has_location (pred))
5167 : 678 : SET_EXPR_LOCATION (expr, rexpr_location (pred));
5168 : 679 : else_ = shortcut_cond_expr (expr, condition_uid);
5169 : 679 : else_se = else_ && TREE_SIDE_EFFECTS (else_);
5170 : 679 : pred = TREE_OPERAND (pred, 0);
5171 : 679 : expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
5172 : 679 : SET_EXPR_LOCATION (expr, locus);
5173 : : }
5174 : : }
5175 : :
5176 : : /* The expr tree should also have the expression id set. */
5177 : 403407 : tree_associate_condition_with_expr (expr, condition_uid);
5178 : :
5179 : : /* If we're done, great. */
5180 : 403407 : if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
5181 : 403407 : && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
5182 : 94734 : return expr;
5183 : :
5184 : : /* Otherwise we need to mess with gotos. Change
5185 : : if (a) c; else d;
5186 : : to
5187 : : if (a); else goto no;
5188 : : c; goto end;
5189 : : no: d; end:
5190 : : and recursively gimplify the condition. */
5191 : :
5192 : 308673 : true_label = false_label = end_label = NULL_TREE;
5193 : :
5194 : : /* If our arms just jump somewhere, hijack those labels so we don't
5195 : : generate jumps to jumps. */
5196 : :
5197 : 308673 : if (tree then_goto = find_goto_label (then_))
5198 : : {
5199 : 4471 : true_label = GOTO_DESTINATION (then_goto);
5200 : 4471 : then_ = NULL;
5201 : 4471 : then_se = false;
5202 : : }
5203 : :
5204 : 308673 : if (tree else_goto = find_goto_label (else_))
5205 : : {
5206 : 4092 : false_label = GOTO_DESTINATION (else_goto);
5207 : 4092 : else_ = NULL;
5208 : 4092 : else_se = false;
5209 : : }
5210 : :
5211 : : /* If we aren't hijacking a label for the 'then' branch, it falls through. */
5212 : 308673 : if (true_label)
5213 : : true_label_p = &true_label;
5214 : : else
5215 : 304202 : true_label_p = NULL;
5216 : :
5217 : : /* The 'else' branch also needs a label if it contains interesting code. */
5218 : 308673 : if (false_label || else_se)
5219 : : false_label_p = &false_label;
5220 : : else
5221 : : false_label_p = NULL;
5222 : :
5223 : : /* If there was nothing else in our arms, just forward the label(s). */
5224 : 154409 : if (!then_se && !else_se)
5225 : 8270 : return shortcut_cond_r (pred, true_label_p, false_label_p,
5226 : 12405 : EXPR_LOC_OR_LOC (expr, input_location), condition_uid);
5227 : :
5228 : : /* If our last subexpression already has a terminal label, reuse it. */
5229 : 304538 : if (else_se)
5230 : 154264 : t = expr_last (else_);
5231 : 150274 : else if (then_se)
5232 : 150274 : t = expr_last (then_);
5233 : : else
5234 : : t = NULL;
5235 : 304538 : if (t && TREE_CODE (t) == LABEL_EXPR)
5236 : 924 : end_label = LABEL_EXPR_LABEL (t);
5237 : :
5238 : : /* If we don't care about jumping to the 'else' branch, jump to the end
5239 : : if the condition is false. */
5240 : 304538 : if (!false_label_p)
5241 : 150031 : false_label_p = &end_label;
5242 : :
5243 : : /* We only want to emit these labels if we aren't hijacking them. */
5244 : 304538 : emit_end = (end_label == NULL_TREE);
5245 : 304538 : emit_false = (false_label == NULL_TREE);
5246 : :
5247 : : /* We only emit the jump over the else clause if we have to--if the
5248 : : then clause may fall through. Otherwise we can wind up with a
5249 : : useless jump and a useless label at the end of gimplified code,
5250 : : which will cause us to think that this conditional as a whole
5251 : : falls through even if it doesn't. If we then inline a function
5252 : : which ends with such a condition, that can cause us to issue an
5253 : : inappropriate warning about control reaching the end of a
5254 : : non-void function. */
5255 : 304538 : jump_over_else = block_may_fallthru (then_);
5256 : :
5257 : 608453 : pred = shortcut_cond_r (pred, true_label_p, false_label_p,
5258 : 608453 : EXPR_LOC_OR_LOC (expr, input_location),
5259 : : condition_uid);
5260 : :
5261 : 304538 : expr = NULL;
5262 : 304538 : append_to_statement_list (pred, &expr);
5263 : :
5264 : 304538 : append_to_statement_list (then_, &expr);
5265 : 304538 : if (else_se)
5266 : : {
5267 : 154264 : if (jump_over_else)
5268 : : {
5269 : 151793 : tree last = expr_last (expr);
5270 : 151793 : t = build_and_jump (&end_label);
5271 : 151793 : if (rexpr_has_location (last))
5272 : 32278 : SET_EXPR_LOCATION (t, rexpr_location (last));
5273 : 151793 : append_to_statement_list (t, &expr);
5274 : : }
5275 : 154264 : if (emit_false)
5276 : : {
5277 : 154264 : t = build1 (LABEL_EXPR, void_type_node, false_label);
5278 : 154264 : append_to_statement_list (t, &expr);
5279 : : }
5280 : 154264 : append_to_statement_list (else_, &expr);
5281 : : }
5282 : 304538 : if (emit_end && end_label)
5283 : : {
5284 : 300905 : t = build1 (LABEL_EXPR, void_type_node, end_label);
5285 : 300905 : append_to_statement_list (t, &expr);
5286 : : }
5287 : :
5288 : 304538 : return expr;
5289 : : }
5290 : :
5291 : : /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
5292 : :
5293 : : tree
5294 : 16405635 : gimple_boolify (tree expr)
5295 : : {
5296 : 16405635 : tree type = TREE_TYPE (expr);
5297 : 16405635 : location_t loc = EXPR_LOCATION (expr);
5298 : :
5299 : 16405635 : if (TREE_CODE (expr) == NE_EXPR
5300 : 5645820 : && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
5301 : 17340260 : && integer_zerop (TREE_OPERAND (expr, 1)))
5302 : : {
5303 : 475491 : tree call = TREE_OPERAND (expr, 0);
5304 : 475491 : tree fn = get_callee_fndecl (call);
5305 : :
5306 : : /* For __builtin_expect ((long) (x), y) recurse into x as well
5307 : : if x is truth_value_p. */
5308 : 475491 : if (fn
5309 : 474916 : && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
5310 : 602566 : && call_expr_nargs (call) == 2)
5311 : : {
5312 : 127075 : tree arg = CALL_EXPR_ARG (call, 0);
5313 : 127075 : if (arg)
5314 : : {
5315 : 127075 : if (TREE_CODE (arg) == NOP_EXPR
5316 : 127075 : && TREE_TYPE (arg) == TREE_TYPE (call))
5317 : 61430 : arg = TREE_OPERAND (arg, 0);
5318 : 127075 : if (truth_value_p (TREE_CODE (arg)))
5319 : : {
5320 : 104910 : arg = gimple_boolify (arg);
5321 : 104910 : CALL_EXPR_ARG (call, 0)
5322 : 209820 : = fold_convert_loc (loc, TREE_TYPE (call), arg);
5323 : : }
5324 : : }
5325 : : }
5326 : : }
5327 : :
5328 : 16405635 : switch (TREE_CODE (expr))
5329 : : {
5330 : 1235126 : case TRUTH_AND_EXPR:
5331 : 1235126 : case TRUTH_OR_EXPR:
5332 : 1235126 : case TRUTH_XOR_EXPR:
5333 : 1235126 : case TRUTH_ANDIF_EXPR:
5334 : 1235126 : case TRUTH_ORIF_EXPR:
5335 : : /* Also boolify the arguments of truth exprs. */
5336 : 1235126 : TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
5337 : : /* FALLTHRU */
5338 : :
5339 : 1696657 : case TRUTH_NOT_EXPR:
5340 : 1696657 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5341 : :
5342 : : /* These expressions always produce boolean results. */
5343 : 1696657 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5344 : 303714 : TREE_TYPE (expr) = boolean_type_node;
5345 : : return expr;
5346 : :
5347 : 4769 : case ANNOTATE_EXPR:
5348 : 4769 : switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
5349 : : {
5350 : 4769 : case annot_expr_ivdep_kind:
5351 : 4769 : case annot_expr_unroll_kind:
5352 : 4769 : case annot_expr_no_vector_kind:
5353 : 4769 : case annot_expr_vector_kind:
5354 : 4769 : case annot_expr_parallel_kind:
5355 : 4769 : case annot_expr_maybe_infinite_kind:
5356 : 4769 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5357 : 4769 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5358 : 2875 : TREE_TYPE (expr) = boolean_type_node;
5359 : : return expr;
5360 : 0 : default:
5361 : 0 : gcc_unreachable ();
5362 : : }
5363 : :
5364 : 14704209 : default:
5365 : 14704209 : if (COMPARISON_CLASS_P (expr))
5366 : : {
5367 : : /* These expressions always produce boolean results. */
5368 : 12820391 : if (TREE_CODE (type) != BOOLEAN_TYPE)
5369 : 1549825 : TREE_TYPE (expr) = boolean_type_node;
5370 : 12820391 : return expr;
5371 : : }
5372 : : /* Other expressions that get here must have boolean values, but
5373 : : might need to be converted to the appropriate mode. */
5374 : 1883818 : if (TREE_CODE (type) == BOOLEAN_TYPE)
5375 : : return expr;
5376 : 193798 : return fold_convert_loc (loc, boolean_type_node, expr);
5377 : : }
5378 : : }
5379 : :
5380 : : /* Given a conditional expression *EXPR_P without side effects, gimplify
5381 : : its operands. New statements are inserted to PRE_P. */
5382 : :
5383 : : static enum gimplify_status
5384 : 46041 : gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
5385 : : {
5386 : 46041 : tree expr = *expr_p, cond;
5387 : 46041 : enum gimplify_status ret, tret;
5388 : 46041 : enum tree_code code;
5389 : :
5390 : 46041 : cond = gimple_boolify (COND_EXPR_COND (expr));
5391 : :
5392 : : /* We need to handle && and || specially, as their gimplification
5393 : : creates pure cond_expr, thus leading to an infinite cycle otherwise. */
5394 : 46041 : code = TREE_CODE (cond);
5395 : 46041 : if (code == TRUTH_ANDIF_EXPR)
5396 : 32 : TREE_SET_CODE (cond, TRUTH_AND_EXPR);
5397 : 46009 : else if (code == TRUTH_ORIF_EXPR)
5398 : 4583 : TREE_SET_CODE (cond, TRUTH_OR_EXPR);
5399 : 46041 : ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_val, fb_rvalue);
5400 : 46041 : COND_EXPR_COND (*expr_p) = cond;
5401 : :
5402 : 46041 : tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
5403 : : is_gimple_val, fb_rvalue);
5404 : 46041 : ret = MIN (ret, tret);
5405 : 46041 : tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
5406 : : is_gimple_val, fb_rvalue);
5407 : :
5408 : 46041 : return MIN (ret, tret);
5409 : : }
5410 : :
5411 : : /* Return true if evaluating EXPR could trap.
5412 : : EXPR is GENERIC, while tree_could_trap_p can be called
5413 : : only on GIMPLE. */
5414 : :
5415 : : bool
5416 : 12954347 : generic_expr_could_trap_p (tree expr)
5417 : : {
5418 : 12954347 : unsigned i, n;
5419 : :
5420 : 12954347 : if (!expr || is_gimple_val (expr))
5421 : 5309657 : return false;
5422 : :
5423 : 7644690 : if (!EXPR_P (expr) || tree_could_trap_p (expr))
5424 : 2365601 : return true;
5425 : :
5426 : 5279089 : n = TREE_OPERAND_LENGTH (expr);
5427 : 11834561 : for (i = 0; i < n; i++)
5428 : 8816413 : if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
5429 : : return true;
5430 : :
5431 : : return false;
5432 : : }
5433 : :
5434 : : /* Associate the condition STMT with the discriminator UID. STMTs that are
5435 : : broken down with ANDIF/ORIF from the same Boolean expression should be given
5436 : : the same UID; 'if (a && b && c) { if (d || e) ... } ...' should yield the
5437 : : { a: 1, b: 1, c: 1, d: 2, e: 2 } when gimplification is done. This is used
5438 : : for condition coverage. */
5439 : : static void
5440 : 5374578 : gimple_associate_condition_with_expr (struct function *fn, gcond *stmt,
5441 : : unsigned uid)
5442 : : {
5443 : 5374578 : if (!condition_coverage_flag)
5444 : : return;
5445 : :
5446 : 636 : if (!fn->cond_uids)
5447 : 135 : fn->cond_uids = new hash_map <gcond*, unsigned> ();
5448 : :
5449 : 636 : fn->cond_uids->put (stmt, uid);
5450 : : }
5451 : :
5452 : : /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
5453 : : into
5454 : :
5455 : : if (p) if (p)
5456 : : t1 = a; a;
5457 : : else or else
5458 : : t1 = b; b;
5459 : : t1;
5460 : :
5461 : : The second form is used when *EXPR_P is of type void.
5462 : :
5463 : : PRE_P points to the list where side effects that must happen before
5464 : : *EXPR_P should be stored. */
5465 : :
5466 : : static enum gimplify_status
5467 : 6140959 : gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
5468 : : {
5469 : 6140959 : tree expr = *expr_p;
5470 : 6140959 : tree type = TREE_TYPE (expr);
5471 : 6140959 : location_t loc = EXPR_LOCATION (expr);
5472 : 6140959 : tree tmp, arm1, arm2;
5473 : 6140959 : enum gimplify_status ret;
5474 : 6140959 : tree label_true, label_false, label_cont;
5475 : 6140959 : bool have_then_clause_p, have_else_clause_p;
5476 : 6140959 : gcond *cond_stmt;
5477 : 6140959 : enum tree_code pred_code;
5478 : 6140959 : gimple_seq seq = NULL;
5479 : :
5480 : : /* If this COND_EXPR has a value, copy the values into a temporary within
5481 : : the arms. */
5482 : 6140959 : if (!VOID_TYPE_P (type))
5483 : : {
5484 : 412962 : tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
5485 : 412962 : tree result;
5486 : :
5487 : : /* If either an rvalue is ok or we do not require an lvalue, create the
5488 : : temporary. But we cannot do that if the type is addressable. */
5489 : 412962 : if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
5490 : 412955 : && !TREE_ADDRESSABLE (type))
5491 : : {
5492 : 412955 : if (gimplify_ctxp->allow_rhs_cond_expr
5493 : : /* If either branch has side effects or could trap, it can't be
5494 : : evaluated unconditionally. */
5495 : 46041 : && !TREE_SIDE_EFFECTS (then_)
5496 : 46041 : && !generic_expr_could_trap_p (then_)
5497 : 46041 : && !TREE_SIDE_EFFECTS (else_)
5498 : 458996 : && !generic_expr_could_trap_p (else_))
5499 : 46041 : return gimplify_pure_cond_expr (expr_p, pre_p);
5500 : :
5501 : 366914 : tmp = create_tmp_var (type, "iftmp");
5502 : 366914 : result = tmp;
5503 : : }
5504 : :
5505 : : /* Otherwise, only create and copy references to the values. */
5506 : : else
5507 : : {
5508 : 7 : type = build_pointer_type (type);
5509 : :
5510 : 7 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5511 : 7 : then_ = build_fold_addr_expr_loc (loc, then_);
5512 : :
5513 : 7 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5514 : 7 : else_ = build_fold_addr_expr_loc (loc, else_);
5515 : :
5516 : 7 : expr
5517 : 7 : = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
5518 : :
5519 : 7 : tmp = create_tmp_var (type, "iftmp");
5520 : 7 : result = build_simple_mem_ref_loc (loc, tmp);
5521 : : }
5522 : :
5523 : : /* Build the new then clause, `tmp = then_;'. But don't build the
5524 : : assignment if the value is void; in C++ it can be if it's a throw. */
5525 : 366921 : if (!VOID_TYPE_P (TREE_TYPE (then_)))
5526 : 285361 : TREE_OPERAND (expr, 1) = build2 (INIT_EXPR, type, tmp, then_);
5527 : :
5528 : : /* Similarly, build the new else clause, `tmp = else_;'. */
5529 : 366921 : if (!VOID_TYPE_P (TREE_TYPE (else_)))
5530 : 356292 : TREE_OPERAND (expr, 2) = build2 (INIT_EXPR, type, tmp, else_);
5531 : :
5532 : 366921 : TREE_TYPE (expr) = void_type_node;
5533 : 366921 : recalculate_side_effects (expr);
5534 : :
5535 : : /* Move the COND_EXPR to the prequeue. */
5536 : 366921 : gimplify_stmt (&expr, pre_p);
5537 : :
5538 : 366921 : *expr_p = result;
5539 : 366921 : return GS_ALL_DONE;
5540 : : }
5541 : :
5542 : : /* Remove any COMPOUND_EXPR so the following cases will be caught. */
5543 : 5742480 : STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
5544 : 5727997 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
5545 : 79895 : gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
5546 : :
5547 : : /* Make sure the condition has BOOLEAN_TYPE. */
5548 : 5727997 : TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
5549 : :
5550 : : /* Break apart && and || conditions. */
5551 : 5727997 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
5552 : 5727997 : || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
5553 : : {
5554 : 352662 : expr = shortcut_cond_expr (expr, next_cond_uid ());
5555 : :
5556 : 352662 : if (expr != *expr_p)
5557 : : {
5558 : 352662 : *expr_p = expr;
5559 : :
5560 : : /* We can't rely on gimplify_expr to re-gimplify the expanded
5561 : : form properly, as cleanups might cause the target labels to be
5562 : : wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
5563 : : set up a conditional context. */
5564 : 352662 : gimple_push_condition ();
5565 : 352662 : gimplify_stmt (expr_p, &seq);
5566 : 352662 : gimple_pop_condition (pre_p);
5567 : 352662 : gimple_seq_add_seq (pre_p, seq);
5568 : :
5569 : 352662 : return GS_ALL_DONE;
5570 : : }
5571 : : }
5572 : :
5573 : : /* Now do the normal gimplification. */
5574 : :
5575 : : /* Gimplify condition. */
5576 : 5375335 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
5577 : : is_gimple_condexpr_for_cond, fb_rvalue);
5578 : 5375335 : if (ret == GS_ERROR)
5579 : : return GS_ERROR;
5580 : 5374578 : gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
5581 : :
5582 : 5374578 : gimple_push_condition ();
5583 : :
5584 : 5374578 : have_then_clause_p = have_else_clause_p = false;
5585 : 5374578 : label_true = find_goto_label (TREE_OPERAND (expr, 1));
5586 : 5374578 : if (label_true
5587 : 1251594 : && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
5588 : : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5589 : : have different locations, otherwise we end up with incorrect
5590 : : location information on the branches. */
5591 : 6626172 : && (optimize
5592 : 223667 : || !EXPR_HAS_LOCATION (expr)
5593 : 196967 : || !rexpr_has_location (label_true)
5594 : 68248 : || EXPR_LOCATION (expr) == rexpr_location (label_true)))
5595 : : {
5596 : 1250704 : have_then_clause_p = true;
5597 : 1250704 : label_true = GOTO_DESTINATION (label_true);
5598 : : }
5599 : : else
5600 : 4123874 : label_true = create_artificial_label (UNKNOWN_LOCATION);
5601 : 5374578 : label_false = find_goto_label (TREE_OPERAND (expr, 2));
5602 : 5374578 : if (label_false
5603 : 704679 : && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
5604 : : /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
5605 : : have different locations, otherwise we end up with incorrect
5606 : : location information on the branches. */
5607 : 6079257 : && (optimize
5608 : 98577 : || !EXPR_HAS_LOCATION (expr)
5609 : 97728 : || !rexpr_has_location (label_false)
5610 : 150 : || EXPR_LOCATION (expr) == rexpr_location (label_false)))
5611 : : {
5612 : 704541 : have_else_clause_p = true;
5613 : 704541 : label_false = GOTO_DESTINATION (label_false);
5614 : : }
5615 : : else
5616 : 4670037 : label_false = create_artificial_label (UNKNOWN_LOCATION);
5617 : :
5618 : 5374578 : unsigned cond_uid = 0;
5619 : 5374578 : if (cond_uids)
5620 : 455 : if (unsigned *v = cond_uids->get (expr))
5621 : 435 : cond_uid = *v;
5622 : 435 : if (cond_uid == 0)
5623 : 5374143 : cond_uid = next_cond_uid ();
5624 : :
5625 : 5374578 : gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
5626 : : &arm2);
5627 : 5374578 : cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
5628 : : label_false);
5629 : 5374578 : gimple_set_location (cond_stmt, EXPR_LOCATION (expr));
5630 : 5374578 : gimple_associate_condition_with_expr (cfun, cond_stmt, cond_uid);
5631 : 5374578 : copy_warning (cond_stmt, COND_EXPR_COND (expr));
5632 : 5374578 : gimplify_seq_add_stmt (&seq, cond_stmt);
5633 : 5374578 : gimple_stmt_iterator gsi = gsi_last (seq);
5634 : 5374578 : maybe_fold_stmt (&gsi);
5635 : :
5636 : 5374578 : label_cont = NULL_TREE;
5637 : 5374578 : if (!have_then_clause_p)
5638 : : {
5639 : : /* For if (...) {} else { code; } put label_true after
5640 : : the else block. */
5641 : 4123874 : if (TREE_OPERAND (expr, 1) == NULL_TREE
5642 : 190070 : && !have_else_clause_p
5643 : 4124543 : && TREE_OPERAND (expr, 2) != NULL_TREE)
5644 : : {
5645 : : /* For if (0) {} else { code; } tell -Wimplicit-fallthrough
5646 : : handling that label_cont == label_true can be only reached
5647 : : through fallthrough from { code; }. */
5648 : 669 : if (integer_zerop (COND_EXPR_COND (expr)))
5649 : 0 : UNUSED_LABEL_P (label_true) = 1;
5650 : : label_cont = label_true;
5651 : : }
5652 : : else
5653 : : {
5654 : 4123205 : bool then_side_effects
5655 : 4123205 : = (TREE_OPERAND (expr, 1)
5656 : 4123205 : && TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)));
5657 : 4123205 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
5658 : 4123205 : have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
5659 : : /* For if (...) { code; } else {} or
5660 : : if (...) { code; } else goto label; or
5661 : : if (...) { code; return; } else { ... }
5662 : : label_cont isn't needed. */
5663 : 4123205 : if (!have_else_clause_p
5664 : 3932649 : && TREE_OPERAND (expr, 2) != NULL_TREE
5665 : 6853946 : && gimple_seq_may_fallthru (seq))
5666 : : {
5667 : 1821641 : gimple *g;
5668 : 1821641 : label_cont = create_artificial_label (UNKNOWN_LOCATION);
5669 : :
5670 : : /* For if (0) { non-side-effect-code } else { code }
5671 : : tell -Wimplicit-fallthrough handling that label_cont can
5672 : : be only reached through fallthrough from { code }. */
5673 : 1821641 : if (integer_zerop (COND_EXPR_COND (expr)))
5674 : : {
5675 : 65699 : UNUSED_LABEL_P (label_true) = 1;
5676 : 65699 : if (!then_side_effects)
5677 : 11341 : UNUSED_LABEL_P (label_cont) = 1;
5678 : : }
5679 : :
5680 : 1821641 : g = gimple_build_goto (label_cont);
5681 : :
5682 : : /* GIMPLE_COND's are very low level; they have embedded
5683 : : gotos. This particular embedded goto should not be marked
5684 : : with the location of the original COND_EXPR, as it would
5685 : : correspond to the COND_EXPR's condition, not the ELSE or the
5686 : : THEN arms. To avoid marking it with the wrong location, flag
5687 : : it as "no location". */
5688 : 1821641 : gimple_set_do_not_emit_location (g);
5689 : :
5690 : 1821641 : gimplify_seq_add_stmt (&seq, g);
5691 : : }
5692 : : }
5693 : : }
5694 : 5374578 : if (!have_else_clause_p)
5695 : : {
5696 : : /* For if (1) { code } or if (1) { code } else { non-side-effect-code }
5697 : : tell -Wimplicit-fallthrough handling that label_false can be only
5698 : : reached through fallthrough from { code }. */
5699 : 4670037 : if (integer_nonzerop (COND_EXPR_COND (expr))
5700 : 4670037 : && (TREE_OPERAND (expr, 2) == NULL_TREE
5701 : 55919 : || !TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2))))
5702 : 6604 : UNUSED_LABEL_P (label_false) = 1;
5703 : 4670037 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
5704 : 4670037 : have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
5705 : : }
5706 : 5374578 : if (label_cont)
5707 : 1822310 : gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
5708 : :
5709 : 5374578 : gimple_pop_condition (pre_p);
5710 : 5374578 : gimple_seq_add_seq (pre_p, seq);
5711 : :
5712 : 5374578 : if (ret == GS_ERROR)
5713 : : ; /* Do nothing. */
5714 : 5374578 : else if (have_then_clause_p || have_else_clause_p)
5715 : : ret = GS_ALL_DONE;
5716 : : else
5717 : : {
5718 : : /* Both arms are empty; replace the COND_EXPR with its predicate. */
5719 : 2669 : expr = TREE_OPERAND (expr, 0);
5720 : 2669 : gimplify_stmt (&expr, pre_p);
5721 : : }
5722 : :
5723 : 5374578 : *expr_p = NULL;
5724 : 5374578 : return ret;
5725 : : }
5726 : :
5727 : : /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
5728 : : to be marked addressable.
5729 : :
5730 : : We cannot rely on such an expression being directly markable if a temporary
5731 : : has been created by the gimplification. In this case, we create another
5732 : : temporary and initialize it with a copy, which will become a store after we
5733 : : mark it addressable. This can happen if the front-end passed us something
5734 : : that it could not mark addressable yet, like a Fortran pass-by-reference
5735 : : parameter (int) floatvar. */
5736 : :
5737 : : static void
5738 : 55315531 : prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
5739 : : {
5740 : 60560882 : while (handled_component_p (*expr_p))
5741 : 5245351 : expr_p = &TREE_OPERAND (*expr_p, 0);
5742 : :
5743 : : /* Do not allow an SSA name as the temporary. */
5744 : 55315531 : if (is_gimple_reg (*expr_p))
5745 : 12089 : *expr_p = internal_get_tmp_var (*expr_p, seq_p, NULL, false, false, true);
5746 : 55315531 : }
5747 : :
5748 : : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5749 : : a call to __builtin_memcpy. */
5750 : :
5751 : : static enum gimplify_status
5752 : 1074 : gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
5753 : : gimple_seq *seq_p)
5754 : : {
5755 : 1074 : tree t, to, to_ptr, from, from_ptr;
5756 : 1074 : gcall *gs;
5757 : 1074 : location_t loc = EXPR_LOCATION (*expr_p);
5758 : :
5759 : 1074 : to = TREE_OPERAND (*expr_p, 0);
5760 : 1074 : from = TREE_OPERAND (*expr_p, 1);
5761 : 1074 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to)))
5762 : : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))));
5763 : :
5764 : : /* Mark the RHS addressable. Beware that it may not be possible to do so
5765 : : directly if a temporary has been created by the gimplification. */
5766 : 1074 : prepare_gimple_addressable (&from, seq_p);
5767 : :
5768 : 1074 : mark_addressable (from);
5769 : 1074 : from_ptr = build_fold_addr_expr_loc (loc, from);
5770 : 1074 : gimplify_arg (&from_ptr, seq_p, loc);
5771 : :
5772 : 1074 : mark_addressable (to);
5773 : 1074 : to_ptr = build_fold_addr_expr_loc (loc, to);
5774 : 1074 : gimplify_arg (&to_ptr, seq_p, loc);
5775 : :
5776 : 1074 : t = builtin_decl_implicit (BUILT_IN_MEMCPY);
5777 : :
5778 : 1074 : gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
5779 : 1074 : gimple_call_set_alloca_for_var (gs, true);
5780 : :
5781 : 1074 : if (want_value)
5782 : : {
5783 : : /* tmp = memcpy() */
5784 : 8 : t = create_tmp_var (TREE_TYPE (to_ptr));
5785 : 8 : gimple_call_set_lhs (gs, t);
5786 : 8 : gimplify_seq_add_stmt (seq_p, gs);
5787 : :
5788 : 8 : *expr_p = build_simple_mem_ref (t);
5789 : 8 : return GS_ALL_DONE;
5790 : : }
5791 : :
5792 : 1066 : gimplify_seq_add_stmt (seq_p, gs);
5793 : 1066 : *expr_p = NULL;
5794 : 1066 : return GS_ALL_DONE;
5795 : : }
5796 : :
5797 : : /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
5798 : : a call to __builtin_memset. In this case we know that the RHS is
5799 : : a CONSTRUCTOR with an empty element list. */
5800 : :
5801 : : static enum gimplify_status
5802 : 66 : gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
5803 : : gimple_seq *seq_p)
5804 : : {
5805 : 66 : tree t, from, to, to_ptr;
5806 : 66 : gcall *gs;
5807 : 66 : location_t loc = EXPR_LOCATION (*expr_p);
5808 : :
5809 : : /* Assert our assumptions, to abort instead of producing wrong code
5810 : : silently if they are not met. Beware that the RHS CONSTRUCTOR might
5811 : : not be immediately exposed. */
5812 : 66 : from = TREE_OPERAND (*expr_p, 1);
5813 : 66 : if (TREE_CODE (from) == WITH_SIZE_EXPR)
5814 : 66 : from = TREE_OPERAND (from, 0);
5815 : :
5816 : 66 : gcc_assert (TREE_CODE (from) == CONSTRUCTOR
5817 : : && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
5818 : :
5819 : : /* Now proceed. */
5820 : 66 : to = TREE_OPERAND (*expr_p, 0);
5821 : 66 : gcc_assert (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (to))));
5822 : :
5823 : 66 : to_ptr = build_fold_addr_expr_loc (loc, to);
5824 : 66 : gimplify_arg (&to_ptr, seq_p, loc);
5825 : 66 : t = builtin_decl_implicit (BUILT_IN_MEMSET);
5826 : :
5827 : 66 : gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
5828 : :
5829 : 66 : if (want_value)
5830 : : {
5831 : : /* tmp = memset() */
5832 : 0 : t = create_tmp_var (TREE_TYPE (to_ptr));
5833 : 0 : gimple_call_set_lhs (gs, t);
5834 : 0 : gimplify_seq_add_stmt (seq_p, gs);
5835 : :
5836 : 0 : *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
5837 : 0 : return GS_ALL_DONE;
5838 : : }
5839 : :
5840 : 66 : gimplify_seq_add_stmt (seq_p, gs);
5841 : 66 : *expr_p = NULL;
5842 : 66 : return GS_ALL_DONE;
5843 : : }
5844 : :
5845 : : /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
5846 : : determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
5847 : : assignment. Return non-null if we detect a potential overlap. */
5848 : :
5849 : : struct gimplify_init_ctor_preeval_data
5850 : : {
5851 : : /* The base decl of the lhs object. May be NULL, in which case we
5852 : : have to assume the lhs is indirect. */
5853 : : tree lhs_base_decl;
5854 : :
5855 : : /* The alias set of the lhs object. */
5856 : : alias_set_type lhs_alias_set;
5857 : : };
5858 : :
5859 : : static tree
5860 : 115218 : gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
5861 : : {
5862 : 115218 : struct gimplify_init_ctor_preeval_data *data
5863 : : = (struct gimplify_init_ctor_preeval_data *) xdata;
5864 : 115218 : tree t = *tp;
5865 : :
5866 : : /* If we find the base object, obviously we have overlap. */
5867 : 115218 : if (data->lhs_base_decl == t)
5868 : : return t;
5869 : :
5870 : : /* If the constructor component is indirect, determine if we have a
5871 : : potential overlap with the lhs. The only bits of information we
5872 : : have to go on at this point are addressability and alias sets. */
5873 : 115217 : if ((INDIRECT_REF_P (t)
5874 : 115217 : || TREE_CODE (t) == MEM_REF)
5875 : 643 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5876 : 115729 : && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
5877 : : return t;
5878 : :
5879 : : /* If the constructor component is a call, determine if it can hide a
5880 : : potential overlap with the lhs through an INDIRECT_REF like above.
5881 : : ??? Ugh - this is completely broken. In fact this whole analysis
5882 : : doesn't look conservative. */
5883 : 114705 : if (TREE_CODE (t) == CALL_EXPR)
5884 : : {
5885 : 0 : tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
5886 : :
5887 : 0 : for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
5888 : 0 : if (POINTER_TYPE_P (TREE_VALUE (type))
5889 : 0 : && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
5890 : 0 : && alias_sets_conflict_p (data->lhs_alias_set,
5891 : : get_alias_set
5892 : 0 : (TREE_TYPE (TREE_VALUE (type)))))
5893 : : return t;
5894 : : }
5895 : :
5896 : 114705 : if (IS_TYPE_OR_DECL_P (t))
5897 : 10964 : *walk_subtrees = 0;
5898 : : return NULL;
5899 : : }
5900 : :
5901 : : /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
5902 : : force values that overlap with the lhs (as described by *DATA)
5903 : : into temporaries. */
5904 : :
5905 : : static void
5906 : 407974 : gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5907 : : struct gimplify_init_ctor_preeval_data *data)
5908 : : {
5909 : 407974 : enum gimplify_status one;
5910 : :
5911 : : /* If the value is constant, then there's nothing to pre-evaluate. */
5912 : 407974 : if (TREE_CONSTANT (*expr_p))
5913 : : {
5914 : : /* Ensure it does not have side effects, it might contain a reference to
5915 : : the object we're initializing. */
5916 : 191880 : gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
5917 : : return;
5918 : : }
5919 : :
5920 : : /* If the type has non-trivial constructors, we can't pre-evaluate. */
5921 : 216094 : if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
5922 : : return;
5923 : :
5924 : : /* Recurse for nested constructors. */
5925 : 216094 : if (TREE_CODE (*expr_p) == CONSTRUCTOR)
5926 : : {
5927 : 73189 : unsigned HOST_WIDE_INT ix;
5928 : 73189 : constructor_elt *ce;
5929 : 73189 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
5930 : :
5931 : 269343 : FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
5932 : 196154 : gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
5933 : :
5934 : : return;
5935 : : }
5936 : :
5937 : : /* If this is a variable sized type, we must remember the size. */
5938 : 142905 : maybe_with_size_expr (expr_p);
5939 : :
5940 : : /* Gimplify the constructor element to something appropriate for the rhs
5941 : : of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
5942 : : the gimplifier will consider this a store to memory. Doing this
5943 : : gimplification now means that we won't have to deal with complicated
5944 : : language-specific trees, nor trees like SAVE_EXPR that can induce
5945 : : exponential search behavior. */
5946 : 142905 : one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
5947 : 142905 : if (one == GS_ERROR)
5948 : : {
5949 : 0 : *expr_p = NULL;
5950 : 0 : return;
5951 : : }
5952 : :
5953 : : /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
5954 : : with the lhs, since "a = { .x=a }" doesn't make sense. This will
5955 : : always be true for all scalars, since is_gimple_mem_rhs insists on a
5956 : : temporary variable for them. */
5957 : 142905 : if (DECL_P (*expr_p))
5958 : : return;
5959 : :
5960 : : /* If this is of variable size, we have no choice but to assume it doesn't
5961 : : overlap since we can't make a temporary for it. */
5962 : 103674 : if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
5963 : : return;
5964 : :
5965 : : /* Otherwise, we must search for overlap ... */
5966 : 103674 : if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
5967 : : return;
5968 : :
5969 : : /* ... and if found, force the value into a temporary. */
5970 : 513 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
5971 : : }
5972 : :
5973 : : /* A subroutine of gimplify_init_ctor_eval. Create a loop for
5974 : : a RANGE_EXPR in a CONSTRUCTOR for an array.
5975 : :
5976 : : var = lower;
5977 : : loop_entry:
5978 : : object[var] = value;
5979 : : if (var == upper)
5980 : : goto loop_exit;
5981 : : var = var + 1;
5982 : : goto loop_entry;
5983 : : loop_exit:
5984 : :
5985 : : We increment var _after_ the loop exit check because we might otherwise
5986 : : fail if upper == TYPE_MAX_VALUE (type for upper).
5987 : :
5988 : : Note that we never have to deal with SAVE_EXPRs here, because this has
5989 : : already been taken care of for us, in gimplify_init_ctor_preeval(). */
5990 : :
5991 : : static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
5992 : : gimple_seq *, bool);
5993 : :
5994 : : static void
5995 : 375 : gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
5996 : : tree value, tree array_elt_type,
5997 : : gimple_seq *pre_p, bool cleared)
5998 : : {
5999 : 375 : tree loop_entry_label, loop_exit_label, fall_thru_label;
6000 : 375 : tree var, var_type, cref, tmp;
6001 : :
6002 : 375 : loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
6003 : 375 : loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
6004 : 375 : fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
6005 : :
6006 : : /* Create and initialize the index variable. */
6007 : 375 : var_type = TREE_TYPE (upper);
6008 : 375 : var = create_tmp_var (var_type);
6009 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
6010 : :
6011 : : /* Add the loop entry label. */
6012 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
6013 : :
6014 : : /* Build the reference. */
6015 : 375 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6016 : : var, NULL_TREE, NULL_TREE);
6017 : :
6018 : : /* If we are a constructor, just call gimplify_init_ctor_eval to do
6019 : : the store. Otherwise just assign value to the reference. */
6020 : :
6021 : 375 : if (TREE_CODE (value) == CONSTRUCTOR)
6022 : : /* NB we might have to call ourself recursively through
6023 : : gimplify_init_ctor_eval if the value is a constructor. */
6024 : 85 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6025 : : pre_p, cleared);
6026 : : else
6027 : : {
6028 : 290 : if (gimplify_expr (&value, pre_p, NULL, is_gimple_val, fb_rvalue)
6029 : : != GS_ERROR)
6030 : 290 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
6031 : : }
6032 : :
6033 : : /* We exit the loop when the index var is equal to the upper bound. */
6034 : 750 : gimplify_seq_add_stmt (pre_p,
6035 : 375 : gimple_build_cond (EQ_EXPR, var, upper,
6036 : : loop_exit_label, fall_thru_label));
6037 : :
6038 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
6039 : :
6040 : : /* Otherwise, increment the index var... */
6041 : 375 : tmp = build2 (PLUS_EXPR, var_type, var,
6042 : : fold_convert (var_type, integer_one_node));
6043 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
6044 : :
6045 : : /* ...and jump back to the loop entry. */
6046 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
6047 : :
6048 : : /* Add the loop exit label. */
6049 : 375 : gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
6050 : 375 : }
6051 : :
6052 : : /* A subroutine of gimplify_init_constructor. Generate individual
6053 : : MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
6054 : : assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
6055 : : CONSTRUCTOR. CLEARED is true if the entire LHS object has been
6056 : : zeroed first. */
6057 : :
6058 : : static void
6059 : 1230930 : gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
6060 : : gimple_seq *pre_p, bool cleared)
6061 : : {
6062 : 1230930 : tree array_elt_type = NULL;
6063 : 1230930 : unsigned HOST_WIDE_INT ix;
6064 : 1230930 : tree purpose, value;
6065 : :
6066 : 1230930 : if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
6067 : 135629 : array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
6068 : :
6069 : 4556010 : FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
6070 : : {
6071 : 3325080 : tree cref;
6072 : :
6073 : : /* NULL values are created above for gimplification errors. */
6074 : 3325080 : if (value == NULL)
6075 : 364825 : continue;
6076 : :
6077 : 3325080 : if (cleared && initializer_zerop (value))
6078 : 364287 : continue;
6079 : :
6080 : : /* ??? Here's to hoping the front end fills in all of the indices,
6081 : : so we don't have to figure out what's missing ourselves. */
6082 : 2960793 : gcc_assert (purpose);
6083 : :
6084 : : /* Skip zero-sized fields, unless value has side-effects. This can
6085 : : happen with calls to functions returning a empty type, which
6086 : : we shouldn't discard. As a number of downstream passes don't
6087 : : expect sets of empty type fields, we rely on the gimplification of
6088 : : the MODIFY_EXPR we make below to drop the assignment statement. */
6089 : 2960793 : if (!TREE_SIDE_EFFECTS (value)
6090 : 2668992 : && TREE_CODE (purpose) == FIELD_DECL
6091 : 5236209 : && is_empty_type (TREE_TYPE (purpose)))
6092 : 163 : continue;
6093 : :
6094 : : /* If we have a RANGE_EXPR, we have to build a loop to assign the
6095 : : whole range. */
6096 : 2960630 : if (TREE_CODE (purpose) == RANGE_EXPR)
6097 : : {
6098 : 385 : tree lower = TREE_OPERAND (purpose, 0);
6099 : 385 : tree upper = TREE_OPERAND (purpose, 1);
6100 : :
6101 : : /* If the lower bound is equal to upper, just treat it as if
6102 : : upper was the index. */
6103 : 385 : if (simple_cst_equal (lower, upper))
6104 : : purpose = upper;
6105 : : else
6106 : : {
6107 : 375 : gimplify_init_ctor_eval_range (object, lower, upper, value,
6108 : : array_elt_type, pre_p, cleared);
6109 : 375 : continue;
6110 : : }
6111 : : }
6112 : :
6113 : 2960255 : if (array_elt_type)
6114 : : {
6115 : : /* Do not use bitsizetype for ARRAY_REF indices. */
6116 : 483950 : if (TYPE_DOMAIN (TREE_TYPE (object)))
6117 : 483950 : purpose
6118 : 483950 : = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
6119 : : purpose);
6120 : 483950 : cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
6121 : : purpose, NULL_TREE, NULL_TREE);
6122 : : }
6123 : : else
6124 : : {
6125 : 2476305 : gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
6126 : 2476305 : cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
6127 : : unshare_expr (object), purpose, NULL_TREE);
6128 : : }
6129 : :
6130 : 2960255 : if (TREE_CODE (value) == CONSTRUCTOR
6131 : 2960255 : && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
6132 : 291473 : gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
6133 : : pre_p, cleared);
6134 : 2668782 : else if (TREE_CODE (value) == RAW_DATA_CST)
6135 : : {
6136 : 31 : if (RAW_DATA_LENGTH (value) <= 32)
6137 : : {
6138 : 13 : for (unsigned int i = 0; i < (unsigned) RAW_DATA_LENGTH (value);
6139 : : ++i)
6140 : 12 : if (!cleared || RAW_DATA_POINTER (value)[i])
6141 : : {
6142 : 12 : if (i)
6143 : : {
6144 : 11 : tree p
6145 : 11 : = fold_build2 (PLUS_EXPR, TREE_TYPE (purpose),
6146 : : purpose,
6147 : : build_int_cst (TREE_TYPE (purpose),
6148 : : i));
6149 : 11 : cref = build4 (ARRAY_REF, array_elt_type,
6150 : : unshare_expr (object), p, NULL_TREE,
6151 : : NULL_TREE);
6152 : : }
6153 : 12 : tree init
6154 : 12 : = build2 (INIT_EXPR, TREE_TYPE (cref), cref,
6155 : 12 : build_int_cst (TREE_TYPE (value),
6156 : 12 : RAW_DATA_UCHAR_ELT (value, i)));
6157 : 12 : gimplify_and_add (init, pre_p);
6158 : 12 : ggc_free (init);
6159 : : }
6160 : : }
6161 : : else
6162 : : {
6163 : 30 : tree rtype = build_array_type_nelts (TREE_TYPE (value),
6164 : 30 : RAW_DATA_LENGTH (value));
6165 : 30 : tree rctor = build_constructor_single (rtype, bitsize_zero_node,
6166 : : value);
6167 : 30 : tree addr = build_fold_addr_expr (cref);
6168 : 30 : cref = build2 (MEM_REF, rtype, addr,
6169 : : build_int_cst (ptr_type_node, 0));
6170 : 30 : rctor = tree_output_constant_def (rctor);
6171 : 30 : if (!useless_type_conversion_p (rtype, TREE_TYPE (rctor)))
6172 : 4 : rctor = build1 (VIEW_CONVERT_EXPR, rtype, rctor);
6173 : 30 : if (gimplify_expr (&cref, pre_p, NULL, is_gimple_lvalue,
6174 : : fb_lvalue) != GS_ERROR)
6175 : 60 : gimplify_seq_add_stmt (pre_p,
6176 : 30 : gimple_build_assign (cref, rctor));
6177 : : }
6178 : : }
6179 : : else
6180 : : {
6181 : 2668751 : tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
6182 : 2668751 : gimplify_and_add (init, pre_p);
6183 : 2668751 : ggc_free (init);
6184 : : }
6185 : : }
6186 : 1230930 : }
6187 : :
6188 : : /* Return the appropriate RHS predicate for this LHS. */
6189 : :
6190 : : gimple_predicate
6191 : 48003976 : rhs_predicate_for (tree lhs)
6192 : : {
6193 : 48003976 : if (is_gimple_reg (lhs))
6194 : : return is_gimple_reg_rhs_or_call;
6195 : : else
6196 : 11347306 : return is_gimple_mem_rhs_or_call;
6197 : : }
6198 : :
6199 : : /* Return the initial guess for an appropriate RHS predicate for this LHS,
6200 : : before the LHS has been gimplified. */
6201 : :
6202 : : static gimple_predicate
6203 : 46819355 : initial_rhs_predicate_for (tree lhs)
6204 : : {
6205 : 46819355 : if (is_gimple_reg_type (TREE_TYPE (lhs)))
6206 : : return is_gimple_reg_rhs_or_call;
6207 : : else
6208 : 2394496 : return is_gimple_mem_rhs_or_call;
6209 : : }
6210 : :
6211 : : /* Gimplify a C99 compound literal expression. This just means adding
6212 : : the DECL_EXPR before the current statement and using its anonymous
6213 : : decl instead. */
6214 : :
6215 : : static enum gimplify_status
6216 : 36535 : gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
6217 : : bool (*gimple_test_f) (tree),
6218 : : fallback_t fallback)
6219 : : {
6220 : 36535 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
6221 : 36535 : tree decl = DECL_EXPR_DECL (decl_s);
6222 : 36535 : tree init = DECL_INITIAL (decl);
6223 : : /* Mark the decl as addressable if the compound literal
6224 : : expression is addressable now, otherwise it is marked too late
6225 : : after we gimplify the initialization expression. */
6226 : 36535 : if (TREE_ADDRESSABLE (*expr_p))
6227 : 495 : TREE_ADDRESSABLE (decl) = 1;
6228 : : /* Otherwise, if we don't need an lvalue and have a literal directly
6229 : : substitute it. Check if it matches the gimple predicate, as
6230 : : otherwise we'd generate a new temporary, and we can as well just
6231 : : use the decl we already have. */
6232 : 36040 : else if (!TREE_ADDRESSABLE (decl)
6233 : 36040 : && !TREE_THIS_VOLATILE (decl)
6234 : 36035 : && init
6235 : 36028 : && (fallback & fb_lvalue) == 0
6236 : 71953 : && gimple_test_f (init))
6237 : : {
6238 : 34861 : *expr_p = init;
6239 : 34861 : return GS_OK;
6240 : : }
6241 : :
6242 : : /* If the decl is not addressable, then it is being used in some
6243 : : expression or on the right hand side of a statement, and it can
6244 : : be put into a readonly data section. */
6245 : 1674 : if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
6246 : 1057 : TREE_READONLY (decl) = 1;
6247 : :
6248 : : /* This decl isn't mentioned in the enclosing block, so add it to the
6249 : : list of temps. FIXME it seems a bit of a kludge to say that
6250 : : anonymous artificial vars aren't pushed, but everything else is. */
6251 : 1674 : if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
6252 : 2 : gimple_add_tmp_var (decl);
6253 : :
6254 : 1674 : gimplify_and_add (decl_s, pre_p);
6255 : 1674 : *expr_p = decl;
6256 : 1674 : return GS_OK;
6257 : : }
6258 : :
6259 : : /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
6260 : : return a new CONSTRUCTOR if something changed. */
6261 : :
6262 : : static tree
6263 : 1823529 : optimize_compound_literals_in_ctor (tree orig_ctor)
6264 : : {
6265 : 1823529 : tree ctor = orig_ctor;
6266 : 1823529 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6267 : 1823529 : unsigned int idx, num = vec_safe_length (elts);
6268 : :
6269 : 6058582 : for (idx = 0; idx < num; idx++)
6270 : : {
6271 : 4235053 : tree value = (*elts)[idx].value;
6272 : 4235053 : tree newval = value;
6273 : 4235053 : if (TREE_CODE (value) == CONSTRUCTOR)
6274 : 520201 : newval = optimize_compound_literals_in_ctor (value);
6275 : 3714852 : else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
6276 : : {
6277 : 79 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
6278 : 79 : tree decl = DECL_EXPR_DECL (decl_s);
6279 : 79 : tree init = DECL_INITIAL (decl);
6280 : :
6281 : 79 : if (!TREE_ADDRESSABLE (value)
6282 : 79 : && !TREE_ADDRESSABLE (decl)
6283 : 79 : && init
6284 : 79 : && TREE_CODE (init) == CONSTRUCTOR)
6285 : 48 : newval = optimize_compound_literals_in_ctor (init);
6286 : : }
6287 : 4235053 : if (newval == value)
6288 : 4234997 : continue;
6289 : :
6290 : 56 : if (ctor == orig_ctor)
6291 : : {
6292 : 45 : ctor = copy_node (orig_ctor);
6293 : 90 : CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
6294 : 45 : elts = CONSTRUCTOR_ELTS (ctor);
6295 : : }
6296 : 56 : (*elts)[idx].value = newval;
6297 : : }
6298 : 1823529 : return ctor;
6299 : : }
6300 : :
6301 : : /* A subroutine of gimplify_modify_expr. Break out elements of a
6302 : : CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
6303 : :
6304 : : Note that we still need to clear any elements that don't have explicit
6305 : : initializers, so if not all elements are initialized we keep the
6306 : : original MODIFY_EXPR, we just remove all of the constructor elements.
6307 : :
6308 : : If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
6309 : : GS_ERROR if we would have to create a temporary when gimplifying
6310 : : this constructor. Otherwise, return GS_OK.
6311 : :
6312 : : If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
6313 : :
6314 : : static enum gimplify_status
6315 : 1303280 : gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6316 : : bool want_value, bool notify_temp_creation)
6317 : : {
6318 : 1303280 : tree object, ctor, type;
6319 : 1303280 : enum gimplify_status ret;
6320 : 1303280 : vec<constructor_elt, va_gc> *elts;
6321 : 1303280 : bool cleared = false;
6322 : 1303280 : bool is_empty_ctor = false;
6323 : 1303280 : bool is_init_expr = (TREE_CODE (*expr_p) == INIT_EXPR);
6324 : :
6325 : 1303280 : gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
6326 : :
6327 : 1303280 : if (!notify_temp_creation)
6328 : : {
6329 : 1295790 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6330 : : is_gimple_lvalue, fb_lvalue);
6331 : 1295790 : if (ret == GS_ERROR)
6332 : : return ret;
6333 : : }
6334 : :
6335 : 1303280 : object = TREE_OPERAND (*expr_p, 0);
6336 : 1303280 : ctor = TREE_OPERAND (*expr_p, 1)
6337 : 1303280 : = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
6338 : 1303280 : type = TREE_TYPE (ctor);
6339 : 1303280 : elts = CONSTRUCTOR_ELTS (ctor);
6340 : 1303280 : ret = GS_ALL_DONE;
6341 : :
6342 : 1303280 : switch (TREE_CODE (type))
6343 : : {
6344 : 1296782 : case RECORD_TYPE:
6345 : 1296782 : case UNION_TYPE:
6346 : 1296782 : case QUAL_UNION_TYPE:
6347 : 1296782 : case ARRAY_TYPE:
6348 : 1296782 : {
6349 : : /* Use readonly data for initializers of this or smaller size
6350 : : regardless of the num_nonzero_elements / num_unique_nonzero_elements
6351 : : ratio. */
6352 : 1296782 : const HOST_WIDE_INT min_unique_size = 64;
6353 : : /* If num_nonzero_elements / num_unique_nonzero_elements ratio
6354 : : is smaller than this, use readonly data. */
6355 : 1296782 : const int unique_nonzero_ratio = 8;
6356 : : /* True if a single access of the object must be ensured. This is the
6357 : : case if the target is volatile, the type is non-addressable and more
6358 : : than one field need to be assigned. */
6359 : 1296782 : const bool ensure_single_access
6360 : 1296782 : = TREE_THIS_VOLATILE (object)
6361 : 222 : && !TREE_ADDRESSABLE (type)
6362 : 1296970 : && vec_safe_length (elts) > 1;
6363 : 1296782 : struct gimplify_init_ctor_preeval_data preeval_data;
6364 : 1296782 : HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
6365 : 1296782 : HOST_WIDE_INT num_unique_nonzero_elements;
6366 : 1296782 : int complete_p;
6367 : 1296782 : bool valid_const_initializer;
6368 : :
6369 : : /* Aggregate types must lower constructors to initialization of
6370 : : individual elements. The exception is that a CONSTRUCTOR node
6371 : : with no elements indicates zero-initialization of the whole. */
6372 : 1296782 : if (vec_safe_is_empty (elts))
6373 : : {
6374 : 330224 : if (notify_temp_creation)
6375 : 9006 : return GS_OK;
6376 : :
6377 : : /* The var will be initialized and so appear on lhs of
6378 : : assignment, it can't be TREE_READONLY anymore. */
6379 : 330176 : if (VAR_P (object))
6380 : 189412 : TREE_READONLY (object) = 0;
6381 : :
6382 : : is_empty_ctor = true;
6383 : 331928 : break;
6384 : : }
6385 : :
6386 : : /* Fetch information about the constructor to direct later processing.
6387 : : We might want to make static versions of it in various cases, and
6388 : : can only do so if it known to be a valid constant initializer. */
6389 : 966558 : valid_const_initializer
6390 : 966558 : = categorize_ctor_elements (ctor, &num_nonzero_elements,
6391 : : &num_unique_nonzero_elements,
6392 : : &num_ctor_elements, &complete_p);
6393 : :
6394 : : /* If a const aggregate variable is being initialized, then it
6395 : : should never be a lose to promote the variable to be static. */
6396 : 966558 : if (valid_const_initializer
6397 : 591556 : && num_nonzero_elements > 1
6398 : 442490 : && TREE_READONLY (object)
6399 : 4065 : && VAR_P (object)
6400 : 3679 : && !DECL_REGISTER (object)
6401 : 3672 : && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)
6402 : 3477 : || DECL_MERGEABLE (object))
6403 : : /* For ctors that have many repeated nonzero elements
6404 : : represented through RANGE_EXPRs, prefer initializing
6405 : : those through runtime loops over copies of large amounts
6406 : : of data from readonly data section. */
6407 : 966558 : && (num_unique_nonzero_elements
6408 : 1752 : > num_nonzero_elements / unique_nonzero_ratio
6409 : 0 : || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
6410 : : <= (unsigned HOST_WIDE_INT) min_unique_size)))
6411 : : {
6412 : 1752 : if (notify_temp_creation)
6413 : : return GS_ERROR;
6414 : :
6415 : 1752 : DECL_INITIAL (object) = ctor;
6416 : 1752 : TREE_STATIC (object) = 1;
6417 : 1752 : if (!DECL_NAME (object) || DECL_NAMELESS (object))
6418 : 1572 : DECL_NAME (object) = create_tmp_var_name ("C");
6419 : 1752 : walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
6420 : :
6421 : : /* ??? C++ doesn't automatically append a .<number> to the
6422 : : assembler name, and even when it does, it looks at FE private
6423 : : data structures to figure out what that number should be,
6424 : : which are not set for this variable. I suppose this is
6425 : : important for local statics for inline functions, which aren't
6426 : : "local" in the object file sense. So in order to get a unique
6427 : : TU-local symbol, we must invoke the lhd version now. */
6428 : 1752 : lhd_set_decl_assembler_name (object);
6429 : :
6430 : 1752 : *expr_p = NULL_TREE;
6431 : 1752 : break;
6432 : : }
6433 : :
6434 : : /* The var will be initialized and so appear on lhs of
6435 : : assignment, it can't be TREE_READONLY anymore. */
6436 : 964806 : if (VAR_P (object) && !notify_temp_creation)
6437 : 766577 : TREE_READONLY (object) = 0;
6438 : :
6439 : : /* If there are "lots" of initialized elements, even discounting
6440 : : those that are not address constants (and thus *must* be
6441 : : computed at runtime), then partition the constructor into
6442 : : constant and non-constant parts. Block copy the constant
6443 : : parts in, then generate code for the non-constant parts. */
6444 : : /* TODO. There's code in cp/typeck.cc to do this. */
6445 : :
6446 : 964806 : if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
6447 : : /* store_constructor will ignore the clearing of variable-sized
6448 : : objects. Initializers for such objects must explicitly set
6449 : : every field that needs to be set. */
6450 : : cleared = false;
6451 : 964789 : else if (!complete_p)
6452 : : /* If the constructor isn't complete, clear the whole object
6453 : : beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
6454 : :
6455 : : ??? This ought not to be needed. For any element not present
6456 : : in the initializer, we should simply set them to zero. Except
6457 : : we'd need to *find* the elements that are not present, and that
6458 : : requires trickery to avoid quadratic compile-time behavior in
6459 : : large cases or excessive memory use in small cases. */
6460 : 177403 : cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
6461 : 787386 : else if (num_ctor_elements - num_nonzero_elements
6462 : 787386 : > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
6463 : 787386 : && num_nonzero_elements < num_ctor_elements / 4)
6464 : : /* If there are "lots" of zeros, it's more efficient to clear
6465 : : the memory and then set the nonzero elements. */
6466 : : cleared = true;
6467 : 785471 : else if (ensure_single_access && num_nonzero_elements == 0)
6468 : : /* If a single access to the target must be ensured and all elements
6469 : : are zero, then it's optimal to clear whatever their number. */
6470 : : cleared = true;
6471 : : /* If the object is small enough to go in registers, and it's
6472 : : not required to be constructed in memory, clear it first.
6473 : : That will avoid wasting cycles preserving any padding bits
6474 : : that might be there, and if there aren't any, the compiler
6475 : : is smart enough to optimize the clearing out. */
6476 : 785466 : else if (complete_p <= 0
6477 : 13989 : && !TREE_ADDRESSABLE (ctor)
6478 : 13973 : && !TREE_THIS_VOLATILE (object)
6479 : 13970 : && (TYPE_MODE (type) != BLKmode || TYPE_NO_FORCE_BLK (type))
6480 : 792695 : && optimize)
6481 : : cleared = true;
6482 : : else
6483 : : cleared = false;
6484 : :
6485 : : /* If there are "lots" of initialized elements, and all of them
6486 : : are valid address constants, then the entire initializer can
6487 : : be dropped to memory, and then memcpy'd out. Don't do this
6488 : : for sparse arrays, though, as it's more efficient to follow
6489 : : the standard CONSTRUCTOR behavior of memset followed by
6490 : : individual element initialization. Also don't do this for small
6491 : : all-zero initializers (which aren't big enough to merit
6492 : : clearing), and don't try to make bitwise copies of
6493 : : TREE_ADDRESSABLE types. */
6494 : 964806 : if (valid_const_initializer
6495 : 589804 : && complete_p
6496 : 427020 : && !(cleared || num_nonzero_elements == 0)
6497 : 334529 : && !TREE_ADDRESSABLE (type))
6498 : : {
6499 : 333501 : HOST_WIDE_INT size = int_size_in_bytes (type);
6500 : 333501 : unsigned int align;
6501 : :
6502 : : /* ??? We can still get unbounded array types, at least
6503 : : from the C++ front end. This seems wrong, but attempt
6504 : : to work around it for now. */
6505 : 333501 : if (size < 0)
6506 : : {
6507 : 8 : size = int_size_in_bytes (TREE_TYPE (object));
6508 : 8 : if (size >= 0)
6509 : 0 : TREE_TYPE (ctor) = type = TREE_TYPE (object);
6510 : : }
6511 : :
6512 : : /* Find the maximum alignment we can assume for the object. */
6513 : : /* ??? Make use of DECL_OFFSET_ALIGN. */
6514 : 333501 : if (DECL_P (object))
6515 : 320012 : align = DECL_ALIGN (object);
6516 : : else
6517 : 13489 : align = TYPE_ALIGN (type);
6518 : :
6519 : : /* Do a block move either if the size is so small as to make
6520 : : each individual move a sub-unit move on average, or if it
6521 : : is so large as to make individual moves inefficient. */
6522 : 333501 : if (size > 0
6523 : 333493 : && num_nonzero_elements > 1
6524 : : /* For ctors that have many repeated nonzero elements
6525 : : represented through RANGE_EXPRs, prefer initializing
6526 : : those through runtime loops over copies of large amounts
6527 : : of data from readonly data section. */
6528 : 303442 : && (num_unique_nonzero_elements
6529 : 303442 : > num_nonzero_elements / unique_nonzero_ratio
6530 : 41 : || size <= min_unique_size)
6531 : 636902 : && (size < num_nonzero_elements
6532 : 303323 : || !can_move_by_pieces (size, align)))
6533 : : {
6534 : 2730 : if (notify_temp_creation)
6535 : : return GS_ERROR;
6536 : :
6537 : 1365 : walk_tree (&ctor, force_labels_r, NULL, NULL);
6538 : 1365 : ctor = tree_output_constant_def (ctor);
6539 : 1365 : if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
6540 : 0 : ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
6541 : 1365 : TREE_OPERAND (*expr_p, 1) = ctor;
6542 : :
6543 : : /* This is no longer an assignment of a CONSTRUCTOR, but
6544 : : we still may have processing to do on the LHS. So
6545 : : pretend we didn't do anything here to let that happen. */
6546 : 1365 : return GS_UNHANDLED;
6547 : : }
6548 : : }
6549 : :
6550 : : /* If a single access to the target must be ensured and there are
6551 : : nonzero elements or the zero elements are not assigned en masse,
6552 : : initialize the target from a temporary. */
6553 : 962076 : if (ensure_single_access && (num_nonzero_elements > 0 || !cleared))
6554 : : {
6555 : 157 : if (notify_temp_creation)
6556 : : return GS_ERROR;
6557 : :
6558 : 151 : tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
6559 : 151 : TREE_OPERAND (*expr_p, 0) = temp;
6560 : 151 : *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
6561 : : *expr_p,
6562 : : build2 (MODIFY_EXPR, void_type_node,
6563 : : object, temp));
6564 : 151 : return GS_OK;
6565 : : }
6566 : :
6567 : 961919 : if (notify_temp_creation)
6568 : : return GS_OK;
6569 : :
6570 : : /* If there are nonzero elements and if needed, pre-evaluate to capture
6571 : : elements overlapping with the lhs into temporaries. We must do this
6572 : : before clearing to fetch the values before they are zeroed-out. */
6573 : 955848 : if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
6574 : : {
6575 : 211820 : preeval_data.lhs_base_decl = get_base_address (object);
6576 : 211820 : if (!DECL_P (preeval_data.lhs_base_decl))
6577 : 40622 : preeval_data.lhs_base_decl = NULL;
6578 : 211820 : preeval_data.lhs_alias_set = get_alias_set (object);
6579 : :
6580 : 211820 : gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
6581 : : pre_p, post_p, &preeval_data);
6582 : : }
6583 : :
6584 : 955848 : bool ctor_has_side_effects_p
6585 : 955848 : = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
6586 : :
6587 : 955848 : if (cleared)
6588 : : {
6589 : : /* Zap the CONSTRUCTOR element list, which simplifies this case.
6590 : : Note that we still have to gimplify, in order to handle the
6591 : : case of variable sized types. Avoid shared tree structures. */
6592 : 185396 : CONSTRUCTOR_ELTS (ctor) = NULL;
6593 : 185396 : TREE_SIDE_EFFECTS (ctor) = 0;
6594 : 185396 : object = unshare_expr (object);
6595 : 185396 : gimplify_stmt (expr_p, pre_p);
6596 : : }
6597 : :
6598 : : /* If we have not block cleared the object, or if there are nonzero
6599 : : elements in the constructor, or if the constructor has side effects,
6600 : : add assignments to the individual scalar fields of the object. */
6601 : 185396 : if (!cleared
6602 : 185396 : || num_nonzero_elements > 0
6603 : 16839 : || ctor_has_side_effects_p)
6604 : 939372 : gimplify_init_ctor_eval (object, elts, pre_p, cleared);
6605 : :
6606 : 955848 : *expr_p = NULL_TREE;
6607 : : }
6608 : 955848 : break;
6609 : :
6610 : 0 : case COMPLEX_TYPE:
6611 : 0 : {
6612 : 0 : tree r, i;
6613 : :
6614 : 0 : if (notify_temp_creation)
6615 : : return GS_OK;
6616 : :
6617 : : /* Extract the real and imaginary parts out of the ctor. */
6618 : 0 : gcc_assert (elts->length () == 2);
6619 : 0 : r = (*elts)[0].value;
6620 : 0 : i = (*elts)[1].value;
6621 : 0 : if (r == NULL || i == NULL)
6622 : : {
6623 : 0 : tree zero = build_zero_cst (TREE_TYPE (type));
6624 : 0 : if (r == NULL)
6625 : 0 : r = zero;
6626 : 0 : if (i == NULL)
6627 : 0 : i = zero;
6628 : : }
6629 : :
6630 : : /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
6631 : : represent creation of a complex value. */
6632 : 0 : if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
6633 : : {
6634 : 0 : ctor = build_complex (type, r, i);
6635 : 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6636 : : }
6637 : : else
6638 : : {
6639 : 0 : ctor = build2 (COMPLEX_EXPR, type, r, i);
6640 : 0 : TREE_OPERAND (*expr_p, 1) = ctor;
6641 : 0 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
6642 : : pre_p,
6643 : : post_p,
6644 : 0 : rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
6645 : : fb_rvalue);
6646 : : }
6647 : : }
6648 : : break;
6649 : :
6650 : 6498 : case VECTOR_TYPE:
6651 : 6498 : {
6652 : 6498 : unsigned HOST_WIDE_INT ix;
6653 : 6498 : constructor_elt *ce;
6654 : :
6655 : 6498 : if (notify_temp_creation)
6656 : 1303280 : return GS_OK;
6657 : :
6658 : : /* Vector types use CONSTRUCTOR all the way through gimple
6659 : : compilation as a general initializer. */
6660 : 59989 : FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
6661 : : {
6662 : 53491 : enum gimplify_status tret;
6663 : 53491 : tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
6664 : : fb_rvalue);
6665 : 53491 : if (tret == GS_ERROR)
6666 : : ret = GS_ERROR;
6667 : 53490 : else if (TREE_STATIC (ctor)
6668 : 53522 : && !initializer_constant_valid_p (ce->value,
6669 : 32 : TREE_TYPE (ce->value)))
6670 : 32 : TREE_STATIC (ctor) = 0;
6671 : : }
6672 : 6498 : recompute_constructor_flags (ctor);
6673 : :
6674 : : /* Go ahead and simplify constant constructors to VECTOR_CST. */
6675 : 6498 : if (TREE_CONSTANT (ctor))
6676 : : {
6677 : 697 : bool constant_p = true;
6678 : : tree value;
6679 : :
6680 : : /* Even when ctor is constant, it might contain non-*_CST
6681 : : elements, such as addresses or trapping values like
6682 : : 1.0/0.0 - 1.0/0.0. Such expressions don't belong
6683 : : in VECTOR_CST nodes. */
6684 : 697 : FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
6685 : 478 : if (!CONSTANT_CLASS_P (value))
6686 : : {
6687 : : constant_p = false;
6688 : : break;
6689 : : }
6690 : :
6691 : 219 : if (constant_p)
6692 : : {
6693 : 219 : TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
6694 : 219 : break;
6695 : : }
6696 : : }
6697 : :
6698 : 6279 : if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
6699 : 895 : TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
6700 : : }
6701 : : break;
6702 : :
6703 : 0 : default:
6704 : : /* So how did we get a CONSTRUCTOR for a scalar type? */
6705 : 0 : gcc_unreachable ();
6706 : : }
6707 : :
6708 : 1294274 : if (ret == GS_ERROR)
6709 : : return GS_ERROR;
6710 : : /* If we have gimplified both sides of the initializer but have
6711 : : not emitted an assignment, do so now. */
6712 : 1294273 : if (*expr_p
6713 : : /* If the type is an empty type, we don't need to emit the
6714 : : assignment. */
6715 : 1294273 : && !is_empty_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
6716 : : {
6717 : 332402 : tree lhs = TREE_OPERAND (*expr_p, 0);
6718 : 332402 : tree rhs = TREE_OPERAND (*expr_p, 1);
6719 : 332402 : if (want_value && object == lhs)
6720 : 12 : lhs = unshare_expr (lhs);
6721 : 332402 : gassign *init = gimple_build_assign (lhs, rhs);
6722 : 332402 : gimplify_seq_add_stmt (pre_p, init);
6723 : : }
6724 : 1294273 : if (want_value)
6725 : : {
6726 : 19 : *expr_p = object;
6727 : 19 : ret = GS_OK;
6728 : : }
6729 : : else
6730 : : {
6731 : 1294254 : *expr_p = NULL;
6732 : 1294254 : ret = GS_ALL_DONE;
6733 : : }
6734 : :
6735 : : /* If the user requests to initialize automatic variables, we
6736 : : should initialize paddings inside the variable. Add a call to
6737 : : __builtin_clear_pading (&object, 0, for_auto_init = true) to
6738 : : initialize paddings of object always to zero regardless of
6739 : : INIT_TYPE. Note, we will not insert this call if the aggregate
6740 : : variable has be completely cleared already or it's initialized
6741 : : with an empty constructor. We cannot insert this call if the
6742 : : variable is a gimple register since __builtin_clear_padding will take
6743 : : the address of the variable. As a result, if a long double/_Complex long
6744 : : double variable will be spilled into stack later, its padding cannot
6745 : : be cleared with __builtin_clear_padding. We should clear its padding
6746 : : when it is spilled into memory. */
6747 : 1294273 : if (is_init_expr
6748 : 924325 : && !is_gimple_reg (object)
6749 : 920372 : && clear_padding_type_may_have_padding_p (type)
6750 : 881894 : && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
6751 : 225386 : || !AGGREGATE_TYPE_P (type))
6752 : 1950781 : && is_var_need_auto_init (object))
6753 : 38 : gimple_add_padding_init_for_auto_var (object, false, pre_p);
6754 : :
6755 : : return ret;
6756 : : }
6757 : :
6758 : : /* Given a pointer value OP0, return a simplified version of an
6759 : : indirection through OP0, or NULL_TREE if no simplification is
6760 : : possible. This may only be applied to a rhs of an expression.
6761 : : Note that the resulting type may be different from the type pointed
6762 : : to in the sense that it is still compatible from the langhooks
6763 : : point of view. */
6764 : :
6765 : : static tree
6766 : 552058 : gimple_fold_indirect_ref_rhs (tree t)
6767 : : {
6768 : 0 : return gimple_fold_indirect_ref (t);
6769 : : }
6770 : :
6771 : : /* Subroutine of gimplify_modify_expr to do simplifications of
6772 : : MODIFY_EXPRs based on the code of the RHS. We loop for as long as
6773 : : something changes. */
6774 : :
6775 : : static enum gimplify_status
6776 : 95698379 : gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
6777 : : gimple_seq *pre_p, gimple_seq *post_p,
6778 : : bool want_value)
6779 : : {
6780 : 95698379 : enum gimplify_status ret = GS_UNHANDLED;
6781 : 96413884 : bool changed;
6782 : :
6783 : 94970095 : do
6784 : : {
6785 : 96413884 : changed = false;
6786 : 96413884 : switch (TREE_CODE (*from_p))
6787 : : {
6788 : 11791136 : case VAR_DECL:
6789 : : /* If we're assigning from a read-only variable initialized with
6790 : : a constructor and not volatile, do the direct assignment from
6791 : : the constructor, but only if the target is not volatile either
6792 : : since this latter assignment might end up being done on a per
6793 : : field basis. However, if the target is volatile and the type
6794 : : is aggregate and non-addressable, gimplify_init_constructor
6795 : : knows that it needs to ensure a single access to the target
6796 : : and it will return GS_OK only in this case. */
6797 : 11791136 : if (TREE_READONLY (*from_p)
6798 : 103202 : && DECL_INITIAL (*from_p)
6799 : 65034 : && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR
6800 : 7490 : && !TREE_THIS_VOLATILE (*from_p)
6801 : 11798626 : && (!TREE_THIS_VOLATILE (*to_p)
6802 : 6 : || (AGGREGATE_TYPE_P (TREE_TYPE (*to_p))
6803 : 6 : && !TREE_ADDRESSABLE (TREE_TYPE (*to_p)))))
6804 : : {
6805 : 7490 : tree old_from = *from_p;
6806 : 7490 : enum gimplify_status subret;
6807 : :
6808 : : /* Move the constructor into the RHS. */
6809 : 7490 : *from_p = unshare_expr (DECL_INITIAL (*from_p));
6810 : :
6811 : : /* Let's see if gimplify_init_constructor will need to put
6812 : : it in memory. */
6813 : 7490 : subret = gimplify_init_constructor (expr_p, NULL, NULL,
6814 : : false, true);
6815 : 7490 : if (subret == GS_ERROR)
6816 : : {
6817 : : /* If so, revert the change. */
6818 : 1371 : *from_p = old_from;
6819 : : }
6820 : : else
6821 : : {
6822 : : ret = GS_OK;
6823 : : changed = true;
6824 : : }
6825 : : }
6826 : : break;
6827 : 558155 : case INDIRECT_REF:
6828 : 558155 : if (!TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
6829 : : /* If we have code like
6830 : :
6831 : : *(const A*)(A*)&x
6832 : :
6833 : : where the type of "x" is a (possibly cv-qualified variant
6834 : : of "A"), treat the entire expression as identical to "x".
6835 : : This kind of code arises in C++ when an object is bound
6836 : : to a const reference, and if "x" is a TARGET_EXPR we want
6837 : : to take advantage of the optimization below. But not if
6838 : : the type is TREE_ADDRESSABLE; then C++17 says that the
6839 : : TARGET_EXPR needs to be a temporary. */
6840 : 1104116 : if (tree t
6841 : 552058 : = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0)))
6842 : : {
6843 : 160886 : bool volatile_p = TREE_THIS_VOLATILE (*from_p);
6844 : 160886 : if (TREE_THIS_VOLATILE (t) != volatile_p)
6845 : : {
6846 : 5 : if (DECL_P (t))
6847 : 0 : t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
6848 : : build_fold_addr_expr (t));
6849 : 5 : if (REFERENCE_CLASS_P (t))
6850 : 5 : TREE_THIS_VOLATILE (t) = volatile_p;
6851 : : }
6852 : 160886 : *from_p = t;
6853 : 160886 : ret = GS_OK;
6854 : 160886 : changed = true;
6855 : : }
6856 : : break;
6857 : :
6858 : 250769 : case TARGET_EXPR:
6859 : 250769 : {
6860 : : /* If we are initializing something from a TARGET_EXPR, strip the
6861 : : TARGET_EXPR and initialize it directly, if possible. This can't
6862 : : be done if the initializer is void, since that implies that the
6863 : : temporary is set in some non-trivial way.
6864 : :
6865 : : ??? What about code that pulls out the temp and uses it
6866 : : elsewhere? I think that such code never uses the TARGET_EXPR as
6867 : : an initializer. If I'm wrong, we'll die because the temp won't
6868 : : have any RTL. In that case, I guess we'll need to replace
6869 : : references somehow. */
6870 : 250769 : tree init = TARGET_EXPR_INITIAL (*from_p);
6871 : :
6872 : 250769 : if (init
6873 : 250053 : && (TREE_CODE (*expr_p) != MODIFY_EXPR
6874 : 89282 : || !TARGET_EXPR_NO_ELIDE (*from_p))
6875 : 500532 : && !VOID_TYPE_P (TREE_TYPE (init)))
6876 : : {
6877 : 223603 : *from_p = init;
6878 : 223603 : ret = GS_OK;
6879 : 223603 : changed = true;
6880 : : }
6881 : : }
6882 : : break;
6883 : :
6884 : 324688 : case COMPOUND_EXPR:
6885 : : /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
6886 : : caught. */
6887 : 324688 : gimplify_compound_expr (from_p, pre_p, true);
6888 : 324688 : ret = GS_OK;
6889 : 324688 : changed = true;
6890 : 324688 : break;
6891 : :
6892 : 1316850 : case CONSTRUCTOR:
6893 : : /* If we already made some changes, let the front end have a
6894 : : crack at this before we break it down. */
6895 : 1316850 : if (ret != GS_UNHANDLED)
6896 : : break;
6897 : :
6898 : : /* If we're initializing from a CONSTRUCTOR, break this into
6899 : : individual MODIFY_EXPRs. */
6900 : 1295790 : ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
6901 : : false);
6902 : 1295790 : return ret;
6903 : :
6904 : 222698 : case COND_EXPR:
6905 : : /* If we're assigning to a non-register type, push the assignment
6906 : : down into the branches. This is mandatory for ADDRESSABLE types,
6907 : : since we cannot generate temporaries for such, but it saves a
6908 : : copy in other cases as well.
6909 : : Also avoid an extra temporary and copy when assigning to
6910 : : a register. */
6911 : 222698 : if (!is_gimple_reg_type (TREE_TYPE (*from_p))
6912 : 222698 : || (is_gimple_reg (*to_p) && !gimplify_ctxp->allow_rhs_cond_expr))
6913 : : {
6914 : : /* This code should mirror the code in gimplify_cond_expr. */
6915 : 142235 : enum tree_code code = TREE_CODE (*expr_p);
6916 : 142235 : tree cond = *from_p;
6917 : 142235 : tree result = *to_p;
6918 : :
6919 : 142235 : ret = gimplify_expr (&result, pre_p, post_p,
6920 : : is_gimple_lvalue, fb_lvalue);
6921 : 142235 : if (ret != GS_ERROR)
6922 : 142235 : ret = GS_OK;
6923 : :
6924 : : /* If we are going to write RESULT more than once, clear
6925 : : TREE_READONLY flag, otherwise we might incorrectly promote
6926 : : the variable to static const and initialize it at compile
6927 : : time in one of the branches. */
6928 : 142235 : if (VAR_P (result)
6929 : 141191 : && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
6930 : 274353 : && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6931 : 132033 : TREE_READONLY (result) = 0;
6932 : 142235 : if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
6933 : 133026 : TREE_OPERAND (cond, 1)
6934 : 266052 : = build2 (code, void_type_node, result,
6935 : 133026 : TREE_OPERAND (cond, 1));
6936 : 142235 : if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
6937 : 142150 : TREE_OPERAND (cond, 2)
6938 : 284300 : = build2 (code, void_type_node, unshare_expr (result),
6939 : 142150 : TREE_OPERAND (cond, 2));
6940 : :
6941 : 142235 : TREE_TYPE (cond) = void_type_node;
6942 : 142235 : recalculate_side_effects (cond);
6943 : :
6944 : 142235 : if (want_value)
6945 : : {
6946 : 66 : gimplify_and_add (cond, pre_p);
6947 : 66 : *expr_p = unshare_expr (result);
6948 : : }
6949 : : else
6950 : 142169 : *expr_p = cond;
6951 : 142235 : return ret;
6952 : : }
6953 : : break;
6954 : :
6955 : 9802882 : case CALL_EXPR:
6956 : : /* For calls that return in memory, give *to_p as the CALL_EXPR's
6957 : : return slot so that we don't generate a temporary. */
6958 : 9802882 : if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
6959 : 9802882 : && aggregate_value_p (*from_p, *from_p))
6960 : : {
6961 : 246463 : bool use_target;
6962 : :
6963 : 246463 : if (!(rhs_predicate_for (*to_p))(*from_p))
6964 : : /* If we need a temporary, *to_p isn't accurate. */
6965 : : use_target = false;
6966 : : /* It's OK to use the return slot directly unless it's an NRV. */
6967 : 245721 : else if (TREE_CODE (*to_p) == RESULT_DECL
6968 : 4755 : && DECL_NAME (*to_p) == NULL_TREE
6969 : 250337 : && needs_to_live_in_memory (*to_p))
6970 : : use_target = true;
6971 : 241105 : else if (is_gimple_reg_type (TREE_TYPE (*to_p))
6972 : 241105 : || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
6973 : : /* Don't force regs into memory. */
6974 : : use_target = false;
6975 : 227057 : else if (TREE_CODE (*expr_p) == INIT_EXPR)
6976 : : /* It's OK to use the target directly if it's being
6977 : : initialized. */
6978 : : use_target = true;
6979 : 13128 : else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
6980 : : != INTEGER_CST)
6981 : : /* Always use the target and thus RSO for variable-sized types.
6982 : : GIMPLE cannot deal with a variable-sized assignment
6983 : : embedded in a call statement. */
6984 : : use_target = true;
6985 : 13100 : else if (TREE_CODE (*to_p) != SSA_NAME
6986 : 13100 : && (!is_gimple_variable (*to_p)
6987 : 10098 : || needs_to_live_in_memory (*to_p)))
6988 : : /* Don't use the original target if it's already addressable;
6989 : : if its address escapes, and the called function uses the
6990 : : NRV optimization, a conforming program could see *to_p
6991 : : change before the called function returns; see c++/19317.
6992 : : When optimizing, the return_slot pass marks more functions
6993 : : as safe after we have escape info. */
6994 : : use_target = false;
6995 : : else
6996 : : use_target = true;
6997 : :
6998 : : if (use_target)
6999 : : {
7000 : 220630 : CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
7001 : 220630 : mark_addressable (*to_p);
7002 : : }
7003 : : }
7004 : : break;
7005 : :
7006 : 1374 : case WITH_SIZE_EXPR:
7007 : : /* Likewise for calls that return an aggregate of non-constant size,
7008 : : since we would not be able to generate a temporary at all. */
7009 : 1374 : if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
7010 : : {
7011 : 196 : *from_p = TREE_OPERAND (*from_p, 0);
7012 : : /* We don't change ret in this case because the
7013 : : WITH_SIZE_EXPR might have been added in
7014 : : gimplify_modify_expr, so returning GS_OK would lead to an
7015 : : infinite loop. */
7016 : 196 : changed = true;
7017 : : }
7018 : : break;
7019 : :
7020 : : /* If we're initializing from a container, push the initialization
7021 : : inside it. */
7022 : 1681 : case CLEANUP_POINT_EXPR:
7023 : 1681 : case BIND_EXPR:
7024 : 1681 : case STATEMENT_LIST:
7025 : 1681 : {
7026 : 1681 : tree wrap = *from_p;
7027 : 1681 : tree t;
7028 : :
7029 : 1681 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
7030 : : fb_lvalue);
7031 : 1681 : if (ret != GS_ERROR)
7032 : 1681 : ret = GS_OK;
7033 : :
7034 : 1681 : t = voidify_wrapper_expr (wrap, *expr_p);
7035 : 1681 : gcc_assert (t == *expr_p);
7036 : :
7037 : 1681 : if (want_value)
7038 : : {
7039 : 6 : gimplify_and_add (wrap, pre_p);
7040 : 6 : *expr_p = unshare_expr (*to_p);
7041 : : }
7042 : : else
7043 : 1675 : *expr_p = wrap;
7044 : : return GS_OK;
7045 : : }
7046 : :
7047 : 9197870 : case NOP_EXPR:
7048 : : /* Pull out compound literal expressions from a NOP_EXPR.
7049 : : Those are created in the C FE to drop qualifiers during
7050 : : lvalue conversion. */
7051 : 9197870 : if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
7052 : 9197870 : && tree_ssa_useless_type_conversion (*from_p))
7053 : : {
7054 : 13 : *from_p = TREE_OPERAND (*from_p, 0);
7055 : 13 : ret = GS_OK;
7056 : 13 : changed = true;
7057 : : }
7058 : : break;
7059 : :
7060 : 4083 : case COMPOUND_LITERAL_EXPR:
7061 : 4083 : {
7062 : 4083 : tree complit = TREE_OPERAND (*expr_p, 1);
7063 : 4083 : tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
7064 : 4083 : tree decl = DECL_EXPR_DECL (decl_s);
7065 : 4083 : tree init = DECL_INITIAL (decl);
7066 : :
7067 : : /* struct T x = (struct T) { 0, 1, 2 } can be optimized
7068 : : into struct T x = { 0, 1, 2 } if the address of the
7069 : : compound literal has never been taken. */
7070 : 4083 : if (!TREE_ADDRESSABLE (complit)
7071 : 4083 : && !TREE_ADDRESSABLE (decl)
7072 : 4083 : && init)
7073 : : {
7074 : 4083 : *expr_p = copy_node (*expr_p);
7075 : 4083 : TREE_OPERAND (*expr_p, 1) = init;
7076 : 4083 : return GS_OK;
7077 : : }
7078 : : }
7079 : :
7080 : : default:
7081 : : break;
7082 : : }
7083 : : }
7084 : : while (changed);
7085 : :
7086 : : return ret;
7087 : : }
7088 : :
7089 : :
7090 : : /* Return true if T looks like a valid GIMPLE statement. */
7091 : :
7092 : : static bool
7093 : 19452692 : is_gimple_stmt (tree t)
7094 : : {
7095 : 19452692 : const enum tree_code code = TREE_CODE (t);
7096 : :
7097 : 19452692 : switch (code)
7098 : : {
7099 : 1793943 : case NOP_EXPR:
7100 : : /* The only valid NOP_EXPR is the empty statement. */
7101 : 1793943 : return IS_EMPTY_STMT (t);
7102 : :
7103 : 0 : case BIND_EXPR:
7104 : 0 : case COND_EXPR:
7105 : : /* These are only valid if they're void. */
7106 : 0 : return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
7107 : :
7108 : : case SWITCH_EXPR:
7109 : : case GOTO_EXPR:
7110 : : case RETURN_EXPR:
7111 : : case LABEL_EXPR:
7112 : : case CASE_LABEL_EXPR:
7113 : : case TRY_CATCH_EXPR:
7114 : : case TRY_FINALLY_EXPR:
7115 : : case EH_FILTER_EXPR:
7116 : : case CATCH_EXPR:
7117 : : case ASM_EXPR:
7118 : : case STATEMENT_LIST:
7119 : : case OACC_PARALLEL:
7120 : : case OACC_KERNELS:
7121 : : case OACC_SERIAL:
7122 : : case OACC_DATA:
7123 : : case OACC_HOST_DATA:
7124 : : case OACC_DECLARE:
7125 : : case OACC_UPDATE:
7126 : : case OACC_ENTER_DATA:
7127 : : case OACC_EXIT_DATA:
7128 : : case OACC_CACHE:
7129 : : case OMP_PARALLEL:
7130 : : case OMP_FOR:
7131 : : case OMP_SIMD:
7132 : : case OMP_DISTRIBUTE:
7133 : : case OMP_LOOP:
7134 : : case OMP_TILE:
7135 : : case OMP_UNROLL:
7136 : : case OACC_LOOP:
7137 : : case OMP_SCAN:
7138 : : case OMP_SCOPE:
7139 : : case OMP_DISPATCH:
7140 : : case OMP_SECTIONS:
7141 : : case OMP_SECTION:
7142 : : case OMP_STRUCTURED_BLOCK:
7143 : : case OMP_SINGLE:
7144 : : case OMP_MASTER:
7145 : : case OMP_MASKED:
7146 : : case OMP_TASKGROUP:
7147 : : case OMP_ORDERED:
7148 : : case OMP_CRITICAL:
7149 : : case OMP_METADIRECTIVE:
7150 : : case OMP_TASK:
7151 : : case OMP_TARGET:
7152 : : case OMP_TARGET_DATA:
7153 : : case OMP_TARGET_UPDATE:
7154 : : case OMP_TARGET_ENTER_DATA:
7155 : : case OMP_TARGET_EXIT_DATA:
7156 : : case OMP_TASKLOOP:
7157 : : case OMP_TEAMS:
7158 : : /* These are always void. */
7159 : : return true;
7160 : :
7161 : : case CALL_EXPR:
7162 : : case MODIFY_EXPR:
7163 : : case PREDICT_EXPR:
7164 : : /* These are valid regardless of their type. */
7165 : : return true;
7166 : :
7167 : : default:
7168 : : return false;
7169 : : }
7170 : : }
7171 : :
7172 : :
7173 : : /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
7174 : : a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a gimple register.
7175 : :
7176 : : IMPORTANT NOTE: This promotion is performed by introducing a load of the
7177 : : other, unmodified part of the complex object just before the total store.
7178 : : As a consequence, if the object is still uninitialized, an undefined value
7179 : : will be loaded into a register, which may result in a spurious exception
7180 : : if the register is floating-point and the value happens to be a signaling
7181 : : NaN for example. Then the fully-fledged complex operations lowering pass
7182 : : followed by a DCE pass are necessary in order to fix things up. */
7183 : :
7184 : : static enum gimplify_status
7185 : 1960 : gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
7186 : : bool want_value)
7187 : : {
7188 : 1960 : enum tree_code code, ocode;
7189 : 1960 : tree lhs, rhs, new_rhs, other, realpart, imagpart;
7190 : :
7191 : 1960 : lhs = TREE_OPERAND (*expr_p, 0);
7192 : 1960 : rhs = TREE_OPERAND (*expr_p, 1);
7193 : 1960 : code = TREE_CODE (lhs);
7194 : 1960 : lhs = TREE_OPERAND (lhs, 0);
7195 : :
7196 : 1960 : ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
7197 : 1960 : other = build1 (ocode, TREE_TYPE (rhs), lhs);
7198 : 1960 : suppress_warning (other);
7199 : 1960 : other = get_formal_tmp_var (other, pre_p);
7200 : :
7201 : 1960 : realpart = code == REALPART_EXPR ? rhs : other;
7202 : 966 : imagpart = code == REALPART_EXPR ? other : rhs;
7203 : :
7204 : 1960 : if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
7205 : 0 : new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
7206 : : else
7207 : 1960 : new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
7208 : :
7209 : 1960 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
7210 : 1960 : *expr_p = (want_value) ? rhs : NULL_TREE;
7211 : :
7212 : 1960 : return GS_ALL_DONE;
7213 : : }
7214 : :
7215 : : /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
7216 : :
7217 : : modify_expr
7218 : : : varname '=' rhs
7219 : : | '*' ID '=' rhs
7220 : :
7221 : : PRE_P points to the list where side effects that must happen before
7222 : : *EXPR_P should be stored.
7223 : :
7224 : : POST_P points to the list where side effects that must happen after
7225 : : *EXPR_P should be stored.
7226 : :
7227 : : WANT_VALUE is nonzero iff we want to use the value of this expression
7228 : : in another expression. */
7229 : :
7230 : : static enum gimplify_status
7231 : 49667969 : gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7232 : : bool want_value)
7233 : : {
7234 : 49667969 : tree *from_p = &TREE_OPERAND (*expr_p, 1);
7235 : 49667969 : tree *to_p = &TREE_OPERAND (*expr_p, 0);
7236 : 49667969 : enum gimplify_status ret = GS_UNHANDLED;
7237 : 49667969 : gimple *assign;
7238 : 49667969 : location_t loc = EXPR_LOCATION (*expr_p);
7239 : 49667969 : gimple_stmt_iterator gsi;
7240 : :
7241 : 49667969 : if (error_operand_p (*from_p) || error_operand_p (*to_p))
7242 : : return GS_ERROR;
7243 : :
7244 : 49667933 : gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
7245 : : || TREE_CODE (*expr_p) == INIT_EXPR);
7246 : :
7247 : : /* Trying to simplify a clobber using normal logic doesn't work,
7248 : : so handle it here. */
7249 : 49667933 : if (TREE_CLOBBER_P (*from_p))
7250 : : {
7251 : 788810 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7252 : 788810 : if (ret == GS_ERROR)
7253 : : return ret;
7254 : 788810 : gcc_assert (!want_value);
7255 : 788810 : if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
7256 : : {
7257 : 235 : tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
7258 : : pre_p, post_p);
7259 : 235 : *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
7260 : : }
7261 : 788810 : gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
7262 : 788810 : *expr_p = NULL;
7263 : 788810 : return GS_ALL_DONE;
7264 : : }
7265 : :
7266 : : /* Convert initialization from an empty variable-size CONSTRUCTOR to
7267 : : memset. */
7268 : 48879123 : if (TREE_TYPE (*from_p) != error_mark_node
7269 : 48879123 : && TYPE_SIZE_UNIT (TREE_TYPE (*from_p))
7270 : 48879123 : && !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (*from_p)))
7271 : 1409 : && TREE_CODE (*from_p) == CONSTRUCTOR
7272 : 48879206 : && CONSTRUCTOR_NELTS (*from_p) == 0)
7273 : : {
7274 : 66 : maybe_with_size_expr (from_p);
7275 : 66 : gcc_assert (TREE_CODE (*from_p) == WITH_SIZE_EXPR);
7276 : 66 : return gimplify_modify_expr_to_memset (expr_p,
7277 : 66 : TREE_OPERAND (*from_p, 1),
7278 : 66 : want_value, pre_p);
7279 : : }
7280 : :
7281 : : /* Insert pointer conversions required by the middle-end that are not
7282 : : required by the frontend. This fixes middle-end type checking for
7283 : : for example gcc.dg/redecl-6.c. */
7284 : 48879057 : if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
7285 : : {
7286 : 13188174 : STRIP_USELESS_TYPE_CONVERSION (*from_p);
7287 : 13188174 : if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
7288 : 267 : *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
7289 : : }
7290 : :
7291 : : /* See if any simplifications can be done based on what the RHS is. */
7292 : 48879057 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7293 : : want_value);
7294 : 48879057 : if (ret != GS_UNHANDLED)
7295 : : return ret;
7296 : :
7297 : : /* For empty types only gimplify the left hand side and right hand
7298 : : side as statements and throw away the assignment. Do this after
7299 : : gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
7300 : : types properly. */
7301 : 46821414 : if (is_empty_type (TREE_TYPE (*from_p))
7302 : 13633 : && !want_value
7303 : : /* Don't do this for calls that return addressable types, expand_call
7304 : : relies on those having a lhs. */
7305 : 46835040 : && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
7306 : 11567 : && TREE_CODE (*from_p) == CALL_EXPR))
7307 : : {
7308 : 2059 : gimplify_stmt (from_p, pre_p);
7309 : 2059 : gimplify_stmt (to_p, pre_p);
7310 : 2059 : *expr_p = NULL_TREE;
7311 : 2059 : return GS_ALL_DONE;
7312 : : }
7313 : :
7314 : : /* If the value being copied is of variable width, compute the length
7315 : : of the copy into a WITH_SIZE_EXPR. Note that we need to do this
7316 : : before gimplifying any of the operands so that we can resolve any
7317 : : PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
7318 : : the size of the expression to be copied, not of the destination, so
7319 : : that is what we must do here. */
7320 : 46819355 : maybe_with_size_expr (from_p);
7321 : :
7322 : : /* As a special case, we have to temporarily allow for assignments
7323 : : with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
7324 : : a toplevel statement, when gimplifying the GENERIC expression
7325 : : MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
7326 : : GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
7327 : :
7328 : : Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
7329 : : prevent gimplify_expr from trying to create a new temporary for
7330 : : foo's LHS, we tell it that it should only gimplify until it
7331 : : reaches the CALL_EXPR. On return from gimplify_expr, the newly
7332 : : created GIMPLE_CALL <foo> will be the last statement in *PRE_P
7333 : : and all we need to do here is set 'a' to be its LHS. */
7334 : :
7335 : : /* Gimplify the RHS first for C++17 and bug 71104. */
7336 : 46819355 : gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
7337 : 46819355 : ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
7338 : 46819355 : if (ret == GS_ERROR)
7339 : : return ret;
7340 : :
7341 : : /* Then gimplify the LHS. */
7342 : : /* If we gimplified the RHS to a CALL_EXPR and that call may return
7343 : : twice we have to make sure to gimplify into non-SSA as otherwise
7344 : : the abnormal edge added later will make those defs not dominate
7345 : : their uses.
7346 : : ??? Technically this applies only to the registers used in the
7347 : : resulting non-register *TO_P. */
7348 : 46819322 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
7349 : 46819322 : if (saved_into_ssa
7350 : 44517962 : && TREE_CODE (*from_p) == CALL_EXPR
7351 : 51776270 : && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
7352 : 1481 : gimplify_ctxp->into_ssa = false;
7353 : 46819322 : ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
7354 : 46819322 : gimplify_ctxp->into_ssa = saved_into_ssa;
7355 : 46819322 : if (ret == GS_ERROR)
7356 : : return ret;
7357 : :
7358 : : /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
7359 : : guess for the predicate was wrong. */
7360 : 46819322 : gimple_predicate final_pred = rhs_predicate_for (*to_p);
7361 : 46819322 : if (final_pred != initial_pred)
7362 : : {
7363 : 7943841 : ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
7364 : 7943841 : if (ret == GS_ERROR)
7365 : : return ret;
7366 : : }
7367 : :
7368 : : /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
7369 : : size as argument to the call. */
7370 : 46819322 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7371 : : {
7372 : 1271 : tree call = TREE_OPERAND (*from_p, 0);
7373 : 1271 : tree vlasize = TREE_OPERAND (*from_p, 1);
7374 : :
7375 : 1271 : if (TREE_CODE (call) == CALL_EXPR
7376 : 1271 : && CALL_EXPR_IFN (call) == IFN_VA_ARG)
7377 : : {
7378 : 95 : int nargs = call_expr_nargs (call);
7379 : 95 : tree type = TREE_TYPE (call);
7380 : 95 : tree ap = CALL_EXPR_ARG (call, 0);
7381 : 95 : tree tag = CALL_EXPR_ARG (call, 1);
7382 : 95 : tree aptag = CALL_EXPR_ARG (call, 2);
7383 : 95 : tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
7384 : : IFN_VA_ARG, type,
7385 : : nargs + 1, ap, tag,
7386 : : aptag, vlasize);
7387 : 95 : TREE_OPERAND (*from_p, 0) = newcall;
7388 : : }
7389 : : }
7390 : :
7391 : : /* Now see if the above changed *from_p to something we handle specially. */
7392 : 46819322 : ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
7393 : : want_value);
7394 : 46819322 : if (ret != GS_UNHANDLED)
7395 : : return ret;
7396 : :
7397 : : /* If we've got a variable sized assignment between two lvalues (i.e. does
7398 : : not involve a call), then we can make things a bit more straightforward
7399 : : by converting the assignment to memcpy or memset. */
7400 : 46818466 : if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
7401 : : {
7402 : 1075 : tree from = TREE_OPERAND (*from_p, 0);
7403 : 1075 : tree size = TREE_OPERAND (*from_p, 1);
7404 : :
7405 : 1075 : if (TREE_CODE (from) == CONSTRUCTOR)
7406 : 0 : return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
7407 : 1075 : else if (is_gimple_addressable (from)
7408 : 1075 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (*to_p)))
7409 : 2149 : && ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (from))))
7410 : : {
7411 : 1074 : *from_p = from;
7412 : 1074 : return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
7413 : 1074 : pre_p);
7414 : : }
7415 : : }
7416 : :
7417 : : /* Transform partial stores to non-addressable complex variables into
7418 : : total stores. This allows us to use real instead of virtual operands
7419 : : for these variables, which improves optimization. */
7420 : 46817392 : if ((TREE_CODE (*to_p) == REALPART_EXPR
7421 : 46817392 : || TREE_CODE (*to_p) == IMAGPART_EXPR)
7422 : 46817392 : && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
7423 : 1960 : return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
7424 : :
7425 : : /* Try to alleviate the effects of the gimplification creating artificial
7426 : : temporaries (see for example is_gimple_reg_rhs) on the debug info, but
7427 : : make sure not to create DECL_DEBUG_EXPR links across functions. */
7428 : 46815432 : if (!gimplify_ctxp->into_ssa
7429 : 2300461 : && VAR_P (*from_p)
7430 : 456095 : && DECL_IGNORED_P (*from_p)
7431 : 333790 : && DECL_P (*to_p)
7432 : 124975 : && !DECL_IGNORED_P (*to_p)
7433 : 29279 : && decl_function_context (*to_p) == current_function_decl
7434 : 46839247 : && decl_function_context (*from_p) == current_function_decl)
7435 : : {
7436 : 23810 : if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
7437 : 18437 : DECL_NAME (*from_p)
7438 : 36874 : = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
7439 : 23810 : DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
7440 : 23810 : SET_DECL_DEBUG_EXPR (*from_p, *to_p);
7441 : : }
7442 : :
7443 : 46815432 : if (want_value && TREE_THIS_VOLATILE (*to_p))
7444 : 647 : *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
7445 : :
7446 : 46815432 : if (TREE_CODE (*from_p) == CALL_EXPR)
7447 : : {
7448 : : /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
7449 : : instead of a GIMPLE_ASSIGN. */
7450 : 4662856 : gcall *call_stmt;
7451 : 4662856 : if (CALL_EXPR_FN (*from_p) == NULL_TREE)
7452 : : {
7453 : : /* Gimplify internal functions created in the FEs. */
7454 : 176448 : int nargs = call_expr_nargs (*from_p), i;
7455 : 176448 : enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
7456 : 176448 : auto_vec<tree> vargs (nargs);
7457 : :
7458 : 901839 : for (i = 0; i < nargs; i++)
7459 : : {
7460 : 548943 : gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
7461 : 548943 : EXPR_LOCATION (*from_p));
7462 : 548943 : vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
7463 : : }
7464 : 176448 : call_stmt = gimple_build_call_internal_vec (ifn, vargs);
7465 : 176448 : gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
7466 : 352896 : gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
7467 : 176448 : }
7468 : : else
7469 : : {
7470 : 4486408 : tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
7471 : 4486408 : CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
7472 : 4486408 : STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
7473 : 4486408 : tree fndecl = get_callee_fndecl (*from_p);
7474 : 4486408 : if (fndecl
7475 : 4372141 : && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
7476 : 4612562 : && call_expr_nargs (*from_p) == 3)
7477 : 63583 : call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
7478 : 63583 : CALL_EXPR_ARG (*from_p, 0),
7479 : 63583 : CALL_EXPR_ARG (*from_p, 1),
7480 : 63583 : CALL_EXPR_ARG (*from_p, 2));
7481 : : else
7482 : : {
7483 : 4422825 : call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
7484 : : }
7485 : : }
7486 : 4662856 : notice_special_calls (call_stmt);
7487 : 4662856 : if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
7488 : 4662756 : gimple_call_set_lhs (call_stmt, *to_p);
7489 : 100 : else if (TREE_CODE (*to_p) == SSA_NAME)
7490 : : /* The above is somewhat premature, avoid ICEing later for a
7491 : : SSA name w/o a definition. We may have uses in the GIMPLE IL.
7492 : : ??? This doesn't make it a default-def. */
7493 : 18 : SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
7494 : :
7495 : : assign = call_stmt;
7496 : : }
7497 : : else
7498 : : {
7499 : 42152576 : assign = gimple_build_assign (*to_p, *from_p);
7500 : 42152576 : gimple_set_location (assign, EXPR_LOCATION (*expr_p));
7501 : 42152576 : if (COMPARISON_CLASS_P (*from_p))
7502 : 1226782 : copy_warning (assign, *from_p);
7503 : : }
7504 : :
7505 : 46815432 : if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
7506 : : {
7507 : : /* We should have got an SSA name from the start. */
7508 : 34644038 : gcc_assert (TREE_CODE (*to_p) == SSA_NAME
7509 : : || ! gimple_in_ssa_p (cfun));
7510 : : }
7511 : :
7512 : 46815432 : gimplify_seq_add_stmt (pre_p, assign);
7513 : 46815432 : gsi = gsi_last (*pre_p);
7514 : 46815432 : maybe_fold_stmt (&gsi);
7515 : :
7516 : 46815432 : if (want_value)
7517 : : {
7518 : 622320 : *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
7519 : 622320 : return GS_OK;
7520 : : }
7521 : : else
7522 : 46193112 : *expr_p = NULL;
7523 : :
7524 : 46193112 : return GS_ALL_DONE;
7525 : : }
7526 : :
7527 : : /* Gimplify a comparison between two variable-sized objects. Do this
7528 : : with a call to BUILT_IN_MEMCMP. */
7529 : :
7530 : : static enum gimplify_status
7531 : 0 : gimplify_variable_sized_compare (tree *expr_p)
7532 : : {
7533 : 0 : location_t loc = EXPR_LOCATION (*expr_p);
7534 : 0 : tree op0 = TREE_OPERAND (*expr_p, 0);
7535 : 0 : tree op1 = TREE_OPERAND (*expr_p, 1);
7536 : 0 : tree t, arg, dest, src, expr;
7537 : :
7538 : 0 : arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
7539 : 0 : arg = unshare_expr (arg);
7540 : 0 : arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
7541 : 0 : src = build_fold_addr_expr_loc (loc, op1);
7542 : 0 : dest = build_fold_addr_expr_loc (loc, op0);
7543 : 0 : t = builtin_decl_implicit (BUILT_IN_MEMCMP);
7544 : 0 : t = build_call_expr_loc (loc, t, 3, dest, src, arg);
7545 : :
7546 : 0 : expr
7547 : 0 : = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
7548 : 0 : SET_EXPR_LOCATION (expr, loc);
7549 : 0 : *expr_p = expr;
7550 : :
7551 : 0 : return GS_OK;
7552 : : }
7553 : :
7554 : : /* Gimplify a comparison between two aggregate objects of integral scalar
7555 : : mode as a comparison between the bitwise equivalent scalar values. */
7556 : :
7557 : : static enum gimplify_status
7558 : 20 : gimplify_scalar_mode_aggregate_compare (tree *expr_p)
7559 : : {
7560 : 20 : const location_t loc = EXPR_LOCATION (*expr_p);
7561 : 20 : const enum tree_code code = TREE_CODE (*expr_p);
7562 : 20 : tree op0 = TREE_OPERAND (*expr_p, 0);
7563 : 20 : tree op1 = TREE_OPERAND (*expr_p, 1);
7564 : 20 : tree type = TREE_TYPE (op0);
7565 : 20 : tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
7566 : :
7567 : 20 : op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
7568 : 20 : op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
7569 : :
7570 : : /* We need to perform ordering comparisons in memory order like memcmp and,
7571 : : therefore, may need to byte-swap operands for little-endian targets. */
7572 : 20 : if (code != EQ_EXPR && code != NE_EXPR)
7573 : : {
7574 : 0 : gcc_assert (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN);
7575 : 0 : gcc_assert (TREE_CODE (scalar_type) == INTEGER_TYPE);
7576 : 0 : tree fndecl;
7577 : :
7578 : 0 : if (BYTES_BIG_ENDIAN)
7579 : : fndecl = NULL_TREE;
7580 : : else
7581 : 0 : switch (int_size_in_bytes (scalar_type))
7582 : : {
7583 : : case 1:
7584 : : fndecl = NULL_TREE;
7585 : : break;
7586 : 0 : case 2:
7587 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP16);
7588 : 0 : break;
7589 : 0 : case 4:
7590 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP32);
7591 : 0 : break;
7592 : 0 : case 8:
7593 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP64);
7594 : 0 : break;
7595 : 0 : case 16:
7596 : 0 : fndecl = builtin_decl_implicit (BUILT_IN_BSWAP128);
7597 : 0 : break;
7598 : 0 : default:
7599 : 0 : gcc_unreachable ();
7600 : : }
7601 : :
7602 : 0 : if (fndecl)
7603 : : {
7604 : 0 : op0 = build_call_expr_loc (loc, fndecl, 1, op0);
7605 : 0 : op1 = build_call_expr_loc (loc, fndecl, 1, op1);
7606 : : }
7607 : : }
7608 : :
7609 : 20 : *expr_p = fold_build2_loc (loc, code, TREE_TYPE (*expr_p), op0, op1);
7610 : :
7611 : 20 : return GS_OK;
7612 : : }
7613 : :
7614 : : /* Gimplify an expression sequence. This function gimplifies each
7615 : : expression and rewrites the original expression with the last
7616 : : expression of the sequence in GIMPLE form.
7617 : :
7618 : : PRE_P points to the list where the side effects for all the
7619 : : expressions in the sequence will be emitted.
7620 : :
7621 : : WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
7622 : :
7623 : : static enum gimplify_status
7624 : 967479 : gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
7625 : : {
7626 : 967479 : tree t = *expr_p;
7627 : :
7628 : 1029832 : do
7629 : : {
7630 : 1029832 : tree *sub_p = &TREE_OPERAND (t, 0);
7631 : :
7632 : 1029832 : if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
7633 : 184101 : gimplify_compound_expr (sub_p, pre_p, false);
7634 : : else
7635 : 845731 : gimplify_stmt (sub_p, pre_p);
7636 : :
7637 : 1029832 : t = TREE_OPERAND (t, 1);
7638 : : }
7639 : 1029832 : while (TREE_CODE (t) == COMPOUND_EXPR);
7640 : :
7641 : 967479 : *expr_p = t;
7642 : 967479 : if (want_value)
7643 : : return GS_OK;
7644 : : else
7645 : : {
7646 : 475997 : gimplify_stmt (expr_p, pre_p);
7647 : 475997 : return GS_ALL_DONE;
7648 : : }
7649 : : }
7650 : :
7651 : : /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
7652 : : gimplify. After gimplification, EXPR_P will point to a new temporary
7653 : : that holds the original value of the SAVE_EXPR node.
7654 : :
7655 : : PRE_P points to the list where side effects that must happen before
7656 : : *EXPR_P should be stored. */
7657 : :
7658 : : static enum gimplify_status
7659 : 412843 : gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7660 : : {
7661 : 412843 : enum gimplify_status ret = GS_ALL_DONE;
7662 : 412843 : tree val;
7663 : :
7664 : 412843 : gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
7665 : 412843 : val = TREE_OPERAND (*expr_p, 0);
7666 : :
7667 : 412843 : if (val && TREE_TYPE (val) == error_mark_node)
7668 : : return GS_ERROR;
7669 : :
7670 : : /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
7671 : 412841 : if (!SAVE_EXPR_RESOLVED_P (*expr_p))
7672 : : {
7673 : : /* The operand may be a void-valued expression. It is
7674 : : being executed only for its side-effects. */
7675 : 155187 : if (TREE_TYPE (val) == void_type_node)
7676 : : {
7677 : 0 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7678 : : is_gimple_stmt, fb_none);
7679 : 0 : val = NULL;
7680 : : }
7681 : : else
7682 : : /* The temporary may not be an SSA name as later abnormal and EH
7683 : : control flow may invalidate use/def domination. When in SSA
7684 : : form then assume there are no such issues and SAVE_EXPRs only
7685 : : appear via GENERIC foldings. */
7686 : 310374 : val = get_initialized_tmp_var (val, pre_p, post_p,
7687 : 310374 : gimple_in_ssa_p (cfun));
7688 : :
7689 : 155187 : TREE_OPERAND (*expr_p, 0) = val;
7690 : 155187 : SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
7691 : : }
7692 : :
7693 : 412841 : *expr_p = val;
7694 : :
7695 : 412841 : return ret;
7696 : : }
7697 : :
7698 : : /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
7699 : :
7700 : : unary_expr
7701 : : : ...
7702 : : | '&' varname
7703 : : ...
7704 : :
7705 : : PRE_P points to the list where side effects that must happen before
7706 : : *EXPR_P should be stored.
7707 : :
7708 : : POST_P points to the list where side effects that must happen after
7709 : : *EXPR_P should be stored. */
7710 : :
7711 : : static enum gimplify_status
7712 : 33321000 : gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7713 : : {
7714 : 33321000 : tree expr = *expr_p;
7715 : 33321000 : tree op0 = TREE_OPERAND (expr, 0);
7716 : 33321000 : enum gimplify_status ret;
7717 : 33321000 : location_t loc = EXPR_LOCATION (*expr_p);
7718 : :
7719 : 33321000 : switch (TREE_CODE (op0))
7720 : : {
7721 : 94743 : case INDIRECT_REF:
7722 : 94743 : do_indirect_ref:
7723 : : /* Check if we are dealing with an expression of the form '&*ptr'.
7724 : : While the front end folds away '&*ptr' into 'ptr', these
7725 : : expressions may be generated internally by the compiler (e.g.,
7726 : : builtins like __builtin_va_end). */
7727 : : /* Caution: the silent array decomposition semantics we allow for
7728 : : ADDR_EXPR means we can't always discard the pair. */
7729 : : /* Gimplification of the ADDR_EXPR operand may drop
7730 : : cv-qualification conversions, so make sure we add them if
7731 : : needed. */
7732 : 94743 : {
7733 : 94743 : tree op00 = TREE_OPERAND (op0, 0);
7734 : 94743 : tree t_expr = TREE_TYPE (expr);
7735 : 94743 : tree t_op00 = TREE_TYPE (op00);
7736 : :
7737 : 94743 : if (!useless_type_conversion_p (t_expr, t_op00))
7738 : 0 : op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
7739 : 94743 : *expr_p = op00;
7740 : 94743 : ret = GS_OK;
7741 : : }
7742 : 94743 : break;
7743 : :
7744 : 0 : case VIEW_CONVERT_EXPR:
7745 : : /* Take the address of our operand and then convert it to the type of
7746 : : this ADDR_EXPR.
7747 : :
7748 : : ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
7749 : : all clear. The impact of this transformation is even less clear. */
7750 : :
7751 : : /* If the operand is a useless conversion, look through it. Doing so
7752 : : guarantees that the ADDR_EXPR and its operand will remain of the
7753 : : same type. */
7754 : 0 : if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
7755 : 0 : op0 = TREE_OPERAND (op0, 0);
7756 : :
7757 : 0 : *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
7758 : : build_fold_addr_expr_loc (loc,
7759 : 0 : TREE_OPERAND (op0, 0)));
7760 : 0 : ret = GS_OK;
7761 : 0 : break;
7762 : :
7763 : 58521 : case MEM_REF:
7764 : 58521 : if (integer_zerop (TREE_OPERAND (op0, 1)))
7765 : 35276 : goto do_indirect_ref;
7766 : :
7767 : : /* fall through */
7768 : :
7769 : 33241711 : default:
7770 : : /* If we see a call to a declared builtin or see its address
7771 : : being taken (we can unify those cases here) then we can mark
7772 : : the builtin for implicit generation by GCC. */
7773 : 33241711 : if (TREE_CODE (op0) == FUNCTION_DECL
7774 : 16433116 : && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
7775 : 37132407 : && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
7776 : 1288302 : set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
7777 : :
7778 : : /* We use fb_either here because the C frontend sometimes takes
7779 : : the address of a call that returns a struct; see
7780 : : gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
7781 : : the implied temporary explicit. */
7782 : :
7783 : : /* Make the operand addressable. */
7784 : 33241711 : ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
7785 : : is_gimple_addressable, fb_either);
7786 : 33241711 : if (ret == GS_ERROR)
7787 : : break;
7788 : :
7789 : : /* Then mark it. Beware that it may not be possible to do so directly
7790 : : if a temporary has been created by the gimplification. */
7791 : 33241709 : prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
7792 : :
7793 : 33241709 : op0 = TREE_OPERAND (expr, 0);
7794 : :
7795 : : /* For various reasons, the gimplification of the expression
7796 : : may have made a new INDIRECT_REF. */
7797 : 33241709 : if (INDIRECT_REF_P (op0)
7798 : 33241709 : || (TREE_CODE (op0) == MEM_REF
7799 : 38699 : && integer_zerop (TREE_OPERAND (op0, 1))))
7800 : 15454 : goto do_indirect_ref;
7801 : :
7802 : 33226255 : mark_addressable (TREE_OPERAND (expr, 0));
7803 : :
7804 : : /* The FEs may end up building ADDR_EXPRs early on a decl with
7805 : : an incomplete type. Re-build ADDR_EXPRs in canonical form
7806 : : here. */
7807 : 33226255 : if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
7808 : 142970 : *expr_p = build_fold_addr_expr (op0);
7809 : :
7810 : : /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
7811 : 33226255 : if (TREE_CODE (*expr_p) == ADDR_EXPR)
7812 : 33226254 : recompute_tree_invariant_for_addr_expr (*expr_p);
7813 : :
7814 : : /* If we re-built the ADDR_EXPR add a conversion to the original type
7815 : : if required. */
7816 : 33226255 : if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
7817 : 0 : *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
7818 : :
7819 : : break;
7820 : : }
7821 : :
7822 : 33321000 : return ret;
7823 : : }
7824 : :
7825 : : /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
7826 : : value; output operands should be a gimple lvalue. */
7827 : :
7828 : : static enum gimplify_status
7829 : 94982 : gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
7830 : : {
7831 : 94982 : tree expr;
7832 : 94982 : int noutputs;
7833 : 94982 : const char **oconstraints;
7834 : 94982 : int i;
7835 : 94982 : tree link;
7836 : 94982 : const char *constraint;
7837 : 94982 : bool allows_mem, allows_reg, is_inout;
7838 : 94982 : enum gimplify_status ret, tret;
7839 : 94982 : gasm *stmt;
7840 : 94982 : vec<tree, va_gc> *inputs;
7841 : 94982 : vec<tree, va_gc> *outputs;
7842 : 94982 : vec<tree, va_gc> *clobbers;
7843 : 94982 : vec<tree, va_gc> *labels;
7844 : 94982 : tree link_next;
7845 : :
7846 : 94982 : expr = *expr_p;
7847 : 94982 : noutputs = list_length (ASM_OUTPUTS (expr));
7848 : 94982 : oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
7849 : :
7850 : 94982 : inputs = NULL;
7851 : 94982 : outputs = NULL;
7852 : 94982 : clobbers = NULL;
7853 : 94982 : labels = NULL;
7854 : :
7855 : 94982 : ret = GS_ALL_DONE;
7856 : 94982 : link_next = NULL_TREE;
7857 : 152232 : for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
7858 : : {
7859 : 57262 : bool ok;
7860 : 57262 : size_t constraint_len;
7861 : :
7862 : 57262 : link_next = TREE_CHAIN (link);
7863 : :
7864 : 114524 : oconstraints[i]
7865 : 57262 : = constraint
7866 : 57262 : = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
7867 : 57262 : constraint_len = strlen (constraint);
7868 : 57262 : if (constraint_len == 0)
7869 : 6 : continue;
7870 : :
7871 : 57256 : ok = parse_output_constraint (&constraint, i, 0, 0,
7872 : : &allows_mem, &allows_reg, &is_inout);
7873 : 57256 : if (!ok)
7874 : : {
7875 : 3 : ret = GS_ERROR;
7876 : 3 : is_inout = false;
7877 : : }
7878 : :
7879 : : /* If we can't make copies, we can only accept memory.
7880 : : Similarly for VLAs. */
7881 : 57256 : tree outtype = TREE_TYPE (TREE_VALUE (link));
7882 : 57256 : if (outtype != error_mark_node
7883 : 57256 : && (TREE_ADDRESSABLE (outtype)
7884 : 57226 : || !COMPLETE_TYPE_P (outtype)
7885 : 57201 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype))))
7886 : : {
7887 : 246 : if (allows_mem)
7888 : 234 : allows_reg = 0;
7889 : : else
7890 : : {
7891 : 12 : error ("impossible constraint in %<asm%>");
7892 : 12 : error ("non-memory output %d must stay in memory", i);
7893 : 12 : return GS_ERROR;
7894 : : }
7895 : : }
7896 : :
7897 : 57244 : if (!allows_reg && allows_mem)
7898 : 1279 : mark_addressable (TREE_VALUE (link));
7899 : :
7900 : 57244 : tree orig = TREE_VALUE (link);
7901 : 105851 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
7902 : : is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7903 : : fb_lvalue | fb_mayfail);
7904 : 57244 : if (tret == GS_ERROR)
7905 : : {
7906 : 15 : if (orig != error_mark_node)
7907 : 0 : error ("invalid lvalue in %<asm%> output %d", i);
7908 : : ret = tret;
7909 : : }
7910 : :
7911 : : /* If the gimplified operand is a register we do not allow memory. */
7912 : 57244 : if (allows_reg
7913 : 55962 : && allows_mem
7914 : 62965 : && (is_gimple_reg (TREE_VALUE (link))
7915 : 269 : || (handled_component_p (TREE_VALUE (link))
7916 : 36 : && is_gimple_reg (TREE_OPERAND (TREE_VALUE (link), 0)))))
7917 : 5453 : allows_mem = 0;
7918 : :
7919 : : /* If the constraint does not allow memory make sure we gimplify
7920 : : it to a register if it is not already but its base is. This
7921 : : happens for complex and vector components. */
7922 : 57244 : if (!allows_mem)
7923 : : {
7924 : 55697 : tree op = TREE_VALUE (link);
7925 : 55697 : if (! is_gimple_val (op)
7926 : 20480 : && is_gimple_reg_type (TREE_TYPE (op))
7927 : 76176 : && is_gimple_reg (get_base_address (op)))
7928 : : {
7929 : 19 : tree tem = create_tmp_reg (TREE_TYPE (op));
7930 : 19 : tree ass;
7931 : 19 : if (is_inout)
7932 : : {
7933 : 7 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
7934 : : tem, unshare_expr (op));
7935 : 7 : gimplify_and_add (ass, pre_p);
7936 : : }
7937 : 19 : ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
7938 : 19 : gimplify_and_add (ass, post_p);
7939 : :
7940 : 19 : TREE_VALUE (link) = tem;
7941 : 19 : tret = GS_OK;
7942 : : }
7943 : : }
7944 : :
7945 : 57244 : vec_safe_push (outputs, link);
7946 : 57244 : TREE_CHAIN (link) = NULL_TREE;
7947 : :
7948 : 57244 : if (is_inout)
7949 : : {
7950 : : /* An input/output operand. To give the optimizers more
7951 : : flexibility, split it into separate input and output
7952 : : operands. */
7953 : 8637 : tree input;
7954 : : /* Buffer big enough to format a 32-bit UINT_MAX into. */
7955 : 8637 : char buf[11];
7956 : :
7957 : : /* Turn the in/out constraint into an output constraint. */
7958 : 8637 : char *p = xstrdup (constraint);
7959 : 8637 : p[0] = '=';
7960 : 8637 : TREE_PURPOSE (link) = unshare_expr (TREE_PURPOSE (link));
7961 : 8637 : TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
7962 : :
7963 : : /* And add a matching input constraint. */
7964 : 8637 : if (allows_reg)
7965 : : {
7966 : 8138 : sprintf (buf, "%u", i);
7967 : :
7968 : : /* If there are multiple alternatives in the constraint,
7969 : : handle each of them individually. Those that allow register
7970 : : will be replaced with operand number, the others will stay
7971 : : unchanged. */
7972 : 8138 : if (strchr (p, ',') != NULL)
7973 : : {
7974 : 100 : size_t len = 0, buflen = strlen (buf);
7975 : 100 : char *beg, *end, *str, *dst;
7976 : :
7977 : 100 : for (beg = p + 1;;)
7978 : : {
7979 : 200 : end = strchr (beg, ',');
7980 : 200 : if (end == NULL)
7981 : 100 : end = strchr (beg, '\0');
7982 : 200 : if ((size_t) (end - beg) < buflen)
7983 : 28 : len += buflen + 1;
7984 : : else
7985 : 172 : len += end - beg + 1;
7986 : 200 : if (*end)
7987 : 100 : beg = end + 1;
7988 : : else
7989 : : break;
7990 : : }
7991 : :
7992 : 100 : str = (char *) alloca (len);
7993 : 100 : for (beg = p + 1, dst = str;;)
7994 : : {
7995 : 200 : const char *tem;
7996 : 200 : bool mem_p, reg_p, inout_p;
7997 : :
7998 : 200 : end = strchr (beg, ',');
7999 : 200 : if (end)
8000 : 100 : *end = '\0';
8001 : 200 : beg[-1] = '=';
8002 : 200 : tem = beg - 1;
8003 : 200 : parse_output_constraint (&tem, i, 0, 0,
8004 : : &mem_p, ®_p, &inout_p);
8005 : 200 : if (dst != str)
8006 : 100 : *dst++ = ',';
8007 : 200 : if (reg_p)
8008 : : {
8009 : 115 : memcpy (dst, buf, buflen);
8010 : 115 : dst += buflen;
8011 : : }
8012 : : else
8013 : : {
8014 : 85 : if (end)
8015 : 0 : len = end - beg;
8016 : : else
8017 : 85 : len = strlen (beg);
8018 : 85 : memcpy (dst, beg, len);
8019 : 85 : dst += len;
8020 : : }
8021 : 200 : if (end)
8022 : 100 : beg = end + 1;
8023 : : else
8024 : : break;
8025 : 100 : }
8026 : 100 : *dst = '\0';
8027 : 100 : input = build_string (dst - str, str);
8028 : : }
8029 : : else
8030 : 8038 : input = build_string (strlen (buf), buf);
8031 : : }
8032 : : else
8033 : 499 : input = build_string (constraint_len - 1, constraint + 1);
8034 : :
8035 : 8637 : free (p);
8036 : :
8037 : 8637 : input = build_tree_list (build_tree_list (NULL_TREE, input),
8038 : 8637 : unshare_expr (TREE_VALUE (link)));
8039 : 8637 : ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
8040 : : }
8041 : : }
8042 : :
8043 : 94970 : link_next = NULL_TREE;
8044 : 134280 : for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
8045 : : {
8046 : 39319 : link_next = TREE_CHAIN (link);
8047 : 39319 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
8048 : 39319 : parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8049 : : oconstraints, &allows_mem, &allows_reg);
8050 : :
8051 : : /* If we can't make copies, we can only accept memory. */
8052 : 39319 : tree intype = TREE_TYPE (TREE_VALUE (link));
8053 : 39319 : if (intype != error_mark_node
8054 : 39319 : && (TREE_ADDRESSABLE (intype)
8055 : 39256 : || !COMPLETE_TYPE_P (intype)
8056 : 39217 : || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype))))
8057 : : {
8058 : 255 : if (allows_mem)
8059 : 246 : allows_reg = 0;
8060 : : else
8061 : : {
8062 : 9 : error ("impossible constraint in %<asm%>");
8063 : 9 : error ("non-memory input %d must stay in memory", i);
8064 : 9 : return GS_ERROR;
8065 : : }
8066 : : }
8067 : :
8068 : : /* If the operand is a memory input, it should be an lvalue. */
8069 : 39310 : if (!allows_reg && allows_mem)
8070 : : {
8071 : 1649 : tree inputv = TREE_VALUE (link);
8072 : 1649 : STRIP_NOPS (inputv);
8073 : 1649 : if (TREE_CODE (inputv) == PREDECREMENT_EXPR
8074 : : || TREE_CODE (inputv) == PREINCREMENT_EXPR
8075 : : || TREE_CODE (inputv) == POSTDECREMENT_EXPR
8076 : 1649 : || TREE_CODE (inputv) == POSTINCREMENT_EXPR
8077 : 1637 : || TREE_CODE (inputv) == MODIFY_EXPR
8078 : 3284 : || VOID_TYPE_P (TREE_TYPE (inputv)))
8079 : 38 : TREE_VALUE (link) = error_mark_node;
8080 : 1649 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8081 : : is_gimple_lvalue, fb_lvalue | fb_mayfail);
8082 : 1649 : if (tret != GS_ERROR)
8083 : : {
8084 : : /* Unlike output operands, memory inputs are not guaranteed
8085 : : to be lvalues by the FE, and while the expressions are
8086 : : marked addressable there, if it is e.g. a statement
8087 : : expression, temporaries in it might not end up being
8088 : : addressable. They might be already used in the IL and thus
8089 : : it is too late to make them addressable now though. */
8090 : 1593 : tree x = TREE_VALUE (link);
8091 : 1734 : while (handled_component_p (x))
8092 : 141 : x = TREE_OPERAND (x, 0);
8093 : 1593 : if (TREE_CODE (x) == MEM_REF
8094 : 1593 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
8095 : 0 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
8096 : 1593 : if ((VAR_P (x)
8097 : : || TREE_CODE (x) == PARM_DECL
8098 : : || TREE_CODE (x) == RESULT_DECL)
8099 : 1204 : && !TREE_ADDRESSABLE (x)
8100 : 23 : && is_gimple_reg (x))
8101 : : {
8102 : 17 : warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
8103 : 17 : input_location), 0,
8104 : : "memory input %d is not directly addressable",
8105 : : i);
8106 : 17 : prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
8107 : : }
8108 : : }
8109 : 1649 : mark_addressable (TREE_VALUE (link));
8110 : 1649 : if (tret == GS_ERROR)
8111 : : {
8112 : 56 : if (inputv != error_mark_node)
8113 : 46 : error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
8114 : : "memory input %d is not directly addressable", i);
8115 : : ret = tret;
8116 : : }
8117 : : }
8118 : : else
8119 : : {
8120 : 37661 : tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
8121 : : is_gimple_asm_val, fb_rvalue);
8122 : 37661 : if (tret == GS_ERROR)
8123 : 91 : ret = tret;
8124 : : }
8125 : :
8126 : 39310 : TREE_CHAIN (link) = NULL_TREE;
8127 : 39310 : vec_safe_push (inputs, link);
8128 : : }
8129 : :
8130 : 94961 : link_next = NULL_TREE;
8131 : 189341 : for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
8132 : : {
8133 : 94380 : link_next = TREE_CHAIN (link);
8134 : 94380 : TREE_CHAIN (link) = NULL_TREE;
8135 : 94380 : vec_safe_push (clobbers, link);
8136 : : }
8137 : :
8138 : 94961 : link_next = NULL_TREE;
8139 : 95724 : for (link = ASM_LABELS (expr); link; ++i, link = link_next)
8140 : : {
8141 : 763 : link_next = TREE_CHAIN (link);
8142 : 763 : TREE_CHAIN (link) = NULL_TREE;
8143 : 763 : vec_safe_push (labels, link);
8144 : : }
8145 : :
8146 : : /* Do not add ASMs with errors to the gimple IL stream. */
8147 : 94961 : if (ret != GS_ERROR)
8148 : : {
8149 : 94856 : stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
8150 : : inputs, outputs, clobbers, labels);
8151 : :
8152 : : /* asm is volatile if it was marked by the user as volatile or
8153 : : there are no outputs or this is an asm goto. */
8154 : 189712 : gimple_asm_set_volatile (stmt,
8155 : 94856 : ASM_VOLATILE_P (expr)
8156 : 7359 : || noutputs == 0
8157 : 102193 : || labels);
8158 : 94856 : gimple_asm_set_basic (stmt, ASM_BASIC_P (expr));
8159 : 94856 : gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
8160 : :
8161 : 94856 : gimplify_seq_add_stmt (pre_p, stmt);
8162 : : }
8163 : :
8164 : : return ret;
8165 : : }
8166 : :
8167 : : /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
8168 : : GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
8169 : : gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
8170 : : return to this function.
8171 : :
8172 : : FIXME should we complexify the prequeue handling instead? Or use flags
8173 : : for all the cleanups and let the optimizer tighten them up? The current
8174 : : code seems pretty fragile; it will break on a cleanup within any
8175 : : non-conditional nesting. But any such nesting would be broken, anyway;
8176 : : we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
8177 : : and continues out of it. We can do that at the RTL level, though, so
8178 : : having an optimizer to tighten up try/finally regions would be a Good
8179 : : Thing. */
8180 : :
8181 : : static enum gimplify_status
8182 : 5669363 : gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
8183 : : {
8184 : 5669363 : gimple_stmt_iterator iter;
8185 : 5669363 : gimple_seq body_sequence = NULL;
8186 : :
8187 : 5669363 : tree temp = voidify_wrapper_expr (*expr_p, NULL);
8188 : :
8189 : : /* We only care about the number of conditions between the innermost
8190 : : CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
8191 : : any cleanups collected outside the CLEANUP_POINT_EXPR. */
8192 : 5669363 : int old_conds = gimplify_ctxp->conditions;
8193 : 5669363 : gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
8194 : 5669363 : bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
8195 : 5669363 : gimplify_ctxp->conditions = 0;
8196 : 5669363 : gimplify_ctxp->conditional_cleanups = NULL;
8197 : 5669363 : gimplify_ctxp->in_cleanup_point_expr = true;
8198 : :
8199 : 5669363 : gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
8200 : :
8201 : 5669363 : gimplify_ctxp->conditions = old_conds;
8202 : 5669363 : gimplify_ctxp->conditional_cleanups = old_cleanups;
8203 : 5669363 : gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
8204 : :
8205 : 28079922 : for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
8206 : : {
8207 : 16776185 : gimple *wce = gsi_stmt (iter);
8208 : :
8209 : 16776185 : if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
8210 : : {
8211 : 636767 : if (gsi_one_before_end_p (iter))
8212 : : {
8213 : : /* Note that gsi_insert_seq_before and gsi_remove do not
8214 : : scan operands, unlike some other sequence mutators. */
8215 : 3564 : if (!gimple_wce_cleanup_eh_only (wce))
8216 : 3133 : gsi_insert_seq_before_without_update (&iter,
8217 : : gimple_wce_cleanup (wce),
8218 : : GSI_SAME_STMT);
8219 : 3564 : gsi_remove (&iter, true);
8220 : 3564 : break;
8221 : : }
8222 : : else
8223 : : {
8224 : 633203 : gtry *gtry;
8225 : 633203 : gimple_seq seq;
8226 : 633203 : enum gimple_try_flags kind;
8227 : :
8228 : 633203 : if (gimple_wce_cleanup_eh_only (wce))
8229 : : kind = GIMPLE_TRY_CATCH;
8230 : : else
8231 : 621024 : kind = GIMPLE_TRY_FINALLY;
8232 : 633203 : seq = gsi_split_seq_after (iter);
8233 : :
8234 : 633203 : gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
8235 : : /* Do not use gsi_replace here, as it may scan operands.
8236 : : We want to do a simple structural modification only. */
8237 : 633203 : gsi_set_stmt (&iter, gtry);
8238 : 1266406 : iter = gsi_start (gtry->eval);
8239 : : }
8240 : : }
8241 : : else
8242 : 16139418 : gsi_next (&iter);
8243 : : }
8244 : :
8245 : 5669363 : gimplify_seq_add_seq (pre_p, body_sequence);
8246 : 5669363 : if (temp)
8247 : : {
8248 : 414209 : *expr_p = temp;
8249 : 414209 : return GS_OK;
8250 : : }
8251 : : else
8252 : : {
8253 : 5255154 : *expr_p = NULL;
8254 : 5255154 : return GS_ALL_DONE;
8255 : : }
8256 : : }
8257 : :
8258 : : /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
8259 : : is the cleanup action required. EH_ONLY is true if the cleanup should
8260 : : only be executed if an exception is thrown, not on normal exit.
8261 : : If FORCE_UNCOND is true perform the cleanup unconditionally; this is
8262 : : only valid for clobbers. */
8263 : :
8264 : : static void
8265 : 641029 : gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
8266 : : bool force_uncond = false)
8267 : : {
8268 : 641029 : gimple *wce;
8269 : 641029 : gimple_seq cleanup_stmts = NULL;
8270 : :
8271 : : /* Errors can result in improperly nested cleanups. Which results in
8272 : : confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
8273 : 641029 : if (seen_error ())
8274 : 4262 : return;
8275 : :
8276 : 636767 : if (gimple_conditional_context ())
8277 : : {
8278 : : /* If we're in a conditional context, this is more complex. We only
8279 : : want to run the cleanup if we actually ran the initialization that
8280 : : necessitates it, but we want to run it after the end of the
8281 : : conditional context. So we wrap the try/finally around the
8282 : : condition and use a flag to determine whether or not to actually
8283 : : run the destructor. Thus
8284 : :
8285 : : test ? f(A()) : 0
8286 : :
8287 : : becomes (approximately)
8288 : :
8289 : : flag = 0;
8290 : : try {
8291 : : if (test) { A::A(temp); flag = 1; val = f(temp); }
8292 : : else { val = 0; }
8293 : : } finally {
8294 : : if (flag) A::~A(temp);
8295 : : }
8296 : : val
8297 : : */
8298 : 11987 : if (force_uncond)
8299 : : {
8300 : 11125 : gimplify_stmt (&cleanup, &cleanup_stmts);
8301 : 11125 : wce = gimple_build_wce (cleanup_stmts);
8302 : 11125 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8303 : : }
8304 : : else
8305 : : {
8306 : 862 : tree flag = create_tmp_var (boolean_type_node, "cleanup");
8307 : 862 : gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
8308 : 862 : gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
8309 : :
8310 : 862 : cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
8311 : 862 : gimplify_stmt (&cleanup, &cleanup_stmts);
8312 : 862 : wce = gimple_build_wce (cleanup_stmts);
8313 : 862 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8314 : :
8315 : 862 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
8316 : 862 : gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
8317 : 862 : gimplify_seq_add_stmt (pre_p, ftrue);
8318 : :
8319 : : /* Because of this manipulation, and the EH edges that jump
8320 : : threading cannot redirect, the temporary (VAR) will appear
8321 : : to be used uninitialized. Don't warn. */
8322 : 862 : suppress_warning (var, OPT_Wuninitialized);
8323 : : }
8324 : : }
8325 : : else
8326 : : {
8327 : 624780 : gimplify_stmt (&cleanup, &cleanup_stmts);
8328 : 624780 : wce = gimple_build_wce (cleanup_stmts);
8329 : 624780 : gimple_wce_set_cleanup_eh_only (wce, eh_only);
8330 : 624780 : gimplify_seq_add_stmt (pre_p, wce);
8331 : : }
8332 : : }
8333 : :
8334 : : /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
8335 : :
8336 : : static enum gimplify_status
8337 : 828254 : gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8338 : : {
8339 : 828254 : tree targ = *expr_p;
8340 : 828254 : tree temp = TARGET_EXPR_SLOT (targ);
8341 : 828254 : tree init = TARGET_EXPR_INITIAL (targ);
8342 : 828254 : enum gimplify_status ret;
8343 : :
8344 : 828254 : bool unpoison_empty_seq = false;
8345 : 828254 : gimple_stmt_iterator unpoison_it;
8346 : :
8347 : 828254 : if (init)
8348 : : {
8349 : 792101 : gimple_seq init_pre_p = NULL;
8350 : :
8351 : : /* TARGET_EXPR temps aren't part of the enclosing block, so add it
8352 : : to the temps list. Handle also variable length TARGET_EXPRs. */
8353 : 792101 : if (!poly_int_tree_p (DECL_SIZE (temp)))
8354 : : {
8355 : 42 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
8356 : 1 : gimplify_type_sizes (TREE_TYPE (temp), &init_pre_p);
8357 : : /* FIXME: this is correct only when the size of the type does
8358 : : not depend on expressions evaluated in init. */
8359 : 42 : gimplify_vla_decl (temp, &init_pre_p);
8360 : : }
8361 : : else
8362 : : {
8363 : : /* Save location where we need to place unpoisoning. It's possible
8364 : : that a variable will be converted to needs_to_live_in_memory. */
8365 : 792059 : unpoison_it = gsi_last (*pre_p);
8366 : 792059 : unpoison_empty_seq = gsi_end_p (unpoison_it);
8367 : :
8368 : 792059 : gimple_add_tmp_var (temp);
8369 : : }
8370 : :
8371 : : /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
8372 : : expression is supposed to initialize the slot. */
8373 : 792101 : if (VOID_TYPE_P (TREE_TYPE (init)))
8374 : 339693 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8375 : : fb_none);
8376 : : else
8377 : : {
8378 : 452408 : tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
8379 : 452408 : init = init_expr;
8380 : 452408 : ret = gimplify_expr (&init, &init_pre_p, post_p, is_gimple_stmt,
8381 : : fb_none);
8382 : 452408 : init = NULL;
8383 : 452408 : ggc_free (init_expr);
8384 : : }
8385 : 792101 : if (ret == GS_ERROR)
8386 : : {
8387 : : /* PR c++/28266 Make sure this is expanded only once. */
8388 : 15 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8389 : 15 : return GS_ERROR;
8390 : : }
8391 : :
8392 : 792086 : if (init)
8393 : 0 : gimplify_and_add (init, &init_pre_p);
8394 : :
8395 : : /* Add a clobber for the temporary going out of scope, like
8396 : : gimplify_bind_expr. But only if we did not promote the
8397 : : temporary to static storage. */
8398 : 792086 : if (gimplify_ctxp->in_cleanup_point_expr
8399 : 644388 : && !TREE_STATIC (temp)
8400 : 1435077 : && needs_to_live_in_memory (temp))
8401 : : {
8402 : 503041 : if (flag_stack_reuse == SR_ALL)
8403 : : {
8404 : 502605 : tree clobber = build_clobber (TREE_TYPE (temp),
8405 : : CLOBBER_STORAGE_END);
8406 : 502605 : clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
8407 : 502605 : gimple_push_cleanup (temp, clobber, false, pre_p, true);
8408 : : }
8409 : 503041 : if (asan_poisoned_variables
8410 : 436 : && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
8411 : 436 : && !TREE_STATIC (temp)
8412 : 436 : && dbg_cnt (asan_use_after_scope)
8413 : 503477 : && !gimplify_omp_ctxp)
8414 : : {
8415 : 428 : tree asan_cleanup = build_asan_poison_call_expr (temp);
8416 : 428 : if (asan_cleanup)
8417 : : {
8418 : 428 : if (unpoison_empty_seq)
8419 : 216 : unpoison_it = gsi_start (*pre_p);
8420 : :
8421 : 428 : asan_poison_variable (temp, false, &unpoison_it,
8422 : : unpoison_empty_seq);
8423 : 428 : gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
8424 : : }
8425 : : }
8426 : : }
8427 : :
8428 : 792086 : gimple_seq_add_seq (pre_p, init_pre_p);
8429 : :
8430 : : /* If needed, push the cleanup for the temp. */
8431 : 792086 : if (TARGET_EXPR_CLEANUP (targ))
8432 : 137996 : gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
8433 : 137996 : CLEANUP_EH_ONLY (targ), pre_p);
8434 : :
8435 : : /* Only expand this once. */
8436 : 792086 : TREE_OPERAND (targ, 3) = init;
8437 : 792086 : TARGET_EXPR_INITIAL (targ) = NULL_TREE;
8438 : : }
8439 : : else
8440 : : /* We should have expanded this before. */
8441 : 36153 : gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
8442 : :
8443 : 828239 : *expr_p = temp;
8444 : 828239 : return GS_OK;
8445 : : }
8446 : :
8447 : : /* Gimplification of expression trees. */
8448 : :
8449 : : /* Gimplify an expression which appears at statement context. The
8450 : : corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
8451 : : NULL, a new sequence is allocated.
8452 : :
8453 : : Return true if we actually added a statement to the queue. */
8454 : :
8455 : : bool
8456 : 102248576 : gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
8457 : : {
8458 : 102248576 : gimple_seq_node last;
8459 : :
8460 : 102248576 : last = gimple_seq_last (*seq_p);
8461 : 102248576 : gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
8462 : 102248576 : return last != gimple_seq_last (*seq_p);
8463 : : }
8464 : :
8465 : : /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
8466 : : to CTX. If entries already exist, force them to be some flavor of private.
8467 : : If there is no enclosing parallel, do nothing. */
8468 : :
8469 : : void
8470 : 124099 : omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
8471 : : {
8472 : 124099 : splay_tree_node n;
8473 : :
8474 : 124099 : if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
8475 : : return;
8476 : :
8477 : 36472 : do
8478 : : {
8479 : 36472 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8480 : 36472 : if (n != NULL)
8481 : : {
8482 : 4269 : if (n->value & GOVD_SHARED)
8483 : 183 : n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
8484 : 4086 : else if (n->value & GOVD_MAP)
8485 : 55 : n->value |= GOVD_MAP_TO_ONLY;
8486 : : else
8487 : : return;
8488 : : }
8489 : 32203 : else if ((ctx->region_type & ORT_TARGET) != 0)
8490 : : {
8491 : 6775 : if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
8492 : 4020 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8493 : : else
8494 : 2755 : omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
8495 : : }
8496 : 25428 : else if (ctx->region_type != ORT_WORKSHARE
8497 : : && ctx->region_type != ORT_TASKGROUP
8498 : 23619 : && ctx->region_type != ORT_SIMD
8499 : 22995 : && ctx->region_type != ORT_ACC
8500 : 22885 : && !(ctx->region_type & ORT_TARGET_DATA))
8501 : 14304 : omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
8502 : :
8503 : 32441 : ctx = ctx->outer_context;
8504 : : }
8505 : 32441 : while (ctx);
8506 : : }
8507 : :
8508 : : /* Similarly for each of the type sizes of TYPE. */
8509 : :
8510 : : static void
8511 : 40651 : omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
8512 : : {
8513 : 40651 : if (type == NULL || type == error_mark_node)
8514 : : return;
8515 : 40647 : type = TYPE_MAIN_VARIANT (type);
8516 : :
8517 : 40647 : if (ctx->privatized_types->add (type))
8518 : : return;
8519 : :
8520 : 30901 : switch (TREE_CODE (type))
8521 : : {
8522 : 13073 : case INTEGER_TYPE:
8523 : 13073 : case ENUMERAL_TYPE:
8524 : 13073 : case BOOLEAN_TYPE:
8525 : 13073 : case REAL_TYPE:
8526 : 13073 : case FIXED_POINT_TYPE:
8527 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
8528 : 13073 : omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
8529 : 13073 : break;
8530 : :
8531 : 5384 : case ARRAY_TYPE:
8532 : 5384 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8533 : 5384 : omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
8534 : 5384 : break;
8535 : :
8536 : 2507 : case RECORD_TYPE:
8537 : 2507 : case UNION_TYPE:
8538 : 2507 : case QUAL_UNION_TYPE:
8539 : 2507 : {
8540 : 2507 : tree field;
8541 : 26707 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8542 : 24200 : if (TREE_CODE (field) == FIELD_DECL)
8543 : : {
8544 : 9004 : omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
8545 : 9004 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
8546 : : }
8547 : : }
8548 : : break;
8549 : :
8550 : 9289 : case POINTER_TYPE:
8551 : 9289 : case REFERENCE_TYPE:
8552 : 9289 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
8553 : 9289 : break;
8554 : :
8555 : : default:
8556 : : break;
8557 : : }
8558 : :
8559 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
8560 : 30901 : omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
8561 : 30901 : lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
8562 : : }
8563 : :
8564 : : /* Add an entry for DECL in the OMP context CTX with FLAGS. */
8565 : :
8566 : : static void
8567 : 700571 : omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
8568 : : {
8569 : 700571 : splay_tree_node n;
8570 : 700571 : unsigned int nflags;
8571 : 700571 : tree t;
8572 : :
8573 : 700571 : if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
8574 : : return;
8575 : :
8576 : : /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
8577 : : there are constructors involved somewhere. Exception is a shared clause,
8578 : : there is nothing privatized in that case. */
8579 : 700426 : if ((flags & GOVD_SHARED) == 0
8580 : 700426 : && (TREE_ADDRESSABLE (TREE_TYPE (decl))
8581 : 640259 : || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
8582 : 7784 : flags |= GOVD_SEEN;
8583 : :
8584 : 700426 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8585 : 700426 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8586 : : {
8587 : : /* We shouldn't be re-adding the decl with the same data
8588 : : sharing class. */
8589 : 821 : gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
8590 : 821 : nflags = n->value | flags;
8591 : : /* The only combination of data sharing classes we should see is
8592 : : FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
8593 : : reduction variables to be used in data sharing clauses. */
8594 : 821 : gcc_assert ((ctx->region_type & ORT_ACC) != 0
8595 : : || ((nflags & GOVD_DATA_SHARE_CLASS)
8596 : : == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
8597 : : || (flags & GOVD_DATA_SHARE_CLASS) == 0);
8598 : 821 : n->value = nflags;
8599 : 821 : return;
8600 : : }
8601 : :
8602 : : /* When adding a variable-sized variable, we have to handle all sorts
8603 : : of additional bits of data: the pointer replacement variable, and
8604 : : the parameters of the type. */
8605 : 699605 : if (DECL_SIZE (decl) && !poly_int_tree_p (DECL_SIZE (decl)))
8606 : : {
8607 : : /* Add the pointer replacement variable as PRIVATE if the variable
8608 : : replacement is private, else FIRSTPRIVATE since we'll need the
8609 : : address of the original variable either for SHARED, or for the
8610 : : copy into or out of the context. */
8611 : 1312 : if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
8612 : : {
8613 : 1222 : if (flags & GOVD_MAP)
8614 : : nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
8615 : 515 : else if (flags & GOVD_PRIVATE)
8616 : : nflags = GOVD_PRIVATE;
8617 : 431 : else if (((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
8618 : 48 : && (flags & GOVD_FIRSTPRIVATE))
8619 : 423 : || (ctx->region_type == ORT_TARGET_DATA
8620 : 6 : && (flags & GOVD_DATA_SHARE_CLASS) == 0))
8621 : : nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
8622 : : else
8623 : 1222 : nflags = GOVD_FIRSTPRIVATE;
8624 : 1222 : nflags |= flags & GOVD_SEEN;
8625 : 1222 : t = DECL_VALUE_EXPR (decl);
8626 : 1222 : gcc_assert (INDIRECT_REF_P (t));
8627 : 1222 : t = TREE_OPERAND (t, 0);
8628 : 1222 : gcc_assert (DECL_P (t));
8629 : 1222 : omp_add_variable (ctx, t, nflags);
8630 : : }
8631 : :
8632 : : /* Add all of the variable and type parameters (which should have
8633 : : been gimplified to a formal temporary) as FIRSTPRIVATE. */
8634 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
8635 : 1312 : omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
8636 : 1312 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8637 : :
8638 : : /* The variable-sized variable itself is never SHARED, only some form
8639 : : of PRIVATE. The sharing would take place via the pointer variable
8640 : : which we remapped above. */
8641 : 1312 : if (flags & GOVD_SHARED)
8642 : 276 : flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
8643 : 276 : | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
8644 : :
8645 : : /* We're going to make use of the TYPE_SIZE_UNIT at least in the
8646 : : alloca statement we generate for the variable, so make sure it
8647 : : is available. This isn't automatically needed for the SHARED
8648 : : case, since we won't be allocating local storage then.
8649 : : For local variables TYPE_SIZE_UNIT might not be gimplified yet,
8650 : : in this case omp_notice_variable will be called later
8651 : : on when it is gimplified. */
8652 : 1036 : else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
8653 : 1036 : && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
8654 : 267 : omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
8655 : : }
8656 : 698293 : else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
8657 : 698293 : && omp_privatize_by_reference (decl))
8658 : : {
8659 : 10278 : omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
8660 : :
8661 : : /* Similar to the direct variable sized case above, we'll need the
8662 : : size of references being privatized. */
8663 : 10278 : if ((flags & GOVD_SHARED) == 0)
8664 : : {
8665 : 6487 : t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
8666 : 6487 : if (t && DECL_P (t))
8667 : 1337 : omp_notice_variable (ctx, t, true);
8668 : : }
8669 : : }
8670 : :
8671 : 699605 : if (n != NULL)
8672 : 1803 : n->value |= flags;
8673 : : else
8674 : 697802 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
8675 : :
8676 : : /* For reductions clauses in OpenACC loop directives, by default create a
8677 : : copy clause on the enclosing parallel construct for carrying back the
8678 : : results. */
8679 : 699605 : if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
8680 : : {
8681 : 4715 : struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
8682 : 6454 : while (outer_ctx)
8683 : : {
8684 : 5787 : n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
8685 : 5787 : if (n != NULL)
8686 : : {
8687 : : /* Ignore local variables and explicitly declared clauses. */
8688 : 3989 : if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
8689 : : break;
8690 : 444 : else if (outer_ctx->region_type == ORT_ACC_KERNELS)
8691 : : {
8692 : : /* According to the OpenACC spec, such a reduction variable
8693 : : should already have a copy map on a kernels construct,
8694 : : verify that here. */
8695 : 144 : gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
8696 : : && (n->value & GOVD_MAP));
8697 : : }
8698 : 300 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8699 : : {
8700 : : /* Remove firstprivate and make it a copy map. */
8701 : 150 : n->value &= ~GOVD_FIRSTPRIVATE;
8702 : 150 : n->value |= GOVD_MAP;
8703 : : }
8704 : : }
8705 : 1798 : else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
8706 : : {
8707 : 503 : splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
8708 : : GOVD_MAP | GOVD_SEEN);
8709 : 503 : break;
8710 : : }
8711 : 1739 : outer_ctx = outer_ctx->outer_context;
8712 : : }
8713 : : }
8714 : : }
8715 : :
8716 : : /* Notice a threadprivate variable DECL used in OMP context CTX.
8717 : : This just prints out diagnostics about threadprivate variable uses
8718 : : in untied tasks. If DECL2 is non-NULL, prevent this warning
8719 : : on that variable. */
8720 : :
8721 : : static bool
8722 : 14183 : omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
8723 : : tree decl2)
8724 : : {
8725 : 14183 : splay_tree_node n;
8726 : 14183 : struct gimplify_omp_ctx *octx;
8727 : :
8728 : 28798 : for (octx = ctx; octx; octx = octx->outer_context)
8729 : 14615 : if ((octx->region_type & ORT_TARGET) != 0
8730 : 14615 : || octx->order_concurrent)
8731 : : {
8732 : 112 : n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
8733 : 112 : if (n == NULL)
8734 : : {
8735 : 20 : if (octx->order_concurrent)
8736 : : {
8737 : 20 : error ("threadprivate variable %qE used in a region with"
8738 : 20 : " %<order(concurrent)%> clause", DECL_NAME (decl));
8739 : 20 : inform (octx->location, "enclosing region");
8740 : : }
8741 : : else
8742 : : {
8743 : 0 : error ("threadprivate variable %qE used in target region",
8744 : 0 : DECL_NAME (decl));
8745 : 0 : inform (octx->location, "enclosing target region");
8746 : : }
8747 : 20 : splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
8748 : : }
8749 : 112 : if (decl2)
8750 : 0 : splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
8751 : : }
8752 : :
8753 : 14183 : if (ctx->region_type != ORT_UNTIED_TASK)
8754 : : return false;
8755 : 37 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8756 : 37 : if (n == NULL)
8757 : : {
8758 : 6 : error ("threadprivate variable %qE used in untied task",
8759 : 6 : DECL_NAME (decl));
8760 : 6 : inform (ctx->location, "enclosing task");
8761 : 6 : splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
8762 : : }
8763 : 37 : if (decl2)
8764 : 4 : splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
8765 : : return false;
8766 : : }
8767 : :
8768 : : /* Return true if global var DECL is device resident. */
8769 : :
8770 : : static bool
8771 : 638 : device_resident_p (tree decl)
8772 : : {
8773 : 638 : tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
8774 : :
8775 : 638 : if (!attr)
8776 : : return false;
8777 : :
8778 : 0 : for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
8779 : : {
8780 : 0 : tree c = TREE_VALUE (t);
8781 : 0 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
8782 : : return true;
8783 : : }
8784 : :
8785 : : return false;
8786 : : }
8787 : :
8788 : : /* Return true if DECL has an ACC DECLARE attribute. */
8789 : :
8790 : : static bool
8791 : 9118 : is_oacc_declared (tree decl)
8792 : : {
8793 : 9118 : tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
8794 : 9118 : tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
8795 : 9118 : return declared != NULL_TREE;
8796 : : }
8797 : :
8798 : : /* Determine outer default flags for DECL mentioned in an OMP region
8799 : : but not declared in an enclosing clause.
8800 : :
8801 : : ??? Some compiler-generated variables (like SAVE_EXPRs) could be
8802 : : remapped firstprivate instead of shared. To some extent this is
8803 : : addressed in omp_firstprivatize_type_sizes, but not
8804 : : effectively. */
8805 : :
8806 : : static unsigned
8807 : 47830 : omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
8808 : : bool in_code, unsigned flags)
8809 : : {
8810 : 47830 : enum omp_clause_default_kind default_kind = ctx->default_kind;
8811 : 47830 : enum omp_clause_default_kind kind;
8812 : :
8813 : 47830 : kind = lang_hooks.decls.omp_predetermined_sharing (decl);
8814 : 47830 : if (ctx->region_type & ORT_TASK)
8815 : : {
8816 : 3980 : tree detach_clause = omp_find_clause (ctx->clauses, OMP_CLAUSE_DETACH);
8817 : :
8818 : : /* The event-handle specified by a detach clause should always be firstprivate,
8819 : : regardless of the current default. */
8820 : 4192 : if (detach_clause && OMP_CLAUSE_DECL (detach_clause) == decl)
8821 : : kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
8822 : : }
8823 : 47830 : if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8824 : : default_kind = kind;
8825 : 45027 : else if (VAR_P (decl) && TREE_STATIC (decl) && DECL_IN_CONSTANT_POOL (decl))
8826 : : default_kind = OMP_CLAUSE_DEFAULT_SHARED;
8827 : : /* For C/C++ default({,first}private), variables with static storage duration
8828 : : declared in a namespace or global scope and referenced in construct
8829 : : must be explicitly specified, i.e. acts as default(none). */
8830 : 45026 : else if ((default_kind == OMP_CLAUSE_DEFAULT_PRIVATE
8831 : 45026 : || default_kind == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
8832 : 360 : && VAR_P (decl)
8833 : 321 : && is_global_var (decl)
8834 : 184 : && (DECL_FILE_SCOPE_P (decl)
8835 : 104 : || (DECL_CONTEXT (decl)
8836 : 104 : && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
8837 : 45154 : && !lang_GNU_Fortran ())
8838 : : default_kind = OMP_CLAUSE_DEFAULT_NONE;
8839 : :
8840 : 47701 : switch (default_kind)
8841 : : {
8842 : 267 : case OMP_CLAUSE_DEFAULT_NONE:
8843 : 267 : {
8844 : 267 : const char *rtype;
8845 : :
8846 : 267 : if (ctx->region_type & ORT_PARALLEL)
8847 : : rtype = "parallel";
8848 : 108 : else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
8849 : : rtype = "taskloop";
8850 : 72 : else if (ctx->region_type & ORT_TASK)
8851 : : rtype = "task";
8852 : 36 : else if (ctx->region_type & ORT_TEAMS)
8853 : : rtype = "teams";
8854 : : else
8855 : 0 : gcc_unreachable ();
8856 : :
8857 : 267 : error ("%qE not specified in enclosing %qs",
8858 : 267 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
8859 : 267 : inform (ctx->location, "enclosing %qs", rtype);
8860 : : }
8861 : : /* FALLTHRU */
8862 : 44195 : case OMP_CLAUSE_DEFAULT_SHARED:
8863 : 44195 : flags |= GOVD_SHARED;
8864 : 44195 : break;
8865 : 155 : case OMP_CLAUSE_DEFAULT_PRIVATE:
8866 : 155 : flags |= GOVD_PRIVATE;
8867 : 155 : break;
8868 : 266 : case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
8869 : 266 : flags |= GOVD_FIRSTPRIVATE;
8870 : 266 : break;
8871 : 3214 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8872 : : /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
8873 : 3214 : gcc_assert ((ctx->region_type & ORT_TASK) != 0);
8874 : 3214 : if (struct gimplify_omp_ctx *octx = ctx->outer_context)
8875 : : {
8876 : 2158 : omp_notice_variable (octx, decl, in_code);
8877 : 2986 : for (; octx; octx = octx->outer_context)
8878 : : {
8879 : 2930 : splay_tree_node n2;
8880 : :
8881 : 2930 : n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
8882 : 2930 : if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
8883 : 5 : && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
8884 : 5 : continue;
8885 : 2925 : if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
8886 : : {
8887 : 896 : flags |= GOVD_FIRSTPRIVATE;
8888 : 896 : goto found_outer;
8889 : : }
8890 : 2029 : if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
8891 : : {
8892 : 1206 : flags |= GOVD_SHARED;
8893 : 1206 : goto found_outer;
8894 : : }
8895 : : }
8896 : : }
8897 : :
8898 : 1112 : if (TREE_CODE (decl) == PARM_DECL
8899 : 1112 : || (!is_global_var (decl)
8900 : 296 : && DECL_CONTEXT (decl) == current_function_decl))
8901 : 735 : flags |= GOVD_FIRSTPRIVATE;
8902 : : else
8903 : 377 : flags |= GOVD_SHARED;
8904 : 47830 : found_outer:
8905 : : break;
8906 : :
8907 : 0 : default:
8908 : 0 : gcc_unreachable ();
8909 : : }
8910 : :
8911 : 47830 : return flags;
8912 : : }
8913 : :
8914 : : /* Return string name for types of OpenACC constructs from ORT_* values. */
8915 : :
8916 : : static const char *
8917 : 570 : oacc_region_type_name (enum omp_region_type region_type)
8918 : : {
8919 : 570 : switch (region_type)
8920 : : {
8921 : : case ORT_ACC_DATA:
8922 : : return "data";
8923 : 140 : case ORT_ACC_PARALLEL:
8924 : 140 : return "parallel";
8925 : 140 : case ORT_ACC_KERNELS:
8926 : 140 : return "kernels";
8927 : 140 : case ORT_ACC_SERIAL:
8928 : 140 : return "serial";
8929 : 0 : default:
8930 : 0 : gcc_unreachable ();
8931 : : }
8932 : : }
8933 : :
8934 : : /* Determine outer default flags for DECL mentioned in an OACC region
8935 : : but not declared in an enclosing clause. */
8936 : :
8937 : : static unsigned
8938 : 8856 : oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
8939 : : {
8940 : 8856 : struct gimplify_omp_ctx *ctx_default = ctx;
8941 : : /* If no 'default' clause appears on this compute construct... */
8942 : 8856 : if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED)
8943 : : {
8944 : : /* ..., see if one appears on a lexically containing 'data'
8945 : : construct. */
8946 : 9635 : while ((ctx_default = ctx_default->outer_context))
8947 : : {
8948 : 1534 : if (ctx_default->region_type == ORT_ACC_DATA
8949 : 1534 : && ctx_default->default_kind != OMP_CLAUSE_DEFAULT_SHARED)
8950 : : break;
8951 : : }
8952 : : /* If not, reset. */
8953 : 8277 : if (!ctx_default)
8954 : 8680 : ctx_default = ctx;
8955 : : }
8956 : :
8957 : 8856 : bool on_device = false;
8958 : 8856 : bool is_private = false;
8959 : 8856 : bool declared = is_oacc_declared (decl);
8960 : 8856 : tree type = TREE_TYPE (decl);
8961 : :
8962 : 8856 : if (omp_privatize_by_reference (decl))
8963 : 338 : type = TREE_TYPE (type);
8964 : :
8965 : : /* For Fortran COMMON blocks, only used variables in those blocks are
8966 : : transfered and remapped. The block itself will have a private clause to
8967 : : avoid transfering the data twice.
8968 : : The hook evaluates to false by default. For a variable in Fortran's COMMON
8969 : : or EQUIVALENCE block, returns 'true' (as we have shared=false) - as only
8970 : : the variables in such a COMMON/EQUIVALENCE block shall be privatized not
8971 : : the whole block. For C++ and Fortran, it can also be true under certain
8972 : : other conditions, if DECL_HAS_VALUE_EXPR. */
8973 : 8856 : if (RECORD_OR_UNION_TYPE_P (type))
8974 : 970 : is_private = lang_hooks.decls.omp_disregard_value_expr (decl, false);
8975 : :
8976 : 8856 : if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
8977 : 8856 : && is_global_var (decl)
8978 : 638 : && device_resident_p (decl)
8979 : 8856 : && !is_private)
8980 : : {
8981 : 0 : on_device = true;
8982 : 0 : flags |= GOVD_MAP_TO_ONLY;
8983 : : }
8984 : :
8985 : 8856 : switch (ctx->region_type)
8986 : : {
8987 : 1491 : case ORT_ACC_KERNELS:
8988 : 1491 : if (is_private)
8989 : 0 : flags |= GOVD_FIRSTPRIVATE;
8990 : 1491 : else if (AGGREGATE_TYPE_P (type))
8991 : : {
8992 : : /* Aggregates default to 'present_or_copy', or 'present'. */
8993 : 429 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
8994 : 411 : flags |= GOVD_MAP;
8995 : : else
8996 : 18 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
8997 : : }
8998 : : else
8999 : : /* Scalars default to 'copy'. */
9000 : 1062 : flags |= GOVD_MAP | GOVD_MAP_FORCE;
9001 : :
9002 : : break;
9003 : :
9004 : 7365 : case ORT_ACC_PARALLEL:
9005 : 7365 : case ORT_ACC_SERIAL:
9006 : 7365 : if (is_private)
9007 : 0 : flags |= GOVD_FIRSTPRIVATE;
9008 : 7365 : else if (on_device || declared)
9009 : 17 : flags |= GOVD_MAP;
9010 : 7348 : else if (AGGREGATE_TYPE_P (type))
9011 : : {
9012 : : /* Aggregates default to 'present_or_copy', or 'present'. */
9013 : 3685 : if (ctx_default->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
9014 : 3419 : flags |= GOVD_MAP;
9015 : : else
9016 : 266 : flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
9017 : : }
9018 : : else
9019 : : /* Scalars default to 'firstprivate'. */
9020 : 3663 : flags |= GOVD_FIRSTPRIVATE;
9021 : :
9022 : : break;
9023 : :
9024 : 0 : default:
9025 : 0 : gcc_unreachable ();
9026 : : }
9027 : :
9028 : 8856 : if (DECL_ARTIFICIAL (decl))
9029 : : ; /* We can get compiler-generated decls, and should not complain
9030 : : about them. */
9031 : 8548 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_NONE)
9032 : : {
9033 : 420 : error ("%qE not specified in enclosing OpenACC %qs construct",
9034 : 210 : DECL_NAME (lang_hooks.decls.omp_report_decl (decl)),
9035 : : oacc_region_type_name (ctx->region_type));
9036 : 210 : if (ctx_default != ctx)
9037 : 150 : inform (ctx->location, "enclosing OpenACC %qs construct and",
9038 : : oacc_region_type_name (ctx->region_type));
9039 : 210 : inform (ctx_default->location,
9040 : : "enclosing OpenACC %qs construct with %qs clause",
9041 : : oacc_region_type_name (ctx_default->region_type),
9042 : : "default(none)");
9043 : : }
9044 : 8338 : else if (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
9045 : : ; /* Handled above. */
9046 : : else
9047 : 7805 : gcc_checking_assert (ctx_default->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
9048 : :
9049 : 8856 : return flags;
9050 : : }
9051 : :
9052 : : /* Record the fact that DECL was used within the OMP context CTX.
9053 : : IN_CODE is true when real code uses DECL, and false when we should
9054 : : merely emit default(none) errors. Return true if DECL is going to
9055 : : be remapped and thus DECL shouldn't be gimplified into its
9056 : : DECL_VALUE_EXPR (if any). */
9057 : :
9058 : : static bool
9059 : 3826544 : omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
9060 : : {
9061 : 3826544 : splay_tree_node n;
9062 : 3826544 : unsigned flags = in_code ? GOVD_SEEN : 0;
9063 : 3826544 : bool ret = false, shared;
9064 : :
9065 : 3826544 : if (error_operand_p (decl))
9066 : : return false;
9067 : :
9068 : 3826544 : if (DECL_ARTIFICIAL (decl))
9069 : : {
9070 : 2191498 : tree attr = lookup_attribute ("omp allocate var", DECL_ATTRIBUTES (decl));
9071 : 2191498 : if (attr)
9072 : 531 : decl = TREE_VALUE (TREE_VALUE (attr));
9073 : : }
9074 : :
9075 : 3826544 : if (ctx->region_type == ORT_NONE)
9076 : 156 : return lang_hooks.decls.omp_disregard_value_expr (decl, false);
9077 : :
9078 : 3826388 : if (is_global_var (decl))
9079 : : {
9080 : : /* Threadprivate variables are predetermined. */
9081 : 469536 : if (DECL_THREAD_LOCAL_P (decl))
9082 : 13565 : return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
9083 : :
9084 : 455971 : if (DECL_HAS_VALUE_EXPR_P (decl))
9085 : : {
9086 : 4380 : if (ctx->region_type & ORT_ACC)
9087 : : /* For OpenACC, defer expansion of value to avoid transfering
9088 : : privatized common block data instead of im-/explicitly transfered
9089 : : variables which are in common blocks. */
9090 : : ;
9091 : : else
9092 : : {
9093 : 2104 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
9094 : :
9095 : 2104 : if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
9096 : 618 : return omp_notice_threadprivate_variable (ctx, decl, value);
9097 : : }
9098 : : }
9099 : :
9100 : 455353 : if (gimplify_omp_ctxp->outer_context == NULL
9101 : 83316 : && VAR_P (decl)
9102 : 538669 : && oacc_get_fn_attrib (current_function_decl))
9103 : : {
9104 : 765 : location_t loc = DECL_SOURCE_LOCATION (decl);
9105 : :
9106 : 765 : if (lookup_attribute ("omp declare target link",
9107 : 765 : DECL_ATTRIBUTES (decl)))
9108 : : {
9109 : 48 : error_at (loc,
9110 : : "%qE with %<link%> clause used in %<routine%> function",
9111 : 24 : DECL_NAME (decl));
9112 : 24 : return false;
9113 : : }
9114 : 741 : else if (!lookup_attribute ("omp declare target",
9115 : 741 : DECL_ATTRIBUTES (decl)))
9116 : : {
9117 : 132 : error_at (loc,
9118 : : "%qE requires a %<declare%> directive for use "
9119 : 66 : "in a %<routine%> function", DECL_NAME (decl));
9120 : 66 : return false;
9121 : : }
9122 : : }
9123 : : }
9124 : :
9125 : 3812115 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9126 : 3812115 : if ((ctx->region_type & ORT_TARGET) != 0)
9127 : : {
9128 : 773898 : if (n == NULL)
9129 : : {
9130 : 44895 : unsigned nflags = flags;
9131 : 44895 : if ((ctx->region_type & ORT_ACC) == 0)
9132 : : {
9133 : 33923 : bool is_declare_target = false;
9134 : 33923 : if (is_global_var (decl)
9135 : 33923 : && varpool_node::get_create (decl)->offloadable)
9136 : : {
9137 : 7004 : struct gimplify_omp_ctx *octx;
9138 : 7004 : for (octx = ctx->outer_context;
9139 : 7012 : octx; octx = octx->outer_context)
9140 : : {
9141 : 9 : n = splay_tree_lookup (octx->variables,
9142 : : (splay_tree_key)decl);
9143 : 9 : if (n
9144 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
9145 : 9 : && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9146 : : break;
9147 : : }
9148 : 7004 : is_declare_target = octx == NULL;
9149 : : }
9150 : 7004 : if (!is_declare_target)
9151 : : {
9152 : 26920 : int gdmk;
9153 : 26920 : enum omp_clause_defaultmap_kind kind;
9154 : 26920 : if (lang_hooks.decls.omp_allocatable_p (decl))
9155 : : gdmk = GDMK_ALLOCATABLE;
9156 : 26564 : else if (lang_hooks.decls.omp_scalar_target_p (decl))
9157 : : gdmk = GDMK_SCALAR_TARGET;
9158 : 26489 : else if (lang_hooks.decls.omp_scalar_p (decl, false))
9159 : : gdmk = GDMK_SCALAR;
9160 : 11534 : else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9161 : 11534 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9162 : 3127 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9163 : : == POINTER_TYPE)))
9164 : : gdmk = GDMK_POINTER;
9165 : : else
9166 : : gdmk = GDMK_AGGREGATE;
9167 : 26920 : kind = lang_hooks.decls.omp_predetermined_mapping (decl);
9168 : 26920 : if (kind != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
9169 : : {
9170 : 944 : if (kind == OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE)
9171 : 101 : nflags |= GOVD_FIRSTPRIVATE;
9172 : 843 : else if (kind == OMP_CLAUSE_DEFAULTMAP_TO)
9173 : 843 : nflags |= GOVD_MAP | GOVD_MAP_TO_ONLY;
9174 : : else
9175 : 0 : gcc_unreachable ();
9176 : : }
9177 : 25976 : else if (ctx->defaultmap[gdmk] == 0)
9178 : : {
9179 : 75 : tree d = lang_hooks.decls.omp_report_decl (decl);
9180 : 75 : error ("%qE not specified in enclosing %<target%>",
9181 : 75 : DECL_NAME (d));
9182 : 75 : inform (ctx->location, "enclosing %<target%>");
9183 : : }
9184 : 25901 : else if (ctx->defaultmap[gdmk]
9185 : 25901 : & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
9186 : 8936 : nflags |= ctx->defaultmap[gdmk];
9187 : 16965 : else if (ctx->defaultmap[gdmk] & GOVD_MAP_FORCE_PRESENT)
9188 : : {
9189 : 42 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9190 : 42 : nflags |= ctx->defaultmap[gdmk] | GOVD_MAP_ALLOC_ONLY;
9191 : : }
9192 : : else
9193 : : {
9194 : 16923 : gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
9195 : 16923 : nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
9196 : : }
9197 : : }
9198 : : }
9199 : :
9200 : 44895 : struct gimplify_omp_ctx *octx = ctx->outer_context;
9201 : 44895 : if ((ctx->region_type & ORT_ACC) && octx)
9202 : : {
9203 : : /* Look in outer OpenACC contexts, to see if there's a
9204 : : data attribute for this variable. */
9205 : 3536 : omp_notice_variable (octx, decl, in_code);
9206 : :
9207 : 5308 : for (; octx; octx = octx->outer_context)
9208 : : {
9209 : 3884 : if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
9210 : : break;
9211 : 3884 : splay_tree_node n2
9212 : 3884 : = splay_tree_lookup (octx->variables,
9213 : : (splay_tree_key) decl);
9214 : 3884 : if (n2)
9215 : : {
9216 : 2112 : if (octx->region_type == ORT_ACC_HOST_DATA)
9217 : 4 : error ("variable %qE declared in enclosing "
9218 : 4 : "%<host_data%> region", DECL_NAME (decl));
9219 : 2112 : nflags |= GOVD_MAP;
9220 : 2112 : if (octx->region_type == ORT_ACC_DATA
9221 : 2108 : && (n2->value & GOVD_MAP_0LEN_ARRAY))
9222 : 288 : nflags |= GOVD_MAP_0LEN_ARRAY;
9223 : 2112 : goto found_outer;
9224 : : }
9225 : : }
9226 : : }
9227 : :
9228 : 42783 : if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
9229 : : | GOVD_MAP_ALLOC_ONLY)) == flags)
9230 : : {
9231 : 32861 : tree type = TREE_TYPE (decl);
9232 : 32861 : location_t loc = DECL_SOURCE_LOCATION (decl);
9233 : :
9234 : 32861 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9235 : 32861 : && omp_privatize_by_reference (decl))
9236 : 29 : type = TREE_TYPE (type);
9237 : :
9238 : 32861 : if (!verify_type_context (loc, TCTX_OMP_MAP_IMP_REF, type))
9239 : : /* Check if TYPE can appear in a target region.
9240 : : verify_type_context has already issued an error if it
9241 : : can't. */
9242 : 0 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9243 : 32861 : else if (!omp_mappable_type (type))
9244 : : {
9245 : 8 : error ("%qD referenced in target region does not have "
9246 : : "a mappable type", decl);
9247 : 8 : nflags |= GOVD_MAP | GOVD_EXPLICIT;
9248 : : }
9249 : : else
9250 : : {
9251 : 32853 : if ((ctx->region_type & ORT_ACC) != 0)
9252 : 8856 : nflags = oacc_default_clause (ctx, decl, flags);
9253 : : else
9254 : 23997 : nflags |= GOVD_MAP;
9255 : : }
9256 : : }
9257 : 9922 : found_outer:
9258 : 44895 : omp_add_variable (ctx, decl, nflags);
9259 : 44895 : if (ctx->region_type & ORT_ACC)
9260 : : /* For OpenACC, as remarked above, defer expansion. */
9261 : : shared = false;
9262 : : else
9263 : 33923 : shared = (nflags & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9264 : 44895 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9265 : : }
9266 : : else
9267 : : {
9268 : 729003 : if (ctx->region_type & ORT_ACC)
9269 : : /* For OpenACC, as remarked above, defer expansion. */
9270 : : shared = false;
9271 : : else
9272 : 433616 : shared = ((n->value | flags)
9273 : 433616 : & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0;
9274 : 729003 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9275 : : /* If nothing changed, there's nothing left to do. */
9276 : 729003 : if ((n->value & flags) == flags)
9277 : : return ret;
9278 : 15731 : flags |= n->value;
9279 : 15731 : n->value = flags;
9280 : : }
9281 : 60626 : goto do_outer;
9282 : : }
9283 : :
9284 : 3038217 : if (n == NULL)
9285 : : {
9286 : 1180082 : if (ctx->region_type == ORT_WORKSHARE
9287 : : || ctx->region_type == ORT_TASKGROUP
9288 : 560342 : || ctx->region_type == ORT_SIMD
9289 : 288653 : || ctx->region_type == ORT_ACC
9290 : 92293 : || (ctx->region_type & ORT_TARGET_DATA) != 0)
9291 : 1132252 : goto do_outer;
9292 : :
9293 : 47830 : flags = omp_default_clause (ctx, decl, in_code, flags);
9294 : :
9295 : 47830 : if ((flags & GOVD_PRIVATE)
9296 : 47830 : && lang_hooks.decls.omp_private_outer_ref (decl))
9297 : 6 : flags |= GOVD_PRIVATE_OUTER_REF;
9298 : :
9299 : 47830 : omp_add_variable (ctx, decl, flags);
9300 : :
9301 : 47830 : shared = (flags & GOVD_SHARED) != 0;
9302 : 47830 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9303 : 47830 : goto do_outer;
9304 : : }
9305 : :
9306 : : /* Don't mark as GOVD_SEEN addressable temporaries seen only in simd
9307 : : lb, b or incr expressions, those shouldn't be turned into simd arrays. */
9308 : 1858135 : if (ctx->region_type == ORT_SIMD
9309 : 150954 : && ctx->in_for_exprs
9310 : 70 : && ((n->value & (GOVD_PRIVATE | GOVD_SEEN | GOVD_EXPLICIT))
9311 : : == GOVD_PRIVATE))
9312 : 1858135 : flags &= ~GOVD_SEEN;
9313 : :
9314 : 1858135 : if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
9315 : 31690 : && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
9316 : 1889786 : && DECL_SIZE (decl))
9317 : : {
9318 : 31650 : tree size;
9319 : 31650 : if (!poly_int_tree_p (DECL_SIZE (decl)))
9320 : : {
9321 : 140 : splay_tree_node n2;
9322 : 140 : tree t = DECL_VALUE_EXPR (decl);
9323 : 140 : gcc_assert (INDIRECT_REF_P (t));
9324 : 140 : t = TREE_OPERAND (t, 0);
9325 : 140 : gcc_assert (DECL_P (t));
9326 : 140 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9327 : 140 : n2->value |= GOVD_SEEN;
9328 : : }
9329 : 31510 : else if (omp_privatize_by_reference (decl)
9330 : 4205 : && (size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
9331 : 35678 : && !poly_int_tree_p (size))
9332 : : {
9333 : 1324 : splay_tree_node n2;
9334 : 1324 : gcc_assert (DECL_P (size));
9335 : 1324 : n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) size);
9336 : 1324 : if (n2)
9337 : 617 : omp_notice_variable (ctx, size, true);
9338 : : }
9339 : : }
9340 : :
9341 : 1858135 : if (ctx->region_type & ORT_ACC)
9342 : : /* For OpenACC, as remarked above, defer expansion. */
9343 : : shared = false;
9344 : : else
9345 : 1676306 : shared = ((flags | n->value) & GOVD_SHARED) != 0;
9346 : 1858135 : ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
9347 : :
9348 : : /* If nothing changed, there's nothing left to do. */
9349 : 1858135 : if ((n->value & flags) == flags)
9350 : : return ret;
9351 : 31651 : flags |= n->value;
9352 : 31651 : n->value = flags;
9353 : :
9354 : 1272359 : do_outer:
9355 : : /* If the variable is private in the current context, then we don't
9356 : : need to propagate anything to an outer context. */
9357 : 1272359 : if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
9358 : : return ret;
9359 : 1262065 : if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9360 : : == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9361 : : return ret;
9362 : 1262005 : if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9363 : : | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9364 : : == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
9365 : : return ret;
9366 : 1262005 : if (ctx->outer_context
9367 : 1262005 : && omp_notice_variable (ctx->outer_context, decl, in_code))
9368 : : return true;
9369 : : return ret;
9370 : : }
9371 : :
9372 : : /* Verify that DECL is private within CTX. If there's specific information
9373 : : to the contrary in the innermost scope, generate an error. */
9374 : :
9375 : : static bool
9376 : 50087 : omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
9377 : : {
9378 : 95786 : splay_tree_node n;
9379 : :
9380 : 95786 : n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
9381 : 95786 : if (n != NULL)
9382 : : {
9383 : 17954 : if (n->value & GOVD_SHARED)
9384 : : {
9385 : 407 : if (ctx == gimplify_omp_ctxp)
9386 : : {
9387 : 0 : if (simd)
9388 : 0 : error ("iteration variable %qE is predetermined linear",
9389 : 0 : DECL_NAME (decl));
9390 : : else
9391 : 0 : error ("iteration variable %qE should be private",
9392 : 0 : DECL_NAME (decl));
9393 : 0 : n->value = GOVD_PRIVATE;
9394 : 0 : return true;
9395 : : }
9396 : : else
9397 : : return false;
9398 : : }
9399 : 17547 : else if ((n->value & GOVD_EXPLICIT) != 0
9400 : 9685 : && (ctx == gimplify_omp_ctxp
9401 : 326 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9402 : 206 : && gimplify_omp_ctxp->outer_context == ctx)))
9403 : : {
9404 : 9565 : if ((n->value & GOVD_FIRSTPRIVATE) != 0)
9405 : 4 : error ("iteration variable %qE should not be firstprivate",
9406 : 4 : DECL_NAME (decl));
9407 : 9561 : else if ((n->value & GOVD_REDUCTION) != 0)
9408 : 8 : error ("iteration variable %qE should not be reduction",
9409 : 8 : DECL_NAME (decl));
9410 : 9553 : else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
9411 : 58 : error ("iteration variable %qE should not be linear",
9412 : 58 : DECL_NAME (decl));
9413 : : }
9414 : 17547 : return (ctx == gimplify_omp_ctxp
9415 : 17547 : || (ctx->region_type == ORT_COMBINED_PARALLEL
9416 : 19411 : && gimplify_omp_ctxp->outer_context == ctx));
9417 : : }
9418 : :
9419 : 77832 : if (ctx->region_type != ORT_WORKSHARE
9420 : : && ctx->region_type != ORT_TASKGROUP
9421 : 49429 : && ctx->region_type != ORT_SIMD
9422 : 36187 : && ctx->region_type != ORT_ACC)
9423 : : return false;
9424 : 53845 : else if (ctx->outer_context)
9425 : : return omp_is_private (ctx->outer_context, decl, simd);
9426 : : return false;
9427 : : }
9428 : :
9429 : : /* Return true if DECL is private within a parallel region
9430 : : that binds to the current construct's context or in parallel
9431 : : region's REDUCTION clause. */
9432 : :
9433 : : static bool
9434 : 11529 : omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
9435 : : {
9436 : 11601 : splay_tree_node n;
9437 : :
9438 : 11601 : do
9439 : : {
9440 : 11601 : ctx = ctx->outer_context;
9441 : 11601 : if (ctx == NULL)
9442 : : {
9443 : 2764 : if (is_global_var (decl))
9444 : : return false;
9445 : :
9446 : : /* References might be private, but might be shared too,
9447 : : when checking for copyprivate, assume they might be
9448 : : private, otherwise assume they might be shared. */
9449 : 1290 : if (copyprivate)
9450 : : return true;
9451 : :
9452 : 1244 : if (omp_privatize_by_reference (decl))
9453 : : return false;
9454 : :
9455 : : /* Treat C++ privatized non-static data members outside
9456 : : of the privatization the same. */
9457 : 1198 : if (omp_member_access_dummy_var (decl))
9458 : : return false;
9459 : :
9460 : : return true;
9461 : : }
9462 : :
9463 : 8837 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9464 : :
9465 : 8837 : if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
9466 : 1291 : && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
9467 : : {
9468 : 242 : if ((ctx->region_type & ORT_TARGET_DATA) != 0
9469 : 238 : || n == NULL
9470 : 230 : || (n->value & GOVD_MAP) == 0)
9471 : 12 : continue;
9472 : : return false;
9473 : : }
9474 : :
9475 : 7546 : if (n != NULL)
9476 : : {
9477 : 5346 : if ((n->value & GOVD_LOCAL) != 0
9478 : 5346 : && omp_member_access_dummy_var (decl))
9479 : : return false;
9480 : 5307 : return (n->value & GOVD_SHARED) == 0;
9481 : : }
9482 : :
9483 : 3249 : if (ctx->region_type == ORT_WORKSHARE
9484 : : || ctx->region_type == ORT_TASKGROUP
9485 : 3193 : || ctx->region_type == ORT_SIMD
9486 : 3189 : || ctx->region_type == ORT_ACC)
9487 : 60 : continue;
9488 : :
9489 : : break;
9490 : : }
9491 : : while (1);
9492 : : return false;
9493 : : }
9494 : :
9495 : : /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
9496 : :
9497 : : static tree
9498 : 3148 : find_decl_expr (tree *tp, int *walk_subtrees, void *data)
9499 : : {
9500 : 3148 : tree t = *tp;
9501 : :
9502 : : /* If this node has been visited, unmark it and keep looking. */
9503 : 3148 : if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
9504 : : return t;
9505 : :
9506 : 2724 : if (IS_TYPE_OR_DECL_P (t))
9507 : 452 : *walk_subtrees = 0;
9508 : : return NULL_TREE;
9509 : : }
9510 : :
9511 : :
9512 : : /* Gimplify the affinity clause but effectively ignore it.
9513 : : Generate:
9514 : : var = begin;
9515 : : if ((step > 1) ? var <= end : var > end)
9516 : : locatator_var_expr; */
9517 : :
9518 : : static void
9519 : 468 : gimplify_omp_affinity (tree *list_p, gimple_seq *pre_p)
9520 : : {
9521 : 468 : tree last_iter = NULL_TREE;
9522 : 468 : tree last_bind = NULL_TREE;
9523 : 468 : tree label = NULL_TREE;
9524 : 468 : tree *last_body = NULL;
9525 : 1203 : for (tree c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9526 : 735 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
9527 : : {
9528 : 735 : tree t = OMP_CLAUSE_DECL (c);
9529 : 735 : if (TREE_CODE (t) == TREE_LIST
9530 : 389 : && TREE_PURPOSE (t)
9531 : 1124 : && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9532 : : {
9533 : 389 : if (TREE_VALUE (t) == null_pointer_node)
9534 : 201 : continue;
9535 : 188 : if (TREE_PURPOSE (t) != last_iter)
9536 : : {
9537 : 127 : if (last_bind)
9538 : : {
9539 : 9 : append_to_statement_list (label, last_body);
9540 : 9 : gimplify_and_add (last_bind, pre_p);
9541 : 9 : last_bind = NULL_TREE;
9542 : : }
9543 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9544 : : {
9545 : 147 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9546 : : is_gimple_val, fb_rvalue) == GS_ERROR
9547 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9548 : : is_gimple_val, fb_rvalue) == GS_ERROR
9549 : 147 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9550 : : is_gimple_val, fb_rvalue) == GS_ERROR
9551 : 294 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9552 : : is_gimple_val, fb_rvalue)
9553 : : == GS_ERROR))
9554 : 0 : return;
9555 : : }
9556 : 127 : last_iter = TREE_PURPOSE (t);
9557 : 127 : tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9558 : 127 : last_bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
9559 : : NULL, block);
9560 : 127 : last_body = &BIND_EXPR_BODY (last_bind);
9561 : 127 : tree cond = NULL_TREE;
9562 : 127 : location_t loc = OMP_CLAUSE_LOCATION (c);
9563 : 274 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9564 : : {
9565 : 147 : tree var = TREE_VEC_ELT (it, 0);
9566 : 147 : tree begin = TREE_VEC_ELT (it, 1);
9567 : 147 : tree end = TREE_VEC_ELT (it, 2);
9568 : 147 : tree step = TREE_VEC_ELT (it, 3);
9569 : 147 : loc = DECL_SOURCE_LOCATION (var);
9570 : 147 : tree tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9571 : : var, begin);
9572 : 147 : append_to_statement_list_force (tem, last_body);
9573 : :
9574 : 147 : tree cond1 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9575 : 147 : step, build_zero_cst (TREE_TYPE (step)));
9576 : 147 : tree cond2 = fold_build2_loc (loc, LE_EXPR, boolean_type_node,
9577 : : var, end);
9578 : 147 : tree cond3 = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9579 : : var, end);
9580 : 147 : cond1 = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
9581 : : cond1, cond2, cond3);
9582 : 147 : if (cond)
9583 : 20 : cond = fold_build2_loc (loc, TRUTH_AND_EXPR,
9584 : : boolean_type_node, cond, cond1);
9585 : : else
9586 : : cond = cond1;
9587 : : }
9588 : 127 : tree cont_label = create_artificial_label (loc);
9589 : 127 : label = build1 (LABEL_EXPR, void_type_node, cont_label);
9590 : 127 : tree tem = fold_build3_loc (loc, COND_EXPR, void_type_node, cond,
9591 : : void_node,
9592 : : build_and_jump (&cont_label));
9593 : 127 : append_to_statement_list_force (tem, last_body);
9594 : : }
9595 : 188 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
9596 : : {
9597 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t), 0),
9598 : : last_body);
9599 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
9600 : : }
9601 : 188 : if (error_operand_p (TREE_VALUE (t)))
9602 : : return;
9603 : 188 : append_to_statement_list_force (TREE_VALUE (t), last_body);
9604 : 188 : TREE_VALUE (t) = null_pointer_node;
9605 : : }
9606 : : else
9607 : : {
9608 : 346 : if (last_bind)
9609 : : {
9610 : 9 : append_to_statement_list (label, last_body);
9611 : 9 : gimplify_and_add (last_bind, pre_p);
9612 : 9 : last_bind = NULL_TREE;
9613 : : }
9614 : 346 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
9615 : : {
9616 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
9617 : : NULL, is_gimple_val, fb_rvalue);
9618 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
9619 : : }
9620 : 346 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
9621 : : return;
9622 : 346 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
9623 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
9624 : : return;
9625 : 346 : gimplify_and_add (OMP_CLAUSE_DECL (c), pre_p);
9626 : : }
9627 : : }
9628 : 468 : if (last_bind)
9629 : : {
9630 : 109 : append_to_statement_list (label, last_body);
9631 : 109 : gimplify_and_add (last_bind, pre_p);
9632 : : }
9633 : : return;
9634 : : }
9635 : :
9636 : : /* If *LIST_P contains any OpenMP depend clauses with iterators,
9637 : : lower all the depend clauses by populating corresponding depend
9638 : : array. Returns 0 if there are no such depend clauses, or
9639 : : 2 if all depend clauses should be removed, 1 otherwise. */
9640 : :
9641 : : static int
9642 : 1898 : gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
9643 : : {
9644 : 1898 : tree c;
9645 : 1898 : gimple *g;
9646 : 1898 : size_t n[5] = { 0, 0, 0, 0, 0 };
9647 : 1898 : bool unused[5];
9648 : 1898 : tree counts[5] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
9649 : 1898 : tree last_iter = NULL_TREE, last_count = NULL_TREE;
9650 : 1898 : size_t i, j;
9651 : 1898 : location_t first_loc = UNKNOWN_LOCATION;
9652 : :
9653 : 6236 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9654 : 4338 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
9655 : : {
9656 : 2200 : switch (OMP_CLAUSE_DEPEND_KIND (c))
9657 : : {
9658 : : case OMP_CLAUSE_DEPEND_IN:
9659 : : i = 2;
9660 : : break;
9661 : : case OMP_CLAUSE_DEPEND_OUT:
9662 : : case OMP_CLAUSE_DEPEND_INOUT:
9663 : : i = 0;
9664 : : break;
9665 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
9666 : : i = 1;
9667 : : break;
9668 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
9669 : : i = 3;
9670 : : break;
9671 : : case OMP_CLAUSE_DEPEND_INOUTSET:
9672 : : i = 4;
9673 : : break;
9674 : 0 : default:
9675 : 0 : gcc_unreachable ();
9676 : : }
9677 : 2200 : tree t = OMP_CLAUSE_DECL (c);
9678 : 2200 : if (first_loc == UNKNOWN_LOCATION)
9679 : 1898 : first_loc = OMP_CLAUSE_LOCATION (c);
9680 : 2200 : if (TREE_CODE (t) == TREE_LIST
9681 : 297 : && TREE_PURPOSE (t)
9682 : 2497 : && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9683 : : {
9684 : 297 : if (TREE_PURPOSE (t) != last_iter)
9685 : : {
9686 : 250 : tree tcnt = size_one_node;
9687 : 534 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9688 : : {
9689 : 284 : if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
9690 : : is_gimple_val, fb_rvalue) == GS_ERROR
9691 : 284 : || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
9692 : : is_gimple_val, fb_rvalue) == GS_ERROR
9693 : 284 : || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
9694 : : is_gimple_val, fb_rvalue) == GS_ERROR
9695 : 568 : || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
9696 : : is_gimple_val, fb_rvalue)
9697 : : == GS_ERROR))
9698 : 0 : return 2;
9699 : 284 : tree var = TREE_VEC_ELT (it, 0);
9700 : 284 : tree begin = TREE_VEC_ELT (it, 1);
9701 : 284 : tree end = TREE_VEC_ELT (it, 2);
9702 : 284 : tree step = TREE_VEC_ELT (it, 3);
9703 : 284 : tree orig_step = TREE_VEC_ELT (it, 4);
9704 : 284 : tree type = TREE_TYPE (var);
9705 : 284 : tree stype = TREE_TYPE (step);
9706 : 284 : location_t loc = DECL_SOURCE_LOCATION (var);
9707 : 284 : tree endmbegin;
9708 : : /* Compute count for this iterator as
9709 : : orig_step > 0
9710 : : ? (begin < end ? (end - begin + (step - 1)) / step : 0)
9711 : : : (begin > end ? (end - begin + (step + 1)) / step : 0)
9712 : : and compute product of those for the entire depend
9713 : : clause. */
9714 : 284 : if (POINTER_TYPE_P (type))
9715 : 42 : endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR,
9716 : : stype, end, begin);
9717 : : else
9718 : 242 : endmbegin = fold_build2_loc (loc, MINUS_EXPR, type,
9719 : : end, begin);
9720 : 284 : tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype,
9721 : : step,
9722 : : build_int_cst (stype, 1));
9723 : 284 : tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
9724 : : build_int_cst (stype, 1));
9725 : 284 : tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
9726 : : unshare_expr (endmbegin),
9727 : : stepm1);
9728 : 284 : pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
9729 : : pos, step);
9730 : 284 : tree neg = fold_build2_loc (loc, PLUS_EXPR, stype,
9731 : : endmbegin, stepp1);
9732 : 284 : if (TYPE_UNSIGNED (stype))
9733 : : {
9734 : 31 : neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
9735 : 31 : step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
9736 : : }
9737 : 284 : neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
9738 : : neg, step);
9739 : 284 : step = NULL_TREE;
9740 : 284 : tree cond = fold_build2_loc (loc, LT_EXPR,
9741 : : boolean_type_node,
9742 : : begin, end);
9743 : 284 : pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
9744 : : build_int_cst (stype, 0));
9745 : 284 : cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
9746 : : end, begin);
9747 : 284 : neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
9748 : : build_int_cst (stype, 0));
9749 : 284 : tree osteptype = TREE_TYPE (orig_step);
9750 : 284 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9751 : : orig_step,
9752 : : build_int_cst (osteptype, 0));
9753 : 284 : tree cnt = fold_build3_loc (loc, COND_EXPR, stype,
9754 : 284 : cond, pos, neg);
9755 : 284 : cnt = fold_convert_loc (loc, sizetype, cnt);
9756 : 284 : if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
9757 : : fb_rvalue) == GS_ERROR)
9758 : : return 2;
9759 : 284 : tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
9760 : : }
9761 : 250 : if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val,
9762 : : fb_rvalue) == GS_ERROR)
9763 : : return 2;
9764 : 250 : last_iter = TREE_PURPOSE (t);
9765 : 250 : last_count = tcnt;
9766 : : }
9767 : 297 : if (counts[i] == NULL_TREE)
9768 : 243 : counts[i] = last_count;
9769 : : else
9770 : 54 : counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
9771 : : PLUS_EXPR, counts[i], last_count);
9772 : : }
9773 : : else
9774 : 1903 : n[i]++;
9775 : : }
9776 : 10601 : for (i = 0; i < 5; i++)
9777 : 8908 : if (counts[i])
9778 : : break;
9779 : 1898 : if (i == 5)
9780 : : return 0;
9781 : :
9782 : 205 : tree total = size_zero_node;
9783 : 1230 : for (i = 0; i < 5; i++)
9784 : : {
9785 : 1025 : unused[i] = counts[i] == NULL_TREE && n[i] == 0;
9786 : 1025 : if (counts[i] == NULL_TREE)
9787 : 782 : counts[i] = size_zero_node;
9788 : 1025 : if (n[i])
9789 : 44 : counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
9790 : 1025 : if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
9791 : : fb_rvalue) == GS_ERROR)
9792 : : return 2;
9793 : 1025 : total = size_binop (PLUS_EXPR, total, counts[i]);
9794 : : }
9795 : :
9796 : 205 : if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
9797 : : == GS_ERROR)
9798 : : return 2;
9799 : 205 : bool is_old = unused[1] && unused[3] && unused[4];
9800 : 205 : tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
9801 : : size_int (is_old ? 1 : 4));
9802 : 205 : if (!unused[4])
9803 : 3 : totalpx = size_binop (PLUS_EXPR, totalpx,
9804 : : size_binop (MULT_EXPR, counts[4], size_int (2)));
9805 : 205 : tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
9806 : 205 : tree array = create_tmp_var_raw (type);
9807 : 205 : TREE_ADDRESSABLE (array) = 1;
9808 : 205 : if (!poly_int_tree_p (totalpx))
9809 : : {
9810 : 81 : if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
9811 : 81 : gimplify_type_sizes (TREE_TYPE (array), pre_p);
9812 : 81 : if (gimplify_omp_ctxp)
9813 : : {
9814 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9815 : : while (ctx
9816 : 20 : && (ctx->region_type == ORT_WORKSHARE
9817 : : || ctx->region_type == ORT_TASKGROUP
9818 : 20 : || ctx->region_type == ORT_SIMD
9819 : 20 : || ctx->region_type == ORT_ACC))
9820 : 0 : ctx = ctx->outer_context;
9821 : 20 : if (ctx)
9822 : 20 : omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
9823 : : }
9824 : 81 : gimplify_vla_decl (array, pre_p);
9825 : : }
9826 : : else
9827 : 124 : gimple_add_tmp_var (array);
9828 : 205 : tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
9829 : : NULL_TREE);
9830 : 205 : tree tem;
9831 : 205 : if (!is_old)
9832 : : {
9833 : 31 : tem = build2 (MODIFY_EXPR, void_type_node, r,
9834 : : build_int_cst (ptr_type_node, 0));
9835 : 31 : gimplify_and_add (tem, pre_p);
9836 : 31 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
9837 : : NULL_TREE);
9838 : : }
9839 : 205 : tem = build2 (MODIFY_EXPR, void_type_node, r,
9840 : : fold_convert (ptr_type_node, total));
9841 : 205 : gimplify_and_add (tem, pre_p);
9842 : 801 : for (i = 1; i < (is_old ? 2 : 4); i++)
9843 : : {
9844 : 267 : r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
9845 : : NULL_TREE, NULL_TREE);
9846 : 267 : tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
9847 : 267 : gimplify_and_add (tem, pre_p);
9848 : : }
9849 : :
9850 : : tree cnts[6];
9851 : 717 : for (j = 5; j; j--)
9852 : 717 : if (!unused[j - 1])
9853 : : break;
9854 : 1230 : for (i = 0; i < 5; i++)
9855 : : {
9856 : 1025 : if (i && (i >= j || unused[i - 1]))
9857 : : {
9858 : 770 : cnts[i] = cnts[i - 1];
9859 : 770 : continue;
9860 : : }
9861 : 255 : cnts[i] = create_tmp_var (sizetype);
9862 : 255 : if (i == 0)
9863 : 236 : g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
9864 : : else
9865 : : {
9866 : 50 : tree t;
9867 : 50 : if (is_old)
9868 : 38 : t = size_binop (PLUS_EXPR, counts[0], size_int (2));
9869 : : else
9870 : 12 : t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
9871 : 50 : if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
9872 : : == GS_ERROR)
9873 : 0 : return 2;
9874 : 50 : g = gimple_build_assign (cnts[i], t);
9875 : : }
9876 : 255 : gimple_seq_add_stmt (pre_p, g);
9877 : : }
9878 : 205 : if (unused[4])
9879 : 202 : cnts[5] = NULL_TREE;
9880 : : else
9881 : : {
9882 : 3 : tree t = size_binop (PLUS_EXPR, total, size_int (5));
9883 : 3 : cnts[5] = create_tmp_var (sizetype);
9884 : 3 : g = gimple_build_assign (cnts[i], t);
9885 : 3 : gimple_seq_add_stmt (pre_p, g);
9886 : : }
9887 : :
9888 : 205 : last_iter = NULL_TREE;
9889 : 205 : tree last_bind = NULL_TREE;
9890 : 205 : tree *last_body = NULL;
9891 : 563 : for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
9892 : 358 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
9893 : : {
9894 : 343 : switch (OMP_CLAUSE_DEPEND_KIND (c))
9895 : : {
9896 : : case OMP_CLAUSE_DEPEND_IN:
9897 : : i = 2;
9898 : : break;
9899 : : case OMP_CLAUSE_DEPEND_OUT:
9900 : : case OMP_CLAUSE_DEPEND_INOUT:
9901 : : i = 0;
9902 : : break;
9903 : : case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
9904 : : i = 1;
9905 : : break;
9906 : : case OMP_CLAUSE_DEPEND_DEPOBJ:
9907 : : i = 3;
9908 : : break;
9909 : : case OMP_CLAUSE_DEPEND_INOUTSET:
9910 : : i = 4;
9911 : : break;
9912 : 0 : default:
9913 : 0 : gcc_unreachable ();
9914 : : }
9915 : 343 : tree t = OMP_CLAUSE_DECL (c);
9916 : 343 : if (TREE_CODE (t) == TREE_LIST
9917 : 297 : && TREE_PURPOSE (t)
9918 : 640 : && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
9919 : : {
9920 : 297 : if (TREE_PURPOSE (t) != last_iter)
9921 : : {
9922 : 250 : if (last_bind)
9923 : 31 : gimplify_and_add (last_bind, pre_p);
9924 : 250 : tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
9925 : 250 : last_bind = build3 (BIND_EXPR, void_type_node,
9926 : 250 : BLOCK_VARS (block), NULL, block);
9927 : 250 : TREE_SIDE_EFFECTS (last_bind) = 1;
9928 : 250 : SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
9929 : 250 : tree *p = &BIND_EXPR_BODY (last_bind);
9930 : 534 : for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
9931 : : {
9932 : 284 : tree var = TREE_VEC_ELT (it, 0);
9933 : 284 : tree begin = TREE_VEC_ELT (it, 1);
9934 : 284 : tree end = TREE_VEC_ELT (it, 2);
9935 : 284 : tree step = TREE_VEC_ELT (it, 3);
9936 : 284 : tree orig_step = TREE_VEC_ELT (it, 4);
9937 : 284 : tree type = TREE_TYPE (var);
9938 : 284 : location_t loc = DECL_SOURCE_LOCATION (var);
9939 : : /* Emit:
9940 : : var = begin;
9941 : : goto cond_label;
9942 : : beg_label:
9943 : : ...
9944 : : var = var + step;
9945 : : cond_label:
9946 : : if (orig_step > 0) {
9947 : : if (var < end) goto beg_label;
9948 : : } else {
9949 : : if (var > end) goto beg_label;
9950 : : }
9951 : : for each iterator, with inner iterators added to
9952 : : the ... above. */
9953 : 284 : tree beg_label = create_artificial_label (loc);
9954 : 284 : tree cond_label = NULL_TREE;
9955 : 284 : tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9956 : : var, begin);
9957 : 284 : append_to_statement_list_force (tem, p);
9958 : 284 : tem = build_and_jump (&cond_label);
9959 : 284 : append_to_statement_list_force (tem, p);
9960 : 284 : tem = build1 (LABEL_EXPR, void_type_node, beg_label);
9961 : 284 : append_to_statement_list (tem, p);
9962 : 284 : tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9963 : : NULL_TREE, NULL_TREE);
9964 : 284 : TREE_SIDE_EFFECTS (bind) = 1;
9965 : 284 : SET_EXPR_LOCATION (bind, loc);
9966 : 284 : append_to_statement_list_force (bind, p);
9967 : 284 : if (POINTER_TYPE_P (type))
9968 : 42 : tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
9969 : : var, fold_convert_loc (loc, sizetype,
9970 : : step));
9971 : : else
9972 : 242 : tem = build2_loc (loc, PLUS_EXPR, type, var, step);
9973 : 284 : tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
9974 : : var, tem);
9975 : 284 : append_to_statement_list_force (tem, p);
9976 : 284 : tem = build1 (LABEL_EXPR, void_type_node, cond_label);
9977 : 284 : append_to_statement_list (tem, p);
9978 : 284 : tree cond = fold_build2_loc (loc, LT_EXPR,
9979 : : boolean_type_node,
9980 : : var, end);
9981 : 284 : tree pos
9982 : 284 : = fold_build3_loc (loc, COND_EXPR, void_type_node,
9983 : : cond, build_and_jump (&beg_label),
9984 : : void_node);
9985 : 284 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9986 : : var, end);
9987 : 284 : tree neg
9988 : 284 : = fold_build3_loc (loc, COND_EXPR, void_type_node,
9989 : : cond, build_and_jump (&beg_label),
9990 : : void_node);
9991 : 284 : tree osteptype = TREE_TYPE (orig_step);
9992 : 284 : cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
9993 : : orig_step,
9994 : : build_int_cst (osteptype, 0));
9995 : 284 : tem = fold_build3_loc (loc, COND_EXPR, void_type_node,
9996 : : cond, pos, neg);
9997 : 284 : append_to_statement_list_force (tem, p);
9998 : 284 : p = &BIND_EXPR_BODY (bind);
9999 : : }
10000 : : last_body = p;
10001 : : }
10002 : 297 : last_iter = TREE_PURPOSE (t);
10003 : 297 : if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
10004 : : {
10005 : 0 : append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
10006 : : 0), last_body);
10007 : 0 : TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
10008 : : }
10009 : 297 : if (error_operand_p (TREE_VALUE (t)))
10010 : : return 2;
10011 : 297 : if (TREE_VALUE (t) != null_pointer_node)
10012 : 291 : TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
10013 : 297 : if (i == 4)
10014 : : {
10015 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10016 : : NULL_TREE, NULL_TREE);
10017 : 3 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10018 : : NULL_TREE, NULL_TREE);
10019 : 3 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10020 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10021 : : void_type_node, r, r2);
10022 : 3 : append_to_statement_list_force (tem, last_body);
10023 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10024 : : void_type_node, cnts[i],
10025 : : size_binop (PLUS_EXPR, cnts[i],
10026 : : size_int (1)));
10027 : 3 : append_to_statement_list_force (tem, last_body);
10028 : 3 : i = 5;
10029 : : }
10030 : 297 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10031 : : NULL_TREE, NULL_TREE);
10032 : 594 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10033 : 297 : void_type_node, r, TREE_VALUE (t));
10034 : 297 : append_to_statement_list_force (tem, last_body);
10035 : 297 : if (i == 5)
10036 : : {
10037 : 3 : r = build4 (ARRAY_REF, ptr_type_node, array,
10038 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10039 : : NULL_TREE, NULL_TREE);
10040 : 3 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10041 : 3 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10042 : : void_type_node, r, tem);
10043 : 3 : append_to_statement_list_force (tem, last_body);
10044 : : }
10045 : 297 : tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
10046 : : void_type_node, cnts[i],
10047 : 297 : size_binop (PLUS_EXPR, cnts[i],
10048 : : size_int (1 + (i == 5))));
10049 : 297 : append_to_statement_list_force (tem, last_body);
10050 : 297 : TREE_VALUE (t) = null_pointer_node;
10051 : : }
10052 : : else
10053 : : {
10054 : 46 : if (last_bind)
10055 : : {
10056 : 18 : gimplify_and_add (last_bind, pre_p);
10057 : 18 : last_bind = NULL_TREE;
10058 : : }
10059 : 46 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
10060 : : {
10061 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
10062 : : NULL, is_gimple_val, fb_rvalue);
10063 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
10064 : : }
10065 : 46 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
10066 : : return 2;
10067 : 46 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
10068 : 46 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
10069 : 46 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
10070 : : is_gimple_val, fb_rvalue) == GS_ERROR)
10071 : : return 2;
10072 : 46 : if (i == 4)
10073 : : {
10074 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10075 : : NULL_TREE, NULL_TREE);
10076 : 0 : tree r2 = build4 (ARRAY_REF, ptr_type_node, array, cnts[5],
10077 : : NULL_TREE, NULL_TREE);
10078 : 0 : r2 = build_fold_addr_expr_with_type (r2, ptr_type_node);
10079 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, r2);
10080 : 0 : gimplify_and_add (tem, pre_p);
10081 : 0 : g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR,
10082 : : cnts[i],
10083 : : size_int (1)));
10084 : 0 : gimple_seq_add_stmt (pre_p, g);
10085 : 0 : i = 5;
10086 : : }
10087 : 46 : r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
10088 : : NULL_TREE, NULL_TREE);
10089 : 46 : tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
10090 : 46 : gimplify_and_add (tem, pre_p);
10091 : 46 : if (i == 5)
10092 : : {
10093 : 0 : r = build4 (ARRAY_REF, ptr_type_node, array,
10094 : : size_binop (PLUS_EXPR, cnts[i], size_int (1)),
10095 : : NULL_TREE, NULL_TREE);
10096 : 0 : tem = build_int_cst (ptr_type_node, GOMP_DEPEND_INOUTSET);
10097 : 0 : tem = build2 (MODIFY_EXPR, void_type_node, r, tem);
10098 : 0 : append_to_statement_list_force (tem, last_body);
10099 : 0 : gimplify_and_add (tem, pre_p);
10100 : : }
10101 : 46 : g = gimple_build_assign (cnts[i],
10102 : 46 : size_binop (PLUS_EXPR, cnts[i],
10103 : : size_int (1 + (i == 5))));
10104 : 46 : gimple_seq_add_stmt (pre_p, g);
10105 : : }
10106 : : }
10107 : 205 : if (last_bind)
10108 : 201 : gimplify_and_add (last_bind, pre_p);
10109 : 205 : tree cond = boolean_false_node;
10110 : 205 : if (is_old)
10111 : : {
10112 : 174 : if (!unused[0])
10113 : 88 : cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
10114 : : size_binop_loc (first_loc, PLUS_EXPR, counts[0],
10115 : : size_int (2)));
10116 : 174 : if (!unused[2])
10117 : 124 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10118 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10119 : : cnts[2],
10120 : : size_binop_loc (first_loc, PLUS_EXPR,
10121 : : totalpx,
10122 : : size_int (1))));
10123 : : }
10124 : : else
10125 : : {
10126 : 31 : tree prev = size_int (5);
10127 : 186 : for (i = 0; i < 5; i++)
10128 : : {
10129 : 155 : if (unused[i])
10130 : 112 : continue;
10131 : 43 : prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
10132 : 43 : cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
10133 : : build2_loc (first_loc, NE_EXPR, boolean_type_node,
10134 : : cnts[i], unshare_expr (prev)));
10135 : : }
10136 : : }
10137 : 205 : tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
10138 : : build_call_expr_loc (first_loc,
10139 : : builtin_decl_explicit (BUILT_IN_TRAP),
10140 : : 0), void_node);
10141 : 205 : gimplify_and_add (tem, pre_p);
10142 : 205 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
10143 : 205 : OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
10144 : 205 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
10145 : 205 : OMP_CLAUSE_CHAIN (c) = *list_p;
10146 : 205 : *list_p = c;
10147 : 205 : return 1;
10148 : : }
10149 : :
10150 : : /* True if mapping node C maps, or unmaps, a (Fortran) array descriptor. */
10151 : :
10152 : : static bool
10153 : 112964 : omp_map_clause_descriptor_p (tree c)
10154 : : {
10155 : 112964 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10156 : : return false;
10157 : :
10158 : 112962 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO_PSET)
10159 : : return true;
10160 : :
10161 : 74443 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_RELEASE
10162 : 68867 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DELETE)
10163 : 74883 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (c))
10164 : 4042 : return true;
10165 : :
10166 : : return false;
10167 : : }
10168 : :
10169 : : /* For a set of mappings describing an array section pointed to by a struct
10170 : : (or derived type, etc.) component, create an "alloc" or "release" node to
10171 : : insert into a list following a GOMP_MAP_STRUCT node. For some types of
10172 : : mapping (e.g. Fortran arrays with descriptors), an additional mapping may
10173 : : be created that is inserted into the list of mapping nodes attached to the
10174 : : directive being processed -- not part of the sorted list of nodes after
10175 : : GOMP_MAP_STRUCT.
10176 : :
10177 : : CODE is the code of the directive being processed. GRP_START and GRP_END
10178 : : are the first and last of two or three nodes representing this array section
10179 : : mapping (e.g. a data movement node like GOMP_MAP_{TO,FROM}, optionally a
10180 : : GOMP_MAP_TO_PSET, and finally a GOMP_MAP_ALWAYS_POINTER). EXTRA_NODE is
10181 : : filled with the additional node described above, if needed.
10182 : :
10183 : : This function does not add the new nodes to any lists itself. It is the
10184 : : responsibility of the caller to do that. */
10185 : :
10186 : : static tree
10187 : 1718 : build_omp_struct_comp_nodes (enum tree_code code, tree grp_start, tree grp_end,
10188 : : tree *extra_node)
10189 : : {
10190 : 1525 : enum gomp_map_kind mkind
10191 : 1718 : = (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
10192 : 1718 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
10193 : :
10194 : 1718 : gcc_assert (grp_start != grp_end);
10195 : :
10196 : 1718 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10197 : 1718 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
10198 : 1718 : OMP_CLAUSE_DECL (c2) = unshare_expr (OMP_CLAUSE_DECL (grp_end));
10199 : 1718 : OMP_CLAUSE_CHAIN (c2) = NULL_TREE;
10200 : 1718 : tree grp_mid = NULL_TREE;
10201 : 1718 : if (OMP_CLAUSE_CHAIN (grp_start) != grp_end)
10202 : 185 : grp_mid = OMP_CLAUSE_CHAIN (grp_start);
10203 : :
10204 : 185 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
10205 : 0 : OMP_CLAUSE_SIZE (c2) = OMP_CLAUSE_SIZE (grp_mid);
10206 : : else
10207 : 1718 : OMP_CLAUSE_SIZE (c2) = TYPE_SIZE_UNIT (ptr_type_node);
10208 : :
10209 : 1718 : if (grp_mid
10210 : 185 : && OMP_CLAUSE_CODE (grp_mid) == OMP_CLAUSE_MAP
10211 : 1903 : && OMP_CLAUSE_MAP_KIND (grp_mid) == GOMP_MAP_ALWAYS_POINTER)
10212 : : {
10213 : 0 : tree c3
10214 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
10215 : 0 : OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
10216 : 0 : OMP_CLAUSE_DECL (c3) = unshare_expr (OMP_CLAUSE_DECL (grp_mid));
10217 : 0 : OMP_CLAUSE_SIZE (c3) = TYPE_SIZE_UNIT (ptr_type_node);
10218 : 0 : OMP_CLAUSE_CHAIN (c3) = NULL_TREE;
10219 : :
10220 : 0 : *extra_node = c3;
10221 : : }
10222 : : else
10223 : 1718 : *extra_node = NULL_TREE;
10224 : :
10225 : 1718 : return c2;
10226 : : }
10227 : :
10228 : : /* Strip ARRAY_REFS or an indirect ref off BASE, find the containing object,
10229 : : and set *BITPOSP and *POFFSETP to the bit offset of the access.
10230 : : If BASE_REF is non-NULL and the containing object is a reference, set
10231 : : *BASE_REF to that reference before dereferencing the object.
10232 : : If BASE_REF is NULL, check that the containing object is a COMPONENT_REF or
10233 : : has array type, else return NULL. */
10234 : :
10235 : : static tree
10236 : 6542 : extract_base_bit_offset (tree base, poly_int64 *bitposp,
10237 : : poly_offset_int *poffsetp,
10238 : : bool *variable_offset)
10239 : : {
10240 : 6542 : tree offset;
10241 : 6542 : poly_int64 bitsize, bitpos;
10242 : 6542 : machine_mode mode;
10243 : 6542 : int unsignedp, reversep, volatilep = 0;
10244 : 6542 : poly_offset_int poffset;
10245 : :
10246 : 6542 : STRIP_NOPS (base);
10247 : :
10248 : 6542 : base = get_inner_reference (base, &bitsize, &bitpos, &offset, &mode,
10249 : : &unsignedp, &reversep, &volatilep);
10250 : :
10251 : 6542 : STRIP_NOPS (base);
10252 : :
10253 : 6542 : if (offset && poly_int_tree_p (offset))
10254 : : {
10255 : 0 : poffset = wi::to_poly_offset (offset);
10256 : 0 : *variable_offset = false;
10257 : : }
10258 : : else
10259 : : {
10260 : 6542 : poffset = 0;
10261 : 6542 : *variable_offset = (offset != NULL_TREE);
10262 : : }
10263 : :
10264 : 6542 : if (maybe_ne (bitpos, 0))
10265 : 4939 : poffset += bits_to_bytes_round_down (bitpos);
10266 : :
10267 : 6542 : *bitposp = bitpos;
10268 : 6542 : *poffsetp = poffset;
10269 : :
10270 : 6542 : return base;
10271 : : }
10272 : :
10273 : : /* Used for topological sorting of mapping groups. UNVISITED means we haven't
10274 : : started processing the group yet. The TEMPORARY mark is used when we first
10275 : : encounter a group on a depth-first traversal, and the PERMANENT mark is used
10276 : : when we have processed all the group's children (i.e. all the base pointers
10277 : : referred to by the group's mapping nodes, recursively). */
10278 : :
10279 : : enum omp_tsort_mark {
10280 : : UNVISITED,
10281 : : TEMPORARY,
10282 : : PERMANENT
10283 : : };
10284 : :
10285 : : /* Hash for trees based on operand_equal_p. Like tree_operand_hash
10286 : : but ignores side effects in the equality comparisons. */
10287 : :
10288 : : struct tree_operand_hash_no_se : tree_operand_hash
10289 : : {
10290 : : static inline bool equal (const value_type &,
10291 : : const compare_type &);
10292 : : };
10293 : :
10294 : : inline bool
10295 : 355836 : tree_operand_hash_no_se::equal (const value_type &t1,
10296 : : const compare_type &t2)
10297 : : {
10298 : 355836 : return operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS);
10299 : : }
10300 : :
10301 : : /* A group of OMP_CLAUSE_MAP nodes that correspond to a single "map"
10302 : : clause. */
10303 : :
10304 : : struct omp_mapping_group {
10305 : : tree *grp_start;
10306 : : tree grp_end;
10307 : : omp_tsort_mark mark;
10308 : : /* If we've removed the group but need to reindex, mark the group as
10309 : : deleted. */
10310 : : bool deleted;
10311 : : /* The group points to an already-created "GOMP_MAP_STRUCT
10312 : : GOMP_MAP_ATTACH_DETACH" pair. */
10313 : : bool reprocess_struct;
10314 : : /* The group should use "zero-length" allocations for pointers that are not
10315 : : mapped "to" on the same directive. */
10316 : : bool fragile;
10317 : : struct omp_mapping_group *sibling;
10318 : : struct omp_mapping_group *next;
10319 : : };
10320 : :
10321 : : DEBUG_FUNCTION void
10322 : 0 : debug_mapping_group (omp_mapping_group *grp)
10323 : : {
10324 : 0 : tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
10325 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = NULL;
10326 : 0 : debug_generic_expr (*grp->grp_start);
10327 : 0 : OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
10328 : 0 : }
10329 : :
10330 : : /* Return the OpenMP "base pointer" of an expression EXPR, or NULL if there
10331 : : isn't one. */
10332 : :
10333 : : static tree
10334 : 32755 : omp_get_base_pointer (tree expr)
10335 : : {
10336 : 32755 : while (TREE_CODE (expr) == ARRAY_REF
10337 : 38035 : || TREE_CODE (expr) == COMPONENT_REF)
10338 : 5280 : expr = TREE_OPERAND (expr, 0);
10339 : :
10340 : 32755 : if (INDIRECT_REF_P (expr)
10341 : 32755 : || (TREE_CODE (expr) == MEM_REF
10342 : 0 : && integer_zerop (TREE_OPERAND (expr, 1))))
10343 : : {
10344 : 9551 : expr = TREE_OPERAND (expr, 0);
10345 : 9588 : while (TREE_CODE (expr) == COMPOUND_EXPR)
10346 : 37 : expr = TREE_OPERAND (expr, 1);
10347 : 9551 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
10348 : 759 : expr = TREE_OPERAND (expr, 0);
10349 : 9551 : if (TREE_CODE (expr) == SAVE_EXPR)
10350 : 31 : expr = TREE_OPERAND (expr, 0);
10351 : 9551 : STRIP_NOPS (expr);
10352 : 9551 : return expr;
10353 : : }
10354 : :
10355 : : return NULL_TREE;
10356 : : }
10357 : :
10358 : : /* An attach or detach operation depends directly on the address being
10359 : : attached/detached. Return that address, or none if there are no
10360 : : attachments/detachments. */
10361 : :
10362 : : static tree
10363 : 14962 : omp_get_attachment (omp_mapping_group *grp)
10364 : : {
10365 : 14962 : tree node = *grp->grp_start;
10366 : :
10367 : 14962 : switch (OMP_CLAUSE_MAP_KIND (node))
10368 : : {
10369 : 12474 : case GOMP_MAP_TO:
10370 : 12474 : case GOMP_MAP_FROM:
10371 : 12474 : case GOMP_MAP_TOFROM:
10372 : 12474 : case GOMP_MAP_ALWAYS_FROM:
10373 : 12474 : case GOMP_MAP_ALWAYS_TO:
10374 : 12474 : case GOMP_MAP_ALWAYS_TOFROM:
10375 : 12474 : case GOMP_MAP_FORCE_FROM:
10376 : 12474 : case GOMP_MAP_FORCE_TO:
10377 : 12474 : case GOMP_MAP_FORCE_TOFROM:
10378 : 12474 : case GOMP_MAP_FORCE_PRESENT:
10379 : 12474 : case GOMP_MAP_PRESENT_ALLOC:
10380 : 12474 : case GOMP_MAP_PRESENT_FROM:
10381 : 12474 : case GOMP_MAP_PRESENT_TO:
10382 : 12474 : case GOMP_MAP_PRESENT_TOFROM:
10383 : 12474 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
10384 : 12474 : case GOMP_MAP_ALWAYS_PRESENT_TO:
10385 : 12474 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10386 : 12474 : case GOMP_MAP_ALLOC:
10387 : 12474 : case GOMP_MAP_RELEASE:
10388 : 12474 : case GOMP_MAP_DELETE:
10389 : 12474 : case GOMP_MAP_FORCE_ALLOC:
10390 : 12474 : if (node == grp->grp_end)
10391 : : return NULL_TREE;
10392 : :
10393 : 5943 : node = OMP_CLAUSE_CHAIN (node);
10394 : 5943 : if (node && omp_map_clause_descriptor_p (node))
10395 : : {
10396 : 1002 : gcc_assert (node != grp->grp_end);
10397 : 1002 : node = OMP_CLAUSE_CHAIN (node);
10398 : : }
10399 : 5943 : if (node)
10400 : 5943 : switch (OMP_CLAUSE_MAP_KIND (node))
10401 : : {
10402 : : case GOMP_MAP_POINTER:
10403 : : case GOMP_MAP_ALWAYS_POINTER:
10404 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10405 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10406 : : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10407 : : return NULL_TREE;
10408 : :
10409 : 2075 : case GOMP_MAP_ATTACH_DETACH:
10410 : 2075 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10411 : 2075 : case GOMP_MAP_DETACH:
10412 : 2075 : return OMP_CLAUSE_DECL (node);
10413 : :
10414 : 0 : default:
10415 : 0 : internal_error ("unexpected mapping node");
10416 : : }
10417 : 0 : return error_mark_node;
10418 : :
10419 : 0 : case GOMP_MAP_TO_PSET:
10420 : 0 : gcc_assert (node != grp->grp_end);
10421 : 0 : node = OMP_CLAUSE_CHAIN (node);
10422 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10423 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10424 : 0 : return OMP_CLAUSE_DECL (node);
10425 : : else
10426 : 0 : internal_error ("unexpected mapping node");
10427 : : return error_mark_node;
10428 : :
10429 : 425 : case GOMP_MAP_ATTACH:
10430 : 425 : case GOMP_MAP_DETACH:
10431 : 425 : node = OMP_CLAUSE_CHAIN (node);
10432 : 425 : if (!node || *grp->grp_start == grp->grp_end)
10433 : 425 : return OMP_CLAUSE_DECL (*grp->grp_start);
10434 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10435 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10436 : 0 : return OMP_CLAUSE_DECL (*grp->grp_start);
10437 : : else
10438 : 0 : internal_error ("unexpected mapping node");
10439 : : return error_mark_node;
10440 : :
10441 : : case GOMP_MAP_STRUCT:
10442 : : case GOMP_MAP_STRUCT_UNORD:
10443 : : case GOMP_MAP_FORCE_DEVICEPTR:
10444 : : case GOMP_MAP_DEVICE_RESIDENT:
10445 : : case GOMP_MAP_LINK:
10446 : : case GOMP_MAP_IF_PRESENT:
10447 : : case GOMP_MAP_FIRSTPRIVATE:
10448 : : case GOMP_MAP_FIRSTPRIVATE_INT:
10449 : : case GOMP_MAP_USE_DEVICE_PTR:
10450 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10451 : : return NULL_TREE;
10452 : :
10453 : 0 : default:
10454 : 0 : internal_error ("unexpected mapping node");
10455 : : }
10456 : :
10457 : : return error_mark_node;
10458 : : }
10459 : :
10460 : : /* Given a pointer START_P to the start of a group of related (e.g. pointer)
10461 : : mappings, return the chain pointer to the end of that group in the list. */
10462 : :
10463 : : static tree *
10464 : 104164 : omp_group_last (tree *start_p)
10465 : : {
10466 : 104164 : tree c = *start_p, nc, *grp_last_p = start_p;
10467 : :
10468 : 104164 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
10469 : :
10470 : 104164 : nc = OMP_CLAUSE_CHAIN (c);
10471 : :
10472 : 182211 : if (!nc || OMP_CLAUSE_CODE (nc) != OMP_CLAUSE_MAP)
10473 : : return grp_last_p;
10474 : :
10475 : 65970 : switch (OMP_CLAUSE_MAP_KIND (c))
10476 : : {
10477 : : default:
10478 : : while (nc
10479 : 106567 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10480 : 223483 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10481 : 99405 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10482 : 89355 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH
10483 : 78691 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_POINTER
10484 : 52868 : || (OMP_CLAUSE_MAP_KIND (nc)
10485 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10486 : 52852 : || (OMP_CLAUSE_MAP_KIND (nc)
10487 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)
10488 : 52485 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH
10489 : 52430 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ALWAYS_POINTER
10490 : 50526 : || omp_map_clause_descriptor_p (nc)))
10491 : : {
10492 : 62698 : tree nc2 = OMP_CLAUSE_CHAIN (nc);
10493 : 62698 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH)
10494 : : {
10495 : : /* In the specific case we're doing "exit data" on an array
10496 : : slice of a reference-to-pointer struct component, we will see
10497 : : DETACH followed by ATTACH_DETACH here. We want to treat that
10498 : : as a single group. In other cases DETACH might represent a
10499 : : stand-alone "detach" clause, so we don't want to consider
10500 : : that part of the group. */
10501 : 55 : if (nc2
10502 : 16 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10503 : 71 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH_DETACH)
10504 : 0 : goto consume_two_nodes;
10505 : : else
10506 : : break;
10507 : : }
10508 : 62643 : if (nc2
10509 : 45785 : && OMP_CLAUSE_CODE (nc2) == OMP_CLAUSE_MAP
10510 : 39272 : && (OMP_CLAUSE_MAP_KIND (nc)
10511 : : == GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION)
10512 : 62659 : && OMP_CLAUSE_MAP_KIND (nc2) == GOMP_MAP_ATTACH)
10513 : : {
10514 : 16 : consume_two_nodes:
10515 : 16 : grp_last_p = &OMP_CLAUSE_CHAIN (nc);
10516 : 16 : c = nc2;
10517 : 16 : nc = OMP_CLAUSE_CHAIN (nc2);
10518 : : }
10519 : : else
10520 : : {
10521 : 62627 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
10522 : 62627 : c = nc;
10523 : 62627 : nc = nc2;
10524 : : }
10525 : : }
10526 : : break;
10527 : :
10528 : 131 : case GOMP_MAP_ATTACH:
10529 : 131 : case GOMP_MAP_DETACH:
10530 : : /* This is a weird artifact of how directives are parsed: bare attach or
10531 : : detach clauses get a subsequent (meaningless) FIRSTPRIVATE_POINTER or
10532 : : FIRSTPRIVATE_REFERENCE node. FIXME. */
10533 : 131 : if (nc
10534 : 131 : && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10535 : 131 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10536 : 131 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER))
10537 : 0 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
10538 : : break;
10539 : :
10540 : 27 : case GOMP_MAP_TO_PSET:
10541 : 27 : if (OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10542 : 27 : && (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH
10543 : 6 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_DETACH))
10544 : 27 : grp_last_p = &OMP_CLAUSE_CHAIN (c);
10545 : : break;
10546 : :
10547 : 5026 : case GOMP_MAP_STRUCT:
10548 : 5026 : case GOMP_MAP_STRUCT_UNORD:
10549 : 5026 : {
10550 : 5026 : unsigned HOST_WIDE_INT num_mappings
10551 : 5026 : = tree_to_uhwi (OMP_CLAUSE_SIZE (c));
10552 : 5026 : if (OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_POINTER
10553 : 4688 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
10554 : 9232 : || OMP_CLAUSE_MAP_KIND (nc) == GOMP_MAP_ATTACH_DETACH)
10555 : 1134 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
10556 : 12263 : for (unsigned i = 0; i < num_mappings; i++)
10557 : 7237 : grp_last_p = &OMP_CLAUSE_CHAIN (*grp_last_p);
10558 : : }
10559 : : break;
10560 : : }
10561 : :
10562 : : return grp_last_p;
10563 : : }
10564 : :
10565 : : /* Walk through LIST_P, and return a list of groups of mappings found (e.g.
10566 : : OMP_CLAUSE_MAP with GOMP_MAP_{TO/FROM/TOFROM} followed by one or two
10567 : : associated GOMP_MAP_POINTER mappings). Return a vector of omp_mapping_group
10568 : : if we have more than one such group, else return NULL. */
10569 : :
10570 : : static void
10571 : 86195 : omp_gather_mapping_groups_1 (tree *list_p, vec<omp_mapping_group> *groups,
10572 : : tree gather_sentinel)
10573 : : {
10574 : 86195 : for (tree *cp = list_p;
10575 : 255011 : *cp && *cp != gather_sentinel;
10576 : 168816 : cp = &OMP_CLAUSE_CHAIN (*cp))
10577 : : {
10578 : 168816 : if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
10579 : 94629 : continue;
10580 : :
10581 : 74187 : tree *grp_last_p = omp_group_last (cp);
10582 : 74187 : omp_mapping_group grp;
10583 : :
10584 : 74187 : grp.grp_start = cp;
10585 : 74187 : grp.grp_end = *grp_last_p;
10586 : 74187 : grp.mark = UNVISITED;
10587 : 74187 : grp.sibling = NULL;
10588 : 74187 : grp.deleted = false;
10589 : 74187 : grp.reprocess_struct = false;
10590 : 74187 : grp.fragile = false;
10591 : 74187 : grp.next = NULL;
10592 : 74187 : groups->safe_push (grp);
10593 : :
10594 : 74187 : cp = grp_last_p;
10595 : : }
10596 : 86195 : }
10597 : :
10598 : : static vec<omp_mapping_group> *
10599 : 85877 : omp_gather_mapping_groups (tree *list_p)
10600 : : {
10601 : 85877 : vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
10602 : :
10603 : 85877 : omp_gather_mapping_groups_1 (list_p, groups, NULL_TREE);
10604 : :
10605 : 85877 : if (groups->length () > 0)
10606 : : return groups;
10607 : : else
10608 : : {
10609 : 43879 : delete groups;
10610 : 43879 : return NULL;
10611 : : }
10612 : : }
10613 : :
10614 : : /* A pointer mapping group GRP may define a block of memory starting at some
10615 : : base address, and maybe also define a firstprivate pointer or firstprivate
10616 : : reference that points to that block. The return value is a node containing
10617 : : the former, and the *FIRSTPRIVATE pointer is set if we have the latter.
10618 : : If we define several base pointers, i.e. for a GOMP_MAP_STRUCT mapping,
10619 : : return the number of consecutive chained nodes in CHAINED. */
10620 : :
10621 : : static tree
10622 : 74581 : omp_group_base (omp_mapping_group *grp, unsigned int *chained,
10623 : : tree *firstprivate)
10624 : : {
10625 : 74581 : tree node = *grp->grp_start;
10626 : :
10627 : 74581 : *firstprivate = NULL_TREE;
10628 : 74581 : *chained = 1;
10629 : :
10630 : 74581 : switch (OMP_CLAUSE_MAP_KIND (node))
10631 : : {
10632 : 70907 : case GOMP_MAP_TO:
10633 : 70907 : case GOMP_MAP_FROM:
10634 : 70907 : case GOMP_MAP_TOFROM:
10635 : 70907 : case GOMP_MAP_ALWAYS_FROM:
10636 : 70907 : case GOMP_MAP_ALWAYS_TO:
10637 : 70907 : case GOMP_MAP_ALWAYS_TOFROM:
10638 : 70907 : case GOMP_MAP_FORCE_FROM:
10639 : 70907 : case GOMP_MAP_FORCE_TO:
10640 : 70907 : case GOMP_MAP_FORCE_TOFROM:
10641 : 70907 : case GOMP_MAP_FORCE_PRESENT:
10642 : 70907 : case GOMP_MAP_PRESENT_ALLOC:
10643 : 70907 : case GOMP_MAP_PRESENT_FROM:
10644 : 70907 : case GOMP_MAP_PRESENT_TO:
10645 : 70907 : case GOMP_MAP_PRESENT_TOFROM:
10646 : 70907 : case GOMP_MAP_ALWAYS_PRESENT_FROM:
10647 : 70907 : case GOMP_MAP_ALWAYS_PRESENT_TO:
10648 : 70907 : case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10649 : 70907 : case GOMP_MAP_ALLOC:
10650 : 70907 : case GOMP_MAP_RELEASE:
10651 : 70907 : case GOMP_MAP_DELETE:
10652 : 70907 : case GOMP_MAP_FORCE_ALLOC:
10653 : 70907 : case GOMP_MAP_IF_PRESENT:
10654 : 70907 : if (node == grp->grp_end)
10655 : : return node;
10656 : :
10657 : 32531 : node = OMP_CLAUSE_CHAIN (node);
10658 : 32531 : if (!node)
10659 : 0 : internal_error ("unexpected mapping node");
10660 : 32531 : if (omp_map_clause_descriptor_p (node))
10661 : : {
10662 : 9666 : if (node == grp->grp_end)
10663 : 0 : return *grp->grp_start;
10664 : 9666 : node = OMP_CLAUSE_CHAIN (node);
10665 : : }
10666 : 32531 : switch (OMP_CLAUSE_MAP_KIND (node))
10667 : : {
10668 : 22854 : case GOMP_MAP_POINTER:
10669 : 22854 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10670 : 22854 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10671 : 22854 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10672 : 22854 : *firstprivate = OMP_CLAUSE_DECL (node);
10673 : 22854 : return *grp->grp_start;
10674 : :
10675 : 9677 : case GOMP_MAP_ALWAYS_POINTER:
10676 : 9677 : case GOMP_MAP_ATTACH_DETACH:
10677 : 9677 : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10678 : 9677 : case GOMP_MAP_DETACH:
10679 : 9677 : return *grp->grp_start;
10680 : :
10681 : 0 : default:
10682 : 0 : internal_error ("unexpected mapping node");
10683 : : }
10684 : : return error_mark_node;
10685 : :
10686 : 18 : case GOMP_MAP_TO_PSET:
10687 : 18 : gcc_assert (node != grp->grp_end);
10688 : 18 : node = OMP_CLAUSE_CHAIN (node);
10689 : 18 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH
10690 : 18 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_DETACH)
10691 : : return NULL_TREE;
10692 : : else
10693 : 0 : internal_error ("unexpected mapping node");
10694 : : return error_mark_node;
10695 : :
10696 : 949 : case GOMP_MAP_ATTACH:
10697 : 949 : case GOMP_MAP_DETACH:
10698 : 949 : node = OMP_CLAUSE_CHAIN (node);
10699 : 949 : if (!node || *grp->grp_start == grp->grp_end)
10700 : : return NULL_TREE;
10701 : 0 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10702 : 0 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10703 : : {
10704 : : /* We're mapping the base pointer itself in a bare attach or detach
10705 : : node. This is a side effect of how parsing works, and the mapping
10706 : : will be removed anyway (at least for enter/exit data directives).
10707 : : We should ignore the mapping here. FIXME. */
10708 : : return NULL_TREE;
10709 : : }
10710 : : else
10711 : 0 : internal_error ("unexpected mapping node");
10712 : : return error_mark_node;
10713 : :
10714 : 2423 : case GOMP_MAP_STRUCT:
10715 : 2423 : case GOMP_MAP_STRUCT_UNORD:
10716 : 2423 : {
10717 : 2423 : unsigned HOST_WIDE_INT num_mappings
10718 : 2423 : = tree_to_uhwi (OMP_CLAUSE_SIZE (node));
10719 : 2423 : node = OMP_CLAUSE_CHAIN (node);
10720 : 2423 : if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_POINTER
10721 : 2423 : || OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
10722 : : {
10723 : 393 : *firstprivate = OMP_CLAUSE_DECL (node);
10724 : 393 : node = OMP_CLAUSE_CHAIN (node);
10725 : : }
10726 : 2030 : else if (OMP_CLAUSE_MAP_KIND (node) == GOMP_MAP_ATTACH_DETACH)
10727 : 314 : node = OMP_CLAUSE_CHAIN (node);
10728 : 2423 : *chained = num_mappings;
10729 : 2423 : return node;
10730 : : }
10731 : :
10732 : : case GOMP_MAP_FORCE_DEVICEPTR:
10733 : : case GOMP_MAP_DEVICE_RESIDENT:
10734 : : case GOMP_MAP_LINK:
10735 : : case GOMP_MAP_FIRSTPRIVATE:
10736 : : case GOMP_MAP_FIRSTPRIVATE_INT:
10737 : : case GOMP_MAP_USE_DEVICE_PTR:
10738 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10739 : : return NULL_TREE;
10740 : :
10741 : 0 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
10742 : 0 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10743 : 0 : case GOMP_MAP_POINTER:
10744 : 0 : case GOMP_MAP_ALWAYS_POINTER:
10745 : 0 : case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10746 : : /* These shouldn't appear by themselves. */
10747 : 0 : if (!seen_error ())
10748 : 0 : internal_error ("unexpected pointer mapping node");
10749 : 0 : return error_mark_node;
10750 : :
10751 : 0 : default:
10752 : 0 : gcc_unreachable ();
10753 : : }
10754 : :
10755 : : return error_mark_node;
10756 : : }
10757 : :
10758 : : /* Given a vector of omp_mapping_groups, build a hash table so we can look up
10759 : : nodes by tree_operand_hash_no_se. */
10760 : :
10761 : : static void
10762 : 42482 : omp_index_mapping_groups_1 (hash_map<tree_operand_hash_no_se,
10763 : : omp_mapping_group *> *grpmap,
10764 : : vec<omp_mapping_group> *groups,
10765 : : tree reindex_sentinel)
10766 : : {
10767 : 42482 : omp_mapping_group *grp;
10768 : 42482 : unsigned int i;
10769 : 42482 : bool reindexing = reindex_sentinel != NULL_TREE, above_hwm = false;
10770 : :
10771 : 117517 : FOR_EACH_VEC_ELT (*groups, i, grp)
10772 : : {
10773 : 75035 : if (reindexing && *grp->grp_start == reindex_sentinel)
10774 : 75035 : above_hwm = true;
10775 : :
10776 : 75035 : if (reindexing && !above_hwm)
10777 : 51788 : continue;
10778 : :
10779 : 74735 : if (grp->reprocess_struct)
10780 : 154 : continue;
10781 : :
10782 : 74581 : tree fpp;
10783 : 74581 : unsigned int chained;
10784 : 74581 : tree node = omp_group_base (grp, &chained, &fpp);
10785 : :
10786 : 74581 : if (node == error_mark_node || (!node && !fpp))
10787 : 1251 : continue;
10788 : :
10789 : : for (unsigned j = 0;
10790 : 147737 : node && j < chained;
10791 : 74407 : node = OMP_CLAUSE_CHAIN (node), j++)
10792 : : {
10793 : 74407 : tree decl = OMP_CLAUSE_DECL (node);
10794 : : /* Sometimes we see zero-offset MEM_REF instead of INDIRECT_REF,
10795 : : meaning node-hash lookups don't work. This is a workaround for
10796 : : that, but ideally we should just create the INDIRECT_REF at
10797 : : source instead. FIXME. */
10798 : 74407 : if (TREE_CODE (decl) == MEM_REF
10799 : 74407 : && integer_zerop (TREE_OPERAND (decl, 1)))
10800 : 0 : decl = build_fold_indirect_ref (TREE_OPERAND (decl, 0));
10801 : :
10802 : 74407 : omp_mapping_group **prev = grpmap->get (decl);
10803 : :
10804 : 74407 : if (prev && *prev == grp)
10805 : : /* Empty. */;
10806 : 74407 : else if (prev)
10807 : : {
10808 : : /* Mapping the same thing twice is normally diagnosed as an error,
10809 : : but can happen under some circumstances, e.g. in pr99928-16.c,
10810 : : the directive:
10811 : :
10812 : : #pragma omp target simd reduction(+:a[:3]) \
10813 : : map(always, tofrom: a[:6])
10814 : : ...
10815 : :
10816 : : will result in two "a[0]" mappings (of different sizes). */
10817 : :
10818 : 140 : grp->sibling = (*prev)->sibling;
10819 : 140 : (*prev)->sibling = grp;
10820 : : }
10821 : : else
10822 : 74267 : grpmap->put (decl, grp);
10823 : : }
10824 : :
10825 : 73330 : if (!fpp)
10826 : 50083 : continue;
10827 : :
10828 : 23247 : omp_mapping_group **prev = grpmap->get (fpp);
10829 : 23247 : if (prev && *prev != grp)
10830 : : {
10831 : 12 : grp->sibling = (*prev)->sibling;
10832 : 12 : (*prev)->sibling = grp;
10833 : : }
10834 : : else
10835 : 23235 : grpmap->put (fpp, grp);
10836 : : }
10837 : 42482 : }
10838 : :
10839 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
10840 : 41998 : omp_index_mapping_groups (vec<omp_mapping_group> *groups)
10841 : : {
10842 : 41998 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
10843 : 41998 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
10844 : :
10845 : 41998 : omp_index_mapping_groups_1 (grpmap, groups, NULL_TREE);
10846 : :
10847 : 41998 : return grpmap;
10848 : : }
10849 : :
10850 : : /* Rebuild group map from partially-processed clause list (during
10851 : : omp_build_struct_sibling_lists). We have already processed nodes up until
10852 : : a high-water mark (HWM). This is a bit tricky because the list is being
10853 : : reordered as it is scanned, but we know:
10854 : :
10855 : : 1. The list after HWM has not been touched yet, so we can reindex it safely.
10856 : :
10857 : : 2. The list before and including HWM has been altered, but remains
10858 : : well-formed throughout the sibling-list building operation.
10859 : :
10860 : : so, we can do the reindex operation in two parts, on the processed and
10861 : : then the unprocessed halves of the list. */
10862 : :
10863 : : static hash_map<tree_operand_hash_no_se, omp_mapping_group *> *
10864 : 318 : omp_reindex_mapping_groups (tree *list_p,
10865 : : vec<omp_mapping_group> *groups,
10866 : : vec<omp_mapping_group> *processed_groups,
10867 : : tree sentinel)
10868 : : {
10869 : 318 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap
10870 : 318 : = new hash_map<tree_operand_hash_no_se, omp_mapping_group *>;
10871 : :
10872 : 318 : processed_groups->truncate (0);
10873 : :
10874 : 318 : omp_gather_mapping_groups_1 (list_p, processed_groups, sentinel);
10875 : 318 : omp_index_mapping_groups_1 (grpmap, processed_groups, NULL_TREE);
10876 : 318 : if (sentinel)
10877 : 166 : omp_index_mapping_groups_1 (grpmap, groups, sentinel);
10878 : :
10879 : 318 : return grpmap;
10880 : : }
10881 : :
10882 : : /* Find the immediately-containing struct for a component ref (etc.)
10883 : : expression EXPR. */
10884 : :
10885 : : static tree
10886 : 37391 : omp_containing_struct (tree expr)
10887 : : {
10888 : 37391 : tree expr0 = expr;
10889 : :
10890 : 37391 : STRIP_NOPS (expr);
10891 : :
10892 : : /* Note: don't strip NOPs unless we're also stripping off array refs or a
10893 : : component ref. */
10894 : 37391 : if (TREE_CODE (expr) != ARRAY_REF && TREE_CODE (expr) != COMPONENT_REF)
10895 : : return expr0;
10896 : :
10897 : 18576 : while (TREE_CODE (expr) == ARRAY_REF)
10898 : 2140 : expr = TREE_OPERAND (expr, 0);
10899 : :
10900 : 16436 : if (TREE_CODE (expr) == COMPONENT_REF)
10901 : 15517 : expr = TREE_OPERAND (expr, 0);
10902 : :
10903 : : return expr;
10904 : : }
10905 : :
10906 : : /* Return TRUE if DECL describes a component that is part of a whole structure
10907 : : that is mapped elsewhere in GRPMAP. *MAPPED_BY_GROUP is set to the group
10908 : : that maps that structure, if present. */
10909 : :
10910 : : static bool
10911 : 21949 : omp_mapped_by_containing_struct (hash_map<tree_operand_hash_no_se,
10912 : : omp_mapping_group *> *grpmap,
10913 : : tree decl,
10914 : : omp_mapping_group **mapped_by_group)
10915 : : {
10916 : 21949 : tree wsdecl = NULL_TREE;
10917 : :
10918 : 21949 : *mapped_by_group = NULL;
10919 : :
10920 : 37391 : while (true)
10921 : : {
10922 : 37391 : wsdecl = omp_containing_struct (decl);
10923 : 37391 : if (wsdecl == decl)
10924 : : break;
10925 : 16436 : omp_mapping_group **wholestruct = grpmap->get (wsdecl);
10926 : 16436 : if (!wholestruct
10927 : 15442 : && TREE_CODE (wsdecl) == MEM_REF
10928 : 16436 : && integer_zerop (TREE_OPERAND (wsdecl, 1)))
10929 : : {
10930 : 0 : tree deref = TREE_OPERAND (wsdecl, 0);
10931 : 0 : deref = build_fold_indirect_ref (deref);
10932 : 0 : wholestruct = grpmap->get (deref);
10933 : : }
10934 : 16436 : if (wholestruct)
10935 : : {
10936 : 994 : *mapped_by_group = *wholestruct;
10937 : 994 : return true;
10938 : : }
10939 : : decl = wsdecl;
10940 : : }
10941 : :
10942 : : return false;
10943 : : }
10944 : :
10945 : : /* Helper function for omp_tsort_mapping_groups. Returns TRUE on success, or
10946 : : FALSE on error. */
10947 : :
10948 : : static bool
10949 : 18121 : omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
10950 : : vec<omp_mapping_group> *groups,
10951 : : hash_map<tree_operand_hash_no_se,
10952 : : omp_mapping_group *> *grpmap,
10953 : : omp_mapping_group *grp)
10954 : : {
10955 : 18121 : if (grp->mark == PERMANENT)
10956 : : return true;
10957 : 14962 : if (grp->mark == TEMPORARY)
10958 : : {
10959 : 0 : fprintf (stderr, "when processing group:\n");
10960 : 0 : debug_mapping_group (grp);
10961 : 0 : internal_error ("base pointer cycle detected");
10962 : : return false;
10963 : : }
10964 : 14962 : grp->mark = TEMPORARY;
10965 : :
10966 : 14962 : tree attaches_to = omp_get_attachment (grp);
10967 : :
10968 : 14962 : if (attaches_to)
10969 : : {
10970 : 2500 : omp_mapping_group **basep = grpmap->get (attaches_to);
10971 : :
10972 : 2500 : if (basep && *basep != grp)
10973 : : {
10974 : 2896 : for (omp_mapping_group *w = *basep; w; w = w->sibling)
10975 : 1448 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
10976 : : return false;
10977 : : }
10978 : : }
10979 : :
10980 : 14962 : tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
10981 : :
10982 : 20378 : while (decl)
10983 : : {
10984 : 20378 : tree base = omp_get_base_pointer (decl);
10985 : :
10986 : 20378 : if (!base)
10987 : : break;
10988 : :
10989 : 7961 : omp_mapping_group **innerp = grpmap->get (base);
10990 : 7961 : omp_mapping_group *wholestruct;
10991 : :
10992 : : /* We should treat whole-structure mappings as if all (pointer, in this
10993 : : case) members are mapped as individual list items. Check if we have
10994 : : such a whole-structure mapping, if we don't have an explicit reference
10995 : : to the pointer member itself. */
10996 : 7961 : if (!innerp
10997 : 3288 : && TREE_CODE (base) == COMPONENT_REF
10998 : 9352 : && omp_mapped_by_containing_struct (grpmap, base, &wholestruct))
10999 : : innerp = &wholestruct;
11000 : :
11001 : 7961 : if (innerp && *innerp != grp)
11002 : : {
11003 : 5090 : for (omp_mapping_group *w = *innerp; w; w = w->sibling)
11004 : 2545 : if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
11005 : 0 : return false;
11006 : : break;
11007 : : }
11008 : :
11009 : 5416 : decl = base;
11010 : : }
11011 : :
11012 : 14962 : grp->mark = PERMANENT;
11013 : :
11014 : : /* Emit grp to output list. */
11015 : :
11016 : 14962 : **outlist = grp;
11017 : 14962 : *outlist = &grp->next;
11018 : :
11019 : 14962 : return true;
11020 : : }
11021 : :
11022 : : /* Topologically sort GROUPS, so that OMP 5.0-defined base pointers come
11023 : : before mappings that use those pointers. This is an implementation of the
11024 : : depth-first search algorithm, described e.g. at:
11025 : :
11026 : : https://en.wikipedia.org/wiki/Topological_sorting
11027 : : */
11028 : :
11029 : : static omp_mapping_group *
11030 : 7649 : omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
11031 : : hash_map<tree_operand_hash_no_se, omp_mapping_group *>
11032 : : *grpmap,
11033 : : bool enter_exit_data)
11034 : : {
11035 : 7649 : omp_mapping_group *grp, *outlist = NULL, **cursor;
11036 : 7649 : unsigned int i;
11037 : 7649 : bool saw_runtime_implicit = false;
11038 : :
11039 : 7649 : cursor = &outlist;
11040 : :
11041 : 22611 : FOR_EACH_VEC_ELT (*groups, i, grp)
11042 : : {
11043 : 14962 : if (grp->mark != PERMANENT)
11044 : : {
11045 : 14128 : if (OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11046 : : {
11047 : 238 : saw_runtime_implicit = true;
11048 : 238 : continue;
11049 : : }
11050 : 13890 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11051 : : return NULL;
11052 : : }
11053 : : }
11054 : :
11055 : 7649 : if (!saw_runtime_implicit)
11056 : 7503 : return outlist;
11057 : :
11058 : 629 : FOR_EACH_VEC_ELT (*groups, i, grp)
11059 : : {
11060 : 483 : if (grp->mark != PERMANENT
11061 : 483 : && OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start))
11062 : : {
11063 : : /* Clear the flag for enter/exit data because it is currently
11064 : : meaningless for those operations in libgomp. */
11065 : 238 : if (enter_exit_data)
11066 : 41 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (*grp->grp_start) = 0;
11067 : :
11068 : 238 : if (!omp_tsort_mapping_groups_1 (&cursor, groups, grpmap, grp))
11069 : : return NULL;
11070 : : }
11071 : : }
11072 : :
11073 : 146 : return outlist;
11074 : : }
11075 : :
11076 : : /* Split INLIST into three parts:
11077 : :
11078 : : - "present" alloc/to/from groups
11079 : : - other to/from groups
11080 : : - other alloc/release/delete groups
11081 : :
11082 : : These sub-lists are then concatenated together to form the final list.
11083 : : Each sub-list retains the order of the original list.
11084 : : Note that ATTACH nodes are later moved to the end of the list in
11085 : : gimplify_adjust_omp_clauses, for target regions. */
11086 : :
11087 : : static omp_mapping_group *
11088 : 7649 : omp_segregate_mapping_groups (omp_mapping_group *inlist)
11089 : : {
11090 : 7649 : omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
11091 : 7649 : omp_mapping_group *p_groups = NULL;
11092 : 7649 : omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
11093 : 7649 : omp_mapping_group **p_tail = &p_groups;
11094 : :
11095 : 22611 : for (omp_mapping_group *w = inlist; w;)
11096 : : {
11097 : 14962 : tree c = *w->grp_start;
11098 : 14962 : omp_mapping_group *next = w->next;
11099 : :
11100 : 14962 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP);
11101 : :
11102 : 14962 : switch (OMP_CLAUSE_MAP_KIND (c))
11103 : : {
11104 : 1673 : case GOMP_MAP_ALLOC:
11105 : 1673 : case GOMP_MAP_RELEASE:
11106 : 1673 : case GOMP_MAP_DELETE:
11107 : 1673 : *ard_tail = w;
11108 : 1673 : w->next = NULL;
11109 : 1673 : ard_tail = &w->next;
11110 : 1673 : break;
11111 : :
11112 : : /* These map types are all semantically identical, so are moved into a
11113 : : single group. They will each be changed into GOMP_MAP_FORCE_PRESENT
11114 : : in gimplify_adjust_omp_clauses. */
11115 : 123 : case GOMP_MAP_PRESENT_ALLOC:
11116 : 123 : case GOMP_MAP_PRESENT_FROM:
11117 : 123 : case GOMP_MAP_PRESENT_TO:
11118 : 123 : case GOMP_MAP_PRESENT_TOFROM:
11119 : 123 : *p_tail = w;
11120 : 123 : w->next = NULL;
11121 : 123 : p_tail = &w->next;
11122 : 123 : break;
11123 : :
11124 : 13166 : default:
11125 : 13166 : *tf_tail = w;
11126 : 13166 : w->next = NULL;
11127 : 13166 : tf_tail = &w->next;
11128 : : }
11129 : :
11130 : : w = next;
11131 : : }
11132 : :
11133 : : /* Now splice the lists together... */
11134 : 7649 : *tf_tail = ard_groups;
11135 : 7649 : *p_tail = tf_groups;
11136 : :
11137 : 7649 : return p_groups;
11138 : : }
11139 : :
11140 : : /* Given a list LIST_P containing groups of mappings given by GROUPS, reorder
11141 : : those groups based on the output list of omp_tsort_mapping_groups --
11142 : : singly-linked, threaded through each element's NEXT pointer starting at
11143 : : HEAD. Each list element appears exactly once in that linked list.
11144 : :
11145 : : Each element of GROUPS may correspond to one or several mapping nodes.
11146 : : Node groups are kept together, and in the reordered list, the positions of
11147 : : the original groups are reused for the positions of the reordered list.
11148 : : Hence if we have e.g.
11149 : :
11150 : : {to ptr ptr} firstprivate {tofrom ptr} ...
11151 : : ^ ^ ^
11152 : : first group non-"map" second group
11153 : :
11154 : : and say the second group contains a base pointer for the first so must be
11155 : : moved before it, the resulting list will contain:
11156 : :
11157 : : {tofrom ptr} firstprivate {to ptr ptr} ...
11158 : : ^ prev. second group ^ prev. first group
11159 : : */
11160 : :
11161 : : static tree *
11162 : 7649 : omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
11163 : : omp_mapping_group *head,
11164 : : tree *list_p)
11165 : : {
11166 : 7649 : omp_mapping_group *grp;
11167 : 7649 : unsigned int i;
11168 : 7649 : unsigned numgroups = groups->length ();
11169 : 7649 : auto_vec<tree> old_heads (numgroups);
11170 : 7649 : auto_vec<tree *> old_headps (numgroups);
11171 : 7649 : auto_vec<tree> new_heads (numgroups);
11172 : 7649 : auto_vec<tree> old_succs (numgroups);
11173 : 7649 : bool map_at_start = (list_p == (*groups)[0].grp_start);
11174 : :
11175 : 7649 : tree *new_grp_tail = NULL;
11176 : :
11177 : : /* Stash the start & end nodes of each mapping group before we start
11178 : : modifying the list. */
11179 : 22611 : FOR_EACH_VEC_ELT (*groups, i, grp)
11180 : : {
11181 : 14962 : old_headps.quick_push (grp->grp_start);
11182 : 14962 : old_heads.quick_push (*grp->grp_start);
11183 : 14962 : old_succs.quick_push (OMP_CLAUSE_CHAIN (grp->grp_end));
11184 : : }
11185 : :
11186 : : /* And similarly, the heads of the groups in the order we want to rearrange
11187 : : the list to. */
11188 : 22611 : for (omp_mapping_group *w = head; w; w = w->next)
11189 : 14962 : new_heads.quick_push (*w->grp_start);
11190 : :
11191 : 22611 : FOR_EACH_VEC_ELT (*groups, i, grp)
11192 : : {
11193 : 14962 : gcc_assert (head);
11194 : :
11195 : 14962 : if (new_grp_tail && old_succs[i - 1] == old_heads[i])
11196 : : {
11197 : : /* a {b c d} {e f g} h i j (original)
11198 : : -->
11199 : : a {k l m} {e f g} h i j (inserted new group on last iter)
11200 : : -->
11201 : : a {k l m} {n o p} h i j (this time, chain last group to new one)
11202 : : ^new_grp_tail
11203 : : */
11204 : 6691 : *new_grp_tail = new_heads[i];
11205 : : }
11206 : 8271 : else if (new_grp_tail)
11207 : : {
11208 : : /* a {b c d} e {f g h} i j k (original)
11209 : : -->
11210 : : a {l m n} e {f g h} i j k (gap after last iter's group)
11211 : : -->
11212 : : a {l m n} e {o p q} h i j (chain last group to old successor)
11213 : : ^new_grp_tail
11214 : : */
11215 : 622 : *new_grp_tail = old_succs[i - 1];
11216 : 622 : *old_headps[i] = new_heads[i];
11217 : : }
11218 : : else
11219 : : {
11220 : : /* The first inserted group -- point to new group, and leave end
11221 : : open.
11222 : : a {b c d} e f
11223 : : -->
11224 : : a {g h i...
11225 : : */
11226 : 7649 : *grp->grp_start = new_heads[i];
11227 : : }
11228 : :
11229 : 14962 : new_grp_tail = &OMP_CLAUSE_CHAIN (head->grp_end);
11230 : :
11231 : 14962 : head = head->next;
11232 : : }
11233 : :
11234 : 7649 : if (new_grp_tail)
11235 : 7649 : *new_grp_tail = old_succs[numgroups - 1];
11236 : :
11237 : 7649 : gcc_assert (!head);
11238 : :
11239 : 10731 : return map_at_start ? (*groups)[0].grp_start : list_p;
11240 : 7649 : }
11241 : :
11242 : : /* DECL is supposed to have lastprivate semantics in the outer contexts
11243 : : of combined/composite constructs, starting with OCTX.
11244 : : Add needed lastprivate, shared or map clause if no data sharing or
11245 : : mapping clause are present. IMPLICIT_P is true if it is an implicit
11246 : : clause (IV on simd), in which case the lastprivate will not be
11247 : : copied to some constructs. */
11248 : :
11249 : : static void
11250 : 14648 : omp_lastprivate_for_combined_outer_constructs (struct gimplify_omp_ctx *octx,
11251 : : tree decl, bool implicit_p)
11252 : : {
11253 : 14648 : struct gimplify_omp_ctx *orig_octx = octx;
11254 : 26964 : for (; octx; octx = octx->outer_context)
11255 : : {
11256 : 25894 : if ((octx->region_type == ORT_COMBINED_PARALLEL
11257 : 20862 : || (octx->region_type & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS)
11258 : 25325 : && splay_tree_lookup (octx->variables,
11259 : : (splay_tree_key) decl) == NULL)
11260 : : {
11261 : 3883 : omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
11262 : 3883 : continue;
11263 : : }
11264 : 18376 : if ((octx->region_type & ORT_TASK) != 0
11265 : 575 : && octx->combined_loop
11266 : 18657 : && splay_tree_lookup (octx->variables,
11267 : : (splay_tree_key) decl) == NULL)
11268 : : {
11269 : 248 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11270 : 248 : continue;
11271 : : }
11272 : 22715 : if (implicit_p
11273 : 13624 : && octx->region_type == ORT_WORKSHARE
11274 : 10404 : && octx->combined_loop
11275 : 10299 : && splay_tree_lookup (octx->variables,
11276 : : (splay_tree_key) decl) == NULL
11277 : 10299 : && octx->outer_context
11278 : 8906 : && octx->outer_context->region_type == ORT_COMBINED_PARALLEL
11279 : 22722 : && splay_tree_lookup (octx->outer_context->variables,
11280 : : (splay_tree_key) decl) == NULL)
11281 : : {
11282 : 4835 : octx = octx->outer_context;
11283 : 4835 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11284 : 4835 : continue;
11285 : : }
11286 : 5567 : if ((octx->region_type == ORT_WORKSHARE || octx->region_type == ORT_ACC)
11287 : 7478 : && octx->combined_loop
11288 : 7291 : && splay_tree_lookup (octx->variables,
11289 : : (splay_tree_key) decl) == NULL
11290 : 19066 : && !omp_check_private (octx, decl, false))
11291 : : {
11292 : 3350 : omp_add_variable (octx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
11293 : 3350 : continue;
11294 : : }
11295 : 9695 : if (octx->region_type == ORT_COMBINED_TARGET)
11296 : : {
11297 : 2004 : splay_tree_node n = splay_tree_lookup (octx->variables,
11298 : : (splay_tree_key) decl);
11299 : 2004 : if (n == NULL)
11300 : : {
11301 : 1982 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11302 : 1982 : octx = octx->outer_context;
11303 : : }
11304 : 22 : else if (!implicit_p
11305 : 22 : && (n->value & GOVD_FIRSTPRIVATE_IMPLICIT))
11306 : : {
11307 : 12 : n->value &= ~(GOVD_FIRSTPRIVATE
11308 : : | GOVD_FIRSTPRIVATE_IMPLICIT
11309 : : | GOVD_EXPLICIT);
11310 : 12 : omp_add_variable (octx, decl, GOVD_MAP | GOVD_SEEN);
11311 : 12 : octx = octx->outer_context;
11312 : : }
11313 : : }
11314 : : break;
11315 : : }
11316 : 14648 : if (octx && (implicit_p || octx != orig_octx))
11317 : 4056 : omp_notice_variable (octx, decl, true);
11318 : 14648 : }
11319 : :
11320 : : /* We might have indexed several groups for DECL, e.g. a "TO" mapping and also
11321 : : a "FIRSTPRIVATE" mapping. Return the one that isn't firstprivate, etc. */
11322 : :
11323 : : static omp_mapping_group *
11324 : 5272 : omp_get_nonfirstprivate_group (hash_map<tree_operand_hash_no_se,
11325 : : omp_mapping_group *> *grpmap,
11326 : : tree decl, bool allow_deleted = false)
11327 : : {
11328 : 5272 : omp_mapping_group **to_group_p = grpmap->get (decl);
11329 : :
11330 : 5272 : if (!to_group_p)
11331 : : return NULL;
11332 : :
11333 : 2296 : omp_mapping_group *to_group = *to_group_p;
11334 : :
11335 : 3488 : for (; to_group; to_group = to_group->sibling)
11336 : : {
11337 : 2333 : tree grp_end = to_group->grp_end;
11338 : 2333 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11339 : : {
11340 : : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11341 : : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11342 : : break;
11343 : :
11344 : 1143 : default:
11345 : 1143 : if (allow_deleted || !to_group->deleted)
11346 : : return to_group;
11347 : : }
11348 : : }
11349 : :
11350 : : return NULL;
11351 : : }
11352 : :
11353 : : /* Return TRUE if the directive (whose clauses are described by the hash table
11354 : : of mapping groups, GRPMAP) maps DECL explicitly. If TO_SPECIFICALLY is
11355 : : true, only count TO mappings. If ALLOW_DELETED is true, ignore the
11356 : : "deleted" flag for groups. If CONTAINED_IN_STRUCT is true, also return
11357 : : TRUE if DECL is mapped as a member of a whole-struct mapping. */
11358 : :
11359 : : static bool
11360 : 3879 : omp_directive_maps_explicitly (hash_map<tree_operand_hash_no_se,
11361 : : omp_mapping_group *> *grpmap,
11362 : : tree decl, omp_mapping_group **base_group,
11363 : : bool to_specifically, bool allow_deleted,
11364 : : bool contained_in_struct)
11365 : : {
11366 : 3879 : omp_mapping_group *decl_group
11367 : 3879 : = omp_get_nonfirstprivate_group (grpmap, decl, allow_deleted);
11368 : :
11369 : 3879 : *base_group = NULL;
11370 : :
11371 : 3879 : if (decl_group)
11372 : : {
11373 : 1024 : tree grp_first = *decl_group->grp_start;
11374 : : /* We might be called during omp_build_struct_sibling_lists, when
11375 : : GOMP_MAP_STRUCT might have been inserted at the start of the group.
11376 : : Skip over that, and also possibly the node after it. */
11377 : 1024 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT
11378 : 1024 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_STRUCT_UNORD)
11379 : : {
11380 : 6 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11381 : 6 : if (OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_FIRSTPRIVATE_POINTER
11382 : 6 : || (OMP_CLAUSE_MAP_KIND (grp_first)
11383 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
11384 : 12 : || OMP_CLAUSE_MAP_KIND (grp_first) == GOMP_MAP_ATTACH_DETACH)
11385 : 0 : grp_first = OMP_CLAUSE_CHAIN (grp_first);
11386 : : }
11387 : 1024 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11388 : 1024 : if (!to_specifically
11389 : 574 : || GOMP_MAP_COPY_TO_P (first_kind)
11390 : 365 : || first_kind == GOMP_MAP_ALLOC)
11391 : : {
11392 : 959 : *base_group = decl_group;
11393 : 959 : return true;
11394 : : }
11395 : : }
11396 : :
11397 : 2920 : if (contained_in_struct
11398 : 2920 : && omp_mapped_by_containing_struct (grpmap, decl, base_group))
11399 : : return true;
11400 : :
11401 : : return false;
11402 : : }
11403 : :
11404 : : /* If we have mappings INNER and OUTER, where INNER is a component access and
11405 : : OUTER is a mapping of the whole containing struct, check that the mappings
11406 : : are compatible. We'll be deleting the inner mapping, so we need to make
11407 : : sure the outer mapping does (at least) the same transfers to/from the device
11408 : : as the inner mapping. */
11409 : :
11410 : : bool
11411 : 172 : omp_check_mapping_compatibility (location_t loc,
11412 : : omp_mapping_group *outer,
11413 : : omp_mapping_group *inner)
11414 : : {
11415 : 172 : tree first_outer = *outer->grp_start, first_inner = *inner->grp_start;
11416 : :
11417 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_outer) == OMP_CLAUSE_MAP);
11418 : 172 : gcc_assert (OMP_CLAUSE_CODE (first_inner) == OMP_CLAUSE_MAP);
11419 : :
11420 : 172 : enum gomp_map_kind outer_kind = OMP_CLAUSE_MAP_KIND (first_outer);
11421 : 172 : enum gomp_map_kind inner_kind = OMP_CLAUSE_MAP_KIND (first_inner);
11422 : :
11423 : 172 : if (outer_kind == inner_kind)
11424 : : return true;
11425 : :
11426 : 70 : switch (outer_kind)
11427 : : {
11428 : 0 : case GOMP_MAP_ALWAYS_TO:
11429 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11430 : 0 : || inner_kind == GOMP_MAP_ALLOC
11431 : 0 : || inner_kind == GOMP_MAP_TO)
11432 : : return true;
11433 : : break;
11434 : :
11435 : 0 : case GOMP_MAP_ALWAYS_FROM:
11436 : 0 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11437 : 0 : || inner_kind == GOMP_MAP_RELEASE
11438 : : || inner_kind == GOMP_MAP_FROM)
11439 : : return true;
11440 : : break;
11441 : :
11442 : 10 : case GOMP_MAP_TO:
11443 : 10 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11444 : 10 : || inner_kind == GOMP_MAP_ALLOC)
11445 : : return true;
11446 : : break;
11447 : :
11448 : 8 : case GOMP_MAP_FROM:
11449 : 8 : if (inner_kind == GOMP_MAP_RELEASE
11450 : 8 : || inner_kind == GOMP_MAP_FORCE_PRESENT)
11451 : : return true;
11452 : : break;
11453 : :
11454 : 32 : case GOMP_MAP_ALWAYS_TOFROM:
11455 : 32 : case GOMP_MAP_TOFROM:
11456 : 32 : if (inner_kind == GOMP_MAP_FORCE_PRESENT
11457 : 32 : || inner_kind == GOMP_MAP_ALLOC
11458 : : || inner_kind == GOMP_MAP_TO
11459 : 24 : || inner_kind == GOMP_MAP_FROM
11460 : 12 : || inner_kind == GOMP_MAP_TOFROM)
11461 : : return true;
11462 : : break;
11463 : :
11464 : 28 : default:
11465 : 28 : ;
11466 : : }
11467 : :
11468 : 84 : error_at (loc, "data movement for component %qE is not compatible with "
11469 : 28 : "movement for struct %qE", OMP_CLAUSE_DECL (first_inner),
11470 : 28 : OMP_CLAUSE_DECL (first_outer));
11471 : :
11472 : 28 : return false;
11473 : : }
11474 : :
11475 : : /* This function handles several cases where clauses on a mapping directive
11476 : : can interact with each other.
11477 : :
11478 : : If we have a FIRSTPRIVATE_POINTER node and we're also mapping the pointer
11479 : : on the same directive, change the mapping of the first node to
11480 : : ATTACH_DETACH. We should have detected that this will happen already in
11481 : : c-omp.cc:c_omp_adjust_map_clauses and marked the appropriate decl
11482 : : as addressable. (If we didn't, bail out.)
11483 : :
11484 : : If we have a FIRSTPRIVATE_REFERENCE (for a reference to pointer) and we're
11485 : : mapping the base pointer also, we may need to change the mapping type to
11486 : : ATTACH_DETACH and synthesize an alloc node for the reference itself.
11487 : :
11488 : : If we have an ATTACH_DETACH node, this is an array section with a pointer
11489 : : base. If we're mapping the base on the same directive too, we can drop its
11490 : : mapping. However, if we have a reference to pointer, make other appropriate
11491 : : adjustments to the mapping nodes instead.
11492 : :
11493 : : If we have an ATTACH_DETACH node with a Fortran pointer-set (array
11494 : : descriptor) mapping for a derived-type component, and we're also mapping the
11495 : : whole of the derived-type variable on another clause, the pointer-set
11496 : : mapping is removed.
11497 : :
11498 : : If we have a component access but we're also mapping the whole of the
11499 : : containing struct, drop the former access.
11500 : :
11501 : : If the expression is a component access, and we're also mapping a base
11502 : : pointer used in that component access in the same expression, change the
11503 : : mapping type of the latter to ALLOC (ready for processing by
11504 : : omp_build_struct_sibling_lists). */
11505 : :
11506 : : void
11507 : 7649 : omp_resolve_clause_dependencies (enum tree_code code,
11508 : : vec<omp_mapping_group> *groups,
11509 : : hash_map<tree_operand_hash_no_se,
11510 : : omp_mapping_group *> *grpmap)
11511 : : {
11512 : 7649 : int i;
11513 : 7649 : omp_mapping_group *grp;
11514 : 7649 : bool repair_chain = false;
11515 : :
11516 : 21766 : FOR_EACH_VEC_ELT (*groups, i, grp)
11517 : : {
11518 : 14117 : tree grp_end = grp->grp_end;
11519 : 14117 : tree decl = OMP_CLAUSE_DECL (grp_end);
11520 : :
11521 : 14117 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11522 : :
11523 : 14117 : switch (OMP_CLAUSE_MAP_KIND (grp_end))
11524 : : {
11525 : 1081 : case GOMP_MAP_FIRSTPRIVATE_POINTER:
11526 : 1081 : {
11527 : 1081 : omp_mapping_group *to_group
11528 : 1081 : = omp_get_nonfirstprivate_group (grpmap, decl);
11529 : :
11530 : 1081 : if (!to_group || to_group == grp)
11531 : 1056 : continue;
11532 : :
11533 : 25 : tree grp_first = *to_group->grp_start;
11534 : 25 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11535 : :
11536 : 25 : if ((GOMP_MAP_COPY_TO_P (first_kind)
11537 : 7 : || first_kind == GOMP_MAP_ALLOC)
11538 : 50 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
11539 : : != GOMP_MAP_FIRSTPRIVATE_POINTER))
11540 : : {
11541 : 25 : gcc_assert (TREE_ADDRESSABLE (OMP_CLAUSE_DECL (grp_end)));
11542 : 25 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
11543 : : }
11544 : : }
11545 : : break;
11546 : :
11547 : 156 : case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
11548 : 156 : {
11549 : 156 : tree ptr = build_fold_indirect_ref (decl);
11550 : :
11551 : 156 : omp_mapping_group *to_group
11552 : 156 : = omp_get_nonfirstprivate_group (grpmap, ptr);
11553 : :
11554 : 156 : if (!to_group || to_group == grp)
11555 : 152 : continue;
11556 : :
11557 : 4 : tree grp_first = *to_group->grp_start;
11558 : 4 : enum gomp_map_kind first_kind = OMP_CLAUSE_MAP_KIND (grp_first);
11559 : :
11560 : 4 : if (GOMP_MAP_COPY_TO_P (first_kind)
11561 : 4 : || first_kind == GOMP_MAP_ALLOC)
11562 : : {
11563 : 4 : OMP_CLAUSE_SET_MAP_KIND (grp_end, GOMP_MAP_ATTACH_DETACH);
11564 : 4 : OMP_CLAUSE_DECL (grp_end) = ptr;
11565 : 4 : if ((OMP_CLAUSE_CHAIN (*to_group->grp_start)
11566 : 4 : == to_group->grp_end)
11567 : 4 : && (OMP_CLAUSE_MAP_KIND (to_group->grp_end)
11568 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
11569 : : {
11570 : 0 : gcc_assert (TREE_ADDRESSABLE
11571 : : (OMP_CLAUSE_DECL (to_group->grp_end)));
11572 : 0 : OMP_CLAUSE_SET_MAP_KIND (to_group->grp_end,
11573 : : GOMP_MAP_ATTACH_DETACH);
11574 : :
11575 : 0 : location_t loc = OMP_CLAUSE_LOCATION (to_group->grp_end);
11576 : 0 : tree alloc
11577 : 0 : = build_omp_clause (loc, OMP_CLAUSE_MAP);
11578 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc, GOMP_MAP_ALLOC);
11579 : 0 : tree tmp = build_fold_addr_expr (OMP_CLAUSE_DECL
11580 : : (to_group->grp_end));
11581 : 0 : tree char_ptr_type = build_pointer_type (char_type_node);
11582 : 0 : OMP_CLAUSE_DECL (alloc)
11583 : 0 : = build2 (MEM_REF, char_type_node,
11584 : : tmp,
11585 : : build_int_cst (char_ptr_type, 0));
11586 : 0 : OMP_CLAUSE_SIZE (alloc) = TYPE_SIZE_UNIT (TREE_TYPE (tmp));
11587 : :
11588 : 0 : OMP_CLAUSE_CHAIN (alloc)
11589 : 0 : = OMP_CLAUSE_CHAIN (*to_group->grp_start);
11590 : 0 : OMP_CLAUSE_CHAIN (*to_group->grp_start) = alloc;
11591 : : }
11592 : : }
11593 : : }
11594 : : break;
11595 : :
11596 : : case GOMP_MAP_ATTACH_DETACH:
11597 : : case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
11598 : : {
11599 : 2052 : tree base_ptr, referenced_ptr_node = NULL_TREE;
11600 : :
11601 : 2052 : while (TREE_CODE (decl) == ARRAY_REF)
11602 : 0 : decl = TREE_OPERAND (decl, 0);
11603 : :
11604 : 2052 : if (TREE_CODE (decl) == INDIRECT_REF)
11605 : 13 : decl = TREE_OPERAND (decl, 0);
11606 : :
11607 : : /* Only component accesses. */
11608 : 2052 : if (DECL_P (decl))
11609 : 205 : continue;
11610 : :
11611 : : /* We want the pointer itself when checking if the base pointer is
11612 : : mapped elsewhere in the same directive -- if we have a
11613 : : reference to the pointer, don't use that. */
11614 : :
11615 : 1847 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
11616 : 1847 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
11617 : : {
11618 : 394 : referenced_ptr_node = OMP_CLAUSE_CHAIN (*grp->grp_start);
11619 : 394 : base_ptr = OMP_CLAUSE_DECL (referenced_ptr_node);
11620 : : }
11621 : : else
11622 : : base_ptr = decl;
11623 : :
11624 : 1669 : gomp_map_kind zlas_kind
11625 : 1847 : = (code == OACC_EXIT_DATA || code == OMP_TARGET_EXIT_DATA)
11626 : 1847 : ? GOMP_MAP_DETACH : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
11627 : :
11628 : 1847 : if (TREE_CODE (TREE_TYPE (base_ptr)) == POINTER_TYPE)
11629 : : {
11630 : : /* If we map the base TO, and we're doing an attachment, we can
11631 : : skip the TO mapping altogether and create an ALLOC mapping
11632 : : instead, since the attachment will overwrite the device
11633 : : pointer in that location immediately anyway. Otherwise,
11634 : : change our mapping to
11635 : : GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION in case the
11636 : : attachment target has not been copied to the device already
11637 : : by some earlier directive. */
11638 : :
11639 : 1423 : bool base_mapped_to = false;
11640 : :
11641 : 1423 : omp_mapping_group *base_group;
11642 : :
11643 : 1423 : if (omp_directive_maps_explicitly (grpmap, base_ptr,
11644 : : &base_group, false, true,
11645 : : false))
11646 : : {
11647 : 450 : if (referenced_ptr_node)
11648 : : {
11649 : 129 : base_mapped_to = true;
11650 : 129 : if ((OMP_CLAUSE_MAP_KIND (base_group->grp_end)
11651 : : == GOMP_MAP_ATTACH_DETACH)
11652 : 129 : && (OMP_CLAUSE_CHAIN (*base_group->grp_start)
11653 : : == base_group->grp_end))
11654 : : {
11655 : 258 : OMP_CLAUSE_CHAIN (*base_group->grp_start)
11656 : 129 : = OMP_CLAUSE_CHAIN (base_group->grp_end);
11657 : 129 : base_group->grp_end = *base_group->grp_start;
11658 : 129 : repair_chain = true;
11659 : : }
11660 : : }
11661 : : else
11662 : : {
11663 : 321 : base_group->deleted = true;
11664 : 321 : OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end) = 1;
11665 : : }
11666 : : }
11667 : :
11668 : : /* We're dealing with a reference to a pointer, and we are
11669 : : attaching both the reference and the pointer. We know the
11670 : : reference itself is on the target, because we are going to
11671 : : create an ALLOC node for it in accumulate_sibling_list. The
11672 : : pointer might be on the target already or it might not, but
11673 : : if it isn't then it's not an error, so use
11674 : : GOMP_MAP_ATTACH_ZLAS for it. */
11675 : 1423 : if (!base_mapped_to && referenced_ptr_node)
11676 : 56 : OMP_CLAUSE_SET_MAP_KIND (referenced_ptr_node, zlas_kind);
11677 : :
11678 : 1423 : omp_mapping_group *struct_group;
11679 : 1423 : tree desc;
11680 : 1423 : if ((desc = OMP_CLAUSE_CHAIN (*grp->grp_start))
11681 : 1423 : && omp_map_clause_descriptor_p (desc)
11682 : 2017 : && omp_mapped_by_containing_struct (grpmap, decl,
11683 : : &struct_group))
11684 : : /* If we have a pointer set but we're mapping (or unmapping)
11685 : : the whole of the containing struct, we can remove the
11686 : : pointer set mapping. */
11687 : 15 : OMP_CLAUSE_CHAIN (*grp->grp_start) = OMP_CLAUSE_CHAIN (desc);
11688 : : }
11689 : 424 : else if (TREE_CODE (TREE_TYPE (base_ptr)) == REFERENCE_TYPE
11690 : 424 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (base_ptr)))
11691 : : == ARRAY_TYPE)
11692 : 562 : && OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION
11693 : : (*grp->grp_start))
11694 : 92 : OMP_CLAUSE_SET_MAP_KIND (grp->grp_end, zlas_kind);
11695 : : }
11696 : : break;
11697 : :
11698 : : case GOMP_MAP_ATTACH:
11699 : : /* Ignore standalone attach here. */
11700 : : break;
11701 : :
11702 : 10787 : default:
11703 : 10787 : {
11704 : 10787 : omp_mapping_group *struct_group;
11705 : 10787 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
11706 : 10787 : && *grp->grp_start == grp_end)
11707 : : {
11708 : 94 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
11709 : : struct_group, grp);
11710 : : /* Remove the whole of this mapping -- redundant. */
11711 : 94 : grp->deleted = true;
11712 : : }
11713 : :
11714 : : tree base = decl;
11715 : 12377 : while ((base = omp_get_base_pointer (base)))
11716 : : {
11717 : 1590 : omp_mapping_group *base_group;
11718 : :
11719 : 1590 : if (omp_directive_maps_explicitly (grpmap, base, &base_group,
11720 : : true, true, false))
11721 : : {
11722 : 342 : tree grp_first = *base_group->grp_start;
11723 : 342 : OMP_CLAUSE_SET_MAP_KIND (grp_first, GOMP_MAP_ALLOC);
11724 : : }
11725 : : }
11726 : : }
11727 : : }
11728 : : }
11729 : :
11730 : 7649 : if (repair_chain)
11731 : : {
11732 : : /* Group start pointers may have become detached from the
11733 : : OMP_CLAUSE_CHAIN of previous groups if elements were removed from the
11734 : : end of those groups. Fix that now. */
11735 : : tree *new_next = NULL;
11736 : 716 : FOR_EACH_VEC_ELT (*groups, i, grp)
11737 : : {
11738 : 587 : if (new_next)
11739 : 458 : grp->grp_start = new_next;
11740 : :
11741 : 587 : new_next = &OMP_CLAUSE_CHAIN (grp->grp_end);
11742 : : }
11743 : : }
11744 : 7649 : }
11745 : :
11746 : : /* Similar to omp_resolve_clause_dependencies, but for OpenACC. The only
11747 : : clause dependencies we handle for now are struct element mappings and
11748 : : whole-struct mappings on the same directive, and duplicate clause
11749 : : detection. */
11750 : :
11751 : : void
11752 : 9421 : oacc_resolve_clause_dependencies (vec<omp_mapping_group> *groups,
11753 : : hash_map<tree_operand_hash_no_se,
11754 : : omp_mapping_group *> *grpmap)
11755 : : {
11756 : 9421 : int i;
11757 : 9421 : omp_mapping_group *grp;
11758 : 9421 : hash_set<tree_operand_hash> *seen_components = NULL;
11759 : 9421 : hash_set<tree_operand_hash> *shown_error = NULL;
11760 : :
11761 : 24510 : FOR_EACH_VEC_ELT (*groups, i, grp)
11762 : : {
11763 : 15089 : tree grp_end = grp->grp_end;
11764 : 15089 : tree decl = OMP_CLAUSE_DECL (grp_end);
11765 : :
11766 : 15089 : gcc_assert (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP);
11767 : :
11768 : 15089 : if (DECL_P (grp_end))
11769 : 14567 : continue;
11770 : :
11771 : 15089 : tree c = OMP_CLAUSE_DECL (*grp->grp_start);
11772 : 16714 : while (TREE_CODE (c) == ARRAY_REF)
11773 : 1625 : c = TREE_OPERAND (c, 0);
11774 : 15089 : if (TREE_CODE (c) != COMPONENT_REF)
11775 : 14567 : continue;
11776 : 522 : if (!seen_components)
11777 : 479 : seen_components = new hash_set<tree_operand_hash> ();
11778 : 522 : if (!shown_error)
11779 : 479 : shown_error = new hash_set<tree_operand_hash> ();
11780 : 522 : if (seen_components->contains (c)
11781 : 522 : && !shown_error->contains (c))
11782 : : {
11783 : 10 : error_at (OMP_CLAUSE_LOCATION (grp_end),
11784 : : "%qE appears more than once in map clauses",
11785 : 5 : OMP_CLAUSE_DECL (grp_end));
11786 : 5 : shown_error->add (c);
11787 : : }
11788 : : else
11789 : 517 : seen_components->add (c);
11790 : :
11791 : 522 : omp_mapping_group *struct_group;
11792 : 522 : if (omp_mapped_by_containing_struct (grpmap, decl, &struct_group)
11793 : 522 : && *grp->grp_start == grp_end)
11794 : : {
11795 : 78 : omp_check_mapping_compatibility (OMP_CLAUSE_LOCATION (grp_end),
11796 : : struct_group, grp);
11797 : : /* Remove the whole of this mapping -- redundant. */
11798 : 78 : grp->deleted = true;
11799 : : }
11800 : : }
11801 : :
11802 : 9421 : if (seen_components)
11803 : 479 : delete seen_components;
11804 : 9421 : if (shown_error)
11805 : 479 : delete shown_error;
11806 : 9421 : }
11807 : :
11808 : : /* Link node NEWNODE so it is pointed to by chain INSERT_AT. NEWNODE's chain
11809 : : is linked to the previous node pointed to by INSERT_AT. */
11810 : :
11811 : : static tree *
11812 : 1076 : omp_siblist_insert_node_after (tree newnode, tree *insert_at)
11813 : : {
11814 : 1076 : OMP_CLAUSE_CHAIN (newnode) = *insert_at;
11815 : 1076 : *insert_at = newnode;
11816 : 1076 : return &OMP_CLAUSE_CHAIN (newnode);
11817 : : }
11818 : :
11819 : : /* Move NODE (which is currently pointed to by the chain OLD_POS) so it is
11820 : : pointed to by chain MOVE_AFTER instead. */
11821 : :
11822 : : static void
11823 : 863 : omp_siblist_move_node_after (tree node, tree *old_pos, tree *move_after)
11824 : : {
11825 : 863 : gcc_assert (node == *old_pos);
11826 : 863 : *old_pos = OMP_CLAUSE_CHAIN (node);
11827 : 863 : OMP_CLAUSE_CHAIN (node) = *move_after;
11828 : 863 : *move_after = node;
11829 : 863 : }
11830 : :
11831 : : /* Move nodes from FIRST_PTR (pointed to by previous node's chain) to
11832 : : LAST_NODE to after MOVE_AFTER chain. Similar to below function, but no
11833 : : new nodes are prepended to the list before splicing into the new position.
11834 : : Return the position we should continue scanning the list at, or NULL to
11835 : : stay where we were. */
11836 : :
11837 : : static tree *
11838 : 254 : omp_siblist_move_nodes_after (tree *first_ptr, tree last_node,
11839 : : tree *move_after)
11840 : : {
11841 : 254 : if (first_ptr == move_after)
11842 : : return NULL;
11843 : :
11844 : 243 : tree tmp = *first_ptr;
11845 : 243 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
11846 : 243 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
11847 : 243 : *move_after = tmp;
11848 : :
11849 : 243 : return first_ptr;
11850 : : }
11851 : :
11852 : : /* Concatenate two lists described by [FIRST_NEW, LAST_NEW_TAIL] and
11853 : : [FIRST_PTR, LAST_NODE], and insert them in the OMP clause list after chain
11854 : : pointer MOVE_AFTER.
11855 : :
11856 : : The latter list was previously part of the OMP clause list, and the former
11857 : : (prepended) part is comprised of new nodes.
11858 : :
11859 : : We start with a list of nodes starting with a struct mapping node. We
11860 : : rearrange the list so that new nodes starting from FIRST_NEW and whose last
11861 : : node's chain is LAST_NEW_TAIL comes directly after MOVE_AFTER, followed by
11862 : : the group of mapping nodes we are currently processing (from the chain
11863 : : FIRST_PTR to LAST_NODE). The return value is the pointer to the next chain
11864 : : we should continue processing from, or NULL to stay where we were.
11865 : :
11866 : : The transformation (in the case where MOVE_AFTER and FIRST_PTR are
11867 : : different) is worked through below. Here we are processing LAST_NODE, and
11868 : : FIRST_PTR points at the preceding mapping clause:
11869 : :
11870 : : #. mapping node chain
11871 : : ---------------------------------------------------
11872 : : A. struct_node [->B]
11873 : : B. comp_1 [->C]
11874 : : C. comp_2 [->D (move_after)]
11875 : : D. map_to_3 [->E]
11876 : : E. attach_3 [->F (first_ptr)]
11877 : : F. map_to_4 [->G (continue_at)]
11878 : : G. attach_4 (last_node) [->H]
11879 : : H. ...
11880 : :
11881 : : *last_new_tail = *first_ptr;
11882 : :
11883 : : I. new_node (first_new) [->F (last_new_tail)]
11884 : :
11885 : : *first_ptr = OMP_CLAUSE_CHAIN (last_node)
11886 : :
11887 : : #. mapping node chain
11888 : : ----------------------------------------------------
11889 : : A. struct_node [->B]
11890 : : B. comp_1 [->C]
11891 : : C. comp_2 [->D (move_after)]
11892 : : D. map_to_3 [->E]
11893 : : E. attach_3 [->H (first_ptr)]
11894 : : F. map_to_4 [->G (continue_at)]
11895 : : G. attach_4 (last_node) [->H]
11896 : : H. ...
11897 : :
11898 : : I. new_node (first_new) [->F (last_new_tail)]
11899 : :
11900 : : OMP_CLAUSE_CHAIN (last_node) = *move_after;
11901 : :
11902 : : #. mapping node chain
11903 : : ---------------------------------------------------
11904 : : A. struct_node [->B]
11905 : : B. comp_1 [->C]
11906 : : C. comp_2 [->D (move_after)]
11907 : : D. map_to_3 [->E]
11908 : : E. attach_3 [->H (continue_at)]
11909 : : F. map_to_4 [->G]
11910 : : G. attach_4 (last_node) [->D]
11911 : : H. ...
11912 : :
11913 : : I. new_node (first_new) [->F (last_new_tail)]
11914 : :
11915 : : *move_after = first_new;
11916 : :
11917 : : #. mapping node chain
11918 : : ---------------------------------------------------
11919 : : A. struct_node [->B]
11920 : : B. comp_1 [->C]
11921 : : C. comp_2 [->I (move_after)]
11922 : : D. map_to_3 [->E]
11923 : : E. attach_3 [->H (continue_at)]
11924 : : F. map_to_4 [->G]
11925 : : G. attach_4 (last_node) [->D]
11926 : : H. ...
11927 : : I. new_node (first_new) [->F (last_new_tail)]
11928 : :
11929 : : or, in order:
11930 : :
11931 : : #. mapping node chain
11932 : : ---------------------------------------------------
11933 : : A. struct_node [->B]
11934 : : B. comp_1 [->C]
11935 : : C. comp_2 [->I (move_after)]
11936 : : I. new_node (first_new) [->F (last_new_tail)]
11937 : : F. map_to_4 [->G]
11938 : : G. attach_4 (last_node) [->D]
11939 : : D. map_to_3 [->E]
11940 : : E. attach_3 [->H (continue_at)]
11941 : : H. ...
11942 : : */
11943 : :
11944 : : static tree *
11945 : 71 : omp_siblist_move_concat_nodes_after (tree first_new, tree *last_new_tail,
11946 : : tree *first_ptr, tree last_node,
11947 : : tree *move_after)
11948 : : {
11949 : 71 : tree *continue_at = NULL;
11950 : 71 : *last_new_tail = *first_ptr;
11951 : 71 : if (first_ptr == move_after)
11952 : 12 : *move_after = first_new;
11953 : : else
11954 : : {
11955 : 59 : *first_ptr = OMP_CLAUSE_CHAIN (last_node);
11956 : 59 : continue_at = first_ptr;
11957 : 59 : OMP_CLAUSE_CHAIN (last_node) = *move_after;
11958 : 59 : *move_after = first_new;
11959 : : }
11960 : 71 : return continue_at;
11961 : : }
11962 : :
11963 : : static omp_addr_token *
11964 : 10025 : omp_first_chained_access_token (vec<omp_addr_token *> &addr_tokens)
11965 : : {
11966 : 10025 : using namespace omp_addr_tokenizer;
11967 : 10025 : int idx = addr_tokens.length () - 1;
11968 : 10025 : gcc_assert (idx >= 0);
11969 : 10025 : if (addr_tokens[idx]->type != ACCESS_METHOD)
11970 : : return addr_tokens[idx];
11971 : 10042 : while (idx > 0 && addr_tokens[idx - 1]->type == ACCESS_METHOD)
11972 : : idx--;
11973 : 10025 : return addr_tokens[idx];
11974 : : }
11975 : :
11976 : : /* Mapping struct members causes an additional set of nodes to be created,
11977 : : starting with GOMP_MAP_STRUCT followed by a number of mappings equal to the
11978 : : number of members being mapped, in order of ascending position (address or
11979 : : bitwise).
11980 : :
11981 : : We scan through the list of mapping clauses, calling this function for each
11982 : : struct member mapping we find, and build up the list of mappings after the
11983 : : initial GOMP_MAP_STRUCT node. For pointer members, these will be
11984 : : newly-created ALLOC nodes. For non-pointer members, the existing mapping is
11985 : : moved into place in the sorted list.
11986 : :
11987 : : struct {
11988 : : int *a;
11989 : : int *b;
11990 : : int c;
11991 : : int *d;
11992 : : };
11993 : :
11994 : : #pragma (acc|omp directive) copy(struct.a[0:n], struct.b[0:n], struct.c,
11995 : : struct.d[0:n])
11996 : :
11997 : : GOMP_MAP_STRUCT (4)
11998 : : [GOMP_MAP_FIRSTPRIVATE_REFERENCE -- for refs to structs]
11999 : : GOMP_MAP_ALLOC (struct.a)
12000 : : GOMP_MAP_ALLOC (struct.b)
12001 : : GOMP_MAP_TO (struct.c)
12002 : : GOMP_MAP_ALLOC (struct.d)
12003 : : ...
12004 : :
12005 : : In the case where we are mapping references to pointers, or in Fortran if
12006 : : we are mapping an array with a descriptor, additional nodes may be created
12007 : : after the struct node list also.
12008 : :
12009 : : The return code is either a pointer to the next node to process (if the
12010 : : list has been rearranged), else NULL to continue with the next node in the
12011 : : original list. */
12012 : :
12013 : : static tree *
12014 : 4050 : omp_accumulate_sibling_list (enum omp_region_type region_type,
12015 : : enum tree_code code,
12016 : : hash_map<tree_operand_hash, tree>
12017 : : *&struct_map_to_clause,
12018 : : hash_map<tree_operand_hash_no_se,
12019 : : omp_mapping_group *> *group_map,
12020 : : tree *grp_start_p, tree grp_end,
12021 : : vec<omp_addr_token *> &addr_tokens, tree **inner,
12022 : : bool *fragile_p, bool reprocessing_struct,
12023 : : tree **added_tail)
12024 : : {
12025 : 4050 : using namespace omp_addr_tokenizer;
12026 : 4050 : poly_offset_int coffset;
12027 : 4050 : poly_int64 cbitpos;
12028 : 4050 : tree ocd = OMP_CLAUSE_DECL (grp_end);
12029 : 4050 : bool openmp = !(region_type & ORT_ACC);
12030 : 4050 : bool target = (region_type & ORT_TARGET) != 0;
12031 : 4050 : tree *continue_at = NULL;
12032 : :
12033 : 4363 : while (TREE_CODE (ocd) == ARRAY_REF)
12034 : 313 : ocd = TREE_OPERAND (ocd, 0);
12035 : :
12036 : 4050 : if (*fragile_p)
12037 : : {
12038 : 156 : omp_mapping_group *to_group
12039 : 156 : = omp_get_nonfirstprivate_group (group_map, ocd, true);
12040 : :
12041 : 156 : if (to_group)
12042 : : return NULL;
12043 : : }
12044 : :
12045 : 3962 : omp_addr_token *last_token = omp_first_chained_access_token (addr_tokens);
12046 : 3962 : if (last_token->type == ACCESS_METHOD)
12047 : : {
12048 : 3962 : switch (last_token->u.access_kind)
12049 : : {
12050 : 735 : case ACCESS_REF:
12051 : 735 : case ACCESS_REF_TO_POINTER:
12052 : 735 : case ACCESS_REF_TO_POINTER_OFFSET:
12053 : 735 : case ACCESS_INDEXED_REF_TO_ARRAY:
12054 : : /* We may see either a bare reference or a dereferenced
12055 : : "convert_from_reference"-like one here. Handle either way. */
12056 : 735 : if (TREE_CODE (ocd) == INDIRECT_REF)
12057 : 64 : ocd = TREE_OPERAND (ocd, 0);
12058 : 735 : gcc_assert (TREE_CODE (TREE_TYPE (ocd)) == REFERENCE_TYPE);
12059 : : break;
12060 : :
12061 : : default:
12062 : : ;
12063 : : }
12064 : : }
12065 : :
12066 : 3962 : bool variable_offset;
12067 : 3962 : tree base
12068 : 3962 : = extract_base_bit_offset (ocd, &cbitpos, &coffset, &variable_offset);
12069 : :
12070 : 3962 : int base_token;
12071 : 19982 : for (base_token = addr_tokens.length () - 1; base_token >= 0; base_token--)
12072 : : {
12073 : 16020 : if (addr_tokens[base_token]->type == ARRAY_BASE
12074 : 16020 : || addr_tokens[base_token]->type == STRUCTURE_BASE)
12075 : : break;
12076 : : }
12077 : :
12078 : : /* The two expressions in the assertion below aren't quite the same: if we
12079 : : have 'struct_base_decl access_indexed_array' for something like
12080 : : "myvar[2].x" then base will be "myvar" and addr_tokens[base_token]->expr
12081 : : will be "myvar[2]" -- the actual base of the structure.
12082 : : The former interpretation leads to a strange situation where we get
12083 : : struct(myvar) alloc(myvar[2].ptr1)
12084 : : That is, the array of structures is kind of treated as one big structure
12085 : : for the purposes of gathering sibling lists, etc. */
12086 : : /* gcc_assert (base == addr_tokens[base_token]->expr); */
12087 : :
12088 : 3962 : bool attach_detach = ((OMP_CLAUSE_MAP_KIND (grp_end)
12089 : : == GOMP_MAP_ATTACH_DETACH)
12090 : 3962 : || (OMP_CLAUSE_MAP_KIND (grp_end)
12091 : 3962 : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
12092 : 3962 : bool has_descriptor = false;
12093 : 3962 : if (OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12094 : : {
12095 : 2421 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
12096 : 2421 : if (grp_mid && omp_map_clause_descriptor_p (grp_mid))
12097 : : has_descriptor = true;
12098 : : }
12099 : :
12100 : 3962 : if (!struct_map_to_clause || struct_map_to_clause->get (base) == NULL)
12101 : : {
12102 : 2705 : enum gomp_map_kind str_kind = GOMP_MAP_STRUCT;
12103 : :
12104 : 2705 : if (struct_map_to_clause == NULL)
12105 : 2277 : struct_map_to_clause = new hash_map<tree_operand_hash, tree>;
12106 : :
12107 : 2705 : if (variable_offset)
12108 : 108 : str_kind = GOMP_MAP_STRUCT_UNORD;
12109 : :
12110 : 2705 : tree l = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end), OMP_CLAUSE_MAP);
12111 : :
12112 : 2705 : OMP_CLAUSE_SET_MAP_KIND (l, str_kind);
12113 : 2705 : OMP_CLAUSE_DECL (l) = unshare_expr (base);
12114 : 2705 : OMP_CLAUSE_SIZE (l) = size_int (1);
12115 : :
12116 : 2705 : struct_map_to_clause->put (base, l);
12117 : :
12118 : : /* On first iterating through the clause list, we insert the struct node
12119 : : just before the component access node that triggers the initial
12120 : : omp_accumulate_sibling_list call for a particular sibling list (and
12121 : : it then forms the first entry in that list). When reprocessing
12122 : : struct bases that are themselves component accesses, we insert the
12123 : : struct node on an off-side list to avoid inserting the new
12124 : : GOMP_MAP_STRUCT into the middle of the old one. */
12125 : 2705 : tree *insert_node_pos = reprocessing_struct ? *added_tail : grp_start_p;
12126 : :
12127 : 2705 : if (has_descriptor)
12128 : : {
12129 : 474 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12130 : 474 : if (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA)
12131 : 97 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12132 : 474 : tree sc = *insert_node_pos;
12133 : 474 : OMP_CLAUSE_CHAIN (l) = desc;
12134 : 474 : OMP_CLAUSE_CHAIN (*grp_start_p) = OMP_CLAUSE_CHAIN (desc);
12135 : 474 : OMP_CLAUSE_CHAIN (desc) = sc;
12136 : 474 : *insert_node_pos = l;
12137 : : }
12138 : 2231 : else if (attach_detach)
12139 : : {
12140 : 1385 : tree extra_node;
12141 : 1385 : tree alloc_node
12142 : 1385 : = build_omp_struct_comp_nodes (code, *grp_start_p, grp_end,
12143 : : &extra_node);
12144 : 1385 : tree *tail;
12145 : 1385 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12146 : :
12147 : 1385 : if (extra_node)
12148 : : {
12149 : 0 : OMP_CLAUSE_CHAIN (extra_node) = *insert_node_pos;
12150 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12151 : 0 : tail = &OMP_CLAUSE_CHAIN (extra_node);
12152 : : }
12153 : : else
12154 : : {
12155 : 1385 : OMP_CLAUSE_CHAIN (alloc_node) = *insert_node_pos;
12156 : 1385 : tail = &OMP_CLAUSE_CHAIN (alloc_node);
12157 : : }
12158 : :
12159 : : /* For OpenMP semantics, we don't want to implicitly allocate
12160 : : space for the pointer here for non-compute regions (e.g. "enter
12161 : : data"). A FRAGILE_P node is only being created so that
12162 : : omp-low.cc is able to rewrite the struct properly.
12163 : : For references (to pointers), we want to actually allocate the
12164 : : space for the reference itself in the sorted list following the
12165 : : struct node.
12166 : : For pointers, we want to allocate space if we had an explicit
12167 : : mapping of the attachment point, but not otherwise. */
12168 : 1385 : if (*fragile_p
12169 : 1385 : || (openmp
12170 : : && !target
12171 : : && attach_detach
12172 : 222 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12173 : 76 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12174 : : {
12175 : 121 : if (!lang_GNU_Fortran ())
12176 : : /* In Fortran, pointers are dereferenced automatically, but may
12177 : : be unassociated. So we still want to allocate space for the
12178 : : pointer (as the base for an attach operation that should be
12179 : : present in the same directive's clause list also). */
12180 : 103 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12181 : 121 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12182 : : }
12183 : :
12184 : 1385 : *insert_node_pos = l;
12185 : :
12186 : 1385 : if (reprocessing_struct)
12187 : : {
12188 : : /* When reprocessing a struct node group used as the base of a
12189 : : subcomponent access, if we have a reference-to-pointer base,
12190 : : we will see:
12191 : : struct(**ptr) attach(*ptr)
12192 : : whereas for a non-reprocess-struct group, we see, e.g.:
12193 : : tofrom(**ptr) attach(*ptr) attach(ptr)
12194 : : and we create the "alloc" for the second "attach", i.e.
12195 : : for the reference itself. When reprocessing a struct group we
12196 : : thus change the pointer attachment into a reference attachment
12197 : : by stripping the indirection. (The attachment of the
12198 : : referenced pointer must happen elsewhere, either on the same
12199 : : directive, or otherwise.) */
12200 : 180 : tree adecl = OMP_CLAUSE_DECL (alloc_node);
12201 : :
12202 : 180 : if ((TREE_CODE (adecl) == INDIRECT_REF
12203 : 148 : || (TREE_CODE (adecl) == MEM_REF
12204 : 0 : && integer_zerop (TREE_OPERAND (adecl, 1))))
12205 : 32 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (adecl, 0)))
12206 : : == REFERENCE_TYPE)
12207 : 212 : && (TREE_CODE (TREE_TYPE (TREE_TYPE
12208 : : (TREE_OPERAND (adecl, 0)))) == POINTER_TYPE))
12209 : 32 : OMP_CLAUSE_DECL (alloc_node) = TREE_OPERAND (adecl, 0);
12210 : :
12211 : 180 : *added_tail = tail;
12212 : : }
12213 : : }
12214 : : else
12215 : : {
12216 : 846 : gcc_assert (*grp_start_p == grp_end);
12217 : 846 : if (reprocessing_struct)
12218 : : {
12219 : : /* If we don't have an attach/detach node, this is a
12220 : : "target data" directive or similar, not an offload region.
12221 : : Synthesize an "alloc" node using just the initiating
12222 : : GOMP_MAP_STRUCT decl. */
12223 : 16 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12224 : 32 : || code == OACC_EXIT_DATA)
12225 : 32 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12226 : 32 : tree alloc_node
12227 : 32 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12228 : : OMP_CLAUSE_MAP);
12229 : 32 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12230 : 32 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12231 : 64 : OMP_CLAUSE_SIZE (alloc_node)
12232 : 32 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12233 : :
12234 : 32 : OMP_CLAUSE_CHAIN (alloc_node) = OMP_CLAUSE_CHAIN (l);
12235 : 32 : OMP_CLAUSE_CHAIN (l) = alloc_node;
12236 : 32 : *insert_node_pos = l;
12237 : 32 : *added_tail = &OMP_CLAUSE_CHAIN (alloc_node);
12238 : : }
12239 : : else
12240 : 814 : grp_start_p = omp_siblist_insert_node_after (l, insert_node_pos);
12241 : : }
12242 : :
12243 : 2705 : unsigned last_access = base_token + 1;
12244 : :
12245 : 2705 : while (last_access + 1 < addr_tokens.length ()
12246 : 2873 : && addr_tokens[last_access + 1]->type == ACCESS_METHOD)
12247 : : last_access++;
12248 : :
12249 : 2705 : if ((region_type & ORT_TARGET)
12250 : 2705 : && addr_tokens[base_token + 1]->type == ACCESS_METHOD)
12251 : : {
12252 : 1652 : bool base_ref = false;
12253 : 1652 : access_method_kinds access_kind
12254 : 1652 : = addr_tokens[last_access]->u.access_kind;
12255 : :
12256 : 1652 : switch (access_kind)
12257 : : {
12258 : : case ACCESS_DIRECT:
12259 : : case ACCESS_INDEXED_ARRAY:
12260 : 1004 : return NULL;
12261 : :
12262 : 403 : case ACCESS_REF:
12263 : 403 : case ACCESS_REF_TO_POINTER:
12264 : 403 : case ACCESS_REF_TO_POINTER_OFFSET:
12265 : 403 : case ACCESS_INDEXED_REF_TO_ARRAY:
12266 : 403 : base_ref = true;
12267 : 403 : break;
12268 : :
12269 : 866 : default:
12270 : 866 : ;
12271 : : }
12272 : 866 : tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12273 : : OMP_CLAUSE_MAP);
12274 : 866 : enum gomp_map_kind mkind;
12275 : 866 : omp_mapping_group *decl_group;
12276 : 866 : tree use_base;
12277 : 866 : switch (access_kind)
12278 : : {
12279 : 463 : case ACCESS_POINTER:
12280 : 463 : case ACCESS_POINTER_OFFSET:
12281 : 463 : use_base = addr_tokens[last_access]->expr;
12282 : 463 : break;
12283 : 198 : case ACCESS_REF_TO_POINTER:
12284 : 198 : case ACCESS_REF_TO_POINTER_OFFSET:
12285 : 198 : use_base
12286 : 198 : = build_fold_indirect_ref (addr_tokens[last_access]->expr);
12287 : 198 : break;
12288 : 205 : default:
12289 : 205 : use_base = addr_tokens[base_token]->expr;
12290 : : }
12291 : 866 : bool mapped_to_p
12292 : 866 : = omp_directive_maps_explicitly (group_map, use_base, &decl_group,
12293 : : true, false, true);
12294 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12295 : 866 : && DECL_P (addr_tokens[last_access]->expr)
12296 : 1372 : && !mapped_to_p)
12297 : 444 : mkind = base_ref ? GOMP_MAP_FIRSTPRIVATE_REFERENCE
12298 : : : GOMP_MAP_FIRSTPRIVATE_POINTER;
12299 : : else
12300 : : mkind = GOMP_MAP_ATTACH_DETACH;
12301 : :
12302 : 866 : OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
12303 : : /* If we have a reference to pointer base, we want to attach the
12304 : : pointer here, not the reference. The reference attachment happens
12305 : : elsewhere. */
12306 : 866 : bool ref_to_ptr
12307 : 866 : = (access_kind == ACCESS_REF_TO_POINTER
12308 : 866 : || access_kind == ACCESS_REF_TO_POINTER_OFFSET);
12309 : 866 : tree sdecl = addr_tokens[last_access]->expr;
12310 : 866 : tree sdecl_ptr = ref_to_ptr ? build_fold_indirect_ref (sdecl)
12311 : : : sdecl;
12312 : : /* For the FIRSTPRIVATE_REFERENCE after the struct node, we
12313 : : want to use the reference itself for the decl, but we
12314 : : still want to use the pointer to calculate the bias. */
12315 : 866 : OMP_CLAUSE_DECL (c2) = (mkind == GOMP_MAP_ATTACH_DETACH)
12316 : 866 : ? sdecl_ptr : sdecl;
12317 : 866 : sdecl = sdecl_ptr;
12318 : 866 : tree baddr = build_fold_addr_expr (base);
12319 : 866 : baddr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12320 : : ptrdiff_type_node, baddr);
12321 : 866 : tree decladdr = fold_convert_loc (OMP_CLAUSE_LOCATION (grp_end),
12322 : : ptrdiff_type_node, sdecl);
12323 : 866 : OMP_CLAUSE_SIZE (c2)
12324 : 866 : = fold_build2_loc (OMP_CLAUSE_LOCATION (grp_end), MINUS_EXPR,
12325 : : ptrdiff_type_node, baddr, decladdr);
12326 : : /* Insert after struct node. */
12327 : 866 : OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
12328 : 866 : OMP_CLAUSE_CHAIN (l) = c2;
12329 : :
12330 : 866 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12331 : 866 : && (addr_tokens[base_token]->u.structure_base_kind
12332 : : == BASE_COMPONENT_EXPR)
12333 : 286 : && mkind == GOMP_MAP_ATTACH_DETACH
12334 : 1152 : && addr_tokens[last_access]->u.access_kind != ACCESS_REF)
12335 : : {
12336 : 218 : *inner = insert_node_pos;
12337 : 218 : if (openmp)
12338 : 166 : *fragile_p = true;
12339 : 218 : return NULL;
12340 : : }
12341 : : }
12342 : :
12343 : 1701 : if (addr_tokens[base_token]->type == STRUCTURE_BASE
12344 : 1701 : && (addr_tokens[base_token]->u.structure_base_kind
12345 : : == BASE_COMPONENT_EXPR)
12346 : 2016 : && addr_tokens[last_access]->u.access_kind == ACCESS_REF)
12347 : 100 : *inner = insert_node_pos;
12348 : :
12349 : 1701 : return NULL;
12350 : : }
12351 : 1257 : else if (struct_map_to_clause)
12352 : : {
12353 : 1257 : tree *osc = struct_map_to_clause->get (base);
12354 : 1257 : tree *sc = NULL, *scp = NULL;
12355 : 1257 : bool unordered = false;
12356 : :
12357 : 1257 : if (osc && OMP_CLAUSE_MAP_KIND (*osc) == GOMP_MAP_STRUCT_UNORD)
12358 : : unordered = true;
12359 : :
12360 : 1257 : unsigned HOST_WIDE_INT i, elems = tree_to_uhwi (OMP_CLAUSE_SIZE (*osc));
12361 : 1257 : sc = &OMP_CLAUSE_CHAIN (*osc);
12362 : : /* The struct mapping might be immediately followed by a
12363 : : FIRSTPRIVATE_POINTER, FIRSTPRIVATE_REFERENCE or an ATTACH_DETACH --
12364 : : if it's an indirect access or a reference, or if the structure base
12365 : : is not a decl. The FIRSTPRIVATE_* nodes are removed in omp-low.cc
12366 : : after they have been processed there, and ATTACH_DETACH nodes are
12367 : : recomputed and moved out of the GOMP_MAP_STRUCT construct once
12368 : : sibling list building is complete. */
12369 : 1257 : if (OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_POINTER
12370 : 1212 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_FIRSTPRIVATE_REFERENCE
12371 : 2424 : || OMP_CLAUSE_MAP_KIND (*sc) == GOMP_MAP_ATTACH_DETACH)
12372 : 201 : sc = &OMP_CLAUSE_CHAIN (*sc);
12373 : 3453 : for (i = 0; i < elems; i++, sc = &OMP_CLAUSE_CHAIN (*sc))
12374 : 2580 : if (attach_detach && sc == grp_start_p)
12375 : : break;
12376 : 2580 : else if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
12377 : 182 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != INDIRECT_REF
12378 : 2762 : && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
12379 : : break;
12380 : : else
12381 : : {
12382 : 2580 : tree sc_decl = OMP_CLAUSE_DECL (*sc);
12383 : 2580 : poly_offset_int offset;
12384 : 2580 : poly_int64 bitpos;
12385 : :
12386 : 2580 : if (TREE_CODE (sc_decl) == ARRAY_REF)
12387 : : {
12388 : 366 : while (TREE_CODE (sc_decl) == ARRAY_REF)
12389 : 184 : sc_decl = TREE_OPERAND (sc_decl, 0);
12390 : 182 : if (TREE_CODE (sc_decl) != COMPONENT_REF
12391 : 182 : || TREE_CODE (TREE_TYPE (sc_decl)) != ARRAY_TYPE)
12392 : : break;
12393 : : }
12394 : 2398 : else if (INDIRECT_REF_P (sc_decl)
12395 : 0 : && TREE_CODE (TREE_OPERAND (sc_decl, 0)) == COMPONENT_REF
12396 : 2398 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (sc_decl, 0)))
12397 : : == REFERENCE_TYPE))
12398 : 0 : sc_decl = TREE_OPERAND (sc_decl, 0);
12399 : :
12400 : 2580 : bool variable_offset2;
12401 : 2580 : tree base2 = extract_base_bit_offset (sc_decl, &bitpos, &offset,
12402 : : &variable_offset2);
12403 : 2580 : if (!base2 || !operand_equal_p (base2, base, 0))
12404 : : break;
12405 : 2580 : if (scp)
12406 : 220 : continue;
12407 : 2360 : if (variable_offset2)
12408 : : {
12409 : 48 : OMP_CLAUSE_SET_MAP_KIND (*osc, GOMP_MAP_STRUCT_UNORD);
12410 : 48 : unordered = true;
12411 : 48 : break;
12412 : : }
12413 : 2312 : else if ((region_type & ORT_ACC) != 0)
12414 : : {
12415 : : /* For OpenACC, allow (ignore) duplicate struct accesses in
12416 : : the middle of a mapping clause, e.g. "mystruct->foo" in:
12417 : : copy(mystruct->foo->bar) copy(mystruct->foo->qux). */
12418 : 223 : if (reprocessing_struct
12419 : 8 : && known_eq (coffset, offset)
12420 : 223 : && known_eq (cbitpos, bitpos))
12421 : 21 : return NULL;
12422 : : }
12423 : 2089 : else if (known_eq (coffset, offset)
12424 : 2089 : && known_eq (cbitpos, bitpos))
12425 : : {
12426 : : /* Having two struct members at the same offset doesn't work,
12427 : : so make sure we don't. (We're allowed to ignore this.
12428 : : Should we report the error?) */
12429 : : /*error_at (OMP_CLAUSE_LOCATION (grp_end),
12430 : : "duplicate struct member %qE in map clauses",
12431 : : OMP_CLAUSE_DECL (grp_end));*/
12432 : : return NULL;
12433 : : }
12434 : 2291 : if (maybe_lt (coffset, offset)
12435 : 4001 : || (known_eq (coffset, offset)
12436 : 2 : && maybe_lt (cbitpos, bitpos)))
12437 : : {
12438 : 581 : if (attach_detach)
12439 : : scp = sc;
12440 : : else
12441 : : break;
12442 : : }
12443 : : }
12444 : :
12445 : : /* If this is an unordered struct, just insert the new element at the
12446 : : end of the list. */
12447 : 1236 : if (unordered)
12448 : : {
12449 : 102 : for (; i < elems; i++)
12450 : 54 : sc = &OMP_CLAUSE_CHAIN (*sc);
12451 : : scp = NULL;
12452 : : }
12453 : :
12454 : 1236 : OMP_CLAUSE_SIZE (*osc)
12455 : 1236 : = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc), size_one_node);
12456 : :
12457 : 1236 : if (reprocessing_struct)
12458 : : {
12459 : : /* If we're reprocessing a struct node, we don't want to do most of
12460 : : the list manipulation below. We only need to handle the (pointer
12461 : : or reference) attach/detach case. */
12462 : 8 : tree extra_node, alloc_node;
12463 : 8 : if (has_descriptor)
12464 : 0 : gcc_unreachable ();
12465 : 8 : else if (attach_detach)
12466 : 8 : alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12467 : : grp_end, &extra_node);
12468 : : else
12469 : : {
12470 : : /* If we don't have an attach/detach node, this is a
12471 : : "target data" directive or similar, not an offload region.
12472 : : Synthesize an "alloc" node using just the initiating
12473 : : GOMP_MAP_STRUCT decl. */
12474 : 0 : gomp_map_kind k = (code == OMP_TARGET_EXIT_DATA
12475 : 0 : || code == OACC_EXIT_DATA)
12476 : 0 : ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
12477 : 0 : alloc_node
12478 : 0 : = build_omp_clause (OMP_CLAUSE_LOCATION (grp_end),
12479 : : OMP_CLAUSE_MAP);
12480 : 0 : OMP_CLAUSE_SET_MAP_KIND (alloc_node, k);
12481 : 0 : OMP_CLAUSE_DECL (alloc_node) = unshare_expr (last_token->expr);
12482 : 0 : OMP_CLAUSE_SIZE (alloc_node)
12483 : 0 : = TYPE_SIZE_UNIT (TREE_TYPE (OMP_CLAUSE_DECL (alloc_node)));
12484 : : }
12485 : :
12486 : 8 : if (scp)
12487 : 0 : omp_siblist_insert_node_after (alloc_node, scp);
12488 : : else
12489 : : {
12490 : 8 : tree *new_end = omp_siblist_insert_node_after (alloc_node, sc);
12491 : 8 : if (sc == *added_tail)
12492 : 8 : *added_tail = new_end;
12493 : : }
12494 : :
12495 : 8 : return NULL;
12496 : : }
12497 : :
12498 : 1228 : if (has_descriptor)
12499 : : {
12500 : 339 : tree desc = OMP_CLAUSE_CHAIN (*grp_start_p);
12501 : 339 : if (code == OMP_TARGET_EXIT_DATA
12502 : 339 : || code == OACC_EXIT_DATA)
12503 : 19 : OMP_CLAUSE_SET_MAP_KIND (desc, GOMP_MAP_RELEASE);
12504 : 678 : omp_siblist_move_node_after (desc,
12505 : 339 : &OMP_CLAUSE_CHAIN (*grp_start_p),
12506 : : scp ? scp : sc);
12507 : : }
12508 : 889 : else if (attach_detach)
12509 : : {
12510 : 325 : tree cl = NULL_TREE, extra_node;
12511 : 325 : tree alloc_node = build_omp_struct_comp_nodes (code, *grp_start_p,
12512 : : grp_end, &extra_node);
12513 : 325 : tree *tail_chain = NULL;
12514 : :
12515 : 325 : if (*fragile_p
12516 : 325 : || (openmp
12517 : : && !target
12518 : : && attach_detach
12519 : 62 : && TREE_CODE (TREE_TYPE (ocd)) == POINTER_TYPE
12520 : 23 : && !OMP_CLAUSE_ATTACHMENT_MAPPING_ERASED (grp_end)))
12521 : : {
12522 : 6 : if (!lang_GNU_Fortran ())
12523 : 6 : OMP_CLAUSE_SIZE (alloc_node) = size_zero_node;
12524 : 6 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (alloc_node) = 1;
12525 : : }
12526 : :
12527 : : /* Here, we have:
12528 : :
12529 : : grp_end : the last (or only) node in this group.
12530 : : grp_start_p : pointer to the first node in a pointer mapping group
12531 : : up to and including GRP_END.
12532 : : sc : pointer to the chain for the end of the struct component
12533 : : list.
12534 : : scp : pointer to the chain for the sorted position at which we
12535 : : should insert in the middle of the struct component list
12536 : : (else NULL to insert at end).
12537 : : alloc_node : the "alloc" node for the structure (pointer-type)
12538 : : component. We insert at SCP (if present), else SC
12539 : : (the end of the struct component list).
12540 : : extra_node : a newly-synthesized node for an additional indirect
12541 : : pointer mapping or a Fortran pointer set, if needed.
12542 : : cl : first node to prepend before grp_start_p.
12543 : : tail_chain : pointer to chain of last prepended node.
12544 : :
12545 : : The general idea is we move the nodes for this struct mapping
12546 : : together: the alloc node goes into the sorted list directly after
12547 : : the struct mapping, and any extra nodes (together with the nodes
12548 : : mapping arrays pointed to by struct components) get moved after
12549 : : that list. When SCP is NULL, we insert the nodes at SC, i.e. at
12550 : : the end of the struct component mapping list. It's important that
12551 : : the alloc_node comes first in that case because it's part of the
12552 : : sorted component mapping list (but subsequent nodes are not!). */
12553 : :
12554 : 325 : if (scp)
12555 : 254 : omp_siblist_insert_node_after (alloc_node, scp);
12556 : :
12557 : : /* Make [cl,tail_chain] a list of the alloc node (if we haven't
12558 : : already inserted it) and the extra_node (if it is present). The
12559 : : list can be empty if we added alloc_node above and there is no
12560 : : extra node. */
12561 : 254 : if (scp && extra_node)
12562 : : {
12563 : 0 : cl = extra_node;
12564 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
12565 : : }
12566 : 325 : else if (extra_node)
12567 : : {
12568 : 0 : OMP_CLAUSE_CHAIN (alloc_node) = extra_node;
12569 : 0 : cl = alloc_node;
12570 : 0 : tail_chain = &OMP_CLAUSE_CHAIN (extra_node);
12571 : : }
12572 : 325 : else if (!scp)
12573 : : {
12574 : 71 : cl = alloc_node;
12575 : 71 : tail_chain = &OMP_CLAUSE_CHAIN (alloc_node);
12576 : : }
12577 : :
12578 : 325 : continue_at
12579 : 71 : = cl ? omp_siblist_move_concat_nodes_after (cl, tail_chain,
12580 : : grp_start_p, grp_end,
12581 : : sc)
12582 : 254 : : omp_siblist_move_nodes_after (grp_start_p, grp_end, sc);
12583 : : }
12584 : 564 : else if (*sc != grp_end)
12585 : : {
12586 : 524 : gcc_assert (*grp_start_p == grp_end);
12587 : :
12588 : : /* We are moving the current node back to a previous struct node:
12589 : : the node that used to point to the current node will now point to
12590 : : the next node. */
12591 : 524 : continue_at = grp_start_p;
12592 : : /* In the non-pointer case, the mapping clause itself is moved into
12593 : : the correct position in the struct component list, which in this
12594 : : case is just SC. */
12595 : 524 : omp_siblist_move_node_after (*grp_start_p, grp_start_p, sc);
12596 : : }
12597 : : }
12598 : : return continue_at;
12599 : : }
12600 : :
12601 : : /* Scan through GROUPS, and create sorted structure sibling lists without
12602 : : gimplifying. */
12603 : :
12604 : : static bool
12605 : 17070 : omp_build_struct_sibling_lists (enum tree_code code,
12606 : : enum omp_region_type region_type,
12607 : : vec<omp_mapping_group> *groups,
12608 : : hash_map<tree_operand_hash_no_se,
12609 : : omp_mapping_group *> **grpmap,
12610 : : tree *list_p)
12611 : : {
12612 : 17070 : using namespace omp_addr_tokenizer;
12613 : 17070 : unsigned i;
12614 : 17070 : omp_mapping_group *grp;
12615 : 17070 : hash_map<tree_operand_hash, tree> *struct_map_to_clause = NULL;
12616 : 17070 : bool success = true;
12617 : 17070 : tree *new_next = NULL;
12618 : 34140 : tree *tail = &OMP_CLAUSE_CHAIN ((*groups)[groups->length () - 1].grp_end);
12619 : 17070 : tree added_nodes = NULL_TREE;
12620 : 17070 : tree *added_tail = &added_nodes;
12621 : 17070 : auto_vec<omp_mapping_group> pre_hwm_groups;
12622 : :
12623 : 63664 : FOR_EACH_VEC_ELT (*groups, i, grp)
12624 : : {
12625 : 29524 : tree c = grp->grp_end;
12626 : 29524 : tree decl = OMP_CLAUSE_DECL (c);
12627 : 29524 : tree grp_end = grp->grp_end;
12628 : 29524 : auto_vec<omp_addr_token *> addr_tokens;
12629 : 29524 : tree sentinel = OMP_CLAUSE_CHAIN (grp_end);
12630 : :
12631 : 29524 : if (new_next && !grp->reprocess_struct)
12632 : 510 : grp->grp_start = new_next;
12633 : :
12634 : 29524 : new_next = NULL;
12635 : :
12636 : 29524 : tree *grp_start_p = grp->grp_start;
12637 : :
12638 : 29524 : if (DECL_P (decl))
12639 : 20233 : continue;
12640 : :
12641 : : /* Skip groups we marked for deletion in
12642 : : {omp,oacc}_resolve_clause_dependencies. */
12643 : 9291 : if (grp->deleted)
12644 : 462 : continue;
12645 : :
12646 : 8829 : if (OMP_CLAUSE_CHAIN (*grp_start_p)
12647 : 8829 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
12648 : : {
12649 : : /* Don't process an array descriptor that isn't inside a derived type
12650 : : as a struct (the GOMP_MAP_POINTER following will have the form
12651 : : "var.data", but such mappings are handled specially). */
12652 : 5373 : tree grpmid = OMP_CLAUSE_CHAIN (*grp_start_p);
12653 : 5373 : if (omp_map_clause_descriptor_p (grpmid)
12654 : 8999 : && DECL_P (OMP_CLAUSE_DECL (grpmid)))
12655 : 2766 : continue;
12656 : : }
12657 : :
12658 : : tree expr = decl;
12659 : :
12660 : 7086 : while (TREE_CODE (expr) == ARRAY_REF)
12661 : 1023 : expr = TREE_OPERAND (expr, 0);
12662 : :
12663 : 6063 : if (!omp_parse_expr (addr_tokens, expr))
12664 : 0 : continue;
12665 : :
12666 : 6063 : omp_addr_token *last_token
12667 : 6063 : = omp_first_chained_access_token (addr_tokens);
12668 : :
12669 : : /* A mapping of a reference to a pointer member that doesn't specify an
12670 : : array section, etc., like this:
12671 : : *mystruct.ref_to_ptr
12672 : : should not be processed by the struct sibling-list handling code --
12673 : : it just transfers the referenced pointer.
12674 : :
12675 : : In contrast, the quite similar-looking construct:
12676 : : *mystruct.ptr
12677 : : which is equivalent to e.g.
12678 : : mystruct.ptr[0]
12679 : : *does* trigger sibling-list processing.
12680 : :
12681 : : An exception for the former case is for "fragile" groups where the
12682 : : reference itself is not handled otherwise; this is subject to special
12683 : : handling in omp_accumulate_sibling_list also. */
12684 : :
12685 : 6063 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
12686 : 2829 : && last_token->type == ACCESS_METHOD
12687 : 2829 : && last_token->u.access_kind == ACCESS_REF
12688 : 6347 : && !grp->fragile)
12689 : 219 : continue;
12690 : :
12691 : 5844 : tree d = decl;
12692 : 5844 : if (TREE_CODE (d) == ARRAY_REF)
12693 : : {
12694 : 1990 : while (TREE_CODE (d) == ARRAY_REF)
12695 : 1006 : d = TREE_OPERAND (d, 0);
12696 : 984 : if (TREE_CODE (d) == COMPONENT_REF
12697 : 984 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
12698 : : decl = d;
12699 : : }
12700 : 5844 : if (d == decl
12701 : 5171 : && INDIRECT_REF_P (decl)
12702 : 689 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
12703 : 105 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
12704 : : == REFERENCE_TYPE)
12705 : 5941 : && (OMP_CLAUSE_MAP_KIND (c)
12706 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
12707 : 97 : decl = TREE_OPERAND (decl, 0);
12708 : :
12709 : 5844 : STRIP_NOPS (decl);
12710 : :
12711 : 5844 : if (TREE_CODE (decl) != COMPONENT_REF)
12712 : 1307 : continue;
12713 : :
12714 : : /* If we're mapping the whole struct in another node, skip adding this
12715 : : node to a sibling list. */
12716 : 4537 : omp_mapping_group *wholestruct;
12717 : 4537 : if (omp_mapped_by_containing_struct (*grpmap, OMP_CLAUSE_DECL (c),
12718 : : &wholestruct))
12719 : 125 : continue;
12720 : :
12721 : 4412 : if (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_TO_PSET
12722 : 4412 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
12723 : 4308 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
12724 : : && code != OACC_UPDATE
12725 : 8653 : && code != OMP_TARGET_UPDATE)
12726 : : {
12727 : 4050 : if (error_operand_p (decl))
12728 : : {
12729 : 0 : success = false;
12730 : 0 : goto error_out;
12731 : : }
12732 : :
12733 : 4050 : tree stype = TREE_TYPE (decl);
12734 : 4050 : if (TREE_CODE (stype) == REFERENCE_TYPE)
12735 : 768 : stype = TREE_TYPE (stype);
12736 : 4050 : if (TYPE_SIZE_UNIT (stype) == NULL
12737 : 4050 : || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
12738 : : {
12739 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
12740 : : "mapping field %qE of variable length "
12741 : 0 : "structure", OMP_CLAUSE_DECL (c));
12742 : 0 : success = false;
12743 : 0 : goto error_out;
12744 : : }
12745 : :
12746 : 4050 : tree *inner = NULL;
12747 : 4050 : bool fragile_p = grp->fragile;
12748 : :
12749 : 4050 : new_next
12750 : 8100 : = omp_accumulate_sibling_list (region_type, code,
12751 : : struct_map_to_clause, *grpmap,
12752 : : grp_start_p, grp_end, addr_tokens,
12753 : : &inner, &fragile_p,
12754 : 4050 : grp->reprocess_struct, &added_tail);
12755 : :
12756 : 4050 : if (inner)
12757 : : {
12758 : 318 : omp_mapping_group newgrp;
12759 : 318 : newgrp.grp_start = inner;
12760 : 318 : if (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (*inner))
12761 : : == GOMP_MAP_ATTACH_DETACH)
12762 : 286 : newgrp.grp_end = OMP_CLAUSE_CHAIN (*inner);
12763 : : else
12764 : 32 : newgrp.grp_end = *inner;
12765 : 318 : newgrp.mark = UNVISITED;
12766 : 318 : newgrp.sibling = NULL;
12767 : 318 : newgrp.deleted = false;
12768 : 318 : newgrp.reprocess_struct = true;
12769 : 318 : newgrp.fragile = fragile_p;
12770 : 318 : newgrp.next = NULL;
12771 : 318 : groups->safe_push (newgrp);
12772 : :
12773 : : /* !!! Growing GROUPS might invalidate the pointers in the group
12774 : : map. Rebuild it here. This is a bit inefficient, but
12775 : : shouldn't happen very often. */
12776 : 636 : delete (*grpmap);
12777 : 318 : *grpmap
12778 : 318 : = omp_reindex_mapping_groups (list_p, groups, &pre_hwm_groups,
12779 : : sentinel);
12780 : : }
12781 : : }
12782 : 29524 : }
12783 : :
12784 : : /* Delete groups marked for deletion above. At this point the order of the
12785 : : groups may no longer correspond to the order of the underlying list,
12786 : : which complicates this a little. First clear out OMP_CLAUSE_DECL for
12787 : : deleted nodes... */
12788 : :
12789 : 46594 : FOR_EACH_VEC_ELT (*groups, i, grp)
12790 : 29524 : if (grp->deleted)
12791 : 462 : for (tree d = *grp->grp_start;
12792 : 924 : d != OMP_CLAUSE_CHAIN (grp->grp_end);
12793 : 462 : d = OMP_CLAUSE_CHAIN (d))
12794 : 462 : OMP_CLAUSE_DECL (d) = NULL_TREE;
12795 : :
12796 : : /* ...then sweep through the list removing the now-empty nodes. */
12797 : :
12798 : : tail = list_p;
12799 : 90338 : while (*tail)
12800 : : {
12801 : 73268 : if (OMP_CLAUSE_CODE (*tail) == OMP_CLAUSE_MAP
12802 : 73268 : && OMP_CLAUSE_DECL (*tail) == NULL_TREE)
12803 : 462 : *tail = OMP_CLAUSE_CHAIN (*tail);
12804 : : else
12805 : 72806 : tail = &OMP_CLAUSE_CHAIN (*tail);
12806 : : }
12807 : :
12808 : : /* Tack on the struct nodes added during nested struct reprocessing. */
12809 : 17070 : if (added_nodes)
12810 : : {
12811 : 192 : *tail = added_nodes;
12812 : 192 : tail = added_tail;
12813 : : }
12814 : :
12815 : : /* Now we have finished building the struct sibling lists, reprocess
12816 : : newly-added "attach" nodes: we need the address of the first
12817 : : mapped element of each struct sibling list for the bias of the attach
12818 : : operation -- not necessarily the base address of the whole struct. */
12819 : 17070 : if (struct_map_to_clause)
12820 : 4982 : for (hash_map<tree_operand_hash, tree>::iterator iter
12821 : 2277 : = struct_map_to_clause->begin ();
12822 : 7259 : iter != struct_map_to_clause->end ();
12823 : 2705 : ++iter)
12824 : : {
12825 : 2705 : tree struct_node = (*iter).second;
12826 : 2705 : gcc_assert (OMP_CLAUSE_CODE (struct_node) == OMP_CLAUSE_MAP);
12827 : 2705 : tree attach = OMP_CLAUSE_CHAIN (struct_node);
12828 : :
12829 : 2705 : if (OMP_CLAUSE_CODE (attach) != OMP_CLAUSE_MAP
12830 : 2705 : || OMP_CLAUSE_MAP_KIND (attach) != GOMP_MAP_ATTACH_DETACH)
12831 : 2283 : continue;
12832 : :
12833 : 422 : OMP_CLAUSE_SET_MAP_KIND (attach, GOMP_MAP_ATTACH);
12834 : :
12835 : : /* Sanity check: the standalone attach node will not work if we have
12836 : : an "enter data" operation (because for those, variables need to be
12837 : : mapped separately and attach nodes must be grouped together with the
12838 : : base they attach to). We should only have created the
12839 : : ATTACH_DETACH node after GOMP_MAP_STRUCT for a target region, so
12840 : : this should never be true. */
12841 : 422 : gcc_assert ((region_type & ORT_TARGET) != 0);
12842 : :
12843 : : /* This is the first sorted node in the struct sibling list. Use it
12844 : : to recalculate the correct bias to use.
12845 : : (&first_node - attach_decl).
12846 : : For GOMP_MAP_STRUCT_UNORD, we need e.g. the
12847 : : min(min(min(first,second),third),fourth) element, because the
12848 : : elements aren't in any particular order. */
12849 : 422 : tree lowest_addr;
12850 : 422 : if (OMP_CLAUSE_MAP_KIND (struct_node) == GOMP_MAP_STRUCT_UNORD)
12851 : : {
12852 : 4 : tree first_node = OMP_CLAUSE_CHAIN (attach);
12853 : 4 : unsigned HOST_WIDE_INT num_mappings
12854 : 4 : = tree_to_uhwi (OMP_CLAUSE_SIZE (struct_node));
12855 : 4 : lowest_addr = OMP_CLAUSE_DECL (first_node);
12856 : 4 : lowest_addr = build_fold_addr_expr (lowest_addr);
12857 : 4 : lowest_addr = fold_convert (pointer_sized_int_node, lowest_addr);
12858 : 4 : tree next_node = OMP_CLAUSE_CHAIN (first_node);
12859 : 8 : while (num_mappings > 1)
12860 : : {
12861 : 4 : tree tmp = OMP_CLAUSE_DECL (next_node);
12862 : 4 : tmp = build_fold_addr_expr (tmp);
12863 : 4 : tmp = fold_convert (pointer_sized_int_node, tmp);
12864 : 4 : lowest_addr = fold_build2 (MIN_EXPR, pointer_sized_int_node,
12865 : : lowest_addr, tmp);
12866 : 4 : next_node = OMP_CLAUSE_CHAIN (next_node);
12867 : 4 : num_mappings--;
12868 : : }
12869 : 4 : lowest_addr = fold_convert (ptrdiff_type_node, lowest_addr);
12870 : : }
12871 : : else
12872 : : {
12873 : 418 : tree first_node = OMP_CLAUSE_DECL (OMP_CLAUSE_CHAIN (attach));
12874 : 418 : first_node = build_fold_addr_expr (first_node);
12875 : 418 : lowest_addr = fold_convert (ptrdiff_type_node, first_node);
12876 : : }
12877 : 422 : tree attach_decl = OMP_CLAUSE_DECL (attach);
12878 : 422 : attach_decl = fold_convert (ptrdiff_type_node, attach_decl);
12879 : 422 : OMP_CLAUSE_SIZE (attach)
12880 : 422 : = fold_build2 (MINUS_EXPR, ptrdiff_type_node, lowest_addr,
12881 : : attach_decl);
12882 : :
12883 : : /* Remove GOMP_MAP_ATTACH node from after struct node. */
12884 : 422 : OMP_CLAUSE_CHAIN (struct_node) = OMP_CLAUSE_CHAIN (attach);
12885 : : /* ...and re-insert it at the end of our clause list. */
12886 : 422 : *tail = attach;
12887 : 422 : OMP_CLAUSE_CHAIN (attach) = NULL_TREE;
12888 : 422 : tail = &OMP_CLAUSE_CHAIN (attach);
12889 : : }
12890 : :
12891 : 14793 : error_out:
12892 : 17070 : if (struct_map_to_clause)
12893 : 2277 : delete struct_map_to_clause;
12894 : :
12895 : 17070 : return success;
12896 : 17070 : }
12897 : :
12898 : : struct instantiate_mapper_info
12899 : : {
12900 : : tree *mapper_clauses_p;
12901 : : struct gimplify_omp_ctx *omp_ctx;
12902 : : gimple_seq *pre_p;
12903 : : };
12904 : :
12905 : : /* Helper function for omp_instantiate_mapper. */
12906 : :
12907 : : static tree
12908 : 1272 : remap_mapper_decl_1 (tree *tp, int *walk_subtrees, void *data)
12909 : : {
12910 : 1272 : copy_body_data *id = (copy_body_data *) data;
12911 : :
12912 : 1272 : if (DECL_P (*tp))
12913 : : {
12914 : 564 : tree replacement = remap_decl (*tp, id);
12915 : 564 : if (*tp != replacement)
12916 : : {
12917 : 289 : *tp = unshare_expr (replacement);
12918 : 289 : *walk_subtrees = 0;
12919 : : }
12920 : : }
12921 : :
12922 : 1272 : return NULL_TREE;
12923 : : }
12924 : :
12925 : : /* A copy_decl implementation (for use with tree-inline.cc functions) that
12926 : : only transform decls or SSA names that are part of a map we already
12927 : : prepared. */
12928 : :
12929 : : static tree
12930 : 94 : omp_mapper_copy_decl (tree var, copy_body_data *cb)
12931 : : {
12932 : 94 : tree *repl = cb->decl_map->get (var);
12933 : :
12934 : 94 : if (repl)
12935 : 0 : return *repl;
12936 : :
12937 : 94 : return var;
12938 : : }
12939 : :
12940 : : static tree *
12941 : 80 : omp_instantiate_mapper (gimple_seq *pre_p,
12942 : : hash_map<omp_name_type<tree>, tree> *implicit_mappers,
12943 : : tree mapperfn, tree expr, enum gomp_map_kind outer_kind,
12944 : : tree *mapper_clauses_p)
12945 : : {
12946 : 80 : tree mapper_name = NULL_TREE;
12947 : 80 : tree mapper = lang_hooks.decls.omp_extract_mapper_directive (mapperfn);
12948 : 80 : gcc_assert (TREE_CODE (mapper) == OMP_DECLARE_MAPPER);
12949 : :
12950 : 80 : tree clause = OMP_DECLARE_MAPPER_CLAUSES (mapper);
12951 : 80 : tree dummy_var = OMP_DECLARE_MAPPER_DECL (mapper);
12952 : :
12953 : : /* The "extraction map" is used to map the mapper variable in the "declare
12954 : : mapper" directive, and also any temporary variables that have been created
12955 : : as part of expanding the mapper function's body (which are expanded as a
12956 : : "bind" expression in the pre_p sequence). */
12957 : 80 : hash_map<tree, tree> extraction_map;
12958 : :
12959 : 80 : extraction_map.put (dummy_var, expr);
12960 : 80 : extraction_map.put (expr, expr);
12961 : :
12962 : : /* This copy_body_data is only used to remap the decls in the
12963 : : OMP_DECLARE_MAPPER tree node expansion itself. All relevant decls should
12964 : : already be in the current function. */
12965 : 80 : copy_body_data id;
12966 : 80 : memset (&id, 0, sizeof (id));
12967 : 80 : id.src_fn = current_function_decl;
12968 : 80 : id.dst_fn = current_function_decl;
12969 : 80 : id.src_cfun = cfun;
12970 : 80 : id.decl_map = &extraction_map;
12971 : 80 : id.copy_decl = omp_mapper_copy_decl;
12972 : 80 : id.transform_call_graph_edges = CB_CGE_DUPLICATE; // ???
12973 : 80 : id.transform_new_cfg = true; // ???
12974 : :
12975 : 238 : for (; clause; clause = OMP_CLAUSE_CHAIN (clause))
12976 : : {
12977 : 158 : enum gomp_map_kind map_kind = OMP_CLAUSE_MAP_KIND (clause);
12978 : 158 : tree *nested_mapper_p = NULL;
12979 : :
12980 : 158 : if (map_kind == GOMP_MAP_PUSH_MAPPER_NAME)
12981 : : {
12982 : 0 : mapper_name = OMP_CLAUSE_DECL (clause);
12983 : 23 : continue;
12984 : : }
12985 : 158 : else if (map_kind == GOMP_MAP_POP_MAPPER_NAME)
12986 : : {
12987 : 0 : mapper_name = NULL_TREE;
12988 : 0 : continue;
12989 : : }
12990 : :
12991 : 158 : tree decl = OMP_CLAUSE_DECL (clause);
12992 : 158 : tree unshared, type;
12993 : 158 : bool nonunit_array_with_mapper = false;
12994 : :
12995 : 158 : if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
12996 : : {
12997 : 62 : location_t loc = OMP_CLAUSE_LOCATION (clause);
12998 : 62 : tree tmp = lang_hooks.decls.omp_map_array_section (loc, decl);
12999 : 62 : if (tmp == decl)
13000 : : {
13001 : 48 : unshared = unshare_expr (clause);
13002 : 48 : nonunit_array_with_mapper = true;
13003 : 48 : type = TREE_TYPE (TREE_TYPE (decl));
13004 : : }
13005 : : else
13006 : : {
13007 : 14 : unshared = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
13008 : 14 : OMP_CLAUSE_CODE (clause));
13009 : 14 : OMP_CLAUSE_DECL (unshared) = tmp;
13010 : 14 : OMP_CLAUSE_SIZE (unshared)
13011 : 28 : = DECL_P (tmp) ? DECL_SIZE_UNIT (tmp)
13012 : 14 : : TYPE_SIZE_UNIT (TREE_TYPE (tmp));
13013 : 14 : type = TREE_TYPE (tmp);
13014 : : }
13015 : : }
13016 : : else
13017 : : {
13018 : 96 : unshared = unshare_expr (clause);
13019 : 96 : type = TREE_TYPE (decl);
13020 : : }
13021 : :
13022 : 158 : walk_tree (&unshared, remap_mapper_decl_1, &id, NULL);
13023 : :
13024 : 158 : if (OMP_CLAUSE_MAP_KIND (unshared) == GOMP_MAP_UNSET)
13025 : 28 : OMP_CLAUSE_SET_MAP_KIND (unshared, outer_kind);
13026 : :
13027 : 158 : decl = OMP_CLAUSE_DECL (unshared);
13028 : 158 : type = TYPE_MAIN_VARIANT (type);
13029 : :
13030 : 158 : nested_mapper_p = implicit_mappers->get ({ mapper_name, type });
13031 : :
13032 : 158 : if (nested_mapper_p && *nested_mapper_p != mapperfn)
13033 : : {
13034 : 23 : if (nonunit_array_with_mapper)
13035 : : {
13036 : 8 : sorry ("user-defined mapper with non-unit length array section");
13037 : 8 : continue;
13038 : : }
13039 : :
13040 : 15 : if (map_kind == GOMP_MAP_UNSET)
13041 : 0 : map_kind = outer_kind;
13042 : :
13043 : 15 : mapper_clauses_p
13044 : 15 : = omp_instantiate_mapper (pre_p, implicit_mappers,
13045 : : *nested_mapper_p, decl, map_kind,
13046 : : mapper_clauses_p);
13047 : 15 : continue;
13048 : : }
13049 : :
13050 : 135 : *mapper_clauses_p = unshared;
13051 : 135 : mapper_clauses_p = &OMP_CLAUSE_CHAIN (unshared);
13052 : : }
13053 : :
13054 : 80 : return mapper_clauses_p;
13055 : 80 : }
13056 : :
13057 : : static int
13058 : 124094 : omp_instantiate_implicit_mappers (splay_tree_node n, void *data)
13059 : : {
13060 : 124094 : tree decl = (tree) n->key;
13061 : 124094 : instantiate_mapper_info *im_info = (instantiate_mapper_info *) data;
13062 : 124094 : gimplify_omp_ctx *ctx = im_info->omp_ctx;
13063 : 124094 : tree *mapper_p = NULL;
13064 : 124094 : tree type = TREE_TYPE (decl);
13065 : 124094 : bool ref_p = false;
13066 : 124094 : unsigned flags = n->value;
13067 : :
13068 : 124094 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
13069 : : return 0;
13070 : 22998 : if ((flags & GOVD_SEEN) == 0)
13071 : : return 0;
13072 : : /* If we already have clauses pertaining to a struct variable, then we don't
13073 : : want to implicitly invoke a user-defined mapper. */
13074 : 19630 : if ((flags & GOVD_EXPLICIT) != 0 && AGGREGATE_TYPE_P (TREE_TYPE (decl)))
13075 : : return 0;
13076 : :
13077 : 19630 : if (TREE_CODE (type) == REFERENCE_TYPE)
13078 : : {
13079 : 896 : ref_p = true;
13080 : 896 : type = TREE_TYPE (type);
13081 : : }
13082 : :
13083 : 19630 : type = TYPE_MAIN_VARIANT (type);
13084 : :
13085 : 19630 : if (DECL_P (decl) && type && AGGREGATE_TYPE_P (type))
13086 : : {
13087 : 5260 : gcc_assert (ctx);
13088 : 5260 : mapper_p = ctx->implicit_mappers->get ({ NULL_TREE, type });
13089 : : }
13090 : :
13091 : 5260 : if (mapper_p)
13092 : : {
13093 : : /* If we have a reference, map the pointed-to object rather than the
13094 : : reference itself. */
13095 : 65 : if (ref_p)
13096 : 2 : decl = build_fold_indirect_ref (decl);
13097 : :
13098 : 65 : im_info->mapper_clauses_p
13099 : 65 : = omp_instantiate_mapper (im_info->pre_p, ctx->implicit_mappers,
13100 : : *mapper_p, decl, GOMP_MAP_TOFROM,
13101 : : im_info->mapper_clauses_p);
13102 : : /* Make sure we don't map the same variable implicitly in
13103 : : gimplify_adjust_omp_clauses_1 also. */
13104 : 65 : n->value |= GOVD_EXPLICIT;
13105 : : }
13106 : :
13107 : : return 0;
13108 : : }
13109 : :
13110 : : /* Scan the OMP clauses in *LIST_P, installing mappings into a new
13111 : : and previous omp contexts. */
13112 : :
13113 : : static void
13114 : 129329 : gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
13115 : : enum omp_region_type region_type,
13116 : : enum tree_code code)
13117 : : {
13118 : 129329 : using namespace omp_addr_tokenizer;
13119 : 129329 : struct gimplify_omp_ctx *ctx, *outer_ctx;
13120 : 129329 : tree c;
13121 : 129329 : tree *orig_list_p = list_p;
13122 : 129329 : int handled_depend_iterators = -1;
13123 : 129329 : int nowait = -1;
13124 : :
13125 : 129329 : ctx = new_omp_context (region_type);
13126 : 129329 : ctx->code = code;
13127 : 129329 : outer_ctx = ctx->outer_context;
13128 : 129329 : if (code == OMP_TARGET)
13129 : : {
13130 : 12541 : if (!lang_GNU_Fortran ())
13131 : 10554 : ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
13132 : 12541 : ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
13133 : 25082 : ctx->defaultmap[GDMK_SCALAR_TARGET] = (lang_GNU_Fortran ()
13134 : 12541 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
13135 : : }
13136 : 129329 : if (!lang_GNU_Fortran ())
13137 : 98859 : switch (code)
13138 : : {
13139 : 17906 : case OMP_TARGET:
13140 : 17906 : case OMP_TARGET_DATA:
13141 : 17906 : case OMP_TARGET_ENTER_DATA:
13142 : 17906 : case OMP_TARGET_EXIT_DATA:
13143 : 17906 : case OACC_DECLARE:
13144 : 17906 : case OACC_HOST_DATA:
13145 : 17906 : case OACC_PARALLEL:
13146 : 17906 : case OACC_KERNELS:
13147 : 17906 : ctx->target_firstprivatize_array_bases = true;
13148 : : default:
13149 : : break;
13150 : : }
13151 : :
13152 : 129329 : vec<omp_mapping_group> *groups = NULL;
13153 : 129329 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
13154 : 129329 : unsigned grpnum = 0;
13155 : 129329 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
13156 : :
13157 : 129329 : if (code == OMP_TARGET
13158 : 129329 : || code == OMP_TARGET_DATA
13159 : 129329 : || code == OMP_TARGET_ENTER_DATA
13160 : : || code == OMP_TARGET_EXIT_DATA
13161 : : || code == OACC_DATA
13162 : : || code == OACC_KERNELS
13163 : : || code == OACC_PARALLEL
13164 : : || code == OACC_SERIAL
13165 : : || code == OACC_ENTER_DATA
13166 : : || code == OACC_EXIT_DATA
13167 : : || code == OACC_UPDATE
13168 : : || code == OACC_DECLARE)
13169 : : {
13170 : 32543 : groups = omp_gather_mapping_groups (list_p);
13171 : :
13172 : 32543 : if (groups)
13173 : 17279 : grpmap = omp_index_mapping_groups (groups);
13174 : : }
13175 : :
13176 : 331029 : while ((c = *list_p) != NULL)
13177 : : {
13178 : 201700 : bool remove = false;
13179 : 201700 : bool notice_outer = true;
13180 : 201700 : bool map_descriptor;
13181 : 201700 : const char *check_non_private = NULL;
13182 : 201700 : unsigned int flags;
13183 : 201700 : tree decl;
13184 : 201700 : auto_vec<omp_addr_token *, 10> addr_tokens;
13185 : 201700 : tree op = NULL_TREE;
13186 : 201700 : location_t loc = OMP_CLAUSE_LOCATION (c);
13187 : :
13188 : 237018 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
13189 : : {
13190 : : grp_start_p = NULL;
13191 : : grp_end = NULL_TREE;
13192 : : }
13193 : :
13194 : 201700 : if (code == OMP_TARGET
13195 : : || code == OMP_TARGET_DATA
13196 : : || code == OMP_TARGET_ENTER_DATA
13197 : 171639 : || code == OMP_TARGET_EXIT_DATA)
13198 : : /* Do some target-specific type checks for map operands. */
13199 : 31607 : switch (OMP_CLAUSE_CODE (c))
13200 : : {
13201 : 22697 : case OMP_CLAUSE_MAP:
13202 : 22697 : op = OMP_CLAUSE_OPERAND (c, 0);
13203 : 22697 : verify_type_context (loc, TCTX_OMP_MAP, TREE_TYPE (op));
13204 : 22697 : break;
13205 : 129 : case OMP_CLAUSE_PRIVATE:
13206 : 129 : op = OMP_CLAUSE_OPERAND (c, 0);
13207 : 129 : verify_type_context (loc, TCTX_OMP_PRIVATE, TREE_TYPE (op));
13208 : 129 : break;
13209 : 1230 : case OMP_CLAUSE_FIRSTPRIVATE:
13210 : 1230 : op = OMP_CLAUSE_OPERAND (c, 0);
13211 : 1230 : verify_type_context (loc, TCTX_OMP_FIRSTPRIVATE, TREE_TYPE (op));
13212 : 1230 : break;
13213 : 2906 : case OMP_CLAUSE_IS_DEVICE_PTR:
13214 : 2906 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13215 : 2906 : case OMP_CLAUSE_USE_DEVICE_PTR:
13216 : 2906 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13217 : 2906 : op = OMP_CLAUSE_OPERAND (c, 0);
13218 : 2906 : verify_type_context (loc, TCTX_OMP_DEVICE_ADDR, TREE_TYPE (op));
13219 : 2906 : break;
13220 : : default:
13221 : : break;
13222 : : }
13223 : :
13224 : 201700 : switch (OMP_CLAUSE_CODE (c))
13225 : : {
13226 : 12137 : case OMP_CLAUSE_PRIVATE:
13227 : 12137 : flags = GOVD_PRIVATE | GOVD_EXPLICIT;
13228 : 12137 : if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
13229 : : {
13230 : 165 : flags |= GOVD_PRIVATE_OUTER_REF;
13231 : 165 : OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
13232 : : }
13233 : : else
13234 : : notice_outer = false;
13235 : 12137 : goto do_add;
13236 : 5510 : case OMP_CLAUSE_SHARED:
13237 : 5510 : flags = GOVD_SHARED | GOVD_EXPLICIT;
13238 : 5510 : goto do_add;
13239 : 7833 : case OMP_CLAUSE_FIRSTPRIVATE:
13240 : 7833 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13241 : 7833 : check_non_private = "firstprivate";
13242 : 7833 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
13243 : : {
13244 : 380 : gcc_assert (code == OMP_TARGET);
13245 : : flags |= GOVD_FIRSTPRIVATE_IMPLICIT;
13246 : : }
13247 : 7833 : goto do_add;
13248 : 7325 : case OMP_CLAUSE_LASTPRIVATE:
13249 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13250 : 496 : switch (code)
13251 : : {
13252 : 25 : case OMP_DISTRIBUTE:
13253 : 25 : error_at (OMP_CLAUSE_LOCATION (c),
13254 : : "conditional %<lastprivate%> clause on "
13255 : : "%qs construct", "distribute");
13256 : 25 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13257 : 25 : break;
13258 : 13 : case OMP_TASKLOOP:
13259 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
13260 : : "conditional %<lastprivate%> clause on "
13261 : : "%qs construct", "taskloop");
13262 : 13 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13263 : 13 : break;
13264 : : default:
13265 : : break;
13266 : : }
13267 : 7325 : flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
13268 : 7325 : if (code != OMP_LOOP)
13269 : 6974 : check_non_private = "lastprivate";
13270 : 7325 : decl = OMP_CLAUSE_DECL (c);
13271 : 7325 : if (error_operand_p (decl))
13272 : 0 : goto do_add;
13273 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
13274 : 7325 : && !lang_hooks.decls.omp_scalar_p (decl, true))
13275 : : {
13276 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
13277 : : "non-scalar variable %qD in conditional "
13278 : : "%<lastprivate%> clause", decl);
13279 : 5 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
13280 : : }
13281 : 7325 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
13282 : 453 : flags |= GOVD_LASTPRIVATE_CONDITIONAL;
13283 : 7325 : omp_lastprivate_for_combined_outer_constructs (outer_ctx, decl,
13284 : : false);
13285 : 7325 : goto do_add;
13286 : 15290 : case OMP_CLAUSE_REDUCTION:
13287 : 15290 : if (OMP_CLAUSE_REDUCTION_TASK (c))
13288 : : {
13289 : 594 : if (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
13290 : : {
13291 : 408 : if (nowait == -1)
13292 : 293 : nowait = omp_find_clause (*list_p,
13293 : 293 : OMP_CLAUSE_NOWAIT) != NULL_TREE;
13294 : 408 : if (nowait
13295 : 15 : && (outer_ctx == NULL
13296 : 0 : || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
13297 : : {
13298 : 15 : error_at (OMP_CLAUSE_LOCATION (c),
13299 : : "%<task%> reduction modifier on a construct "
13300 : : "with a %<nowait%> clause");
13301 : 15 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13302 : : }
13303 : : }
13304 : 186 : else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
13305 : : {
13306 : 40 : error_at (OMP_CLAUSE_LOCATION (c),
13307 : : "invalid %<task%> reduction modifier on construct "
13308 : : "other than %<parallel%>, %qs, %<sections%> or "
13309 : 20 : "%<scope%>", lang_GNU_Fortran () ? "do" : "for");
13310 : 20 : OMP_CLAUSE_REDUCTION_TASK (c) = 0;
13311 : : }
13312 : : }
13313 : 15290 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
13314 : 831 : switch (code)
13315 : : {
13316 : 4 : case OMP_SECTIONS:
13317 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13318 : : "%<inscan%> %<reduction%> clause on "
13319 : : "%qs construct", "sections");
13320 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13321 : 4 : break;
13322 : 4 : case OMP_PARALLEL:
13323 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13324 : : "%<inscan%> %<reduction%> clause on "
13325 : : "%qs construct", "parallel");
13326 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13327 : 4 : break;
13328 : 4 : case OMP_TEAMS:
13329 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13330 : : "%<inscan%> %<reduction%> clause on "
13331 : : "%qs construct", "teams");
13332 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13333 : 4 : break;
13334 : 4 : case OMP_TASKLOOP:
13335 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13336 : : "%<inscan%> %<reduction%> clause on "
13337 : : "%qs construct", "taskloop");
13338 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13339 : 4 : break;
13340 : 4 : case OMP_SCOPE:
13341 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
13342 : : "%<inscan%> %<reduction%> clause on "
13343 : : "%qs construct", "scope");
13344 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
13345 : 4 : break;
13346 : : default:
13347 : : break;
13348 : : }
13349 : : /* FALLTHRU */
13350 : 17890 : case OMP_CLAUSE_IN_REDUCTION:
13351 : 17890 : case OMP_CLAUSE_TASK_REDUCTION:
13352 : 17890 : flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
13353 : : /* OpenACC permits reductions on private variables. */
13354 : 17890 : if (!(region_type & ORT_ACC)
13355 : : /* taskgroup is actually not a worksharing region. */
13356 : 12374 : && code != OMP_TASKGROUP)
13357 : 11844 : check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
13358 : 17890 : decl = OMP_CLAUSE_DECL (c);
13359 : 17890 : if (TREE_CODE (decl) == MEM_REF)
13360 : : {
13361 : 2614 : tree type = TREE_TYPE (decl);
13362 : 2614 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
13363 : 2614 : gimplify_ctxp->into_ssa = false;
13364 : 2614 : if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
13365 : : NULL, is_gimple_val, fb_rvalue, false)
13366 : : == GS_ERROR)
13367 : : {
13368 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13369 : 0 : remove = true;
13370 : 0 : break;
13371 : : }
13372 : 2614 : gimplify_ctxp->into_ssa = saved_into_ssa;
13373 : 2614 : tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13374 : 2614 : if (DECL_P (v))
13375 : : {
13376 : 571 : omp_firstprivatize_variable (ctx, v);
13377 : 571 : omp_notice_variable (ctx, v, true);
13378 : : }
13379 : 2614 : decl = TREE_OPERAND (decl, 0);
13380 : 2614 : if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
13381 : : {
13382 : 477 : gimplify_ctxp->into_ssa = false;
13383 : 477 : if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
13384 : : NULL, is_gimple_val, fb_rvalue, false)
13385 : : == GS_ERROR)
13386 : : {
13387 : 0 : gimplify_ctxp->into_ssa = saved_into_ssa;
13388 : 0 : remove = true;
13389 : 0 : break;
13390 : : }
13391 : 477 : gimplify_ctxp->into_ssa = saved_into_ssa;
13392 : 477 : v = TREE_OPERAND (decl, 1);
13393 : 477 : if (DECL_P (v))
13394 : : {
13395 : 477 : omp_firstprivatize_variable (ctx, v);
13396 : 477 : omp_notice_variable (ctx, v, true);
13397 : : }
13398 : 477 : decl = TREE_OPERAND (decl, 0);
13399 : : }
13400 : 2614 : if (TREE_CODE (decl) == ADDR_EXPR
13401 : 1409 : || TREE_CODE (decl) == INDIRECT_REF)
13402 : 1375 : decl = TREE_OPERAND (decl, 0);
13403 : : }
13404 : 17890 : goto do_add_decl;
13405 : 2668 : case OMP_CLAUSE_LINEAR:
13406 : 2668 : if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
13407 : : is_gimple_val, fb_rvalue) == GS_ERROR)
13408 : : {
13409 : : remove = true;
13410 : : break;
13411 : : }
13412 : : else
13413 : : {
13414 : 2668 : if (code == OMP_SIMD
13415 : 2668 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13416 : : {
13417 : 1154 : struct gimplify_omp_ctx *octx = outer_ctx;
13418 : 1154 : if (octx
13419 : 741 : && octx->region_type == ORT_WORKSHARE
13420 : 500 : && octx->combined_loop
13421 : 500 : && !octx->distribute)
13422 : : {
13423 : 492 : if (octx->outer_context
13424 : 430 : && (octx->outer_context->region_type
13425 : : == ORT_COMBINED_PARALLEL))
13426 : 393 : octx = octx->outer_context->outer_context;
13427 : : else
13428 : : octx = octx->outer_context;
13429 : : }
13430 : 905 : if (octx
13431 : 316 : && octx->region_type == ORT_WORKSHARE
13432 : 16 : && octx->combined_loop
13433 : 16 : && octx->distribute)
13434 : : {
13435 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
13436 : : "%<linear%> clause for variable other than "
13437 : : "loop iterator specified on construct "
13438 : : "combined with %<distribute%>");
13439 : 16 : remove = true;
13440 : 16 : break;
13441 : : }
13442 : : }
13443 : : /* For combined #pragma omp parallel for simd, need to put
13444 : : lastprivate and perhaps firstprivate too on the
13445 : : parallel. Similarly for #pragma omp for simd. */
13446 : : struct gimplify_omp_ctx *octx = outer_ctx;
13447 : : bool taskloop_seen = false;
13448 : : decl = NULL_TREE;
13449 : 3443 : do
13450 : : {
13451 : 3443 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13452 : 3443 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13453 : : break;
13454 : 3383 : decl = OMP_CLAUSE_DECL (c);
13455 : 3383 : if (error_operand_p (decl))
13456 : : {
13457 : : decl = NULL_TREE;
13458 : : break;
13459 : : }
13460 : 3383 : flags = GOVD_SEEN;
13461 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
13462 : 1672 : flags |= GOVD_FIRSTPRIVATE;
13463 : 3383 : if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13464 : 3383 : flags |= GOVD_LASTPRIVATE;
13465 : 3383 : if (octx
13466 : 2506 : && octx->region_type == ORT_WORKSHARE
13467 : 1213 : && octx->combined_loop)
13468 : : {
13469 : 1207 : if (octx->outer_context
13470 : 1037 : && (octx->outer_context->region_type
13471 : : == ORT_COMBINED_PARALLEL))
13472 : : octx = octx->outer_context;
13473 : 476 : else if (omp_check_private (octx, decl, false))
13474 : : break;
13475 : : }
13476 : : else if (octx
13477 : 1299 : && (octx->region_type & ORT_TASK) != 0
13478 : 307 : && octx->combined_loop)
13479 : : taskloop_seen = true;
13480 : : else if (octx
13481 : 996 : && octx->region_type == ORT_COMBINED_PARALLEL
13482 : 301 : && ((ctx->region_type == ORT_WORKSHARE
13483 : 201 : && octx == outer_ctx)
13484 : 100 : || taskloop_seen))
13485 : : flags = GOVD_SEEN | GOVD_SHARED;
13486 : : else if (octx
13487 : 695 : && ((octx->region_type & ORT_COMBINED_TEAMS)
13488 : : == ORT_COMBINED_TEAMS))
13489 : : flags = GOVD_SEEN | GOVD_SHARED;
13490 : 540 : else if (octx
13491 : 540 : && octx->region_type == ORT_COMBINED_TARGET)
13492 : : {
13493 : 195 : if (flags & GOVD_LASTPRIVATE)
13494 : 195 : flags = GOVD_SEEN | GOVD_MAP;
13495 : : }
13496 : : else
13497 : : break;
13498 : 2071 : splay_tree_node on
13499 : 2071 : = splay_tree_lookup (octx->variables,
13500 : : (splay_tree_key) decl);
13501 : 2071 : if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
13502 : : {
13503 : : octx = NULL;
13504 : : break;
13505 : : }
13506 : 2067 : omp_add_variable (octx, decl, flags);
13507 : 2067 : if (octx->outer_context == NULL)
13508 : : break;
13509 : : octx = octx->outer_context;
13510 : : }
13511 : : while (1);
13512 : 2652 : if (octx
13513 : 2652 : && decl
13514 : 2652 : && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13515 : 728 : || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
13516 : 1711 : omp_notice_variable (octx, decl, true);
13517 : : }
13518 : 2652 : flags = GOVD_LINEAR | GOVD_EXPLICIT;
13519 : 2652 : if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
13520 : 2652 : && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
13521 : : {
13522 : : notice_outer = false;
13523 : : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
13524 : : }
13525 : 2652 : goto do_add;
13526 : :
13527 : 48163 : case OMP_CLAUSE_MAP:
13528 : 48163 : if (!grp_start_p)
13529 : : {
13530 : 29333 : grp_start_p = list_p;
13531 : 29333 : grp_end = (*groups)[grpnum].grp_end;
13532 : 29333 : grpnum++;
13533 : : }
13534 : 48163 : decl = OMP_CLAUSE_DECL (c);
13535 : :
13536 : 48163 : if (error_operand_p (decl))
13537 : : {
13538 : : remove = true;
13539 : : break;
13540 : : }
13541 : :
13542 : 48163 : if (!omp_parse_expr (addr_tokens, decl))
13543 : : {
13544 : : remove = true;
13545 : : break;
13546 : : }
13547 : :
13548 : 48163 : if (remove)
13549 : : break;
13550 : 48163 : if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
13551 : : {
13552 : : struct gimplify_omp_ctx *octx;
13553 : 1169 : for (octx = outer_ctx; octx; octx = octx->outer_context)
13554 : : {
13555 : 1169 : if (octx->region_type != ORT_ACC_HOST_DATA)
13556 : : break;
13557 : 12 : splay_tree_node n2
13558 : 12 : = splay_tree_lookup (octx->variables,
13559 : : (splay_tree_key) decl);
13560 : 12 : if (n2)
13561 : 4 : error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
13562 : : "declared in enclosing %<host_data%> region",
13563 : 4 : DECL_NAME (decl));
13564 : : }
13565 : : }
13566 : :
13567 : 48163 : map_descriptor = false;
13568 : :
13569 : : /* This condition checks if we're mapping an array descriptor that
13570 : : isn't inside a derived type -- these have special handling, and
13571 : : are not handled as structs in omp_build_struct_sibling_lists.
13572 : : See that function for further details. */
13573 : 48163 : if (*grp_start_p != grp_end
13574 : 32045 : && OMP_CLAUSE_CHAIN (*grp_start_p)
13575 : 80208 : && OMP_CLAUSE_CHAIN (*grp_start_p) != grp_end)
13576 : : {
13577 : 15457 : tree grp_mid = OMP_CLAUSE_CHAIN (*grp_start_p);
13578 : 15457 : if (omp_map_clause_descriptor_p (grp_mid)
13579 : 29147 : && DECL_P (OMP_CLAUSE_DECL (grp_mid)))
13580 : : map_descriptor = true;
13581 : : }
13582 : 32706 : else if (OMP_CLAUSE_CODE (grp_end) == OMP_CLAUSE_MAP
13583 : 32706 : && (OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_RELEASE
13584 : 32103 : || OMP_CLAUSE_MAP_KIND (grp_end) == GOMP_MAP_DELETE)
13585 : 33451 : && OMP_CLAUSE_RELEASE_DESCRIPTOR (grp_end))
13586 : : map_descriptor = true;
13587 : :
13588 : : /* Adding the decl for a struct access: we haven't created
13589 : : GOMP_MAP_STRUCT nodes yet, so this statement needs to predict
13590 : : whether they will be created in gimplify_adjust_omp_clauses.
13591 : : NOTE: Technically we should probably look through DECL_VALUE_EXPR
13592 : : here because something that looks like a DECL_P may actually be a
13593 : : struct access, e.g. variables in a lambda closure
13594 : : (__closure->__foo) or class members (this->foo). Currently in both
13595 : : those cases we map the whole of the containing object (directly in
13596 : : the C++ FE) though, so struct nodes are not created. */
13597 : 48163 : if (c == grp_end
13598 : 29333 : && addr_tokens[0]->type == STRUCTURE_BASE
13599 : 7613 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13600 : 55776 : && !map_descriptor)
13601 : : {
13602 : 4850 : gcc_assert (addr_tokens[1]->type == ACCESS_METHOD);
13603 : : /* If we got to this struct via a chain of pointers, maybe we
13604 : : want to map it implicitly instead. */
13605 : 4850 : if (omp_access_chain_p (addr_tokens, 1))
13606 : : break;
13607 : 4690 : omp_mapping_group *wholestruct;
13608 : 4690 : if (!(region_type & ORT_ACC)
13609 : 8109 : && omp_mapped_by_containing_struct (grpmap,
13610 : 3419 : OMP_CLAUSE_DECL (c),
13611 : : &wholestruct))
13612 : : break;
13613 : 4506 : decl = addr_tokens[1]->expr;
13614 : 4506 : if (splay_tree_lookup (ctx->variables, (splay_tree_key) decl))
13615 : : break;
13616 : : /* Standalone attach or detach clauses for a struct element
13617 : : should not inhibit implicit mapping of the whole struct. */
13618 : 2764 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13619 : 2764 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13620 : : break;
13621 : 2609 : flags = GOVD_MAP | GOVD_EXPLICIT;
13622 : :
13623 : 2609 : gcc_assert (addr_tokens[1]->u.access_kind != ACCESS_DIRECT
13624 : : || TREE_ADDRESSABLE (decl));
13625 : 2609 : goto do_add_decl;
13626 : : }
13627 : :
13628 : 43313 : if (!DECL_P (decl))
13629 : : {
13630 : 19714 : tree d = decl, *pd;
13631 : 19714 : if (TREE_CODE (d) == ARRAY_REF)
13632 : : {
13633 : 4882 : while (TREE_CODE (d) == ARRAY_REF)
13634 : 2498 : d = TREE_OPERAND (d, 0);
13635 : 2384 : if (TREE_CODE (d) == COMPONENT_REF
13636 : 2384 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
13637 : : decl = d;
13638 : : }
13639 : 19714 : pd = &OMP_CLAUSE_DECL (c);
13640 : 19714 : if (d == decl
13641 : 17376 : && TREE_CODE (decl) == INDIRECT_REF
13642 : 13265 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
13643 : 1069 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
13644 : : == REFERENCE_TYPE)
13645 : 20365 : && (OMP_CLAUSE_MAP_KIND (c)
13646 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
13647 : : {
13648 : 647 : pd = &TREE_OPERAND (decl, 0);
13649 : 647 : decl = TREE_OPERAND (decl, 0);
13650 : : }
13651 : :
13652 : 19714 : if (addr_tokens[0]->type == STRUCTURE_BASE
13653 : 10264 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13654 : 10264 : && addr_tokens[1]->type == ACCESS_METHOD
13655 : 10264 : && (addr_tokens[1]->u.access_kind == ACCESS_POINTER
13656 : 9769 : || (addr_tokens[1]->u.access_kind
13657 : : == ACCESS_POINTER_OFFSET))
13658 : 20291 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)))
13659 : : {
13660 : 0 : tree base = addr_tokens[1]->expr;
13661 : 0 : splay_tree_node n
13662 : 0 : = splay_tree_lookup (ctx->variables,
13663 : : (splay_tree_key) base);
13664 : 0 : n->value |= GOVD_SEEN;
13665 : : }
13666 : :
13667 : 19714 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
13668 : : {
13669 : : /* Don't gimplify *pd fully at this point, as the base
13670 : : will need to be adjusted during omp lowering. */
13671 : 88 : auto_vec<tree, 10> expr_stack;
13672 : 88 : tree *p = pd;
13673 : 88 : while (handled_component_p (*p)
13674 : : || TREE_CODE (*p) == INDIRECT_REF
13675 : : || TREE_CODE (*p) == ADDR_EXPR
13676 : : || TREE_CODE (*p) == MEM_REF
13677 : 224 : || TREE_CODE (*p) == NON_LVALUE_EXPR)
13678 : : {
13679 : 136 : expr_stack.safe_push (*p);
13680 : 136 : p = &TREE_OPERAND (*p, 0);
13681 : : }
13682 : 312 : for (int i = expr_stack.length () - 1; i >= 0; i--)
13683 : : {
13684 : 136 : tree t = expr_stack[i];
13685 : 136 : if (TREE_CODE (t) == ARRAY_REF
13686 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
13687 : : {
13688 : 56 : if (TREE_OPERAND (t, 2) == NULL_TREE)
13689 : : {
13690 : 56 : tree low = unshare_expr (array_ref_low_bound (t));
13691 : 56 : if (!is_gimple_min_invariant (low))
13692 : : {
13693 : 0 : TREE_OPERAND (t, 2) = low;
13694 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
13695 : : pre_p, NULL,
13696 : : is_gimple_reg,
13697 : : fb_rvalue) == GS_ERROR)
13698 : 0 : remove = true;
13699 : : }
13700 : : }
13701 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
13702 : : NULL, is_gimple_reg,
13703 : : fb_rvalue) == GS_ERROR)
13704 : 0 : remove = true;
13705 : 56 : if (TREE_OPERAND (t, 3) == NULL_TREE)
13706 : : {
13707 : 56 : tree elmt_size = array_ref_element_size (t);
13708 : 56 : if (!is_gimple_min_invariant (elmt_size))
13709 : : {
13710 : 0 : elmt_size = unshare_expr (elmt_size);
13711 : 0 : tree elmt_type
13712 : 0 : = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t,
13713 : : 0)));
13714 : 0 : tree factor
13715 : 0 : = size_int (TYPE_ALIGN_UNIT (elmt_type));
13716 : 0 : elmt_size
13717 : 0 : = size_binop (EXACT_DIV_EXPR, elmt_size,
13718 : : factor);
13719 : 0 : TREE_OPERAND (t, 3) = elmt_size;
13720 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 3),
13721 : : pre_p, NULL,
13722 : : is_gimple_reg,
13723 : : fb_rvalue) == GS_ERROR)
13724 : 0 : remove = true;
13725 : : }
13726 : : }
13727 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
13728 : : NULL, is_gimple_reg,
13729 : : fb_rvalue) == GS_ERROR)
13730 : 0 : remove = true;
13731 : : }
13732 : 80 : else if (TREE_CODE (t) == COMPONENT_REF)
13733 : : {
13734 : 0 : if (TREE_OPERAND (t, 2) == NULL_TREE)
13735 : : {
13736 : 0 : tree offset = component_ref_field_offset (t);
13737 : 0 : if (!is_gimple_min_invariant (offset))
13738 : : {
13739 : 0 : offset = unshare_expr (offset);
13740 : 0 : tree field = TREE_OPERAND (t, 1);
13741 : 0 : tree factor
13742 : 0 : = size_int (DECL_OFFSET_ALIGN (field)
13743 : : / BITS_PER_UNIT);
13744 : 0 : offset = size_binop (EXACT_DIV_EXPR, offset,
13745 : : factor);
13746 : 0 : TREE_OPERAND (t, 2) = offset;
13747 : 0 : if (gimplify_expr (&TREE_OPERAND (t, 2),
13748 : : pre_p, NULL,
13749 : : is_gimple_reg,
13750 : : fb_rvalue) == GS_ERROR)
13751 : 0 : remove = true;
13752 : : }
13753 : : }
13754 : 0 : else if (gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
13755 : : NULL, is_gimple_reg,
13756 : : fb_rvalue) == GS_ERROR)
13757 : 0 : remove = true;
13758 : : }
13759 : : }
13760 : 224 : for (; expr_stack.length () > 0; )
13761 : : {
13762 : 136 : tree t = expr_stack.pop ();
13763 : :
13764 : 136 : if (TREE_CODE (t) == ARRAY_REF
13765 : 136 : || TREE_CODE (t) == ARRAY_RANGE_REF)
13766 : : {
13767 : 56 : if (!is_gimple_min_invariant (TREE_OPERAND (t, 1))
13768 : 56 : && gimplify_expr (&TREE_OPERAND (t, 1), pre_p,
13769 : : NULL, is_gimple_val,
13770 : : fb_rvalue) == GS_ERROR)
13771 : 144 : remove = true;
13772 : : }
13773 : : }
13774 : 88 : }
13775 : : break;
13776 : : }
13777 : :
13778 : 23599 : if ((code == OMP_TARGET
13779 : : || code == OMP_TARGET_DATA
13780 : : || code == OMP_TARGET_ENTER_DATA
13781 : 14053 : || code == OMP_TARGET_EXIT_DATA)
13782 : 24157 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
13783 : : {
13784 : : /* If we have attach/detach but the decl we have is a pointer to
13785 : : pointer, we're probably mapping the "base level" array
13786 : : implicitly. Make sure we don't add the decl as if we mapped
13787 : : it explicitly. That is,
13788 : :
13789 : : int **arr;
13790 : : [...]
13791 : : #pragma omp target map(arr[a][b:c])
13792 : :
13793 : : should *not* map "arr" explicitly. That way we get a
13794 : : zero-length "alloc" mapping for it, and assuming it's been
13795 : : mapped by some previous directive, etc., things work as they
13796 : : should. */
13797 : :
13798 : 192 : tree basetype = TREE_TYPE (addr_tokens[0]->expr);
13799 : :
13800 : 192 : if (TREE_CODE (basetype) == REFERENCE_TYPE)
13801 : 34 : basetype = TREE_TYPE (basetype);
13802 : :
13803 : 192 : if (code == OMP_TARGET
13804 : 42 : && addr_tokens[0]->type == ARRAY_BASE
13805 : 42 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
13806 : 42 : && TREE_CODE (basetype) == POINTER_TYPE
13807 : 234 : && TREE_CODE (TREE_TYPE (basetype)) == POINTER_TYPE)
13808 : : break;
13809 : : }
13810 : :
13811 : 23589 : flags = GOVD_MAP | GOVD_EXPLICIT;
13812 : 23589 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
13813 : 23478 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM
13814 : 22974 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TO
13815 : 46537 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_PRESENT_TOFROM)
13816 : : flags |= GOVD_MAP_ALWAYS_TO;
13817 : :
13818 : 23589 : goto do_add;
13819 : :
13820 : 468 : case OMP_CLAUSE_AFFINITY:
13821 : 468 : gimplify_omp_affinity (list_p, pre_p);
13822 : 468 : remove = true;
13823 : 468 : break;
13824 : 8 : case OMP_CLAUSE_DOACROSS:
13825 : 8 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
13826 : : {
13827 : 4 : tree deps = OMP_CLAUSE_DECL (c);
13828 : 8 : while (deps && TREE_CODE (deps) == TREE_LIST)
13829 : : {
13830 : 4 : if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
13831 : 4 : && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
13832 : 0 : gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
13833 : : pre_p, NULL, is_gimple_val, fb_rvalue);
13834 : 4 : deps = TREE_CHAIN (deps);
13835 : : }
13836 : : }
13837 : : else
13838 : 4 : gcc_assert (OMP_CLAUSE_DOACROSS_KIND (c)
13839 : : == OMP_CLAUSE_DOACROSS_SOURCE);
13840 : : break;
13841 : 2200 : case OMP_CLAUSE_DEPEND:
13842 : 2200 : if (handled_depend_iterators == -1)
13843 : 1898 : handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
13844 : 2200 : if (handled_depend_iterators)
13845 : : {
13846 : 343 : if (handled_depend_iterators == 2)
13847 : 0 : remove = true;
13848 : : break;
13849 : : }
13850 : 1857 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
13851 : : {
13852 : 0 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
13853 : : NULL, is_gimple_val, fb_rvalue);
13854 : 0 : OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
13855 : : }
13856 : 1857 : if (error_operand_p (OMP_CLAUSE_DECL (c)))
13857 : : {
13858 : : remove = true;
13859 : : break;
13860 : : }
13861 : 1857 : if (OMP_CLAUSE_DECL (c) != null_pointer_node)
13862 : : {
13863 : 1826 : OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
13864 : 1826 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
13865 : : is_gimple_val, fb_rvalue) == GS_ERROR)
13866 : : {
13867 : : remove = true;
13868 : : break;
13869 : : }
13870 : : }
13871 : 1857 : if (code == OMP_TASK)
13872 : 1399 : ctx->has_depend = true;
13873 : : break;
13874 : :
13875 : 8188 : case OMP_CLAUSE_TO:
13876 : 8188 : case OMP_CLAUSE_FROM:
13877 : 8188 : case OMP_CLAUSE__CACHE_:
13878 : 8188 : decl = OMP_CLAUSE_DECL (c);
13879 : 8188 : if (error_operand_p (decl))
13880 : : {
13881 : : remove = true;
13882 : : break;
13883 : : }
13884 : 8188 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
13885 : 7429 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
13886 : 684 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
13887 : 8188 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
13888 : : NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
13889 : : {
13890 : : remove = true;
13891 : : break;
13892 : : }
13893 : 8188 : if (!DECL_P (decl))
13894 : : {
13895 : 2007 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
13896 : : NULL, is_gimple_lvalue, fb_lvalue)
13897 : : == GS_ERROR)
13898 : : {
13899 : 0 : remove = true;
13900 : : break;
13901 : : }
13902 : : break;
13903 : : }
13904 : 6181 : goto do_notice;
13905 : :
13906 : 125 : case OMP_CLAUSE__MAPPER_BINDING_:
13907 : 125 : {
13908 : 125 : tree name = OMP_CLAUSE__MAPPER_BINDING__ID (c);
13909 : 125 : tree var = OMP_CLAUSE__MAPPER_BINDING__DECL (c);
13910 : 125 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (var));
13911 : 125 : tree fndecl = OMP_CLAUSE__MAPPER_BINDING__MAPPER (c);
13912 : 125 : ctx->implicit_mappers->put ({ name, type }, fndecl);
13913 : 125 : remove = true;
13914 : 125 : break;
13915 : : }
13916 : :
13917 : 2118 : case OMP_CLAUSE_USE_DEVICE_PTR:
13918 : 2118 : case OMP_CLAUSE_USE_DEVICE_ADDR:
13919 : 2118 : flags = GOVD_EXPLICIT;
13920 : 2118 : goto do_add;
13921 : :
13922 : 543 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
13923 : 543 : decl = OMP_CLAUSE_DECL (c);
13924 : 543 : while (TREE_CODE (decl) == INDIRECT_REF
13925 : 592 : || TREE_CODE (decl) == ARRAY_REF)
13926 : 49 : decl = TREE_OPERAND (decl, 0);
13927 : 543 : flags = GOVD_EXPLICIT;
13928 : 543 : goto do_add_decl;
13929 : :
13930 : 498 : case OMP_CLAUSE_IS_DEVICE_PTR:
13931 : 498 : flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
13932 : 498 : goto do_add;
13933 : :
13934 : 61852 : do_add:
13935 : 61852 : decl = OMP_CLAUSE_DECL (c);
13936 : 82894 : do_add_decl:
13937 : 82894 : if (error_operand_p (decl))
13938 : : {
13939 : : remove = true;
13940 : : break;
13941 : : }
13942 : 82886 : if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
13943 : : {
13944 : 1756 : tree t = omp_member_access_dummy_var (decl);
13945 : 1756 : if (t)
13946 : : {
13947 : 662 : tree v = DECL_VALUE_EXPR (decl);
13948 : 662 : DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
13949 : 662 : if (outer_ctx)
13950 : 134 : omp_notice_variable (outer_ctx, t, true);
13951 : : }
13952 : : }
13953 : 82886 : if (code == OACC_DATA
13954 : 2403 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13955 : 85289 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13956 : 308 : flags |= GOVD_MAP_0LEN_ARRAY;
13957 : 82886 : omp_add_variable (ctx, decl, flags);
13958 : 82886 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13959 : 67596 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13960 : 65526 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13961 : 85486 : && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13962 : : {
13963 : 2087 : struct gimplify_omp_ctx *pctx
13964 : 2147 : = code == OMP_TARGET ? outer_ctx : ctx;
13965 : 2147 : if (pctx)
13966 : 2117 : omp_add_variable (pctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
13967 : : GOVD_LOCAL | GOVD_SEEN);
13968 : 2117 : if (pctx
13969 : 2117 : && OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
13970 : 632 : && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13971 : : find_decl_expr,
13972 : : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
13973 : : NULL) == NULL_TREE)
13974 : 208 : omp_add_variable (pctx,
13975 : 208 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
13976 : : GOVD_LOCAL | GOVD_SEEN);
13977 : 2147 : gimplify_omp_ctxp = pctx;
13978 : 2147 : push_gimplify_context ();
13979 : :
13980 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
13981 : 2147 : OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
13982 : :
13983 : 2147 : gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
13984 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
13985 : 2147 : pop_gimplify_context
13986 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
13987 : 2147 : push_gimplify_context ();
13988 : 4294 : gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
13989 : 2147 : &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
13990 : 2147 : pop_gimplify_context
13991 : 2147 : (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
13992 : 2147 : OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
13993 : 2147 : OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
13994 : :
13995 : 2147 : gimplify_omp_ctxp = outer_ctx;
13996 : : }
13997 : 80739 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
13998 : 80739 : && OMP_CLAUSE_LASTPRIVATE_STMT (c))
13999 : : {
14000 : 303 : gimplify_omp_ctxp = ctx;
14001 : 303 : push_gimplify_context ();
14002 : 303 : if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
14003 : : {
14004 : 303 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14005 : : NULL, NULL);
14006 : 303 : TREE_SIDE_EFFECTS (bind) = 1;
14007 : 303 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
14008 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
14009 : : }
14010 : 606 : gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
14011 : 303 : &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
14012 : 303 : pop_gimplify_context
14013 : 303 : (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
14014 : 303 : OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
14015 : :
14016 : 303 : gimplify_omp_ctxp = outer_ctx;
14017 : : }
14018 : 80436 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14019 : 80436 : && OMP_CLAUSE_LINEAR_STMT (c))
14020 : : {
14021 : 60 : gimplify_omp_ctxp = ctx;
14022 : 60 : push_gimplify_context ();
14023 : 60 : if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
14024 : : {
14025 : 60 : tree bind = build3 (BIND_EXPR, void_type_node, NULL,
14026 : : NULL, NULL);
14027 : 60 : TREE_SIDE_EFFECTS (bind) = 1;
14028 : 60 : BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
14029 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = bind;
14030 : : }
14031 : 120 : gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
14032 : 60 : &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
14033 : 60 : pop_gimplify_context
14034 : 60 : (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
14035 : 60 : OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
14036 : :
14037 : 60 : gimplify_omp_ctxp = outer_ctx;
14038 : : }
14039 : 82886 : if (notice_outer)
14040 : 70854 : goto do_notice;
14041 : : break;
14042 : :
14043 : 906 : case OMP_CLAUSE_COPYIN:
14044 : 906 : case OMP_CLAUSE_COPYPRIVATE:
14045 : 906 : decl = OMP_CLAUSE_DECL (c);
14046 : 906 : if (error_operand_p (decl))
14047 : : {
14048 : : remove = true;
14049 : : break;
14050 : : }
14051 : 906 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
14052 : : && !remove
14053 : 906 : && !omp_check_private (ctx, decl, true))
14054 : : {
14055 : 35 : remove = true;
14056 : 35 : if (is_global_var (decl))
14057 : : {
14058 : 30 : if (DECL_THREAD_LOCAL_P (decl))
14059 : : remove = false;
14060 : 10 : else if (DECL_HAS_VALUE_EXPR_P (decl))
14061 : : {
14062 : 2 : tree value = get_base_address (DECL_VALUE_EXPR (decl));
14063 : :
14064 : 2 : if (value
14065 : 2 : && DECL_P (value)
14066 : 4 : && DECL_THREAD_LOCAL_P (value))
14067 : : remove = false;
14068 : : }
14069 : : }
14070 : : if (remove)
14071 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
14072 : : "copyprivate variable %qE is not threadprivate"
14073 : 13 : " or private in outer context", DECL_NAME (decl));
14074 : : }
14075 : 77941 : do_notice:
14076 : 77941 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14077 : 62651 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14078 : 54818 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14079 : 30448 : && outer_ctx
14080 : 17558 : && ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
14081 : 16231 : || (region_type == ORT_WORKSHARE
14082 : 3884 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14083 : 1087 : && (OMP_CLAUSE_REDUCTION_INSCAN (c)
14084 : 912 : || code == OMP_LOOP)))
14085 : 79874 : && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
14086 : 845 : || (code == OMP_LOOP
14087 : 138 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14088 : 138 : && ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
14089 : : == ORT_COMBINED_TEAMS))))
14090 : : {
14091 : 1209 : splay_tree_node on
14092 : 1209 : = splay_tree_lookup (outer_ctx->variables,
14093 : : (splay_tree_key)decl);
14094 : 1209 : if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
14095 : : {
14096 : 891 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14097 : 705 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14098 : 1003 : && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
14099 : 56 : || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14100 : 0 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
14101 : : == POINTER_TYPE))))
14102 : 56 : omp_firstprivatize_variable (outer_ctx, decl);
14103 : : else
14104 : : {
14105 : 835 : omp_add_variable (outer_ctx, decl,
14106 : : GOVD_SEEN | GOVD_SHARED);
14107 : 835 : if (outer_ctx->outer_context)
14108 : 235 : omp_notice_variable (outer_ctx->outer_context, decl,
14109 : : true);
14110 : : }
14111 : : }
14112 : : }
14113 : 77341 : if (outer_ctx)
14114 : 29599 : omp_notice_variable (outer_ctx, decl, true);
14115 : 77941 : if (check_non_private
14116 : 26651 : && (region_type == ORT_WORKSHARE || code == OMP_SCOPE)
14117 : 4747 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14118 : 1641 : || decl == OMP_CLAUSE_DECL (c)
14119 : 224 : || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
14120 : 224 : && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14121 : : == ADDR_EXPR
14122 : 113 : || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
14123 : : == POINTER_PLUS_EXPR
14124 : 22 : && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
14125 : : (OMP_CLAUSE_DECL (c), 0), 0))
14126 : : == ADDR_EXPR)))))
14127 : 82586 : && omp_check_private (ctx, decl, false))
14128 : : {
14129 : 58 : error ("%s variable %qE is private in outer context",
14130 : 29 : check_non_private, DECL_NAME (decl));
14131 : 29 : remove = true;
14132 : : }
14133 : : break;
14134 : :
14135 : 190 : case OMP_CLAUSE_DETACH:
14136 : 190 : flags = GOVD_FIRSTPRIVATE | GOVD_SEEN;
14137 : 190 : goto do_add;
14138 : :
14139 : 4092 : case OMP_CLAUSE_IF:
14140 : 4092 : if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
14141 : 4092 : && OMP_CLAUSE_IF_MODIFIER (c) != code)
14142 : : {
14143 : : const char *p[2];
14144 : 168 : for (int i = 0; i < 2; i++)
14145 : 112 : switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
14146 : : {
14147 : 8 : case VOID_CST: p[i] = "cancel"; break;
14148 : 24 : case OMP_PARALLEL: p[i] = "parallel"; break;
14149 : 4 : case OMP_SIMD: p[i] = "simd"; break;
14150 : 12 : case OMP_TASK: p[i] = "task"; break;
14151 : 12 : case OMP_TASKLOOP: p[i] = "taskloop"; break;
14152 : 8 : case OMP_TARGET_DATA: p[i] = "target data"; break;
14153 : 12 : case OMP_TARGET: p[i] = "target"; break;
14154 : 12 : case OMP_TARGET_UPDATE: p[i] = "target update"; break;
14155 : 8 : case OMP_TARGET_ENTER_DATA:
14156 : 8 : p[i] = "target enter data"; break;
14157 : 12 : case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
14158 : 0 : default: gcc_unreachable ();
14159 : : }
14160 : 56 : error_at (OMP_CLAUSE_LOCATION (c),
14161 : : "expected %qs %<if%> clause modifier rather than %qs",
14162 : : p[0], p[1]);
14163 : 56 : remove = true;
14164 : : }
14165 : : /* Fall through. */
14166 : :
14167 : 4798 : case OMP_CLAUSE_SELF:
14168 : 4798 : case OMP_CLAUSE_FINAL:
14169 : 4798 : OMP_CLAUSE_OPERAND (c, 0)
14170 : 9596 : = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
14171 : : /* Fall through. */
14172 : :
14173 : 5718 : case OMP_CLAUSE_NUM_TEAMS:
14174 : 5718 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
14175 : 920 : && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14176 : 5963 : && !is_gimple_min_invariant (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14177 : : {
14178 : 208 : if (error_operand_p (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)))
14179 : : {
14180 : : remove = true;
14181 : : break;
14182 : : }
14183 : 208 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
14184 : 416 : = get_initialized_tmp_var (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c),
14185 : : pre_p, NULL, true);
14186 : : }
14187 : : /* Fall through. */
14188 : :
14189 : 21738 : case OMP_CLAUSE_SCHEDULE:
14190 : 21738 : case OMP_CLAUSE_NUM_THREADS:
14191 : 21738 : case OMP_CLAUSE_THREAD_LIMIT:
14192 : 21738 : case OMP_CLAUSE_DIST_SCHEDULE:
14193 : 21738 : case OMP_CLAUSE_DEVICE:
14194 : 21738 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14195 : 21738 : && OMP_CLAUSE_DEVICE_ANCESTOR (c))
14196 : : {
14197 : 125 : if (code != OMP_TARGET)
14198 : : {
14199 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
14200 : : "%<device%> clause with %<ancestor%> is only "
14201 : : "allowed on %<target%> construct");
14202 : 20 : remove = true;
14203 : 20 : break;
14204 : : }
14205 : :
14206 : 105 : tree clauses = *orig_list_p;
14207 : 330 : for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses))
14208 : 236 : if (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEVICE
14209 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_FIRSTPRIVATE
14210 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_PRIVATE
14211 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_DEFAULTMAP
14212 : : && OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_MAP
14213 : : )
14214 : : {
14215 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
14216 : : "with %<ancestor%>, only the %<device%>, "
14217 : : "%<firstprivate%>, %<private%>, %<defaultmap%>, "
14218 : : "and %<map%> clauses may appear on the "
14219 : : "construct");
14220 : 11 : remove = true;
14221 : 11 : break;
14222 : : }
14223 : : }
14224 : 21613 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE
14225 : 21613 : && code == OMP_DISPATCH)
14226 : : {
14227 : 272 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
14228 : 272 : gimplify_ctxp->into_ssa = false;
14229 : 272 : if (gimplify_expr (&OMP_CLAUSE_DEVICE_ID (c), pre_p, NULL,
14230 : : is_gimple_val, fb_rvalue)
14231 : : == GS_ERROR)
14232 : : remove = true;
14233 : 272 : else if (DECL_P (OMP_CLAUSE_DEVICE_ID (c)))
14234 : 55 : omp_add_variable (ctx, OMP_CLAUSE_DEVICE_ID (c),
14235 : : GOVD_SHARED | GOVD_SEEN);
14236 : 272 : gimplify_ctxp->into_ssa = saved_into_ssa;
14237 : 272 : break;
14238 : : }
14239 : : /* Fall through. */
14240 : :
14241 : 31151 : case OMP_CLAUSE_PRIORITY:
14242 : 31151 : case OMP_CLAUSE_GRAINSIZE:
14243 : 31151 : case OMP_CLAUSE_NUM_TASKS:
14244 : 31151 : case OMP_CLAUSE_FILTER:
14245 : 31151 : case OMP_CLAUSE_HINT:
14246 : 31151 : case OMP_CLAUSE_ASYNC:
14247 : 31151 : case OMP_CLAUSE_WAIT:
14248 : 31151 : case OMP_CLAUSE_NUM_GANGS:
14249 : 31151 : case OMP_CLAUSE_NUM_WORKERS:
14250 : 31151 : case OMP_CLAUSE_VECTOR_LENGTH:
14251 : 31151 : case OMP_CLAUSE_WORKER:
14252 : 31151 : case OMP_CLAUSE_VECTOR:
14253 : 31151 : if (OMP_CLAUSE_OPERAND (c, 0)
14254 : 31151 : && !is_gimple_min_invariant (OMP_CLAUSE_OPERAND (c, 0)))
14255 : : {
14256 : 7711 : if (error_operand_p (OMP_CLAUSE_OPERAND (c, 0)))
14257 : : {
14258 : : remove = true;
14259 : : break;
14260 : : }
14261 : : /* All these clauses care about value, not a particular decl,
14262 : : so try to force it into a SSA_NAME or fresh temporary. */
14263 : 7704 : OMP_CLAUSE_OPERAND (c, 0)
14264 : 15408 : = get_initialized_tmp_var (OMP_CLAUSE_OPERAND (c, 0),
14265 : : pre_p, NULL, true);
14266 : : }
14267 : : break;
14268 : :
14269 : 2331 : case OMP_CLAUSE_GANG:
14270 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
14271 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14272 : 0 : remove = true;
14273 : 2331 : if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
14274 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14275 : 0 : remove = true;
14276 : : break;
14277 : :
14278 : : case OMP_CLAUSE_NOWAIT:
14279 : 200301 : nowait = 1;
14280 : : break;
14281 : :
14282 : : case OMP_CLAUSE_ORDERED:
14283 : : case OMP_CLAUSE_UNTIED:
14284 : : case OMP_CLAUSE_COLLAPSE:
14285 : : case OMP_CLAUSE_TILE:
14286 : : case OMP_CLAUSE_AUTO:
14287 : : case OMP_CLAUSE_SEQ:
14288 : : case OMP_CLAUSE_INDEPENDENT:
14289 : : case OMP_CLAUSE_MERGEABLE:
14290 : : case OMP_CLAUSE_PROC_BIND:
14291 : : case OMP_CLAUSE_SAFELEN:
14292 : : case OMP_CLAUSE_SIMDLEN:
14293 : : case OMP_CLAUSE_NOGROUP:
14294 : : case OMP_CLAUSE_THREADS:
14295 : : case OMP_CLAUSE_SIMD:
14296 : : case OMP_CLAUSE_BIND:
14297 : : case OMP_CLAUSE_IF_PRESENT:
14298 : : case OMP_CLAUSE_FINALIZE:
14299 : : case OMP_CLAUSE_INTEROP:
14300 : : case OMP_CLAUSE_INIT:
14301 : : case OMP_CLAUSE_USE:
14302 : : case OMP_CLAUSE_DESTROY:
14303 : : break;
14304 : :
14305 : 3969 : case OMP_CLAUSE_ORDER:
14306 : 3969 : ctx->order_concurrent = true;
14307 : 3969 : break;
14308 : :
14309 : 1014 : case OMP_CLAUSE_DEFAULTMAP:
14310 : 1014 : enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
14311 : 1014 : switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
14312 : : {
14313 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
14314 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
14315 : : gdmkmin = GDMK_SCALAR;
14316 : : gdmkmax = GDMK_POINTER;
14317 : : break;
14318 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14319 : : gdmkmin = GDMK_SCALAR;
14320 : : gdmkmax = GDMK_SCALAR_TARGET;
14321 : : break;
14322 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14323 : : gdmkmin = gdmkmax = GDMK_AGGREGATE;
14324 : : break;
14325 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
14326 : : gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
14327 : : break;
14328 : : case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14329 : : gdmkmin = gdmkmax = GDMK_POINTER;
14330 : : break;
14331 : 0 : default:
14332 : 0 : gcc_unreachable ();
14333 : : }
14334 : 4487 : for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
14335 : 3473 : switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
14336 : : {
14337 : 91 : case OMP_CLAUSE_DEFAULTMAP_ALLOC:
14338 : 91 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
14339 : 91 : break;
14340 : 121 : case OMP_CLAUSE_DEFAULTMAP_TO:
14341 : 121 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
14342 : 121 : break;
14343 : 28 : case OMP_CLAUSE_DEFAULTMAP_FROM:
14344 : 28 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
14345 : 28 : break;
14346 : 671 : case OMP_CLAUSE_DEFAULTMAP_TOFROM:
14347 : 671 : ctx->defaultmap[gdmk] = GOVD_MAP;
14348 : 671 : break;
14349 : 380 : case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
14350 : 380 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14351 : 380 : break;
14352 : 2079 : case OMP_CLAUSE_DEFAULTMAP_NONE:
14353 : 2079 : ctx->defaultmap[gdmk] = 0;
14354 : 2079 : break;
14355 : 45 : case OMP_CLAUSE_DEFAULTMAP_PRESENT:
14356 : 45 : ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
14357 : 45 : break;
14358 : 58 : case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
14359 : 58 : switch (gdmk)
14360 : : {
14361 : 11 : case GDMK_SCALAR:
14362 : 11 : ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
14363 : 11 : break;
14364 : 11 : case GDMK_SCALAR_TARGET:
14365 : 11 : ctx->defaultmap[gdmk] = (lang_GNU_Fortran ()
14366 : 11 : ? GOVD_MAP : GOVD_FIRSTPRIVATE);
14367 : 11 : break;
14368 : 14 : case GDMK_AGGREGATE:
14369 : 14 : case GDMK_ALLOCATABLE:
14370 : 14 : ctx->defaultmap[gdmk] = GOVD_MAP;
14371 : 14 : break;
14372 : 22 : case GDMK_POINTER:
14373 : 22 : ctx->defaultmap[gdmk] = GOVD_MAP;
14374 : 22 : if (!lang_GNU_Fortran ())
14375 : 14 : ctx->defaultmap[gdmk] |= GOVD_MAP_0LEN_ARRAY;
14376 : : break;
14377 : : default:
14378 : : gcc_unreachable ();
14379 : : }
14380 : : break;
14381 : 0 : default:
14382 : 0 : gcc_unreachable ();
14383 : : }
14384 : : break;
14385 : :
14386 : 824 : case OMP_CLAUSE_ALIGNED:
14387 : 824 : decl = OMP_CLAUSE_DECL (c);
14388 : 824 : if (error_operand_p (decl))
14389 : : {
14390 : : remove = true;
14391 : : break;
14392 : : }
14393 : 824 : if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
14394 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14395 : : {
14396 : : remove = true;
14397 : : break;
14398 : : }
14399 : 824 : if (!is_global_var (decl)
14400 : 824 : && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14401 : 592 : omp_add_variable (ctx, decl, GOVD_ALIGNED);
14402 : : break;
14403 : :
14404 : 489 : case OMP_CLAUSE_NONTEMPORAL:
14405 : 489 : decl = OMP_CLAUSE_DECL (c);
14406 : 489 : if (error_operand_p (decl))
14407 : : {
14408 : : remove = true;
14409 : : break;
14410 : : }
14411 : 489 : omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
14412 : 489 : break;
14413 : :
14414 : 3541 : case OMP_CLAUSE_ALLOCATE:
14415 : 3541 : decl = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
14416 : 3541 : if (decl
14417 : 1580 : && TREE_CODE (decl) == INTEGER_CST
14418 : 3570 : && wi::eq_p (wi::to_widest (decl), GOMP_OMP_PREDEF_ALLOC_THREADS)
14419 : 3570 : && (code == OMP_TARGET || code == OMP_TASK || code == OMP_TASKLOOP))
14420 : 35 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
14421 : : "allocator with access trait set to %<thread%> "
14422 : : "results in undefined behavior for %qs directive",
14423 : : code == OMP_TARGET ? "target"
14424 : : : (code == OMP_TASK
14425 : 15 : ? "task" : "taskloop"));
14426 : 3541 : decl = OMP_CLAUSE_DECL (c);
14427 : 3541 : if (error_operand_p (decl))
14428 : : {
14429 : : remove = true;
14430 : : break;
14431 : : }
14432 : 3541 : if (gimplify_expr (&OMP_CLAUSE_ALLOCATE_ALLOCATOR (c), pre_p, NULL,
14433 : : is_gimple_val, fb_rvalue) == GS_ERROR)
14434 : : {
14435 : : remove = true;
14436 : : break;
14437 : : }
14438 : 3541 : else if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) == NULL_TREE
14439 : 3541 : || (TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c))
14440 : : == INTEGER_CST))
14441 : : ;
14442 : 491 : else if (code == OMP_TASKLOOP
14443 : 491 : || !DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
14444 : 66 : OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
14445 : 132 : = get_initialized_tmp_var (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
14446 : : pre_p, NULL, false);
14447 : : break;
14448 : :
14449 : 4349 : case OMP_CLAUSE_DEFAULT:
14450 : 4349 : ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
14451 : 4349 : break;
14452 : :
14453 : 640 : case OMP_CLAUSE_INCLUSIVE:
14454 : 640 : case OMP_CLAUSE_EXCLUSIVE:
14455 : 640 : decl = OMP_CLAUSE_DECL (c);
14456 : 640 : {
14457 : 640 : splay_tree_node n = splay_tree_lookup (outer_ctx->variables,
14458 : : (splay_tree_key) decl);
14459 : 640 : if (n == NULL || (n->value & GOVD_REDUCTION) == 0)
14460 : : {
14461 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
14462 : : "%qD specified in %qs clause but not in %<inscan%> "
14463 : : "%<reduction%> clause on the containing construct",
14464 : 5 : decl, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14465 : 5 : remove = true;
14466 : : }
14467 : : else
14468 : : {
14469 : 635 : n->value |= GOVD_REDUCTION_INSCAN;
14470 : 635 : if (outer_ctx->region_type == ORT_SIMD
14471 : 520 : && outer_ctx->outer_context
14472 : 107 : && outer_ctx->outer_context->region_type == ORT_WORKSHARE)
14473 : : {
14474 : 107 : n = splay_tree_lookup (outer_ctx->outer_context->variables,
14475 : : (splay_tree_key) decl);
14476 : 107 : if (n && (n->value & GOVD_REDUCTION) != 0)
14477 : 107 : n->value |= GOVD_REDUCTION_INSCAN;
14478 : : }
14479 : : }
14480 : : }
14481 : : break;
14482 : :
14483 : 97 : case OMP_CLAUSE_NOVARIANTS:
14484 : 97 : OMP_CLAUSE_NOVARIANTS_EXPR (c)
14485 : 97 : = gimple_boolify (OMP_CLAUSE_NOVARIANTS_EXPR (c));
14486 : 97 : break;
14487 : 115 : case OMP_CLAUSE_NOCONTEXT:
14488 : 115 : OMP_CLAUSE_NOCONTEXT_EXPR (c)
14489 : 115 : = gimple_boolify (OMP_CLAUSE_NOCONTEXT_EXPR (c));
14490 : 115 : break;
14491 : 0 : case OMP_CLAUSE_NOHOST:
14492 : 0 : default:
14493 : 0 : gcc_unreachable ();
14494 : : }
14495 : :
14496 : 1399 : if (code == OACC_DATA
14497 : 5127 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14498 : 205116 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
14499 : 4507 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
14500 : : remove = true;
14501 : 201392 : if (remove)
14502 : 1066 : *list_p = OMP_CLAUSE_CHAIN (c);
14503 : : else
14504 : 200634 : list_p = &OMP_CLAUSE_CHAIN (c);
14505 : 201700 : }
14506 : :
14507 : 129329 : if (groups)
14508 : : {
14509 : 34558 : delete grpmap;
14510 : 17279 : delete groups;
14511 : : }
14512 : :
14513 : 129329 : ctx->clauses = *orig_list_p;
14514 : 129329 : gimplify_omp_ctxp = ctx;
14515 : 129329 : }
14516 : :
14517 : : /* Return true if DECL is a candidate for shared to firstprivate
14518 : : optimization. We only consider non-addressable scalars, not
14519 : : too big, and not references. */
14520 : :
14521 : : static bool
14522 : 403666 : omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
14523 : : {
14524 : 403666 : if (TREE_ADDRESSABLE (decl))
14525 : : return false;
14526 : 355207 : tree type = TREE_TYPE (decl);
14527 : 355207 : if (!is_gimple_reg_type (type)
14528 : 330683 : || TREE_CODE (type) == REFERENCE_TYPE
14529 : 680311 : || TREE_ADDRESSABLE (type))
14530 : : return false;
14531 : : /* Don't optimize too large decls, as each thread/task will have
14532 : : its own. */
14533 : 325104 : HOST_WIDE_INT len = int_size_in_bytes (type);
14534 : 325104 : if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
14535 : : return false;
14536 : 325087 : if (omp_privatize_by_reference (decl))
14537 : : return false;
14538 : : return true;
14539 : : }
14540 : :
14541 : : /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
14542 : : For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
14543 : : GOVD_WRITTEN in outer contexts. */
14544 : :
14545 : : static void
14546 : 302259 : omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
14547 : : {
14548 : 464067 : for (; ctx; ctx = ctx->outer_context)
14549 : : {
14550 : 447096 : splay_tree_node n = splay_tree_lookup (ctx->variables,
14551 : : (splay_tree_key) decl);
14552 : 447096 : if (n == NULL)
14553 : 158232 : continue;
14554 : 288864 : else if (n->value & GOVD_SHARED)
14555 : : {
14556 : 9093 : n->value |= GOVD_WRITTEN;
14557 : 9093 : return;
14558 : : }
14559 : 279771 : else if (n->value & GOVD_DATA_SHARE_CLASS)
14560 : : return;
14561 : : }
14562 : : }
14563 : :
14564 : : /* Helper callback for walk_gimple_seq to discover possible stores
14565 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
14566 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
14567 : : for those. */
14568 : :
14569 : : static tree
14570 : 1121726 : omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
14571 : : {
14572 : 1121726 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
14573 : :
14574 : 1121726 : *walk_subtrees = 0;
14575 : 1121726 : if (!wi->is_lhs)
14576 : : return NULL_TREE;
14577 : :
14578 : 320788 : tree op = *tp;
14579 : 398985 : do
14580 : : {
14581 : 398985 : if (handled_component_p (op))
14582 : 78197 : op = TREE_OPERAND (op, 0);
14583 : 320788 : else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
14584 : 320788 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
14585 : 0 : op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
14586 : : else
14587 : : break;
14588 : : }
14589 : : while (1);
14590 : 320788 : if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
14591 : 58804 : return NULL_TREE;
14592 : :
14593 : 261984 : omp_mark_stores (gimplify_omp_ctxp, op);
14594 : 261984 : return NULL_TREE;
14595 : : }
14596 : :
14597 : : /* Helper callback for walk_gimple_seq to discover possible stores
14598 : : to omp_shared_to_firstprivate_optimizable_decl_p decls and set
14599 : : GOVD_WRITTEN if they are GOVD_SHARED in some outer context
14600 : : for those. */
14601 : :
14602 : : static tree
14603 : 624089 : omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
14604 : : bool *handled_ops_p,
14605 : : struct walk_stmt_info *wi)
14606 : : {
14607 : 624089 : gimple *stmt = gsi_stmt (*gsi_p);
14608 : 624089 : switch (gimple_code (stmt))
14609 : : {
14610 : : /* Don't recurse on OpenMP constructs for which
14611 : : gimplify_adjust_omp_clauses already handled the bodies,
14612 : : except handle gimple_omp_for_pre_body. */
14613 : 26729 : case GIMPLE_OMP_FOR:
14614 : 26729 : *handled_ops_p = true;
14615 : 26729 : if (gimple_omp_for_pre_body (stmt))
14616 : 1386 : walk_gimple_seq (gimple_omp_for_pre_body (stmt),
14617 : : omp_find_stores_stmt, omp_find_stores_op, wi);
14618 : : break;
14619 : 8585 : case GIMPLE_OMP_PARALLEL:
14620 : 8585 : case GIMPLE_OMP_TASK:
14621 : 8585 : case GIMPLE_OMP_SECTIONS:
14622 : 8585 : case GIMPLE_OMP_SINGLE:
14623 : 8585 : case GIMPLE_OMP_SCOPE:
14624 : 8585 : case GIMPLE_OMP_TARGET:
14625 : 8585 : case GIMPLE_OMP_TEAMS:
14626 : 8585 : case GIMPLE_OMP_CRITICAL:
14627 : 8585 : *handled_ops_p = true;
14628 : 8585 : break;
14629 : : default:
14630 : : break;
14631 : : }
14632 : 624089 : return NULL_TREE;
14633 : : }
14634 : :
14635 : : struct gimplify_adjust_omp_clauses_data
14636 : : {
14637 : : tree *list_p;
14638 : : gimple_seq *pre_p;
14639 : : };
14640 : :
14641 : : /* For all variables that were not actually used within the context,
14642 : : remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
14643 : :
14644 : : static int
14645 : 661468 : gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
14646 : : {
14647 : 661468 : tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
14648 : 661468 : gimple_seq *pre_p
14649 : : = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
14650 : 661468 : tree decl = (tree) n->key;
14651 : 661468 : unsigned flags = n->value;
14652 : 661468 : enum omp_clause_code code;
14653 : 661468 : tree clause;
14654 : 661468 : bool private_debug;
14655 : :
14656 : 661468 : if (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
14657 : 132747 : && (flags & GOVD_LASTPRIVATE_CONDITIONAL) != 0)
14658 : : flags = GOVD_SHARED | GOVD_SEEN | GOVD_WRITTEN;
14659 : 661343 : if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
14660 : : return 0;
14661 : 177654 : if ((flags & GOVD_SEEN) == 0)
14662 : : return 0;
14663 : 156888 : if (flags & GOVD_DEBUG_PRIVATE)
14664 : : {
14665 : 256 : gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
14666 : : private_debug = true;
14667 : : }
14668 : 156632 : else if (flags & GOVD_MAP)
14669 : : private_debug = false;
14670 : : else
14671 : 138599 : private_debug
14672 : 138599 : = lang_hooks.decls.omp_private_debug_clause (decl,
14673 : 138599 : !!(flags & GOVD_SHARED));
14674 : 138599 : if (private_debug)
14675 : : code = OMP_CLAUSE_PRIVATE;
14676 : 156510 : else if (flags & GOVD_MAP)
14677 : : {
14678 : 18033 : code = OMP_CLAUSE_MAP;
14679 : 18033 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
14680 : 18033 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
14681 : : {
14682 : 2 : error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
14683 : 2 : return 0;
14684 : : }
14685 : 18031 : if (VAR_P (decl)
14686 : 16095 : && DECL_IN_CONSTANT_POOL (decl)
14687 : 18032 : && !lookup_attribute ("omp declare target",
14688 : 1 : DECL_ATTRIBUTES (decl)))
14689 : : {
14690 : 1 : tree id = get_identifier ("omp declare target");
14691 : 1 : DECL_ATTRIBUTES (decl)
14692 : 1 : = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14693 : 1 : varpool_node *node = varpool_node::get (decl);
14694 : 1 : if (node)
14695 : : {
14696 : 1 : node->offloadable = 1;
14697 : 1 : if (ENABLE_OFFLOADING)
14698 : : g->have_offload = true;
14699 : : }
14700 : : }
14701 : : }
14702 : 138477 : else if (flags & GOVD_SHARED)
14703 : : {
14704 : 49006 : if (is_global_var (decl))
14705 : : {
14706 : 16106 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
14707 : 25286 : while (ctx != NULL)
14708 : : {
14709 : 17593 : splay_tree_node on
14710 : 17593 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
14711 : 17593 : if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
14712 : : | GOVD_PRIVATE | GOVD_REDUCTION
14713 : : | GOVD_LINEAR | GOVD_MAP)) != 0)
14714 : : break;
14715 : 9180 : ctx = ctx->outer_context;
14716 : : }
14717 : 16106 : if (ctx == NULL)
14718 : : return 0;
14719 : : }
14720 : 41313 : code = OMP_CLAUSE_SHARED;
14721 : : /* Don't optimize shared into firstprivate for read-only vars
14722 : : on tasks with depend clause, we shouldn't try to copy them
14723 : : until the dependencies are satisfied. */
14724 : 41313 : if (gimplify_omp_ctxp->has_depend)
14725 : 350 : flags |= GOVD_WRITTEN;
14726 : : }
14727 : 89471 : else if (flags & GOVD_PRIVATE)
14728 : : code = OMP_CLAUSE_PRIVATE;
14729 : 31036 : else if (flags & GOVD_FIRSTPRIVATE)
14730 : : {
14731 : 21636 : code = OMP_CLAUSE_FIRSTPRIVATE;
14732 : 21636 : if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
14733 : 13637 : && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
14734 : 31594 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
14735 : : {
14736 : 1 : error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
14737 : : "%<target%> construct", decl);
14738 : 1 : return 0;
14739 : : }
14740 : : }
14741 : 9400 : else if (flags & GOVD_LASTPRIVATE)
14742 : : code = OMP_CLAUSE_LASTPRIVATE;
14743 : 241 : else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
14744 : : return 0;
14745 : 118 : else if (flags & GOVD_CONDTEMP)
14746 : : {
14747 : 118 : code = OMP_CLAUSE__CONDTEMP_;
14748 : 118 : gimple_add_tmp_var (decl);
14749 : : }
14750 : : else
14751 : 0 : gcc_unreachable ();
14752 : :
14753 : 139910 : if (((flags & GOVD_LASTPRIVATE)
14754 : 139220 : || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
14755 : 155115 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14756 : 15191 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
14757 : :
14758 : 149069 : tree chain = *list_p;
14759 : 149069 : clause = build_omp_clause (input_location, code);
14760 : 149069 : OMP_CLAUSE_DECL (clause) = decl;
14761 : 149069 : OMP_CLAUSE_CHAIN (clause) = chain;
14762 : 149069 : if (private_debug)
14763 : 378 : OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
14764 : 148691 : else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
14765 : 6 : OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
14766 : 148685 : else if (code == OMP_CLAUSE_SHARED
14767 : 41313 : && (flags & GOVD_WRITTEN) == 0
14768 : 183952 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
14769 : 20983 : OMP_CLAUSE_SHARED_READONLY (clause) = 1;
14770 : 127702 : else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
14771 : 21635 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
14772 : 106067 : else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
14773 : : {
14774 : 644 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
14775 : 644 : OMP_CLAUSE_DECL (nc) = decl;
14776 : 644 : if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
14777 : 644 : && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
14778 : 15 : OMP_CLAUSE_DECL (clause)
14779 : 30 : = build_fold_indirect_ref_loc (input_location, decl);
14780 : 644 : OMP_CLAUSE_DECL (clause)
14781 : 644 : = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
14782 : : build_int_cst (build_pointer_type (char_type_node), 0));
14783 : 644 : OMP_CLAUSE_SIZE (clause) = size_zero_node;
14784 : 644 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
14785 : 644 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
14786 : 644 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
14787 : 644 : tree dtype = TREE_TYPE (decl);
14788 : 644 : if (TREE_CODE (dtype) == REFERENCE_TYPE)
14789 : 15 : dtype = TREE_TYPE (dtype);
14790 : : /* FIRSTPRIVATE_POINTER doesn't work well if we have a
14791 : : multiply-indirected pointer. If we have a reference to a pointer to
14792 : : a pointer, it's possible that this should really be
14793 : : GOMP_MAP_FIRSTPRIVATE_REFERENCE -- but that also doesn't work at the
14794 : : moment, so stick with this. (See PR113279 and testcases
14795 : : baseptrs-{4,6}.C:ref2ptrptr_offset_decl_member_slice). */
14796 : 644 : if (TREE_CODE (dtype) == POINTER_TYPE
14797 : 644 : && TREE_CODE (TREE_TYPE (dtype)) == POINTER_TYPE)
14798 : 19 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
14799 : : else
14800 : 625 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
14801 : 644 : OMP_CLAUSE_CHAIN (nc) = chain;
14802 : 644 : OMP_CLAUSE_CHAIN (clause) = nc;
14803 : 644 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14804 : 644 : gimplify_omp_ctxp = ctx->outer_context;
14805 : 644 : gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
14806 : : pre_p, NULL, is_gimple_val, fb_rvalue);
14807 : 644 : gimplify_omp_ctxp = ctx;
14808 : 644 : }
14809 : 17387 : else if (code == OMP_CLAUSE_MAP)
14810 : : {
14811 : 17387 : int kind;
14812 : : /* Not all combinations of these GOVD_MAP flags are actually valid. */
14813 : 17387 : switch (flags & (GOVD_MAP_TO_ONLY
14814 : : | GOVD_MAP_FORCE
14815 : : | GOVD_MAP_FORCE_PRESENT
14816 : : | GOVD_MAP_ALLOC_ONLY
14817 : : | GOVD_MAP_FROM_ONLY))
14818 : : {
14819 : : case 0:
14820 : : kind = GOMP_MAP_TOFROM;
14821 : : break;
14822 : 1062 : case GOVD_MAP_FORCE:
14823 : 1062 : kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
14824 : 1062 : break;
14825 : 868 : case GOVD_MAP_TO_ONLY:
14826 : 868 : kind = GOMP_MAP_TO;
14827 : 868 : break;
14828 : 16 : case GOVD_MAP_FROM_ONLY:
14829 : 16 : kind = GOMP_MAP_FROM;
14830 : 16 : break;
14831 : 37 : case GOVD_MAP_ALLOC_ONLY:
14832 : 37 : kind = GOMP_MAP_ALLOC;
14833 : 37 : break;
14834 : 0 : case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
14835 : 0 : kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
14836 : 0 : break;
14837 : : case GOVD_MAP_FORCE_PRESENT:
14838 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
14839 : : break;
14840 : : case GOVD_MAP_FORCE_PRESENT | GOVD_MAP_ALLOC_ONLY:
14841 : 326 : kind = GOMP_MAP_FORCE_PRESENT;
14842 : : break;
14843 : 0 : default:
14844 : 0 : gcc_unreachable ();
14845 : : }
14846 : 17387 : OMP_CLAUSE_SET_MAP_KIND (clause, kind);
14847 : : /* Setting of the implicit flag for the runtime is currently disabled for
14848 : : OpenACC. */
14849 : 17387 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
14850 : 9834 : OMP_CLAUSE_MAP_RUNTIME_IMPLICIT_P (clause) = 1;
14851 : 17387 : if (DECL_SIZE (decl)
14852 : 17387 : && !poly_int_tree_p (DECL_SIZE (decl)))
14853 : : {
14854 : 535 : tree decl2 = DECL_VALUE_EXPR (decl);
14855 : 535 : gcc_assert (INDIRECT_REF_P (decl2));
14856 : 535 : decl2 = TREE_OPERAND (decl2, 0);
14857 : 535 : gcc_assert (DECL_P (decl2));
14858 : 535 : tree mem = build_simple_mem_ref (decl2);
14859 : 535 : OMP_CLAUSE_DECL (clause) = mem;
14860 : 535 : OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
14861 : 535 : if (gimplify_omp_ctxp->outer_context)
14862 : : {
14863 : 446 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
14864 : 446 : omp_notice_variable (ctx, decl2, true);
14865 : 446 : omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
14866 : : }
14867 : 535 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
14868 : : OMP_CLAUSE_MAP);
14869 : 535 : OMP_CLAUSE_DECL (nc) = decl;
14870 : 535 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
14871 : 535 : if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
14872 : 535 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
14873 : : else
14874 : 0 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
14875 : 535 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
14876 : 535 : OMP_CLAUSE_CHAIN (clause) = nc;
14877 : : }
14878 : 16852 : else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
14879 : 16852 : && omp_privatize_by_reference (decl))
14880 : : {
14881 : 29 : OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
14882 : 29 : OMP_CLAUSE_SIZE (clause)
14883 : 29 : = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
14884 : 29 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14885 : 29 : gimplify_omp_ctxp = ctx->outer_context;
14886 : 29 : gimplify_expr (&OMP_CLAUSE_SIZE (clause),
14887 : : pre_p, NULL, is_gimple_val, fb_rvalue);
14888 : 29 : gimplify_omp_ctxp = ctx;
14889 : 29 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
14890 : : OMP_CLAUSE_MAP);
14891 : 29 : OMP_CLAUSE_DECL (nc) = decl;
14892 : 29 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
14893 : 29 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
14894 : 29 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
14895 : 29 : OMP_CLAUSE_CHAIN (clause) = nc;
14896 : : }
14897 : : else
14898 : 16823 : OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
14899 : : }
14900 : 149069 : if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
14901 : : {
14902 : 690 : tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
14903 : 690 : OMP_CLAUSE_DECL (nc) = decl;
14904 : 690 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
14905 : 690 : OMP_CLAUSE_CHAIN (nc) = chain;
14906 : 690 : OMP_CLAUSE_CHAIN (clause) = nc;
14907 : 690 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14908 : 690 : gimplify_omp_ctxp = ctx->outer_context;
14909 : 690 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
14910 : 690 : (ctx->region_type & ORT_ACC) != 0);
14911 : 690 : gimplify_omp_ctxp = ctx;
14912 : : }
14913 : 149069 : *list_p = clause;
14914 : 149069 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14915 : 149069 : gimplify_omp_ctxp = ctx->outer_context;
14916 : : /* Don't call omp_finish_clause on implicitly added OMP_CLAUSE_PRIVATE
14917 : : in simd. Those are only added for the local vars inside of simd body
14918 : : and they don't need to be e.g. default constructible. */
14919 : 149069 : if (code != OMP_CLAUSE_PRIVATE || ctx->region_type != ORT_SIMD)
14920 : 141051 : lang_hooks.decls.omp_finish_clause (clause, pre_p,
14921 : 141051 : (ctx->region_type & ORT_ACC) != 0);
14922 : 149069 : if (gimplify_omp_ctxp)
14923 : 184127 : for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
14924 : 93478 : if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
14925 : 93478 : && DECL_P (OMP_CLAUSE_SIZE (clause)))
14926 : 1243 : omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
14927 : : true);
14928 : 149069 : gimplify_omp_ctxp = ctx;
14929 : 149069 : return 0;
14930 : : }
14931 : :
14932 : : static void
14933 : 127872 : gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
14934 : : enum tree_code code)
14935 : : {
14936 : 127872 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
14937 : 127872 : tree *orig_list_p = list_p;
14938 : 127872 : tree c, decl;
14939 : 127872 : bool has_inscan_reductions = false;
14940 : :
14941 : 127872 : if (body)
14942 : : {
14943 : : struct gimplify_omp_ctx *octx;
14944 : 223205 : for (octx = ctx; octx; octx = octx->outer_context)
14945 : 172873 : if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
14946 : : break;
14947 : 110708 : if (octx)
14948 : : {
14949 : 60376 : struct walk_stmt_info wi;
14950 : 60376 : memset (&wi, 0, sizeof (wi));
14951 : 60376 : walk_gimple_seq (body, omp_find_stores_stmt,
14952 : : omp_find_stores_op, &wi);
14953 : : }
14954 : : }
14955 : :
14956 : 127872 : if (ctx->add_safelen1)
14957 : : {
14958 : : /* If there are VLAs in the body of simd loop, prevent
14959 : : vectorization. */
14960 : 2 : gcc_assert (ctx->region_type == ORT_SIMD);
14961 : 2 : c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
14962 : 2 : OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
14963 : 2 : OMP_CLAUSE_CHAIN (c) = *list_p;
14964 : 2 : *list_p = c;
14965 : 2 : list_p = &OMP_CLAUSE_CHAIN (c);
14966 : : }
14967 : :
14968 : 127872 : if (ctx->region_type == ORT_WORKSHARE
14969 : 39266 : && ctx->outer_context
14970 : 27894 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
14971 : : {
14972 : 24951 : for (c = ctx->outer_context->clauses; c; c = OMP_CLAUSE_CHAIN (c))
14973 : 12585 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
14974 : 12585 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
14975 : : {
14976 : 125 : decl = OMP_CLAUSE_DECL (c);
14977 : 125 : splay_tree_node n
14978 : 125 : = splay_tree_lookup (ctx->outer_context->variables,
14979 : : (splay_tree_key) decl);
14980 : 125 : gcc_checking_assert (!splay_tree_lookup (ctx->variables,
14981 : : (splay_tree_key) decl));
14982 : 125 : omp_add_variable (ctx, decl, n->value);
14983 : 125 : tree c2 = copy_node (c);
14984 : 125 : OMP_CLAUSE_CHAIN (c2) = *list_p;
14985 : 125 : *list_p = c2;
14986 : 125 : if ((n->value & GOVD_FIRSTPRIVATE) == 0)
14987 : 103 : continue;
14988 : 22 : c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
14989 : : OMP_CLAUSE_FIRSTPRIVATE);
14990 : 22 : OMP_CLAUSE_DECL (c2) = decl;
14991 : 22 : OMP_CLAUSE_CHAIN (c2) = *list_p;
14992 : 22 : *list_p = c2;
14993 : : }
14994 : : }
14995 : :
14996 : 127872 : if (code == OMP_TARGET
14997 : 127872 : || code == OMP_TARGET_DATA
14998 : 127872 : || code == OMP_TARGET_ENTER_DATA
14999 : 112703 : || code == OMP_TARGET_EXIT_DATA)
15000 : : {
15001 : 15817 : tree mapper_clauses = NULL_TREE;
15002 : 15817 : instantiate_mapper_info im_info;
15003 : :
15004 : 15817 : im_info.mapper_clauses_p = &mapper_clauses;
15005 : 15817 : im_info.omp_ctx = ctx;
15006 : 15817 : im_info.pre_p = pre_p;
15007 : :
15008 : 15817 : splay_tree_foreach (ctx->variables,
15009 : : omp_instantiate_implicit_mappers,
15010 : : (void *) &im_info);
15011 : :
15012 : 15817 : if (mapper_clauses)
15013 : : {
15014 : 45 : mapper_clauses
15015 : 45 : = lang_hooks.decls.omp_finish_mapper_clauses (mapper_clauses);
15016 : :
15017 : : /* Stick the implicitly-expanded mapper clauses at the end of the
15018 : : clause list. */
15019 : 45 : tree *tail = list_p;
15020 : 139 : while (*tail)
15021 : 94 : tail = &OMP_CLAUSE_CHAIN (*tail);
15022 : 45 : *tail = mapper_clauses;
15023 : : }
15024 : :
15025 : 15817 : vec<omp_mapping_group> *groups;
15026 : 15817 : groups = omp_gather_mapping_groups (list_p);
15027 : 15817 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap = NULL;
15028 : :
15029 : 15817 : if (groups)
15030 : : {
15031 : 7649 : grpmap = omp_index_mapping_groups (groups);
15032 : :
15033 : 7649 : omp_resolve_clause_dependencies (code, groups, grpmap);
15034 : 7649 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15035 : : &grpmap, list_p);
15036 : :
15037 : 7649 : omp_mapping_group *outlist = NULL;
15038 : :
15039 : 15298 : delete grpmap;
15040 : 7649 : delete groups;
15041 : :
15042 : : /* Rebuild now we have struct sibling lists. */
15043 : 7649 : groups = omp_gather_mapping_groups (list_p);
15044 : 7649 : grpmap = omp_index_mapping_groups (groups);
15045 : :
15046 : 7649 : bool enter_exit = (code == OMP_TARGET_ENTER_DATA
15047 : 7649 : || code == OMP_TARGET_EXIT_DATA);
15048 : :
15049 : 7649 : outlist = omp_tsort_mapping_groups (groups, grpmap, enter_exit);
15050 : 7649 : outlist = omp_segregate_mapping_groups (outlist);
15051 : 7649 : list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
15052 : :
15053 : 7649 : delete grpmap;
15054 : 7649 : delete groups;
15055 : : }
15056 : 15817 : }
15057 : 112055 : else if (ctx->region_type & ORT_ACC)
15058 : : {
15059 : 29868 : vec<omp_mapping_group> *groups;
15060 : 29868 : groups = omp_gather_mapping_groups (list_p);
15061 : 29868 : if (groups)
15062 : : {
15063 : 9421 : hash_map<tree_operand_hash_no_se, omp_mapping_group *> *grpmap;
15064 : 9421 : grpmap = omp_index_mapping_groups (groups);
15065 : :
15066 : 9421 : oacc_resolve_clause_dependencies (groups, grpmap);
15067 : 9421 : omp_build_struct_sibling_lists (code, ctx->region_type, groups,
15068 : : &grpmap, list_p);
15069 : :
15070 : 9421 : delete groups;
15071 : 18842 : delete grpmap;
15072 : : }
15073 : : }
15074 : :
15075 : 127872 : tree attach_list = NULL_TREE;
15076 : 127872 : tree *attach_tail = &attach_list;
15077 : :
15078 : 127872 : tree *grp_start_p = NULL, grp_end = NULL_TREE;
15079 : :
15080 : 370720 : while ((c = *list_p) != NULL)
15081 : : {
15082 : 242848 : splay_tree_node n;
15083 : 242848 : bool remove = false;
15084 : 242848 : bool move_attach = false;
15085 : :
15086 : 282887 : if (grp_end && c == OMP_CLAUSE_CHAIN (grp_end))
15087 : : grp_end = NULL_TREE;
15088 : :
15089 : 242848 : switch (OMP_CLAUSE_CODE (c))
15090 : : {
15091 : 7849 : case OMP_CLAUSE_FIRSTPRIVATE:
15092 : 7849 : if ((ctx->region_type & ORT_TARGET)
15093 : 1835 : && (ctx->region_type & ORT_ACC) == 0
15094 : 9079 : && TYPE_ATOMIC (strip_array_types
15095 : : (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
15096 : : {
15097 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15098 : : "%<_Atomic%> %qD in %<firstprivate%> clause on "
15099 : 2 : "%<target%> construct", OMP_CLAUSE_DECL (c));
15100 : 2 : remove = true;
15101 : 2 : break;
15102 : : }
15103 : 7847 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c))
15104 : : {
15105 : 380 : decl = OMP_CLAUSE_DECL (c);
15106 : 380 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15107 : 380 : if ((n->value & GOVD_MAP) != 0)
15108 : : {
15109 : : remove = true;
15110 : : break;
15111 : : }
15112 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c) = 0;
15113 : 368 : OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 0;
15114 : : }
15115 : : /* FALLTHRU */
15116 : 36505 : case OMP_CLAUSE_PRIVATE:
15117 : 36505 : case OMP_CLAUSE_SHARED:
15118 : 36505 : case OMP_CLAUSE_LINEAR:
15119 : 36505 : decl = OMP_CLAUSE_DECL (c);
15120 : 36505 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15121 : 36505 : remove = !(n->value & GOVD_SEEN);
15122 : 36505 : if ((n->value & GOVD_LASTPRIVATE_CONDITIONAL) != 0
15123 : 48 : && code == OMP_PARALLEL
15124 : 36527 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15125 : : remove = true;
15126 : 36483 : if (! remove)
15127 : : {
15128 : 31911 : bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
15129 : 31911 : if ((n->value & GOVD_DEBUG_PRIVATE)
15130 : 31911 : || lang_hooks.decls.omp_private_debug_clause (decl, shared))
15131 : : {
15132 : 83 : gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
15133 : : || ((n->value & GOVD_DATA_SHARE_CLASS)
15134 : : == GOVD_SHARED));
15135 : 83 : OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
15136 : 83 : OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
15137 : : }
15138 : 31911 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15139 : 4129 : && ctx->has_depend
15140 : 32469 : && DECL_P (decl))
15141 : 558 : n->value |= GOVD_WRITTEN;
15142 : 31911 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15143 : 4129 : && (n->value & GOVD_WRITTEN) == 0
15144 : 3018 : && DECL_P (decl)
15145 : 34929 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15146 : 526 : OMP_CLAUSE_SHARED_READONLY (c) = 1;
15147 : 31385 : else if (DECL_P (decl)
15148 : 31385 : && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15149 : 3603 : && (n->value & GOVD_WRITTEN) != 0)
15150 : 30274 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15151 : 9024 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
15152 : 38415 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15153 : 5726 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15154 : : }
15155 : : else
15156 : 4594 : n->value &= ~GOVD_EXPLICIT;
15157 : : break;
15158 : :
15159 : 12996 : case OMP_CLAUSE_LASTPRIVATE:
15160 : : /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
15161 : : accurately reflect the presence of a FIRSTPRIVATE clause. */
15162 : 12996 : decl = OMP_CLAUSE_DECL (c);
15163 : 12996 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15164 : 12996 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
15165 : 12996 : = (n->value & GOVD_FIRSTPRIVATE) != 0;
15166 : 12996 : if (code == OMP_DISTRIBUTE
15167 : 12996 : && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
15168 : : {
15169 : 4 : remove = true;
15170 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15171 : : "same variable used in %<firstprivate%> and "
15172 : : "%<lastprivate%> clauses on %<distribute%> "
15173 : : "construct");
15174 : : }
15175 : 12996 : if (!remove
15176 : 12992 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
15177 : 12992 : && DECL_P (decl)
15178 : 12992 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15179 : 11399 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15180 : 12996 : if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) && code == OMP_PARALLEL)
15181 : : remove = true;
15182 : : break;
15183 : :
15184 : 824 : case OMP_CLAUSE_ALIGNED:
15185 : 824 : decl = OMP_CLAUSE_DECL (c);
15186 : 824 : if (!is_global_var (decl))
15187 : : {
15188 : 738 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15189 : 738 : remove = n == NULL || !(n->value & GOVD_SEEN);
15190 : 88 : if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15191 : : {
15192 : 88 : struct gimplify_omp_ctx *octx;
15193 : 88 : if (n != NULL
15194 : 88 : && (n->value & (GOVD_DATA_SHARE_CLASS
15195 : : & ~GOVD_FIRSTPRIVATE)))
15196 : : remove = true;
15197 : : else
15198 : 100 : for (octx = ctx->outer_context; octx;
15199 : 12 : octx = octx->outer_context)
15200 : : {
15201 : 24 : n = splay_tree_lookup (octx->variables,
15202 : : (splay_tree_key) decl);
15203 : 24 : if (n == NULL)
15204 : 12 : continue;
15205 : 12 : if (n->value & GOVD_LOCAL)
15206 : : break;
15207 : : /* We have to avoid assigning a shared variable
15208 : : to itself when trying to add
15209 : : __builtin_assume_aligned. */
15210 : 12 : if (n->value & GOVD_SHARED)
15211 : : {
15212 : : remove = true;
15213 : : break;
15214 : : }
15215 : : }
15216 : : }
15217 : : }
15218 : 86 : else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
15219 : : {
15220 : 83 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15221 : 83 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
15222 : : remove = true;
15223 : : }
15224 : : break;
15225 : :
15226 : 538 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
15227 : 538 : decl = OMP_CLAUSE_DECL (c);
15228 : 538 : while (INDIRECT_REF_P (decl)
15229 : 587 : || TREE_CODE (decl) == ARRAY_REF)
15230 : 49 : decl = TREE_OPERAND (decl, 0);
15231 : 538 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15232 : 538 : remove = n == NULL || !(n->value & GOVD_SEEN);
15233 : : break;
15234 : :
15235 : 898 : case OMP_CLAUSE_IS_DEVICE_PTR:
15236 : 898 : case OMP_CLAUSE_NONTEMPORAL:
15237 : 898 : decl = OMP_CLAUSE_DECL (c);
15238 : 898 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15239 : 898 : remove = n == NULL || !(n->value & GOVD_SEEN);
15240 : : break;
15241 : :
15242 : 53230 : case OMP_CLAUSE_MAP:
15243 : 53230 : decl = OMP_CLAUSE_DECL (c);
15244 : 53230 : if (!grp_end)
15245 : : {
15246 : 29977 : grp_start_p = list_p;
15247 : 29977 : grp_end = *omp_group_last (grp_start_p);
15248 : : }
15249 : 53230 : switch (OMP_CLAUSE_MAP_KIND (c))
15250 : : {
15251 : 123 : case GOMP_MAP_PRESENT_ALLOC:
15252 : 123 : case GOMP_MAP_PRESENT_TO:
15253 : 123 : case GOMP_MAP_PRESENT_FROM:
15254 : 123 : case GOMP_MAP_PRESENT_TOFROM:
15255 : 123 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_PRESENT);
15256 : 123 : break;
15257 : : default:
15258 : : break;
15259 : : }
15260 : 53230 : switch (code)
15261 : : {
15262 : 4568 : case OACC_DATA:
15263 : 4568 : if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
15264 : : break;
15265 : : /* Fallthrough. */
15266 : 17001 : case OACC_HOST_DATA:
15267 : 17001 : case OACC_ENTER_DATA:
15268 : 17001 : case OACC_EXIT_DATA:
15269 : 17001 : case OMP_TARGET_DATA:
15270 : 17001 : case OMP_TARGET_ENTER_DATA:
15271 : 17001 : case OMP_TARGET_EXIT_DATA:
15272 : 17001 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15273 : 17001 : || (OMP_CLAUSE_MAP_KIND (c)
15274 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15275 : : /* For target {,enter ,exit }data only the array slice is
15276 : : mapped, but not the pointer to it. */
15277 : : remove = true;
15278 : 17001 : if (code == OMP_TARGET_EXIT_DATA
15279 : 17001 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
15280 : 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER))
15281 : : remove = true;
15282 : : break;
15283 : : case OMP_TARGET:
15284 : : break;
15285 : : default:
15286 : : break;
15287 : : }
15288 : 16999 : if (remove)
15289 : : break;
15290 : 52895 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15291 : : {
15292 : : /* Sanity check: attach/detach map kinds use the size as a bias,
15293 : : and it's never right to use the decl size for such
15294 : : mappings. */
15295 : 16773 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
15296 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
15297 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH
15298 : : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
15299 : : && (OMP_CLAUSE_MAP_KIND (c)
15300 : : != GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION));
15301 : 20542 : OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
15302 : 3769 : : TYPE_SIZE_UNIT (TREE_TYPE (decl));
15303 : : }
15304 : 52895 : gimplify_omp_ctxp = ctx->outer_context;
15305 : 52895 : if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p, NULL,
15306 : : is_gimple_val, fb_rvalue) == GS_ERROR)
15307 : : {
15308 : 0 : gimplify_omp_ctxp = ctx;
15309 : 0 : remove = true;
15310 : 0 : break;
15311 : : }
15312 : 52895 : else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
15313 : 50135 : || (OMP_CLAUSE_MAP_KIND (c)
15314 : : == GOMP_MAP_FIRSTPRIVATE_REFERENCE)
15315 : 49726 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15316 : 58872 : && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
15317 : : {
15318 : 786 : OMP_CLAUSE_SIZE (c)
15319 : 786 : = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
15320 : : false);
15321 : 786 : if ((ctx->region_type & ORT_TARGET) != 0)
15322 : 601 : omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
15323 : : GOVD_FIRSTPRIVATE | GOVD_SEEN);
15324 : : }
15325 : 52895 : gimplify_omp_ctxp = ctx;
15326 : : /* Data clauses associated with reductions must be
15327 : : compatible with present_or_copy. Warn and adjust the clause
15328 : : if that is not the case. */
15329 : 52895 : if (ctx->region_type == ORT_ACC_PARALLEL
15330 : 43917 : || ctx->region_type == ORT_ACC_SERIAL)
15331 : : {
15332 : 9533 : tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
15333 : 9533 : n = NULL;
15334 : :
15335 : 9533 : if (DECL_P (t))
15336 : 7768 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
15337 : :
15338 : 7768 : if (n && (n->value & GOVD_REDUCTION))
15339 : : {
15340 : 807 : enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
15341 : :
15342 : 807 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15343 : 807 : if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
15344 : 159 : && kind != GOMP_MAP_FORCE_PRESENT
15345 : 159 : && kind != GOMP_MAP_POINTER)
15346 : : {
15347 : 120 : warning_at (OMP_CLAUSE_LOCATION (c), 0,
15348 : : "incompatible data clause with reduction "
15349 : : "on %qE; promoting to %<present_or_copy%>",
15350 : 120 : DECL_NAME (t));
15351 : 120 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
15352 : : }
15353 : : }
15354 : : }
15355 : 52895 : if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15356 : 50298 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15357 : 53003 : && (code == OMP_TARGET_EXIT_DATA || code == OACC_EXIT_DATA))
15358 : : {
15359 : : remove = true;
15360 : : break;
15361 : : }
15362 : : /* If we have a DECL_VALUE_EXPR (e.g. this is a class member and/or
15363 : : a variable captured in a lambda closure), look through that now
15364 : : before the DECL_P check below. (A code other than COMPONENT_REF,
15365 : : i.e. INDIRECT_REF, will be a VLA/variable-length array
15366 : : section. A global var may be a variable in a common block. We
15367 : : don't want to do this here for either of those.) */
15368 : 52483 : if ((ctx->region_type & ORT_ACC) == 0
15369 : 26396 : && DECL_P (decl)
15370 : 11633 : && !is_global_var (decl)
15371 : 10342 : && DECL_HAS_VALUE_EXPR_P (decl)
15372 : 52673 : && TREE_CODE (DECL_VALUE_EXPR (decl)) == COMPONENT_REF)
15373 : 0 : decl = OMP_CLAUSE_DECL (c) = DECL_VALUE_EXPR (decl);
15374 : 52483 : if (TREE_CODE (decl) == TARGET_EXPR)
15375 : : {
15376 : 0 : if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
15377 : : is_gimple_lvalue, fb_lvalue) == GS_ERROR)
15378 : 25098 : remove = true;
15379 : : }
15380 : 52483 : else if (!DECL_P (decl))
15381 : : {
15382 : 27385 : if ((ctx->region_type & ORT_TARGET) != 0
15383 : 27385 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15384 : : {
15385 : 24 : if (INDIRECT_REF_P (decl)
15386 : 0 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15387 : 24 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15388 : : == REFERENCE_TYPE))
15389 : 0 : decl = TREE_OPERAND (decl, 0);
15390 : 24 : if (TREE_CODE (decl) == COMPONENT_REF)
15391 : : {
15392 : 0 : while (TREE_CODE (decl) == COMPONENT_REF)
15393 : 0 : decl = TREE_OPERAND (decl, 0);
15394 : 0 : if (DECL_P (decl))
15395 : : {
15396 : 0 : n = splay_tree_lookup (ctx->variables,
15397 : : (splay_tree_key) decl);
15398 : 0 : if (!(n->value & GOVD_SEEN))
15399 : 27385 : remove = true;
15400 : : }
15401 : : }
15402 : : }
15403 : :
15404 : 27385 : tree d = decl, *pd;
15405 : 27385 : if (TREE_CODE (d) == ARRAY_REF)
15406 : : {
15407 : 5586 : while (TREE_CODE (d) == ARRAY_REF)
15408 : 2853 : d = TREE_OPERAND (d, 0);
15409 : 2733 : if (TREE_CODE (d) == COMPONENT_REF
15410 : 2733 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
15411 : : decl = d;
15412 : : }
15413 : 27385 : pd = &OMP_CLAUSE_DECL (c);
15414 : 27385 : if (d == decl
15415 : 25009 : && TREE_CODE (decl) == INDIRECT_REF
15416 : 14770 : && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
15417 : 1470 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
15418 : : == REFERENCE_TYPE)
15419 : 28185 : && (OMP_CLAUSE_MAP_KIND (c)
15420 : : != GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION))
15421 : : {
15422 : 796 : pd = &TREE_OPERAND (decl, 0);
15423 : 796 : decl = TREE_OPERAND (decl, 0);
15424 : : }
15425 : :
15426 : 27385 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15427 : 2591 : switch (code)
15428 : : {
15429 : 427 : case OACC_ENTER_DATA:
15430 : 427 : case OACC_EXIT_DATA:
15431 : 427 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
15432 : : == ARRAY_TYPE)
15433 : : remove = true;
15434 : 417 : else if (code == OACC_ENTER_DATA)
15435 : 263 : goto change_to_attach;
15436 : : /* Fallthrough. */
15437 : 347 : case OMP_TARGET_EXIT_DATA:
15438 : 347 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DETACH);
15439 : 347 : break;
15440 : 71 : case OACC_UPDATE:
15441 : : /* An "attach/detach" operation on an update directive
15442 : : should behave as a GOMP_MAP_ALWAYS_POINTER. Note that
15443 : : both GOMP_MAP_ATTACH_DETACH and GOMP_MAP_ALWAYS_POINTER
15444 : : kinds depend on the previous mapping (for non-TARGET
15445 : : regions). */
15446 : 71 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
15447 : 71 : break;
15448 : 2173 : default:
15449 : 2173 : change_to_attach:
15450 : 2173 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH);
15451 : 2173 : if ((ctx->region_type & ORT_TARGET) != 0)
15452 : 1769 : move_attach = true;
15453 : : }
15454 : 24794 : else if ((ctx->region_type & ORT_TARGET) != 0
15455 : 24794 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15456 : 11443 : || (OMP_CLAUSE_MAP_KIND (c)
15457 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15458 : : move_attach = true;
15459 : :
15460 : : /* If we have e.g. map(struct: *var), don't gimplify the
15461 : : argument since omp-low.cc wants to see the decl itself. */
15462 : 27385 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
15463 : : break;
15464 : :
15465 : : /* We've already partly gimplified this in
15466 : : gimplify_scan_omp_clauses. Don't do any more. */
15467 : 26250 : if (code == OMP_TARGET && OMP_CLAUSE_MAP_IN_REDUCTION (c))
15468 : : break;
15469 : :
15470 : 26162 : gimplify_omp_ctxp = ctx->outer_context;
15471 : 26162 : if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue,
15472 : : fb_lvalue) == GS_ERROR)
15473 : 0 : remove = true;
15474 : 26162 : gimplify_omp_ctxp = ctx;
15475 : 26162 : break;
15476 : : }
15477 : :
15478 : 25098 : if ((code == OMP_TARGET
15479 : : || code == OMP_TARGET_DATA
15480 : : || code == OMP_TARGET_ENTER_DATA
15481 : 14275 : || code == OMP_TARGET_EXIT_DATA)
15482 : 25646 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15483 : : {
15484 : 217 : bool firstprivatize = false;
15485 : :
15486 : 222 : for (struct gimplify_omp_ctx *octx = ctx->outer_context; octx;
15487 : 5 : octx = octx->outer_context)
15488 : : {
15489 : 6 : splay_tree_node n
15490 : 12 : = splay_tree_lookup (octx->variables,
15491 : 6 : (splay_tree_key) OMP_CLAUSE_DECL (c));
15492 : : /* If this is contained in an outer OpenMP region as a
15493 : : firstprivate value, remove the attach/detach. */
15494 : 6 : if (n && (n->value & GOVD_FIRSTPRIVATE))
15495 : : {
15496 : : firstprivatize = true;
15497 : : break;
15498 : : }
15499 : : }
15500 : :
15501 : 217 : enum gomp_map_kind map_kind;
15502 : 217 : if (firstprivatize)
15503 : : map_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
15504 : 216 : else if (code == OMP_TARGET_EXIT_DATA)
15505 : : map_kind = GOMP_MAP_DETACH;
15506 : : else
15507 : 168 : map_kind = GOMP_MAP_ATTACH;
15508 : 217 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15509 : : }
15510 : 24881 : else if ((ctx->region_type & ORT_ACC) != 0
15511 : 24881 : && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH)
15512 : : {
15513 : 0 : enum gomp_map_kind map_kind = (code == OACC_EXIT_DATA
15514 : 0 : ? GOMP_MAP_DETACH
15515 : : : GOMP_MAP_ATTACH);
15516 : 0 : OMP_CLAUSE_SET_MAP_KIND (c, map_kind);
15517 : : }
15518 : :
15519 : 25098 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15520 : 25098 : if ((ctx->region_type & ORT_TARGET) != 0
15521 : 15548 : && !(n->value & GOVD_SEEN)
15522 : 1363 : && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
15523 : 26218 : && (!is_global_var (decl)
15524 : 75 : || !lookup_attribute ("omp declare target link",
15525 : 75 : DECL_ATTRIBUTES (decl))))
15526 : : {
15527 : 1108 : remove = true;
15528 : : /* For struct element mapping, if struct is never referenced
15529 : : in target block and none of the mapping has always modifier,
15530 : : remove all the struct element mappings, which immediately
15531 : : follow the GOMP_MAP_STRUCT map clause. */
15532 : 1108 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
15533 : 1108 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT_UNORD)
15534 : : {
15535 : 28 : HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
15536 : 76 : while (cnt--)
15537 : 48 : OMP_CLAUSE_CHAIN (c)
15538 : 48 : = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
15539 : : }
15540 : : }
15541 : 23990 : else if (DECL_SIZE (decl)
15542 : 23966 : && !poly_int_tree_p (DECL_SIZE (decl))
15543 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
15544 : 199 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
15545 : 24041 : && (OMP_CLAUSE_MAP_KIND (c)
15546 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
15547 : : {
15548 : : /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
15549 : : for these, TREE_CODE (DECL_SIZE (decl)) will always be
15550 : : INTEGER_CST. */
15551 : 51 : gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
15552 : :
15553 : 51 : tree decl2 = DECL_VALUE_EXPR (decl);
15554 : 51 : gcc_assert (INDIRECT_REF_P (decl2));
15555 : 51 : decl2 = TREE_OPERAND (decl2, 0);
15556 : 51 : gcc_assert (DECL_P (decl2));
15557 : 51 : tree mem = build_simple_mem_ref (decl2);
15558 : 51 : OMP_CLAUSE_DECL (c) = mem;
15559 : 51 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15560 : 51 : if (ctx->outer_context)
15561 : : {
15562 : 15 : omp_notice_variable (ctx->outer_context, decl2, true);
15563 : 30 : omp_notice_variable (ctx->outer_context,
15564 : 15 : OMP_CLAUSE_SIZE (c), true);
15565 : : }
15566 : 51 : if (((ctx->region_type & ORT_TARGET) != 0
15567 : 24 : || !ctx->target_firstprivatize_array_bases)
15568 : 34 : && ((n->value & GOVD_SEEN) == 0
15569 : 30 : || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
15570 : : {
15571 : 34 : tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15572 : : OMP_CLAUSE_MAP);
15573 : 34 : OMP_CLAUSE_DECL (nc) = decl;
15574 : 34 : OMP_CLAUSE_SIZE (nc) = size_zero_node;
15575 : 34 : if (ctx->target_firstprivatize_array_bases)
15576 : 27 : OMP_CLAUSE_SET_MAP_KIND (nc,
15577 : : GOMP_MAP_FIRSTPRIVATE_POINTER);
15578 : : else
15579 : 7 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
15580 : 34 : OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
15581 : 34 : OMP_CLAUSE_CHAIN (c) = nc;
15582 : 34 : c = nc;
15583 : : }
15584 : : }
15585 : : else
15586 : : {
15587 : 23939 : if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15588 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
15589 : 23939 : gcc_assert ((n->value & GOVD_SEEN) == 0
15590 : : || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
15591 : : == 0));
15592 : : }
15593 : :
15594 : : /* If we have a target region, we can push all the attaches to the
15595 : : end of the list (we may have standalone "attach" operations
15596 : : synthesized for GOMP_MAP_STRUCT nodes that must be processed after
15597 : : the attachment point AND the pointed-to block have been mapped).
15598 : : If we have something else, e.g. "enter data", we need to keep
15599 : : "attach" nodes together with the previous node they attach to so
15600 : : that separate "exit data" operations work properly (see
15601 : : libgomp/target.c). */
15602 : 25098 : if ((ctx->region_type & ORT_TARGET) != 0
15603 : 25098 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15604 : 15444 : || (OMP_CLAUSE_MAP_KIND (c)
15605 : : == GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION)))
15606 : : move_attach = true;
15607 : :
15608 : : break;
15609 : :
15610 : 8188 : case OMP_CLAUSE_TO:
15611 : 8188 : case OMP_CLAUSE_FROM:
15612 : 8188 : case OMP_CLAUSE__CACHE_:
15613 : 8188 : decl = OMP_CLAUSE_DECL (c);
15614 : 8188 : if (!DECL_P (decl))
15615 : : break;
15616 : 6181 : if (DECL_SIZE (decl)
15617 : 6181 : && !poly_int_tree_p (DECL_SIZE (decl)))
15618 : : {
15619 : 4 : tree decl2 = DECL_VALUE_EXPR (decl);
15620 : 4 : gcc_assert (INDIRECT_REF_P (decl2));
15621 : 4 : decl2 = TREE_OPERAND (decl2, 0);
15622 : 4 : gcc_assert (DECL_P (decl2));
15623 : 4 : tree mem = build_simple_mem_ref (decl2);
15624 : 4 : OMP_CLAUSE_DECL (c) = mem;
15625 : 4 : OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
15626 : 4 : if (ctx->outer_context)
15627 : : {
15628 : 4 : omp_notice_variable (ctx->outer_context, decl2, true);
15629 : 4 : omp_notice_variable (ctx->outer_context,
15630 : 4 : OMP_CLAUSE_SIZE (c), true);
15631 : : }
15632 : : }
15633 : 6177 : else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
15634 : 0 : OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
15635 : : break;
15636 : :
15637 : 15285 : case OMP_CLAUSE_REDUCTION:
15638 : 15285 : if (OMP_CLAUSE_REDUCTION_INSCAN (c))
15639 : : {
15640 : 811 : decl = OMP_CLAUSE_DECL (c);
15641 : 811 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15642 : 811 : if ((n->value & GOVD_REDUCTION_INSCAN) == 0)
15643 : : {
15644 : 69 : remove = true;
15645 : 69 : error_at (OMP_CLAUSE_LOCATION (c),
15646 : : "%qD specified in %<inscan%> %<reduction%> clause "
15647 : : "but not in %<scan%> directive clause", decl);
15648 : 69 : break;
15649 : : }
15650 : : has_inscan_reductions = true;
15651 : : }
15652 : : /* FALLTHRU */
15653 : 17816 : case OMP_CLAUSE_IN_REDUCTION:
15654 : 17816 : case OMP_CLAUSE_TASK_REDUCTION:
15655 : 17816 : decl = OMP_CLAUSE_DECL (c);
15656 : : /* OpenACC reductions need a present_or_copy data clause.
15657 : : Add one if necessary. Emit error when the reduction is private. */
15658 : 17816 : if (ctx->region_type == ORT_ACC_PARALLEL
15659 : 17285 : || ctx->region_type == ORT_ACC_SERIAL)
15660 : : {
15661 : 801 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15662 : 801 : if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
15663 : : {
15664 : 13 : remove = true;
15665 : 13 : error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
15666 : 13 : "reduction on %qE", DECL_NAME (decl));
15667 : : }
15668 : 788 : else if ((n->value & GOVD_MAP) == 0)
15669 : : {
15670 : 522 : tree next = OMP_CLAUSE_CHAIN (c);
15671 : 522 : tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
15672 : 522 : OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
15673 : 522 : OMP_CLAUSE_DECL (nc) = decl;
15674 : 522 : OMP_CLAUSE_CHAIN (c) = nc;
15675 : 522 : lang_hooks.decls.omp_finish_clause (nc, pre_p,
15676 : 522 : (ctx->region_type
15677 : : & ORT_ACC) != 0);
15678 : 548 : while (1)
15679 : : {
15680 : 535 : OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
15681 : 535 : if (OMP_CLAUSE_CHAIN (nc) == NULL)
15682 : : break;
15683 : 13 : nc = OMP_CLAUSE_CHAIN (nc);
15684 : : }
15685 : 522 : OMP_CLAUSE_CHAIN (nc) = next;
15686 : 522 : n->value |= GOVD_MAP;
15687 : : }
15688 : : }
15689 : 17816 : if (DECL_P (decl)
15690 : 17816 : && omp_shared_to_firstprivate_optimizable_decl_p (decl))
15691 : 7959 : omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
15692 : : break;
15693 : :
15694 : 3541 : case OMP_CLAUSE_ALLOCATE:
15695 : 3541 : decl = OMP_CLAUSE_DECL (c);
15696 : 3541 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
15697 : 3541 : if (n != NULL && !(n->value & GOVD_SEEN))
15698 : : {
15699 : 1176 : if ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE | GOVD_LINEAR))
15700 : : != 0
15701 : 1176 : && (n->value & (GOVD_REDUCTION | GOVD_LASTPRIVATE)) == 0)
15702 : : remove = true;
15703 : : }
15704 : : if (!remove
15705 : 2365 : && OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
15706 : 1153 : && TREE_CODE (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)) != INTEGER_CST
15707 : 471 : && ((ctx->region_type & (ORT_PARALLEL | ORT_TARGET)) != 0
15708 : 347 : || (ctx->region_type & ORT_TASKLOOP) == ORT_TASK
15709 : 287 : || (ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS))
15710 : : {
15711 : 202 : tree allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
15712 : 202 : n = splay_tree_lookup (ctx->variables, (splay_tree_key) allocator);
15713 : 202 : if (n == NULL)
15714 : : {
15715 : 82 : enum omp_clause_default_kind default_kind
15716 : : = ctx->default_kind;
15717 : 82 : ctx->default_kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
15718 : 82 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
15719 : : true);
15720 : 82 : ctx->default_kind = default_kind;
15721 : : }
15722 : : else
15723 : 120 : omp_notice_variable (ctx, OMP_CLAUSE_ALLOCATE_ALLOCATOR (c),
15724 : : true);
15725 : : }
15726 : : break;
15727 : :
15728 : : case OMP_CLAUSE_COPYIN:
15729 : : case OMP_CLAUSE_COPYPRIVATE:
15730 : : case OMP_CLAUSE_IF:
15731 : : case OMP_CLAUSE_SELF:
15732 : : case OMP_CLAUSE_NUM_THREADS:
15733 : : case OMP_CLAUSE_NUM_TEAMS:
15734 : : case OMP_CLAUSE_THREAD_LIMIT:
15735 : : case OMP_CLAUSE_DIST_SCHEDULE:
15736 : : case OMP_CLAUSE_DEVICE:
15737 : : case OMP_CLAUSE_SCHEDULE:
15738 : : case OMP_CLAUSE_NOWAIT:
15739 : : case OMP_CLAUSE_ORDERED:
15740 : : case OMP_CLAUSE_DEFAULT:
15741 : : case OMP_CLAUSE_UNTIED:
15742 : : case OMP_CLAUSE_COLLAPSE:
15743 : : case OMP_CLAUSE_FINAL:
15744 : : case OMP_CLAUSE_MERGEABLE:
15745 : : case OMP_CLAUSE_PROC_BIND:
15746 : : case OMP_CLAUSE_SAFELEN:
15747 : : case OMP_CLAUSE_SIMDLEN:
15748 : : case OMP_CLAUSE_DEPEND:
15749 : : case OMP_CLAUSE_DOACROSS:
15750 : : case OMP_CLAUSE_PRIORITY:
15751 : : case OMP_CLAUSE_GRAINSIZE:
15752 : : case OMP_CLAUSE_NUM_TASKS:
15753 : : case OMP_CLAUSE_NOGROUP:
15754 : : case OMP_CLAUSE_THREADS:
15755 : : case OMP_CLAUSE_SIMD:
15756 : : case OMP_CLAUSE_FILTER:
15757 : : case OMP_CLAUSE_HINT:
15758 : : case OMP_CLAUSE_DEFAULTMAP:
15759 : : case OMP_CLAUSE_ORDER:
15760 : : case OMP_CLAUSE_BIND:
15761 : : case OMP_CLAUSE_DETACH:
15762 : : case OMP_CLAUSE_USE_DEVICE_PTR:
15763 : : case OMP_CLAUSE_USE_DEVICE_ADDR:
15764 : : case OMP_CLAUSE_ASYNC:
15765 : : case OMP_CLAUSE_WAIT:
15766 : : case OMP_CLAUSE_INDEPENDENT:
15767 : : case OMP_CLAUSE_NUM_GANGS:
15768 : : case OMP_CLAUSE_NUM_WORKERS:
15769 : : case OMP_CLAUSE_VECTOR_LENGTH:
15770 : : case OMP_CLAUSE_GANG:
15771 : : case OMP_CLAUSE_WORKER:
15772 : : case OMP_CLAUSE_VECTOR:
15773 : : case OMP_CLAUSE_AUTO:
15774 : : case OMP_CLAUSE_SEQ:
15775 : : case OMP_CLAUSE_TILE:
15776 : : case OMP_CLAUSE_IF_PRESENT:
15777 : : case OMP_CLAUSE_FINALIZE:
15778 : : case OMP_CLAUSE_INCLUSIVE:
15779 : : case OMP_CLAUSE_EXCLUSIVE:
15780 : : break;
15781 : :
15782 : 0 : case OMP_CLAUSE_NOHOST:
15783 : 0 : default:
15784 : 0 : gcc_unreachable ();
15785 : : }
15786 : :
15787 : 125465 : if (remove)
15788 : 9549 : *list_p = OMP_CLAUSE_CHAIN (c);
15789 : 233299 : else if (move_attach)
15790 : : {
15791 : : /* Remove attach node from here, separate out into its own list. */
15792 : 2293 : *attach_tail = c;
15793 : 2293 : *list_p = OMP_CLAUSE_CHAIN (c);
15794 : 2293 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
15795 : 2293 : attach_tail = &OMP_CLAUSE_CHAIN (c);
15796 : : }
15797 : : else
15798 : 231006 : list_p = &OMP_CLAUSE_CHAIN (c);
15799 : : }
15800 : :
15801 : : /* Splice attach nodes at the end of the list. */
15802 : 127872 : if (attach_list)
15803 : : {
15804 : 1097 : *list_p = attach_list;
15805 : 1097 : list_p = attach_tail;
15806 : : }
15807 : :
15808 : : /* Add in any implicit data sharing. */
15809 : 127872 : struct gimplify_adjust_omp_clauses_data data;
15810 : 127872 : if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0)
15811 : : {
15812 : : /* OpenMP. Implicit clauses are added at the start of the clause list,
15813 : : but after any non-map clauses. */
15814 : : tree *implicit_add_list_p = orig_list_p;
15815 : 248761 : while (*implicit_add_list_p
15816 : 248761 : && OMP_CLAUSE_CODE (*implicit_add_list_p) != OMP_CLAUSE_MAP)
15817 : 150757 : implicit_add_list_p = &OMP_CLAUSE_CHAIN (*implicit_add_list_p);
15818 : 98004 : data.list_p = implicit_add_list_p;
15819 : : }
15820 : : else
15821 : : /* OpenACC. */
15822 : 29868 : data.list_p = list_p;
15823 : 127872 : data.pre_p = pre_p;
15824 : 127872 : splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
15825 : :
15826 : 127872 : if (has_inscan_reductions)
15827 : 2431 : for (c = *orig_list_p; c; c = OMP_CLAUSE_CHAIN (c))
15828 : 1822 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15829 : 1822 : && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
15830 : : {
15831 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
15832 : : "%<inscan%> %<reduction%> clause used together with "
15833 : : "%<linear%> clause for a variable other than loop "
15834 : : "iterator");
15835 : 5 : break;
15836 : : }
15837 : :
15838 : 127872 : gimplify_omp_ctxp = ctx->outer_context;
15839 : 127872 : delete_omp_context (ctx);
15840 : 127872 : }
15841 : :
15842 : : /* Try to evaluate a novariants clause. Return 1 if true, 0 if false or absent,
15843 : : * -1 if run-time evaluation is needed. */
15844 : :
15845 : : int
15846 : 242744 : omp_has_novariants (void)
15847 : : {
15848 : 242744 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15849 : 242744 : if (ctx != NULL && ctx->code == OMP_DISPATCH && !ctx->in_call_args)
15850 : : {
15851 : 3274 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOVARIANTS);
15852 : 3274 : if (c != NULL_TREE)
15853 : : {
15854 : 287 : if (integer_nonzerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
15855 : : return 1;
15856 : 171 : else if (integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
15857 : : return 0;
15858 : : else
15859 : : return -1;
15860 : : }
15861 : : return 0;
15862 : : }
15863 : : return 0;
15864 : : }
15865 : :
15866 : : /* Try to evaluate a nocontext clause. Return 1 if true, 0 if false or absent,
15867 : : * -1 if run-time evaluation is needed. */
15868 : :
15869 : : static int
15870 : 1890 : omp_has_nocontext (void)
15871 : : {
15872 : 1890 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
15873 : 1890 : if (ctx != NULL && ctx->code == OMP_DISPATCH)
15874 : : {
15875 : 1890 : tree c = omp_find_clause (ctx->clauses, OMP_CLAUSE_NOCONTEXT);
15876 : 1890 : if (c != NULL_TREE)
15877 : : {
15878 : 256 : if (integer_nonzerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
15879 : : return 1;
15880 : 94 : else if (integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
15881 : : return 0;
15882 : : else
15883 : : return -1;
15884 : : }
15885 : : return 0;
15886 : : }
15887 : : return 0;
15888 : : }
15889 : :
15890 : : /* Collect a list of traits for enclosing constructs in the current
15891 : : OpenMP context. The list is in the same format as the trait selector
15892 : : list of construct trait sets built by the front ends.
15893 : :
15894 : : Per the OpenMP specification, the construct trait set includes constructs
15895 : : up to an enclosing "target" construct. If there is no "target" construct,
15896 : : then additional things may be added to the construct trait set (simd for
15897 : : simd clones, additional constructs associated with "declare variant",
15898 : : the target trait for "declare target"); those are not handled here.
15899 : : In particular simd clones are not known during gimplification so
15900 : : matching/scoring of context selectors that might involve them needs
15901 : : to be deferred to the omp_device_lower pass. */
15902 : :
15903 : : tree
15904 : 1989 : omp_get_construct_context (void)
15905 : : {
15906 : 1989 : tree result = NULL_TREE;
15907 : 3945 : for (struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp; ctx;)
15908 : : {
15909 : 2037 : if (((ctx->region_type & (ORT_TARGET | ORT_IMPLICIT_TARGET | ORT_ACC))
15910 : : == ORT_TARGET)
15911 : 81 : && ctx->code == OMP_TARGET)
15912 : : {
15913 : 81 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TARGET,
15914 : : NULL_TREE, NULL_TREE, result);
15915 : : /* We're not interested in any outer constructs. */
15916 : 81 : break;
15917 : : }
15918 : 1956 : else if ((ctx->region_type & ORT_PARALLEL) && ctx->code == OMP_PARALLEL)
15919 : 210 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_PARALLEL,
15920 : : NULL_TREE, NULL_TREE, result);
15921 : 1746 : else if ((ctx->region_type & ORT_TEAMS) && ctx->code == OMP_TEAMS)
15922 : 71 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_TEAMS,
15923 : : NULL_TREE, NULL_TREE, result);
15924 : 1675 : else if (ctx->region_type == ORT_WORKSHARE && ctx->code == OMP_FOR)
15925 : 164 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_FOR,
15926 : : NULL_TREE, NULL_TREE, result);
15927 : 1511 : else if (ctx->code == OMP_DISPATCH && omp_has_nocontext () != 1)
15928 : 1272 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_DISPATCH,
15929 : : NULL_TREE, NULL_TREE, result);
15930 : 239 : else if (ctx->region_type == ORT_SIMD
15931 : 32 : && ctx->code == OMP_SIMD
15932 : 271 : && !omp_find_clause (ctx->clauses, OMP_CLAUSE_BIND))
15933 : : {
15934 : 32 : tree props = NULL_TREE;
15935 : 32 : tree *last = &props;
15936 : 72 : for (tree c = ctx->clauses; c; c = OMP_CLAUSE_CHAIN (c))
15937 : 40 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SIMDLEN
15938 : 32 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INBRANCH
15939 : 72 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOTINBRANCH)
15940 : : {
15941 : 8 : *last = unshare_expr (c);
15942 : 8 : last = &(OMP_CLAUSE_CHAIN (c));
15943 : : }
15944 : 32 : result = make_trait_selector (OMP_TRAIT_CONSTRUCT_SIMD,
15945 : : NULL_TREE, props, result);
15946 : : }
15947 : 207 : else if (ctx->region_type == ORT_WORKSHARE
15948 : 5 : && ctx->code == OMP_LOOP
15949 : 0 : && ctx->outer_context
15950 : 0 : && ctx->outer_context->region_type == ORT_COMBINED_PARALLEL
15951 : 0 : && ctx->outer_context->outer_context
15952 : 0 : && ctx->outer_context->outer_context->code == OMP_LOOP
15953 : 0 : && ctx->outer_context->outer_context->distribute)
15954 : 1956 : ctx = ctx->outer_context->outer_context;
15955 : 1956 : ctx = ctx->outer_context;
15956 : : }
15957 : :
15958 : 1989 : return result;
15959 : : }
15960 : :
15961 : : /* Gimplify OACC_CACHE. */
15962 : :
15963 : : static void
15964 : 706 : gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
15965 : : {
15966 : 706 : tree expr = *expr_p;
15967 : :
15968 : 706 : gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
15969 : : OACC_CACHE);
15970 : 706 : gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
15971 : : OACC_CACHE);
15972 : :
15973 : : /* TODO: Do something sensible with this information. */
15974 : :
15975 : 706 : *expr_p = NULL_TREE;
15976 : 706 : }
15977 : :
15978 : : /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
15979 : : if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
15980 : : kind. The entry kind will replace the one in CLAUSE, while the exit
15981 : : kind will be used in a new omp_clause and returned to the caller. */
15982 : :
15983 : : static tree
15984 : 190 : gimplify_oacc_declare_1 (tree clause)
15985 : : {
15986 : 190 : HOST_WIDE_INT kind, new_op;
15987 : 190 : bool ret = false;
15988 : 190 : tree c = NULL;
15989 : :
15990 : 190 : kind = OMP_CLAUSE_MAP_KIND (clause);
15991 : :
15992 : 190 : switch (kind)
15993 : : {
15994 : : case GOMP_MAP_ALLOC:
15995 : : new_op = GOMP_MAP_RELEASE;
15996 : : ret = true;
15997 : : break;
15998 : :
15999 : 29 : case GOMP_MAP_FROM:
16000 : 29 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
16001 : 29 : new_op = GOMP_MAP_FROM;
16002 : 29 : ret = true;
16003 : 29 : break;
16004 : :
16005 : 40 : case GOMP_MAP_TOFROM:
16006 : 40 : OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
16007 : 40 : new_op = GOMP_MAP_FROM;
16008 : 40 : ret = true;
16009 : 40 : break;
16010 : :
16011 : : case GOMP_MAP_DEVICE_RESIDENT:
16012 : : case GOMP_MAP_FORCE_DEVICEPTR:
16013 : : case GOMP_MAP_FORCE_PRESENT:
16014 : : case GOMP_MAP_LINK:
16015 : : case GOMP_MAP_POINTER:
16016 : : case GOMP_MAP_TO:
16017 : : break;
16018 : :
16019 : 0 : default:
16020 : 0 : gcc_unreachable ();
16021 : 69 : break;
16022 : : }
16023 : :
16024 : 69 : if (ret)
16025 : : {
16026 : 116 : c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
16027 : 116 : OMP_CLAUSE_SET_MAP_KIND (c, new_op);
16028 : 116 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
16029 : : }
16030 : :
16031 : 190 : return c;
16032 : : }
16033 : :
16034 : : /* Gimplify OACC_DECLARE. */
16035 : :
16036 : : static void
16037 : 254 : gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
16038 : : {
16039 : 254 : tree expr = *expr_p;
16040 : 254 : gomp_target *stmt;
16041 : 254 : tree clauses, t, decl;
16042 : :
16043 : 254 : clauses = OACC_DECLARE_CLAUSES (expr);
16044 : :
16045 : 254 : gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
16046 : 254 : gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
16047 : :
16048 : 516 : for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16049 : : {
16050 : 262 : decl = OMP_CLAUSE_DECL (t);
16051 : :
16052 : 262 : if (TREE_CODE (decl) == MEM_REF)
16053 : 8 : decl = TREE_OPERAND (decl, 0);
16054 : :
16055 : 262 : if (VAR_P (decl) && !is_oacc_declared (decl))
16056 : : {
16057 : 262 : tree attr = get_identifier ("oacc declare target");
16058 : 262 : DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
16059 : 262 : DECL_ATTRIBUTES (decl));
16060 : : }
16061 : :
16062 : 262 : if (VAR_P (decl)
16063 : 262 : && !is_global_var (decl)
16064 : 452 : && DECL_CONTEXT (decl) == current_function_decl)
16065 : : {
16066 : 190 : tree c = gimplify_oacc_declare_1 (t);
16067 : 190 : if (c)
16068 : : {
16069 : 116 : if (oacc_declare_returns == NULL)
16070 : 40 : oacc_declare_returns = new hash_map<tree, tree>;
16071 : :
16072 : 116 : oacc_declare_returns->put (decl, c);
16073 : : }
16074 : : }
16075 : :
16076 : 262 : if (gimplify_omp_ctxp)
16077 : 72 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
16078 : : }
16079 : :
16080 : 254 : stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
16081 : : clauses);
16082 : :
16083 : 254 : gimplify_seq_add_stmt (pre_p, stmt);
16084 : :
16085 : 254 : *expr_p = NULL_TREE;
16086 : 254 : }
16087 : :
16088 : : /* Gimplify the contents of an OMP_PARALLEL statement. This involves
16089 : : gimplification of the body, as well as scanning the body for used
16090 : : variables. We need to do this scan now, because variable-sized
16091 : : decls will be decomposed during gimplification. */
16092 : :
16093 : : static void
16094 : 18213 : gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
16095 : : {
16096 : 18213 : tree expr = *expr_p;
16097 : 18213 : gimple *g;
16098 : 18213 : gimple_seq body = NULL;
16099 : :
16100 : 36426 : gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
16101 : 18213 : OMP_PARALLEL_COMBINED (expr)
16102 : : ? ORT_COMBINED_PARALLEL
16103 : : : ORT_PARALLEL, OMP_PARALLEL);
16104 : :
16105 : 18213 : push_gimplify_context ();
16106 : :
16107 : 18213 : g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
16108 : 18213 : if (gimple_code (g) == GIMPLE_BIND)
16109 : 18213 : pop_gimplify_context (g);
16110 : : else
16111 : 0 : pop_gimplify_context (NULL);
16112 : :
16113 : 18213 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
16114 : : OMP_PARALLEL);
16115 : :
16116 : 36426 : g = gimple_build_omp_parallel (body,
16117 : 18213 : OMP_PARALLEL_CLAUSES (expr),
16118 : : NULL_TREE, NULL_TREE);
16119 : 18213 : if (OMP_PARALLEL_COMBINED (expr))
16120 : 12553 : gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
16121 : 18213 : gimplify_seq_add_stmt (pre_p, g);
16122 : 18213 : *expr_p = NULL_TREE;
16123 : 18213 : }
16124 : :
16125 : : /* Gimplify the contents of an OMP_TASK statement. This involves
16126 : : gimplification of the body, as well as scanning the body for used
16127 : : variables. We need to do this scan now, because variable-sized
16128 : : decls will be decomposed during gimplification. */
16129 : :
16130 : : static void
16131 : 4014 : gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
16132 : : {
16133 : 4014 : tree expr = *expr_p;
16134 : 4014 : gimple *g;
16135 : 4014 : gimple_seq body = NULL;
16136 : 4014 : bool nowait = false;
16137 : 4014 : bool has_depend = false;
16138 : :
16139 : 4014 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16140 : : {
16141 : 236 : for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
16142 : 150 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
16143 : : {
16144 : 108 : has_depend = true;
16145 : 108 : if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
16146 : : {
16147 : 9 : error_at (OMP_CLAUSE_LOCATION (c),
16148 : : "%<mutexinoutset%> kind in %<depend%> clause on a "
16149 : : "%<taskwait%> construct");
16150 : 9 : break;
16151 : : }
16152 : : }
16153 : 42 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT)
16154 : 42 : nowait = true;
16155 : 95 : if (nowait && !has_depend)
16156 : : {
16157 : 5 : error_at (EXPR_LOCATION (expr),
16158 : : "%<taskwait%> construct with %<nowait%> clause but no "
16159 : : "%<depend%> clauses");
16160 : 5 : *expr_p = NULL_TREE;
16161 : 5 : return;
16162 : : }
16163 : : }
16164 : :
16165 : 12027 : gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
16166 : 4009 : omp_find_clause (OMP_TASK_CLAUSES (expr),
16167 : : OMP_CLAUSE_UNTIED)
16168 : : ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
16169 : :
16170 : 4009 : if (OMP_TASK_BODY (expr))
16171 : : {
16172 : 3919 : push_gimplify_context ();
16173 : :
16174 : 3919 : g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
16175 : 3919 : if (gimple_code (g) == GIMPLE_BIND)
16176 : 3919 : pop_gimplify_context (g);
16177 : : else
16178 : 0 : pop_gimplify_context (NULL);
16179 : : }
16180 : :
16181 : 4009 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
16182 : : OMP_TASK);
16183 : :
16184 : 8018 : g = gimple_build_omp_task (body,
16185 : 4009 : OMP_TASK_CLAUSES (expr),
16186 : : NULL_TREE, NULL_TREE,
16187 : : NULL_TREE, NULL_TREE, NULL_TREE);
16188 : 4009 : if (OMP_TASK_BODY (expr) == NULL_TREE)
16189 : 90 : gimple_omp_task_set_taskwait_p (g, true);
16190 : 4009 : gimplify_seq_add_stmt (pre_p, g);
16191 : 4009 : *expr_p = NULL_TREE;
16192 : : }
16193 : :
16194 : : /* Helper function for gimplify_omp_for. If *TP is not a gimple constant,
16195 : : force it into a temporary initialized in PRE_P and add firstprivate clause
16196 : : to ORIG_FOR_STMT. */
16197 : :
16198 : : static void
16199 : 4385 : gimplify_omp_taskloop_expr (tree type, tree *tp, gimple_seq *pre_p,
16200 : : tree orig_for_stmt)
16201 : : {
16202 : 4385 : if (*tp == NULL || is_gimple_constant (*tp))
16203 : : return;
16204 : :
16205 : 776 : if (TREE_CODE (*tp) == SAVE_EXPR)
16206 : 135 : gimplify_save_expr (tp, pre_p, NULL);
16207 : : else
16208 : 641 : *tp = get_initialized_tmp_var (*tp, pre_p, NULL, false);
16209 : : /* Reference to pointer conversion is considered useless,
16210 : : but is significant for firstprivate clause. Force it
16211 : : here. */
16212 : 776 : if (type
16213 : 684 : && TREE_CODE (type) == POINTER_TYPE
16214 : 866 : && TREE_CODE (TREE_TYPE (*tp)) == REFERENCE_TYPE)
16215 : : {
16216 : 12 : tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
16217 : 12 : tree m = build2 (INIT_EXPR, TREE_TYPE (v), v, *tp);
16218 : 12 : gimplify_and_add (m, pre_p);
16219 : 12 : *tp = v;
16220 : : }
16221 : :
16222 : 776 : tree c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16223 : 776 : OMP_CLAUSE_DECL (c) = *tp;
16224 : 776 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
16225 : 776 : OMP_FOR_CLAUSES (orig_for_stmt) = c;
16226 : : }
16227 : :
16228 : : /* Helper function of gimplify_omp_for, find OMP_ORDERED with
16229 : : null OMP_ORDERED_BODY inside of OMP_FOR's body. */
16230 : :
16231 : : static tree
16232 : 16453 : find_standalone_omp_ordered (tree *tp, int *walk_subtrees, void *)
16233 : : {
16234 : 16453 : switch (TREE_CODE (*tp))
16235 : : {
16236 : 896 : case OMP_ORDERED:
16237 : 896 : if (OMP_ORDERED_BODY (*tp) == NULL_TREE)
16238 : : return *tp;
16239 : : break;
16240 : 23 : case OMP_SIMD:
16241 : 23 : case OMP_PARALLEL:
16242 : 23 : case OMP_TARGET:
16243 : 23 : *walk_subtrees = 0;
16244 : 23 : break;
16245 : : default:
16246 : : break;
16247 : : }
16248 : : return NULL_TREE;
16249 : : }
16250 : :
16251 : : /* Gimplify standalone loop transforming directive which has the
16252 : : transformations applied already. So, all that is needed is gimplify
16253 : : the remaining loops as normal loops. */
16254 : :
16255 : : static enum gimplify_status
16256 : 2264 : gimplify_omp_loop_xform (tree *expr_p, gimple_seq *pre_p)
16257 : : {
16258 : 2264 : tree for_stmt = *expr_p;
16259 : :
16260 : 2264 : if (OMP_FOR_PRE_BODY (for_stmt))
16261 : 759 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), pre_p);
16262 : :
16263 : 2264 : gimple_seq pre_body = NULL, post_body = NULL;
16264 : 5203 : for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16265 : : {
16266 : 2939 : if (TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i) == NULL_TREE)
16267 : 1707 : continue;
16268 : 1283 : tree iters = NULL_TREE;
16269 : 1283 : if (i == 0
16270 : 826 : && TREE_CODE (for_stmt) == OMP_UNROLL
16271 : 1771 : && !omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_PARTIAL))
16272 : : {
16273 : 311 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_FULL))
16274 : 269 : iters = omp_loop_number_of_iterations (for_stmt, 0, NULL);
16275 : : else
16276 : 42 : iters = build_int_cst (integer_type_node, 8);
16277 : : }
16278 : 1283 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16279 : 1283 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16280 : 1283 : tree decl = TREE_OPERAND (t, 0);
16281 : 1283 : gcc_assert (DECL_P (decl));
16282 : 1283 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16283 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
16284 : 1283 : if (DECL_ARTIFICIAL (decl)
16285 : 876 : && TREE_PRIVATE (t)
16286 : 867 : && gimplify_omp_ctxp
16287 : 1613 : && gimplify_omp_ctxp->region_type != ORT_NONE)
16288 : : {
16289 : : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
16290 : 504 : do
16291 : : {
16292 : 504 : splay_tree_node n
16293 : 504 : = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
16294 : 504 : if (n != NULL)
16295 : : break;
16296 : 346 : else if (ctx->region_type != ORT_WORKSHARE
16297 : : && ctx->region_type != ORT_TASKGROUP
16298 : 62 : && ctx->region_type != ORT_SIMD
16299 : 38 : && ctx->region_type != ORT_ACC
16300 : 38 : && !(ctx->region_type & ORT_TARGET_DATA))
16301 : : {
16302 : 38 : omp_add_variable (ctx, decl, GOVD_PRIVATE);
16303 : 38 : break;
16304 : : }
16305 : 308 : ctx = ctx->outer_context;
16306 : : }
16307 : 308 : while (ctx);
16308 : : }
16309 : 1283 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16310 : : {
16311 : 23 : gcc_assert (seen_error ());
16312 : 23 : continue;
16313 : : }
16314 : 1260 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16315 : : fb_rvalue);
16316 : 1260 : gimplify_and_add (t, &pre_body);
16317 : 1260 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16318 : 1260 : gcc_assert (TREE_OPERAND (t, 0) == decl);
16319 : 1260 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16320 : : {
16321 : 28 : gcc_assert (seen_error ());
16322 : 28 : continue;
16323 : : }
16324 : 1232 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16325 : : fb_rvalue);
16326 : 1232 : tree l1 = create_artificial_label (UNKNOWN_LOCATION);
16327 : 1232 : tree l2 = create_artificial_label (UNKNOWN_LOCATION);
16328 : 1232 : tree l3 = create_artificial_label (UNKNOWN_LOCATION);
16329 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_goto (l2));
16330 : 1232 : gimplify_seq_add_stmt (&pre_body, gimple_build_label (l1));
16331 : 1232 : gimple_seq this_post_body = NULL;
16332 : 1232 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16333 : 1232 : if (TREE_CODE (t) == MODIFY_EXPR)
16334 : : {
16335 : 975 : t = TREE_OPERAND (t, 1);
16336 : 975 : if (TREE_CODE (t) == PLUS_EXPR
16337 : 975 : && TREE_OPERAND (t, 1) == decl)
16338 : : {
16339 : 0 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
16340 : 0 : TREE_OPERAND (t, 0) = decl;
16341 : : }
16342 : 975 : gimplify_expr (&TREE_OPERAND (t, 1), pre_p, NULL, is_gimple_val,
16343 : : fb_rvalue);
16344 : : }
16345 : 1232 : gimplify_and_add (TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i),
16346 : : &this_post_body);
16347 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l2));
16348 : 1232 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16349 : 1232 : gcond *cond = NULL;
16350 : 1232 : tree d = decl;
16351 : 1232 : gimplify_expr (&d, &this_post_body, NULL, is_gimple_val, fb_rvalue);
16352 : 1232 : if (iters && tree_fits_uhwi_p (iters))
16353 : : {
16354 : 299 : unsigned HOST_WIDE_INT niters = tree_to_uhwi (iters);
16355 : 299 : if ((unsigned HOST_WIDE_INT) (int) niters == niters
16356 : 299 : && (int) niters > 0)
16357 : : {
16358 : 299 : t = build2 (TREE_CODE (t), boolean_type_node, d,
16359 : 299 : TREE_OPERAND (t, 1));
16360 : 299 : t = build3 (ANNOTATE_EXPR, TREE_TYPE (t), t,
16361 : : build_int_cst (integer_type_node,
16362 : : annot_expr_unroll_kind),
16363 : 299 : build_int_cst (integer_type_node, niters));
16364 : 299 : gimplify_expr (&t, &this_post_body, NULL, is_gimple_val,
16365 : : fb_rvalue);
16366 : 299 : cond = gimple_build_cond (NE_EXPR, t, boolean_false_node,
16367 : : l1, l3);
16368 : : }
16369 : : }
16370 : 299 : if (cond == NULL)
16371 : 933 : cond = gimple_build_cond (TREE_CODE (t), d, TREE_OPERAND (t, 1),
16372 : : l1, l3);
16373 : 1232 : gimplify_seq_add_stmt (&this_post_body, cond);
16374 : 1232 : gimplify_seq_add_stmt (&this_post_body, gimple_build_label (l3));
16375 : 1232 : gimplify_seq_add_seq (&this_post_body, post_body);
16376 : 1232 : post_body = this_post_body;
16377 : : }
16378 : 2264 : gimplify_seq_add_seq (pre_p, pre_body);
16379 : 2264 : gimplify_and_add (OMP_FOR_BODY (for_stmt), pre_p);
16380 : 2264 : gimplify_seq_add_seq (pre_p, post_body);
16381 : :
16382 : 2264 : *expr_p = NULL_TREE;
16383 : 2264 : return GS_ALL_DONE;
16384 : : }
16385 : :
16386 : : /* Gimplify the gross structure of an OMP_FOR statement. */
16387 : :
16388 : : static enum gimplify_status
16389 : 58103 : gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
16390 : : {
16391 : 58103 : tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
16392 : 58103 : enum gimplify_status ret = GS_ALL_DONE;
16393 : 58103 : enum gimplify_status tret;
16394 : 58103 : gomp_for *gfor;
16395 : 58103 : gimple_seq for_body, for_pre_body;
16396 : 58103 : int i;
16397 : 58103 : bitmap has_decl_expr = NULL;
16398 : 58103 : enum omp_region_type ort = ORT_WORKSHARE;
16399 : 58103 : bool openacc = TREE_CODE (*expr_p) == OACC_LOOP;
16400 : :
16401 : 58103 : orig_for_stmt = for_stmt = *expr_p;
16402 : :
16403 : 58103 : bool loop_p = (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_BIND)
16404 : 58103 : != NULL_TREE);
16405 : 58174 : while (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16406 : : {
16407 : 17760 : tree *data[4] = { NULL, NULL, NULL, NULL };
16408 : 17760 : gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
16409 : 17760 : inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
16410 : : find_combined_omp_for, data, NULL);
16411 : 17760 : if (inner_for_stmt == NULL_TREE)
16412 : : {
16413 : 36 : gcc_assert (seen_error ());
16414 : 36 : *expr_p = NULL_TREE;
16415 : 3743 : return GS_ERROR;
16416 : : }
16417 : 17724 : gcc_assert (inner_for_stmt == *data[3]);
16418 : 17724 : omp_maybe_apply_loop_xforms (data[3],
16419 : 17724 : data[2]
16420 : 4270 : ? OMP_FOR_CLAUSES (*data[2])
16421 : 13454 : : TREE_CODE (for_stmt) == OMP_FOR
16422 : 13454 : ? OMP_FOR_CLAUSES (for_stmt)
16423 : : : NULL_TREE);
16424 : 17724 : if (inner_for_stmt != *data[3])
16425 : 71 : continue;
16426 : 17653 : if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
16427 : : {
16428 : 2 : append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
16429 : : &OMP_FOR_PRE_BODY (for_stmt));
16430 : 2 : OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
16431 : : }
16432 : 17653 : if (OMP_FOR_PRE_BODY (inner_for_stmt))
16433 : : {
16434 : 7224 : append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
16435 : : &OMP_FOR_PRE_BODY (for_stmt));
16436 : 7224 : OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
16437 : : }
16438 : :
16439 : 17653 : if (data[0])
16440 : : {
16441 : : /* We have some statements or variable declarations in between
16442 : : the composite construct directives. Move them around the
16443 : : inner_for_stmt. */
16444 : 3707 : data[0] = expr_p;
16445 : 14828 : for (i = 0; i < 3; i++)
16446 : 11121 : if (data[i])
16447 : : {
16448 : 7037 : tree t = *data[i];
16449 : 7037 : if (i < 2 && data[i + 1] == &OMP_BODY (t))
16450 : 1002 : data[i + 1] = data[i];
16451 : 7037 : *data[i] = OMP_BODY (t);
16452 : 7037 : tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
16453 : : NULL_TREE, make_node (BLOCK));
16454 : 7037 : OMP_BODY (t) = body;
16455 : 7037 : append_to_statement_list_force (inner_for_stmt,
16456 : : &BIND_EXPR_BODY (body));
16457 : 7037 : *data[3] = t;
16458 : 7037 : data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
16459 : 7037 : gcc_assert (*data[3] == inner_for_stmt);
16460 : : }
16461 : : return GS_OK;
16462 : : }
16463 : :
16464 : 36746 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16465 : 22800 : if (!loop_p
16466 : 21945 : && OMP_FOR_ORIG_DECLS (inner_for_stmt)
16467 : 11124 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16468 : : i)) == TREE_LIST
16469 : 22868 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16470 : : i)))
16471 : : {
16472 : 40 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16473 : : /* Class iterators aren't allowed on OMP_SIMD, so the only
16474 : : case we need to solve is distribute parallel for. They are
16475 : : allowed on the loop construct, but that is already handled
16476 : : in gimplify_omp_loop. */
16477 : 40 : gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
16478 : : && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
16479 : : && data[1]);
16480 : 40 : tree orig_decl = TREE_PURPOSE (orig);
16481 : 40 : tree last = TREE_VALUE (orig);
16482 : 40 : tree *pc;
16483 : 40 : for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
16484 : 74 : *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
16485 : 42 : if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16486 : 35 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
16487 : 43 : && OMP_CLAUSE_DECL (*pc) == orig_decl)
16488 : : break;
16489 : 40 : if (*pc == NULL_TREE)
16490 : : {
16491 : 32 : tree *spc;
16492 : 32 : for (spc = &OMP_PARALLEL_CLAUSES (*data[1]);
16493 : 98 : *spc; spc = &OMP_CLAUSE_CHAIN (*spc))
16494 : 67 : if (OMP_CLAUSE_CODE (*spc) == OMP_CLAUSE_PRIVATE
16495 : 67 : && OMP_CLAUSE_DECL (*spc) == orig_decl)
16496 : : break;
16497 : 32 : if (*spc)
16498 : : {
16499 : 1 : tree c = *spc;
16500 : 1 : *spc = OMP_CLAUSE_CHAIN (c);
16501 : 1 : OMP_CLAUSE_CHAIN (c) = NULL_TREE;
16502 : 1 : *pc = c;
16503 : : }
16504 : : }
16505 : 40 : if (*pc == NULL_TREE)
16506 : : ;
16507 : 9 : else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
16508 : : {
16509 : : /* private clause will appear only on inner_for_stmt.
16510 : : Change it into firstprivate, and add private clause
16511 : : on for_stmt. */
16512 : 8 : tree c = copy_node (*pc);
16513 : 8 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16514 : 8 : OMP_FOR_CLAUSES (for_stmt) = c;
16515 : 8 : OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
16516 : 8 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16517 : : }
16518 : : else
16519 : : {
16520 : : /* lastprivate clause will appear on both inner_for_stmt
16521 : : and for_stmt. Add firstprivate clause to
16522 : : inner_for_stmt. */
16523 : 1 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
16524 : : OMP_CLAUSE_FIRSTPRIVATE);
16525 : 1 : OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
16526 : 1 : OMP_CLAUSE_CHAIN (c) = *pc;
16527 : 1 : *pc = c;
16528 : 1 : lang_hooks.decls.omp_finish_clause (*pc, pre_p, openacc);
16529 : : }
16530 : 40 : tree c = build_omp_clause (UNKNOWN_LOCATION,
16531 : : OMP_CLAUSE_FIRSTPRIVATE);
16532 : 40 : OMP_CLAUSE_DECL (c) = last;
16533 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16534 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
16535 : 40 : c = build_omp_clause (UNKNOWN_LOCATION,
16536 : 40 : *pc ? OMP_CLAUSE_SHARED
16537 : : : OMP_CLAUSE_FIRSTPRIVATE);
16538 : 40 : OMP_CLAUSE_DECL (c) = orig_decl;
16539 : 40 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16540 : 40 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
16541 : : }
16542 : : /* Similarly, take care of C++ range for temporaries, those should
16543 : : be firstprivate on OMP_PARALLEL if any. */
16544 : 13946 : if (data[1])
16545 : 17540 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
16546 : 11120 : if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
16547 : 5753 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16548 : : i)) == TREE_LIST
16549 : 11196 : && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
16550 : : i)))
16551 : : {
16552 : 49 : tree orig
16553 : 49 : = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
16554 : 49 : tree v = TREE_CHAIN (orig);
16555 : 49 : tree c = build_omp_clause (UNKNOWN_LOCATION,
16556 : : OMP_CLAUSE_FIRSTPRIVATE);
16557 : : /* First add firstprivate clause for the __for_end artificial
16558 : : decl. */
16559 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
16560 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
16561 : : == REFERENCE_TYPE)
16562 : 0 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
16563 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16564 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
16565 : 49 : if (TREE_VEC_ELT (v, 0))
16566 : : {
16567 : : /* And now the same for __for_range artificial decl if it
16568 : : exists. */
16569 : 49 : c = build_omp_clause (UNKNOWN_LOCATION,
16570 : : OMP_CLAUSE_FIRSTPRIVATE);
16571 : 49 : OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
16572 : 49 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
16573 : : == REFERENCE_TYPE)
16574 : 49 : OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
16575 : 49 : OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
16576 : 49 : OMP_PARALLEL_CLAUSES (*data[1]) = c;
16577 : : }
16578 : : }
16579 : 13946 : break;
16580 : : }
16581 : 54360 : if (OMP_FOR_INIT (for_stmt) != NULL_TREE)
16582 : : {
16583 : 40414 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
16584 : 40414 : if (*expr_p != for_stmt)
16585 : : return GS_OK;
16586 : : }
16587 : :
16588 : 53478 : switch (TREE_CODE (for_stmt))
16589 : : {
16590 : 18177 : case OMP_FOR:
16591 : 30390 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
16592 : : {
16593 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16594 : : OMP_CLAUSE_SCHEDULE))
16595 : 47 : error_at (EXPR_LOCATION (for_stmt),
16596 : : "%qs clause may not appear on non-rectangular %qs",
16597 : 27 : "schedule", lang_GNU_Fortran () ? "do" : "for");
16598 : 458 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED))
16599 : 34 : error_at (EXPR_LOCATION (for_stmt),
16600 : : "%qs clause may not appear on non-rectangular %qs",
16601 : 18 : "ordered", lang_GNU_Fortran () ? "do" : "for");
16602 : : }
16603 : : break;
16604 : 8244 : case OMP_DISTRIBUTE:
16605 : 9155 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt)
16606 : 8244 : && omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16607 : : OMP_CLAUSE_DIST_SCHEDULE))
16608 : 29 : error_at (EXPR_LOCATION (for_stmt),
16609 : : "%qs clause may not appear on non-rectangular %qs",
16610 : : "dist_schedule", "distribute");
16611 : : break;
16612 : : case OACC_LOOP:
16613 : : ort = ORT_ACC;
16614 : : break;
16615 : 1579 : case OMP_TASKLOOP:
16616 : 2509 : if (OMP_FOR_NON_RECTANGULAR (inner_for_stmt ? inner_for_stmt : for_stmt))
16617 : : {
16618 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16619 : : OMP_CLAUSE_GRAINSIZE))
16620 : 11 : error_at (EXPR_LOCATION (for_stmt),
16621 : : "%qs clause may not appear on non-rectangular %qs",
16622 : : "grainsize", "taskloop");
16623 : 36 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16624 : : OMP_CLAUSE_NUM_TASKS))
16625 : 6 : error_at (EXPR_LOCATION (for_stmt),
16626 : : "%qs clause may not appear on non-rectangular %qs",
16627 : : "num_tasks", "taskloop");
16628 : : }
16629 : 1579 : if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
16630 : : ort = ORT_UNTIED_TASKLOOP;
16631 : : else
16632 : : ort = ORT_TASKLOOP;
16633 : : break;
16634 : 10641 : case OMP_SIMD:
16635 : 10641 : ort = ORT_SIMD;
16636 : 10641 : break;
16637 : 2264 : case OMP_TILE:
16638 : 2264 : case OMP_UNROLL:
16639 : 2264 : gcc_assert (inner_for_stmt == NULL_TREE);
16640 : 2264 : return gimplify_omp_loop_xform (expr_p, pre_p);
16641 : 0 : default:
16642 : 0 : gcc_unreachable ();
16643 : : }
16644 : :
16645 : : /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
16646 : : clause for the IV. */
16647 : 10688 : if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
16648 : : {
16649 : 7829 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
16650 : 7829 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16651 : 7829 : decl = TREE_OPERAND (t, 0);
16652 : 18659 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
16653 : 11944 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16654 : 11944 : && OMP_CLAUSE_DECL (c) == decl)
16655 : : {
16656 : 1114 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
16657 : 1114 : break;
16658 : : }
16659 : : }
16660 : :
16661 : 51214 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
16662 : 51538 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
16663 : 1903 : loop_p && TREE_CODE (for_stmt) != OMP_SIMD
16664 : : ? OMP_LOOP : TREE_CODE (for_stmt));
16665 : :
16666 : 51214 : if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
16667 : 8244 : gimplify_omp_ctxp->distribute = true;
16668 : :
16669 : : /* Handle OMP_FOR_INIT. */
16670 : 51214 : for_pre_body = NULL;
16671 : 51214 : if ((ort == ORT_SIMD
16672 : 40573 : || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
16673 : 61940 : && OMP_FOR_PRE_BODY (for_stmt))
16674 : : {
16675 : 6776 : has_decl_expr = BITMAP_ALLOC (NULL);
16676 : 6776 : if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
16677 : 6776 : && VAR_P (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt))))
16678 : : {
16679 : 18 : t = OMP_FOR_PRE_BODY (for_stmt);
16680 : 18 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
16681 : : }
16682 : 6758 : else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
16683 : : {
16684 : 6756 : tree_stmt_iterator si;
16685 : 11206 : for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
16686 : 4450 : tsi_next (&si))
16687 : : {
16688 : 4450 : t = tsi_stmt (si);
16689 : 4450 : if (TREE_CODE (t) == DECL_EXPR
16690 : 4450 : && VAR_P (DECL_EXPR_DECL (t)))
16691 : 4377 : bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
16692 : : }
16693 : : }
16694 : : }
16695 : 51214 : if (OMP_FOR_PRE_BODY (for_stmt))
16696 : : {
16697 : 21005 : if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
16698 : 20284 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
16699 : : else
16700 : : {
16701 : 721 : struct gimplify_omp_ctx ctx;
16702 : 721 : memset (&ctx, 0, sizeof (ctx));
16703 : 721 : ctx.region_type = ORT_NONE;
16704 : 721 : gimplify_omp_ctxp = &ctx;
16705 : 721 : gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
16706 : 721 : gimplify_omp_ctxp = NULL;
16707 : : }
16708 : : }
16709 : 51214 : OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
16710 : :
16711 : 51214 : if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
16712 : 13946 : for_stmt = inner_for_stmt;
16713 : :
16714 : : /* For taskloop, need to gimplify the start, end and step before the
16715 : : taskloop, outside of the taskloop omp context. */
16716 : 51214 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
16717 : : {
16718 : 3444 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16719 : : {
16720 : 1865 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16721 : 1865 : gimple_seq *for_pre_p = (gimple_seq_empty_p (for_pre_body)
16722 : 1865 : ? pre_p : &for_pre_body);
16723 : 1865 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
16724 : 1865 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16725 : : {
16726 : 34 : tree v = TREE_OPERAND (t, 1);
16727 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
16728 : : for_pre_p, orig_for_stmt);
16729 : 34 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
16730 : : for_pre_p, orig_for_stmt);
16731 : : }
16732 : : else
16733 : 1831 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
16734 : : orig_for_stmt);
16735 : :
16736 : : /* Handle OMP_FOR_COND. */
16737 : 1865 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
16738 : 1865 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
16739 : : {
16740 : 31 : tree v = TREE_OPERAND (t, 1);
16741 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 1),
16742 : : for_pre_p, orig_for_stmt);
16743 : 31 : gimplify_omp_taskloop_expr (type, &TREE_VEC_ELT (v, 2),
16744 : : for_pre_p, orig_for_stmt);
16745 : : }
16746 : : else
16747 : 1834 : gimplify_omp_taskloop_expr (type, &TREE_OPERAND (t, 1), for_pre_p,
16748 : : orig_for_stmt);
16749 : :
16750 : : /* Handle OMP_FOR_INCR. */
16751 : 1865 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
16752 : 1865 : if (TREE_CODE (t) == MODIFY_EXPR)
16753 : : {
16754 : 590 : decl = TREE_OPERAND (t, 0);
16755 : 590 : t = TREE_OPERAND (t, 1);
16756 : 590 : tree *tp = &TREE_OPERAND (t, 1);
16757 : 590 : if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
16758 : 22 : tp = &TREE_OPERAND (t, 0);
16759 : :
16760 : 590 : gimplify_omp_taskloop_expr (NULL_TREE, tp, for_pre_p,
16761 : : orig_for_stmt);
16762 : : }
16763 : : }
16764 : :
16765 : 1579 : gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
16766 : : OMP_TASKLOOP);
16767 : : }
16768 : :
16769 : 51214 : if (orig_for_stmt != for_stmt)
16770 : 13946 : gimplify_omp_ctxp->combined_loop = true;
16771 : :
16772 : 51214 : for_body = NULL;
16773 : 51214 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16774 : : == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
16775 : 51214 : gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16776 : : == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
16777 : :
16778 : 51214 : tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
16779 : 51214 : bool is_doacross = false;
16780 : 51214 : if (c && walk_tree_without_duplicates (&OMP_FOR_BODY (for_stmt),
16781 : : find_standalone_omp_ordered, NULL))
16782 : : {
16783 : 526 : OMP_CLAUSE_ORDERED_DOACROSS (c) = 1;
16784 : 526 : is_doacross = true;
16785 : 526 : int len = TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt));
16786 : 526 : gimplify_omp_ctxp->loop_iter_var.create (len * 2);
16787 : 2190 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
16788 : 1664 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
16789 : : {
16790 : 10 : error_at (OMP_CLAUSE_LOCATION (*pc),
16791 : : "%<linear%> clause may not be specified together "
16792 : : "with %<ordered%> clause if stand-alone %<ordered%> "
16793 : : "construct is nested in it");
16794 : 10 : *pc = OMP_CLAUSE_CHAIN (*pc);
16795 : : }
16796 : : else
16797 : 1654 : pc = &OMP_CLAUSE_CHAIN (*pc);
16798 : : }
16799 : 51214 : int collapse = 1, tile = 0;
16800 : 51214 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
16801 : 51214 : if (c)
16802 : 13657 : collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
16803 : 51214 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
16804 : 51214 : if (c)
16805 : 384 : tile = list_length (OMP_CLAUSE_TILE_LIST (c));
16806 : 51214 : c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ALLOCATE);
16807 : 51214 : hash_set<tree> *allocate_uids = NULL;
16808 : 51214 : if (c)
16809 : : {
16810 : 275 : allocate_uids = new hash_set<tree>;
16811 : 1682 : for (; c; c = OMP_CLAUSE_CHAIN (c))
16812 : 1132 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE)
16813 : 478 : allocate_uids->add (OMP_CLAUSE_DECL (c));
16814 : : }
16815 : 124101 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
16816 : : {
16817 : 72887 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
16818 : 72887 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
16819 : 72887 : decl = TREE_OPERAND (t, 0);
16820 : 72887 : gcc_assert (DECL_P (decl));
16821 : 72887 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
16822 : : || POINTER_TYPE_P (TREE_TYPE (decl)));
16823 : 72887 : if (is_doacross)
16824 : : {
16825 : 1649 : if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
16826 : : {
16827 : 956 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16828 : 956 : if (TREE_CODE (orig_decl) == TREE_LIST)
16829 : : {
16830 : 15 : orig_decl = TREE_PURPOSE (orig_decl);
16831 : 15 : if (!orig_decl)
16832 : 0 : orig_decl = decl;
16833 : : }
16834 : 956 : gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
16835 : : }
16836 : : else
16837 : 693 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
16838 : 1649 : gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
16839 : : }
16840 : :
16841 : 72887 : if (for_stmt == orig_for_stmt)
16842 : : {
16843 : 50087 : tree orig_decl = decl;
16844 : 50087 : if (OMP_FOR_ORIG_DECLS (for_stmt))
16845 : : {
16846 : 23925 : tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16847 : 23925 : if (TREE_CODE (orig_decl) == TREE_LIST)
16848 : : {
16849 : : orig_decl = TREE_PURPOSE (orig_decl);
16850 : : if (!orig_decl)
16851 : : orig_decl = decl;
16852 : : }
16853 : : }
16854 : 50087 : if (is_global_var (orig_decl) && DECL_THREAD_LOCAL_P (orig_decl))
16855 : 36 : error_at (EXPR_LOCATION (for_stmt),
16856 : : "threadprivate iteration variable %qD", orig_decl);
16857 : : }
16858 : :
16859 : : /* Make sure the iteration variable is private. */
16860 : 72887 : tree c = NULL_TREE;
16861 : 72887 : tree c2 = NULL_TREE;
16862 : 72887 : if (orig_for_stmt != for_stmt)
16863 : : {
16864 : : /* Preserve this information until we gimplify the inner simd. */
16865 : 22800 : if (has_decl_expr
16866 : 22800 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
16867 : 3396 : TREE_PRIVATE (t) = 1;
16868 : : }
16869 : 50087 : else if (ort == ORT_SIMD)
16870 : : {
16871 : 15697 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
16872 : : (splay_tree_key) decl);
16873 : 15697 : omp_is_private (gimplify_omp_ctxp, decl,
16874 : 15697 : 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
16875 : : != 1));
16876 : 15697 : if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
16877 : : {
16878 : 2512 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
16879 : 2512 : if (n->value & GOVD_LASTPRIVATE_CONDITIONAL)
16880 : 0 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16881 : : OMP_CLAUSE_LASTPRIVATE);
16882 : 0 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
16883 : : OMP_CLAUSE_LASTPRIVATE))
16884 : 0 : if (OMP_CLAUSE_DECL (c3) == decl)
16885 : : {
16886 : 0 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
16887 : : "conditional %<lastprivate%> on loop "
16888 : : "iterator %qD ignored", decl);
16889 : 0 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
16890 : 0 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
16891 : : }
16892 : : }
16893 : 13185 : else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1 && !loop_p)
16894 : : {
16895 : 5631 : c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
16896 : 5631 : OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
16897 : 5631 : unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
16898 : 5631 : if ((has_decl_expr
16899 : 1306 : && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
16900 : 6315 : || TREE_PRIVATE (t))
16901 : : {
16902 : 2277 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16903 : 2277 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16904 : : }
16905 : 5631 : struct gimplify_omp_ctx *outer
16906 : 5631 : = gimplify_omp_ctxp->outer_context;
16907 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
16908 : : {
16909 : 2433 : if (outer->region_type == ORT_WORKSHARE
16910 : 2193 : && outer->combined_loop)
16911 : : {
16912 : 2142 : n = splay_tree_lookup (outer->variables,
16913 : : (splay_tree_key)decl);
16914 : 2142 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
16915 : : {
16916 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16917 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16918 : : }
16919 : : else
16920 : : {
16921 : 2142 : struct gimplify_omp_ctx *octx = outer->outer_context;
16922 : 2142 : if (octx
16923 : 1714 : && octx->region_type == ORT_COMBINED_PARALLEL
16924 : 1476 : && octx->outer_context
16925 : 1209 : && (octx->outer_context->region_type
16926 : : == ORT_WORKSHARE)
16927 : 1079 : && octx->outer_context->combined_loop)
16928 : : {
16929 : 1079 : octx = octx->outer_context;
16930 : 1079 : n = splay_tree_lookup (octx->variables,
16931 : : (splay_tree_key)decl);
16932 : 1079 : if (n != NULL && (n->value & GOVD_LOCAL) != 0)
16933 : : {
16934 : 0 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
16935 : 0 : flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
16936 : : }
16937 : : }
16938 : : }
16939 : : }
16940 : : }
16941 : :
16942 : 5631 : OMP_CLAUSE_DECL (c) = decl;
16943 : 5631 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16944 : 5631 : OMP_FOR_CLAUSES (for_stmt) = c;
16945 : 5631 : omp_add_variable (gimplify_omp_ctxp, decl, flags);
16946 : 5631 : if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
16947 : 2433 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
16948 : : true);
16949 : : }
16950 : : else
16951 : : {
16952 : 7554 : bool lastprivate
16953 : : = (!has_decl_expr
16954 : 7554 : || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
16955 : 7554 : if (TREE_PRIVATE (t))
16956 : 1771 : lastprivate = false;
16957 : 7554 : if (loop_p && OMP_FOR_ORIG_DECLS (for_stmt))
16958 : : {
16959 : 507 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
16960 : 507 : if (TREE_CODE (elt) == TREE_LIST && TREE_PURPOSE (elt))
16961 : : lastprivate = false;
16962 : : }
16963 : :
16964 : 7554 : struct gimplify_omp_ctx *outer
16965 : 7554 : = gimplify_omp_ctxp->outer_context;
16966 : 7554 : if (outer && lastprivate)
16967 : 4890 : omp_lastprivate_for_combined_outer_constructs (outer, decl,
16968 : : true);
16969 : :
16970 : 9544 : c = build_omp_clause (input_location,
16971 : : lastprivate ? OMP_CLAUSE_LASTPRIVATE
16972 : : : OMP_CLAUSE_PRIVATE);
16973 : 7554 : OMP_CLAUSE_DECL (c) = decl;
16974 : 7554 : OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
16975 : 7554 : OMP_FOR_CLAUSES (for_stmt) = c;
16976 : 7554 : omp_add_variable (gimplify_omp_ctxp, decl,
16977 : : (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
16978 : : | GOVD_EXPLICIT | GOVD_SEEN);
16979 : 7554 : c = NULL_TREE;
16980 : : }
16981 : : }
16982 : 34390 : else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
16983 : : {
16984 : 8711 : omp_notice_variable (gimplify_omp_ctxp, decl, true);
16985 : 8711 : splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
16986 : : (splay_tree_key) decl);
16987 : 8711 : if (n && (n->value & GOVD_LASTPRIVATE_CONDITIONAL))
16988 : 80 : for (tree c3 = omp_find_clause (OMP_FOR_CLAUSES (for_stmt),
16989 : : OMP_CLAUSE_LASTPRIVATE);
16990 : 80 : c3; c3 = omp_find_clause (OMP_CLAUSE_CHAIN (c3),
16991 : : OMP_CLAUSE_LASTPRIVATE))
16992 : 40 : if (OMP_CLAUSE_DECL (c3) == decl)
16993 : : {
16994 : 40 : warning_at (OMP_CLAUSE_LOCATION (c3), OPT_Wopenmp,
16995 : : "conditional %<lastprivate%> on loop "
16996 : : "iterator %qD ignored", decl);
16997 : 40 : OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c3) = 0;
16998 : 40 : n->value &= ~GOVD_LASTPRIVATE_CONDITIONAL;
16999 : : }
17000 : : }
17001 : : else
17002 : 25679 : omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
17003 : :
17004 : : /* If DECL is not a gimple register, create a temporary variable to act
17005 : : as an iteration counter. This is valid, since DECL cannot be
17006 : : modified in the body of the loop. Similarly for any iteration vars
17007 : : in simd with collapse > 1 where the iterator vars must be
17008 : : lastprivate. And similarly for vars mentioned in allocate clauses. */
17009 : 72887 : if (orig_for_stmt != for_stmt)
17010 : 22800 : var = decl;
17011 : 50087 : else if (!is_gimple_reg (decl)
17012 : 46815 : || (ort == ORT_SIMD
17013 : 14098 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
17014 : 89865 : || (allocate_uids && allocate_uids->contains (decl)))
17015 : : {
17016 : 10341 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17017 : : /* Make sure omp_add_variable is not called on it prematurely.
17018 : : We call it ourselves a few lines later. */
17019 : 10341 : gimplify_omp_ctxp = NULL;
17020 : 10341 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17021 : 10341 : gimplify_omp_ctxp = ctx;
17022 : 10341 : TREE_OPERAND (t, 0) = var;
17023 : :
17024 : 10341 : gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
17025 : :
17026 : 10341 : if (ort == ORT_SIMD
17027 : 10341 : && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
17028 : : {
17029 : 768 : c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
17030 : 768 : OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
17031 : 768 : OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
17032 : 768 : OMP_CLAUSE_DECL (c2) = var;
17033 : 768 : OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
17034 : 768 : OMP_FOR_CLAUSES (for_stmt) = c2;
17035 : 768 : omp_add_variable (gimplify_omp_ctxp, var,
17036 : : GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
17037 : 768 : if (c == NULL_TREE)
17038 : : {
17039 : 518 : c = c2;
17040 : 518 : c2 = NULL_TREE;
17041 : : }
17042 : : }
17043 : : else
17044 : 9573 : omp_add_variable (gimplify_omp_ctxp, var,
17045 : : GOVD_PRIVATE | GOVD_SEEN);
17046 : : }
17047 : : else
17048 : : var = decl;
17049 : :
17050 : 72887 : gimplify_omp_ctxp->in_for_exprs = true;
17051 : 72887 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17052 : : {
17053 : 719 : tree lb = TREE_OPERAND (t, 1);
17054 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 1), &for_pre_body, NULL,
17055 : : is_gimple_val, fb_rvalue, false);
17056 : 719 : ret = MIN (ret, tret);
17057 : 719 : tret = gimplify_expr (&TREE_VEC_ELT (lb, 2), &for_pre_body, NULL,
17058 : : is_gimple_val, fb_rvalue, false);
17059 : : }
17060 : : else
17061 : 72168 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17062 : : is_gimple_val, fb_rvalue, false);
17063 : 72887 : gimplify_omp_ctxp->in_for_exprs = false;
17064 : 72887 : ret = MIN (ret, tret);
17065 : 72887 : if (ret == GS_ERROR)
17066 : : return ret;
17067 : :
17068 : : /* Handle OMP_FOR_COND. */
17069 : 72887 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17070 : 72887 : gcc_assert (COMPARISON_CLASS_P (t));
17071 : 72887 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17072 : :
17073 : 72887 : gimplify_omp_ctxp->in_for_exprs = true;
17074 : 72887 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC)
17075 : : {
17076 : 599 : tree ub = TREE_OPERAND (t, 1);
17077 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 1), &for_pre_body, NULL,
17078 : : is_gimple_val, fb_rvalue, false);
17079 : 599 : ret = MIN (ret, tret);
17080 : 599 : tret = gimplify_expr (&TREE_VEC_ELT (ub, 2), &for_pre_body, NULL,
17081 : : is_gimple_val, fb_rvalue, false);
17082 : : }
17083 : : else
17084 : 72288 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17085 : : is_gimple_val, fb_rvalue, false);
17086 : 72887 : gimplify_omp_ctxp->in_for_exprs = false;
17087 : 72887 : ret = MIN (ret, tret);
17088 : :
17089 : : /* Handle OMP_FOR_INCR. */
17090 : 72887 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17091 : 72887 : switch (TREE_CODE (t))
17092 : : {
17093 : 35676 : case PREINCREMENT_EXPR:
17094 : 35676 : case POSTINCREMENT_EXPR:
17095 : 35676 : {
17096 : 35676 : tree decl = TREE_OPERAND (t, 0);
17097 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17098 : : called to massage things appropriately. */
17099 : 35676 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17100 : :
17101 : 35676 : if (orig_for_stmt != for_stmt)
17102 : : break;
17103 : 25293 : t = build_int_cst (TREE_TYPE (decl), 1);
17104 : 25293 : if (c)
17105 : 4466 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17106 : 25293 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17107 : 25293 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17108 : 25293 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17109 : 25293 : break;
17110 : : }
17111 : :
17112 : 3826 : case PREDECREMENT_EXPR:
17113 : 3826 : case POSTDECREMENT_EXPR:
17114 : : /* c_omp_for_incr_canonicalize_ptr() should have been
17115 : : called to massage things appropriately. */
17116 : 3826 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
17117 : 3826 : if (orig_for_stmt != for_stmt)
17118 : : break;
17119 : 1988 : t = build_int_cst (TREE_TYPE (decl), -1);
17120 : 1988 : if (c)
17121 : 112 : OMP_CLAUSE_LINEAR_STEP (c) = t;
17122 : 1988 : t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
17123 : 1988 : t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
17124 : 1988 : TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
17125 : 1988 : break;
17126 : :
17127 : 33385 : case MODIFY_EXPR:
17128 : 33385 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17129 : 33385 : TREE_OPERAND (t, 0) = var;
17130 : :
17131 : 33385 : t = TREE_OPERAND (t, 1);
17132 : 33385 : switch (TREE_CODE (t))
17133 : : {
17134 : 26460 : case PLUS_EXPR:
17135 : 26460 : if (TREE_OPERAND (t, 1) == decl)
17136 : : {
17137 : 215 : TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
17138 : 215 : TREE_OPERAND (t, 0) = var;
17139 : 215 : break;
17140 : : }
17141 : :
17142 : : /* Fallthru. */
17143 : 33170 : case MINUS_EXPR:
17144 : 33170 : case POINTER_PLUS_EXPR:
17145 : 33170 : gcc_assert (TREE_OPERAND (t, 0) == decl);
17146 : 33170 : TREE_OPERAND (t, 0) = var;
17147 : 33170 : break;
17148 : 0 : default:
17149 : 0 : gcc_unreachable ();
17150 : : }
17151 : :
17152 : 33385 : gimplify_omp_ctxp->in_for_exprs = true;
17153 : 33385 : tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
17154 : : is_gimple_val, fb_rvalue, false);
17155 : 33385 : ret = MIN (ret, tret);
17156 : 33385 : if (c)
17157 : : {
17158 : 1571 : tree step = TREE_OPERAND (t, 1);
17159 : 1571 : tree stept = TREE_TYPE (decl);
17160 : 1571 : if (POINTER_TYPE_P (stept))
17161 : 280 : stept = sizetype;
17162 : 1571 : step = fold_convert (stept, step);
17163 : 1571 : if (TREE_CODE (t) == MINUS_EXPR)
17164 : 295 : step = fold_build1 (NEGATE_EXPR, stept, step);
17165 : 1571 : OMP_CLAUSE_LINEAR_STEP (c) = step;
17166 : 1571 : if (step != TREE_OPERAND (t, 1))
17167 : : {
17168 : 295 : tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
17169 : : &for_pre_body, NULL,
17170 : : is_gimple_val, fb_rvalue, false);
17171 : 295 : ret = MIN (ret, tret);
17172 : : }
17173 : : }
17174 : 33385 : gimplify_omp_ctxp->in_for_exprs = false;
17175 : 33385 : break;
17176 : :
17177 : 0 : default:
17178 : 0 : gcc_unreachable ();
17179 : : }
17180 : :
17181 : 72887 : if (c2)
17182 : : {
17183 : 250 : gcc_assert (c);
17184 : 250 : OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
17185 : : }
17186 : :
17187 : 72887 : if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
17188 : : {
17189 : 86401 : for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
17190 : 64917 : if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17191 : 16198 : && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
17192 : 55919 : || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17193 : 2155 : && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
17194 : 1371 : && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
17195 : 75264 : && OMP_CLAUSE_DECL (c) == decl)
17196 : : {
17197 : 7522 : if (is_doacross && (collapse == 1 || i >= collapse))
17198 : : t = var;
17199 : : else
17200 : : {
17201 : 7449 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17202 : 7449 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17203 : 7449 : gcc_assert (TREE_OPERAND (t, 0) == var);
17204 : 7449 : t = TREE_OPERAND (t, 1);
17205 : 7449 : gcc_assert (TREE_CODE (t) == PLUS_EXPR
17206 : : || TREE_CODE (t) == MINUS_EXPR
17207 : : || TREE_CODE (t) == POINTER_PLUS_EXPR);
17208 : 7449 : gcc_assert (TREE_OPERAND (t, 0) == var);
17209 : 14796 : t = build2 (TREE_CODE (t), TREE_TYPE (decl),
17210 : : is_doacross ? var : decl,
17211 : 7449 : TREE_OPERAND (t, 1));
17212 : : }
17213 : 7522 : gimple_seq *seq;
17214 : 7522 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
17215 : 6928 : seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
17216 : : else
17217 : 594 : seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
17218 : 7522 : push_gimplify_context ();
17219 : 7522 : gimplify_assign (decl, t, seq);
17220 : 7522 : gimple *bind = NULL;
17221 : 7522 : if (gimplify_ctxp->temps)
17222 : : {
17223 : 2070 : bind = gimple_build_bind (NULL_TREE, *seq, NULL_TREE);
17224 : 2070 : *seq = NULL;
17225 : 2070 : gimplify_seq_add_stmt (seq, bind);
17226 : : }
17227 : 7522 : pop_gimplify_context (bind);
17228 : : }
17229 : : }
17230 : 72887 : if (OMP_FOR_NON_RECTANGULAR (for_stmt) && var != decl)
17231 : 2609 : for (int j = i + 1; j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17232 : : {
17233 : 1197 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17234 : 1197 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17235 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17236 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17237 : 336 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17238 : 1197 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17239 : 1197 : gcc_assert (COMPARISON_CLASS_P (t));
17240 : 1197 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17241 : 1197 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17242 : 343 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17243 : : }
17244 : : }
17245 : :
17246 : 51214 : BITMAP_FREE (has_decl_expr);
17247 : 51489 : delete allocate_uids;
17248 : :
17249 : 51214 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17250 : 49635 : || (loop_p && orig_for_stmt == for_stmt))
17251 : : {
17252 : 2636 : push_gimplify_context ();
17253 : 2636 : if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
17254 : : {
17255 : 1888 : OMP_FOR_BODY (orig_for_stmt)
17256 : 1888 : = build3 (BIND_EXPR, void_type_node, NULL,
17257 : 1888 : OMP_FOR_BODY (orig_for_stmt), NULL);
17258 : 1888 : TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
17259 : : }
17260 : : }
17261 : :
17262 : 51214 : gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
17263 : : &for_body);
17264 : :
17265 : 51214 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP
17266 : 49635 : || (loop_p && orig_for_stmt == for_stmt))
17267 : : {
17268 : 2636 : if (gimple_code (g) == GIMPLE_BIND)
17269 : 2636 : pop_gimplify_context (g);
17270 : : else
17271 : 0 : pop_gimplify_context (NULL);
17272 : : }
17273 : :
17274 : 51214 : if (orig_for_stmt != for_stmt)
17275 : 36746 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17276 : : {
17277 : 22800 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17278 : 22800 : decl = TREE_OPERAND (t, 0);
17279 : 22800 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17280 : 22800 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17281 : 772 : gimplify_omp_ctxp = ctx->outer_context;
17282 : 22800 : var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
17283 : 22800 : gimplify_omp_ctxp = ctx;
17284 : 22800 : omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
17285 : 22800 : TREE_OPERAND (t, 0) = var;
17286 : 22800 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17287 : 22800 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17288 : 22800 : TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
17289 : 22800 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17290 : 791 : for (int j = i + 1;
17291 : 791 : j < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); j++)
17292 : : {
17293 : 377 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), j);
17294 : 377 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17295 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17296 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17297 : : {
17298 : 133 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17299 : 133 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17300 : : }
17301 : 377 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), j);
17302 : 377 : gcc_assert (COMPARISON_CLASS_P (t));
17303 : 377 : if (TREE_CODE (TREE_OPERAND (t, 1)) == TREE_VEC
17304 : 377 : && TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) == decl)
17305 : : {
17306 : 67 : TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
17307 : 67 : TREE_VEC_ELT (TREE_OPERAND (t, 1), 0) = var;
17308 : : }
17309 : : }
17310 : : }
17311 : :
17312 : 51214 : gimplify_adjust_omp_clauses (pre_p, for_body,
17313 : : &OMP_FOR_CLAUSES (orig_for_stmt),
17314 : 51214 : TREE_CODE (orig_for_stmt));
17315 : :
17316 : 51214 : int kind;
17317 : 51214 : switch (TREE_CODE (orig_for_stmt))
17318 : : {
17319 : : case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
17320 : 10641 : case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
17321 : 8244 : case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
17322 : 1579 : case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
17323 : 12573 : case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
17324 : 0 : default:
17325 : 0 : gcc_unreachable ();
17326 : : }
17327 : 51214 : if (loop_p && kind == GF_OMP_FOR_KIND_SIMD)
17328 : : {
17329 : 1057 : gimplify_seq_add_seq (pre_p, for_pre_body);
17330 : 1057 : for_pre_body = NULL;
17331 : : }
17332 : 51214 : gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
17333 : 51214 : TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
17334 : : for_pre_body);
17335 : 51214 : if (orig_for_stmt != for_stmt)
17336 : 13946 : gimple_omp_for_set_combined_p (gfor, true);
17337 : 51214 : if (gimplify_omp_ctxp
17338 : 42626 : && (gimplify_omp_ctxp->combined_loop
17339 : 35100 : || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
17340 : 12316 : && gimplify_omp_ctxp->outer_context
17341 : 7887 : && gimplify_omp_ctxp->outer_context->combined_loop)))
17342 : : {
17343 : 13946 : gimple_omp_for_set_combined_into_p (gfor, true);
17344 : 13946 : if (gimplify_omp_ctxp->combined_loop)
17345 : 7526 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
17346 : : else
17347 : 6420 : gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
17348 : : }
17349 : :
17350 : 124101 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17351 : : {
17352 : 72887 : t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17353 : 72887 : gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
17354 : 72887 : gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
17355 : 72887 : t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
17356 : 72887 : gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
17357 : 72887 : gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
17358 : 72887 : t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
17359 : 72887 : gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
17360 : : }
17361 : :
17362 : : /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
17363 : : constructs with GIMPLE_OMP_TASK sandwiched in between them.
17364 : : The outer taskloop stands for computing the number of iterations,
17365 : : counts for collapsed loops and holding taskloop specific clauses.
17366 : : The task construct stands for the effect of data sharing on the
17367 : : explicit task it creates and the inner taskloop stands for expansion
17368 : : of the static loop inside of the explicit task construct. */
17369 : 51214 : if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
17370 : : {
17371 : 1579 : tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
17372 : 1579 : tree task_clauses = NULL_TREE;
17373 : 1579 : tree c = *gfor_clauses_ptr;
17374 : 1579 : tree *gtask_clauses_ptr = &task_clauses;
17375 : 1579 : tree outer_for_clauses = NULL_TREE;
17376 : 1579 : tree *gforo_clauses_ptr = &outer_for_clauses;
17377 : 1579 : bitmap lastprivate_uids = NULL;
17378 : 1579 : if (omp_find_clause (c, OMP_CLAUSE_ALLOCATE))
17379 : : {
17380 : 36 : c = omp_find_clause (c, OMP_CLAUSE_LASTPRIVATE);
17381 : 36 : if (c)
17382 : : {
17383 : 18 : lastprivate_uids = BITMAP_ALLOC (NULL);
17384 : 54 : for (; c; c = omp_find_clause (OMP_CLAUSE_CHAIN (c),
17385 : : OMP_CLAUSE_LASTPRIVATE))
17386 : 18 : bitmap_set_bit (lastprivate_uids,
17387 : 18 : DECL_UID (OMP_CLAUSE_DECL (c)));
17388 : : }
17389 : 36 : c = *gfor_clauses_ptr;
17390 : : }
17391 : 12185 : for (; c; c = OMP_CLAUSE_CHAIN (c))
17392 : 10606 : switch (OMP_CLAUSE_CODE (c))
17393 : : {
17394 : : /* These clauses are allowed on task, move them there. */
17395 : 6190 : case OMP_CLAUSE_SHARED:
17396 : 6190 : case OMP_CLAUSE_FIRSTPRIVATE:
17397 : 6190 : case OMP_CLAUSE_DEFAULT:
17398 : 6190 : case OMP_CLAUSE_IF:
17399 : 6190 : case OMP_CLAUSE_UNTIED:
17400 : 6190 : case OMP_CLAUSE_FINAL:
17401 : 6190 : case OMP_CLAUSE_MERGEABLE:
17402 : 6190 : case OMP_CLAUSE_PRIORITY:
17403 : 6190 : case OMP_CLAUSE_REDUCTION:
17404 : 6190 : case OMP_CLAUSE_IN_REDUCTION:
17405 : 6190 : *gtask_clauses_ptr = c;
17406 : 6190 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17407 : 6190 : break;
17408 : 2061 : case OMP_CLAUSE_PRIVATE:
17409 : 2061 : if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
17410 : : {
17411 : : /* We want private on outer for and firstprivate
17412 : : on task. */
17413 : 26 : *gtask_clauses_ptr
17414 : 26 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17415 : : OMP_CLAUSE_FIRSTPRIVATE);
17416 : 26 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17417 : 26 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17418 : : openacc);
17419 : 26 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17420 : 26 : *gforo_clauses_ptr = c;
17421 : 26 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17422 : : }
17423 : : else
17424 : : {
17425 : 2035 : *gtask_clauses_ptr = c;
17426 : 2035 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17427 : : }
17428 : : break;
17429 : : /* These clauses go into outer taskloop clauses. */
17430 : 529 : case OMP_CLAUSE_GRAINSIZE:
17431 : 529 : case OMP_CLAUSE_NUM_TASKS:
17432 : 529 : case OMP_CLAUSE_NOGROUP:
17433 : 529 : *gforo_clauses_ptr = c;
17434 : 529 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17435 : 529 : break;
17436 : : /* Collapse clause we duplicate on both taskloops. */
17437 : 580 : case OMP_CLAUSE_COLLAPSE:
17438 : 580 : *gfor_clauses_ptr = c;
17439 : 580 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17440 : 580 : *gforo_clauses_ptr = copy_node (c);
17441 : 580 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17442 : 580 : break;
17443 : : /* For lastprivate, keep the clause on inner taskloop, and add
17444 : : a shared clause on task. If the same decl is also firstprivate,
17445 : : add also firstprivate clause on the inner taskloop. */
17446 : 1184 : case OMP_CLAUSE_LASTPRIVATE:
17447 : 1184 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17448 : : {
17449 : : /* For taskloop C++ lastprivate IVs, we want:
17450 : : 1) private on outer taskloop
17451 : : 2) firstprivate and shared on task
17452 : : 3) lastprivate on inner taskloop */
17453 : 38 : *gtask_clauses_ptr
17454 : 38 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17455 : : OMP_CLAUSE_FIRSTPRIVATE);
17456 : 38 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17457 : 38 : lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL,
17458 : : openacc);
17459 : 38 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17460 : 38 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
17461 : 38 : *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17462 : : OMP_CLAUSE_PRIVATE);
17463 : 38 : OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
17464 : 38 : OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
17465 : 38 : TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
17466 : 38 : gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
17467 : : }
17468 : 1184 : *gfor_clauses_ptr = c;
17469 : 1184 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17470 : 1184 : *gtask_clauses_ptr
17471 : 1184 : = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
17472 : 1184 : OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
17473 : 1184 : if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
17474 : 309 : OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
17475 : 1184 : gtask_clauses_ptr
17476 : 1184 : = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17477 : 1184 : break;
17478 : : /* Allocate clause we duplicate on task and inner taskloop
17479 : : if the decl is lastprivate, otherwise just put on task. */
17480 : 62 : case OMP_CLAUSE_ALLOCATE:
17481 : 62 : if (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
17482 : 62 : && DECL_P (OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)))
17483 : : {
17484 : : /* Additionally, put firstprivate clause on task
17485 : : for the allocator if it is not constant. */
17486 : 34 : *gtask_clauses_ptr
17487 : 34 : = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17488 : : OMP_CLAUSE_FIRSTPRIVATE);
17489 : 34 : OMP_CLAUSE_DECL (*gtask_clauses_ptr)
17490 : 34 : = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
17491 : 34 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17492 : : }
17493 : 62 : if (lastprivate_uids
17494 : 97 : && bitmap_bit_p (lastprivate_uids,
17495 : 35 : DECL_UID (OMP_CLAUSE_DECL (c))))
17496 : : {
17497 : 17 : *gfor_clauses_ptr = c;
17498 : 17 : gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17499 : 17 : *gtask_clauses_ptr = copy_node (c);
17500 : 17 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
17501 : : }
17502 : : else
17503 : : {
17504 : 45 : *gtask_clauses_ptr = c;
17505 : 45 : gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
17506 : : }
17507 : : break;
17508 : 0 : default:
17509 : 0 : gcc_unreachable ();
17510 : : }
17511 : 1579 : *gfor_clauses_ptr = NULL_TREE;
17512 : 1579 : *gtask_clauses_ptr = NULL_TREE;
17513 : 1579 : *gforo_clauses_ptr = NULL_TREE;
17514 : 1579 : BITMAP_FREE (lastprivate_uids);
17515 : 1579 : gimple_set_location (gfor, input_location);
17516 : 1579 : g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
17517 : 1579 : g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
17518 : : NULL_TREE, NULL_TREE, NULL_TREE);
17519 : 1579 : gimple_set_location (g, input_location);
17520 : 1579 : gimple_omp_task_set_taskloop_p (g, true);
17521 : 1579 : g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
17522 : 1579 : gomp_for *gforo
17523 : 1579 : = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
17524 : : gimple_omp_for_collapse (gfor),
17525 : : gimple_omp_for_pre_body (gfor));
17526 : 1579 : gimple_omp_for_set_pre_body (gfor, NULL);
17527 : 1579 : gimple_omp_for_set_combined_p (gforo, true);
17528 : 1579 : gimple_omp_for_set_combined_into_p (gfor, true);
17529 : 3444 : for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
17530 : : {
17531 : 1865 : tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
17532 : 1865 : tree v = create_tmp_var (type);
17533 : 1865 : gimple_omp_for_set_index (gforo, i, v);
17534 : 1865 : t = unshare_expr (gimple_omp_for_initial (gfor, i));
17535 : 1865 : gimple_omp_for_set_initial (gforo, i, t);
17536 : 1865 : gimple_omp_for_set_cond (gforo, i,
17537 : : gimple_omp_for_cond (gfor, i));
17538 : 1865 : t = unshare_expr (gimple_omp_for_final (gfor, i));
17539 : 1865 : gimple_omp_for_set_final (gforo, i, t);
17540 : 1865 : t = unshare_expr (gimple_omp_for_incr (gfor, i));
17541 : 1865 : gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
17542 : 1865 : TREE_OPERAND (t, 0) = v;
17543 : 1865 : gimple_omp_for_set_incr (gforo, i, t);
17544 : 1865 : t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17545 : 1865 : OMP_CLAUSE_DECL (t) = v;
17546 : 1865 : OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
17547 : 1865 : gimple_omp_for_set_clauses (gforo, t);
17548 : 1865 : if (OMP_FOR_NON_RECTANGULAR (for_stmt))
17549 : : {
17550 : 90 : tree *p1 = NULL, *p2 = NULL;
17551 : 90 : t = gimple_omp_for_initial (gforo, i);
17552 : 90 : if (TREE_CODE (t) == TREE_VEC)
17553 : 34 : p1 = &TREE_VEC_ELT (t, 0);
17554 : 90 : t = gimple_omp_for_final (gforo, i);
17555 : 90 : if (TREE_CODE (t) == TREE_VEC)
17556 : : {
17557 : 31 : if (p1)
17558 : 23 : p2 = &TREE_VEC_ELT (t, 0);
17559 : : else
17560 : 8 : p1 = &TREE_VEC_ELT (t, 0);
17561 : : }
17562 : 90 : if (p1)
17563 : : {
17564 : : int j;
17565 : 58 : for (j = 0; j < i; j++)
17566 : 58 : if (*p1 == gimple_omp_for_index (gfor, j))
17567 : : {
17568 : 42 : *p1 = gimple_omp_for_index (gforo, j);
17569 : 42 : if (p2)
17570 : 23 : *p2 = *p1;
17571 : : break;
17572 : : }
17573 : 42 : gcc_assert (j < i);
17574 : : }
17575 : : }
17576 : : }
17577 : 1579 : gimplify_seq_add_stmt (pre_p, gforo);
17578 : : }
17579 : : else
17580 : 49635 : gimplify_seq_add_stmt (pre_p, gfor);
17581 : :
17582 : 51214 : if (TREE_CODE (orig_for_stmt) == OMP_FOR)
17583 : : {
17584 : 18177 : struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
17585 : 18177 : unsigned lastprivate_conditional = 0;
17586 : 18177 : while (ctx
17587 : 18213 : && (ctx->region_type == ORT_TARGET_DATA
17588 : 14200 : || ctx->region_type == ORT_TASKGROUP))
17589 : 36 : ctx = ctx->outer_context;
17590 : 18177 : if (ctx && (ctx->region_type & ORT_PARALLEL) != 0)
17591 : 13940 : for (tree c = gimple_omp_for_clauses (gfor);
17592 : 63187 : c; c = OMP_CLAUSE_CHAIN (c))
17593 : 49247 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17594 : 49247 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
17595 : 158 : ++lastprivate_conditional;
17596 : 13940 : if (lastprivate_conditional)
17597 : : {
17598 : 118 : struct omp_for_data fd;
17599 : 118 : omp_extract_for_data (gfor, &fd, NULL);
17600 : 118 : tree type = build_array_type_nelts (unsigned_type_for (fd.iter_type),
17601 : 118 : lastprivate_conditional);
17602 : 118 : tree var = create_tmp_var_raw (type);
17603 : 118 : tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__CONDTEMP_);
17604 : 118 : OMP_CLAUSE_DECL (c) = var;
17605 : 118 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
17606 : 118 : gimple_omp_for_set_clauses (gfor, c);
17607 : 118 : omp_add_variable (ctx, var, GOVD_CONDTEMP | GOVD_SEEN);
17608 : : }
17609 : : }
17610 : 33037 : else if (TREE_CODE (orig_for_stmt) == OMP_SIMD)
17611 : : {
17612 : 10641 : unsigned lastprivate_conditional = 0;
17613 : 49070 : for (tree c = gimple_omp_for_clauses (gfor); c; c = OMP_CLAUSE_CHAIN (c))
17614 : 38429 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
17615 : 38429 : && OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
17616 : 119 : ++lastprivate_conditional;
17617 : 10641 : if (lastprivate_conditional)
17618 : : {
17619 : 91 : struct omp_for_data fd;
17620 : 91 : omp_extract_for_data (gfor, &fd, NULL);
17621 : 91 : tree type = unsigned_type_for (fd.iter_type);
17622 : 301 : while (lastprivate_conditional--)
17623 : : {
17624 : 119 : tree c = build_omp_clause (UNKNOWN_LOCATION,
17625 : : OMP_CLAUSE__CONDTEMP_);
17626 : 119 : OMP_CLAUSE_DECL (c) = create_tmp_var (type);
17627 : 119 : OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (gfor);
17628 : 119 : gimple_omp_for_set_clauses (gfor, c);
17629 : : }
17630 : : }
17631 : : }
17632 : :
17633 : 51214 : if (ret != GS_ALL_DONE)
17634 : : return GS_ERROR;
17635 : 51214 : *expr_p = NULL_TREE;
17636 : 51214 : return GS_ALL_DONE;
17637 : : }
17638 : :
17639 : : /* Helper for gimplify_omp_loop, called through walk_tree. */
17640 : :
17641 : : static tree
17642 : 294 : note_no_context_vars (tree *tp, int *, void *data)
17643 : : {
17644 : 294 : if (VAR_P (*tp)
17645 : 56 : && DECL_CONTEXT (*tp) == NULL_TREE
17646 : 309 : && !is_global_var (*tp))
17647 : : {
17648 : 15 : vec<tree> *d = (vec<tree> *) data;
17649 : 15 : d->safe_push (*tp);
17650 : 15 : DECL_CONTEXT (*tp) = current_function_decl;
17651 : : }
17652 : 294 : return NULL_TREE;
17653 : : }
17654 : :
17655 : : /* Gimplify the gross structure of an OMP_LOOP statement. */
17656 : :
17657 : : static enum gimplify_status
17658 : 1057 : gimplify_omp_loop (tree *expr_p, gimple_seq *pre_p)
17659 : : {
17660 : 1057 : tree for_stmt = *expr_p;
17661 : 1057 : tree clauses = OMP_FOR_CLAUSES (for_stmt);
17662 : 1057 : struct gimplify_omp_ctx *octx = gimplify_omp_ctxp;
17663 : 1057 : enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
17664 : 1057 : int i;
17665 : :
17666 : 1057 : omp_maybe_apply_loop_xforms (expr_p, NULL_TREE);
17667 : 1057 : if (*expr_p != for_stmt)
17668 : : return GS_OK;
17669 : :
17670 : : /* If order is not present, the behavior is as if order(concurrent)
17671 : : appeared. */
17672 : 1057 : tree order = omp_find_clause (clauses, OMP_CLAUSE_ORDER);
17673 : 1057 : if (order == NULL_TREE)
17674 : : {
17675 : 804 : order = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_ORDER);
17676 : 804 : OMP_CLAUSE_CHAIN (order) = clauses;
17677 : 804 : OMP_FOR_CLAUSES (for_stmt) = clauses = order;
17678 : : }
17679 : :
17680 : 1057 : tree bind = omp_find_clause (clauses, OMP_CLAUSE_BIND);
17681 : 1057 : if (bind == NULL_TREE)
17682 : : {
17683 : 579 : if (!flag_openmp) /* flag_openmp_simd */
17684 : : ;
17685 : 562 : else if (octx && (octx->region_type & ORT_TEAMS) != 0)
17686 : : kind = OMP_CLAUSE_BIND_TEAMS;
17687 : 337 : else if (octx && (octx->region_type & ORT_PARALLEL) != 0)
17688 : : kind = OMP_CLAUSE_BIND_PARALLEL;
17689 : : else
17690 : : {
17691 : 187 : for (; octx; octx = octx->outer_context)
17692 : : {
17693 : 79 : if ((octx->region_type & ORT_ACC) != 0
17694 : 79 : || octx->region_type == ORT_NONE
17695 : 79 : || octx->region_type == ORT_IMPLICIT_TARGET)
17696 : 0 : continue;
17697 : : break;
17698 : : }
17699 : 187 : if (octx == NULL && !in_omp_construct)
17700 : 4 : error_at (EXPR_LOCATION (for_stmt),
17701 : : "%<bind%> clause not specified on a %<loop%> "
17702 : : "construct not nested inside another OpenMP construct");
17703 : : }
17704 : 579 : bind = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_BIND);
17705 : 579 : OMP_CLAUSE_CHAIN (bind) = clauses;
17706 : 579 : OMP_CLAUSE_BIND_KIND (bind) = kind;
17707 : 579 : OMP_FOR_CLAUSES (for_stmt) = bind;
17708 : : }
17709 : : else
17710 : 478 : switch (OMP_CLAUSE_BIND_KIND (bind))
17711 : : {
17712 : : case OMP_CLAUSE_BIND_THREAD:
17713 : : break;
17714 : 166 : case OMP_CLAUSE_BIND_PARALLEL:
17715 : 166 : if (!flag_openmp) /* flag_openmp_simd */
17716 : : {
17717 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17718 : 0 : break;
17719 : : }
17720 : 240 : for (; octx; octx = octx->outer_context)
17721 : 78 : if (octx->region_type == ORT_SIMD
17722 : 78 : && omp_find_clause (octx->clauses, OMP_CLAUSE_BIND) == NULL_TREE)
17723 : : {
17724 : 4 : error_at (EXPR_LOCATION (for_stmt),
17725 : : "%<bind(parallel)%> on a %<loop%> construct nested "
17726 : : "inside %<simd%> construct");
17727 : 4 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17728 : 4 : break;
17729 : : }
17730 : : kind = OMP_CLAUSE_BIND_PARALLEL;
17731 : : break;
17732 : 158 : case OMP_CLAUSE_BIND_TEAMS:
17733 : 158 : if (!flag_openmp) /* flag_openmp_simd */
17734 : : {
17735 : 0 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17736 : 0 : break;
17737 : : }
17738 : 158 : if ((octx
17739 : 128 : && octx->region_type != ORT_IMPLICIT_TARGET
17740 : 111 : && octx->region_type != ORT_NONE
17741 : 111 : && (octx->region_type & ORT_TEAMS) == 0)
17742 : 106 : || in_omp_construct)
17743 : : {
17744 : 64 : error_at (EXPR_LOCATION (for_stmt),
17745 : : "%<bind(teams)%> on a %<loop%> region not strictly "
17746 : : "nested inside of a %<teams%> region");
17747 : 64 : OMP_CLAUSE_BIND_KIND (bind) = OMP_CLAUSE_BIND_THREAD;
17748 : 64 : break;
17749 : : }
17750 : : kind = OMP_CLAUSE_BIND_TEAMS;
17751 : : break;
17752 : 0 : default:
17753 : 0 : gcc_unreachable ();
17754 : : }
17755 : :
17756 : 4268 : for (tree *pc = &OMP_FOR_CLAUSES (for_stmt); *pc; )
17757 : 3211 : switch (OMP_CLAUSE_CODE (*pc))
17758 : : {
17759 : 343 : case OMP_CLAUSE_REDUCTION:
17760 : 343 : if (OMP_CLAUSE_REDUCTION_INSCAN (*pc))
17761 : : {
17762 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
17763 : : "%<inscan%> %<reduction%> clause on "
17764 : : "%qs construct", "loop");
17765 : 4 : OMP_CLAUSE_REDUCTION_INSCAN (*pc) = 0;
17766 : : }
17767 : 343 : if (OMP_CLAUSE_REDUCTION_TASK (*pc))
17768 : : {
17769 : 8 : error_at (OMP_CLAUSE_LOCATION (*pc),
17770 : : "invalid %<task%> reduction modifier on construct "
17771 : : "other than %<parallel%>, %qs or %<sections%>",
17772 : 4 : lang_GNU_Fortran () ? "do" : "for");
17773 : 4 : OMP_CLAUSE_REDUCTION_TASK (*pc) = 0;
17774 : : }
17775 : 343 : pc = &OMP_CLAUSE_CHAIN (*pc);
17776 : 343 : break;
17777 : : case OMP_CLAUSE_LASTPRIVATE:
17778 : 293 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17779 : : {
17780 : 289 : tree t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
17781 : 289 : gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
17782 : 289 : if (OMP_CLAUSE_DECL (*pc) == TREE_OPERAND (t, 0))
17783 : : break;
17784 : 26 : if (OMP_FOR_ORIG_DECLS (for_stmt)
17785 : 25 : && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
17786 : : i)) == TREE_LIST
17787 : 48 : && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt),
17788 : : i)))
17789 : : {
17790 : 22 : tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17791 : 22 : if (OMP_CLAUSE_DECL (*pc) == TREE_PURPOSE (orig))
17792 : : break;
17793 : : }
17794 : : }
17795 : 289 : if (i == TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)))
17796 : : {
17797 : 4 : error_at (OMP_CLAUSE_LOCATION (*pc),
17798 : : "%<lastprivate%> clause on a %<loop%> construct refers "
17799 : : "to a variable %qD which is not the loop iterator",
17800 : 4 : OMP_CLAUSE_DECL (*pc));
17801 : 4 : *pc = OMP_CLAUSE_CHAIN (*pc);
17802 : 4 : break;
17803 : : }
17804 : 285 : pc = &OMP_CLAUSE_CHAIN (*pc);
17805 : 285 : break;
17806 : 2579 : default:
17807 : 2579 : pc = &OMP_CLAUSE_CHAIN (*pc);
17808 : 2579 : break;
17809 : : }
17810 : :
17811 : 1057 : TREE_SET_CODE (for_stmt, OMP_SIMD);
17812 : :
17813 : 1057 : int last;
17814 : 1057 : switch (kind)
17815 : : {
17816 : : case OMP_CLAUSE_BIND_THREAD: last = 0; break;
17817 : 424 : case OMP_CLAUSE_BIND_PARALLEL: last = 1; break;
17818 : 211 : case OMP_CLAUSE_BIND_TEAMS: last = 2; break;
17819 : : }
17820 : 1903 : for (int pass = 1; pass <= last; pass++)
17821 : : {
17822 : 846 : if (pass == 2)
17823 : : {
17824 : 211 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL,
17825 : : make_node (BLOCK));
17826 : 211 : append_to_statement_list (*expr_p, &BIND_EXPR_BODY (bind));
17827 : 211 : *expr_p = make_node (OMP_PARALLEL);
17828 : 211 : TREE_TYPE (*expr_p) = void_type_node;
17829 : 211 : OMP_PARALLEL_BODY (*expr_p) = bind;
17830 : 211 : OMP_PARALLEL_COMBINED (*expr_p) = 1;
17831 : 211 : SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (for_stmt));
17832 : 211 : tree *pc = &OMP_PARALLEL_CLAUSES (*expr_p);
17833 : 425 : for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
17834 : 214 : if (OMP_FOR_ORIG_DECLS (for_stmt)
17835 : 214 : && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i))
17836 : : == TREE_LIST))
17837 : : {
17838 : 16 : tree elt = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
17839 : 16 : if (TREE_PURPOSE (elt) && TREE_VALUE (elt))
17840 : : {
17841 : 13 : *pc = build_omp_clause (UNKNOWN_LOCATION,
17842 : : OMP_CLAUSE_FIRSTPRIVATE);
17843 : 13 : OMP_CLAUSE_DECL (*pc) = TREE_VALUE (elt);
17844 : 13 : pc = &OMP_CLAUSE_CHAIN (*pc);
17845 : : }
17846 : : }
17847 : : }
17848 : 846 : tree t = make_node (pass == 2 ? OMP_DISTRIBUTE : OMP_FOR);
17849 : 846 : tree *pc = &OMP_FOR_CLAUSES (t);
17850 : 846 : TREE_TYPE (t) = void_type_node;
17851 : 846 : OMP_FOR_BODY (t) = *expr_p;
17852 : 846 : SET_EXPR_LOCATION (t, EXPR_LOCATION (for_stmt));
17853 : 3914 : for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
17854 : 3068 : switch (OMP_CLAUSE_CODE (c))
17855 : : {
17856 : 1958 : case OMP_CLAUSE_BIND:
17857 : 1958 : case OMP_CLAUSE_ORDER:
17858 : 1958 : case OMP_CLAUSE_COLLAPSE:
17859 : 1958 : *pc = copy_node (c);
17860 : 1958 : pc = &OMP_CLAUSE_CHAIN (*pc);
17861 : 1958 : break;
17862 : : case OMP_CLAUSE_PRIVATE:
17863 : : case OMP_CLAUSE_FIRSTPRIVATE:
17864 : : /* Only needed on innermost. */
17865 : : break;
17866 : 351 : case OMP_CLAUSE_LASTPRIVATE:
17867 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) && pass != last)
17868 : : {
17869 : 7 : *pc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17870 : : OMP_CLAUSE_FIRSTPRIVATE);
17871 : 7 : OMP_CLAUSE_DECL (*pc) = OMP_CLAUSE_DECL (c);
17872 : 7 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
17873 : 7 : pc = &OMP_CLAUSE_CHAIN (*pc);
17874 : : }
17875 : 351 : *pc = copy_node (c);
17876 : 351 : OMP_CLAUSE_LASTPRIVATE_STMT (*pc) = NULL_TREE;
17877 : 351 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
17878 : 351 : if (OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c))
17879 : : {
17880 : 22 : if (pass != last)
17881 : 7 : OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (*pc) = 1;
17882 : : else
17883 : 15 : lang_hooks.decls.omp_finish_clause (*pc, NULL, false);
17884 : 22 : OMP_CLAUSE_LASTPRIVATE_LOOP_IV (*pc) = 0;
17885 : : }
17886 : 351 : pc = &OMP_CLAUSE_CHAIN (*pc);
17887 : 351 : break;
17888 : 463 : case OMP_CLAUSE_REDUCTION:
17889 : 463 : *pc = copy_node (c);
17890 : 463 : OMP_CLAUSE_DECL (*pc) = unshare_expr (OMP_CLAUSE_DECL (c));
17891 : 463 : TREE_TYPE (*pc) = unshare_expr (TREE_TYPE (c));
17892 : 463 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc))
17893 : : {
17894 : 15 : auto_vec<tree> no_context_vars;
17895 : 15 : int walk_subtrees = 0;
17896 : 15 : note_no_context_vars (&OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
17897 : : &walk_subtrees, &no_context_vars);
17898 : 15 : if (tree p = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c))
17899 : 0 : note_no_context_vars (&p, &walk_subtrees, &no_context_vars);
17900 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_INIT (c),
17901 : : note_no_context_vars,
17902 : : &no_context_vars);
17903 : 15 : walk_tree_without_duplicates (&OMP_CLAUSE_REDUCTION_MERGE (c),
17904 : : note_no_context_vars,
17905 : : &no_context_vars);
17906 : :
17907 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc)
17908 : 15 : = copy_node (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c));
17909 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
17910 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc)
17911 : 0 : = copy_node (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c));
17912 : :
17913 : 15 : hash_map<tree, tree> decl_map;
17914 : 15 : decl_map.put (OMP_CLAUSE_DECL (c), OMP_CLAUSE_DECL (c));
17915 : 15 : decl_map.put (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
17916 : 15 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (*pc));
17917 : 15 : if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc))
17918 : 0 : decl_map.put (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
17919 : 0 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (*pc));
17920 : :
17921 : 15 : copy_body_data id;
17922 : 15 : memset (&id, 0, sizeof (id));
17923 : 15 : id.src_fn = current_function_decl;
17924 : 15 : id.dst_fn = current_function_decl;
17925 : 15 : id.src_cfun = cfun;
17926 : 15 : id.decl_map = &decl_map;
17927 : 15 : id.copy_decl = copy_decl_no_change;
17928 : 15 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
17929 : 15 : id.transform_new_cfg = true;
17930 : 15 : id.transform_return_to_modify = false;
17931 : 15 : id.eh_lp_nr = 0;
17932 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_INIT (*pc), copy_tree_body_r,
17933 : : &id, NULL);
17934 : 15 : walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (*pc), copy_tree_body_r,
17935 : : &id, NULL);
17936 : :
17937 : 60 : for (tree d : no_context_vars)
17938 : : {
17939 : 15 : DECL_CONTEXT (d) = NULL_TREE;
17940 : 15 : DECL_CONTEXT (*decl_map.get (d)) = NULL_TREE;
17941 : : }
17942 : 15 : }
17943 : : else
17944 : : {
17945 : 448 : OMP_CLAUSE_REDUCTION_INIT (*pc)
17946 : 448 : = unshare_expr (OMP_CLAUSE_REDUCTION_INIT (c));
17947 : 448 : OMP_CLAUSE_REDUCTION_MERGE (*pc)
17948 : 896 : = unshare_expr (OMP_CLAUSE_REDUCTION_MERGE (c));
17949 : : }
17950 : 463 : pc = &OMP_CLAUSE_CHAIN (*pc);
17951 : 463 : break;
17952 : 0 : default:
17953 : 0 : gcc_unreachable ();
17954 : : }
17955 : 846 : *pc = NULL_TREE;
17956 : 846 : *expr_p = t;
17957 : : }
17958 : 1057 : return gimplify_expr (expr_p, pre_p, NULL, is_gimple_stmt, fb_none);
17959 : : }
17960 : :
17961 : :
17962 : : /* Helper function of optimize_target_teams, find OMP_TEAMS inside
17963 : : of OMP_TARGET's body. */
17964 : :
17965 : : static tree
17966 : 79643 : find_omp_teams (tree *tp, int *walk_subtrees, void *)
17967 : : {
17968 : 79643 : *walk_subtrees = 0;
17969 : 79643 : switch (TREE_CODE (*tp))
17970 : : {
17971 : : case OMP_TEAMS:
17972 : : return *tp;
17973 : 23816 : case BIND_EXPR:
17974 : 23816 : case STATEMENT_LIST:
17975 : 23816 : *walk_subtrees = 1;
17976 : 23816 : break;
17977 : : default:
17978 : : break;
17979 : : }
17980 : : return NULL_TREE;
17981 : : }
17982 : :
17983 : : /* Helper function of optimize_target_teams, determine if the expression
17984 : : can be computed safely before the target construct on the host. */
17985 : :
17986 : : static tree
17987 : 1311 : computable_teams_clause (tree *tp, int *walk_subtrees, void *)
17988 : : {
17989 : 1831 : splay_tree_node n;
17990 : :
17991 : 1831 : if (TYPE_P (*tp))
17992 : : {
17993 : 0 : *walk_subtrees = 0;
17994 : 0 : return NULL_TREE;
17995 : : }
17996 : 1831 : switch (TREE_CODE (*tp))
17997 : : {
17998 : 990 : case VAR_DECL:
17999 : 990 : case PARM_DECL:
18000 : 990 : case RESULT_DECL:
18001 : 990 : *walk_subtrees = 0;
18002 : 990 : if (error_operand_p (*tp)
18003 : 990 : || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
18004 : 990 : || DECL_HAS_VALUE_EXPR_P (*tp)
18005 : 990 : || DECL_THREAD_LOCAL_P (*tp)
18006 : 990 : || TREE_SIDE_EFFECTS (*tp)
18007 : 1980 : || TREE_THIS_VOLATILE (*tp))
18008 : 0 : return *tp;
18009 : 990 : if (is_global_var (*tp)
18010 : 990 : && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
18011 : 0 : || lookup_attribute ("omp declare target link",
18012 : 0 : DECL_ATTRIBUTES (*tp))))
18013 : 16 : return *tp;
18014 : 974 : if (VAR_P (*tp)
18015 : 668 : && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
18016 : 50 : && !is_global_var (*tp)
18017 : 1024 : && decl_function_context (*tp) == current_function_decl)
18018 : 50 : return *tp;
18019 : 1848 : n = splay_tree_lookup (gimplify_omp_ctxp->variables,
18020 : 924 : (splay_tree_key) *tp);
18021 : 924 : if (n == NULL)
18022 : : {
18023 : 292 : if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
18024 : : return NULL_TREE;
18025 : 24 : return *tp;
18026 : : }
18027 : 632 : else if (n->value & GOVD_LOCAL)
18028 : 0 : return *tp;
18029 : 632 : else if (n->value & GOVD_FIRSTPRIVATE)
18030 : : return NULL_TREE;
18031 : 112 : else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18032 : : == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
18033 : : return NULL_TREE;
18034 : 96 : return *tp;
18035 : 76 : case INTEGER_CST:
18036 : 76 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18037 : : return *tp;
18038 : : return NULL_TREE;
18039 : 520 : case TARGET_EXPR:
18040 : 520 : if (TARGET_EXPR_INITIAL (*tp)
18041 : 520 : || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
18042 : : return *tp;
18043 : 520 : return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
18044 : 520 : walk_subtrees, NULL);
18045 : : /* Allow some reasonable subset of integral arithmetics. */
18046 : 193 : case PLUS_EXPR:
18047 : 193 : case MINUS_EXPR:
18048 : 193 : case MULT_EXPR:
18049 : 193 : case TRUNC_DIV_EXPR:
18050 : 193 : case CEIL_DIV_EXPR:
18051 : 193 : case FLOOR_DIV_EXPR:
18052 : 193 : case ROUND_DIV_EXPR:
18053 : 193 : case TRUNC_MOD_EXPR:
18054 : 193 : case CEIL_MOD_EXPR:
18055 : 193 : case FLOOR_MOD_EXPR:
18056 : 193 : case ROUND_MOD_EXPR:
18057 : 193 : case RDIV_EXPR:
18058 : 193 : case EXACT_DIV_EXPR:
18059 : 193 : case MIN_EXPR:
18060 : 193 : case MAX_EXPR:
18061 : 193 : case LSHIFT_EXPR:
18062 : 193 : case RSHIFT_EXPR:
18063 : 193 : case BIT_IOR_EXPR:
18064 : 193 : case BIT_XOR_EXPR:
18065 : 193 : case BIT_AND_EXPR:
18066 : 193 : case NEGATE_EXPR:
18067 : 193 : case ABS_EXPR:
18068 : 193 : case BIT_NOT_EXPR:
18069 : 193 : case NON_LVALUE_EXPR:
18070 : 193 : CASE_CONVERT:
18071 : 193 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
18072 : : return *tp;
18073 : : return NULL_TREE;
18074 : : /* And disallow anything else, except for comparisons. */
18075 : 52 : default:
18076 : 52 : if (COMPARISON_CLASS_P (*tp))
18077 : : return NULL_TREE;
18078 : : return *tp;
18079 : : }
18080 : : }
18081 : :
18082 : : /* Try to determine if the num_teams and/or thread_limit expressions
18083 : : can have their values determined already before entering the
18084 : : target construct.
18085 : : INTEGER_CSTs trivially are,
18086 : : integral decls that are firstprivate (explicitly or implicitly)
18087 : : or explicitly map(always, to:) or map(always, tofrom:) on the target
18088 : : region too, and expressions involving simple arithmetics on those
18089 : : too, function calls are not ok, dereferencing something neither etc.
18090 : : Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
18091 : : EXPR based on what we find:
18092 : : 0 stands for clause not specified at all, use implementation default
18093 : : -1 stands for value that can't be determined easily before entering
18094 : : the target construct.
18095 : : -2 means that no explicit teams construct was specified
18096 : : If teams construct is not present at all, use 1 for num_teams
18097 : : and 0 for thread_limit (only one team is involved, and the thread
18098 : : limit is implementation defined. */
18099 : :
18100 : : static void
18101 : 12541 : optimize_target_teams (tree target, gimple_seq *pre_p)
18102 : : {
18103 : 12541 : tree body = OMP_BODY (target);
18104 : 12541 : tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
18105 : 12541 : tree num_teams_lower = NULL_TREE;
18106 : 12541 : tree num_teams_upper = integer_zero_node;
18107 : 12541 : tree thread_limit = integer_zero_node;
18108 : 12541 : location_t num_teams_loc = EXPR_LOCATION (target);
18109 : 12541 : location_t thread_limit_loc = EXPR_LOCATION (target);
18110 : 12541 : tree c, *p, expr;
18111 : 12541 : struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
18112 : :
18113 : 12541 : if (teams == NULL_TREE)
18114 : 6439 : num_teams_upper = build_int_cst (integer_type_node, -2);
18115 : : else
18116 : 10573 : for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
18117 : : {
18118 : 4471 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
18119 : : {
18120 : 617 : p = &num_teams_upper;
18121 : 617 : num_teams_loc = OMP_CLAUSE_LOCATION (c);
18122 : 617 : if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c))
18123 : : {
18124 : 148 : expr = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
18125 : 148 : if (TREE_CODE (expr) == INTEGER_CST)
18126 : 21 : num_teams_lower = expr;
18127 : 127 : else if (walk_tree (&expr, computable_teams_clause,
18128 : : NULL, NULL))
18129 : 19 : num_teams_lower = integer_minus_one_node;
18130 : : else
18131 : : {
18132 : 108 : num_teams_lower = expr;
18133 : 108 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18134 : 108 : if (gimplify_expr (&num_teams_lower, pre_p, NULL,
18135 : : is_gimple_val, fb_rvalue, false)
18136 : : == GS_ERROR)
18137 : : {
18138 : 0 : gimplify_omp_ctxp = target_ctx;
18139 : 0 : num_teams_lower = integer_minus_one_node;
18140 : : }
18141 : : else
18142 : : {
18143 : 108 : gimplify_omp_ctxp = target_ctx;
18144 : 108 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18145 : 28 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
18146 : 56 : = num_teams_lower;
18147 : : }
18148 : : }
18149 : : }
18150 : : }
18151 : 3854 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18152 : : {
18153 : 471 : p = &thread_limit;
18154 : 471 : thread_limit_loc = OMP_CLAUSE_LOCATION (c);
18155 : : }
18156 : : else
18157 : 3383 : continue;
18158 : 1088 : expr = OMP_CLAUSE_OPERAND (c, 0);
18159 : 1088 : if (TREE_CODE (expr) == INTEGER_CST)
18160 : : {
18161 : 173 : *p = expr;
18162 : 173 : continue;
18163 : : }
18164 : 915 : if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
18165 : : {
18166 : 219 : *p = integer_minus_one_node;
18167 : 219 : continue;
18168 : : }
18169 : 696 : *p = expr;
18170 : 696 : gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
18171 : 696 : if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
18172 : : == GS_ERROR)
18173 : : {
18174 : 0 : gimplify_omp_ctxp = target_ctx;
18175 : 0 : *p = integer_minus_one_node;
18176 : 0 : continue;
18177 : : }
18178 : 696 : gimplify_omp_ctxp = target_ctx;
18179 : 696 : if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
18180 : 48 : OMP_CLAUSE_OPERAND (c, 0) = *p;
18181 : : }
18182 : 12541 : if (!omp_find_clause (OMP_TARGET_CLAUSES (target), OMP_CLAUSE_THREAD_LIMIT))
18183 : : {
18184 : 12273 : c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
18185 : 12273 : OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
18186 : 12273 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18187 : 12273 : OMP_TARGET_CLAUSES (target) = c;
18188 : : }
18189 : 12541 : c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
18190 : 12541 : OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = num_teams_upper;
18191 : 12541 : OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = num_teams_lower;
18192 : 12541 : OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
18193 : 12541 : OMP_TARGET_CLAUSES (target) = c;
18194 : 12541 : }
18195 : :
18196 : : /* Gimplify the gross structure of several OMP constructs. */
18197 : :
18198 : : static void
18199 : 38886 : gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
18200 : : {
18201 : 38886 : tree expr = *expr_p;
18202 : 38886 : gimple *stmt;
18203 : 38886 : gimple_seq body = NULL;
18204 : 38886 : enum omp_region_type ort;
18205 : :
18206 : 38886 : switch (TREE_CODE (expr))
18207 : : {
18208 : : case OMP_SECTIONS:
18209 : : case OMP_SINGLE:
18210 : : ort = ORT_WORKSHARE;
18211 : : break;
18212 : 214 : case OMP_SCOPE:
18213 : 214 : ort = ORT_TASKGROUP;
18214 : 214 : break;
18215 : 12541 : case OMP_TARGET:
18216 : 12541 : ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
18217 : : break;
18218 : 2564 : case OACC_KERNELS:
18219 : 2564 : ort = ORT_ACC_KERNELS;
18220 : 2564 : break;
18221 : 7898 : case OACC_PARALLEL:
18222 : 7898 : ort = ORT_ACC_PARALLEL;
18223 : 7898 : break;
18224 : 1026 : case OACC_SERIAL:
18225 : 1026 : ort = ORT_ACC_SERIAL;
18226 : 1026 : break;
18227 : 1950 : case OACC_DATA:
18228 : 1950 : ort = ORT_ACC_DATA;
18229 : 1950 : break;
18230 : 1863 : case OMP_TARGET_DATA:
18231 : 1863 : ort = ORT_TARGET_DATA;
18232 : 1863 : break;
18233 : 8813 : case OMP_TEAMS:
18234 : 8813 : ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
18235 : 8813 : if (gimplify_omp_ctxp == NULL
18236 : 6142 : || gimplify_omp_ctxp->region_type == ORT_IMPLICIT_TARGET)
18237 : 2671 : ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
18238 : : break;
18239 : 117 : case OACC_HOST_DATA:
18240 : 117 : ort = ORT_ACC_HOST_DATA;
18241 : 117 : break;
18242 : 0 : default:
18243 : 0 : gcc_unreachable ();
18244 : : }
18245 : :
18246 : 38886 : bool save_in_omp_construct = in_omp_construct;
18247 : 38886 : if ((ort & ORT_ACC) == 0)
18248 : 25331 : in_omp_construct = false;
18249 : 38886 : gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
18250 : : TREE_CODE (expr));
18251 : 38886 : if (TREE_CODE (expr) == OMP_TARGET)
18252 : 12541 : optimize_target_teams (expr, pre_p);
18253 : 38886 : if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
18254 : 10927 : || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18255 : : {
18256 : 30630 : push_gimplify_context ();
18257 : 30630 : gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
18258 : 30630 : if (gimple_code (g) == GIMPLE_BIND)
18259 : 30630 : pop_gimplify_context (g);
18260 : : else
18261 : 0 : pop_gimplify_context (NULL);
18262 : 30630 : if ((ort & ORT_TARGET_DATA) != 0)
18263 : : {
18264 : 3930 : enum built_in_function end_ix;
18265 : 3930 : switch (TREE_CODE (expr))
18266 : : {
18267 : : case OACC_DATA:
18268 : : case OACC_HOST_DATA:
18269 : : end_ix = BUILT_IN_GOACC_DATA_END;
18270 : : break;
18271 : 1863 : case OMP_TARGET_DATA:
18272 : 1863 : end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
18273 : 1863 : break;
18274 : 0 : default:
18275 : 0 : gcc_unreachable ();
18276 : : }
18277 : 3930 : tree fn = builtin_decl_explicit (end_ix);
18278 : 3930 : g = gimple_build_call (fn, 0);
18279 : 3930 : gimple_seq cleanup = NULL;
18280 : 3930 : gimple_seq_add_stmt (&cleanup, g);
18281 : 3930 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
18282 : 3930 : body = NULL;
18283 : 3930 : gimple_seq_add_stmt (&body, g);
18284 : : }
18285 : : }
18286 : : else
18287 : 8256 : gimplify_and_add (OMP_BODY (expr), &body);
18288 : 38886 : gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
18289 : 38886 : TREE_CODE (expr));
18290 : 38886 : in_omp_construct = save_in_omp_construct;
18291 : :
18292 : 38886 : switch (TREE_CODE (expr))
18293 : : {
18294 : 1950 : case OACC_DATA:
18295 : 3900 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
18296 : 1950 : OMP_CLAUSES (expr));
18297 : 1950 : break;
18298 : 117 : case OACC_HOST_DATA:
18299 : 117 : if (omp_find_clause (OMP_CLAUSES (expr), OMP_CLAUSE_IF_PRESENT))
18300 : : {
18301 : 128 : for (tree c = OMP_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18302 : 95 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR)
18303 : 57 : OMP_CLAUSE_USE_DEVICE_PTR_IF_PRESENT (c) = 1;
18304 : : }
18305 : :
18306 : 234 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
18307 : 117 : OMP_CLAUSES (expr));
18308 : 117 : break;
18309 : 2564 : case OACC_KERNELS:
18310 : 5128 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
18311 : 2564 : OMP_CLAUSES (expr));
18312 : 2564 : break;
18313 : 7898 : case OACC_PARALLEL:
18314 : 15796 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
18315 : 7898 : OMP_CLAUSES (expr));
18316 : 7898 : break;
18317 : 1026 : case OACC_SERIAL:
18318 : 2052 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_SERIAL,
18319 : 1026 : OMP_CLAUSES (expr));
18320 : 1026 : break;
18321 : 626 : case OMP_SECTIONS:
18322 : 626 : stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
18323 : 626 : break;
18324 : 1274 : case OMP_SINGLE:
18325 : 1274 : stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
18326 : 1274 : break;
18327 : 214 : case OMP_SCOPE:
18328 : 214 : stmt = gimple_build_omp_scope (body, OMP_CLAUSES (expr));
18329 : 214 : break;
18330 : 12541 : case OMP_TARGET:
18331 : 25082 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
18332 : 12541 : OMP_CLAUSES (expr));
18333 : 12541 : break;
18334 : 1863 : case OMP_TARGET_DATA:
18335 : : /* Put use_device_{ptr,addr} clauses last, as map clauses are supposed
18336 : : to be evaluated before the use_device_{ptr,addr} clauses if they
18337 : : refer to the same variables. */
18338 : 1863 : {
18339 : 1863 : tree use_device_clauses;
18340 : 1863 : tree *pc, *uc = &use_device_clauses;
18341 : 9916 : for (pc = &OMP_CLAUSES (expr); *pc; )
18342 : 8053 : if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
18343 : 8053 : || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
18344 : : {
18345 : 1959 : *uc = *pc;
18346 : 1959 : *pc = OMP_CLAUSE_CHAIN (*pc);
18347 : 1959 : uc = &OMP_CLAUSE_CHAIN (*uc);
18348 : : }
18349 : : else
18350 : 6094 : pc = &OMP_CLAUSE_CHAIN (*pc);
18351 : 1863 : *uc = NULL_TREE;
18352 : 1863 : *pc = use_device_clauses;
18353 : 1863 : stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
18354 : 1863 : OMP_CLAUSES (expr));
18355 : : }
18356 : 1863 : break;
18357 : 8813 : case OMP_TEAMS:
18358 : 8813 : stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
18359 : 8813 : if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
18360 : 2671 : gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
18361 : : break;
18362 : 0 : default:
18363 : 0 : gcc_unreachable ();
18364 : : }
18365 : :
18366 : 38886 : gimplify_seq_add_stmt (pre_p, stmt);
18367 : 38886 : *expr_p = NULL_TREE;
18368 : 38886 : }
18369 : :
18370 : : /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
18371 : : target update constructs. */
18372 : :
18373 : : static void
18374 : 11869 : gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
18375 : : {
18376 : 11869 : tree expr = *expr_p;
18377 : 11869 : int kind;
18378 : 11869 : gomp_target *stmt;
18379 : 11869 : enum omp_region_type ort = ORT_WORKSHARE;
18380 : :
18381 : 11869 : switch (TREE_CODE (expr))
18382 : : {
18383 : : case OACC_ENTER_DATA:
18384 : : kind = GF_OMP_TARGET_KIND_OACC_ENTER_DATA;
18385 : : ort = ORT_ACC;
18386 : : break;
18387 : : case OACC_EXIT_DATA:
18388 : : kind = GF_OMP_TARGET_KIND_OACC_EXIT_DATA;
18389 : : ort = ORT_ACC;
18390 : : break;
18391 : : case OACC_UPDATE:
18392 : : kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
18393 : : ort = ORT_ACC;
18394 : : break;
18395 : : case OMP_TARGET_UPDATE:
18396 : : kind = GF_OMP_TARGET_KIND_UPDATE;
18397 : : break;
18398 : : case OMP_TARGET_ENTER_DATA:
18399 : : kind = GF_OMP_TARGET_KIND_ENTER_DATA;
18400 : : break;
18401 : : case OMP_TARGET_EXIT_DATA:
18402 : : kind = GF_OMP_TARGET_KIND_EXIT_DATA;
18403 : : break;
18404 : 0 : default:
18405 : 0 : gcc_unreachable ();
18406 : : }
18407 : 11869 : gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
18408 : : ort, TREE_CODE (expr));
18409 : 11869 : gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
18410 : 11869 : TREE_CODE (expr));
18411 : 11869 : if (TREE_CODE (expr) == OACC_UPDATE
18412 : 11869 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18413 : : OMP_CLAUSE_IF_PRESENT))
18414 : : {
18415 : : /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
18416 : : clause. */
18417 : 123 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18418 : 97 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18419 : 47 : switch (OMP_CLAUSE_MAP_KIND (c))
18420 : : {
18421 : 14 : case GOMP_MAP_FORCE_TO:
18422 : 14 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
18423 : 14 : break;
18424 : 24 : case GOMP_MAP_FORCE_FROM:
18425 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
18426 : 24 : break;
18427 : : default:
18428 : : break;
18429 : : }
18430 : : }
18431 : 11843 : else if (TREE_CODE (expr) == OACC_EXIT_DATA
18432 : 11843 : && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
18433 : : OMP_CLAUSE_FINALIZE))
18434 : : {
18435 : : /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote "finalize"
18436 : : semantics. */
18437 : 69 : bool have_clause = false;
18438 : 253 : for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18439 : 184 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18440 : 111 : switch (OMP_CLAUSE_MAP_KIND (c))
18441 : : {
18442 : 41 : case GOMP_MAP_FROM:
18443 : 41 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
18444 : 41 : have_clause = true;
18445 : 41 : break;
18446 : 43 : case GOMP_MAP_RELEASE:
18447 : 43 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
18448 : 43 : have_clause = true;
18449 : 43 : break;
18450 : : case GOMP_MAP_TO_PSET:
18451 : : /* Fortran arrays with descriptors must map that descriptor when
18452 : : doing standalone "attach" operations (in OpenACC). In that
18453 : : case GOMP_MAP_TO_PSET appears by itself with no preceding
18454 : : clause (see trans-openmp.cc:gfc_trans_omp_clauses). */
18455 : : break;
18456 : 2 : case GOMP_MAP_POINTER:
18457 : : /* TODO PR92929: we may see these here, but they'll always follow
18458 : : one of the clauses above, and will be handled by libgomp as
18459 : : one group, so no handling required here. */
18460 : 2 : gcc_assert (have_clause);
18461 : : break;
18462 : 24 : case GOMP_MAP_DETACH:
18463 : 24 : OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_DETACH);
18464 : 24 : have_clause = false;
18465 : 24 : break;
18466 : : case GOMP_MAP_STRUCT:
18467 : : case GOMP_MAP_STRUCT_UNORD:
18468 : 24 : have_clause = false;
18469 : : break;
18470 : 0 : default:
18471 : 0 : gcc_unreachable ();
18472 : : }
18473 : : }
18474 : 11869 : stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
18475 : :
18476 : 11869 : gimplify_seq_add_stmt (pre_p, stmt);
18477 : 11869 : *expr_p = NULL_TREE;
18478 : 11869 : }
18479 : :
18480 : : /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
18481 : : stabilized the lhs of the atomic operation as *ADDR. Return true if
18482 : : EXPR is this stabilized form. */
18483 : :
18484 : : static bool
18485 : 36822 : goa_lhs_expr_p (tree expr, tree addr)
18486 : : {
18487 : : /* Also include casts to other type variants. The C front end is fond
18488 : : of adding these for e.g. volatile variables. This is like
18489 : : STRIP_TYPE_NOPS but includes the main variant lookup. */
18490 : 36822 : STRIP_USELESS_TYPE_CONVERSION (expr);
18491 : :
18492 : 36822 : if (INDIRECT_REF_P (expr))
18493 : : {
18494 : 5020 : expr = TREE_OPERAND (expr, 0);
18495 : 5020 : while (expr != addr
18496 : 92 : && (CONVERT_EXPR_P (expr)
18497 : 92 : || TREE_CODE (expr) == NON_LVALUE_EXPR)
18498 : 0 : && TREE_CODE (expr) == TREE_CODE (addr)
18499 : 5020 : && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
18500 : : {
18501 : 0 : expr = TREE_OPERAND (expr, 0);
18502 : 0 : addr = TREE_OPERAND (addr, 0);
18503 : : }
18504 : 5020 : if (expr == addr)
18505 : : return true;
18506 : 92 : return (TREE_CODE (addr) == ADDR_EXPR
18507 : 62 : && TREE_CODE (expr) == ADDR_EXPR
18508 : 92 : && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
18509 : : }
18510 : 31802 : if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
18511 : : return true;
18512 : : return false;
18513 : : }
18514 : :
18515 : : /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
18516 : : expression does not involve the lhs, evaluate it into a temporary.
18517 : : Return 1 if the lhs appeared as a subexpression, 0 if it did not,
18518 : : or -1 if an error was encountered. */
18519 : :
18520 : : static int
18521 : 36822 : goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
18522 : : tree lhs_var, tree &target_expr, bool rhs, int depth)
18523 : : {
18524 : 36822 : tree expr = *expr_p;
18525 : 36822 : int saw_lhs = 0;
18526 : :
18527 : 36822 : if (goa_lhs_expr_p (expr, lhs_addr))
18528 : : {
18529 : 9191 : if (pre_p)
18530 : 8533 : *expr_p = lhs_var;
18531 : 9191 : return 1;
18532 : : }
18533 : 27631 : if (is_gimple_val (expr))
18534 : : return 0;
18535 : :
18536 : : /* Maximum depth of lhs in expression is for the
18537 : : __builtin_clear_padding (...), __builtin_clear_padding (...),
18538 : : __builtin_memcmp (&TARGET_EXPR <lhs, >, ...) == 0 ? ... : lhs; */
18539 : 17299 : if (++depth > 7)
18540 : 16 : goto finish;
18541 : :
18542 : 17283 : switch (TREE_CODE_CLASS (TREE_CODE (expr)))
18543 : : {
18544 : 9660 : case tcc_binary:
18545 : 9660 : case tcc_comparison:
18546 : 9660 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
18547 : : lhs_var, target_expr, true, depth);
18548 : : /* FALLTHRU */
18549 : 11709 : case tcc_unary:
18550 : 11709 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
18551 : : lhs_var, target_expr, true, depth);
18552 : 11709 : break;
18553 : 2982 : case tcc_expression:
18554 : 2982 : switch (TREE_CODE (expr))
18555 : : {
18556 : 836 : case TRUTH_ANDIF_EXPR:
18557 : 836 : case TRUTH_ORIF_EXPR:
18558 : 836 : case TRUTH_AND_EXPR:
18559 : 836 : case TRUTH_OR_EXPR:
18560 : 836 : case TRUTH_XOR_EXPR:
18561 : 836 : case BIT_INSERT_EXPR:
18562 : 836 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18563 : : lhs_addr, lhs_var, target_expr, true,
18564 : : depth);
18565 : : /* FALLTHRU */
18566 : 888 : case TRUTH_NOT_EXPR:
18567 : 888 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18568 : : lhs_addr, lhs_var, target_expr, true,
18569 : : depth);
18570 : 888 : break;
18571 : 550 : case MODIFY_EXPR:
18572 : 550 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
18573 : : target_expr, true, depth))
18574 : : break;
18575 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18576 : : lhs_addr, lhs_var, target_expr, true,
18577 : : depth);
18578 : 544 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18579 : : lhs_addr, lhs_var, target_expr, false,
18580 : : depth);
18581 : 544 : break;
18582 : : /* FALLTHRU */
18583 : 164 : case ADDR_EXPR:
18584 : 164 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr, lhs_var,
18585 : : target_expr, true, depth))
18586 : : break;
18587 : 138 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18588 : : lhs_addr, lhs_var, target_expr, false,
18589 : : depth);
18590 : 138 : break;
18591 : : case COMPOUND_EXPR:
18592 : : /* Break out any preevaluations from cp_build_modify_expr. */
18593 : 76 : for (; TREE_CODE (expr) == COMPOUND_EXPR;
18594 : 38 : expr = TREE_OPERAND (expr, 1))
18595 : : {
18596 : : /* Special-case __builtin_clear_padding call before
18597 : : __builtin_memcmp. */
18598 : 38 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
18599 : : {
18600 : 2 : tree fndecl = get_callee_fndecl (TREE_OPERAND (expr, 0));
18601 : 2 : if (fndecl
18602 : 2 : && fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)
18603 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
18604 : 2 : && (!pre_p
18605 : 0 : || goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL,
18606 : : lhs_addr, lhs_var,
18607 : : target_expr, true, depth)))
18608 : : {
18609 : 0 : if (pre_p)
18610 : 0 : *expr_p = expr;
18611 : 0 : saw_lhs = goa_stabilize_expr (&TREE_OPERAND (expr, 0),
18612 : : pre_p, lhs_addr, lhs_var,
18613 : : target_expr, true, depth);
18614 : 0 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1),
18615 : : pre_p, lhs_addr, lhs_var,
18616 : : target_expr, rhs, depth);
18617 : 0 : return saw_lhs;
18618 : : }
18619 : : }
18620 : :
18621 : 38 : if (pre_p)
18622 : 35 : gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
18623 : : }
18624 : 38 : if (!pre_p)
18625 : 3 : return goa_stabilize_expr (&expr, pre_p, lhs_addr, lhs_var,
18626 : 3 : target_expr, rhs, depth);
18627 : 35 : *expr_p = expr;
18628 : 35 : return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var,
18629 : 35 : target_expr, rhs, depth);
18630 : 441 : case COND_EXPR:
18631 : 441 : if (!goa_stabilize_expr (&TREE_OPERAND (expr, 0), NULL, lhs_addr,
18632 : : lhs_var, target_expr, true, depth))
18633 : : break;
18634 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18635 : : lhs_addr, lhs_var, target_expr, true,
18636 : : depth);
18637 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
18638 : : lhs_addr, lhs_var, target_expr, true,
18639 : : depth);
18640 : 428 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 2), pre_p,
18641 : : lhs_addr, lhs_var, target_expr, true,
18642 : : depth);
18643 : 428 : break;
18644 : 880 : case TARGET_EXPR:
18645 : 880 : if (TARGET_EXPR_INITIAL (expr))
18646 : : {
18647 : 880 : if (pre_p && !goa_stabilize_expr (expr_p, NULL, lhs_addr,
18648 : : lhs_var, target_expr, true,
18649 : : depth))
18650 : : break;
18651 : 702 : if (expr == target_expr)
18652 : : saw_lhs = 1;
18653 : : else
18654 : : {
18655 : 702 : saw_lhs = goa_stabilize_expr (&TARGET_EXPR_INITIAL (expr),
18656 : : pre_p, lhs_addr, lhs_var,
18657 : : target_expr, true, depth);
18658 : 702 : if (saw_lhs && target_expr == NULL_TREE && pre_p)
18659 : 26 : target_expr = expr;
18660 : : }
18661 : : }
18662 : : break;
18663 : : default:
18664 : : break;
18665 : : }
18666 : : break;
18667 : 565 : case tcc_reference:
18668 : 565 : if (TREE_CODE (expr) == BIT_FIELD_REF
18669 : 499 : || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
18670 : 362 : saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
18671 : : lhs_addr, lhs_var, target_expr, true,
18672 : : depth);
18673 : : break;
18674 : 58 : case tcc_vl_exp:
18675 : 58 : if (TREE_CODE (expr) == CALL_EXPR)
18676 : : {
18677 : 58 : if (tree fndecl = get_callee_fndecl (expr))
18678 : 58 : if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING,
18679 : : BUILT_IN_MEMCMP))
18680 : : {
18681 : 56 : int nargs = call_expr_nargs (expr);
18682 : 224 : for (int i = 0; i < nargs; i++)
18683 : 168 : saw_lhs |= goa_stabilize_expr (&CALL_EXPR_ARG (expr, i),
18684 : : pre_p, lhs_addr, lhs_var,
18685 : : target_expr, true, depth);
18686 : : }
18687 : : }
18688 : : break;
18689 : : default:
18690 : : break;
18691 : : }
18692 : :
18693 : 17261 : finish:
18694 : 17261 : if (saw_lhs == 0 && pre_p)
18695 : : {
18696 : 3498 : enum gimplify_status gs;
18697 : 3498 : if (TREE_CODE (expr) == CALL_EXPR && VOID_TYPE_P (TREE_TYPE (expr)))
18698 : : {
18699 : 0 : gimplify_stmt (&expr, pre_p);
18700 : 0 : return saw_lhs;
18701 : : }
18702 : 3498 : else if (rhs)
18703 : 3320 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
18704 : : else
18705 : 178 : gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_lvalue, fb_lvalue);
18706 : 3498 : if (gs != GS_ALL_DONE)
18707 : 36822 : saw_lhs = -1;
18708 : : }
18709 : :
18710 : : return saw_lhs;
18711 : : }
18712 : :
18713 : : /* Gimplify an OMP_ATOMIC statement. */
18714 : :
18715 : : static enum gimplify_status
18716 : 10249 : gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
18717 : : {
18718 : 10249 : tree addr = TREE_OPERAND (*expr_p, 0);
18719 : 10249 : tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
18720 : 10249 : ? NULL : TREE_OPERAND (*expr_p, 1);
18721 : 10249 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
18722 : 10249 : tree tmp_load;
18723 : 10249 : gomp_atomic_load *loadstmt;
18724 : 10249 : gomp_atomic_store *storestmt;
18725 : 10249 : tree target_expr = NULL_TREE;
18726 : :
18727 : 10249 : tmp_load = create_tmp_reg (type);
18728 : 10249 : if (rhs
18729 : 10249 : && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load, target_expr,
18730 : : true, 0) < 0)
18731 : : return GS_ERROR;
18732 : :
18733 : 10249 : if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
18734 : : != GS_ALL_DONE)
18735 : : return GS_ERROR;
18736 : :
18737 : 10249 : loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
18738 : 10249 : OMP_ATOMIC_MEMORY_ORDER (*expr_p));
18739 : 10249 : gimplify_seq_add_stmt (pre_p, loadstmt);
18740 : 10249 : if (rhs)
18741 : : {
18742 : : /* BIT_INSERT_EXPR is not valid for non-integral bitfield
18743 : : representatives. Use BIT_FIELD_REF on the lhs instead. */
18744 : 9068 : tree rhsarg = rhs;
18745 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
18746 : 428 : rhsarg = TREE_OPERAND (rhs, 1);
18747 : 9068 : if (TREE_CODE (rhsarg) == BIT_INSERT_EXPR
18748 : 9068 : && !INTEGRAL_TYPE_P (TREE_TYPE (tmp_load)))
18749 : : {
18750 : 32 : tree bitpos = TREE_OPERAND (rhsarg, 2);
18751 : 32 : tree op1 = TREE_OPERAND (rhsarg, 1);
18752 : 32 : tree bitsize;
18753 : 32 : tree tmp_store = tmp_load;
18754 : 32 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_CAPTURE_OLD)
18755 : 12 : tmp_store = get_initialized_tmp_var (tmp_load, pre_p);
18756 : 32 : if (INTEGRAL_TYPE_P (TREE_TYPE (op1)))
18757 : 32 : bitsize = bitsize_int (TYPE_PRECISION (TREE_TYPE (op1)));
18758 : : else
18759 : 0 : bitsize = TYPE_SIZE (TREE_TYPE (op1));
18760 : 32 : gcc_assert (TREE_OPERAND (rhsarg, 0) == tmp_load);
18761 : 32 : tree t = build2_loc (EXPR_LOCATION (rhsarg),
18762 : : MODIFY_EXPR, void_type_node,
18763 : 32 : build3_loc (EXPR_LOCATION (rhsarg),
18764 : 32 : BIT_FIELD_REF, TREE_TYPE (op1),
18765 : : tmp_store, bitsize, bitpos), op1);
18766 : 32 : if (TREE_CODE (rhs) == COND_EXPR)
18767 : 16 : t = build3_loc (EXPR_LOCATION (rhs), COND_EXPR, void_type_node,
18768 : 16 : TREE_OPERAND (rhs, 0), t, void_node);
18769 : 32 : gimplify_and_add (t, pre_p);
18770 : 32 : rhs = tmp_store;
18771 : : }
18772 : 9068 : bool save_allow_rhs_cond_expr = gimplify_ctxp->allow_rhs_cond_expr;
18773 : 9068 : if (TREE_CODE (rhs) == COND_EXPR)
18774 : 412 : gimplify_ctxp->allow_rhs_cond_expr = true;
18775 : 9068 : enum gimplify_status gs = gimplify_expr (&rhs, pre_p, NULL,
18776 : : is_gimple_val, fb_rvalue);
18777 : 9068 : gimplify_ctxp->allow_rhs_cond_expr = save_allow_rhs_cond_expr;
18778 : 9068 : if (gs != GS_ALL_DONE)
18779 : : return GS_ERROR;
18780 : : }
18781 : :
18782 : 10249 : if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
18783 : 1181 : rhs = tmp_load;
18784 : 10249 : storestmt
18785 : 10249 : = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
18786 : 10249 : if (TREE_CODE (*expr_p) != OMP_ATOMIC_READ && OMP_ATOMIC_WEAK (*expr_p))
18787 : : {
18788 : 37 : gimple_omp_atomic_set_weak (loadstmt);
18789 : 37 : gimple_omp_atomic_set_weak (storestmt);
18790 : : }
18791 : 10249 : gimplify_seq_add_stmt (pre_p, storestmt);
18792 : 10249 : switch (TREE_CODE (*expr_p))
18793 : : {
18794 : 2065 : case OMP_ATOMIC_READ:
18795 : 2065 : case OMP_ATOMIC_CAPTURE_OLD:
18796 : 2065 : *expr_p = tmp_load;
18797 : 2065 : gimple_omp_atomic_set_need_value (loadstmt);
18798 : 2065 : break;
18799 : 868 : case OMP_ATOMIC_CAPTURE_NEW:
18800 : 868 : *expr_p = rhs;
18801 : 868 : gimple_omp_atomic_set_need_value (storestmt);
18802 : 868 : break;
18803 : 7316 : default:
18804 : 7316 : *expr_p = NULL;
18805 : 7316 : break;
18806 : : }
18807 : :
18808 : : return GS_ALL_DONE;
18809 : : }
18810 : :
18811 : : /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
18812 : : body, and adding some EH bits. */
18813 : :
18814 : : static enum gimplify_status
18815 : 478 : gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
18816 : : {
18817 : 478 : tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
18818 : 478 : gimple *body_stmt;
18819 : 478 : gtransaction *trans_stmt;
18820 : 478 : gimple_seq body = NULL;
18821 : 478 : int subcode = 0;
18822 : :
18823 : : /* Wrap the transaction body in a BIND_EXPR so we have a context
18824 : : where to put decls for OMP. */
18825 : 478 : if (TREE_CODE (tbody) != BIND_EXPR)
18826 : : {
18827 : 439 : tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
18828 : 439 : TREE_SIDE_EFFECTS (bind) = 1;
18829 : 439 : SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
18830 : 439 : TRANSACTION_EXPR_BODY (expr) = bind;
18831 : : }
18832 : :
18833 : 478 : push_gimplify_context ();
18834 : 478 : temp = voidify_wrapper_expr (*expr_p, NULL);
18835 : :
18836 : 478 : body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
18837 : 478 : pop_gimplify_context (body_stmt);
18838 : :
18839 : 478 : trans_stmt = gimple_build_transaction (body);
18840 : 478 : if (TRANSACTION_EXPR_OUTER (expr))
18841 : : subcode = GTMA_IS_OUTER;
18842 : 447 : else if (TRANSACTION_EXPR_RELAXED (expr))
18843 : 81 : subcode = GTMA_IS_RELAXED;
18844 : 478 : gimple_transaction_set_subcode (trans_stmt, subcode);
18845 : :
18846 : 478 : gimplify_seq_add_stmt (pre_p, trans_stmt);
18847 : :
18848 : 478 : if (temp)
18849 : : {
18850 : 76 : *expr_p = temp;
18851 : 76 : return GS_OK;
18852 : : }
18853 : :
18854 : 402 : *expr_p = NULL_TREE;
18855 : 402 : return GS_ALL_DONE;
18856 : : }
18857 : :
18858 : : /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
18859 : : is the OMP_BODY of the original EXPR (which has already been
18860 : : gimplified so it's not present in the EXPR).
18861 : :
18862 : : Return the gimplified GIMPLE_OMP_ORDERED tuple. */
18863 : :
18864 : : static gimple *
18865 : 1940 : gimplify_omp_ordered (tree expr, gimple_seq body)
18866 : : {
18867 : 1940 : tree c, decls;
18868 : 1940 : int failures = 0;
18869 : 1940 : unsigned int i;
18870 : 1940 : tree source_c = NULL_TREE;
18871 : 1940 : tree sink_c = NULL_TREE;
18872 : :
18873 : 1940 : if (gimplify_omp_ctxp)
18874 : : {
18875 : 3271 : for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
18876 : 1505 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18877 : 1505 : && gimplify_omp_ctxp->loop_iter_var.is_empty ())
18878 : : {
18879 : 74 : error_at (OMP_CLAUSE_LOCATION (c),
18880 : : "%<ordered%> construct with %qs clause must be "
18881 : : "closely nested inside a loop with %<ordered%> clause",
18882 : 74 : OMP_CLAUSE_DOACROSS_DEPEND (c) ? "depend" : "doacross");
18883 : 74 : failures++;
18884 : : }
18885 : 1431 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18886 : 1431 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
18887 : : {
18888 : 637 : bool fail = false;
18889 : 637 : sink_c = c;
18890 : 637 : if (OMP_CLAUSE_DECL (c) == NULL_TREE)
18891 : 72 : continue; /* omp_cur_iteration - 1 */
18892 : 565 : for (decls = OMP_CLAUSE_DECL (c), i = 0;
18893 : 3191 : decls && TREE_CODE (decls) == TREE_LIST;
18894 : 2626 : decls = TREE_CHAIN (decls), ++i)
18895 : 2626 : if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
18896 : 4 : continue;
18897 : 5244 : else if (TREE_VALUE (decls)
18898 : 2622 : != gimplify_omp_ctxp->loop_iter_var[2 * i])
18899 : : {
18900 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
18901 : : "variable %qE is not an iteration "
18902 : : "of outermost loop %d, expected %qE",
18903 : 8 : TREE_VALUE (decls), i + 1,
18904 : 8 : gimplify_omp_ctxp->loop_iter_var[2 * i]);
18905 : 8 : fail = true;
18906 : 8 : failures++;
18907 : : }
18908 : : else
18909 : 5228 : TREE_VALUE (decls)
18910 : 2614 : = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
18911 : 1126 : if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
18912 : : {
18913 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
18914 : : "number of variables in %qs clause with "
18915 : : "%<sink%> modifier does not match number of "
18916 : : "iteration variables",
18917 : 16 : OMP_CLAUSE_DOACROSS_DEPEND (c)
18918 : : ? "depend" : "doacross");
18919 : 16 : failures++;
18920 : : }
18921 : : }
18922 : 794 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DOACROSS
18923 : 794 : && OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SOURCE)
18924 : : {
18925 : 470 : if (source_c)
18926 : : {
18927 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
18928 : : "more than one %qs clause with %<source%> "
18929 : : "modifier on an %<ordered%> construct",
18930 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c)
18931 : : ? "depend" : "doacross");
18932 : 4 : failures++;
18933 : : }
18934 : : else
18935 : : source_c = c;
18936 : : }
18937 : : }
18938 : 1940 : if (source_c && sink_c)
18939 : : {
18940 : 4 : error_at (OMP_CLAUSE_LOCATION (source_c),
18941 : : "%qs clause with %<source%> modifier specified "
18942 : : "together with %qs clauses with %<sink%> modifier "
18943 : : "on the same construct",
18944 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (source_c) ? "depend" : "doacross",
18945 : 4 : OMP_CLAUSE_DOACROSS_DEPEND (sink_c) ? "depend" : "doacross");
18946 : 4 : failures++;
18947 : : }
18948 : :
18949 : 1940 : if (failures)
18950 : 102 : return gimple_build_nop ();
18951 : 1838 : return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
18952 : : }
18953 : :
18954 : : /* Gimplify an OMP_INTEROP statement. */
18955 : :
18956 : : static enum gimplify_status
18957 : 612 : gimplify_omp_interop (tree *expr_p, gimple_seq *pre_p)
18958 : : {
18959 : 612 : tree expr = *expr_p;
18960 : :
18961 : 612 : gimplify_scan_omp_clauses (&OMP_INTEROP_CLAUSES (expr), pre_p, ORT_TASK,
18962 : : OMP_INTEROP);
18963 : 612 : gimple *stmt = gimple_build_omp_interop (OMP_INTEROP_CLAUSES (expr));
18964 : 612 : gimplify_seq_add_stmt (pre_p, stmt);
18965 : 612 : *expr_p = NULL_TREE;
18966 : 612 : return GS_ALL_DONE;
18967 : : }
18968 : :
18969 : : /* Callback for walk_tree to find an IFN_GOMP_DISPATCH. */
18970 : :
18971 : : static tree
18972 : 3081 : find_ifn_gomp_dispatch (tree *tp, int *, void *modify)
18973 : : {
18974 : 3081 : tree t = *tp;
18975 : :
18976 : 3081 : if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_IFN (t) == IFN_GOMP_DISPATCH)
18977 : 1406 : return *(tree *) modify ? *(tree *) modify : *tp;
18978 : :
18979 : 2236 : if (TREE_CODE (t) == MODIFY_EXPR)
18980 : 507 : *(tree *) modify = *tp;
18981 : :
18982 : : return NULL_TREE;
18983 : : }
18984 : :
18985 : : /* Gimplify an OMP_DISPATCH construct. */
18986 : :
18987 : : static enum gimplify_status
18988 : 845 : gimplify_omp_dispatch (tree *expr_p, gimple_seq *pre_p)
18989 : : {
18990 : 845 : tree expr = *expr_p;
18991 : 845 : gimple_seq body = NULL;
18992 : :
18993 : 845 : gimplify_scan_omp_clauses (&OMP_DISPATCH_CLAUSES (expr), pre_p, ORT_DISPATCH,
18994 : : OMP_DISPATCH);
18995 : 845 : push_gimplify_context ();
18996 : :
18997 : : // If device clause, adjust ICV
18998 : 845 : tree device
18999 : 845 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_DEVICE);
19000 : : // If no device clause exists but an interop clause with a single list
19001 : : // item, use it to obtain the device number.
19002 : 845 : if (device)
19003 : 272 : device = OMP_CLAUSE_DEVICE_ID (device);
19004 : : else
19005 : : {
19006 : 573 : tree first_interop_obj
19007 : 573 : = omp_find_clause (OMP_DISPATCH_CLAUSES (expr), OMP_CLAUSE_INTEROP);
19008 : 573 : if (first_interop_obj)
19009 : 95 : for (tree c = TREE_CHAIN (first_interop_obj); c; c = TREE_CHAIN (c))
19010 : 8 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP)
19011 : : {
19012 : : first_interop_obj = NULL_TREE;
19013 : : break;
19014 : : }
19015 : 94 : if (first_interop_obj)
19016 : : {
19017 : 87 : device = create_tmp_var (integer_type_node);
19018 : 87 : tree c = build_omp_clause (OMP_CLAUSE_LOCATION (first_interop_obj),
19019 : : OMP_CLAUSE_DEVICE);
19020 : 87 : OMP_CLAUSE_DEVICE_ID (c) = device;
19021 : 87 : TREE_CHAIN (c) = TREE_CHAIN (first_interop_obj);
19022 : 87 : TREE_CHAIN (first_interop_obj) = c;
19023 : 87 : first_interop_obj = OMP_CLAUSE_DECL (first_interop_obj);
19024 : : /* device = omp_get_interop_int (obj, omp_ipr_device_num, NULL); */
19025 : 87 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_INTEROP_INT);
19026 : 87 : fn = build_call_expr (fn, 3, first_interop_obj,
19027 : : build_int_cst (integer_type_node, -5),
19028 : : null_pointer_node);
19029 : 87 : gimplify_assign (device, fold_convert (integer_type_node, fn), &body);
19030 : : }
19031 : : }
19032 : 845 : tree saved_device_icv = NULL_TREE;
19033 : 845 : if (device
19034 : 845 : && (TREE_CODE (device) != INTEGER_CST
19035 : 703 : || !wi::eq_p (wi::to_wide (device), -1 /* omp_initial_device */)))
19036 : : {
19037 : : // Save current default-device-var ICV
19038 : 359 : saved_device_icv = create_tmp_var (integer_type_node);
19039 : 359 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_DEFAULT_DEVICE);
19040 : 359 : gcall *call = gimple_build_call (fn, 0);
19041 : 359 : gimple_call_set_lhs (call, saved_device_icv);
19042 : 359 : gimplify_seq_add_stmt (&body, call);
19043 : :
19044 : : // Set default device
19045 : 359 : fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19046 : 359 : call = gimple_build_call (fn, 1, device);
19047 : 359 : gimplify_seq_add_stmt (&body, call);
19048 : : }
19049 : :
19050 : : // If the novariants and nocontext clauses are not compile-time constants,
19051 : : // we need to generate code for all possible cases:
19052 : : // if (novariants) // implies nocontext
19053 : : // base()
19054 : : // else if (nocontext)
19055 : : // variant1()
19056 : : // else
19057 : : // variant2()
19058 : 845 : tree *dispatch_body_p = &OMP_DISPATCH_BODY (expr);
19059 : 845 : if (TREE_CODE (*dispatch_body_p) == BIND_EXPR)
19060 : 28 : dispatch_body_p = &BIND_EXPR_BODY (*dispatch_body_p);
19061 : 845 : tree dispatch_body = *dispatch_body_p;
19062 : :
19063 : : // Look for IFN_GOMP_DISPATCH and extract the base function call
19064 : 845 : tree base_call_expr = NULL_TREE;
19065 : 845 : if (TREE_CODE (dispatch_body) == STATEMENT_LIST)
19066 : 243 : for (tree_stmt_iterator tsi = tsi_start (dispatch_body); !tsi_end_p (tsi);
19067 : 187 : tsi_next (&tsi))
19068 : : {
19069 : 243 : tree modify = NULL_TREE;
19070 : 243 : tree stmt = tsi_stmt (tsi);
19071 : 243 : base_call_expr
19072 : 243 : = walk_tree (&stmt, find_ifn_gomp_dispatch, &modify, NULL);
19073 : 243 : if (base_call_expr != NULL_TREE)
19074 : : break;
19075 : : }
19076 : : else
19077 : : {
19078 : 789 : tree modify = NULL_TREE;
19079 : 789 : base_call_expr
19080 : 789 : = walk_tree (dispatch_body_p, find_ifn_gomp_dispatch, &modify, NULL);
19081 : : }
19082 : 845 : gcc_assert (base_call_expr != NULL_TREE);
19083 : :
19084 : 845 : tree dst = NULL_TREE;
19085 : 845 : if (TREE_CODE (base_call_expr) == MODIFY_EXPR)
19086 : : {
19087 : 284 : dst = TREE_OPERAND (base_call_expr, 0);
19088 : 284 : base_call_expr = TREE_OPERAND (base_call_expr, 1);
19089 : : }
19090 : :
19091 : 873 : while (TREE_CODE (base_call_expr) == FLOAT_EXPR
19092 : : || TREE_CODE (base_call_expr) == CONVERT_EXPR
19093 : : || TREE_CODE (base_call_expr) == COMPLEX_EXPR
19094 : : || TREE_CODE (base_call_expr) == INDIRECT_REF
19095 : 873 : || TREE_CODE (base_call_expr) == NOP_EXPR)
19096 : 28 : base_call_expr = TREE_OPERAND (base_call_expr, 0);
19097 : :
19098 : 845 : gcc_assert (CALL_EXPR_IFN (base_call_expr) == IFN_GOMP_DISPATCH);
19099 : 845 : base_call_expr = CALL_EXPR_ARG (base_call_expr, 0);
19100 : :
19101 : 845 : tree base_fndecl = get_callee_fndecl (base_call_expr);
19102 : 845 : if (base_fndecl != NULL_TREE)
19103 : : {
19104 : 837 : if (DECL_VIRTUAL_P (base_fndecl))
19105 : : {
19106 : 6 : error_at (
19107 : 3 : EXPR_LOCATION (base_call_expr),
19108 : : "%qD is a virtual function but only a direct call is allowed "
19109 : : "in a dispatch construct",
19110 : 3 : DECL_NAME (base_fndecl));
19111 : : }
19112 : :
19113 : : /* We are not actually going to expand the variant call or use
19114 : : the result of omp_get_dynamic candidates here; only check that
19115 : : it does not trivially resolve to a call to the base function
19116 : : so that we can avoid some extra work in building code that's
19117 : : not needed in that case. */
19118 : 837 : tree construct_context = omp_get_construct_context ();
19119 : 837 : vec<struct omp_variant> all_candidates
19120 : 837 : = omp_declare_variant_candidates (base_fndecl, construct_context);
19121 : 837 : gcc_assert (!all_candidates.is_empty ());
19122 : 837 : vec<struct omp_variant> candidates
19123 : 837 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19124 : 837 : tree variant_fndecl
19125 : 1662 : = (candidates.length () == 1 ? candidates[0].alternative : NULL_TREE);
19126 : :
19127 : 837 : if (base_fndecl != variant_fndecl
19128 : 837 : && (omp_has_novariants () == -1 || omp_has_nocontext () == -1))
19129 : : {
19130 : 22 : tree novariants_clause = NULL_TREE, nocontext_clause = NULL_TREE,
19131 : 22 : novariants_cond = NULL_TREE, nocontext_cond = NULL_TREE;
19132 : 59 : for (tree c = OMP_DISPATCH_CLAUSES (expr); c; c = TREE_CHAIN (c))
19133 : : {
19134 : 37 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOVARIANTS
19135 : 37 : && !integer_zerop (OMP_CLAUSE_NOVARIANTS_EXPR (c)))
19136 : : {
19137 : 16 : gcc_assert (novariants_cond == NULL_TREE);
19138 : 16 : novariants_clause = c;
19139 : 16 : novariants_cond = OMP_CLAUSE_NOVARIANTS_EXPR (c);
19140 : : }
19141 : 21 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOCONTEXT
19142 : 21 : && !integer_zerop (OMP_CLAUSE_NOCONTEXT_EXPR (c)))
19143 : : {
19144 : 16 : gcc_assert (nocontext_cond == NULL_TREE);
19145 : 16 : nocontext_clause = c;
19146 : 16 : nocontext_cond = OMP_CLAUSE_NOCONTEXT_EXPR (c);
19147 : : }
19148 : : }
19149 : 22 : gcc_assert (novariants_cond != NULL_TREE
19150 : : || nocontext_cond != NULL_TREE);
19151 : :
19152 : 22 : enum gimplify_status ret
19153 : 22 : = gimplify_expr (&novariants_cond, &body, NULL, is_gimple_val,
19154 : : fb_rvalue);
19155 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19156 : 0 : return ret;
19157 : 22 : ret = gimplify_expr (&nocontext_cond, &body, NULL, is_gimple_val,
19158 : : fb_rvalue);
19159 : 22 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19160 : : return ret;
19161 : :
19162 : 22 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19163 : :
19164 : 22 : if (novariants_cond != NULL_TREE)
19165 : : {
19166 : 16 : tree base_label = create_artificial_label (UNKNOWN_LOCATION);
19167 : 16 : tree cond_label = create_artificial_label (UNKNOWN_LOCATION);
19168 : 16 : gcond *novariants_cond_stmt
19169 : 16 : = gimple_build_cond_from_tree (novariants_cond, base_label,
19170 : : cond_label);
19171 : 16 : gimplify_seq_add_stmt (&body, novariants_cond_stmt);
19172 : :
19173 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (base_label));
19174 : 16 : tree base_call_expr2 = copy_node (base_call_expr);
19175 : 16 : base_call_expr2
19176 : 16 : = build_call_expr_internal_loc (EXPR_LOCATION (base_call_expr2),
19177 : : IFN_GOMP_DISPATCH,
19178 : 16 : TREE_TYPE (base_call_expr2), 1,
19179 : : base_call_expr2);
19180 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19181 : : {
19182 : 16 : base_call_expr2 = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19183 : : base_call_expr2);
19184 : : }
19185 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19186 : 16 : = boolean_true_node;
19187 : 16 : gimplify_and_add (base_call_expr2, &body);
19188 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19189 : :
19190 : 16 : OMP_CLAUSE_NOVARIANTS_EXPR (novariants_clause)
19191 : 16 : = boolean_false_node;
19192 : 16 : gimplify_seq_add_stmt (&body, gimple_build_label (cond_label));
19193 : : }
19194 : :
19195 : 22 : if (nocontext_cond != NULL_TREE)
19196 : : {
19197 : 16 : tree variant1_label = create_artificial_label (UNKNOWN_LOCATION);
19198 : 16 : tree variant2_label = create_artificial_label (UNKNOWN_LOCATION);
19199 : 16 : gcond *nocontext_cond_stmt
19200 : 16 : = gimple_build_cond_from_tree (nocontext_cond, variant1_label,
19201 : : variant2_label);
19202 : 16 : gimplify_seq_add_stmt (&body, nocontext_cond_stmt);
19203 : :
19204 : 32 : gimplify_seq_add_stmt (&body,
19205 : 16 : gimple_build_label (variant1_label));
19206 : 16 : tree variant_call_expr = copy_node (base_call_expr);
19207 : 32 : variant_call_expr = build_call_expr_internal_loc (
19208 : 16 : EXPR_LOCATION (variant_call_expr), IFN_GOMP_DISPATCH,
19209 : 16 : TREE_TYPE (variant_call_expr), 1, variant_call_expr);
19210 : 16 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19211 : : {
19212 : 16 : variant_call_expr = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst,
19213 : : variant_call_expr);
19214 : : }
19215 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_true_node;
19216 : 16 : gimplify_and_add (variant_call_expr, &body);
19217 : 16 : gimplify_seq_add_stmt (&body, gimple_build_goto (end_label));
19218 : 16 : OMP_CLAUSE_NOCONTEXT_EXPR (nocontext_clause) = boolean_false_node;
19219 : 32 : gimplify_seq_add_stmt (&body,
19220 : 16 : gimple_build_label (variant2_label));
19221 : : }
19222 : :
19223 : 22 : tree variant_call_expr = base_call_expr;
19224 : 22 : variant_call_expr
19225 : 22 : = build_call_expr_internal_loc (EXPR_LOCATION (variant_call_expr),
19226 : : IFN_GOMP_DISPATCH,
19227 : 22 : TREE_TYPE (variant_call_expr), 1,
19228 : : variant_call_expr);
19229 : 22 : if (TREE_CODE (dispatch_body) == MODIFY_EXPR)
19230 : : {
19231 : 22 : variant_call_expr
19232 : 22 : = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, variant_call_expr);
19233 : : }
19234 : 22 : gimplify_and_add (variant_call_expr, &body);
19235 : 22 : gimplify_seq_add_stmt (&body, gimple_build_label (end_label));
19236 : : }
19237 : : else
19238 : 815 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19239 : : }
19240 : : else
19241 : 8 : gimplify_and_add (OMP_DISPATCH_BODY (expr), &body);
19242 : :
19243 : : // Restore default-device-var ICV
19244 : 845 : if (saved_device_icv != NULL_TREE)
19245 : : {
19246 : 359 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_SET_DEFAULT_DEVICE);
19247 : 359 : gcall *call = gimple_build_call (fn, 1, saved_device_icv);
19248 : 359 : gimplify_seq_add_stmt (&body, call);
19249 : : }
19250 : :
19251 : : // Wrap dispatch body into a bind
19252 : 845 : gimple *bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
19253 : 845 : pop_gimplify_context (bind);
19254 : :
19255 : : // Manually tear down context created by gimplify_scan_omp_clauses to avoid a
19256 : : // call to gimplify_adjust_omp_clauses
19257 : 845 : gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
19258 : 845 : if (ctx != NULL)
19259 : : {
19260 : 845 : gcc_assert (ctx->code == OMP_DISPATCH);
19261 : 845 : gimplify_omp_ctxp = ctx->outer_context;
19262 : 845 : delete_omp_context (ctx);
19263 : : }
19264 : :
19265 : : // Remove nowait as it has no effect on dispatch (OpenMP 5.2), device as it
19266 : : // has been handled above, and depend as the front end handled it by inserting
19267 : : // taskwait.
19268 : 845 : tree *dispatch_clauses_ptr = &OMP_DISPATCH_CLAUSES (expr);
19269 : 1431 : for (tree c = *dispatch_clauses_ptr; c; c = *dispatch_clauses_ptr)
19270 : : {
19271 : 983 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NOWAIT
19272 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
19273 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE)
19274 : : {
19275 : 397 : *dispatch_clauses_ptr = OMP_CLAUSE_CHAIN (c);
19276 : 397 : break;
19277 : : }
19278 : : else
19279 : 586 : dispatch_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
19280 : : }
19281 : :
19282 : 845 : gimple *stmt = gimple_build_omp_dispatch (bind, OMP_DISPATCH_CLAUSES (expr));
19283 : 845 : gimplify_seq_add_stmt (pre_p, stmt);
19284 : 845 : *expr_p = NULL_TREE;
19285 : 845 : return GS_ALL_DONE;
19286 : : }
19287 : :
19288 : : /* Expand a metadirective that has been resolved at gimplification time
19289 : : into the candidate directive variants in CANDIDATES. */
19290 : :
19291 : : static enum gimplify_status
19292 : 158 : expand_omp_metadirective (vec<struct omp_variant> &candidates,
19293 : : gimple_seq *pre_p)
19294 : : {
19295 : 158 : auto_vec<tree> selectors;
19296 : 158 : auto_vec<tree> directive_labels;
19297 : 158 : auto_vec<gimple_seq> directive_bodies;
19298 : 158 : tree body_label = NULL_TREE;
19299 : 158 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19300 : :
19301 : : /* Construct bodies for each candidate. */
19302 : 381 : for (unsigned i = 0; i < candidates.length(); i++)
19303 : : {
19304 : 223 : struct omp_variant &candidate = candidates[i];
19305 : 223 : gimple_seq body = NULL;
19306 : :
19307 : 223 : selectors.safe_push (omp_dynamic_cond (candidate.selector,
19308 : : find_supercontext ()));
19309 : 223 : directive_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19310 : :
19311 : 223 : gimplify_seq_add_stmt (&body,
19312 : 223 : gimple_build_label (directive_labels.last ()));
19313 : 223 : if (candidate.alternative != NULL_TREE)
19314 : 223 : gimplify_stmt (&candidate.alternative, &body);
19315 : 223 : if (candidate.body != NULL_TREE)
19316 : : {
19317 : 34 : if (body_label != NULL_TREE)
19318 : 0 : gimplify_seq_add_stmt (&body, gimple_build_goto (body_label));
19319 : : else
19320 : : {
19321 : 34 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19322 : 34 : gimplify_seq_add_stmt (&body, gimple_build_label (body_label));
19323 : 34 : gimplify_stmt (&candidate.body, &body);
19324 : : }
19325 : : }
19326 : :
19327 : 223 : directive_bodies.safe_push (body);
19328 : : }
19329 : :
19330 : 158 : auto_vec<tree> cond_labels;
19331 : :
19332 : 158 : cond_labels.safe_push (NULL_TREE);
19333 : 352 : for (unsigned i = 1; i < candidates.length () - 1; i++)
19334 : 18 : cond_labels.safe_push (create_artificial_label (UNKNOWN_LOCATION));
19335 : 158 : if (candidates.length () > 1)
19336 : 47 : cond_labels.safe_push (directive_labels.last ());
19337 : :
19338 : : /* Generate conditionals to test each dynamic selector in turn, executing
19339 : : the directive candidate if successful. */
19340 : 446 : for (unsigned i = 0; i < candidates.length () - 1; i++)
19341 : : {
19342 : 65 : if (i != 0)
19343 : 18 : gimplify_seq_add_stmt (pre_p, gimple_build_label (cond_labels [i]));
19344 : :
19345 : 65 : enum gimplify_status ret = gimplify_expr (&selectors[i], pre_p, NULL,
19346 : : is_gimple_val, fb_rvalue);
19347 : 65 : if (ret == GS_ERROR || ret == GS_UNHANDLED)
19348 : : return ret;
19349 : :
19350 : 65 : gcond *cond_stmt
19351 : 65 : = gimple_build_cond_from_tree (selectors[i], directive_labels[i],
19352 : 65 : cond_labels[i + 1]);
19353 : :
19354 : 65 : gimplify_seq_add_stmt (pre_p, cond_stmt);
19355 : 65 : gimplify_seq_add_seq (pre_p, directive_bodies[i]);
19356 : 65 : gimplify_seq_add_stmt (pre_p, gimple_build_goto (end_label));
19357 : : }
19358 : :
19359 : 158 : gimplify_seq_add_seq (pre_p, directive_bodies.last ());
19360 : 158 : gimplify_seq_add_stmt (pre_p, gimple_build_label (end_label));
19361 : :
19362 : 158 : return GS_ALL_DONE;
19363 : 158 : }
19364 : :
19365 : : /* Expand a variant construct that requires late resolution in the ompdevlow
19366 : : pass. It's a bit easier to do this in tree form and then gimplify that,
19367 : : than to emit gimple. The output is going to look something like:
19368 : :
19369 : : switch_var = OMP_NEXT_VARIANT (0, state);
19370 : : loop_label:
19371 : : switch (switch_var)
19372 : : {
19373 : : case 1:
19374 : : if (dynamic_selector_predicate_1)
19375 : : {
19376 : : alternative_1;
19377 : : goto end_label;
19378 : : }
19379 : : else
19380 : : {
19381 : : switch_var = OMP_NEXT_VARIANT (1, state);
19382 : : goto loop_label;
19383 : : }
19384 : : case 2:
19385 : : ...
19386 : : }
19387 : : end_label:
19388 : :
19389 : : OMP_NEXT_VARIANT is a magic cookie that is replaced with the switch variable
19390 : : index of the next variant to try, after late resolution. */
19391 : :
19392 : : static tree
19393 : 16 : expand_late_variant_directive (vec<struct omp_variant> all_candidates,
19394 : : tree construct_context)
19395 : : {
19396 : 16 : tree body_label = NULL_TREE;
19397 : 16 : tree standalone_body = NULL_TREE;
19398 : 16 : tree loop_label = create_artificial_label (UNKNOWN_LOCATION);
19399 : 16 : tree end_label = create_artificial_label (UNKNOWN_LOCATION);
19400 : 32 : tree selectors = make_tree_vec (all_candidates.length ());
19401 : 16 : tree switch_body = NULL_TREE;
19402 : 16 : tree switch_var = create_tmp_var (integer_type_node, "variant");
19403 : 16 : tree state = tree_cons (NULL_TREE, construct_context, selectors);
19404 : :
19405 : 92 : for (unsigned int i = 0; i < all_candidates.length (); i++)
19406 : : {
19407 : 76 : tree selector = all_candidates[i].selector;
19408 : 76 : tree alternative = all_candidates[i].alternative;
19409 : 76 : tree body = all_candidates[i].body;
19410 : 76 : TREE_VEC_ELT (selectors, i) = selector;
19411 : :
19412 : : /* Case label. Numbering is 1-based. */
19413 : 76 : tree case_val = build_int_cst (integer_type_node, i + 1);
19414 : 76 : tree case_label
19415 : 76 : = build_case_label (case_val, NULL_TREE,
19416 : : create_artificial_label (UNKNOWN_LOCATION));
19417 : 76 : append_to_statement_list (case_label, &switch_body);
19418 : :
19419 : : /* The actual body of the variant. */
19420 : 76 : tree variant_body = NULL_TREE;
19421 : 76 : append_to_statement_list (alternative, &variant_body);
19422 : :
19423 : 76 : if (body != NULL_TREE)
19424 : : {
19425 : 0 : if (standalone_body == NULL)
19426 : : {
19427 : 0 : standalone_body = body;
19428 : 0 : body_label = create_artificial_label (UNKNOWN_LOCATION);
19429 : : }
19430 : 0 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19431 : : body_label),
19432 : : &variant_body);
19433 : : }
19434 : : else
19435 : 76 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19436 : : end_label),
19437 : : &variant_body);
19438 : :
19439 : : /* If this is a dynamic selector, wrap variant_body with a conditional.
19440 : : If the predicate doesn't match, the else clause sets switch_var and
19441 : : jumps to loop_var to try again. */
19442 : 76 : tree dynamic_selector = omp_dynamic_cond (selector, find_supercontext ());
19443 : 76 : if (dynamic_selector)
19444 : : {
19445 : 20 : tree else_stmt = NULL_TREE;
19446 : 20 : tree next = build2 (OMP_NEXT_VARIANT, integer_type_node,
19447 : : case_val, state);
19448 : 20 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19449 : : switch_var, next),
19450 : : &else_stmt);
19451 : 20 : append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
19452 : : loop_label),
19453 : : &else_stmt);
19454 : 20 : variant_body = build3 (COND_EXPR, void_type_node, dynamic_selector,
19455 : : variant_body, else_stmt);
19456 : : }
19457 : 76 : append_to_statement_list (variant_body, &switch_body);
19458 : : }
19459 : :
19460 : : /* Put it all together. */
19461 : 16 : tree result = NULL_TREE;
19462 : 16 : tree first = build2 (OMP_NEXT_VARIANT, integer_type_node, integer_zero_node,
19463 : : state);
19464 : 16 : append_to_statement_list (build2 (MODIFY_EXPR, integer_type_node,
19465 : : switch_var, first),
19466 : : &result);
19467 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, loop_label),
19468 : : &result);
19469 : 16 : append_to_statement_list (build2 (SWITCH_EXPR, integer_type_node,
19470 : : switch_var, switch_body),
19471 : : &result);
19472 : 16 : if (standalone_body)
19473 : : {
19474 : 0 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
19475 : : body_label),
19476 : : &result);
19477 : 0 : append_to_statement_list (standalone_body, &result);
19478 : : }
19479 : 16 : append_to_statement_list (build1 (LABEL_EXPR, void_type_node, end_label),
19480 : : &result);
19481 : 16 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
19482 : 16 : return result;
19483 : : }
19484 : :
19485 : :
19486 : : /* Gimplify an OMP_METADIRECTIVE construct. EXPR is the tree version.
19487 : : The metadirective will be resolved at this point if possible, otherwise
19488 : : a GIMPLE_OMP_VARIANT_CONSTRUCT is created. */
19489 : :
19490 : : static enum gimplify_status
19491 : 162 : gimplify_omp_metadirective (tree *expr_p, gimple_seq *pre_p, gimple_seq *,
19492 : : bool (*) (tree), fallback_t)
19493 : : {
19494 : : /* Try to resolve the metadirective. */
19495 : 162 : tree construct_context = omp_get_construct_context ();
19496 : 162 : vec<struct omp_variant> all_candidates
19497 : 162 : = omp_metadirective_candidates (*expr_p, construct_context);
19498 : 162 : vec<struct omp_variant> candidates
19499 : 162 : = omp_get_dynamic_candidates (all_candidates, construct_context);
19500 : 162 : if (!candidates.is_empty ())
19501 : 158 : return expand_omp_metadirective (candidates, pre_p);
19502 : :
19503 : : /* The metadirective cannot be resolved yet. Turn it into a loop with
19504 : : a nested switch statement, using OMP_NEXT_VARIANT to set the control
19505 : : variable for the switch. */
19506 : 4 : *expr_p = expand_late_variant_directive (all_candidates, construct_context);
19507 : 4 : return GS_OK;
19508 : : }
19509 : :
19510 : : /* Gimplify an OMP_DECLARE_MAPPER node (by just removing it). */
19511 : :
19512 : : static enum gimplify_status
19513 : 0 : gimplify_omp_declare_mapper (tree *expr_p)
19514 : : {
19515 : 0 : *expr_p = NULL_TREE;
19516 : 0 : return GS_ALL_DONE;
19517 : : }
19518 : :
19519 : : /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
19520 : : expression produces a value to be used as an operand inside a GIMPLE
19521 : : statement, the value will be stored back in *EXPR_P. This value will
19522 : : be a tree of class tcc_declaration, tcc_constant, tcc_reference or
19523 : : an SSA_NAME. The corresponding sequence of GIMPLE statements is
19524 : : emitted in PRE_P and POST_P.
19525 : :
19526 : : Additionally, this process may overwrite parts of the input
19527 : : expression during gimplification. Ideally, it should be
19528 : : possible to do non-destructive gimplification.
19529 : :
19530 : : EXPR_P points to the GENERIC expression to convert to GIMPLE. If
19531 : : the expression needs to evaluate to a value to be used as
19532 : : an operand in a GIMPLE statement, this value will be stored in
19533 : : *EXPR_P on exit. This happens when the caller specifies one
19534 : : of fb_lvalue or fb_rvalue fallback flags.
19535 : :
19536 : : PRE_P will contain the sequence of GIMPLE statements corresponding
19537 : : to the evaluation of EXPR and all the side-effects that must
19538 : : be executed before the main expression. On exit, the last
19539 : : statement of PRE_P is the core statement being gimplified. For
19540 : : instance, when gimplifying 'if (++a)' the last statement in
19541 : : PRE_P will be 'if (t.1)' where t.1 is the result of
19542 : : pre-incrementing 'a'.
19543 : :
19544 : : POST_P will contain the sequence of GIMPLE statements corresponding
19545 : : to the evaluation of all the side-effects that must be executed
19546 : : after the main expression. If this is NULL, the post
19547 : : side-effects are stored at the end of PRE_P.
19548 : :
19549 : : The reason why the output is split in two is to handle post
19550 : : side-effects explicitly. In some cases, an expression may have
19551 : : inner and outer post side-effects which need to be emitted in
19552 : : an order different from the one given by the recursive
19553 : : traversal. For instance, for the expression (*p--)++ the post
19554 : : side-effects of '--' must actually occur *after* the post
19555 : : side-effects of '++'. However, gimplification will first visit
19556 : : the inner expression, so if a separate POST sequence was not
19557 : : used, the resulting sequence would be:
19558 : :
19559 : : 1 t.1 = *p
19560 : : 2 p = p - 1
19561 : : 3 t.2 = t.1 + 1
19562 : : 4 *p = t.2
19563 : :
19564 : : However, the post-decrement operation in line #2 must not be
19565 : : evaluated until after the store to *p at line #4, so the
19566 : : correct sequence should be:
19567 : :
19568 : : 1 t.1 = *p
19569 : : 2 t.2 = t.1 + 1
19570 : : 3 *p = t.2
19571 : : 4 p = p - 1
19572 : :
19573 : : So, by specifying a separate post queue, it is possible
19574 : : to emit the post side-effects in the correct order.
19575 : : If POST_P is NULL, an internal queue will be used. Before
19576 : : returning to the caller, the sequence POST_P is appended to
19577 : : the main output sequence PRE_P.
19578 : :
19579 : : GIMPLE_TEST_F points to a function that takes a tree T and
19580 : : returns nonzero if T is in the GIMPLE form requested by the
19581 : : caller. The GIMPLE predicates are in gimple.cc.
19582 : :
19583 : : FALLBACK tells the function what sort of a temporary we want if
19584 : : gimplification cannot produce an expression that complies with
19585 : : GIMPLE_TEST_F.
19586 : :
19587 : : fb_none means that no temporary should be generated
19588 : : fb_rvalue means that an rvalue is OK to generate
19589 : : fb_lvalue means that an lvalue is OK to generate
19590 : : fb_either means that either is OK, but an lvalue is preferable.
19591 : : fb_mayfail means that gimplification may fail (in which case
19592 : : GS_ERROR will be returned)
19593 : :
19594 : : The return value is either GS_ERROR or GS_ALL_DONE, since this
19595 : : function iterates until EXPR is completely gimplified or an error
19596 : : occurs. */
19597 : :
19598 : : enum gimplify_status
19599 : 513049612 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
19600 : : bool (*gimple_test_f) (tree), fallback_t fallback)
19601 : : {
19602 : 513049612 : tree tmp;
19603 : 513049612 : gimple_seq internal_pre = NULL;
19604 : 513049612 : gimple_seq internal_post = NULL;
19605 : 513049612 : tree save_expr;
19606 : 513049612 : bool is_statement;
19607 : 513049612 : location_t saved_location;
19608 : 513049612 : enum gimplify_status ret;
19609 : 513049612 : gimple_stmt_iterator pre_last_gsi, post_last_gsi;
19610 : 513049612 : tree label;
19611 : :
19612 : 513049612 : save_expr = *expr_p;
19613 : 513049612 : if (save_expr == NULL_TREE)
19614 : : return GS_ALL_DONE;
19615 : :
19616 : : /* If we are gimplifying a top-level statement, PRE_P must be valid. */
19617 : 462388114 : is_statement = gimple_test_f == is_gimple_stmt;
19618 : 462388114 : if (is_statement)
19619 : 101181980 : gcc_assert (pre_p);
19620 : :
19621 : : /* Consistency checks. */
19622 : 462388114 : if (gimple_test_f == is_gimple_reg)
19623 : 6492500 : gcc_assert (fallback & (fb_rvalue | fb_lvalue));
19624 : 455895614 : else if (gimple_test_f == is_gimple_val
19625 : 337274862 : || gimple_test_f == is_gimple_call_addr
19626 : 320909042 : || gimple_test_f == is_gimple_condexpr_for_cond
19627 : 315381865 : || gimple_test_f == is_gimple_mem_rhs
19628 : 315238960 : || gimple_test_f == is_gimple_mem_rhs_or_call
19629 : 304122786 : || gimple_test_f == is_gimple_reg_rhs
19630 : 303535448 : || gimple_test_f == is_gimple_reg_rhs_or_call
19631 : 232670662 : || gimple_test_f == is_gimple_asm_val
19632 : 232632979 : || gimple_test_f == is_gimple_mem_ref_addr)
19633 : 238840875 : gcc_assert (fallback & fb_rvalue);
19634 : 217054739 : else if (gimple_test_f == is_gimple_min_lval
19635 : 187477420 : || gimple_test_f == is_gimple_lvalue)
19636 : 82631048 : gcc_assert (fallback & fb_lvalue);
19637 : 134423691 : else if (gimple_test_f == is_gimple_addressable)
19638 : 33241711 : gcc_assert (fallback & fb_either);
19639 : 101181980 : else if (gimple_test_f == is_gimple_stmt)
19640 : 101181980 : gcc_assert (fallback == fb_none);
19641 : : else
19642 : : {
19643 : : /* We should have recognized the GIMPLE_TEST_F predicate to
19644 : : know what kind of fallback to use in case a temporary is
19645 : : needed to hold the value or address of *EXPR_P. */
19646 : 0 : gcc_unreachable ();
19647 : : }
19648 : :
19649 : : /* We used to check the predicate here and return immediately if it
19650 : : succeeds. This is wrong; the design is for gimplification to be
19651 : : idempotent, and for the predicates to only test for valid forms, not
19652 : : whether they are fully simplified. */
19653 : 462388114 : if (pre_p == NULL)
19654 : 0 : pre_p = &internal_pre;
19655 : :
19656 : 462388114 : if (post_p == NULL)
19657 : 186818534 : post_p = &internal_post;
19658 : :
19659 : : /* Remember the last statements added to PRE_P and POST_P. Every
19660 : : new statement added by the gimplification helpers needs to be
19661 : : annotated with location information. To centralize the
19662 : : responsibility, we remember the last statement that had been
19663 : : added to both queues before gimplifying *EXPR_P. If
19664 : : gimplification produces new statements in PRE_P and POST_P, those
19665 : : statements will be annotated with the same location information
19666 : : as *EXPR_P. */
19667 : 462388114 : pre_last_gsi = gsi_last (*pre_p);
19668 : 462388114 : post_last_gsi = gsi_last (*post_p);
19669 : :
19670 : 462388114 : saved_location = input_location;
19671 : 462388114 : if (save_expr != error_mark_node
19672 : 462388114 : && EXPR_HAS_LOCATION (*expr_p))
19673 : 173851925 : input_location = EXPR_LOCATION (*expr_p);
19674 : :
19675 : : /* Loop over the specific gimplifiers until the toplevel node
19676 : : remains the same. */
19677 : 481455350 : do
19678 : : {
19679 : : /* Strip away as many useless type conversions as possible
19680 : : at the toplevel. */
19681 : 481455350 : STRIP_USELESS_TYPE_CONVERSION (*expr_p);
19682 : :
19683 : : /* Remember the expr. */
19684 : 481455350 : save_expr = *expr_p;
19685 : :
19686 : : /* Die, die, die, my darling. */
19687 : 481455350 : if (error_operand_p (save_expr))
19688 : : {
19689 : : ret = GS_ERROR;
19690 : : break;
19691 : : }
19692 : :
19693 : : /* Do any language-specific gimplification. */
19694 : 481451912 : ret = ((enum gimplify_status)
19695 : 481451912 : lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
19696 : 481451912 : if (ret == GS_OK)
19697 : : {
19698 : 29442240 : if (*expr_p == NULL_TREE)
19699 : : break;
19700 : 29442240 : if (*expr_p != save_expr)
19701 : 5142860 : continue;
19702 : : }
19703 : 452009672 : else if (ret != GS_UNHANDLED)
19704 : : break;
19705 : :
19706 : : /* Make sure that all the cases set 'ret' appropriately. */
19707 : 475697312 : ret = GS_UNHANDLED;
19708 : 475697312 : switch (TREE_CODE (*expr_p))
19709 : : {
19710 : : /* First deal with the special cases. */
19711 : :
19712 : 1130501 : case POSTINCREMENT_EXPR:
19713 : 1130501 : case POSTDECREMENT_EXPR:
19714 : 1130501 : case PREINCREMENT_EXPR:
19715 : 1130501 : case PREDECREMENT_EXPR:
19716 : 2261002 : ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
19717 : : fallback != fb_none,
19718 : 1130501 : TREE_TYPE (*expr_p));
19719 : 1130501 : break;
19720 : :
19721 : 642110 : case VIEW_CONVERT_EXPR:
19722 : 642110 : if ((fallback & fb_rvalue)
19723 : 641999 : && is_gimple_reg_type (TREE_TYPE (*expr_p))
19724 : 1108105 : && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
19725 : : {
19726 : 455338 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
19727 : : post_p, is_gimple_val, fb_rvalue);
19728 : 455338 : recalculate_side_effects (*expr_p);
19729 : 455338 : break;
19730 : : }
19731 : : /* Fallthru. */
19732 : :
19733 : 29515091 : case ARRAY_REF:
19734 : 29515091 : case ARRAY_RANGE_REF:
19735 : 29515091 : case REALPART_EXPR:
19736 : 29515091 : case IMAGPART_EXPR:
19737 : 29515091 : case COMPONENT_REF:
19738 : 29515091 : ret = gimplify_compound_lval (expr_p, pre_p, post_p,
19739 : : fallback ? fallback : fb_rvalue);
19740 : 29515091 : break;
19741 : :
19742 : 6140959 : case COND_EXPR:
19743 : 6140959 : ret = gimplify_cond_expr (expr_p, pre_p, fallback);
19744 : :
19745 : : /* C99 code may assign to an array in a structure value of a
19746 : : conditional expression, and this has undefined behavior
19747 : : only on execution, so create a temporary if an lvalue is
19748 : : required. */
19749 : 6140959 : if (fallback == fb_lvalue)
19750 : : {
19751 : 7 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
19752 : 7 : mark_addressable (*expr_p);
19753 : 7 : ret = GS_OK;
19754 : : }
19755 : : break;
19756 : :
19757 : 16742050 : case CALL_EXPR:
19758 : 16742050 : ret = gimplify_call_expr (expr_p, pre_p, fallback);
19759 : :
19760 : : /* C99 code may assign to an array in a structure returned
19761 : : from a function, and this has undefined behavior only on
19762 : : execution, so create a temporary if an lvalue is
19763 : : required. */
19764 : 16742050 : if (fallback == fb_lvalue)
19765 : : {
19766 : 14109 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
19767 : 14109 : mark_addressable (*expr_p);
19768 : 14109 : ret = GS_OK;
19769 : : }
19770 : : break;
19771 : :
19772 : 0 : case TREE_LIST:
19773 : 0 : gcc_unreachable ();
19774 : :
19775 : 0 : case OMP_ARRAY_SECTION:
19776 : 0 : gcc_unreachable ();
19777 : :
19778 : 378795 : case COMPOUND_EXPR:
19779 : 378795 : ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
19780 : 378795 : break;
19781 : :
19782 : 36535 : case COMPOUND_LITERAL_EXPR:
19783 : 36535 : ret = gimplify_compound_literal_expr (expr_p, pre_p,
19784 : : gimple_test_f, fallback);
19785 : 36535 : break;
19786 : :
19787 : 49667969 : case MODIFY_EXPR:
19788 : 49667969 : case INIT_EXPR:
19789 : 49667969 : ret = gimplify_modify_expr (expr_p, pre_p, post_p,
19790 : : fallback != fb_none);
19791 : 49667969 : break;
19792 : :
19793 : 117172 : case TRUTH_ANDIF_EXPR:
19794 : 117172 : case TRUTH_ORIF_EXPR:
19795 : 117172 : {
19796 : : /* Preserve the original type of the expression and the
19797 : : source location of the outer expression. */
19798 : 117172 : tree org_type = TREE_TYPE (*expr_p);
19799 : 117172 : *expr_p = gimple_boolify (*expr_p);
19800 : 117172 : *expr_p = build3_loc (input_location, COND_EXPR,
19801 : : org_type, *expr_p,
19802 : : fold_convert_loc
19803 : : (input_location,
19804 : : org_type, boolean_true_node),
19805 : : fold_convert_loc
19806 : : (input_location,
19807 : : org_type, boolean_false_node));
19808 : 117172 : ret = GS_OK;
19809 : 117172 : break;
19810 : : }
19811 : :
19812 : 274372 : case TRUTH_NOT_EXPR:
19813 : 274372 : {
19814 : 274372 : tree type = TREE_TYPE (*expr_p);
19815 : : /* The parsers are careful to generate TRUTH_NOT_EXPR
19816 : : only with operands that are always zero or one.
19817 : : We do not fold here but handle the only interesting case
19818 : : manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
19819 : 274372 : *expr_p = gimple_boolify (*expr_p);
19820 : 274372 : if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
19821 : 274372 : *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
19822 : 274372 : TREE_TYPE (*expr_p),
19823 : 274372 : TREE_OPERAND (*expr_p, 0));
19824 : : else
19825 : 0 : *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
19826 : 0 : TREE_TYPE (*expr_p),
19827 : 0 : TREE_OPERAND (*expr_p, 0),
19828 : 0 : build_int_cst (TREE_TYPE (*expr_p), 1));
19829 : 274372 : if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
19830 : 6397 : *expr_p = fold_convert_loc (input_location, type, *expr_p);
19831 : : ret = GS_OK;
19832 : : break;
19833 : : }
19834 : :
19835 : 33321000 : case ADDR_EXPR:
19836 : 33321000 : ret = gimplify_addr_expr (expr_p, pre_p, post_p);
19837 : 33321000 : break;
19838 : :
19839 : 5068 : case ANNOTATE_EXPR:
19840 : 5068 : {
19841 : 5068 : tree cond = TREE_OPERAND (*expr_p, 0);
19842 : 5068 : tree kind = TREE_OPERAND (*expr_p, 1);
19843 : 5068 : tree data = TREE_OPERAND (*expr_p, 2);
19844 : 5068 : tree type = TREE_TYPE (cond);
19845 : 5068 : if (!INTEGRAL_TYPE_P (type))
19846 : : {
19847 : 0 : *expr_p = cond;
19848 : 0 : ret = GS_OK;
19849 : 0 : break;
19850 : : }
19851 : 5068 : tree tmp = create_tmp_var (type);
19852 : 5068 : gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
19853 : 5068 : gcall *call
19854 : 5068 : = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
19855 : 5068 : gimple_call_set_lhs (call, tmp);
19856 : 5068 : gimplify_seq_add_stmt (pre_p, call);
19857 : 5068 : *expr_p = tmp;
19858 : 5068 : ret = GS_ALL_DONE;
19859 : 5068 : break;
19860 : : }
19861 : :
19862 : 50777 : case VA_ARG_EXPR:
19863 : 50777 : ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
19864 : 50777 : break;
19865 : :
19866 : 17110761 : CASE_CONVERT:
19867 : 17110761 : if (IS_EMPTY_STMT (*expr_p))
19868 : : {
19869 : : ret = GS_ALL_DONE;
19870 : : break;
19871 : : }
19872 : :
19873 : 15316828 : if (VOID_TYPE_P (TREE_TYPE (*expr_p))
19874 : 15316828 : || fallback == fb_none)
19875 : : {
19876 : : /* Just strip a conversion to void (or in void context) and
19877 : : try again. */
19878 : 2766170 : *expr_p = TREE_OPERAND (*expr_p, 0);
19879 : 2766170 : ret = GS_OK;
19880 : 2766170 : break;
19881 : : }
19882 : :
19883 : 12550658 : ret = gimplify_conversion (expr_p);
19884 : 12550658 : if (ret == GS_ERROR)
19885 : : break;
19886 : 12550658 : if (*expr_p != save_expr)
19887 : : break;
19888 : : /* FALLTHRU */
19889 : :
19890 : 12676092 : case FIX_TRUNC_EXPR:
19891 : : /* unary_expr: ... | '(' cast ')' val | ... */
19892 : 12676092 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19893 : : is_gimple_val, fb_rvalue);
19894 : 12676092 : recalculate_side_effects (*expr_p);
19895 : 12676092 : break;
19896 : :
19897 : 6441008 : case INDIRECT_REF:
19898 : 6441008 : {
19899 : 6441008 : bool volatilep = TREE_THIS_VOLATILE (*expr_p);
19900 : 6441008 : bool notrap = TREE_THIS_NOTRAP (*expr_p);
19901 : 6441008 : tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
19902 : :
19903 : 6441008 : *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
19904 : 6441008 : if (*expr_p != save_expr)
19905 : : {
19906 : : ret = GS_OK;
19907 : : break;
19908 : : }
19909 : :
19910 : 6417513 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19911 : : is_gimple_reg, fb_rvalue);
19912 : 6417513 : if (ret == GS_ERROR)
19913 : : break;
19914 : :
19915 : 6417512 : recalculate_side_effects (*expr_p);
19916 : 12835024 : *expr_p = fold_build2_loc (input_location, MEM_REF,
19917 : 6417512 : TREE_TYPE (*expr_p),
19918 : 6417512 : TREE_OPERAND (*expr_p, 0),
19919 : : build_int_cst (saved_ptr_type, 0));
19920 : 6417512 : TREE_THIS_VOLATILE (*expr_p) = volatilep;
19921 : 6417512 : TREE_THIS_NOTRAP (*expr_p) = notrap;
19922 : 6417512 : ret = GS_OK;
19923 : 6417512 : break;
19924 : : }
19925 : :
19926 : : /* We arrive here through the various re-gimplifcation paths. */
19927 : 16070575 : case MEM_REF:
19928 : : /* First try re-folding the whole thing. */
19929 : 16070575 : tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
19930 : : TREE_OPERAND (*expr_p, 0),
19931 : : TREE_OPERAND (*expr_p, 1));
19932 : 16070575 : if (tmp)
19933 : : {
19934 : 5878 : REF_REVERSE_STORAGE_ORDER (tmp)
19935 : 2939 : = REF_REVERSE_STORAGE_ORDER (*expr_p);
19936 : 2939 : *expr_p = tmp;
19937 : 2939 : recalculate_side_effects (*expr_p);
19938 : 2939 : ret = GS_OK;
19939 : 2939 : break;
19940 : : }
19941 : : /* Avoid re-gimplifying the address operand if it is already
19942 : : in suitable form. Re-gimplifying would mark the address
19943 : : operand addressable. Always gimplify when not in SSA form
19944 : : as we still may have to gimplify decls with value-exprs. */
19945 : 16067636 : if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
19946 : 17080119 : || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
19947 : : {
19948 : 15109913 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
19949 : : is_gimple_mem_ref_addr, fb_rvalue);
19950 : 15109913 : if (ret == GS_ERROR)
19951 : : break;
19952 : : }
19953 : 16067636 : recalculate_side_effects (*expr_p);
19954 : 16067636 : ret = GS_ALL_DONE;
19955 : 16067636 : break;
19956 : :
19957 : : /* Constants need not be gimplified. */
19958 : 42644246 : case INTEGER_CST:
19959 : 42644246 : case REAL_CST:
19960 : 42644246 : case FIXED_CST:
19961 : 42644246 : case STRING_CST:
19962 : 42644246 : case COMPLEX_CST:
19963 : 42644246 : case VECTOR_CST:
19964 : : /* Drop the overflow flag on constants, we do not want
19965 : : that in the GIMPLE IL. */
19966 : 42644246 : if (TREE_OVERFLOW_P (*expr_p))
19967 : 1148 : *expr_p = drop_tree_overflow (*expr_p);
19968 : : ret = GS_ALL_DONE;
19969 : : break;
19970 : :
19971 : 115957 : case CONST_DECL:
19972 : : /* If we require an lvalue, such as for ADDR_EXPR, retain the
19973 : : CONST_DECL node. Otherwise the decl is replaceable by its
19974 : : value. */
19975 : : /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
19976 : 115957 : if (fallback & fb_lvalue)
19977 : : ret = GS_ALL_DONE;
19978 : : else
19979 : : {
19980 : 3511 : *expr_p = DECL_INITIAL (*expr_p);
19981 : 3511 : ret = GS_OK;
19982 : : }
19983 : : break;
19984 : :
19985 : 6449951 : case DECL_EXPR:
19986 : 6449951 : ret = gimplify_decl_expr (expr_p, pre_p);
19987 : 6449951 : break;
19988 : :
19989 : 5961975 : case BIND_EXPR:
19990 : 5961975 : ret = gimplify_bind_expr (expr_p, pre_p);
19991 : 5961975 : break;
19992 : :
19993 : 194002 : case LOOP_EXPR:
19994 : 194002 : ret = gimplify_loop_expr (expr_p, pre_p);
19995 : 194002 : break;
19996 : :
19997 : 51698 : case SWITCH_EXPR:
19998 : 51698 : ret = gimplify_switch_expr (expr_p, pre_p);
19999 : 51698 : break;
20000 : :
20001 : 3099 : case EXIT_EXPR:
20002 : 3099 : ret = gimplify_exit_expr (expr_p);
20003 : 3099 : break;
20004 : :
20005 : 1095928 : case GOTO_EXPR:
20006 : : /* If the target is not LABEL, then it is a computed jump
20007 : : and the target needs to be gimplified. */
20008 : 1095928 : if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
20009 : : {
20010 : 1099 : ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
20011 : : NULL, is_gimple_val, fb_rvalue);
20012 : 1099 : if (ret == GS_ERROR)
20013 : : break;
20014 : : }
20015 : 2191854 : gimplify_seq_add_stmt (pre_p,
20016 : 1095927 : gimple_build_goto (GOTO_DESTINATION (*expr_p)));
20017 : 1095927 : ret = GS_ALL_DONE;
20018 : 1095927 : break;
20019 : :
20020 : 114616 : case PREDICT_EXPR:
20021 : 458464 : gimplify_seq_add_stmt (pre_p,
20022 : 114616 : gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
20023 : 114616 : PREDICT_EXPR_OUTCOME (*expr_p)));
20024 : 114616 : ret = GS_ALL_DONE;
20025 : 114616 : break;
20026 : :
20027 : 2490647 : case LABEL_EXPR:
20028 : 2490647 : ret = gimplify_label_expr (expr_p, pre_p);
20029 : 2490647 : label = LABEL_EXPR_LABEL (*expr_p);
20030 : 2490647 : gcc_assert (decl_function_context (label) == current_function_decl);
20031 : :
20032 : : /* If the label is used in a goto statement, or address of the label
20033 : : is taken, we need to unpoison all variables that were seen so far.
20034 : : Doing so would prevent us from reporting a false positives. */
20035 : 2490647 : if (asan_poisoned_variables
20036 : 2499 : && asan_used_labels != NULL
20037 : 265 : && asan_used_labels->contains (label)
20038 : 2490819 : && !gimplify_omp_ctxp)
20039 : 164 : asan_poison_variables (asan_poisoned_variables, false, pre_p);
20040 : : break;
20041 : :
20042 : 1079077 : case CASE_LABEL_EXPR:
20043 : 1079077 : ret = gimplify_case_label_expr (expr_p, pre_p);
20044 : :
20045 : 1079077 : if (gimplify_ctxp->live_switch_vars)
20046 : 1078890 : asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
20047 : : pre_p);
20048 : : break;
20049 : :
20050 : 2233508 : case RETURN_EXPR:
20051 : 2233508 : ret = gimplify_return_expr (*expr_p, pre_p);
20052 : 2233508 : break;
20053 : :
20054 : 935262 : case CONSTRUCTOR:
20055 : : /* Don't reduce this in place; let gimplify_init_constructor work its
20056 : : magic. Buf if we're just elaborating this for side effects, just
20057 : : gimplify any element that has side-effects. */
20058 : 935262 : if (fallback == fb_none)
20059 : : {
20060 : 359 : unsigned HOST_WIDE_INT ix;
20061 : 359 : tree val;
20062 : 359 : tree temp = NULL_TREE;
20063 : 375 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
20064 : 16 : if (TREE_SIDE_EFFECTS (val))
20065 : 4 : append_to_statement_list (val, &temp);
20066 : :
20067 : 359 : *expr_p = temp;
20068 : 359 : ret = temp ? GS_OK : GS_ALL_DONE;
20069 : : }
20070 : : /* C99 code may assign to an array in a constructed
20071 : : structure or union, and this has undefined behavior only
20072 : : on execution, so create a temporary if an lvalue is
20073 : : required. */
20074 : 934903 : else if (fallback == fb_lvalue)
20075 : : {
20076 : 12 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
20077 : 12 : mark_addressable (*expr_p);
20078 : 12 : ret = GS_OK;
20079 : : }
20080 : : else
20081 : : ret = GS_ALL_DONE;
20082 : : break;
20083 : :
20084 : : /* The following are special cases that are not handled by the
20085 : : original GIMPLE grammar. */
20086 : :
20087 : : /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
20088 : : eliminated. */
20089 : 412708 : case SAVE_EXPR:
20090 : 412708 : ret = gimplify_save_expr (expr_p, pre_p, post_p);
20091 : 412708 : break;
20092 : :
20093 : 443598 : case BIT_FIELD_REF:
20094 : 443598 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20095 : : post_p, is_gimple_lvalue, fb_either);
20096 : 443598 : recalculate_side_effects (*expr_p);
20097 : 443598 : break;
20098 : :
20099 : 1311 : case TARGET_MEM_REF:
20100 : 1311 : {
20101 : 1311 : enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
20102 : :
20103 : 1311 : if (TMR_BASE (*expr_p))
20104 : 1311 : r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
20105 : : post_p, is_gimple_mem_ref_addr, fb_either);
20106 : 1311 : if (TMR_INDEX (*expr_p))
20107 : 1128 : r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
20108 : : post_p, is_gimple_val, fb_rvalue);
20109 : 1311 : if (TMR_INDEX2 (*expr_p))
20110 : 75 : r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
20111 : : post_p, is_gimple_val, fb_rvalue);
20112 : : /* TMR_STEP and TMR_OFFSET are always integer constants. */
20113 : 1311 : ret = MIN (r0, r1);
20114 : : }
20115 : : break;
20116 : :
20117 : 0 : case NON_LVALUE_EXPR:
20118 : : /* This should have been stripped above. */
20119 : 0 : gcc_unreachable ();
20120 : :
20121 : 94982 : case ASM_EXPR:
20122 : 94982 : ret = gimplify_asm_expr (expr_p, pre_p, post_p);
20123 : 94982 : break;
20124 : :
20125 : 523336 : case TRY_FINALLY_EXPR:
20126 : 523336 : case TRY_CATCH_EXPR:
20127 : 523336 : {
20128 : 523336 : gimple_seq eval, cleanup;
20129 : 523336 : gtry *try_;
20130 : :
20131 : : /* Calls to destructors are generated automatically in FINALLY/CATCH
20132 : : block. They should have location as UNKNOWN_LOCATION. However,
20133 : : gimplify_call_expr will reset these call stmts to input_location
20134 : : if it finds stmt's location is unknown. To prevent resetting for
20135 : : destructors, we set the input_location to unknown.
20136 : : Note that this only affects the destructor calls in FINALLY/CATCH
20137 : : block, and will automatically reset to its original value by the
20138 : : end of gimplify_expr. */
20139 : 523336 : input_location = UNKNOWN_LOCATION;
20140 : 523336 : eval = cleanup = NULL;
20141 : 523336 : gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
20142 : 523336 : bool save_in_handler_expr = gimplify_ctxp->in_handler_expr;
20143 : 523336 : if (TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20144 : 523336 : && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == EH_ELSE_EXPR)
20145 : : {
20146 : 86 : gimple_seq n = NULL, e = NULL;
20147 : 86 : gimplify_ctxp->in_handler_expr = true;
20148 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20149 : : 0), &n);
20150 : 86 : gimplify_and_add (TREE_OPERAND (TREE_OPERAND (*expr_p, 1),
20151 : : 1), &e);
20152 : 86 : if (!gimple_seq_empty_p (n) || !gimple_seq_empty_p (e))
20153 : : {
20154 : 85 : geh_else *stmt = gimple_build_eh_else (n, e);
20155 : 85 : gimple_seq_add_stmt (&cleanup, stmt);
20156 : : }
20157 : : }
20158 : : else
20159 : : {
20160 : 523250 : gimplify_ctxp->in_handler_expr = true;
20161 : 523250 : gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
20162 : : }
20163 : 523336 : gimplify_ctxp->in_handler_expr = save_in_handler_expr;
20164 : : /* Don't create bogus GIMPLE_TRY with empty cleanup. */
20165 : 523336 : if (gimple_seq_empty_p (cleanup))
20166 : : {
20167 : 27615 : gimple_seq_add_seq (pre_p, eval);
20168 : 27615 : ret = GS_ALL_DONE;
20169 : 27615 : break;
20170 : : }
20171 : 495721 : try_ = gimple_build_try (eval, cleanup,
20172 : 495721 : TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
20173 : : ? GIMPLE_TRY_FINALLY
20174 : : : GIMPLE_TRY_CATCH);
20175 : 495721 : if (EXPR_HAS_LOCATION (save_expr))
20176 : 916592 : gimple_set_location (try_, EXPR_LOCATION (save_expr));
20177 : 37425 : else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
20178 : 27171 : gimple_set_location (try_, saved_location);
20179 : 495721 : if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
20180 : 181701 : gimple_try_set_catch_is_cleanup (try_,
20181 : 181701 : TRY_CATCH_IS_CLEANUP (*expr_p));
20182 : 495721 : gimplify_seq_add_stmt (pre_p, try_);
20183 : 495721 : ret = GS_ALL_DONE;
20184 : 495721 : break;
20185 : : }
20186 : :
20187 : 5669363 : case CLEANUP_POINT_EXPR:
20188 : 5669363 : ret = gimplify_cleanup_point_expr (expr_p, pre_p);
20189 : 5669363 : break;
20190 : :
20191 : 828254 : case TARGET_EXPR:
20192 : 828254 : ret = gimplify_target_expr (expr_p, pre_p, post_p);
20193 : 828254 : break;
20194 : :
20195 : 38812 : case CATCH_EXPR:
20196 : 38812 : {
20197 : 38812 : gimple *c;
20198 : 38812 : gimple_seq handler = NULL;
20199 : 38812 : gimplify_and_add (CATCH_BODY (*expr_p), &handler);
20200 : 38812 : c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
20201 : 38812 : gimplify_seq_add_stmt (pre_p, c);
20202 : 38812 : ret = GS_ALL_DONE;
20203 : 38812 : break;
20204 : : }
20205 : :
20206 : 4269 : case EH_FILTER_EXPR:
20207 : 4269 : {
20208 : 4269 : gimple *ehf;
20209 : 4269 : gimple_seq failure = NULL;
20210 : :
20211 : 4269 : gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
20212 : 4269 : ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
20213 : 4269 : copy_warning (ehf, *expr_p);
20214 : 4269 : gimplify_seq_add_stmt (pre_p, ehf);
20215 : 4269 : ret = GS_ALL_DONE;
20216 : 4269 : break;
20217 : : }
20218 : :
20219 : 50139 : case OBJ_TYPE_REF:
20220 : 50139 : {
20221 : 50139 : enum gimplify_status r0, r1;
20222 : 50139 : r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
20223 : : post_p, is_gimple_val, fb_rvalue);
20224 : 50139 : r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
20225 : : post_p, is_gimple_val, fb_rvalue);
20226 : 50139 : TREE_SIDE_EFFECTS (*expr_p) = 0;
20227 : 50139 : ret = MIN (r0, r1);
20228 : : }
20229 : : break;
20230 : :
20231 : 40362 : case LABEL_DECL:
20232 : : /* We get here when taking the address of a label. We mark
20233 : : the label as "forced"; meaning it can never be removed and
20234 : : it is a potential target for any computed goto. */
20235 : 40362 : FORCED_LABEL (*expr_p) = 1;
20236 : 40362 : ret = GS_ALL_DONE;
20237 : 40362 : break;
20238 : :
20239 : 8850025 : case STATEMENT_LIST:
20240 : 8850025 : ret = gimplify_statement_list (expr_p, pre_p);
20241 : 8850025 : break;
20242 : :
20243 : 1553 : case WITH_SIZE_EXPR:
20244 : 1553 : {
20245 : 1835 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20246 : : post_p == &internal_post ? NULL : post_p,
20247 : : gimple_test_f, fallback);
20248 : 1553 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20249 : : is_gimple_val, fb_rvalue);
20250 : 1553 : ret = GS_ALL_DONE;
20251 : : }
20252 : 1553 : break;
20253 : :
20254 : 108241262 : case VAR_DECL:
20255 : 108241262 : case PARM_DECL:
20256 : 108241262 : ret = gimplify_var_or_parm_decl (expr_p);
20257 : 108241262 : break;
20258 : :
20259 : 338016 : case RESULT_DECL:
20260 : : /* When within an OMP context, notice uses of variables. */
20261 : 338016 : if (gimplify_omp_ctxp)
20262 : 1358 : omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
20263 : : /* Handlers can refer to the function result; if that has been
20264 : : moved, we need to track it. */
20265 : 338016 : if (gimplify_ctxp->in_handler_expr && gimplify_ctxp->return_temp)
20266 : 48 : *expr_p = gimplify_ctxp->return_temp;
20267 : : ret = GS_ALL_DONE;
20268 : : break;
20269 : :
20270 : 0 : case DEBUG_EXPR_DECL:
20271 : 0 : gcc_unreachable ();
20272 : :
20273 : 2652913 : case DEBUG_BEGIN_STMT:
20274 : 5305826 : gimplify_seq_add_stmt (pre_p,
20275 : : gimple_build_debug_begin_stmt
20276 : 2652913 : (TREE_BLOCK (*expr_p),
20277 : 2652913 : EXPR_LOCATION (*expr_p)));
20278 : 2652913 : ret = GS_ALL_DONE;
20279 : 2652913 : *expr_p = NULL;
20280 : 2652913 : break;
20281 : :
20282 : : case SSA_NAME:
20283 : : /* Allow callbacks into the gimplifier during optimization. */
20284 : : ret = GS_ALL_DONE;
20285 : : break;
20286 : :
20287 : 18213 : case OMP_PARALLEL:
20288 : 18213 : gimplify_omp_parallel (expr_p, pre_p);
20289 : 18213 : ret = GS_ALL_DONE;
20290 : 18213 : break;
20291 : :
20292 : 4014 : case OMP_TASK:
20293 : 4014 : gimplify_omp_task (expr_p, pre_p);
20294 : 4014 : ret = GS_ALL_DONE;
20295 : 4014 : break;
20296 : :
20297 : 10671 : case OMP_SIMD:
20298 : 10671 : {
20299 : : /* Temporarily disable into_ssa, as scan_omp_simd
20300 : : which calls copy_gimple_seq_and_replace_locals can't deal
20301 : : with SSA_NAMEs defined outside of the body properly. */
20302 : 10671 : bool saved_into_ssa = gimplify_ctxp->into_ssa;
20303 : 10671 : gimplify_ctxp->into_ssa = false;
20304 : 10671 : ret = gimplify_omp_for (expr_p, pre_p);
20305 : 10671 : gimplify_ctxp->into_ssa = saved_into_ssa;
20306 : 10671 : break;
20307 : : }
20308 : :
20309 : 47432 : case OMP_FOR:
20310 : 47432 : case OMP_DISTRIBUTE:
20311 : 47432 : case OMP_TASKLOOP:
20312 : 47432 : case OMP_TILE:
20313 : 47432 : case OMP_UNROLL:
20314 : 47432 : case OACC_LOOP:
20315 : 47432 : ret = gimplify_omp_for (expr_p, pre_p);
20316 : 47432 : break;
20317 : :
20318 : 1057 : case OMP_LOOP:
20319 : 1057 : ret = gimplify_omp_loop (expr_p, pre_p);
20320 : 1057 : break;
20321 : :
20322 : 706 : case OACC_CACHE:
20323 : 706 : gimplify_oacc_cache (expr_p, pre_p);
20324 : 706 : ret = GS_ALL_DONE;
20325 : 706 : break;
20326 : :
20327 : 254 : case OACC_DECLARE:
20328 : 254 : gimplify_oacc_declare (expr_p, pre_p);
20329 : 254 : ret = GS_ALL_DONE;
20330 : 254 : break;
20331 : :
20332 : 38886 : case OACC_HOST_DATA:
20333 : 38886 : case OACC_DATA:
20334 : 38886 : case OACC_KERNELS:
20335 : 38886 : case OACC_PARALLEL:
20336 : 38886 : case OACC_SERIAL:
20337 : 38886 : case OMP_SCOPE:
20338 : 38886 : case OMP_SECTIONS:
20339 : 38886 : case OMP_SINGLE:
20340 : 38886 : case OMP_TARGET:
20341 : 38886 : case OMP_TARGET_DATA:
20342 : 38886 : case OMP_TEAMS:
20343 : 38886 : gimplify_omp_workshare (expr_p, pre_p);
20344 : 38886 : ret = GS_ALL_DONE;
20345 : 38886 : break;
20346 : :
20347 : 11869 : case OACC_ENTER_DATA:
20348 : 11869 : case OACC_EXIT_DATA:
20349 : 11869 : case OACC_UPDATE:
20350 : 11869 : case OMP_TARGET_UPDATE:
20351 : 11869 : case OMP_TARGET_ENTER_DATA:
20352 : 11869 : case OMP_TARGET_EXIT_DATA:
20353 : 11869 : gimplify_omp_target_update (expr_p, pre_p);
20354 : 11869 : ret = GS_ALL_DONE;
20355 : 11869 : break;
20356 : :
20357 : 6981 : case OMP_SECTION:
20358 : 6981 : case OMP_STRUCTURED_BLOCK:
20359 : 6981 : case OMP_MASTER:
20360 : 6981 : case OMP_MASKED:
20361 : 6981 : case OMP_ORDERED:
20362 : 6981 : case OMP_CRITICAL:
20363 : 6981 : case OMP_SCAN:
20364 : 6981 : {
20365 : 6981 : gimple_seq body = NULL;
20366 : 6981 : gimple *g;
20367 : 6981 : bool saved_in_omp_construct = in_omp_construct;
20368 : :
20369 : 6981 : in_omp_construct = true;
20370 : 6981 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20371 : 6981 : in_omp_construct = saved_in_omp_construct;
20372 : 6981 : switch (TREE_CODE (*expr_p))
20373 : : {
20374 : 1271 : case OMP_SECTION:
20375 : 1271 : g = gimple_build_omp_section (body);
20376 : 1271 : break;
20377 : 774 : case OMP_STRUCTURED_BLOCK:
20378 : 774 : g = gimple_build_omp_structured_block (body);
20379 : 774 : break;
20380 : 886 : case OMP_MASTER:
20381 : 886 : g = gimple_build_omp_master (body);
20382 : 886 : break;
20383 : 1940 : case OMP_ORDERED:
20384 : 1940 : g = gimplify_omp_ordered (*expr_p, body);
20385 : 1940 : if (OMP_BODY (*expr_p) == NULL_TREE
20386 : 1940 : && gimple_code (g) == GIMPLE_OMP_ORDERED)
20387 : 1015 : gimple_omp_ordered_standalone (g);
20388 : : break;
20389 : 446 : case OMP_MASKED:
20390 : 446 : gimplify_scan_omp_clauses (&OMP_MASKED_CLAUSES (*expr_p),
20391 : : pre_p, ORT_WORKSHARE, OMP_MASKED);
20392 : 446 : gimplify_adjust_omp_clauses (pre_p, body,
20393 : : &OMP_MASKED_CLAUSES (*expr_p),
20394 : : OMP_MASKED);
20395 : 892 : g = gimple_build_omp_masked (body,
20396 : 446 : OMP_MASKED_CLAUSES (*expr_p));
20397 : 446 : break;
20398 : 546 : case OMP_CRITICAL:
20399 : 546 : gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
20400 : : pre_p, ORT_WORKSHARE, OMP_CRITICAL);
20401 : 546 : gimplify_adjust_omp_clauses (pre_p, body,
20402 : : &OMP_CRITICAL_CLAUSES (*expr_p),
20403 : : OMP_CRITICAL);
20404 : 1638 : g = gimple_build_omp_critical (body,
20405 : 546 : OMP_CRITICAL_NAME (*expr_p),
20406 : 546 : OMP_CRITICAL_CLAUSES (*expr_p));
20407 : 546 : break;
20408 : 1118 : case OMP_SCAN:
20409 : 1118 : gimplify_scan_omp_clauses (&OMP_SCAN_CLAUSES (*expr_p),
20410 : : pre_p, ORT_WORKSHARE, OMP_SCAN);
20411 : 1118 : gimplify_adjust_omp_clauses (pre_p, body,
20412 : : &OMP_SCAN_CLAUSES (*expr_p),
20413 : : OMP_SCAN);
20414 : 1118 : g = gimple_build_omp_scan (body, OMP_SCAN_CLAUSES (*expr_p));
20415 : 1118 : break;
20416 : 0 : default:
20417 : 0 : gcc_unreachable ();
20418 : : }
20419 : 6981 : gimplify_seq_add_stmt (pre_p, g);
20420 : 6981 : ret = GS_ALL_DONE;
20421 : 6981 : break;
20422 : : }
20423 : :
20424 : 611 : case OMP_TASKGROUP:
20425 : 611 : {
20426 : 611 : gimple_seq body = NULL;
20427 : :
20428 : 611 : tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
20429 : 611 : bool saved_in_omp_construct = in_omp_construct;
20430 : 611 : gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
20431 : : OMP_TASKGROUP);
20432 : 611 : gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
20433 : :
20434 : 611 : in_omp_construct = true;
20435 : 611 : gimplify_and_add (OMP_BODY (*expr_p), &body);
20436 : 611 : in_omp_construct = saved_in_omp_construct;
20437 : 611 : gimple_seq cleanup = NULL;
20438 : 611 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
20439 : 611 : gimple *g = gimple_build_call (fn, 0);
20440 : 611 : gimple_seq_add_stmt (&cleanup, g);
20441 : 611 : g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
20442 : 611 : body = NULL;
20443 : 611 : gimple_seq_add_stmt (&body, g);
20444 : 611 : g = gimple_build_omp_taskgroup (body, *pclauses);
20445 : 611 : gimplify_seq_add_stmt (pre_p, g);
20446 : 611 : ret = GS_ALL_DONE;
20447 : 611 : break;
20448 : : }
20449 : :
20450 : 612 : case OMP_INTEROP:
20451 : 612 : ret = gimplify_omp_interop (expr_p, pre_p);
20452 : 612 : break;
20453 : 10249 : case OMP_ATOMIC:
20454 : 10249 : case OMP_ATOMIC_READ:
20455 : 10249 : case OMP_ATOMIC_CAPTURE_OLD:
20456 : 10249 : case OMP_ATOMIC_CAPTURE_NEW:
20457 : 10249 : ret = gimplify_omp_atomic (expr_p, pre_p);
20458 : 10249 : break;
20459 : :
20460 : 845 : case OMP_DISPATCH:
20461 : 845 : ret = gimplify_omp_dispatch (expr_p, pre_p);
20462 : 845 : break;
20463 : :
20464 : 162 : case OMP_METADIRECTIVE:
20465 : 162 : ret = gimplify_omp_metadirective (expr_p, pre_p, post_p,
20466 : : gimple_test_f, fallback);
20467 : 162 : break;
20468 : :
20469 : 36 : case OMP_NEXT_VARIANT:
20470 : 36 : case OMP_TARGET_DEVICE_MATCHES:
20471 : : /* These are placeholders for constants. There's nothing to do with
20472 : : them here but we must mark the containing function as needing
20473 : : to run the ompdevlow pass to resolve them. Note that
20474 : : OMP_TARGET_DEVICE_MATCHES, in particular, may be inserted by
20475 : : the front ends. */
20476 : 36 : cgraph_node::get (cfun->decl)->has_omp_variant_constructs = 1;
20477 : 36 : ret = GS_ALL_DONE;
20478 : 36 : break;
20479 : :
20480 : 0 : case OMP_DECLARE_MAPPER:
20481 : 0 : ret = gimplify_omp_declare_mapper (expr_p);
20482 : 0 : break;
20483 : :
20484 : 478 : case TRANSACTION_EXPR:
20485 : 478 : ret = gimplify_transaction (expr_p, pre_p);
20486 : 478 : break;
20487 : :
20488 : 296621 : case TRUTH_AND_EXPR:
20489 : 296621 : case TRUTH_OR_EXPR:
20490 : 296621 : case TRUTH_XOR_EXPR:
20491 : 296621 : {
20492 : 296621 : tree orig_type = TREE_TYPE (*expr_p);
20493 : 296621 : tree new_type, xop0, xop1;
20494 : 296621 : *expr_p = gimple_boolify (*expr_p);
20495 : 296621 : new_type = TREE_TYPE (*expr_p);
20496 : 296621 : if (!useless_type_conversion_p (orig_type, new_type))
20497 : : {
20498 : 1122 : *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
20499 : 1122 : ret = GS_OK;
20500 : 1122 : break;
20501 : : }
20502 : :
20503 : : /* Boolified binary truth expressions are semantically equivalent
20504 : : to bitwise binary expressions. Canonicalize them to the
20505 : : bitwise variant. */
20506 : 295499 : switch (TREE_CODE (*expr_p))
20507 : : {
20508 : 172588 : case TRUTH_AND_EXPR:
20509 : 172588 : TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
20510 : 172588 : break;
20511 : 122322 : case TRUTH_OR_EXPR:
20512 : 122322 : TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
20513 : 122322 : break;
20514 : 589 : case TRUTH_XOR_EXPR:
20515 : 589 : TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
20516 : 589 : break;
20517 : : default:
20518 : : break;
20519 : : }
20520 : : /* Now make sure that operands have compatible type to
20521 : : expression's new_type. */
20522 : 295499 : xop0 = TREE_OPERAND (*expr_p, 0);
20523 : 295499 : xop1 = TREE_OPERAND (*expr_p, 1);
20524 : 295499 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
20525 : 326 : TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
20526 : : new_type,
20527 : : xop0);
20528 : 295499 : if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
20529 : 366 : TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
20530 : : new_type,
20531 : : xop1);
20532 : : /* Continue classified as tcc_binary. */
20533 : 295499 : goto expr_2;
20534 : : }
20535 : :
20536 : 9793 : case VEC_COND_EXPR:
20537 : 9793 : goto expr_3;
20538 : :
20539 : 132598 : case VEC_PERM_EXPR:
20540 : : /* Classified as tcc_expression. */
20541 : 132598 : goto expr_3;
20542 : :
20543 : 48 : case BIT_INSERT_EXPR:
20544 : : /* Argument 3 is a constant. */
20545 : 48 : goto expr_2;
20546 : :
20547 : 3855184 : case POINTER_PLUS_EXPR:
20548 : 3855184 : {
20549 : 3855184 : enum gimplify_status r0, r1;
20550 : 3855184 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20551 : : post_p, is_gimple_val, fb_rvalue);
20552 : 3855184 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20553 : : post_p, is_gimple_val, fb_rvalue);
20554 : 3855184 : recalculate_side_effects (*expr_p);
20555 : 3855184 : ret = MIN (r0, r1);
20556 : : break;
20557 : : }
20558 : :
20559 : 43804525 : default:
20560 : 43804525 : switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
20561 : : {
20562 : 6904654 : case tcc_comparison:
20563 : : /* Handle comparison of objects of non scalar mode aggregates
20564 : : with a call to memcmp. It would be nice to only have to do
20565 : : this for variable-sized objects, but then we'd have to allow
20566 : : the same nest of reference nodes we allow for MODIFY_EXPR and
20567 : : that's too complex.
20568 : :
20569 : : Compare scalar mode aggregates as scalar mode values. Using
20570 : : memcmp for them would be very inefficient at best, and is
20571 : : plain wrong if bitfields are involved. */
20572 : 6904654 : if (error_operand_p (TREE_OPERAND (*expr_p, 1)))
20573 : : ret = GS_ERROR;
20574 : : else
20575 : : {
20576 : 6904654 : tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
20577 : :
20578 : : /* Vector comparisons need no boolification. */
20579 : 6904654 : if (TREE_CODE (type) == VECTOR_TYPE)
20580 : 11694 : goto expr_2;
20581 : 6892960 : else if (!AGGREGATE_TYPE_P (type))
20582 : : {
20583 : 6892940 : tree org_type = TREE_TYPE (*expr_p);
20584 : 6892940 : *expr_p = gimple_boolify (*expr_p);
20585 : 6892940 : if (!useless_type_conversion_p (org_type,
20586 : 6892940 : TREE_TYPE (*expr_p)))
20587 : : {
20588 : 53328 : *expr_p = fold_convert_loc (input_location,
20589 : : org_type, *expr_p);
20590 : 53328 : ret = GS_OK;
20591 : : }
20592 : : else
20593 : 6839612 : goto expr_2;
20594 : : }
20595 : 20 : else if (SCALAR_INT_MODE_P (TYPE_MODE (type)))
20596 : 20 : ret = gimplify_scalar_mode_aggregate_compare (expr_p);
20597 : : else
20598 : 0 : ret = gimplify_variable_sized_compare (expr_p);
20599 : : }
20600 : : break;
20601 : :
20602 : : /* If *EXPR_P does not need to be special-cased, handle it
20603 : : according to its class. */
20604 : 1629180 : case tcc_unary:
20605 : 1629180 : ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20606 : : post_p, is_gimple_val, fb_rvalue);
20607 : 1629180 : break;
20608 : :
20609 : 25895416 : case tcc_binary:
20610 : 25895416 : expr_2:
20611 : 25895416 : {
20612 : 25895416 : enum gimplify_status r0, r1;
20613 : :
20614 : 25895416 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20615 : : post_p, is_gimple_val, fb_rvalue);
20616 : 25895416 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20617 : : post_p, is_gimple_val, fb_rvalue);
20618 : :
20619 : 25895416 : ret = MIN (r0, r1);
20620 : : break;
20621 : : }
20622 : :
20623 : 142391 : expr_3:
20624 : 142391 : {
20625 : 142391 : enum gimplify_status r0, r1, r2;
20626 : :
20627 : 142391 : r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
20628 : : post_p, is_gimple_val, fb_rvalue);
20629 : 142391 : r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
20630 : : post_p, is_gimple_val, fb_rvalue);
20631 : 142391 : r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
20632 : : post_p, is_gimple_val, fb_rvalue);
20633 : :
20634 : 142391 : ret = MIN (MIN (r0, r1), r2);
20635 : : break;
20636 : : }
20637 : :
20638 : 16522128 : case tcc_declaration:
20639 : 16522128 : case tcc_constant:
20640 : 16522128 : ret = GS_ALL_DONE;
20641 : 16522128 : goto dont_recalculate;
20642 : :
20643 : 0 : default:
20644 : 0 : gcc_unreachable ();
20645 : : }
20646 : :
20647 : 27720335 : recalculate_side_effects (*expr_p);
20648 : :
20649 : 475697312 : dont_recalculate:
20650 : : break;
20651 : : }
20652 : :
20653 : 475697312 : gcc_assert (*expr_p || ret != GS_OK);
20654 : : }
20655 : 480840172 : while (ret == GS_OK);
20656 : :
20657 : : /* If we encountered an error_mark somewhere nested inside, either
20658 : : stub out the statement or propagate the error back out. */
20659 : 462388114 : if (ret == GS_ERROR)
20660 : : {
20661 : 4598 : if (is_statement)
20662 : 3647 : *expr_p = NULL;
20663 : 4598 : goto out;
20664 : : }
20665 : :
20666 : : /* This was only valid as a return value from the langhook, which
20667 : : we handled. Make sure it doesn't escape from any other context. */
20668 : 462383516 : gcc_assert (ret != GS_UNHANDLED);
20669 : :
20670 : 462383516 : if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
20671 : : {
20672 : : /* We aren't looking for a value, and we don't have a valid
20673 : : statement. If it doesn't have side-effects, throw it away.
20674 : : We can also get here with code such as "*&&L;", where L is
20675 : : a LABEL_DECL that is marked as FORCED_LABEL. */
20676 : 1047289 : if (TREE_CODE (*expr_p) == LABEL_DECL
20677 : 1047289 : || !TREE_SIDE_EFFECTS (*expr_p))
20678 : 1046268 : *expr_p = NULL;
20679 : 1021 : else if (!TREE_THIS_VOLATILE (*expr_p))
20680 : : {
20681 : : /* This is probably a _REF that contains something nested that
20682 : : has side effects. Recurse through the operands to find it. */
20683 : 0 : enum tree_code code = TREE_CODE (*expr_p);
20684 : :
20685 : 0 : switch (code)
20686 : : {
20687 : 0 : case COMPONENT_REF:
20688 : 0 : case REALPART_EXPR:
20689 : 0 : case IMAGPART_EXPR:
20690 : 0 : case VIEW_CONVERT_EXPR:
20691 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20692 : : gimple_test_f, fallback);
20693 : 0 : break;
20694 : :
20695 : 0 : case ARRAY_REF:
20696 : 0 : case ARRAY_RANGE_REF:
20697 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
20698 : : gimple_test_f, fallback);
20699 : 0 : gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
20700 : : gimple_test_f, fallback);
20701 : 0 : break;
20702 : :
20703 : 0 : default:
20704 : : /* Anything else with side-effects must be converted to
20705 : : a valid statement before we get here. */
20706 : 0 : gcc_unreachable ();
20707 : : }
20708 : :
20709 : 0 : *expr_p = NULL;
20710 : : }
20711 : 1021 : else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
20712 : 971 : && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode
20713 : 1980 : && !is_empty_type (TREE_TYPE (*expr_p)))
20714 : : {
20715 : : /* Historically, the compiler has treated a bare reference
20716 : : to a non-BLKmode volatile lvalue as forcing a load. */
20717 : 893 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
20718 : :
20719 : : /* Normally, we do not want to create a temporary for a
20720 : : TREE_ADDRESSABLE type because such a type should not be
20721 : : copied by bitwise-assignment. However, we make an
20722 : : exception here, as all we are doing here is ensuring that
20723 : : we read the bytes that make up the type. We use
20724 : : create_tmp_var_raw because create_tmp_var will abort when
20725 : : given a TREE_ADDRESSABLE type. */
20726 : 893 : tree tmp = create_tmp_var_raw (type, "vol");
20727 : 893 : gimple_add_tmp_var (tmp);
20728 : 893 : gimplify_assign (tmp, *expr_p, pre_p);
20729 : 893 : *expr_p = NULL;
20730 : : }
20731 : : else
20732 : : /* We can't do anything useful with a volatile reference to
20733 : : an incomplete type, so just throw it away. Likewise for
20734 : : a BLKmode type, since any implicit inner load should
20735 : : already have been turned into an explicit one by the
20736 : : gimplification process. */
20737 : 128 : *expr_p = NULL;
20738 : : }
20739 : :
20740 : : /* If we are gimplifying at the statement level, we're done. Tack
20741 : : everything together and return. */
20742 : 462383516 : if (fallback == fb_none || is_statement)
20743 : : {
20744 : : /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
20745 : : it out for GC to reclaim it. */
20746 : 101178333 : *expr_p = NULL_TREE;
20747 : :
20748 : 101178333 : if (!gimple_seq_empty_p (internal_pre)
20749 : 101178333 : || !gimple_seq_empty_p (internal_post))
20750 : : {
20751 : 19 : gimplify_seq_add_seq (&internal_pre, internal_post);
20752 : 19 : gimplify_seq_add_seq (pre_p, internal_pre);
20753 : : }
20754 : :
20755 : : /* The result of gimplifying *EXPR_P is going to be the last few
20756 : : statements in *PRE_P and *POST_P. Add location information
20757 : : to all the statements that were added by the gimplification
20758 : : helpers. */
20759 : 101178333 : if (!gimple_seq_empty_p (*pre_p))
20760 : 98693126 : annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
20761 : :
20762 : 101178333 : if (!gimple_seq_empty_p (*post_p))
20763 : 19 : annotate_all_with_location_after (*post_p, post_last_gsi,
20764 : : input_location);
20765 : :
20766 : 101178333 : goto out;
20767 : : }
20768 : :
20769 : : #ifdef ENABLE_GIMPLE_CHECKING
20770 : 361205183 : if (*expr_p)
20771 : : {
20772 : 361205183 : enum tree_code code = TREE_CODE (*expr_p);
20773 : : /* These expressions should already be in gimple IR form. */
20774 : 361205183 : gcc_assert (code != MODIFY_EXPR
20775 : : && code != ASM_EXPR
20776 : : && code != BIND_EXPR
20777 : : && code != CATCH_EXPR
20778 : : && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
20779 : : && code != EH_FILTER_EXPR
20780 : : && code != GOTO_EXPR
20781 : : && code != LABEL_EXPR
20782 : : && code != LOOP_EXPR
20783 : : && code != SWITCH_EXPR
20784 : : && code != TRY_FINALLY_EXPR
20785 : : && code != EH_ELSE_EXPR
20786 : : && code != OACC_PARALLEL
20787 : : && code != OACC_KERNELS
20788 : : && code != OACC_SERIAL
20789 : : && code != OACC_DATA
20790 : : && code != OACC_HOST_DATA
20791 : : && code != OACC_DECLARE
20792 : : && code != OACC_UPDATE
20793 : : && code != OACC_ENTER_DATA
20794 : : && code != OACC_EXIT_DATA
20795 : : && code != OACC_CACHE
20796 : : && code != OMP_CRITICAL
20797 : : && code != OMP_FOR
20798 : : && code != OACC_LOOP
20799 : : && code != OMP_MASTER
20800 : : && code != OMP_MASKED
20801 : : && code != OMP_TASKGROUP
20802 : : && code != OMP_ORDERED
20803 : : && code != OMP_PARALLEL
20804 : : && code != OMP_SCAN
20805 : : && code != OMP_SECTIONS
20806 : : && code != OMP_SECTION
20807 : : && code != OMP_STRUCTURED_BLOCK
20808 : : && code != OMP_SINGLE
20809 : : && code != OMP_SCOPE
20810 : : && code != OMP_DISPATCH);
20811 : : }
20812 : : #endif
20813 : :
20814 : : /* Otherwise we're gimplifying a subexpression, so the resulting
20815 : : value is interesting. If it's a valid operand that matches
20816 : : GIMPLE_TEST_F, we're done. Unless we are handling some
20817 : : post-effects internally; if that's the case, we need to copy into
20818 : : a temporary before adding the post-effects to POST_P. */
20819 : 361205183 : if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
20820 : 335450567 : goto out;
20821 : :
20822 : : /* Otherwise, we need to create a new temporary for the gimplified
20823 : : expression. */
20824 : :
20825 : : /* We can't return an lvalue if we have an internal postqueue. The
20826 : : object the lvalue refers to would (probably) be modified by the
20827 : : postqueue; we need to copy the value out first, which means an
20828 : : rvalue. */
20829 : 25754616 : if ((fallback & fb_lvalue)
20830 : 588199 : && gimple_seq_empty_p (internal_post)
20831 : 26342815 : && is_gimple_addressable (*expr_p))
20832 : : {
20833 : : /* An lvalue will do. Take the address of the expression, store it
20834 : : in a temporary, and replace the expression with an INDIRECT_REF of
20835 : : that temporary. */
20836 : 15 : tree ref_alias_type = reference_alias_ptr_type (*expr_p);
20837 : 15 : unsigned int ref_align = get_object_alignment (*expr_p);
20838 : 15 : tree ref_type = TREE_TYPE (*expr_p);
20839 : 15 : tmp = build_fold_addr_expr_loc (input_location, *expr_p);
20840 : 15 : gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
20841 : 15 : if (TYPE_ALIGN (ref_type) != ref_align)
20842 : 1 : ref_type = build_aligned_type (ref_type, ref_align);
20843 : 15 : *expr_p = build2 (MEM_REF, ref_type,
20844 : : tmp, build_zero_cst (ref_alias_type));
20845 : : }
20846 : 25754601 : else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
20847 : : {
20848 : : /* An rvalue will do. Assign the gimplified expression into a
20849 : : new temporary TMP and replace the original expression with
20850 : : TMP. First, make sure that the expression has a type so that
20851 : : it can be assigned into a temporary. */
20852 : 25754593 : gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
20853 : 25754593 : *expr_p = get_formal_tmp_var (*expr_p, pre_p);
20854 : : }
20855 : : else
20856 : : {
20857 : : #ifdef ENABLE_GIMPLE_CHECKING
20858 : 8 : if (!(fallback & fb_mayfail))
20859 : : {
20860 : 0 : fprintf (stderr, "gimplification failed:\n");
20861 : 0 : print_generic_expr (stderr, *expr_p);
20862 : 0 : debug_tree (*expr_p);
20863 : 0 : internal_error ("gimplification failed");
20864 : : }
20865 : : #endif
20866 : 8 : gcc_assert (fallback & fb_mayfail);
20867 : :
20868 : : /* If this is an asm statement, and the user asked for the
20869 : : impossible, don't die. Fail and let gimplify_asm_expr
20870 : : issue an error. */
20871 : 8 : ret = GS_ERROR;
20872 : 8 : goto out;
20873 : : }
20874 : :
20875 : : /* Make sure the temporary matches our predicate. */
20876 : 25754608 : gcc_assert ((*gimple_test_f) (*expr_p));
20877 : :
20878 : 25754608 : if (!gimple_seq_empty_p (internal_post))
20879 : : {
20880 : 0 : annotate_all_with_location (internal_post, input_location);
20881 : 0 : gimplify_seq_add_seq (pre_p, internal_post);
20882 : : }
20883 : :
20884 : 25754608 : out:
20885 : 462388114 : input_location = saved_location;
20886 : 462388114 : return ret;
20887 : : }
20888 : :
20889 : : /* Like gimplify_expr but make sure the gimplified result is not itself
20890 : : a SSA name (but a decl if it were). Temporaries required by
20891 : : evaluating *EXPR_P may be still SSA names. */
20892 : :
20893 : : static enum gimplify_status
20894 : 33493708 : gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
20895 : : bool (*gimple_test_f) (tree), fallback_t fallback,
20896 : : bool allow_ssa)
20897 : : {
20898 : 33493708 : enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
20899 : : gimple_test_f, fallback);
20900 : 33493708 : if (! allow_ssa
20901 : 264605 : && TREE_CODE (*expr_p) == SSA_NAME)
20902 : 67453 : *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
20903 : 33493708 : return ret;
20904 : : }
20905 : :
20906 : : /* Look through TYPE for variable-sized objects and gimplify each such
20907 : : size that we find. Add to LIST_P any statements generated. */
20908 : :
20909 : : void
20910 : 8128700 : gimplify_type_sizes (tree type, gimple_seq *list_p)
20911 : : {
20912 : 8128700 : if (type == NULL || type == error_mark_node)
20913 : : return;
20914 : :
20915 : 8128454 : const bool ignored_p
20916 : 8128454 : = TYPE_NAME (type)
20917 : 4010363 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20918 : 11926189 : && DECL_IGNORED_P (TYPE_NAME (type));
20919 : 8128454 : tree t;
20920 : :
20921 : : /* We first do the main variant, then copy into any other variants. */
20922 : 8128454 : type = TYPE_MAIN_VARIANT (type);
20923 : :
20924 : : /* Avoid infinite recursion. */
20925 : 8128454 : if (TYPE_SIZES_GIMPLIFIED (type))
20926 : : return;
20927 : :
20928 : 2416578 : TYPE_SIZES_GIMPLIFIED (type) = 1;
20929 : :
20930 : 2416578 : switch (TREE_CODE (type))
20931 : : {
20932 : 477767 : case INTEGER_TYPE:
20933 : 477767 : case ENUMERAL_TYPE:
20934 : 477767 : case BOOLEAN_TYPE:
20935 : 477767 : case REAL_TYPE:
20936 : 477767 : case FIXED_POINT_TYPE:
20937 : 477767 : gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
20938 : 477767 : gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
20939 : :
20940 : 8247408 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
20941 : : {
20942 : 7769641 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
20943 : 7769641 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
20944 : : }
20945 : : break;
20946 : :
20947 : 178381 : case ARRAY_TYPE:
20948 : : /* These types may not have declarations, so handle them here. */
20949 : 178381 : gimplify_type_sizes (TREE_TYPE (type), list_p);
20950 : 178381 : gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
20951 : : /* Ensure VLA bounds aren't removed, for -O0 they should be variables
20952 : : with assigned stack slots, for -O1+ -g they should be tracked
20953 : : by VTA. */
20954 : 178381 : if (!ignored_p
20955 : 178381 : && TYPE_DOMAIN (type)
20956 : 356537 : && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
20957 : : {
20958 : 178156 : t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
20959 : 178156 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
20960 : 0 : DECL_IGNORED_P (t) = 0;
20961 : 178156 : t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20962 : 178156 : if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
20963 : 18595 : DECL_IGNORED_P (t) = 0;
20964 : : }
20965 : : break;
20966 : :
20967 : 486416 : case RECORD_TYPE:
20968 : 486416 : case UNION_TYPE:
20969 : 486416 : case QUAL_UNION_TYPE:
20970 : 10484635 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
20971 : 9998219 : if (TREE_CODE (field) == FIELD_DECL)
20972 : : {
20973 : 1066208 : gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
20974 : : /* Likewise, ensure variable offsets aren't removed. */
20975 : 1066208 : if (!ignored_p
20976 : 1066208 : && (t = DECL_FIELD_OFFSET (field))
20977 : 1066196 : && VAR_P (t)
20978 : 1066368 : && DECL_ARTIFICIAL (t))
20979 : 160 : DECL_IGNORED_P (t) = 0;
20980 : 1066208 : gimplify_one_sizepos (&DECL_SIZE (field), list_p);
20981 : 1066208 : gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
20982 : 1066208 : gimplify_type_sizes (TREE_TYPE (field), list_p);
20983 : : }
20984 : : break;
20985 : :
20986 : : case POINTER_TYPE:
20987 : : case REFERENCE_TYPE:
20988 : : /* We used to recurse on the pointed-to type here, which turned out to
20989 : : be incorrect because its definition might refer to variables not
20990 : : yet initialized at this point if a forward declaration is involved.
20991 : :
20992 : : It was actually useful for anonymous pointed-to types to ensure
20993 : : that the sizes evaluation dominates every possible later use of the
20994 : : values. Restricting to such types here would be safe since there
20995 : : is no possible forward declaration around, but would introduce an
20996 : : undesirable middle-end semantic to anonymity. We then defer to
20997 : : front-ends the responsibility of ensuring that the sizes are
20998 : : evaluated both early and late enough, e.g. by attaching artificial
20999 : : type declarations to the tree. */
21000 : : break;
21001 : :
21002 : : default:
21003 : : break;
21004 : : }
21005 : :
21006 : 2416578 : gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
21007 : 2416578 : gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
21008 : :
21009 : 14153127 : for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
21010 : : {
21011 : 11736549 : TYPE_SIZE (t) = TYPE_SIZE (type);
21012 : 11736549 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
21013 : 11736549 : TYPE_SIZES_GIMPLIFIED (t) = 1;
21014 : : }
21015 : : }
21016 : :
21017 : : /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
21018 : : a size or position, has had all of its SAVE_EXPRs evaluated.
21019 : : We add any required statements to *STMT_P. */
21020 : :
21021 : : void
21022 : 9005168 : gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
21023 : : {
21024 : 9005168 : tree expr = *expr_p;
21025 : :
21026 : : /* We don't do anything if the value isn't there, is constant, or contains
21027 : : A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
21028 : : a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
21029 : : will want to replace it with a new variable, but that will cause problems
21030 : : if this type is from outside the function. It's OK to have that here. */
21031 : 9005168 : if (expr == NULL_TREE
21032 : 9008938 : || is_gimple_constant (expr)
21033 : 79300 : || VAR_P (expr)
21034 : 9080698 : || CONTAINS_PLACEHOLDER_P (expr))
21035 : 8929638 : return;
21036 : :
21037 : 75530 : *expr_p = unshare_expr (expr);
21038 : :
21039 : : /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
21040 : : if the def vanishes. */
21041 : 75530 : gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
21042 : :
21043 : : /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
21044 : : FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
21045 : : as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
21046 : 75530 : if (is_gimple_constant (*expr_p))
21047 : 493 : *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
21048 : : }
21049 : :
21050 : : /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
21051 : : containing the sequence of corresponding GIMPLE statements. If DO_PARMS
21052 : : is true, also gimplify the parameters. */
21053 : :
21054 : : gbind *
21055 : 2897241 : gimplify_body (tree fndecl, bool do_parms)
21056 : : {
21057 : 2897241 : location_t saved_location = input_location;
21058 : 2897241 : gimple_seq parm_stmts, parm_cleanup = NULL, seq;
21059 : 2897241 : gimple *outer_stmt;
21060 : 2897241 : gbind *outer_bind;
21061 : :
21062 : 2897241 : timevar_push (TV_TREE_GIMPLIFY);
21063 : :
21064 : 2897241 : init_tree_ssa (cfun);
21065 : :
21066 : : /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
21067 : : gimplification. */
21068 : 2897241 : default_rtl_profile ();
21069 : :
21070 : 2897241 : gcc_assert (gimplify_ctxp == NULL);
21071 : 2897241 : push_gimplify_context (true);
21072 : :
21073 : 2897241 : if (flag_openacc || flag_openmp)
21074 : : {
21075 : 53567 : gcc_assert (gimplify_omp_ctxp == NULL);
21076 : 53567 : if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
21077 : 9829 : gimplify_omp_ctxp = new_omp_context (ORT_IMPLICIT_TARGET);
21078 : : }
21079 : :
21080 : : /* Unshare most shared trees in the body and in that of any nested functions.
21081 : : It would seem we don't have to do this for nested functions because
21082 : : they are supposed to be output and then the outer function gimplified
21083 : : first, but the g++ front end doesn't always do it that way. */
21084 : 2897241 : unshare_body (fndecl);
21085 : 2897241 : unvisit_body (fndecl);
21086 : :
21087 : : /* Make sure input_location isn't set to something weird. */
21088 : 2897241 : input_location = DECL_SOURCE_LOCATION (fndecl);
21089 : :
21090 : : /* Resolve callee-copies. This has to be done before processing
21091 : : the body so that DECL_VALUE_EXPR gets processed correctly. */
21092 : 2897241 : parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
21093 : :
21094 : : /* Gimplify the function's body. */
21095 : 2897241 : seq = NULL;
21096 : 2897241 : gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
21097 : 2897241 : outer_stmt = gimple_seq_first_nondebug_stmt (seq);
21098 : 2897241 : if (!outer_stmt)
21099 : : {
21100 : 24951 : outer_stmt = gimple_build_nop ();
21101 : 24951 : gimplify_seq_add_stmt (&seq, outer_stmt);
21102 : : }
21103 : :
21104 : : /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
21105 : : not the case, wrap everything in a GIMPLE_BIND to make it so. */
21106 : 2897241 : if (gimple_code (outer_stmt) == GIMPLE_BIND
21107 : 2897241 : && (gimple_seq_first_nondebug_stmt (seq)
21108 : 1543578 : == gimple_seq_last_nondebug_stmt (seq)))
21109 : : {
21110 : 1480149 : outer_bind = as_a <gbind *> (outer_stmt);
21111 : 1480149 : if (gimple_seq_first_stmt (seq) != outer_stmt
21112 : 1480149 : || gimple_seq_last_stmt (seq) != outer_stmt)
21113 : : {
21114 : : /* If there are debug stmts before or after outer_stmt, move them
21115 : : inside of outer_bind body. */
21116 : 1 : gimple_stmt_iterator gsi = gsi_for_stmt (outer_stmt, &seq);
21117 : 1 : gimple_seq second_seq = NULL;
21118 : 1 : if (gimple_seq_first_stmt (seq) != outer_stmt
21119 : 2 : && gimple_seq_last_stmt (seq) != outer_stmt)
21120 : : {
21121 : 0 : second_seq = gsi_split_seq_after (gsi);
21122 : 0 : gsi_remove (&gsi, false);
21123 : : }
21124 : 1 : else if (gimple_seq_first_stmt (seq) != outer_stmt)
21125 : 1 : gsi_remove (&gsi, false);
21126 : : else
21127 : : {
21128 : 0 : gsi_remove (&gsi, false);
21129 : 0 : second_seq = seq;
21130 : 0 : seq = NULL;
21131 : : }
21132 : 1 : gimple_seq_add_seq_without_update (&seq,
21133 : : gimple_bind_body (outer_bind));
21134 : 1 : gimple_seq_add_seq_without_update (&seq, second_seq);
21135 : 1 : gimple_bind_set_body (outer_bind, seq);
21136 : : }
21137 : : }
21138 : : else
21139 : 1417092 : outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
21140 : :
21141 : 2897241 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21142 : :
21143 : : /* If we had callee-copies statements, insert them at the beginning
21144 : : of the function and clear DECL_HAS_VALUE_EXPR_P on the parameters. */
21145 : 2897241 : if (!gimple_seq_empty_p (parm_stmts))
21146 : : {
21147 : 41 : tree parm;
21148 : :
21149 : 41 : gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
21150 : 41 : if (parm_cleanup)
21151 : : {
21152 : 0 : gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
21153 : : GIMPLE_TRY_FINALLY);
21154 : 0 : parm_stmts = NULL;
21155 : 0 : gimple_seq_add_stmt (&parm_stmts, g);
21156 : : }
21157 : 41 : gimple_bind_set_body (outer_bind, parm_stmts);
21158 : :
21159 : 41 : for (parm = DECL_ARGUMENTS (current_function_decl);
21160 : 101 : parm; parm = DECL_CHAIN (parm))
21161 : 60 : if (DECL_HAS_VALUE_EXPR_P (parm))
21162 : : {
21163 : 0 : DECL_HAS_VALUE_EXPR_P (parm) = 0;
21164 : 0 : DECL_IGNORED_P (parm) = 0;
21165 : : }
21166 : : }
21167 : :
21168 : 2897241 : if ((flag_openacc || flag_openmp || flag_openmp_simd)
21169 : 55801 : && gimplify_omp_ctxp)
21170 : : {
21171 : 9882 : delete_omp_context (gimplify_omp_ctxp);
21172 : 9882 : gimplify_omp_ctxp = NULL;
21173 : : }
21174 : :
21175 : 2897241 : pop_gimplify_context (outer_bind);
21176 : 2897241 : gcc_assert (gimplify_ctxp == NULL);
21177 : :
21178 : 2897241 : if (flag_checking && !seen_error ())
21179 : 2849125 : verify_gimple_in_seq (gimple_bind_body (outer_bind));
21180 : :
21181 : 2897241 : timevar_pop (TV_TREE_GIMPLIFY);
21182 : 2897241 : input_location = saved_location;
21183 : :
21184 : 2897241 : return outer_bind;
21185 : : }
21186 : :
21187 : : typedef char *char_p; /* For DEF_VEC_P. */
21188 : :
21189 : : /* Return whether we should exclude FNDECL from instrumentation. */
21190 : :
21191 : : static bool
21192 : 39 : flag_instrument_functions_exclude_p (tree fndecl)
21193 : : {
21194 : 39 : vec<char_p> *v;
21195 : :
21196 : 39 : v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
21197 : 40 : if (v && v->length () > 0)
21198 : : {
21199 : 1 : const char *name;
21200 : 1 : int i;
21201 : 1 : char *s;
21202 : :
21203 : 1 : name = lang_hooks.decl_printable_name (fndecl, 1);
21204 : 2 : FOR_EACH_VEC_ELT (*v, i, s)
21205 : 1 : if (strstr (name, s) != NULL)
21206 : 2 : return true;
21207 : : }
21208 : :
21209 : 38 : v = (vec<char_p> *) flag_instrument_functions_exclude_files;
21210 : 39 : if (v && v->length () > 0)
21211 : : {
21212 : 1 : const char *name;
21213 : 1 : int i;
21214 : 1 : char *s;
21215 : :
21216 : 1 : name = DECL_SOURCE_FILE (fndecl);
21217 : 1 : FOR_EACH_VEC_ELT (*v, i, s)
21218 : 1 : if (strstr (name, s) != NULL)
21219 : 2 : return true;
21220 : : }
21221 : :
21222 : : return false;
21223 : : }
21224 : :
21225 : : /* Build a call to the instrumentation function FNCODE and add it to SEQ.
21226 : : If COND_VAR is not NULL, it is a boolean variable guarding the call to
21227 : : the instrumentation function. IF STMT is not NULL, it is a statement
21228 : : to be executed just before the call to the instrumentation function. */
21229 : :
21230 : : static void
21231 : 74 : build_instrumentation_call (gimple_seq *seq, enum built_in_function fncode,
21232 : : tree cond_var, gimple *stmt)
21233 : : {
21234 : : /* The instrumentation hooks aren't going to call the instrumented
21235 : : function and the address they receive is expected to be matchable
21236 : : against symbol addresses. Make sure we don't create a trampoline,
21237 : : in case the current function is nested. */
21238 : 74 : tree this_fn_addr = build_fold_addr_expr (current_function_decl);
21239 : 74 : TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
21240 : :
21241 : 74 : tree label_true, label_false;
21242 : 74 : if (cond_var)
21243 : : {
21244 : 16 : label_true = create_artificial_label (UNKNOWN_LOCATION);
21245 : 16 : label_false = create_artificial_label (UNKNOWN_LOCATION);
21246 : 16 : gcond *cond = gimple_build_cond (EQ_EXPR, cond_var, boolean_false_node,
21247 : : label_true, label_false);
21248 : 16 : gimplify_seq_add_stmt (seq, cond);
21249 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_true));
21250 : 16 : gimplify_seq_add_stmt (seq, gimple_build_predict (PRED_COLD_LABEL,
21251 : : NOT_TAKEN));
21252 : : }
21253 : :
21254 : 74 : if (stmt)
21255 : 8 : gimplify_seq_add_stmt (seq, stmt);
21256 : :
21257 : 74 : tree x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
21258 : 74 : gcall *call = gimple_build_call (x, 1, integer_zero_node);
21259 : 74 : tree tmp_var = create_tmp_var (ptr_type_node, "return_addr");
21260 : 74 : gimple_call_set_lhs (call, tmp_var);
21261 : 74 : gimplify_seq_add_stmt (seq, call);
21262 : 74 : x = builtin_decl_implicit (fncode);
21263 : 74 : call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
21264 : 74 : gimplify_seq_add_stmt (seq, call);
21265 : :
21266 : 74 : if (cond_var)
21267 : 16 : gimplify_seq_add_stmt (seq, gimple_build_label (label_false));
21268 : 74 : }
21269 : :
21270 : : /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
21271 : : node for the function we want to gimplify.
21272 : :
21273 : : Return the sequence of GIMPLE statements corresponding to the body
21274 : : of FNDECL. */
21275 : :
21276 : : void
21277 : 2896723 : gimplify_function_tree (tree fndecl)
21278 : : {
21279 : 2896723 : gimple_seq seq;
21280 : 2896723 : gbind *bind;
21281 : :
21282 : 2896723 : gcc_assert (!gimple_body (fndecl));
21283 : :
21284 : 2896723 : if (DECL_STRUCT_FUNCTION (fndecl))
21285 : 2893242 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
21286 : : else
21287 : 3481 : push_struct_function (fndecl);
21288 : :
21289 : 2896723 : reset_cond_uid ();
21290 : 2896723 : if (cond_uids)
21291 : : {
21292 : 65 : delete cond_uids;
21293 : 65 : cond_uids = NULL;
21294 : : }
21295 : :
21296 : : /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
21297 : : if necessary. */
21298 : 2896723 : cfun->curr_properties |= PROP_gimple_lva;
21299 : :
21300 : 2896723 : if (asan_sanitize_use_after_scope ())
21301 : 7893 : asan_poisoned_variables = new hash_set<tree> ();
21302 : 2896723 : if (flag_openmp)
21303 : 45930 : omp_resolved_variant_calls = new hash_set<tree> ();
21304 : :
21305 : 2896723 : bind = gimplify_body (fndecl, true);
21306 : :
21307 : 2896723 : if (omp_resolved_variant_calls)
21308 : : {
21309 : 45930 : delete omp_resolved_variant_calls;
21310 : 45930 : omp_resolved_variant_calls = NULL;
21311 : : }
21312 : 2896723 : if (asan_poisoned_variables)
21313 : : {
21314 : 7893 : delete asan_poisoned_variables;
21315 : 7893 : asan_poisoned_variables = NULL;
21316 : : }
21317 : :
21318 : : /* The tree body of the function is no longer needed, replace it
21319 : : with the new GIMPLE body. */
21320 : 2896723 : seq = NULL;
21321 : 2896723 : gimple_seq_add_stmt (&seq, bind);
21322 : 2896723 : gimple_set_body (fndecl, seq);
21323 : :
21324 : : /* If we're instrumenting function entry/exit, then prepend the call to
21325 : : the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
21326 : : catch the exit hook. */
21327 : : /* ??? Add some way to ignore exceptions for this TFE. */
21328 : 2896723 : if (flag_instrument_function_entry_exit
21329 : 91 : && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
21330 : : /* Do not instrument extern inline functions. */
21331 : 40 : && !(DECL_DECLARED_INLINE_P (fndecl)
21332 : 5 : && DECL_EXTERNAL (fndecl)
21333 : 1 : && DECL_DISREGARD_INLINE_LIMITS (fndecl))
21334 : 2896762 : && !flag_instrument_functions_exclude_p (fndecl))
21335 : : {
21336 : 37 : gimple_seq body = NULL, cleanup = NULL;
21337 : 37 : gassign *assign;
21338 : 37 : tree cond_var;
21339 : :
21340 : : /* If -finstrument-functions-once is specified, generate:
21341 : :
21342 : : static volatile bool C.0 = false;
21343 : : bool tmp_called;
21344 : :
21345 : : tmp_called = C.0;
21346 : : if (!tmp_called)
21347 : : {
21348 : : C.0 = true;
21349 : : [call profiling enter function]
21350 : : }
21351 : :
21352 : : without specific protection for data races. */
21353 : 37 : if (flag_instrument_function_entry_exit > 1)
21354 : : {
21355 : 8 : tree first_var
21356 : 8 : = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
21357 : : VAR_DECL,
21358 : : create_tmp_var_name ("C"),
21359 : : boolean_type_node);
21360 : 8 : DECL_ARTIFICIAL (first_var) = 1;
21361 : 8 : DECL_IGNORED_P (first_var) = 1;
21362 : 8 : TREE_STATIC (first_var) = 1;
21363 : 8 : TREE_THIS_VOLATILE (first_var) = 1;
21364 : 8 : TREE_USED (first_var) = 1;
21365 : 8 : DECL_INITIAL (first_var) = boolean_false_node;
21366 : 8 : varpool_node::add (first_var);
21367 : :
21368 : 8 : cond_var = create_tmp_var (boolean_type_node, "tmp_called");
21369 : 8 : assign = gimple_build_assign (cond_var, first_var);
21370 : 8 : gimplify_seq_add_stmt (&body, assign);
21371 : :
21372 : 8 : assign = gimple_build_assign (first_var, boolean_true_node);
21373 : : }
21374 : :
21375 : : else
21376 : : {
21377 : : cond_var = NULL_TREE;
21378 : : assign = NULL;
21379 : : }
21380 : :
21381 : 37 : build_instrumentation_call (&body, BUILT_IN_PROFILE_FUNC_ENTER,
21382 : : cond_var, assign);
21383 : :
21384 : : /* If -finstrument-functions-once is specified, generate:
21385 : :
21386 : : if (!tmp_called)
21387 : : [call profiling exit function]
21388 : :
21389 : : without specific protection for data races. */
21390 : 37 : build_instrumentation_call (&cleanup, BUILT_IN_PROFILE_FUNC_EXIT,
21391 : : cond_var, NULL);
21392 : :
21393 : 37 : gimple *tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
21394 : 37 : gimplify_seq_add_stmt (&body, tf);
21395 : 37 : gbind *new_bind = gimple_build_bind (NULL, body, NULL);
21396 : :
21397 : : /* Replace the current function body with the body
21398 : : wrapped in the try/finally TF. */
21399 : 37 : seq = NULL;
21400 : 37 : gimple_seq_add_stmt (&seq, new_bind);
21401 : 37 : gimple_set_body (fndecl, seq);
21402 : 37 : bind = new_bind;
21403 : : }
21404 : :
21405 : 2896723 : if (sanitize_flags_p (SANITIZE_THREAD)
21406 : 2896723 : && param_tsan_instrument_func_entry_exit)
21407 : : {
21408 : 1069 : gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
21409 : 1069 : gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
21410 : 1069 : gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
21411 : : /* Replace the current function body with the body
21412 : : wrapped in the try/finally TF. */
21413 : 1069 : seq = NULL;
21414 : 1069 : gimple_seq_add_stmt (&seq, new_bind);
21415 : 1069 : gimple_set_body (fndecl, seq);
21416 : : }
21417 : :
21418 : 2896723 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
21419 : 2896723 : cfun->curr_properties |= PROP_gimple_any;
21420 : :
21421 : 2896723 : pop_cfun ();
21422 : :
21423 : 2896723 : dump_function (TDI_gimple, fndecl);
21424 : 2896723 : }
21425 : :
21426 : : /* Return a dummy expression of type TYPE in order to keep going after an
21427 : : error. */
21428 : :
21429 : : static tree
21430 : 30 : dummy_object (tree type)
21431 : : {
21432 : 30 : tree t = build_int_cst (build_pointer_type (type), 0);
21433 : 30 : return build2 (MEM_REF, type, t, t);
21434 : : }
21435 : :
21436 : : /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
21437 : : builtin function, but a very special sort of operator. */
21438 : :
21439 : : enum gimplify_status
21440 : 50777 : gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
21441 : : gimple_seq *post_p ATTRIBUTE_UNUSED)
21442 : : {
21443 : 50777 : tree promoted_type, have_va_type;
21444 : 50777 : tree valist = TREE_OPERAND (*expr_p, 0);
21445 : 50777 : tree type = TREE_TYPE (*expr_p);
21446 : 50777 : tree t, tag, aptag;
21447 : 50777 : location_t loc = EXPR_LOCATION (*expr_p);
21448 : :
21449 : : /* Verify that valist is of the proper type. */
21450 : 50777 : have_va_type = TREE_TYPE (valist);
21451 : 50777 : if (have_va_type == error_mark_node)
21452 : : return GS_ERROR;
21453 : 50758 : have_va_type = targetm.canonical_va_list_type (have_va_type);
21454 : 50758 : if (have_va_type == NULL_TREE
21455 : 50758 : && POINTER_TYPE_P (TREE_TYPE (valist)))
21456 : : /* Handle 'Case 1: Not an array type' from c-common.cc/build_va_arg. */
21457 : 258 : have_va_type
21458 : 258 : = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
21459 : 50758 : gcc_assert (have_va_type != NULL_TREE);
21460 : :
21461 : : /* Generate a diagnostic for requesting data of a type that cannot
21462 : : be passed through `...' due to type promotion at the call site. */
21463 : 50758 : if ((promoted_type = lang_hooks.types.type_promotes_to (type))
21464 : : != type)
21465 : : {
21466 : 30 : static bool gave_help;
21467 : 30 : bool warned;
21468 : : /* Use the expansion point to handle cases such as passing bool (defined
21469 : : in a system header) through `...'. */
21470 : 30 : location_t xloc
21471 : 30 : = expansion_point_location_if_in_system_header (loc);
21472 : :
21473 : : /* Unfortunately, this is merely undefined, rather than a constraint
21474 : : violation, so we cannot make this an error. If this call is never
21475 : : executed, the program is still strictly conforming. */
21476 : 30 : auto_diagnostic_group d;
21477 : 30 : warned = warning_at (xloc, 0,
21478 : : "%qT is promoted to %qT when passed through %<...%>",
21479 : : type, promoted_type);
21480 : 30 : if (!gave_help && warned)
21481 : : {
21482 : 15 : gave_help = true;
21483 : 15 : inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
21484 : : promoted_type, type);
21485 : : }
21486 : :
21487 : : /* We can, however, treat "undefined" any way we please.
21488 : : Call abort to encourage the user to fix the program. */
21489 : 23 : if (warned)
21490 : 23 : inform (xloc, "if this code is reached, the program will abort");
21491 : : /* Before the abort, allow the evaluation of the va_list
21492 : : expression to exit or longjmp. */
21493 : 30 : gimplify_and_add (valist, pre_p);
21494 : 60 : t = build_call_expr_loc (loc,
21495 : : builtin_decl_implicit (BUILT_IN_TRAP), 0);
21496 : 30 : gimplify_and_add (t, pre_p);
21497 : :
21498 : : /* This is dead code, but go ahead and finish so that the
21499 : : mode of the result comes out right. */
21500 : 30 : *expr_p = dummy_object (type);
21501 : 30 : return GS_ALL_DONE;
21502 : 30 : }
21503 : :
21504 : 50728 : tag = build_int_cst (build_pointer_type (type), 0);
21505 : 50728 : aptag = build_int_cst (TREE_TYPE (valist), 0);
21506 : :
21507 : 50728 : *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
21508 : : valist, tag, aptag);
21509 : :
21510 : : /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
21511 : : needs to be expanded. */
21512 : 50728 : cfun->curr_properties &= ~PROP_gimple_lva;
21513 : :
21514 : 50728 : return GS_OK;
21515 : : }
21516 : :
21517 : : /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
21518 : :
21519 : : DST/SRC are the destination and source respectively. You can pass
21520 : : ungimplified trees in DST or SRC, in which case they will be
21521 : : converted to a gimple operand if necessary.
21522 : :
21523 : : This function returns the newly created GIMPLE_ASSIGN tuple. */
21524 : :
21525 : : gimple *
21526 : 736089 : gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
21527 : : {
21528 : 736089 : tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
21529 : 736089 : gimplify_and_add (t, seq_p);
21530 : 736089 : ggc_free (t);
21531 : 736089 : return gimple_seq_last_stmt (*seq_p);
21532 : : }
21533 : :
21534 : : inline hashval_t
21535 : 1594602 : gimplify_hasher::hash (const elt_t *p)
21536 : : {
21537 : 1594602 : tree t = p->val;
21538 : 1594602 : return iterative_hash_expr (t, 0);
21539 : : }
21540 : :
21541 : : inline bool
21542 : 618660 : gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
21543 : : {
21544 : 618660 : tree t1 = p1->val;
21545 : 618660 : tree t2 = p2->val;
21546 : 618660 : enum tree_code code = TREE_CODE (t1);
21547 : :
21548 : 618660 : if (TREE_CODE (t2) != code
21549 : 618660 : || TREE_TYPE (t1) != TREE_TYPE (t2))
21550 : : return false;
21551 : :
21552 : 354653 : if (!operand_equal_p (t1, t2, 0))
21553 : : return false;
21554 : :
21555 : : return true;
21556 : : }
|